#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure combat_p_proc;
procedure map_update_p_proc;
procedure talk_p_proc;
procedure destroy_p_proc;
procedure travel;
procedure no_travel;
procedure pick_start;
procedure Seth01;
procedure Seth02;
procedure Seth03;
procedure Seth04;
procedure Seth05;
procedure Seth06;
procedure Seth07;
procedure Seth08;
procedure Seth08a;
procedure Seth09;
procedure Seth10;
procedure Seth11;
procedure Seth12;
procedure Seth13;
procedure Seth14;
procedure Sethend;
procedure TanSeth00;
procedure TanSeth01;
procedure TanSeth02;
procedure TanSeth03;
procedure TanSeth04;
procedure TanSeth05;
procedure TanSeth06;
procedure TanSeth07;
procedure sleeping;

variable night_person;
variable wake_time;
variable sleep_time;
variable home_tile;
variable sleep_tile;
variable hostile1;
variable initialized1;
variable round_counter;

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 (script_action == combat_proc) then begin
		call combat_p_proc;
	end
	else begin
		if (script_action == map_update_proc) then begin
			call map_update_p_proc;
		end
		else begin
			if (script_action == talk_proc) then begin
				call talk_p_proc;
			end
			else begin
				if (script_action == destroy_proc) then begin
					call destroy_p_proc;
				end
				else begin
					if ((script_action == look_at_proc) or (script_action == description_proc)) then begin
						script_overrides;
						display_msg(message_str(SCRIPT_SETH, 100));
					end
					else begin
						if (script_action == pickup_proc) then begin
							hostile1 := 1;
						end
						else begin
							if (script_action == destroy_proc) then begin
								set_global_var(GVAR_SETH_STATUS, 3);
								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
							else begin
								if (script_action == critter_proc) then begin
									if (local_var(6) == 0) then begin
										set_local_var(6, 1);
										if ((obj_item_subtype(critter_inven_obj(dude_obj, 1)) == 3) or (obj_item_subtype(critter_inven_obj(dude_obj, 2)) == 3)) then begin
											float_msg(self_obj, message_str(SCRIPT_SETH, 204), FLOAT_MSG_YELLOW);
										end
										else begin
											float_msg(self_obj, message_str(SCRIPT_SETH, 205), FLOAT_MSG_YELLOW);
										end
									end
									if (hostile1) then begin
										hostile1 := 0;
										attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
									end
								end
							end
						end
					end
				end
			end
		end
	end
end

procedure combat_p_proc
begin
	if (fixed_param == 4) then begin
		round_counter := round_counter + 1;
	end
	if (round_counter > 3) then begin
		if (global_var(GVAR_ENEMY_SHADY_SANDS) == 0) then begin
			set_global_var(GVAR_ENEMY_SHADY_SANDS, 1);
			set_global_var(GVAR_PLAYER_REPUTATION, global_var(GVAR_PLAYER_REPUTATION) - 5);
		end
	end
end

procedure map_update_p_proc
begin
	if (not(initialized1)) then begin
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 2);
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_GUARD);
		home_tile := 14108;
		sleep_tile := 15925;
		wake_time := 600;
		sleep_time := 2100;
		initialized1 := 1;
	end
end

procedure talk_p_proc
begin
	call Get_Reaction;
	if (local_var(9) == 1) then begin
		float_msg(self_obj, message_str(SCRIPT_SETH, 320), FLOAT_MSG_RED);
		hostile1 := 1;
	end
	else begin
		if ((global_var(GVAR_TANDI_STATUS) == 1) and (get_critter_stat(dude_obj, 4) > 3)) then begin
			set_global_var(GVAR_TALKED_ABOUT_TANDI, 1);
			if (local_var(8) == 0) then begin
				set_local_var(8, 1);
				start_gdialog(SCRIPT_SETH, self_obj, 4, -1, -1);
				gsay_start;
				call TanSeth00;
				gsay_end;
				end_dialogue;
			end
			else begin
				start_gdialog(SCRIPT_SETH, self_obj, 4, -1, -1);
				gsay_start;
				call TanSeth01;
				gsay_end;
				end_dialogue;
			end
		end
		else begin
			if (local_var(5) == 1) then begin
				float_msg(self_obj, message_str(SCRIPT_GENVAULT, 166), FLOAT_MSG_NORMAL);
			end
			else begin
				if (global_var(GVAR_ENEMY_SHADY_SANDS) == 1) then begin
					float_msg(self_obj, message_str(SCRIPT_SETH, 101), FLOAT_MSG_RED);
					hostile1 := 1;
				end
				else begin
					start_gdialog(SCRIPT_SETH, self_obj, 4, -1, -1);
					gsay_start;
					call pick_start;
					gsay_end;
					end_dialogue;
				end
			end
		end
	end
