Skip to content

Commit

Permalink
Fixed synchronisation in multiplayer when using PLAYER mode
Browse files Browse the repository at this point in the history
Former-commit-id: 6c70631
  • Loading branch information
mwydmuch committed Nov 26, 2016
1 parent cac40cf commit 64a31a1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 51 deletions.
71 changes: 36 additions & 35 deletions src/vizdoom/src/d_net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
EXTERN_CVAR (Bool, viz_controlled)
EXTERN_CVAR (Bool, viz_async)
EXTERN_CVAR (Bool, viz_allow_input)
EXTERN_CVAR (Int, viz_sync_timeout)
EXTERN_CVAR (Bool, viz_nosound)

EXTERN_CVAR (Int, disableautosave)
Expand Down Expand Up @@ -96,7 +97,7 @@ BYTE NetMode = NET_PeerToPeer;
//
// gametic is the tic about to (or currently being) run
// maketic is the tick that hasn't had control made for it yet
// nettics[] has the maketics for all players
// nettics[] has the maketics for all players
//
// a gametic cannot be run until nettics[] > gametic for all players
//
Expand Down Expand Up @@ -128,8 +129,8 @@ int maketic;
int skiptics;
int ticdup;

void D_ProcessEvents (void);
void G_BuildTiccmd (ticcmd_t *cmd);
void D_ProcessEvents (void);
void G_BuildTiccmd (ticcmd_t *cmd);
void D_DoAdvanceDemo (void);

static void SendSetup (DWORD playersdetected[MAXNETNODES], BYTE gotsetup[MAXNETNODES], int len);
Expand Down Expand Up @@ -422,14 +423,14 @@ int ExpandTics (int low)
int mt = maketic / ticdup;

delta = low - (mt&0xff);

if (delta >= -64 && delta <= 64)
return (mt&~0xff) + low;
if (delta > 64)
return (mt&~0xff) - 256 + low;
if (delta < -64)
return (mt&~0xff) + 256 + low;

I_Error ("ExpandTics: strange value %i at maketic %i", low, maketic);
return 0;
}
Expand Down Expand Up @@ -481,7 +482,7 @@ void HSendPacket (int node, int len)
node,
ExpandTics(netbuffer[1]),
numtics, realretrans, len);

for (i = 0; i < len; i++)
fprintf (debugfile, "%c%2x", i==k?'|':' ', ((BYTE *)netbuffer)[i]);
}
Expand Down Expand Up @@ -584,7 +585,7 @@ bool HGetPacket (void)
InBuffer.Push(store);
doomcom.remotenode = -1;
}

if (doomcom.remotenode == -1)
{
bool gotmessage = false;
Expand All @@ -607,7 +608,7 @@ bool HGetPacket (void)
return false;
}
#endif

if (debugfile)
{
int i, k, realretrans;
Expand Down Expand Up @@ -649,7 +650,7 @@ bool HGetPacket (void)
doomcom.remotenode,
ExpandTics(netbuffer[1]),
numtics, realretrans, doomcom.datalength);

for (i = 0; i < doomcom.datalength; i++)
fprintf (debugfile, "%c%2x", i==k?'|':' ', ((BYTE *)netbuffer)[i]);
if (numtics)
Expand All @@ -671,7 +672,7 @@ bool HGetPacket (void)
return false;
}

return true;
return true;
}

void PlayerIsGone (int netnode, int netconsole)
Expand Down Expand Up @@ -762,7 +763,7 @@ void GetPackets (void)
int k;
BYTE playerbytes[MAXNETNODES];
int numplayers;

while ( HGetPacket() )
{
if (netbuffer[0] & NCMD_SETUP)
Expand All @@ -775,12 +776,13 @@ void GetPackets (void)
}
continue; // extra setup packet
}

netnode = doomcom.remotenode;
netconsole = playerfornode[netnode] & ~PL_DRONE;

//VIZDOOM_CODE
vizNodesInSync[netconsole] = 1;
vizNodesRecv[netconsole] += 1;
VIZ_InterruptionPoint();

