#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure combat;
procedure critter_p_proc;
procedure pickup_p_proc;
procedure talk_p_proc;
procedure destroy_p_proc;
procedure damage_p_proc;
procedure look_at_p_proc;
procedure Staple00;
procedure Staple01;
procedure Staple02;
procedure Staple03;
procedure Staple04;
procedure Staple05;
procedure Staple06;
procedure Staple06a;
procedure Staple06b;
procedure Staple06c;
procedure Staple07;
procedure Staple08;
procedure Staple09;
procedure Staple10;
procedure Staple11;
procedure Staple12;
procedure Staple13;
procedure Staple14;
procedure Staple15;
procedure Staple15a;
procedure Staple16;
procedure Staple17;
procedure StapleEnd;
procedure Get_Stuff;
procedure Put_Stuff;

variable hostile1;
variable Only_Once := 1;

procedure Get_Reaction;
procedure React_To_Level;
procedure Level_To_React;
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 exit_line;


procedure start
begin
	if (Only_Once) then begin
		Only_Once := 0;
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 70);
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_MERCHANT);
	end
	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
			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
				end
			end
		end
	end
end

procedure combat
begin
	hostile1 := 1;
end

procedure critter_p_proc
begin
	if (hostile1) then begin
		hostile1 := 0;
		attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
	end
end

procedure pickup_p_proc
begin
	if (source_obj == dude_obj) then begin
		hostile1 := 1;
	end
end

procedure talk_p_proc
begin
	call Get_Stuff;
	call Get_Reaction;
	gdialog_set_barter_mod(-50);
	if (local_var(4) == 0) then begin
		set_local_var(4, 1);
		start_gdialog(SCRIPT_STAPLE, self_obj, 4, -1, -1);
		gsay_start;
		call Staple00;
		gsay_end;
		end_dialogue;
	end
	else begin
		if (local_var(6) == 1) then begin
			call Staple13;
		end
		else begin
			start_gdialog(SCRIPT_STAPLE, self_obj, 4, -1, -1);
			gsay_start;
			call Staple01;
			gsay_end;
			end_dialogue;
		end
	end
	call Put_Stuff;
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
end

procedure damage_p_proc
begin
	variable LVar0 := 0;
	LVar0 := obj_pid(source_obj);
	if (party_member_obj(LVar0) != 0) then begin
		set_global_var(GVAR_ENEMY_HUB, 1);
	end
end

procedure look_at_p_proc
begin
	script_overrides;
	display_msg(message_str(SCRIPT_STAPLE, 100));
end

procedure Staple00
begin
	gsay_reply(SCRIPT_STAPLE, 102);
	call Staple02;
end

procedure Staple01
begin
	gsay_reply(SCRIPT_STAPLE, 103);
	call Staple02;
end

procedure Staple02
begin
	giq_option(4, SCRIPT_STAPLE, 104, Staple03, NEUTRAL_REACTION);
	if ((global_var(GVAR_FIND_WATER_CHIP) != 2) and (local_var(5) == 0)) then begin
		giq_option(4, SCRIPT_STAPLE, 105, Staple04, NEUTRAL_REACTION);
	end
	if (global_var(GVAR_CHEMISTRY_BOOK) == 1) then begin
		giq_option(4, SCRIPT_STAPLE, 130, Staple15, NEUTRAL_REACTION);
	end
	giq_option(4, SCRIPT_STAPLE, 106, StapleEnd, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_STAPLE, 125, Staple14, NEUTRAL_REACTION);
end

procedure Staple03
begin
	gsay_message(SCRIPT_STAPLE, 107, NEUTRAL_REACTION);
	gdialog_mod_barter(-50);
	gsay_reply(SCRIPT_STAPLE, 127);
	giq_option(4, SCRIPT_STAPLE, 128, StapleEnd, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_STAPLE, 129, StapleEnd, NEUTRAL_REACTION);
end

procedure Staple04
begin
	gsay_reply(SCRIPT_STAPLE, 108);
	giq_option(4, SCRIPT_STAPLE, 109, Staple05, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_STAPLE, 110, Staple06, NEUTRAL_REACTION);
end

procedure Staple05
begin
	gsay_reply(SCRIPT_STAPLE, 111);
	giq_option(4, SCRIPT_STAPLE, 112, Staple06, NEUTRAL_REACTION);
end

