function on_game_load()

  info_from_cap()
	clean_lock()
  spawn_milstalker()
  unlock_restrictions()
  railway_spawn()
  remove_sr()
  yantar_update()
  spawn_traders()
  give_story_info()
  check_btr_status()
  
end

function on_actor_update()

 monthly_update()
 update_blowout()

end

function monthly_update()

 if level.get_time_days()~=30 or level.get_time_days()~=31 then
   check_repeatedly()
  check_hourly()
 end

 if level.get_time_days()==30 or level.get_time_days()==31 then
   amk.del_variable("next_event_time")
  amk.del_variable("ol_next_alife_spawn")
 end
end

function update_blowout()

 if level.get_time_days()==1 and
  not has_alife_info("surge_day_active") then
   amk_mod.start_blow_timer()
  db.actor:give_info_portion("surge_day_active")
 end

 if level.get_time_days()~=1 and
   has_alife_info("surge_day_active") then
  db.actor:disable_info_portion("surge_day_active")
 end
 
 if amk.load_variable("blowout",0)>0 then
  if amk_mod.blowout_type()<1 then
   if level.get_weather()=="default" or 
    level.get_weather()=="default2" then
    level.set_weather("pre_blow")
    level.set_weather_fx("amk_surge_day")
   end
  end
 end
 
end

local alife_levels = {
	 l04u_labx18 = true, 
	 l12u_sarcofag = true,
	 l12_stancia_2 = true }

local ol_last_checked_hour = -1

function check_hourly()
	if ol_last_checked_hour == -1 or (ol_last_checked_hour ~= level.get_time_hours() and level.get_time_minutes() < 5) then
		ol_last_checked_hour = level.get_time_hours()

		if alife_levels[level.name()] then
  			check_alife_spawn()
  		end  
	end
end
	 
function alife_spawn()
	if amk.load_variable("ol_next_alife_spawn",-1) == -1 and alife_levels[level.name()] then
		if olp_mod and olp_mod.x18_alife then
			if (level.name() == "l04u_labx18") and (has_alife_info("yan_labx16_switcher_primary_off")) then
				x18_spawn_usual()
			end
		end
	
		if olp_mod and olp_mod.sar_alife then
			if (level.name() == "l12u_sarcofag") then
				sar_spawner()
			end
		end
	
		if olp_mod and olp_mod.st2_alife then
			if (level.name() == "l12_stancia_2") then
				aes_horror_zone()
			end
		end

		local spawn_time = level.get_time_days()*60*24+(level.get_time_hours()+3)*60+level.get_time_minutes()
		amk.save_variable("ol_next_alife_spawn",spawn_time)
	end
end

function check_alife_spawn()
	local next_spawn_time = amk.load_variable("ol_next_alife_spawn",-1)
	if next_spawn_time >= 0 then
		local time = level.get_time_days()*60*24+level.get_time_hours()*60+level.get_time_minutes()  --time in game minutes (credits to AMK)		
		if next_spawn_time < time then
			amk.del_variable("ol_next_alife_spawn")
			alife_spawn()
		end
	elseif next_spawn_time == -1 then
		alife_spawn()
	end
end

local spawn_table = {}

local levels = {
  "l01_escape",
  "l02_garbage",
  "l03_agroprom",
  "l03u_agr_underground",
  "l04_darkvalley",
  "l04u_labx18",
  "l05_bar",
  "l06_rostok",
  "l07_military",
  "l08_yantar",
  "l08u_brainlab",
  "l10_radar",
  "l10u_bunker",
  "l11_pripyat",
  "l12_stancia",
  "l12_stancia_2",
  "l12u_control_monolith",
  "l12u_sarcofag"
}

local event_vars_by_level = {
  l01_escape="esc_le",
  l02_garbage="gar_le",
  l03_agroprom="agr_le",
  l03u_agr_underground="kat_le",
  l04_darkvalley="val_le",
  l04u_labx18="dar_le",
  l05_bar="bar_le",
  l06_rostok="ros_le",
  l07_military="mil_le",
  l08_yantar="yan_le",
  l08u_brainlab="x16_le",
  l10_radar="rad_le",
  l10u_bunker="bun_le",
  l11_pripyat="pri_le",
  l12_stancia="aes_le",
  l12_stancia_2="aes2_le",
  l12u_control_monolith="con_le",
  l12u_sarcofag="sar_le"
}

local lock_infos_by_level = {
  l01_escape="ol_events_esc_lock",
  l02_garbage="ol_events_gar_lock",
  l03_agroprom="ol_events_agr_lock",
  l03u_agr_underground="ol_events_kat_lock",
  l04_darkvalley="ol_events_val_lock",
  l04u_labx18="ol_events_dar_lock",
  l05_bar="ol_events_bar_lock",
  l06_rostok="ol_events_ros_lock",
  l07_military="ol_events_mil_lock",
  l08_yantar="ol_events_yan_lock",
  l08u_brainlab="ol_events_x16_lock",
  l10_radar="ol_events_rad_lock",
  l10u_bunker="ol_events_bun_lock",
  l11_pripyat="ol_events_pri_lock",
  l12_stancia="ol_events_aes_lock",
  l12_stancia_2="ol_events_aes2_lock",
  l12u_control_monolith="ol_events_con_lock",
  l12u_sarcofag="ol_events_sar_lock"
}

local ol_last_repeated_check = 0
local ol_repeated_check_met = 0

function check_repeatedly()
	local mins = level.get_time_minutes()
	if mins ~= ol_last_repeated_check then
		ol_last_repeated_check = level.get_time_minutes()
		local interval = 7
		if mins < interval then
			ol_repeated_check_met = 0
		end
		if ol_repeated_check_met + interval < mins then
			ol_repeated_check_met = level.get_time_minutes()
			--
			--
			--
			check_extended_alife()
		end
	end
end

function check_extended_alife()
	if bind_monster.recall_victims and  type(bind_monster.recall_victims) == 'number' then
		local usr_set_hours = 12		-- bind_monster.recall_victims
		if usr_set_hours > 0 then	
			local time = level.get_time_days()*60*24+level.get_time_hours()*60+level.get_time_minutes()  --time in game minutes (credits to AMK)														
			local next_event_time = amk.load_variable("next_event_time",nil)
			if not next_event_time then
				amk.save_variable("next_event_time",time+720)
			elseif next_event_time <= time then											
				if sleep_manager.is_sleep_active() then
					amk.save_variable("next_event_time",time+120)
				elseif amk.load_variable("blowout",0)>0 then
				  amk.save_variable("next_event_time",time+60)
				else
	               	common_evaluator()
	
					local min_event_time_shifter = 2
					local max_event_time_shifter = 5
					local event_time_shifter = math.random(min_event_time_shifter,max_event_time_shifter)
					local rnd = math.random(0,1)
					local prefix = 1
					if rnd == 0 then
						prefix = -1
					end
					local ets = event_time_shifter * prefix	
					next_event_time =  level.get_time_days()*60*24+(level.get_time_hours()+ets+usr_set_hours)*60+level.get_time_minutes() 	
					
					local i = 0
					while next_event_time <= time do
						next_event_time =  level.get_time_days()*60*24+(level.get_time_hours()+event_time_shifter+usr_set_hours+i)*60+level.get_time_minutes() 
						i = i + 1	
					end 
					
					amk.save_variable("next_event_time",next_event_time)
				end
			end
		end
	--else
		--amk.send_tip("%c[255,255,128,128]Oblivion Lost: Extended alife broken! \\n%c[default]You've set a wrong variable in %c[255,255,128,128]olp_mod.script%c[default]!","Atrocious:",nil,10,"ghost")
	end
end

local function iterate_trough_external_list(xtl,ID)
	for i=1,#xtl do 
		if xtl[i].spawn_ID == ID then 
			return xtl[i] 
		end 
	end
	return nil
end
	

