aboutsummaryrefslogtreecommitdiff
path: root/src/parsec_server/include/e_simulator.h
blob: c339cf45422bd08bd407f8598ba5050f6e41dd6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
* PARSEC HEADER: e_simulator.h
*/

#ifndef _E_SIMULATOR_H_
#define _E_SIMULATOR_H_

#include "net_util.h"
#include "utl_math.h"			// for playerlerp_s

// forward decls --------------------------------------------------------------
//
class E_Distributable;
class E_SimClientNetOutput;
class E_ClientInfo;
class E_GameServer;
class E_REList;
template <class T> class UTL_List;


//FIXME:
// gamecode stuff is skipped for now
#define GAMECODE( x )		{}

// gamecode2 is executed
#define GAMECODE2( x )      x


// class holding a ship state -------------------------------------------------
//
class E_SimShipState
{
protected:

	Xmatrx		m_ObjPosition;
	fixed_t		m_CurSpeed;			
	bams_t 		m_CurYaw;				
	bams_t 		m_CurPitch;			
	bams_t 		m_CurRoll;			
	geomv_t		m_CurSlideHorz;		
	geomv_t		m_CurSlideVert;

public:	
	// standard ctor
	E_SimShipState()
	{
		Reset();
	}

	// reset al data members
	void Reset();

	E_SimShipState& operator= ( E_SimShipState& _other )
	{
		ASSERT( FALSE );
		return _other;//remove the warning without it
	}

	// copy from other 
	E_SimShipState& CopyFrom( E_SimShipState& _other )
	{
		memcpy( &m_ObjPosition, &_other.m_ObjPosition, sizeof( Xmatrx ) );
		m_CurSpeed		= _other.m_CurSpeed;
		m_CurYaw		= _other.m_CurYaw;
		m_CurPitch		= _other.m_CurPitch;
		m_CurRoll		= _other.m_CurRoll;
		m_CurSlideHorz	= _other.m_CurSlideHorz;
		m_CurSlideVert	= _other.m_CurSlideVert;
		
		return *this;
	}

	// copy information from ShipRemInfo struct
	void CopyFromPlayerAndShipStatus( RE_PlayerAndShipStatus& _other )
	{
		memcpy( &m_ObjPosition, &_other.ObjPosition, sizeof( Xmatrx ) );
		m_CurSpeed		= _other.CurSpeed;
		m_CurYaw		= _other.CurYaw;
		m_CurPitch		= _other.CurPitch;
		m_CurRoll		= _other.CurRoll;
		m_CurSlideHorz	= _other.CurSlideHorz;
		m_CurSlideVert	= _other.CurSlideVert;
	}

	// various get/set functions
	pXmatrx GetObjPosition()				{ return m_ObjPosition; }

	//FIXME: get rid of friend classes
	friend class E_SimClientState;
	friend class E_REList;
};


// class handling the network input for a client -----------------------------
//
class E_SimClientState {
protected:
	E_SimShipState*			m_States;
	int							m_nNumStateSlots;

	E_SimShipState			m_InputState;
	dword						m_nInputStateMessageID;
	dword						m_nLastInputStateMessageId;

	refframe_t					m_LastSwitchToSimRefFrame;

	int							m_nClientID;

	enum ClientMovementMode_t { CMM_SIMULATING, CMM_SMOOTHING };
	ClientMovementMode_t        m_ClientMovementMode;
	playerlerp_s				m_playerlerp;

	bool_t						m_ResyncClient;
    bool_t                      m_ClientHasStateSync;

public:
	E_SimClientState()
	{
		m_States				   = NULL;
		m_nNumStateSlots		   = -1;
		m_nInputStateMessageID	   = 0;
		m_nLastInputStateMessageId = 0;
		m_ClientMovementMode	   = CMM_SIMULATING;
		m_ResyncClient			   = FALSE;
		m_LastSwitchToSimRefFrame  = -1;
        m_ClientHasStateSync       = FALSE;
	}

	~E_SimClientState()
	{
		Reset();
	}

	// reset all data-members
	void Reset()
	{
		if ( m_States != NULL ) {
			delete []m_States;
			m_States = NULL;
		}
		m_nNumStateSlots			= -1;
		m_nInputStateMessageID		= 0;
		m_nLastInputStateMessageId	= 0;
		m_InputState.Reset();
		m_ClientMovementMode		= CMM_SIMULATING;
		m_ResyncClient				= FALSE;
		m_LastSwitchToSimRefFrame	= -1;
        m_ClientHasStateSync       = FALSE;
	}

