#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure do_dialogue;
procedure guard02a;
procedure guard02b;
procedure guard00;
procedure guard00i;
procedure guard00ii;
procedure guard01;
procedure guard01i;
procedure guard02;
procedure guard02i;
procedure guard03;
procedure guard04;
procedure guard05;
procedure guardend;
procedure weapon_check;

variable Caught;
variable armed;
variable Scared;
variable Occurance;
variable Checked;
variable hostile1;
variable rndx;

procedure React_To_Level;
procedure Level_To_React;
procedure ModReact;
procedure Up_React;
procedure Down_React;
procedure Bottom_React;
procedure Top_React;
procedure Big_Up_React;
procedure Big_Down_React;
procedure Up_React_Level;
procedure Down_React_Level;
procedure Goodbyes;

variable reaction := 50;
variable reaction_level := 2;
variable Talk;
variable exit_line;


procedure start
begin
	if (script_action == talk_proc) then begin
		call do_dialogue;
	end
	else begin
		if (script_action == critter_proc) then begin
			call weapon_check;
			if (obj_can_see_obj(self_obj, dude_obj)) then begin
				if ((global_var(GVAR_WEAPON_LOCKER) == 0) and armed and not(Scared) and not(Checked)) then begin
					Checked := 1;
					dialogue_system_enter;
				end
			end
			if (hostile1) then begin
				attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
			end
		end
		else begin
			if (script_action == timed_event_proc) then begin
				if (fixed_param == 1) then begin
					if (obj_can_see_obj(self_obj, dude_obj)) then begin
						call weapon_check;
						if (armed) then begin
							attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
						end
					end
					else begin
						if (script_action == destroy_proc) then begin
							give_exp_points(150);
							display_msg(message_str(SCRIPT_GUARDEMO, 123));
						end
						else begin
							if ((script_action == look_at_proc) or 3) then begin
								script_overrides;
								display_msg(message_str(SCRIPT_GUARDEMO, 100));
							end
						end
					end
				end
			end
		end
	end
end

procedure do_dialogue
begin
	if ((global_var(GVAR_WEAPON_LOCKER) == 0) and armed and not(Scared)) then begin
		set_global_var(GVAR_WEAPON_LOCKER, 1);
		start_gdialog(SCRIPT_GUARDEMO, self_obj, 4, -1, -1);
		sayStart;
		call guard00;
		sayEnd;
		end_dialogue;
		add_timer_event(self_obj, game_ticks(5), 1);
	end
	else begin
		call guard02a;
	end
end

procedure guard02a
begin
	rndx := random(1, 3);
	if (rndx == 1) then begin
		float_msg(self_obj, message_str(SCRIPT_GUARDEMO, 101), FLOAT_MSG_NORMAL);
	end
	else begin
		if (rndx == 2) then begin
			float_msg(self_obj, message_str(SCRIPT_GUARDEMO, 102), FLOAT_MSG_NORMAL);
		end
		else begin
			if (rndx == 3) then begin
				float_msg(self_obj, message_str(SCRIPT_GUARDEMO, 103), FLOAT_MSG_NORMAL);
			end
		end
	end
end

procedure guard02b
begin
	sayReply(0, message_str(SCRIPT_GUARDEMO, 114));
	if (get_critter_stat(dude_obj, 2) >= 4) then begin
		sayOption(message_str(SCRIPT_GUARDEMO, 115), guardend);
	end
	if (get_critter_stat(dude_obj, 2) >= 4) then begin
		sayOption(message_str(SCRIPT_GUARDEMO, 116), guard02i);
	end
end

procedure guard00
begin
	sayReply(0, message_str(SCRIPT_GUARDEMO, 104));
	if (get_critter_stat(dude_obj, 2) >= 4) then begin
		sayOption(message_str(SCRIPT_GUARDEMO, 105), guard01);
	end
	if (get_critter_stat(dude_obj, 2) >= 4) then begin
		sayOption(message_str(SCRIPT_GUARDEMO, 106), guard00i);
	end
	if (get_critter_stat(dude_obj, 2) >= 5) then begin
		sayOption(message_str(SCRIPT_GUARDEMO, 107), guard00ii);
	end
	if (-get_critter_stat(dude_obj, 2) >= -3) then begin
		sayOption(message_str(SCRIPT_GUARDEMO, 108), guard05);
	end
end

procedure guard00i
begin
	call Down_React;
	call guard02;
end

procedure guard00ii
begin
	call Up_React;
	call guard04;
