#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 combat_p_proc;
procedure damage_p_proc;
procedure Justin00;
procedure Justin01;
procedure Justin01a;
procedure Justin01b;
procedure Justin02;
procedure Justin03;
procedure Justin04;
procedure Justin05a;
procedure Justin05b;
procedure Justin06;
procedure Justin07;
procedure Justin08;
procedure Justin09;
procedure Justin10;
procedure Justin11;
procedure Justin12;
procedure Justin13;
procedure Justin14;
procedure Justin15;
procedure Justin15a;
procedure Justin15b;
procedure Justin15c;
procedure Justin15d;
procedure Justin16;
procedure Justin17;
procedure Justin18;
procedure Justin19;
procedure Justin20;
procedure Justin26;
procedure Justin27;
procedure Justin28;
procedure Justin29;
procedure Justin30;
procedure Justin31;
procedure Justin32;
procedure Justin33;
procedure Justin34;
procedure Justin35;
procedure JustinEnd;
procedure JustinCombat;
procedure JustinTravel;
procedure GoToDecker;

variable hostile1;
variable Only_Once := 1;
variable travel;

import variable Kane_Ptr;
import variable Decker_Ptr;
import variable Justin_Ptr;
import variable Fry_Stub_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 Justin22;


procedure start
begin
	if (Only_Once) then begin
		Only_Once := 0;
		Justin_Ptr := self_obj;
		if (global_var(GVAR_GREENE_DEAD) == 1) then begin
			set_obj_visibility(self_obj, 0);
		end
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 40);
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_HUB_POLICE);
	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
					else begin
						if (script_action == combat_proc) then begin
							call combat_p_proc;
						end
					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 (travel == 1) then begin
		travel := 0;
		call GoToDecker;
	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(53) == 1) then begin
		set_map_var(53, 0);
		start_gdialog(SCRIPT_JUSTIN, self_obj, 4, -1, -1);
		gsay_start;
		call Justin35;
		gsay_end;
		end_dialogue;
	end
	else begin
		if (local_var(7) == 1) then begin
			start_gdialog(SCRIPT_JUSTIN, self_obj, 4, -1, -1);
			gsay_start;
			call Justin29;
			gsay_end;
			end_dialogue;
		end
		else begin
			if (global_var(GVAR_DECKER_STATUS) == 1) then begin
				start_gdialog(SCRIPT_JUSTIN, self_obj, 4, -1, -1);
				gsay_start;
				call Justin27;
				gsay_end;
				end_dialogue;
			end
			else begin
				if (local_var(6) == 1) then begin
					start_gdialog(SCRIPT_JUSTIN, self_obj, 4, -1, -1);
					gsay_start;
					call Justin26;
					gsay_end;
					end_dialogue;
				end
				else begin
					if (local_var(6) == 2) then begin
						start_gdialog(SCRIPT_JUSTIN, self_obj, 4, -1, -1);
						gsay_start;
						call Justin28;
						gsay_end;
						end_dialogue;
					end
					else begin
						if (local_var(4) == 0) then begin
							set_local_var(4, 1);
							start_gdialog(SCRIPT_JUSTIN, self_obj, 4, -1, -1);
							gsay_start;
							call Justin00;
							gsay_end;
							end_dialogue;
						end
						else begin
							if (global_var(GVAR_CHILDKILLER_REPUTATION) > 2) then begin
								call Justin03;
							end
							else begin
								if ((local_var(1) == 3) or (local_var(1) == 0)) then begin
									start_gdialog(SCRIPT_JUSTIN, self_obj, 4, -1, -1);
									gsay_start;
									call Justin31;
									gsay_end;
									end_dialogue;
								end
								else begin
									if (local_var(1) == 2) then begin
										start_gdialog(SCRIPT_JUSTIN, self_obj, 4, -1, -1);
										gsay_start;
										call Justin32;
										gsay_end;
										end_dialogue;
									end
									else begin
										if (local_var(1) == 1) then begin
											start_gdialog(SCRIPT_JUSTIN, self_obj, 4, -1, -1);
											gsay_start;
											call Justin33;
											gsay_end;
											end_dialogue;
										end
									end
								end
							end
						end
					end
				end
			end
		end
	end
end

procedure destroy_p_proc
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
	set_global_var(GVAR_GREENE_DEAD, 1);
end

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

procedure combat_p_proc
begin
	if (map_var(54) == 1) then begin
		script_overrides;
	end
end

procedure damage_p_proc
begin
	variable LVar0 := 0;
	LVar0 := obj_pid(source_obj);
	if ((party_member_obj(LVar0) != 0) and (map_var(52) == 0)) then begin
		set_global_var(GVAR_ENEMY_HUB, 1);
	end
end

