From 685e3afd3635525d342d33e4bd8578b6898bf9d2 Mon Sep 17 00:00:00 2001 From: Renato Machado Date: Fri, 22 Mar 2024 18:21:27 -0300 Subject: [PATCH 1/6] refactor: move codes from std::bind to lambda expressions (#2475) This modernizes our codebase by transitioning from std::bind to lambda expressions, aligning with modern C++ best practices to enhance readability, efficiency, and maintainability. This refactor simplifies syntax, taps into the compiler's ability to better optimize lambda expressions, and contributes to a cleaner, more straightforward code structure. Lambda expressions, being a pivotal feature of C++11 and beyond, offer improved code conciseness, performance benefits due to more effective compiler optimizations, and increased flexibility in handling local variable captures and inline code. By embracing these modern C++ features, we aim to bolster the quality and future development of our project, and we welcome the community's ongoing feedback and contributions to these efforts. --- src/creatures/combat/condition.cpp | 6 +- src/creatures/creature.cpp | 22 +- src/creatures/interactions/chat.cpp | 4 +- src/creatures/monsters/monster.cpp | 2 +- .../monsters/spawns/spawn_monster.cpp | 14 +- src/creatures/npcs/npc.cpp | 8 +- src/creatures/npcs/spawns/spawn_npc.cpp | 12 +- src/creatures/players/player.cpp | 14 +- src/game/game.cpp | 213 ++++++++---- src/items/bed.cpp | 4 +- src/items/decay/decay.cpp | 12 +- src/lua/creature/raids.cpp | 16 +- .../functions/core/game/global_functions.cpp | 2 +- src/lua/global/globalevent.cpp | 12 +- src/server/network/connection/connection.cpp | 39 ++- src/server/network/message/outputmessage.cpp | 5 +- src/server/network/protocol/protocolgame.cpp | 309 +++++++++--------- src/server/network/protocol/protocolgame.hpp | 6 - src/server/network/protocol/protocollogin.cpp | 6 +- .../network/protocol/protocolstatus.cpp | 11 +- src/server/server.cpp | 17 +- 21 files changed, 438 insertions(+), 296 deletions(-) diff --git a/src/creatures/combat/condition.cpp b/src/creatures/combat/condition.cpp index d54a3deb1d0..9d0f6962884 100644 --- a/src/creatures/combat/condition.cpp +++ b/src/creatures/combat/condition.cpp @@ -2041,7 +2041,11 @@ bool ConditionFeared::executeCondition(std::shared_ptr 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"); } } diff --git a/src/creatures/creature.cpp b/src/creatures/creature.cpp index d17ee454402..6ee605769a7 100644 --- a/src/creatures/creature.cpp +++ b/src/creatures/creature.cpp @@ -258,8 +258,8 @@ void Creature::addEventWalk(bool firstStep) { } self->eventWalk = g_dispatcher().scheduleEvent( - static_cast(ticks), std::bind(&Game::checkCreatureWalk, &g_game(), self->getID()), - "Creature::checkCreatureWalk" + static_cast(ticks), + [creatureId = self->getID()] { g_game().checkCreatureWalk(creatureId); }, "Creature::checkCreatureWalk" ); }); } @@ -600,7 +600,7 @@ void Creature::onCreatureMove(const std::shared_ptr &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())) { @@ -615,7 +615,7 @@ void Creature::onCreatureMove(const std::shared_ptr &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()) { @@ -814,10 +814,10 @@ bool Creature::dropCorpse(std::shared_ptr 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"); } } } @@ -861,7 +861,7 @@ void Creature::changeHealth(int32_t healthChange, bool sendHealthChange /* = tru g_game().addCreatureHealth(static_self_cast()); } 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"); } } @@ -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; } diff --git a/src/creatures/interactions/chat.cpp b/src/creatures/interactions/chat.cpp index f30c3f219e3..e16af670fcc 100644 --- a/src/creatures/interactions/chat.cpp +++ b/src/creatures/interactions/chat.cpp @@ -81,7 +81,9 @@ bool ChatChannel::addUser(const std::shared_ptr &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" + ); } } diff --git a/src/creatures/monsters/monster.cpp b/src/creatures/monsters/monster.cpp index 5f31c67aa72..f15616d6af5 100644 --- a/src/creatures/monsters/monster.cpp +++ b/src/creatures/monsters/monster.cpp @@ -668,7 +668,7 @@ bool Monster::selectTarget(const std::shared_ptr &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); diff --git a/src/creatures/monsters/spawns/spawn_monster.cpp b/src/creatures/monsters/spawns/spawn_monster.cpp index aec222d723a..5d9f386e25f 100644 --- a/src/creatures/monsters/spawns/spawn_monster.cpp +++ b/src/creatures/monsters/spawns/spawn_monster.cpp @@ -141,7 +141,9 @@ bool SpawnsMonster::isInZone(const Position ¢erPos, 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" + ); } } @@ -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); } @@ -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" + ); } } @@ -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" + ); } } diff --git a/src/creatures/npcs/npc.cpp b/src/creatures/npcs/npc.cpp index 0f9f60cec9b..d347ec6b66c 100644 --- a/src/creatures/npcs/npc.cpp +++ b/src/creatures/npcs/npc.cpp @@ -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 "; @@ -366,7 +368,9 @@ void Npc::onPlayerSellItem(std::shared_ptr 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; } diff --git a/src/creatures/npcs/spawns/spawn_npc.cpp b/src/creatures/npcs/spawns/spawn_npc.cpp index 7fd99d6c37f..968deca5331 100644 --- a/src/creatures/npcs/spawns/spawn_npc.cpp +++ b/src/creatures/npcs/spawns/spawn_npc.cpp @@ -131,7 +131,9 @@ bool SpawnsNpc::isInZone(const Position ¢erPos, 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" + ); } } @@ -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__ + ); } } @@ -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__ + ); } } diff --git a/src/creatures/players/player.cpp b/src/creatures/players/player.cpp index 874089e5602..76ca1755c70 100644 --- a/src/creatures/players/player.cpp +++ b/src/creatures/players/player.cpp @@ -1934,7 +1934,7 @@ void Player::onCreatureMove(const std::shared_ptr &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()) { @@ -4245,7 +4245,7 @@ bool Player::updateSaleShopList(std::shared_ptr 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; } @@ -4316,7 +4316,7 @@ bool Player::setAttackedCreature(std::shared_ptr 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; } @@ -4376,7 +4376,11 @@ void Player::doAttacking(uint32_t) { result = Weapon::useFist(static_self_cast(), attackedCreature); } - std::shared_ptr task = createPlayerTask(std::max(SCHEDULER_MINTICKS, delay), std::bind(&Game::checkCreatureAttack, &g_game(), getID()), "Game::checkCreatureAttack"); + const auto &task = createPlayerTask( + std::max(SCHEDULER_MINTICKS, delay), + [playerId = getID()] { g_game().checkCreatureAttack(playerId); }, "Game::checkCreatureAttack" + ); + if (!classicSpeed) { setNextActionTask(task, false); } else { @@ -7776,7 +7780,7 @@ bool Player::canAutoWalk(const Position &toPosition, const std::function // Check if can walk to the toPosition and send event to use function stdext::arraylist 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 = createPlayerTask(delay, function, __FUNCTION__); setNextWalkActionTask(task); diff --git a/src/game/game.cpp b/src/game/game.cpp index 7adaf196dff..c69b481e32c 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -342,17 +342,30 @@ void Game::start(ServiceManager* manager) { int minutes = tms->tm_min; lightHour = (minutes * LIGHT_DAY_LENGTH) / 60; - g_dispatcher().scheduleEvent(EVENT_MS + 1000, std::bind_front(&Game::createFiendishMonsters, this), "Game::createFiendishMonsters"); - g_dispatcher().scheduleEvent(EVENT_MS + 1000, std::bind_front(&Game::createInfluencedMonsters, this), "Game::createInfluencedMonsters"); - - g_dispatcher().cycleEvent(EVENT_MS, std::bind_front(&Game::updateForgeableMonsters, this), "Game::updateForgeableMonsters"); - g_dispatcher().cycleEvent(EVENT_LIGHTINTERVAL_MS, std::bind(&Game::checkLight, this), "Game::checkLight"); - g_dispatcher().cycleEvent(EVENT_CHECK_CREATURE_INTERVAL, std::bind(&Game::checkCreatures, this), "Game::checkCreatures"); - g_dispatcher().cycleEvent(EVENT_IMBUEMENT_INTERVAL, std::bind(&Game::checkImbuements, this), "Game::checkImbuements"); + g_dispatcher().scheduleEvent( + EVENT_MS + 1000, [this] { createFiendishMonsters(); }, "Game::createFiendishMonsters" + ); + g_dispatcher().scheduleEvent( + EVENT_MS + 1000, [this] { createInfluencedMonsters(); }, "Game::createInfluencedMonsters" + ); + g_dispatcher().cycleEvent( + EVENT_MS, [this] { updateForgeableMonsters(); }, "Game::updateForgeableMonsters" + ); + g_dispatcher().cycleEvent( + EVENT_LIGHTINTERVAL_MS, [this] { checkLight(); }, "Game::checkLight" + ); + g_dispatcher().cycleEvent( + EVENT_CHECK_CREATURE_INTERVAL, [this] { checkCreatures(); }, "Game::checkCreatures" + ); + g_dispatcher().cycleEvent( + EVENT_IMBUEMENT_INTERVAL, [this] { checkImbuements(); }, "Game::checkImbuements" + ); g_dispatcher().cycleEvent( EVENT_LUA_GARBAGE_COLLECTION, [this] { g_luaEnvironment().collectGarbage(); }, "Calling GC" ); - g_dispatcher().cycleEvent(EVENT_REFRESH_MARKET_PRICES, std::bind_front(&Game::loadItemsPrice, this), "Game::loadItemsPrice"); + g_dispatcher().cycleEvent( + EVENT_REFRESH_MARKET_PRICES, [this] { loadItemsPrice(); }, "Game::loadItemsPrice" + ); } GameState_t Game::getGameState() const { @@ -418,7 +431,7 @@ void Game::setGameState(GameState_t newState) { saveMotdNum(); g_saveManager().saveAll(); - g_dispatcher().addEvent(std::bind(&Game::shutdown, this), "Game::shutdown"); + g_dispatcher().addEvent([this] { shutdown(); }, "Game::shutdown"); break; } @@ -1165,9 +1178,11 @@ void Game::playerMoveThing(uint32_t playerId, const Position &fromPos, uint16_t } if (Position::areInRange<1, 1, 0>(movingCreature->getPosition(), player->getPosition())) { - std::shared_ptr task = createPlayerTask( + const auto &task = createPlayerTask( g_configManager().getNumber(PUSH_DELAY, __FUNCTION__), - std::bind(&Game::playerMoveCreatureByID, this, player->getID(), movingCreature->getID(), movingCreature->getPosition(), tile->getPosition()), + [this, player, movingCreature, tile] { + playerMoveCreatureByID(player->getID(), movingCreature->getID(), movingCreature->getPosition(), tile->getPosition()); + }, "Game::playerMoveCreatureByID" ); player->setNextActionPushTask(task); @@ -1208,8 +1223,9 @@ void Game::playerMoveCreatureByID(uint32_t playerId, uint32_t movingCreatureId, void Game::playerMoveCreature(std::shared_ptr player, std::shared_ptr movingCreature, const Position &movingCreatureOrigPos, std::shared_ptr toTile) { metrics::method_latency measure(__METHOD_NAME__); if (!player->canDoAction()) { - uint32_t delay = 600; - std::shared_ptr task = createPlayerTask(delay, std::bind(&Game::playerMoveCreatureByID, this, player->getID(), movingCreature->getID(), movingCreatureOrigPos, toTile->getPosition()), "Game::playerMoveCreatureByID"); + const auto &task = createPlayerTask( + 600, [this, player, movingCreature, toTile, movingCreatureOrigPos] { playerMoveCreatureByID(player->getID(), movingCreature->getID(), movingCreatureOrigPos, toTile->getPosition()); }, "Game::playerMoveCreatureByID" + ); player->setNextActionPushTask(task); return; @@ -1221,10 +1237,10 @@ void Game::playerMoveCreature(std::shared_ptr player, std::shared_ptr listDir(128); if (player->getPathTo(movingCreatureOrigPos, listDir, 0, 1, true, true)) { - g_dispatcher().addEvent(std::bind(&Game::playerAutoWalk, this, player->getID(), listDir.data()), "Game::playerAutoWalk"); - - std::shared_ptr task = createPlayerTask(600, std::bind(&Game::playerMoveCreatureByID, this, player->getID(), movingCreature->getID(), movingCreatureOrigPos, toTile->getPosition()), "Game::playerMoveCreatureByID"); - + g_dispatcher().addEvent([this, playerId = player->getID(), listDir = listDir.data()] { playerAutoWalk(playerId, listDir); }, "Game::playerAutoWalk"); + const auto &task = createPlayerTask( + 600, [this, player, movingCreature, toTile, movingCreatureOrigPos] { playerMoveCreatureByID(player->getID(), movingCreature->getID(), movingCreatureOrigPos, toTile->getPosition()); }, "Game::playerMoveCreatureByID" + ); player->pushEvent(true); player->setNextActionPushTask(task); } else { @@ -1424,7 +1440,12 @@ void Game::playerMoveItemByPlayerID(uint32_t playerId, const Position &fromPos, void Game::playerMoveItem(std::shared_ptr player, const Position &fromPos, uint16_t itemId, uint8_t fromStackPos, const Position &toPos, uint8_t count, std::shared_ptr item, std::shared_ptr toCylinder) { if (!player->canDoAction()) { uint32_t delay = player->getNextActionTime(); - std::shared_ptr task = createPlayerTask(delay, std::bind(&Game::playerMoveItemByPlayerID, this, player->getID(), fromPos, itemId, fromStackPos, toPos, count), "Game::playerMoveItemByPlayerID"); + std::shared_ptr task = createPlayerTask( + delay, [this, playerId = player->getID(), fromPos, itemId, fromStackPos, toPos, count] { + playerMoveItemByPlayerID(playerId, fromPos, itemId, fromStackPos, toPos, count); + }, + "Game::playerMoveItemByPlayerID" + ); player->setNextActionTask(task); return; } @@ -1519,9 +1540,14 @@ void Game::playerMoveItem(std::shared_ptr player, const Position &fromPo // need to walk to the item first before using it stdext::arraylist listDir(128); if (player->getPathTo(item->getPosition(), listDir, 0, 1, true, true)) { - g_dispatcher().addEvent(std::bind(&Game::playerAutoWalk, this, player->getID(), listDir.data()), "Game::playerAutoWalk"); + g_dispatcher().addEvent([this, playerId = player->getID(), listDir = listDir.data()] { playerAutoWalk(playerId, listDir); }, "Game::playerAutoWalk"); - std::shared_ptr task = createPlayerTask(400, std::bind(&Game::playerMoveItemByPlayerID, this, player->getID(), fromPos, itemId, fromStackPos, toPos, count), "Game::playerMoveItemByPlayerID"); + std::shared_ptr task = createPlayerTask( + 400, [this, playerId = player->getID(), fromPos, itemId, fromStackPos, toPos, count] { + playerMoveItemByPlayerID(playerId, fromPos, itemId, fromStackPos, toPos, count); + }, + "Game::playerMoveItemByPlayerID" + ); player->setNextWalkActionTask(task); } else { player->sendCancelMessage(RETURNVALUE_THEREISNOWAY); @@ -1576,9 +1602,14 @@ void Game::playerMoveItem(std::shared_ptr player, const Position &fromPo stdext::arraylist listDir(128); if (player->getPathTo(walkPos, listDir, 0, 0, true, true)) { - g_dispatcher().addEvent(std::bind(&Game::playerAutoWalk, this, player->getID(), listDir.data()), "Game::playerAutoWalk"); - - std::shared_ptr task = createPlayerTask(400, std::bind(&Game::playerMoveItemByPlayerID, this, player->getID(), itemPos, itemId, itemStackPos, toPos, count), "Game::playerMoveItemByPlayerID"); + g_dispatcher().addEvent([this, playerId = player->getID(), listDir = listDir.data()] { playerAutoWalk(playerId, listDir); }, "Game::playerAutoWalk"); + + std::shared_ptr task = createPlayerTask( + 400, [this, playerId = player->getID(), itemPos, itemId, itemStackPos, toPos, count] { + playerMoveItemByPlayerID(playerId, itemPos, itemId, itemStackPos, toPos, count); + }, + "Game::playerMoveItemByPlayerID" + ); player->setNextWalkActionTask(task); } else { player->sendCancelMessage(RETURNVALUE_THEREISNOWAY); @@ -3484,9 +3515,11 @@ void Game::playerUseItemEx(uint32_t playerId, const Position &fromPos, uint8_t f stdext::arraylist listDir(128); if (player->getPathTo(walkToPos, listDir, 0, 1, true, true)) { - g_dispatcher().addEvent(std::bind(&Game::playerAutoWalk, this, player->getID(), listDir.data()), "Game::playerAutoWalk"); + g_dispatcher().addEvent([this, playerId = player->getID(), listDir = listDir.data()] { playerAutoWalk(playerId, listDir); }, "Game::playerAutoWalk"); - std::shared_ptr task = createPlayerTask(400, std::bind(&Game::playerUseItemEx, this, playerId, itemPos, itemStackPos, fromItemId, toPos, toStackPos, toItemId), "Game::playerUseItemEx"); + std::shared_ptr task = createPlayerTask( + 400, [this, playerId, itemPos, itemStackPos, fromItemId, toPos, toStackPos, toItemId] { playerUseItemEx(playerId, itemPos, itemStackPos, fromItemId, toPos, toStackPos, toItemId); }, "Game::playerUseItemEx" + ); if (it.isRune() || it.type == ITEM_TYPE_POTION) { player->setNextPotionActionTask(task); } else { @@ -3512,7 +3545,9 @@ void Game::playerUseItemEx(uint32_t playerId, const Position &fromPos, uint8_t f if (it.isRune() || it.type == ITEM_TYPE_POTION) { delay = player->getNextPotionActionTime(); } - std::shared_ptr task = createPlayerTask(delay, std::bind(&Game::playerUseItemEx, this, playerId, fromPos, fromStackPos, fromItemId, toPos, toStackPos, toItemId), "Game::playerUseItemEx"); + std::shared_ptr task = createPlayerTask( + delay, [this, playerId, fromPos, fromStackPos, fromItemId, toPos, toStackPos, toItemId] { playerUseItemEx(playerId, fromPos, fromStackPos, fromItemId, toPos, toStackPos, toItemId); }, "Game::playerUseItemEx" + ); if (it.isRune() || it.type == ITEM_TYPE_POTION) { player->setNextPotionActionTask(task); } else { @@ -3594,9 +3629,11 @@ void Game::playerUseItem(uint32_t playerId, const Position &pos, uint8_t stackPo if (ret == RETURNVALUE_TOOFARAWAY) { stdext::arraylist listDir(128); if (player->getPathTo(pos, listDir, 0, 1, true, true)) { - g_dispatcher().addEvent(std::bind(&Game::playerAutoWalk, this, player->getID(), listDir.data()), "Game::playerAutoWalk"); + g_dispatcher().addEvent([this, playerId = player->getID(), listDir = listDir.data()] { playerAutoWalk(playerId, listDir); }, "Game::playerAutoWalk"); - std::shared_ptr task = createPlayerTask(400, std::bind(&Game::playerUseItem, this, playerId, pos, stackPos, index, itemId), "Game::playerUseItem"); + std::shared_ptr task = createPlayerTask( + 400, [this, playerId, pos, stackPos, index, itemId] { playerUseItem(playerId, pos, stackPos, index, itemId); }, "Game::playerUseItem" + ); if (it.isRune() || it.type == ITEM_TYPE_POTION) { player->setNextPotionActionTask(task); } else { @@ -3622,7 +3659,9 @@ void Game::playerUseItem(uint32_t playerId, const Position &pos, uint8_t stackPo if (it.isRune() || it.type == ITEM_TYPE_POTION) { delay = player->getNextPotionActionTime(); } - std::shared_ptr task = createPlayerTask(delay, std::bind(&Game::playerUseItem, this, playerId, pos, stackPos, index, itemId), "Game::playerUseItem"); + std::shared_ptr task = createPlayerTask( + delay, [this, playerId, pos, stackPos, index, itemId] { playerUseItem(playerId, pos, stackPos, index, itemId); }, "Game::playerUseItem" + ); if (it.isRune() || it.type == ITEM_TYPE_POTION) { player->setNextPotionActionTask(task); } else { @@ -3734,9 +3773,14 @@ void Game::playerUseWithCreature(uint32_t playerId, const Position &fromPos, uin stdext::arraylist listDir(128); if (player->getPathTo(walkToPos, listDir, 0, 1, true, true)) { - g_dispatcher().addEvent(std::bind(&Game::playerAutoWalk, this, player->getID(), listDir.data()), "Game::playerAutoWalk"); - - std::shared_ptr task = createPlayerTask(400, std::bind(&Game::playerUseWithCreature, this, playerId, itemPos, itemStackPos, creatureId, itemId), "Game::playerUseWithCreature"); + g_dispatcher().addEvent([this, playerId = player->getID(), listDir = listDir.data()] { playerAutoWalk(playerId, listDir); }, "Game::playerAutoWalk"); + + std::shared_ptr task = createPlayerTask( + 400, [this, playerId, itemPos, itemStackPos, creatureId, itemId] { + playerUseWithCreature(playerId, itemPos, itemStackPos, creatureId, itemId); + }, + "Game::playerUseWithCreature" + ); if (it.isRune() || it.type == ITEM_TYPE_POTION) { player->setNextPotionActionTask(task); } else { @@ -3762,7 +3806,9 @@ void Game::playerUseWithCreature(uint32_t playerId, const Position &fromPos, uin if (it.isRune() || it.type == ITEM_TYPE_POTION) { delay = player->getNextPotionActionTime(); } - std::shared_ptr task = createPlayerTask(delay, std::bind(&Game::playerUseWithCreature, this, playerId, fromPos, fromStackPos, creatureId, itemId), "Game::playerUseWithCreature"); + std::shared_ptr task = createPlayerTask( + delay, [this, playerId, fromPos, fromStackPos, creatureId, itemId] { playerUseWithCreature(playerId, fromPos, fromStackPos, creatureId, itemId); }, "Game::playerUseWithCreature" + ); if (it.isRune() || it.type == ITEM_TYPE_POTION) { player->setNextPotionActionTask(task); @@ -3888,9 +3934,14 @@ void Game::playerRotateItem(uint32_t playerId, const Position &pos, uint8_t stac if (pos.x != 0xFFFF && !Position::areInRange<1, 1, 0>(pos, player->getPosition())) { stdext::arraylist listDir(128); if (player->getPathTo(pos, listDir, 0, 1, true, true)) { - g_dispatcher().addEvent(std::bind(&Game::playerAutoWalk, this, player->getID(), listDir.data()), "Game::playerAutoWalk"); + g_dispatcher().addEvent([this, playerId = player->getID(), listDir = listDir.data()] { playerAutoWalk(playerId, listDir); }, "Game::playerAutoWalk"); - std::shared_ptr task = createPlayerTask(400, std::bind(&Game::playerRotateItem, this, playerId, pos, stackPos, itemId), "Game::playerRotateItem"); + std::shared_ptr task = createPlayerTask( + 400, [this, playerId, pos, stackPos, itemId] { + playerRotateItem(playerId, pos, stackPos, itemId); + }, + "Game::playerRotateItem" + ); player->setNextWalkActionTask(task); } else { player->sendCancelMessage(RETURNVALUE_THEREISNOWAY); @@ -3939,12 +3990,16 @@ void Game::playerConfigureShowOffSocket(uint32_t playerId, const Position &pos, if (!Position::areInRange<1, 1, 0>(pos, player->getPosition())) { stdext::arraylist listDir(128); if (player->getPathTo(pos, listDir, 0, 1, true, false)) { - g_dispatcher().addEvent(std::bind(&Game::playerAutoWalk, this, player->getID(), listDir.data()), "Game::playerAutoWalk"); + g_dispatcher().addEvent([this, playerId = player->getID(), listDir = listDir.data()] { playerAutoWalk(playerId, listDir); }, "Game::playerAutoWalk"); std::shared_ptr task; if (isPodiumOfRenown) { - task = createPlayerTask(400, std::bind_front(&Player::sendPodiumWindow, player, item, pos, itemId, stackPos), "Game::playerConfigureShowOffSocket"); + task = createPlayerTask( + 400, [player, item, pos, itemId, stackPos] { player->sendPodiumWindow(item, pos, itemId, stackPos); }, "Game::playerConfigureShowOffSocket" + ); } else { - task = createPlayerTask(400, std::bind_front(&Player::sendMonsterPodiumWindow, player, item, pos, itemId, stackPos), "Game::playerConfigureShowOffSocket"); + task = createPlayerTask( + 400, [player, item, pos, itemId, stackPos] { player->sendMonsterPodiumWindow(item, pos, itemId, stackPos); }, "Game::playerConfigureShowOffSocket" + ); } player->setNextWalkActionTask(task); } else { @@ -3996,8 +4051,10 @@ void Game::playerSetShowOffSocket(uint32_t playerId, Outfit_t &outfit, const Pos if (!Position::areInRange<1, 1, 0>(pos, player->getPosition())) { stdext::arraylist listDir(128); if (player->getPathTo(pos, listDir, 0, 1, true, false)) { - g_dispatcher().addEvent(std::bind(&Game::playerAutoWalk, this, player->getID(), listDir.data()), "Game::playerAutoWalk"); - std::shared_ptr task = createPlayerTask(400, std::bind(&Game::playerBrowseField, this, playerId, pos), "Game::playerBrowseField"); + g_dispatcher().addEvent([this, playerId = player->getID(), listDir = listDir.data()] { playerAutoWalk(playerId, listDir); }, "Game::playerAutoWalk"); + std::shared_ptr task = createPlayerTask( + 400, [this, playerId, pos] { playerBrowseField(playerId, pos); }, "Game::playerBrowseField" + ); player->setNextWalkActionTask(task); } else { player->sendCancelMessage(RETURNVALUE_THEREISNOWAY); @@ -4135,9 +4192,11 @@ void Game::playerWrapableItem(uint32_t playerId, const Position &pos, uint8_t st if (pos.x != 0xFFFF && !Position::areInRange<1, 1, 0>(pos, player->getPosition())) { stdext::arraylist listDir(128); if (player->getPathTo(pos, listDir, 0, 1, true, true)) { - g_dispatcher().addEvent(std::bind(&Game::playerAutoWalk, this, player->getID(), listDir.data()), "Game::playerAutoWalk"); + g_dispatcher().addEvent([this, playerId = player->getID(), listDir = listDir.data()] { playerAutoWalk(playerId, listDir); }, "Game::playerAutoWalk"); - std::shared_ptr task = createPlayerTask(400, std::bind(&Game::playerWrapableItem, this, playerId, pos, stackPos, itemId), "Game::playerWrapableItem"); + std::shared_ptr task = createPlayerTask( + 400, [this, playerId, pos, stackPos, itemId] { playerWrapableItem(playerId, pos, stackPos, itemId); }, "Game::playerWrapableItem" + ); player->setNextWalkActionTask(task); } else { player->sendCancelMessage(RETURNVALUE_THEREISNOWAY); @@ -4314,8 +4373,10 @@ void Game::playerBrowseField(uint32_t playerId, const Position &pos) { if (!Position::areInRange<1, 1>(playerPos, pos)) { stdext::arraylist listDir(128); if (player->getPathTo(pos, listDir, 0, 1, true, true)) { - g_dispatcher().addEvent(std::bind(&Game::playerAutoWalk, this, player->getID(), listDir.data()), "Game::playerAutoWalk"); - std::shared_ptr task = createPlayerTask(400, std::bind(&Game::playerBrowseField, this, playerId, pos), "Game::playerBrowseField"); + g_dispatcher().addEvent([this, playerId = player->getID(), listDir = listDir.data()] { playerAutoWalk(playerId, listDir); }, "Game::playerAutoWalk"); + std::shared_ptr task = createPlayerTask( + 400, [this, playerId, pos] { playerBrowseField(playerId, pos); }, "Game::playerBrowseField" + ); player->setNextWalkActionTask(task); } else { player->sendCancelMessage(RETURNVALUE_THEREISNOWAY); @@ -4575,9 +4636,11 @@ void Game::playerRequestTrade(uint32_t playerId, const Position &pos, uint8_t st if (!Position::areInRange<1, 1>(tradeItemPosition, playerPosition)) { stdext::arraylist listDir(128); if (player->getPathTo(pos, listDir, 0, 1, true, true)) { - g_dispatcher().addEvent(std::bind(&Game::playerAutoWalk, this, player->getID(), listDir.data()), "Game::playerAutoWalk"); + g_dispatcher().addEvent([this, playerId = player->getID(), listDir = listDir.data()] { playerAutoWalk(playerId, listDir); }, "Game::playerAutoWalk"); - std::shared_ptr task = createPlayerTask(400, std::bind(&Game::playerRequestTrade, this, playerId, pos, stackPos, tradePlayerId, itemId), "Game::playerRequestTrade"); + std::shared_ptr task = createPlayerTask( + 400, [this, playerId, pos, stackPos, tradePlayerId, itemId] { playerRequestTrade(playerId, pos, stackPos, tradePlayerId, itemId); }, "Game::playerRequestTrade" + ); player->setNextWalkActionTask(task); } else { player->sendCancelMessage(RETURNVALUE_THEREISNOWAY); @@ -5120,7 +5183,12 @@ void Game::playerQuickLoot(uint32_t playerId, const Position &pos, uint16_t item if (!autoLoot && !player->canDoAction()) { uint32_t delay = player->getNextActionTime(); - std::shared_ptr task = createPlayerTask(delay, std::bind(&Game::playerQuickLoot, this, player->getID(), pos, itemId, stackPos, defaultItem, lootAllCorpses, autoLoot), "Game::playerQuickLoot"); + std::shared_ptr task = createPlayerTask( + delay, [this, playerId = player->getID(), pos, itemId, stackPos, defaultItem, lootAllCorpses, autoLoot] { + playerQuickLoot(playerId, pos, itemId, stackPos, defaultItem, lootAllCorpses, autoLoot); + }, + "Game::playerQuickLoot" + ); player->setNextActionTask(task); return; } @@ -5130,8 +5198,13 @@ void Game::playerQuickLoot(uint32_t playerId, const Position &pos, uint16_t item // need to walk to the corpse first before looting it stdext::arraylist listDir(128); if (player->getPathTo(pos, listDir, 0, 1, true, true)) { - g_dispatcher().addEvent(std::bind(&Game::playerAutoWalk, this, player->getID(), listDir.data()), "Game::playerAutoWalk"); - std::shared_ptr task = createPlayerTask(0, std::bind(&Game::playerQuickLoot, this, player->getID(), pos, itemId, stackPos, defaultItem, lootAllCorpses, autoLoot), "Game::playerQuickLoot"); + g_dispatcher().addEvent([this, playerId = player->getID(), listDir = listDir.data()] { playerAutoWalk(playerId, listDir); }, "Game::playerAutoWalk"); + std::shared_ptr task = createPlayerTask( + 0, [this, playerId = player->getID(), pos, itemId, stackPos, defaultItem, lootAllCorpses, autoLoot] { + playerQuickLoot(playerId, pos, itemId, stackPos, defaultItem, lootAllCorpses, autoLoot); + }, + "Game::playerQuickLoot" + ); player->setNextWalkActionTask(task); } else { player->sendCancelMessage(RETURNVALUE_THEREISNOWAY); @@ -5494,7 +5567,7 @@ void Game::playerSetAttackedCreature(uint32_t playerId, uint32_t creatureId) { } player->setAttackedCreature(attackCreature); - g_dispatcher().addEvent(std::bind(&Game::updateCreatureWalk, this, player->getID()), "Game::updateCreatureWalk"); + g_dispatcher().addEvent([this, plyerId = player->getID()] { updateCreatureWalk(plyerId); }, "Game::updateCreatureWalk"); } void Game::playerFollowCreature(uint32_t playerId, uint32_t creatureId) { @@ -5504,7 +5577,7 @@ void Game::playerFollowCreature(uint32_t playerId, uint32_t creatureId) { } player->setAttackedCreature(nullptr); - g_dispatcher().addEvent(std::bind(&Game::updateCreatureWalk, this, player->getID()), "Game::updateCreatureWalk"); + g_dispatcher().addEvent([this, plyerId = player->getID()] { updateCreatureWalk(plyerId); }, "Game::updateCreatureWalk"); player->setFollowCreature(getCreatureByID(creatureId)); } @@ -6252,7 +6325,6 @@ bool Game::combatBlockHit(CombatDamage &damage, std::shared_ptr attack std::shared_ptr targetPlayer = target->getPlayer(); if (damage.primary.type != COMBAT_NONE) { - damage.primary.value = -damage.primary.value; // Damage healing primary if (attacker) { @@ -9330,8 +9402,10 @@ void Game::playerSetMonsterPodium(uint32_t playerId, uint32_t monsterRaceId, con if (!Position::areInRange<1, 1, 0>(pos, player->getPosition())) { if (stdext::arraylist listDir(128); player->getPathTo(pos, listDir, 0, 1, true, false)) { - g_dispatcher().addEvent(std::bind_front(&Game::playerAutoWalk, this, player->getID(), listDir.data()), "Game::playerAutoWalk"); - std::shared_ptr task = createPlayerTask(400, std::bind_front(&Game::playerBrowseField, this, playerId, pos), "Game::playerBrowseField"); + g_dispatcher().addEvent([this, playerId = player->getID(), listDir = listDir.data()] { playerAutoWalk(playerId, listDir); }, "Game::playerAutoWalk"); + std::shared_ptr task = createPlayerTask( + 400, [this, playerId, pos] { playerBrowseField(playerId, pos); }, "Game::playerBrowseField" + ); player->setNextWalkActionTask(task); } else { player->sendCancelMessage(RETURNVALUE_THEREISNOWAY); @@ -9428,9 +9502,13 @@ void Game::playerRotatePodium(uint32_t playerId, const Position &pos, uint8_t st if (pos.x != 0xFFFF && !Position::areInRange<1, 1, 0>(pos, player->getPosition())) { if (stdext::arraylist listDir(128); player->getPathTo(pos, listDir, 0, 1, true, true)) { - g_dispatcher().addEvent(std::bind_front(&Game::playerAutoWalk, this, player->getID(), listDir.data()), "Game::playerAutoWalk"); - - std::shared_ptr task = createPlayerTask(400, std::bind_front(&Game::playerRotatePodium, this, playerId, pos, stackPos, itemId), "Game::playerRotatePodium"); + g_dispatcher().addEvent([this, playerId = player->getID(), listDir = listDir.data()] { playerAutoWalk(playerId, listDir); }, "Game::playerAutoWalk"); + std::shared_ptr task = createPlayerTask( + 400, [this, playerId, pos, stackPos, itemId] { + playerRotatePodium(playerId, pos, stackPos, itemId); + }, + "Game::playerRotatePodium" + ); player->setNextWalkActionTask(task); } else { player->sendCancelMessage(RETURNVALUE_THEREISNOWAY); @@ -9947,7 +10025,7 @@ uint32_t Game::makeFiendishMonster(uint32_t forgeableMonsterId /* = 0*/, bool cr auto schedulerTask = createPlayerTask( finalTime, - std::bind_front(&Game::updateFiendishMonsterStatus, this, monster->getID(), monster->getName()), + [this, monster] { updateFiendishMonsterStatus(monster->getID(), monster->getName()); }, "Game::updateFiendishMonsterStatus" ); forgeMonsterEventIds[monster->getID()] = g_dispatcher().scheduleEvent(schedulerTask); @@ -9986,7 +10064,9 @@ bool Game::removeInfluencedMonster(uint32_t id, bool create /* = false*/) { influencedMonsters.erase(find); if (create) { - g_dispatcher().scheduleEvent(200 * 1000, std::bind_front(&Game::makeInfluencedMonster, this), "Game::makeInfluencedMonster"); + g_dispatcher().scheduleEvent( + 200 * 1000, [this] { makeInfluencedMonster(); }, "Game::makeInfluencedMonster" + ); } } else { g_logger().warn("[Game::removeInfluencedMonster] - Failed to remove a Influenced Monster, error code: monster id not exist in the influenced monsters map"); @@ -10002,7 +10082,9 @@ bool Game::removeFiendishMonster(uint32_t id, bool create /* = true*/) { checkForgeEventId(id); if (create) { - g_dispatcher().scheduleEvent(300 * 1000, std::bind_front(&Game::makeFiendishMonster, this, 0, false), "Game::makeFiendishMonster"); + g_dispatcher().scheduleEvent( + 300 * 1000, [this] { makeFiendishMonster(0, false); }, "Game::makeFiendishMonster" + ); } } else { g_logger().warn("[Game::removeFiendishMonster] - Failed to remove a Fiendish Monster, error code: monster id not exist in the fiendish monsters map"); @@ -10200,7 +10282,9 @@ void Game::playerCheckActivity(const std::string &playerName, int interval) { } } - g_dispatcher().scheduleEvent(1000, std::bind(&Game::playerCheckActivity, this, playerName, interval), "Game::playerCheckActivity"); + g_dispatcher().scheduleEvent( + 1000, [this, playerName, interval] { playerCheckActivity(playerName, interval); }, "Game::playerCheckActivity" + ); } void Game::playerRewardChestCollect(uint32_t playerId, const Position &pos, uint16_t itemId, uint8_t stackPos, uint32_t maxMoveItems /* = 0*/) { @@ -10221,8 +10305,11 @@ void Game::playerRewardChestCollect(uint32_t playerId, const Position &pos, uint return; } - if (auto function = std::bind(&Game::playerRewardChestCollect, this, player->getID(), pos, itemId, stackPos, maxMoveItems); - player->canAutoWalk(item->getPosition(), function)) { + const auto &function = [this, playerId = player->getID(), pos, itemId, stackPos, maxMoveItems] { + playerRewardChestCollect(playerId, pos, itemId, stackPos, maxMoveItems); + }; + + if (player->canAutoWalk(item->getPosition(), function)) { return; } diff --git a/src/items/bed.cpp b/src/items/bed.cpp index 4ce4a1e0edd..d093dcc5fd9 100644 --- a/src/items/bed.cpp +++ b/src/items/bed.cpp @@ -165,7 +165,9 @@ bool BedItem::sleep(std::shared_ptr player) { g_game().addMagicEffect(player->getPosition(), CONST_ME_SLEEP); // logout player after he sees himself walk onto the bed and it change id - g_dispatcher().scheduleEvent(SCHEDULER_MINTICKS, std::bind(&ProtocolGame::logout, player->client, false, false), "ProtocolGame::logout"); + g_dispatcher().scheduleEvent( + SCHEDULER_MINTICKS, [client = player->client] { client->logout(false, false); }, "ProtocolGame::logout" + ); // change self and partner's appearance updateAppearance(player); diff --git a/src/items/decay/decay.cpp b/src/items/decay/decay.cpp index 27ae7b6b485..848acffbf68 100644 --- a/src/items/decay/decay.cpp +++ b/src/items/decay/decay.cpp @@ -47,11 +47,15 @@ void Decay::startDecay(std::shared_ptr item) { int64_t timestamp = OTSYS_TIME() + duration; if (decayMap.empty()) { - eventId = g_dispatcher().scheduleEvent(std::max(SCHEDULER_MINTICKS, duration), std::bind(&Decay::checkDecay, this), "Decay::checkDecay"); + eventId = g_dispatcher().scheduleEvent( + std::max(SCHEDULER_MINTICKS, duration), [this] { checkDecay(); }, "Decay::checkDecay" + ); } else { if (timestamp < decayMap.begin()->first) { g_dispatcher().stopEvent(eventId); - eventId = g_dispatcher().scheduleEvent(std::max(SCHEDULER_MINTICKS, duration), std::bind(&Decay::checkDecay, this), "Decay::checkDecay"); + eventId = g_dispatcher().scheduleEvent( + std::max(SCHEDULER_MINTICKS, duration), [this] { checkDecay(); }, "Decay::checkDecay" + ); } } @@ -138,7 +142,9 @@ void Decay::checkDecay() { } if (it != end) { - eventId = g_dispatcher().scheduleEvent(std::max(SCHEDULER_MINTICKS, static_cast(it->first - timestamp)), std::bind(&Decay::checkDecay, this), "Decay::checkDecay"); + eventId = g_dispatcher().scheduleEvent( + std::max(SCHEDULER_MINTICKS, static_cast(it->first - timestamp)), [this] { checkDecay(); }, "Decay::checkDecay" + ); } } diff --git a/src/lua/creature/raids.cpp b/src/lua/creature/raids.cpp index f0d7cecf402..a4500d126ba 100644 --- a/src/lua/creature/raids.cpp +++ b/src/lua/creature/raids.cpp @@ -102,7 +102,9 @@ bool Raids::startup() { setLastRaidEnd(OTSYS_TIME()); - checkRaidsEvent = g_dispatcher().scheduleEvent(CHECK_RAIDS_INTERVAL * 1000, std::bind(&Raids::checkRaids, this), "Raids::checkRaids"); + checkRaidsEvent = g_dispatcher().scheduleEvent( + CHECK_RAIDS_INTERVAL * 1000, [this] { checkRaids(); }, "Raids::checkRaids" + ); started = true; return started; @@ -134,7 +136,9 @@ void Raids::checkRaids() { } } - checkRaidsEvent = g_dispatcher().scheduleEvent(CHECK_RAIDS_INTERVAL * 1000, std::bind(&Raids::checkRaids, this), "Raids::checkRaids"); + checkRaidsEvent = g_dispatcher().scheduleEvent( + CHECK_RAIDS_INTERVAL * 1000, [this] { checkRaids(); }, "Raids::checkRaids" + ); } void Raids::clear() { @@ -216,7 +220,9 @@ void Raid::startRaid() { const auto raidEvent = getNextRaidEvent(); if (raidEvent) { state = RAIDSTATE_EXECUTING; - nextEventEvent = g_dispatcher().scheduleEvent(raidEvent->getDelay(), std::bind(&Raid::executeRaidEvent, this, raidEvent), "Raid::executeRaidEvent"); + nextEventEvent = g_dispatcher().scheduleEvent( + raidEvent->getDelay(), [this, raidEvent] { executeRaidEvent(raidEvent); }, "Raid::executeRaidEvent" + ); } else { g_logger().warn("[raids] Raid {} has no events", name); resetRaid(); @@ -230,7 +236,9 @@ void Raid::executeRaidEvent(const std::shared_ptr raidEvent) { if (newRaidEvent) { uint32_t ticks = static_cast(std::max(RAID_MINTICKS, newRaidEvent->getDelay() - raidEvent->getDelay())); - nextEventEvent = g_dispatcher().scheduleEvent(ticks, std::bind(&Raid::executeRaidEvent, this, newRaidEvent), __FUNCTION__); + nextEventEvent = g_dispatcher().scheduleEvent( + ticks, [this, newRaidEvent] { executeRaidEvent(newRaidEvent); }, __FUNCTION__ + ); } else { resetRaid(); } diff --git a/src/lua/functions/core/game/global_functions.cpp b/src/lua/functions/core/game/global_functions.cpp index cbb133211f8..0403e88dd95 100644 --- a/src/lua/functions/core/game/global_functions.cpp +++ b/src/lua/functions/core/game/global_functions.cpp @@ -664,7 +664,7 @@ int GlobalFunctions::luaAddEvent(lua_State* L) { auto &lastTimerEventId = g_luaEnvironment().lastEventTimerId; eventDesc.eventId = g_dispatcher().scheduleEvent( delay, - std::bind(&LuaEnvironment::executeTimerEvent, &g_luaEnvironment(), lastTimerEventId), + [lastTimerEventId] { g_luaEnvironment().executeTimerEvent(lastTimerEventId); }, "LuaEnvironment::executeTimerEvent" ); diff --git a/src/lua/global/globalevent.cpp b/src/lua/global/globalevent.cpp index 62650b429a9..9626173dc9f 100644 --- a/src/lua/global/globalevent.cpp +++ b/src/lua/global/globalevent.cpp @@ -35,7 +35,9 @@ bool GlobalEvents::registerLuaEvent(const std::shared_ptr globalEve auto result = timerMap.emplace(globalEvent->getName(), globalEvent); if (result.second) { if (timerEventId == 0) { - timerEventId = g_dispatcher().scheduleEvent(SCHEDULER_MINTICKS, std::bind(&GlobalEvents::timer, this), "GlobalEvents::timer"); + timerEventId = g_dispatcher().scheduleEvent( + SCHEDULER_MINTICKS, [this] { timer(); }, "GlobalEvents::timer" + ); } return true; } @@ -99,7 +101,9 @@ void GlobalEvents::timer() { } if (nextScheduledTime != std::numeric_limits::max()) { - timerEventId = g_dispatcher().scheduleEvent(std::max(1000, nextScheduledTime * 1000), std::bind(&GlobalEvents::timer, this), __FUNCTION__); + timerEventId = g_dispatcher().scheduleEvent( + std::max(1000, nextScheduledTime * 1000), [this] { timer(); }, __FUNCTION__ + ); } } @@ -136,7 +140,9 @@ void GlobalEvents::think() { if (nextScheduledTime != std::numeric_limits::max()) { auto delay = static_cast(nextScheduledTime); - thinkEventId = g_dispatcher().scheduleEvent(delay, std::bind(&GlobalEvents::think, this), "GlobalEvents::think"); + thinkEventId = g_dispatcher().scheduleEvent( + delay, [this] { think(); }, "GlobalEvents::think" + ); } } diff --git a/src/server/network/connection/connection.cpp b/src/server/network/connection/connection.cpp index 41789b88387..cc27eb0e9eb 100644 --- a/src/server/network/connection/connection.cpp +++ b/src/server/network/connection/connection.cpp @@ -62,7 +62,7 @@ void Connection::close(bool force) { connectionState = CONNECTION_STATE_CLOSED; if (protocol) { - g_dispatcher().addEvent(std::bind_front(&Protocol::release, protocol), "Protocol::release", std::chrono::milliseconds(CONNECTION_WRITE_TIMEOUT * 1000).count()); + g_dispatcher().addEvent([protocol = protocol] { protocol->release(); }, "Protocol::release", std::chrono::milliseconds(CONNECTION_WRITE_TIMEOUT * 1000).count()); } if (messageQueue.empty() || force) { @@ -98,19 +98,23 @@ void Connection::closeSocket() { void Connection::accept(Protocol_ptr protocolPtr) { connectionState = CONNECTION_STATE_IDENTIFYING; protocol = std::move(protocolPtr); - g_dispatcher().addEvent(std::bind_front(&Protocol::onConnect, protocol), "Protocol::onConnect", std::chrono::milliseconds(CONNECTION_WRITE_TIMEOUT * 1000).count()); + g_dispatcher().addEvent([protocol = protocol] { protocol->onConnect(); }, "Protocol::onConnect", std::chrono::milliseconds(CONNECTION_WRITE_TIMEOUT * 1000).count()); acceptInternal(false); } void Connection::acceptInternal(bool toggleParseHeader) { readTimer.expires_from_now(std::chrono::seconds(CONNECTION_READ_TIMEOUT)); - readTimer.async_wait(std::bind(&Connection::handleTimeout, std::weak_ptr(shared_from_this()), std::placeholders::_1)); + readTimer.async_wait([self = shared_from_this()](const std::error_code &error) { Connection::handleTimeout(std::weak_ptr(self), error); }); try { - auto readCallback = toggleParseHeader ? std::bind(&Connection::parseHeader, shared_from_this(), std::placeholders::_1) - : std::bind(&Connection::parseProxyIdentification, shared_from_this(), std::placeholders::_1); - asio::async_read(socket, asio::buffer(msg.getBuffer(), HEADER_LENGTH), readCallback); + asio::async_read(socket, asio::buffer(msg.getBuffer(), HEADER_LENGTH), [self = shared_from_this(), toggleParseHeader](const std::error_code &error, std::size_t N) { + if (toggleParseHeader) { + self->parseHeader(error); + } else { + self->parseProxyIdentification(error); + } + }); } catch (const std::system_error &e) { g_logger().error("[Connection::acceptInternal] - Exception in async_read: {}", e.what()); close(FORCE_CLOSE); @@ -143,10 +147,10 @@ void Connection::parseProxyIdentification(const std::error_code &error) { connectionState = CONNECTION_STATE_READINGS; try { readTimer.expires_from_now(std::chrono::seconds(CONNECTION_READ_TIMEOUT)); - readTimer.async_wait(std::bind(&Connection::handleTimeout, std::weak_ptr(shared_from_this()), std::placeholders::_1)); + readTimer.async_wait([self = shared_from_this()](const std::error_code &error) { Connection::handleTimeout(std::weak_ptr(self), error); }); // Read the remainder of proxy identification - asio::async_read(socket, asio::buffer(msg.getBuffer(), remainder), std::bind(&Connection::parseProxyIdentification, shared_from_this(), std::placeholders::_1)); + asio::async_read(socket, asio::buffer(msg.getBuffer(), remainder), [self = shared_from_this()](const std::error_code &error, std::size_t N) { self->parseProxyIdentification(error); }); } catch (const std::system_error &e) { g_logger().error("Connection::parseProxyIdentification] - error: {}", e.what()); close(FORCE_CLOSE); @@ -204,11 +208,12 @@ void Connection::parseHeader(const std::error_code &error) { try { readTimer.expires_from_now(std::chrono::seconds(CONNECTION_READ_TIMEOUT)); - readTimer.async_wait(std::bind(&Connection::handleTimeout, std::weak_ptr(shared_from_this()), std::placeholders::_1)); + readTimer.async_wait([self = shared_from_this()](const std::error_code &error) { Connection::handleTimeout(std::weak_ptr(self), error); }); // Read packet content msg.setLength(size + HEADER_LENGTH); - asio::async_read(socket, asio::buffer(msg.getBodyBuffer(), size), std::bind(&Connection::parsePacket, shared_from_this(), std::placeholders::_1)); + // Read the remainder of proxy identification + asio::async_read(socket, asio::buffer(msg.getBodyBuffer(), size), [self = shared_from_this()](const std::error_code &error, std::size_t N) { self->parsePacket(error); }); } catch (const std::system_error &e) { g_logger().error("[Connection::parseHeader] - error: {}", e.what()); close(FORCE_CLOSE); @@ -270,11 +275,11 @@ void Connection::parsePacket(const std::error_code &error) { try { readTimer.expires_from_now(std::chrono::seconds(CONNECTION_READ_TIMEOUT)); - readTimer.async_wait(std::bind(&Connection::handleTimeout, std::weak_ptr(shared_from_this()), std::placeholders::_1)); + readTimer.async_wait([self = shared_from_this()](const std::error_code &error) { Connection::handleTimeout(std::weak_ptr(self), error); }); if (!skipReadingNextPacket) { // Wait to the next packet - asio::async_read(socket, asio::buffer(msg.getBuffer(), HEADER_LENGTH), std::bind(&Connection::parseHeader, shared_from_this(), std::placeholders::_1)); + asio::async_read(socket, asio::buffer(msg.getBuffer(), HEADER_LENGTH), [self = shared_from_this()](const std::error_code &error, std::size_t N) { self->parseHeader(error); }); } } catch (const std::system_error &e) { g_logger().error("[Connection::parsePacket] - error: {}", e.what()); @@ -284,10 +289,10 @@ void Connection::parsePacket(const std::error_code &error) { void Connection::resumeWork() { readTimer.expires_from_now(std::chrono::seconds(CONNECTION_READ_TIMEOUT)); - readTimer.async_wait(std::bind(&Connection::handleTimeout, std::weak_ptr(shared_from_this()), std::placeholders::_1)); + readTimer.async_wait([self = shared_from_this()](const std::error_code &error) { Connection::handleTimeout(std::weak_ptr(self), error); }); try { - asio::async_read(socket, asio::buffer(msg.getBuffer(), HEADER_LENGTH), std::bind(&Connection::parseHeader, shared_from_this(), std::placeholders::_1)); + asio::async_read(socket, asio::buffer(msg.getBuffer(), HEADER_LENGTH), [self = shared_from_this()](const std::error_code &error, std::size_t N) { self->parseHeader(error); }); } catch (const std::system_error &e) { g_logger().error("[Connection::resumeWork] - Exception in async_read: {}", e.what()); close(FORCE_CLOSE); @@ -306,7 +311,7 @@ void Connection::send(const OutputMessage_ptr &outputMessage) { if (noPendingWrite) { if (socket.is_open()) { try { - asio::post(socket.get_executor(), std::bind(&Connection::internalWorker, shared_from_this())); + asio::post(socket.get_executor(), [self = shared_from_this()] { self->internalWorker(); }); } catch (const std::system_error &e) { g_logger().error("[Connection::send] - Exception in posting write operation: {}", e.what()); close(FORCE_CLOSE); @@ -353,10 +358,10 @@ uint32_t Connection::getIP() { void Connection::internalSend(const OutputMessage_ptr &outputMessage) { writeTimer.expires_from_now(std::chrono::seconds(CONNECTION_WRITE_TIMEOUT)); - writeTimer.async_wait(std::bind(&Connection::handleTimeout, std::weak_ptr(shared_from_this()), std::placeholders::_1)); + readTimer.async_wait([self = shared_from_this()](const std::error_code &error) { Connection::handleTimeout(std::weak_ptr(self), error); }); try { - asio::async_write(socket, asio::buffer(outputMessage->getOutputBuffer(), outputMessage->getLength()), std::bind(&Connection::onWriteOperation, shared_from_this(), std::placeholders::_1)); + asio::async_write(socket, asio::buffer(outputMessage->getOutputBuffer(), outputMessage->getLength()), [self = shared_from_this()](const std::error_code &error, std::size_t N) { self->onWriteOperation(error); }); } catch (const std::system_error &e) { g_logger().error("[Connection::internalSend] - Exception in async_write: {}", e.what()); close(FORCE_CLOSE); diff --git a/src/server/network/message/outputmessage.cpp b/src/server/network/message/outputmessage.cpp index 6ede36da406..efed7fe8367 100644 --- a/src/server/network/message/outputmessage.cpp +++ b/src/server/network/message/outputmessage.cpp @@ -16,8 +16,9 @@ const std::chrono::milliseconds OUTPUTMESSAGE_AUTOSEND_DELAY { 10 }; void OutputMessagePool::scheduleSendAll() { - auto function = std::bind_front(&OutputMessagePool::sendAll, this); - g_dispatcher().scheduleEvent(OUTPUTMESSAGE_AUTOSEND_DELAY.count(), function, "OutputMessagePool::sendAll"); + g_dispatcher().scheduleEvent( + OUTPUTMESSAGE_AUTOSEND_DELAY.count(), [this] { sendAll(); }, "OutputMessagePool::sendAll" + ); } void OutputMessagePool::sendAll() { diff --git a/src/server/network/protocol/protocolgame.cpp b/src/server/network/protocol/protocolgame.cpp index 5398155067e..218fc857d3c 100644 --- a/src/server/network/protocol/protocolgame.cpp +++ b/src/server/network/protocol/protocolgame.cpp @@ -248,16 +248,6 @@ ProtocolGame::ProtocolGame(Connection_ptr initConnection) : version = CLIENT_VERSION; } -template -void ProtocolGame::addGameTask(Callable function, Args &&... args) { - g_dispatcher().addEvent(std::bind(function, &g_game(), std::forward(args)...), "ProtocolGame::addGameTask"); -} - -template -void ProtocolGame::addGameTaskTimed(uint32_t delay, std::string_view context, Callable function, Args &&... args) { - g_dispatcher().addEvent(std::bind(function, &g_game(), std::forward(args)...), context, delay); -} - void ProtocolGame::AddItem(NetworkMessage &msg, uint16_t id, uint8_t count, uint8_t tier) { const ItemType &it = Item::items[id]; @@ -629,7 +619,10 @@ void ProtocolGame::login(const std::string &name, uint32_t accountId, OperatingS foundPlayer->disconnect(); foundPlayer->isConnecting = true; - eventConnect = g_dispatcher().scheduleEvent(1000, std::bind(&ProtocolGame::connect, getThis(), foundPlayer->getName(), operatingSystem), "ProtocolGame::connect"); + eventConnect = g_dispatcher().scheduleEvent( + 1000, + [self = getThis(), playerName = foundPlayer->getName(), operatingSystem] { self->connect(playerName, operatingSystem); }, "ProtocolGame::connect" + ); } else { connect(foundPlayer->getName(), operatingSystem); } @@ -842,11 +835,13 @@ void ProtocolGame::onRecvFirstMessage(NetworkMessage &msg) { output->addByte(0x14); output->addString(ss.str(), "ProtocolGame::onRecvFirstMessage - ss.str()"); send(output); - g_dispatcher().scheduleEvent(1000, std::bind(&ProtocolGame::disconnect, getThis()), "ProtocolGame::disconnect"); + g_dispatcher().scheduleEvent( + 1000, [self = getThis()] { self->disconnect(); }, "ProtocolGame::disconnect" + ); return; } - g_dispatcher().addEvent(std::bind(&ProtocolGame::login, getThis(), characterName, accountId, operatingSystem), "ProtocolGame::login"); + g_dispatcher().addEvent([self = getThis(), characterName, accountId, operatingSystem] { self->login(characterName, accountId, operatingSystem); }, "ProtocolGame::login"); } void ProtocolGame::onConnect() { @@ -907,20 +902,20 @@ void ProtocolGame::parsePacket(NetworkMessage &msg) { if (player->isDead() || player->getHealth() <= 0) { // Check player activity on death screen if (m_playerDeathTime == 0) { - addGameTask(&Game::playerCheckActivity, player->getName(), 1000); + g_game().playerCheckActivity(player->getName(), 1000); m_playerDeathTime++; } - g_dispatcher().addEvent(std::bind(&ProtocolGame::parsePacketDead, getThis(), recvbyte), "ProtocolGame::parsePacketDead"); + parsePacketDead(recvbyte); return; } // Modules system if (player && recvbyte != 0xD3) { - g_dispatcher().addEvent(std::bind(&Modules::executeOnRecvbyte, &g_modules(), player->getID(), msg, recvbyte), "Modules::executeOnRecvbyte"); + g_modules().executeOnRecvbyte(player->getID(), msg, recvbyte); } - g_dispatcher().addEvent(std::bind(&ProtocolGame::parsePacketFromDispatcher, getThis(), msg, recvbyte), "ProtocolGame::parsePacketFromDispatcher"); + parsePacketFromDispatcher(msg, recvbyte); } void ProtocolGame::parsePacketDead(uint8_t recvbyte) { @@ -930,7 +925,7 @@ void ProtocolGame::parsePacketDead(uint8_t recvbyte) { g_game().removePlayerUniqueLogin(player->getName()); } disconnect(); - g_dispatcher().addEvent(std::bind(&IOLoginData::updateOnlineStatus, player->getGUID(), false), "IOLoginData::updateOnlineStatus"); + IOLoginData::updateOnlineStatus(player->getGUID(), false); return; } @@ -939,23 +934,27 @@ void ProtocolGame::parsePacketDead(uint8_t recvbyte) { return; } - g_dispatcher().scheduleEvent(100, std::bind(&ProtocolGame::sendPing, getThis()), "ProtocolGame::sendPing"); + g_dispatcher().scheduleEvent( + 100, [self = getThis()] { self->sendPing(); }, "ProtocolGame::sendPing" + ); if (!player->spawn()) { disconnect(); - addGameTask(&Game::removeCreature, player, true); + g_game().removeCreature(player, true); return; } - g_dispatcher().addEvent(std::bind(&ProtocolGame::sendAddCreature, getThis(), player, player->getPosition(), 0, false), "ProtocolGame::sendAddCreature"); - g_dispatcher().addEvent(std::bind(&ProtocolGame::addBless, getThis()), "ProtocolGame::addBless"); + sendAddCreature(player, player->getPosition(), 0, false); + addBless(); resetPlayerDeathTime(); return; } if (recvbyte == 0x1D) { // keep the connection alive - g_dispatcher().scheduleEvent(100, std::bind(&ProtocolGame::sendPingBack, getThis()), "ProtocolGame::sendPingBack"); + g_dispatcher().scheduleEvent( + 100, [self = getThis()] { self->sendPingBack(); }, "ProtocolGame::sendPingBack" + ); return; } } @@ -978,13 +977,13 @@ void ProtocolGame::parsePacketFromDispatcher(NetworkMessage msg, uint8_t recvbyt switch (recvbyte) { case 0x14: - g_dispatcher().addEvent(std::bind(&ProtocolGame::logout, getThis(), true, false), "ProtocolGame::logout"); + logout(true, false); break; case 0x1D: - addGameTask(&Game::playerReceivePingBack, player->getID()); + g_game().playerReceivePingBack(player->getID()); break; case 0x1E: - addGameTask(&Game::playerReceivePing, player->getID()); + g_game().playerReceivePing(player->getID()); break; case 0x2a: parseCyclopediaMonsterTracker(msg); @@ -1020,43 +1019,43 @@ void ProtocolGame::parsePacketFromDispatcher(NetworkMessage msg, uint8_t recvbyt parseAutoWalk(msg); break; case 0x65: - addGameTask(&Game::playerMove, player->getID(), DIRECTION_NORTH); + g_game().playerMove(player->getID(), DIRECTION_NORTH); break; case 0x66: - addGameTask(&Game::playerMove, player->getID(), DIRECTION_EAST); + g_game().playerMove(player->getID(), DIRECTION_EAST); break; case 0x67: - addGameTask(&Game::playerMove, player->getID(), DIRECTION_SOUTH); + g_game().playerMove(player->getID(), DIRECTION_SOUTH); break; case 0x68: - addGameTask(&Game::playerMove, player->getID(), DIRECTION_WEST); + g_game().playerMove(player->getID(), DIRECTION_WEST); break; case 0x69: - addGameTask(&Game::playerStopAutoWalk, player->getID()); + g_game().playerStopAutoWalk(player->getID()); break; case 0x6A: - addGameTask(&Game::playerMove, player->getID(), DIRECTION_NORTHEAST); + g_game().playerMove(player->getID(), DIRECTION_NORTHEAST); break; case 0x6B: - addGameTask(&Game::playerMove, player->getID(), DIRECTION_SOUTHEAST); + g_game().playerMove(player->getID(), DIRECTION_SOUTHEAST); break; case 0x6C: - addGameTask(&Game::playerMove, player->getID(), DIRECTION_SOUTHWEST); + g_game().playerMove(player->getID(), DIRECTION_SOUTHWEST); break; case 0x6D: - addGameTask(&Game::playerMove, player->getID(), DIRECTION_NORTHWEST); + g_game().playerMove(player->getID(), DIRECTION_NORTHWEST); break; case 0x6F: - addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, "Game::playerTurn", &Game::playerTurn, player->getID(), DIRECTION_NORTH); + g_game().playerTurn(player->getID(), DIRECTION_NORTH); break; case 0x70: - addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, "Game::playerTurn", &Game::playerTurn, player->getID(), DIRECTION_EAST); + g_game().playerTurn(player->getID(), DIRECTION_EAST); break; case 0x71: - addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, "Game::playerTurn", &Game::playerTurn, player->getID(), DIRECTION_SOUTH); + g_game().playerTurn(player->getID(), DIRECTION_SOUTH); break; case 0x72: - addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, "Game::playerTurn", &Game::playerTurn, player->getID(), DIRECTION_WEST); + g_game().playerTurn(player->getID(), DIRECTION_WEST); break; case 0x73: parseTeleport(msg); @@ -1077,7 +1076,7 @@ void ProtocolGame::parsePacketFromDispatcher(NetworkMessage msg, uint8_t recvbyt parsePlayerSellOnShop(msg); break; case 0x7C: - addGameTask(&Game::playerCloseShop, player->getID()); + g_game().playerCloseShop(player->getID()); break; case 0x7D: parseRequestTrade(msg); @@ -1086,10 +1085,10 @@ void ProtocolGame::parsePacketFromDispatcher(NetworkMessage msg, uint8_t recvbyt parseLookInTrade(msg); break; case 0x7F: - addGameTask(&Game::playerAcceptTrade, player->getID()); + g_game().playerAcceptTrade(player->getID()); break; case 0x80: - addGameTask(&Game::playerCloseTrade, player->getID()); + g_game().playerCloseTrade(player->getID()); break; case 0x82: parseUseItem(msg); @@ -1154,7 +1153,7 @@ void ProtocolGame::parsePacketFromDispatcher(NetworkMessage msg, uint8_t recvbyt parseSay(msg); break; case 0x97: - addGameTask(&Game::playerRequestChannels, player->getID()); + g_game().playerRequestChannels(player->getID()); break; case 0x98: parseOpenChannel(msg); @@ -1166,7 +1165,7 @@ void ProtocolGame::parsePacketFromDispatcher(NetworkMessage msg, uint8_t recvbyt parseOpenPrivateChannel(msg); break; case 0x9E: - addGameTask(&Game::playerCloseNpcChannel, player->getID()); + g_game().playerCloseNpcChannel(player->getID()); break; case 0x9F: parseSetMonsterPodium(msg); @@ -1193,13 +1192,13 @@ void ProtocolGame::parsePacketFromDispatcher(NetworkMessage msg, uint8_t recvbyt parsePassPartyLeadership(msg); break; case 0xA7: - addGameTask(&Game::playerLeaveParty, player->getID()); + g_game().playerLeaveParty(player->getID()); break; case 0xA8: parseEnableSharedPartyExperience(msg); break; case 0xAA: - addGameTask(&Game::playerCreatePrivateChannel, player->getID()); + g_game().playerCreatePrivateChannel(player->getID()); break; case 0xAB: parseChannelInvite(msg); @@ -1223,7 +1222,7 @@ void ProtocolGame::parsePacketFromDispatcher(NetworkMessage msg, uint8_t recvbyt parseTaskHuntingAction(msg); break; case 0xBE: - addGameTask(&Game::playerCancelAttackAndFollow, player->getID()); + g_game().playerCancelAttackAndFollow(player->getID()); break; case 0xBF: parseForgeEnter(msg); @@ -1246,11 +1245,10 @@ void ProtocolGame::parsePacketFromDispatcher(NetworkMessage msg, uint8_t recvbyt parseInspectionObject(msg); break; case 0xD2: - addGameTask(&Game::playerRequestOutfit, player->getID()); + g_game().playerRequestOutfit(player->getID()); break; - // g_dispatcher().addEvent(std::bind(&Modules::executeOnRecvbyte, g_modules, player, msg, recvbyte)); case 0xD3: - g_dispatcher().addEvent(std::bind(&ProtocolGame::parseSetOutfit, getThis(), msg), "ProtocolGame::parseSetOutfit"); + parseSetOutfit(msg); break; case 0xD4: parseToggleMount(msg); @@ -1309,7 +1307,7 @@ void ProtocolGame::parsePacketFromDispatcher(NetworkMessage msg, uint8_t recvbyt // Premium coins transfer // case 0xEF: parseCoinTransfer(msg); break; case 0xF0: - addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, "Game::playerShowQuestLog", &Game::playerShowQuestLog, player->getID()); + g_game().playerShowQuestLog(player->getID()); break; case 0xF1: parseQuestLine(msg); @@ -1360,7 +1358,7 @@ void ProtocolGame::parseHotkeyEquip(NetworkMessage &msg) { uint16_t itemId = msg.get(); uint8_t tier = msg.get(); - addGameTask(&Game::playerEquipItem, player->getID(), itemId, Item::items[itemId].upgradeClassification > 0, tier); + g_game().playerEquipItem(player->getID(), itemId, Item::items[itemId].upgradeClassification > 0, tier); } void ProtocolGame::GetTileDescription(std::shared_ptr tile, NetworkMessage &msg) { @@ -1563,27 +1561,27 @@ bool ProtocolGame::canSee(int32_t x, int32_t y, int32_t z) const { // Parse methods void ProtocolGame::parseChannelInvite(NetworkMessage &msg) { const std::string name = msg.getString(); - addGameTask(&Game::playerChannelInvite, player->getID(), name); + g_game().playerChannelInvite(player->getID(), name); } void ProtocolGame::parseChannelExclude(NetworkMessage &msg) { const std::string name = msg.getString(); - addGameTask(&Game::playerChannelExclude, player->getID(), name); + g_game().playerChannelExclude(player->getID(), name); } void ProtocolGame::parseOpenChannel(NetworkMessage &msg) { uint16_t channelId = msg.get(); - addGameTask(&Game::playerOpenChannel, player->getID(), channelId); + g_game().playerOpenChannel(player->getID(), channelId); } void ProtocolGame::parseCloseChannel(NetworkMessage &msg) { uint16_t channelId = msg.get(); - addGameTask(&Game::playerCloseChannel, player->getID(), channelId); + g_game().playerCloseChannel(player->getID(), channelId); } void ProtocolGame::parseOpenPrivateChannel(NetworkMessage &msg) { - const std::string receiver = msg.getString(); - addGameTask(&Game::playerOpenPrivateChannel, player->getID(), receiver); + std::string receiver = msg.getString(); + g_game().playerOpenPrivateChannel(player->getID(), receiver); } void ProtocolGame::parseAutoWalk(NetworkMessage &msg) { @@ -1631,7 +1629,7 @@ void ProtocolGame::parseAutoWalk(NetworkMessage &msg) { return; } - addGameTask(&Game::playerAutoWalk, player->getID(), path.data()); + g_game().playerAutoWalk(player->getID(), path.data()); } void ProtocolGame::parseSetOutfit(NetworkMessage &msg) { @@ -1688,23 +1686,23 @@ void ProtocolGame::parseSetOutfit(NetworkMessage &msg) { void ProtocolGame::parseToggleMount(NetworkMessage &msg) { bool mount = msg.getByte() != 0; - addGameTask(&Game::playerToggleMount, player->getID(), mount); + g_game().playerToggleMount(player->getID(), mount); } void ProtocolGame::parseApplyImbuement(NetworkMessage &msg) { uint8_t slot = msg.getByte(); uint32_t imbuementId = msg.get(); bool protectionCharm = msg.getByte() != 0x00; - addGameTask(&Game::playerApplyImbuement, player->getID(), imbuementId, slot, protectionCharm); + g_game().playerApplyImbuement(player->getID(), imbuementId, slot, protectionCharm); } void ProtocolGame::parseClearImbuement(NetworkMessage &msg) { uint8_t slot = msg.getByte(); - addGameTask(&Game::playerClearImbuement, player->getID(), slot); + g_game().playerClearImbuement(player->getID(), slot); } void ProtocolGame::parseCloseImbuementWindow(NetworkMessage &) { - addGameTask(&Game::playerCloseImbuementWindow, player->getID()); + g_game().playerCloseImbuementWindow(player->getID()); } void ProtocolGame::parseUseItem(NetworkMessage &msg) { @@ -1712,7 +1710,7 @@ void ProtocolGame::parseUseItem(NetworkMessage &msg) { uint16_t itemId = msg.get(); uint8_t stackpos = msg.getByte(); uint8_t index = msg.getByte(); - addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, "Game::playerUseItem", &Game::playerUseItem, player->getID(), pos, stackpos, index, itemId); + g_game().playerUseItem(player->getID(), pos, stackpos, index, itemId); } void ProtocolGame::parseUseItemEx(NetworkMessage &msg) { @@ -1722,7 +1720,7 @@ void ProtocolGame::parseUseItemEx(NetworkMessage &msg) { Position toPos = msg.getPosition(); uint16_t toItemId = msg.get(); uint8_t toStackPos = msg.getByte(); - addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, "Game::playerUseItemEx", &Game::playerUseItemEx, player->getID(), fromPos, fromStackPos, fromItemId, toPos, toStackPos, toItemId); + g_game().playerUseItemEx(player->getID(), fromPos, fromStackPos, fromItemId, toPos, toStackPos, toItemId); } void ProtocolGame::parseUseWithCreature(NetworkMessage &msg) { @@ -1730,27 +1728,27 @@ void ProtocolGame::parseUseWithCreature(NetworkMessage &msg) { uint16_t itemId = msg.get(); uint8_t fromStackPos = msg.getByte(); uint32_t creatureId = msg.get(); - addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, "Game::playerUseWithCreature", &Game::playerUseWithCreature, player->getID(), fromPos, fromStackPos, creatureId, itemId); + g_game().playerUseWithCreature(player->getID(), fromPos, fromStackPos, creatureId, itemId); } void ProtocolGame::parseCloseContainer(NetworkMessage &msg) { uint8_t cid = msg.getByte(); - addGameTask(&Game::playerCloseContainer, player->getID(), cid); + g_game().playerCloseContainer(player->getID(), cid); } void ProtocolGame::parseUpArrowContainer(NetworkMessage &msg) { uint8_t cid = msg.getByte(); - addGameTask(&Game::playerMoveUpContainer, player->getID(), cid); + g_game().playerMoveUpContainer(player->getID(), cid); } void ProtocolGame::parseUpdateContainer(NetworkMessage &msg) { uint8_t cid = msg.getByte(); - addGameTask(&Game::playerUpdateContainer, player->getID(), cid); + g_game().playerUpdateContainer(player->getID(), cid); } void ProtocolGame::parseTeleport(NetworkMessage &msg) { Position newPosition = msg.getPosition(); - addGameTask(&Game::playerTeleport, player->getID(), newPosition); + g_game().playerTeleport(player->getID(), newPosition); } void ProtocolGame::parseThrow(NetworkMessage &msg) { @@ -1761,7 +1759,7 @@ void ProtocolGame::parseThrow(NetworkMessage &msg) { uint8_t count = msg.getByte(); if (toPos != fromPos) { - addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, "Game::playerMoveThing", &Game::playerMoveThing, player->getID(), fromPos, itemId, fromStackpos, toPos, count); + g_game().playerMoveThing(player->getID(), fromPos, itemId, fromStackpos, toPos, count); } } @@ -1769,12 +1767,12 @@ void ProtocolGame::parseLookAt(NetworkMessage &msg) { Position pos = msg.getPosition(); uint16_t itemId = msg.get(); uint8_t stackpos = msg.getByte(); - addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, "Game::playerLookAt", &Game::playerLookAt, player->getID(), itemId, pos, stackpos); + g_game().playerLookAt(player->getID(), itemId, pos, stackpos); } void ProtocolGame::parseLookInBattleList(NetworkMessage &msg) { uint32_t creatureId = msg.get(); - addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, "Game::playerLookInBattleList", &Game::playerLookInBattleList, player->getID(), creatureId); + g_game().playerLookInBattleList(player->getID(), creatureId); } void ProtocolGame::parseQuickLoot(NetworkMessage &msg) { @@ -1787,7 +1785,7 @@ void ProtocolGame::parseQuickLoot(NetworkMessage &msg) { uint8_t stackpos = msg.getByte(); bool lootAllCorpses = msg.getByte(); bool autoLoot = msg.getByte(); - addGameTask(&Game::playerQuickLoot, player->getID(), pos, itemId, stackpos, nullptr, lootAllCorpses, autoLoot); + g_game().playerQuickLoot(player->getID(), pos, itemId, stackpos, nullptr, lootAllCorpses, autoLoot); } void ProtocolGame::parseLootContainer(NetworkMessage &msg) { @@ -1801,29 +1799,29 @@ void ProtocolGame::parseLootContainer(NetworkMessage &msg) { Position pos = msg.getPosition(); uint16_t itemId = msg.get(); uint8_t stackpos = msg.getByte(); - addGameTask(&Game::playerSetManagedContainer, player->getID(), category, pos, itemId, stackpos, true); + g_game().playerSetManagedContainer(player->getID(), category, pos, itemId, stackpos, true); } else if (action == 1) { ObjectCategory_t category = (ObjectCategory_t)msg.getByte(); - addGameTask(&Game::playerClearManagedContainer, player->getID(), category, true); + g_game().playerClearManagedContainer(player->getID(), category, true); } else if (action == 2) { ObjectCategory_t category = (ObjectCategory_t)msg.getByte(); - addGameTask(&Game::playerOpenManagedContainer, player->getID(), category, true); + g_game().playerOpenManagedContainer(player->getID(), category, true); } else if (action == 3) { bool useMainAsFallback = msg.getByte() == 1; - addGameTask(&Game::playerSetQuickLootFallback, player->getID(), useMainAsFallback); + g_game().playerSetQuickLootFallback(player->getID(), useMainAsFallback); } else if (action == 4) { ObjectCategory_t category = (ObjectCategory_t)msg.getByte(); Position pos = msg.getPosition(); uint16_t itemId = msg.get(); uint8_t stackpos = msg.getByte(); g_logger().debug("[{}] action {}, category {}, pos {}, itemId {}, stackPos {}", __FUNCTION__, action, static_cast(category), pos.toString(), itemId, stackpos); - addGameTask(&Game::playerSetManagedContainer, player->getID(), category, pos, itemId, stackpos, false); + g_game().playerSetManagedContainer(player->getID(), category, pos, itemId, stackpos, false); } else if (action == 5) { ObjectCategory_t category = (ObjectCategory_t)msg.getByte(); - addGameTask(&Game::playerClearManagedContainer, player->getID(), category, false); + g_game().playerClearManagedContainer(player->getID(), category, false); } else if (action == 6) { ObjectCategory_t category = (ObjectCategory_t)msg.getByte(); - addGameTask(&Game::playerOpenManagedContainer, player->getID(), category, false); + g_game().playerOpenManagedContainer(player->getID(), category, false); } g_logger().debug("[{}] action type {}", __FUNCTION__, action); @@ -1844,7 +1842,7 @@ void ProtocolGame::parseQuickLootBlackWhitelist(NetworkMessage &msg) { listedItems.push_back(msg.get()); } - addGameTask(&Game::playerQuickLootBlackWhitelist, player->getID(), filter, listedItems); + g_game().playerQuickLootBlackWhitelist(player->getID(), filter, listedItems); } void ProtocolGame::parseSay(NetworkMessage &msg) { @@ -1874,7 +1872,7 @@ void ProtocolGame::parseSay(NetworkMessage &msg) { return; } - addGameTask(&Game::playerSay, player->getID(), channelId, type, receiver, text); + g_game().playerSay(player->getID(), channelId, type, receiver, text); } void ProtocolGame::parseFightModes(NetworkMessage &msg) { @@ -1892,38 +1890,38 @@ void ProtocolGame::parseFightModes(NetworkMessage &msg) { fightMode = FIGHTMODE_DEFENSE; } - addGameTask(&Game::playerSetFightModes, player->getID(), fightMode, rawChaseMode != 0, rawSecureMode != 0); + g_game().playerSetFightModes(player->getID(), fightMode, rawChaseMode != 0, rawSecureMode != 0); } void ProtocolGame::parseAttack(NetworkMessage &msg) { uint32_t creatureId = msg.get(); // msg.get(); creatureId (same as above) - addGameTask(&Game::playerSetAttackedCreature, player->getID(), creatureId); + g_game().playerSetAttackedCreature(player->getID(), creatureId); } void ProtocolGame::parseFollow(NetworkMessage &msg) { uint32_t creatureId = msg.get(); // msg.get(); creatureId (same as above) - addGameTask(&Game::playerFollowCreature, player->getID(), creatureId); + g_game().playerFollowCreature(player->getID(), creatureId); } void ProtocolGame::parseTextWindow(NetworkMessage &msg) { uint32_t windowTextId = msg.get(); const std::string newText = msg.getString(); - addGameTask(&Game::playerWriteItem, player->getID(), windowTextId, newText); + g_game().playerWriteItem(player->getID(), windowTextId, newText); } void ProtocolGame::parseHouseWindow(NetworkMessage &msg) { uint8_t doorId = msg.getByte(); uint32_t id = msg.get(); const std::string text = msg.getString(); - addGameTask(&Game::playerUpdateHouseWindow, player->getID(), doorId, id, text); + g_game().playerUpdateHouseWindow(player->getID(), doorId, id, text); } void ProtocolGame::parseLookInShop(NetworkMessage &msg) { uint16_t id = msg.get(); uint8_t count = msg.getByte(); - addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, "Game::playerLookInShop", &Game::playerLookInShop, player->getID(), id, count); + g_game().playerLookInShop(player->getID(), id, count); } void ProtocolGame::parsePlayerBuyOnShop(NetworkMessage &msg) { @@ -1932,7 +1930,7 @@ void ProtocolGame::parsePlayerBuyOnShop(NetworkMessage &msg) { uint16_t amount = oldProtocol ? static_cast(msg.getByte()) : msg.get(); bool ignoreCap = msg.getByte() != 0; bool inBackpacks = msg.getByte() != 0; - addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, "Game::playerBuyItem", &Game::playerBuyItem, player->getID(), id, count, amount, ignoreCap, inBackpacks); + g_game().playerBuyItem(player->getID(), id, count, amount, ignoreCap, inBackpacks); } void ProtocolGame::parsePlayerSellOnShop(NetworkMessage &msg) { @@ -1941,7 +1939,7 @@ void ProtocolGame::parsePlayerSellOnShop(NetworkMessage &msg) { uint16_t amount = oldProtocol ? static_cast(msg.getByte()) : msg.get(); bool ignoreEquipped = msg.getByte() != 0; - addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, "Game::playerSellItem", &Game::playerSellItem, player->getID(), id, count, amount, ignoreEquipped); + g_game().playerSellItem(player->getID(), id, count, amount, ignoreEquipped); } void ProtocolGame::parseRequestTrade(NetworkMessage &msg) { @@ -1949,23 +1947,23 @@ void ProtocolGame::parseRequestTrade(NetworkMessage &msg) { uint16_t itemId = msg.get(); uint8_t stackpos = msg.getByte(); uint32_t playerId = msg.get(); - addGameTask(&Game::playerRequestTrade, player->getID(), pos, stackpos, playerId, itemId); + g_game().playerRequestTrade(player->getID(), pos, stackpos, playerId, itemId); } void ProtocolGame::parseLookInTrade(NetworkMessage &msg) { bool counterOffer = (msg.getByte() == 0x01); uint8_t index = msg.getByte(); - addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, "Game::playerLookInTrade", &Game::playerLookInTrade, player->getID(), counterOffer, index); + g_game().playerLookInTrade(player->getID(), counterOffer, index); } void ProtocolGame::parseAddVip(NetworkMessage &msg) { const std::string name = msg.getString(); - addGameTask(&Game::playerRequestAddVip, player->getID(), name); + g_game().playerRequestAddVip(player->getID(), name); } void ProtocolGame::parseRemoveVip(NetworkMessage &msg) { uint32_t guid = msg.get(); - addGameTask(&Game::playerRequestRemoveVip, player->getID(), guid); + g_game().playerRequestRemoveVip(player->getID(), guid); } void ProtocolGame::parseEditVip(NetworkMessage &msg) { @@ -1973,7 +1971,7 @@ void ProtocolGame::parseEditVip(NetworkMessage &msg) { const std::string description = msg.getString(); uint32_t icon = std::min(10, msg.get()); // 10 is max icon in 9.63 bool notify = msg.getByte() != 0; - addGameTask(&Game::playerRequestEditVip, player->getID(), guid, description, icon, notify); + g_game().playerRequestEditVip(player->getID(), guid, description, icon, notify); } void ProtocolGame::parseRotateItem(NetworkMessage &msg) { @@ -1982,9 +1980,9 @@ void ProtocolGame::parseRotateItem(NetworkMessage &msg) { uint8_t stackpos = msg.getByte(); const auto &itemType = Item::items[itemId]; if (itemType.isPodium) { - addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, "Game::playerRotatePodium", &Game::playerRotatePodium, player->getID(), pos, stackpos, itemId); + g_game().playerRotatePodium(player->getID(), pos, stackpos, itemId); } else { - addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, "Game::playerRotateItem", &Game::playerRotateItem, player->getID(), pos, stackpos, itemId); + g_game().playerRotateItem(player->getID(), pos, stackpos, itemId); } } @@ -1992,7 +1990,7 @@ void ProtocolGame::parseWrapableItem(NetworkMessage &msg) { Position pos = msg.getPosition(); uint16_t itemId = msg.get(); uint8_t stackpos = msg.getByte(); - addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, "Game::playerWrapableItem", &Game::playerWrapableItem, player->getID(), pos, stackpos, itemId); + g_game().playerWrapableItem(player->getID(), pos, stackpos, itemId); } void ProtocolGame::parseInspectionObject(NetworkMessage &msg) { @@ -2106,7 +2104,7 @@ void ProtocolGame::parseTaskHuntingAction(NetworkMessage &msg) { return; } - addGameTask(&Game::playerTaskHuntingAction, player->getID(), slot, action, upgrade, raceId); + g_game().playerTaskHuntingAction(player->getID(), slot, action, upgrade, raceId); } void ProtocolGame::sendHighscoresNoData() { @@ -2220,7 +2218,7 @@ void ProtocolGame::parseRuleViolationReport(NetworkMessage &msg) { msg.get(); // statement id, used to get whatever player have said, we don't log that. } - addGameTask(&Game::playerReportRuleViolationReport, player->getID(), targetName, reportType, reportReason, comment, translation); + g_game().playerReportRuleViolationReport(player->getID(), targetName, reportType, reportReason, comment, translation); } void ProtocolGame::parseBestiarysendRaces() { @@ -2942,12 +2940,12 @@ void ProtocolGame::parseBugReport(NetworkMessage &msg) { position = msg.getPosition(); } - addGameTask(&Game::playerReportBug, player->getID(), message, position, category); + g_game().playerReportBug(player->getID(), message, position, category); } void ProtocolGame::parseGreet(NetworkMessage &msg) { uint32_t npcId = msg.get(); - addGameTask(&Game::playerNpcGreet, player->getID(), npcId); + g_game().playerNpcGreet(player->getID(), npcId); } void ProtocolGame::parseDebugAssert(NetworkMessage &msg) { @@ -2961,7 +2959,7 @@ void ProtocolGame::parseDebugAssert(NetworkMessage &msg) { std::string date = msg.getString(); std::string description = msg.getString(); std::string comment = msg.getString(); - addGameTask(&Game::playerDebugAssert, player->getID(), assertLine, date, description, comment); + g_game().playerDebugAssert(player->getID(), assertLine, date, description, comment); } void ProtocolGame::parsePreyAction(NetworkMessage &msg) { @@ -2982,7 +2980,7 @@ void ProtocolGame::parsePreyAction(NetworkMessage &msg) { return; } - addGameTask(&Game::playerPreyAction, player->getID(), slot, action, option, index, raceId); + g_game().playerPreyAction(player->getID(), slot, action, option, index, raceId); } void ProtocolGame::parseSendResourceBalance() { @@ -3001,52 +2999,52 @@ void ProtocolGame::parseSendResourceBalance() { void ProtocolGame::parseInviteToParty(NetworkMessage &msg) { uint32_t targetId = msg.get(); - addGameTask(&Game::playerInviteToParty, player->getID(), targetId); + g_game().playerInviteToParty(player->getID(), targetId); } void ProtocolGame::parseJoinParty(NetworkMessage &msg) { uint32_t targetId = msg.get(); - addGameTask(&Game::playerJoinParty, player->getID(), targetId); + g_game().playerJoinParty(player->getID(), targetId); } void ProtocolGame::parseRevokePartyInvite(NetworkMessage &msg) { uint32_t targetId = msg.get(); - addGameTask(&Game::playerRevokePartyInvitation, player->getID(), targetId); + g_game().playerRevokePartyInvitation(player->getID(), targetId); } void ProtocolGame::parsePassPartyLeadership(NetworkMessage &msg) { uint32_t targetId = msg.get(); - addGameTask(&Game::playerPassPartyLeadership, player->getID(), targetId); + g_game().playerPassPartyLeadership(player->getID(), targetId); } void ProtocolGame::parseEnableSharedPartyExperience(NetworkMessage &msg) { bool sharedExpActive = msg.getByte() == 1; - addGameTask(&Game::playerEnableSharedPartyExperience, player->getID(), sharedExpActive); + g_game().playerEnableSharedPartyExperience(player->getID(), sharedExpActive); } void ProtocolGame::parseQuestLine(NetworkMessage &msg) { uint16_t questId = msg.get(); - addGameTask(&Game::playerShowQuestLine, player->getID(), questId); + g_game().playerShowQuestLine(player->getID(), questId); } void ProtocolGame::parseMarketLeave() { - addGameTask(&Game::playerLeaveMarket, player->getID()); + g_game().playerLeaveMarket(player->getID()); } void ProtocolGame::parseMarketBrowse(NetworkMessage &msg) { uint16_t browseId = oldProtocol ? msg.get() : static_cast(msg.getByte()); if ((oldProtocol && browseId == MARKETREQUEST_OWN_OFFERS_OLD) || (!oldProtocol && browseId == MARKETREQUEST_OWN_OFFERS)) { - addGameTask(&Game::playerBrowseMarketOwnOffers, player->getID()); + g_game().playerBrowseMarketOwnOffers(player->getID()); } else if ((oldProtocol && browseId == MARKETREQUEST_OWN_HISTORY_OLD) || (!oldProtocol && browseId == MARKETREQUEST_OWN_HISTORY)) { - addGameTask(&Game::playerBrowseMarketOwnHistory, player->getID()); + g_game().playerBrowseMarketOwnHistory(player->getID()); } else if (!oldProtocol) { uint16_t itemId = msg.get(); uint8_t tier = msg.get(); player->sendMarketEnter(player->getLastDepotId()); - addGameTask(&Game::playerBrowseMarket, player->getID(), itemId, tier); + g_game().playerBrowseMarket(player->getID(), itemId, tier); } else { - addGameTask(&Game::playerBrowseMarket, player->getID(), browseId, 0); + g_game().playerBrowseMarket(player->getID(), browseId, 0); } } @@ -3062,7 +3060,7 @@ void ProtocolGame::parseMarketCreateOffer(NetworkMessage &msg) { uint64_t price = oldProtocol ? static_cast(msg.get()) : msg.get(); bool anonymous = (msg.getByte() != 0); if (amount > 0 && price > 0) { - addGameTask(&Game::playerCreateMarketOffer, player->getID(), type, itemId, amount, price, itemTier, anonymous); + g_game().playerCreateMarketOffer(player->getID(), type, itemId, amount, price, itemTier, anonymous); } } @@ -3070,7 +3068,7 @@ void ProtocolGame::parseMarketCancelOffer(NetworkMessage &msg) { uint32_t timestamp = msg.get(); uint16_t counter = msg.get(); if (counter > 0) { - addGameTask(&Game::playerCancelMarketOffer, player->getID(), timestamp, counter); + g_game().playerCancelMarketOffer(player->getID(), timestamp, counter); } updateCoinBalance(); @@ -3081,7 +3079,7 @@ void ProtocolGame::parseMarketAcceptOffer(NetworkMessage &msg) { uint16_t counter = msg.get(); uint16_t amount = msg.get(); if (amount > 0 && counter > 0) { - addGameTask(&Game::playerAcceptMarketOffer, player->getID(), timestamp, counter, amount); + g_game().playerAcceptMarketOffer(player->getID(), timestamp, counter, amount); } updateCoinBalance(); @@ -3091,7 +3089,7 @@ void ProtocolGame::parseModalWindowAnswer(NetworkMessage &msg) { uint32_t id = msg.get(); uint8_t button = msg.getByte(); uint8_t choice = msg.getByte(); - addGameTask(&Game::playerAnswerModalWindow, player->getID(), id, button, choice); + g_game().playerAnswerModalWindow(player->getID(), id, button, choice); } void ProtocolGame::parseRewardChestCollect(NetworkMessage &msg) { @@ -3106,19 +3104,19 @@ void ProtocolGame::parseRewardChestCollect(NetworkMessage &msg) { } auto maxCollectItems = g_configManager().getNumber(REWARD_CHEST_MAX_COLLECT_ITEMS, __FUNCTION__); - addGameTask(&Game::playerRewardChestCollect, player->getID(), position, itemId, stackPosition, maxCollectItems); + g_game().playerRewardChestCollect(player->getID(), position, itemId, stackPosition, maxCollectItems); } void ProtocolGame::parseBrowseField(NetworkMessage &msg) { const Position &pos = msg.getPosition(); - addGameTask(&Game::playerBrowseField, player->getID(), pos); + g_game().playerBrowseField(player->getID(), pos); } void ProtocolGame::parseSeekInContainer(NetworkMessage &msg) { uint8_t containerId = msg.getByte(); uint16_t index = msg.get(); auto primaryType = msg.getByte(); - addGameTask(&Game::playerSeekInContainer, player->getID(), containerId, index, primaryType); + g_game().playerSeekInContainer(player->getID(), containerId, index, primaryType); } // Send methods @@ -4660,23 +4658,18 @@ void ProtocolGame::updateCoinBalance() { return; } - g_dispatcher().addEvent( - std::bind( - [](uint32_t playerId) { - auto threadPlayer = g_game().getPlayerByID(playerId); - if (threadPlayer && threadPlayer->getAccount()) { - auto [coins, errCoin] = threadPlayer->getAccount()->getCoins(enumToValue(CoinType::Normal)); - auto [transferCoins, errTCoin] = threadPlayer->getAccount()->getCoins(enumToValue(CoinType::Transferable)); + g_dispatcher().addEvent([playerId = player->getID()] { + const auto &threadPlayer = g_game().getPlayerByID(playerId); + if (threadPlayer && threadPlayer->getAccount()) { + const auto [coins, errCoin] = threadPlayer->getAccount()->getCoins(enumToValue(CoinType::Normal)); + const auto [transferCoins, errTCoin] = threadPlayer->getAccount()->getCoins(enumToValue(CoinType::Transferable)); - threadPlayer->coinBalance = coins; - threadPlayer->coinTransferableBalance = transferCoins; - threadPlayer->sendCoinBalance(); - } - }, - player->getID() - ), - "ProtocolGame::updateCoinBalance" - ); + threadPlayer->coinBalance = coins; + threadPlayer->coinTransferableBalance = transferCoins; + threadPlayer->sendCoinBalance(); + } + }, + "ProtocolGame::updateCoinBalance"); } void ProtocolGame::sendMarketLeave() { @@ -5224,11 +5217,11 @@ void ProtocolGame::parseForgeEnter(NetworkMessage &msg) { bool usedCore = msg.getByte(); bool reduceTierLoss = msg.getByte(); if (actionType == ForgeAction_t::FUSION) { - addGameTask(&Game::playerForgeFuseItems, player->getID(), actionType, firstItem, tier, secondItem, usedCore, reduceTierLoss, convergence); + g_game().playerForgeFuseItems(player->getID(), actionType, firstItem, tier, secondItem, usedCore, reduceTierLoss, convergence); } else if (actionType == ForgeAction_t::TRANSFER) { - addGameTask(&Game::playerForgeTransferItemTier, player->getID(), actionType, firstItem, tier, secondItem, convergence); + g_game().playerForgeTransferItemTier(player->getID(), actionType, firstItem, tier, secondItem, convergence); } else if (actionType <= ForgeAction_t::INCREASELIMIT) { - addGameTask(&Game::playerForgeResourceConversion, player->getID(), actionType); + g_game().playerForgeResourceConversion(player->getID(), actionType); } } @@ -5237,7 +5230,7 @@ void ProtocolGame::parseForgeBrowseHistory(NetworkMessage &msg) { return; } - addGameTask(&Game::playerBrowseForgeHistory, player->getID(), msg.getByte()); + g_game().playerBrowseForgeHistory(player->getID(), msg.getByte()); } void ProtocolGame::sendForgeResult(ForgeAction_t actionType, uint16_t leftItemId, uint8_t leftTier, uint16_t rightItemId, uint8_t rightTier, bool success, uint8_t bonus, uint8_t coreCount, bool convergence) { @@ -7884,7 +7877,7 @@ void ProtocolGame::parseExtendedOpcode(NetworkMessage &msg) { const std::string &buffer = msg.getString(); // process additional opcodes via lua script event - addGameTask(&Game::parsePlayerExtendedOpcode, player->getID(), opcode, buffer); + g_game().parsePlayerExtendedOpcode(player->getID(), opcode, buffer); } // OTCv8 @@ -7917,7 +7910,7 @@ void ProtocolGame::parseInventoryImbuements(NetworkMessage &msg) { } bool isTrackerOpen = msg.getByte(); // Window is opened or closed - addGameTask(&Game::playerRequestInventoryImbuements, player->getID(), isTrackerOpen); + g_game().playerRequestInventoryImbuements(player->getID(), isTrackerOpen); } void ProtocolGame::sendInventoryImbuements(const std::map> items) { @@ -8082,28 +8075,28 @@ void ProtocolGame::parseStashWithdraw(NetworkMessage &msg) { uint16_t itemId = msg.get(); uint8_t stackpos = msg.getByte(); uint32_t count = msg.getByte(); - addGameTask(&Game::playerStowItem, player->getID(), pos, itemId, stackpos, count, false); + g_game().playerStowItem(player->getID(), pos, itemId, stackpos, count, false); break; } case SUPPLY_STASH_ACTION_STOW_CONTAINER: { Position pos = msg.getPosition(); uint16_t itemId = msg.get(); uint8_t stackpos = msg.getByte(); - addGameTask(&Game::playerStowItem, player->getID(), pos, itemId, stackpos, 0, false); + g_game().playerStowItem(player->getID(), pos, itemId, stackpos, 0, false); break; } case SUPPLY_STASH_ACTION_STOW_STACK: { Position pos = msg.getPosition(); uint16_t itemId = msg.get(); uint8_t stackpos = msg.getByte(); - addGameTask(&Game::playerStowItem, player->getID(), pos, itemId, stackpos, 0, true); + g_game().playerStowItem(player->getID(), pos, itemId, stackpos, 0, true); break; } case SUPPLY_STASH_ACTION_WITHDRAW: { uint16_t itemId = msg.get(); uint32_t count = msg.get(); uint8_t stackpos = msg.getByte(); - addGameTask(&Game::playerStashWithdraw, player->getID(), itemId, count, stackpos); + g_game().playerStashWithdraw(player->getID(), itemId, count, stackpos); break; } default: @@ -8196,7 +8189,7 @@ void ProtocolGame::parseOpenDepotSearch() { return; } - addGameTask(&Game::playerRequestDepotItems, player->getID()); + g_game().playerRequestDepotItems(player->getID()); } void ProtocolGame::parseCloseDepotSearch() { @@ -8204,7 +8197,7 @@ void ProtocolGame::parseCloseDepotSearch() { return; } - addGameTask(&Game::playerRequestCloseDepotSearch, player->getID()); + g_game().playerRequestCloseDepotSearch(player->getID()); } void ProtocolGame::parseDepotSearchItemRequest(NetworkMessage &msg) { @@ -8218,7 +8211,7 @@ void ProtocolGame::parseDepotSearchItemRequest(NetworkMessage &msg) { itemTier = msg.getByte(); } - addGameTask(&Game::playerRequestDepotSearchItem, player->getID(), itemId, itemTier); + g_game().playerRequestDepotSearchItem(player->getID(), itemId, itemTier); } void ProtocolGame::parseRetrieveDepotSearch(NetworkMessage &msg) { @@ -8233,7 +8226,7 @@ void ProtocolGame::parseRetrieveDepotSearch(NetworkMessage &msg) { } uint8_t type = msg.getByte(); - addGameTask(&Game::playerRequestDepotSearchRetrieve, player->getID(), itemId, itemTier, type); + g_game().playerRequestDepotSearchRetrieve(player->getID(), itemId, itemTier, type); } void ProtocolGame::parseOpenParentContainer(NetworkMessage &msg) { @@ -8242,7 +8235,7 @@ void ProtocolGame::parseOpenParentContainer(NetworkMessage &msg) { } Position pos = msg.getPosition(); - addGameTask(&Game::playerRequestOpenContainerFromDepotSearch, player->getID(), pos); + g_game().playerRequestOpenContainerFromDepotSearch(player->getID(), pos); } void ProtocolGame::sendUpdateCreature(std::shared_ptr creature) { @@ -8530,7 +8523,7 @@ void ProtocolGame::parseBosstiarySlot(NetworkMessage &msg) { uint8_t slotBossId = msg.getByte(); uint32_t selectedBossId = msg.get(); - addGameTask(&Game::playerBosstiarySlot, player->getID(), slotBossId, selectedBossId); + g_game().playerBosstiarySlot(player->getID(), slotBossId, selectedBossId); } void ProtocolGame::sendPodiumDetails(NetworkMessage &msg, const std::vector &toSendMonsters, bool isBoss) const { @@ -8745,7 +8738,7 @@ void ProtocolGame::parseOpenWheel(NetworkMessage &msg) { } auto ownerId = msg.get(); - addGameTask(&Game::playerOpenWheel, player->getID(), ownerId); + g_game().playerOpenWheel(player->getID(), ownerId); } void ProtocolGame::parseWheelGemAction(NetworkMessage &msg) { @@ -8753,7 +8746,7 @@ void ProtocolGame::parseWheelGemAction(NetworkMessage &msg) { return; } - addGameTask(&Game::playerWheelGemAction, player->getID(), msg); + g_game().playerWheelGemAction(player->getID(), msg); } void ProtocolGame::sendOpenWheelWindow(uint32_t ownerId) { @@ -8771,7 +8764,7 @@ void ProtocolGame::parseSaveWheel(NetworkMessage &msg) { return; } - addGameTask(&Game::playerSaveWheel, player->getID(), msg); + g_game().playerSaveWheel(player->getID(), msg); } void ProtocolGame::sendDisableLoginMusic() { diff --git a/src/server/network/protocol/protocolgame.hpp b/src/server/network/protocol/protocolgame.hpp index 61c3aa19af0..306bb873f25 100644 --- a/src/server/network/protocol/protocolgame.hpp +++ b/src/server/network/protocol/protocolgame.hpp @@ -73,12 +73,6 @@ class ProtocolGame final : public Protocol { } private: - // Helpers so we don't need to bind every time - template - void addGameTask(Callable function, Args &&... args); - template - void addGameTaskTimed(uint32_t delay, std::string_view context, Callable function, Args &&... args); - ProtocolGame_ptr getThis() { return std::static_pointer_cast(shared_from_this()); } diff --git a/src/server/network/protocol/protocollogin.cpp b/src/server/network/protocol/protocollogin.cpp index 4e38a7503c7..b59a3704429 100644 --- a/src/server/network/protocol/protocollogin.cpp +++ b/src/server/network/protocol/protocollogin.cpp @@ -174,6 +174,8 @@ void ProtocolLogin::onRecvFirstMessage(NetworkMessage &msg) { return; } - auto thisPtr = std::static_pointer_cast(shared_from_this()); - g_dispatcher().addEvent(std::bind(&ProtocolLogin::getCharacterList, thisPtr, accountDescriptor, password), "ProtocolLogin::getCharacterList"); + g_dispatcher().addEvent([self = std::static_pointer_cast(shared_from_this()), accountDescriptor, password] { + self->getCharacterList(accountDescriptor, password); + }, + "ProtocolLogin::getCharacterList"); } diff --git a/src/server/network/protocol/protocolstatus.cpp b/src/server/network/protocol/protocolstatus.cpp index 8d9f245f5df..a4dfe1651d6 100644 --- a/src/server/network/protocol/protocolstatus.cpp +++ b/src/server/network/protocol/protocolstatus.cpp @@ -44,7 +44,10 @@ void ProtocolStatus::onRecvFirstMessage(NetworkMessage &msg) { // XML info protocol case 0xFF: { if (msg.getString(4) == "info") { - g_dispatcher().addEvent(std::bind(&ProtocolStatus::sendStatusString, std::static_pointer_cast(shared_from_this())), "ProtocolStatus::sendStatusString"); + g_dispatcher().addEvent([self = std::static_pointer_cast(shared_from_this())] { + self->sendStatusString(); + }, + "ProtocolStatus::sendStatusString"); return; } break; @@ -57,7 +60,11 @@ void ProtocolStatus::onRecvFirstMessage(NetworkMessage &msg) { if (requestedInfo & REQUEST_PLAYER_STATUS_INFO) { characterName = msg.getString(); } - g_dispatcher().addEvent(std::bind(&ProtocolStatus::sendInfo, std::static_pointer_cast(shared_from_this()), requestedInfo, characterName), "ProtocolStatus::sendInfo"); + g_dispatcher().addEvent([self = std::static_pointer_cast(shared_from_this()), requestedInfo, characterName] { + self->sendInfo(requestedInfo, characterName); + }, + "ProtocolStatus::sendInfo"); + return; } diff --git a/src/server/server.cpp b/src/server/server.cpp index 2968033bafa..fc8690468fe 100644 --- a/src/server/server.cpp +++ b/src/server/server.cpp @@ -47,7 +47,7 @@ void ServiceManager::stop() { for (auto &servicePortIt : acceptors) { try { - io_service.post(std::bind_front(&ServicePort::onStopServer, servicePortIt.second)); + io_service.post([servicePort = servicePortIt.second] { servicePort->onStopServer(); }); } catch (const std::system_error &e) { g_logger().warn("[ServiceManager::stop] - Network error: {}", e.what()); } @@ -56,7 +56,9 @@ void ServiceManager::stop() { acceptors.clear(); death_timer.expires_from_now(std::chrono::seconds(3)); - death_timer.async_wait(std::bind(&ServiceManager::die, this)); + death_timer.async_wait([this](const std::error_code &err) { + die(); + }); } ServicePort::~ServicePort() { @@ -87,7 +89,7 @@ void ServicePort::accept() { } auto connection = ConnectionManager::getInstance().createConnection(io_service, shared_from_this()); - acceptor->async_accept(connection->getSocket(), std::bind(&ServicePort::onAccept, shared_from_this(), connection, std::placeholders::_1)); + acceptor->async_accept(connection->getSocket(), [self = shared_from_this(), connection](const std::error_code &error) { self->onAccept(connection, error); }); } void ServicePort::onAccept(Connection_ptr connection, const std::error_code &error) { @@ -113,7 +115,9 @@ void ServicePort::onAccept(Connection_ptr connection, const std::error_code &err if (!pendingStart) { close(); pendingStart = true; - g_dispatcher().scheduleEvent(15000, std::bind_front(&ServicePort::openAcceptor, std::weak_ptr(shared_from_this()), serverPort), "ServicePort::openAcceptor"); + g_dispatcher().scheduleEvent( + 15000, [self = shared_from_this(), serverPort = serverPort] { ServicePort::openAcceptor(std::weak_ptr(self), serverPort); }, "ServicePort::openAcceptor" + ); } } } @@ -162,7 +166,10 @@ void ServicePort::open(uint16_t port) { g_logger().warn("[ServicePort::open] - Error code: {}", e.what()); pendingStart = true; - g_dispatcher().scheduleEvent(15000, std::bind_front(&ServicePort::openAcceptor, std::weak_ptr(shared_from_this()), port), "ServicePort::openAcceptor"); + g_dispatcher().scheduleEvent( + 15000, + [self = shared_from_this(), port] { ServicePort::openAcceptor(std::weak_ptr(self), port); }, "ServicePort::openAcceptor" + ); } } From 5879ed5fe1c3f754ee69dad36b7fbb39f2f3d4fb Mon Sep 17 00:00:00 2001 From: Eduardo Dantas Date: Fri, 22 Mar 2024 18:22:29 -0300 Subject: [PATCH 2/6] fix: client update blockable spawn monsters with god (#2482) Resolves #2479 --- src/creatures/monsters/spawns/spawn_monster.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/creatures/monsters/spawns/spawn_monster.cpp b/src/creatures/monsters/spawns/spawn_monster.cpp index 5d9f386e25f..21eab1a7c0b 100644 --- a/src/creatures/monsters/spawns/spawn_monster.cpp +++ b/src/creatures/monsters/spawns/spawn_monster.cpp @@ -180,6 +180,7 @@ bool SpawnMonster::spawnMonster(uint32_t spawnMonsterId, spawnBlock_t &sb, const return false; } } else { + g_logger().debug("[SpawnMonster] Spawning {} at {}", monsterType->name, sb.pos.toString()); if (!g_game().placeCreature(monster, sb.pos, false, true)) { return false; } @@ -260,7 +261,7 @@ void SpawnMonster::checkSpawnMonster() { } if (mType->info.isBlockable) { - spawnMonster(spawnMonsterId, sb, mType, true); + spawnMonster(spawnMonsterId, sb, mType); } else { scheduleSpawn(spawnMonsterId, sb, mType, 3 * NONBLOCKABLE_SPAWN_MONSTER_INTERVAL); } From ed2dbaeb1d55d49b1f7fce3c3744791d38fd232e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Lu=C3=ADs=20Lucarelo=20Lamonato?= Date: Fri, 22 Mar 2024 22:25:14 -0300 Subject: [PATCH 3/6] fix: missing function loadPlayerInstantSpellList in IOLoginData (#2483) Makes the player forget the spells he learned from NPC --- src/io/iologindata.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/io/iologindata.cpp b/src/io/iologindata.cpp index 7e113ef901e..4c6b95dc32e 100644 --- a/src/io/iologindata.cpp +++ b/src/io/iologindata.cpp @@ -172,6 +172,9 @@ bool IOLoginData::loadPlayer(std::shared_ptr player, DBResult_ptr result // Load task hunting class IOLoginDataLoad::loadPlayerTaskHuntingClass(player, result); + // Load instant spells list + IOLoginDataLoad::loadPlayerInstantSpellList(player, result); + if (disableIrrelevantInfo) { return true; } From b56395833a3eb39faf1ec3d65cb40e06a10bdc2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Lu=C3=ADs=20Lucarelo=20Lamonato?= Date: Sat, 23 Mar 2024 16:47:28 -0300 Subject: [PATCH 4/6] fix: possibility of stashing items that are far away (#2489) --- src/game/game.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/game/game.cpp b/src/game/game.cpp index c69b481e32c..6754e990595 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -1518,11 +1518,6 @@ void Game::playerMoveItem(std::shared_ptr player, const Position &fromPo return; } - if (isTryingToStow(toPos, toCylinder)) { - player->stowItem(item, count, false); - return; - } - if (!item->isPushable() || item->hasAttribute(ItemAttribute_t::UNIQUEID)) { player->sendCancelMessage(RETURNVALUE_NOTMOVABLE); return; @@ -1654,7 +1649,12 @@ void Game::playerMoveItem(std::shared_ptr player, const Position &fromPo return; } } - + + if (isTryingToStow(toPos, toCylinder)) { + player->stowItem(item, count, false); + return; + } + ReturnValue ret = internalMoveItem(fromCylinder, toCylinder, toIndex, item, count, nullptr, 0, player); if (ret != RETURNVALUE_NOERROR) { player->sendCancelMessage(ret); From 9ccfe62fdc74932149bbf3895c2022b065f34208 Mon Sep 17 00:00:00 2001 From: Beats Date: Mon, 25 Mar 2024 10:01:15 -0400 Subject: [PATCH 5/6] fix: guard all depot in stash (#2491) --- src/creatures/players/player.cpp | 8 ++++---- src/game/game.cpp | 14 +++++++------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/creatures/players/player.cpp b/src/creatures/players/player.cpp index 76ca1755c70..75e5b5ef2af 100644 --- a/src/creatures/players/player.cpp +++ b/src/creatures/players/player.cpp @@ -6452,7 +6452,7 @@ void Player::stowItem(std::shared_ptr item, uint32_t count, bool allItems) // Stow locker items std::shared_ptr depotLocker = getDepotLocker(getLastDepotId()); auto [itemVector, itemMap] = requestLockerItems(depotLocker); - for (auto lockerItem : itemVector) { + for (const auto &lockerItem : itemVector) { if (lockerItem == nullptr) { break; } @@ -6463,7 +6463,7 @@ void Player::stowItem(std::shared_ptr item, uint32_t count, bool allItems) } } else if (item->getContainer()) { itemDict = item->getContainer()->getStowableItems(); - for (std::shared_ptr containerItem : item->getContainer()->getItems(true)) { + for (const std::shared_ptr &containerItem : item->getContainer()->getItems(true)) { uint32_t depotChest = g_configManager().getNumber(DEPOTCHEST, __FUNCTION__); bool validDepot = depotChest > 0 && depotChest < 21; if (g_configManager().getBoolean(STASH_MOVING, __FUNCTION__) && containerItem && !containerItem->isStackable() && validDepot) { @@ -6473,10 +6473,10 @@ void Player::stowItem(std::shared_ptr item, uint32_t count, bool allItems) } } } else { - itemDict.push_back(std::pair, uint32_t>(item, count)); + itemDict.emplace_back(item, count); } - if (itemDict.size() == 0) { + if (itemDict.empty()) { sendCancelMessage("There is no stowable items on this container."); return; } diff --git a/src/game/game.cpp b/src/game/game.cpp index 6754e990595..11ed432da15 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -1518,11 +1518,6 @@ void Game::playerMoveItem(std::shared_ptr player, const Position &fromPo return; } - if (!item->isPushable() || item->hasAttribute(ItemAttribute_t::UNIQUEID)) { - player->sendCancelMessage(RETURNVALUE_NOTMOVABLE); - return; - } - const Position &playerPos = player->getPosition(); auto cylinderTile = fromCylinder->getTile(); const Position &mapFromPos = cylinderTile ? cylinderTile->getPosition() : item->getPosition(); @@ -1649,12 +1644,17 @@ void Game::playerMoveItem(std::shared_ptr player, const Position &fromPo return; } } - + if (isTryingToStow(toPos, toCylinder)) { player->stowItem(item, count, false); return; } - + + if (!item->isPushable() || item->hasAttribute(ItemAttribute_t::UNIQUEID)) { + player->sendCancelMessage(RETURNVALUE_NOTMOVABLE); + return; + } + ReturnValue ret = internalMoveItem(fromCylinder, toCylinder, toIndex, item, count, nullptr, 0, player); if (ret != RETURNVALUE_NOERROR) { player->sendCancelMessage(ret); From 8dec6f96aa85bf6873c5a6598e538b7fb8853d8e Mon Sep 17 00:00:00 2001 From: Leandro Date: Mon, 25 Mar 2024 22:31:55 -0300 Subject: [PATCH 6/6] fix: auto loot reachable corpses only (#2473) --- src/creatures/creature.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/creatures/creature.cpp b/src/creatures/creature.cpp index 6ee605769a7..6101ff0473d 100644 --- a/src/creatures/creature.cpp +++ b/src/creatures/creature.cpp @@ -813,7 +813,17 @@ bool Creature::dropCorpse(std::shared_ptr lastHitCreature, std::shared player->sendLootMessage(lootMessage.str()); } - if (player->checkAutoLoot(monster->isRewardBoss()) && corpseContainer && mostDamageCreature->getPlayer()) { + stdext::arraylist dirList(128); + FindPathParams fpp; + fpp.minTargetDist = 0; + fpp.maxTargetDist = 1; + fpp.fullPathSearch = true; + fpp.clearSight = true; + fpp.maxSearchDist = 0; + + auto isReachable = g_game().map.getPathMatching(player->getPosition(), dirList, FrozenPathingConditionCall(corpse->getPosition()), fpp); + + if (player->checkAutoLoot(monster->isRewardBoss()) && corpseContainer && mostDamageCreature->getPlayer() && isReachable) { g_dispatcher().addEvent([player, corpseContainer, corpsePosition = corpse->getPosition()] { g_game().playerQuickLootCorpse(player, corpseContainer, corpsePosition); },