#include <define.h>

//#define NAME                    SCRIPT_

#include <command.h>
//#include <ModReact.h>



procedure start;
#include <push.h>
procedure critter_p_proc;
procedure damage_p_proc;
procedure destroy_p_proc;
procedure look_at_p_proc;
procedure map_enter_p_proc;
procedure pickup_p_proc;
procedure talk_p_proc;
procedure Sherry01;
procedure Sherry02;
procedure Sherry03;
procedure Sherry04;
procedure Sherry05;
procedure Sherry06;
procedure Sherry07;
procedure Sherry08;
procedure Sherry09;
procedure Sherry10;
procedure Sherry11;
procedure Sherry12;
procedure Sherry13;
procedure Sherry14;
procedure Sherry15;
procedure Sherry16;
procedure Sherry17;
procedure Sherry18;
procedure Sherry18a;
procedure Sherry19;
procedure Sherry20;
procedure Sherry21;
procedure Sherry22;
procedure Sherry23;
procedure Sherry24;
procedure Sherry25;
procedure Sherry26;
procedure Sherry27;
procedure Sherry27a;
procedure Sherry28;
procedure Sherry29;
procedure SherryCombat;
procedure SherryCook;
procedure SherryEnd;

variable hostile1;
variable initialized1;
variable feed_dude;

procedure sleeping;

variable night_person;
variable wake_time;
variable sleep_time;
variable home_tile;
variable sleep_tile;

procedure Get_Reaction;
procedure React_To_Level;
procedure Level_To_React;
procedure Up_React;
procedure Down_React;
procedure Bottom_React;
procedure Top_React;
procedure Big_Up_React;
procedure Big_Down_React;
procedure Up_React_Level;
procedure Down_React_Level;
procedure Goodbyes;

variable exit_line;


procedure start
begin
	if (script_action == critter_proc) then begin
		call critter_p_proc;
	end
	else begin
		if (script_action == destroy_proc) then begin
			call destroy_p_proc;
		end
		else begin
			if (script_action == look_at_proc) then begin
				call look_at_p_proc;
			end
			else begin
				if (script_action == map_enter_proc) then begin
					call map_enter_p_proc;
				end
				else begin
					if (script_action == pickup_proc) then begin
						call pickup_p_proc;
					end
					else begin
						if (script_action == talk_proc) then begin
							call talk_p_proc;
						end
					end
				end
			end
		end
	end
end

procedure critter_p_proc
begin
	if (hostile1) then begin
		hostile1 := 0;
		attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
	end
	else begin
		if (global_var(GVAR_SHERRY_TURNS) == 1) then begin
			if (((game_time / (10 * 60 * 60 * 24)) - local_var(6)) > 1) then begin
				critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 26);
				set_global_var(GVAR_SHERRY_TURNS, 2);
				set_local_var(6, 0);
				home_tile := 18125;
				sleep_tile := 17924;
			end
		end
		if ((game_time_hour >= 700) and (game_time_hour <= 1930)) then begin
			if (tile_num(self_obj) != home_tile) then begin
				animate_move_obj_to_tile(self_obj, home_tile, ANIMATE_WALK);
			end
		end
		call sleeping;
	end
	if (global_var(GVAR_ENEMY_JUNKTOWN) == 1) then begin
		if (obj_can_see_obj(self_obj, dude_obj)) then begin
			attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
		end
	end
end

procedure damage_p_proc
begin
	if (source_obj == dude_obj) then begin
		set_global_var(GVAR_ENEMY_JUNKTOWN, 1);
	end
end

