Skip to content

Commit

Permalink
fix: walk double step
Browse files Browse the repository at this point in the history
  • Loading branch information
mehah committed Nov 8, 2024
1 parent 01ebe1b commit 8005fc3
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 47 deletions.
4 changes: 0 additions & 4 deletions src/client/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,6 @@ void Creature::walk(const Position& oldPos, const Position& newPos)
m_walkTimer.restart();
m_walkedPixels = 0;

if (++m_walkSteps == 0)
m_walkSteps = 1;

// no direction need to be changed when the walk ends
m_walkTurnDirection = Otc::InvalidDirection;

Expand Down Expand Up @@ -701,7 +698,6 @@ void Creature::terminateWalk()

const auto self = static_self_cast<Creature>();
m_walkFinishAnimEvent = g_dispatcher.scheduleEvent([self] {
self->m_walkSteps = 0;
self->m_walkAnimationPhase = 0;
self->m_walkFinishAnimEvent = nullptr;
}, g_game.getServerBeat());
Expand Down
4 changes: 0 additions & 4 deletions src/client/creature.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ class Creature : public Thing
void hideStaticSquare() { m_showStaticSquare = false; }

// walk related
int getWalkSteps() const { return m_walkSteps; }
void setWalkSteps(uint8_t step) { m_walkSteps = step; }
void turn(Otc::Direction direction);
void jump(int height, int duration);
void allowAppearWalk() { m_allowAppearWalk = true; }
Expand Down Expand Up @@ -294,8 +292,6 @@ class Creature : public Thing

uint8_t m_disableWalkAnimation{ 0 };

uint8_t m_walkSteps{ 0 };

// Mount Shader
uint8_t m_mountShaderId{ 0 };

Expand Down
59 changes: 21 additions & 38 deletions src/client/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -630,19 +630,9 @@ void Game::safeLogout()
m_protocolGame->sendLogout();
}

bool Game::walk(const Otc::Direction direction, bool force)
bool Game::walk(const Otc::Direction direction)
{
static ScheduledEventPtr nextWalkSchedule = nullptr;

if (direction == Otc::InvalidDirection) {
if (nextWalkSchedule) {
nextWalkSchedule->cancel();
nextWalkSchedule = nullptr;
}
return false;
}

if (!canPerformGameAction())
if (!canPerformGameAction() || direction == Otc::InvalidDirection)
return false;

// must cancel auto walking, and wait next try
Expand All @@ -652,39 +642,32 @@ bool Game::walk(const Otc::Direction direction, bool force)
return false;
}

if (!force) {
if (nextWalkSchedule)
return false;
static ScheduledEventPtr nextWalkSchedule = nullptr;
static uint16_t steps = 0;
static Timer timer;

if (m_localPlayer->getWalkSteps() > 0) {
uint16_t delay = 0;
if (m_localPlayer->getWalkSteps() == 1) {
if (m_localPlayer->isWalking())
return false;

delay = m_walkFirstStepDelay;
} else if (direction != m_localPlayer->getDirection())
delay = m_walkTurnDelay;

if (delay > 0) {
nextWalkSchedule = g_dispatcher.scheduleEvent([this, direction] {
if (m_localPlayer) {
m_localPlayer->setWalkSteps(1);
walk(direction, true);
}

nextWalkSchedule = nullptr;
}, delay);
return false;
}
}
}
if (nextWalkSchedule) nextWalkSchedule->cancel();
nextWalkSchedule = g_dispatcher.scheduleEvent([this] {
nextWalkSchedule = nullptr;
steps = 0;
}, 150);

// check we can walk and add new walk event if false
if (!m_localPlayer->canWalk(direction)) {
return false;
}

if (steps == 1) {
if (timer.ticksElapsed() <= m_walkFirstStepDelay)
return false;
} else if (direction != m_localPlayer->getDirection()) {
if (timer.ticksElapsed() <= m_walkTurnDelay)
return false;
}

++steps;
timer.restart();

const auto& toPos = m_localPlayer->getPosition().translatedToDirection(direction);

// only do prewalks to walkable tiles (like grounds and not walls)
Expand Down
2 changes: 1 addition & 1 deletion src/client/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ class Game
void safeLogout();

// walk related
bool walk(const Otc::Direction direction, bool force = false);
bool walk(const Otc::Direction direction);
void autoWalk(const std::vector<Otc::Direction>& dirs, const Position& startPos);
void forceWalk(const Otc::Direction direction);
void turn(const Otc::Direction direction);
Expand Down

0 comments on commit 8005fc3

Please sign in to comment.