local nrg,med,cnt,gv,lv,w
in_hide=false
local sound_obj_right,sound_obj_left,snd_volume,snd_obj_eq
local sync_done=false
local dynlasthour
zombied={}

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

local indoors = {
l03u_agr_underground = true,
l04u_labx18 = true,
l08u_brainlab = true,
l10u_bunker = true,
l12u_control_monolith = true,
l12u_sarcofag = true
}

-- Эта функция вызывается первой. Онлайновые объекты недоступны! db.actor недоступен!
function on_game_start()
end

--первый запуск мода
function first_run()
   
  if amk.load_variable("x_first_run",true) then
  
    
    --amk.g_start_timer("show_news",0,0,10)
    amk.spawn_item_in_inv("matras")
    amk.spawn_item_in_inv("amk_metka")
    amk.spawn_item_in_inv("wpn_knife")

    amk.save_variable("x_first_run",false)
  end
  
  if amk.load_variable("amk_12",0)==0 then
  
    amk.spawn_item("amk_zapiska",vector():set(-126.96,-28.41,-379.12),27,125181)
    amk.spawn_item("amk_zapiska",vector():set(21.6,-3.94,-18.28),1140,5991)
    amk.spawn_item("amk_zapiska",vector():set(-126.92,23.27,-36.98),1543,7083)
    amk.spawn_item("controller_mainboss",vector():set(585.19,-0.03,278.54),2618,177555)
    amk.spawn_item("m_controller_old",vector():set(-0.18,12.25,-11.79),2449,1728)
    oblivion_lost.x18_spawn()
    oblivion_lost.pripyat_spawn_first()
  
    amk.save_variable("amk_12",1)
  end
  
  --if amk.load_variable("amk_13",0)==0 then

    -- pri_space_restrictor_0011  
    --local obj = alife():story_object(830)
    --if obj then
      --alife():release(obj)
      --alife():create(7373)
    --end
        
    --amk.save_variable("amk_13",1)
  --end
  
end

function check_spawn()

-- спавним аномалии
  --initial_spawn()

oblivion_lost.on_game_load()

  --local obj = alife():story_object(6000)
  --if not obj then
    --alife():create(2)
    --obj = alife():story_object(6000)
  --end
  
  --if amk.load_variable("freeplay",0)==1 then
    --amk.save_variable("freeplay",2)
  --elseif amk.load_variable("freeplay",0)==2 and (not has_alife_info("cit_fail_first_task")) then
    --db.actor:give_info_portion("cit_fail_first_task")
  --end
  
  --if not db.actor:has_info("bar_rescue_research_reward") then
    --local objt = alife():story_object(6002)
    --if objt then
      --alife():release(objt)
    --end
  --end
  
  --level_changers section
  
  --spots section
  obj = alife():story_object(6001)
  if obj then
    level.map_add_object_spot(obj.id, "level_changer", "to_pripyat")
  end
  
  obj = alife():story_object(6002)
  if obj then
    level.map_add_object_spot(obj.id, "level_changer", "to_darkvalley")
  end
  
    obj = alife():story_object(6000)
  if obj then
    level.map_add_object_spot(obj.id, "level_changer", "to_stancia")
  end
  
    obj = alife():story_object(6003)
  if obj and has_alife_info("unlock_madness") then
    level.map_add_object_spot(obj.id, "level_changer", "to_cmon")
  end
  
  if weather_types[lname]==0 then
    w=amk.load_variable("dynweather",nil) or level.get_weather() 
  else
    w=level.get_weather() 
  end
  
  if amk.load_variable("blowout",0)>0 then amk.save_variable("weather",level.get_weather()) end
  level.set_weather("amk_for_blow",true)
  game.start_tutorial("restore_sun")

  if amk.load_variable("blowout",0)>0 and amk.load_variable("blowout",0)<4 then
    local bt=blowout_type()
    level.add_cam_effector("camera_effects\\earthquake.anm", 2002, true, "")
    if bt<1 then
      level.set_weather("amk_for_blow",true)
      level.add_pp_effector("vibros.ppe", 2001, true)
      --blowout_psy_sound("start")
    end
  end
  
end

function restore_sun()
  update_anoms()
  if amk.load_variable("blowout",0)==0 then level.set_weather(w,true) end
  
  local lname = level.name()
  local sname = amk.load_variable("level_on_save","")
  amk.save_variable("level_on_save",lname)
  if sname=="l12_stancia_2" and lname=="l11_pripyat" then
    sleep_manager.main(1)
  end
     
  --if amk.load_variable("blow_started")==0 then
  --  start_blow_timer()
  --  amk.save_variable("blow_started",1)
  --end

  sync_done=true
  
end

function freeplay()
  --amk.save_variable("freeplay",1)
  
  --local obj = alife():story_object(6001)
  --if not obj then
    --alife():create(1)
  --end

end

--новости
function show_news()
  
 local when,where,text,mat = amk_news_lists.get_strings()
 local name,sname = amk_names_lists.get_strings()
 local reason,dead = amk_death_lists.get_strings()
 local uniq=amk_uniq_news_lists.get_strings()
 local mod_news=amk_modders.get_strings()
 local dolg_news=amk_dolg.get_strings()
 local freedom_news=amk_freedom.get_strings()
    
  if math.random(0,1)>0.5 then
    when,where = where,when
  end
  
  if math.random(0,1)>0.8 then
    name=""
  end

  if math.random(0,1)>0.1 or amk.load_variable("option_mat",0)==0 then
    mat=""
  end

  if math.random(0,1)>0.2 then
    if math.random(0,1)>0.875 then
      if math.random(0,1)>0.5 then
        db.actor:give_game_news(dolg_news, "ui\\ui_iconsTotal", Frect():set(498,141,83,47), 0, 15000)
      else
        db.actor:give_game_news(freedom_news, "ui\\ui_iconsTotal", Frect():set(498,94,83,47), 0, 15000)
      end
    else
      if math.random(0,1)>0.57 then
        amk.send_tip(when.." "..where.." "..text.." "..mat,name.." "..sname,nil,15,"gen_info")
      else
        amk.send_tip(name.." "..sname..". "..dead..". "..reason,"Погиб сталкер:",nil,10,"death")
      end
    end
  else
    if math.random(0,1)>0.5 then
      amk.send_tip(uniq,name.." "..sname,nil,15,"uniq")
    else
      db.actor:give_game_news(mod_news, "ui\\ui_iconsTotal", Frect():set(498,47,83,47), 0, 15000)
    end
    end
  
  amk.g_start_timer("show_news",0,0,math.random(40,80))
end
-------------------

--сон
function reduce_need_sleep(time)
  local tmp=amk.load_variable("gg_need_sleep",0)
  tmp=tmp-time*120
  if tmp<0 then tmp=0 end
  amk.save_variable("gg_need_sleep_nrg",0)
  amk.save_variable("block_sleep_menu",0)
  amk.save_variable("gg_need_sleep",tmp)
  test_sleep_pp()
end

function test_for_need_sleep()
  
  if sleep_manager.is_sleep_active() == false then 
    amk.save_variable("gg_need_sleep",amk.load_variable("gg_need_sleep",0)+1) 
    test_sleep_pp()
  end
  
  amk.g_start_timer("gg_need_sleep",0,0,6)
  
end

function test_sleep_pp()
  local tmp=amk.load_variable("gg_need_sleep",0)
  if tmp>360 then
    sleep_manager.main(5+amk.load_variable("gg_need_sleep_nrg",0))
  end
  if tmp>300 then
    level.add_pp_effector("yantar_underground_psi.ppe", 999, true)
    level.set_pp_effector_factor(999, 5.0)  
  end
  if tmp<=300 then
    level.remove_pp_effector(999)
  end
end

function check_sleep_item(obj)

  local section = obj:section()
  local stype=nil
  if section=="energy_drink" then
    stype="nrg"
  elseif (section=="medkit" or section=="medkit_army" or section=="medkit_scientic") then
    stype="med"
  elseif section=="matras" then
    stype="matras"
  end
  
  if stype~=nil then
    amk.start_timer("sleep_"..stype,0.1,obj:id())
  end

end

function test_for_need_sleep_nrg(oid)
  if alife():object(oid)==nil then
    local n=amk.load_variable("gg_need_sleep_nrg",0)
    if n<3 then
      amk.save_variable("gg_need_sleep_nrg",n+1)
      amk.save_variable("gg_need_sleep",amk.load_variable("gg_need_sleep",0)-30+n*10) 
      test_sleep_pp()
    end
    
    amk.g_start_timer("block_sleep_menu",0,1,0)
    amk.save_variable("block_sleep_menu",1)
  end
end
function test_for_need_sleep_med(oid)
  if alife():object(oid)==nil then
    amk.save_variable("gg_need_sleep",amk.load_variable("gg_need_sleep",0)+5) 
    test_sleep_pp()
  end
end
function test_for_need_sleep_matras(oid)
  if alife():object(oid)==nil then
    if amk.load_variable("blowout",-1)>-1 and amk.load_variable("blowout",-1)<5 then
       amk.send_tip("Blowout! Don't sleep! Run for your life!","Status",nil,5)
    else
        if amk.load_variable("block_sleep_menu",0)==0 then
          --if amk.load_variable("gg_need_sleep",0)>20 then
            local spwn=ui_cheat.cheat(get_hud())
            level.start_stop_menu(spwn,true)
          else
			amk.send_tip("Can't sleep because I just had an energydrink.","Status",nil,5)
          --end
        --else
          --amk.send_tip("Меченый не может спать из-за действия энергетика","Сообщение",nil,5)
        end
    end
    amk.spawn_item_in_inv("matras")
  end
end
----------------------

-------------багфикс радара-------------------
function check_radar_off()
  --if (level.name() == "l10_radar") then
    --if not has_alife_info("bar_deactivate_radar_done") then
      --local pos = db.actor:position()
      --if pos.z>65 and pos.x>350 and pos.x<410 then
        --if amk.load_variable("radar_fix",0)==0 then
          --level.add_pp_effector("fire_hit.ppe", 1523, true)
          --level.set_pp_effector_factor(1523, 0.5)
          --amk.save_variable("radar_fix",1)
          --amk.start_timer("radar_fix",5)
        --end
      --end
    --end
  --end
