#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure map_enter_p_proc;
procedure map_update_p_proc;
procedure map_exit_p_proc;
procedure Darkness;
procedure cheat_mode;
procedure cheat2;
procedure TagInven;
procedure NamedInven;
procedure base_inventory;
procedure endgame_part1;
procedure endgame_part2;
procedure endgame_part3;

variable endgame_running;

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;
export variable vault_door_ptr;

variable new_obj;
variable Overseer_ptr;

procedure Lighting;
procedure Invasion;


procedure start
begin
	if (script_action == map_enter_proc) then begin
		call map_enter_p_proc;
	end
	else begin
		if (script_action == map_update_proc) then begin
			call map_update_p_proc;
		end
		else begin
			if (script_action == map_exit_proc) then begin
				call map_exit_p_proc;
			end
		end
	end
end

procedure map_enter_p_proc
begin
	if (metarule(14, 0)) then begin
		override_map_start(90, 88, 0, 2);
		call base_inventory;
		call TagInven;
		call Darkness;
	end
	else begin
		if (global_var(GVAR_MASTER_BLOWN) and global_var(GVAR_VATS_BLOWN) and (endgame_running == 0)) then begin
			call endgame_part1;
		end
		else begin
			override_map_start(136, 129, 0, 1);
			if (global_var(GVAR_VAULT_13_INVADED_DATE) <= 0) then begin
				set_global_var(GVAR_VAULT_INVADED, 1);
			end
			call Darkness;
		end
	end
	call add_party;
end

procedure map_update_p_proc
begin
	if (endgame_running == 1) then begin
		call endgame_part2;
	end
	else begin
		if (endgame_running == 2) then begin
			call endgame_part3;
		end
	end
end

procedure map_exit_p_proc
begin
	call remove_party;
end

procedure Darkness
begin
	set_light_level(40);
end

