#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure combat_p_proc;
procedure Lighting;
procedure Tandi_Move;
procedure Where_Is_Tandi;
procedure Where_To_Start;
procedure add_party;
procedure update_party;
procedure remove_party;

variable party_elevation;
variable dude_start_hex;

export variable Ian_ptr;
export variable Dog_ptr;
export variable Tycho_ptr;
export variable Katja_ptr;
export variable Tandi_ptr;

procedure Darkness;
procedure Invasion;


procedure start
begin
	if (script_action == map_enter_proc) then begin
		if (metarule(14, 0)) then begin
			display_msg(message_str(SCRIPT_SHADYWST, 101));
		end
		set_global_var(GVAR_MARK_SHADY_1, 1);
		if ((game_time / (10 * 60 * 60 * 24)) >= global_var(GVAR_SHADY_SANDS_INVADED_DATE)) then begin
			set_global_var(GVAR_SHADY_SANDS_INVADED, 1);
		end
		if ((days_since_visited > 1) and (map_var(4) == 0) and (global_var(GVAR_TANDI_STATUS) != 2) and (global_var(GVAR_TANDI_STATUS) != 1) and (global_var(GVAR_TANDI_STATUS) != 3) and (global_var(GVAR_RADSCORPION_SEED) == 2) and (global_var(GVAR_TOTAL_RAIDERS) > 0) and (global_var(GVAR_TANDI_STATUS) != 5) and (global_var(GVAR_GARL_DEAD) == 0)) then begin
			set_global_var(GVAR_TANDI_STATUS, 1);
			set_map_var(4, 1);
		end
		else begin
			if (global_var(GVAR_TANDI_STATUS) == 5) then begin
				set_global_var(GVAR_TANDI_STATUS, 2);
				if ((map_var(1) == 0) and (map_var(3) == 0)) then begin
					set_map_var(3, 1);
					set_map_var(1, 1);
					display_msg(message_str(SCRIPT_SHADYWST, 100));
					give_exp_points(400);
				end
			end
		end
		call Lighting;
		call Where_To_Start;
	end
	else begin
		if (script_action == map_update_proc) then begin
			call Lighting;
		end
		else begin
			if (script_action == map_exit_proc) then begin
			end
			else begin
				if (script_action == combat_proc) then begin
					call combat_p_proc;
				end
			end
		end
	end
end

procedure combat_p_proc
begin
	script_overrides;
	gfade_out(600);
	move_to(dude_obj, 12107, 0);
	game_time_advance(game_ticks(1800));
	gfade_in(600);
end

procedure Lighting
begin
	variable LVar0 := 0;
	LVar0 := game_time_hour;
	if ((LVar0 >= 600) and (LVar0 < 700)) then begin
		set_light_level(LVar0 - 600 + 40);
	end
	else begin
		if ((LVar0 >= 700) and (LVar0 < 1800)) then begin
			set_light_level(100);
		end
		else begin
			if ((LVar0 >= 1800) and (LVar0 < 1900)) then begin
				set_light_level(100 - (LVar0 - 1800));
			end
			else begin
				set_light_level(40);
			end
		end
	end
end

procedure Tandi_Move
begin
	variable LVar0 := 0;
	LVar0 := game_time_hour;
	if ((LVar0 > 2100) or (LVar0 <= 600)) then begin
		Tandi_ptr := create_object_sid(16777256, 0, 0, 57);
		move_to(Tandi_ptr, 24678, 0);
	end
	else begin
		if ((LVar0 > 600) and (LVar0 <= 610)) then begin
			Tandi_ptr := create_object_sid(16777256, 0, 0, 57);
			move_to(Tandi_ptr, 18709, 0);
		end
		else begin
			if ((LVar0 > 610) and (LVar0 <= 615)) then begin
				Tandi_ptr := create_object_sid(16777256, 0, 0, 57);
				move_to(Tandi_ptr, 23232, 0);
			end
			else begin
				if (LVar0 == 650) then begin
					Tandi_ptr := create_object_sid(16777256, 0, 0, 57);
					move_to(Tandi_ptr, 23232, 0);
				end
				else begin
					if ((LVar0 > 650) and (LVar0 <= 1000)) then begin
						Tandi_ptr := create_object_sid(16777256, 0, 0, 57);
						move_to(Tandi_ptr, 15683, 0);
					end
					else begin
						if ((LVar0 > 1000) and (LVar0 <= 1200)) then begin
							Tandi_ptr := create_object_sid(16777256, 0, 0, 57);
							move_to(Tandi_ptr, 18709, 0);
						end
						else begin
							if ((LVar0 > 1200) and (LVar0 <= 1300)) then begin
								Tandi_ptr := create_object_sid(16777256, 0, 0, 57);
								move_to(Tandi_ptr, 24678, 0);
							end
							else begin
								if ((LVar0 > 1300) and (LVar0 <= 1305)) then begin
									Tandi_ptr := create_object_sid(16777256, 0, 0, 57);
									move_to(Tandi_ptr, 23232, 0);
								end
								else begin
									if (LVar0 == 1905) then begin
										Tandi_ptr := create_object_sid(16777256, 0, 0, 57);
										move_to(Tandi_ptr, 23232, 0);
									end
									else begin
										if ((LVar0 > 1905) and (LVar0 <= 2100)) then begin
											Tandi_ptr := create_object_sid(16777256, 0, 0, 57);
											move_to(Tandi_ptr, 18709, 0);
										end
									end
								end
							end
						end
					end
				end
			end
		end
	end
