Skip to content

Commit

Permalink
Merge branch 'main' into dudantas/improve-remove-protobuf-generated-f…
Browse files Browse the repository at this point in the history
…iles
  • Loading branch information
dudantas authored Oct 28, 2024
2 parents 615f458 + bdf9ab8 commit 91ca336
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 62 deletions.
4 changes: 4 additions & 0 deletions modules/corelib/keyboard.lua
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ local function connectKeyPressEvent(widget)
widget.boundKeyPressCombos = {}
end

function g_keyboard.setKeyDelay(key, delay)
g_window.setKeyDelay(getKeyCode(key), delay);
end

-- public functions
function g_keyboard.bindKeyDown(keyComboDesc, callback, widget, alone)
widget = widget or rootWidget
Expand Down
12 changes: 5 additions & 7 deletions modules/game_interface/gameinterface.lua
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,21 @@ 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()
smartWalk(dir)
end, gameRootPanel)

g_keyboard.setKeyDelay(key, 10)
end

function unbindWalkKey(key)
g_keyboard.unbindKeyDown(key, gameRootPanel)
g_keyboard.unbindKeyUp(key, gameRootPanel)
g_keyboard.unbindKeyPress(key, gameRootPanel)

g_keyboard.setKeyDelay(key, 30)
end

function bindTurnKey(key, dir)
Expand Down Expand Up @@ -494,12 +497,7 @@ function smartWalk(dir)
return false
end


local dire = smartWalkDir or dir

g_game.getLocalPlayer():setNextWalkDir(dire)

g_game.walk(dire)
g_game.walk(smartWalkDir or dir)

return true
end
Expand Down
15 changes: 7 additions & 8 deletions src/client/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -642,10 +642,6 @@ void Creature::nextWalkUpdate()
// do the update
updateWalk();

if (isLocalPlayer()) {
g_map.notificateCameraMove(m_walkOffset);
}

if (!m_walking) return;

auto action = [self = static_self_cast<Creature>()] {
Expand All @@ -658,17 +654,23 @@ void Creature::nextWalkUpdate()

void Creature::updateWalk(const bool isPreWalking)
{
const float walkTicksPerPixel = getStepDuration(true) / static_cast<float>(g_gameConfig.getSpriteSize());
const float walkTicksPerPixel = (getStepDuration(true) + 8.f) / static_cast<float>(g_gameConfig.getSpriteSize());

const int totalPixelsWalked = std::min<int>(m_walkTimer.ticksElapsed() / walkTicksPerPixel, g_gameConfig.getSpriteSize());

// needed for paralyze effect
m_walkedPixels = std::max<int>(m_walkedPixels, totalPixelsWalked);

const auto oldWalkOffset = m_walkOffset;

updateWalkAnimation();
updateWalkOffset(m_walkedPixels);
updateWalkingTile();

if (isLocalPlayer() && oldWalkOffset != m_walkOffset) {
g_map.notificateCameraMove(m_walkOffset);
}

if (m_walkedPixels == g_gameConfig.getSpriteSize() && !isPreWalking) {
terminateWalk();
}
Expand Down Expand Up @@ -939,9 +941,6 @@ uint16_t Creature::getStepDuration(bool ignoreDiagonal, Otc::Direction dir)
stepDuration = ((stepDuration + serverBeat - 1) / serverBeat) * serverBeat;
}

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

m_stepCache.duration = stepDuration;

m_stepCache.walkDuration = std::min<int>(stepDuration / g_gameConfig.getSpriteSize(), DrawPool::FPS60);
Expand Down
23 changes: 13 additions & 10 deletions src/client/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -645,20 +645,30 @@ bool Game::walk(const Otc::Direction direction, bool force)
if (!canPerformGameAction())
return false;

// must cancel auto walking, and wait next try
if (m_localPlayer->isAutoWalking()) {
m_protocolGame->sendStop();
m_localPlayer->stopAutoWalk();
return false;
}

if (!force) {
if (nextWalkSchedule)
return false;

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->getNextWalkDir() != Otc::InvalidDirection) {
if (m_localPlayer) {
m_localPlayer->setWalkSteps(1);
walk(direction, true);
}
Expand All @@ -670,15 +680,8 @@ bool Game::walk(const Otc::Direction direction, bool force)
}
}

// must cancel auto walking, and wait next try
if (m_localPlayer->isAutoWalking()) {
m_protocolGame->sendStop();
m_localPlayer->stopAutoWalk();
return false;
}

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

