#include <define.h>

//#define NAME                    SCRIPT_

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


variable GVar0 := 7000;
variable GVar1;
variable GVar2;
variable GVar3;
variable GVar4;
variable GVar5;
variable GVar6;

procedure start;
#include <push.h>
procedure critter_p_proc;
procedure description_p_proc;
procedure destroy_p_proc;
procedure map_enter_p_proc;
procedure pickup_p_proc;
procedure talk_p_proc;
procedure timed_event_p_proc;
procedure damage_p_proc;
procedure JTGenGrd00;
procedure JTGenGrd00a;
procedure JTGenGrd00b;
procedure JTGenGrd01;
procedure JTGenGrd01a;
procedure JTGenGrd02;
procedure JTGenGrd02a;
procedure JTGenGrd03;
procedure JTGenGrd04;
procedure JTGenGrd05;
procedure JTGenGrd06;
procedure JTGenGrd06a;
procedure JTGenGrd07;
procedure JTGenGrd08;
procedure JTGenGrd08a;
procedure JTGenGrd09;
procedure JTGenGrd16;
procedure JTGenGrd18;
procedure JTGenGrd19;
procedure JTGenGrd20;
procedure JTGenGrdEnd;

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;


procedure start
begin
	if (script_action == critter_proc) then begin
		call critter_p_proc;
	end
	else begin
		if (script_action == description_proc) then begin
			call description_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
						else begin
							if (script_action == timed_event_proc) then begin
								call timed_event_p_proc;
							end
						end
					end
				end
			end
		end
	end
end

procedure critter_p_proc
begin
	if (GVar1) then begin
		GVar1 := 0;
		attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
	end
	else begin
		if ((cur_map_index == MAP_JUNKKILL) or (cur_map_index == MAP_JUNKENT)) then begin
			if (tile_num(self_obj) != local_var(5)) then begin
				animate_move_obj_to_tile(self_obj, local_var(5), ANIMATE_WALK);
			end
			if (obj_can_see_obj(self_obj, dude_obj) and (tile_distance_objs(self_obj, dude_obj) < 12)) then begin
				if (global_var(GVAR_ENEMY_JUNKTOWN) == 1) then begin
					float_msg(self_obj, message_str(SCRIPT_JTGENGRD, 138), FLOAT_MSG_RED);
					GVar1 := 1;
				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 (local_var(4) == 0) and (map_var(2) == 0) and (global_var(GVAR_HIRED_BY_KILLIAN) == 0) and (global_var(GVAR_CAPTURE_GIZMO) == 0) and (cur_map_index == MAP_JUNKKILL)) then begin
						if (not(weapon_checked)) then begin
							weapon_checked := 1;
							add_timer_event(self_obj, game_ticks(10), 1);
							dialogue_system_enter;
						end
					end
					else begin
						if (using_skill(dude_obj, 8) and not(sneak_checked)) then begin
							GVar3 := 1;
							sneak_checked := 1;
							add_timer_event(self_obj, game_ticks(5), 2);
							dialogue_system_enter;
						end
						else begin
							if (cur_map_index == MAP_JUNKKILL) then begin
								if ((map_var(8) == 1) and (tile_distance_objs(self_obj, dude_obj) < 8)) then begin
									set_map_var(8, 0);
									float_msg(self_obj, message_str(SCRIPT_JTGENGRD, 136), FLOAT_MSG_RED);
									if (GVar4) then begin
										GVar1 := 1;
									end
									else begin
										GVar4 := 1;
									end
								end
							end
						end
					end
				end
			end
		end
		else begin
			if (cur_map_index == MAP_JUNKCSNO) then begin
				if (map_var(2) == 2) then begin
					if (GVar5 != 0) then begin
						if (tile_distance(tile_num(self_obj), GVar0) > 3) then begin
							animate_move_obj_to_tile(self_obj, GVar0, ANIMATE_WALK);
						end
						else begin
							if (GVar5 == 1) then begin
								GVar0 := 20284;
								GVar5 := 2;
							end
							else begin
								if (GVar5 == 2) then begin
									GVar0 := 23465;
								end
								if ((tile_distance(tile_num(self_obj), GVar0) > 3) and (GVar5 != 0)) then begin
									animate_move_obj_to_tile(self_obj, GVar0, ANIMATE_WALK);
								end
								else begin
									if (GVar5 == 2) then begin
										GVar0 := 26855;
										GVar5 := 3;
									end
									else begin
										if (GVar5 == 3) then begin
											destroy_object(self_obj);
										end
									end
								end
							end
						end
					end
				end
			end
		end
	end
	if (global_var(GVAR_ENEMY_JUNKTOWN) == 1) then begin
		if (obj_can_see_obj(self_obj, dude_obj)) then begin
			attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
		end
	end
