#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure mutant00;
procedure combat;
procedure timetomove;
procedure Critter_Action;

variable hostile1;
variable init_teams;
variable loopcount;
variable noevent;
variable new_tile;
variable maxleash := 9;
variable rndx;


procedure start
begin
	if (not(init_teams)) then begin
		if (local_var(0) == 0) then begin
			set_local_var(0, tile_num(self_obj));
		end
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 34);
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_SUPER_MUTANT_NECRO);
		init_teams := 1;
	end
	else begin
		if (script_action == timed_event_proc) then begin
			call timetomove;
		end
	end
	if (script_action == talk_proc) then begin
		call mutant00;
	end
	else begin
		if (script_action == look_at_proc) then begin
			script_overrides;
			display_msg(message_str(SCRIPT_INVADER, 100));
		end
		else begin
			if (script_action == pickup_proc) then begin
				hostile1 := 1;
			end
			else begin
				if (script_action == critter_proc) then begin
					call Critter_Action;
				end
				else begin
					if (script_action == destroy_proc) 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
			end
		end
	end
end

procedure mutant00
begin
	rndx := random(0, 1);
	if (rndx == 0) then begin
		float_msg(self_obj, message_str(SCRIPT_INVADER, 101), FLOAT_MSG_RED);
	end
	else begin
		if (rndx == 1) then begin
			float_msg(self_obj, message_str(SCRIPT_INVADER, 102), FLOAT_MSG_RED);
		end
	end
	call combat;
end

procedure combat
begin
	hostile1 := 1;
end

procedure timetomove
begin
	noevent := 0;
	set_local_var(1, tile_num(self_obj));
	new_tile := tile_num_in_direction(local_var(1), random(0, 5), 2);
	if (tile_distance(local_var(0), new_tile) < maxleash) then begin
		animate_move_obj_to_tile(self_obj, new_tile, ANIMATE_WALK);
	end
end

procedure Critter_Action
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) and (tile_distance_objs(self_obj, dude_obj) < 6)) then begin
			call mutant00;
		end
		else begin
			loopcount := loopcount + 1;
			if (loopcount > 40) then begin
				loopcount := 0;
				if (noevent == 0) then begin
					noevent := 1;
					add_timer_event(self_obj, game_ticks(random(5, 13)), 0);
				end
			end
		end
	end
end