function get_spawn_table_by_ID(ID)
	if not ID then
		return nil
	end
	
	local returned_table = nil
	
	returned_table = iterate_trough_external_list(events_loner.available_spawns,ID)
	if returned_table then return returned_table end
	returned_table = iterate_trough_external_list(events_loner.unavailable_spawns,ID)
	if returned_table then return returned_table end
	returned_table = iterate_trough_external_list(events_loner.alternative_spawns,ID)
	if returned_table then return returned_table end
	
	if has_alife_info("played_as_duty") then
		returned_table = iterate_trough_external_list(events_duty.available_spawns,ID)
		if returned_table then return returned_table end
		returned_table = iterate_trough_external_list(events_duty.unavailable_spawns,ID)
		if returned_table then return returned_table end
		returned_table = iterate_trough_external_list(events_duty.alternative_spawns,ID)
		if returned_table then return returned_table end
		end
	if has_alife_info("played_as_freedom") then
		returned_table = iterate_trough_external_list(events_freedom.available_spawns,ID)
		if returned_table then return returned_table end
		returned_table = iterate_trough_external_list(events_freedom.unavailable_spawns,ID)
		if returned_table then return returned_table end
		returned_table = iterate_trough_external_list(events_freedom.alternative_spawns,ID)
		if returned_table then return returned_table end
		end
	if has_alife_info("played_as_bandit") then
		returned_table = iterate_trough_external_list(events_bandit.available_spawns,ID)
		if returned_table then return returned_table end
		returned_table = iterate_trough_external_list(events_bandit.unavailable_spawns,ID)
		if returned_table then return returned_table end
		returned_table = iterate_trough_external_list(events_bandit.alternative_spawns,ID)
		if returned_table then return returned_table end
		end
	if has_alife_info("played_as_merc") then
		returned_table = iterate_trough_external_list(events_merc.available_spawns,ID)
		if returned_table then return returned_table end
		returned_table = iterate_trough_external_list(events_merc.unavailable_spawns,ID)
		if returned_table then return returned_table end
		returned_table = iterate_trough_external_list(events_merc.alternative_spawns,ID)
		if returned_table then return returned_table end
	end
	if has_alife_info("played_as_ecolog") then
		returned_table = iterate_trough_external_list(events_ecolog.available_spawns,ID)
		if returned_table then return returned_table end
		returned_table = iterate_trough_external_list(events_ecolog.unavailable_spawns,ID)
		if returned_table then return returned_table end
		returned_table = iterate_trough_external_list(events_ecolog.alternative_spawns,ID)
		if returned_table then return returned_table end
	end
	if has_alife_info("played_as_military") then
		returned_table = iterate_trough_external_list(events_military.available_spawns,ID)
		if returned_table then return returned_table end
		returned_table = iterate_trough_external_list(events_military.unavailable_spawns,ID)
		if returned_table then return returned_table end
		returned_table = iterate_trough_external_list(events_military.alternative_spawns,ID)
		if returned_table then return returned_table end
	end
	if has_alife_info("played_as_monolith") then
		returned_table = iterate_trough_external_list(events_monolith.available_spawns,ID)
		if returned_table then return returned_table end
		returned_table = iterate_trough_external_list(events_monolith.unavailable_spawns,ID)
		if returned_table then return returned_table end
		returned_table = iterate_trough_external_list(events_monolith.alternative_spawns,ID)
		if returned_table then return returned_table end
	end	
	return nil
end
	
function spawn_executable()
	if not spawn_table then
		return false
	end
	local not_executable = false
	if spawn_table.requirement and not has_alife_info(spawn_table.requirement) then
		not_executable = true
	end
	if spawn_table.blockade and has_alife_info(spawn_table.blockade) then
		not_executable = true
	end
	if not_executable then
		if spawn_table.alternative then
			return spawn_table.alternative
		else
			return false
		end
	end
	return true
end

local use_alternative_spawn = false
local failed_tries = 0

function common_evaluator()
	local spawn_count = 0
	
	local available_spawns
	local alternative_spawns 
	local actor_community = db.actor:character_community()
	if actor_community == "actor" then
		available_spawns = events_loner.available_spawns
		alternative_spawns = events_loner.alternative_spawns
	elseif actor_community == "actor_dolg" then
		available_spawns = events_duty.available_spawns
		alternative_spawns = events_duty.alternative_spawns
	elseif actor_community == "actor_freedom" then
		available_spawns = events_freedom.available_spawns
		alternative_spawns = events_freedom.alternative_spawns
	elseif actor_community == "actor_bandit" then
		available_spawns = events_bandit.available_spawns
		alternative_spawns = events_bandit.alternative_spawns
	elseif actor_community == "actor_killer" then
		available_spawns = events_merc.available_spawns
		alternative_spawns = events_merc.alternative_spawns
	elseif actor_community == "actor_ecolog" then
		available_spawns = events_ecolog.available_spawns
		alternative_spawns = events_ecolog.alternative_spawns
	elseif actor_community == "actor_military" then
		available_spawns = events_military.available_spawns
		alternative_spawns = events_military.alternative_spawns
	elseif actor_community == "actor_monolith" then
		available_spawns = events_monolith.available_spawns
		alternative_spawns = events_monolith.alternative_spawns
	else
		available_spawns = events_loner.available_spawns
		alternative_spawns = events_loner.alternative_spawns
	end		
													 
	if use_alternative_spawn then
		while alternative_spawns[spawn_count+1] do
	 		spawn_count = spawn_count + 1
		end
		local spawn_number = math.random(1, spawn_count)
		spawn_table = alternative_spawns[spawn_number]		
	else
		while available_spawns[spawn_count+1] do
	 		spawn_count = spawn_count + 1
		end
		local spawn_number = math.random(1, spawn_count)
		spawn_table = available_spawns[spawn_number]		
	end
	
	local spawn_chk_result = spawn_executable()
	while not (spawn_chk_result == true) and not (spawn_table == nil) do
		if spawn_chk_result == false then
			failed_tries = failed_tries + 1
			common_evaluator()	
			return
		else
			spawn_table = spawn_chk_result
			spawn_chk_result = spawn_executable()
		end
	end

	local locked_or_active = false
	
	if spawn_table and spawn_table.timeout then
		local var_name = event_vars_by_level[spawn_table.level_name]
		local le = amk.load_variable(var_name,nil)
		if le then					
			locked_or_active = true
		end
	end
	if spawn_table and spawn_table.lock then
		local info_name = lock_infos_by_level[spawn_table.level_name]
		if has_alife_info(info_name) then
			locked_or_active = true
		end
	end

	if locked_or_active then
		if failed_tries > 25 then				--the lower the number, the more fake messages you have
			use_alternative_spawn = true
			common_evaluator()	
		else
			failed_tries = failed_tries + 1
			common_evaluator()	
		end
	elseif spawn_table then
		use_alternative_spawn = false
		failed_tries = 0
		spawn_execute()
	end
end

