#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 map_update_p_proc;
procedure pickup_p_proc;
procedure talk_p_proc;
procedure timed_event_p_proc;
procedure Jake00;
procedure Jake01;
procedure Jake02;
procedure Jake03;
procedure Jake04;
procedure Jake05;
procedure Jake06;
procedure Jake07;
procedure Jake08;
procedure Jake09;
procedure Jake10;
procedure Jake11;
procedure Jake12;
procedure Jake13;
procedure Jake14;
procedure Jake15;
procedure Jake16;
procedure Jake17;
procedure Jake18;
procedure JakeCombat;
procedure JakeEnd;

variable hostile1;
variable initialized1;


procedure start
begin
	if (not(initialized1)) then begin
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_FOLLOWER_LEADER);
		initialized1 := 1;
	end
	else begin
		if (script_action == critter_proc) then begin
			call critter_p_proc;
		end
		else begin
			if (script_action == timed_event_proc) then begin
				call timed_event_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 == map_update_proc) then begin
							call map_update_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
	end
end

procedure critter_p_proc
begin
	if (obj_can_see_obj(self_obj, dude_obj) and (map_var(1) == 1)) then begin
		float_msg(self_obj, message_str(SCRIPT_JAKE, 143), FLOAT_MSG_GREEN);
		animate_move_obj_to_tile(self_obj, 15484, ANIMATE_WALK);
		add_timer_event(self_obj, game_ticks(15), 1);
	end
	if (obj_can_see_obj(self_obj, dude_obj) and (map_var(1) == 2)) then begin
		float_msg(self_obj, message_str(SCRIPT_JAKE, 144), FLOAT_MSG_GREEN);
		hostile1 := 1;
	end
	if (hostile1) then begin
		hostile1 := 0;
		attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
	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_BAD_MONSTER, global_var(GVAR_BAD_MONSTER) + 1);
		if ((global_var(GVAR_BAD_MONSTER) % 6) == 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) == 1) then begin
		display_msg(message_str(SCRIPT_JAKE, 100));
	end
	else begin
		display_msg(message_str(SCRIPT_JAKE, 101));
	end
end

procedure map_update_p_proc
begin
	if (global_var(GVAR_DESTROY_FOLLOWERS) == 2) then begin
		kill_critter(self_obj, 57);
	end
end

procedure pickup_p_proc
begin
	hostile1 := 1;
end

procedure talk_p_proc
begin
	if (not(local_var(5))) then begin
		start_gdialog(SCRIPT_JAKE, self_obj, 4, -1, -1);
		gsay_start;
		set_local_var(4, 1);
		if ((game_time_hour >= 1900) or (game_time_hour < 600)) then begin
			call Jake14;
		end
		else begin
			if (global_var(GVAR_TRAIN_FOLLOWERS)) then begin
				call Jake17;
			end
			else begin
				if (global_var(GVAR_FIND_AGENT) == 2) then begin
					call Jake16;
				end
				else begin
					if (global_var(GVAR_FIND_AGENT) == 1) then begin
						call Jake15;
					end
					else begin
						call Jake00;
					end
				end
			end
		end
		gsay_end;
		end_dialogue;
	end
end

procedure timed_event_p_proc
begin
	float_msg(self_obj, message_str(SCRIPT_JAKE, 143), FLOAT_MSG_GREEN);
	set_map_var(1, 2);
end

procedure Jake00
begin
	gsay_reply(SCRIPT_JAKE, 102);
	giq_option(-3, SCRIPT_JAKE, 103, Jake01, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JAKE, 104, Jake02, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JAKE, 105, Jake12, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JAKE, 106, Jake13, NEUTRAL_REACTION);
	if ((global_var(GVAR_DESTROY_FOLLOWERS) == 1) and (obj_is_carrying_obj_pid(dude_obj, 72) != 0)) then begin
		giq_option(4, SCRIPT_JAKE, 141, Jake18, NEUTRAL_REACTION);
	end
end

procedure Jake01
begin
	gsay_message(SCRIPT_JAKE, 107, NEUTRAL_REACTION);
end

procedure Jake02
begin
	gsay_reply(SCRIPT_JAKE, 108);
	giq_option(4, SCRIPT_JAKE, 109, Jake03, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JAKE, 110, JakeEnd, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JAKE, 111, Jake04, NEUTRAL_REACTION);
end

procedure Jake03
begin
	gsay_reply(SCRIPT_JAKE, 112);
	giq_option(4, SCRIPT_JAKE, 113, JakeEnd, NEUTRAL_REACTION);
end

procedure Jake04
begin
	gsay_reply(SCRIPT_JAKE, 114);
	giq_option(4, SCRIPT_JAKE, 115, Jake05, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JAKE, 116, Jake06, NEUTRAL_REACTION);
end

procedure Jake05
begin
	gsay_message(SCRIPT_JAKE, 117, NEUTRAL_REACTION);
	set_local_var(5, 1);
end

procedure Jake06
begin
	gsay_reply(SCRIPT_JAKE, 118);
	giq_option(4, SCRIPT_JAKE, 119, Jake07, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JAKE, 120, Jake11, NEUTRAL_REACTION);
end

procedure Jake07
begin
	gsay_reply(SCRIPT_JAKE, 121);
	giq_option(4, SCRIPT_JAKE, 122, Jake08, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JAKE, 123, Jake09, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JAKE, 124, Jake10, NEUTRAL_REACTION);
end

procedure Jake08
begin
	set_global_var(GVAR_FIND_AGENT, 1);
	gsay_message(SCRIPT_JAKE, 125, NEUTRAL_REACTION);
end

procedure Jake09
begin
	gsay_reply(SCRIPT_JAKE, 126);
	giq_option(4, SCRIPT_JAKE, 127, Jake08, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JAKE, 128, Jake10, NEUTRAL_REACTION);
end

procedure Jake10
begin
	gsay_message(SCRIPT_JAKE, 129, NEUTRAL_REACTION);
	call JakeCombat;
end

procedure Jake11
begin
	gsay_message(SCRIPT_JAKE, 130, NEUTRAL_REACTION);
end

procedure Jake12
begin
	gsay_reply(SCRIPT_JAKE, 131);
	giq_option(4, SCRIPT_JAKE, 132, Jake07, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JAKE, 133, Jake08, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JAKE, 134, Jake09, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JAKE, 135, Jake10, NEUTRAL_REACTION);
end

procedure Jake13
begin
	gsay_message(SCRIPT_JAKE, 136, NEUTRAL_REACTION);
end

procedure Jake14
begin
	gsay_message(SCRIPT_JAKE, 137, NEUTRAL_REACTION);
end

procedure Jake15
begin
	gsay_message(SCRIPT_JAKE, 138, NEUTRAL_REACTION);
end

procedure Jake16
begin
	gsay_message(SCRIPT_JAKE, 139, NEUTRAL_REACTION);
end

procedure Jake17
begin
	gsay_message(SCRIPT_JAKE, 140, NEUTRAL_REACTION);
end

procedure Jake18
begin
	variable LVar0 := 0;
	LVar0 := obj_carrying_pid_obj(dude_obj, 72);
	rm_obj_from_inven(dude_obj, LVar0);
	destroy_object(LVar0);
	gsay_message(SCRIPT_JAKE, 142, NEUTRAL_REACTION);
end

procedure JakeCombat
begin
	hostile1 := 1;
end

procedure JakeEnd
begin
end