end

procedure description_p_proc
begin
	display_msg(message_str(SCRIPT_JTGENGRD, 100));
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
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 11);
	end
	if (cur_map_index == MAP_JUNKCSNO) then begin
		if (map_var(2) == 1) then begin
			set_map_var(1, map_var(1) - 1);
		end
	end
end

procedure map_enter_p_proc
begin
	if (cur_map_index == MAP_JUNKKILL) then begin
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 12);
	end
	else begin
		if (cur_map_index == MAP_JUNKENT) then begin
			critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 11);
		end
		else begin
			if (cur_map_index == MAP_JUNKCSNO) then begin
				critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 0);
				GVar0 := 23666;
				GVar5 := 1;
				if (map_var(2) == 0) then begin
					destroy_object(self_obj);
				end
			end
		end
	end
	if (local_var(5) == 0) then begin
		set_local_var(5, tile_num(self_obj));
	end
end

procedure pickup_p_proc
begin
	float_msg(self_obj, message_str(SCRIPT_JTGENGRD, random(136, 138)), FLOAT_MSG_RED);
	GVar1 := 1;
end

procedure talk_p_proc
begin
	call Get_Reaction;
	if (GVar3 and (times_caught_sneaking >= 3)) then begin
		call JTGenGrd18;
	end
	else begin
		if (not(local_var(4)) and ((obj_item_subtype(critter_inven_obj(dude_obj, 1)) == 3) or (obj_item_subtype(critter_inven_obj(dude_obj, 2)) == 3)) or (GVar3 and (times_caught_sneaking < 3))) then begin
			start_gdialog(SCRIPT_JTGENGRD, 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)) then begin
				call JTGenGrd00;
			end
			else begin
				if (GVar3) then begin
					call JTGenGrd06;
				end
			end
			gsay_end;
			end_dialogue;
		end
		else begin
			if (global_var(GVAR_CHILDKILLER_REPUTATION) > 2) then begin
				call JTGenGrd19;
			end
			else 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)) or (local_var(1) == 1)) then begin
					call JTGenGrd09;
				end
				else begin
					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)) or (local_var(1) == 3)) then begin
						call JTGenGrd16;
					end
					else begin
						call JTGenGrd20;
					end
				end
			end
		end
	end
end

procedure timed_event_p_proc
begin
	if (fixed_param == 1) then begin
		if (weapon_checked) 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
				float_msg(self_obj, message_str(SCRIPT_JTGENGRD, 134), FLOAT_MSG_NORMAL);
				add_timer_event(self_obj, game_ticks(5), 3);
			end
			else begin
				weapon_checked := 0;
			end
		end
	end
	else begin
		if (fixed_param == 2) then begin
			sneak_checked := 0;
		end
		else begin
			if (fixed_param == 3) then begin
				GVar1 := 1;
			end
			else begin
				if (fixed_param == 4) then begin
					float_msg(self_obj, message_str(SCRIPT_JTGENGRD, 139), FLOAT_MSG_NORMAL);
				end
			end
		end
	end
end

procedure damage_p_proc
begin
	if (source_obj == dude_obj) then begin
		set_global_var(GVAR_ENEMY_JUNKTOWN, 1);
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 11);
	end
end

procedure JTGenGrd00
begin
	call Down_React;
	gsay_reply(SCRIPT_JTGENGRD, 110);
	giq_option(4, SCRIPT_JTGENGRD, 111, JTGenGrd01, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JTGENGRD, 112, JTGenGrd00a, BAD_REACTION);
	giq_option(4, SCRIPT_MODREACT, 105, JTGenGrdEnd, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_JTGENGRD, 113, JTGenGrd00b, GOOD_REACTION);
	giq_option(-3, SCRIPT_JTGENGRD, 114, JTGenGrd05, NEUTRAL_REACTION);
