#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure do_dialogue;
procedure goto01;
procedure goto02;
procedure goto03;
procedure goto04;
procedure goto05;
procedure goto06;
procedure goto07;
procedure goto08;
procedure goto09;
procedure goto10;
procedure goto11;
procedure goto12;
procedure goto13;
procedure goto14;
procedure goto15;
procedure goto16;
procedure goto17;
procedure goto18;
procedure goto19;
procedure goto20;
procedure goto21;
procedure goto22;
procedure goto23;
procedure goto24;
procedure goto24a;
procedure goto25;
procedure goto26;
procedure goto27;
procedure goto75;
procedure gotoend;
procedure gotopart;
procedure combat;
procedure critter_p_proc;
procedure pickup_p_proc;
procedure talk_p_proc;
procedure destroy_p_proc;
procedure look_at_p_proc;
procedure smalltalk;

variable known;
variable only_once := 1;
variable hostile1;
variable radx;
variable ccount;
variable loot;
variable stuff;

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 pre_dialogue;


procedure start
begin
	if (only_once) then begin
		only_once := 0;
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 44);
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_BROTHERHOOD_KNIGHT);
	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 do_dialogue
begin
	call Get_Reaction;
	start_gdialog(SCRIPT_KYLE, self_obj, 4, -1, -1);
	gsay_start;
	if (local_var(4) == 0) then begin
		set_local_var(4, 1);
		call goto01;
	end
	else begin
		if (global_var(GVAR_CALM_REBELS_7) == 2) then begin
			call goto26;
		end
		else begin
			if (global_var(GVAR_CALM_REBELS_7) == 3) then begin
				call goto26;
			end
			else begin
				if (global_var(GVAR_CALM_REBELS_7) == 4) then begin
					call smalltalk;
				end
				else begin
					if (obj_carrying_pid_obj(dude_obj, 229)) then begin
						call goto23;
					end
					else begin
						if (global_var(GVAR_CALM_REBELS_7)) then begin
							call goto17;
						end
						else begin
							call goto27;
						end
					end
				end
			end
		end
	end
	gsay_end;
	end_dialogue;
end

procedure goto01
begin
	gsay_reply(SCRIPT_KYLE, 102);
	giq_option(-3, SCRIPT_KYLE, 103, goto02, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KYLE, 103, goto02, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KYLE, 104, goto13, NEUTRAL_REACTION);
end

procedure goto02
begin
	gsay_reply(SCRIPT_KYLE, 105);
	giq_option(4, SCRIPT_KYLE, 106, goto04, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KYLE, 107, goto03, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KYLE, 108, goto14, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_KYLE, 103, gotoend, NEUTRAL_REACTION);
end

procedure goto03
begin
	gsay_reply(SCRIPT_KYLE, 109);
	giq_option(4, SCRIPT_KYLE, 110, goto04, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KYLE, 111, goto06, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KYLE, 112, goto09, BAD_REACTION);
end

procedure goto04
begin
	gsay_reply(SCRIPT_KYLE, 113);
	if (global_var(GVAR_NECROP_WATER_CHIP_TAKEN) != 1) then begin
		giq_option(4, SCRIPT_KYLE, 114, goto12, NEUTRAL_REACTION);
	end
	giq_option(4, SCRIPT_KYLE, 115, goto05, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KYLE, 116, gotoend, NEUTRAL_REACTION);
end

procedure goto05
begin
	gsay_reply(SCRIPT_KYLE, 117);
	giq_option(4, SCRIPT_KYLE, 118, goto06, NEUTRAL_REACTION);
	if (global_var(GVAR_NECROP_WATER_CHIP_TAKEN) != 1) then begin
		giq_option(4, SCRIPT_KYLE, 114, goto12, NEUTRAL_REACTION);
	end
end

procedure goto06
begin
	gsay_reply(SCRIPT_KYLE, 119);
	giq_option(4, SCRIPT_KYLE, 120, goto07, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KYLE, 121, gotoend, NEUTRAL_REACTION);
end

procedure goto07
begin
	gsay_reply(SCRIPT_KYLE, 122);
	giq_option(4, SCRIPT_KYLE, 123, goto08, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KYLE, 124, goto10, NEUTRAL_REACTION);
end

procedure goto08
begin
	gsay_reply(SCRIPT_KYLE, 125);
	giq_option(4, SCRIPT_KYLE, 124, goto10, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KYLE, 126, goto09, BAD_REACTION);
end

procedure goto09
begin
	call Bottom_React;
	gsay_message(SCRIPT_KYLE, 127, BAD_REACTION);
end

procedure goto10
begin
	gsay_reply(SCRIPT_KYLE, 128);
	giq_option(4, SCRIPT_KYLE, 129, goto11, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KYLE, 130, gotopart, NEUTRAL_REACTION);
end

procedure goto11
begin
	gsay_reply(SCRIPT_KYLE, 131);
	giq_option(4, SCRIPT_KYLE, 132, gotopart, NEUTRAL_REACTION);
end

procedure goto12
begin
	gsay_reply(SCRIPT_KYLE, 133);
	giq_option(4, SCRIPT_KYLE, 134, gotoend, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KYLE, 135, goto09, BAD_REACTION);
	giq_option(4, SCRIPT_KYLE, 136, goto06, NEUTRAL_REACTION);
end

procedure goto13
begin
	gsay_reply(SCRIPT_KYLE, 137);
	giq_option(4, SCRIPT_KYLE, 138, goto02, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KYLE, 139, goto04, BAD_REACTION);
	giq_option(4, SCRIPT_KYLE, 140, gotoend, NEUTRAL_REACTION);
end

