#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 look_at_p_proc;
procedure timed_event_p_proc;
procedure BarterGuy00;
procedure BarterGuy00a;
procedure BarterGuy01;
procedure BarterGuy02;
procedure BarterGuy02a;
procedure BarterGuy03;
procedure BarterGuy04;
procedure BarterGuy05;
procedure BarterGuy06;
procedure BarterGuy07;
procedure BarterGuy08;
procedure BarterGuy09;
procedure BarterGuy10;
procedure BarterGuy11;
procedure BarterGuy12;
procedure BarterGuy13;
procedure BarterGuy14;
procedure BarterGuyEnd;

variable hostile1;
variable Only_Once := 1;
variable Caught_Stealing;
variable Here_To_Shop;
variable Go_Balistic;

import variable Shop_Ptr;
import variable Shopkepper_Ptr;

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, 20);
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_CHILDREN_TRUE);
		Shopkepper_Ptr := self_obj;
	end
	if (script_action == look_at_proc) then begin
		call look_at_p_proc;
	end
	else begin
		if (script_action == timed_event_proc) then begin
			call timed_event_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
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
	variable LVar0 := 0;
	variable LVar1 := 0;
	call Get_Reaction;
	LVar1 := obj_carrying_pid_obj(self_obj, 113);
	rm_obj_from_inven(self_obj, LVar1);
	move_obj_inven_to_obj(Shop_Ptr, self_obj);
	start_gdialog(SCRIPT_CATHSHOP, self_obj, 4, -1, -1);
	gsay_start;
	if (Caught_Stealing) then begin
		call BarterGuy00;
	end
	else begin
		if ((obj_item_subtype(critter_inven_obj(dude_obj, 1)) == 3) or (obj_item_subtype(critter_inven_obj(dude_obj, 2)) == 3) or (global_var(GVAR_CATHEDRAL_ENEMY) == 1)) then begin
			call BarterGuy02;
		end
		else begin
			if (obj_pid(critter_inven_obj(dude_obj, 0)) == 113) then begin
				call BarterGuy07;
			end
			else begin
				call BarterGuy10;
			end
		end
	end
	gsay_end;
	end_dialogue;
	move_obj_inven_to_obj(self_obj, Shop_Ptr);
	add_obj_to_inven(self_obj, LVar1);
	wield_obj_critter(self_obj, LVar1);
	if (Go_Balistic) then begin
		LVar0 := create_object_sid(12, 0, 0, -1);
		add_obj_to_inven(self_obj, LVar0);
		wield_obj_critter(self_obj, LVar0);
	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
end

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

procedure timed_event_p_proc
begin
	if (fixed_param == 1) then begin
		Here_To_Shop := 1;
		dialogue_system_enter;
	end
	else begin
		if (fixed_param == 2) then begin
			Caught_Stealing := 1;
			dialogue_system_enter;
		end
	end
end

procedure BarterGuy00
begin
	gsay_reply(SCRIPT_CATHSHOP, 101);
	giq_option(-3, SCRIPT_CATHSHOP, 102, BarterGuy01, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_CATHSHOP, 103, BarterGuy01, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_CATHSHOP, 104, BarterGuy00a, NEUTRAL_REACTION);
end

procedure BarterGuy00a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, 0))) then begin
		call combat;
	end
	else begin
		call BarterGuy01;
	end
end

procedure BarterGuy01
begin
	gsay_message(SCRIPT_CATHSHOP, 105, BAD_REACTION);
	call combat;
end

procedure BarterGuy02
begin
	gsay_reply(SCRIPT_CATHSHOP, 106);
	giq_option(-3, SCRIPT_CATHSHOP, 107, BarterGuyEnd, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_CATHSHOP, 108, BarterGuy03, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_CATHSHOP, 109, BarterGuy02a, NEUTRAL_REACTION);
end

procedure BarterGuy02a
begin
	if (global_var(GVAR_CATHEDRAL_ENEMY) == 1) then begin
		call BarterGuy04;
	end
	else begin
		call BarterGuy05;
	end
end

procedure BarterGuy03
begin
	gsay_message(SCRIPT_CATHSHOP, 110, NEUTRAL_REACTION);
end

procedure BarterGuy04
begin
	gsay_reply(SCRIPT_CATHSHOP, 111);
	giq_option(6, SCRIPT_CATHSHOP, 112, BarterGuy01, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_CATHSHOP, 113, BarterGuyEnd, NEUTRAL_REACTION);
end

procedure BarterGuy05
begin
	gsay_reply(SCRIPT_CATHSHOP, 114);
	giq_option(6, SCRIPT_CATHSHOP, 115, BarterGuy00a, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_CATHSHOP, 116, BarterGuy06, NEUTRAL_REACTION);
end

procedure BarterGuy06
begin
	gsay_message(SCRIPT_CATHSHOP, 117, NEUTRAL_REACTION);
end

procedure BarterGuy07
begin
	gsay_reply(SCRIPT_CATHSHOP, 118);
	giq_option(-3, SCRIPT_CATHSHOP, 119, BarterGuy08, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_CATHSHOP, 120, BarterGuy12, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_CATHSHOP, 121, BarterGuy09, NEUTRAL_REACTION);
end

procedure BarterGuy08
begin
	gsay_reply(SCRIPT_CATHSHOP, 122);
	giq_option(-3, SCRIPT_CATHSHOP, 123, BarterGuyEnd, NEUTRAL_REACTION);
end

procedure BarterGuy09
begin
	gsay_message(SCRIPT_CATHSHOP, 124, NEUTRAL_REACTION);
end

procedure BarterGuy10
begin
	gsay_reply(SCRIPT_CATHSHOP, 125);
	giq_option(-3, SCRIPT_CATHSHOP, 126, BarterGuy08, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_CATHSHOP, 127, BarterGuy13, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_CATHSHOP, 128, BarterGuy11, NEUTRAL_REACTION);
end

procedure BarterGuy11
begin
	gsay_reply(SCRIPT_CATHSHOP, 129);
	giq_option(4, SCRIPT_CATHSHOP, 130, BarterGuyEnd, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_CATHSHOP, 131, BarterGuy01, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_CATHSHOP, 132, BarterGuy13, NEUTRAL_REACTION);
end

procedure BarterGuy12
begin
	gsay_reply(SCRIPT_CATHSHOP, 133);
	giq_option(4, SCRIPT_CATHSHOP, 134, BarterGuyEnd, NEUTRAL_REACTION);
end

procedure BarterGuy13
begin
	gsay_reply(SCRIPT_CATHSHOP, 135);
	giq_option(4, SCRIPT_CATHSHOP, 136, BarterGuy14, NEUTRAL_REACTION);
end

procedure BarterGuy14
begin
	gdialog_mod_barter(-10);
	gsay_message(SCRIPT_CATHSHOP, 137, NEUTRAL_REACTION);
end

procedure BarterGuyEnd
begin
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