pawn.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. };
  120. /**
  121. NetworkRewindType
  122. The rewind type with which the Pawn is reconciled on the owner
  123. */
  124. enum NetworkRewindType
  125. {
  126. //! Rewind type hasn't been set yet by C++, meaning RewindState can process through any of the paths below. Internally treated as REPLAY
  127. //! NOTE: DROP may stil be set but it could cause destruction of state which would require additional states to be sent
  128. NOT_SET,
  129. //! Rewind can't be perfomed, we treat this rewind as just acknlowledgement
  130. DROP,
  131. //! Rewind requests moves to be replayed
  132. REPLAY,
  133. //! Rewind requests special behaviour where the moves just become additive, akin to how BacktrackingComponent works
  134. ADDITIVE
  135. };
  136. /**
  137. NetworkCompareResult
  138. Result of comparison for how correction or approval should proceed
  139. */
  140. enum NetworkCompareResult
  141. {
  142. //! Approve client simulation as within bounds
  143. APPROVE,
  144. //! Requests correction if necessary
  145. CORRECT,
  146. //! Ignores RTT delay and forces a correction - should be used minimally as it is expensive on network traffic
  147. FORCE_CORRECT
  148. };
  149. /**
  150. Pawn
  151. An entity that can be possessed. Only one entity can be possesed by one PlayerIdentity at a single time.
  152. Meaning when something other than a Player is possessed, the Player can no longer be reconciled and desync can occur.
  153. */
  154. class Pawn : EntityAI
  155. {
  156. //! Returns true if the Pawn is being simulated by the owner
  157. proto native bool IsOwner();
  158. //! Returns true if the Pawn is being simulated on an authority
  159. proto native bool IsAuthority();
  160. //! Returns true if the Pawn is being simulated on an authority and has no owner
  161. proto native bool IsAuthorityOwner();
  162. //! Returns true if the Pawn is being processed on a proxy
  163. proto native bool IsProxy();
  164. //! Force a correction to the owner as a server only event was called (called on authority only)
  165. proto native void ForceCorrection();
  166. //! Returns the identity that is in possession of the Pawn
  167. proto native PlayerIdentity GetOwnerIdentity();
  168. //! Returns the strategy with which the pawn is reconciled on the Owner
  169. proto native NetworkMoveStrategy GetNetworkMoveStrategy();
  170. /**
  171. \brief Event that fires when the Pawn is possessed
  172. */
  173. protected event void OnPossess()
  174. {
  175. }
  176. /**
  177. \brief Event that fires when the Pawn is no longer possessed
  178. */
  179. protected event void OnUnPossess()
  180. {
  181. }
  182. /**
  183. \brief Initializes the NetworkMovement with the type of PawnOwnerState at construction
  184. \return The typename of the PawnOwnerState
  185. */
  186. protected event typename GetOwnerStateType()
  187. {
  188. return PawnOwnerState;
  189. }
  190. /**
  191. \brief Initializes the NetworkMovement with the type of PawnMove at construction and for storing
  192. \return The typename of the PawnMove
  193. */
  194. protected event typename GetMoveType()
  195. {
  196. return PawnMove;
  197. }
  198. /**
  199. \brief Fill the move of the pawn with the inputs for the pawn to be replayed
  200. @param move, Move which the variables are being written to
  201. */
  202. protected event void ObtainMove(/*inout*/ PawnMove pMove)
  203. {
  204. }
  205. /**
  206. \brief Compares the incoming move from the owner on the authority
  207. @param move The incoming move
  208. @return result, also performed natively, both results are compared and higher one is used
  209. */
  210. protected event NetworkCompareResult CompareMove(PawnMove pMove)
  211. {
  212. return NetworkCompareResult.APPROVE;
  213. }
  214. /**
  215. \brief Apply the moves inputs on the server
  216. @param move The next move to be consumed and applied on the authority
  217. */
  218. protected event void ConsumeMove(PawnMove pMove)
  219. {
  220. }
  221. /**
  222. \brief Replay the move on the owner client
  223. @param move The incoming move
  224. @return false if further moves should not be replayed
  225. */
  226. protected event bool ReplayMove(PawnMove pMove)
  227. {
  228. return true;
  229. }
  230. /**
  231. \brief Simulate the replayed move on the owner client
  232. @param move The incoming move
  233. */
  234. protected event void SimulateMove(PawnMove pMove)
  235. {
  236. }
  237. /**
  238. \brief Fill the state of the pawn with it to be approriately rewound
  239. @param state The state which the variables are being written to
  240. */
  241. protected event void ObtainState(/*inout*/ PawnOwnerState pState)
  242. {
  243. }
  244. /**
  245. \brief Replays the state, setting the internal variables of the entity back to the state. Decides the method for rewinding
  246. @param state The state recieved from the server to which the client is rewinding back to
  247. @param move The move generated at this state, which is modified if NetworkRewindType.ADDITIVE is used and then becomes the delta move, (see ReplayAdditiveMove)
  248. @param rewindType The method which replay will execute in
  249. */
  250. protected event void RewindState(PawnOwnerState pState, /*inout*/ PawnMove pMove, inout NetworkRewindType pRewindType)
  251. {
  252. }
  253. /**
  254. \brief Iterator function over moves ahead of the authority. Intended for the incoming move to be combined with the delta move.
  255. @param pMove The incoming move which is being updated
  256. @param pDeltaMove The delta of the move between the server and client state at the time of RewindState
  257. */
  258. protected event void ReplayAdditiveMove(/*inout*/ PawnMove pMove, /*const*/ PawnMove pDeltaMove)
  259. {
  260. }
  261. /**
  262. \brief Applying the delta move to the pawns internal state. Instead of Rewind, this is effectively (T1 = T + D)
  263. @param pDeltaMove The delta of the move between the server and client state at the time of RewindState
  264. */
  265. protected event void ApplyAdditiveMove(PawnMove pDeltaMove)
  266. {
  267. }
  268. };
  269. #endif