#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 timed_event_p_proc;
procedure do_dialogue;
procedure pre_dialogue;
procedure guard00a;
procedure guard01a;
procedure guard02a;
procedure guard00;
procedure guard00i;
procedure guard00ii;
procedure guard01;
procedure guard01i;
procedure guard02;
procedure guard02i;
procedure guard03;
procedure guard04;
procedure guard05;
procedure guard06;
procedure guard06i;
procedure guard07;
procedure guard08;
procedure guard08i;
procedure guard09;
procedure guard10;
procedure guard11;
procedure guard12;
procedure guard12i;
procedure guard13;
procedure guard14;
procedure guard15;
procedure guard16;
procedure guard17;
procedure Guard18;
procedure guard00N;
procedure guard00Na;
procedure guard01N;
procedure guard02N;
procedure guard03N;
procedure guard04N;
procedure guardend;

variable sneaking;
variable RoundCounter;
variable hostile1;
variable warned;
variable line166flag;

import variable weapon_checked;
import variable sneak_checked;
import variable times_caught_sneaking;

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 == damage_proc) then begin
			call damage_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 pre_dialogue;
						end
						else begin
							if (script_action == look_at_proc) then begin
								script_overrides;
								display_msg(message_str(SCRIPT_ASSBLOW, 100));
							end
							else begin
								if (script_action == timed_event_proc) then begin
									call timed_event_p_proc;
								end
							end
						end
					end
				end
			end
		end
	end
end

procedure critter_p_proc
begin
	if (hostile1 and not(local_var(4))) then begin
		hostile1 := 0;
		set_local_var(4, 1);
		attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
	end
	else begin
		if (obj_can_see_obj(self_obj, dude_obj)) then begin
			if (global_var(GVAR_ENEMY_JUNKTOWN) == 1) then begin
				if (line166flag == 0) then begin
					dialogue_system_enter;
				end
			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) and not(local_var(4)) and (global_var(GVAR_HIRED_BY_KILLIAN) == 0) and (global_var(GVAR_CAPTURE_GIZMO) == 0)) then begin
					if (not(weapon_checked)) then begin
						weapon_checked := 1;
						rm_timer_event(self_obj);
						add_timer_event(self_obj, game_ticks(5), 1);
						dialogue_system_enter;
					end
				end
				else begin
					if (using_skill(dude_obj, 8) and not(sneak_checked)) then begin
						sneaking := 1;
						sneak_checked := 1;
						rm_timer_event(self_obj);
						add_timer_event(self_obj, game_ticks(5), 2);
						dialogue_system_enter;
					end
				end
			end
		end
		if ((game_time_hour >= 1900) or (game_time_hour < 600)) then begin
			if (tile_distance(tile_num(dude_obj), 27106) > tile_distance(tile_num(dude_obj), 25905)) then begin
				if (local_var(5) == 0) then begin
					if (tile_distance_objs(self_obj, dude_obj) < 12) then begin
						if (not(warned)) then begin
							if (not(using_skill(dude_obj, 8))) then begin
								warned := 1;
								dialogue_system_enter;
								rm_timer_event(self_obj);
								add_timer_event(self_obj, game_ticks(3), 4);
							end
						end
					end
				end
			end
		end
	end
end

procedure damage_p_proc
begin
	if (source_obj == dude_obj) then begin
		set_global_var(GVAR_ENEMY_JUNKTOWN, 1);
		set_local_var(6, 1);
	end
end

