#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure do_dialogue;
procedure PalGuard01;
procedure PalGuard02;

variable line;
variable Only_Once;

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 == 0) then begin
		Only_Once := 1;
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 44);
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_BROTHERHOOD_PALADIN);
	end
	if (script_action == talk_proc) then begin
		call do_dialogue;
	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_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
		else begin
			if ((script_action == look_at_proc) or (script_action == description_proc)) then begin
				script_overrides;
				display_msg(message_str(SCRIPT_PALGUARD, 100));
			end
			else begin
				if (script_action == timed_event_proc) then begin
					attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
				end
			end
		end
	end
end

procedure do_dialogue
begin
	call Get_Reaction;
	if (local_var(1) < 2) then begin
		call PalGuard01;
	end
	else begin
		call PalGuard02;
	end
end

procedure PalGuard01
begin
	if (not(line)) then begin
		line := random(1, 9);
	end
	else begin
		line := line + 1;
	end
	if (line > 9) then begin
		line := 1;
	end
	if (line == 1) then begin
		float_msg(self_obj, message_str(SCRIPT_PALGUARD, 101), FLOAT_MSG_RED);
	end
	else begin
		if (line == 2) then begin
			float_msg(self_obj, message_str(SCRIPT_PALGUARD, 102), FLOAT_MSG_RED);
		end
		else begin
			if (line == 3) then begin
				float_msg(self_obj, message_str(SCRIPT_PALGUARD, 103), FLOAT_MSG_RED);
			end
			else begin
				if (line == 4) then begin
					float_msg(self_obj, message_str(SCRIPT_PALGUARD, 104), FLOAT_MSG_RED);
				end
				else begin
					if (line == 5) then begin
						float_msg(self_obj, message_str(SCRIPT_PALGUARD, 105), FLOAT_MSG_RED);
					end
					else begin
						if (line == 6) then begin
							if (dude_is_female) then begin
								float_msg(self_obj, message_str(SCRIPT_PALGUARD, 106), FLOAT_MSG_RED);
							end
							else begin
								line := line + 1;
							end
						end
						else begin
							if (line == 7) then begin
								if (global_var(GVAR_BECOME_AN_INITIATE) == 2) then begin
									float_msg(self_obj, message_str(SCRIPT_PALGUARD, 107), FLOAT_MSG_RED);
								end
								else begin
									line := line + 1;
								end
							end
							else begin
								if (line == 8) then begin
									if (global_var(GVAR_BECOME_AN_INITIATE) < 2) then begin
										float_msg(self_obj, message_str(SCRIPT_PALGUARD, 108), FLOAT_MSG_RED);
									end
									else begin
										line := line + 1;
									end
								end
								else begin
									if (line == 9) then begin
										if (global_var(GVAR_BECOME_AN_INITIATE) < 2) then begin
											float_msg(self_obj, message_str(SCRIPT_PALGUARD, 109), FLOAT_MSG_RED);
										end
										else begin
											line := 1;
											float_msg(self_obj, message_str(SCRIPT_PALGUARD, 110), FLOAT_MSG_RED);
										end
									end
								end
							end
						end
					end
				end
			end
		end
	end
end

procedure PalGuard02
begin
	if (not(line)) then begin
		line := random(1, 10);
	end
	else begin
		line := line + 1;
	end
	if (line > 10) then begin
		line := 1;
	end
	if (line == 1) then begin
		float_msg(self_obj, message_str(SCRIPT_PALGUARD, 111), FLOAT_MSG_RED);
	end
	else begin
		if (line == 2) then begin
			float_msg(self_obj, message_str(SCRIPT_PALGUARD, 112), FLOAT_MSG_RED);
		end
		else begin
			if (line == 3) then begin
				float_msg(self_obj, message_str(SCRIPT_PALGUARD, 113), FLOAT_MSG_RED);
			end
			else begin
				if (line == 4) then begin
					float_msg(self_obj, message_str(SCRIPT_PALGUARD, 114), FLOAT_MSG_RED);
				end
				else begin
					if (line == 5) then begin
						float_msg(self_obj, message_str(SCRIPT_PALGUARD, 115), FLOAT_MSG_RED);
					end
					else begin
						if (line == 7) then begin
							if (global_var(GVAR_BECOME_AN_INITIATE) == 2) then begin
								float_msg(self_obj, message_str(SCRIPT_PALGUARD, 116), FLOAT_MSG_RED);
							end
							else begin
								float_msg(self_obj, message_str(SCRIPT_PALGUARD, 117), FLOAT_MSG_RED);
							end
						end
						else begin
							if (line == 7) then begin
								if (global_var(GVAR_BECOME_AN_INITIATE) == 2) then begin
									float_msg(self_obj, message_str(SCRIPT_PALGUARD, 118), FLOAT_MSG_RED);
								end
								else begin
									float_msg(self_obj, message_str(SCRIPT_PALGUARD, 119), FLOAT_MSG_RED);
								end
							end
						end
					end
				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