pawn.c 8.4 KB

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