end

function radar_fix()
  --level.remove_pp_effector(1523)
  --local pos = db.actor:position()
  --if pos.z>65 and pos.x>350 and pos.x<410 then
    --db.actor:kill(db.actor)
  --end
  --amk.del_variable("radar_fix")
end
----------------- end -------------

----------------артефакты------------------
local anom_list=nil
local cur_level=nil
local havegoodart=false
local nearest_anomaly=nil

function get_radius_for_section(section)
  return system_ini():r_float(section,"effective_radius")
end

function add_anomaly(id,cls,sect,rad,sid)
  if anom_list==nil then
    anom_list={}
  end
  if cur_level~=level.name() then
    anom_list={}
    cur_level=level.name()
  end
  anom_list[id]={pos=(alife():object(id)==nil and 0)or alife():object(id).position,clsid=cls,radius=rad,section=sect,sid=sid}
end

function remove_anomaly(id)
  if anom_list then
    anom_list[id]=nil
  end
end

function get_nearest_anomaly(npc)
  local posn=npc:position()
  local anomid=nil
  local mindist=10000000
  local pos1=nil
  local cls=0
  local anomdesc={}
  local sect=nil
  for id,o in pairs(anom_list) do
    if o then
      if o.pos==0 then
        local obj=level.object_by_id(id)
        if obj then
          o.pos=obj:position()
        end
      end
      if o.pos~=0 then
        if o.sid>=amk_hideouts.anom_sid0+amk_hideouts.anom_count or o.sid<amk_hideouts.anom_sid0 or
            anomalies_state[o.sid] then
          local dist=posn:distance_to(o.pos)-o.radius
          if dist<mindist then
            mindist=dist
            anomid=id
            pos1=o.pos
            cls=o.clsid
            anomdesc=o
            sect=o.section
          end
        end
      end
    end
  end
  if anomid then
    nearest_anomaly={hasanom=true,id=anomid,pos=pos1,cls=cls,dist=mindist+anomdesc.radius,radius=anomdesc.radius,section=sect}
    return true,nearest_anomaly
  else
    nearest_anomaly={hasanom=false}
    return false
  end
end

function get_anomaly_list(npc,radius)
  local posn=npc:position()
  local ret={}
  if anom_list then
    for id,o in pairs(anom_list) do
      if o.sid>=amk_hideouts.anom_sid0+amk_hideouts.anom_count or o.sid<amk_hideouts.anom_sid0 or
          anomalies_state[o.sid] then
        local obj=level.object_by_id(id)
        if obj then
        -- amk.mylog("chkp2 "..type(o.pos))
          if o.pos==0 then
            o.pos=obj:position()
          end
          local dist=posn:distance_to(o.pos)
          --amk.mylog("chkp3 "..dist.." rad "..radius.." anom "..obj:name())
          if dist<radius then
        -- amk.mylog("chkp4 "..dist.." "..o.radius.." "..id)
            table.insert(ret,{id=id,name=obj:name(),pos=o.pos,sid=obj:story_id(),sect=obj:section()})
        -- amk.mylog("chkp5 "..dist.." "..o.radius.." "..id)
          end
        end
      end
    end
  end
  return ret
end

local afs={
--симбион
"af_medusa",
"af_blood",
"af_rusty_thorn",

--жарка
"af_soul",
"af_spirit_1",
"af_spirit_2",
"af_cry_1",
"af_rusty_sea-urchin", 
"af_kol_3",
"af_armor_3",

--электра
"af_spirit_3",
"af_drops", -- +симбион
"af_dummy_glassbeads",
"af_dummy_dummy",
"af_pudd_1",
"af_pudd_2",
"af_dik_1",

--холодец
"af_cry_2",
"af_babka_1",
"af_pudd_3",
"af_dik_2",
"af_kol_2",
"af_armor_2",

--карусель
"af_babka_2",
"af_kol_1",
"af_armor_1",

--трамплин
"af_dik_3",
"af_fuzz_kolobok",
"af_dummy_pellicle"
}

function check_for_af_drop(obj)
  local section=obj:section()
  for a=1,table.getn(afs),1 do
    if afs[a]==section then
      get_nearest_anomaly(obj)
      if nearest_anomaly.hasanom and nearest_anomaly.radius-nearest_anomaly.dist>-1 then
        if olp_mod.anoms then
        ckeck_af_transform(obj)
        else
        check_af_transform(obj)
        end
      end
      return
    end
  end
end

function ckeck_af_transform(af)
  local af_sect=af:section()
  local anom_sect=nearest_anomaly.section
  local pos=af:position()
  gv=level.object_by_id(nearest_anomaly.id):game_vertex_id()
  if gv==65535 then gv=af:game_vertex_id() end
  if gv==65535 then gv=db.actor:game_vertex_id() end
  if gv==65535 then return end
  lv=db.actor:level_vertex_id()
  local actor = db.actor
  local dis = level.get_time_hours()
  
  if dis==18 or dis==19 or dis==20 or dis==21 or dis==22 or dis==23 or dis==0 or dis==1 or dis==2 or dis==3 or dis==4 or dis==5 then	
  if string.find(anom_sect,"_zharka")~=nil then
    if af_sect=="af_soul" and actor:has_info("info_amk_recipt_souls") then
      af_flash(af)
      af_start_transform_timer(af_start_transform(70,25,af_sect,"af_spirit_1"), pos ,0,4,0,"Soul Drops")
    end
    if af_sect=="af_spirit_1" and actor:has_info("info_amk_recipt_souls") then
      af_flash(af)
      af_start_transform_timer(af_start_transform(60,20,af_sect,"af_spirit_2"), pos ,0,5,0,"Fiery Soul")
    end
    if af_sect=="af_spirit_2" and actor:has_info("info_amk_recipt_souls") then
      af_flash(af)
      af_start_transform_timer(af_start_transform(50,35,af_sect,"af_spirit_3"), pos ,0,6,0,"Crystal Soul")
    end
    if af_sect=="af_cry_1" and actor:has_info("info_amk_recipt_tears_fire") then
      af_flash(af)
      af_start_transform_timer(af_start_transform(65,20,af_sect,"af_cry_2"), pos ,0,5,0,"Fire Tears")
    end
    if af_sect=="af_rusty_sea-urchin" and actor:has_info("info_amk_recipt_dikoobraz") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(65,20,af_sect,"af_dik_1"), pos ,0,3,0,"Porcupine")
    end
    if af_sect=="af_kol_3" and actor:has_info("info_amk_recipt_giant_small_brother") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(49,30,af_sect,"af_kol_4"), pos ,0,8,0,"Sphere")
    end
    if af_sect=="af_armor_3" and actor:has_info("info_amk_recipt_controller_skalp") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(40,25,af_sect,"af_armor_4"), pos ,0,8,0,"Controller Scalp")
    end   
  end
  if string.find(anom_sect,"_galant")~=nil then
    if af_sect=="af_spirit_3" and actor:has_info("info_amk_recipt_souls") then 
      af_flash(af)
 af_start_transform_timer(af_start_transform(50,30,af_sect,"af_spirit_4"), pos ,0,3,0,"Fervent Crystal")
    end
    if af_sect=="af_drops" and actor:has_info("info_amk_recipt_tears_electra") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(75,10,af_sect,"af_cry_1"), pos ,0,2,0,"Electro Tears")
    end
    if af_sect=="af_dummy_glassbeads" and actor:has_info("info_amk_recipt_grandmother_glassbeards") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(85,10,af_sect,"af_babka_1"), pos ,0,3,0,"Old Glassbeads")
    end
    if af_sect=="af_dummy_dummy" and actor:has_info("info_amk_recipt_dummy") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(70,25,af_sect,"af_pudd_1"), pos ,0,3,0,"Fire Dummy")
    end
    if af_sect=="af_pudd_1" and actor:has_info("info_amk_recipt_dummy") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(59,20,af_sect,"af_pudd_2"), pos ,0,5,0,"Shining Dummy")
    end
    if af_sect=="af_pudd_2" and actor:has_info("info_amk_recipt_dummy") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(50,35,af_sect,"af_pudd_3"), pos ,0,7,0,"Moon Dummy")
    end
    if af_sect=="af_dik_1" and actor:has_info("info_amk_recipt_electra_dikoobraz") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(72,25,af_sect,"af_dik_2"), pos ,0,4,0,"Electro Porcupine")
    end
  end
  if string.find(anom_sect,"_buzz")~=nil then
    if af_sect=="af_cry_2" and actor:has_info("info_amk_recipt_tears_chimaera") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(55,25,af_sect,"af_cry_3"), pos ,0,4,0,"Chimaera Tears")
    end
    if af_sect=="af_babka_1" and actor:has_info("info_amk_recipt_pra_grandmother_glassbeards") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(72,12,af_sect,"af_babka_2"), pos ,0,6,0,"Ancient Glassbeads")
    end
    if af_sect=="af_pudd_3" and actor:has_info("info_amk_recipt_dummy") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(80,10,af_sect,"af_pudd_4"), pos ,0,8,0,"Pudding")
    end
    if af_sect=="af_dik_2" and actor:has_info("info_amk_recipt_sopl_dikoobraz") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(74,10,af_sect,"af_dik_3"), pos ,0,5,0,"Acidic Porcupine")
    end
    if af_sect=="af_kol_2" and actor:has_info("info_amk_recipt_almaz_kolobok") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(78,17,af_sect,"af_kol_3"), pos ,0,7,0,"Diamond Kolobok")
    end
    if af_sect=="af_armor_2" and actor:has_info("info_amk_recipt_pancir") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(65,25,af_sect,"af_armor_3"), pos ,0,6,0,"Steel Scale")
    end
    
    ----симбион
    if af_sect=="af_medusa" or af_sect=="af_drops" or af_sect=="af_blood" or af_sect=="af_rusty_thorn" and actor:has_info("info_amk_recipt_simbion") then
      local s={}
        s.aid=-1
        s.koluchka=-1
        s.medusa=-1
        s.drop=-1
        s.blood=-1

      s=amk.unpack_array_from_string(amk.load_variable("af_simbion_activation",amk.pack_array_to_string(s)))
      
      if s.aid~=nearest_anomaly.id then
        s.aid=nearest_anomaly.id
        s.koluchka=-1
        s.medusa=-1
        s.drop=-1
        s.blood=-1
      end
      if af_sect=="af_medusa" and s.medusa==-1 then
        s.medusa=af:id()
      end
      if af_sect=="af_drops" and s.drop==-1 then
        s.drop=af:id()
      end
      if af_sect=="af_rusty_thorn" and s.koluchka==-1 then
        s.koluchka=af:id()
      end
      if af_sect=="af_blood" and s.blood==-1 then
        s.blood=af:id()
      end
      if s.medusa~=-1 and s.drop~=-1 and s.koluchka~=-1 and s.blood~=-1 then
        level.add_pp_effector("teleport.ppe", 1524, false)
        alife():release(alife():object(s.medusa), true)
        alife():release(alife():object(s.drop), true)
        alife():release(alife():object(s.koluchka), true)
        alife():release(alife():object(s.blood), true)
        amk.del_variable("af_simbion_activation")
    
        af_start_transform_timer(af_start_transform(75,25,"af_buliz","af_simbion") , pos ,0,7,0,"Simbion")
        return
      end
      amk.save_variable("af_simbion_activation",amk.pack_array_to_string(s))
    end
  end
  if string.find(anom_sect,"_mincer")~=nil then
    if af_sect=="af_babka_2" and actor:has_info("info_amk_recipt_burer_grandmother_glassbeards") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(85,10,af_sect,"af_babka_3"), pos ,0,6,0,"Elder Glassbeads")
    end
    if af_sect=="af_kol_1" and actor:has_info("info_amk_recipt_titan_kolobok") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(55,35,af_sect,"af_kol_2"), pos ,0,5,0,"Titan Kolobok")
    end
    if af_sect=="af_armor_1" and actor:has_info("info_amk_recipt_cheshya") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(80,10,af_sect,"af_armor_2"), pos ,0,3,0,"Scale")
    end
  end
  if string.find(anom_sect,"_mosquito_bald")~=nil then
    if af_sect=="af_dik_3" and actor:has_info("info_amk_recipt_stone_dikoobraz") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(50,28,af_sect,"af_dik_4"), pos ,0,8,0,"Stone Porcupine")
    end
    if af_sect=="af_fuzz_kolobok" and actor:has_info("info_amk_recipt_steel_kolobok") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(70,25,af_sect,"af_kol_1"), pos ,0,3,0,"Steel Kolobok")
    end
    if af_sect=="af_dummy_pellicle" and actor:has_info("info_amk_recipt_shkura") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(70,25,af_sect,"af_armor_1"), pos ,0,4,0,"Skin")
    end
  end
