--[[
Copyright: © Ирбиs , Real Wolf
]]

-- Через сколько часов после начала игры начнется выброс

t_start = 7

-- Периодичность выброса в игровых часах.

t_surge = 82

-- Описание выпуска мода

-- Название мода
mod_title = "Lost World"

-- Название игры, для которой предназначен мод
mod_game_addon = "S.T.A.L.K.E.R."

-- Версия мода
mod_version = "Requital"

--' Вывод на карту всех обектов игры, подробная настройка, что выводить в sim_statistic
DebugMode = false

-- Функции квеста с FN-2000
function spawn_fn2000()
alife():create("wpn_fn2000", vector():set(305.17,-38.7,-24.4), 68330, 1930)
for i=1,3 do
	alife():create("ammo_5.56x45_he", vector():set(305.17,-39.4,-25), 68330, 1930)
end
end

-- Вступления

function join_bandit (actor, npc)
    printf ("ACTOR NOW IN BANDIT COMMUNITY")
    if db.actor then
       db.actor:set_character_community ("bandit", 0, 0)
    end
end

function join_military (actor, npc)
    printf ("ACTOR NOW IN MILITARY COMMUNITY")
    if db.actor then
       db.actor:set_character_community ("military", 0, 0)
    end
end

function join_dragon (actor, npc)
    printf ("ACTOR NOW IN DRAGON COMMUNITY")
    if db.actor then
       db.actor:set_character_community ("dragon", 0, 0)
    end
end

function join_killer (actor, npc)
    printf ("ACTOR NOW IN KILLER COMMUNITY")
    if db.actor then
       db.actor:set_character_community ("killer", 0, 0)
    end
end

function join_ecolog (actor, npc)
    printf ("ACTOR NOW IN ECOLOG COMMUNITY")
    if db.actor then
       db.actor:set_character_community ("ecolog", 0, 0)
    end
end

function join_monolith (actor, npc)
    printf ("ACTOR NOW IN MONOLITH COMMUNITY")
    if db.actor then
       db.actor:set_character_community ("monolith", 0, 0)
    end
end

function PlaySound(sound,obj)
	if obj == nil then obj = db.actor end
	snd = xr_sound.get_safe_sound_object(sound)
	snd:play_no_feedback(obj, sound_object.s2d, 0, vector(), 1.0)
end

function DelItemFromInv(section, count)
	local k = 0
	db.actor:iterate_inventory(function (dummy, item) 
		if item:section() == "mutant_psevdodog_tail" then
			if k <= 10 then 
				db.actor:drop_item(item)
				amk.remove_item(item)
			end 
			k = k + 1
		end 
	end, nil)
end


function DelItemFromInv_2(section, count)
	local k = 0
	db.actor:iterate_inventory(function (dummy, item) 
		if item:section() == "mutant_psevdodog_tail" then
			if k <= 5 then 
				db.actor:drop_item(item)
				amk.remove_item(item)
			end 
			k = k + 1
		end 
	end, nil)
end


function timefactor()
	return system_ini():r_float("alife","time_factor")
end

--' Коллбеки

function NetSpawn()
	lwc_detector.anom()
	medkit_use.load()
	lwc_order_barmen.Load()
	lwc_order_duty.Load()
	lwc_order_free.Load()
	lwc_order_kruger.Load()
	lwc_order_merc.Load()
	lwc_order_mono.Load()
	lwc_order_novikov.Load()
	lwc_order_sidor.Load()

	if level.name() == "garbage_old" then
		if has_alife_info("pre_realwolf_quest_start") and not has_alife_info("pre_realwolf_quest_vod_end") then
			db.actor:give_info_portion("pre_realwolf_quest_vod_end")
		end
	end

	if level.name() == "jupiter_underground" then
		if has_alife_info("upi_second_side_quest_start") and not has_alife_info("upi_second_side_quest_end") then
			db.actor:give_info_portion("upi_second_side_quest_end")
		end
	end
end


function OnItemDrop(obj)

end

function OnInfo(info)
	--' СМС от Мордера
	if info == "pre_night_star_quest_end_stop" or info == "pre_night_star_cancel_stop" then
		amk.g_start_timer("sms_morder",0,0,2)
		db.actor:give_info_portion("pre_talk_with_krohobor_end")
	--' Удаляем метку с Мордера
	elseif info == "pre_morder_quest_start" then
		local sobj = alife():story_object(1602)
		if sobj then amk.remove_spot_from_map(sobj.id,"blue_location") end
	elseif info == "dar_open_door2" then
		alife():create("snork_strong",vector():set(-0.728782176971436,4.19056940078735,-12.6155691146851),2663,1127)
		alife():create("zombie_strong",vector():set(-8.19663238525391,4.19024467468262,-9.09216213226318),1773,1166)
		alife():create("snork_strong",vector():set(3.17601346969604,4.19124412536621,0.457948327064514),3284,1124)
		alife():create("zombie_strong",vector():set(-3.50823497772217,9.37473487854004,-9.04184913635254),2243,1109)
	end
end

local k = true
local next_update = 0
local watchdog_max = 16
local watchdog = 0
ammo_tr_fix = false
oau_watchdog = nil

