pawn.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. #ifdef FEATURE_NETWORK_RECONCILIATION
  2. //! TODO(kumarjac): Create and link external documentation.
  3. //! Glossary:
  4. //! Authority: The server
  5. //! Owner: The client that is controlling the pawn
  6. //! Proxy/Remote: The client(s) that aren't controlling the pawn
  7. //! NOTE: Do not depend on Serializer.
  8. //! Future updates may break compatibility if you diverge from only using Serializer.Write/Serializer.Read
  9. //! Serializer is not optimized for network traffic.
  10. typedef Serializer PawnStateWriter;
  11. typedef Serializer PawnStateReader;
  12. typedef Serializer PawnMoveWriter;
  13. typedef Serializer PawnMoveReader;
  14. /**
  15. PawnOwnerState
  16. Contains the full state of the Pawn for purposes of full state synchronization.
  17. Purpose is to be infrequently sent to the owner when there is a desync detected in playing of moves, triggering a replay on the owner.
  18. */
  19. class PawnOwnerState
  20. {
  21. protected void PawnOwnerState() {}
  22. protected void ~PawnOwnerState() {}
  23. proto native void SetMoveId(int value);
  24. proto native int GetMoveId();
  25. proto native int GetSimulationTimestamp();
  26. proto native void SetPhysicsTimeStamp(int value);
  27. proto native int GetPhysicsTimeStamp();
  28. proto native void SetWaterTime(float value);
  29. proto native float GetWaterTime();
  30. protected event void Write(PawnStateWriter ctx)
  31. {
  32. }
  33. protected event void Read(PawnStateReader ctx)
  34. {
  35. }
  36. #ifdef DIAG_DEVELOPER
  37. protected event void DiagWrite(PawnStateWriter ctx)
  38. {
  39. }
  40. protected event void DiagRead(PawnStateReader ctx)
  41. {
  42. }
  43. #endif
  44. /**
  45. \brief Estimate the additional maximum size for network buffer transmission on top of the native size
  46. */
  47. protected event int EstimateMaximumSize()
  48. {
  49. return 0;
  50. }
  51. #ifdef DIAG_DEVELOPER
  52. /**
  53. \brief Event used only for diagnostic purposes
  54. @param transform, World space transform of the move
  55. */
  56. event void GetTransform(inout vector transform[4])
  57. {
  58. }
  59. #endif
  60. };
  61. /**
  62. PawnMove
  63. Contains information about the move to be used on the owner and sent to the authority.
  64. Authority will consume and play the same move. When desync is detected the moves that haven't been acknlowedged by the authority yet are replayed.
  65. */
  66. class PawnMove
  67. {
  68. protected void PawnMove() {}
  69. protected void ~PawnMove() {}
  70. proto native void SetMoveId(int value);
  71. proto native int GetMoveId();
  72. //! Not avaliable on server
  73. proto native void SetSimulationTimestamp(int value);
  74. proto native int GetSimulationTimestamp();
  75. //! Not avaliable on server
  76. proto native void SetTimeSlice(float value);
  77. proto native float GetTimeSlice();
  78. /**
  79. * @ctx Stream to write the data to
  80. * @prev Previous move for delta encoding
  81. */
  82. protected event void Write(PawnMoveWriter ctx, PawnMove prev)
  83. {
  84. }
  85. /**
  86. * @ctx Stream to read the data from
  87. * @prev Previous move for delta encoding
  88. */
  89. protected event void Read(PawnMoveReader ctx, PawnMove prev)
  90. {
  91. }
  92. /**
  93. \brief Estimate the additional maximum size for network buffer transmission on top of the native size
  94. */
  95. protected event int EstimateMaximumSize()
  96. {
  97. return 0;
  98. }
  99. #ifdef DIAG_DEVELOPER
  100. /**
  101. \brief Event used only for diagnostic purposes
  102. @param transform, World space transform of the move
  103. */
  104. event void GetTransform(inout vector transform[4])
  105. {
  106. }
  107. #endif
  108. };
  109. /**
  110. NetworkMoveStrategy
  111. The strategy with which the Pawn is reconciled on the owner
  112. */
  113. enum NetworkMoveStrategy
  114. {
  115. //! Has no support for NetworkMovement
  116. NONE,
  117. //! Places the move as the last input inside 'NetworkInput' and simulates with the input message
  118. LATEST,
  119. //! Sends over a fixed buffer of moves and re-simulates the physics steps on correction as a static scene
  120. PHYSICS,
  121. };
  122. /**
  123. NetworkRewindType
  124. The rewind type with which the Pawn is reconciled on the owner
  125. */
  126. enum NetworkRewindType
  127. {
  128. //! Rewind type hasn't been set yet by C++, meaning RewindState can process through any of the paths below. Internally treated as REPLAY
  129. //! NOTE: DROP may stil be set but it could cause destruction of state which would require additional states to be sent
  130. NOT_SET,
  131. //! Rewind can't be perfomed, we treat this rewind as just acknlowledgement
  132. DROP,
  133. //! Rewind requests moves to be replayed
  134. REPLAY,
  135. //! Rewind requests special behaviour where the moves just become additive, akin to how BacktrackingComponent works
  136. ADDITIVE
  137. };
  138. /**
  139. NetworkCompareResult
  140. Result of comparison for how correction or approval should proceed
  141. */
  142. enum NetworkCompareResult
  143. {
  144. //! Approve client simulation as within bounds
  145. APPROVE,
  146. //! Requests correction if necessary
  147. CORRECT,
  148. //! Ignores RTT delay and forces a correction - should be used minimally as it is expensive on network traffic
  149. FORCE_CORRECT
  150. };
  151. /**
  152. Pawn
  153. An entity that can be possessed. Only one entity can be possesed by one PlayerIdentity at a single time.
  154. Meaning when something other than a Player is possessed, the Player can no longer be reconciled and desync can occur.
  155. */
  156. class Pawn : EntityAI
  157. {
  158. //! Returns true if the Pawn is being simulated by the owner
  159. proto native bool IsOwner();
  160. //! Returns true if the Pawn is being simulated on an authority
  161. proto native bool IsAuthority();
  162. //! Returns true if the Pawn is being simulated on an authority and has no owner
  163. proto native bool IsAuthorityOwner();
  164. //! Returns true if the Pawn is being processed on a proxy
  165. proto native bool IsProxy();
  166. //! Force a correction to the owner as a server only event was called (called on authority only)
  167. proto native void ForceCorrection();
  168. //! Returns the identity that is in possession of the Pawn
  169. proto native PlayerIdentity GetOwnerIdentity();
  170. //! Returns the strategy with which the pawn is reconciled on the Owner
  171. proto native NetworkMoveStrategy GetNetworkMoveStrategy();
  172. /**
  173. \brief Event that fires when the Pawn is possessed
  174. */
  175. protected event void OnPossess()
  176. {
  177. }
  178. /**
  179. \brief Event that fires when the Pawn is no longer possessed
  180. */
  181. protected event void OnUnPossess()
  182. {
  183. }
  184. /**
  185. \brief Initializes the NetworkMovement with the type of PawnOwnerState at construction
  186. \return The typename of the PawnOwnerState
  187. */
  188. protected event typename GetOwnerStateType()
  189. {
  190. return PawnOwnerState;
  191. }
  192. /**
  193. \brief Initializes the NetworkMovement with the type of PawnMove at construction and for storing
  194. \return The typename of the PawnMove
  195. */
  196. protected event typename GetMoveType()
  197. {
  198. return PawnMove;
  199. }
  200. /**
  201. \brief Fill the move of the pawn with the inputs for the pawn to be replayed
  202. @param move, Move which the variables are being written to
  203. */
  204. protected event void ObtainMove(/*inout*/ PawnMove pMove)
  205. {
  206. }
  207. /**
  208. \brief Compares the incoming move from the owner on the authority
  209. @param move The incoming move
  210. @return result, also performed natively, both results are compared and higher one is used
  211. */
  212. protected event NetworkCompareResult CompareMove(PawnMove pMove)
  213. {
  214. return NetworkCompareResult.APPROVE;
  215. }
  216. /**
  217. \brief Apply the moves inputs on the server
  218. @param move The next move to be consumed and applied on the authority
  219. */
  220. protected event void ConsumeMove(PawnMove pMove)
  221. {
  222. }
  223. /**
  224. \brief Replay the move on the owner client
  225. @param move The incoming move
  226. @return false if further moves should not be replayed
  227. */
  228. protected event bool ReplayMove(PawnMove pMove)
  229. {
  230. return true;
  231. }
  232. /**
  233. \brief Simulate the replayed move on the owner client
  234. @param move The incoming move
  235. */
  236. protected event void SimulateMove(PawnMove pMove)
  237. {
  238. }
  239. /**
  240. \brief Fill the state of the pawn with it to be approriately rewound
  241. @param state The state which the variables are being written to
  242. */
  243. protected event void ObtainState(/*inout*/ PawnOwnerState pState)
  244. {
  245. }
  246. /**
  247. \brief Replays the state, setting the internal variables of the entity back to the state. Decides the method for rewinding
  248. @param state The state recieved from the server to which the client is rewinding back to
  249. @param move The move generated at this state, which is modified if NetworkRewindType.ADDITIVE is used and then becomes the delta move, (see ReplayAdditiveMove)
  250. @param rewindType The method which replay will execute in
  251. */
  252. protected event void RewindState(PawnOwnerState pState, /*inout*/ PawnMove pMove, inout NetworkRewindType pRewindType)
  253. {
  254. }
  255. /**
  256. \brief Iterator function over moves ahead of the authority. Intended for the incoming move to be combined with the delta move.
  257. @param pMove The incoming move which is being updated
  258. @param pDeltaMove The delta of the move between the server and client state at the time of RewindState
  259. */
  260. protected event void ReplayAdditiveMove(/*inout*/ PawnMove pMove, /*const*/ PawnMove pDeltaMove)
  261. {
  262. }
  263. /**
  264. \brief Applying the delta move to the pawns internal state. Instead of Rewind, this is effectively (T1 = T + D)
  265. @param pDeltaMove The delta of the move between the server and client state at the time of RewindState
  266. */
  267. protected event void ApplyAdditiveMove(PawnMove pDeltaMove)
  268. {
  269. }
  270. };
  271. #endif