From 97d7171123a8751e0851dc600d9853845cb3ab28 Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Wed, 21 Apr 2021 00:12:55 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A5=20Get=20rid=20of=20=5Fptargx/y?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/debug.cpp | 5 ++++- Source/loadsave.cpp | 12 ++++++++---- Source/multi.cpp | 11 ++++------- Source/pack.cpp | 8 ++++---- Source/player.cpp | 24 ++++++++---------------- Source/player.h | 11 +++-------- Source/track.cpp | 5 +++-- test/writehero_test.cpp | 2 -- 8 files changed, 34 insertions(+), 44 deletions(-) diff --git a/Source/debug.cpp b/Source/debug.cpp index 71e038549..1efb3742c 100644 --- a/Source/debug.cpp +++ b/Source/debug.cpp @@ -4,6 +4,8 @@ * Implementation of debug functions. */ +#include + #include "cursor.h" #include "inv.h" #include "spells.h" @@ -145,7 +147,8 @@ void PrintDebugPlayer(bool bNextPlayer) NetSendCmdString(1 << myplr, dstr); sprintf(dstr, " Lvl = %i : Change = %i", plr[dbgplr].plrlevel, plr[dbgplr]._pLvlChanging); NetSendCmdString(1 << myplr, dstr); - sprintf(dstr, " x = %i, y = %i : tx = %i, ty = %i", plr[dbgplr]._px, plr[dbgplr]._py, plr[dbgplr]._ptargx, plr[dbgplr]._ptargy); + const SDL_Point target = plr[dbgplr].GetTargetPosition(); + sprintf(dstr, " x = %i, y = %i : tx = %i, ty = %i", plr[dbgplr]._px, plr[dbgplr]._py, target.x, target.y); NetSendCmdString(1 << myplr, dstr); sprintf(dstr, " mode = %i : daction = %i : walk[0] = %i", plr[dbgplr]._pmode, plr[dbgplr].destAction, plr[dbgplr].walkpath[0]); NetSendCmdString(1 << myplr, dstr); diff --git a/Source/loadsave.cpp b/Source/loadsave.cpp index a6fbee43b..bdd638a1e 100644 --- a/Source/loadsave.cpp +++ b/Source/loadsave.cpp @@ -5,6 +5,7 @@ */ #include "loadsave.h" +#include #include #include "automap.h" @@ -327,8 +328,7 @@ static void LoadPlayer(LoadHelper *file, int p) pPlayer->_py = file->nextLE(); pPlayer->_pfutx = file->nextLE(); pPlayer->_pfuty = file->nextLE(); - pPlayer->_ptargx = file->nextLE(); - pPlayer->_ptargy = file->nextLE(); + file->skip(8); // Skip _ptargx and _ptargy pPlayer->_pownerx = file->nextLE(); pPlayer->_pownery = file->nextLE(); pPlayer->_poldx = file->nextLE(); @@ -1303,8 +1303,12 @@ static void SavePlayer(SaveHelper *file, int p) file->writeLE(pPlayer->_py); file->writeLE(pPlayer->_pfutx); file->writeLE(pPlayer->_pfuty); - file->writeLE(pPlayer->_ptargx); - file->writeLE(pPlayer->_ptargy); + + // For backwards compatibility + const SDL_Point target = pPlayer->GetTargetPosition(); + file->writeLE(target.x); + file->writeLE(target.y); + file->writeLE(pPlayer->_pownerx); file->writeLE(pPlayer->_pownery); file->writeLE(pPlayer->_poldx); diff --git a/Source/multi.cpp b/Source/multi.cpp index 6916fbc24..a269373e3 100644 --- a/Source/multi.cpp +++ b/Source/multi.cpp @@ -4,6 +4,7 @@ * Implementation of functions for keeping multiplaye games in sync. */ +#include #include #include "diablo.h" @@ -114,13 +115,13 @@ static BYTE *multi_recv_packet(TBuffer *pBuf, BYTE *body, DWORD *size) static void NetRecvPlrData(TPkt *pkt) { - plr[myplr].UpdateTargetPosition(); + const SDL_Point target = plr[myplr].GetTargetPosition(); pkt->hdr.wCheck = LOAD_BE32("\0\0ip"); pkt->hdr.px = plr[myplr]._px; pkt->hdr.py = plr[myplr]._py; - pkt->hdr.targx = plr[myplr]._ptargx; - pkt->hdr.targy = plr[myplr]._ptargy; + pkt->hdr.targx = target.x; + pkt->hdr.targy = target.y; pkt->hdr.php = plr[myplr]._pHitPoints; pkt->hdr.pmhp = plr[myplr]._pMaxHP; pkt->hdr.bstr = plr[myplr]._pBaseStr; @@ -510,8 +511,6 @@ void multi_process_network_packets() plr[dwID]._py = pkt->py; plr[dwID]._pfutx = pkt->px; plr[dwID]._pfuty = pkt->py; - plr[dwID]._ptargx = pkt->targx; - plr[dwID]._ptargy = pkt->targy; } } } @@ -637,8 +636,6 @@ static void SetupLocalCoords() plr[myplr]._py = y; plr[myplr]._pfutx = x; plr[myplr]._pfuty = y; - plr[myplr]._ptargx = x; - plr[myplr]._ptargy = y; plr[myplr].plrlevel = currlevel; plr[myplr]._pLvlChanging = true; plr[myplr].pLvlLoad = 0; diff --git a/Source/pack.cpp b/Source/pack.cpp index ade370227..33c3ca6ec 100644 --- a/Source/pack.cpp +++ b/Source/pack.cpp @@ -62,8 +62,10 @@ void PackPlayer(PkPlayerStruct *pPack, int pnum, bool manashield) pPack->plrlevel = pPlayer->plrlevel; pPack->px = pPlayer->_px; pPack->py = pPlayer->_py; - pPack->targx = pPlayer->_ptargx; - pPack->targy = pPlayer->_ptargy; + if (gbVanilla) { + pPack->targx = pPlayer->_px; + pPack->targy = pPlayer->_py; + } strcpy(pPack->pName, pPlayer->_pName); pPack->pClass = static_cast(pPlayer->_pClass); pPack->pBaseStr = pPlayer->_pBaseStr; @@ -215,8 +217,6 @@ void UnPackPlayer(PkPlayerStruct *pPack, int pnum, bool netSync) pPlayer->_py = pPack->py; pPlayer->_pfutx = pPack->px; pPlayer->_pfuty = pPack->py; - pPlayer->_ptargx = pPack->targx; - pPlayer->_ptargy = pPack->targy; pPlayer->plrlevel = pPack->plrlevel; ClrPlrPath(pnum); pPlayer->destAction = ACTION_NONE; diff --git a/Source/player.cpp b/Source/player.cpp index eece2231f..0f5f28c98 100644 --- a/Source/player.cpp +++ b/Source/player.cpp @@ -3,6 +3,7 @@ * * Implementation of player functionality, leveling, actions, creation, loading, etc. */ +#include #include #include "control.h" @@ -224,22 +225,22 @@ int PlayerStruct::GetMaximumAttributeValue(CharacterAttribute attribute) const return MaxStats[static_cast(_pClass)][static_cast(attribute)]; } -void PlayerStruct::UpdateTargetPosition() +SDL_Point PlayerStruct::GetTargetPosition() const { // clang-format off - int directionOffsetX[8] = { 0,-1, 1, 0,-1, 1, 1,-1 }; - int directionOffsetY[8] = { -1, 0, 0, 1,-1,-1, 1, 1 }; + constexpr int directionOffsetX[8] = { 0,-1, 1, 0,-1, 1, 1,-1 }; + constexpr int directionOffsetY[8] = { -1, 0, 0, 1,-1,-1, 1, 1 }; // clang-format on - _ptargx = _pfutx; - _ptargy = _pfuty; + SDL_Point target { _pfutx, _pfuty }; for (auto step : walkpath) { if (step == WALK_NONE) break; if (step > 0) { - _ptargx += directionOffsetX[step - 1]; - _ptargy += directionOffsetY[step - 1]; + target.x += directionOffsetX[step - 1]; + target.y += directionOffsetY[step - 1]; } } + return target; } _sfx_id herosounds[enum_size::value][102] = { @@ -1078,11 +1079,7 @@ void InitPlayer(int pnum, bool FirstTime) plr[pnum]._px = ViewX; plr[pnum]._py = ViewY; } - plr[pnum]._ptargx = plr[pnum]._px; - plr[pnum]._ptargy = plr[pnum]._py; } else { - plr[pnum]._ptargx = plr[pnum]._px; - plr[pnum]._ptargy = plr[pnum]._py; for (i = 0; i < 8 && !PosOkPlayer(pnum, plrxoff2[i] + plr[pnum]._px, plryoff2[i] + plr[pnum]._py); i++) ; plr[pnum]._px += plrxoff2[i]; @@ -4018,9 +4015,6 @@ void SyncInitPlrPos(int pnum) DWORD i; bool posOk; - plr[pnum]._ptargx = plr[pnum]._px; - plr[pnum]._ptargy = plr[pnum]._py; - if (!gbIsMultiplayer || plr[pnum].plrlevel != currlevel) { return; } @@ -4055,8 +4049,6 @@ void SyncInitPlrPos(int pnum) if (pnum == myplr) { plr[pnum]._pfutx = x; plr[pnum]._pfuty = y; - plr[pnum]._ptargx = x; - plr[pnum]._ptargy = y; ViewX = x; ViewY = y; } diff --git a/Source/player.h b/Source/player.h index cbe33dc99..3beeeab02 100644 --- a/Source/player.h +++ b/Source/player.h @@ -5,6 +5,7 @@ */ #pragma once +#include #include #include "diablo.h" @@ -160,12 +161,6 @@ struct PlayerStruct { int _py; // Tile Y-position of player int _pfutx; // Future tile X-position of player. Set at start of walking animation int _pfuty; - /* - * These values store the target position for non-local players during network play. - * This is needed because player positions drift over time due to desyncs, so we have - * clients try to pathfind non-local players to this position. - */ - int _ptargx, _ptargy; int _pownerx; // Tile X-position of player. Set via network on player input int _pownery; // Tile X-position of player. Set via network on player input int _poldx; // Most recent X-position in dPlayer. @@ -372,9 +367,9 @@ struct PlayerStruct { int GetMaximumAttributeValue(CharacterAttribute attribute) const; /** - * @brief Update tile coordinates a player is moving to (if not moving, then it corresponds to current position). + * @brief Get the tile coordinates a player is moving to (if not moving, then it corresponds to current position). */ - void UpdateTargetPosition(); + SDL_Point GetTargetPosition() const; /** * @brief Play a speach file. diff --git a/Source/track.cpp b/Source/track.cpp index 091cfeb22..7f9fba8a7 100644 --- a/Source/track.cpp +++ b/Source/track.cpp @@ -3,6 +3,7 @@ * * Implementation of functionality tracking what the mouse cursor is pointing at. */ +#include #include "cursor.h" #include "player.h" @@ -28,8 +29,8 @@ void track_process() if (plr[myplr]._pVar8 <= 6 && plr[myplr]._pmode != PM_STAND) return; - plr[myplr].UpdateTargetPosition(); - if (cursmx != plr[myplr]._ptargx || cursmy != plr[myplr]._ptargy) { + const SDL_Point target = plr[myplr].GetTargetPosition(); + if (cursmx != target.x || cursmy != target.y) { Uint32 tick = SDL_GetTicks(); if ((int)(tick - sgdwLastWalk) >= gnTickDelay * 6) { sgdwLastWalk = tick; diff --git a/test/writehero_test.cpp b/test/writehero_test.cpp index a329df7a3..790b75b7e 100644 --- a/test/writehero_test.cpp +++ b/test/writehero_test.cpp @@ -254,8 +254,6 @@ static void AssertPlayer(PlayerStruct *pPlayer) ASSERT_EQ(pPlayer->_py, 68); ASSERT_EQ(pPlayer->_pfutx, 75); ASSERT_EQ(pPlayer->_pfuty, 68); - ASSERT_EQ(pPlayer->_ptargx, 75); - ASSERT_EQ(pPlayer->_ptargy, 68); ASSERT_EQ(pPlayer->plrlevel, 0); ASSERT_EQ(pPlayer->destAction, -1); ASSERT_STREQ(pPlayer->_pName, "TestPlayer");