function spawn_execute()
	local i = 1
	while spawn_table.names and spawn_table.names[i] and spawn_table.vectors and spawn_table.vectors[i] and spawn_table.IDs and spawn_table.IDs[i] do
		amk.spawn_item(spawn_table.names[i],
					spawn_table.vectors[i],
					spawn_table.IDs[i][1],
					spawn_table.IDs[i][2])
					i = i + 1
	end
	
	if spawn_table.spawn_ID == "yantar_spawn_bandit1" or 
	   spawn_table.spawn_ID == "yantar_spawn_monol1" or
	   spawn_table.spawn_ID == "yantar_spawn_freed1" or
	   spawn_table.spawn_ID == "yantar_spawn_loner1" or
	   spawn_table.spawn_ID == "yantar_spawn_merc1" then
	
  amk_mod.spawn_military_tech_pack_to_yantar( alife():create("helicopter",vector():set(53.97,40.0,-88.02),90641,1462 ) ,"helicopter") 
	end

	if spawn_table.spawn_ID == "radar_spawn_bandit2" or 
	   spawn_table.spawn_ID == "radar_spawn_monol2" or
	   spawn_table.spawn_ID == "radar_spawn_freed2" or
	   spawn_table.spawn_ID == "radar_spawn_loner2" or
	   spawn_table.spawn_ID == "radar_spawn_merc2" then
	   
		amk_mod.spawn_military_tech_pack_to_radar( alife():create("helicopter",vector():set(-4.69, -1.99, 42.027),9003,2020 ) ,"helicopter")
	end
	
	if not has_alife_info("block_rad_btr") then
	if spawn_table.spawn_ID == "radar_spawn_bandit2" or 
	   spawn_table.spawn_ID == "radar_spawn_monol2" or
	   spawn_table.spawn_ID == "radar_spawn_freed2" or
	   spawn_table.spawn_ID == "radar_spawn_loner2" or
	   spawn_table.spawn_ID == "radar_spawn_merc2" then
	
		amk_mod.spawn_military_tech_pack_to_radar( alife():create("vehicle_btr",vector():set(113.04, -0.186, -8.157),47382,1950 ) ,"vehicle_btr")
		db.actor:give_info_portion("block_rad_btr")
		end
	end

	if spawn_table.spawn_ID == "pripyat_spawn_bandit2" or 
	   spawn_table.spawn_ID == "pripyat_spawn_monol2" or
	   spawn_table.spawn_ID == "pripyat_spawn_freed2" or
	   spawn_table.spawn_ID == "pripyat_spawn_loner2" or
	   spawn_table.spawn_ID == "pripyat_spawn_merc2" then
	   
	amk_mod.spawn_military_tech_pack_to_pripyat( alife():create("helicopter",vector():set(9.49, 27.316, 119.45),113785,2166 ) ,"helicopter")
	end
		
	if spawn_table.message and spawn_table.sender then
		if spawn_table.icon then
			amk.send_tip(spawn_table.message,spawn_table.sender,nil,5,spawn_table.icon)
		else
			amk.send_tip(spawn_table.message,spawn_table.sender,nil,5)
		end
	end
	
	if spawn_table.timeout and spawn_table.level_name then
		local timeout = 5
		if timeout and type(timeout) == 'number' and timeout > 0 then
			local time = level.get_time_days()*60*24+level.get_time_hours()*60+level.get_time_minutes()  --time in game minutes (credits to AMK)
			timeout = time + (timeout * 60)
			if spawn_table.level_name == "l01_escape" then
				amk.save_variable("esc_let",timeout)
				amk.save_variable("esc_le",spawn_table.spawn_ID)
			elseif spawn_table.level_name == "l02_garbage" then
				amk.save_variable("gar_let",timeout)
				amk.save_variable("gar_le",spawn_table.spawn_ID)
			elseif spawn_table.level_name == "l03_agroprom" then
				amk.save_variable("agr_let",timeout)
				amk.save_variable("agr_le",spawn_table.spawn_ID)
			elseif spawn_table.level_name == "l03u_agr_underground" then
				amk.save_variable("kat_let",timeout)
				amk.save_variable("kat_le",spawn_table.spawn_ID)
			elseif spawn_table.level_name == "l04_darkvalley" then
				amk.save_variable("val_let",timeout)
				amk.save_variable("val_le",spawn_table.spawn_ID)
			elseif spawn_table.level_name == "l04u_labx18" then
				amk.save_variable("dar_let",timeout)
				amk.save_variable("dar_le",spawn_table.spawn_ID)
			elseif spawn_table.level_name == "l05_bar" then
				amk.save_variable("bar_let",timeout)
				amk.save_variable("bar_le",spawn_table.spawn_ID)
			elseif spawn_table.level_name == "l06_rostok" then
				amk.save_variable("ros_let",timeout)
				amk.save_variable("ros_le",spawn_table.spawn_ID)
			elseif spawn_table.level_name == "l07_military" then
				amk.save_variable("mil_let",timeout)
				amk.save_variable("mil_le",spawn_table.spawn_ID)
			elseif spawn_table.level_name == "l08_yantar" then
				amk.save_variable("yan_let",timeout)
				amk.save_variable("yan_le",spawn_table.spawn_ID)
			elseif spawn_table.level_name == "l08u_brainlab" then
				amk.save_variable("x16_let",timeout)
				amk.save_variable("x16_le",spawn_table.spawn_ID)										
			elseif spawn_table.level_name == "l10_radar" then
				amk.save_variable("rad_let",timeout)
				amk.save_variable("rad_le",spawn_table.spawn_ID)					
			elseif spawn_table.level_name == "l10u_bunker" then
				amk.save_variable("bun_let",timeout)
				amk.save_variable("bun_le",spawn_table.spawn_ID)					
			elseif spawn_table.level_name == "l11_pripyat" then
				amk.save_variable("pri_let",timeout)
				amk.save_variable("pri_le",spawn_table.spawn_ID)					
			elseif spawn_table.level_name == "l12_stancia" then
				amk.save_variable("aes_let",timeout)
				amk.save_variable("aes_le",spawn_table.spawn_ID)					
			elseif spawn_table.level_name == "l12_stancia_2" then
				amk.save_variable("aes2_let",timeout)
				amk.save_variable("aes2_le",spawn_table.spawn_ID)					
			elseif spawn_table.level_name == "l12u_control_monolith" then
				amk.save_variable("con_let",timeout)
				amk.save_variable("con_le",spawn_table.spawn_ID)					
			elseif spawn_table.level_name == "l12u_sarcofag" then
				amk.save_variable("sar_let",timeout)
				amk.save_variable("sar_le",spawn_table.spawn_ID)	
			end				
		--else
			--amk.send_tip("%c[255,255,128,128]Oblivion Lost: Extended alife broken! \\n%c[default]You've set a wrong TIMEOUT in %c[255,255,128,128]olp_mod.script%c[default]!","Atrocious:",nil,10,"ghost")
		end
	end	

	if spawn_table.lock and spawn_table.level_name then
		local info_name = lock_infos_by_level[spawn_table.level_name]
		db.actor:give_info_portion(info_name)
	end	
end

function check_spawned_npc(npc)
	local var_name = event_vars_by_level[level.name()]
	local le = amk.load_variable(var_name,nil)
	if le then
		local var_time_name = var_name.."t"
		local let = amk.load_variable(var_time_name,nil)
		local time = level.get_time_days()*60*24+level.get_time_hours()*60+level.get_time_minutes()  --time in game minutes (credits to AMK)
		if let and let < time then
			local table = get_spawn_table_by_ID(le)
			if table and not table.timeout then
				return 
			end
			local i = 1
			local name = nil
			while table and table.names[i] do
				name = table.names[i]
				if npc and npc:section() and npc:section() == name then
					npc:kill(npc)
					break
				end
				i = i + 1
			end
		end
	end
end

function process_death(npc, level_name)
	if not npc then
		return
	end
	for i=1,#levels,1 do
		local var_name = event_vars_by_level[levels[i]]
		local le = amk.load_variable(var_name,nil)
		local npc_identified = false
		if le then
			local table = get_spawn_table_by_ID(le)
			local j = 1
			local name = nil
			while table and table.names[j] do
				name = table.names[j]
				if npc and npc:section() and npc:section() == name then
					local kill_count = amk.load_variable(var_name.."_kc",0) + 1
					amk.save_variable(var_name.."_kc",kill_count) 
					npc_identified = true
					break
				end
				j = j + 1
			end
			if table and table.names[1] and amk.load_variable(var_name.."_kc",-1) == #table.names then
				amk.del_variable(var_name)
				amk.del_variable(var_time_name)	
				amk.del_variable(var_name.."_kc")	
				if table.end_message and table.end_sender then
					if table.end_icon then
						amk.send_tip(table.end_message,table.end_sender,nil,5,table.end_icon)
					else
						amk.send_tip(table.end_message,table.end_sender,nil,5)
					end
				end
			end
			if npc_identified then
				break
			end
		end
	end
end

function clean_lock()
	local info_name = lock_infos_by_level[level.name()]
	if has_alife_info(info_name) then 
		db.actor:disable_info_portion(info_name) 
	end
end

function Spawns2( namesp, px,pz,mon_c )
	local ind
	local new_position
	local posit = db.actor:position()
	new_position = vector():set(px,posit.y + 20,pz)
	for ind = 1, mon_c do	
	alife():create(namesp,new_position, 1, db.actor:game_vertex_id())
	end
end

function S_Escape()

 local ess = math.random(1, 4)

 if ess==1 then
 Spawns2("cat_weak",84, 557, 2)
 Spawns2("zombie_weak",127, 28, 2)
 Spawns2("fracture_weak",164, 131, 2)
 Spawns2("pseudodog_weak",136, 206, 2)
 Spawns2("zombie_weak",100, 461, 2)
 end
 if ess==2 then
 Spawns2("fracture_weak",84, 557, 2)
 Spawns2("zombie_immortal",127, 28, 2)
 Spawns2("zombie_strong",127, 28, 2)
 Spawns2("pseudodog_weak",164, 131, 1)
 Spawns2("cat_kany",136, 206, 2)
 Spawns2("cat_ol",100, 461, 2)
 end
 if ess==3 then
 Spawns2("bloodsucker_weak",84, 557, 1)
 Spawns2("pseudodog_weak",127, 28, 2)
 Spawns2("cat_kany",164, 131, 2)
 Spawns2("zombie_weak",136, 206, 2)
 Spawns2("fracture_weak",100, 461, 2)
 end
 if ess==4 then
 Spawns2("pseudodog_weak",84, 557, 1)
 Spawns2("cat_kany",127, 28, 1)
 Spawns2("zombie_immortal",164, 131, 1)
 Spawns2("cat_weak",136, 206, 3)
 Spawns2("fracture_weak",100, 461, 1)
 end
end