end

procedure Where_Is_Tandi
begin
	if (global_var(GVAR_TANDI_STATUS) == 1) then begin
	end
	else begin
		if (global_var(GVAR_TANDI_STATUS) == 0) then begin
			call Tandi_Move;
		end
		else begin
			if (global_var(GVAR_TANDI_STATUS) == 2) then begin
				call Tandi_Move;
			end
		end
	end
end

procedure Where_To_Start
begin
	if (global_var(GVAR_LOAD_MAP_INDEX) == 1) then begin
		override_map_start(107, 60, 0, 2);
	end
	else begin
		if (global_var(GVAR_LOAD_MAP_INDEX) == 2) then begin
			override_map_start(65, 93, 0, 4);
		end
		else begin
			if (global_var(GVAR_LOAD_MAP_INDEX) == 3) then begin
				override_map_start(107, 76, 0, 5);
			end
			else begin
				override_map_start(107, 60, 0, 2);
			end
		end
	end
end

procedure add_party
begin
	variable LVar0 := 0;
	variable LVar1 := 0;
	party_elevation := elevation(dude_obj);
	if (global_var(GVAR_TANDI_STATUS) == 5) then begin
		if (Tandi_ptr == 0) then begin
		end
		critter_add_trait(Tandi_ptr, 1, 6, 0);
	end
end

procedure update_party
begin
	variable LVar0 := 0;
	variable LVar1 := 0;
	if (elevation(dude_obj) != party_elevation) then begin
		party_elevation := elevation(dude_obj);
		if (global_var(GVAR_IAN_STATUS) == 2) then begin
			if (Ian_ptr == 0) then begin
			end
			else begin
				move_to(Ian_ptr, tile_num_in_direction(tile_num(dude_obj), 1, 2), elevation(dude_obj));
			end
		end
		if (global_var(GVAR_DOG_EMPATHY)) then begin
			if (Dog_ptr == 0) then begin
			end
			else begin
				move_to(Dog_ptr, tile_num_in_direction(tile_num(dude_obj), 2, 1), elevation(dude_obj));
			end
		end
		if (global_var(GVAR_HERNANDEZ_STATUS) == 2) then begin
			if (Tycho_ptr == 0) then begin
			end
			else begin
				move_to(Tycho_ptr, tile_num_in_direction(tile_num(dude_obj), 3, 2), elevation(dude_obj));
			end
		end
		if (global_var(GVAR_KATJA_STATUS) == 2) then begin
			if (Katja_ptr == 0) then begin
			end
			else begin
				move_to(Katja_ptr, tile_num_in_direction(tile_num(dude_obj), 4, 2), elevation(dude_obj));
			end
		end
		if (global_var(GVAR_TANDI_STATUS) == 5) then begin
			if (Tandi_ptr == 0) then begin
			end
			else begin
				move_to(Tandi_ptr, tile_num_in_direction(tile_num(dude_obj), 4, 4), elevation(dude_obj));
			end
		end
	end
end

procedure remove_party
begin
	if (global_var(GVAR_IAN_STATUS) == 2) then begin
		set_global_var(GVAR_IAN_STATUS, 2);
	end
	if (global_var(GVAR_DOG_EMPATHY)) then begin
		set_global_var(GVAR_DOG_EMPATHY, 1);
	end
	if (global_var(GVAR_HERNANDEZ_STATUS) == 2) then begin
		set_global_var(GVAR_HERNANDEZ_STATUS, 2);
	end
	if (global_var(GVAR_KATJA_STATUS) == 2) then begin
		set_global_var(GVAR_KATJA_STATUS, 2);
	end
	if (global_var(GVAR_TANDI_STATUS) == 5) then begin
	end
end

procedure Darkness
begin
	set_light_level(40);
end

procedure Invasion
begin
	if (not(global_var(GVAR_MASTER_BLOWN) == 2)) then begin
		if (global_var(GVAR_NECROPOLIS_INVADED_DATE) > (game_time / (10 * 60 * 60 * 24))) then begin
			set_global_var(GVAR_NECROPOLIS_INVADED, 1);
		end
		if (global_var(GVAR_THE_HUB_INVADED_DATE) > (game_time / (10 * 60 * 60 * 24))) then begin
			set_global_var(GVAR_HUB_INVADED, 1);
		end
		if (global_var(GVAR_BROTHERHOOD_INVADED_DATE) > (game_time / (10 * 60 * 60 * 24))) then begin
			set_global_var(GVAR_BROTHERHOOD_INVADED, 1);
		end
		if (global_var(GVAR_JUNKTOWN_INVADED_DATE) > (game_time / (10 * 60 * 60 * 24))) then begin
			set_global_var(GVAR_JUNKTOWN_INVADED, 1);
		end
		if (global_var(GVAR_SHADY_SANDS_INVADED_DATE) > (game_time / (10 * 60 * 60 * 24))) then begin
			set_global_var(GVAR_SHADY_SANDS_INVADED, 1);
		end
		if (global_var(GVAR_VAULT_13_INVADED_DATE) <= 0) then begin
			set_global_var(GVAR_VAULT_INVADED, 1);
		end
		if (global_var(GVAR_FOLLOWERS_INVADED_DATE) > (game_time / (10 * 60 * 60 * 24))) then begin
			set_global_var(GVAR_FOLLOWERS_INVADED, 1);
		end
	end
end