// [RH] Get "ping" times - totally useless, since it's bound to the frequency
// packets go out at.
Expand Down Expand Up @@ -873,9 +875,9 @@ void GetPackets (void)
// Figure out what the rest of the bytes are
realstart = ExpandTics (netbuffer[1]);
realend = (realstart + numtics);

nodeforplayer[netconsole] = netnode;

// check for retransmit request
if (resendcount[netnode] <= 0 && (netbuffer[0] & NCMD_RETRANSMIT))
{
Expand All @@ -888,19 +890,19 @@ void GetPackets (void)
{
resendcount[netnode]--;
}
// check for out of order / duplicated packet

// check for out of order / duplicated packet
if (realend == nettics[netnode])
continue;

if (realend < nettics[netnode])
{
if (debugfile)
fprintf (debugfile, "out of order packet (%i + %i)\n" ,
realstart, numtics);
continue;
}

// check for a missed packet
if (realstart > nettics[netnode])
{
Expand Down Expand Up @@ -1177,7 +1179,7 @@ void NetUpdate (void)
switch (net_extratic)
{
case 0:
default:
default:
resendto[i] = lowtic; break;
case 1: resendto[i] = MAX(0, lowtic - 1); break;
case 2: resendto[i] = nettics[i]; break;
Expand Down Expand Up @@ -1352,7 +1354,7 @@ void NetUpdate (void)
totalavg = lastaverage;
}
}

mastertics = nettics[nodeforplayer[Net_Arbitrator]] + totalavg;
}
if (nettics[0] <= mastertics)
Expand Down Expand Up @@ -1881,7 +1883,7 @@ void TryRunTics (void)
// }

VIZ_DebugMsg(2, VIZ_FUNC, "gametic: %d, lowtic %d, availabletics: %d, realtics: %d, counts: %d", gametic, lowtic, availabletics, realtics, counts);

// Uncapped framerate needs seprate checks
if (counts == 0 && !doWait)
{
Expand All @@ -1907,18 +1909,17 @@ void TryRunTics (void)
realtics, availabletics, counts);

//VIZDOOM_CODE
vizNetticsSum = netticsSum;
if(*viz_controlled && !*viz_async && netgame){
int syncCount;
int lowRecv;
unsigned int enterTime = I_MSTime ();
do {
syncCount = 0;
NetUpdate();
for (i = 0; i < numplaying; ++i)
syncCount += vizNodesInSync[i];
} while(syncCount!= numplaying);
lowRecv = INT_MAX;

for (i = 0; i < MAXPLAYERS; i++)
vizNodesInSync[i] = 0;
for (i = 1; i < numplaying; i++) {
if (lowRecv > vizNodesRecv[i]) lowRecv = vizNodesRecv[i];
}
} while(lowRecv == lowtic && *viz_sync_timeout * numplaying > I_MSTime() - enterTime);
}