function S_Garbage()

  local gas = math.random(1, 4)

  if gas==1 then
  Spawns2("psy_dog",-160, 0, 1)
  Spawns2("fracture_weak",-160, 0, 1)
  Spawns2("cat_kany",-160, 0, 3)
  Spawns2("zombie_weak",-160, 0, 1)
  end
  if gas==2 then
  Spawns2("zombie_immortal",-160, 0, 2)
  Spawns2("zombie_strong",-160, 0, 2)
  Spawns2("bloodsucker_weak",-160, 0, 1)
  Spawns2("cat_ol",-160, 0, 3)
  Spawns2("fracture_weak",-160, 0, 1)
  end
  if gas==3 then
  Spawns2("zombie_weak",-160, 0, 2)
  Spawns2("psy_dog",-160, 0, 1)
  Spawns2("cat_ol",-160, 0, 3)
  Spawns2("fracture_weak",-160, 0, 1)
  end
  if gas==4 then
  Spawns2("zombie_immortal",-160, 0, 2)
  Spawns2("fracture_weak",-160, 0, 1)
  Spawns2("tushkano_normal",-160, 0, 10)
  Spawns2("cat_kany",-160, 0, 2)
  end
end

function S_Agroprom()

  local ags = math.random(1, 4)

  if ags==1 then
  Spawns2("cat_ol",-70, -35, 3)
  Spawns2("fracture_weak",-70, -35, 2)
  Spawns2("zombie_weak",-70, -35, 3)
  end
  if ags==2 then
  Spawns2("fracture_weak",-70, -35, 2)
  Spawns2("zombie_weak",-70, -35, 3)
  Spawns2("cat_ol",-70, -35, 3)
  end
  if ags==3 then
  Spawns2("zombie_weak",-70, -35, 2)
  Spawns2("cat_kany",-70, -35, 3)
  Spawns2("bloodsucker_weak",-70, -35, 1)
  end
  if ags==4 then
  Spawns2("zombie_weak",-70, -35, 2)
  Spawns2("cat_kany",-70, -35, 3)
  Spawns2("fracture_weak",-70, -35, 2)
  end
end

function S_Kat()
  Spawns2("bloodsucker_normal",-111, -50, 1)
  Spawns2("controller_tubeman",-76, -71, 1)
  Spawns2("m_poltergeist_normal_flame",-111, -50, 1)
end

function S_Val()

  local dvs = math.random(1, 4)

  if dvs==1 then
  Spawns2("tushkano_normal",60, -255, 10)
  Spawns2("tushkano_normal",-80, -430, 10)
  Spawns2("gigant_normal",-110, -190, 1)
  Spawns2("bloodsucker_normal",-80, -430, 2)
  Spawns2("bloodsucker_normal",60, -255, 2)
  Spawns2("burer_weak",60, -255, 1)
  end
  if dvs==2 then
  Spawns2("burer_weak",60, -255, 1)
  Spawns2("burer_weak",60, -255, 1)
  Spawns2("fracture_weak",-80, -430, 1)
  Spawns2("bloodsucker_strong",-80, -430, 2)
  Spawns2("psy_dog",-80, -430, 2)
  Spawns2("snork_weak",-80, -430, 1)
  end
  if dvs==3 then
  Spawns2("bloodsucker_weak",-80, -430, 1)
  Spawns2("psy_dog",-80, -430, 1)
  Spawns2("controller_tubeman",-110, -190, 1)
  Spawns2("snork_strong",60, -255, 2)
  Spawns2("zombie_immortal",60, -255, 2)
  Spawns2("zombie_strong",60, -255, 2)
  Spawns2("tushkano_normal",-80, -430, 10)
  end
  if dvs==4 then
  Spawns2("psy_dog",-80, -430, 1)
  Spawns2("cat_weak",-80, -430, 1)
  Spawns2("tushkano_normal",-80, -430, 10)
  Spawns2("cat_ol",60, -255, 2)
  Spawns2("fracture_weak",-80, -430, 2)
  Spawns2("zombie_immortal",60, -255, 1)
  end
end

function S_Dar()
  Spawns2("burer_weak",-33, 8, 2)
  Spawns2("snork_normal",22, 18, 2)
  Spawns2("snork_normal",14, 75, 3)
  Spawns2("m_poltergeist_normal_flame",-33, 8, 1)
end

function S_Rostok()

  local wts = math.random(1, 4)

  if wts==1 then
  Spawns2("cat_weak",-280, 51, 3)
  Spawns2("bloodsucker_normal",-280, 51, 1)
  Spawns2("burer_weak",-280, 51, 1)
  Spawns2("fracture_weak",37, 139, 2)
  Spawns2("m_poltergeist_normal_flame",-280, 51, 1)
  end
  if wts==2 then
  Spawns2("cat_kany",-280, 51, 2)
  Spawns2("fracture_weak",-280, 51, 1)
  Spawns2("gigant_strong",-280, 51, 1)
  Spawns2("zombie_weak",37, 139, 2)
  Spawns2("zombie_immortal",37, 139, 2)
  Spawns2("m_poltergeist_normal_flame",-280, 51, 1)
  end
  if wts==3 then
  Spawns2("burer_weak",-280, 51, 2)
  Spawns2("bloodsucker_weak",-280, 51, 1)
  Spawns2("bloodsucker_strong",-280, 51, 1)
  Spawns2("tushkano_normal",37, 139, 10)
  Spawns2("m_poltergeist_normal_flame",-280, 51, 1)
  end
  if wts==4 then
  Spawns2("gigant_strong",-280, 51, 1)
  Spawns2("bloodsucker_strong",-280, 51, 2)
  Spawns2("m_poltergeist_normal_tele",-280, 51, 3)
  Spawns2("m_poltergeist_normal_tele",37, 139, 3)
  Spawns2("m_poltergeist_normal_flame",-280, 51, 1)
  end
end

function S_Mil()

  local aws = math.random(1, 4)

  if aws==1 then
  Spawns2("chimera_weak",-195, 179, 1)
  Spawns2("burer_weak",-221, 267, 2)
  Spawns2("chimera_weak",-137, 67, 1)
  Spawns2("bloodsucker_normal",-63, 322, 2)
  Spawns2("psy_dog",-195, 179, 1)
  end
  if aws==2 then
  Spawns2("bloodsucker_weak",-195, 179, 1)
  Spawns2("gigant_strong",-221, 267, 2)
  Spawns2("fracture_weak",-137, 67, 2)
  Spawns2("snork_strong",-63, 322, 2)
  Spawns2("psy_dog",-195, 179, 1)
  end
  if aws==3 then
  Spawns2("chimera_weak",-195, 179, 1)
  Spawns2("chimera_weak",-221, 267, 2)
  Spawns2("burer_weak",-137, 67, 2)
  Spawns2("fracture_weak",-63, 322, 2)
  Spawns2("psy_dog",-195, 179, 1)
  end
  if aws==4 then
  Spawns2("burer_weak",-195, 179, 1)
  Spawns2("fracture_weak",-221, 267, 1)
  Spawns2("bloodsucker_strong",-137, 67, 2)
  Spawns2("gigant_strong",-63, 322, 2)
  Spawns2("psy_dog",-137, 67, 1)
  end
end

function S_Rad()

  local rfs = math.random(1, 4)

  if rfs==1 then
  Spawns2("chimera_weak",614, -311, 1)
  Spawns2("bloodsucker_normal",8, -78, 1)
  Spawns2("chimera_weak",245, 42, 1)
  Spawns2("gigant_strong",569, -212, 1)
  Spawns2("burer_weak",293, -316, 2)
  Spawns2("controller_tubeman",318, -178, 1)
  Spawns2("zombie_normal",170, 68, 2)
  Spawns2("m_poltergeist_normal_flame",318, -178, 1)
  end
  if rfs==2 then
  Spawns2("burer_weak",614, -311, 1)
  Spawns2("psy_dog",8, -78, 1)
  Spawns2("fracture_weak",245, 42, 1)
  Spawns2("gigant_strong",569, -212, 2)
  Spawns2("chimera_weak",293, -316, 2)
  Spawns2("controller_tubeman",318, -178, 1)
  Spawns2("zombie_immortal",170, 68, 2)
  Spawns2("m_poltergeist_normal_flame",318, -178, 1)
  end
  if rfs==3 then
  Spawns2("bloodsucker_strong",614, -311, 1)
  Spawns2("burer_weak",8, -78, 1)
  Spawns2("chimera_weak",245, 42, 1)
  Spawns2("zombie_ghost",569, -212, 2)
  Spawns2("zombie_weak",293, -316, 2)
  Spawns2("controller_tubeman",318, -178, 1)
  Spawns2("m_poltergeist_normal_tele",170, 68, 3)
  Spawns2("m_poltergeist_normal_flame",318, -178, 1)
  end
  if rfs==4 then
  Spawns2("chimera_weak",614, -311, 1)
  Spawns2("m_poltergeist_normal_tele",8, -78, 2)
  Spawns2("burer_weak",245, 42, 2)
  Spawns2("bloodsucker_strong",569, -212, 2)
  Spawns2("bloodsucker_weak",293, -316, 2)
  Spawns2("controller_tubeman",318, -178, 1)
  Spawns2("psy_dog",170, 68, 3)
  Spawns2("m_poltergeist_normal_flame",318, -178, 1)
  end