procedure destroy_p_proc
begin
	if (source_obj == dude_obj) then begin
		set_global_var(GVAR_ENEMY_JUNKTOWN, 1);
		if (source_obj == dude_obj) then begin
			if (((global_var(GVAR_BAD_MONSTER) + global_var(GVAR_GOOD_MONSTER)) >= 25) and ((global_var(GVAR_GOOD_MONSTER) > (2 * global_var(GVAR_BAD_MONSTER))) or (global_var(GVAR_BERSERKER_REPUTATION) == 1))) then begin
				set_global_var(GVAR_BERSERKER_REPUTATION, 1);
				set_global_var(GVAR_CHAMPION_REPUTATION, 0);
			end
			if (((global_var(GVAR_BAD_MONSTER) + global_var(GVAR_GOOD_MONSTER)) >= 25) and ((global_var(GVAR_BAD_MONSTER) > (3 * global_var(GVAR_GOOD_MONSTER))) or (global_var(GVAR_CHAMPION_REPUTATION) == 1))) then begin
				set_global_var(GVAR_CHAMPION_REPUTATION, 1);
				set_global_var(GVAR_BERSERKER_REPUTATION, 0);
			end
			set_global_var(GVAR_GOOD_MONSTER, global_var(GVAR_GOOD_MONSTER) + 1);
			if ((global_var(GVAR_GOOD_MONSTER) % 2) == 0) then begin
				set_global_var(GVAR_PLAYER_REPUTATION, global_var(GVAR_PLAYER_REPUTATION) - 1);
			end
		end
	end
end

procedure look_at_p_proc
begin
	script_overrides;
	if (local_var(4)) then begin
		display_msg(message_str(SCRIPT_SHERRY, 100));
	end
	else begin
		display_msg(message_str(SCRIPT_SHERRY, 101));
	end
end

procedure map_enter_p_proc
begin
	sleep_time := 2000;
	wake_time := 630;
	if (global_var(GVAR_SHERRY_TURNS) == 0) then begin
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 14);
		home_tile := 15513;
		sleep_tile := 13494;
	end
	else begin
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 26);
		home_tile := 18125;
		sleep_tile := 17924;
	end
end

procedure pickup_p_proc
begin
	float_msg(self_obj, message_str(SCRIPT_SHERRY, 102), FLOAT_MSG_NORMAL);
	hostile1 := 1;
end

procedure talk_p_proc
begin
	if (local_var(5) == 1) then begin
		float_msg(self_obj, message_str(SCRIPT_GENVAULT, 166), FLOAT_MSG_NORMAL);
	end
	else begin
		call Get_Reaction;
		start_gdialog(SCRIPT_SHERRY, self_obj, 4, -1, -1);
		gsay_start;
		if (global_var(GVAR_SHERRY_TURNS) == 2) then begin
			call Sherry22;
		end
		else begin
			if (local_var(4) == 1) then begin
				call Sherry15;
			end
			else begin
				call Sherry01;
			end
		end
		gsay_end;
		end_dialogue;
	end
	if (feed_dude) then begin
		call SherryCook;
	end
end

procedure Sherry01
begin
	set_local_var(4, 1);
	gsay_reply(SCRIPT_SHERRY, 103);
	giq_option(4, SCRIPT_SHERRY, message_str(SCRIPT_SHERRY, 104) + proto_data(obj_pid(dude_obj), 1) + message_str(SCRIPT_SHERRY, 105), Sherry02, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_SHERRY, 106, Sherry03, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_SHERRY, 107, Sherry04, NEUTRAL_REACTION);
end

procedure Sherry02
begin
	gsay_reply(SCRIPT_SHERRY, 108);
	giq_option(4, SCRIPT_SHERRY, 109, SherryCombat, BAD_REACTION);
	giq_option(4, SCRIPT_SHERRY, 110, SherryEnd, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_SHERRY, 111, Sherry05, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_SHERRY, 112, Sherry07, NEUTRAL_REACTION);
end

procedure Sherry03
begin
	call Down_React;
	gsay_reply(SCRIPT_SHERRY, 113);
	giq_option(4, SCRIPT_SHERRY, 114, SherryEnd, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_SHERRY, 115, SherryCombat, NEUTRAL_REACTION);
end

procedure Sherry04
begin
	gsay_message(SCRIPT_SHERRY, 116, NEUTRAL_REACTION);
end

