#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure do_dialogue;
procedure ghoulend;
procedure ghoulcbt;
procedure ghoul00a;
procedure ghoul00b;
procedure ghoul01;
procedure ghoul02;
procedure ghoul03;
procedure ghoul03a;
procedure ghoul05;
procedure Critter_Action;
procedure timetomove;

variable Hostile;
variable init;
variable maxleash := 9;
variable noevent;
variable loopcount;
variable new_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 (not(init)) then begin
		if (local_var(7) == 0) then begin
			set_local_var(7, tile_num(self_obj));
			set_local_var(5, 0);
		end
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 30);
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_GHOUL_COWARD);
		init := 1;
	end
	else begin
		if (script_action == talk_proc) then begin
			if (global_var(GVAR_ENEMY_NECROPOLIS)) then begin
				attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
			end
			else begin
				call do_dialogue;
			end
		end
		else begin
			if (script_action == look_at_proc) then begin
				script_overrides;
				display_msg(message_str(SCRIPT_GHDORGRD, 100));
			end
			else begin
				if (script_action == pickup_proc) then begin
					Hostile := 1;
				end
				else begin
					if (script_action == critter_proc) then begin
						if (obj_can_see_obj(self_obj, dude_obj) and global_var(GVAR_ENEMY_NECROPOLIS)) then begin
							Hostile := 1;
						end
						call Critter_Action;
					end
					else begin
						if (script_action == destroy_proc) then begin
							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

procedure do_dialogue
begin
	start_gdialog(SCRIPT_GHDORGRD, self_obj, 4, -1, -1);
	gsay_start;
	if (local_var(4)) then begin
		call ghoul02;
	end
	if ((game_time_hour >= 1900) or (game_time_hour < 600)) then begin
		if (local_var(1) >= 3) then begin
			call ghoul00b;
		end
		else begin
			set_local_var(4, 1);
			call ghoul03;
		end
	end
	else begin
		if (local_var(1) >= 3) then begin
			call ghoul01;
		end
		else begin
			set_local_var(4, 1);
			call ghoul03;
		end
	end
	gsay_end;
	end_dialogue;
end

procedure ghoulend
begin
end

procedure ghoulcbt
begin
	Hostile := 1;
end

procedure ghoul00a
begin
	gsay_reply(SCRIPT_GHDORGRD, 111);
	giq_option(4, SCRIPT_GHDORGRD, 102, ghoulend, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_GHDORGRD, 103, ghoulend, NEUTRAL_REACTION);
end

procedure ghoul00b
begin
	gsay_message(SCRIPT_GHDORGRD, 104, NEUTRAL_REACTION);
	call ghoulend;
end

procedure ghoul01
begin
	gsay_reply(SCRIPT_GHDORGRD, 105);
	giq_option(4, SCRIPT_GHDORGRD, 106, ghoulend, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_GHDORGRD, 107, ghoulend, NEUTRAL_REACTION);
end

procedure ghoul02
begin
	gsay_reply(SCRIPT_GHDORGRD, 108);
	giq_option(4, SCRIPT_GHDORGRD, 109, ghoulend, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_GHDORGRD, 110, ghoulend, NEUTRAL_REACTION);
end

procedure ghoul03
begin
	gsay_reply(SCRIPT_GHDORGRD, 101);
	giq_option(5, SCRIPT_GHDORGRD, 112, ghoul03a, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_GHDORGRD, 113, ghoulend, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_GHDORGRD, 114, ghoulend, NEUTRAL_REACTION);
end

procedure ghoul03a
begin
	call ghoul02;
end

procedure ghoul05
begin
	gsay_message(SCRIPT_GHDORGRD, 116, NEUTRAL_REACTION);
end

procedure Critter_Action
begin
	if (Hostile > 0) then begin
		Hostile := 0;
		attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
	end
	else begin
		if (local_var(5)) then begin
			loopcount := loopcount + 1;
			if (loopcount > 40) then begin
				loopcount := 0;
				if (noevent == 0) then begin
					noevent := 1;
					add_timer_event(self_obj, game_ticks(random(5, 13)), 0);
				end
			end
		end
	end
end

procedure timetomove
begin
	noevent := 0;
	set_local_var(6, tile_num(self_obj));
	new_tile := tile_num_in_direction(local_var(6), random(0, 5), 2);
	if (tile_distance(local_var(7), new_tile) < maxleash) then begin
		animate_move_obj_to_tile(self_obj, new_tile, ANIMATE_WALK);
	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