Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

improve: smooth walk #924

Merged
merged 10 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions modules/client_options/control.otui
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,6 @@ Panel
!text: tr('Enable smart walking')
!tooltip: tr('Will detect when to use diagonal step based on the\nkeys you are pressing')

SmallReversedQtPanel
anchors.left: parent.left
anchors.right: parent.right
anchors.top: prev.bottom
margin-top: 7
height: 22

OptionCheckBoxMarked
id: preciseControl
!text: tr('Enable precise control')
!tooltip: tr('You will have more precision over the character walking,\nbut it can feel more abrupt or unnatural')
@onCheckChange: g_game.setScheduleLastWalk(not self:isChecked())

SmallReversedQtPanel
anchors.left: parent.left
anchors.right: parent.right
Expand Down
6 changes: 0 additions & 6 deletions modules/client_options/data_options.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ return {
},
classicControl = false,
smartWalk = false,
preciseControl = {
value = false,
action = function(value, options, controller, panels, extraWidgets)
g_game.setScheduleLastWalk(not value)
end
},
autoChaseOverride = true,
moveStack = false,
showStatusMessagesInConsole = true,
Expand Down
13 changes: 0 additions & 13 deletions modules/client_options/styles/controls/general.otui
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,6 @@ UIWidget
!text: tr('Enable smart walking')
!tooltip: tr('Will detect when to use diagonal step based on the\nkeys you are pressing')

SmallReversedQtPanel
anchors.left: parent.left
anchors.right: parent.right
anchors.top: prev.bottom
margin-top: 7
height: 22

OptionCheckBoxMarked
id: preciseControl
!text: tr('Enable precise control')
!tooltip: tr('You will have more precision over the character walking,\nbut it can feel more abrupt or unnatural')
@onCheckChange: g_game.setScheduleLastWalk(not self:isChecked())

SmallReversedQtPanel
anchors.left: parent.left
anchors.right: parent.right
Expand Down
15 changes: 8 additions & 7 deletions modules/game_interface/gameinterface.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@ limitedZoom = false
currentViewMode = 0
smartWalkDirs = {}
smartWalkDir = nil
firstStep = false
leftIncreaseSidePanels = nil
leftDecreaseSidePanels = nil
rightIncreaseSidePanels = nil
rightDecreaseSidePanels = nil
hookedMenuOptions = {}
lastDirTime = g_clock.millis()
lastManualWalk = 0

function init()
g_ui.importStyle('styles/countwindow')
Expand Down Expand Up @@ -132,7 +130,7 @@ function init()
end

function bindKeys()
gameRootPanel:setAutoRepeatDelay(200)
gameRootPanel:setAutoRepeatDelay(50)

