#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure critter_p_proc;
procedure destroy_p_proc;
procedure look_at_p_proc;
procedure pickup_p_proc;
procedure talk_p_proc;
procedure Jon01;
procedure Jon02;
procedure Jon03;
procedure Jon04;
procedure Jon05;
procedure Jon06;
procedure Jon07;
procedure Jon08;
procedure Jon09;
procedure Jon10;
procedure Jon11;
procedure Jon12;
procedure Jon13;
procedure Jon14;
procedure Jon15;
procedure Jon16;
procedure Jon17;
procedure JonQuest;
procedure JonCombat;
procedure JonEnd;

variable dinner;
variable hostile1;
variable initialized1;
variable item;
variable taking_outside;

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 description_p_proc;


procedure start
begin
	if (not(initialized1)) then begin
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 49);
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_ADYTUM_LEADER);
		initialized1 := 1;
	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 == 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
					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 (obj_can_see_obj(self_obj, dude_obj)) then begin
			if (local_var(4) == 0) then begin
				dialogue_system_enter;
			end
		end
	end
end

procedure destroy_p_proc
begin
	if (source_obj == dude_obj) then begin
		set_global_var(GVAR_GOOD_MONSTER, global_var(GVAR_GOOD_MONSTER) + 1);
		if ((global_var(GVAR_GOOD_MONSTER) % 7) == 0) then begin
			set_global_var(GVAR_PLAYER_REPUTATION, global_var(GVAR_PLAYER_REPUTATION) - 1);
		end
	end
end

procedure look_at_p_proc
begin
	script_overrides;
	if (local_var(4)) then begin
		display_msg(message_str(SCRIPT_JON, 100));
	end
	else begin
		display_msg(message_str(SCRIPT_JON, 101));
	end
end

procedure pickup_p_proc
begin
	hostile1 := 1;
end

procedure talk_p_proc
begin
	call Get_Reaction;
	start_gdialog(SCRIPT_JON, self_obj, 4, -1, -1);
	gsay_start;
	if ((global_var(GVAR_FIX_FARM) == 2) and not(local_var(5))) then begin
		call Jon12;
	end
	else begin
		if (global_var(GVAR_JASON_STATUS) == 1) then begin
			call Jon14;
		end
		else begin
			if (global_var(GVAR_GANG_WAR) == 1) then begin
				call Jon09;
			end
			else begin
				if ((global_var(GVAR_GANG_WAR) == 2) and (local_var(8) == 0)) then begin
					call Jon16;
				end
				else begin
					if (local_var(4)) then begin
						call Jon13;
					end
					else begin
						if (local_var(1) < 2) then begin
							call Jon08;
						end
						else begin
							call Jon01;
						end
					end
				end
			end
		end
	end
	gsay_end;
	end_dialogue;
	if (taking_outside) then begin
		gfade_out(600);
		move_to(dude_obj, 10700, 0);
		gfade_in(600);
		taking_outside := 0;
	end
	if (local_var(8) == 1) then begin
		set_local_var(8, 2);
		gfade_out(600);
		game_time_advance(game_ticks(3600 * (1800 - game_time_hour)));
		critter_heal(dude_obj, 5);
		display_msg(message_str(SCRIPT_JON, 151));
		gfade_in(600);
	end
end

procedure Jon01
begin
	set_local_var(4, 1);
	gsay_reply(SCRIPT_JON, 103);
	giq_option(4, SCRIPT_JON, 104, Jon04, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JON, 105, Jon03, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JON, 106, Jon02, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_JON, 107, Jon06, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JON, 108, Jon05, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_JON, 109, Jon07, NEUTRAL_REACTION);
end

procedure Jon02
begin
	gsay_reply(SCRIPT_JON, 110);
	giq_option(4, SCRIPT_JON, 111, Jon04, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JON, 112, Jon03, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JON, 113, Jon05, NEUTRAL_REACTION);
end

procedure Jon03
begin
	gsay_reply(SCRIPT_JON, 114);
	giq_option(4, SCRIPT_JON, 115, Jon04, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JON, 116, Jon05, NEUTRAL_REACTION);
end

procedure Jon04
begin
	gsay_message(SCRIPT_JON, 117, NEUTRAL_REACTION);
	gsay_message(SCRIPT_JON, 118, NEUTRAL_REACTION);
	call JonQuest;
end

procedure Jon05
begin
	gsay_message(SCRIPT_JON, 119, NEUTRAL_REACTION);
	call JonQuest;
end

procedure Jon06
begin
	gsay_reply(SCRIPT_JON, 120);
	call Down_React;
	giq_option(4, SCRIPT_JON, 121, Jon02, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JON, 122, Jon05, NEUTRAL_REACTION);
