#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 float_line0;
procedure float_line1;
procedure float_line2;
procedure flee_dude;

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) == 0) 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 (global_var(GVAR_DESTROY_FOLLOWERS) == 2) then begin
		removal_ptr := self_obj;
	end
	if (local_var(1) or global_var(GVAR_ENEMY_FOLLOWERS)) then begin
		if (tile_distance_objs(dude_obj, self_obj) < 8) then begin
			call flee_dude;
		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
	script_overrides;
	display_msg(message_str(SCRIPT_FOLINIT, 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
		if (global_var(GVAR_FIND_AGENT) == 1) then begin
			call float_line1;
		end
		else begin
			if (global_var(GVAR_TRAIN_FOLLOWERS) == 1) then begin
				call float_line2;
			end
			else begin
				call float_line0;
			end
		end
	end
end

procedure float_line0
begin
	float_msg(self_obj, message_str(SCRIPT_FOLINIT, random(110, 117)), FLOAT_MSG_NORMAL);
end

procedure float_line1
begin
	float_msg(self_obj, message_str(SCRIPT_FOLINIT, random(118, 122)), FLOAT_MSG_NORMAL);
end

procedure float_line2
begin
	float_msg(self_obj, message_str(SCRIPT_FOLINIT, random(123, 126)), FLOAT_MSG_NORMAL);
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