#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure do_dialogue;
procedure garretend;
procedure garretcbt;
procedure goodstuff;
procedure neutstuff;
procedure badstuff;
procedure reward;
procedure done;
procedure garret00a;
procedure garret00b;
procedure garret00c;
procedure garret00ca;
procedure garret01a;
procedure garret01c;
procedure garret02a;
procedure garret02c;
procedure garret03c;
procedure garret04;
procedure give_flare;
procedure give_cola;

import variable Fridge_ptr;
import variable Garret_ptr;

variable Shotgun_ptr;
variable Shells_ptr;
variable Cola_ptr;
variable Flare_ptr;
variable Hostile;
variable init;
variable maxleash := 9;
variable noevent;
variable loopcount;
variable new_tile;
variable gavelist;
variable moving;
variable stuff;
variable indlog;
variable robbed;
variable flare_count;
variable cola_count;
variable cash;
variable THRONE;
variable target_hex := 20113;
variable my_hex;

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 (not(init)) then begin
		if (local_var(7) == 0) then begin
			set_local_var(7, tile_num(self_obj));
		end
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 30);
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_GHOUL_NASTY);
		Garret_ptr := self_obj;
		init := 1;
	end
	else begin
		if (script_action == damage_proc) then begin
			set_global_var(GVAR_ENEMY_NECROPOLIS, 1);
		end
		else begin
			if (script_action == talk_proc) then begin
				call do_dialogue;
			end
			else begin
				if (script_action == look_at_proc) then begin
					script_overrides;
					display_msg(message_str(SCRIPT_GARRET, 100));
				end
				else begin
					if (script_action == pickup_proc) then begin
						Hostile := 1;
					end
					else begin
						if (script_action == critter_proc) then begin
							if (global_var(GVAR_ENEMY_NECROPOLIS) and obj_can_see_obj(self_obj, dude_obj)) then begin
								attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
							end
							else begin
								my_hex := tile_num(self_obj);
								if (Hostile > 0) then begin
									set_global_var(GVAR_ENEMY_NECROPOLIS, 1);
									attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
								end
								if (global_var(GVAR_SIGNAL_REWARD) bwand 1) then begin
									if (local_var(5) == 0) then begin
										moving := 1;
										float_msg(self_obj, message_str(SCRIPT_GARRET, 114), FLOAT_MSG_NORMAL);
										set_local_var(5, 1);
									end
									else begin
										if (local_var(5) == 1) then begin
											moving := 1;
											if (my_hex != target_hex) then begin
												animate_move_obj_to_tile(self_obj, target_hex, ANIMATE_WALK);
											end
											else begin
												set_local_var(5, 2);
												use_obj(Fridge_ptr);
											end
										end
										else begin
											if (local_var(5) == 2) then begin
												moving := 1;
												if (tile_distance_objs(self_obj, dude_obj) < 5) then begin
													call reward;
													set_local_var(5, 3);
													use_obj(Fridge_ptr);
												end
											end
											else begin
												if (local_var(5) == 3) then begin
													moving := 1;
													target_hex := local_var(7);
													if (my_hex != target_hex) then begin
														animate_move_obj_to_tile(self_obj, target_hex, ANIMATE_WALK);
													end
													else begin
														call done;
													end
												end
											end
										end
									end
								end
							end
						end
						else begin
							if (script_action == destroy_proc) then begin
								set_global_var(GVAR_ENEMY_NECROPOLIS, 1);
								set_global_var(GVAR_GARRET_STATUS, 3);
								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_BAD_MONSTER, global_var(GVAR_BAD_MONSTER) + 1);
									if ((global_var(GVAR_BAD_MONSTER) % 6) == 0) then begin
										set_global_var(GVAR_PLAYER_REPUTATION, global_var(GVAR_PLAYER_REPUTATION) + 1);
									end
								end
							end
						end
					end
				end
			end
		end
	end
end

