Skip to content

Commit 5f61b5e

Browse files
authored
improve: smooth walk (#924)
1 parent a437a26 commit 5f61b5e

File tree

12 files changed

+77
-110
lines changed

12 files changed

+77
-110
lines changed

modules/client_options/control.otui

-13
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,6 @@ Panel
4343
!text: tr('Enable smart walking')
4444
!tooltip: tr('Will detect when to use diagonal step based on the\nkeys you are pressing')
4545

46-
SmallReversedQtPanel
47-
anchors.left: parent.left
48-
anchors.right: parent.right
49-
anchors.top: prev.bottom
50-
margin-top: 7
51-
height: 22
52-
53-
OptionCheckBoxMarked
54-
id: preciseControl
55-
!text: tr('Enable precise control')
56-
!tooltip: tr('You will have more precision over the character walking,\nbut it can feel more abrupt or unnatural')
57-
@onCheckChange: g_game.setScheduleLastWalk(not self:isChecked())
58-
5946
SmallReversedQtPanel
6047
anchors.left: parent.left
6148
anchors.right: parent.right

modules/client_options/data_options.lua

-6
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@ return {
2525
},
2626
classicControl = false,
2727
smartWalk = false,
28-
preciseControl = {
29-
value = false,
30-
action = function(value, options, controller, panels, extraWidgets)
31-
g_game.setScheduleLastWalk(not value)
32-
end
33-
},
3428
autoChaseOverride = true,
3529
moveStack = false,
3630
showStatusMessagesInConsole = true,

modules/client_options/styles/controls/general.otui

-13
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,6 @@ UIWidget
5656
!text: tr('Enable smart walking')
5757
!tooltip: tr('Will detect when to use diagonal step based on the\nkeys you are pressing')
5858

59-
SmallReversedQtPanel
60-
anchors.left: parent.left
61-
anchors.right: parent.right
62-
anchors.top: prev.bottom
63-
margin-top: 7
64-
height: 22
65-
66-
OptionCheckBoxMarked
67-
id: preciseControl
68-
!text: tr('Enable precise control')
69-
!tooltip: tr('You will have more precision over the character walking,\nbut it can feel more abrupt or unnatural')
70-
@onCheckChange: g_game.setScheduleLastWalk(not self:isChecked())
71-
7259
SmallReversedQtPanel
7360
anchors.left: parent.left
7461
anchors.right: parent.right

modules/game_interface/gameinterface.lua

+8-7
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,12 @@ limitedZoom = false
2525
currentViewMode = 0
2626
smartWalkDirs = {}
2727
smartWalkDir = nil
28-
firstStep = false
2928
leftIncreaseSidePanels = nil
3029
leftDecreaseSidePanels = nil
3130
rightIncreaseSidePanels = nil
3231
rightDecreaseSidePanels = nil
3332
hookedMenuOptions = {}
3433
lastDirTime = g_clock.millis()
35-
lastManualWalk = 0
3634

3735
function init()
3836
g_ui.importStyle('styles/countwindow')
@@ -132,7 +130,7 @@ function init()
132130
end
133131

134132
function bindKeys()
135-
gameRootPanel:setAutoRepeatDelay(200)
133+
gameRootPanel:setAutoRepeatDelay(50)
136134

137135
bindWalkKey('Up', North)
138136
bindWalkKey('Right', East)
@@ -184,6 +182,7 @@ function bindWalkKey(key, dir)
184182
onWalkKeyDown(dir)
185183
end, gameRootPanel, true)
186184
g_keyboard.bindKeyUp(key, function()
185+
g_game.getLocalPlayer():setNextWalkDir(Directions.Invalid)
187186
changeWalkDir(dir, true)
188187
end, gameRootPanel, true)
189188
g_keyboard.bindKeyPress(key, function()
@@ -455,7 +454,6 @@ function onWalkKeyDown(dir)
455454
g_game.setChaseMode(DontChase)
456455
end
457456
end
458-
firstStep = true
459457
changeWalkDir(dir)
460458
end
461459

@@ -496,10 +494,13 @@ function smartWalk(dir)
496494
return false
497495
end
498496

497+
499498
local dire = smartWalkDir or dir
500-
g_game.walk(dire, firstStep)
501-
firstStep = false
502-
lastManualWalk = g_clock.millis()
499+
500+
g_game.getLocalPlayer():setNextWalkDir(dire)
501+
502+
g_game.walk(dire)
503+
503504
return true
504505
end
505506

modules/gamelib/const.lua

+3-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ Directions = {
4141
NorthEast = 4,
4242
SouthEast = 5,
4343
SouthWest = 6,
44-
NorthWest = 7
44+
NorthWest = 7,
45+
Invalid = 8
4546
}
4647

4748
Skill = {
@@ -197,7 +198,7 @@ GameContainerFilter = 113
197198
GameEnterGameShowAppearance = 114
198199
GameSmoothWalkElevation = 115
199200
GameNegativeOffset = 116
200-
GameItemTooltipV8 = 117
201+
GameItemTooltipV8 = 117
201202
GameWingsAurasEffectsShader = 118
202203
GameForgeConvergence = 119
203204
GameAllowCustomBotScripts = 120

src/client/creature.cpp

+21-6
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ void Creature::walk(const Position& oldPos, const Position& newPos)
389389

390390
// starts counting walk
391391
m_walking = true;
392+
m_walkSteps = std::max<int>(++m_walkSteps, 1);
392393
m_walkTimer.restart();
393394
m_walkedPixels = 0;
394395

@@ -644,12 +645,12 @@ void Creature::nextWalkUpdate()
644645

645646
if (!m_walking) return;
646647

647-
// schedules next update
648-
auto self = static_self_cast<Creature>();
649-
m_walkUpdateEvent = g_dispatcher.scheduleEvent([self] {
648+
auto action = [self = static_self_cast<Creature>()] {
650649
self->m_walkUpdateEvent = nullptr;
651650
self->nextWalkUpdate();
652-
}, m_stepCache.walkDuration);
651+
};
652+
653+
m_walkUpdateEvent = isLocalPlayer() ? g_dispatcher.addEvent(action) : g_dispatcher.scheduleEvent(action, m_stepCache.walkDuration);
653654
}
654655

655656
void Creature::updateWalk(const bool isPreWalking)
@@ -666,7 +667,7 @@ void Creature::updateWalk(const bool isPreWalking)
666667
updateWalkingTile();
667668

668669
if (m_walkedPixels == g_gameConfig.getSpriteSize()) {
669-
if (isPreWalking) resetWalkAnimationPhase(true);
670+
if (isPreWalking) resetWalkAnimationPhase(false);
670671
else terminateWalk();
671672
}
672673
}
@@ -694,6 +695,15 @@ void Creature::terminateWalk()
694695
m_walkOffset = {};
695696
m_walking = false;
696697