end

function S_Bun()
  Spawns2("tushkano_normal",13, 25, 10)
  Spawns2("burer_weak",-24, -7, 1)
  Spawns2("bloodsucker_strong",-38, -14, 1)
  Spawns2("burer_weak",13, 25, 1)
  Spawns2("controller_tubeman",-7, -60, 1)
  Spawns2("m_poltergeist_normal_flame",-7, -60, 1)
end

function S_Pri()

  local rbc = math.random(1, 4)

  if rbc==1 then
  Spawns2("chimera_weak",45, 36, 1)
  Spawns2("bloodsucker_strong",31, 262, 2)
  Spawns2("chimera_weak",31, 410, 1)
  Spawns2("chimera_weak",-144, 42, 1)
  Spawns2("burer_weak",66, 114, 2)
  Spawns2("controller_tubeman",170, 68, 1)
  Spawns2("zombie_ghost",170, 68, 3)
  Spawns2("zombie_strong",170, 68, 3)
  Spawns2("m_poltergeist_normal_flame",170, 68, 1)
  end
  if rbc==2 then
  Spawns2("psy_dog",45, 36, 1)
  Spawns2("snork_strong",31, 262, 3)
  Spawns2("burer_weak",31, 410, 1)
  Spawns2("burer_weak",-144, 42, 1)
  Spawns2("chimera_weak",66, 114, 2)
  Spawns2("controller_tubeman",170, 68, 1)
  Spawns2("zombie_immortal",170, 68, 3)
  Spawns2("m_poltergeist_normal_flame",170, 68, 1)
  end
  if rbc==3 then
  Spawns2("burer_weak",45, 36, 2)
  Spawns2("snork_strong",31, 262, 3)
  Spawns2("bloodsucker_strong",31, 410, 1)
  Spawns2("tushkano_normal",-144, 42, 10)
  Spawns2("chimera_weak",66, 114, 1)
  Spawns2("controller_tubeman",170, 68, 1)
  Spawns2("zombie_weak",170, 68, 3)
  Spawns2("m_poltergeist_normal_flame",170, 68, 1)
  end
  if rbc==4 then
  Spawns2("bloodsucker_strong",45, 36, 2)
  Spawns2("fracture_weak",-144, 42, 2)
  Spawns2("psy_dog",31, 410, 2)
  Spawns2("m_poltergeist_normal_tele",-144, 42, 3)
  Spawns2("burer_weak",66, 114, 2)
  Spawns2("controller_tubeman",170, 68, 1)
  Spawns2("zombie_ghost",170, 68, 3)
  Spawns2("m_poltergeist_normal_flame",170, 68, 1)
  end
end

function S_Stancia()

  local sta = math.random(1, 1)

  if sta==1 then
  Spawns2("gigant_strong",900, -40, 1)
  Spawns2("bloodsucker_strong",393, -42, 1)
  Spawns2("bloodsucker_strong",600, -57, 1)
  Spawns2("chimera_weak",600, -57, 1)
  Spawns2("chimera_weak",600, -57, 1)
  Spawns2("burer_weak",900, -40, 1)
  Spawns2("zombie_normal",393, -42, 1)
  Spawns2("zombie_strong",600, -57, 1)
  Spawns2("zombie_strong",393, -42, 1)
  Spawns2("zombie_normal",393, -42, 1)
  Spawns2("zombie_normal",393, -42, 1)
  Spawns2("burer_weak",900, -40, 1)
  Spawns2("fracture_weak",600, -57, 1)
  Spawns2("zombie_immortal",900, -40, 1)
  Spawns2("zombie_normal",900, -40, 1)
  Spawns2("zombie_normal",900, -40, 1)
  end
end

function Spawn_Rank()

  local level_name = level.name()
  
  if level_name ==  "l01_escape" then
   S_Escape()
  end

  if level_name ==  "l02_garbage" then
   S_Garbage()
  end

  if level_name ==  "l03_agroprom" then
   S_Agroprom()
  end

  if level_name ==  "l03u_agr_underground" then
   S_Kat()
  end

  if level_name ==  "l04_darkvalley" then
   S_Val()
  end

  if level_name ==  "l04u_labx18" then
   --S_Dar()
  end

  if level_name ==  "l06_rostok" then
   S_Rostok()
  end

  if level_name ==  "l07_military" then
   S_Mil()
  end

  if level_name ==  "l10_radar" then
   S_Rad()
  end

  if level_name ==  "l10u_bunker" then
   S_Bun()
  end

  if level_name ==  "l11_pripyat" then
   S_Pri()
  end

--  * blowout monster spawns for stancia, commented out because of some lag, delete the "--" to enable
--  if level_name ==  "l12_stancia" then
--   S_Stancia()
--  end

 end

function Spawns3( namesp,msp, mon_c)

	local level_centers={
	  l01_escape = vector():set(70, 50, 120),
	  l02_garbage = vector():set(0, 130, 0),
	  l03_agroprom = vector():set(13, 60, -71),
	  l03u_agr_underground=nil,
	  l04_darkvalley = vector():set(50, 30, -310),
	  l04u_labx18=nil,
	  l05_bar = vector():set(152,45, 87),
	  l06_rostok = vector():set(-236, 100, 108),
	  l07_military = vector():set(-128, 0, 270),
	  l08_yantar = vector():set(23, 10, -248),
	  l08u_brainlab=nil,
	  l10_radar = vector():set(450, -5, -120),
	  l10u_bunker=nil,
	  l11_pripyat = vector():set(55, 60, 145),
	  l12_stancia = vector():set(800, 20, -64),
	  l12_stancia_2=nil,
	  l12u_control_monolith=nil,
	  l12u_sarcofag=nil
	}
	
	local level_spread={
	  l01_escape = {200, 200},
	  l02_garbage = {200, 200},
	  l03_agroprom = {160, 160},
	  l03u_agr_underground = {30, 30},
	  l04_darkvalley = {180, 220},
	  l04u_labx18 = {30, 30},
	  l05_bar = {60, 40},
	  l06_rostok = {40, 65},
	  l07_military = {200, 90},
	  l08_yantar = {95, 45},
	  l08u_brainlab = {30, 30},
	  l10_radar = {160, 150},
	  l10u_bunker = {30, 30},
	  l11_pripyat = {110, 220},
	  l12_stancia = {150, 20},
	  l12_stancia_2 = {200, 200},
	  l12u_control_monolith = {30, 30},
	  l12u_sarcofag = {30, 30}
	}
	
	local width = level_spread[level.name()][1]
  	local height = level_spread[level.name()][2]
	
	local posit = level_centers[level.name()]
	if not posit then
		posit = db.actor:position()	
		posit.y = posit.y + 1
	end
	
	if level.name() == "l06_rostok" then
		local rd = math.random(1,4)	
		if rd < 3 then			
			posit = vector():set(-85, 20, 137)
			width = 130
			height = 15
		elseif rd == 3 then			
			posit = vector():set(-63, 0, -170)
			width = 15
			height = 25
		end
	end

	if level.name() == "l08_yantar" then
		local rd = math.random(1,9)
		if rd == 1 then
			posit = vector():set(61, 70, -3)
			width = 10
			height = 20
		elseif rd > 6 then
			posit = vector():set(146, 20, -104)
			width = 40
			height = 40
		end
	end
	
	if level.name() == "l07_military" then
		local rd = math.random(1,2)
		if rd == 1 then
			posit = vector():set(-121, 30, 28)
			height = 50
		end
	end

	for i = 1, mon_c do
	 
  		local rd = math.random(1,4)
  		if rd == 1 then
  			posit.x = posit.x + math.random(-width,width)
  			posit.z = posit.z - math.random(-height,height)
		end
		if rd == 2 then
			posit.x = posit.x - math.random(-width,width)
			posit.z = posit.z + math.random(-height,height)
		end
		if rd == 3 then
			posit.x = posit.x + math.random(-width,width)
			posit.z = posit.z + math.random(-height,height)
		end
		if rd == 4 then
			posit.x = posit.x - math.random(-width,width)
			posit.z = posit.z - math.random(-height,height)
		end
  		
  		alife():create(namesp, posit, 1, db.actor:game_vertex_id())
  		
  		local ln = level.name()
  		if ln ~= "l08_yantar" and ln ~= "l05_bar" then
   			if msp ~= "controller_tubeman" then
    			alife():create(msp, posit, 1, db.actor:game_vertex_id())
   			else
    			alife():create(msp, posit, 1, db.actor:game_vertex_id())
    			for j = 1, 10 do
     				alife():create("zombie_weak", posit, 1, db.actor:game_vertex_id())
    			end
   			end
  		end
 	end
