#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure combat;
procedure critter_p_proc;
procedure pickup_p_proc;
procedure talk_p_proc;
procedure destroy_p_proc;
procedure look_at_p_proc;
procedure damage_p_proc;
procedure ChildGuard00;
procedure ChildGuard01;
procedure ChildGuard02;
procedure ChildGuard02a;
procedure ChildGuard03;
procedure ChildGuard04;
procedure ChildGuard05;
procedure ChildGuard06;
procedure ChildGuard07;
procedure ChildGuard08;
procedure ChildGuard09;
procedure ChildGuard10;
procedure ChildGuard11;
procedure ChildGuard12;
procedure ChildGuard13;
procedure ChildGuard14;
procedure ChildGuard15;
procedure ChildGuard16;
procedure ChildGuard17;
procedure ChildGuard18;
procedure ChildGuard19;
procedure ChildGuard20;
procedure ChildGuard21;
procedure ChildGuard22;
procedure ChildGuard23;
procedure ChildGuard24;
procedure ChildGuard25;
procedure ChildGuard26;
procedure ChildGuard27;
procedure ChildGuard28;
procedure ChildGuard29;
procedure ChildGuard30;
procedure ChildGuard31;
procedure ChildGuard32;
procedure ChildGuard33;
procedure ChildGuard34;
procedure ChildGuard35;
procedure ChildGuard36;
procedure ChildGuard37;

variable hostile1;
variable Only_Once := 1;

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 (Only_Once) then begin
		Only_Once := 0;
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 72);
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_COC_HUB_BRAINWASHED);
	end
	if (script_action == look_at_proc) then begin
		call look_at_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
			else 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
				end
			end
		end
	end
end

procedure combat
begin
	hostile1 := 1;
end

procedure critter_p_proc
begin
	if (hostile1) then begin
		hostile1 := 0;
		attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
	end
	if ((map_var(6) == 1) and (obj_can_see_obj(self_obj, dude_obj) == 1)) then begin
		call combat;
	end
	if (map_var(4) == 1) then begin
		dialogue_system_enter;
	end
end

procedure pickup_p_proc
begin
	if (source_obj == dude_obj) then begin
		hostile1 := 1;
	end
end

procedure talk_p_proc
begin
	call Get_Reaction;
	if (map_var(4) == 1) then begin
		set_map_var(4, 0);
		if (local_var(6) == 0) then begin
			start_gdialog(SCRIPT_HCHDGRD, self_obj, 4, -1, -1);
			gsay_start;
			call ChildGuard32;
			gsay_end;
			end_dialogue;
		end
		else begin
			call ChildGuard37;
		end
	end
	else begin
		if (global_var(GVAR_ENEMY_CATHEDRAL) == 1) then begin
			call ChildGuard00;
		end
		else begin
			if (map_var(0) == 1) then begin
				call ChildGuard36;
			end
			else begin
				if ((global_var(GVAR_CHILDKILLER_REPUTATION) == 1) or (global_var(GVAR_BERSERKER_REPUTATION) == 1)) then begin
					call ChildGuard01;
				end
				else begin
					if (local_var(5) == 0) then begin
						start_gdialog(SCRIPT_HCHDGRD, self_obj, 4, -1, -1);
						gsay_start;
						call ChildGuard02;
						gsay_end;
						end_dialogue;
					end
					else begin
						call ChildGuard30;
					end
				end
			end
		end
	end
end

