PageRenderTime 48ms CodeModel.GetById 26ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 1ms

/src/control/C4GameControl.h

https://bitbucket.org/randrian/openclonk2
C Header | 171 lines | 99 code | 35 blank | 37 comment | 5 complexity | 60ba9a38620800f6196b6ddf490b8260 MD5 | raw file
Possible License(s): WTFPL, 0BSD, LGPL-2.1, CC-BY-3.0
  1/*
  2 * OpenClonk, http://www.openclonk.org
  3 *
  4 * Copyright (c) 2004-2005  Peter Wortmann
  5 * Copyright (c) 2005  Sven Eberhardt
  6 * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de
  7 *
  8 * Portions might be copyrighted by other authors who have contributed
  9 * to OpenClonk.
 10 *
 11 * Permission to use, copy, modify, and/or distribute this software for any
 12 * purpose with or without fee is hereby granted, provided that the above
 13 * copyright notice and this permission notice appear in all copies.
 14 * See isc_license.txt for full license and disclaimer.
 15 *
 16 * "Clonk" is a registered trademark of Matthes Bender.
 17 * See clonk_trademark_license.txt for full license.
 18 */
 19/* control management */
 20
 21#ifndef INC_C4GameControl
 22#define INC_C4GameControl
 23
 24#include "C4Control.h"
 25#include "C4Network2Client.h"
 26#include "C4Record.h"
 27
 28enum C4ControlMode
 29{
 30	CM_None,
 31	CM_Local,					// control = input
 32	CM_Network,				// control = input + network input
 33	CM_Replay,				// control = replay
 34};
 35
 36enum C4ControlDeliveryType
 37{
 38  CDT_Queue		= 0,	// Send in control queue (sync)
 39  CDT_Sync		= 1,	// Send, delay execution until net is sync (sync)
 40  CDT_Direct	= 2,	// Send directly to all clients (not sync)
 41  CDT_Private	= 3,  // Send only to some clients (not sync, obviously)
 42
 43  CDT_Decide,				// Use whatever sync mode seems fastest atm (sync)
 44};
 45
 46// Additional notes / requirements:
 47// * Direct control always reaches all clients faster than queue control.
 48// * Sync is the only synchronous control mode were it's garantueed that
 49//   the control is actually executed within a fixed time.
 50// * CDT_Decide is guesswork, control isn't garantueed to be faster.
 51
 52#include "C4GameControlNetwork.h"
 53
 54#ifdef _DEBUG
 55const int32_t C4SyncCheckRate = 1,
 56#else
 57const int32_t C4SyncCheckRate = 100,
 58#endif
 59					C4SyncCheckMaxKeep = 50;
 60
 61class C4GameControl
 62{
 63	friend class C4ControlSyncCheck;
 64	friend class C4GameControlNetwork;
 65public:
 66	C4GameControl();
 67	~C4GameControl();
 68
 69public:
 70	C4Control						Input;
 71	C4GameControlNetwork Network;
 72
 73protected:
 74	C4ControlMode				eMode;
 75  bool                fPreInit, fInitComplete;
 76	bool								fHost;							// (set for local, too)
 77	bool								fActivated;
 78	bool                fRecordNeeded;
 79	int32_t							iClientID;
 80
 81	C4Record						*pRecord;
 82	C4Playback					*pPlayback;
 83
 84	C4Control						SyncChecks;
 85
 86  C4GameControlClient *pClients;
 87
 88	C4Control           *pExecutingControl; // Control that is in the process of being executed - needed by non-initial records
 89
 90public:
 91	// ticks
 92	int32_t							ControlRate;
 93	int32_t							ControlTick;
 94	int32_t							SyncRate;
 95	bool								DoSync;
 96
 97public:
 98
 99  // configuration
100	bool isLocal() const { return eMode == CM_Local; }
101	bool isNetwork() const { return eMode == CM_Network; }
102	bool isReplay() const { return eMode == CM_Replay; }
103	bool isCtrlHost() const { return fHost; }
104	bool isRecord() const { return !! pRecord; }
105	int32_t  ClientID() const { return iClientID; }
106	bool SyncMode() const { return eMode != CM_Local || pRecord; }
107
108	bool NoInput() const { return isReplay(); }
109
110  // client list
111  C4GameControlClient *getClient(int32_t iID);
112  C4GameControlClient *getClient(const char *szName);
113
114  // initialization
115	bool InitLocal(C4Client *pLocal);
116	bool InitNetwork(C4Client *pLocal);
117	bool InitReplay(C4Group &rGroup);
118
119	void ChangeToLocal();
120
121	void Clear();
122	void Default();
123
124  // records
125	bool StartRecord(bool fInitial, bool fStreaming);
126	void StopRecord(StdStrBuf *pRecordName = NULL, BYTE *pRecordSHA1 = NULL);
127	void RequestRuntimeRecord();
128	bool IsRuntimeRecordPossible() const;
129	bool RecAddFile(const char *szLocalFilename, const char *szAddAs);
130
131	// execution
132	bool Prepare();
133	void Execute();
134	void Ticks();
135
136	// public helpers
137  bool CtrlTickReached(int32_t iTick);
138	int32_t getCtrlTick(int32_t iFrame) const;
139	int32_t getNextControlTick() const;
140
141	// control rate
142	void AdjustControlRate(int32_t iBy);
143	bool KeyAdjustControlRate(int32_t iBy)
144		{ AdjustControlRate(iBy); return true; }
145
146	// activation
147	void SetActivated(bool fActivated);
148
149  // input
150  void DoInput(C4PacketType eCtrlType, C4ControlPacket *pPkt, C4ControlDeliveryType eDelivery);
151  void DbgRec(C4RecordChunkType eType, const uint8_t *pData=NULL, size_t iSize=0); // record debug stuff
152	C4ControlDeliveryType DecideControlDelivery();
153
154  // sync check
155  void DoSyncCheck();
156
157	// execute and record control (by self or C4GameControlNetwork)
158	void ExecControl(const C4Control &rCtrl);
159	void ExecControlPacket(C4PacketType eCtrlType, class C4ControlPacket *pPkt);
160	void OnGameSynchronizing(); // start record if desired
161
162protected:
163
164	// sync checks
165	C4ControlSyncCheck *GetSyncCheck(int32_t iTick);
166	void RemoveOldSyncChecks();
167
168};
169extern C4GameControl Control;
170
171#endif // INC_C4GameControl