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

variable hostile1;
variable initialized1;
variable creation_pid;
variable critter_tile;


procedure start
begin
	if (not(initialized1) and metarule(14, 0)) then begin
		creation_pid := obj_pid(self_obj);
		if (creation_pid == 16777261) then begin
			critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_FLOATER);
			critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 43);
			debug_msg(" Init Floater.");
		end
		else begin
			if (creation_pid == 16777259) then begin
				critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_CENTAUR);
				critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 43);
				debug_msg(" Init Centaur.");
			end
			else begin
				if (creation_pid == 16777230) then begin
					critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_GHOUL_BRAVE);
					critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 43);
					debug_msg(" Init ghoul.");
				end
				else begin
					critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, -1);
					critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 43);
					debug_msg(" Init defualt.");
				end
			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
		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) or (tile_distance_objs(dude_obj, self_obj) <= 4)) then begin
			hostile1 := 1;
		end
	end
end

procedure destroy_p_proc
begin
	if (source_obj == dude_obj) then 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
end