Expand Down Expand Up @@ -751,7 +754,7 @@ void Game::autoWalk(const std::vector<Otc::Direction>& dirs, const Position& sta

const Otc::Direction direction = *dirs.begin();
if (const auto& toTile = g_map.getTile(startPos.translatedToDirection(direction))) {
if (startPos == m_localPlayer->m_lastPrewalkDestination && toTile->isWalkable() && m_localPlayer->canWalk(true)) {
if (startPos == m_localPlayer->m_lastPrewalkDestination && toTile->isWalkable() && m_localPlayer->canWalk(direction, true)) {
m_localPlayer->preWalk(direction);
}
}
Expand Down
13 changes: 2 additions & 11 deletions src/client/localplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void LocalPlayer::lockWalk(uint16_t millis)
m_walkLockExpiration = std::max<ticks_t>(m_walkLockExpiration, g_clock.millis() + millis);
}

bool LocalPlayer::canWalk(bool ignoreLock)
bool LocalPlayer::canWalk(Otc::Direction dir, bool ignoreLock)
{
// paralyzed
if (isDead())
Expand All @@ -41,7 +41,7 @@ bool LocalPlayer::canWalk(bool ignoreLock)
if (isWalkLocked() && !ignoreLock)
return false;

return !m_walking && m_walkTimer.ticksElapsed() >= getStepDuration();
return m_walkTimer.ticksElapsed() >= getStepDuration() && (dir == m_direction || !m_walking);
}

void LocalPlayer::walk(const Position& oldPos, const Position& newPos)
Expand Down Expand Up @@ -208,15 +208,6 @@ void LocalPlayer::terminateWalk()
{
Creature::terminateWalk();
m_preWalking = false;

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

void LocalPlayer::onPositionChange(const Position& newPos, const Position& oldPos)
Expand Down
7 changes: 1 addition & 6 deletions src/client/localplayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class LocalPlayer : public Player
void stopAutoWalk();

bool autoWalk(const Position& destination, bool retry = false);
bool canWalk(bool ignoreLock = false);
bool canWalk(Otc::Direction dir, bool ignoreLock = false);

void setStates(uint32_t states);
void setSkill(Otc::Skill skillId, uint16_t level, uint16_t levelPercent);
Expand All @@ -59,14 +59,11 @@ class LocalPlayer : public Player
void setSpells(const std::vector<uint16_t>& spells);
void setBlessings(uint16_t blessings);
void setResourceBalance(Otc::ResourceTypes_t type, uint64_t value);
void setNextWalkDir(Otc::Direction dir) { m_nextWalkDir = dir; }
void takeScreenshot(uint8_t type);

uint32_t getFreeCapacity() { return m_freeCapacity; }
uint32_t getTotalCapacity() { return m_totalCapacity; }

auto getNextWalkDir() { return m_nextWalkDir; }

uint8_t getVocation() { return m_vocation; }
uint8_t getMagicLevel() { return m_magicLevel; }
uint8_t getMagicLevelPercent() { return m_magicLevelPercent; }
Expand Down Expand Up @@ -185,6 +182,4 @@ class LocalPlayer : public Player
uint16_t m_stamina{ 0 };
uint16_t m_regenerationTime{ 0 };
uint16_t m_offlineTrainingTime{ 0 };

Otc::Direction m_nextWalkDir{ Otc::InvalidDirection };
};
2 changes: 0 additions & 2 deletions src/client/luafunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -855,8 +855,6 @@ void Client::registerLuaFunctions()
g_lua.bindClassMemberFunction<LocalPlayer>("getResourceBalance", &LocalPlayer::getResourceBalance);
g_lua.bindClassMemberFunction<LocalPlayer>("setResourceBalance", &LocalPlayer::setResourceBalance);
g_lua.bindClassMemberFunction<LocalPlayer>("getTotalMoney", &LocalPlayer::getTotalMoney);
g_lua.bindClassMemberFunction<LocalPlayer>("setNextWalkDir", &LocalPlayer::setNextWalkDir);
g_lua.bindClassMemberFunction<LocalPlayer>("getNextWalkDir", &LocalPlayer::getNextWalkDir);

g_lua.registerClass<Tile, AttachableObject>();
g_lua.bindClassMemberFunction<Tile>("clean", &Tile::clean);
Expand Down
1 change: 1 addition & 0 deletions src/framework/luafunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ void Application::registerLuaFunctions()
g_lua.bindSingletonFunction("g_window", "isMaximized", &PlatformWindow::isMaximized, &g_window);
g_lua.bindSingletonFunction("g_window", "hasFocus", &PlatformWindow::hasFocus, &g_window);
g_lua.bindSingletonFunction("g_window", "getDisplayDensity", &PlatformWindow::getDisplayDensity, &g_window);
g_lua.bindSingletonFunction("g_window", "setKeyDelay", &PlatformWindow::setKeyDelay, &g_window);

// Input
g_lua.registerSingletonClass("g_mouse");
Expand Down
30 changes: 16 additions & 14 deletions src/framework/platform/platformwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ void PlatformWindow::processKeyDown(Fw::Key keyCode)
return;
}

if (m_keysState[keyCode])
if (m_keyInfo[keyCode].state)
return;

m_keysState[keyCode] = true;
m_lastKeysPress[keyCode] = -1;
m_keyInfo[keyCode].state = true;
m_keyInfo[keyCode].lastTicks = -1;

m_inputEvent.reset(Fw::KeyDownInputEvent);
m_inputEvent.type = Fw::KeyDownInputEvent;
Expand All @@ -107,8 +107,8 @@ void PlatformWindow::processKeyDown(Fw::Key keyCode)

