#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 map_enter_p_proc;
procedure pickup_p_proc;
procedure talk_p_proc;
procedure Officer00;
procedure Officer01;
procedure Officer02;
procedure Officer03;
procedure Officer04;
procedure Officer05;
procedure Officer06;
procedure Officer07;
procedure Officer08;
procedure Officer09;
procedure Officer10;
procedure Officer11;
procedure Officer12;
procedure Officer13;
procedure Officer14;
procedure Officer15;
procedure Officer15a;
procedure Officer16;
procedure Officer17;
procedure Officer18;
procedure Officer19;
procedure Officer20;
procedure Officer21;
procedure Officer21a;
procedure Officer22;
procedure Officer23;
procedure Officer24;
procedure Officer25;
procedure Officer26;
procedure Officer27;
procedure Officer28;
procedure Officer29;
procedure Officer30;
procedure Officer31;
procedure OfficerCombat;
procedure OfficerEnd;

variable hostile1;

import variable SecDoor_ptr;
import variable armory_access;
import variable Officer_ptr;

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 == 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

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_WATER_CHIP_4)) then begin
			if (obj_can_see_obj(self_obj, dude_obj)) then begin
				dialogue_system_enter;
			end
		end
		else begin
			if ((armory_access == 1) and not(obj_is_open(SecDoor_ptr))) then begin
				use_obj(SecDoor_ptr);
			end
			else begin
				if (armory_access == 2) then begin
					anim(self_obj, 1000, 5);
					float_msg(self_obj, message_str(SCRIPT_OFFICER, 159), FLOAT_MSG_NORMAL);
					armory_access := 0;
				end
				else begin
					if (not(armory_access) and (elevation(dude_obj) == elevation(self_obj))) then begin
						if (tile_distance(tile_num(dude_obj), 21292) < 2) then begin
							hostile1 := 1;
						end
					end
				end
			end
		end
	end
end

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

procedure destroy_p_proc
begin
	Officer_ptr := 0;
	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
	if (source_obj == dude_obj) then begin
		set_global_var(GVAR_WATER_CHIP_4, 1);
	end
end

procedure map_enter_p_proc
begin
	critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 1);
	Officer_ptr := self_obj;
end

procedure pickup_p_proc
begin
	hostile1 := 1;
end

procedure talk_p_proc
begin
	call Get_Reaction;
	start_gdialog(SCRIPT_OFFICER, self_obj, 4, -1, -1);
	gsay_start;
	if (global_var(GVAR_WATER_CHIP_4)) then begin
		call Officer00;
	end
	else begin
		if ((obj_item_subtype(critter_inven_obj(dude_obj, 1)) == 3) or (obj_item_subtype(critter_inven_obj(dude_obj, 2)) == 3)) then begin
			call Officer27;
		end
		else begin
			if ((global_var(GVAR_FIND_WATER_CHIP) != 0) and (global_var(GVAR_FIND_WATER_CHIP) != 1)) then begin
				call Officer01;
			end
			else begin
				if (local_var(1) > 1) then begin
					if (armory_access) then begin
						call Officer25;
					end
					else begin
						call Officer02;
					end
				end
				else begin
					if (armory_access) then begin
						call Officer26;
					end
					else begin
						call Officer18;
					end
				end
			end
		end
	end
	gsay_end;
	end_dialogue;
end

procedure Officer00
begin
	gsay_message(SCRIPT_OFFICER, 100, BAD_REACTION);
	hostile1 := 1;
end

procedure Officer01
begin
	gsay_message(SCRIPT_OFFICER, 101, NEUTRAL_REACTION);
end

procedure Officer02
begin
	gsay_reply(SCRIPT_OFFICER, 102);
	giq_option(4, SCRIPT_OFFICER, 103, Officer04, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_OFFICER, 104, Officer08, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_OFFICER, 105, Officer11, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_OFFICER, 106, Officer03, NEUTRAL_REACTION);
end

procedure Officer03
begin
	gsay_reply(SCRIPT_OFFICER, 107);
	giq_option(4, SCRIPT_OFFICER, 159, OfficerEnd, NEUTRAL_REACTION);
end

procedure Officer04
begin
	gsay_reply(SCRIPT_OFFICER, 108);
	giq_option(4, SCRIPT_OFFICER, 109, OfficerEnd, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_OFFICER, 110, Officer05, NEUTRAL_REACTION);
end

procedure Officer05
begin
	gsay_reply(SCRIPT_OFFICER, 111);
	giq_option(5, SCRIPT_OFFICER, 112, Officer06, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_OFFICER, 113, Officer07, NEUTRAL_REACTION);
end

procedure Officer06
begin
	gsay_reply(SCRIPT_OFFICER, 114);
	giq_option(5, SCRIPT_OFFICER, 115, OfficerEnd, NEUTRAL_REACTION);