end

function R_Art()
local level_name = level.name()

if level_name ==  "l01_escape" then
 local rbk = math.random(1, 4)
 
 if rbk==1 then
 Spawns3("af_medusa", "cat_weak", 1)
 end
 if rbk==2 then
 Spawns3("af_blood", "cat_kany", 1)
 end
 if rbk==3 then
 Spawns3("af_cristall_flower", "cat_ol", 1)
 end
 if rbk==4 then
 Spawns3("af_vyvert", "cat_weak", 1)
 end
end

if level_name ==  "l02_garbage" or level_name ==  "l03_agroprom" then
local fcd = math.random(1, 4)

 if fcd==1 then
 Spawns3("af_rusty_thorn","fracture_weak", 1)
 Spawns3("af_cristall_flower","cat_kany", 1)
 end
 if fcd==2 then
 Spawns3("af_vyvert","cat_ol", 1)
 Spawns3("af_gravi","fracture_weak", 1)
 end
 if fcd==3 then
 Spawns3("af_blood","cat_kany", 1)
 Spawns3("af_soul","fracture_weak", 1)
 end
 if fcd==4 then
 Spawns3("af_vyvert","pseudodog_weak", 1)
 Spawns3("af_mincer_meat","bloodsucker_weak", 1)
 end
end

if level_name ==  "l04_darkvalley" or level_name ==  "l06_rostok" then
 local yay = math.random(1, 4)
 
 if yay==1 then
 Spawns3("af_electra_flash","cat_weak", 1)
 Spawns3("af_drops","cat_kany", 1)
 Spawns3("af_cristall","pseudodog_weak",1)
 end
 if yay==2 then
 Spawns3("af_gravi","bloodsucker_normal",1)
 Spawns3("af_mincer_meat","tushkano_normal",10)
 Spawns3("af_fireball","gigant_normal",1)
 end
 if yay==3 then
 Spawns3("af_dummy_pellicle","bloodsucker_strong",1)
 Spawns3("af_soul","snork_strong",1)
 Spawns3("af_cristall_flower","burer_weak",1)
 end
 if yay==4 then
 Spawns3("af_rusty_sea-urchin","m_poltergeist_normal_tele",1)
 Spawns3("af_gravi","m_poltergeist_normal_tele",1)
 Spawns3("af_vyvert","m_poltergeist_normal_tele",1)
 end
end


if level_name ==  "l08_yantar" then
 local sdf = math.random(1, 4)
 
 if sdf==1 then
 Spawns3("af_fireball","snork_strong",1)
 Spawns3("af_cristall_flower","snork_strong",1)
 Spawns3("af_gold_fish","snork_strong",1)
 end
 if sdf==2 then
 Spawns3("af_mincer_meat","burer_weak",1)
 Spawns3("af_fuzz_kolobok","fracture_weak",1)
 Spawns3("af_drops","psy_dog",1)
 end
 if sdf==3 then
 Spawns3("af_mincer_meat","psy_dog",1)
 Spawns3("af_dummy_battery","gigant_strong",1)
 Spawns3("af_gravi","bloodsucker_strong",1)
 end
 if sdf==4 then
 Spawns3("af_drops","snork_strong",1)
 Spawns3("af_blood","bloodsucker_weak",1)
 Spawns3("af_electra_sparkler","psy_dog",1)
 end
end

if level_name ==  "l07_military" then
 spawn_psy_attack()
 
 local naf = math.random(1, 4)
 
 if naf==1 then
 Spawns3("af_dummy_dummy","af_fireball",1)
 Spawns3("af_cristall","snork_strong",1)
 end
 if naf==2 then
 Spawns3("af_dummy_pellicle","af_soul",1)
 Spawns3("af_drops","burer_weak",1)
 end
 if naf==3 then
 Spawns3("af_dummy_glassbeads","af_ameba_slime",1)
 Spawns3("af_vyvert","bloodsucker_weak",1)
 end
 if naf==4 then
 Spawns3("af_night_star","af_gravi",1)
 Spawns3("af_electra_flash","chimera_weak",1)
 end
end

if level_name == "l10_radar" then
 local ztr = math.random(1, 4)
 
 if ztr==1 then
 Spawns3("af_dummy_dummy","bloodsucker_strong",1)
 Spawns3("af_cristall","psy_dog",1)
 Spawns3("af_gold_fish","gigant_strong",1)
 end
 if ztr==2 then
 Spawns3("af_dummy_pellicle","zombie_weak",1)
 Spawns3("af_fuzz_kolobok","fracture_weak",1)
 Spawns3("af_soul","burer_weak",1)
 end
 if ztr==3 then
 Spawns3("af_dummy_glassbeads","zombie_ghost",1)
 Spawns3("af_dummy_pellicle","gigant_strong",1)
 Spawns3("af_ameba_slime","bloodsucker_strong",1)
 end
 if ztr==4 then
 Spawns3("af_night_star","gigant_strong",1)
 Spawns3("af_electra_flash","bloodsucker_weak",1)
 Spawns3("af_gravi","zombie_immortal",1)
 end
end
 
if level_name == "l11_pripyat" then
local poi = math.random(1, 4)

 if poi==1 then
 Spawns3("af_rusty_kristall","snork_strong",1)
 Spawns3("af_ameba_slug","zombie_ghost",1)
 end
 if poi==2 then
 Spawns3("af_dummy_spring","burer_weak",1)
 end
 if poi==3 then
 Spawns3("af_ameba_mica","fracture_weak",1)
 Spawns3("af_mincer_meat","gigant_strong",1)
 end
 if poi==4 then
 Spawns3("af_electra_moonlight","bloodsucker_strong",1)
 end
end
 
if level_name ==  "l12_stancia" then
 Spawns3("af_dummy_glassbeads","gigant_strong",1)
 Spawns3("af_rusty_kristall","chimera_weak",1)
 Spawns3("af_cristall","chimera_weak",1)
end

end

function spawn_psy_attack()

local mtd = math.random(1, 4)

if mtd==1 then
amk.spawn_item("controller_tubeman",vector():set(13.00,-2.09,112.10),1637,320611)
amk.spawn_item("zombie_strong",vector():set(13.00,-2.09,112.10),1637,320611)
amk.spawn_item("zombie_strong",vector():set(13.00,-2.09,112.10),1637,320611)
amk.spawn_item("zombie_immortal",vector():set(13.00,-2.09,112.10),1637,320611)
end
if mtd==2 then
amk.spawn_item("controller_tubeman",vector():set(82.04,-13.06,48.74),1558,379088)
amk.spawn_item("zombie_strong",vector():set(82.04,-13.06,48.74),1558,379088)
amk.spawn_item("zombie_immortal",vector():set(82.04,-13.06,48.74),1558,379088)
amk.spawn_item("zombie_immortal",vector():set(82.04,-13.06,48.74),1558,379088)
end
if mtd==3 then
amk.spawn_item("controller_tubeman",vector():set(-259.17,-15.45,54.81),1687,76735)
amk.spawn_item("zombie_strong",vector():set(-259.17,-15.45,54.81),1687,76735)
amk.spawn_item("zombie_strong",vector():set(-259.17,-15.45,54.81),1687,76735)
amk.spawn_item("zombie_immortal",vector():set(-259.17,-15.45,54.81),1687,76735)
end
if mtd==4 then
amk.spawn_item("controller_tubeman",vector():set(-248.50,-22.53,204.26),1764,85831)
amk.spawn_item("zombie_strong",vector():set(-248.50,-22.53,204.26),1764,85831)
amk.spawn_item("zombie_immortal",vector():set(-248.50,-22.53,204.26),1764,85831)
amk.spawn_item("zombie_immortal",vector():set(-248.50,-22.53,204.26),1764,85831)
end

