#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure look_at_p_proc;
procedure description_p_proc;
procedure talk_p_proc;
procedure critter_p_proc;
procedure damage_p_proc;
procedure destroy_p_proc;
procedure pickup_p_proc;

variable Initialize := 1;
variable PsstTime;
variable Hostile;

procedure timed_event_p_proc;


procedure Start
begin
	if (Initialize) then begin
		if (obj_is_carrying_obj_pid(self_obj, 41) == 0) then begin
			item_caps_adjust(self_obj, random(1, 10));
		end
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 89);
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_REGULATORS);
		add_timer_event(self_obj, game_ticks(random(1, 3)), 1);
		if (local_var(4) == 0) then begin
			set_map_var(1, map_var(1) + 1);
			set_local_var(4, 1);
		end
		Initialize := 0;
	end
end

procedure look_at_p_proc
begin
	script_overrides;
	display_msg(message_str(SCRIPT_REGULATR, 100));
end

procedure description_p_proc
begin
	script_overrides;
	display_msg(message_str(SCRIPT_REGULATR, 100));
end

procedure talk_p_proc
begin
	if ((global_var(GVAR_ENEMY_ADYTUM) == 1) or (global_var(GVAR_ENEMY_REGULATOR) == 1)) then begin
		float_msg(self_obj, message_str(SCRIPT_ENEMY, random(100, 105)), FLOAT_MSG_RED);
	end
	else begin
		float_msg(self_obj, message_str(SCRIPT_REGULATR, random(134, 138)), FLOAT_MSG_NORMAL);
	end
end

procedure critter_p_proc
begin
	variable LVar0 := 0;
	if (obj_can_see_obj(self_obj, dude_obj)) then begin
		if ((global_var(GVAR_ENEMY_ADYTUM) == 1) or (global_var(GVAR_ENEMY_REGULATOR) == 1)) then begin
			attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
		end
	end
	LVar0 := global_var(GVAR_CURE_JARVIS_2);
	if ((((game_time / 10) - LVar0) >= 5) and (tile_distance_objs(self_obj, dude_obj) <= 4) and (global_var(GVAR_ENEMY_ADYTUM) == 0) and (global_var(GVAR_ENEMY_REGULATOR) == 0)) then begin
		float_msg(self_obj, message_str(SCRIPT_REGULATR, random(134, 138)), FLOAT_MSG_NORMAL);
		LVar0 := game_time / 10;
		set_global_var(GVAR_CURE_JARVIS_2, LVar0);
	end
end

procedure damage_p_proc
begin
	if (source_obj == dude_obj) then begin
		set_global_var(GVAR_ENEMY_REGULATOR, 1);
	end
end

procedure destroy_p_proc
begin
	set_map_var(1, map_var(1) - 1);
	if (source_obj == dude_obj) then begin
		set_global_var(GVAR_ENEMY_REGULATOR, 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
	if (map_var(1) == 0) then begin
		terminate_combat;
	end
end

procedure pickup_p_proc
begin
	set_global_var(GVAR_ENEMY_REGULATOR, 1);
end

procedure timed_event_p_proc
begin
	animate_move_obj_to_tile(self_obj, tile_num_in_direction(tile_num(self_obj), random(0, 5), random(1, 5)), ANIMATE_WALK);
	add_timer_event(self_obj, game_ticks(random(1, 3)), 1);
end