procedure cheat_mode
begin
	new_obj := create_object_sid(3, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(12, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(36, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(36, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(36, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(40, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(40, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(40, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(40, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(40, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(40, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(40, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(40, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(40, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(40, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(40, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(54, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(30, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	override_map_start(90, 88, 0, 2);
end

procedure cheat2
begin
	new_obj := create_object_sid(117, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(3, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(116, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(9, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(36, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(36, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(36, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(38, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(38, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(109, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(109, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(77, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(84, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(120, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(162, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(163, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(163, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(87, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(87, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(25, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(25, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(25, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(26, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(26, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(26, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(7, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(40, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(40, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(51, 0, 0, -1);
	add_mult_objs_to_inven(dude_obj, new_obj, 5);
	new_obj := create_object_sid(41, 0, 0, -1);
	add_mult_objs_to_inven(dude_obj, new_obj, 5000);
end

procedure TagInven
begin
	if (((has_skill(dude_obj, 9) - ((get_critter_stat(dude_obj, 1) + get_critter_stat(dude_obj, 5)) / 2) - 20 + (has_trait(2, dude_obj, 15) * 10)) >= 20) or ((has_skill(dude_obj, 10) - get_critter_stat(dude_obj, 5) - 20 + (has_trait(2, dude_obj, 15) * 10)) >= 20)) then begin
		new_obj := create_object_sid(84, 0, 0, -1);
		add_obj_to_inven(dude_obj, new_obj);
	end
	if ((has_skill(dude_obj, 3) - ((get_critter_stat(dude_obj, 5) + get_critter_stat(dude_obj, 0)) / 2) - 65 + (has_trait(2, dude_obj, 15) * 10) + (has_trait(2, dude_obj, 10) * 10)) >= 20) then begin
		new_obj := create_object_sid(21, 0, 0, -1);
		add_obj_to_inven(dude_obj, new_obj);
	end
	if ((has_skill(dude_obj, 6) - ((get_critter_stat(dude_obj, 1) + get_critter_stat(dude_obj, 4)) / 2) - 30 - (has_trait(2, dude_obj, 10) * 20) + (has_trait(2, dude_obj, 15) * 10) - (has_trait(2, dude_obj, 10) * 15)) >= 20) then begin
		new_obj := create_object_sid(40, 0, 0, -1);
		add_mult_objs_to_inven(dude_obj, new_obj, 2);
	end
	if ((has_skill(dude_obj, 7) - ((get_critter_stat(dude_obj, 1) + get_critter_stat(dude_obj, 4)) / 2) - 15 - (has_trait(2, dude_obj, 10) * 20) + (has_trait(2, dude_obj, 15) * 10) - (has_trait(2, dude_obj, 10) * 15)) >= 20) then begin
		new_obj := create_object_sid(47, 0, 0, -1);
		add_obj_to_inven(dude_obj, new_obj);
	end
	if ((has_skill(dude_obj, 0) - get_critter_stat(dude_obj, 5) - 35 + (has_trait(2, dude_obj, 10) * 10) + (has_trait(2, dude_obj, 15) * 10)) >= 20) then begin
		new_obj := create_object_sid(29, 0, 0, -1);
		add_obj_to_inven(dude_obj, new_obj);
	end
	if ((has_skill(dude_obj, 5) - get_critter_stat(dude_obj, 5) - 40 + (has_trait(2, dude_obj, 10) * 10) + (has_trait(2, dude_obj, 15) * 10)) >= 20) then begin
		new_obj := create_object_sid(45, 0, 0, -1);
		add_mult_objs_to_inven(dude_obj, new_obj, 2);
	end
	if ((has_skill(dude_obj, 13) - get_critter_stat(dude_obj, 4) - 20 + (has_trait(2, dude_obj, 15) * 10)) >= 20) then begin
		new_obj := create_object_sid(75, 0, 0, -1);
		add_obj_to_inven(dude_obj, new_obj);
	end
	if ((has_skill(dude_obj, 17) - ((get_critter_stat(dude_obj, 4) + get_critter_stat(dude_obj, 2)) / 2) - 5 + (has_trait(2, dude_obj, 15) * 10)) >= 20) then begin
		new_obj := create_object_sid(126, 0, 0, -1);
		add_mult_objs_to_inven(dude_obj, new_obj, 3);
	end
	if ((has_skill(dude_obj, 12) - (2 * get_critter_stat(dude_obj, 4)) - 25 - (has_trait(2, dude_obj, 15) * 10)) >= 20) then begin
		new_obj := create_object_sid(53, 0, 0, -1);
		add_mult_objs_to_inven(dude_obj, new_obj, 2);
		new_obj := create_object_sid(87, 0, 0, -1);
		add_mult_objs_to_inven(dude_obj, new_obj, 2);
	end
end

procedure NamedInven
begin
	if (proto_data(obj_pid(dude_obj), 1) == "Max Stone") then begin
		new_obj := create_object_sid(30, 0, 0, -1);
		add_obj_to_inven(dude_obj, new_obj);
		new_obj := create_object_sid(21, 0, 0, -1);
		add_obj_to_inven(dude_obj, new_obj);
	end
	if (proto_data(obj_pid(dude_obj), 1) == "Natalia") then begin
		new_obj := create_object_sid(45, 0, 0, -1);
		add_obj_to_inven(dude_obj, new_obj);
		new_obj := create_object_sid(84, 0, 0, -1);
		add_obj_to_inven(dude_obj, new_obj);
	end
	if (proto_data(obj_pid(dude_obj), 1) == "Albert") then begin
		new_obj := create_object_sid(40, 0, 0, -1);
		add_obj_to_inven(dude_obj, new_obj);
		new_obj := create_object_sid(53, 0, 0, -1);
		add_obj_to_inven(dude_obj, new_obj);
	end
end

procedure base_inventory
begin
	new_obj := create_object_sid(4, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(8, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(29, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(29, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(40, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(40, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(40, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(40, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(79, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
	new_obj := create_object_sid(79, 0, 0, -1);
	add_obj_to_inven(dude_obj, new_obj);
end

procedure endgame_part1
begin
	kill_critter_type(16777264, 0);
	set_light_level(100);
	override_map_start(90, 96, 0, 5);
	set_global_var(GVAR_CALM_REBELS_2, 1);
	endgame_slideshow;
	gfade_out(1);
	endgame_running := 2;
end

procedure endgame_part2
begin
	endgame_running := 2;
end

procedure endgame_part3
begin
	variable LVar0 := 0;
	if (global_var(GVAR_CALM_REBELS_2) == 0) then begin
		LVar0 := create_object_sid(16777263, 0, 0, 55);
		set_map_var(0, LVar0);
		anim(LVar0, 1000, 2);
		critter_attempt_placement(LVar0, 18290, elevation(dude_obj));
		add_timer_event(dude_obj, 10, 5);
		endgame_running := 3;
	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 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 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