	// connect a client
	void Connect( int nClientID );

	// disconnect a client
	void Disconnect()
	{
		Reset();
	}

	// check the movement bounds between the last received state
	int CheckMovementBounds( dword MessageID, RE_PlayerAndShipStatus* pPAS );

	// store the received state as the newest 
	void StoreNewState( dword MessageID, RE_PlayerAndShipStatus* pPAS );

	// reset the sim state
	void ForceNewState( E_SimShipState* pSimShipState );
	
	// either apply simulation or smoothing to get newest client-state
	void CalcNewState( refframe_t CurSimRefFrames );

	// dump some SimClientState for debugging purposes
	void Dump();

	// return the current/last simulation-state-slot
	E_SimShipState* GetCurSimFrameStateSlot();
	E_SimShipState* GetPrevSimFrameStateSlot();

	// set the client resync flag ( RE_PLAYERANDSHIPSTATUS for this client sent from server to client )
	void SetClientResync() { m_ResyncClient = TRUE; }

	// clear the client resync flag
	void ClearClientResync() { m_ResyncClient = FALSE; }

	// check whether we must resync this client
	int NeedsResync() { return m_ResyncClient; }
    
    // Check whether state variables (nebula id, ammo pack sizes) have been sent
    int HasState() { return m_ClientHasStateSync; }
    
    void SetState() { m_ClientHasStateSync = TRUE; }

protected:
	// calculate the newest smooothing target
	void _CalcNewSmoothingTarget();

	// check whether the change from the last to the current state involved a movement
	int _CheckForMovement( dword MessageID, RE_PlayerAndShipStatus* pPAS );

	// update the message ids the lateste state comes from ( dead reckoning )
	void _UpdateStateMessageID( dword MessageID );
};


// class for handling the overall simulation as well as all the states --------
//
class E_Simulator {
protected:

	refframe_t					m_CurRefFrame;				// the current RefFrame
	refframe_t					m_CurSimRefFrames;			// refframes to advance in current sim step
	refframe_t					m_LastSimRefFrame;			// last sim refframe
	
	E_SimPlayerInfo*			m_SimPlayerInfos;
	E_SimClientState*			m_SimClientState;

	int							m_nSimFrame;

	// ctor/dtor
	E_Simulator();
	~E_Simulator();

public:
	// SINGLETON access
	static E_Simulator* GetSimulator()
	{
		static E_Simulator _TheSimulator;
		return &_TheSimulator;
	}

	// main simulation method
	int DoSim( refframe_t CurSimRefFrame );

	// get the PlayerInfo for a client
	E_SimPlayerInfo* GetSimPlayerInfo( int nClientID );

	// return the current simulation refframe
	refframe_t GetCurSimRefFrame() { return m_CurRefFrame; }

	// return the refframes the simulation runs in this sim-frame
	refframe_t GetThisFrameRefFrames() { return m_CurSimRefFrames; }

	// get the SimClientState for a specific client
	E_SimClientState* GetSimClientState( int nClientID );

	// get the latest valid player
	E_SimShipState* GetLatestSimShipState( int nClientID );

	// reset all data
	void Reset();

	// connect the player in a specific slot
	int ConnectPlayer( int nClientID );

	// disconnect the player in a specific slot
	int DisconnectPlayer( int nClientID );

	// check whether a player in a slot is joined
	int IsPlayerJoined( int nClientID );

	// check whether a player in a slot is connected
	int IsPlayerDisconnected( int nClientID );

	// return the current simulation frame
	int	GetSimFrame() { return m_nSimFrame; }

protected:

	// update the shipstate ( according to the current sim refframes )
	void _UpdateShipState( int nClientID );

	// simulate ship movements
	void _SimulateShips();

	// calculate the refframes we want to apply to this simulation step
	int _CalcSimRefFrames();

	// apply the current sim-state to the engine state ( E_SimShipState -> ShipObject ) 
	void _ApplySimToEngineState();

	// apply the current engine state to the sim-state ( ShipObject -> E_SimShipState ) 
	void _ApplyEngineToSimState();
};


#endif // _E_SIMULATOR_H_