---------------- Схема поведения НПС при выбросе для OGSM 2.x -----------------

------------------------- Copyright 2007-2008 DEXXX ---------------------------

restrictor_list = {}

local npc_psy = {}
local npc_run = {}
local npc_sit = {}
local npc_go = {}


-- We check whether NPC is in specific rectangle zone (are set for special places, 
-- such as bar, where NPCs should not react to blowout at all)
function npc_in_zone(obj,lev,pos_psy_x,pos_psy_y,size_psy_x,size_psy_y,z1,z2)
local level_name = level.name()
if level_name ~= lev then
	return false
end
local pos = obj:position()
local x1 = pos_psy_x
local x2 = pos_psy_y
local y1 = size_psy_x
local y2 = size_psy_y
if (pos.x >= x1 and pos.x <= x2) and (pos.z >= y1 and pos.z <= y2) and (pos.y >= z1) and (pos.y <= z2) then
	return true
end
return false
end

-- We get the nearest to NPC hide
function get_nearest_hide(npc)
local hideout_list = {}
for k,v in pairs(restrictor_list) do
	local community, radius = get_community_hide(v)
	local obj = level.object_by_id(v.id)
	if obj and string.find(community, npc:character_community()) then
		local npc_pos = npc:position()
		local hide_pos = obj:position()
		local distance = npc_pos:distance_to(hide_pos)
		local hide = {obj = obj, rad = radius, dist = distance}
		table.insert(hideout_list, hide)
	end
end
table.sort(hideout_list, function(a,b) return a.dist < b.dist end)
local nearest_hide = hideout_list[1]
return nearest_hide
end


-- We get the hideout data (communities, radius)
function get_community_hide(obj)
if not obj then return "none" end
local packet = net_packet()
obj:STATE_Write(packet)
local game_vertex_id = packet:r_u16()
local cse_alife_object__unk1_f32 = packet:r_float()
local cse_alife_object__unk2_u32 = packet:r_s32()
local level_vertex_id = packet:r_s32()
local object_flags = packet:r_s32()
local custom_data = packet:r_stringZ()
local cd = parse_custom_data(custom_data)
if not cd.parameters then return "none" end
if not cd.parameters.community then return "none" end
if not cd.parameters.radius then return "none" end
return cd.parameters.community, cd.parameters.radius
end

function is_in_specific_zone(npc)
	if npc_in_zone(npc,"l01_escape",-256,-246,-137,-132,-25,-20) or		-- Бункер Сидора
	   npc_in_zone(npc,"l03_agroprom",-198.31,-195.66,85.4,96.54,2,5.3) or	-- Вагон дезертира
	   npc_in_zone(npc,"l04_darkvalley",30,50,-60,-25,-4,0) or			-- Тюрьма в Темной долине
	   npc_in_zone(npc,"l05_bar",123,140,18,30,-6,0) or 				-- Бар
	   npc_in_zone(npc,"l05_bar",118,121,34,41,-4,1) or 				-- Охранник Бара
	   npc_in_zone(npc,"l05_bar",149,158,67,74,0,4) or 				-- Приемная Арни
	   npc_in_zone(npc,"l05_bar",136,168,73,134,-22,-10) or			-- Арена
	   npc_in_zone(npc,"l05_bar",208,234,120,139,-6,2) or				-- База Долга
	   npc_in_zone(npc,"l06_rostok",-90,-87,125,151,0,6) or 			-- Блокпост наемников
	   npc_in_zone(npc,"l07_military",-27,-16,-33,-14,-9,0) or 			-- База Свободы
	   npc_in_zone(npc,"l08_yantar",23,40,-282,-269,-16,0) or			-- Бункер ученых
	   npc_in_zone(npc,"l08_yantar",-265,-22,-218,-6,-20,-11) or			-- Кишка
	   npc_in_zone(npc,"marsh",518,512,-158,-147,-0.5,1.5) or           -- Подвал 1 
	   npc_in_zone(npc,"pripyat",149,130,-208,-147,5,-3) or           -- Магазин
	   npc_in_zone(npc,"red_forest",-200,-163,-316,-313,1,0) or
	   npc_in_zone(npc,"aver",-356,-339,16,-24,-9,-5) or
       npc_in_zone(npc,"predbannik",-37,-39,335,331,-0.5,-2) or   -- Погреб у лагеря новичков
       npc_in_zone(npc,"predbannik",17,16,35,27,9,8) or       -- Погреб в потеряной деревне
	   npc_in_zone(npc,"l11_pripyat",-16,-2,188,204,-5,0) then      -- Подвал ДК
		return true
	end
	return false
end

function is_in_hide(npc)
	if npc_in_zone(npc,"l01_escape",-211,-198,-132,-125,-23,-20) or 	-- Подвал 1 в лагере новичков
	   npc_in_zone(npc,"l01_escape",-216,-209,-133,-120,-24,-20) or 	-- Подвал 2 в лагере новичков
	   npc_in_zone(npc,"l02_garbage",-112,-57,-6,25,-9,90) or		-- Ангар на Свалке
	   npc_in_zone(npc,"l05_bar",206,213,50,68,0,4) or			-- Магазин Петренко
	   npc_in_zone(npc,"l06_rostok",-285,-235,78,112,-7,0) or 		-- Тоннель Фримена
	   npc_in_zone(npc,"l10_radar",639,664,167,189,-45,-37) or 		-- Секретная база Монолита
	   npc_in_zone(npc,"l10_radar",86.73,153,-27,-21.70,-10,-4.6) or 	-- Вход в бункер
	   npc_in_zone(npc,"l11_pripyat",-18,-12,58,83,-6,-3) or		-- Подземный переход
	   npc_in_zone(npc,"l11_pripyat",167,173,58,83,-6,-3) or		-- Подземный переход
	   npc_in_zone(npc,"l11_pripyat",-24,55,116,122,-4,0) or 		-- Канализация
	   npc_in_zone(npc,"marsh",49,43,-240,-235,-0.5,2.5) or           -- Укрытие проводника 
	   npc_in_zone(npc,"marsh",-182,-179,-285,-276,2,0.5) or           -- Комната Лебедева
       npc_in_zone(npc,"zaton",128,124,189,176,-7.5,-7) or 
	   npc_in_zone(npc,"zaton",136,105,184,182,-7.5,-1) or
       npc_in_zone(npc,"jupiter",-52,-48,212,176,10,0) or            --янов
       npc_in_zone(npc,"jupiter",-230,-210,200,198,7,-4) or            --бункер
       npc_in_zone(npc,"jupiter",-60,-49,78,73,6,-6) or	        --подвал янов
       npc_in_zone(npc,"predbannik",-449,-452,-149,-156,0,-6) or	        --подвал cталкерского городка1
       npc_in_zone(npc,"predbannik",-435,-445,-140,-143,0,-6) or	        --подвал cталкерского городка2
       npc_in_zone(npc,"predbannik",-436,-445,-149,-151,0,-6) or	        --подвал cталкерского городка3
       npc_in_zone(npc,"predbannik",-225,-237,239,237,17,15) or             --пещера
       npc_in_zone(npc,"predbannik",-22,-23,334,327,-1,-4) or                  --подвал торговца
	   npc_in_zone(npc,"red_forest",27,25,26,24,4,0) or                     --лесник
       npc_in_zone(npc,"predbannik",-436,-445,-149,-151,0,-6) or
	   npc_in_zone(npc,"marsh",-125.0,-124.9,-276,-273,0,-2) then           -- Подвал на базе ЧН 
	   	return true
	else
		local hide = get_nearest_hide(npc)
		if hide then
			if utils.npc_in_zone(npc, hide.obj) then
				return true
			end
		end
	end
	return false
