#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure Lighting;
procedure Darkness;
procedure add_party;
procedure update_party;
procedure remove_party;

variable party_elevation;

export variable Ian_ptr;
export variable Dog_ptr;
export variable Tycho_ptr;

variable Player_Elevation;

procedure Invasion;


procedure start
begin
	if (script_action == map_enter_proc) then begin
		set_light_level(75);
		Player_Elevation := elevation(dude_obj);
		if (global_var(GVAR_LOAD_MAP_INDEX) == 0) then begin
			override_map_start(137, 103, 0, 3);
		end
		else begin
			if (global_var(GVAR_LOAD_MAP_INDEX) == 4) then begin
				override_map_start(98, 60, 0, 3);
			end
			else begin
				if (global_var(GVAR_LOAD_MAP_INDEX) == 1) then begin
					override_map_start(94, 98, 1, 3);
				end
				else begin
					override_map_start(137, 103, 0, 3);
				end
			end
		end
		if (global_var(GVAR_COUNTDOWN_TO_DESTRUCTION) != 0) then begin
			display_msg(message_str(SCRIPT_MASTER1, 100) + (240 - ((game_time / 10) - global_var(GVAR_COUNTDOWN_TO_DESTRUCTION))) + message_str(SCRIPT_MASTER1, 101));
			if (((game_time / 10) - global_var(GVAR_COUNTDOWN_TO_DESTRUCTION)) > 240) then begin
				display_msg(message_str(SCRIPT_MASTER1, 102));
				play_gmovie(6);
				metarule(13, 0);
			end
		end
		call add_party;
	end
	if (script_action == map_update_proc) then begin
		set_light_level(75);
		if (global_var(GVAR_COUNTDOWN_TO_DESTRUCTION) != 0) then begin
			display_msg(message_str(SCRIPT_MASTER1, 104) + (240 - ((game_time / 10) - global_var(GVAR_COUNTDOWN_TO_DESTRUCTION))) + message_str(SCRIPT_MASTER1, 105));
			if (((game_time / 10) - global_var(GVAR_COUNTDOWN_TO_DESTRUCTION)) > 240) then begin
				display_msg(message_str(SCRIPT_MASTER1, 106));
				play_gmovie(6);
				metarule(13, 0);
			end
		end
		call update_party;
	end
	if (script_action == map_exit_proc) then begin
		if (global_var(GVAR_COUNTDOWN_TO_DESTRUCTION) != 0) then begin
			display_msg(message_str(SCRIPT_MASTER1, 108) + (240 - ((game_time / 10) - global_var(GVAR_COUNTDOWN_TO_DESTRUCTION))) + message_str(SCRIPT_MASTER1, 109));
			if (((game_time / 10) - global_var(GVAR_COUNTDOWN_TO_DESTRUCTION)) > 240) then begin
				display_msg(message_str(SCRIPT_MASTER1, 110));
				play_gmovie(6);
				metarule(13, 0);
			end
		end
		call remove_party;
	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;
	party_elevation := elevation(dude_obj);
	if (global_var(GVAR_IAN_STATUS) == 2) then begin
		if (not(Ian_ptr)) then begin
			Ian_ptr := create_object_sid(16777233, 0, 0, 235);
			critter_attempt_placement(Ian_ptr, tile_num_in_direction(tile_num(dude_obj), 1, 2), elevation(dude_obj));
		end
		critter_add_trait(Ian_ptr, 1, 6, 0);
		LVar0 := create_object_sid(74, 0, 0, -1);
		add_obj_to_inven(Ian_ptr, LVar0);
		wield_obj_critter(Ian_ptr, LVar0);
		LVar0 := create_object_sid(8, 0, 0, -1);
		add_obj_to_inven(Ian_ptr, LVar0);
	end
	if (global_var(GVAR_DOG_EMPATHY)) then begin
		if (not(Dog_ptr)) then begin
			Dog_ptr := create_object_sid(16777252, 0, 0, 229);
			critter_attempt_placement(Dog_ptr, tile_num_in_direction(tile_num(dude_obj), 2, 2), elevation(dude_obj));
		end
		critter_add_trait(Dog_ptr, 1, 6, 0);
	end
	if (global_var(GVAR_HERNANDEZ_STATUS) == 2) then begin
		if (not(Tycho_ptr)) then begin
			Tycho_ptr := create_object_sid(16777218, 0, 0, 389);
			critter_attempt_placement(Tycho_ptr, tile_num_in_direction(tile_num(dude_obj), 3, 2), elevation(dude_obj));
		end
		critter_add_trait(Tycho_ptr, 1, 6, 0);
		LVar0 := create_object_sid(1, 0, 0, -1);
		add_obj_to_inven(Tycho_ptr, LVar0);
		wield_obj_critter(Tycho_ptr, LVar0);
		LVar0 := create_object_sid(94, 0, 0, -1);
		add_obj_to_inven(Tycho_ptr, LVar0);
	end
end

procedure update_party
begin
	if (elevation(dude_obj) != party_elevation) then begin
		party_elevation := elevation(dude_obj);
		if (global_var(GVAR_IAN_STATUS) == 2) then begin
			move_to(Ian_ptr, tile_num_in_direction(tile_num(dude_obj), 1, 2), elevation(dude_obj));
		end
		if (global_var(GVAR_DOG_EMPATHY)) then begin
			move_to(Dog_ptr, tile_num_in_direction(tile_num(dude_obj), 2, 1), elevation(dude_obj));
		end
		if (global_var(GVAR_HERNANDEZ_STATUS) == 2) then begin
			move_to(Tycho_ptr, tile_num_in_direction(tile_num(dude_obj), 3, 2), elevation(dude_obj));
		end
	end
end

procedure remove_party
begin
	if (global_var(GVAR_IAN_STATUS) == 2) then begin
		destroy_object(Ian_ptr);
		set_global_var(GVAR_IAN_STATUS, 2);
	end
	if (global_var(GVAR_DOG_EMPATHY)) then begin
		destroy_object(Dog_ptr);
		set_global_var(GVAR_DOG_EMPATHY, 1);
	end
	if (global_var(GVAR_HERNANDEZ_STATUS) == 2) then begin
		destroy_object(Tycho_ptr);
		set_global_var(GVAR_HERNANDEZ_STATUS, 2);
	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) > (game_time / (10 * 60 * 60 * 24))) 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