procedure destroy_p_proc
begin
	set_map_var(5, 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

procedure look_at_p_proc
begin
	script_overrides;
	display_msg(message_str(SCRIPT_HCHDGRD, 100));
end

procedure damage_p_proc
begin
	variable LVar0 := 0;
	LVar0 := obj_pid(source_obj);
	if (party_member_obj(LVar0) != 0) then begin
		set_map_var(6, 1);
	end
end

procedure ChildGuard00
begin
	set_global_var(GVAR_ENEMY_CATHEDRAL, 1);
	float_msg(self_obj, message_str(SCRIPT_HCHDGRD, 101), FLOAT_MSG_RED);
	call combat;
end

procedure ChildGuard01
begin
	float_msg(self_obj, message_str(SCRIPT_HCHDGRD, 102), FLOAT_MSG_RED);
end

procedure ChildGuard02
begin
	set_local_var(5, 1);
	gsay_reply(SCRIPT_HCHDGRD, 103);
	giq_option(7, SCRIPT_HCHDGRD, 104, ChildGuard03, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HCHDGRD, 105, ChildGuard04, NEUTRAL_REACTION);
	if (global_var(GVAR_FIND_WATER_CHIP) != 2) then begin
		giq_option(4, SCRIPT_HCHDGRD, 106, ChildGuard05, GOOD_REACTION);
	end
	if (global_var(GVAR_DECKER_KNOWN) == 1) then begin
		giq_option(4, SCRIPT_HCHDGRD, 107, ChildGuard06, GOOD_REACTION);
	end
	giq_option(4, SCRIPT_HCHDGRD, 108, ChildGuard02a, GOOD_REACTION);
	giq_option(-3, SCRIPT_HCHDGRD, 109, ChildGuard09, NEUTRAL_REACTION);
end

procedure ChildGuard02a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, 0)) == 1) then begin
		call ChildGuard07;
	end
	else begin
		call ChildGuard08;
	end
end

procedure ChildGuard03
begin
	gsay_reply(SCRIPT_HCHDGRD, 110);
	giq_option(7, SCRIPT_HCHDGRD, 111, ChildGuard10, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HCHDGRD, 112, ChildGuard11, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HCHDGRD, 113, ChildGuard12, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HCHDGRD, 114, ChildGuard13, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HCHDGRD, 115, ChildGuard14, BAD_REACTION);
	giq_option(4, SCRIPT_HCHDGRD, 116, ChildGuard15, NEUTRAL_REACTION);
end

procedure ChildGuard04
begin
	gsay_reply(SCRIPT_HCHDGRD, 117);
	giq_option(7, SCRIPT_HCHDGRD, 118, ChildGuard16, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HCHDGRD, 119, ChildGuard17, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HCHDGRD, 120, ChildGuard12, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HCHDGRD, 121, ChildGuard18, NEUTRAL_REACTION);
end

procedure ChildGuard05
begin
	gsay_reply(SCRIPT_HCHDGRD, 122);
	giq_option(4, SCRIPT_HCHDGRD, 123, ChildGuard19, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HCHDGRD, 124, ChildGuard20, BAD_REACTION);
	giq_option(4, SCRIPT_HCHDGRD, 125, ChildGuard21, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HCHDGRD, 126, ChildGuard22, NEUTRAL_REACTION);
end

procedure ChildGuard06
begin
	gsay_reply(SCRIPT_HCHDGRD, 127);
	giq_option(4, SCRIPT_HCHDGRD, 128, ChildGuard23, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HCHDGRD, 129, ChildGuard24, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HCHDGRD, 130, ChildGuard25, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HCHDGRD, 131, ChildGuard21, NEUTRAL_REACTION);
end

procedure ChildGuard07
begin
	gsay_reply(SCRIPT_HCHDGRD, 132);
	giq_option(7, SCRIPT_HCHDGRD, 133, ChildGuard26, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HCHDGRD, 134, ChildGuard27, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HCHDGRD, 135, ChildGuard28, NEUTRAL_REACTION);
	if (global_var(GVAR_FIND_WATER_CHIP) != 2) then begin
		giq_option(4, SCRIPT_HCHDGRD, 136, ChildGuard29, NEUTRAL_REACTION);
	end
	if (global_var(GVAR_DECKER_KNOWN) == 1) then begin
		giq_option(4, SCRIPT_HCHDGRD, 137, ChildGuard06, NEUTRAL_REACTION);
	end
end

procedure ChildGuard08
begin
	gsay_message(SCRIPT_HCHDGRD, 138, NEUTRAL_REACTION);
end

procedure ChildGuard09
begin
	gsay_message(SCRIPT_HCHDGRD, 139, NEUTRAL_REACTION);
end

procedure ChildGuard10
begin
	gsay_message(SCRIPT_HCHDGRD, 140, NEUTRAL_REACTION);
end

procedure ChildGuard11
begin
	gsay_message(SCRIPT_HCHDGRD, 141, NEUTRAL_REACTION);