end


-- Spawn function. Better watch out :)
function spawn_restrictor(p_level, p_vector, p_lv, p_gv, p_type, p_radius)

local p_name

p_name = p_level.."_hide_restrictor"

local obj = alife():create(p_name, p_vector, p_lv, p_gv)

local hide_name
if string.find(p_type, "_") then hide_name = "ogsm_common_hide"
else hide_name = "ogsm_"..p_type.."_hide" end

local mapspot
if p_radius < 4 then mapspot = "ogsm_hide_spot_min"
elseif p_radius < 11 then mapspot = "ogsm_hide_spot_mdl"
else mapspot = "ogsm_hide_spot_max" end

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_u32 = 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_u32 = packet:r_s32()

-- свойства cse_shape
local shape_count = packet:r_u8()
local shape_type
local center
local radius

if shape_count == 0 then
	shape_type = 0
	center = vector():set(0,0,0)
	radius = 10.0
	shape_count = 1
else
for i=1,shape_count do
	local shape_type = packet:r_u8()
	if shape_type == 0 then
		local center = packet:r_vec3()
		local radius = packet:r_float()
	else
		local v1 = packet:r_vec3()
		local v2 = packet:r_vec3()
		local v3 = packet:r_vec3()
		local v4 = packet:r_vec3()
	end
end
end

-- свойства cse_alife_space_restrictor
local restrictor_type = packet:r_u8()

-- свойства cse_alife_object
packet:w_u16(game_vertex_id)
packet:w_float(cse_alife_object__unk1_f32)
packet:w_s32(cse_alife_object__unk2_u32)
packet:w_s32(level_vertex_id)
packet:w_s32(object_flags)

custom_data = "[logic] \nactive = sr_mapspot \n[sr_mapspot] \nhint = "..hide_name.." \nlocation = "..mapspot

local cd = parse_custom_data(custom_data)
if not cd then cd={} end
if not cd.parameters then cd.parameters = {} end
cd.parameters.community = p_type
cd.parameters.radius = p_radius
custom_data = gen_custom_data(cd)

packet:w_stringZ(custom_data)
packet:w_s32(story_id)
packet:w_s32(cse_alife_object__unk3_u32)

-- свойства cse_shape
packet:w_u8(shape_count)

for i=1,shape_count do
	packet:w_u8(shape_type)
	if shape_type == 0 then
		packet:w_vec3(center)
		packet:w_float(p_radius)
	else
		packet:w_vec3(v1)
		packet:w_vec3(v2)
		packet:w_vec3(v3)
		packet:w_vec3(v4)
	end
end

-- свойства cse_alife_space_restrictor
packet:w_u8(restrictor_type)

obj:STATE_Read(packet, packet:w_tell()-packet:r_tell())

return obj
end


-- Don't touch this too.
function parse_custom_data(str)
 local t={}
 if str then
  for section, section_data in string.gfind(str,"%s*%[([^%]]*)%]%s*([^%[%z]*)%s*") do
   section = trim(section)
   t[section]={}
   for line in string.gfind(trim(section_data), "([^\n]*)\n*") do
    if string.find(line,"=")~=nil then
     for k, v in string.gfind(line, "([^=]-)%s*=%s*(.*)") do
      k = trim(k)
      if k~=nil and k~='' and v~=nil then 
       t[section][k]=trim(v) 
      end
     end
    else
     for k, v in string.gfind(line, "(.*)") do
      k = trim(k)
      if k~=nil and k~='' then
       t[section][k]="<<no_value>>" 
      end
     end
    end
   end
  end
 end
 return t
end
function gen_custom_data(tbl)
 local str=''
 for key, value in pairs(tbl) do
  str = str.."["..key.."]\n"
  for k, v in pairs(value) do
   if v~="<<no_value>>" then
    str=str..k.." = "..v.."\n"
   else
    str=str..k.."\n"
   end
  end
 end
 return str
end
function trim (s)
 return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
end


-- Getting the list of hideouts on game load
function get_level_hides()
for i=1,65535 do
	local obj = alife():object(i)
	if obj then
		if string.find(obj:name(), level.name().."_hide_restrictor") then
			table.insert(restrictor_list, obj)
		end
	end
end
end


-- First run spawn. If parameter p_type is "fake_" - it means that the hide is fake - it is only for actor
-- NPCs ignore such fake hides
function first_run()

------------------
-- Escape Hides --
------------------

