--[[------------------------------------------------------------------------------------------------
Space restrictor binder
Чугай Александр

Примечания:
  - обновлений у рестрикторов нету, поэтому для тех, кому надо, актёр вызывает искусственно на своём обновлении
--------------------------------------------------------------------------------------------------]]

local updatable_binders = {}    -- биндеры, которые требуют обновлений (update)

function actor_update(delta)
  for i, v in pairs(updatable_binders) do
    v:actor_update(delta)
  end
end

function bind( obj )
--  db.storage[obj:id()] = { }
  if obj:name()=="gar_vagon_in_restrictor" or obj:name()=="bar_restrictor" or 
     obj:name()=="rostok_sniper_killer_restrictor" or 
     obj:name()=="agr_u_bloodsucker_rest" then
    local sobj=alife():object(obj:id())
    if sobj then
      alife():release(sobj,true)
      return
    end
  end

  obj:bind_object( restrictor_binder( obj ) )
end

----------------------------------------------------------------------------------------------------
class "restrictor_binder" ( object_binder )

function restrictor_binder:__init(obj, char_ini) super(obj)
  self.initialized = false
  self.loaded = false
end

function restrictor_binder:reload(section)
  object_binder.reload(self, section)
end

function restrictor_binder:reinit()
  object_binder.reinit(self)

  db.storage[self.object:id()] = { }

  self.st = db.storage[self.object:id()]
end

function restrictor_binder:net_spawn(data)
  if not object_binder.net_spawn( self,data ) then
    return false
  end

  db.add_zone( self.object )

  -- Не регистрироваться на апдейты, если у объекта в customdata
  -- нет секции logic вообще.
  local char_ini = xr_logic.get_customdata_or_ini_file(self.object, "<customdata>")
  if char_ini:section_exist("logic") then
    updatable_binders[self.object:id()] = self
  end

  return true
end

function restrictor_binder:net_destroy()
  local st = db.storage[self.object:id()]
  if st.active_scheme then
    xr_logic.issue_event(self.object, st[st.active_scheme], "net_destroy")
  end

  db.del_zone( self.object )

  updatable_binders[self.object:id()] = nil

  db.storage[self.object:id()] = nil

  object_binder.net_destroy(self)
end

-- вызывается из обновления актёра!
function restrictor_binder:actor_update(delta)
  if not self.initialized and db.actor then
    self.initialized = true
    xr_logic.initialize_obj(self.object, self.st, self.loaded, db.actor, modules.stype_restrictor)
  end

  if self.st.active_section ~= nil then
    xr_logic.issue_event(self.object, self.st[self.st.active_scheme], "update", delta)
  end
end

function restrictor_binder:net_save_relevant()
  return true
end

function restrictor_binder:save(packet)
  object_binder.save(self, packet)

  xr_logic.save_obj(self.object, packet)
end

function restrictor_binder:load(reader)
  self.loaded = true

  printf( "restrictor_binder:load" )

  object_binder.load(self, reader)

  xr_logic.load_obj(self.object, reader)
end
