#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure critter_p_proc;
procedure destroy_p_proc;
procedure look_at_p_proc;
procedure pickup_p_proc;
procedure talk_p_proc;
procedure show_true_name;
procedure show_false_name;
procedure Beca00;
procedure Beca01;
procedure Beca02;
procedure BecaCombat;
procedure BecaEnd;

variable hostile1;
variable initialized1;


procedure start
begin
	if (not(initialized1)) then begin
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 47);
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_BLADES);
		initialized1 := 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 == look_at_proc) then begin
					call look_at_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
end

procedure critter_p_proc
begin
	if (obj_can_see_obj(self_obj, dude_obj) and (global_var(GVAR_BLADES_LEFT) < 41)) then begin
		hostile1 := 1;
	end
	if (hostile1) then begin
		hostile1 := 0;
		attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
	end
end

procedure destroy_p_proc
begin
	set_global_var(GVAR_BLADES_LEFT, global_var(GVAR_BLADES_LEFT) - 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_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 look_at_p_proc
begin
	if ((global_var(GVAR_BECOME_BLADE) == 2) or (get_critter_stat(dude_obj, 6) > 6)) then begin
		call show_true_name;
	end
	else begin
		if (get_critter_stat(dude_obj, 6) < 4) then begin
			call show_false_name;
		end
		else begin
			if (get_critter_stat(dude_obj, 4) < 5) then begin
				call show_false_name;
			end
			else begin
				if (random(0, 1)) then begin
					call show_false_name;
				end
				else begin
					call show_true_name;
				end
			end
		end
	end
end

procedure pickup_p_proc
begin
	hostile1 := 1;
end

procedure talk_p_proc
begin
	if (get_critter_stat(dude_obj, 4) < 4) then begin
		display_msg(message_str(SCRIPT_GENCHAT, 175));
	end
	else begin
		set_local_var(0, 1);
		if (global_var(GVAR_BECOME_BLADE) == 2) then begin
			call Beca02;
		end
		else begin
			start_gdialog(SCRIPT_BECA, self_obj, 4, -1, -1);
			gsay_start;
			call Beca00;
			gsay_end;
			end_dialogue;
		end
	end
end

procedure show_true_name
begin
	script_overrides;
	if (local_var(0)) then begin
		if ((game_time_hour >= 1900) or (game_time_hour < 600)) then begin
			display_msg(message_str(SCRIPT_BECA, 100));
		end
		else begin
			display_msg(message_str(SCRIPT_BECA, 101));
		end
	end
	else begin
		display_msg(message_str(SCRIPT_BECA, 104));
	end
end

procedure show_false_name
begin
	script_overrides;
	display_msg(message_str(SCRIPT_BECA, random(105, 111)));
end

procedure Beca00
begin
	gsay_reply(SCRIPT_BECA, 112);
	giq_option(4, SCRIPT_BECA, 113, BecaCombat, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_BECA, 114, BecaEnd, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_BECA, 115, Beca01, NEUTRAL_REACTION);
end

procedure Beca01
begin
	gsay_message(SCRIPT_BECA, 116, NEUTRAL_REACTION);
end

procedure Beca02
begin
	float_msg(self_obj, message_str(SCRIPT_BECA, 117), FLOAT_MSG_RED);
end

procedure BecaCombat
begin
	hostile1 := 1;
end

procedure BecaEnd
begin
end