-- Бункер Сидора
spawn_restrictor("l01_escape", vector():set(-249.9,-24.8,-134.4), 10136, 7, "fake_", 3)
-- Лагерь новичков
spawn_restrictor("l01_escape", vector():set(-206.5,-22.53,-128.1), 46582, 58, "stalker", 3)
spawn_restrictor("l01_escape", vector():set(-211.4,-23.17,-129.5), 41963, 59, "stalker", 3)
-- Ферма
spawn_restrictor("l01_escape", vector():set(-8.4,0.64,258.3), 268724, 183, "stalker_actor", 4)
-- Заброшенный дом рядом с Лисом
spawn_restrictor("l01_escape", vector():set(133.0,0.22,339.5), 437657, 232, "stalker_bandit_actor", 3)
-- АТП
spawn_restrictor("l01_escape", vector():set(109.9,-7.48,7.7), 411371, 118, "stalker_bandit", 5)
-- Блокпост
spawn_restrictor("l01_escape", vector():set(-119.7,-30.04,-386.4), 133789, 27, "military", 4)
spawn_restrictor("l01_escape", vector():set(-140.7,-29.68,-361.2), 109717, 18, "military", 4)
-- Мельница
spawn_restrictor("l01_escape", vector():set(13.3,2.7,98.7), 290783, 92, "military", 3)

-------------------
-- Garbage Hides --
-------------------

-- Ангар - восточная часть
spawn_restrictor("l02_garbage", vector():set(-61.6,0.75,9.1), 135489, 330, "stalker_actor", 6)
-- Изгородь
spawn_restrictor("l02_garbage", vector():set(66.5,0.6,144.9), 237602, 364, "bandit", 2)
-- Ангар - западная часть
spawn_restrictor("l02_garbage", vector():set(-102.9,0.44,11.9), 110339, 328, "bandit", 6)
-- блокпост Долга
spawn_restrictor("l02_garbage", vector():set(43.2,0.91,239.5), 217298, 359, "dolg_actor", 3)

--------------------
-- Agroprom Hides --
--------------------

-- Строение в районе Крота
spawn_restrictor("l03_agroprom", vector():set(16.8,0,14), 256750, 693, "stalker_bandit_military", 6)
-- Убежище Крота
spawn_restrictor("l03_agroprom", vector():set(39.9,2.5,-10.5), 278041, 693, "stalker", 6)
-- Туннель
spawn_restrictor("l03_agroprom", vector():set(251.3,0.3,71.4), 427597, 498, "stalker_actor", 15)
-- Казармы у подземелья
spawn_restrictor("l03_agroprom", vector():set(-158.2,1.1,-151.2), 79088, 693, "military", 5)
-- Казармы у НИИ
spawn_restrictor("l03_agroprom", vector():set(-153.3,1.1,-166.6), 84356, 693, "military", 5)
-- Строение у НИИ
spawn_restrictor("l03_agroprom", vector():set(-179.9,0,-187.6), 56704, 693, "military", 6)
-- НИИ Агропром
spawn_restrictor("l03_agroprom", vector():set(-145.6,0,-200.9), 92513, 693, "military", 6)

----------------------
-- Darkvalley Hides --
----------------------

-- Строения над входом в Х18
spawn_restrictor("l04_darkvalley", vector():set(146.3,0.22,-263.2), 345170, 813, "bandit_military_dolg", 15)
spawn_restrictor("l04_darkvalley", vector():set(128.1,0.14,-294), 325149, 813, "bandit_military_dolg", 7)
-- Ферма
spawn_restrictor("l04_darkvalley", vector():set(-135.1,0.97,-511), 22718, 813, "stalker", 8)
-- Бензоколонка
spawn_restrictor("l04_darkvalley", vector():set(101.5,0.7,-7.7), 297166, 813, "stalker_", 6)
-- Подвал рядом с ямой
spawn_restrictor("l04_darkvalley", vector():set(34.3,-2.93,-0.7), 216848, 813, "bandit_dolg", 4)
-- Элеватор
spawn_restrictor("l04_darkvalley", vector():set(-7,0.11,-57.4), 165893, 813, "bandit", 4)
-- Комната Борова
spawn_restrictor("l04_darkvalley", vector():set(39.2,4.54,-84), 223238, 813, "bandit", 5)
-- Холл
spawn_restrictor("l04_darkvalley", vector():set(37.1,0.375,-66.5), 220567, 813, "bandit", 5)
-- Над тюрьмой
spawn_restrictor("l04_darkvalley", vector():set(40.6,1.01,-31.5), 225406, 813, "bandit", 7)

---------------
-- Bar Hides --
---------------

-- Бункер на заставе
spawn_restrictor("l05_bar", vector():set(214.2,-1.57,-18.9), 52736, 1233, "stalker_dolg_actor", 3)
-- Иди своей дорогой, сталкер...
spawn_restrictor("l05_bar", vector():set(178.5,0.09,49.0), 44766, 1233, "stalker_dolg_actor", 6)
-- Магазин Петренко
spawn_restrictor("l05_bar", vector():set(210.0,0.42,61.6), 51205, 1233, "dolg", 5)
-- База Долга
spawn_restrictor("l05_bar", vector():set(224.0,-5.26,130.9), 56342, 1233, "fake_", 8)
-- Предбанник Арены
spawn_restrictor("l05_bar", vector():set(142.8,0.09,93.1), 38428, 1233, "stalker_dolg", 6)
-- Строения у Бара
spawn_restrictor("l05_bar", vector():set(112.7,0.097,68.6), 33337, 1233, "stalker_dolg", 6)
spawn_restrictor("l05_bar", vector():set(144.9,-0.012,40.6), 38939, 1233, "stalker_dolg", 6)
-- Бар
spawn_restrictor("l05_bar", vector():set(131.6,-4.83,23.8), 36240, 1233, "fake__actor", 10)

------------------
-- Rostok Hides --
------------------