698+
if (isLocalPlayer() && !static_self_cast<LocalPlayer>()->isAutoWalking() && getWalkSteps() > 1) {
699+
g_dispatcher.addEvent([] {
700+
g_dispatcher.deferEvent([] {
701+
if (g_game.getLocalPlayer())
702+
g_game.walk(g_game.getLocalPlayer()->getNextWalkDir());
703+
});
704+
});
705+
}
706+
697707
resetWalkAnimationPhase(true);
698708
}
699709

@@ -705,6 +715,7 @@ void Creature::resetWalkAnimationPhase(bool toSchedule) {
705715

706716
const auto self = static_self_cast<Creature>();
707717
m_walkFinishAnimEvent = g_dispatcher.scheduleEvent([self] {
718+
self->m_walkSteps = 0;
708719
self->m_walkAnimationPhase = 0;
709720
self->m_walkFinishAnimEvent = nullptr;
710721
}, g_game.getServerBeat());
@@ -944,7 +955,11 @@ uint16_t Creature::getStepDuration(bool ignoreDiagonal, Otc::Direction dir)
944955
stepDuration = ((stepDuration + serverBeat - 1) / serverBeat) * serverBeat;
945956
}
946957

947-
m_stepCache.duration = stepDuration + 10;
958+
if (isLocalPlayer() && stepDuration <= 100)
959+
stepDuration += 10;
960+
961+
m_stepCache.duration = stepDuration;
962+
948963
m_stepCache.walkDuration = std::min<int>(stepDuration / g_gameConfig.getSpriteSize(), DrawPool::FPS60);
949964
m_stepCache.diagonalDuration = stepDuration *
950965
(g_game.getClientVersion() > 810 || g_gameConfig.isForcingNewWalkingFormula()

src/client/creature.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ class Creature : public Thing
9494
void hideStaticSquare() { m_showStaticSquare = false; }
9595

9696
// walk related
97+
int getWalkSteps() const { return m_walkSteps; }
98+
void setWalkSteps(uint8_t step) { m_walkSteps = step; }
9799
void turn(Otc::Direction direction);
98100
void jump(int height, int duration);
99101
void allowAppearWalk() { m_allowAppearWalk = true; }
@@ -148,6 +150,7 @@ class Creature : public Thing
148150

149151
bool isPassable() const { return m_passable; }
150152
bool isWalking() { return m_walking; }
153+
151154
bool isRemoved() { return m_removed; }
152155
bool isInvisible() { return m_outfit.isEffect() && m_outfit.getAuxId() == 13; }
153156
bool isDead() { return m_healthPercent <= 0; }
@@ -194,6 +197,7 @@ class Creature : public Thing
194197
void onPositionChange(const Position& newPos, const Position& oldPos) override;
195198

196199
bool m_walking{ false };
200+
197201
Point m_walkOffset;
198202
Otc::Direction m_direction{ Otc::South };
199203

@@ -232,7 +236,7 @@ class Creature : public Thing
232236
TexturePtr m_iconTexture;
233237
TexturePtr m_typingIconTexture;
234238

235-
ScheduledEventPtr m_walkUpdateEvent;
239+
EventPtr m_walkUpdateEvent;
236240
ScheduledEventPtr m_walkFinishAnimEvent;
237241
ScheduledEventPtr m_outfitColorUpdateEvent;
238242

@@ -291,6 +295,8 @@ class Creature : public Thing
291295

292296
uint8_t m_disableWalkAnimation{ 0 };
293297

298+
uint8_t m_walkSteps{ 0 };
299+
294300
// Mount Shader
295301
uint8_t m_mountShaderId{ 0 };
296302

0 commit comments

Comments
 (0)