end

function vehicle_to_be_fixed(vh)
	if (vh~=nil) and (system_ini():r_string(vh:section(), "class")=="SCRPTCAR") then
		return true
	end
	return false
end

function vehicles_float(car_table)
	local i = 0
	while car_table[i] ~= nil do
		local vh = car_table[i]
		if (vh~=nil) and (system_ini():r_string(vh:section(), "class")=="SCRPTCAR") then
			local ps=vh:get_physics_shell()
			if ps ~= nil then
			ps:apply_force(1,1,1)
				car_table[i] = nil
			end
		end
		i = i + 1 
	end
end

function x18_spawn()
amk.spawn_item("burer_weak",vector():set(-13.74,-10.82,-4.31),1150,1249)
amk.spawn_item("zombie_ghost",vector():set(-13.74,-10.82,-4.31),1150,1249)
amk.spawn_item("zombie_ghost",vector():set(-13.74,-10.82,-4.31),1150,1249)
amk.spawn_item("burer_weak",vector():set(-39.57,-13.01,-15.61),1153,233)
amk.spawn_item("zombie_normal",vector():set(-39.57,-13.01,-15.61),1153,233)
amk.spawn_item("zombie_normal",vector():set(-39.57,-13.01,-15.61),1153,233)
amk.spawn_item("bloodsucker_strong",vector():set(-13.65,-12.43,-10.48),1156,4780)
end

function sar_spawner()
amk.spawn_item("burer_weak",vector():set(36.15,-0.14,41.52),2427,6906)
amk.spawn_item("burer_weak",vector():set(56.49,12.26,-9.28),2415,9483)
amk.spawn_item("bloodsucker_strong",vector():set(7.44,11.60,28.89),2443,3282)
end

function x18_spawn_usual()

local xlb = math.random(1, 2)

if xlb==1 then
amk.spawn_item("bloodsucker_weak",vector():set(-13.65,-12.43,-10.48),1156,4780)
amk.spawn_item("burer_weak",vector():set(-39.57,-13.01,-15.61),1153,233)
amk.spawn_item("zombie_weak",vector():set(-39.57,-13.01,-15.61),1153,233)
amk.spawn_item("tushkano_normal",vector():set(-13.74,-10.82,-4.31),1150,1249)
amk.spawn_item("zombie_ghost",vector():set(-13.74,-10.82,-4.31),1150,1249)
end

if xlb==2 then
amk.spawn_item("tushkano_normal",vector():set(-13.65,-12.43,-10.48),1156,4780)
amk.spawn_item("tushkano_normal",vector():set(-39.57,-13.01,-15.61),1153,233)
amk.spawn_item("burer_weak",vector():set(-39.57,-13.01,-15.61),1153,233)
amk.spawn_item("bloodsucker_weak",vector():set(-13.74,-10.82,-4.31),1150,1249)
amk.spawn_item("zombie_normal",vector():set(-13.74,-10.82,-4.31),1150,1249)
end
end

function aes_horror_zone()

local qft = math.random(1, 2)

if qft==1 then
amk.spawn_item("zombie_normal",vector():set(13.87,-0.00,229.86),2532,30974)
amk.spawn_item("burer_weak",vector():set(365.99,-0.00,368.70),2581,9996)
amk.spawn_item("chimera_weak",vector():set(564.71,-0.04,404.43),2581,9996)
amk.spawn_item("gigant_strong",vector():set(675.27,-0.10,387.64),2657,193177)
amk.spawn_item("zombie_normal",vector():set(909.04,-0.10,396.28),2652,240287)
amk.spawn_item("zombie_normal",vector():set(944.31,0.02,349.68),2648,251144)
end
if qft==2 then
amk.spawn_item("burer_weak",vector():set(813.76,-0.10,388.12),2654,207991)
amk.spawn_item("zombie_normal",vector():set(8.15,-0.10,241.89),2532,28325)
amk.spawn_item("zombie_normal",vector():set(58.35,-0.10,235.37),2538,50718)
amk.spawn_item("bloodsucker_strong",vector():set(217.47,-0.00,323.54),2546,75531)
amk.spawn_item("chimera_weak",vector():set(1025.49,-0.09,141.69),2646,264442)
amk.spawn_item("fracture_weak",vector():set(897.40,-0.09,86.93),2635,235910)
end

end

function pripyat_spawn_first()
amk.spawn_item("m_controller_old",vector():set(91.92,-2.22,-42.18),2144,202685)
amk.spawn_item("m_controller_old",vector():set(74.96,-2.39,-23.00),2144,186284)
amk.spawn_item("zombie_immortal",vector():set(79.13,-2.39,-57.52),2137,190366)
amk.spawn_item("zombie_ghost",vector():set(79.13,-2.39,-57.52),2137,190366)
amk.spawn_item("zombie_weak",vector():set(82.49,-2.22,-44.23),2144,193988)
amk.spawn_item("zombie_weak",vector():set(82.49,-2.22,-44.23),2144,193988)
amk.spawn_item("burer_weak",vector():set(-57.54,-0.00,158.04),2213,43878)
amk.spawn_item("burer_weak",vector():set(-43.21,-0.64,118.72),2203,55311)
amk.spawn_item("burer_weak",vector():set(-107.97,-2.39,41.18),2248,12549)
end




function spawn_milstalker()

if not has_alife_info("military_stalker_spawned") then

amk.spawn_item("aes_milsta_pkm",vector():set(929.5,-0.09,16.53),2276,412306)
amk.spawn_item("aes_milsta_pkm",vector():set(929.5,-0.09,16.53),2276,412306)
amk.spawn_item("aes_milsta_mgn",vector():set(929.5,-0.09,16.53),2276,412306)
amk.spawn_item("wpn_smg_tmp",vector():set(31.54,5.45,-85.93),1099,212889)
amk.spawn_item("wpn_benelli_short",vector():set(-64.50,-2.45,19.41),330,134381)
amk.spawn_item("wpn_saiga12c",vector():set(-83.68,1.95,155.0),1420,47772)
amk.spawn_item("wpn_ots33",vector():set(-274.0,-22.13,299.9),1832,65221)
amk.spawn_item("wpn_fn2000_old",vector():set(-3.04,4.52,-6.63),1525,37953)
amk.spawn_item("wpn_tec9",vector():set(-191.9,-16.03,371.1),1859,133551)
amk.spawn_item("wpn_mg3",vector():set(35.90,-3.37,-40.35),1997,22222)
amk.spawn_item("wpn_fn_p90",vector():set(-8.10,2.49,12.33),482,231748)
amk.spawn_item("wpn_pkm",vector():set(838.1,0.21,-57.25),2307,365518)
amk.spawn_item("wpn_benelli_short",vector():set(3.54,2.598,-30.438),1023,178237)
amk.spawn_item("wpn_smg_tmp",vector():set(102.46,8.199,177.705),363,268535)
amk.spawn_item("wpn_mp7a3",vector():set(-116.69,1.378,135.063),1312,42885)
amk.spawn_item("wpn_ots33",vector():set(100.74,3.999,123.83),2182,215817)
amk.spawn_item("wpn_fn2000_old",vector():set(25.822,5.896,-10.87),1507,51859)
amk.spawn_item("wpn_tec9",vector():set(-7.505,5.599,-109.405),822,164923)
amk.spawn_item("wpn_mg3",vector():set(165.57,58.48,137.26),2566,59557)
amk.spawn_item("wpn_bizon",vector():set(294.51,16.817,69.198),2594,102442)
amk.spawn_item("wpn_saiga12c",vector():set(-6.421,11.755,11.7),682,233822)
amk.spawn_item("wpn_pkm",vector():set(-29.565,3.833,-49.182),1961,2470)
amk.spawn_item("wpn_kriss_super_v",vector():set(880.72,15.098,107.81),2635,229783)
amk.spawn_item("pripyat_trader",vector():set(128.81,10.67,120.58),2173,233255)

db.actor:give_info_portion("military_stalker_spawned")

end
end

------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------


function get_ranks_prices()
    local rango, rank_price 
	if db.actor then
	    rango = db.actor:character_rank()
	    if rango >= 0 and rango <= 300 then
		rank_price = 1.5
	    elseif rango >= 301 and rango <= 600 then
		rank_price = 1.3
	    elseif rango >= 601 and rango <= 900 then
		rank_price = 1.1
	    else
		rank_price = 0.9
	    end 
	return rank_price
	end
    return nil
end
--