end

procedure guard01
begin
	sayReply(0, message_str(SCRIPT_GUARDEMO, 109));
	if (get_critter_stat(dude_obj, 2) >= 4) then begin
		sayOption(message_str(SCRIPT_GUARDEMO, 110), guard02);
	end
	if (get_critter_stat(dude_obj, 2) >= 4) then begin
		sayOption(message_str(SCRIPT_GUARDEMO, 111), guard01i);
	end
end

procedure guard01i
begin
	if (is_success(roll_vs_skill(dude_obj, 14, -20))) then begin
		call guard03;
	end
	else begin
		call guard02;
	end
end

procedure guard02
begin
	sayReply(0, message_str(SCRIPT_GUARDEMO, 112));
	if (get_critter_stat(dude_obj, 2) >= 0) then begin
		sayOption(message_str(SCRIPT_GUARDEMO, 113), guard02b);
	end
end

procedure guard02i
begin
	hostile1 := 1;
end

procedure guard03
begin
	sayReply(0, message_str(SCRIPT_GUARDEMO, 117));
	if (get_critter_stat(dude_obj, 2) >= 0) then begin
		sayOption(message_str(SCRIPT_GUARDEMO, 118), guardend);
	end
	Scared := 1;
end

procedure guard04
begin
	sayReply(0, message_str(SCRIPT_GUARDEMO, 119));
	if (get_critter_stat(dude_obj, 2) >= 0) then begin
		sayOption(message_str(SCRIPT_GUARDEMO, 120), guardend);
	end
end

procedure guard05
begin
	sayReply(0, message_str(SCRIPT_GUARDEMO, 121));
	if (get_critter_stat(dude_obj, 2) >= 0) then begin
		sayOption(message_str(SCRIPT_GUARDEMO, 122), guardend);
	end
end

procedure guardend
begin
end

procedure weapon_check
begin
	if ((obj_item_subtype(critter_inven_obj(dude_obj, 1)) == 3) or (obj_item_subtype(critter_inven_obj(dude_obj, 2)) == 3)) then begin
		armed := 1;
	end
	else begin
		armed := 0;
	end
end

procedure React_To_Level
begin
	if (reaction < 24) then begin
		reaction_level := 1;
	end
	else begin
		if (reaction < 74) then begin
			reaction_level := 2;
		end
		else begin
			reaction_level := 3;
		end
	end
end

procedure Level_To_React
begin
	if (reaction_level == 1) then begin
		reaction := random(1, 24);
	end
	else begin
		if (reaction_level == 2) then begin
			reaction := random(25, 74);
		end
		else begin
			reaction := random(75, 100);
		end
	end
end

procedure ModReact
begin
	Talk := roll_vs_skill(dude_obj, 14, 0);
	if (action_being_used == 14) then begin
		if (is_success(Talk)) then begin
			reaction_level := reaction_level + 1;
		end
	end
	call Level_To_React;
end

procedure Up_React
begin
	reaction := reaction + 10;
	call React_To_Level;
end

procedure Down_React
begin
	reaction := reaction - 10;
	call React_To_Level;
end

procedure Bottom_React
begin
	reaction_level := 1;
	reaction := 1;
end

procedure Top_React
begin
	reaction := 100;
	reaction_level := 3;
end

procedure Big_Up_React
begin
	reaction := reaction + 25;
	call React_To_Level;
end

procedure Big_Down_React
begin
	reaction := reaction - 25;
	call React_To_Level;
end

procedure Up_React_Level
begin
	reaction_level := reaction_level + 1;
	if (reaction_level > 3) then begin
		reaction_level := 3;
	end
	call Level_To_React;
end

procedure Down_React_Level
begin
	reaction_level := reaction_level - 1;
	if (reaction_level < 1) then begin
		reaction_level := 1;
	end
	call Level_To_React;
end

procedure Goodbyes
begin
	variable LVar0 := 0;
	LVar0 := random(1, 6);
	if (LVar0 == 1) then begin
		exit_line := "Thanks, bye.";
	end
	if (LVar0 == 2) then begin
		exit_line := "Okay, thanks.";
	end
	if (LVar0 == 3) then begin
		exit_line := "Hmm. Bye.";
	end
	if (LVar0 == 4) then begin
		exit_line := "See ya.";
	end
	if (LVar0 == 5) then begin
		exit_line := "Thanks.";
	end
	if (LVar0 == 6) then begin
		exit_line := "Okay, bye.";
	end
end