-- Блокпост наемников
spawn_restrictor("l06_rostok", vector():set(-88.9,0,137.2), 46715, 1311, "stalker_dolg_killer_ecolog", 2)
spawn_restrictor("l06_rostok", vector():set(-82.6,1.95,156.8), 48150, 1311, "stalker_dolg_killer_ecolog", 3)
-- Ангар с пси-зоной
spawn_restrictor("l06_rostok", vector():set(-191.8,0,130.9), 28448, 1311, "stalker_dolg_killer_ecolog", 6)
-- Тоннель Фримена
spawn_restrictor("l06_rostok", vector():set(-280,-6,109.2), 1769, 1311, "ecolog", 3)
-- Засады наемников
spawn_restrictor("l06_rostok", vector():set(-242.9,0,140), 12634, 1311, "killer", 2)
spawn_restrictor("l06_rostok", vector():set(-228.2,0.13,141.4), 19047, 1311, "killer", 2)
spawn_restrictor("l06_rostok", vector():set(-250.6,1.43,14), 9490, 1311, "killer", 2)

--------------------
-- Military Hides --
--------------------

-- База Долга
spawn_restrictor("l07_military", vector():set(-191.8,-13,39.9), 133097, 1546, "dolg_actor", 4)
-- Деревня кровососов
spawn_restrictor("l07_military", vector():set(-228.9,-21.78,196.7), 102257, 1546, "freedom", 4)
-- Стоянка нейтралов
spawn_restrictor("l07_military", vector():set(-71.4,-16.83,242.9), 240966, 1546, "stalker", 3)
-- Барьер
spawn_restrictor("l07_military", vector():set(84.7,-7,341.6), 382116, 1546, "freedom", 3)
-- Хутор Павлика
spawn_restrictor("l07_military", vector():set(-190.4,-16,371), 134714, 1546, "killer_freedom", 4)
-- Стоянка наёмников
spawn_restrictor("l07_military", vector():set(-361.5,-13.6,386.7), 4172, 1847, "killer", 2)
-- Штаб Свободы
spawn_restrictor("l07_military", vector():set(-21.7,-4.12,-23.8), 285439, 1546, "fake_", 6)
-- Казармы
spawn_restrictor("l07_military", vector():set(9.1,-7.28,16.1), 316470, 1546, "freedom_actor", 6)
spawn_restrictor("l07_military", vector():set(57.4,-8.054,16.8), 356438, 1546, "freedom_actor", 6)
-- Кухня
spawn_restrictor("l07_military", vector():set(-77,-11.34,30.1), 235657, 1546, "freedom", 6)

------------------
-- Yantar Hides --
------------------

-- Бункер ученых
spawn_restrictor("l08_yantar", vector():set(31.5,-11.68,-272.3), 54643, 1459, "fake_", 6)
-- Контейнер
spawn_restrictor("l08_yantar", vector():set(0.7,-11.74,-263.95), 39537, 1448, "fake_", 2)
-- Мастерские завода
spawn_restrictor("l08_yantar", vector():set(91.15,0.03,-11.87), 87142, 1514, "stalker", 5)

-----------------
-- Radar Hides --
-----------------

-- Секретная база Монолита
spawn_restrictor("l10_radar", vector():set(650.955,-43.887,182.48), 226701, 1868, "dolg_freedom_stalker_military_monolith", 7)
-- Вход в бункер
spawn_restrictor("l10_radar", vector():set(147.35,-9.277,-24.21), 52689, 1868, "dolg_freedom_stalker_military_monolith", 4)
-- Казармы на Радаре
spawn_restrictor("l10_radar", vector():set(33.6,-3.38,-42), 21487, 1868, "dolg_freedom_stalker_military_monolith", 4)

-------------------
-- Pripyat Hides --
-------------------

-- Детский сад
spawn_restrictor("l11_pripyat", vector():set(112.4,-0.9,-26.5), 223254, 2234, "stalker_dolg_freedom", 3)
-- Подземная автостоянка
spawn_restrictor("l11_pripyat", vector():set(85.4,-7.39,20.3), 196856, 2269, "stalker_military_freedom_dolg_killer", 15)
-- Сберкасса
spawn_restrictor("l11_pripyat", vector():set(60.2,-1.74,33.6), 172091, 2269, "stalker_military_freedom_dolg_killer", 4)
-- Милиция
spawn_restrictor("l11_pripyat", vector():set(-35.7,-1.68,34.3), 63044, 2269, "stalker_military_freedom_dolg_killer", 6)
-- Сельхозуправление
spawn_restrictor("l11_pripyat", vector():set(-51.1,0,140), 48522, 2269, "stalker_military_freedom_dolg_killer", 12)
-- Подвал Дома культуры
spawn_restrictor("l11_pripyat", vector():set(-5.6,-5.169,193.2), 95262, 2269, "fake_", 6)
-- Подземные переходы
spawn_restrictor("l11_pripyat", vector():set(-15.4,-5.55,69.3), 83833, 2269, "stalker_military_freedom_dolg_killer", 3)
spawn_restrictor("l11_pripyat", vector():set(170.8,-5.55,69.3), 253270, 2269, "stalker_military_freedom_dolg_killer", 3)
-- Канализация
spawn_restrictor("l11_pripyat", vector():set(19.6,-3.634,121.1), 125990, 2269, "stalker_military_freedom_dolg_killer", 2)
-- Автовокзал
spawn_restrictor("l11_pripyat", vector():set(-101.3,-2.2,-3.0), 15863, 2251, "stalker_dolg", 5)
-- Гастроном
spawn_restrictor("l11_pripyat", vector():set(-115.7,-3.5,103.1), 8324, 2216, "stalker_dolg", 3)
-- Гараж
spawn_restrictor("l11_pripyat", vector():set(-46.3,-2.2,-96.6), 53084, 2258, "stalker_freedom", 3)
-----------------------------------------------------
-- fixed by valdemar093
------------------------------------------------------
-------------------
-- Marsh Hides --
-------------------
-- Южный хутор
spawn_restrictor("marsh", vector():set(498.9,2.8,-152.6), 479140, 3563, "renegade_nebo", 5)
-- церковь
spawn_restrictor("marsh", vector():set(292.8,0.1,-161.0), 326484, 3531, "renegade_nebo", 5)
-- Комната Лебедева
spawn_restrictor("marsh", vector():set(-180.9,1.8,-281.0), 50163, 3367, "nebo", 4)
-- казарма на базе ЧН
spawn_restrictor("marsh", vector():set(-140.3,0.7,-314.9), 75337, 3374, "nebo", 4)
-- рыбацкий хутор
spawn_restrictor("marsh", vector():set(40.4,2.9,-243.9), 160230, 3443, "nebo", 5)
-- камни у фортпоста
spawn_restrictor("marsh", vector():set(-302.4,1.0,-43.1), 160230, 3443, "nebo", 3)
-- камни у севпоста
spawn_restrictor("marsh", vector():set(-233.0,1.0,-47.7), 1138, 3338, "nebo", 3)
-- шахта у разрушенного моста
spawn_restrictor("marsh", vector():set(-182.0,2.1,534.4), 984, 3338, "nebo", 3)
-- лодочная станция
spawn_restrictor("marsh", vector():set(-178.1,2.5,418.6), 52731, 3361, "renegade", 3)
-- погреб в посёлке
spawn_restrictor("marsh", vector():set(-42.3,-0.9,267.4), 118647, 3402, "renegade", 2)
-- механизаторский двор
spawn_restrictor("marsh", vector():set(410.7,3.4,238.4), 410944, 3546, "renegade", 5)
-- насосная
spawn_restrictor("marsh", vector():set(-3.9,3.4,11.9), 136892, 3418, "renegade", 3)
-- Северный хутор
spawn_restrictor("marsh", vector():set(580.0,3.631,422.174), 517690, 3578, "renegade", 2.5)

