#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 talk_p_proc;
procedure Scout0;
procedure Scout1;
procedure Scout2;
procedure Scout3;
procedure Scout4;
procedure Scout5;
procedure Scout6;
procedure Scout7;
procedure Scout8;
procedure Scout9;
procedure Scout10;
procedure Scout11;
procedure Scout12;
procedure Scout13;
procedure Scout14;
procedure Scout15;
procedure Scout16;
procedure Scout16a;
procedure Scout17;
procedure Scout18;
procedure Scout19;
procedure Scout20;
procedure Scout21;
procedure Scout22;
procedure Scout23;
procedure combat;
procedure ScoutEnd;
procedure flee_dude;

variable hostile1;
variable initialized1;

import variable removal_ptr;

procedure damage_p_proc;


procedure start
begin
	if (not(initialized1)) then begin
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 46);
		if (global_var(GVAR_DESTROY_FOLLOWERS) == 2) then begin
			if (random(0, 1)) then begin
				kill_critter(self_obj, 59);
			end
			else begin
				kill_critter(self_obj, 57);
			end
		end
		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 == talk_proc) then begin
						call talk_p_proc;
					end
				end
			end
		end
	end
end

procedure critter_p_proc
begin
	if (global_var(GVAR_DESTROY_FOLLOWERS) == 2) then begin
		removal_ptr := self_obj;
	end
	if (hostile1) then begin
		hostile1 := 0;
		attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
	end
	else begin
		if (local_var(1) or global_var(GVAR_ENEMY_FOLLOWERS)) then begin
			if (tile_distance_objs(self_obj, dude_obj) < 8) then begin
				call flee_dude;
			end
		end
	end
end

procedure destroy_p_proc
begin
	if (source_obj == dude_obj) then begin
		set_global_var(GVAR_ENEMY_FOLLOWERS, 1);
	end
	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
	if (local_var(0) == 0) then begin
		if (get_critter_stat(self_obj, 34) == 0) then begin
			set_local_var(0, random(100, 104));
		end
		else begin
			set_local_var(0, random(105, 109));
		end
	end
	script_overrides;
	display_msg(message_str(SCRIPT_FOLSCOUT, local_var(0)));
end

procedure talk_p_proc
begin
	script_overrides;
	if (local_var(1) or global_var(GVAR_ENEMY_FOLLOWERS)) then begin
		float_msg(self_obj, message_str(SCRIPT_ENEMY, random(100, 105)), FLOAT_MSG_NORMAL);
	end
	else begin
		start_gdialog(SCRIPT_FOLSCOUT, self_obj, 4, -1, -1);
		gsay_start;
		if (global_var(GVAR_FIND_AGENT) == 1) then begin
			call Scout16;
		end
		else begin
			if (global_var(GVAR_FIND_AGENT) == 2) then begin
				call Scout22;
			end
			else begin
				call Scout0;
			end
		end
		gsay_end;
		end_dialogue;
	end
end

procedure Scout0
begin
	gsay_reply(SCRIPT_FOLSCOUT, 110);
	giq_option(-3, SCRIPT_FOLSCOUT, 111, Scout1, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FOLSCOUT, message_str(SCRIPT_FOLSCOUT, 112) + proto_data(obj_pid(dude_obj), 1) + message_str(SCRIPT_FOLSCOUT, 113), Scout4, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FOLSCOUT, message_str(SCRIPT_FOLSCOUT, 114) + proto_data(obj_pid(dude_obj), 1) + message_str(SCRIPT_FOLSCOUT, 115), combat, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_FOLSCOUT, 116, Scout11, NEUTRAL_REACTION);
end

procedure Scout1
begin
	gsay_reply(SCRIPT_FOLSCOUT, 117);
	giq_option(-3, SCRIPT_FOLSCOUT, message_str(SCRIPT_FOLSCOUT, 118) + proto_data(obj_pid(dude_obj), 1) + message_str(SCRIPT_FOLSCOUT, 119), combat, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_FOLSCOUT, message_str(SCRIPT_FOLSCOUT, 120) + proto_data(obj_pid(dude_obj), 1) + message_str(SCRIPT_FOLSCOUT, 121), Scout2, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_FOLSCOUT, message_str(SCRIPT_FOLSCOUT, 122), Scout3, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_FOLSCOUT, message_str(SCRIPT_FOLSCOUT, 123) + proto_data(obj_pid(dude_obj), 1) + message_str(SCRIPT_FOLSCOUT, 124), Scout3, NEUTRAL_REACTION);
end

procedure Scout2
begin
	gsay_message(SCRIPT_FOLSCOUT, 125, NEUTRAL_REACTION);
end

procedure Scout3
begin
	gsay_message(SCRIPT_FOLSCOUT, 126, NEUTRAL_REACTION);
end

procedure Scout4
begin
	gsay_reply(SCRIPT_FOLSCOUT, 127);
	giq_option(4, SCRIPT_FOLSCOUT, 128, Scout5, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FOLSCOUT, 129, combat, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FOLSCOUT, 130, Scout6, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FOLSCOUT, 131, Scout10, NEUTRAL_REACTION);
end

procedure Scout5
begin
	gsay_message(SCRIPT_FOLSCOUT, 132, NEUTRAL_REACTION);
end

procedure Scout6
begin
	gsay_reply(SCRIPT_FOLSCOUT, 133);
	giq_option(4, SCRIPT_FOLSCOUT, 134, Scout5, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FOLSCOUT, 135, Scout7, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FOLSCOUT, 136, Scout9, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FOLSCOUT, 137, Scout8, NEUTRAL_REACTION);