end
--olp--
end

function check_af_transform(af)
  local af_sect=af:section()
  local anom_sect=nearest_anomaly.section
  local pos=af:position()
  gv=level.object_by_id(nearest_anomaly.id):game_vertex_id()
  if gv==65535 then gv=af:game_vertex_id() end
  if gv==65535 then gv=db.actor:game_vertex_id() end
  if gv==65535 then return end
  lv=db.actor:level_vertex_id()
  local actor = db.actor
  	
  if string.find(anom_sect,"_zharka")~=nil then
    if af_sect=="af_soul" and actor:has_info("info_amk_recipt_souls") then
      af_flash(af)
      af_start_transform_timer(af_start_transform(70,25,af_sect,"af_spirit_1"), pos ,0,4,0,"Soul Drops")
    end
    if af_sect=="af_spirit_1" and actor:has_info("info_amk_recipt_souls") then
      af_flash(af)
      af_start_transform_timer(af_start_transform(60,20,af_sect,"af_spirit_2"), pos ,0,5,0,"Fiery Soul")
    end
    if af_sect=="af_spirit_2" and actor:has_info("info_amk_recipt_souls") then
      af_flash(af)
      af_start_transform_timer(af_start_transform(50,35,af_sect,"af_spirit_3"), pos ,0,6,0,"Crystal Soul")
    end
    if af_sect=="af_cry_1" and actor:has_info("info_amk_recipt_tears_fire") then
      af_flash(af)
      af_start_transform_timer(af_start_transform(65,20,af_sect,"af_cry_2"), pos ,0,5,0,"Fire Tears")
    end
    if af_sect=="af_rusty_sea-urchin" and actor:has_info("info_amk_recipt_dikoobraz") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(65,20,af_sect,"af_dik_1"), pos ,0,3,0,"Porcupine")
    end
    if af_sect=="af_kol_3" and actor:has_info("info_amk_recipt_giant_small_brother") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(49,30,af_sect,"af_kol_4"), pos ,0,8,0,"Sphere")
    end
    if af_sect=="af_armor_3" and actor:has_info("info_amk_recipt_controller_skalp") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(40,25,af_sect,"af_armor_4"), pos ,0,8,0,"Controller Scalp")
    end   
  end
  if string.find(anom_sect,"_galant")~=nil then
    if af_sect=="af_spirit_3" and actor:has_info("info_amk_recipt_souls") then 
      af_flash(af)
 af_start_transform_timer(af_start_transform(50,30,af_sect,"af_spirit_4"), pos ,0,3,0,"Fervent Crystal")
    end
    if af_sect=="af_drops" and actor:has_info("info_amk_recipt_tears_electra") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(75,10,af_sect,"af_cry_1"), pos ,0,2,0,"Electro Tears")
    end
    if af_sect=="af_dummy_glassbeads" and actor:has_info("info_amk_recipt_grandmother_glassbeards") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(85,10,af_sect,"af_babka_1"), pos ,0,3,0,"Old Glassbeads")
    end
    if af_sect=="af_dummy_dummy" and actor:has_info("info_amk_recipt_dummy") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(70,25,af_sect,"af_pudd_1"), pos ,0,3,0,"Fire Dummy")
    end
    if af_sect=="af_pudd_1" and actor:has_info("info_amk_recipt_dummy") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(59,20,af_sect,"af_pudd_2"), pos ,0,5,0,"Shining Dummy")
    end
    if af_sect=="af_pudd_2" and actor:has_info("info_amk_recipt_dummy") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(50,35,af_sect,"af_pudd_3"), pos ,0,7,0,"Moon Dummy")
    end
    if af_sect=="af_dik_1" and actor:has_info("info_amk_recipt_electra_dikoobraz") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(72,25,af_sect,"af_dik_2"), pos ,0,4,0,"Electro Porcupine")
    end
  end
  if string.find(anom_sect,"_buzz")~=nil then
    if af_sect=="af_cry_2" and actor:has_info("info_amk_recipt_tears_chimaera") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(55,25,af_sect,"af_cry_3"), pos ,0,4,0,"Chimaera Tears")
    end
    if af_sect=="af_babka_1" and actor:has_info("info_amk_recipt_pra_grandmother_glassbeards") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(72,12,af_sect,"af_babka_2"), pos ,0,6,0,"Ancient Glassbeads")
    end
    if af_sect=="af_pudd_3" and actor:has_info("info_amk_recipt_dummy") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(80,10,af_sect,"af_pudd_4"), pos ,0,8,0,"Pudding")
    end
    if af_sect=="af_dik_2" and actor:has_info("info_amk_recipt_sopl_dikoobraz") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(74,10,af_sect,"af_dik_3"), pos ,0,5,0,"Acidic Porcupine")
    end
    if af_sect=="af_kol_2" and actor:has_info("info_amk_recipt_almaz_kolobok") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(78,17,af_sect,"af_kol_3"), pos ,0,7,0,"Diamond Kolobok")
    end
    if af_sect=="af_armor_2" and actor:has_info("info_amk_recipt_pancir") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(65,25,af_sect,"af_armor_3"), pos ,0,6,0,"Steel Scale")
    end
    
    ----симбион
    if af_sect=="af_medusa" or af_sect=="af_drops" or af_sect=="af_blood" or af_sect=="af_rusty_thorn" and actor:has_info("info_amk_recipt_simbion") then
      local s={}
        s.aid=-1
        s.koluchka=-1
        s.medusa=-1
        s.drop=-1
        s.blood=-1

      s=amk.unpack_array_from_string(amk.load_variable("af_simbion_activation",amk.pack_array_to_string(s)))
      
      if s.aid~=nearest_anomaly.id then
        s.aid=nearest_anomaly.id
        s.koluchka=-1
        s.medusa=-1
        s.drop=-1
        s.blood=-1
      end
      if af_sect=="af_medusa" and s.medusa==-1 then
        s.medusa=af:id()
      end
      if af_sect=="af_drops" and s.drop==-1 then
        s.drop=af:id()
      end
      if af_sect=="af_rusty_thorn" and s.koluchka==-1 then
        s.koluchka=af:id()
      end
      if af_sect=="af_blood" and s.blood==-1 then
        s.blood=af:id()
      end
      if s.medusa~=-1 and s.drop~=-1 and s.koluchka~=-1 and s.blood~=-1 then
        level.add_pp_effector("teleport.ppe", 1524, false)
        alife():release(alife():object(s.medusa), true)
        alife():release(alife():object(s.drop), true)
        alife():release(alife():object(s.koluchka), true)
        alife():release(alife():object(s.blood), true)
        amk.del_variable("af_simbion_activation")
    
        af_start_transform_timer(af_start_transform(75,25,"af_buliz","af_simbion") , pos ,0,7,0,"Simbion")
        return
      end
      amk.save_variable("af_simbion_activation",amk.pack_array_to_string(s))
    end
  end
  if string.find(anom_sect,"_mincer")~=nil then
    if af_sect=="af_babka_2" and actor:has_info("info_amk_recipt_burer_grandmother_glassbeards") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(85,10,af_sect,"af_babka_3"), pos ,0,6,0,"Elder Glassbeads")
    end
    if af_sect=="af_kol_1" and actor:has_info("info_amk_recipt_titan_kolobok") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(55,35,af_sect,"af_kol_2"), pos ,0,5,0,"Titan Kolobok")
    end
    if af_sect=="af_armor_1" and actor:has_info("info_amk_recipt_cheshya") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(80,10,af_sect,"af_armor_2"), pos ,0,3,0,"Scale")
    end
  end
  if string.find(anom_sect,"_mosquito_bald")~=nil then
    if af_sect=="af_dik_3" and actor:has_info("info_amk_recipt_stone_dikoobraz") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(50,28,af_sect,"af_dik_4"), pos ,0,8,0,"Stone Porcupine")
    end
    if af_sect=="af_fuzz_kolobok" and actor:has_info("info_amk_recipt_steel_kolobok") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(70,25,af_sect,"af_kol_1"), pos ,0,3,0,"Steel Kolobok")
    end
    if af_sect=="af_dummy_pellicle" and actor:has_info("info_amk_recipt_shkura") then
      af_flash(af)
 af_start_transform_timer(af_start_transform(70,25,af_sect,"af_armor_1"), pos ,0,4,0,"Skin")
    end
  end