-------------------
-- pripyat --
-------------------
-- прачечная
spawn_restrictor("pripyat", vector():set(154.7,-0.1,-187.5), 383936, 3765, "fake_", 5)
-- подвал универмага
spawn_restrictor("pripyat", vector():set(-120.0,-11.1,-226.7), 75795, 3752, "fake_", 5)
-- магазин "Книги"
spawn_restrictor("pripyat", vector():set(-178.3,0.5,-345.1), 35637, 3753, "fake_", 7)
-- КБО "Юбилейный"
spawn_restrictor("pripyat", vector():set(-71.0,0.4,102.5), 118983, 3754, "stalker", 5)
-- подвал школы
spawn_restrictor("pripyat", vector():set(24.2,-4.3,127.7), 230005, 3758, "stalker", 3)
-- кинотеатр
spawn_restrictor("pripyat", vector():set(80.1,0.7,367.8), 300687, 3760, "stalker", 10)


-------------------
-- red_forest --
-------------------
-- тайная дверь
spawn_restrictor("red_forest", vector():set(200.1,0.1,-315.4), 421, 3231, "fake_", 5)
-- башня Лесника
spawn_restrictor("red_forest", vector():set(27.9,-0.0,25.3), 82467, 3263, "stalker", 4)
-- база бандитов в шахте
spawn_restrictor("red_forest", vector():set(-152.5,2.3,-27.5), 6299, 3240, "bandit", 5)
-- военный туннель
spawn_restrictor("red_forest", vector():set(-97.7,-0.8,-191.0), 12624, 3241, "fake_", 7)

-- limansk --
-------------------
-- подвал дома
spawn_restrictor("limansk", vector():set(-52.0,-11.9,-166.3), 2891, 2993, "fake_", 7)
-- стройка
spawn_restrictor("limansk", vector():set(34.7,14.0,190.8), 44841, 3010, "killer", 8)


-- swamp_old --
-------------------
-- туннель
spawn_restrictor("swamp_old", vector():set(-322.8,2.3,69.1), 450681, 4386, "stalker", 5)
-- домик командира
spawn_restrictor("swamp_old", vector():set(-68.1,2.5,264.6), 997547, 4399, "stalker", 3)
-- домик сталкеров
spawn_restrictor("swamp_old", vector():set(-41.4,5.7,272.6), 1055036, 4399, "stalker", 4)
-- домик Болотного Доктора
spawn_restrictor("swamp_old", vector():set(197.5,2.8,-37.8), 1577986, 4399, "stalker", 3)


-- atp_for_test22 --
-------------------
-- блокпост в поле
spawn_restrictor("atp_for_test22", vector():set(-173.9,3.557,225.523), 125764, 2799, "stalker", 3)
-- бар
spawn_restrictor("atp_for_test22", vector():set(109.5,-7.0,8.3), 534930, 2827, "stalker_trader", 5)
-- казарма
spawn_restrictor("atp_for_test22", vector():set(156.6,-7.0,-63.0), 601832, 2833, "stalker", 5)
-- блокпост в лесу
spawn_restrictor("swamp_old", vector():set(322.0,-6.0,-294.1), 837294, 2847, "stalker", 2)


-- dead_city --
-------------------
-- разрушенная пятиэтажка
spawn_restrictor("dead_city", vector():set(-110.5,2.9,106.2), 230777, 3609, "ecolog", 3)
-- торгово-спортивный комплекс
spawn_restrictor("dead_city", vector():set(-13.5,7.4,96.9), 342433, 3630, "fake_", 4)
-- дом-1
spawn_restrictor("dead_city", vector():set(27.2,8.7,-129.4), 387117, 3646, "fake_", 2)
-- дом-2
spawn_restrictor("dead_city", vector():set(-317.6,9.7,123.6), 44621, 3588, "fake_", 2)

-------------------
-- lost_village --
-------------------
-- погреб
spawn_restrictor("lost_village", vector():set(18.0,9.7,29.6), 18870, 3330, "fake_", 3)

-------------------
-- generators --
-------------------
-- труба
spawn_restrictor("generators", vector():set(144.4,26.0,-503.5), 502834, 3131, "fake_", 2)
-- вход в лабу
spawn_restrictor("generators", vector():set(171.7,30.8,-484.6), 534622, 3135, "dragon", 3)

-------------------
-- hospital --
-------------------
-- южное укрытие
spawn_restrictor("hospital", vector():set(-83.9,20.2,563.6), 4308, 3075, "fake_", 4)
-- северное укрытие
spawn_restrictor("hospital", vector():set(-90.8,24.9,750.9), 2324, 3043, "fake_", 4)

-------------------
-- zaton --
-------------------
-- "Скадовск"
spawn_restrictor("zaton", vector():set(131.6,-7.3,183.4), 1147694, 3686, "stalker_trader_ecolog", 5)
spawn_restrictor("zaton", vector():set(106.5,-1.3,186.2), 1149484, 3686, "stalker", 2)