procedure goto14
begin
	gsay_reply(SCRIPT_KYLE, 141);
	giq_option(4, SCRIPT_KYLE, 142, goto15, NEUTRAL_REACTION);
end

procedure goto15
begin
	gsay_reply(SCRIPT_KYLE, 143);
	giq_option(4, SCRIPT_KYLE, message_str(SCRIPT_KYLE, 144) + proto_data(obj_pid(dude_obj), 1) + message_str(SCRIPT_KYLE, 145), goto13, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KYLE, 146, goto16, NEUTRAL_REACTION);
end

procedure goto16
begin
	gsay_reply(SCRIPT_KYLE, 147);
	giq_option(4, SCRIPT_KYLE, 148, goto06, NEUTRAL_REACTION);
end

procedure goto17
begin
	gsay_reply(SCRIPT_KYLE, 149);
	giq_option(4, SCRIPT_KYLE, 150, goto18, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KYLE, 151, gotoend, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KYLE, 152, gotoend, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_KYLE, 103, gotoend, NEUTRAL_REACTION);
end

procedure goto18
begin
	gsay_reply(SCRIPT_KYLE, 153);
	giq_option(4, SCRIPT_KYLE, 154, goto09, BAD_REACTION);
	giq_option(4, SCRIPT_KYLE, 155, goto19, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KYLE, 156, goto11, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KYLE, 157, gotoend, NEUTRAL_REACTION);
end

procedure goto19
begin
	gsay_reply(SCRIPT_KYLE, 158);
	giq_option(4, SCRIPT_KYLE, 159, gotoend, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KYLE, 160, goto11, NEUTRAL_REACTION);
end

procedure goto20
begin
end

procedure goto21
begin
	float_msg(self_obj, message_str(SCRIPT_KYLE, 161), FLOAT_MSG_RED);
end

procedure goto22
begin
	float_msg(self_obj, message_str(SCRIPT_KYLE, 162), FLOAT_MSG_RED);
end

procedure goto23
begin
	loot := 0;
	gsay_reply(SCRIPT_KYLE, 163);
	loot := obj_carrying_pid_obj(dude_obj, 229);
	if (loot) then begin
		giq_option(4, SCRIPT_KYLE, 164, goto24, NEUTRAL_REACTION);
	end
	giq_option(4, SCRIPT_KYLE, 165, goto25, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_KYLE, 103, gotoend, NEUTRAL_REACTION);
end

procedure goto24
begin
	rm_obj_from_inven(dude_obj, loot);
	gsay_reply(SCRIPT_KYLE, 166);
	giq_option(4, SCRIPT_KYLE, 300, goto24a, NEUTRAL_REACTION);
end

procedure goto24a
begin
	gfade_out(600);
	gfade_in(600);
	stuff := create_object_sid(76, 0, 0, -1);
	add_obj_to_inven(dude_obj, stuff);
	set_global_var(GVAR_CALM_REBELS_7, 2);
	gsay_message(SCRIPT_KYLE, 167, NEUTRAL_REACTION);
end

procedure goto25
begin
	gsay_message(SCRIPT_KYLE, 168, NEUTRAL_REACTION);
end

procedure goto26
begin
	gsay_reply(SCRIPT_KYLE, 169);
	giq_option(4, SCRIPT_KYLE, 170, gotoend, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_KYLE, 103, gotoend, NEUTRAL_REACTION);
end

procedure goto27
begin
	gsay_reply(SCRIPT_KYLE, 171);
	giq_option(4, SCRIPT_KYLE, 106, goto04, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KYLE, 104, goto13, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_KYLE, 103, gotoend, NEUTRAL_REACTION);
end

procedure goto75
begin
end

procedure gotoend
begin
end

procedure gotopart
begin
	set_global_var(GVAR_CALM_REBELS_7, 1);
end

procedure combat
begin
	hostile1 := 1;
end

procedure critter_p_proc
begin
	if (global_var(GVAR_ENEMY_BROTHERHOOD)) then begin
		hostile1 := 1;
	end
	if (tile_distance_objs(self_obj, dude_obj) > 12) then begin
		hostile1 := 0;
	end
	if (hostile1) then begin
		set_global_var(GVAR_ENEMY_BROTHERHOOD, 1);
		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 do_dialogue;
end

procedure destroy_p_proc
begin
	set_global_var(GVAR_ENEMY_BROTHERHOOD, 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
	script_overrides;
	if (known) then begin
		display_msg(message_str(SCRIPT_KYLE, 100));
	end
	else begin
		display_msg(message_str(SCRIPT_KYLE, 101));
	end
end

procedure smalltalk
begin
	ccount := ccount + 1;
	if (ccount < 6) then begin
		float_msg(self_obj, message_str(SCRIPT_RD1SCRB1, 200 + random(0, 2)), FLOAT_MSG_RED);
	end
	else begin
		if (ccount < 9) then begin
			float_msg(self_obj, message_str(SCRIPT_RD1SCRB1, 203 + random(0, 4)), FLOAT_MSG_RED);
		end
		else begin
			if (ccount < 12) then begin
				float_msg(self_obj, message_str(SCRIPT_RD1SCRB1, 209 + random(0, 3)), FLOAT_MSG_RED);
			end
			else begin
				display_msg(message_str(SCRIPT_RD1SCRB1, 213));
			end
		end
	end
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

procedure pre_dialogue
begin
	if (global_var(GVAR_CALM_REBELS_7) == 4) then begin
		call smalltalk;
	end
	else begin
		if ((local_var(4) == 1) and (local_var(1) < 2)) then begin
			call goto22;
		end
		else begin
			call do_dialogue;
		end
	end
end