procedure destroy_p_proc
begin
	if (source_obj == dude_obj) then begin
		set_global_var(GVAR_ENEMY_JUNKTOWN, 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
end

procedure map_enter_p_proc
begin
	if (global_var(GVAR_JUNKTOWN_INVADED) == 1) then begin
		kill_critter(self_obj, 59);
	end
end

procedure pickup_p_proc
begin
	hostile1 := 1;
end

procedure timed_event_p_proc
begin
	if (fixed_param == 1) then 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
			hostile1 := 1;
		end
		else begin
			weapon_checked := 0;
		end
	end
	else begin
		if (fixed_param == 2) then begin
			sneak_checked := 0;
		end
		else begin
			if (fixed_param == 3) then begin
				hostile1 := 1;
			end
			else begin
				if (fixed_param == 4) then begin
					if ((game_time_hour >= 1900) or (game_time_hour < 600)) then begin
						if (tile_distance(tile_num(dude_obj), 27106) > tile_distance(tile_num(dude_obj), 25905)) then begin
							if (local_var(5) == 0) then begin
								if (not(using_skill(dude_obj, 8))) then begin
									hostile1 := 1;
								end
							end
						end
						else begin
							warned := 0;
						end
					end
				end
			end
		end
	end
end

procedure do_dialogue
begin
	call Get_Reaction;
	if ((game_time_hour >= 1900) or (game_time_hour < 600) and (local_var(7) == 1)) then begin
		if (local_var(5) == 1) then begin
			float_msg(self_obj, message_str(SCRIPT_ASSBLOW, 167), FLOAT_MSG_NORMAL);
		end
		else begin
			float_msg(self_obj, message_str(SCRIPT_ASSBLOW, 156), FLOAT_MSG_NORMAL);
		end
	end
	else begin
		start_gdialog(SCRIPT_ASSBLOW, self_obj, 4, -1, -1);
		gsay_start;
		if ((obj_item_subtype(critter_inven_obj(dude_obj, 1)) == 3) or (obj_item_subtype(critter_inven_obj(dude_obj, 2)) == 3) and not(local_var(4))) then begin
			call guard00;
		end
		else begin
			if (sneaking) then begin
				call guard06;
			end
			else begin
				if ((game_time_hour >= 1900) or (game_time_hour < 600)) then begin
					call guard00N;
				end
				else begin
					if (local_var(8) == 0) then begin
						call guard10;
					end
				end
			end
		end
		gsay_end;
		end_dialogue;
	end
end

procedure pre_dialogue
begin
	if ((global_var(GVAR_ENEMY_JUNKTOWN) == 1) and (line166flag == 0)) then begin
		call Guard18;
	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) and not(local_var(4))) then begin
			call do_dialogue;
		end
		else begin
			if (sneaking) then begin
				call do_dialogue;
			end
			else begin
				if ((game_time_hour >= 1900) or (game_time_hour < 600)) then begin
					call do_dialogue;
				end
				else begin
					if (local_var(8) == 0) then begin
						call do_dialogue;
					end
					else begin
						call guard02a;
					end
				end
			end
		end
	end
end

procedure guard00a
begin
	float_msg(self_obj, message_str(SCRIPT_ASSBLOW, random(101, 103)), FLOAT_MSG_RED);
end

procedure guard01a
begin
	float_msg(self_obj, message_str(SCRIPT_ASSBLOW, random(104, 106)), FLOAT_MSG_GREEN);
end

procedure guard02a
begin
	float_msg(self_obj, message_str(SCRIPT_ASSBLOW, random(107, 109)), FLOAT_MSG_NORMAL);
end

procedure guard00
begin
	gsay_reply(SCRIPT_ASSBLOW, 110);
	giq_option(4, SCRIPT_ASSBLOW, 111, guard01, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_ASSBLOW, 112, guard00i, BAD_REACTION);
	giq_option(4, SCRIPT_MODREACT, 105, guardend, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_ASSBLOW, 113, guard00ii, GOOD_REACTION);
	giq_option(-3, SCRIPT_ASSBLOW, 114, guard05, NEUTRAL_REACTION);
end

procedure guard00i
begin
	call Big_Down_React;
	call guard02;
end

procedure guard00ii
begin
	call Big_Up_React;
	call guard04;
end

procedure guard01
begin
	gsay_reply(SCRIPT_ASSBLOW, 115);
	giq_option(4, SCRIPT_ASSBLOW, 116, guard02, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_ASSBLOW, 117, guard01i, NEUTRAL_REACTION);
end

procedure guard01i
begin
	if (is_success(roll_vs_skill(dude_obj, 14, -30))) then begin
		call guard03;
	end
	else begin
		call guard02;
	end
end

procedure guard02
begin
	gsay_reply(SCRIPT_ASSBLOW, 118);
	giq_option(4, SCRIPT_ASSBLOW, 119, guardend, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_ASSBLOW, 120, guard02i, BAD_REACTION);
end

procedure guard02i
begin
	hostile1 := 1;
end

procedure guard03
begin
	set_local_var(4, 1);
	gsay_message(SCRIPT_ASSBLOW, 121, NEUTRAL_REACTION);
end

procedure guard04
begin
	gsay_message(SCRIPT_ASSBLOW, 122, NEUTRAL_REACTION);
end

procedure guard05
begin
	gsay_message(SCRIPT_ASSBLOW, 123, NEUTRAL_REACTION);
end

procedure guard06
begin
	sneaking := 0;
	gsay_reply(SCRIPT_ASSBLOW, 124);
	giq_option(4, SCRIPT_ASSBLOW, 125, guard07, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_ASSBLOW, 126, guard06i, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_ASSBLOW, 127, guard07, NEUTRAL_REACTION);
end

procedure guard06i
begin
	variable LVar0 := 0;
	times_caught_sneaking := times_caught_sneaking + 1;
	LVar0 := -5 * times_caught_sneaking;
	if (is_success(roll_vs_skill(dude_obj, 14, LVar0))) then begin
		call guard08;
	end
	else begin
		call guard07;
	end
