----------------------------------------------------------------------------------------------------
-- Walkers
----------------------------------------------------------------------------------------------------
-- Разработчик: Andrey Fidrya (Zmey) af@svitonline.com
----------------------------------------------------------------------------------------------------

function printf()
end

---------------------------------------------------------------------------------------------------------------------
class "evaluator_need_walker" (property_evaluator)

function evaluator_need_walker:__init(storage, name) super(nil, name)
    self.st = storage
end

function evaluator_need_walker:evaluate()
  if actor_need_help and actor_need_help.obj_id_in_team(self.object:id()) then
  -- Чтобы не пытался ходить по патрульным путям с другого уровня
    return false
  end
  return xr_logic.is_active(self.object, self.st)
end

----------------------------------------------------------------------------------------------------------------------
class "action_walker_activity" (action_base)

function action_walker_activity:__init (npc, action_name, storage) super(nil, action_name)
  self.st = storage
  self.move_mgr = db.storage[npc:id()].move_mgr
end

function action_walker_activity:initialize()
  action_base.initialize(self)
  self.object:set_desired_position()
  self.object:set_desired_direction()
  self:reset_scheme(nil, self.object)
end

function action_walker_activity:activate_scheme(loading, npc)
  self.st.signals = {}
  self:reset_scheme(loading, npc)
end

function action_walker_activity:reset_scheme(loading, npc)
  if self.st.path_walk_info == nil then     
    self.st.path_walk_info = utils.path_parse_waypoints(self.st.path_walk)
  end
  if self.st.path_look_info == nil then
    self.st.path_look_info = utils.path_parse_waypoints(self.st.path_look)
  end

  self.move_mgr:reset(self.st.path_walk, self.st.path_walk_info, self.st.path_look, self.st.path_look_info, self.st.team, self.st.suggested_state)
  xr_sound.set_sound(npc, self.st.sound_idle)
end

function action_walker_activity:execute()
  action_base.execute(self)

  self.move_mgr:update()
  if xr_logic.try_switch_to_another_section(self.object, self.st, db.actor) then
    return
  end
end

function action_walker_activity:finalize()
  self.move_mgr:finalize()
  xr_sound.set_sound(self.object, nil)
  action_base.finalize(self)
end

----------------------------------------------------------------------------------------------------------------------
--walker binder
----------------------------------------------------------------------------------------------------------------------
function add_to_binder(npc, ini, scheme, section, storage)
    printf("DEBUG: add_to_binder: scheme='%s', section='%s'", scheme, section)
      local operators = {}
      local properties  = {}

      local manager = npc:motivation_action_manager()

    properties["event"] = xr_evaluators_id.reaction
    properties["need_walker"] = xr_evaluators_id.zmey_walker_base + 1

    operators["action_walker"] = xr_actions_id.zmey_walker_base + 1

      -- // evaluators
    manager:add_evaluator(properties["need_walker"], this.evaluator_need_walker(storage, "walker_need"))

    local new_action = this.action_walker_activity(npc, "action_walker_activity", storage)
    new_action:add_precondition(world_property(stalker_ids.property_alive, true))
    new_action:add_precondition(world_property(stalker_ids.property_danger,false))
    new_action:add_precondition(world_property(stalker_ids.property_enemy, false))
    new_action:add_precondition(world_property(stalker_ids.property_anomaly,false))
    new_action:add_precondition(world_property(properties["need_walker"], true))
    xr_motivator.addCommonPrecondition(new_action)
      new_action:add_effect(world_property(properties["need_walker"], false))
    manager:add_action(operators["action_walker"], new_action)

    -- Зарегистрировать все actions, в которых должен быть вызван метод reset_scheme при изменении настроек схемы:
    xr_logic.subscribe_action_for_events(npc, storage, new_action)

      new_action = manager:action(xr_actions_id.alife)
      new_action:add_precondition(world_property(properties["need_walker"], false))
end

-- Включение схемы
--
-- enabled - включена ли схема
-- path_walk - основной путь, по которому ходит охранник
-- path_look [опционально] - путь, куда смотрит охранник
-- team [опционально] - команда для синхронизации
--
function set_scheme(npc, ini, scheme, section, gulag_name)
  printf("DEBUG: set_scheme: scheme='%s', section='%s'", scheme, section)
  local st = xr_logic.assign_storage_and_bind(npc, ini, scheme, section)
  printf("DEBUG: set_scheme: storage assigned")

  st.logic     = xr_logic.cfg_get_switch_conditions(ini, section, npc)

  st.path_walk = utils.cfg_get_string(ini, section, "path_walk", npc, true,  gulag_name)
  st.path_look = utils.cfg_get_string(ini, section, "path_look", npc, false, gulag_name)
  st.team      = utils.cfg_get_string(ini, section, "team",      npc, false, gulag_name)
  st.sound_idle= utils.cfg_get_string(ini, section, "sound_idle",npc, false, "")

  st.suggested_state = {}
  st.suggested_state.standing = utils.cfg_get_string(ini, section, "def_state_standing", npc, false, "")
  st.suggested_state.moving1 = utils.cfg_get_string(ini, section, "def_state_moving1", npc, false, "")
  st.suggested_state.moving2 = utils.cfg_get_string(ini, section, "def_state_moving2", npc, false, "")
  st.suggested_state.moving3 = utils.cfg_get_string(ini, section, "def_state_moving3", npc, false, "")

  st.path_walk_info = nil -- Будут инициализированы в reset(), сейчас пути могут быть еще
  st.path_look_info = nil -- не загружены.
end