end

procedure Jon07
begin
	gsay_reply(SCRIPT_JON, 123);
	giq_option(-3, SCRIPT_JON, 124, JonQuest, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_JON, 125, Jon05, NEUTRAL_REACTION);
end

procedure Jon08
begin
	gsay_reply(SCRIPT_JON, 126);
	giq_option(4, SCRIPT_JON, 127, Jon02, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JON, 128, Jon05, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JON, 129, JonCombat, NEUTRAL_REACTION);
end

procedure Jon09
begin
	gsay_reply(SCRIPT_JON, 130);
	if (local_var(7) == 0) then begin
		giq_option(4, SCRIPT_JON, 131, Jon10, NEUTRAL_REACTION);
	end
	giq_option(4, SCRIPT_JON, 132, Jon11, NEUTRAL_REACTION);
	if (local_var(7) == 0) then begin
		giq_option(-3, SCRIPT_JON, 133, Jon10, NEUTRAL_REACTION);
	end
	giq_option(-3, SCRIPT_JON, 134, Jon11, NEUTRAL_REACTION);
end

procedure Jon10
begin
	variable LVar0 := 0;
	if (obj_is_carrying_obj_pid(dude_obj, 116) and obj_is_carrying_obj_pid(dude_obj, 114)) then begin
		set_local_var(7, 1);
		LVar0 := create_object_sid(23, 0, 0, -1);
		add_obj_to_inven(dude_obj, LVar0);
		LVar0 := create_object_sid(36, 0, 0, -1);
		add_mult_objs_to_inven(dude_obj, LVar0, 3);
		gsay_message(SCRIPT_JON, 135, NEUTRAL_REACTION);
		call Up_React;
	end
	else begin
		gsay_message(SCRIPT_JON, 136, NEUTRAL_REACTION);
	end
end

procedure Jon11
begin
	gsay_message(SCRIPT_JON, 137, NEUTRAL_REACTION);
end

procedure Jon12
begin
	item_caps_adjust(dude_obj, 500);
	set_local_var(5, 1);
	gsay_message(SCRIPT_JON, message_str(SCRIPT_JON, 138) + " " + message_str(SCRIPT_JON, 139), NEUTRAL_REACTION);
	call Up_React;
end

procedure Jon13
begin
	if (local_var(1) == 1) then begin
		gsay_reply(SCRIPT_JON, 140);
	end
	else begin
		gsay_reply(SCRIPT_JON, 141);
	end
	giq_option(-3, SCRIPT_JON, 142, JonEnd, NEUTRAL_REACTION);
	if (local_var(6) == 0) then begin
		giq_option(4, SCRIPT_JON, 144, Jon15, NEUTRAL_REACTION);
	end
	giq_option(4, SCRIPT_JON, 145, Jon16, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JON, 146, JonEnd, NEUTRAL_REACTION);
end

procedure Jon14
begin
	gsay_message(SCRIPT_JON, 143, NEUTRAL_REACTION);
end

procedure Jon15
begin
	set_local_var(6, 1);
	if (local_var(1) == 1) then begin
		gsay_message(SCRIPT_JON, 148, NEUTRAL_REACTION);
	end
	else begin
		item := create_object_sid(35, 0, 0, -1);
		add_mult_objs_to_inven(dude_obj, item, 2);
		item := create_object_sid(29, 0, 0, -1);
		add_mult_objs_to_inven(dude_obj, item, 2);
		item := create_object_sid(40, 0, 0, -1);
		add_mult_objs_to_inven(dude_obj, item, 2);
		item := create_object_sid(125, 0, 0, -1);
		add_obj_to_inven(dude_obj, item);
		gsay_message(SCRIPT_JON, 147, GOOD_REACTION);
	end
end

procedure Jon16
begin
	if (local_var(1) == 3) then begin
		gsay_reply(SCRIPT_JON, 150);
		giq_option(0, SCRIPT_MODREACT, 152, Jon17, GOOD_REACTION);
	end
	else begin
		gsay_message(SCRIPT_JON, 149, NEUTRAL_REACTION);
	end
end

procedure Jon17
begin
	set_local_var(8, 1);
end

procedure JonQuest
begin
	set_global_var(GVAR_GANG_WAR, 1);
	taking_outside := 1;
end

procedure JonCombat
begin
	hostile1 := 1;
end

procedure JonEnd
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)) > 34) 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)) > 34) 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 description_p_proc
begin
	script_overrides;
	display_msg(message_str(SCRIPT_JON, 102));
end