end

function af_flash(af)
  level.add_pp_effector("teleport.ppe", 1524, false)
  amk.remove_item(af)
end

function af_start_transform(v1,v2,af_from,af_target)
  local rnd=math.random(0,100)
  if rnd>v1 then
    if rnd>v2+v1 then
      return af_from
    else
      return "af_buliz"
    end
  else
    return af_target
  end
end

function af_start_transform_timer(af,pos,delay_d,delay_h,delay_m,af_sect)
  local t={}
  t.section=af
  t.pos={}
    t.pos.x=pos.x
    t.pos.y=pos.y
    t.pos.z=pos.z
  t.gv=gv
  t.lv=lv
  t.from_sect=af_sect

  amk.g_start_timer("af_transform",delay_d,delay_h,delay_m,amk.pack_array_to_string(t))
end

function af_transform_end(params)
  local from_sect="\""..params.from_sect.."\""
  local obj
  obj=amk.spawn_item(params.section,vector():set(params.pos.x,params.pos.y,params.pos.z),params.gv,params.lv)

if olp_mod and olp_mod.mapspot then amk.add_spot_on_map(obj.id,"red_location",game.translate_string("trans_finished_title").."%c[255,255,0,0]"..from_sect)
end
  amk.send_tip(game.translate_string("trans_finished_msg").."%c[255,255,0,0]"..from_sect)
end
---------------------------------------------
--beacon
local beakons={}

function check_beacon_drop(obj)
  local sim=not (alife()==nil)
  local actor=db~=nil and db.actor~=nil
  local lp=level.present()
  local dp=device()==nil or device():is_paused()
  if obj:section()=="amk_metka" then
    beakons[obj:id()]=true
  end
end

function check_metka()
  for id,v in pairs(beakons) do
    local obj=level.object_by_id(id)
    if obj then
      beakons[id]=nil
      if obj:parent()==nil then
        local spwn=ui_dots.dots(get_hud(),id)
    level.start_stop_menu(spwn,true)
      end
    end
  end  
end

function check_beacon_take(obj)
  if obj:section()=="amk_metka" then
    amk.remove_spot_from_map(obj:id(),"red_location")
  end
end
---------------

function blowout_phantoms()
  local blow=amk.load_variable("blowout",-1)
  local bt=blowout_type()
  if blow>1 and blow<4 and bt<2 then
    local lname=level.name()
    if not in_hide and amk_hideouts.blowouts_phantoms[lname] then
      local phantoms=amk_hideouts.blowouts_phantoms[lname]
      if phantoms and phantoms.count>0 then
        if math.random()<phantoms.probability then
          if phantom_manager:phantom_count()<phantoms.count then
            local yaw       = math.pi*2.0*math.random()
            local radius      = phantoms.radius*(math.random()/2.0+0.5)
            local height      = phantoms.height*math.random()
            local a_pos       = db.actor:position()
            local pos         = vector():set(math.sin(yaw)*radius+a_pos.x,a_pos.y+height,math.cos(yaw)*radius+a_pos.z)
            phantom_manager.spawn_phantom(pos)
          end
        end
      end
    end
  end
end

function blowout_scary_sounds()
  local blow=amk.load_variable("blowout",-1)
  if blow>0 and blow<4 then
    local snd_obj = xr_sound.get_safe_sound_object(amk_hideouts.blowout_sounds[math.random(table.getn(amk_hideouts.blowout_sounds))])
    local a=vector()
    a.x=math.random(0,10)
    a.y=math.random(0,10)
    a.z=math.random(0,10)
    snd_obj:play_at_pos(db.actor, db.actor:position():add(a))
    amk.start_timer("blowout_ss",math.random(10,30),0)
  end
end

function blowout_psy_sound(action)
  if action=="start" then
    snd_obj_eq = sound_object([[ambient\earthquake]],sound_object.looped+sound_object.s2d)
    snd_obj_eq:play(db.actor, 4, sound_object.looped+sound_object.s2d)
  end
  if action == "stop" then
    if snd_obj_eq then snd_obj_eq:stop() end
  end
  
  local lname=level.name()
  if not amk_hideouts.blowouts_phantoms[lname] then return end
  local psy_sound=amk_hideouts.blowouts_phantoms[lname].psy_sounds or false 
  if not psy_sound then return end

  if action=="start" then
    local level_vol=amk_hideouts.blowouts_phantoms[lname].level_vol or 1        
    sound_obj_right, sound_obj_left = xr_sound.get_sound_object("psy_voices", "random")
    sound_obj_left:play_at_pos (db.actor, vector():set(-1, 0, 1), 0, sound_object.s2d + sound_object.looped)
    sound_obj_right:play_at_pos(db.actor, vector():set( 1, 0, 1), 0, sound_object.s2d + sound_object.looped)
    snd_volume  = level.get_snd_volume()
    level.set_snd_volume( level_vol )
  end
  if action == "stop" then
    if sound_obj_left then sound_obj_left:stop() end
    if sound_obj_right then sound_obj_right:stop() end
    if snd_volume then level.set_snd_volume(snd_volume) end
  end
  
end

function Run_Blowout_pp()
  
  local blow_enabled = system_ini():r_float("blowout_period","enabled")

  if blow_enabled>0 then
    local bt=blowout_type()
  
    sleep_manager.stopper()
    amk.save_variable("blowout",1)
    if bt<1 then
      level.add_pp_effector("monolith_off.ppe", 2003, false)
      level.add_cam_effector("camera_effects\\shell_shock.anm", 2004, false, "")
      local snd_obj = xr_sound.get_safe_sound_object([[ambient\organic_moan1]])
      snd_obj:play_no_feedback(db.actor, sound_object.s2d, 0, vector(), 1.0)
    end
    if level.get_time_days()~=28 or
      level.get_time_days()~=29 or
      level.get_time_days()~=30 or
      level.get_time_days()~=31 then
     amk.start_timer("blowout_ss",math.random(5,15),0)
     amk.start_timer("blowout",11,0)
    end
  end
  if level.get_time_days()~=28 or
   level.get_time_days()~=29 or
   level.get_time_days()~=30 or
   level.get_time_days()~=31 then
    start_blow_timer()
  end
end

function start_blow_timer()
  
  local time = system_ini():r_float("blowout_period","time")
  local time2 = system_ini():r_float("blowout_period","time2")
  
  local shift = system_ini():r_float("blowout_period","shift")
  local shift1 = system_ini():r_float("blowout_period","shift1")
  local shift2 = system_ini():r_float("blowout_period","shift2")
  local shift3 = system_ini():r_float("blowout_period","shift3")
  local shift4 = system_ini():r_float("blowout_period","shift4")
  local shift5 = system_ini():r_float("blowout_period","shift5")
  local shift6 = system_ini():r_float("blowout_period","shift6")
  local shift7 = system_ini():r_float("blowout_period","shift7")
  
  local const = math.random(1, 2)
  
  if const==1 then
  next_blow = time
  end
  if const==2 then
  next_blow = time2
  end
  
  local rndmz = math.random(1, 4)

  if rndmz==1 then
  kany = math.random()*shift-shift
  end
  if rndmz==2 then
  kany = math.random()*shift1-shift1
  end
  if rndmz==3 then
  kany = math.random()*shift2-shift2
  end
  if rndmz==4 then
  kany = math.random()*shift3-shift3
  end
 
  if level.get_time_days()~=28 or
   level.get_time_days()~=29 or
   level.get_time_days()~=30 or
   level.get_time_days()~=31 then
  amk.g_start_timer( "blow_shift",0,0,next_blow+kany )
 end
  if level.get_time_days()==1 then
  amk.g_start_timer("gg_need_sleep",0,0,6)
 end
end