procedure Staple06
begin
	gsay_reply(SCRIPT_STAPLE, 113);
	giq_option(4, SCRIPT_STAPLE, 114, Staple06a, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_STAPLE, 115, Staple06b, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_STAPLE, 116, Staple06c, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_STAPLE, 117, StapleEnd, NEUTRAL_REACTION);
end

procedure Staple06a
begin
	if (item_caps_total(dude_obj) >= 750) then begin
		call Staple09;
	end
	else begin
		call Staple07;
	end
end

procedure Staple06b
begin
	if (is_success(do_check(dude_obj, 3, 0)) or is_success(roll_vs_skill(dude_obj, 15, 0))) then begin
		if (item_caps_total(dude_obj) >= 500) then begin
			call Staple10;
		end
		else begin
			call Staple07;
		end
	end
	else begin
		call Staple07;
	end
end

procedure Staple06c
begin
	if (is_success(do_check(dude_obj, 0, 0))) then begin
		call Staple11;
	end
	else begin
		call Staple08;
	end
end

procedure Staple07
begin
	gsay_message(SCRIPT_STAPLE, 118, NEUTRAL_REACTION);
end

procedure Staple08
begin
	set_local_var(6, 1);
	gsay_message(SCRIPT_STAPLE, 119, BAD_REACTION);
end

procedure Staple09
begin
	variable LVar0 := 0;
	LVar0 := item_caps_adjust(dude_obj, -750);
	gsay_message(SCRIPT_STAPLE, 120, NEUTRAL_REACTION);
	call Staple12;
end

procedure Staple10
begin
	variable LVar0 := 0;
	LVar0 := item_caps_adjust(dude_obj, -500);
	gsay_message(SCRIPT_STAPLE, 121, NEUTRAL_REACTION);
	call Staple12;
end

procedure Staple11
begin
	set_local_var(6, 1);
	gsay_message(SCRIPT_STAPLE, 122, BAD_REACTION);
	call Staple12;
end

procedure Staple12
begin
	variable LVar0 := 0;
	set_local_var(5, 1);
	LVar0 := create_object_sid(230, 0, 0, -1);
	add_obj_to_inven(dude_obj, LVar0);
	gsay_message(SCRIPT_STAPLE, 123, NEUTRAL_REACTION);
end

procedure Staple13
begin
	float_msg(self_obj, message_str(SCRIPT_STAPLE, 124), FLOAT_MSG_RED);
end

procedure Staple14
begin
	gsay_message(SCRIPT_STAPLE, 126, NEUTRAL_REACTION);
end

procedure Staple15
begin
	gsay_reply(SCRIPT_STAPLE, 131);
	giq_option(4, SCRIPT_STAPLE, 114, Staple15a, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_STAPLE, 117, StapleEnd, NEUTRAL_REACTION);
end

procedure Staple15a
begin
	if (item_caps_total(dude_obj) >= 750) then begin
		call Staple16;
	end
	else begin
		call Staple17;
	end
end

procedure Staple16
begin
	variable LVar0 := 0;
	variable LVar1 := 0;
	LVar0 := item_caps_adjust(dude_obj, -750);
	set_global_var(GVAR_CHEMISTRY_BOOK, 2);
	LVar1 := create_object_sid(237, 0, 0, -1);
	add_obj_to_inven(dude_obj, LVar1);
	gsay_message(SCRIPT_STAPLE, 132, NEUTRAL_REACTION);
end

procedure Staple17
begin
	gsay_message(SCRIPT_STAPLE, 133, NEUTRAL_REACTION);
end

procedure StapleEnd
begin
end

procedure Get_Stuff
begin
	variable LVar0 := 0;
	variable LVar1 := 0;
	LVar0 := create_object_sid(73, 0, 0, -1);
	add_mult_objs_to_inven(self_obj, LVar0, 5);
	LVar0 := create_object_sid(76, 0, 0, -1);
	add_mult_objs_to_inven(self_obj, LVar0, 5);
	LVar0 := create_object_sid(80, 0, 0, -1);
	add_mult_objs_to_inven(self_obj, LVar0, 5);
	LVar0 := create_object_sid(86, 0, 0, -1);
	add_mult_objs_to_inven(self_obj, LVar0, 5);
	LVar0 := create_object_sid(102, 0, 0, -1);
	add_mult_objs_to_inven(self_obj, LVar0, 5);
	LVar1 := item_caps_adjust(self_obj, 500);
end