procedure Justin00
begin
	if (global_var(GVAR_CHILDKILLER_REPUTATION) > 2) then begin
		gsay_message(SCRIPT_JUSTIN, 101, NEUTRAL_REACTION);
	end
	else begin
		if (dude_is_male) then begin
			gsay_reply(SCRIPT_JUSTIN, 102);
		end
		else begin
			gsay_reply(SCRIPT_JUSTIN, 103);
		end
		call Justin30;
	end
end

procedure Justin01
begin
	gsay_reply(SCRIPT_JUSTIN, 104);
	if ((global_var(GVAR_DECKER_STATUS) == 0) and ((map_var(46) > 0) and (map_var(46) != 6))) then begin
		giq_option(4, SCRIPT_JUSTIN, 105, Justin01a, NEUTRAL_REACTION);
	end
	if ((global_var(GVAR_DECKER_STATUS) == 0) and ((map_var(47) > 0) and (map_var(47) != 7))) then begin
		giq_option(4, SCRIPT_JUSTIN, 106, Justin01b, NEUTRAL_REACTION);
	end
	giq_option(4, SCRIPT_JUSTIN, 107, JustinEnd, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JUSTIN, 108, Justin04, NEUTRAL_REACTION);
end

procedure Justin01a
begin
	if (global_var(GVAR_KILL_MERCHANT) != 2) then begin
		call Justin05a;
	end
	else begin
		if (is_success(roll_vs_skill(dude_obj, 14, 0))) then begin
			call Justin06;
		end
		else begin
			call Justin07;
		end
	end
end

procedure Justin01b
begin
	if (global_var(GVAR_KILL_JAIN) != 2) then begin
		call Justin05a;
	end
	else begin
		if (is_success(roll_vs_skill(dude_obj, 14, 0))) then begin
			call Justin06;
		end
		else begin
			call Justin08;
		end
	end
end

procedure Justin02
begin
	if (local_var(5) == 0) then begin
		call Up_React;
		set_local_var(5, 1);
		gsay_message(SCRIPT_JUSTIN, 109, GOOD_REACTION);
	end
	else begin
		gsay_message(SCRIPT_JUSTIN, 110, GOOD_REACTION);
	end
end

procedure Justin03
begin
	float_msg(self_obj, message_str(SCRIPT_JUSTIN, 111), FLOAT_MSG_RED);
end

procedure Justin04
begin
	gsay_message(SCRIPT_JUSTIN, 112, NEUTRAL_REACTION);
end

procedure Justin05a
begin
	gsay_reply(SCRIPT_JUSTIN, 170);
	giq_option(4, SCRIPT_JUSTIN, 171, Justin05b, NEUTRAL_REACTION);
end

procedure Justin05b
begin
	gsay_reply(SCRIPT_JUSTIN, 113);
	call Justin09;
end

procedure Justin06
begin
	gsay_reply(SCRIPT_JUSTIN, 114);
	call Justin09;
end

procedure Justin07
begin
	gsay_reply(SCRIPT_JUSTIN, 115);
	call Justin15;
end

procedure Justin08
begin
	gsay_reply(SCRIPT_JUSTIN, 116);
	call Justin15;
end

procedure Justin09
begin
	giq_option(4, SCRIPT_JUSTIN, 117, Justin11, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JUSTIN, 118, Justin10, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JUSTIN, 119, Justin12, NEUTRAL_REACTION);
end

procedure Justin10
begin
	gsay_reply(SCRIPT_JUSTIN, 120);
	giq_option(4, SCRIPT_JUSTIN, 121, Justin11, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JUSTIN, 122, Justin12, NEUTRAL_REACTION);
end

procedure Justin11
begin
	variable LVar0 := 0;
	set_local_var(6, 1);
	LVar0 := item_caps_adjust(dude_obj, 300);
	gsay_message(SCRIPT_JUSTIN, 123, NEUTRAL_REACTION);
	gsay_reply(SCRIPT_JUSTIN, 174);
	giq_option(4, SCRIPT_JUSTIN, 124, Justin20, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JUSTIN, 125, JustinEnd, NEUTRAL_REACTION);
end

procedure Justin12
begin
	set_local_var(6, 2);
	gsay_reply(SCRIPT_JUSTIN, 126);
	giq_option(4, SCRIPT_JUSTIN, 127, Justin13, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JUSTIN, 128, Justin14, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JUSTIN, 129, JustinEnd, NEUTRAL_REACTION);
end

procedure Justin13
begin
	variable LVar0 := 0;
	set_local_var(6, 1);
	LVar0 := item_caps_adjust(dude_obj, 300);
	gsay_message(SCRIPT_JUSTIN, 130, GOOD_REACTION);
	gsay_message(SCRIPT_JUSTIN, 175, NEUTRAL_REACTION);
end

procedure Justin14
begin
	gsay_message(SCRIPT_JUSTIN, 131, NEUTRAL_REACTION);
end

