/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

  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. #ifndef INC_C4GameControl
  21. #define INC_C4GameControl
  22. #include "C4Control.h"
  23. #include "C4Network2Client.h"
  24. #include "C4Record.h"
  25. enum C4ControlMode
  26. {
  27. CM_None,
  28. CM_Local, // control = input
  29. CM_Network, // control = input + network input
  30. CM_Replay, // control = replay
  31. };
  32. enum C4ControlDeliveryType
  33. {
  34. CDT_Queue = 0, // Send in control queue (sync)
  35. CDT_Sync = 1, // Send, delay execution until net is sync (sync)
  36. CDT_Direct = 2, // Send directly to all clients (not sync)
  37. CDT_Private = 3, // Send only to some clients (not sync, obviously)
  38. CDT_Decide, // Use whatever sync mode seems fastest atm (sync)
  39. };
  40. // Additional notes / requirements:
  41. // * Direct control always reaches all clients faster than queue control.
  42. // * Sync is the only synchronous control mode were it's garantueed that
  43. // the control is actually executed within a fixed time.
  44. // * CDT_Decide is guesswork, control isn't garantueed to be faster.
  45. #include "C4GameControlNetwork.h"
  46. #ifdef _DEBUG
  47. const int32_t C4SyncCheckRate = 1,
  48. #else
  49. const int32_t C4SyncCheckRate = 100,
  50. #endif
  51. C4SyncCheckMaxKeep = 50;
  52. class C4GameControl
  53. {
  54. friend class C4ControlSyncCheck;
  55. friend class C4GameControlNetwork;
  56. public:
  57. C4GameControl();
  58. ~C4GameControl();
  59. public:
  60. C4Control Input;
  61. C4GameControlNetwork Network;
  62. protected:
  63. C4ControlMode eMode;
  64. bool fPreInit, fInitComplete;
  65. bool fHost; // (set for local, too)
  66. bool fActivated;
  67. bool fRecordNeeded;
  68. int32_t iClientID;
  69. C4Record *pRecord;
  70. C4Playback *pPlayback;
  71. C4Control SyncChecks;
  72. C4GameControlClient *pClients;
  73. C4Control *pExecutingControl; // Control that is in the process of being executed - needed by non-initial records
  74. public:
  75. // ticks
  76. int32_t ControlRate;
  77. int32_t ControlTick;
  78. int32_t SyncRate;
  79. bool DoSync;
  80. public:
  81. // configuration
  82. bool isLocal() const { return eMode == CM_Local; }
  83. bool isNetwork() const { return eMode == CM_Network; }
  84. bool isReplay() const { return eMode == CM_Replay; }
  85. bool isCtrlHost() const { return fHost; }
  86. bool isRecord() const { return !! pRecord; }
  87. int32_t ClientID() const { return iClientID; }
  88. bool SyncMode() const { return eMode != CM_Local || pRecord; }
  89. bool NoInput() const { return isReplay(); }
  90. // client list
  91. C4GameControlClient *getClient(int32_t iID);
  92. C4GameControlClient *getClient(const char *szName);
  93. // initialization
  94. bool InitLocal(C4Client *pLocal);
  95. bool InitNetwork(C4Client *pLocal);
  96. bool InitReplay(C4Group &rGroup);
  97. void ChangeToLocal();
  98. void Clear();
  99. void Default();
  100. // records
  101. bool StartRecord(bool fInitial, bool fStreaming);
  102. void StopRecord(StdStrBuf *pRecordName = NULL, BYTE *pRecordSHA1 = NULL);
  103. void RequestRuntimeRecord();
  104. bool IsRuntimeRecordPossible() const;
  105. bool RecAddFile(const char *szLocalFilename, const char *szAddAs);
  106. // execution
  107. bool Prepare();
  108. void Execute();
  109. void Ticks();
  110. // public helpers
  111. bool CtrlTickReached(int32_t iTick);
  112. int32_t getCtrlTick(int32_t iFrame) const;
  113. int32_t getNextControlTick() const;
  114. // control rate
  115. void AdjustControlRate(int32_t iBy);
  116. bool KeyAdjustControlRate(int32_t iBy)
  117. { AdjustControlRate(iBy); return true; }
  118. // activation
  119. void SetActivated(bool fActivated);
  120. // input
  121. void DoInput(C4PacketType eCtrlType, C4ControlPacket *pPkt, C4ControlDeliveryType eDelivery);
  122. void DbgRec(C4RecordChunkType eType, const uint8_t *pData=NULL, size_t iSize=0); // record debug stuff
  123. C4ControlDeliveryType DecideControlDelivery();
  124. // sync check
  125. void DoSyncCheck();
  126. // execute and record control (by self or C4GameControlNetwork)
  127. void ExecControl(const C4Control &rCtrl);
  128. void ExecControlPacket(C4PacketType eCtrlType, class C4ControlPacket *pPkt);
  129. void OnGameSynchronizing(); // start record if desired
  130. protected:
  131. // sync checks
  132. C4ControlSyncCheck *GetSyncCheck(int32_t iTick);
  133. void RemoveOldSyncChecks();
  134. };
  135. extern C4GameControl Control;
  136. #endif // INC_C4GameControl