Skip to content

Commit

Permalink
feat: remember 'mount' state per player
Browse files Browse the repository at this point in the history
  • Loading branch information
luan committed Dec 10, 2023
1 parent c778b8e commit 273ce86
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
31 changes: 30 additions & 1 deletion src/creatures/players/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1685,6 +1685,12 @@ void Player::onCreatureAppear(std::shared_ptr<Creature> creature, bool isLogin)
}
sendBlessStatus();
}

if (getCurrentMount() != 0) {
toggleMount(true);
}

g_game().changePlayerSpeed(static_self_cast<Player>(), 0);
}
}

Expand Down Expand Up @@ -1717,6 +1723,12 @@ void Player::onChangeZone(ZoneType_t zone) {
wasMounted = true;
}
} else {
int32_t ticks = g_configManager().getNumber(STAIRHOP_DELAY, __FUNCTION__);
if (ticks > 0) {
if (const auto &condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_PACIFIED, ticks, 0)) {
addCondition(condition);
}
}
if (wasMounted) {
toggleMount(true);
wasMounted = false;
Expand Down Expand Up @@ -4393,6 +4405,7 @@ void Player::onAddCondition(ConditionType_t type) {

if (type == CONDITION_OUTFIT && isMounted()) {
dismount();
wasMounted = true;
}

sendIcons();
Expand Down Expand Up @@ -4466,6 +4479,10 @@ void Player::onEndCondition(ConditionType_t type) {
}
}

if (type == CONDITION_OUTFIT && wasMounted) {
toggleMount(true);
}

sendIcons();
}

Expand Down Expand Up @@ -5610,6 +5627,14 @@ void Player::sendUnjustifiedPoints() {
}
}

uint8_t Player::getLastMount() const {
int32_t value = getStorageValue(PSTRG_MOUNTS_CURRENTMOUNT);
if (value > 0) {
return value;
}
return static_cast<uint8_t>(kv()->get("last-mount")->get<int>());
}

uint8_t Player::getCurrentMount() const {
int32_t value = getStorageValue(PSTRG_MOUNTS_CURRENTMOUNT);
if (value > 0) {
Expand Down Expand Up @@ -5668,7 +5693,7 @@ bool Player::toggleMount(bool mount) {
return false;
}

uint8_t currentMountId = getCurrentMount();
uint8_t currentMountId = getLastMount();
if (currentMountId == 0) {
sendOutfitWindow();
return false;
Expand All @@ -5685,6 +5710,7 @@ bool Player::toggleMount(bool mount) {

if (!hasMount(currentMount)) {
setCurrentMount(0);
kv()->set("last-mount", 0);
sendOutfitWindow();
return false;
}
Expand All @@ -5701,6 +5727,7 @@ bool Player::toggleMount(bool mount) {

defaultOutfit.lookMount = currentMount->clientId;
setCurrentMount(currentMount->id);
kv()->set("last-mount", currentMount->id);

if (currentMount->speed != 0) {
g_game().changeSpeed(static_self_cast<Player>(), currentMount->speed);
Expand Down Expand Up @@ -5760,6 +5787,7 @@ bool Player::untameMount(uint8_t mountId) {
}

setCurrentMount(0);
kv()->set("last-mount", 0);
}

return true;
Expand Down Expand Up @@ -5791,6 +5819,7 @@ void Player::dismount() {
}

defaultOutfit.lookMount = 0;
setCurrentMount(0);
}

bool Player::addOfflineTrainingTries(skills_t skill, uint64_t tries) {
Expand Down
1 change: 1 addition & 0 deletions src/creatures/players/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ class Player final : public Creature, public Cylinder, public Bankable {
return CREATURETYPE_PLAYER;
}

uint8_t getLastMount() const;
uint8_t getCurrentMount() const;
void setCurrentMount(uint8_t mountId);
bool isMounted() const {
Expand Down
2 changes: 1 addition & 1 deletion src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5479,7 +5479,7 @@ void Game::playerChangeOutfit(uint32_t playerId, Outfit_t outfit, uint8_t isMoun

auto deltaSpeedChange = mount->speed;
if (player->isMounted()) {
const auto prevMount = mounts.getMountByID(player->getCurrentMount());
const auto prevMount = mounts.getMountByID(player->getLastMount());
if (prevMount) {
deltaSpeedChange -= prevMount->speed;
}
Expand Down
4 changes: 2 additions & 2 deletions src/server/network/protocol/protocolgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6411,7 +6411,7 @@ void ProtocolGame::sendOutfitWindow() {

if (oldProtocol) {
Outfit_t currentOutfit = player->getDefaultOutfit();
const auto currentMount = g_game().mounts.getMountByID(player->getCurrentMount());
const auto currentMount = g_game().mounts.getMountByID(player->getLastMount());
if (currentMount) {
currentOutfit.lookMount = currentMount->clientId;
}
Expand Down Expand Up @@ -6471,7 +6471,7 @@ void ProtocolGame::sendOutfitWindow() {

bool mounted = false;
Outfit_t currentOutfit = player->getDefaultOutfit();
const auto currentMount = g_game().mounts.getMountByID(player->getCurrentMount());
const auto currentMount = g_game().mounts.getMountByID(player->getLastMount());
if (currentMount) {
mounted = (currentOutfit.lookMount == currentMount->clientId);
currentOutfit.lookMount = currentMount->clientId;
Expand Down

0 comments on commit 273ce86

Please sign in to comment.