end

procedure destroy_p_proc
begin
	if (source_obj == dude_obj) then begin
		set_global_var(GVAR_ENEMY_SHADY_SANDS, 1);
	end
	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 travel
begin
	game_time_advance(game_ticks(60 * 30));
	if ((global_var(GVAR_RADSCORPION_SEED) == 0) and (global_var(GVAR_RADSCORPION_SEED) != 2)) then begin
		set_global_var(GVAR_RADSCORPION_SEED, 1);
	end
	load_map("caves.map", 1);
end

procedure no_travel
begin
	if (global_var(GVAR_RADSCORPION_SEED) == 2) then begin
		gsay_message(SCRIPT_SETH, 203, NEUTRAL_REACTION);
	end
	else begin
		gsay_message(SCRIPT_SETH, 128, NEUTRAL_REACTION);
	end
end

procedure pick_start
begin
	if (global_var(GVAR_SETH_STATUS) == 0) then begin
		if (local_var(1) < 2) then begin
			call Seth04;
		end
		else begin
			call Seth05;
		end
	end
	else begin
		if (global_var(GVAR_RADSCORPION_SEED) == 2) then begin
			call Big_Up_React;
			gsay_message(SCRIPT_SETH, 200, GOOD_REACTION);
			if (get_critter_stat(dude_obj, 4) > 3) then begin
				call Seth07;
			end
		end
		else begin
			if (local_var(1) < 2) then begin
				call Seth09;
			end
			else begin
				call Seth07;
			end
		end
	end
end

procedure Seth01
begin
	gsay_message(SCRIPT_SETH, 101, NEUTRAL_REACTION);
	hostile1 := 1;
end

procedure Seth02
begin
	gsay_message(SCRIPT_SETH, 102, NEUTRAL_REACTION);
	hostile1 := 1;
end

procedure Seth03
begin
	gsay_reply(SCRIPT_SETH, 103);
	set_local_var(4, 1);
	gsay_option(SCRIPT_SETH, 104, Seth13, NEUTRAL_REACTION);
	gsay_option(SCRIPT_SETH, 105, Seth08a, NEUTRAL_REACTION);
end

procedure Seth04
begin
	gsay_reply(SCRIPT_SETH, 106);
	set_global_var(GVAR_SETH_STATUS, 1);
	giq_option(4, SCRIPT_SETH, 107, Seth02, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_SETH, 108, Seth06, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_SETH, 109, Seth03, NEUTRAL_REACTION);
end

procedure Seth05
begin
	gsay_reply(SCRIPT_SETH, 110);
	set_global_var(GVAR_SETH_STATUS, 1);
	giq_option(4, SCRIPT_SETH, 111, Seth06, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_SETH, 112, Seth03, NEUTRAL_REACTION);
end

procedure Seth06
begin
	gsay_reply(SCRIPT_SETH, 113);
	call Seth08;
end

procedure Seth07
begin
	gsay_reply(SCRIPT_SETH, 114);
	call Seth08;
end

procedure Seth08
begin
	if (global_var(GVAR_RADSCORPION_SEED) == 1) then begin
		giq_option(4, SCRIPT_SETH, 115, Seth10, NEUTRAL_REACTION);
	end
	if (global_var(GVAR_TANDI_STATUS) == 1) then begin
		giq_option(4, SCRIPT_SETH, 116, Seth11, NEUTRAL_REACTION);
	end
	giq_option(4, SCRIPT_SETH, 117, Seth12, NEUTRAL_REACTION);
	if ((local_var(4) == 1) and (global_var(GVAR_RADSCORPION_SEED) != 2)) then begin
		giq_option(4, SCRIPT_SETH, 118, Seth13, NEUTRAL_REACTION);
		giq_option(-3, SCRIPT_SETH, 119, Seth13, NEUTRAL_REACTION);
	end
	giq_option(4, SCRIPT_SETH, 120, Seth08a, NEUTRAL_REACTION);