procedure Sherry05
begin
	gsay_reply(SCRIPT_SHERRY, 117);
	giq_option(4, SCRIPT_SHERRY, 118, Sherry11, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_SHERRY, 119, SherryEnd, NEUTRAL_REACTION);
end

procedure Sherry06
begin
	gsay_reply(SCRIPT_SHERRY, 120);
	giq_option(4, SCRIPT_SHERRY, 121, Sherry07, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_SHERRY, 122, Sherry08, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_SHERRY, 123, SherryEnd, NEUTRAL_REACTION);
end

procedure Sherry07
begin
	gsay_message(SCRIPT_SHERRY, 124, BAD_REACTION);
end

procedure Sherry08
begin
	gsay_reply(SCRIPT_SHERRY, 125);
	call Sherry13;
end

procedure Sherry09
begin
	set_local_var(7, 1);
	gsay_reply(SCRIPT_SHERRY, 126);
	giq_option(4, SCRIPT_SHERRY, 127, Sherry10, NEUTRAL_REACTION);
	call Sherry13;
end

procedure Sherry10
begin
	gsay_reply(SCRIPT_SHERRY, 128);
	call Sherry13;
end

procedure Sherry11
begin
	gsay_reply(SCRIPT_SHERRY, 129);
	call Sherry13;
end

procedure Sherry12
begin
	set_local_var(8, 1);
	gsay_reply(SCRIPT_SHERRY, 130);
	giq_option(4, SCRIPT_SHERRY, 131, Sherry26, NEUTRAL_REACTION);
	call Sherry13;
end

procedure Sherry13
begin
	if (not(local_var(7))) then begin
		giq_option(5, SCRIPT_SHERRY, 133, Sherry09, NEUTRAL_REACTION);
	end
	if (not(local_var(8))) then begin
		giq_option(4, SCRIPT_SHERRY, 132, Sherry12, NEUTRAL_REACTION);
	end
	if (not(local_var(9))) then begin
		giq_option(6, SCRIPT_SHERRY, 134, Sherry14, NEUTRAL_REACTION);
	end
	call Goodbyes;
	giq_option(4, SCRIPT_SHERRY, exit_line, SherryEnd, NEUTRAL_REACTION);
end

procedure Sherry14
begin
	set_local_var(9, 1);
	gsay_reply(SCRIPT_SHERRY, 135);
	call Sherry13;
end

procedure Sherry15
begin
	gsay_reply(SCRIPT_SHERRY, 136);
	giq_option(4, SCRIPT_SHERRY, 137, Sherry16, NEUTRAL_REACTION);
	if (local_var(6) == 0) then begin
		giq_option(5, SCRIPT_SHERRY, 138, Sherry17, NEUTRAL_REACTION);
	end
	call Goodbyes;
	giq_option(4, SCRIPT_SHERRY, exit_line, SherryEnd, NEUTRAL_REACTION);
end

procedure Sherry16
begin
	gsay_message(SCRIPT_SHERRY, 139, BAD_REACTION);
	call SherryCombat;
end

procedure Sherry17
begin
	gsay_reply(SCRIPT_SHERRY, 140);
	giq_option(4, SCRIPT_SHERRY, 141, SherryEnd, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_SHERRY, 142, Sherry18, NEUTRAL_REACTION);
end

procedure Sherry18
begin
	gsay_reply(SCRIPT_SHERRY, 143);
	giq_option(4, SCRIPT_SHERRY, 144, SherryEnd, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_SHERRY, 145, Sherry18a, NEUTRAL_REACTION);
end

procedure Sherry18a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, 0))) then begin
		call Sherry19;
	end
	else begin
		call Sherry20;
	end
end

procedure Sherry19
begin
	gsay_reply(SCRIPT_SHERRY, 146);
	giq_option(6, SCRIPT_SHERRY, 147, Sherry21, NEUTRAL_REACTION);
end

procedure Sherry20
begin
	call Down_React;
	gsay_message(SCRIPT_SHERRY, 148, BAD_REACTION);
