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

Player set/get cannotMove #2553

Merged
merged 12 commits into from
Dec 1, 2020
5 changes: 5 additions & 0 deletions src/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@ bool Creature::getNextStep(Direction& dir, uint32_t&)

void Creature::startAutoWalk(const std::forward_list<Direction>& listDir)
{
if (getPlayer() && !getPlayer()->getCanMove()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Player* player = getPlayer();
if (player && player->getCanMove()) {

same with sendCancelWalk

getPlayer()->sendCancelWalk();
return;
}

listWalkDir = listDir;

size_t size = 0;
Expand Down
9 changes: 9 additions & 0 deletions src/creature.h
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,14 @@ class Creature : virtual public Thing
void setUseDefense(bool useDefense) {
canUseDefense = useDefense;
}
void setCanMove(bool move)
{
rookgaard marked this conversation as resolved.
Show resolved Hide resolved
canMove = move;
cancelNextWalk = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we set canMove to true, do we still cancelNextWalk?

Copy link
Contributor

@soul4soul soul4soul Apr 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I too was wondering about cancelNextWalk always being set to true in this function. What is the reason for the code not being this or similar

Suggested change
cancelNextWalk = true;
cancelNextWalk = !move;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still curious about this.

Copy link
Member

@Znote Znote Nov 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My guess is to fix desync issues mid-action, this is called to toggle the states, but no matter which direction it toggles to, you dont want to give the user half a sqm headstart as it may create odd client behavior. So even if you just got permission to walk, the character pos gets a cancel byte one last time to set the timings straight. (I might be wrong though).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Znote but if player is already movement blocked then he cannot walk, so what desynchro?

}
bool getCanMove() const {
return canMove;
}

//creature script events
bool registerCreatureEvent(const std::string& name);
Expand Down Expand Up @@ -526,6 +534,7 @@ class Creature : virtual public Thing
bool forceUpdateFollowPath = false;
bool hiddenHealth = false;
bool canUseDefense = true;
bool canMove = true;

//creature script events
bool hasEventRegistered(CreatureEventType_t event) const {
Expand Down
10 changes: 10 additions & 0 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,11 @@ void Game::playerMoveCreature(Player* player, Creature* movingCreature, const Po
return;
}

if (!movingCreature->getCanMove()) {
player->sendCancelMessage(RETURNVALUE_NOTMOVEABLE);
return;
}

player->setNextActionTask(nullptr);

if (!Position::areInRange<1, 1, 0>(movingCreatureOrigPos, player->getPosition())) {
Expand Down Expand Up @@ -1770,6 +1775,11 @@ void Game::playerMove(uint32_t playerId, Direction direction)
return;
}

if (!player->getCanMove()) {
player->sendCancelWalk();
return;
}

player->resetIdleTime();
player->setNextWalkActionTask(nullptr);

Expand Down
27 changes: 27 additions & 0 deletions src/luascript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2115,6 +2115,7 @@ void LuaScriptInterface::registerFunctions()
registerMethod("Creature", "isCreature", LuaScriptInterface::luaCreatureIsCreature);
registerMethod("Creature", "isInGhostMode", LuaScriptInterface::luaCreatureIsInGhostMode);
registerMethod("Creature", "isHealthHidden", LuaScriptInterface::luaCreatureIsHealthHidden);
registerMethod("Creature", "canMove", LuaScriptInterface::luaCreatureCanMove);
registerMethod("Creature", "isImmune", LuaScriptInterface::luaCreatureIsImmune);

registerMethod("Creature", "canSee", LuaScriptInterface::luaCreatureCanSee);
Expand Down Expand Up @@ -2155,6 +2156,7 @@ void LuaScriptInterface::registerFunctions()
registerMethod("Creature", "getMaxHealth", LuaScriptInterface::luaCreatureGetMaxHealth);
registerMethod("Creature", "setMaxHealth", LuaScriptInterface::luaCreatureSetMaxHealth);
registerMethod("Creature", "setHiddenHealth", LuaScriptInterface::luaCreatureSetHiddenHealth);
registerMethod("Creature", "setMove", LuaScriptInterface::luaCreatureSetMove);

registerMethod("Creature", "getSkull", LuaScriptInterface::luaCreatureGetSkull);
registerMethod("Creature", "setSkull", LuaScriptInterface::luaCreatureSetSkull);
Expand Down Expand Up @@ -6748,6 +6750,18 @@ int LuaScriptInterface::luaCreatureIsHealthHidden(lua_State* L)
return 1;
}

int LuaScriptInterface::luaCreatureCanMove(lua_State* L)
{
// creature:canMove()
const Creature* creature = getUserdata<const Creature>(L, 1);
if (creature) {
pushBoolean(L, creature->getCanMove());
} else {
lua_pushnil(L);
}
return 1;
}

int LuaScriptInterface::luaCreatureCanSee(lua_State* L)
{
// creature:canSee(position)
Expand Down Expand Up @@ -7168,6 +7182,19 @@ int LuaScriptInterface::luaCreatureSetHiddenHealth(lua_State* L)
return 1;
}

int LuaScriptInterface::luaCreatureSetMove(lua_State* L)
{
// creature:setMove(move)
Creature* creature = getUserdata<Creature>(L, 1);
if (creature) {
creature->setCanMove(getBoolean(L, 2));
pushBoolean(L, true);
} else {
lua_pushnil(L);
}
return 1;
}

int LuaScriptInterface::luaCreatureGetSkull(lua_State* L)
{
// creature:getSkull()
Expand Down
2 changes: 2 additions & 0 deletions src/luascript.h
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,7 @@ class LuaScriptInterface
static int luaCreatureIsCreature(lua_State* L);
static int luaCreatureIsInGhostMode(lua_State* L);
static int luaCreatureIsHealthHidden(lua_State* L);
static int luaCreatureCanMove(lua_State* L);
static int luaCreatureIsImmune(lua_State* L);

static int luaCreatureCanSee(lua_State* L);
Expand Down Expand Up @@ -790,6 +791,7 @@ class LuaScriptInterface
static int luaCreatureGetMaxHealth(lua_State* L);
static int luaCreatureSetMaxHealth(lua_State* L);
static int luaCreatureSetHiddenHealth(lua_State* L);
static int luaCreatureSetMove(lua_State* L);

static int luaCreatureGetSkull(lua_State* L);
static int luaCreatureSetSkull(lua_State* L);
Expand Down