#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 Vance01;
procedure Vance02;
procedure Vance02a;
procedure Vance03;
procedure Vance04;
procedure Vance05;
procedure Vance06;
procedure Vance07;
procedure Vance08;
procedure Vance09;
procedure Vance10;
procedure Vance11;
procedure Vance11a;
procedure Vance12;
procedure Vance13;
procedure Vance14;
procedure Vance15;
procedure VanceEnd;
procedure Barter;
procedure Get_Stuff;
procedure Put_Stuff;

variable hostile1;
variable Only_Once := 1;
variable kickOut;
variable initiateDialogue := 1;

import variable Vance_Box_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
	variable LVar0 := 0;
	gdialog_set_barter_mod(-20);
	if (Only_Once) then begin
		Only_Once := 0;
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 81);
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_GUARD);
	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
	if ((initiateDialogue == 1) and (tile_distance_objs(self_obj, dude_obj) <= 4)) then begin
		initiateDialogue := 0;
		dialogue_system_enter;
	end
	if (kickOut == 1) then begin
		kickOut := 0;
		initiateDialogue := 1;
		gfade_out(500);
		move_to(dude_obj, 16265, 0);
		gfade_in(500);
	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;
	if (global_var(GVAR_ENEMY_HUB) == 1) then begin
		start_gdialog(SCRIPT_VANCE, self_obj, 4, -1, -1);
		gsay_start;
		call Vance12;
		gsay_end;
		end_dialogue;
	end
	else begin
		if (local_var(1) == 1) then begin
			start_gdialog(SCRIPT_VANCE, self_obj, 4, -1, -1);
			gsay_start;
			call Vance11;
			gsay_end;
			end_dialogue;
		end
		else begin
			if ((local_var(4) == 0) or (global_var(GVAR_MASTER_FILLER_9) == 0)) then begin
				set_local_var(4, 1);
				start_gdialog(SCRIPT_VANCE, self_obj, 4, -1, -1);
				gsay_start;
				call Vance01;
				gsay_end;
				end_dialogue;
			end
			else begin
				start_gdialog(SCRIPT_VANCE, self_obj, 4, -1, -1);
				gsay_start;
				call Vance06;
				gsay_end;
				end_dialogue;
			end
		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 look_at_p_proc
begin
	script_overrides;
	display_msg(message_str(SCRIPT_VANCE, 100));
end

procedure Vance01
begin
	gsay_reply(SCRIPT_VANCE, 101);
	giq_option(-3, SCRIPT_VANCE, 102, Vance05, NEUTRAL_REACTION);
	if (global_var(GVAR_MASTER_FILLER_9) == 1) then begin
		giq_option(4, SCRIPT_VANCE, 105, Vance05, NEUTRAL_REACTION);
	end
	giq_option(4, SCRIPT_VANCE, 103, Vance02, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_VANCE, 104, Vance03, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_VANCE, 106, Vance04, NEUTRAL_REACTION);
end

procedure Vance02
begin
	gsay_reply(SCRIPT_VANCE, 107);
	giq_option(4, SCRIPT_VANCE, 108, Vance03, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_VANCE, 109, Vance02a, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_VANCE, 111, Vance04, NEUTRAL_REACTION);
end

procedure Vance02a
begin
	if ((is_success(roll_vs_skill(dude_obj, 14, -30)) == 1) or is_success(do_check(dude_obj, 3, -3))) then begin
		call Vance05;
	end
	else begin
		call Vance03;
	end
end

procedure Vance03
begin
	gsay_reply(SCRIPT_VANCE, 112);
	giq_option(4, SCRIPT_VANCE, 113, Vance13, BAD_REACTION);
	giq_option(4, SCRIPT_VANCE, 114, Vance14, NEUTRAL_REACTION);
end

procedure Vance04
begin
	gsay_reply(SCRIPT_VANCE, 115);
	giq_option(4, SCRIPT_VANCE, 116, Vance13, BAD_REACTION);
	giq_option(4, SCRIPT_VANCE, 117, Vance14, NEUTRAL_REACTION);
end

procedure Vance05
begin
	call Barter;
end

procedure Vance06
begin
	gsay_reply(SCRIPT_VANCE, 120);
	giq_option(-3, SCRIPT_VANCE, 121, Barter, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_VANCE, 122, Barter, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_VANCE, 123, Vance07, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_VANCE, 124, Vance08, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_VANCE, 125, Vance09, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_VANCE, 126, Vance15, NEUTRAL_REACTION);
end

procedure Vance07
begin
	gsay_reply(SCRIPT_VANCE, 127);
	giq_option(4, SCRIPT_VANCE, 128, Vance13, BAD_REACTION);
	giq_option(4, SCRIPT_VANCE, 129, Vance14, NEUTRAL_REACTION);
end

procedure Vance08
begin
	gsay_reply(SCRIPT_VANCE, 130);
	giq_option(4, SCRIPT_VANCE, 131, Vance13, BAD_REACTION);
	giq_option(4, SCRIPT_VANCE, 132, Vance10, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_VANCE, 133, Vance15, NEUTRAL_REACTION);
end

procedure Vance09
begin
	gsay_reply(SCRIPT_VANCE, 134);
	giq_option(4, SCRIPT_VANCE, 135, Barter, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_VANCE, 136, Vance07, BAD_REACTION);
	giq_option(4, SCRIPT_VANCE, 137, Vance15, NEUTRAL_REACTION);
end

procedure Vance10
begin
	gsay_reply(SCRIPT_VANCE, 138);
	giq_option(4, SCRIPT_VANCE, 139, Barter, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_VANCE, 140, Vance15, NEUTRAL_REACTION);
end

procedure Vance11
begin
	gsay_reply(SCRIPT_VANCE, 141);
	giq_option(4, SCRIPT_VANCE, 142, Vance13, BAD_REACTION);
	giq_option(4, SCRIPT_VANCE, 143, Vance14, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_VANCE, 144, Vance11a, GOOD_REACTION);
end

procedure Vance11a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, -20)) == 1) then begin
		call Vance02;
	end
	else begin
		call Vance13;
	end
end

procedure Vance12
begin
	gsay_reply(SCRIPT_VANCE, 145);
	giq_option(-3, SCRIPT_VANCE, 146, Vance13, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_VANCE, 147, Vance14, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_VANCE, 148, Vance13, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_VANCE, 149, Vance14, NEUTRAL_REACTION);
end

procedure Vance13
begin
	call Bottom_React;
	call combat;
end

procedure Vance14
begin
	kickOut := 1;
end

procedure Vance15
begin
end

procedure VanceEnd
begin
end

procedure Barter
begin
	set_global_var(GVAR_MASTER_FILLER_9, 1);
	gsay_message(SCRIPT_VANCE, 118, NEUTRAL_REACTION);
	gdialog_mod_barter(-20);
	gsay_reply(SCRIPT_VANCE, 150);
	giq_option(4, SCRIPT_VANCE, 151, VanceEnd, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_VANCE, 152, VanceEnd, NEUTRAL_REACTION);
end

procedure Get_Stuff
begin
	move_obj_inven_to_obj(Vance_Box_Ptr, self_obj);
end

procedure Put_Stuff
begin
	move_obj_inven_to_obj(self_obj, Vance_Box_Ptr);
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