Browse Source

🔥 Get rid of _ptargx/y

pull/1635/head
Anders Jenbo 5 years ago
parent
commit
97d7171123
  1. 5
      Source/debug.cpp
  2. 12
      Source/loadsave.cpp
  3. 11
      Source/multi.cpp
  4. 8
      Source/pack.cpp
  5. 24
      Source/player.cpp
  6. 11
      Source/player.h
  7. 5
      Source/track.cpp
  8. 2
      test/writehero_test.cpp

5
Source/debug.cpp

@ -4,6 +4,8 @@
* Implementation of debug functions.
*/
#include <SDL.h>
#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);

12
Source/loadsave.cpp

@ -5,6 +5,7 @@
*/
#include "loadsave.h"
#include <SDL.h>
#include <climits>
#include "automap.h"
@ -327,8 +328,7 @@ static void LoadPlayer(LoadHelper *file, int p)
pPlayer->_py = file->nextLE<Sint32>();
pPlayer->_pfutx = file->nextLE<Sint32>();
pPlayer->_pfuty = file->nextLE<Sint32>();
pPlayer->_ptargx = file->nextLE<Sint32>();
pPlayer->_ptargy = file->nextLE<Sint32>();
file->skip(8); // Skip _ptargx and _ptargy
pPlayer->_pownerx = file->nextLE<Sint32>();
pPlayer->_pownery = file->nextLE<Sint32>();
pPlayer->_poldx = file->nextLE<Sint32>();
@ -1303,8 +1303,12 @@ static void SavePlayer(SaveHelper *file, int p)
file->writeLE<Sint32>(pPlayer->_py);
file->writeLE<Sint32>(pPlayer->_pfutx);
file->writeLE<Sint32>(pPlayer->_pfuty);
file->writeLE<Sint32>(pPlayer->_ptargx);
file->writeLE<Sint32>(pPlayer->_ptargy);
// For backwards compatibility
const SDL_Point target = pPlayer->GetTargetPosition();
file->writeLE<Sint32>(target.x);
file->writeLE<Sint32>(target.y);
file->writeLE<Sint32>(pPlayer->_pownerx);
file->writeLE<Sint32>(pPlayer->_pownery);
file->writeLE<Sint32>(pPlayer->_poldx);

11
Source/multi.cpp

@ -4,6 +4,7 @@
* Implementation of functions for keeping multiplaye games in sync.
*/
#include <SDL.h>
#include <config.h>
#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;

8
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<Sint8>(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;

24
Source/player.cpp

@ -3,6 +3,7 @@
*
* Implementation of player functionality, leveling, actions, creation, loading, etc.
*/
#include <SDL.h>
#include <algorithm>
#include "control.h"
@ -224,22 +225,22 @@ int PlayerStruct::GetMaximumAttributeValue(CharacterAttribute attribute) const
return MaxStats[static_cast<std::size_t>(_pClass)][static_cast<std::size_t>(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<HeroClass>::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;
}

11
Source/player.h

@ -5,6 +5,7 @@
*/
#pragma once
#include <SDL.h>
#include <stdint.h>
#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.

5
Source/track.cpp

@ -3,6 +3,7 @@
*
* Implementation of functionality tracking what the mouse cursor is pointing at.
*/
#include <SDL.h>
#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;

2
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");

Loading…
Cancel
Save