#include <define.h>

//#define NAME                    SCRIPT_

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


variable GVar0;
variable GVar1;
variable GVar2;
variable GVar3;

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

import variable random_seed_1;
import variable random_seed_2;


procedure start
begin
	if (not(GVar1)) then begin
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 35);
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, random(15, 19));
		GVar0 := global_var(GVAR_SAVE_TANDI_5);
		GVar1 := 1;
	end
	else begin
		if (script_action == critter_proc) then begin
			call critter_p_proc;
		end
		else begin
			if (script_action == destroy_proc) then begin
				call destroy_p_proc;
			end
			else begin
				if (script_action == pickup_proc) then begin
					call pickup_p_proc;
				end
				else begin
					if (script_action == talk_proc) then begin
						call talk_p_proc;
					end
				end
			end
		end
	end
end

procedure critter_p_proc
begin
	if (tile_distance_objs(self_obj, dude_obj) < 8) then begin
		if (GVar2) then begin
			call GVar3;
		end
		else begin
			if (GVar0) then begin
				GVar0 := 0;
				GVar2 := 1;
				set_global_var(GVAR_SAVE_TANDI_5, 1);
				attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
			end
		end
	end
end

procedure destroy_p_proc
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
	if (source_obj == dude_obj) then begin
		random_seed_2 := 1;
	end
end

procedure pickup_p_proc
begin
	if (not(GVar2)) then begin
		GVar0 := 1;
		set_global_var(GVAR_SAVE_TANDI_5, 1);
	end
end

procedure talk_p_proc
begin
	if (GVar2) then begin
		if (random_seed_2) then begin
			float_msg(self_obj, message_str(SCRIPT_GENGRDB, 103), FLOAT_MSG_RED);
		end
		else begin
			float_msg(self_obj, message_str(SCRIPT_GENGRDB, 104), FLOAT_MSG_GREEN);
		end
	end
	else begin
		if (random_seed_2) then begin
			float_msg(self_obj, message_str(SCRIPT_GENGRDB, 102), FLOAT_MSG_RED);
		end
		else begin
			if (random_seed_1 == 0) then begin
				float_msg(self_obj, message_str(SCRIPT_GENGRDB, 100), FLOAT_MSG_BLUE);
			end
			else begin
				float_msg(self_obj, message_str(SCRIPT_GENGRDB, 101), FLOAT_MSG_GREEN);
			end
		end
	end
end