processList.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _PROCESSLIST_H_
  23. #define _PROCESSLIST_H_
  24. #ifndef _SIM_H_
  25. #include "console/sim.h"
  26. #endif
  27. #ifndef _TSIGNAL_H_
  28. #include "core/util/tSignal.h"
  29. #endif
  30. //----------------------------------------------------------------------------
  31. #define TickMs 32
  32. #define TickSec (F32(TickMs) / 1000.0f)
  33. //----------------------------------------------------------------------------
  34. class GameConnection;
  35. struct Move;
  36. class ProcessObject
  37. {
  38. public:
  39. ProcessObject();
  40. virtual ~ProcessObject() { removeFromProcessList(); }
  41. /// Removes this object from the tick-processing list
  42. void removeFromProcessList() { plUnlink(); }
  43. /// Set the status of tick processing.
  44. ///
  45. /// Set true to receive processTick, advanceTime, and interpolateTick calls.
  46. ///
  47. /// @see processTick
  48. /// @param t If true, tick processing is enabled.
  49. virtual void setProcessTick( bool t ) { mProcessTick = t; }
  50. /// Returns true if this object processes ticks.
  51. bool isTicking() const { return mProcessTick; }
  52. /// This is really implemented in GameBase and is only here to avoid
  53. /// casts within ProcessList.
  54. virtual GameConnection* getControllingClient() { return NULL; }
  55. /// This is really implemented in GameBase and is only here to avoid
  56. /// casts within ProcessList.
  57. virtual U32 getPacketDataChecksum( GameConnection *conn ) { return -1; }
  58. /// Force this object to process after some other object.
  59. ///
  60. /// For example, a player mounted to a vehicle would want to process after
  61. /// the vehicle to prevent a visible 'lagging' from occurring when the
  62. /// vehicle moves. So the player would be set to processAfter(theVehicle).
  63. ///
  64. /// @param obj Object to process after
  65. virtual void processAfter( ProcessObject *obj ) {}
  66. /// Clears the effects of a call to processAfter()
  67. virtual void clearProcessAfter() {}
  68. /// Returns the object that this processes after.
  69. ///
  70. /// @see processAfter
  71. virtual ProcessObject* getAfterObject() const { return NULL; }
  72. /// Processes a move event and updates object state once every 32 milliseconds.
  73. ///
  74. /// This takes place both on the client and server, every 32 milliseconds (1 tick).
  75. ///
  76. /// @see ProcessList
  77. /// @param move Move event corresponding to this tick, or NULL.
  78. virtual void processTick( const Move *move ) {}
  79. /// Interpolates between tick events. This takes place on the CLIENT ONLY.
  80. ///
  81. /// @param delta Time since last call to interpolate
  82. virtual void interpolateTick( F32 delta ) {}
  83. /// Advances simulation time for animations. This is called every frame.
  84. ///
  85. /// @param dt Time since last advance call
  86. virtual void advanceTime( F32 dt ) {}
  87. /// Allow object to modify the Move before it is ticked or sent to the server.
  88. /// This is only called for the control object on the client-side.
  89. virtual void preprocessMove( Move *move ) {}
  90. //protected:
  91. struct Link
  92. {
  93. ProcessObject *next;
  94. ProcessObject *prev;
  95. };
  96. // Processing interface
  97. void plUnlink();
  98. void plLinkAfter(ProcessObject*);
  99. void plLinkBefore(ProcessObject*);
  100. void plJoin(ProcessObject*);
  101. U32 mProcessTag; // Tag used during sort
  102. U32 mOrderGUID; // UID for keeping order synced (e.g., across network or runs of sim)
  103. Link mProcessLink; // Ordered process queue
  104. bool mProcessTick;
  105. bool mIsGameBase;
  106. };
  107. //----------------------------------------------------------------------------
  108. typedef Signal<void()> PreTickSignal;
  109. typedef Signal<void(SimTime)> PostTickSignal;
  110. class GameBase;
  111. /// List of ProcessObjects.
  112. class ProcessList
  113. {
  114. public:
  115. ProcessList();
  116. virtual ~ProcessList() {}
  117. void markDirty() { mDirty = true; }
  118. bool isDirty() { return mDirty; }
  119. SimTime getLastTime() { return mLastTime; }
  120. F32 getLastDelta() { return mLastDelta; }
  121. F32 getLastInterpDelta() { return mLastDelta / F32(TickMs); }
  122. U32 getTotalTicks() { return mTotalTicks; }
  123. void dumpToConsole();
  124. PreTickSignal& preTickSignal() { return mPreTick; }
  125. PostTickSignal& postTickSignal() { return mPostTick; }
  126. virtual void addObject( ProcessObject *obj );
  127. /// Returns true if a tick was processed.
  128. virtual bool advanceTime( SimTime timeDelta );
  129. protected:
  130. void orderList();
  131. GameBase* getGameBase( ProcessObject *obj );
  132. virtual void advanceObjects();
  133. virtual void onAdvanceObjects() { advanceObjects(); }
  134. virtual void onPreTickObject( ProcessObject* ) {}
  135. virtual void onTickObject( ProcessObject* ) {}
  136. protected:
  137. ProcessObject mHead;
  138. U32 mCurrentTag;
  139. bool mDirty;
  140. U32 mTotalTicks;
  141. SimTime mLastTick;
  142. SimTime mLastTime;
  143. F32 mLastDelta;
  144. PreTickSignal mPreTick;
  145. PostTickSignal mPostTick;
  146. };
  147. #endif // _PROCESSLIST_H_