function Blowout_pp(phase)
  amk.mylog("blowout "..phase)
  local bt=blowout_type()
  if phase==0 then
    if bt<1 then
      level.set_weather("pre_blow",true)
      level.set_weather_fx("amk_surge_day")
    end 
    amk.g_start_timer("blowout",0,0,4,1)
  elseif phase==1 then
    local snd_obj = xr_sound.get_safe_sound_object([[ambient\earthquake]])
    snd_obj:play_no_feedback(db.actor, sound_object.s2d, 0, vector(), 1.0)
    level.add_cam_effector("camera_effects\\earthquake.anm", 2002, true, "")
    snd_obj = xr_sound.get_safe_sound_object([[anomaly\blowout]])
    snd_obj:play_no_feedback(db.actor, sound_object.s2d, 0, vector(), 1.0)
    --blowout_psy_sound("start")
    --db.actor:give_info_portion("surge_day_active")
    
    if bt<1 then
      level.add_pp_effector("vibros.ppe", 2001, true)
    end
    
    in_hide=false
    amk.g_start_timer("blowout",0,0,6,2)
    amk.save_variable("blowout",2)
  elseif phase==2 then
    amk.g_start_timer("blowout",0,0,5,3)
    --amk.save_variable("blowout",2)
  elseif phase==3 then

    amk.save_variable("blowout",3)
    
    --[[
    local h = hit()
    local cr
    h.type = hit.strike
    h.power = 1000
    h.impulse = 1000
    
    for a=1,65534,1 do
      cr=level.object_by_id(a)
      if cr~=nil and cr:section()=="helicopter" and math.random(0,100)<70 then
        h.draftsman = cr
        h.direction = cr:direction()
        cr:hit(h)
      end
    end
    ]]--
    
    remove_anoms()
    amk.g_start_timer("blowout",0,0,10,4)
  elseif phase==4 then
    amk.save_variable("blowout",4)
    level.remove_cam_effector(2002)
    if bt<1 then
      level.remove_pp_effector(2001)
      level.add_pp_effector("monolith_off.ppe", 2003, false)
    end
    --blowout_psy_sound("stop")
    
    create_anoms()
    oblivion_lost.Spawn_Rank()
    oblivion_lost.R_Art()
    amk.start_timer("blowout",11,5)
  elseif phase==5 then
  
  --level.set_weather(amk.load_variable("weather","default"))
        
    if bt<1 then
      local snd_obj = xr_sound.get_safe_sound_object([[anomaly\teleport_work2]])
      snd_obj:play_no_feedback(db.actor, sound_object.s2d, 0, vector(), 1.0)
    
    if not indoors[lname] then
      level.set_weather("chernobyl")
			amk.save_variable("rand_wth_h",level.get_time_hours())
			amk.save_variable("weather","chernobyl")
		end
		
      --amk.save_variable("dynw","pasmurno")
      --amk.save_variable("dynlasthour", level.get_time_hours())
      --amk.save_variable("dynweather", "amk_pasmurno_pasmurno_"..level.get_time_hours())

 	 end
 	  --db.actor:disable_info_portion("surge_day_active")
    amk.del_variable("blowout")
    amk.save_variable("blowout_happened",1)
  end
end

local next_actor_hit_time=0
function on_blowout_hit_actor()
  if next_actor_hit_time<time_global() then
    next_actor_hit_time=time_global()+1000
    on_blowout_hit(db.actor)
  end
end

function on_blowout_hit(npc,hide)
  local bt=blowout_type()
  if bt>0 or amk.load_variable("blowout",-1)~=3 then 
    if npc:id()==db.actor:id() then in_hide = true end
    return false
  end
  local lname=level.name()
    local need_hit
    need_hit=true
    local khit=1
    if hide and hide.zone then
        need_hit = not check_npc_in_hideout(npc,hide)
        if not need_hit then
            if hide.defence then 
                khit=hide.defence
            else
                khit=0
            end
        end
    else
        local hides=amk_hideouts.hide[lname]
        local tmp
        if hides then
            for i,o in ipairs(hides) do
                if o.zone then
                    for j,v in ipairs(o.zone) do
                        if v.p3 then
                          tmp=amk.check_npc_in_box(npc,vector():set(unpack(v.p1)),vector():set(unpack(v.p2)),vector():set(unpack(v.p3)))
                        else
                          tmp=amk.check_npc_in_box(npc,vector():set(unpack(v.p1)),vector():set(unpack(v.p2)))
                        end
                        need_hit = need_hit and not tmp
                        if tmp then
                            if o.defence then 
                                khit=o.defence
                            else
                                khit=0
                            end
                        end
                    end
                end
            end
        end
    end

    if npc:id()==db.actor:id() then in_hide = not need_hit end
    
    if (need_hit or khit>0) and amk.load_variable("blowout",-1)==3 then
      local h = hit()
      h.power = (amk_hideouts.blowout_damages[lname] or 0.01)*khit
      h.impulse = 0
      h.draftsman = npc
      h.direction = vector():set(0,0,0)
      h:bone("bip01_spine") -- чтобы учитывалась броня
      h.type = hit.strike
      npc:hit(h)
      --h.type = hit.telepatic
      --npc:hit(h)
      h.type = hit.radiation
      npc:hit(h)
    end
    
    return need_hit or khit>0
end

function check_npc_in_hideout(npc,hide)
  local not_in_zone=true
  if hide and hide.zone then
    for k,v in pairs(hide.zone) do
      if v.p3 then 
        not_in_zone = not_in_zone and not amk.check_npc_in_box(npc,vector():set(unpack(v.p1)),vector():set(unpack(v.p2)),vector():set(unpack(v.p3)))
      else
        not_in_zone = not_in_zone and not amk.check_npc_in_box(npc,vector():set(unpack(v.p1)),vector():set(unpack(v.p2)))
      end
    end
  end
  return not not_in_zone
end

function initial_spawn(action)
  local fr=0
  for i=0,amk_hideouts.anom_count-1,1 do
    local sobj = alife():story_object(amk_hideouts.anom_sid0+i)
    if not sobj then 
      alife():create(i+amk_hideouts.anom_num0) 
      fr=1
    end
  end
  if fr==1 then create_anoms() end
end

anomalies_state={}

function remove_anoms()
  amk.save_variable("blowout_anoms",string.rep(string.char(128),math.ceil(amk_hideouts.anom_count/7)))
  update_anoms()
end

function create_anoms()
  anomalies_state={}
  for i=amk_hideouts.anom_sid0,amk_hideouts.anom_sid0+amk_hideouts.anom_count-1,1 do
    anomalies_state[i]=math.random(0,1)==1
  end
  local str=pack_anoms(anomalies_state)
  amk.save_variable("blowout_anoms",str)
  update_anoms()
end

function update_anoms()
  local anoms=amk.load_variable("blowout_anoms",string.rep(string.char(128),math.ceil(amk_hideouts.anom_count/7)))
  anomalies_state=unpack_anoms(anoms)
  for i=amk_hideouts.anom_sid0,amk_hideouts.anom_sid0+amk_hideouts.anom_count-1,1 do
    local sobj=alife():story_object(i)
    if sobj then
      local obj=level.object_by_id(sobj.id)
      if obj then
        if anomalies_state[i] then
          obj:enable_anomaly()
        else
          obj:disable_anomaly()
          end
        end
      end
    end
end

-- anom_state[sid]==true для активных аномалий
function pack_anoms(anom_state)
  local str=""
  for i=0,amk_hideouts.anom_count-1,7 do
    local cval=128
    local mul=1
    for j=0,6,1 do
      if anom_state[i+j+amk_hideouts.anom_sid0] then
        cval=cval+mul
      end
      mul=mul*2
    end
    str=str..string.char(cval)
  end
  return str
end

function unpack_anoms(str)
  local anom={}
  for i=1,string.len(str),1 do
    local cval=string.byte(string.sub(str,i,i))
    local mul=1
    for j=0,6,1 do
      anom[(i-1)*7+j+amk_hideouts.anom_sid0]=bit_and(mul,cval)~=0
      mul=mul*2
    end
  end
  return anom
end

function blowout_type()
  local lname=level.name()
  local t=amk_hideouts.blowout_types[lname]
  --if t>1 and amk.load_variable("freeplay",0)==2 then t=0 end
  return t -- or 0
end
---------------
function check_usable_item(obj)
  local info=nil
  if obj:section()=="vehicle_btr" then
    db.actor:kill(db.actor)
  end
  if obj:name()=="mil_stalker0012" then
    info="info_amk_recipt_stone_dikoobraz"
  elseif level.name()=="l01_escape" and obj:section()=="amk_zapiska" then
    info="info_amk_recipt_shkura"
    amk.drop_item(db.actor,obj)
    amk.remove_item(obj)
  elseif level.name()=="l04u_labx18" and obj:section()=="amk_zapiska" then
    info="info_amk_recipt_simbion"
    amk.drop_item(db.actor,obj)
    amk.remove_item(obj)
  elseif level.name()=="l08u_brainlab" and obj:section()=="amk_zapiska" then
    info="info_amk_recipt_dummy"
    amk.drop_item(db.actor,obj)
    amk.remove_item(obj)
  else
    local recipes=amk.load_table("amk_body_recipe")
    info=recipes[obj:id()]
  end  
  if db.actor==nil or info==nil or db.actor:has_info(info) then
    return
  end
  db.actor:give_info_portion(info)
  amk_dialogs.info_received()
end

function generate_recipe(obj)
  local info=nil
  local prop=100
  
  if obj.name and obj:name()=="bar_dolg_regular_5" and not has_alife_info("bar_dolg_base_pass") then
  db.actor:give_info_portion("bar_dolg_base_pass")
  db.actor:give_info_portion("bar_dolg_community_leader")
  end
   
  if obj.name and obj:name()=="mil_blockpost_freedomstalker001" then
   if not has_alife_info("info_amk_recipt_electra_dikoobraz") or
      not has_alife_info("info_amk_recipt_giant_small_brother") then
    info="info_kanyhalos_recipt"
    prob=100
  end
  end
  
  if level.name()=="l07_military" and obj.character_community and
          obj:character_community()=="killer" then
    info="info_amk_recipt_titan_kolobok"
    prob=30
    
  elseif level.name()=="l10_radar" and obj.character_community and
          obj:character_community()=="monolith" then
    info="info_amk_recipt_controller_skalp"
    prob=10
    
  elseif level.name()=="l01_escape" and obj.character_community and
          obj:character_community()=="military" then
    info="info_amk_recipt_shkura"
    prob=20
    
  elseif level.name()=="l06_rostok" and obj.character_community and
          obj:character_community()=="bandit" then
    info="info_amk_recipt_simbion"
    prob=40
    
  elseif level.name()=="l04_darkvalley" and obj.character_community and
          obj:character_community()=="bandit" then
    info="info_amk_recipt_pra_grandmother_glassbeards"
    prob=10
    
  elseif not has_alife_info("info_amk_recipt_almaz_kolobok") and obj.name and 
obj:name()=="bar_ecolog_professor" then
    info="info_amk_recipt_almaz_kolobok"
    prob=100
    
  elseif not has_alife_info("info_amk_recipt_sopl_dikoobraz") and obj.name and 
