Skip to content

Commit

Permalink
review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ArturKnopik committed Dec 27, 2024
1 parent 32d01a9 commit 3f23308
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 40 deletions.
57 changes: 27 additions & 30 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ void Game::playerMoveThing(uint32_t playerId, const Position& fromPos, uint16_t
}

if (movingCreature->getPosition().isInRange(player->getPosition(), 1, 1, 0)) {
SchedulerTask_ptr task = createSchedulerTask(
auto task = createSchedulerTask(
MOVE_CREATURE_INTERVAL, [=, this, playerID = player->getID(), creatureID = movingCreature->getID()]() {
playerMoveCreatureByID(playerID, creatureID, fromPos, toPos);
});
Expand Down Expand Up @@ -703,12 +703,11 @@ void Game::playerMoveCreature(Player* player, Creature* movingCreature, const Po
g_dispatcher.addTask([this, playerID = player->getID(), listDir = std::move(listDir)]() {
playerAutoWalk(playerID, listDir);
});
SchedulerTask_ptr task =
createSchedulerTask(RANGE_MOVE_CREATURE_INTERVAL, [=, this, playerID = player->getID(),
movingCreatureID = movingCreature->getID(),
toPos = toTile->getPosition()] {
playerMoveCreatureByID(playerID, movingCreatureID, movingCreatureOrigPos, toPos);
});
auto task = createSchedulerTask(RANGE_MOVE_CREATURE_INTERVAL, [=, this, playerID = player->getID(),
movingCreatureID = movingCreature->getID(),
toPos = toTile->getPosition()] {
playerMoveCreatureByID(playerID, movingCreatureID, movingCreatureOrigPos, toPos);
});
player->setNextWalkActionTask(std::move(task));
} else {
player->sendCancelMessage(RETURNVALUE_THEREISNOWAY);
Expand Down Expand Up @@ -891,7 +890,7 @@ void Game::playerMoveItem(Player* player, const Position& fromPos, uint16_t spri
{
if (!player->canDoAction()) {
uint32_t delay = player->getNextActionTime();
SchedulerTask_ptr task = createSchedulerTask(delay, [=, this, playerID = player->getID()]() {
auto task = createSchedulerTask(delay, [=, this, playerID = player->getID()]() {
playerMoveItemByPlayerID(playerID, fromPos, spriteId, fromStackPos, toPos, count);
});
player->setNextActionTask(std::move(task));
Expand Down Expand Up @@ -960,10 +959,9 @@ void Game::playerMoveItem(Player* player, const Position& fromPos, uint16_t spri
g_dispatcher.addTask([this, playerID = player->getID(), listDir = std::move(listDir)]() {
playerAutoWalk(playerID, listDir);
});
SchedulerTask_ptr task =
createSchedulerTask(RANGE_MOVE_ITEM_INTERVAL, [=, this, playerID = player->getID()]() {
playerMoveItemByPlayerID(playerID, fromPos, spriteId, fromStackPos, toPos, count);
});
auto task = createSchedulerTask(RANGE_MOVE_ITEM_INTERVAL, [=, this, playerID = player->getID()]() {
playerMoveItemByPlayerID(playerID, fromPos, spriteId, fromStackPos, toPos, count);
});
player->setNextWalkActionTask(std::move(task));
} else {
player->sendCancelMessage(RETURNVALUE_THEREISNOWAY);
Expand Down Expand Up @@ -1022,11 +1020,10 @@ void Game::playerMoveItem(Player* player, const Position& fromPos, uint16_t spri
g_dispatcher.addTask([this, playerID = player->getID(), listDir = std::move(listDir)]() {
playerAutoWalk(playerID, listDir);
});
SchedulerTask_ptr task = createSchedulerTask(
RANGE_MOVE_ITEM_INTERVAL,
[this, playerID = player->getID(), itemPos, spriteId, itemStackPos, toPos, count]() {
playerMoveItemByPlayerID(playerID, itemPos, spriteId, itemStackPos, toPos, count);
});
auto task = createSchedulerTask(RANGE_MOVE_ITEM_INTERVAL, [this, playerID = player->getID(), itemPos,
spriteId, itemStackPos, toPos, count]() {
playerMoveItemByPlayerID(playerID, itemPos, spriteId, itemStackPos, toPos, count);
});
player->setNextWalkActionTask(std::move(task));
} else {
player->sendCancelMessage(RETURNVALUE_THEREISNOWAY);
Expand Down Expand Up @@ -2137,7 +2134,7 @@ void Game::playerUseItemEx(uint32_t playerId, const Position& fromPos, uint8_t f
g_dispatcher.addTask([this, playerID = player->getID(), listDir = std::move(listDir)]() {
playerAutoWalk(playerID, listDir);
});
SchedulerTask_ptr task = createSchedulerTask(RANGE_USE_ITEM_EX_INTERVAL, [=, this]() {
auto task = createSchedulerTask(RANGE_USE_ITEM_EX_INTERVAL, [=, this]() {
playerUseItemEx(playerId, itemPos, itemStackPos, fromSpriteId, toPos, toStackPos, toSpriteId);
});
player->setNextWalkActionTask(std::move(task));
Expand All @@ -2153,7 +2150,7 @@ void Game::playerUseItemEx(uint32_t playerId, const Position& fromPos, uint8_t f

if (!player->canDoAction()) {
uint32_t delay = player->getNextActionTime();
SchedulerTask_ptr task = createSchedulerTask(delay, [=, this]() {
auto task = createSchedulerTask(delay, [=, this]() {
playerUseItemEx(playerId, fromPos, fromStackPos, fromSpriteId, toPos, toStackPos, toSpriteId);
});
player->setNextActionTask(std::move(task));
Expand Down Expand Up @@ -2198,7 +2195,7 @@ void Game::playerUseItem(uint32_t playerId, const Position& pos, uint8_t stackPo
g_dispatcher.addTask([this, playerID = player->getID(), listDir = std::move(listDir)]() {
playerAutoWalk(playerID, listDir);
});
SchedulerTask_ptr task = createSchedulerTask(
auto task = createSchedulerTask(
RANGE_USE_ITEM_INTERVAL, [=, this]() { playerUseItem(playerId, pos, stackPos, index, spriteId); });
player->setNextWalkActionTask(std::move(task));
return;
Expand All @@ -2213,7 +2210,7 @@ void Game::playerUseItem(uint32_t playerId, const Position& pos, uint8_t stackPo

if (!player->canDoAction()) {
uint32_t delay = player->getNextActionTime();
SchedulerTask_ptr task =
auto task =
createSchedulerTask(delay, [=, this]() { playerUseItem(playerId, pos, stackPos, index, spriteId); });
player->setNextActionTask(std::move(task));
return;
Expand Down Expand Up @@ -2297,7 +2294,7 @@ void Game::playerUseWithCreature(uint32_t playerId, const Position& fromPos, uin
g_dispatcher.addTask([this, playerID = player->getID(), listDir = std::move(listDir)]() {
playerAutoWalk(playerID, listDir);
});
SchedulerTask_ptr task = createSchedulerTask(RANGE_USE_WITH_CREATURE_INTERVAL, [=, this]() {
auto task = createSchedulerTask(RANGE_USE_WITH_CREATURE_INTERVAL, [=, this]() {
playerUseWithCreature(playerId, itemPos, itemStackPos, creatureId, spriteId);
});
player->setNextWalkActionTask(std::move(task));
Expand All @@ -2313,7 +2310,7 @@ void Game::playerUseWithCreature(uint32_t playerId, const Position& fromPos, uin

if (!player->canDoAction()) {
uint32_t delay = player->getNextActionTime();
SchedulerTask_ptr task = createSchedulerTask(
auto task = createSchedulerTask(
delay, [=, this]() { playerUseWithCreature(playerId, fromPos, fromStackPos, creatureId, spriteId); });
player->setNextActionTask(std::move(task));
return;
Expand Down Expand Up @@ -2416,8 +2413,8 @@ void Game::playerRotateItem(uint32_t playerId, const Position& pos, uint8_t stac
g_dispatcher.addTask([this, playerID = player->getID(), listDir = std::move(listDir)]() {
playerAutoWalk(playerID, listDir);
});
SchedulerTask_ptr task = createSchedulerTask(
RANGE_ROTATE_ITEM_INTERVAL, [=, this]() { playerRotateItem(playerId, pos, stackPos, spriteId); });
auto task = createSchedulerTask(RANGE_ROTATE_ITEM_INTERVAL,
[=, this]() { playerRotateItem(playerId, pos, stackPos, spriteId); });
player->setNextWalkActionTask(std::move(task));
} else {
player->sendCancelMessage(RETURNVALUE_THEREISNOWAY);
Expand Down Expand Up @@ -2512,7 +2509,7 @@ void Game::playerBrowseField(uint32_t playerId, const Position& pos)
g_dispatcher.addTask([this, playerID = player->getID(), listDir = std::move(listDir)]() {
playerAutoWalk(playerID, listDir);
});
SchedulerTask_ptr task =
auto task =
createSchedulerTask(RANGE_BROWSE_FIELD_INTERVAL, [=, this]() { playerBrowseField(playerId, pos); });
player->setNextWalkActionTask(std::move(task));
} else {
Expand Down Expand Up @@ -2618,8 +2615,8 @@ void Game::playerWrapItem(uint32_t playerId, const Position& position, uint8_t s
g_dispatcher.addTask([this, playerID = player->getID(), listDir = std::move(listDir)]() {
playerAutoWalk(playerID, listDir);
});
SchedulerTask_ptr task = createSchedulerTask(
RANGE_WRAP_ITEM_INTERVAL, [=, this]() { playerWrapItem(playerId, position, stackPos, spriteId); });
auto task = createSchedulerTask(RANGE_WRAP_ITEM_INTERVAL,
[=, this]() { playerWrapItem(playerId, position, stackPos, spriteId); });
player->setNextWalkActionTask(std::move(task));
} else {
player->sendCancelMessage(RETURNVALUE_THEREISNOWAY);
Expand Down Expand Up @@ -2690,7 +2687,7 @@ void Game::playerRequestTrade(uint32_t playerId, const Position& pos, uint8_t st
g_dispatcher.addTask([this, playerID = player->getID(), listDir = std::move(listDir)]() {
playerAutoWalk(playerID, listDir);
});
SchedulerTask_ptr task = createSchedulerTask(RANGE_REQUEST_TRADE_INTERVAL, [=, this]() {
auto task = createSchedulerTask(RANGE_REQUEST_TRADE_INTERVAL, [=, this]() {
playerRequestTrade(playerId, pos, stackPos, tradePlayerId, spriteId);
});
player->setNextWalkActionTask(std::move(task));
Expand Down Expand Up @@ -3413,7 +3410,7 @@ void Game::playerRequestEditPodium(uint32_t playerId, const Position& position,
g_dispatcher.addTask([this, playerID = player->getID(), listDir = std::move(listDir)]() {
playerAutoWalk(playerID, listDir);
});
SchedulerTask_ptr task = createSchedulerTask(
auto task = createSchedulerTask(
400, [=, this]() { playerRequestEditPodium(playerId, position, stackPos, spriteId); });
player->setNextWalkActionTask(std::move(task));
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3406,8 +3406,8 @@ void Player::doAttacking(uint32_t)
result = Weapon::useFist(this, attackedCreature);
}

SchedulerTask_ptr task = createSchedulerTask(std::max<uint32_t>(SCHEDULER_MINTICKS, delay),
[id = getID()]() { g_game.checkCreatureAttack(id); });
auto task = createSchedulerTask(std::max<uint32_t>(SCHEDULER_MINTICKS, delay),
[id = getID()]() { g_game.checkCreatureAttack(id); });
if (!classicSpeed) {
setNextActionTask(std::move(task), false);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,5 @@ void Scheduler::shutdown()

SchedulerTask_ptr createSchedulerTask(uint32_t delay, TaskFunc&& f)
{
return SchedulerTask_ptr(new SchedulerTask(delay, std::move(f)));
return std::make_unique<SchedulerTask>(delay, std::forward<TaskFunc>(f));
}
4 changes: 2 additions & 2 deletions src/scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ using SchedulerTask_ptr = std::unique_ptr<SchedulerTask>;
class SchedulerTask : public Task
{
public:
SchedulerTask(uint32_t delay, TaskFunc&& f) : Task(std::forward<TaskFunc>(f)), delay(delay) {}

void setEventId(uint32_t id) { eventId = id; }
uint32_t getEventId() const { return eventId; }

uint32_t getDelay() const { return delay; }

private:
SchedulerTask(uint32_t delay, TaskFunc&& f) : Task(std::move(f)), delay(delay) {}

uint32_t eventId = 0;
uint32_t delay = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/tasks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void Dispatcher::threadMain()
if (!task->hasExpired()) {
++dispatcherCycle;
// execute it
(*task)();
std::invoke(*task);
}
}
tmpTaskList.clear();
Expand Down
11 changes: 7 additions & 4 deletions src/tasks.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ class Task
{
public:
// DO NOT allocate this class on the stack
explicit Task(TaskFunc&& f) : func(std::move(f)) {}
explicit Task(TaskFunc&& f) : func(std::forward<TaskFunc>(f)) {}
Task(uint32_t ms, TaskFunc&& f) :
expiration(std::chrono::system_clock::now() + std::chrono::milliseconds(ms)), func(std::move(f))
expiration(std::chrono::system_clock::now() + std::chrono::milliseconds(ms)), func(std::forward<TaskFunc>(f))
{}

virtual ~Task() = default;
Expand Down Expand Up @@ -53,9 +53,12 @@ class Dispatcher : public ThreadHolder<Dispatcher>
public:
void addTask(Task_ptr task);

void addTask(TaskFunc&& f) { addTask(std::make_unique<Task>(std::move(f))); }
void addTask(TaskFunc&& f) { addTask(std::make_unique<Task>(std::forward<TaskFunc>(f))); }

void addTask(uint32_t expiration, TaskFunc&& f) { addTask(make_unique<Task>(expiration, std::move(f))); }
void addTask(uint32_t expiration, TaskFunc&& f)
{
addTask(std::make_unique<Task>(expiration, std::forward<TaskFunc>(f)));
}

void shutdown();

Expand Down

0 comments on commit 3f23308

Please sign in to comment.