procedure do_dialogue
begin
	if ((local_var(8) == 0) and (global_var(GVAR_SIGNAL_REWARD) bwand 2)) then begin
		set_local_var(8, 1);
		float_msg(self_obj, message_str(SCRIPT_GARRET, 201), FLOAT_MSG_NORMAL);
		display_msg(message_str(SCRIPT_GARRET, 202));
		stuff := create_object_sid(38, 0, 0, -1);
		add_obj_to_inven(dude_obj, stuff);
		stuff := create_object_sid(39, 0, 0, -1);
		add_obj_to_inven(dude_obj, stuff);
		stuff := create_object_sid(87, 0, 0, -1);
		add_obj_to_inven(dude_obj, stuff);
		stuff := create_object_sid(53, 0, 0, -1);
		add_obj_to_inven(dude_obj, stuff);
		stuff := create_object_sid(40, 0, 0, -1);
		add_obj_to_inven(dude_obj, stuff);
		stuff := create_object_sid(40, 0, 0, -1);
		add_obj_to_inven(dude_obj, stuff);
		stuff := create_object_sid(25, 0, 0, -1);
		add_obj_to_inven(dude_obj, stuff);
		stuff := create_object_sid(25, 0, 0, -1);
		add_obj_to_inven(dude_obj, stuff);
		stuff := create_object_sid(26, 0, 0, -1);
		add_obj_to_inven(dude_obj, stuff);
		stuff := create_object_sid(26, 0, 0, -1);
		add_obj_to_inven(dude_obj, stuff);
		stuff := create_object_sid(27, 0, 0, -1);
		add_obj_to_inven(dude_obj, stuff);
		stuff := create_object_sid(27, 0, 0, -1);
		add_obj_to_inven(dude_obj, stuff);
		stuff := create_object_sid(35, 0, 0, -1);
		add_obj_to_inven(dude_obj, stuff);
		stuff := create_object_sid(35, 0, 0, -1);
		add_obj_to_inven(dude_obj, stuff);
		stuff := create_object_sid(35, 0, 0, -1);
		add_obj_to_inven(dude_obj, stuff);
		stuff := create_object_sid(35, 0, 0, -1);
		add_obj_to_inven(dude_obj, stuff);
		stuff := create_object_sid(36, 0, 0, -1);
		add_obj_to_inven(dude_obj, stuff);
		stuff := create_object_sid(36, 0, 0, -1);
		add_obj_to_inven(dude_obj, stuff);
		stuff := create_object_sid(36, 0, 0, -1);
		add_obj_to_inven(dude_obj, stuff);
		stuff := create_object_sid(36, 0, 0, -1);
		add_obj_to_inven(dude_obj, stuff);
	end
	else begin
		if (moving) then begin
			display_msg(message_str(SCRIPT_GARRET, 200));
		end
		else begin
			if ((game_time_hour >= 700) and (game_time_hour < 1800)) then begin
				call Get_Reaction;
				if (local_var(4)) then begin
					call garret02a;
				end
				else begin
					set_local_var(4, 1);
					if (local_var(1) >= 2) then begin
						call garret04;
					end
					else begin
						call garret01a;
					end
				end
			end
			else begin
				call garret00b;
			end
		end
	end
end

procedure garretend
begin
end

procedure garretcbt
begin
	Hostile := 1;
end

procedure goodstuff
begin
	cash := cash + 100;
	add_mult_objs_to_inven(dude_obj, create_object_sid(41, 0, 0, -1), 100);
	call neutstuff;
	call badstuff;
end

procedure neutstuff
begin
	cash := cash + 50;
	add_mult_objs_to_inven(dude_obj, create_object_sid(41, 0, 0, -1), 50);
	if (obj_is_carrying_obj_pid(Fridge_ptr, 94)) then begin
		display_msg(message_str(SCRIPT_GARRET, 204));
		Shotgun_ptr := obj_carrying_pid_obj(Fridge_ptr, 94);
		rm_obj_from_inven(Fridge_ptr, Shotgun_ptr);
		add_obj_to_inven(dude_obj, Shotgun_ptr);
	end
	else begin
		robbed := 1;
	end
	if (obj_is_carrying_obj_pid(Fridge_ptr, 95)) then begin
		display_msg(message_str(SCRIPT_GARRET, 205));
		Shells_ptr := obj_carrying_pid_obj(Fridge_ptr, 95);
		rm_obj_from_inven(Fridge_ptr, Shells_ptr);
		add_obj_to_inven(dude_obj, Shells_ptr);
	end
	else begin
		robbed := 1;
	end
	if (obj_is_carrying_obj_pid(Fridge_ptr, 95)) then begin
		Shells_ptr := obj_carrying_pid_obj(Fridge_ptr, 95);
		rm_obj_from_inven(Fridge_ptr, Shells_ptr);
		add_obj_to_inven(dude_obj, Shells_ptr);
	end
	else begin
		robbed := 1;
	end
end

procedure badstuff
begin
	cash := cash + 50;
	add_mult_objs_to_inven(dude_obj, create_object_sid(41, 0, 0, -1), 50);
	flare_count := 0;
	call give_flare;
	call give_flare;
	call give_flare;
	call give_flare;
	cola_count := 0;
	call give_cola;
	call give_cola;
	call give_cola;
	call give_cola;