obj:name()=="val_sos_wounded" then
    info="info_amk_recipt_sopl_dikoobraz"
    prob=100
    
  elseif not has_alife_info("info_amk_recipt_burer_grandmother_glassbeards") and obj.name and 
obj:name()=="esc_stalker_fanat" then
    info="info_amk_recipt_burer_grandmother_glassbeards"
    prob=100
    
  elseif not has_alife_info("info_amk_recipt_burer_grandmother_glassbeards") and obj.name and 
obj:name()=="agr_ratcatcher" then
    info="info_amk_recipt_burer_grandmother_glassbeards"
    prob=100
    
  elseif not has_alife_info("info_amk_recipt_grandmother_glassbeards") and obj.name and 
obj:name()=="bar_bar_drunk_dolg" then
    info="info_amk_recipt_grandmother_glassbeards"
    prob=100
    
  elseif not has_alife_info("info_amk_recipt_souls") and obj.name and 
obj:name()=="mil_freedom_member0012" then
    info="info_amk_recipt_souls"
    prob=100
    
  elseif not has_alife_info("info_amk_recipt_steel_kolobok") and obj.name and 
obj:name()=="bar_bar_visitor_hunter" then
    info="info_amk_recipt_steel_kolobok"
    prob=100
    
  elseif not has_alife_info("info_amk_recipt_cheshya") and obj.name
  and obj:name()=="bar_dolg_leader" then
    info="info_amk_recipt_cheshya"
    prob=100
    
  elseif not has_alife_info("info_amk_recipt_dikoobraz") and obj.name and 
obj:name()=="bar_bar_osvedomitel" then
    info="info_amk_recipt_dikoobraz"
    prob=100
    
  end
  if not info then return end
  local recipes=amk.load_table("amk_body_recipe")
  -- Проверим не генерился ли уже этот рецепт
  for k,v in pairs(recipes) do
    if v==info then
      return
    end
  end
  -- Сгенерим рецепт
  if math.random(100)<=prob then
    recipes[obj:id()]=info
    amk.save_table("amk_body_recipe",recipes)
  end
end


local currhud
local curr_tex
local cur_weapon_fov

local hud_tbl={
--hud_gaz
svoboda_gaz_outfit_m1="hud_gaz",
dolg_gaz_outfit_m1="hud_gaz",
neytral_gaz_outfit_m1="hud_gaz",
neytral_novice_gaz_outfit_m1="hud_gaz",
svoboda_heavy_gaz_outfit_m1="hud_gaz",
bandit_gaz_outfit_m1="hud_gaz",
--hud_exo
neytral_exo_gaz_outfit_m1="hud_exo",
svoboda_yellow_exo_outfit_m1="hud_exo",
exo_outfit="hud_exo",
outfit_exo_m1="hud_exo",
--hud_merc
killer_gaz_outfit_m1="hud_merc",
--hud_sci
dolg_scientific_outfit="hud_sci",
ecolog_outfit="hud_sci",
protection_outfit="hud_sci",
scientific_outfit="hud_sci",
--hud_military
military_outfit="hud_military",
militaryspec_outfit="hud_military"
}

local current_static
local static_enabled
local current_zoom=1
update_hud=false

function set_hud_tex(static,enabled)
  local zoom=67.5/device().fov
  zoom=(zoom-1)*1.5+1
  if zoom<1.001 then
    zoom=1.001
  end
  local stretchy=0.75/(math.floor(device().aspect_ratio*1000)/1000)
  if stretchy<1 then stretchy=1 end
  local rect={x=-768*zoom+768,y=(-512*zoom+512)*stretchy-(stretchy-1)*300,w=1024*zoom,h=768*zoom*stretchy}
  if amk.load_variable("option_hud",2)==2 then
    set_blurs(enabled and curr_tex~="hud_sci" and static~=nil,rect)
  else
    set_blurs(false,rect)
  end
  if enabled==static_enabled and current_static==static and 
      zoom==current_zoom and (not update_hud) then
    return
  end
  update_hud=false
  current_zoom=zoom
  local need_change=false
  if current_static~=static then
    if current_static then
      get_hud():RemoveCustomStatic(current_static)
    end
    if static then
      get_hud():AddCustomStatic(static)
    end
    need_change=true
    current_static=static
  end
  local wnd=get_hud():GetCustomStatic(current_static) and 
            get_hud():GetCustomStatic(current_static):wnd()
  if (not enabled) and wnd then
    wnd:SetWidth(0)    
  elseif wnd then
    wnd:SetWndRect(rect.x,rect.y,rect.w,rect.h)
  end
  static_enabled=enabled
end

local blurs=nil
local blurval=0 -- уровень запотевания от 0 до 1
local blurlt=0 -- время последнего обновления
local blurcyctime=0 -- время начала последнего цикла дыхания (выдох)
local blurlastphase=0

function set_blurs(enabled,rect)
  if (not blurs) or get_hud():GetCustomStatic("hud_blur1")==nil then
    blurs={}
    for i=1,4 do
      get_hud():AddCustomStatic("hud_blur"..i)
      blurs[i]=get_hud():GetCustomStatic("hud_blur"..i):wnd()
      blurs[i]:SetWidth(0)
    end
  end
  if not enabled then
    for i=1,4 do
      blurs[i]:SetWidth(0)
    end
    return    
  end
  
-- Циклы в зависимости от силы дыхания: 0->1->0 0->1->2->4->5->0 0->1->2->3->4->5->0 5->4->3->4->5 4->3->4

  local power=db.actor.power
  
  local period=1.0+power*power*1.0 -- текущая частота дыхания от 30 до 120 циклов в минуту
  local expirt=0.3
  local breathpower=3
  
  local delta=(time_global()-blurlt)/1000 -- дельта в секундах
  local phase=(time_global()-blurcyctime)/1000 -- фаза дыхательного цикла в сек.
  blurlt=time_global()
  if phase>period then
    phase=phase%period
    blurcyctime=blurlt-phase*1000
  end
  if blurlastphase>phase then
    blurlastphase=0
  end
  
  local blurdelta=delta*-0.7 -- работа вентилляции
  if blurlastphase<expirt and phase<expirt then
    blurdelta=blurdelta+(phase-blurlastphase)*breathpower
  elseif blurlastphase<expirt then
    blurdelta=blurdelta+(expirt-blurlastphase)*breathpower
  end
  blurlastphase=phase
  
  blurval=blurval+blurdelta
  if blurval>0.999 then
    blurval=0.999
  elseif blurval<0 then
    blurval=0
  end
  
  local tm=math.floor(blurval*3)
  local tmn=(tm+1)
  local v=blurval*3-math.floor(blurval*3)
  v=1-v
  local v1=1-v
  if tm~=0 then
    blurs[tm]:SetColor(GetARGB(v*255,255,255,255))
  end
  if tmn~=0 then
    blurs[tmn]:SetColor(GetARGB(v1*255,255,255,255))
  end
  for i=1,4 do
    if i==tm or i==tmn then
      blurs[i]:SetWndRect(rect.x,rect.y,rect.w,rect.h)
    else
      blurs[i]:SetWndRect(rect.x,rect.y,0,0)
    end  
  end
end

local lhup=0

function check_hud()
  local hudotf,actor
  if db and db.actor then
    actor=db.actor
  else
    return false
  end

--[[
  if lhup<time_global() then
    lhup=time_global()+1000
    local wnd
    while true do
      wnd=get_hud():GetCustomStatic("hud_coord") and 
          get_hud():GetCustomStatic("hud_coord"):wnd()
      if wnd then break end
      get_hud():AddCustomStatic("hud_coord")
    end
    
    local dir=device().cam_dir
    
    local a=vector()
    
    a.x=math.atan2(-dir.y,dir.z)
    a.y=math.atan2(-dir.x,dir.z)  
    a.z=math.atan2(-dir.y,dir.x)

    local pos=device().cam_pos
    local act_pos=actor:position()
    local lvid=actor:level_vertex_id()
    local gvid=actor:game_vertex_id()
    local vdist=level.vertex_position(lvid):distance_to(actor:position())
    local hasanom,anom=get_nearest_anomaly(db.actor)
    local msg
    if hasanom then
      local an=level.object_by_id(anom.id)
      local adist=an:position():distance_to(db.actor:position())
      local anname=an:name()
      local sid=an:story_id()
      msg=string.format("actor: x %.2f y %.2f z %.2f lvid %d gvid %d lv_dist %.1f\\ncam pos: x %.2f y %.2f z %.2f\\ncam angles: %.2f %.2f %.2f\\nanom %s sid %d dist %.1f",act_pos.x,act_pos.y,act_pos.z,lvid,gvid,vdist,pos.x,pos.y,pos.z,a.x,a.y,a.z,anname,sid,adist)
    else
      msg=string.format("actor: x %.2f y %.2f z %.2f lvid %d gvid %d lv_dist %.1f\\ncam pos: x %.2f y %.2f z %.2f\\ncam angles: %.2f %.2f %.2f",                           act_pos.x,act_pos.y,act_pos.z,lvid,gvid,vdist,pos.x,pos.y,pos.z,a.x,a.y,a.z)
    end
    wnd:SetTextST(msg)
  end
]]

 
  local hudotf=actor:get_current_outfit()
  local hudotf_id="nil"
  if hudotf then
    hudotf_id=hudotf:section()
  end
  
  local tex
  if hudotf_id then
    tex = hud_tbl[hudotf_id]
  end

  curr_tex=tex

  local act_it=db.actor:active_item()
  
  if db.actor.health>0 and not (act_it and act_it:section()=="wpn_binoc" and device().fov<50) and device().fov>30 and amk.load_variable("option_hud",2)~=0 then
    set_hud_tex(tex,true)
  else
    set_hud_tex(tex,false)
  end    
end