end

procedure Sherry21
begin
	set_local_var(6, game_time / (10 * 60 * 60 * 24));
	set_global_var(GVAR_SHERRY_TURNS, 1);
	gsay_message(SCRIPT_SHERRY, 149, NEUTRAL_REACTION);
end

procedure Sherry22
begin
	gsay_reply(SCRIPT_SHERRY, 150);
	giq_option(4, SCRIPT_SHERRY, 151, SherryEnd, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_SHERRY, 152, Sherry23, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_SHERRY, 153, Sherry24, NEUTRAL_REACTION);
	if ((local_var(10) == 0) and global_var(GVAR_BUST_SKULZ)) then begin
		giq_option(4, SCRIPT_SHERRY, 166, Sherry27, NEUTRAL_REACTION);
	end
end

procedure Sherry23
begin
	gsay_reply(SCRIPT_SHERRY, 154);
	giq_option(4, SCRIPT_SHERRY, 155, Sherry25, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_SHERRY, 156, SherryEnd, NEUTRAL_REACTION);
end

procedure Sherry24
begin
	gsay_message(SCRIPT_SHERRY, 157, NEUTRAL_REACTION);
end

procedure Sherry25
begin
	feed_dude := 1;
end

procedure Sherry26
begin
	gsay_reply(SCRIPT_SHERRY, 159);
	call Sherry13;
end

procedure Sherry27
begin
	set_local_var(10, 1);
	gsay_reply(SCRIPT_SHERRY, 160);
	giq_option(4, SCRIPT_SHERRY, 161, Sherry27a, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_SHERRY, 162, SherryEnd, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_SHERRY, 163, Sherry28, NEUTRAL_REACTION);
end

procedure Sherry27a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, 0))) then begin
		call Sherry28;
	end
	else begin
		call Sherry29;
	end
end

procedure Sherry28
begin
	gsay_message(SCRIPT_SHERRY, 164, NEUTRAL_REACTION);
	set_global_var(GVAR_GENERIC_FILLER_20, 1);
end

procedure Sherry29
begin
	gsay_message(SCRIPT_SHERRY, 165, NEUTRAL_REACTION);
end

procedure SherryCombat
begin
	hostile1 := 1;
end

procedure SherryCook
begin
	use_obj(dude_obj);
	float_msg(self_obj, message_str(SCRIPT_SHERRY, 158), FLOAT_MSG_NORMAL);
	critter_heal(dude_obj, 1);
	feed_dude := 0;
end

procedure SherryEnd
begin
end

procedure sleeping
begin
	if (local_var(5) == 1) then begin
		if (not(night_person) and (game_time_hour >= wake_time) and (game_time_hour < sleep_time) or (night_person and ((game_time_hour >= wake_time) or (game_time_hour < sleep_time)))) then begin
			if (((game_time_hour - wake_time) < 10) and ((game_time_hour - wake_time) > 0)) then begin
				if (tile_num(self_obj) != home_tile) then begin
					animate_move_obj_to_tile(self_obj, home_tile, ANIMATE_WALK);
				end
				else begin
					set_local_var(5, 0);
				end
			end
			else begin
				move_to(self_obj, home_tile, elevation(self_obj));
				if (tile_num(self_obj) == home_tile) then begin
					set_local_var(5, 0);
				end
			end
		end
	end
	else begin
		if (night_person and (game_time_hour >= sleep_time) and (game_time_hour < wake_time) or (not(night_person) and ((game_time_hour >= sleep_time) or (game_time_hour < wake_time)))) then begin
			if (((game_time_hour - sleep_time) < 10) and ((game_time_hour - sleep_time) > 0)) then begin
				if (tile_num(self_obj) != sleep_tile) then begin
					animate_move_obj_to_tile(self_obj, self_obj, ANIMATE_WALK);
				end
				else begin
					set_local_var(5, 1);
				end
			end
			else begin
				if (tile_num(self_obj) != sleep_tile) then begin
					move_to(self_obj, sleep_tile, elevation(self_obj));
				end
				else begin
					set_local_var(5, 1);
				end
			end
		end
	end
