#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure critter_p_proc;
procedure damage_p_proc;
procedure destroy_p_proc;
procedure look_at_p_proc;
procedure talk_p_proc;
procedure guard0;
procedure guard1;
procedure guard2;
procedure guard3;
procedure guard4;
procedure guard5;
procedure guard6;
procedure guard7;
procedure guard8;
procedure guard9;
procedure guard10;
procedure guard11;
procedure guard12;
procedure guard13;
procedure guard14;
procedure guard15;
procedure guard16;
procedure guard16a;
procedure guard17;
procedure guard18;
procedure guard19;
procedure guard20;
procedure guard21;
procedure guard22;
procedure guard23;
procedure combat;
procedure guardend;

variable hostile1;
variable initialized1;

import variable removal_ptr;


procedure start
begin
	if (not(initialized1)) then begin
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 46);
		if (local_var(0) == 0) then begin
			if (get_critter_stat(self_obj, 34) == 1) then begin
				set_local_var(0, random(100, 104));
			end
			else begin
				set_local_var(0, random(105, 109));
			end
		end
		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 == damage_proc) then begin
				call damage_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
end

procedure critter_p_proc
begin
	if (map_var(1) == 1) then begin
		animate_move_obj_to_tile(self_obj, tile_num_in_direction(tile_num(dude_obj), random(0, 6), random(3, 7)), ANIMATE_WALK);
	end
	else begin
		if ((map_var(1) == 2) and obj_can_see_obj(self_obj, dude_obj)) then begin
			hostile1 := 1;
		end
	end
	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 (tile_distance_objs(self_obj, dude_obj) < 8) then begin
			if (local_var(1) or global_var(GVAR_ENEMY_FOLLOWERS)) then begin
				hostile1 := 1;
			end
		end
	end
end

procedure damage_p_proc
begin
	if (source_obj == dude_obj) then begin
		set_local_var(1, 1);
	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
		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
end

