----------------------------------------------------------------------
--	created:	2004/12/03
--	created:	3:12:2004   14:46
--	filename: 	x:\gamedata\scripts\zone_spawner.script
--	file path:	x:\gamedata\scripts
--	file base:	zone_spawner
--	author:		Sidorovich
--	purpose:	
----------------------------------------------------------------------
local temporary_store = {}

local zones = {}

-------------------------------------------------------------------------
function add_member (obj)

    local ini = obj:spawn_ini ()
    if ini == nil or ini:section_exist ("zone_spawner") == false then
       return 
    end
    
    if ini:line_exist ("zone_spawner", "id") == false then
       --printf ("Object %s can't attach to zone spawner - ID number not found", obj:name ()) 
       return
    end       
    
    local zone_id = ini:r_u32 ("zone_spawner", "id")
    for a = 1, #zones, 1 do
        if zones[a].id == zone_id then
           printf ("Object %s attached to zone with id %d", obj:name (), zones[a].id)
           zones[a]:add_member (obj)
           return
        end
    end 
    
    local t = {id = obj.id, id_spawner = zone_id}
    printf ("Object %s include to temporary store", obj:name ())
    table.insert (temporary_store, t)
    this.switch_offline (t.id)
    
end
-------------------------------------------------------------------------
function activate_by_infoportion (info)
    for a = 1, #zones, 1 do
        if #zones[a].infoportions ~= 0 then
           for b = 1, #zones[a].infoportions, 1 do
               if info == zones[a].infoportions[b] then
                  zones[a]:on_infoportion (info)  
                end
           end     
        end
    end 
end



-------------------------------------------------------------------------
class "zone_spawner"
-----------------------
function zone_spawner:__init (zone, ini)
    self.ini_ok = true
    self.activated = false
    self.infoportions = {}
    if ini:line_exist ("zone_spawner", "id") == false then
       --printf ("Zone spawner without ID. Set defualt ID = 0") 
       self.id = 0
    else
       self.id = ini:r_u32 ("zone_spawner", "id")
    end   
    
    if ini:line_exist ("zone_spawner", "infoportion") == true then
       local infos = ini:r_string ("zone_spawner", "infoportion")
       if infos ~= nil then 
          printf ("Zone has infoportions - %s", infos)
          self.infoportions = utils.parse_params (infos)
       end
    end   
    
    table.insert (zones, self)
    printf ("Zone %s is added to binder", zone:name ())
    self.members = {}
    for a = 1, #temporary_store, 1 do
        if temporary_store[a].id_spawner == self.id then
           table.insert (self.members,  temporary_store[a])
           this.switch_offline (temporary_store[a].id)
        end
    end
end
-----------------------
function zone_spawner:on_enter (obj)
    local actor = level.actor ()
    if actor == nil or obj:id () ~= actor:id () then 
       return 
    end
    if self.activated == true then return end
    
    local founded = false
    local size = #self.infoportions
    if size ~= 0 then
       for a = 1, size, 1 do
          if has_alife_info(self.infoportions[a]) == true then
             --printf ("Actor have a infoportion %s", self.infoportions[a]) 
             founded = true
          --else
             --printf ("Actor don't have a infoportion %s", self.infoportions[a])    
          end
       end
    else 
       founded = true         
    end    
    
    if founded == false then return end
    
    self.activated = true
    if #self.members == 0 then return end
    for a = 1, #self.members, 1 do
        this.switch_online (self.members[a].id)
    end
end
-----------------------
function zone_spawner:add_member (obj)
    if self.activated == true then return end
    local t = {id = obj.id, id_spawner = zone_id}    
    table.insert (self.members, t)
    this.switch_offline (obj.id)
end
-----------------------
function zone_spawner:on_infoportion (info)
    local founded = false
    local size = #self.infoportions
    if size ~= 0 then
       for a = 1, size, 1 do
          if info == self.infoportions[a] then
             --printf ("Actor have a infoportion %s", self.infoportions[a]) 
             founded = true
          --else
             --printf ("Actor don't have a infoportion %s", self.infoportions[a])    
          end
       end
    else 
       founded = true         
    end    
    
    if founded == false then return end
    
    self.activated = true
    if #self.members == 0 then return end
    for a = 1, #self.members, 1 do
        this.switch_online (self.members[a].id)
    end
end


-------------------------------------------------------------------------------------------
function switch_online (id)
    if id == -1 then return end
    local sim = alife ()
    if sim ~= nil then
       sim:set_switch_online  (id, true)
       sim:set_switch_offline (id, false)
    end
end
-------------------------------------------------------------------------------------------
function switch_offline (id)
    if id == -1 then return end
    local sim = alife ()
    if sim ~= nil then
       sim:set_switch_online  (id, false)
       sim:set_switch_offline (id, true)
    end
end