end

procedure reward
begin
	cash := 0;
	robbed := 0;
	display_msg(message_str(SCRIPT_GARRET, 203));
	if (local_var(1) >= 3) then begin
		call goodstuff;
	end
	else begin
		if (local_var(1) >= 2) then begin
			call neutstuff;
		end
		else begin
			call badstuff;
		end
	end
	if (flare_count) then begin
		if (flare_count > 1) then begin
			display_msg(message_str(SCRIPT_GARRET, 206));
		end
		else begin
			display_msg(message_str(SCRIPT_GARRET, 207));
		end
	end
	if (cola_count) then begin
		if (cola_count > 1) then begin
			display_msg(message_str(SCRIPT_GARRET, 208));
		end
		else begin
			display_msg(message_str(SCRIPT_GARRET, 209));
		end
	end
	display_msg("$" + cash);
	if (robbed) then begin
		float_msg(self_obj, message_str(SCRIPT_GARRET, 121), FLOAT_MSG_NORMAL);
	end
	else begin
		float_msg(self_obj, message_str(SCRIPT_GARRET, 201), FLOAT_MSG_NORMAL);
	end
end

procedure done
begin
	moving := 0;
	set_local_var(5, 4);
end

procedure garret00a
begin
	gsay_reply(SCRIPT_GARRET, 101);
	giq_option(4, SCRIPT_GARRET, 102, garretend, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_GARRET, 103, garret01a, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_GARRET, 104, garret02a, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_GARRET, 112, garretend, NEUTRAL_REACTION);
end

procedure garret00b
begin
	float_msg(self_obj, message_str(SCRIPT_GARRET, 105), FLOAT_MSG_NORMAL);
	call garretend;
end

procedure garret00c
begin
	gsay_reply(SCRIPT_GARRET, 109);
	giq_option(3, SCRIPT_GARRET, 110, garret01c, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_GARRET, 111, garret00ca, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_GARRET, 112, garret02c, NEUTRAL_REACTION);
end

procedure garret00ca
begin
	if (is_success(roll_vs_skill(dude_obj, 14, 0))) then begin
		call garret03c;
	end
	else begin
		call garret02c;
	end
end

procedure garret01a
begin
	if (indlog) then begin
		gsay_message(SCRIPT_GARRET, 113, NEUTRAL_REACTION);
	end
	else begin
		float_msg(self_obj, message_str(SCRIPT_GARRET, 113), FLOAT_MSG_NORMAL);
	end
	call garretend;
end

procedure garret01c
begin
	gsay_message(SCRIPT_GARRET, 115, NEUTRAL_REACTION);
	call Down_React;
	call garretend;
end

procedure garret02a
begin
	call Down_React;
	if (indlog) then begin
		gsay_message(SCRIPT_GARRET, 116, NEUTRAL_REACTION);
	end
	else begin
		float_msg(self_obj, message_str(SCRIPT_GARRET, 116), FLOAT_MSG_NORMAL);
	end
	call garretend;
end

procedure garret02c
begin
	gsay_message(SCRIPT_GARRET, 117, NEUTRAL_REACTION);
	call garretend;
end

procedure garret03c
begin
	gsay_message(SCRIPT_GARRET, 118, NEUTRAL_REACTION);
	display_msg(message_str(SCRIPT_GARRET, 119));
	gsay_message(SCRIPT_GARRET, 120, NEUTRAL_REACTION);
	call garretend;
end

procedure garret04
begin
	start_gdialog(SCRIPT_GARRET, self_obj, 4, -1, -1);
	gsay_start;
	indlog := 1;
	call garret00a;
	indlog := 0;
	gsay_end;
	end_dialogue;
end

procedure give_flare
begin
	if (obj_is_carrying_obj_pid(Fridge_ptr, 79)) then begin
		flare_count := flare_count + 1;
		Flare_ptr := obj_carrying_pid_obj(Fridge_ptr, 79);
		rm_obj_from_inven(Fridge_ptr, Flare_ptr);
		add_obj_to_inven(dude_obj, Flare_ptr);
	end
	else begin
		robbed := 1;
	end
end

procedure give_cola
begin
	if (obj_is_carrying_obj_pid(Fridge_ptr, 106)) then begin
		cola_count := cola_count + 1;
		Cola_ptr := obj_carrying_pid_obj(Fridge_ptr, 106);
		rm_obj_from_inven(Fridge_ptr, Cola_ptr);
		add_obj_to_inven(dude_obj, Cola_ptr);
	end
	else begin
		robbed := 1;
	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