<commit_before>#include	"Action/MoveTo.hh"

/**
   Move to a location

   @param x,y           Coordinates of the destination
   @param allowApprox   If the bot should stop 1 tile before the destination
 */
Action::MoveTo::MoveTo(uint16_t x, uint16_t y, bool allowApprox)
  : _owInit(false), _approx(allowApprox), _tx(x), _ty(y), _tid(-1), _path(NULL)
{
  addListener("onFrame", &Action::MoveTo::_checkNPCMovement);
}

/**
   Move to a person

   @param tid           Index of the person in the person array
 */
Action::MoveTo::MoveTo(uint8_t tid)
  : _owInit(false), _approx(true), _tx(0), _ty(0), _tid(tid), _path(NULL)
{
  addListener("onFrame", &Action::MoveTo::_checkNPCMovement);
}

Action::MoveTo::~MoveTo()
{
}

void		Action::MoveTo::addListener(const std::string &signal, void (Action::MoveTo::*listener)())
{
  std::function<void (AAction *)> l = [listener](AAction *that){(static_cast<Action::MoveTo *>(that)->*listener)();};

  _listeners[signal]._cppListeners.push_back(l);
}

void		Action::MoveTo::_checkNPCMovement()
{
  const OverWorld       &p = _data.getOverWorld(0);
  const OverWorld	*ows = _data.getOverWorlds();

  // If a npc moved on the screen, recompute the path to avoid it
  for (int i = 1; _owInit && i < 16 && (ows[i].getMapId() || ows[i].getBankId()); i++)
    {
      if (ows[i].getBankId() == _data.getPlayer().getBankId() &&// If the overworld is in our map
	  ows[i].getMapId() == _data.getPlayer().getMapId() &&
	  (ows[i].getDestX() != _oldow[i].getDestX() ||		// If the overworld is moving
	   ows[i].getDestY() != _oldow[i].getDestY() ||
	   ows[i].getBankId() != _oldow[i].getBankId() ||		// Or if it's a new overworld
	   ows[i].getMapId() != _oldow[i].getMapId()))
	emit("onInit");
    }
  memcpy((void *) (_oldow + 1), (void *) (ows + 1), 15 * sizeof(OverWorld));
  _owInit = true;

  // If the bot was un-synchronized for some reason, reset it
  if (_path && _pathi > 0 && (abs(p.getDestX() - (*_path)[_pathi - 1]->getX()) > 0 && abs(p.getDestY() - (*_path)[_pathi - 1]->getY()) > 0))
      emit("onInit");
}

void		Action::MoveTo::_releaseKeys()
{
  for (uint8_t i = KEY_LEFT; i <= KEY_DOWN; i++)
    sdlSetButton((EKey) i, false);
}

void            Action::MoveTo::_updateTargetPos()
{
  if (_tid == -1)
    return;

  const OverWorld	*ows = _data.getOverWorlds();
  Player	&p = _data.getPlayer();
  World::Map    &m = _data.getWorld()[p.getBankId()][p.getMapId()];
  const World::PersonEvt &pers = m.getPerson(_tid);

  _tx = pers.getX();
  _ty = pers.getY();
  for (int i = 1; i < 16 && (ows[i].getMapId() || ows[i].getBankId()); i++)
    {
      if (ows[i].getEventNb() == pers.getEventNb())
        {
          _tx = ows[i].getDestX();
          _ty = ows[i].getDestY();
          return;
        }
    }
}

void            Action::MoveTo::_searchBehindBar()
{
  Player	&p = _data.getPlayer();
  World::Map    &m = _data.getWorld()[p.getBankId()][p.getMapId()];

  for (int i = 0; i < 4; i++)
    {
      uint32_t  x = _tx + !(i & 1) * (i - 1);
      uint32_t  y = _ty + (i & 1) * (i - 2);
      // Person is behind a bar
      if (x >= 0 && y >= 0 && x < m.getWidth() && y < m.getHeight() && m.getNode(x, y).getBehavior() == 0x80)
        {
          _tx = x;
          _ty = y;
          _tid = -1;
          _init();
          return;
        }
    }
}

void		Action::MoveTo::_init()<commit_msg>fix a bug when searching for a NPC after screen change<commit_after>#include	"Action/MoveTo.hh"

/**
   Move to a location

   @param x,y           Coordinates of the destination
   @param allowApprox   If the bot should stop 1 tile before the destination
 */