-- БТР на блокпосту
function spawn_military_btr()
  
  --spawn_military_tech_pack( alife():create("vehicle_btr",vector():set(-168.11, -29.71, -303.21),81808,38 ) ,"vehicle_btr")
  --spawn_military_tech_pack( alife():create("vehicle_btr",vector():set(-237.85, -26.23, -378.81),17147,14 ) ,"vehicle_btr")
  spawn_military_tech_pack( alife():create("helicopter",vector():set(53.97,40.0,-88.02),90641,1462 ) ,"helicopter")
--129145 gvid 11 x -121.17 y 4.06 z -873.15
end

function spawn_military_tech_pack(obj, spawn_item)  
  
  local packet = net_packet()
  obj:STATE_Write(packet)

  -- свойства cse_alife_object
  local game_vertex_id = packet:r_u16()
  local cse_alife_object__unk1_f32 = packet:r_float()
  local cse_alife_object__unk2_s32 = packet:r_s32()
  local level_vertex_id = packet:r_s32()
  local object_flags = packet:r_s32()
  local custom_data = packet:r_stringZ()
  local story_id = packet:r_s32()
  local cse_alife_object__unk3_s32 = packet:r_s32()

  -- свойства cse_visual
  local model_visual = packet:r_stringZ()
  local cse_visual__unk1_u8 = packet:r_u8()
  
  local motion_name
  
  if spawn_item == "helicopter" then
    -- свойства cse_motion
    motion_name = packet:r_stringZ()
  end
  
  -- свойства cse_ph_skeleton
  local skeleton_name = packet:r_stringZ()
  local cse_ph_skeleton__unk1_u8 = packet:r_u8()
  local cse_ph_skeleton__unk2_u16 = packet:r_u16()
  
  local health 
  local cse_alife_helicopter__unk1_sz 
  local engine_sound 
    
  if spawn_item == "helicopter" then
    -- свойства cse_alife_helicopter
    cse_alife_helicopter__unk1_sz = packet:r_stringZ()
    engine_sound = packet:r_stringZ()
  elseif  spawn_item == "vehicle_btr" then
    health = packet:r_float()
  end
  
  -- теперь заполняем нужные параметры
  -- свойства cse_alife_object
  packet:w_begin(game_vertex_id)
  packet:w_float(cse_alife_object__unk1_f32)
  packet:w_s32(cse_alife_object__unk2_s32)
  packet:w_s32(level_vertex_id)
  object_flags = bit_not(5) -- ~5 = 0xfffffffa
  packet:w_s32(object_flags)
  if spawn_item == "helicopter" then
  packet:w_stringZ([[[logic]
cfg = scripts\yantar_heli.ltx

]])
  elseif  spawn_item == "vehicle_btr" then
  packet:w_stringZ([[[logic]
cfg = scripts\esc\esc_blockpost_btr.ltx

]])
  end
  packet:w_s32(-1)
  packet:w_s32(cse_alife_object__unk3_s32)

  -- свойства cse_visual
  packet:w_stringZ(model_visual)
  packet:w_u8(cse_visual__unk1_u8)
  
  if spawn_item == "helicopter" then
    packet:w_stringZ(motion_name)   
  end 
  
  -- свойства cse_ph_skeleton
  skeleton_name = "idle"
  packet:w_stringZ(skeleton_name)
  packet:w_u8(cse_ph_skeleton__unk1_u8)
  packet:w_u16(cse_ph_skeleton__unk2_u16)

  if spawn_item == "helicopter" then
    -- свойства cse_alife_helicopter
    cse_alife_helicopter__unk1_sz = "idle"
    engine_sound = "alexmx\\helicopter"
    packet:w_stringZ(cse_alife_helicopter__unk1_sz)
    packet:w_stringZ(engine_sound)
  elseif  spawn_item == "vehicle_btr" then
    health = 1
    packet:w_float(health)
  end 
  -- считываем скорректированные параметры
  packet:r_seek(0)
  obj:STATE_Read(packet, packet:w_tell())

end

function spawn_military_tech_pack_to_yantar(obj, spawn_item)  
  
  local packet = net_packet()
  obj:STATE_Write(packet)

  -- свойства cse_alife_object
  local game_vertex_id = packet:r_u16()
  local cse_alife_object__unk1_f32 = packet:r_float()
  local cse_alife_object__unk2_s32 = packet:r_s32()
  local level_vertex_id = packet:r_s32()
  local object_flags = packet:r_s32()
  local custom_data = packet:r_stringZ()
  local story_id = packet:r_s32()
  local cse_alife_object__unk3_s32 = packet:r_s32()

  -- свойства cse_visual
  local model_visual = packet:r_stringZ()
  local cse_visual__unk1_u8 = packet:r_u8()
  
  local motion_name
  
  if spawn_item == "helicopter" then
    -- свойства cse_motion
    motion_name = packet:r_stringZ()
  end
  
  -- свойства cse_ph_skeleton
  local skeleton_name = packet:r_stringZ()
  local cse_ph_skeleton__unk1_u8 = packet:r_u8()
  local cse_ph_skeleton__unk2_u16 = packet:r_u16()
  
  local health 
  local cse_alife_helicopter__unk1_sz 
  local engine_sound 
    
  if spawn_item == "helicopter" then
    -- свойства cse_alife_helicopter
    cse_alife_helicopter__unk1_sz = packet:r_stringZ()
    engine_sound = packet:r_stringZ()
  elseif  spawn_item == "vehicle_btr" then
    health = packet:r_float()
  end
  
  -- теперь заполняем нужные параметры
  -- свойства cse_alife_object
  packet:w_begin(game_vertex_id)
  packet:w_float(cse_alife_object__unk1_f32)
  packet:w_s32(cse_alife_object__unk2_s32)
  packet:w_s32(level_vertex_id)
  object_flags = bit_not(5) -- ~5 = 0xfffffffa
  packet:w_s32(object_flags)
  if spawn_item == "helicopter" then
  packet:w_stringZ([[[logic]
cfg = scripts\yantar_heli.ltx

]])
  elseif  spawn_item == "vehicle_btr" then
  packet:w_stringZ([[[logic]
cfg = scripts\esc\esc_blockpost_btr.ltx

]])
  end
  packet:w_s32(-1)
  packet:w_s32(cse_alife_object__unk3_s32)

  -- свойства cse_visual
  packet:w_stringZ(model_visual)
  packet:w_u8(cse_visual__unk1_u8)
  
  if spawn_item == "helicopter" then
    packet:w_stringZ(motion_name)   
  end 
  
  -- свойства cse_ph_skeleton
  skeleton_name = "idle"
  packet:w_stringZ(skeleton_name)
  packet:w_u8(cse_ph_skeleton__unk1_u8)
  packet:w_u16(cse_ph_skeleton__unk2_u16)

  if spawn_item == "helicopter" then
    -- свойства cse_alife_helicopter
    cse_alife_helicopter__unk1_sz = "idle"
    engine_sound = "alexmx\\helicopter"
    packet:w_stringZ(cse_alife_helicopter__unk1_sz)
    packet:w_stringZ(engine_sound)
  elseif  spawn_item == "vehicle_btr" then
    health = 1
    packet:w_float(health)
  end 
  -- считываем скорректированные параметры
  packet:r_seek(0)
  obj:STATE_Read(packet, packet:w_tell())

end


function spawn_military_tech_pack_to_radar(obj, spawn_item)  
  
  local packet = net_packet()
  obj:STATE_Write(packet)

  -- свойства cse_alife_object
  local game_vertex_id = packet:r_u16()
  local cse_alife_object__unk1_f32 = packet:r_float()
  local cse_alife_object__unk2_s32 = packet:r_s32()
  local level_vertex_id = packet:r_s32()
  local object_flags = packet:r_s32()
  local custom_data = packet:r_stringZ()
  local story_id = packet:r_s32()
  local cse_alife_object__unk3_s32 = packet:r_s32()

  -- свойства cse_visual
  local model_visual = packet:r_stringZ()
  local cse_visual__unk1_u8 = packet:r_u8()
  
  local motion_name
  
  if spawn_item == "helicopter" then
    -- свойства cse_motion
    motion_name = packet:r_stringZ()
  end
  
  -- свойства cse_ph_skeleton
  local skeleton_name = packet:r_stringZ()
  local cse_ph_skeleton__unk1_u8 = packet:r_u8()
  local cse_ph_skeleton__unk2_u16 = packet:r_u16()
  
  local health 
  local cse_alife_helicopter__unk1_sz 
  local engine_sound 
    
  if spawn_item == "helicopter" then
    -- свойства cse_alife_helicopter
    cse_alife_helicopter__unk1_sz = packet:r_stringZ()
    engine_sound = packet:r_stringZ()
  elseif  spawn_item == "vehicle_btr" then
    health = packet:r_float()
  end
  
  -- теперь заполняем нужные параметры
  -- свойства cse_alife_object
  packet:w_begin(game_vertex_id)
  packet:w_float(cse_alife_object__unk1_f32)
  packet:w_s32(cse_alife_object__unk2_s32)
  packet:w_s32(level_vertex_id)
  object_flags = bit_not(5) -- ~5 = 0xfffffffa
  packet:w_s32(object_flags)
  if spawn_item == "helicopter" then
  packet:w_stringZ([[[logic]
cfg = scripts\radar_heli.ltx

]])
  elseif  spawn_item == "vehicle_btr" then
  packet:w_stringZ([[[logic]
cfg = scripts\radar_btr.ltx

]])
  end
  packet:w_s32(-1)
  packet:w_s32(cse_alife_object__unk3_s32)

  -- свойства cse_visual
  packet:w_stringZ(model_visual)
  packet:w_u8(cse_visual__unk1_u8)
  
  if spawn_item == "helicopter" then
    packet:w_stringZ(motion_name)   
  end 
  
  -- свойства cse_ph_skeleton
  skeleton_name = "idle"
  packet:w_stringZ(skeleton_name)
  packet:w_u8(cse_ph_skeleton__unk1_u8)
  packet:w_u16(cse_ph_skeleton__unk2_u16)

  if spawn_item == "helicopter" then
    -- свойства cse_alife_helicopter
    cse_alife_helicopter__unk1_sz = "idle"
    engine_sound = "alexmx\\helicopter"
    packet:w_stringZ(cse_alife_helicopter__unk1_sz)
    packet:w_stringZ(engine_sound)
  elseif  spawn_item == "vehicle_btr" then
    health = 1
    packet:w_float(health)
  end 
  -- считываем скорректированные параметры
  packet:r_seek(0)
  obj:STATE_Read(packet, packet:w_tell())

