#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure do_dialogue;
procedure combat;
procedure do_action;
procedure timetomove;

import variable table_ptr;

variable Hostile;
variable init;
variable maxleash := 10;
variable noevent;
variable loopcount;
variable rndx;
variable rndy;
variable rndz;
variable dist;
variable new_tile;


procedure start
begin
	if (not(init)) then begin
		if (local_var(3) == 0) then begin
			set_local_var(3, tile_num(self_obj));
			set_local_var(1, 1);
		end
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 32);
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_BROTHERHOOD_KNIGHT);
		init := 1;
	end
	else begin
		if (script_action == timed_event_proc) then begin
			call timetomove;
		end
		else begin
			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
						call do_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_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
					end
				end
			end
		end
	end
end

procedure do_dialogue
begin
end

procedure combat
begin
	Hostile := 1;
end

procedure do_action
begin
	if (global_var(GVAR_ENEMY_BROTHERHOOD)) then begin
		Hostile := 1;
	end
	if (tile_distance_objs(self_obj, dude_obj) > 12) then begin
		Hostile := 0;
	end
	if (Hostile) then begin
		set_global_var(GVAR_ENEMY_BROTHERHOOD, 1);
		Hostile := 0;
		attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
	end
	else begin
		if (local_var(1)) then begin
			if (random(1, 20) < 2) then begin
				if (random(1, 10) < 2) then begin
					use_obj(table_ptr);
				end
				else begin
					call timetomove;
				end
			end
		end
	end
end

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