procedure Justin15
begin
	giq_option(4, SCRIPT_JUSTIN, 132, JustinCombat, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JUSTIN, 133, Justin15a, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JUSTIN, 134, Justin15b, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JUSTIN, 135, Justin15c, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JUSTIN, 136, Justin15d, NEUTRAL_REACTION);
end

procedure Justin15a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, 30))) then begin
		call Justin16;
	end
	else begin
		call Justin17;
	end
end

procedure Justin15b
begin
	if (is_success(roll_vs_skill(dude_obj, 14, 0))) then begin
		call Justin16;
	end
	else begin
		call Justin17;
	end
end

procedure Justin15c
begin
	if (is_success(roll_vs_skill(dude_obj, 14, 15))) then begin
		call Justin16;
	end
	else begin
		call Justin17;
	end
end

procedure Justin15d
begin
	if (is_success(roll_vs_skill(dude_obj, 14, -50))) then begin
		call Justin18;
	end
	else begin
		call Justin19;
	end
end

procedure Justin16
begin
	gsay_reply(SCRIPT_JUSTIN, 137);
	call Justin09;
end

procedure Justin17
begin
	set_local_var(7, 1);
	gsay_message(SCRIPT_JUSTIN, 138, BAD_REACTION);
end

procedure Justin18
begin
	gsay_reply(SCRIPT_JUSTIN, 139);
	call Justin09;
end

procedure Justin19
begin
	set_local_var(7, 1);
	gsay_message(SCRIPT_JUSTIN, 140, BAD_REACTION);
end

procedure Justin20
begin
	gsay_message(SCRIPT_JUSTIN, 141, NEUTRAL_REACTION);
	call JustinTravel;
end

procedure Justin26
begin
	gsay_reply(SCRIPT_JUSTIN, 151);
	giq_option(4, SCRIPT_JUSTIN, 152, JustinTravel, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JUSTIN, 153, JustinEnd, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_JUSTIN, 154, Justin34, NEUTRAL_REACTION);
end

procedure Justin27
begin
	gsay_message(SCRIPT_JUSTIN, 155, GOOD_REACTION);
end

procedure Justin28
begin
	gsay_reply(SCRIPT_JUSTIN, 156);
	giq_option(4, SCRIPT_JUSTIN, 157, Justin11, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JUSTIN, 158, JustinEnd, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_JUSTIN, 159, Justin34, NEUTRAL_REACTION);
end

procedure Justin29
begin
	gsay_message(SCRIPT_JUSTIN, 160, NEUTRAL_REACTION);
end

procedure Justin30
begin
	giq_option(4, SCRIPT_JUSTIN, 161, Justin01, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JUSTIN, 162, Justin02, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JUSTIN, 163, JustinEnd, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_JUSTIN, 164, Justin34, NEUTRAL_REACTION);
end

procedure Justin31
begin
	if (dude_is_male) then begin
		gsay_reply(SCRIPT_JUSTIN, 165);
	end
	else begin
		gsay_reply(SCRIPT_JUSTIN, 166);
	end
	call Justin30;
end

procedure Justin32
begin
	gsay_reply(SCRIPT_JUSTIN, 167);
	call Justin30;
end

procedure Justin33
begin
	gsay_reply(SCRIPT_JUSTIN, 168);
	call Justin30;
end

procedure Justin34
begin
	gsay_message(SCRIPT_JUSTIN, 169, NEUTRAL_REACTION);
end

procedure Justin35
begin
	variable LVar0 := 0;
	LVar0 := item_caps_adjust(dude_obj, 1000);
	set_global_var(GVAR_PLAYER_REPUTATION, global_var(GVAR_PLAYER_REPUTATION) + 4);
	give_exp_points(1400);
	display_msg(message_str(SCRIPT_GENCHAT, 103) + 1400 + message_str(SCRIPT_GENCHAT, 104));
	gsay_message(SCRIPT_JUSTIN, 172, GOOD_REACTION);
	gsay_message(SCRIPT_JUSTIN, 173, GOOD_REACTION);
end

procedure JustinEnd
begin
end

procedure JustinCombat
begin
	call combat;
end

procedure JustinTravel
begin
	travel := 1;
end

procedure GoToDecker
begin
	set_map_var(52, 1);
	gfade_out(1000);
	set_obj_visibility(Fry_Stub_Ptr, 0);
	move_to(self_obj, 23926, 1);
	anim(self_obj, 1000, rotation_to_tile(tile_num(self_obj), tile_num(Decker_Ptr)));
	critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 0);
	move_to(Kane_Ptr, 22526, 1);
	anim(Kane_Ptr, 1000, rotation_to_tile(tile_num(Kane_Ptr), tile_num(Decker_Ptr)));
	move_to(dude_obj, 23924, 1);
	anim(dude_obj, 1000, rotation_to_tile(tile_num(dude_obj), tile_num(Decker_Ptr)));
	gfade_in(1000);
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

procedure Justin22
begin
end