function sid_weap_prof()
    local t_wpn_pro, t_wpn_price, sid_wprof
	if db.actor then
	    t_wpn_price = get_ranks_prices()
	    t_wpn_pro = 1.6
	    sid_wprof = t_wpn_pro * t_wpn_price
	return sid_wprof
	end
    return nil
end

function bar_weap_prof()
    local b_wpn_pro, b_wpn_price, bar_wprof
	if db.actor then
	    b_wpn_price = get_ranks_prices()
	    b_wpn_pro = 2.0
	    bar_wprof = b_wpn_pro * b_wpn_price
	return bar_wprof
	end
    return nil
end
--

function doc_weap_prof()
    local d_wpn_pro, d_wpn_price, doc_wprof
	if db.actor then
	    d_wpn_price = get_ranks_prices()
	    d_wpn_pro = 1.8
	    doc_wprof = d_wpn_pro * d_wpn_price
	return doc_wprof
	end
    return nil
end
--

function mil_weap_prof()
    local m_wpn_pro, m_wpn_price, mil_wprof
	if db.actor then
	    m_wpn_price = get_ranks_prices()
	    m_wpn_pro = 2.0
	    mil_wprof = m_wpn_pro * m_wpn_price
	return mil_wprof
	end
    return nil
end
--

function sid_armo_prof()
    local t_arm_pro, t_arm_price, sid_aprof 
	if db.actor then
	    t_arm_price = get_ranks_prices()
	    t_arm_pro = 1.5
	    sid_aprof = t_arm_pro * t_arm_price
	return sid_aprof
	end
    return nil
end

function bar_armo_prof()
    local b_arm_pro, b_arm_price, bar_aprof 
	if db.actor then
	    b_arm_price = get_ranks_prices()
	    b_arm_pro = 1.9
	    bar_aprof = b_arm_pro * b_arm_price
	return bar_aprof
	end
    return nil
end

function doc_armo_prof()
    local d_arm_pro, d_arm_price, doc_aprof 
	if db.actor then
	    d_arm_price = get_ranks_prices()
	    d_arm_pro = 1.7
	    doc_aprof = d_arm_pro * d_arm_price
	return doc_aprof
	end
    return nil
end

function mil_armo_prof()
    local m_arm_pro, m_arm_price, mil_aprof 
	if db.actor then
	    m_arm_price = get_ranks_prices()
	    m_arm_pro = 1.9
	    mil_aprof = m_arm_pro * m_arm_price
	return mil_aprof
	end
    return nil
end

function remove_sr()
--pri_stadium_restr

 if not has_alife_info("release_stadium") then
     
    local obj = alife():story_object(827)
    if obj then
      alife():release(obj)
      alife():create(7373)
    end
        
    db.actor:give_info_portion("release_stadium")
  end
end

function railway_spawn()

if has_alife_info("yan_labx16_switcher_primary_off") and not has_alife_info("rostok_camper_npc") then
amk.spawn_item("controller_tubeman",vector():set(-29.79,0.07,148.5),1417,62856)
amk.spawn_item("wpn_benelli_short",vector():set(-29.79,0.07,148.5),1417,62856)
amk.spawn_item("ros_neu_camper1",vector():set(-236.3,0.00,144.74),1347,15602)
amk.spawn_item("ros_neu_camper2",vector():set(-236.3,0.00,144.74),1347,15602)
db.actor:give_info_portion("rostok_camper_npc")
end
end

function info_from_cap()

 if level.name()=="l08_yantar" and has_alife_info("yan_labx16_switcher_primary_off") and
   not has_alife_info("info_yantar_fix") then
  db.actor:give_info_portion("info_amk_recipt_dummy")
 db.actor:give_info_portion("info_yantar_fix")
end

 if level.name()=="l12_stancia" and not has_alife_info("unlock_pripyat_heli") then
  db.actor:give_info_portion("unlock_pripyat_heli")
end

 if has_alife_info("info_kanyhalos_recipt") and
  not has_alife_info("info_amk_recipt_electra_dikoobraz") then
 db.actor:give_info_portion("info_amk_recipt_electra_dikoobraz")
end

 if has_alife_info("info_kanyhalos_recipt") and
  not has_alife_info("info_amk_recipt_giant_small_brother") then
  db.actor:give_info_portion("info_amk_recipt_giant_small_brother")
 end
end

function unlock_restrictions()

 if level.name()=="l12_stancia_2" and not has_alife_info("unlock_madness") then
  db.actor:give_info_portion("unlock_madness")
 end
end

function yantar_update()

if has_alife_info("yan_labx16_switcher_primary_off") and not has_alife_info("spawn_eco_npcs") then

amk.spawn_item("yan_ecolog",vector():set(28.283,-11.903,-289.93),1479,52751)
amk.spawn_item("yan_ecolog",vector():set(21.851,-11.88,-249.48),1478,49471)
amk.spawn_item("yan_ecolog",vector():set(84.052,-10.434,-266.701),1450,83250)

amk.spawn_item("yan_ecolog",vector():set(166.55,-8.182,-103.83),1442,131649)
amk.spawn_item("yan_ecolog",vector():set(57.151,0.0485,-59.494),1516,69241)

amk.spawn_item("yan_ecolog",vector():set(40.453,0.0495,-14.723),1507,59487)
amk.spawn_item("yan_ecolog",vector():set(13.645,0.031,-8.191),1503,45709)

db.actor:give_info_portion("spawn_eco_npcs")

 end
end

function spawn_traders()

if has_alife_info("played_as_merc") and not has_alife_info("spawn_merc_trader") then

amk.spawn_item("merc_trader",vector():set(-364.42,-13.62,387.57),1848,2667)

amk.spawn_item("mil_killer_exo",vector():set(-378.51,-13.97,401.05),1848,541)
amk.spawn_item("mil_killer_exo",vector():set(-373.76,-13.95,399.54),1848,805)
amk.spawn_item("mil_killer_exo",vector():set(-376.4,-14.04,395.09),1848,624)
amk.spawn_item("mil_killer_exo",vector():set(-383.14,-14.096,399.18),1848,364)
amk.spawn_item("mil_killer_exo",vector():set(-383.14,-14.096,399.18),1848,364)

db.actor:give_info_portion("spawn_merc_trader")
end

if has_alife_info("played_as_bandit") and not has_alife_info("spawn_bandit_trader") then
amk.spawn_item("bandit_trader",vector():set(34.06,1.02,-49.42),1084,216721)
db.actor:give_info_portion("spawn_bandit_trader")
end

if has_alife_info("unlock_madness") and not has_alife_info("spawn_military_trader") then
amk.spawn_item("stancia_trader",vector():set(273.31,-0.00,-218.27),2350,113131)
db.actor:give_info_portion("spawn_military_trader")
end

end

function give_story_info()

	if level.name() == "l03_agroprom" and has_alife_info("agr_find_gunslinger_cache_found") and has_alife_info("agr_find_ghost_start") then
		if not has_alife_info("agr_find_gunslinger_cache_done") then
			db.actor:give_info_portion("agr_find_gunslinger_cache_done")
		end
		if not has_alife_info("agr_find_gunslinger_cache_final") then
			db.actor:give_info_portion("agr_find_gunslinger_cache_final")
		end
	end

--enabling this cheat opens the 26room in Hotel Pripyat
	--[[	
	if level.name()=="l11_pripyat" then
   if not has_alife_info("cit_jail_doctor_pryp_task") then
    db.actor:give_info_portion("cit_jail_doctor_pryp_task")
   end
	end
	]]
	
	if level.name()=="l12_stancia" then
	 if not has_alife_info("pri_followers_leader_phrase1_end") then
	  db.actor:give_info_portion("pri_followers_leader_phrase1_end")
	 end
	 if not has_alife_info("pri_aes_mapspot") then
	  db.actor:give_info_portion("pri_aes_mapspot")
	 end
   if not has_alife_info("pri_stadium_reached") then
    db.actor:give_info_portion("pri_stadium_reached")
   end
	end

end

function check_btr_status()

 if has_alife_info("unlock_madness") then
  release_btrs()
 end
end

function release_btrs()

 if not has_alife_info("remove_aes_btrs") then
  del_1()
   del_2()
    del_3()
  db.actor:give_info_portion("remove_aes_btrs")
 end
end

function del_1()

	local obj = alife():story_object(1112)

 if obj~=nil then
  alife():release(obj)
 end
end

function del_2()

	local obj = alife():story_object(1113)

 if obj~=nil then
  alife():release(obj)
 end
end

function del_3()

	local obj = alife():story_object(1114)

 if obj~=nil then
  alife():release(obj)
 end
end