end

procedure Scout7
begin
	gsay_reply(SCRIPT_FOLSCOUT, 138);
	giq_option(4, SCRIPT_FOLSCOUT, 139, Scout5, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FOLSCOUT, 140, Scout8, NEUTRAL_REACTION);
end

procedure Scout8
begin
	gsay_message(SCRIPT_FOLSCOUT, 141, NEUTRAL_REACTION);
end

procedure Scout9
begin
	gsay_reply(SCRIPT_FOLSCOUT, 142);
	giq_option(4, SCRIPT_FOLSCOUT, 143, ScoutEnd, NEUTRAL_REACTION);
end

procedure Scout10
begin
	gsay_reply(SCRIPT_FOLSCOUT, 144);
	giq_option(4, SCRIPT_FOLSCOUT, 145, Scout5, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FOLSCOUT, 146, Scout8, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FOLSCOUT, 147, Scout6, NEUTRAL_REACTION);
end

procedure Scout11
begin
	gsay_reply(SCRIPT_FOLSCOUT, 148);
	giq_option(6, SCRIPT_FOLSCOUT, 149, ScoutEnd, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_FOLSCOUT, 150, Scout12, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_FOLSCOUT, 151, Scout15, NEUTRAL_REACTION);
end

procedure Scout12
begin
	gsay_reply(SCRIPT_FOLSCOUT, 152);
	giq_option(6, SCRIPT_FOLSCOUT, 153, ScoutEnd, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_FOLSCOUT, 154, Scout13, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_FOLSCOUT, 155, Scout14, NEUTRAL_REACTION);
end

procedure Scout13
begin
	gsay_reply(SCRIPT_FOLSCOUT, 156);
	giq_option(6, SCRIPT_FOLSCOUT, 157, ScoutEnd, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_FOLSCOUT, 158, combat, NEUTRAL_REACTION);
end

procedure Scout14
begin
	gsay_message(SCRIPT_FOLSCOUT, 159, NEUTRAL_REACTION);
end

procedure Scout15
begin
	gsay_reply(SCRIPT_FOLSCOUT, 160);
	giq_option(6, SCRIPT_FOLSCOUT, 161, ScoutEnd, NEUTRAL_REACTION);
end

procedure Scout16
begin
	gsay_reply(SCRIPT_FOLSCOUT, 162);
	giq_option(4, SCRIPT_FOLSCOUT, 163, Scout17, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FOLSCOUT, 164, Scout19, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_FOLSCOUT, 165, Scout16a, NEUTRAL_REACTION);
end

procedure Scout16a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, -10))) then begin
		call Scout20;
	end
	else begin
		call Scout21;
	end
end

procedure Scout17
begin
	gsay_reply(SCRIPT_FOLSCOUT, 166);
	giq_option(4, SCRIPT_FOLSCOUT, 167, Scout18, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FOLSCOUT, 168, ScoutEnd, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FOLSCOUT, 169, combat, NEUTRAL_REACTION);
end

procedure Scout18
begin
	gsay_reply(SCRIPT_FOLSCOUT, 170);
	giq_option(4, SCRIPT_FOLSCOUT, 171, ScoutEnd, NEUTRAL_REACTION);
end

procedure Scout19
begin
	gsay_message(SCRIPT_FOLSCOUT, 172, NEUTRAL_REACTION);
	call combat;
end

procedure Scout20
begin
	gsay_message(SCRIPT_FOLSCOUT, 173, NEUTRAL_REACTION);
end

procedure Scout21
begin
	gsay_message(SCRIPT_FOLSCOUT, 174, NEUTRAL_REACTION);
	call combat;
end

procedure Scout22
begin
	gsay_reply(SCRIPT_FOLSCOUT, 175);
	giq_option(4, SCRIPT_FOLSCOUT, 176, ScoutEnd, NEUTRAL_REACTION);
	if (global_var(GVAR_TRAIN_FOLLOWERS) == 0) then begin
		giq_option(4, SCRIPT_FOLSCOUT, 177, Scout23, NEUTRAL_REACTION);
	end
end

procedure Scout23
begin
	variable LVar0 := 0;
	LVar0 := 4 * 3600;
	set_global_var(GVAR_TRAIN_FOLLOWERS, 1);
	gsay_reply(SCRIPT_FOLSCOUT, 178);
	game_time_advance(game_ticks(LVar0));
	gfade_out(400);
	gfade_in(400);
	giq_option(4, SCRIPT_FOLSCOUT, 179, ScoutEnd, NEUTRAL_REACTION);
end

procedure combat
begin
	hostile1 := 1;
end

procedure ScoutEnd
begin
end

procedure flee_dude
begin
	variable LVar0 := 0;
	variable LVar1 := 0;
	variable LVar2 := 0;
	while(LVar1 < 5) do begin
		if (tile_distance(tile_num(dude_obj), tile_num_in_direction(tile_num(self_obj), LVar1, 3)) > LVar2) then begin
			LVar0 := tile_num_in_direction(tile_num(self_obj), LVar1, 3);
			LVar2 := tile_distance(tile_num(dude_obj), LVar0);
		end
		LVar1 := LVar1 + 1;
	end
	animate_move_obj_to_tile(self_obj, LVar0, ANIMATE_RUN);
end

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