#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure lighting;
procedure Kill_Neal;

variable HEREBEFORE;
variable time;
variable item;

export variable Killian_ptr;
export variable Gizmo_ptr;
export variable growling;
export variable dog_is_angry := 1;
export variable smartass;
export variable smartass2;
export variable Phil_approaches;
export variable KillSafe_ptr;
export variable assassin_seed;
export variable helped_Killian;
export variable Killian_barter_var;
export variable Killian_store1_ptr;
export variable Killian_store2_ptr;
export variable Killian_store3_ptr;
export variable weapon_checked;
export variable sneak_checked;
export variable times_caught_sneaking;
export variable Gizmo_is_angry;
export variable show_to_door;
export variable removal_ptr;
export variable payment;
export variable messing_with_SkumDoor;
export variable Neal_closing_door;
export variable Neal_ptr;
export variable Skul_target;
export variable SkumDoor_ptr;
export variable Trish_ptr;
export variable challenger_ptr;
export variable fight;
export variable Saul_loses;
export variable Saul_wins;
export variable shot_challenger;

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
	variable LVar0 := 0;
	variable LVar1 := 0;
	if (script_action == map_enter_proc) then begin
		set_global_var(GVAR_MARK_JUNK_3, 1);
		call lighting;
		if (global_var(GVAR_LOAD_MAP_INDEX) == 1) then begin
			override_map_start(99, 94, 0, 3);
		end
		else begin
			if ((global_var(GVAR_LOAD_MAP_INDEX) == 4) and (metarule(22, 0) == 0)) then begin
				gfade_in(600);
				override_map_start(120, 87, 0, 5);
				LVar0 := create_object_sid(16777295, 0, 0, 47);
				critter_add_trait(LVar0, 1, 6, 0);
				item := create_object_sid(18, 0, 0, -1);
				critter_attempt_placement(LVar0, 17522, 0);
				add_obj_to_inven(LVar0, item);
				LVar1 := create_object_sid(16777296, 0, 0, 518);
				critter_add_trait(LVar1, 1, 6, 0);
				item := create_object_sid(94, 0, 0, -1);
				critter_attempt_placement(LVar1, 17323, 0);
				add_obj_to_inven(LVar1, item);
				item := create_object_sid(95, 0, 0, -1);
				add_obj_to_inven(LVar1, item);
				item := create_object_sid(100, 0, 0, -1);
				add_obj_to_inven(LVar1, item);
			end
			else begin
				if ((global_var(GVAR_LOAD_MAP_INDEX) == 7) and (metarule(22, 0) == 0)) then begin
					call Kill_Neal;
				end
				else begin
					override_map_start(99, 94, 0, 5);
				end
			end
		end
		if ((global_var(GVAR_DESTROY_VATS_11) != 0) and (Neal_ptr != 0)) then begin
			if ((global_var(GVAR_DESTROY_VATS_11) < (game_time / (10 * 60 * 60 * 24))) and (metarule(22, 0) == 0)) then begin
				kill_critter(Neal_ptr, 49);
			end
		end
	end
	else begin
		if (script_action == map_update_proc) then begin
			call update_party;
			call lighting;
			if (metarule(22, 0) == 0) then begin
				if ((global_var(GVAR_GIZMO_DEAD) == 1) and (map_var(3) == 0) and (combat_is_initialized == 0) and (global_var(GVAR_CURE_JARVIS_9) == 0)) then begin
					display_msg(message_str(SCRIPT_GIZMO, 217));
					give_exp_points(600);
					set_global_var(GVAR_PLAYER_REPUTATION, global_var(GVAR_PLAYER_REPUTATION) + 3);
					set_map_var(3, 1);
					debug_msg("gave_exp");
					if (global_var(GVAR_LOAD_MAP_INDEX) == 4) then begin
						if (LVar0 != 0) then begin
							set_obj_visibility(LVar0, 1);
						end
						if (LVar1 != 0) then begin
							set_obj_visibility(LVar1, 1);
						end
						set_global_var(GVAR_CAPTURE_GIZMO, 2);
						load_map(10, 5);
					end
				end
				if ((map_var(2) == 1) and (combat_is_initialized == 0)) then begin
					if (map_var(0) <= 0) then begin
						set_global_var(GVAR_BUST_SKULZ, 2);
						set_map_var(2, 2);
						set_global_var(GVAR_DESTROY_VATS_11, 0);
						display_msg(message_str(SCRIPT_LARS, 154));
						set_global_var(GVAR_PLAYER_REPUTATION, global_var(GVAR_PLAYER_REPUTATION) + 1);
						give_exp_points(300);
					end
					else begin
						if ((map_var(1) <= 0) and (global_var(GVAR_DESTROY_VATS_12) == 1)) then begin
							set_global_var(GVAR_DESTROY_VATS_13, 2);
							set_map_var(2, 2);
							display_msg(message_str(SCRIPT_VINNIE, 164));
							give_exp_points(300);
							set_global_var(GVAR_PLAYER_REPUTATION, global_var(GVAR_PLAYER_REPUTATION) - 1);
							if (global_var(GVAR_ENEMY_JUNKTOWN) == 0) then begin
								set_global_var(GVAR_ENEMY_JUNKTOWN, 1);
								set_global_var(GVAR_PLAYER_REPUTATION, global_var(GVAR_PLAYER_REPUTATION) - 5);
							end
						end
					end
				end
				if ((global_var(GVAR_DESTROY_VATS_11) != 0) and (Neal_ptr != 0)) then begin
					if (global_var(GVAR_DESTROY_VATS_11) < (game_time / (10 * 60 * 60 * 24))) then begin
						kill_critter(Neal_ptr, 49);
					end
				end
				if (removal_ptr != 0) then begin
					destroy_object(removal_ptr);
					removal_ptr := 0;
				end
			end
		end
		else begin
			if (script_action == map_exit_proc) then begin
				if (challenger_ptr != 0) then begin
					destroy_object(challenger_ptr);
				end
				fight := 0;
				Saul_wins := 0;
				Saul_loses := 0;
			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 Kill_Neal
