#include <define.h>

//#define NAME                    SCRIPT_

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


variable hostile1;

import variable Student_ptr;

procedure start;
#include <push.h>
procedure destroy_p_proc;
procedure critter_p_proc;

variable initialized1;
variable my_knife;
variable home_face := 2;
variable temp;


procedure start
begin
	if (not(initialized1)) then begin
		if (local_var(3) == 0) then begin
			set_local_var(3, tile_num(self_obj));
		end
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 44);
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_BROTHERHOOD_INITIATE);
		my_knife := obj_carrying_pid_obj(self_obj, 4);
		if (local_var(0) != 0) then begin
			if (map_var(4) == local_var(0)) then begin
				Student_ptr := self_obj;
				set_map_var(0, 1);
			end
		end
		else begin
			temp := map_var(5) + 1;
			set_map_var(5, temp);
			set_local_var(0, temp);
			Student_ptr := self_obj;
			set_map_var(0, 1);
		end
		initialized1 := 1;
	end
	else begin
		if (script_action == destroy_proc) then begin
			call destroy_p_proc;
		end
		else begin
			if (script_action == critter_proc) then begin
				if (global_var(GVAR_ENEMY_BROTHERHOOD)) then begin
					hostile1 := 1;
				end
				if (tile_distance_objs(self_obj, dude_obj) > 12) then begin
					hostile1 := 0;
				end
				if (hostile1) then begin
					set_global_var(GVAR_ENEMY_BROTHERHOOD, 1);
					hostile1 := 0;
					attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
				end
				else begin
					if (global_var(GVAR_ENEMY_BROTHERHOOD) == 0) then begin
						call critter_p_proc;
					end
				end
			end
		end
	end
end

procedure destroy_p_proc
begin
	set_global_var(GVAR_ENEMY_BROTHERHOOD, 1);
	rm_timer_event(self_obj);
	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 critter_p_proc
begin
	if (local_var(1) == 1) then begin
		if (tile_num(self_obj) != map_var(1)) then begin
			animate_move_obj_to_tile(self_obj, map_var(1), ANIMATE_WALK);
		end
		else begin
			set_local_var(1, 0);
			set_map_var(0, 1);
			anim(self_obj, 1000, 1);
		end
	end
	else begin
		if (local_var(2) == 1) then begin
			if (tile_num(self_obj) != local_var(3)) then begin
				animate_move_obj_to_tile(self_obj, local_var(3), ANIMATE_WALK);
			end
			else begin
				set_local_var(2, 0);
				anim(self_obj, 1000, home_face);
			end
		end
		else begin
			if (map_var(2) == local_var(0)) then begin
				set_map_var(2, 0);
				Student_ptr := self_obj;
				set_map_var(3, map_var(4));
				set_map_var(4, local_var(0));
				set_local_var(1, 1);
			end
			else begin
				if (map_var(3) == local_var(0)) then begin
					set_map_var(3, 0);
					set_local_var(2, 1);
				end
			end
		end
	end
end