-- "Шевченко"
spawn_restrictor("zaton", vector():set(-2.7,-0.5,37.1), 938430, 3677, "bandit", 3)
spawn_restrictor("zaton", vector():set(8.1,-5.2,36.5), 961108, 3677, "bandit", 5)

-- пещера Тремора 
spawn_restrictor("zaton", vector():set(25.8,4.0,367.0), 997053, 3682, "stalker", 3)
-- труба 
spawn_restrictor("zaton", vector():set(-437.1,2.4,10.5), 126763, 3660, "fake_", 3)
-- ВНЗ "Круг" 
spawn_restrictor("zaton", vector():set(-402.1,0.4,26.1), 175695, 3660, "fake_", 5)
-- лаборатория 
spawn_restrictor("zaton", vector():set(-424.3,24.2,-382.0), 142020, 3664, "fake_", 5)
-- подстанция 
spawn_restrictor("zaton", vector():set(-145.7,23.6,-429.5), 651105, 3678, "killer", 3)
-- станция очистки 
spawn_restrictor("zaton", vector():set(297.0,29.8,-408.2), 1470756, 3690, "fake_", 2)
-- лесничество 
spawn_restrictor("zaton", vector():set(417.3,36.3,-13.1), 1649843, 3693, "fake_", 3)
-- земснаряд 
spawn_restrictor("zaton", vector():set(410.5,-5.8,222.4), 1637732, 3692, "fake_", 1)
-- баржа Ноя 
spawn_restrictor("zaton", vector():set(153.0,-6.8,-138.7), 1228279, 3688, "stalker", 3)

-------------------
-- jupiter --
-------------------
--"Янов"
spawn_restrictor("jupiter", vector():set(-50.5,3.4,198.0), 635020, 3717, "stalker_trader", 9)
--подвал "Янов"
spawn_restrictor("jupiter", vector():set(-55.0,-0.6,198.7), 627238, 3717, "stalker", 4)
--бункер
spawn_restrictor("jupiter", vector():set(-217.8,-3.5,74.0), 348407, 3708, "ecolog_trader", 3)
--бункер_2
spawn_restrictor("jupiter", vector():set(-228.5,1.6,74.6), 343412, 3708, "stalker", 2)
--бункер_3
spawn_restrictor("jupiter", vector():set(-221.0,-3.5,60.7), 342137, 3708, "stalker", 3)
-- под цементным заводом
spawn_restrictor("jupiter", vector():set(365.7,-5.8,352.1), 1296703, 3742, "fake_", 2)
-- в колодце
spawn_restrictor("jupiter", vector():set(72.3,-0.8,332.4), 839769, 3728, "fake_", 3)
-- склад "Долга"
spawn_restrictor("jupiter", vector():set(171.7,-8.7,211.4), 996811, 3732, "fake_", 3)
-- ж.д. туннель
spawn_restrictor("jupiter", vector():set(-361.2,4.2,390.0), 111766, 3703, "fake_", 5)
-- локационная станция
spawn_restrictor("jupiter", vector():set(-381.2,2.6,-5.5), 72292, 3700, "fake_", 5)
-- контейнерный склад
spawn_restrictor("jupiter", vector():set(-450.1,0.1,-380.2), 373, 3702, "bandit", 4)
-- контейнерный склад_2
spawn_restrictor("jupiter", vector():set(-447.6, 0.1, -365.7), 685, 3702, "bandit", 4)
-- контейнерный склад_3
spawn_restrictor("jupiter", vector():set(-413.3,0.2,-382.6), 26300, 3702, "bandit", 2)
-- вентиляционный комплекс
spawn_restrictor("jupiter", vector():set(-180.5,0.8,-271.6), 410370, 3710, "fake_", 5)
-- КПП
spawn_restrictor("jupiter", vector():set(10.0,24.6,-186.5), 730508, 3727, "fake_", 5)
-- завод "Юпитер"
spawn_restrictor("jupiter", vector():set(241.9,28.3,-444.3), 1127920, 3734, "fake_", 3)
-- завод "Юпитер"
spawn_restrictor("jupiter", vector():set(420.4,28.0,-230.4), 1379308, 3743, "fake_", 3)
-- завод "Юпитер"
spawn_restrictor("jupiter", vector():set(347.5,27.8,-356.0), 1269445, 3745, "fake_", 50)
-- восточный туннель
spawn_restrictor("jupiter", vector():set(399.2,4.5,-106.2), 1344511, 3744, "fake_", 10)

-------------------
-- aver --
-------------------
-- сторожка
spawn_restrictor("aver", vector():set(-356.6,-6.8,16.2), 255342, 2889, "fake_", 3.8)
-- вагончик
spawn_restrictor("aver", vector():set(145.8,-2.6,-310.9), 1122742, 2900, "fake_", 2.5)

-------------------
-- garbage_old --
-------------------
-- развалины
spawn_restrictor("garbage_old", vector():set(-25.1,16.8,-286.2), 597373, 4144, "fake_", 3)
-- дом
spawn_restrictor("garbage_old", vector():set(48.9,22.0,-144.2), 790583, 4209, "stalker_freedom", 3)
-- туннель 1
spawn_restrictor("garbage_old", vector():set(-124.7,21.6,-75.0), 318456, 4040, "fake_", 5)
-- туннель 2
spawn_restrictor("garbage_old", vector():set(240.1,11.7,-104.9), 1150205, 4333, "fake_", 5)
-- ангар
spawn_restrictor("garbage_old", vector():set(-135.0,6.0,260.1), 289150, 4034, "stalker", 7)

-------------------
-- yantar_old --
-------------------
-- база
spawn_restrictor("yantar_old", vector():set(-210.3,18.9,116.2), 58570, 4345, "fake_", 3)
-- подземка-1
spawn_restrictor("yantar_old", vector():set(-106.9,-6.6,111.4), 165099, 4384, "fake_", 5)
-- подземка-2
spawn_restrictor("yantar_old", vector():set(59.5,-6.7,123.6), 349417, 4384, "fake_", 7)
-- подземка-3
spawn_restrictor("yantar_old", vector():set(-72.2,-13.9,110.5), 207233, 4384, "fake_",10)
-- подземка-4
spawn_restrictor("yantar_old", vector():set(-72.4,-13.9,-13.4), 206878, 4384, "fake_", 7)
-- подземка-5
spawn_restrictor("yantar_old", vector():set(-72.1,-13.8,204.7), 207489, 4384, "fake_", 7)

