----------
-- чтение формы из нет-пакета
----------
function r_shape(packet)
	local s
	local st = {}
	st.count = packet:r_u8()
	st.shapes = {}
	for i=1, st.count do
		s = {}
		s.type = packet:r_u8()
		if s.type == 0 then
			s.center = packet:r_vec3()
			s.radius = packet:r_float()
		else
			s.axis_x = packet:r_vec3()
			s.axis_y = packet:r_vec3()
			s.axis_z = packet:r_vec3()
			s.offset = packet:r_vec3()
		end
		st.shapes[i] = s
	end
	return st
end

----------
-- запись формы в нет-пакет
----------
function w_shape(packet, st)
	local s
	packet:w_u8(st.count)
	for i=1, st.count do
		s = st.shapes[i]
		packet:w_u8(s.type)
		if s.type == 0 then
			packet:w_vec3(s.center)
			packet:w_float(s.radius)
		else
			packet:w_vec3(s.axis_x)
			packet:w_vec3(s.axis_y)
			packet:w_vec3(s.axis_z)
			packet:w_vec3(s.offset)
		end
	end
end

----------
-- перепаковка нет-пакета созданного скриптом рестрикта
----------
function rewrite_restrictor(se_obj, custom, radius)
	local packet = net_packet()
	se_obj:STATE_Write(packet)
	local game_vertex_id	= packet:r_u16()
	local distance		= packet:r_float()
	local direct_control	= 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 spawn_story_id	= packet:r_s32()
	local shape		= r_shape(packet)
	local restrictor_type	= packet:r_u8()

	custom_data = custom
	shape = {}
	shape.count = 1
	shape.shapes = {}
	shape.shapes[1] = {}
	shape.shapes[1].type = 0
	shape.shapes[1].center = vector():set(0,0,0)
	shape.shapes[1].radius = radius

	packet:w_u16(game_vertex_id)
	packet:w_float(distance)
	packet:w_s32(direct_control)
	packet:w_s32(level_vertex_id)
	packet:w_s32(object_flags)
	packet:w_stringZ(custom_data)
	packet:w_s32(story_id)
	packet:w_s32(spawn_story_id)
	w_shape(packet, shape)
	packet:w_u8(restrictor_type)
	se_obj:STATE_Read(packet, packet:w_tell() - packet:r_tell())
end
 
function create_hram_restr_1() 
	local se_obj = alife():create("space_restrictor",vector():set(273.517,13.651,-161.714),311136,3531)
	local custom = "[logic]\ncfg = scripts\\marsh\\hram_restrictor_1.ltx" 
	rewrite_restrictor(se_obj, custom, 1.0) 	
end 

function create_hram_restr_2() 
	local se_obj = alife():create("space_restrictor",vector():set(292.859,0.144,-169.183),326472,3531)
	local custom = "[logic]\ncfg = scripts\\marsh\\hram_restrictor_2.ltx" 
	rewrite_restrictor(se_obj, custom, 2.0) 	
end 

function create_hram_restr_3() 
	local se_obj = alife():create("space_restrictor",vector():set(292.67,0.144,-152.751),326496,3531)
	local custom = "[logic]\ncfg = scripts\\marsh\\hram_restrictor_3.ltx" 
	rewrite_restrictor(se_obj, custom, 2.0) 	
end 

function create_hram_restr_4() 
	local se_obj = alife():create("space_restrictor",vector():set(277.766,0.164,-161.091),314951,3510)
	local custom = "[logic]\ncfg = scripts\\marsh\\hram_restrictor_4.ltx" 
	rewrite_restrictor(se_obj, custom, 2.5) 	
end 

function create_hram_restr_5() 
	local se_obj = alife():create("space_restrictor",vector():set(299.387,4.768,-159.800),311136,3510)
	local custom = "[logic]\ncfg = scripts\\marsh\\hram_restrictor_5.ltx" 
	rewrite_restrictor(se_obj, custom, 2.0) 	
end

function create_hram_restr_6() 
	local se_obj = alife():create("space_restrictor",vector():set(273.517,13.651,-161.714),311136,3531)
	local custom = "[logic]\ncfg = scripts\\marsh\\hram_restrictor_6.ltx" 
	rewrite_restrictor(se_obj, custom, 2.0) 	
end 

function fortpost_restr() 
	local se_obj = alife():create("space_restrictor",vector():set(-279.268,1.455,22.784),7074,3350)
	local custom = "[logic]\ncfg = scripts\\marsh\\fortpost_restr.ltx" 
	rewrite_restrictor(se_obj, custom, 7.0) 	
end

function spawn_machete_restr() 
	local se_obj = alife():create("space_restrictor",vector():set(-144.912,1.122,82.893),72988,3379)
	local custom = "[logic]\ncfg = scripts\\marsh\\spawn_machete_restr.ltx" 
	rewrite_restrictor(se_obj, custom, 2.0)
end

function create_shram_restr() 
	local sr = alife():create("space_restrictor",vector():set(292.661,0.154,-179.599),326457,3531) 
	local custom = [[[logic] 
	active = sr_idle 
	[sr_idle] 
	on_npc_in_zone = 15045|]]..sr:name()..[[|nil %+shram_delete%]] 
	rewrite_restrictor(sr, custom, 1.0) 