end

procedure JTGenGrd00a
begin
	call Down_React;
	call JTGenGrd02;
end

procedure JTGenGrd00b
begin
	call Up_React;
	call JTGenGrd04;
end

procedure JTGenGrd01
begin
	gsay_reply(SCRIPT_JTGENGRD, 115);
	giq_option(4, SCRIPT_JTGENGRD, 116, JTGenGrd02, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JTGENGRD, 117, JTGenGrd01a, NEUTRAL_REACTION);
end

procedure JTGenGrd01a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, -20))) then begin
		call JTGenGrd03;
	end
	else begin
		call JTGenGrd02;
	end
end

procedure JTGenGrd02
begin
	gsay_reply(SCRIPT_JTGENGRD, 118);
	giq_option(4, SCRIPT_JTGENGRD, 119, JTGenGrdEnd, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JTGENGRD, 120, JTGenGrd02a, BAD_REACTION);
end

procedure JTGenGrd02a
begin
	GVar1 := 1;
end

procedure JTGenGrd03
begin
	set_local_var(4, 1);
	gsay_message(SCRIPT_JTGENGRD, 121, NEUTRAL_REACTION);
end

procedure JTGenGrd04
begin
	gsay_message(SCRIPT_JTGENGRD, 122, NEUTRAL_REACTION);
end

procedure JTGenGrd05
begin
	gsay_message(SCRIPT_JTGENGRD, 123, NEUTRAL_REACTION);
end

procedure JTGenGrd06
begin
	times_caught_sneaking := times_caught_sneaking + 1;
	add_timer_event(self_obj, game_ticks(5), 2);
	gsay_reply(SCRIPT_JTGENGRD, 124);
	giq_option(4, SCRIPT_JTGENGRD, 125, JTGenGrd07, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_JTGENGRD, 126, JTGenGrd06a, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_JTGENGRD, 127, JTGenGrd07, NEUTRAL_REACTION);
end

procedure JTGenGrd06a
begin
	variable LVar0 := 0;
	LVar0 := -5 * times_caught_sneaking;
	if (is_success(roll_vs_skill(dude_obj, 14, LVar0))) then begin
		call JTGenGrd08;
	end
	else begin
		call JTGenGrd07;
	end
end

procedure JTGenGrd07
begin
	gsay_message(SCRIPT_JTGENGRD, 128, NEUTRAL_REACTION);
end

procedure JTGenGrd08
begin
	gsay_reply(SCRIPT_JTGENGRD, 130);
	giq_option(5, SCRIPT_JTGENGRD, 131, JTGenGrdEnd, NEUTRAL_REACTION);
	if (dude_is_male) then begin
		giq_option(5, SCRIPT_JTGENGRD, 132, JTGenGrd08a, NEUTRAL_REACTION);
	end
	else begin
		giq_option(5, SCRIPT_JTGENGRD, 133, JTGenGrd08a, NEUTRAL_REACTION);
	end
end

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

procedure JTGenGrd09
begin
	float_msg(self_obj, message_str(SCRIPT_JTGENGRD, random(101, 103)), FLOAT_MSG_NORMAL);
end

procedure JTGenGrd16
begin
	float_msg(self_obj, message_str(SCRIPT_JTGENGRD, random(104, 106)), FLOAT_MSG_NORMAL);
end

procedure JTGenGrd18
begin
	float_msg(self_obj, message_str(SCRIPT_JTGENGRD, 135), FLOAT_MSG_RED);
	add_timer_event(self_obj, game_ticks(5), 3);
end

procedure JTGenGrd19
begin
	display_msg(message_str(SCRIPT_JTGENGRD, 129));
	GVar1 := 1;
end

procedure JTGenGrd20
begin
	float_msg(self_obj, message_str(SCRIPT_JTGENGRD, random(107, 109)), FLOAT_MSG_NORMAL);
end

procedure JTGenGrdEnd
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
	GVar6 := message_str(SCRIPT_MODREACT, random(100, 105));
end