Action::MoveTo::MoveTo(uint16_t x, uint16_t y, bool allowApprox)
  : _owInit(false), _approx(allowApprox), _tx(x), _ty(y), _tid(-1), _path(NULL)
{
  addListener("onFrame", &Action::MoveTo::_checkNPCMovement);
}

/**
   Move to a person

   @param tid           Index of the person in the person array
 */
Action::MoveTo::MoveTo(uint8_t tid)
  : _owInit(false), _approx(true), _tx(0), _ty(0), _tid(tid), _path(NULL)
{
  addListener("onFrame", &Action::MoveTo::_checkNPCMovement);
}

Action::MoveTo::~MoveTo()
{
}

void		Action::MoveTo::addListener(const std::string &signal, void (Action::MoveTo::*listener)())
{
  std::function<void (AAction *)> l = [listener](AAction *that){(static_cast<Action::MoveTo *>(that)->*listener)();};

  _listeners[signal]._cppListeners.push_back(l);
}

void		Action::MoveTo::_checkNPCMovement()
{
  const OverWorld       &p = _data.getOverWorld(0);
  const OverWorld	*ows = _data.getOverWorlds();

  // If a npc moved on the screen, recompute the path to avoid it
  for (int i = 1; _owInit && i < 16 && (ows[i].getMapId() || ows[i].getBankId()); i++)
    {
      if (ows[i].getBankId() == _data.getPlayer().getBankId() &&// If the overworld is in our map
	  ows[i].getMapId() == _data.getPlayer().getMapId() &&
	  (ows[i].getDestX() != _oldow[i].getDestX() ||		// If the overworld is moving
	   ows[i].getDestY() != _oldow[i].getDestY() ||
	   ows[i].getBankId() != _oldow[i].getBankId() ||		// Or if it's a new overworld
	   ows[i].getMapId() != _oldow[i].getMapId()))
	emit("onInit");
    }
  memcpy((void *) (_oldow + 1), (void *) (ows + 1), 15 * sizeof(OverWorld));
  _owInit = true;

  // If the bot was un-synchronized for some reason, reset it
  if (_path && _pathi > 0 && (abs(p.getDestX() - (*_path)[_pathi - 1]->getX()) > 0 && abs(p.getDestY() - (*_path)[_pathi - 1]->getY()) > 0))
      emit("onInit");
}

void		Action::MoveTo::_releaseKeys()
{
  for (uint8_t i = KEY_LEFT; i <= KEY_DOWN; i++)
    sdlSetButton((EKey) i, false);
}

void            Action::MoveTo::_updateTargetPos()
{
  if (_tid == -1)
    return;

  const OverWorld	*ows = _data.getOverWorlds();
  Player	&p = _data.getPlayer();
  World::Map    &m = _data.getWorld()[p.getBankId()][p.getMapId()];
  const World::PersonEvt &pers = m.getPerson(_tid);

  // Check loaded NPCs to get the target's position
  for (int i = 1; i < 16 && (ows[i].getMapId() || ows[i].getBankId()); i++)
    {
      if (ows[i].getMapId() == p.getMapId() && ows[i].getBankId() == p.getBankId() &&
          ows[i].getEventNb() == pers.getEventNb())
        {
          _tx = ows[i].getDestX();
          _ty = ows[i].getDestY();
          return;
        }
    }
  // Default location of the NPC
  _tx = pers.getX();
  _ty = pers.getY();
}

void            Action::MoveTo::_searchBehindBar()
{
  Player	&p = _data.getPlayer();
  World::Map    &m = _data.getWorld()[p.getBankId()][p.getMapId()];

  for (int i = 0; i < 4; i++)
    {
      uint32_t  x = _tx + !(i & 1) * (i - 1);
      uint32_t  y = _ty + (i & 1) * (i - 2);
      // Person is behind a bar
      if (x >= 0 && y >= 0 && x < m.getWidth() && y < m.getHeight() && m.getNode(x, y).getBehavior() == 0x80)
        {
          _tx = x;
          _ty = y;
          _tid = -1;
          _init();
          return;
        }
    }
}

void		Action::MoveTo::_init()
{'fraction_non_alphanumeric': 0.19204052098408103, 'fraction_numerical': 0.012445730824891461, 'mean_word_length': 3.0415204678362575, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3671626', 'n_tokens_mistral': 2708, 'n_tokens_neox': 2626, 'n_words': 592}