#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure do_dialogue;
procedure timeforwhat;
procedure genericend;
procedure genericcbt;

variable Hostile;
variable init_teams;
variable noevent;
variable loopcount;
variable rndx;
variable rndy;
variable rndz;
variable dist;
variable time;
variable maxsight;
variable maxleash := 9;
variable new_tile;


procedure start
begin
	if (not(init_teams)) then begin
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 34);
		init_teams := 1;
	end
	if (script_action == timed_event_proc) then begin
		call timeforwhat;
	end
	else begin
		if (local_var(2) == 0) then begin
			set_local_var(2, tile_num(self_obj));
		end
		if (script_action == talk_proc) then begin
			call do_dialogue;
		end
		else begin
			if (script_action == pickup_proc) then begin
				Hostile := 1;
			end
			else begin
				if (script_action == critter_proc) then begin
					if (global_var(GVAR_NECROPOLIS_INVADED) == 0) then begin
						set_obj_visibility(self_obj, 1);
					end
					else begin
						maxsight := get_critter_stat(self_obj, 1);
						time := game_time_hour;
						if ((time > 2300) or (time < 600)) then begin
							maxsight := 4;
						end
						if ((tile_distance_objs(self_obj, dude_obj) <= maxsight) and obj_can_see_obj(self_obj, dude_obj)) then begin
							if ((Hostile > 0) or (local_var(0) > 0)) then begin
								Hostile := 0;
								attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
							end
							else begin
								call do_dialogue;
							end
						end
						loopcount := loopcount + 1;
						if (loopcount > 40) then begin
							loopcount := 0;
							if (noevent == 0) then begin
								noevent := 1;
								rndx := 5 + random(1, 10);
								add_timer_event(self_obj, game_ticks(rndx), 0);
							end
						end
					end
				end
				else begin
					if (script_action == destroy_proc) then begin
						set_global_var(GVAR_SUPER_MUTANTS_KILLED, global_var(GVAR_SUPER_MUTANTS_KILLED) + 1);
						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
					else begin
						if (script_action == look_at_proc) then begin
							display_msg(message_str(SCRIPT_MUTANT2, 100));
						end
					end
				end
			end
		end
	end
end

procedure do_dialogue
begin
	start_gdialog(SCRIPT_MUTANT2, self_obj, 4, -1, -1);
	gsay_start;
	set_local_var(0, 1);
	if (dude_is_male) then begin
		gsay_message(SCRIPT_MUTANT2, 101, NEUTRAL_REACTION);
	end
	else begin
		gsay_message(SCRIPT_MUTANT2, 102, NEUTRAL_REACTION);
	end
	gsay_end;
	end_dialogue;
end

procedure timeforwhat
begin
	noevent := 0;
	rndy := random(1, 6);
	if (rndy > 1) then begin
		dist := 2;
	end
	if (rndy > 5) then begin
		dist := 3;
	end
	rndz := random(0, 5);
	set_local_var(3, tile_num(self_obj));
	new_tile := tile_num_in_direction(local_var(3), rndz, dist);
	if (tile_distance(local_var(2), new_tile) < maxleash) then begin
		animate_move_obj_to_tile(self_obj, new_tile, ANIMATE_WALK);
	end
end

procedure genericend
begin
end

procedure genericcbt
begin
end