end

procedure ChildGuard12
begin
	gsay_message(SCRIPT_HCHDGRD, 142, NEUTRAL_REACTION);
end

procedure ChildGuard13
begin
	gsay_message(SCRIPT_HCHDGRD, 143, NEUTRAL_REACTION);
end

procedure ChildGuard14
begin
	set_global_var(GVAR_ENEMY_CATHEDRAL, 1);
	gsay_message(SCRIPT_HCHDGRD, 144, NEUTRAL_REACTION);
	call combat;
end

procedure ChildGuard15
begin
	gsay_message(SCRIPT_HCHDGRD, 145, NEUTRAL_REACTION);
end

procedure ChildGuard16
begin
	gsay_message(SCRIPT_HCHDGRD, 146, NEUTRAL_REACTION);
end

procedure ChildGuard17
begin
	gsay_message(SCRIPT_HCHDGRD, 147, NEUTRAL_REACTION);
end

procedure ChildGuard18
begin
	gsay_message(SCRIPT_HCHDGRD, 148, NEUTRAL_REACTION);
end

procedure ChildGuard19
begin
	gsay_message(SCRIPT_HCHDGRD, 149, NEUTRAL_REACTION);
end

procedure ChildGuard20
begin
	gsay_message(SCRIPT_HCHDGRD, 150, NEUTRAL_REACTION);
end

procedure ChildGuard21
begin
	gsay_message(SCRIPT_HCHDGRD, 151, NEUTRAL_REACTION);
end

procedure ChildGuard22
begin
	gsay_message(SCRIPT_HCHDGRD, 152, NEUTRAL_REACTION);
end

procedure ChildGuard23
begin
	gsay_message(SCRIPT_HCHDGRD, 153, NEUTRAL_REACTION);
end

procedure ChildGuard24
begin
	gsay_message(SCRIPT_HCHDGRD, 154, NEUTRAL_REACTION);
end

procedure ChildGuard25
begin
	gsay_message(SCRIPT_HCHDGRD, 155, NEUTRAL_REACTION);
end

procedure ChildGuard26
begin
	gsay_message(SCRIPT_HCHDGRD, 156, NEUTRAL_REACTION);
end

procedure ChildGuard27
begin
	gsay_message(SCRIPT_HCHDGRD, 157, NEUTRAL_REACTION);
end

procedure ChildGuard28
begin
	set_global_var(GVAR_DECKER_KNOWN, 1);
	gsay_message(SCRIPT_HCHDGRD, 158, NEUTRAL_REACTION);
end

procedure ChildGuard29
begin
	gsay_message(SCRIPT_HCHDGRD, 159, NEUTRAL_REACTION);
end

procedure ChildGuard30
begin
	float_msg(self_obj, message_str(SCRIPT_HCHDGRD, 160), FLOAT_MSG_RED);
end

procedure ChildGuard31
begin
	set_global_var(GVAR_ENEMY_CATHEDRAL, 1);
	gsay_message(SCRIPT_HCHDGRD, 161, NEUTRAL_REACTION);
	call combat;
end

procedure ChildGuard32
begin
	set_local_var(6, 1);
	gsay_reply(SCRIPT_HCHDGRD, 162);
	giq_option(4, SCRIPT_HCHDGRD, 163, ChildGuard33, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HCHDGRD, 164, ChildGuard34, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HCHDGRD, 165, ChildGuard35, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HCHDGRD, 166, ChildGuard37, NEUTRAL_REACTION);
end

procedure ChildGuard33
begin
	gsay_message(SCRIPT_HCHDGRD, 167, NEUTRAL_REACTION);
end

procedure ChildGuard34
begin
	gsay_message(SCRIPT_HCHDGRD, 168, NEUTRAL_REACTION);
end

procedure ChildGuard35
begin
	gsay_message(SCRIPT_HCHDGRD, 169, NEUTRAL_REACTION);
end

procedure ChildGuard36
begin
	float_msg(self_obj, message_str(SCRIPT_HCHDGRD, 170), FLOAT_MSG_RED);
end

procedure ChildGuard37
begin
	set_global_var(GVAR_ENEMY_CATHEDRAL, 1);
	call combat;
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