Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move codes from std::bind to lambda expressions #2475

Merged
merged 14 commits into from
Mar 22, 2024
6 changes: 5 additions & 1 deletion src/creatures/combat/condition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2041,7 +2041,11 @@ bool ConditionFeared::executeCondition(std::shared_ptr<Creature> creature, int32
}

if (getFleePath(creature, currentPos, listDir)) {
g_dispatcher().addEvent(std::bind(&Game::forcePlayerAutoWalk, &g_game(), creature->getID(), listDir.data()), "ConditionFeared::executeCondition");
g_dispatcher().addEvent([id = creature->getID(), listDir = listDir.data()] {
g_game().forcePlayerAutoWalk(id, listDir);
},
"ConditionFeared::executeCondition");

g_logger().debug("[ConditionFeared::executeCondition] Walking Scheduled");
}
}
Expand Down
22 changes: 10 additions & 12 deletions src/creatures/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ void Creature::addEventWalk(bool firstStep) {
}

self->eventWalk = g_dispatcher().scheduleEvent(
static_cast<uint32_t>(ticks), std::bind(&Game::checkCreatureWalk, &g_game(), self->getID()),
"Creature::checkCreatureWalk"
static_cast<uint32_t>(ticks),
[creatureId = self->getID()] { g_game().checkCreatureWalk(creatureId); }, "Creature::checkCreatureWalk"
);
});
}
Expand Down Expand Up @@ -600,7 +600,7 @@ void Creature::onCreatureMove(const std::shared_ptr<Creature> &creature, const s
if (followCreature && (creature == getCreature() || creature == followCreature)) {
if (hasFollowPath) {
isUpdatingPath = true;
g_dispatcher().addEvent(std::bind(&Game::updateCreatureWalk, &g_game(), getID()), "Game::updateCreatureWalk");
g_dispatcher().addEvent([creatureId = getID()] { g_game().updateCreatureWalk(creatureId); }, "Game::updateCreatureWalk");
}

if (newPos.z != oldPos.z || !canSee(followCreature->getPosition())) {
Expand All @@ -615,7 +615,7 @@ void Creature::onCreatureMove(const std::shared_ptr<Creature> &creature, const s
} else {
if (hasExtraSwing()) {
// our target is moving lets see if we can get in hit
g_dispatcher().addEvent(std::bind(&Game::checkCreatureAttack, &g_game(), getID()), "Game::checkCreatureAttack");
g_dispatcher().addEvent([creatureId = getID()] { g_game().checkCreatureAttack(creatureId); }, "Game::checkCreatureAttack");
}

if (newTile->getZoneType() != oldTile->getZoneType()) {
Expand Down Expand Up @@ -814,10 +814,10 @@ bool Creature::dropCorpse(std::shared_ptr<Creature> lastHitCreature, std::shared
}

if (player->checkAutoLoot(monster->isRewardBoss()) && corpseContainer && mostDamageCreature->getPlayer()) {
g_dispatcher().addEvent(
std::bind(&Game::playerQuickLootCorpse, &g_game(), player, corpseContainer, corpse->getPosition()),
"Game::playerQuickLootCorpse"
);
g_dispatcher().addEvent([player, corpseContainer, corpsePosition = corpse->getPosition()] {
g_game().playerQuickLootCorpse(player, corpseContainer, corpsePosition);
},
"Game::playerQuickLootCorpse");
}
}
}
Expand Down Expand Up @@ -861,7 +861,7 @@ void Creature::changeHealth(int32_t healthChange, bool sendHealthChange /* = tru
g_game().addCreatureHealth(static_self_cast<Creature>());
}
if (health <= 0) {
g_dispatcher().addEvent(std::bind(&Game::executeDeath, &g_game(), getID()), "Game::executeDeath");
g_dispatcher().addEvent([creatureId = getID()] { g_game().executeDeath(creatureId); }, "Game::executeDeath");
}
}

Expand Down Expand Up @@ -1401,9 +1401,7 @@ void Creature::removeCondition(ConditionType_t conditionType, ConditionId_t cond
int32_t walkDelay = getWalkDelay();
if (walkDelay > 0) {
g_dispatcher().scheduleEvent(
walkDelay,
std::bind(&Game::forceRemoveCondition, &g_game(), getID(), conditionType, conditionId),
"Game::forceRemoveCondition"
walkDelay, [creatureId = getID(), conditionType, conditionId] { g_game().forceRemoveCondition(creatureId, conditionType, conditionId); }, "Game::forceRemoveCondition"
);
return;
}
Expand Down
4 changes: 3 additions & 1 deletion src/creatures/interactions/chat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ bool ChatChannel::addUser(const std::shared_ptr<Player> &player) {
if (id == CHANNEL_GUILD) {
const auto guild = player->getGuild();
if (guild && !guild->getMotd().empty()) {
g_dispatcher().scheduleEvent(150, std::bind(&Game::sendGuildMotd, &g_game(), player->getID()), "Game::sendGuildMotd");
g_dispatcher().scheduleEvent(
150, [playerId = player->getID()] { g_game().sendGuildMotd(playerId); }, "Game::sendGuildMotd"
);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/creatures/monsters/monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ bool Monster::selectTarget(const std::shared_ptr<Creature> &creature) {

if (isHostile() || isSummon()) {
if (setAttackedCreature(creature)) {
g_dispatcher().addEvent(std::bind(&Game::checkCreatureAttack, &g_game(), getID()), "Game::checkCreatureAttack");
g_dispatcher().addEvent([creatureId = getID()] { g_game().checkCreatureAttack(creatureId); }, "Game::checkCreatureAttack");
}
}
return setFollowCreature(creature);
Expand Down
14 changes: 10 additions & 4 deletions src/creatures/monsters/spawns/spawn_monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ bool SpawnsMonster::isInZone(const Position &centerPos, int32_t radius, const Po

void SpawnMonster::startSpawnMonsterCheck() {
if (checkSpawnMonsterEvent == 0) {
checkSpawnMonsterEvent = g_dispatcher().scheduleEvent(getInterval(), std::bind(&SpawnMonster::checkSpawnMonster, this), "SpawnMonster::checkSpawnMonster");
checkSpawnMonsterEvent = g_dispatcher().scheduleEvent(
getInterval(), [this] { checkSpawnMonster(); }, "SpawnMonster::checkSpawnMonster"
);
}
}

Expand Down Expand Up @@ -225,7 +227,7 @@ void SpawnMonster::startup(bool delayed) {
continue;
}
if (delayed) {
g_dispatcher().addEvent(std::bind(&SpawnMonster::scheduleSpawn, this, spawnMonsterId, sb, mType, 0, true), "SpawnMonster::startup");
g_dispatcher().addEvent([this, spawnMonsterId, &sb, mType] { scheduleSpawn(spawnMonsterId, sb, mType, 0, true); }, "SpawnMonster::startup");
} else {
scheduleSpawn(spawnMonsterId, sb, mType, 0, true);
}
Expand Down Expand Up @@ -265,7 +267,9 @@ void SpawnMonster::checkSpawnMonster() {
}

if (spawnedMonsterMap.size() < spawnMonsterMap.size()) {
checkSpawnMonsterEvent = g_dispatcher().scheduleEvent(getInterval(), std::bind(&SpawnMonster::checkSpawnMonster, this), "SpawnMonster::checkSpawnMonster");
checkSpawnMonsterEvent = g_dispatcher().scheduleEvent(
getInterval(), [this] { checkSpawnMonster(); }, "SpawnMonster::checkSpawnMonster"
);
}
}

Expand All @@ -274,7 +278,9 @@ void SpawnMonster::scheduleSpawn(uint32_t spawnMonsterId, spawnBlock_t &sb, cons
spawnMonster(spawnMonsterId, sb, mType, startup);
} else {
g_game().addMagicEffect(sb.pos, CONST_ME_TELEPORT);
g_dispatcher().scheduleEvent(NONBLOCKABLE_SPAWN_MONSTER_INTERVAL, std::bind(&SpawnMonster::scheduleSpawn, this, spawnMonsterId, sb, mType, interval - NONBLOCKABLE_SPAWN_MONSTER_INTERVAL, startup), "SpawnMonster::scheduleSpawn");
g_dispatcher().scheduleEvent(
NONBLOCKABLE_SPAWN_MONSTER_INTERVAL, [=, this, &sb] { scheduleSpawn(spawnMonsterId, sb, mType, interval - NONBLOCKABLE_SPAWN_MONSTER_INTERVAL, startup); }, "SpawnMonster::scheduleSpawn"
);
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/creatures/npcs/npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,9 @@ void Npc::onPlayerSellAllLoot(uint32_t playerId, uint16_t itemId, bool ignore, u
return;
}
if (hasMore) {
g_dispatcher().scheduleEvent(SCHEDULER_MINTICKS, std::bind(&Npc::onPlayerSellAllLoot, this, player->getID(), itemId, ignore, totalPrice), __FUNCTION__);
g_dispatcher().scheduleEvent(
SCHEDULER_MINTICKS, [this, playerId = player->getID(), itemId, ignore, totalPrice] { onPlayerSellAllLoot(playerId, itemId, ignore, totalPrice); }, __FUNCTION__
);
return;
}
ss << "You sold all of the items from your loot pouch for ";
Expand All @@ -366,7 +368,9 @@ void Npc::onPlayerSellItem(std::shared_ptr<Player> player, uint16_t itemId, uint
return;
}
if (itemId == ITEM_GOLD_POUCH) {
g_dispatcher().scheduleEvent(SCHEDULER_MINTICKS, std::bind(&Npc::onPlayerSellAllLoot, this, player->getID(), itemId, ignore, 0), __FUNCTION__);
g_dispatcher().scheduleEvent(
SCHEDULER_MINTICKS, [this, playerId = player->getID(), itemId, ignore] { onPlayerSellAllLoot(playerId, itemId, ignore, 0); }, __FUNCTION__
);
return;
}

Expand Down
12 changes: 9 additions & 3 deletions src/creatures/npcs/spawns/spawn_npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ bool SpawnsNpc::isInZone(const Position &centerPos, int32_t radius, const Positi

void SpawnNpc::startSpawnNpcCheck() {
if (checkSpawnNpcEvent == 0) {
checkSpawnNpcEvent = g_dispatcher().scheduleEvent(getInterval(), std::bind(&SpawnNpc::checkSpawnNpc, this), "SpawnNpc::checkSpawnNpc");
checkSpawnNpcEvent = g_dispatcher().scheduleEvent(
getInterval(), [this] { checkSpawnNpc(); }, "SpawnNpc::checkSpawnNpc"
);
}
}

Expand Down Expand Up @@ -217,7 +219,9 @@ void SpawnNpc::checkSpawnNpc() {
}

if (spawnedNpcMap.size() < spawnNpcMap.size()) {
checkSpawnNpcEvent = g_dispatcher().scheduleEvent(getInterval(), std::bind(&SpawnNpc::checkSpawnNpc, this), __FUNCTION__);
checkSpawnNpcEvent = g_dispatcher().scheduleEvent(
getInterval(), [this] { checkSpawnNpc(); }, __FUNCTION__
);
}
}

Expand All @@ -226,7 +230,9 @@ void SpawnNpc::scheduleSpawnNpc(uint32_t spawnId, spawnBlockNpc_t &sb, uint16_t
spawnNpc(spawnId, sb.npcType, sb.pos, sb.direction);
} else {
g_game().addMagicEffect(sb.pos, CONST_ME_TELEPORT);
g_dispatcher().scheduleEvent(1400, std::bind(&SpawnNpc::scheduleSpawnNpc, this, spawnId, sb, interval - NONBLOCKABLE_SPAWN_NPC_INTERVAL), __FUNCTION__);
g_dispatcher().scheduleEvent(
1400, [=, this, &sb] { scheduleSpawnNpc(spawnId, sb, interval - NONBLOCKABLE_SPAWN_NPC_INTERVAL); }, __FUNCTION__
);
}
}

Expand Down
14 changes: 9 additions & 5 deletions src/creatures/players/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1934,7 +1934,7 @@ void Player::onCreatureMove(const std::shared_ptr<Creature> &creature, const std
const auto &followCreature = getFollowCreature();
if (hasFollowPath && (creature == followCreature || (creature.get() == this && followCreature))) {
isUpdatingPath = false;
g_dispatcher().addEvent(std::bind(&Game::updateCreatureWalk, &g_game(), getID()), "Game::updateCreatureWalk");
g_dispatcher().addEvent([creatureId = getID()] { g_game().updateCreatureWalk(creatureId); }, "Game::updateCreatureWalk");
}

if (creature != getPlayer()) {
Expand Down Expand Up @@ -4245,7 +4245,7 @@ bool Player::updateSaleShopList(std::shared_ptr<Item> item) {
return true;
}

g_dispatcher().addEvent(std::bind(&Game::updatePlayerSaleItems, &g_game(), getID()), "updatePlayerSaleItems");
g_dispatcher().addEvent([creatureId = getID()] { g_game().updatePlayerSaleItems(creatureId); }, "Game::updatePlayerSaleItems");
scheduledSaleUpdate = true;
return true;
}
Expand Down Expand Up @@ -4316,7 +4316,7 @@ bool Player::setAttackedCreature(std::shared_ptr<Creature> creature) {
}

if (creature) {
g_dispatcher().addEvent(std::bind(&Game::checkCreatureAttack, &g_game(), getID()), "Game::checkCreatureAttack");
g_dispatcher().addEvent([creatureId = getID()] { g_game().checkCreatureAttack(creatureId); }, "Game::checkCreatureAttack");
}
return true;
}
Expand Down Expand Up @@ -4376,7 +4376,11 @@ void Player::doAttacking(uint32_t) {
result = Weapon::useFist(static_self_cast<Player>(), attackedCreature);
}

std::shared_ptr<Task> task = createPlayerTask(std::max<uint32_t>(SCHEDULER_MINTICKS, delay), std::bind(&Game::checkCreatureAttack, &g_game(), getID()), "Game::checkCreatureAttack");
const auto &task = createPlayerTask(
std::max<uint32_t>(SCHEDULER_MINTICKS, delay),
[playerId = getID()] { g_game().checkCreatureAttack(playerId); }, "Game::checkCreatureAttack"
);

if (!classicSpeed) {
setNextActionTask(task, false);
} else {
Expand Down Expand Up @@ -7776,7 +7780,7 @@ bool Player::canAutoWalk(const Position &toPosition, const std::function<void()>
// Check if can walk to the toPosition and send event to use function
stdext::arraylist<Direction> listDir(128);
if (getPathTo(toPosition, listDir, 0, 1, true, true)) {
g_dispatcher().addEvent(std::bind(&Game::playerAutoWalk, &g_game(), getID(), listDir.data()), __FUNCTION__);
g_dispatcher().addEvent([creatureId = getID(), dirs = listDir.data()] { g_game().playerAutoWalk(creatureId, dirs); }, __FUNCTION__);

std::shared_ptr<Task> task = createPlayerTask(delay, function, __FUNCTION__);
setNextWalkActionTask(task);
Expand Down
Loading
Loading