end 

function create_kino_restr() 
	local se_obj = alife():create("space_restrictor",vector():set(32.093,-0.391,362.837),240860,3760)
	local custom = "[logic]\ncfg = scripts\\pripyat_z\\kino_restr.ltx" 
	rewrite_restrictor(se_obj, custom, 5.0) 
end

function create_kino_restr2() 
	local sr = alife():create("space_restrictor",vector():set(40.933,1.991,363.758),251840,3760) 
	local custom = [[[logic] 
	active = sr_idle 
	[sr_idle] 
	on_npc_in_zone = 15047|]]..sr:name()..[[|nil %+shuhov_delete%]] 
	rewrite_restrictor(sr, custom, 1.0)  
end 

function telephon_restr() 
	local se_obj = alife():create("space_restrictor",vector():set(-205.809,1.000,-165.560),21097,3752)
	local custom = "[logic]\ncfg = scripts\\pripyat_v\\telephon_restr.ltx" 
	rewrite_restrictor(se_obj, custom, 2.0) 
end

function spawn_mon_restrictors() 
	local se_obj = alife():create("space_restrictor",vector():set(131.447,-0.065,-180.216),358740,3765)
	local custom = "[logic]\ncfg = scripts\\pripyat_v\\spawn_mon_restr_1.ltx" 
	rewrite_restrictor(se_obj, custom, 1.0)
	local se_obj = alife():create("space_restrictor",vector():set(140.952,-0.059,-183.419),369719,3765)
	local custom = "[logic]\ncfg = scripts\\pripyat_v\\spawn_mon_restr_2.ltx" 
	rewrite_restrictor(se_obj, custom, 1.0) 
	local se_obj = alife():create("space_restrictor",vector():set(133.804,2.404,-172.333),361197,3765)
	local custom = "[logic]\ncfg = scripts\\pripyat_v\\spawn_mon_restr_3.ltx" 
	rewrite_restrictor(se_obj, custom, 1.0)
	local se_obj = alife():create("space_restrictor",vector():set(141.099,4.940,-183.361),370553,3765)
	local custom = "[logic]\ncfg = scripts\\pripyat_v\\spawn_mon_restr_4.ltx" 
	rewrite_restrictor(se_obj, custom, 1.0)
end

function spawn_mon_plen_restr() 
	local se_obj = alife():create("space_restrictor",vector():set(75.447,-3.622,124.053),187116,2168)
	local custom = "[logic]\ncfg = scripts\\pripyat_z\\spawn_mon_plen_restr.ltx" 
	rewrite_restrictor(se_obj, custom, 10.0)
end

function create_tunnel_pred_restr_sheph() 
	local sr = alife():create("space_restrictor",vector():set(169.977,8.560,-27.908),1208512,4028) 
	local custom = [[[logic] 
	active = sr_idle 
	[sr_idle] 
	on_npc_in_zone = 1610|]]..sr:name()..[[|nil %+sheph_del%]]
	rewrite_restrictor(sr, custom, 2.0)  
end

function create_tunnel_pred_restr_varjag() 
	local sr = alife():create("space_restrictor",vector():set(169.977,8.560,-27.908),1208512,4028) 
	local custom = [[[logic] 
	active = sr_idle 
	[sr_idle] 
	on_npc_in_zone = 1861|]]..sr:name()..[[|nil %+varjag_del_2%]]
	rewrite_restrictor(sr, custom, 2.0)
end 

function create_tunnel_pred_restr_actor() 
	local se_obj = alife():create("space_restrictor",vector():set(169.977,8.560,-27.908),1208512,4028)
	local custom = "[logic]\ncfg = scripts\\predbannik\\actor_restr.ltx" 
	rewrite_restrictor(se_obj, custom, 3.0)
end

function create_aver_restr() 
	local se_obj = alife():create("space_restrictor",vector():set(-366.741,40.932,376.416),238190,2887)
	local custom = "[logic]\ncfg = scripts\\aver\\spawn_monstrov.ltx" 
	rewrite_restrictor(se_obj, custom, 2.0)
end

function create_door_restr() 
	local se_obj = alife():create("space_restrictor",vector():set(-350.842,4.232,393.059),123808,3703)
	local custom = "[logic]\ncfg = scripts\\jupiter\\jup_tunnel_door.ltx" 
	rewrite_restrictor(se_obj, custom, 1.0)
end

function restr_spawn_soldier_usa()
	local se_obj = alife():create("space_restrictor",vector():set(-350.842,4.232,393.059),123808,3703)
	local custom = "[logic]\ncfg = scripts\\jupiter\\jup_tunnel_door.ltx" 
	rewrite_restrictor(se_obj, custom, 1.0)
	local se_obj = alife():create("space_restrictor",vector():set(-359.592,4.288,396.895),115323,3703)
	local custom = "[logic]\ncfg = scripts\\jupiter\\spawn_soldier_usa.ltx" 
	rewrite_restrictor(se_obj, custom, 2.0)
end