-- подземка-7
spawn_restrictor("yantar_old", vector():set(-135.0,6.0,260.1), 450370, 4384, "fake_", 10)
-- подземка-8
spawn_restrictor("yantar_old", vector():set(201.1,-12.0,-32.8), 510209, 4384, "fake_", 10)
-- ангар
spawn_restrictor("yantar_old", vector():set(90.9,9.4,107.5), 385764, 4365, "fake_", 7)
-- башня
spawn_restrictor("yantar_old", vector():set(-170.7,4.8,-118.2), 97735, 4346, "fake_", 3)
-- погреб
spawn_restrictor("yantar_old", vector():set(243.5,5.7,-122.8), 557512, 4380, "fake_", 3)
-- гаражи
spawn_restrictor("yantar_old", vector():set(213.1,7.6,-29.2), 523559, 4377, "fake_", 5)
-- бункер
spawn_restrictor("yantar_old", vector():set(190.4,9.6,202.7), 498426, 4375, "fake_", 15)

-------------------
-- puzir --
-------------------
-- в камнях
spawn_restrictor("puzir", vector():set(86.4,1.1,-52.6), 258531, 2884, "fake_", 2)

-------------------
-- predbannik --
-------------------
--подвал cталкерского городка
spawn_restrictor("predbannik", vector():set(-450.0,-5.7,-150), 253033, 3902, "stalker", 15)
--пещера
spawn_restrictor("predbannik", vector():set(-232.2,16.8,239.6), 582875, 3946, "stalker", 5)
--подвал торговца
spawn_restrictor("predbannik", vector():set(-23.6,-4.4,330), 906616, 3989, "stalker", 5)
--подвал у лагеря новичков
spawn_restrictor("predbannik", vector():set(-38.9,-1.1,332.9), 887935, 3985, "fake_", 5)
--подвал в потерянной деревне
spawn_restrictor("predbannik", vector():set(17.5,9.7,29.7), 970446, 4000, "fake_", 7)

end


-- The Scheme itself below should not be edited if you don't how to do it

property_base = 12221
property_blowout = property_base+1
property_hideout_lost = property_base+2
property_inhide = property_base+3
act_base = 13332
act_hideout = act_base+1
act_psy_effect = act_base+2
act_inhide = act_base+3

-- Создаем эвалуатор - проверяем, начался ли выброс
class "evaluator_blowout" (property_evaluator)
function evaluator_blowout:__init(name, storage) super (nil, name)
	self.st = storage
end
function evaluator_blowout:evaluate()
	if not self.object:best_enemy() and 
	   db.Flag2==1 and db.Dead2==0 and 
	   not (level.name()=="l11_pripyat" and self.object:character_community() == "monolith") and
	   not is_in_hide(self.object) and not is_in_specific_zone(self.object) then
		self.st.blowout = true
	else
		self.st.blowout = false
	end
	return self.st.blowout == true
end

-- Создаем эвалуатор - проверяем, потерял ли непись укрытие
class "evaluator_hideout" (property_evaluator)
function evaluator_hideout:__init(name, storage) super (nil, name)
	self.st = storage
end
function evaluator_hideout:evaluate()
	if db.Dead2==1 and not is_in_hide(self.object) and not is_in_specific_zone(self.object) then
		self.st.hideout_lost = true
	else
		self.st.hideout_lost = false
	end
	return self.st.hideout_lost == true
end

-- Создаем эвалуатор - проверяем, сидит ли уже непись в укрытии
class "evaluator_inhide" (property_evaluator)
function evaluator_inhide:__init(name, storage) super (nil, name)
	self.st = storage
end
function evaluator_inhide:evaluate()
	if db.Flag2==1 and is_in_hide(self.object) then
		self.st.inhide = true
	else
		self.st.inhide = false
	end
	return self.st.inhide == true
end 


-- Создаем оператор - направляем НПС в укрытие
class "action_hideout" (action_base)
function action_hideout:__init(name, storage) super (nil, name)
	self.st = storage
end
function action_hideout:initialize()
	local npc = self.object
	npc:set_desired_position()
	npc:set_desired_direction()
	npc:clear_animations()
	npc:set_detail_path_type(move.line)
	npc:set_path_type(game_object.level_path)
	npc:remove_all_restrictions()
	if npc_run[npc:id()] ~= 1 then
		self.hide = get_nearest_hide(npc)
		if self.hide then
			npc:add_restrictions(self.hide.obj:name(), "")
			state_mgr.set_state(npc, "assault")
		else
			npc:remove_all_restrictions()
			state_mgr.set_state(npc, "hide")
		end
		npc_run[npc:id()] = 1
	end
end
function action_hideout:execute()
	local npc = self.object
	npc:disable_talk()
end
function action_hideout:finalize()
	action_base.finalize(self)
	local npc = self.object
	npc_run[npc:id()] = 0
end

-- Создаем оператор - колбасим НПС, если он не нашел укрытие
class "action_raskolbas" (action_base)
function action_raskolbas:__init(name, storage) super (nil, name)
	self.st = storage
end
function action_raskolbas:initialize()
	local npc = self.object
	npc:set_desired_position()
	npc:set_desired_direction()
	npc:clear_animations()
	npc:remove_all_restrictions()
	local ran = math.random(1,2)
	if npc_psy[npc:id()] ~= 1 then
		if npc:character_community() == "monolith" then
			state_mgr.set_state(npc, "trans_1")
		else
			if ran == 1 then
				state_mgr.set_state(npc, "psycho_pain")
			else
				state_mgr.set_state(npc, "psy_pain")
			end
		end
		npc_psy[npc:id()] = 1
	end
end
function action_raskolbas:execute()
	local npc=self.object
	npc:disable_talk()
end
function action_raskolbas:finalize()
	action_base.finalize(self)
	local npc = self.object
	npc_psy[npc:id()] = 0