procedure Put_Stuff
begin
	variable LVar0 := 0;
	variable LVar1 := 0;
	LVar0 := obj_carrying_pid_obj(self_obj, 73);
	if (LVar0 != 0) then begin
		LVar1 := rm_mult_objs_from_inven(self_obj, LVar0, 5);
		destroy_object(LVar0);
	end
	LVar0 := obj_carrying_pid_obj(self_obj, 76);
	if (LVar0 != 0) then begin
		LVar1 := rm_mult_objs_from_inven(self_obj, LVar0, 5);
		destroy_object(LVar0);
	end
	LVar0 := obj_carrying_pid_obj(self_obj, 80);
	if (LVar0 != 0) then begin
		LVar1 := rm_mult_objs_from_inven(self_obj, LVar0, 5);
		destroy_object(LVar0);
	end
	LVar0 := obj_carrying_pid_obj(self_obj, 86);
	if (LVar0 != 0) then begin
		LVar1 := rm_mult_objs_from_inven(self_obj, LVar0, 5);
		destroy_object(LVar0);
	end
	LVar0 := obj_carrying_pid_obj(self_obj, 102);
	if (LVar0 != 0) then begin
		LVar1 := rm_mult_objs_from_inven(self_obj, LVar0, 5);
		destroy_object(LVar0);
	end
	LVar1 := item_caps_adjust(self_obj, -item_caps_total(self_obj));
	LVar1 := item_caps_adjust(self_obj, 24);
end

procedure Get_Reaction
begin
	if (local_var(2) == 0) then begin
		set_local_var(0, 50);
		set_local_var(1, 2);
		set_local_var(2, 1);
		set_local_var(0, local_var(0) + (5 * get_critter_stat(dude_obj, 3)) - 25);
		set_local_var(0, local_var(0) + (10 * has_trait(0, dude_obj, 10)));
		if (has_trait(0, dude_obj, 39)) then begin
			if (global_var(GVAR_PLAYER_REPUTATION) > 0) then begin
				set_local_var(0, local_var(0) + global_var(GVAR_PLAYER_REPUTATION));
			end
			else begin
				set_local_var(0, local_var(0) - global_var(GVAR_PLAYER_REPUTATION));
			end
		end
		else begin
			if (local_var(3) == 1) then begin
				set_local_var(0, local_var(0) - global_var(GVAR_PLAYER_REPUTATION));
			end
			else begin
				set_local_var(0, local_var(0) + global_var(GVAR_PLAYER_REPUTATION));
			end
		end
		if (global_var(GVAR_CHILDKILLER_REPUTATION) > 2) then begin
			set_local_var(0, local_var(0) - 30);
		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_local_var(0, local_var(0) + 20);
		end
		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_local_var(0, local_var(0) - 20);
		end
		call React_To_Level;
	end
end

procedure React_To_Level
begin
	if (local_var(0) <= 25) then begin
		set_local_var(1, 1);
	end
	else begin
		if (local_var(0) <= 75) then begin
			set_local_var(1, 2);
		end
		else begin
			set_local_var(1, 3);
		end
	end
end

procedure Level_To_React
begin
	if (local_var(1) == 1) then begin
		set_local_var(0, random(1, 25));
	end
	else begin
		if (local_var(1) == 2) then begin
			set_local_var(0, random(26, 75));
		end
		else begin
			set_local_var(0, random(76, 100));
		end
	end
end

procedure Up_React
begin
	set_local_var(0, local_var(0) + 10);
	call React_To_Level;
end

procedure Down_React
begin
	set_local_var(0, local_var(0) - 10);
	call React_To_Level;
end

procedure Bottom_React
begin
	set_local_var(1, 1);
	set_local_var(0, 1);
end

procedure Top_React
begin
	set_local_var(0, 100);
	set_local_var(1, 3);
end

procedure Big_Up_React
begin
	set_local_var(0, local_var(0) + 25);
	call React_To_Level;
end

procedure Big_Down_React
begin
	set_local_var(0, local_var(0) - 25);
	call React_To_Level;
end

procedure Up_React_Level
begin
	set_local_var(1, local_var(1) + 1);
	if (local_var(1) > 3) then begin
		set_local_var(1, 3);
	end
	call Level_To_React;
end

procedure Down_React_Level
begin
	set_local_var(1, local_var(1) - 1);
	if (local_var(1) < 1) then begin
		set_local_var(1, 1);
	end
	call Level_To_React;
end

procedure Goodbyes
begin
	exit_line := message_str(SCRIPT_MODREACT, random(100, 105));
end