// wait for new tics if needed
Expand Down Expand Up @@ -2097,7 +2098,7 @@ void FDynamicBuffer::SetData (const BYTE *data, int len)
m_Len = len;
memcpy (m_Data, data, len);
}
else
else
{
m_Len = 0;
}
Expand Down Expand Up @@ -2144,7 +2145,7 @@ static int RemoveClass(const PClass *cls)
player = true;
continue;
}
removecount++;
removecount++;
actor->ClearCounters();
actor->Destroy();
}
Expand Down Expand Up @@ -2366,7 +2367,7 @@ void Net_DoCommand (int type, BYTE **stream, int player)
{
if (type == DEM_SUMMONFRIEND || type == DEM_SUMMONFRIEND2 || type == DEM_SUMMONMBF)
{
if (spawned->CountsAsKill())
if (spawned->CountsAsKill())
{
level.total_monsters--;
}
Expand Down Expand Up @@ -2563,7 +2564,7 @@ void Net_DoCommand (int type, BYTE **stream, int player)
break;

case DEM_CROUCH:
if (gamestate == GS_LEVEL && players[player].mo != NULL &&
if (gamestate == GS_LEVEL && players[player].mo != NULL &&
players[player].health > 0 && !(players[player].oldbuttons & BT_JUMP) &&
!P_IsPlayerTotallyFrozen(&players[player]))
{
Expand Down Expand Up @@ -2727,7 +2728,7 @@ static void RunScript(BYTE **stream, APlayerPawn *pawn, int snum, int argn, int
{
int arg[4] = { 0, 0, 0, 0 };
int i;

for (i = 0; i < argn; ++i)
{
int argval = ReadLong(stream);
Expand Down
23 changes: 11 additions & 12 deletions src/vizdoom/src/viz_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ CVAR (Int, viz_debug, 0, CVAR_NOSET)

CVAR (Bool, viz_debug_instances, 0, CVAR_NOSET) // prints instance id with every message


// control
CVAR (Bool, viz_controlled, false, CVAR_NOSET)
CVAR (String, viz_instance_id, "0", CVAR_NOSET)
Expand All @@ -79,6 +78,7 @@ CVAR (Int, viz_seed, 0, CVAR_NOSET)
// modes
CVAR (Bool, viz_async, false, CVAR_NOSET)
CVAR (Bool, viz_allow_input, false, CVAR_NOSET)
CVAR (Int, viz_sync_timeout, 3500, CVAR_NOSET)

// buffers
CVAR (Int, viz_screen_format, 0, 0)
Expand Down Expand Up @@ -113,21 +113,18 @@ CCMD(viz_set_seed){
/* Flow */
/*--------------------------------------------------------------------------------------------------------------------*/

int vizNetticsSum = -1;
int vizNodesInSync[VIZ_MAX_PLAYERS];
int vizNodesLastSync[VIZ_MAX_PLAYERS];
int vizTime = 0;
bool vizNextTic = false;
bool vizUpdate = false;
int vizLastUpdate = 0;
int vizNodesRecv[VIZ_MAX_PLAYERS];

int vizSavedTime = 0;
bool vizFreeze = false;
int (*_I_GetTime)(bool);
int (*_I_WaitForTic)(int);
void (*_I_FreezeTime)(bool);

int vizTime = 0;
bool vizNextTic = false;
bool vizUpdate = false;
unsigned int vizLastUpdate = 0;

int VIZ_GetTime(bool saveMS){
if(saveMS) vizSavedTime = vizTime;
return vizTime;
Expand Down Expand Up @@ -170,6 +167,8 @@ void VIZ_Init(){
I_WaitForTic = &VIZ_WaitForTic;
I_FreezeTime = &VIZ_FreezeTime;
}

//for(size_t i = 0; i < VIZ_MAX_PLAYERS; ++i) vizNodesRecv[i] = 0;
}
}

Expand Down Expand Up @@ -221,7 +220,7 @@ void VIZ_Tic(){
}

void VIZ_Update(){
VIZ_DebugMsg(3, VIZ_FUNC, "tic: %d, vizTime: %d, lastupdate: %d", gametic, vizTime, vizLastUpdate);
VIZ_DebugMsg(3, VIZ_FUNC, "tic: %d, vizTime: %d, lastupdate: %d", gametic, VIZ_TIME, vizLastUpdate);

if(!*viz_nocheat){
VIZ_D_MapDisplay();
Expand All @@ -231,7 +230,7 @@ void VIZ_Update(){
VIZ_ScreenUpdate();
VIZ_GameStateUpdateLabels();

vizLastUpdate = vizTime;
vizLastUpdate = VIZ_TIME;
vizUpdate = false;
}

Expand Down Expand Up @@ -437,7 +436,7 @@ void VIZ_InterruptionPoint(){
try{
bt::interruption_point();
}
catch(b::thread_interrupted &ex ){
catch(b::thread_interrupted &ex){
exit(0);
}
}
6 changes: 2 additions & 4 deletions src/vizdoom/src/viz_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@
#include "viz_defines.h"

extern int vizTime;
extern int vizNetticsSum;
extern int vizNodesInSync[VIZ_MAX_PLAYERS];
extern int vizNodesLastSync[VIZ_MAX_PLAYERS];
extern bool vizNextTic;
extern bool vizUpdate;
extern unsigned int vizLastUpdate;
extern int vizLastUpdate;
extern int vizNodesRecv[VIZ_MAX_PLAYERS];

void VIZ_Init();

Expand Down

0 comments on commit 64a31a1

Please sign in to comment.