end


function spawn_military_tech_pack_to_pripyat(obj, spawn_item)  
  
  local packet = net_packet()
  obj:STATE_Write(packet)

  -- свойства cse_alife_object
  local game_vertex_id = packet:r_u16()
  local cse_alife_object__unk1_f32 = packet:r_float()
  local cse_alife_object__unk2_s32 = packet:r_s32()
  local level_vertex_id = packet:r_s32()
  local object_flags = packet:r_s32()
  local custom_data = packet:r_stringZ()
  local story_id = packet:r_s32()
  local cse_alife_object__unk3_s32 = packet:r_s32()

  -- свойства cse_visual
  local model_visual = packet:r_stringZ()
  local cse_visual__unk1_u8 = packet:r_u8()
  
  local motion_name
  
  if spawn_item == "helicopter" then
    -- свойства cse_motion
    motion_name = packet:r_stringZ()
  end
  
  -- свойства cse_ph_skeleton
  local skeleton_name = packet:r_stringZ()
  local cse_ph_skeleton__unk1_u8 = packet:r_u8()
  local cse_ph_skeleton__unk2_u16 = packet:r_u16()
  
  local health 
  local cse_alife_helicopter__unk1_sz 
  local engine_sound 
    
  if spawn_item == "helicopter" then
    -- свойства cse_alife_helicopter
    cse_alife_helicopter__unk1_sz = packet:r_stringZ()
    engine_sound = packet:r_stringZ()
  elseif  spawn_item == "vehicle_btr" then
    health = packet:r_float()
  end
  
  -- теперь заполняем нужные параметры
  -- свойства cse_alife_object
  packet:w_begin(game_vertex_id)
  packet:w_float(cse_alife_object__unk1_f32)
  packet:w_s32(cse_alife_object__unk2_s32)
  packet:w_s32(level_vertex_id)
  object_flags = bit_not(5) -- ~5 = 0xfffffffa
  packet:w_s32(object_flags)
  if spawn_item == "helicopter" then
  packet:w_stringZ([[[logic]
cfg = scripts\pripyat_heli.ltx

]])
  elseif  spawn_item == "vehicle_btr" then
  packet:w_stringZ([[[logic]
cfg = scripts\radar_btr.ltx

]])
  end
  packet:w_s32(-1)
  packet:w_s32(cse_alife_object__unk3_s32)

  -- свойства cse_visual
  packet:w_stringZ(model_visual)
  packet:w_u8(cse_visual__unk1_u8)
  
  if spawn_item == "helicopter" then
    packet:w_stringZ(motion_name)   
  end 
  
  -- свойства cse_ph_skeleton
  skeleton_name = "idle"
  packet:w_stringZ(skeleton_name)
  packet:w_u8(cse_ph_skeleton__unk1_u8)
  packet:w_u16(cse_ph_skeleton__unk2_u16)

  if spawn_item == "helicopter" then
    -- свойства cse_alife_helicopter
    cse_alife_helicopter__unk1_sz = "idle"
    engine_sound = "alexmx\\helicopter"
    packet:w_stringZ(cse_alife_helicopter__unk1_sz)
    packet:w_stringZ(engine_sound)
  elseif  spawn_item == "vehicle_btr" then
    health = 1
    packet:w_float(health)
  end 
  -- считываем скорректированные параметры
  packet:r_seek(0)
  obj:STATE_Read(packet, packet:w_tell())

end

dynweathers={
  clear={
    clear=0.6,
    pasmurno=0.2,
    rain=0.15,
    groza=0.05
  },
  pasmurno={
    clear=0.2,
    pasmurno=0.4,
    rain=0.2,
    groza=0.2
  },
  rain={
    clear=0.2,
    pasmurno=0.3,
    rain=0.3,
    groza=0.2
  },
  groza={
    clear=0.4,
    pasmurno=0.4,
    rain=0.1,
    groza=0.1
  }
}

function weather_manager()
  if sync_done==false then return end
  local hour = level.get_time_hours()
  local dynlasthour = amk.load_variable("dynlasthour",-1)
  if dynlasthour<hour or (hour==0 and dynlasthour==23) then
    local next_weather, rnd = nil, math.random()
    local dynw = amk.load_variable("dynw","clear")
    for k,v in pairs(dynweathers[dynw]) do
      if rnd<=v then 
        next_weather=k 
        break
      end
      rnd=rnd-v
    end
    local bt=blowout_type()
    if (amk.load_variable("blowout",0)==0 and bt<1) or level.name()==0 then 
      level.set_weather("amk_"..dynw.."_"..next_weather.."_"..hour)
    end
    amk.mylog(hour.." "..dynw.."->"..next_weather)
    
    amk.save_variable("dynw",next_weather)
    amk.save_variable("dynlasthour",hour)
    amk.save_variable("dynweather","amk_"..dynw.."_"..next_weather.."_"..hour)
  end
end

local last_armor_id=0
local last_condition=0

function repair_armor(val)
  local armor = db.actor:item_in_slot(6)
  if armor then
    last_armor_id=armor:id()
    last_condition=val
    armor:set_condition(val)
  end
end

local exo_list={
"neytral_exo_gaz_outfit_m1",
"svoboda_yellow_exo_outfit_m1",
"exo_outfit",
"outfit_exo_m1"
}

function check_armor()
  local armor = db.actor:item_in_slot(6)
  
  local weapon = db.actor:item_in_slot(2)
  
  if db.actor:active_slot()==2 and weapon then
    local tmp
    if armor then 
      tmp = armor:section()
    else
      tmp = "none"
    end

    local tmp1=false
    
    for k,v in pairs(exo_list) do
      if tmp==v then
        tmp1=true
        break
      end
    end
    
    if tmp1==false and weapon:section()=="wpn_pkm" then 
      db.actor:drop_item(weapon)
      local tmpw = amk.spawn_item_in_inv("wpn_pkm")
      db.actor:transfer_item(weapon,db.actor)
      alife():release(tmpw)
    end
    
    if tmp1==false and weapon:section()=="wpn_wulcano" then 
      db.actor:drop_item(weapon)
      local tmpa = amk.spawn_item_in_inv("wpn_wulcano")
      db.actor:transfer_item(weapon,db.actor)
      alife():release(tmpa)
    end
    
    if tmp1==false and weapon:section()=="wpn_mg3" then 
      db.actor:drop_item(weapon)
      local tmpb = amk.spawn_item_in_inv("wpn_mg3")
      db.actor:transfer_item(weapon,db.actor)
      alife():release(tmpb)
    end
    
  end
  
  if not armor then
    last_armor_id=0
    return
  end
  if armor:id()~=last_armor_id then
    last_armor_id=armor:id()
    last_condition=armor:condition()
    return
  end
  
  --reenabled the suit repair in anomalies
  --if last_condition<armor:condition() then
  -- Хехе
    --armor:set_condition(last_condition)
  --else
    --last_condition=armor:condition()
  --end
  
  last_condition=armor:condition()
  
end

function dezombify(id,v)
  local npc=level.object_by_id(id)
  if npc then
    if npc:alive()==false then
      return true
    end
    local sobj=alife():object(id)
    if sobj then
      npc:set_character_community(v.comm,0,0)
      npc:set_goodwill(v.gw,db.actor)
      local orgtbl=amk.read_stalker_params(sobj)
      orgtbl.profile=string.gsub(orgtbl.profile,"(.*)_zombi","%1")
      -- amk.mylog("dezombified "..npc:name().." profile "..orgtbl.profile)
      amk.write_stalker_params(orgtbl,sobj)
      return true
    end    
  end
  return false
end

btrs_friends={}

function build_btrs_table(obj)
  if obj and (IsStalker(obj) and obj:character_community()=="military" and obj:alive()) or 
   obj:section()=="vehicle_btr" then
      btrs_friends[obj:id()] = obj:section()
  end
end

--[[
copyright © xStream & Red75

MMMMMMM    MMMMM    MMMMMM  MMMMMMMMMMMMM  MMMMMMMMMMM     MMMMMMMMMMM    MMMMMM    MMMMMMM     MMMMMM
 MMMMMMM MMMMMM  MMMMMMMMMM MMMMMMMMMMMMMM MMMMMMMMMMMMMM  MMMMMMMMMMM   MMMMMMMM   MMMMMMMM   MMMMMMM
  MMMMMMMMMMMM  MMMMMMMMMMMM MMMMMMMMMMMMM  MMMMMMMMMMMMMM MMMMMMMMMMM   MMMMMMMM    MMMMMMMM MMMMMMMM
   MMMMMMMMM     MMMMMMMM MMM   MMMMMMM     MMMMMMMMMMMMMM MMMMMMM      MMMMMMMMMM   MMMMMMMMMMMMMMMMM
    MMMMMMM       MMMMMMM       MMMMMM      MMMMMMM  MMMMM MMMMMMM      MMMMMMMMMM   MMMMMMMMMMMMMMMMM
    MMMMMMMM       MMMMMMM      MMMMMM      MMMMMMM MMMM   MMMMMMMMMM  MMMM MMMMMMM  MMMMMMMMMMMMMMMMM
   MMMMMMMMMM        MMMMM      MMMMMM      MMMMMMMMMMM    MMMMMMM     MMMM  MMMMMM  MMMMMMMMMMMMMMMMM
  MMMMM  MMMMM    MMMMMMMM      MMMMMM      MMMMMMMMMMMMMM MMMMMMMMMMM MMMMMMMMMMMMM MMMMM MMMMM MMMMM
 MMMMM   MMMMMM    MMMMMM       MMMMMM      MMMMMMM MMMMM  MMMMMMMMMM MMMMMMM MMMMMM MMMM   MM   MMMM 
 
]]