function Update(self)
	--' Апдейты с высоким приоритетом.
	lwc_bdet.Update()
	oau_watchdog = 1
	lwc_detector.update_map()
	oau_watchdog = 2
	lwc_health.update()
	oau_watchdog = 3
	lwc_use.weapontimer()
	oau_watchdog = 4

	if amk.load_variable("first_run", 0) ~= 1 then
		FirstRun()
		amk.save_variable("first_run", 1)
	end
	oau_watchdog = 5

	--' Апдейт раз в секунду
	if next_update <= time_global() then
		lwc_detector.simple()
		lwc_detector.advances()
		lwc_detector.elite()

		next_update = time_global() + 1000
	end
	oau_watchdog = 6

	if watchdog == 1 then
		if self.object.hud_timer == nil then
			if get_hud():GetCustomStatic("hud_timer") == nil then
				get_hud():AddCustomStatic("hud_timer", true)
				get_hud():GetCustomStatic("hud_timer")
			end
			if get_hud():GetCustomStatic("hud_timer") ~= nil then
				get_hud():GetCustomStatic("hud_timer"):wnd():SetText(string.format("%02d:%02d\n", level.get_time_hours(), level.get_time_minutes()))
			end
        	end
	end
	oau_watchdog = 7

	if watchdog == 2 then
		if has_alife_info("hide_hud") then
    			level.hide_indicators()
    			level.disable_input()
 		end
	end
	oau_watchdog = 8

	if watchdog == 3 then
		if db.actor:get_bleeding() > 0.01 then
			if not get_hud():GetCustomStatic("md_blood") then
				get_hud():AddCustomStatic("md_blood")
			end
		else
			if get_hud():GetCustomStatic("md_blood") then
				get_hud():RemoveCustomStatic("md_blood")
			end
		end
	end
	oau_watchdog = 9

	if watchdog == 4 then
		if ammo_tr_fix == true then
			treasure_manager.get_treasure_manager():loss_workaround()
		end
	end
	oau_watchdog = 10
	
	if watchdog == 5 then
		if db.actor and db.actor:alive() and db.actor.radiation > 50 then
			level.add_pp_effector("rad_effect.ppe", 2011, false)
			level.set_pp_effector_factor(2011, math.floor( db.actor.radiation*100) )
		else
			level.remove_pp_effector(2011)
		end
	end	
	oau_watchdog = 11

	if watchdog == 6 then
		xr_giditara.item_update()
        	xr_garmon.item_update()
		xr_racya.item_update()
	end
	oau_watchdog = 12

	if watchdog == 7 then
		if sleep_manager.is_sleep_active() and xr_conditions.actor_dead() then
			xr_logic.issue_event(db.actor, db.storage[db.actor:id()]["ar_sleep"], "update")
		end
	end
	oau_watchdog = 13
	
	if watchdog == 8 then
		aem_manager.get_aem():update()
	end
	oau_watchdog = 14

	if watchdog == 9 then
		lwc_pda.check_metka()
	end
	oau_watchdog = 15

	if watchdog == 10 then
		xr_companion.reset_enemy_by_time()
	end
	oau_watchdog = 16

	if watchdog == 11 then
		xrs_ai.actor_update(delta)
	end
	oau_watchdog = 17

	if watchdog == 12 then
          	lwc_health.wounded_pp_update()
	end
	oau_watchdog = 18

	if watchdog == 13 then
		terrorizm.update(delta)
	end
	oau_watchdog = 19

	if watchdog == 14 then
		inventory.update()
	end
	oau_watchdog = 20

	if watchdog == 15 then
		mod_man.on_actor_update()
	end
	oau_watchdog = 21

	if watchdog == 16 then
		medkit_use.show_med()
	end
	oau_watchdog = 22	
		
	-- НЕ ТРОГАТЬ!!! БИБЛИОТЕЧНАЯ КОНСТРУКЦИЯ! ДОЛЖНО ЗАМЫКАТЬ ФУНКЦИЮ!
	oau_watchdog = 0
	watchdog = watchdog + 1
	if watchdog > watchdog_max then
		watchdog = 0
	end

end

function Load()

end

function Save()
	lwc_bdet.Save()
	medkit_use.save()
	lwc_order_barmen.Save()
	lwc_order_duty.Save()
	lwc_order_free.Save()
	lwc_order_kruger.Save()
	lwc_order_merc.Save()
	lwc_order_mono.Save()
	lwc_order_novikov.Save()
	lwc_order_sidor.Save()
end

function CheckStart()
	lwc_bdet.Load()
end

function FirstRun()
	db.actor:give_info_portion("info_af")
	lwc_game_spawn.spawn()
end



function lootmoney(npc)
	if npc ~= nil and not string.find(npc:section(),"arena") and npc:character_community()~="arena_enemy" then
		local money = npc:money()
		if money ~= nil and money ~=0 then
			local deadmoney = money
                        local npc_rank
			npc_rank = ranks.get_obj_rank_name(npc)
			if npc_rank ~= nil then
				if npc_rank == "novice" and deadmoney >= 400 then 
					deadmoney=math.random(1,500)    
				elseif npc_rank == "experienced" and deadmoney >=500 then 
					deadmoney=math.random(1,1000)  
				elseif npc_rank == "veteran" and deadmoney >=600 then 
					deadmoney=math.random(1,1500) 
				elseif npc_rank == "master" and deadmoney >=700 then 
					deadmoney=math.random(1,2000)  
                        	end
			end
			local news_texti = "\\n%c[255,255,0,0]Мёртвый сталкер: %c[default]"..npc:character_name().."\\n%c[255,255,0,0]Обнаружено денег: %c[default]"..game.translate_string(tostring(deadmoney).." руб.")
			db.actor:give_game_news(news_texti, "ui\\ui_iconsTotal", Frect():set(0,0,83,47), 1, 4000)
			db.actor:give_money(deadmoney)
			game_stats.money_quest_update(deadmoney) 
			npc:give_money(-money)
			game_stats.money_quest_update(-money)
		end
	end
end