#include <define.h>

//#define NAME                    SCRIPT_

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



export variable Set_Pointer;
export variable Garret_ptr;
export variable Fridge_ptr;
export variable Shotgun_ptr;
export variable Shells1_ptr;
export variable Shells2_ptr;
export variable Flare1_ptr;
export variable Flare2_ptr;
export variable Flare3_ptr;
export variable Flare4_ptr;
export variable Cola1_ptr;
export variable Cola2_ptr;
export variable Cola3_ptr;
export variable Cola4_ptr;
export variable Manhole_Pointer_Top;
export variable Manhole_Pointer_Middle;
export variable Manhole_Pointer_Bottom;
export variable Use_Manhole_Top;
export variable Use_Manhole_Bottom;
export variable Use_Manhole_Middle;
export variable set_gone;
export variable set_dead;

variable fugee_ptr;
variable fugee_hex;

procedure start;
#include <push.h>
procedure Lighting;
procedure Darkness;
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 Invasion;


procedure start
begin
	if (script_action == map_enter_proc) then begin
		if (global_var(GVAR_LOAD_MAP_INDEX) == 2) then begin
			override_map_start(153, 100, 1, 0);
		end
		else begin
			if (global_var(GVAR_LOAD_MAP_INDEX) == 12) then begin
				override_map_start(103, 58, 0, 2);
			end
			else begin
				if (global_var(GVAR_LOAD_MAP_INDEX) == 13) then begin
					override_map_start(141, 134, 0, 5);
				end
				else begin
					override_map_start(153, 100, 1, 0);
				end
			end
		end
		set_global_var(GVAR_MARK_NECROP_2, 1);
		if ((game_time / (10 * 60 * 60 * 24)) >= global_var(GVAR_NECROPOLIS_INVADED_DATE)) then begin
			set_global_var(GVAR_NECROPOLIS_INVADED, 1);
		end
		if (global_var(GVAR_NECROP_MUTANTS_KILLED) == 2) then begin
			if (((game_time / (10 * 60 * 60 * 24)) - global_var(GVAR_HUB_FILLER_28)) > 29) then begin
				set_global_var(GVAR_NECROPOLIS_INVADED, 1);
			end
		end
		if ((global_var(GVAR_NECROPOLIS_INVADED) == 1) and (global_var(GVAR_MASTER_BLOWN) == 0) and (metarule(22, 0) == 0)) then begin
			kill_critter_type(16777320, 1);
			kill_critter_type(16777321, 1);
			kill_critter_type(16777521, 1);
			kill_critter_type(16777322, 1);
			kill_critter_type(16777230, 1);
			kill_critter_type(16777232, 1);
			if (map_var(10) == 0) then begin
				set_map_var(10, 1);
				fugee_ptr := create_object_sid(16777401, 0, 0, 74);
				fugee_hex := 28689;
				critter_attempt_placement(fugee_ptr, fugee_hex, 1);
				critter_add_trait(fugee_ptr, 1, 6, 0);
				fugee_ptr := create_object_sid(16777404, 0, 0, 912);
				critter_add_trait(fugee_ptr, 1, 5, 48);
				fugee_hex := 16886;
				critter_attempt_placement(fugee_ptr, fugee_hex, 1);
				fugee_ptr := create_object_sid(16777404, 0, 0, 912);
				critter_add_trait(fugee_ptr, 1, 5, 48);
				fugee_hex := 16084;
				critter_attempt_placement(fugee_ptr, fugee_hex, 1);
				fugee_ptr := create_object_sid(16777404, 0, 0, 912);
				critter_add_trait(fugee_ptr, 1, 5, 48);
				fugee_hex := 20111;
				critter_attempt_placement(fugee_ptr, fugee_hex, 1);
			end
		end
		else begin
			if (global_var(GVAR_WATER_CHIP_6) == 1) then begin
				kill_critter_type(16777320, 1);
				kill_critter_type(16777321, 1);
			end
		end
		if ((global_var(GVAR_NECROP_WATER_CHIP_TAKEN) == 1) and (metarule(22, 0) == 0)) then begin
			if (((game_time / (10 * 60 * 60 * 24)) - global_var(GVAR_TIME_CHIP_GONE)) > 7) then begin
				if (global_var(GVAR_NECROP_WATER_PUMP_FIXED) != 2) then begin
					kill_critter_type(16777320, 2);
					kill_critter_type(16777321, 2);
					kill_critter_type(16777521, 2);
					kill_critter_type(16777322, 2);
					kill_critter_type(16777230, 2);
					kill_critter_type(16777232, 2);
				end
			end
		end
		if (global_var(GVAR_GARRET_STATUS) == 3) then begin
			kill_critter_type(16777321, 0);
		end
		call add_party;
	end
	else begin
		if (script_action == map_update_proc) then begin
			if (elevation(dude_obj) == 0) then begin
				call Darkness;
			end
			else begin
				call Lighting;
			end
			if (Use_Manhole_Top) then begin
				animate_stand_reverse_obj(Manhole_Pointer_Top);
			end
			if (Use_Manhole_Middle) then begin
				animate_stand_reverse_obj(Manhole_Pointer_Middle);
			end
			if (Use_Manhole_Bottom) then begin
				animate_stand_reverse_obj(Manhole_Pointer_Bottom);
			end
			call update_party;
		end
		else begin
			if (script_action == map_exit_proc) then begin
				call remove_party;
			end
		end
	end
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 Darkness
begin
	set_light_level(40);
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 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