end

-- Создаем оператор - усаживаем НПС, если он нашел укрытие
class "action_inhide" (action_base)
function action_inhide:__init(name, storage) super (nil, name)
	self.st = storage
end
function action_inhide:initialize()
	local npc = self.object
	npc:set_desired_position()
	npc:set_desired_direction()
	npc:clear_animations()
	npc:set_detail_path_type(move.line)
	npc:set_path_type(game_object.level_path)
	npc:remove_all_restrictions()
	self.hide = get_nearest_hide(npc)
	if self.hide then
		self.vertex = self.hide.obj:level_vertex_id()
		self.offset = vector():set(math.random()*3-1, 0, math.random()*3-1)
		self.offset:normalize()
		self.radius = math.random(1, self.hide.rad-1)
		self.lvid = npc:vertex_in_direction(self.vertex, self.offset, self.radius)
		if npc:level_vertex_id()~=self.lvid then
			state_mgr.set_state(npc,"raid")
			utils.send_to_nearest_accessible_vertex(npc, self.lvid) 
		else
			state_mgr.set_state(npc,"hide")
		end
	else
		state_mgr.set_state(npc,"hide")
	end
end
function action_inhide:execute()
	local npc = self.object
	npc:disable_talk()
	if npc:level_vertex_id()~=self.lvid then
		if npc_go[npc:id()]~=1 then
			state_mgr.set_state(npc,"raid")
			utils.send_to_nearest_accessible_vertex(npc, self.lvid) 
			npc_go[npc:id()] = 1
			npc_sit[npc:id()] = 0
		end
	else
		if npc_sit[npc:id()]~=1 then
			if npc:character_community()=="monolith" then
				state_mgr.set_state(npc,"trans_0")
			else
				state_mgr.set_state(npc,"hide")
			end
			npc_sit[npc:id()] = 1
			npc_go[npc:id()] = 0
		end
	end
end
function action_inhide:finalize()
	action_base.finalize(self)
	local npc = self.object
	npc_sit[npc:id()] = 0
	npc_go[npc:id()] = 0
	npc:remove_all_restrictions()
end


-- Добавляем в планировщик нашу схему
function add_to_binder(object, char_ini, scheme, section, st)
	local manager = object:motivation_action_manager()
	local property_wounded = xr_evaluators_id.sidor_wounded_base

	manager:remove_evaluator(property_blowout)
	manager:add_evaluator(property_blowout, evaluator_blowout("evaluator_blowout", st))

	manager:remove_evaluator(property_hideout_lost)
	manager:add_evaluator(property_hideout_lost, evaluator_hideout("evaluator_hideout", st))

	manager:remove_evaluator(property_inhide)
	manager:add_evaluator(property_inhide, evaluator_inhide("evaluator_inhide", st))

	local action = action_hideout("action_hideout", st)
	action:add_precondition(world_property(stalker_ids.property_alive, true))
	action:add_precondition(world_property(property_wounded, false))
	action:add_precondition(world_property(stalker_ids.property_enemy, false))
	action:add_precondition(world_property(stalker_ids.property_danger, false))
	action:add_precondition(world_property(property_hideout_lost, false))
	action:add_precondition(world_property(property_inhide, false))
	action:add_precondition(world_property(property_blowout, true))
	action:add_effect(world_property(property_blowout, false))
	manager:add_action(act_hideout, action)

	local action = action_raskolbas("action_raskolbas", st)
	action:add_precondition(world_property(stalker_ids.property_alive, true))
	action:add_precondition(world_property(property_wounded, false))
	action:add_precondition(world_property(property_blowout, false))
	action:add_precondition(world_property(property_inhide, false))
	action:add_precondition(world_property(property_hideout_lost, true))
	action:add_effect(world_property(property_hideout_lost, false))
	manager:add_action(act_psy_effect, action)

	local action = action_inhide("action_inhide", st)
	action:add_precondition(world_property(stalker_ids.property_alive, true))
	action:add_precondition(world_property(property_wounded, false))
	action:add_precondition(world_property(stalker_ids.property_enemy, false))
	action:add_precondition(world_property(stalker_ids.property_danger, false))
	action:add_precondition(world_property(property_blowout, false))
	action:add_precondition(world_property(property_hideout_lost, false))
	action:add_precondition(world_property(property_inhide, true))
	action:add_effect(world_property(property_inhide, false))
	manager:add_action(act_inhide, action)

	action = manager:action(stalker_ids.action_alife_planner)
	action:add_precondition(world_property(property_blowout, false))
	action:add_precondition(world_property(property_hideout_lost, false))
	action:add_precondition(world_property(property_inhide, false))

	action = manager:action(stalker_ids.action_combat_planner)
	action:add_precondition(world_property(property_hideout_lost, false))

	action = manager:action(stalker_ids.action_danger_planner)
	action:add_precondition(world_property(property_hideout_lost, false))
end


-- Функции включения/выключения схемы
function enable_scheme(npc, ini)
	local st = xr_logic.assign_storage_and_bind(npc, ini, "ogsm_hideout")
	st.enabled=true
end
function disable_scheme(npc, scheme)
	local st = db.storage[npc:id()][scheme]
	if st then
		st.enabled = false
	end
end


---------------- Схема поведения НПС при выбросе для OGSM 2.x -----------------

------------------------- Copyright 2007-2008 DEXXX ---------------------------

--[[

If you're going to use the whole of this script or its parts in your own creative 
developments for the S.T.A.L.K.E.R. game, please don't become such a goddamn 
motherfucker like the notorious author of the ABC Mod - Carbrobro. Leave the 
copyrights, note the real author(s) and don't claim others' ideas and their 
realization to be your own ones. It's just simple Modmakers' Ethics. Thank you!

Если вы собираетесь использовать данный скрипт целиком или частично в своих 
разработках по игре S.T.A.L.K.E.R., пожалуйста не опускайтесь до уровня печально 
известного автора ABC мода - Carbrobro. Не удаляйте копирайты, указывайте настоящего 
автора(ов) и не выдавайте чужие идеи и их реализацию за свои. Ведь это элементарная 
этика модостроителей! Спасибо за понимание.

]]--