end

procedure Seth08a
begin
	if (local_var(7) == 0) then begin
		set_local_var(7, 1);
		call Seth14;
	end
	else begin
		call Sethend;
	end
end

procedure Seth09
begin
	gsay_reply(SCRIPT_SETH, 121);
	if (global_var(GVAR_RADSCORPION_SEED) == 1) then begin
		giq_option(4, SCRIPT_SETH, 115, Seth10, NEUTRAL_REACTION);
	end
	if (global_var(GVAR_TANDI_STATUS) == 1) then begin
		giq_option(4, SCRIPT_SETH, 116, Seth11, NEUTRAL_REACTION);
	end
	giq_option(4, SCRIPT_SETH, 117, Seth12, NEUTRAL_REACTION);
	if (local_var(4) == 1) then begin
		giq_option(4, SCRIPT_SETH, 118, Seth13, NEUTRAL_REACTION);
		giq_option(-3, SCRIPT_SETH, 119, Seth13, NEUTRAL_REACTION);
	end
	giq_option(4, SCRIPT_SETH, 120, Seth08a, NEUTRAL_REACTION);
end

procedure Seth10
begin
	if (global_var(GVAR_RADSCORPION_SEED) == 2) then begin
		gsay_reply(SCRIPT_SETH, 201);
	end
	else begin
		gsay_reply(SCRIPT_SETH, 122);
	end
	set_local_var(4, 1);
	if (global_var(GVAR_TANDI_STATUS) == 1) then begin
		giq_option(4, SCRIPT_SETH, 116, Seth11, NEUTRAL_REACTION);
	end
	giq_option(4, SCRIPT_SETH, 117, Seth12, NEUTRAL_REACTION);
	if (local_var(4) == 1) then begin
		giq_option(4, SCRIPT_SETH, 118, Seth13, NEUTRAL_REACTION);
		giq_option(-3, SCRIPT_SETH, 119, Seth13, NEUTRAL_REACTION);
	end
	giq_option(4, SCRIPT_SETH, 120, Seth08a, NEUTRAL_REACTION);
end

procedure Seth11
begin
	set_global_var(GVAR_RAIDERS, 1);
	gsay_reply(SCRIPT_SETH, 123);
	if (global_var(GVAR_RADSCORPION_SEED) == 1) then begin
		giq_option(4, SCRIPT_SETH, 115, Seth10, NEUTRAL_REACTION);
	end
	giq_option(4, SCRIPT_SETH, 117, Seth12, NEUTRAL_REACTION);
	if (local_var(4) == 1) then begin
		giq_option(4, SCRIPT_SETH, 118, Seth13, NEUTRAL_REACTION);
		giq_option(-3, SCRIPT_SETH, 119, Seth13, NEUTRAL_REACTION);
	end
	giq_option(4, SCRIPT_SETH, 120, Seth08a, NEUTRAL_REACTION);
end

procedure Seth12
begin
	gsay_reply(SCRIPT_SETH, 124);
	giq_option(4, SCRIPT_SETH, 115, Seth10, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_SETH, 116, Seth11, NEUTRAL_REACTION);
	if (local_var(4) == 1) then begin
		giq_option(4, SCRIPT_SETH, 118, Seth13, NEUTRAL_REACTION);
		giq_option(-3, SCRIPT_SETH, 119, Seth13, NEUTRAL_REACTION);
	end
	giq_option(4, SCRIPT_SETH, 120, Seth08a, NEUTRAL_REACTION);
end

procedure Seth13
begin
	if (global_var(GVAR_RADSCORPION_SEED) == 2) then begin
		gsay_reply(SCRIPT_SETH, 202);
	end
	else begin
		gsay_reply(SCRIPT_SETH, 125);
	end
	giq_option(4, SCRIPT_SETH, 126, travel, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_SETH, 127, no_travel, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_SETH, 130, travel, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_SETH, 131, no_travel, NEUTRAL_REACTION);
end

procedure Seth14
begin
	gsay_message(SCRIPT_SETH, 206, NEUTRAL_REACTION);
