----------------------------------------------------------------------------------------------------------------------
--  Проверка условий разрешения или запрещения спавна
--  автор: Диденко Руслан (Stohe)
--  TODO:
----------------------------------------------------------------------------------------------------------------------
--[[
spawner = nil

function spawn(npc)
	local char_ini = npc:spawn_ini()
		
	xr_spawner.create_spawner()
	xr_spawner.spawner:addList(npc)
	xr_spawner.spawner:check_spawn(npc.id)
	
	--'!! Подумать и убрать нахрен, there could be only one!!!
	if char_ini:section_exist ("zone_spawner") then
	   zone_spawner.add_member (npc)
	end    
	if char_ini:section_exist ("escape_raid") then
	   escape_raid.add_member (npc)
	end    
	return true
end

function spawn_client(npc)
	local char_ini = npc:spawn_ini()

	xr_spawner.create_spawner()
	local sim = alife()
	if sim == nil then return true end
	sobject = sim:object(npc:id())
	xr_spawner.spawner:addList(sobject)
	xr_spawner.spawner:check_spawn(npc:id())
	return true
end

class "object_spawner"
function object_spawner:__init()
	self.list = {}
	self.timer = game.get_game_time()
	self.idle = 60
end
function object_spawner:check_spawn(npc_id)
	---' !!! Тут вставить проверку по кондлисту
	local actor = db.actor
	--print_table(self.list)
	if actor ~= nil and
	   self.list[npc_id].cond ~= nil
	then
		if xr_logic.pick_section_from_condlist(actor,nil,self.list[npc_id].cond.condlist) ~= nil then
			self:restore(npc_id)
		else
			self:hide(npc_id)
		end
	end
end
function object_spawner:addList(sobject)
	local char_ini = sobject:spawn_ini()
	
	---' !!! Тут добавить чтение кондлиста
	self.list[sobject.id] = {	id = sobject.id, 
								on = sobject:can_switch_online(), 
								off = sobject:can_switch_offline(), 
								int = sobject:interactive(), 
								pos = sobject.position,
								cond = xr_logic.cfg_get_condlist(char_ini,"spawner","cond",sobject) }
end
function object_spawner:remove_from_list (id)
    printf ("Query for remove object with ID : %d", id)
    for k, v in  pairs (self.list) do
        if self.list[k].id == id then
           self.list[k] = nil
           printf ("Object with ID : %d successfully removed from spawn list", id)
           return
        end
    end        
    
    printf ("Object with ID : %d not found in spawn list", id)
end

function object_spawner:restore(npc_id)
	local sim = alife()
	if sim == nil then return true end	
	sim:set_switch_online	(npc_id, self.list[npc_id].on)
	sim:set_switch_offline	(npc_id, self.list[npc_id].off)
	sim:set_interactive		(npc_id, self.list[npc_id].int)
end
function object_spawner:hide(npc_id)
	local sim = alife()
	if sim == nil then return true end
	sim:set_switch_online	(npc_id, false)
	sim:set_switch_offline	(npc_id, true)
	sim:set_interactive		(npc_id, false)
end
function object_spawner:update()	
	--printf("!!%s!! %s", self.name, game.get_game_time():diffSec(self.timer))
	if game.get_game_time():diffSec(self.timer) > self.idle then
		for k,v in pairs(self.list) do
			self:check_spawn(v.id)
		end		
		self.timer = game.get_game_time()
	end
end
function object_spawner:destroy()	
	for k,v in pairs(self.list) do
		self:restore(k)
		self.list[k] = nil
	end
end

function create_spawner()
	if xr_spawner.spawner == nil then
		xr_spawner.spawner = xr_spawner.object_spawner()
		printf("spawner created")
		return true	
	end
	return false
end
function update()
	if xr_spawner.spawner ~= nil then
		xr_spawner.spawner:update()
	end
end

function remove_from_list (id)
    if xr_spawner.spawner ~= nil then
       xr_spawner.spawner:remove_from_list (id)
    end
end        
]]--
-----------------------------------------------------------------------------------
local spawn_list = {}
-----------------------------
function add_to_spawn_list (s_object)
	for k,v in pairs(spawn_list) do
		if v.id == s_object.id then 
		   return 
        end
	end
	
	spawn_list[s_object.id] = 
	                        {
	                        id = s_object.id,
	                        cond = xr_logic.cfg_get_condlist (s_object:spawn_ini (), "spawner", "cond", s_object),
	                        npc = nil,
	                        online = false
	                        }
    -- printf ("Add object : %s", s_object:name ())
    -- if spawn_list[s_object.id].cond then print_table (spawn_list[s_object.id].cond) end
end
-----------------------------
function check_spawn (s_object)
	if this.is_object_in_list (s_object) == false then
		this.add_to_spawn_list (s_object)
	end

	local actor = db.actor
	if actor ~= nil then
		if spawn_list[s_object.id].cond ~= nil then
		    if spawn_list[s_object.id].npc == nil then
               if level.object_by_id(s_object.id) ~= nil then 
                  if is_object_online (s_object.id) == true then
                     spawn_list[s_object.id].npc = level.object_by_id(s_object.id)
                  else   
                     spawn_list[s_object.id].npc = nil
                  end
               else       
                  spawn_list[s_object.id].npc = nil  
               end
            end   
			if xr_logic.pick_section_from_condlist(actor, spawn_list[s_object.id].npc, 
			 								   spawn_list[s_object.id].cond.condlist) ~= nil 
		    then
				return true
			else
			    spawn_list[s_object.id].npc = nil
			    spawn_list[s_object.id].online = false
				return false 
			end
		else
			return true
		end
	end
	spawn_list[s_object.id].npc = nil
	return false
end
-----------------------------------------------------------------------------------
function is_object_in_list (s_object)
    return spawn_list[s_object.id] ~= nil
end