m_inputEvent.reset(Fw::KeyPressInputEvent);
m_inputEvent.keyCode = keyCode;
m_lastKeysPress[keyCode] = g_clock.millis();
m_firstKeysPress[keyCode] = g_clock.millis();
m_keyInfo[keyCode].lastTicks = g_clock.millis();
m_keyInfo[keyCode].firstTicks = g_clock.millis();
m_onInputEvent(m_inputEvent);
}
}
Expand Down Expand Up @@ -138,15 +138,15 @@ void PlatformWindow::processKeyUp(Fw::Key keyCode)
}
if (keyCode == Fw::KeyNumLock) {
for (uint8_t k = Fw::KeyNumpad0; k <= Fw::KeyNumpad9; ++k) {
if (m_keysState[static_cast<Fw::Key>(k)])
if (m_keyInfo[static_cast<Fw::Key>(k)].state)
processKeyUp(static_cast<Fw::Key>(k));
}
}

if (!m_keysState[keyCode])
if (!m_keyInfo[keyCode].state)
return;

m_keysState[keyCode] = false;
m_keyInfo[keyCode].state = false;

if (m_onInputEvent) {
m_inputEvent.reset(Fw::KeyUpInputEvent);
Expand All @@ -158,7 +158,7 @@ void PlatformWindow::processKeyUp(Fw::Key keyCode)
void PlatformWindow::releaseAllKeys()
{
for (size_t keyCode = 0; keyCode < Fw::KeyLast; ++keyCode) {
const bool pressed = m_keysState[keyCode];
const bool pressed = m_keyInfo[keyCode].state;
if (!pressed)
continue;

Expand All @@ -177,21 +177,23 @@ void PlatformWindow::fireKeysPress()
m_keyPressTimer.restart();

for (size_t keyCode = 0; keyCode < Fw::KeyLast; ++keyCode) {
const bool pressed = m_keysState[keyCode];
auto& keyInfo = m_keyInfo[keyCode];

const bool pressed = keyInfo.state;

if (!pressed)
continue;

const ticks_t lastPressTicks = m_lastKeysPress[keyCode];
const ticks_t firstKeyPress = m_firstKeysPress[keyCode];
if (g_clock.millis() - lastPressTicks >= KEY_PRESS_REPEAT_INTERVAL) {
const ticks_t lastPressTicks = keyInfo.lastTicks;
const ticks_t firstKeyPress = keyInfo.firstTicks;
if (g_clock.millis() - lastPressTicks >= keyInfo.delay) {
if (m_onInputEvent) {
m_inputEvent.reset(Fw::KeyPressInputEvent);
m_inputEvent.keyCode = static_cast<Fw::Key>(keyCode);
m_inputEvent.autoRepeatTicks = g_clock.millis() - firstKeyPress;
m_onInputEvent(m_inputEvent);
}
m_lastKeysPress[keyCode] = g_clock.millis();
keyInfo.lastTicks = g_clock.millis();
}
}
}
17 changes: 13 additions & 4 deletions src/framework/platform/platformwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ class PlatformWindow
KEY_PRESS_REPEAT_INTERVAL = 30,
};

struct KeyInfo
{
ticks_t firstTicks = 0;
ticks_t lastTicks = 0;
bool state = false;
uint8_t delay = KEY_PRESS_REPEAT_INTERVAL;
};

using OnResizeCallback = std::function<void(const Size&)>;
using OnInputEventCallback = std::function<void(const InputEvent&)>;

Expand Down Expand Up @@ -87,7 +95,7 @@ class PlatformWindow
Point getMousePosition() { return m_inputEvent.mousePos; }
int getKeyboardModifiers() { return m_inputEvent.keyboardModifiers; }

bool isKeyPressed(Fw::Key keyCode) { return m_keysState[keyCode]; }
bool isKeyPressed(Fw::Key keyCode) { return m_keyInfo[keyCode].state; }
bool isMouseButtonPressed(Fw::MouseButton mouseButton)
{ if (mouseButton == Fw::MouseNoButton) return m_mouseButtonStates != 0; return (m_mouseButtonStates & (1u << mouseButton)) == (1u << mouseButton); }
bool isVisible() { return m_visible; }
Expand All @@ -103,7 +111,10 @@ class PlatformWindow

void addKeyListener(std::function<void(const InputEvent&)> listener) { m_keyListeners.push_back(listener); }

void setKeyDelay(const Fw::Key key, uint8_t delay) { if (key < Fw::KeyLast) m_keyInfo[key].delay = delay; }

protected:

virtual int internalLoadMouseCursor(const ImagePtr& image, const Point& hotSpot) = 0;

void updateUnmaximizedCoords();
Expand All @@ -114,9 +125,7 @@ class PlatformWindow
void fireKeysPress();

stdext::map<int, Fw::Key> m_keyMap;
std::array<bool, Fw::KeyLast> m_keysState;
std::array<ticks_t, Fw::KeyLast> m_firstKeysPress;
std::array<ticks_t, Fw::KeyLast> m_lastKeysPress;
std::array<KeyInfo, Fw::KeyLast> m_keyInfo = {};
Timer m_keyPressTimer;

Size m_size;
Expand Down

0 comments on commit 91ca336

Please sign in to comment.