end

procedure Officer07
begin
	call Up_React;
	gsay_message(SCRIPT_OFFICER, 116, NEUTRAL_REACTION);
end

procedure Officer08
begin
	gsay_reply(SCRIPT_OFFICER, 117);
	giq_option(4, SCRIPT_OFFICER, 118, Officer09, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_OFFICER, 119, Officer10, NEUTRAL_REACTION);
end

procedure Officer09
begin
	call Down_React;
	gsay_message(SCRIPT_OFFICER, 120, NEUTRAL_REACTION);
end

procedure Officer10
begin
	gsay_message(SCRIPT_OFFICER, 121, NEUTRAL_REACTION);
end

procedure Officer11
begin
	gsay_reply(SCRIPT_OFFICER, 122);
	giq_option(5, SCRIPT_OFFICER, 123, Officer13, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_OFFICER, 124, Officer12, NEUTRAL_REACTION);
end

procedure Officer12
begin
	gsay_message(SCRIPT_OFFICER, 125, NEUTRAL_REACTION);
end

procedure Officer13
begin
	gsay_reply(SCRIPT_OFFICER, 126);
	giq_option(5, SCRIPT_OFFICER, 127, Officer15, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_OFFICER, 128, Officer14, NEUTRAL_REACTION);
end

procedure Officer14
begin
	gsay_message(SCRIPT_OFFICER, 129, NEUTRAL_REACTION);
end

procedure Officer15
begin
	gsay_reply(SCRIPT_OFFICER, 130);
	giq_option(5, SCRIPT_OFFICER, 131, Officer15a, NEUTRAL_REACTION);
end

procedure Officer15a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, 0))) then begin
		call Officer17;
	end
	else begin
		call Officer16;
	end
end

procedure Officer16
begin
	gsay_message(SCRIPT_OFFICER, 132, NEUTRAL_REACTION);
end

procedure Officer17
begin
	armory_access := 1;
	gsay_message(SCRIPT_OFFICER, 133, NEUTRAL_REACTION);
end

procedure Officer18
begin
	gsay_reply(SCRIPT_OFFICER, message_str(SCRIPT_OFFICER, 134) + proto_data(obj_pid(dude_obj), 1) + message_str(SCRIPT_OFFICER, 135));
	giq_option(4, SCRIPT_OFFICER, 136, Officer20, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_OFFICER, 137, Officer21, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_OFFICER, 138, Officer19, NEUTRAL_REACTION);
end

procedure Officer19
begin
	gsay_message(SCRIPT_OFFICER, 139, NEUTRAL_REACTION);
end

procedure Officer20
begin
	gsay_message(SCRIPT_OFFICER, 140, NEUTRAL_REACTION);
end

procedure Officer21
begin
	gsay_reply(SCRIPT_OFFICER, 141);
	giq_option(5, SCRIPT_OFFICER, 142, Officer22, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_OFFICER, 143, Officer21a, NEUTRAL_REACTION);
end

procedure Officer21a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, 0))) then begin
		call Officer23;
	end
	else begin
		call Officer24;
	end
end

procedure Officer22
begin
	gsay_reply(SCRIPT_OFFICER, 144);
	giq_option(5, SCRIPT_OFFICER, 145, OfficerEnd, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_OFFICER, 146, OfficerCombat, NEUTRAL_REACTION);
end

procedure Officer23
begin
	gsay_message(SCRIPT_OFFICER, 147, NEUTRAL_REACTION);
end

procedure Officer24
begin
	gsay_message(SCRIPT_OFFICER, 148, NEUTRAL_REACTION);
end

procedure Officer25
begin
	gsay_message(SCRIPT_OFFICER, 149, NEUTRAL_REACTION);
end

procedure Officer26
begin
	gsay_message(SCRIPT_OFFICER, 150, NEUTRAL_REACTION);
end

procedure Officer27
begin
	gsay_message(SCRIPT_OFFICER, 151, NEUTRAL_REACTION);
end

procedure Officer28
begin
	gsay_reply(SCRIPT_OFFICER, 152);
	giq_option(4, SCRIPT_OFFICER, 153, Officer30, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_OFFICER, 154, Officer31, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_OFFICER, 155, Officer29, NEUTRAL_REACTION);
end

procedure Officer29
begin
	gsay_message(SCRIPT_OFFICER, 156, NEUTRAL_REACTION);
end

procedure Officer30
begin
	gsay_message(SCRIPT_OFFICER, 157, NEUTRAL_REACTION);
end

procedure Officer31
begin
	gsay_message(SCRIPT_OFFICER, 158, NEUTRAL_REACTION);
end

procedure OfficerCombat
begin
	hostile1 := 1;
end

procedure OfficerEnd
begin
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