begin
	variable LVar0 := 0;
	override_map_start(81, 103, 0, 5);
	set_map_var(2, 1);
	LVar0 := create_object_sid(16777318, 20277, 0, 385);
	item := create_object_sid(8, 0, 0, -1);
	add_obj_to_inven(LVar0, item);
	item := create_object_sid(74, 0, 0, -1);
	add_obj_to_inven(LVar0, item);
	item := create_object_sid(4, 0, 0, -1);
	add_obj_to_inven(LVar0, item);
	LVar0 := create_object_sid(16777240, 20476, 0, 390);
	item := create_object_sid(8, 0, 0, -1);
	add_obj_to_inven(LVar0, item);
	item := create_object_sid(4, 0, 0, -1);
	add_obj_to_inven(LVar0, item);
	LVar0 := create_object_sid(16777315, 20279, 0, 390);
	item := create_object_sid(4, 0, 0, -1);
	add_obj_to_inven(LVar0, item);
	LVar0 := create_object_sid(16777315, 20482, 0, 390);
	item := create_object_sid(4, 0, 0, -1);
	add_obj_to_inven(LVar0, item);
	set_map_var(0, 4);
	if (global_var(GVAR_DESTROY_VATS_15) == 1) then begin
		set_map_var(1, 4);
		LVar0 := create_object_sid(16777218, 22083, 0, 37);
		item := create_object_sid(8, 0, 0, -1);
		add_obj_to_inven(LVar0, item);
		add_timer_event(LVar0, game_ticks(2), 4);
		LVar0 := create_object_sid(16777218, 22485, 0, 37);
		item := create_object_sid(8, 0, 0, -1);
		add_obj_to_inven(LVar0, item);
		LVar0 := create_object_sid(16777218, 21082, 0, 37);
		item := create_object_sid(7, 0, 0, -1);
		add_obj_to_inven(LVar0, item);
		LVar0 := create_object_sid(16777218, 22885, 0, 37);
		item := create_object_sid(7, 0, 0, -1);
		add_obj_to_inven(LVar0, item);
	end
	game_time_advance(game_ticks(60 * (60 - (game_time_hour % 100))));
	if (game_time_hour != 300) then begin
		if (game_time_hour < 300) then begin
			game_time_advance(game_ticks(36 * (300 - game_time_hour)));
		end
		else begin
			game_time_advance(game_ticks(36 * (2700 - game_time_hour)));
		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