end

procedure Get_Reaction
begin
	if (local_var(2) == 0) then begin
		set_local_var(0, 50);
		set_local_var(1, 2);
		set_local_var(2, 1);
		set_local_var(0, local_var(0) + (5 * get_critter_stat(dude_obj, 3)) - 25);
		set_local_var(0, local_var(0) + (10 * has_trait(0, dude_obj, 10)));
		if (has_trait(0, dude_obj, 39)) then begin
			if (global_var(GVAR_PLAYER_REPUTATION) > 0) then begin
				set_local_var(0, local_var(0) + global_var(GVAR_PLAYER_REPUTATION));
			end
			else begin
				set_local_var(0, local_var(0) - global_var(GVAR_PLAYER_REPUTATION));
			end
		end
		else begin
			if (local_var(3) == 1) then begin
				set_local_var(0, local_var(0) - global_var(GVAR_PLAYER_REPUTATION));
			end
			else begin
				set_local_var(0, local_var(0) + global_var(GVAR_PLAYER_REPUTATION));
			end
		end
		if (global_var(GVAR_CHILDKILLER_REPUTATION) > 2) then begin
			set_local_var(0, local_var(0) - 30);
		end
		if (((global_var(GVAR_BAD_MONSTER) + global_var(GVAR_GOOD_MONSTER)) >= 25) and ((global_var(GVAR_BAD_MONSTER) > (3 * global_var(GVAR_GOOD_MONSTER))) or (global_var(GVAR_CHAMPION_REPUTATION) == 1))) then begin
			set_local_var(0, local_var(0) + 20);
		end
		if (((global_var(GVAR_BAD_MONSTER) + global_var(GVAR_GOOD_MONSTER)) >= 25) and ((global_var(GVAR_GOOD_MONSTER) > (2 * global_var(GVAR_BAD_MONSTER))) or (global_var(GVAR_BERSERKER_REPUTATION) == 1))) then begin
			set_local_var(0, local_var(0) - 20);
		end
		call React_To_Level;
	end
end

procedure React_To_Level
begin
	if (local_var(0) <= 25) then begin
		set_local_var(1, 1);
	end
	else begin
		if (local_var(0) <= 75) then begin
			set_local_var(1, 2);
		end
		else begin
			set_local_var(1, 3);
		end
	end
end

procedure Level_To_React
begin
	if (local_var(1) == 1) then begin
		set_local_var(0, random(1, 25));
	end
	else begin
		if (local_var(1) == 2) then begin
			set_local_var(0, random(26, 75));
		end
		else begin
			set_local_var(0, random(76, 100));
		end
	end
end

procedure Up_React
begin
	set_local_var(0, local_var(0) + 10);
	call React_To_Level;
end

procedure Down_React
begin
	set_local_var(0, local_var(0) - 10);
	call React_To_Level;
end

procedure Bottom_React
begin
	set_local_var(1, 1);
	set_local_var(0, 1);
end

procedure Top_React
begin
	set_local_var(0, 100);
	set_local_var(1, 3);
end

procedure Big_Up_React
begin
	set_local_var(0, local_var(0) + 25);
	call React_To_Level;
end

procedure Big_Down_React
begin
	set_local_var(0, local_var(0) - 25);
	call React_To_Level;
end

procedure Up_React_Level
begin
	set_local_var(1, local_var(1) + 1);
	if (local_var(1) > 3) then begin
		set_local_var(1, 3);
	end
	call Level_To_React;
end

procedure Down_React_Level
begin
	set_local_var(1, local_var(1) - 1);
	if (local_var(1) < 1) then begin
		set_local_var(1, 1);
	end
	call Level_To_React;
end

procedure Goodbyes
begin
	exit_line := message_str(SCRIPT_MODREACT, random(100, 105));
end