- hooked up ZDoom's d_net.cpp file.

Still not active but this contains some code needed to do a proper main loop that can work with the networker.
This commit is contained in:
Christoph Oelckers 2020-08-29 23:24:18 +02:00
parent 15adf1f6e5
commit c0ebe3e08b
7 changed files with 3039 additions and 0 deletions

View file

@ -783,6 +783,8 @@ set (PCH_SOURCES
core/mathutil.cpp core/mathutil.cpp
core/rts.cpp core/rts.cpp
core/ct_chat.cpp core/ct_chat.cpp
core/d_net.cpp
core/d_protocol.cpp
core/gameconfigfile.cpp core/gameconfigfile.cpp
core/gamecvars.cpp core/gamecvars.cpp
core/gamecontrol.cpp core/gamecontrol.cpp
@ -912,6 +914,7 @@ set (PCH_SOURCES
common/engine/sc_man.cpp common/engine/sc_man.cpp
common/engine/palettecontainer.cpp common/engine/palettecontainer.cpp
common/engine/stringtable.cpp common/engine/stringtable.cpp
common/engine/i_net.cpp
common/engine/i_interface.cpp common/engine/i_interface.cpp
common/engine/renderstyle.cpp common/engine/renderstyle.cpp
common/engine/v_colortables.cpp common/engine/v_colortables.cpp

2356
source/core/d_net.cpp Normal file

File diff suppressed because it is too large Load diff

74
source/core/d_net.h Normal file
View file

@ -0,0 +1,74 @@
#ifndef __D_NET__
#define __D_NET__
#include "i_net.h"
#include "d_ticcmd.h"
enum
{
MAXPLAYERS = 8
};
class FDynamicBuffer
{
public:
FDynamicBuffer ();
~FDynamicBuffer ();
void SetData (const uint8_t *data, int len);
uint8_t *GetData (int *len = NULL);
private:
uint8_t *m_Data;
int m_Len, m_BufferLen;
};
extern FDynamicBuffer NetSpecs[MAXPLAYERS][BACKUPTICS];
// Create any new ticcmds and broadcast to other players.
void NetUpdate (void);
// Broadcasts special packets to other players
// to notify of game exit
void D_QuitNetGame (void);
//? how many ticks to run?
void TryRunTics (void);
//Use for checking to see if the netgame has stalled
void Net_CheckLastReceived(int);
// [RH] Functions for making and using special "ticcmds"
void Net_NewMakeTic ();
void Net_WriteByte (uint8_t);
void Net_WriteWord (short);
void Net_WriteLong (int);
void Net_WriteFloat (float);
void Net_WriteString (const char *);
void Net_WriteBytes (const uint8_t *, int len);
void Net_DoCommand (int type, uint8_t **stream, int player);
void Net_SkipCommand (int type, uint8_t **stream);
void Net_ClearBuffers ();
// Netgame stuff (buffers and pointers, i.e. indices).
// This is the interface to the packet driver, a separate program
// in DOS, but just an abstraction here.
extern doomcom_t doomcom;
extern struct ticcmd_t localcmds[LOCALCMDTICS];
extern int maketic;
extern int nettics[MAXNETNODES];
extern int netdelay[MAXNETNODES][BACKUPTICS];
extern int nodeforplayer[MAXPLAYERS];
extern ticcmd_t netcmds[MAXPLAYERS][BACKUPTICS];
extern int ticdup;
#endif

443
source/core/d_protocol.cpp Normal file
View file

@ -0,0 +1,443 @@
/*
** d_protocol.cpp
** Basic network packet creation routines and simple IFF parsing
**
**---------------------------------------------------------------------------
** Copyright 1998-2006 Randy Heit
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
**
** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
** 3. The name of the author may not be used to endorse or promote products
** derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**---------------------------------------------------------------------------
**
*/
#include "d_protocol.h"
#include "d_net.h"
#include "cmdlib.h"
#include "serializer.h"
extern int gametic;
char *ReadString (uint8_t **stream)
{
char *string = *((char **)stream);
*stream += strlen (string) + 1;
return copystring (string);
}
const char *ReadStringConst(uint8_t **stream)
{
const char *string = *((const char **)stream);
*stream += strlen (string) + 1;
return string;
}
int ReadByte (uint8_t **stream)
{
uint8_t v = **stream;
*stream += 1;
return v;
}
int ReadWord (uint8_t **stream)
{
short v = (((*stream)[0]) << 8) | (((*stream)[1]));
*stream += 2;
return v;
}
int ReadLong (uint8_t **stream)
{
int v = (((*stream)[0]) << 24) | (((*stream)[1]) << 16) | (((*stream)[2]) << 8) | (((*stream)[3]));
*stream += 4;
return v;
}
float ReadFloat (uint8_t **stream)
{
union
{
int i;
float f;
} fakeint;
fakeint.i = ReadLong (stream);
return fakeint.f;
}
void WriteString (const char *string, uint8_t **stream)
{
char *p = *((char **)stream);
while (*string) {
*p++ = *string++;
}
*p++ = 0;
*stream = (uint8_t *)p;
}
void WriteByte (uint8_t v, uint8_t **stream)
{
**stream = v;
*stream += 1;
}
void WriteWord (short v, uint8_t **stream)
{
(*stream)[0] = v >> 8;
(*stream)[1] = v & 255;
*stream += 2;
}
void WriteLong (int v, uint8_t **stream)
{
(*stream)[0] = v >> 24;
(*stream)[1] = (v >> 16) & 255;
(*stream)[2] = (v >> 8) & 255;
(*stream)[3] = v & 255;
*stream += 4;
}
void WriteFloat (float v, uint8_t **stream)
{
union
{
int i;
float f;
} fakeint;
fakeint.f = v;
WriteLong (fakeint.i, stream);
}
// Returns the number of bytes read
int UnpackUserCmd (InputPacket *ucmd, const InputPacket *basis, uint8_t **stream)
{
uint8_t *start = *stream;
uint8_t flags;
if (basis != NULL)
{
if (basis != ucmd)
{
memcpy (ucmd, basis, sizeof(InputPacket));
}
}
else
{
memset (ucmd, 0, sizeof(InputPacket));
}
flags = ReadByte (stream);
if (flags)
{
// We can support up to 29 buttons, using from 0 to 4 bytes to store them.
if (flags & UCMDF_BUTTONS)
ucmd->actions = ESyncBits::FromInt(ReadLong(stream));
if (flags & UCMDF_PITCH)
ucmd->q16horz = ReadLong(stream);
if (flags & UCMDF_YAW)
ucmd->q16avel = ReadLong(stream);
if (flags & UCMDF_FORWARDMOVE)
ucmd->fvel = ReadWord (stream);
if (flags & UCMDF_SIDEMOVE)
ucmd->svel = ReadWord (stream);
if (flags & UCMDF_UPMOVE)
ucmd->q16horiz = ReadWord (stream);
if (flags & UCMDF_ROLL)
ucmd->q16ang = ReadWord (stream);
}
return int(*stream - start);
}
// Returns the number of bytes written
int PackUserCmd (const InputPacket *ucmd, const InputPacket *basis, uint8_t **stream)
{
uint8_t flags = 0;
uint8_t *temp = *stream;
uint8_t *start = *stream;
InputPacket blank;
if (basis == NULL)
{
memset (&blank, 0, sizeof(blank));
basis = &blank;
}
WriteByte (0, stream); // Make room for the packing bits
if (ucmd->actions != basis->actions)
{
flags |= UCMDF_BUTTONS;
WriteLong(ucmd->actions, stream);
}
if (ucmd->q16horz != basis->q16horz)
{
flags |= UCMDF_PITCH;
WriteLong (ucmd->q16horz, stream);
}
if (ucmd->q16avel != basis->q16avel)
{
flags |= UCMDF_YAW;
WriteLong (ucmd->q16avel, stream);
}
if (ucmd->fvel != basis->fvel)
{
flags |= UCMDF_FORWARDMOVE;
WriteWord (ucmd->fvel, stream);
}
if (ucmd->svel != basis->svel)
{
flags |= UCMDF_SIDEMOVE;
WriteWord (ucmd->svel, stream);
}
if (ucmd->q16horiz != basis->q16horiz)
{
flags |= UCMDF_UPMOVE;
WriteLong (ucmd->q16horiz, stream);
}
if (ucmd->q16ang != basis->q16ang)
{
flags |= UCMDF_ROLL;
WriteLong (ucmd->q16ang, stream);
}
// Write the packing bits
WriteByte (flags, &temp);
return int(*stream - start);
}
FSerializer &Serialize(FSerializer &arc, const char *key, ticcmd_t &cmd, ticcmd_t *def)
{
if (arc.BeginObject(key))
{
arc("consistency", cmd.consistancy)
("ucmd", cmd.ucmd)
.EndObject();
}
return arc;
}
FSerializer &Serialize(FSerializer &arc, const char *key, InputPacket &cmd, InputPacket *def)
{
if (arc.BeginObject(key))
{
arc("actions", cmd.actions)
("horz", cmd.q16horz)
("avel", cmd.q16avel)
("ang", cmd.q16ang)
("fvel", cmd.fvel)
("svwl", cmd.svel)
("q16horiz", cmd.q16horiz)
.EndObject();
}
return arc;
}
int WriteUserCmdMessage (InputPacket *ucmd, const InputPacket *basis, uint8_t **stream)
{
if (basis == NULL)
{
if (ucmd->actions != 0 ||
ucmd->q16horz != 0 ||
ucmd->q16avel != 0 ||
ucmd->fvel != 0 ||
ucmd->svel != 0 ||
ucmd->q16horiz != 0 ||
ucmd->q16ang != 0)
{
WriteByte (DEM_USERCMD, stream);
return PackUserCmd (ucmd, basis, stream) + 1;
}
}
else
if (ucmd->actions != basis->actions ||
ucmd->q16horz != basis->q16horz ||
ucmd->q16avel != basis->q16avel ||
ucmd->fvel != basis->fvel ||
ucmd->svel != basis->svel ||
ucmd->q16horiz != basis->q16horiz ||
ucmd->q16ang != basis->q16ang)
{
WriteByte (DEM_USERCMD, stream);
return PackUserCmd (ucmd, basis, stream) + 1;
}
WriteByte (DEM_EMPTYUSERCMD, stream);
return 1;
}
int SkipTicCmd (uint8_t **stream, int count)
{
int i, skip;
uint8_t *flow = *stream;
for (i = count; i > 0; i--)
{
bool moreticdata = true;
flow += 2; // Skip consistancy marker
while (moreticdata)
{
uint8_t type = *flow++;
if (type == DEM_USERCMD)
{
moreticdata = false;
skip = 1;
if (*flow & UCMDF_PITCH) skip += 4;
if (*flow & UCMDF_YAW) skip += 4;
if (*flow & UCMDF_FORWARDMOVE) skip += 2;
if (*flow & UCMDF_SIDEMOVE) skip += 2;
if (*flow & UCMDF_UPMOVE) skip += 4;
if (*flow & UCMDF_ROLL) skip += 4;
if (*flow & UCMDF_BUTTONS) skip += 4;
flow += skip;
}
else if (type == DEM_EMPTYUSERCMD)
{
moreticdata = false;
}
else
{
Net_SkipCommand (type, &flow);
}
}
}
skip = int(flow - *stream);
*stream = flow;
return skip;
}
extern short consistancy[MAXPLAYERS][BACKUPTICS];
void ReadTicCmd (uint8_t **stream, int player, int tic)
{
int type;
uint8_t *start;
ticcmd_t *tcmd;
int ticmod = tic % BACKUPTICS;
tcmd = &netcmds[player][ticmod];
tcmd->consistancy = ReadWord (stream);
start = *stream;
while ((type = ReadByte (stream)) != DEM_USERCMD && type != DEM_EMPTYUSERCMD)
Net_SkipCommand (type, stream);
NetSpecs[player][ticmod].SetData (start, int(*stream - start - 1));
if (type == DEM_USERCMD)
{
UnpackUserCmd (&tcmd->ucmd,
tic ? &netcmds[player][(tic-1)%BACKUPTICS].ucmd : NULL, stream);
}
else
{
if (tic)
{
memcpy (&tcmd->ucmd, &netcmds[player][(tic-1)%BACKUPTICS].ucmd, sizeof(tcmd->ucmd));
}
else
{
memset (&tcmd->ucmd, 0, sizeof(tcmd->ucmd));
}
}
#if 0
if (player==consoleplayer&&tic>BACKUPTICS)
assert(consistancy[player][ticmod] == tcmd->consistancy);
#endif
}
void RunNetSpecs (int player, int buf)
{
uint8_t *stream;
int len;
if (gametic % ticdup == 0)
{
stream = NetSpecs[player][buf].GetData (&len);
if (stream)
{
uint8_t *end = stream + len;
while (stream < end)
{
int type = ReadByte (&stream);
Net_DoCommand (type, &stream, player);
}
#if 0
if (!demorecording)
#endif
NetSpecs[player][buf].SetData (NULL, 0);
}
}
}
uint8_t *lenspot;
// Write the header of an IFF chunk and leave space
// for the length field.
void StartChunk (int id, uint8_t **stream)
{
WriteLong (id, stream);
lenspot = *stream;
*stream += 4;
}
// Write the length field for the chunk and insert
// pad byte if the chunk is odd-sized.
void FinishChunk (uint8_t **stream)
{
int len;
if (!lenspot)
return;
len = int(*stream - lenspot - 4);
WriteLong (len, &lenspot);
if (len & 1)
WriteByte (0, stream);
lenspot = NULL;
}
// Skip past an unknown chunk. *stream should be
// pointing to the chunk's length field.
void SkipChunk (uint8_t **stream)
{
int len;
len = ReadLong (stream);
*stream += len + (len & 1);
}

119
source/core/d_protocol.h Normal file
View file

@ -0,0 +1,119 @@
/*
** d_protocol.h
**
**---------------------------------------------------------------------------
** Copyright 1998-2006 Randy Heit
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
**
** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
** 3. The name of the author may not be used to endorse or promote products
** derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**---------------------------------------------------------------------------
**
*/
#ifndef __D_PROTOCOL_H__
#define __D_PROTOCOL_H__
#include <stdint.h>
#include "packet.h"
// The IFF routines here all work with big-endian IDs, even if the host
// system is little-endian.
#define BIGE_ID(a,b,c,d) ((d)|((c)<<8)|((b)<<16)|((a)<<24))
#define FORM_ID BIGE_ID('F','O','R','M')
#define ZDEM_ID BIGE_ID('R','D','E','M')
#define ZDHD_ID BIGE_ID('R','Z','H','D')
#define VARS_ID BIGE_ID('V','A','R','S')
#define UINF_ID BIGE_ID('U','I','N','F')
#define COMP_ID BIGE_ID('C','O','M','P')
#define BODY_ID BIGE_ID('B','O','D','Y')
#define NETD_ID BIGE_ID('N','E','T','D')
#define WEAP_ID BIGE_ID('W','E','A','P')
#define ANGLE2SHORT(x) ((((x)/360) & 65535)
#define SHORT2ANGLE(x) ((x)*360)
struct zdemoheader_s {
uint8_t demovermajor;
uint8_t demoverminor;
uint8_t minvermajor;
uint8_t minverminor;
uint8_t map[8];
unsigned int rngseed;
uint8_t consoleplayer;
};
class FArchive;
// When transmitted, the above message is preceded by a uint8_t
// indicating which fields are actually present in the message.
enum
{
UCMDF_BUTTONS = 0x01,
UCMDF_PITCH = 0x02,
UCMDF_YAW = 0x04,
UCMDF_FORWARDMOVE = 0x08,
UCMDF_SIDEMOVE = 0x10,
UCMDF_UPMOVE = 0x20,
UCMDF_ROLL = 0x40,
};
// When changing the following enum, be sure to update Net_SkipCommand()
// and Net_DoCommand() in d_net.cpp.
enum EDemoCommand
{
DEM_BAD, // 0 Bad command
DEM_USERCMD,
DEM_EMPTYUSERCMD,
};
void StartChunk (int id, uint8_t **stream);
void FinishChunk (uint8_t **stream);
void SkipChunk (uint8_t **stream);
int UnpackUserCmd (InputPacket *ucmd, const InputPacket*basis, uint8_t **stream);
int PackUserCmd (const InputPacket*ucmd, const InputPacket*basis, uint8_t **stream);
int WriteUserCmdMessage (InputPacket*ucmd, const InputPacket*basis, uint8_t **stream);
struct ticcmd_t;
int SkipTicCmd (uint8_t **stream, int count);
void ReadTicCmd (uint8_t **stream, int player, int tic);
void RunNetSpecs (int player, int buf);
int Readuint8_t (uint8_t **stream);
int Reauint32_t (uint8_t **stream);
int ReadLong (uint8_t **stream);
float ReadFloat (uint8_t **stream);
char *ReadString (uint8_t **stream);
const char *ReadStringConst(uint8_t **stream);
void WriteByte (uint8_t val, uint8_t **stream);
void WriteWord (short val, uint8_t **stream);
void WriteLong (int val, uint8_t **stream);
void WriteFloat (float val, uint8_t **stream);
void WriteString (const char *string, uint8_t **stream);
#endif //__D_PROTOCOL_H__

42
source/core/d_ticcmd.h Normal file
View file

@ -0,0 +1,42 @@
// Emacs style mode select -*- C++ -*-
//-----------------------------------------------------------------------------
//
// $Id:$
//
// Copyright (C) 1993-1996 by id Software, Inc.
//
// This source is available for distribution and/or modification
// only under the terms of the DOOM Source Code License as
// published by id Software. All rights reserved.
//
// The source is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// FITNESS FOR A PARTICULAR PURPOSE. See the DOOM Source Code License
// for more details.
//
// DESCRIPTION:
// System specific interface stuff.
//
//-----------------------------------------------------------------------------
#ifndef __D_TICCMD_H__
#define __D_TICCMD_H__
#include "d_protocol.h"
#include "packet.h"
// The data sampled per tick (single player)
// and transmitted to other peers (multiplayer).
// Mainly movements/button commands per game tick,
// plus a checksum for internal state consistency.
struct ticcmd_t
{
InputPacket ucmd;
uint16_t consistancy; // checks for net game
};
FArchive &operator<< (FArchive &arc, ticcmd_t &cmd);
#endif // __D_TICCMD_H__

View file

@ -82,6 +82,8 @@ const char *GetVersionString();
#define SAVEVER_SW 7 #define SAVEVER_SW 7
#define SAVEVER_PS 6 #define SAVEVER_PS 6
#define NETGAMEVERSION 1
#if defined(__APPLE__) || defined(_WIN32) #if defined(__APPLE__) || defined(_WIN32)
#define GAME_DIR GAMENAME #define GAME_DIR GAMENAME
#else #else