bindWalkKey('Up', North)
bindWalkKey('Right', East)
Expand Down Expand Up @@ -184,6 +182,7 @@ function bindWalkKey(key, dir)
onWalkKeyDown(dir)
end, gameRootPanel, true)
g_keyboard.bindKeyUp(key, function()
g_game.getLocalPlayer():setNextWalkDir(Directions.Invalid)
changeWalkDir(dir, true)
end, gameRootPanel, true)
g_keyboard.bindKeyPress(key, function()
Expand Down Expand Up @@ -455,7 +454,6 @@ function onWalkKeyDown(dir)
g_game.setChaseMode(DontChase)
end
end
firstStep = true
changeWalkDir(dir)
end

Expand Down Expand Up @@ -496,10 +494,13 @@ function smartWalk(dir)
return false
end


local dire = smartWalkDir or dir
g_game.walk(dire, firstStep)
firstStep = false
lastManualWalk = g_clock.millis()

g_game.getLocalPlayer():setNextWalkDir(dire)

g_game.walk(dire)

return true
end

Expand Down
5 changes: 3 additions & 2 deletions modules/gamelib/const.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ Directions = {
NorthEast = 4,
SouthEast = 5,
SouthWest = 6,
NorthWest = 7
NorthWest = 7,
Invalid = 8
}

Skill = {
Expand Down Expand Up @@ -197,7 +198,7 @@ GameContainerFilter = 113
GameEnterGameShowAppearance = 114
GameSmoothWalkElevation = 115
GameNegativeOffset = 116
GameItemTooltipV8 = 117
GameItemTooltipV8 = 117
GameWingsAurasEffectsShader = 118
GameForgeConvergence = 119
GameAllowCustomBotScripts = 120
Expand Down
27 changes: 21 additions & 6 deletions src/client/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ void Creature::walk(const Position& oldPos, const Position& newPos)

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

Expand Down Expand Up @@ -644,12 +645,12 @@ void Creature::nextWalkUpdate()

if (!m_walking) return;

// schedules next update
auto self = static_self_cast<Creature>();
m_walkUpdateEvent = g_dispatcher.scheduleEvent([self] {
auto action = [self = static_self_cast<Creature>()] {
self->m_walkUpdateEvent = nullptr;
self->nextWalkUpdate();
}, m_stepCache.walkDuration);
};

m_walkUpdateEvent = isLocalPlayer() ? g_dispatcher.addEvent(action) : g_dispatcher.scheduleEvent(action, m_stepCache.walkDuration);
}

void Creature::updateWalk(const bool isPreWalking)
Expand All @@ -666,7 +667,7 @@ void Creature::updateWalk(const bool isPreWalking)
updateWalkingTile();

if (m_walkedPixels == g_gameConfig.getSpriteSize()) {
if (isPreWalking) resetWalkAnimationPhase(true);
if (isPreWalking) resetWalkAnimationPhase(false);
else terminateWalk();
}
}
Expand Down Expand Up @@ -694,6 +695,15 @@ void Creature::terminateWalk()
m_walkOffset = {};
m_walking = false;

if (isLocalPlayer() && !static_self_cast<LocalPlayer>()->isAutoWalking() && getWalkSteps() > 1) {
g_dispatcher.addEvent([] {
g_dispatcher.deferEvent([] {
if (g_game.getLocalPlayer())
g_game.walk(g_game.getLocalPlayer()->getNextWalkDir());
});
});
}

resetWalkAnimationPhase(true);
}

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

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 Expand Up @@ -944,7 +955,11 @@ uint16_t Creature::getStepDuration(bool ignoreDiagonal, Otc::Direction dir)
stepDuration = ((stepDuration + serverBeat - 1) / serverBeat) * serverBeat;
}

m_stepCache.duration = stepDuration + 10;
if (isLocalPlayer() && stepDuration <= 100)
stepDuration += 10;

m_stepCache.duration = stepDuration;

m_stepCache.walkDuration = std::min<int>(stepDuration / g_gameConfig.getSpriteSize(), DrawPool::FPS60);
m_stepCache.diagonalDuration = stepDuration *
(g_game.getClientVersion() > 810 || g_gameConfig.isForcingNewWalkingFormula()
Expand Down
8 changes: 7 additions & 1 deletion src/client/creature.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ 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 @@ -148,6 +150,7 @@ class Creature : public Thing

bool isPassable() const { return m_passable; }
bool isWalking() { return m_walking; }

bool isRemoved() { return m_removed; }
bool isInvisible() { return m_outfit.isEffect() && m_outfit.getAuxId() == 13; }
bool isDead() { return m_healthPercent <= 0; }
Expand Down Expand Up @@ -194,6 +197,7 @@ class Creature : public Thing
void onPositionChange(const Position& newPos, const Position& oldPos) override;

bool m_walking{ false };

Point m_walkOffset;
Otc::Direction m_direction{ Otc::South };

Expand Down Expand Up @@ -232,7 +236,7 @@ class Creature : public Thing
TexturePtr m_iconTexture;
TexturePtr m_typingIconTexture;

ScheduledEventPtr m_walkUpdateEvent;
EventPtr m_walkUpdateEvent;
ScheduledEventPtr m_walkFinishAnimEvent;
ScheduledEventPtr m_outfitColorUpdateEvent;

Expand Down Expand Up @@ -291,6 +295,8 @@ class Creature : public Thing

uint8_t m_disableWalkAnimation{ 0 };

uint8_t m_walkSteps{ 0 };

// Mount Shader
uint8_t m_mountShaderId{ 0 };

Expand Down
Loading
Loading