end

procedure guard07
begin
	if (times_caught_sneaking > 3) then begin
		gsay_message(SCRIPT_ASSBLOW, 129, BAD_REACTION);
		add_timer_event(self_obj, game_ticks(10), 3);
	end
	else begin
		gsay_message(SCRIPT_ASSBLOW, 128, BAD_REACTION);
	end
end

procedure guard08
begin
	gsay_reply(SCRIPT_ASSBLOW, 130);
	giq_option(5, SCRIPT_ASSBLOW, 131, guardend, NEUTRAL_REACTION);
	if (dude_is_male) then begin
		giq_option(5, SCRIPT_ASSBLOW, 132, guard08i, NEUTRAL_REACTION);
	end
	else begin
		giq_option(5, SCRIPT_ASSBLOW, 133, guard08i, NEUTRAL_REACTION);
	end
end

procedure guard08i
begin
	if (random(0, 1)) then begin
		call Up_React;
	end
	else begin
		call Down_React;
	end
end

procedure guard09
begin
	gsay_message(SCRIPT_ASSBLOW, 134, NEUTRAL_REACTION);
end

procedure guard10
begin
	set_local_var(8, 1);
	gsay_reply(SCRIPT_ASSBLOW, 135);
	giq_option(4, SCRIPT_ASSBLOW, 136, guardend, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_ASSBLOW, 137, guard12, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_ASSBLOW, 138, guard15, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_ASSBLOW, 139, guard11, NEUTRAL_REACTION);
end

procedure guard11
begin
	gsay_message(SCRIPT_ASSBLOW, 140, NEUTRAL_REACTION);
end

procedure guard12
begin
	gsay_reply(SCRIPT_ASSBLOW, 141);
	giq_option(5, SCRIPT_ASSBLOW, 142, guard12i, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_ASSBLOW, 143, guard14, NEUTRAL_REACTION);
end

procedure guard12i
begin
	if (is_success(roll_vs_skill(dude_obj, 14, 15))) then begin
		call guard13;
	end
	else begin
		call guard14;
	end
end

procedure guard13
begin
	gsay_reply(SCRIPT_ASSBLOW, 144);
	giq_option(5, SCRIPT_ASSBLOW, 145, guardend, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_ASSBLOW, 146, Big_Down_React, BAD_REACTION);
end

procedure guard14
begin
	gsay_message(SCRIPT_ASSBLOW, 147, NEUTRAL_REACTION);
end

procedure guard15
begin
	gsay_reply(SCRIPT_ASSBLOW, 148);
	giq_option(5, SCRIPT_ASSBLOW, 149, guardend, NEUTRAL_REACTION);
	if (dude_is_female) then begin
		giq_option(5, SCRIPT_ASSBLOW, 150, guard16, NEUTRAL_REACTION);
	end
end

procedure guard16
begin
	gsay_message(SCRIPT_ASSBLOW, 152, NEUTRAL_REACTION);
end

procedure guard17
begin
	gsay_message(SCRIPT_ASSBLOW, random(153, 155), NEUTRAL_REACTION);
end

procedure Guard18
begin
	float_msg(self_obj, message_str(SCRIPT_ASSBLOW, 166), FLOAT_MSG_RED);
	rm_timer_event(self_obj);
	add_timer_event(self_obj, game_ticks(5), 3);
end

procedure guard00N
begin
	set_local_var(7, 1);
	gsay_reply(SCRIPT_ASSBLOW, 156);
	giq_option(4, SCRIPT_ASSBLOW, 157, guard02N, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_ASSBLOW, 158, guardend, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_ASSBLOW, 159, guard00Na, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_ASSBLOW, 160, guard01N, NEUTRAL_REACTION);
end

procedure guard00Na
begin
	if (is_success(roll_vs_skill(dude_obj, 14, 0))) then begin
		call guard03N;
	end
	else begin
		call guard02N;
	end
end

procedure guard01N
begin
	gsay_message(SCRIPT_ASSBLOW, 161, NEUTRAL_REACTION);
end

procedure guard02N
begin
	gsay_message(SCRIPT_ASSBLOW, 162, NEUTRAL_REACTION);
end

procedure guard03N
begin
	set_local_var(5, 1);
	gsay_message(SCRIPT_ASSBLOW, 163, NEUTRAL_REACTION);
end

procedure guard04N
begin
	set_local_var(5, 1);
	gsay_message(SCRIPT_ASSBLOW, message_str(SCRIPT_ASSBLOW, 164) + proto_data(obj_pid(dude_obj), 1) + message_str(SCRIPT_ASSBLOW, 165), NEUTRAL_REACTION);
end

procedure guardend
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