procedure look_at_p_proc
begin
	display_msg(message_str(SCRIPT_FOLGUARD, 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_FOLGUARD, self_obj, 4, -1, -1);
		gsay_start;
		if (global_var(GVAR_FIND_AGENT) == 1) then begin
			call guard16;
		end
		else begin
			if (global_var(GVAR_FIND_AGENT) == 2) then begin
				call guard22;
			end
			else begin
				call guard0;
			end
		end
		gsay_end;
		end_dialogue;
	end
end

procedure guard0
begin
	gsay_reply(SCRIPT_FOLGUARD, 110);
	giq_option(-3, SCRIPT_FOLGUARD, 111, guard1, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FOLGUARD, message_str(SCRIPT_FOLGUARD, 112) + proto_data(obj_pid(dude_obj), 1) + message_str(SCRIPT_FOLGUARD, 113), guard4, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FOLGUARD, message_str(SCRIPT_FOLGUARD, 114) + proto_data(obj_pid(dude_obj), 1) + message_str(SCRIPT_FOLGUARD, 115), combat, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_FOLGUARD, 116, guard11, NEUTRAL_REACTION);
end

procedure guard1
begin
	gsay_reply(SCRIPT_FOLGUARD, 117);
	giq_option(-3, SCRIPT_FOLGUARD, message_str(SCRIPT_FOLGUARD, 118) + proto_data(obj_pid(dude_obj), 1) + message_str(SCRIPT_FOLGUARD, 119), combat, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_FOLGUARD, message_str(SCRIPT_FOLGUARD, 120) + proto_data(obj_pid(dude_obj), 1) + message_str(SCRIPT_FOLGUARD, 121), guard2, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_FOLGUARD, message_str(SCRIPT_FOLGUARD, 122), guard3, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_FOLGUARD, message_str(SCRIPT_FOLGUARD, 123) + proto_data(obj_pid(dude_obj), 1) + message_str(SCRIPT_FOLGUARD, 124), guard3, NEUTRAL_REACTION);
end

procedure guard2
begin
	gsay_message(SCRIPT_FOLGUARD, 125, NEUTRAL_REACTION);
end

procedure guard3
begin
	gsay_message(SCRIPT_FOLGUARD, 126, NEUTRAL_REACTION);
end

procedure guard4
begin
	gsay_reply(SCRIPT_FOLGUARD, 127);
	giq_option(4, SCRIPT_FOLGUARD, 128, guard5, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FOLGUARD, 129, combat, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FOLGUARD, 130, guard6, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FOLGUARD, 131, guard10, NEUTRAL_REACTION);
end

procedure guard5
begin
	gsay_message(SCRIPT_FOLGUARD, 132, NEUTRAL_REACTION);
end

procedure guard6
begin
	gsay_reply(SCRIPT_FOLGUARD, 133);
	giq_option(4, SCRIPT_FOLGUARD, 134, guard5, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FOLGUARD, 135, guard7, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FOLGUARD, 136, guard9, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FOLGUARD, 137, guard8, NEUTRAL_REACTION);
end

procedure guard7
begin
	gsay_reply(SCRIPT_FOLGUARD, 138);
	giq_option(4, SCRIPT_FOLGUARD, 139, guard5, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FOLGUARD, 140, guard8, NEUTRAL_REACTION);
end

procedure guard8
begin
	gsay_message(SCRIPT_FOLGUARD, 141, NEUTRAL_REACTION);
end

procedure guard9
begin
	gsay_reply(SCRIPT_FOLGUARD, 142);
	giq_option(4, SCRIPT_FOLGUARD, 143, guardend, NEUTRAL_REACTION);
end

procedure guard10
begin
	gsay_reply(SCRIPT_FOLGUARD, 144);
	giq_option(4, SCRIPT_FOLGUARD, 145, guard5, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FOLGUARD, 146, guard8, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FOLGUARD, 147, guard6, NEUTRAL_REACTION);
end

procedure guard11
begin
	gsay_reply(SCRIPT_FOLGUARD, 148);
	giq_option(6, SCRIPT_FOLGUARD, 149, guardend, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_FOLGUARD, 150, guard12, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_FOLGUARD, 151, guard15, NEUTRAL_REACTION);
end

procedure guard12
begin
	gsay_reply(SCRIPT_FOLGUARD, 152);
	giq_option(6, SCRIPT_FOLGUARD, 153, guardend, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_FOLGUARD, 154, guard13, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_FOLGUARD, 155, guard14, NEUTRAL_REACTION);
end

procedure guard13
begin
	gsay_reply(SCRIPT_FOLGUARD, 156);
	giq_option(6, SCRIPT_FOLGUARD, 157, guardend, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_FOLGUARD, 158, combat, NEUTRAL_REACTION);
end

procedure guard14
begin
	gsay_message(SCRIPT_FOLGUARD, 159, NEUTRAL_REACTION);
end

procedure guard15
begin
	gsay_reply(SCRIPT_FOLGUARD, 160);
	giq_option(6, SCRIPT_FOLGUARD, 161, guardend, NEUTRAL_REACTION);
end

procedure guard16
begin
	gsay_reply(SCRIPT_FOLGUARD, 162);
	giq_option(4, SCRIPT_FOLGUARD, 163, guard17, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FOLGUARD, 164, guard19, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_FOLGUARD, 165, guard16a, NEUTRAL_REACTION);
end

procedure guard16a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, -10))) then begin
		call guard20;
	end
	else begin
		call guard21;
	end
end

procedure guard17
begin
	gsay_reply(SCRIPT_FOLGUARD, 166);
	giq_option(4, SCRIPT_FOLGUARD, 167, guard18, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FOLGUARD, 168, guardend, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FOLGUARD, 169, combat, NEUTRAL_REACTION);
end

procedure guard18
begin
	gsay_reply(SCRIPT_FOLGUARD, 170);
	giq_option(4, SCRIPT_FOLGUARD, 171, guardend, NEUTRAL_REACTION);
end

procedure guard19
begin
	gsay_message(SCRIPT_FOLGUARD, 172, NEUTRAL_REACTION);
	call combat;
end

procedure guard20
begin
	gsay_message(SCRIPT_FOLGUARD, 173, NEUTRAL_REACTION);
end

procedure guard21
begin
	gsay_message(SCRIPT_FOLGUARD, 174, NEUTRAL_REACTION);
	call combat;
end

procedure guard22
begin
	gsay_reply(SCRIPT_FOLGUARD, 175);
	giq_option(4, SCRIPT_FOLGUARD, 176, guardend, NEUTRAL_REACTION);
	if (global_var(GVAR_TRAIN_FOLLOWERS) == 0) then begin
		giq_option(4, SCRIPT_FOLGUARD, 177, guard23, NEUTRAL_REACTION);
	end
end

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

procedure combat
begin
	hostile1 := 1;
end

procedure guardend
begin
end