end

procedure Sethend
begin
end

procedure TanSeth00
begin
	gsay_reply(SCRIPT_SETH, 300);
	giq_option(4, SCRIPT_SETH, 301, TanSeth03, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_SETH, 302, TanSeth02, BAD_REACTION);
	giq_option(4, SCRIPT_SETH, 303, TanSeth02, NEUTRAL_REACTION);
	if (global_var(GVAR_RADSCORPION_SEED) == 1) then begin
		giq_option(4, SCRIPT_SETH, 304, TanSeth07, NEUTRAL_REACTION);
	end
end

procedure TanSeth01
begin
	gsay_reply(SCRIPT_SETH, 305);
	giq_option(4, SCRIPT_SETH, 306, TanSeth06, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_SETH, 307, TanSeth05, BAD_REACTION);
	giq_option(4, SCRIPT_SETH, 308, TanSeth06, NEUTRAL_REACTION);
	if (global_var(GVAR_RADSCORPION_SEED) == 1) then begin
		giq_option(4, SCRIPT_SETH, 309, TanSeth07, NEUTRAL_REACTION);
	end
end

procedure TanSeth02
begin
	gsay_message(SCRIPT_SETH, 310, NEUTRAL_REACTION);
end

procedure TanSeth03
begin
	set_global_var(GVAR_RAIDERS, 1);
	gsay_reply(SCRIPT_SETH, 311);
	giq_option(4, SCRIPT_SETH, 312, Sethend, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_SETH, 313, TanSeth02, BAD_REACTION);
	giq_option(4, SCRIPT_SETH, 314, TanSeth04, BAD_REACTION);
	giq_option(4, SCRIPT_SETH, 315, TanSeth02, BAD_REACTION);
end

procedure TanSeth04
begin
	gsay_reply(SCRIPT_SETH, 316);
	giq_option(4, SCRIPT_SETH, 317, TanSeth05, BAD_REACTION);
	giq_option(4, SCRIPT_SETH, 318, Sethend, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_SETH, 319, TanSeth02, BAD_REACTION);
end

procedure TanSeth05
begin
	set_local_var(9, 1);
	hostile1 := 1;
	gsay_message(SCRIPT_SETH, 320, BAD_REACTION);
end

procedure TanSeth06
begin
	gsay_message(SCRIPT_SETH, 321, NEUTRAL_REACTION);
end

procedure TanSeth07
begin
	gsay_reply(SCRIPT_SETH, 322);
	giq_option(4, SCRIPT_SETH, 323, travel, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_SETH, 324, TanSeth02, BAD_REACTION);
	giq_option(4, SCRIPT_SETH, 325, Sethend, NEUTRAL_REACTION);
end

procedure sleeping
begin
	if (local_var(5) == 1) then begin
		if (not(night_person) and (game_time_hour >= wake_time) and (game_time_hour < sleep_time) or (night_person and ((game_time_hour >= wake_time) or (game_time_hour < sleep_time)))) then begin
			if (((game_time_hour - wake_time) < 10) and ((game_time_hour - wake_time) > 0)) then begin
				if (tile_num(self_obj) != home_tile) then begin
					animate_move_obj_to_tile(self_obj, home_tile, ANIMATE_WALK);
				end
				else begin
					set_local_var(5, 0);
				end
			end
			else begin
				move_to(self_obj, home_tile, elevation(self_obj));
				if (tile_num(self_obj) == home_tile) then begin
					set_local_var(5, 0);
				end
			end
		end
	end
	else begin
		if (night_person and (game_time_hour >= sleep_time) and (game_time_hour < wake_time) or (not(night_person) and ((game_time_hour >= sleep_time) or (game_time_hour < wake_time)))) then begin
			if (((game_time_hour - sleep_time) < 10) and ((game_time_hour - sleep_time) > 0)) then begin
				if (tile_num(self_obj) != sleep_tile) then begin
					animate_move_obj_to_tile(self_obj, self_obj, ANIMATE_WALK);
				end
				else begin
					set_local_var(5, 1);
				end
			end
			else begin
				if (tile_num(self_obj) != sleep_tile) then begin
					move_to(self_obj, sleep_tile, elevation(self_obj));
				end
				else begin
					set_local_var(5, 1);
				end
			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