processList.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  23. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  24. // Copyright (C) 2015 Faust Logic, Inc.
  25. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  26. #ifndef _PROCESSLIST_H_
  27. #define _PROCESSLIST_H_
  28. #ifndef _SIM_H_
  29. #include "console/sim.h"
  30. #endif
  31. #ifndef _TSIGNAL_H_
  32. #include "core/util/tSignal.h"
  33. #endif
  34. //----------------------------------------------------------------------------
  35. #define TickMs 32
  36. #define TickSec (F32(TickMs) / 1000.0f)
  37. //----------------------------------------------------------------------------
  38. class GameConnection;
  39. struct Move;
  40. class ProcessObject
  41. {
  42. public:
  43. ProcessObject();
  44. virtual ~ProcessObject() { removeFromProcessList(); }
  45. /// Removes this object from the tick-processing list
  46. void removeFromProcessList() { plUnlink(); }
  47. /// Set the status of tick processing.
  48. ///
  49. /// Set true to receive processTick, advanceTime, and interpolateTick calls.
  50. ///
  51. /// @see processTick
  52. /// @param t If true, tick processing is enabled.
  53. virtual void setProcessTick( bool t ) { mProcessTick = t; }
  54. /// Returns true if this object processes ticks.
  55. bool isTicking() const { return mProcessTick; }
  56. /// This is really implemented in GameBase and is only here to avoid
  57. /// casts within ProcessList.
  58. virtual GameConnection* getControllingClient() { return NULL; }
  59. /// This is really implemented in GameBase and is only here to avoid
  60. /// casts within ProcessList.
  61. virtual U32 getPacketDataChecksum( GameConnection *conn ) { return -1; }
  62. /// Force this object to process after some other object.
  63. ///
  64. /// For example, a player mounted to a vehicle would want to process after
  65. /// the vehicle to prevent a visible 'lagging' from occurring when the
  66. /// vehicle moves. So the player would be set to processAfter(theVehicle).
  67. ///
  68. /// @param obj Object to process after
  69. virtual void processAfter( ProcessObject *obj ) {}
  70. /// Clears the effects of a call to processAfter()
  71. virtual void clearProcessAfter() {}
  72. /// Returns the object that this processes after.
  73. ///
  74. /// @see processAfter
  75. virtual ProcessObject* getAfterObject() const { return NULL; }
  76. /// Processes a move event and updates object state once every 32 milliseconds.
  77. ///
  78. /// This takes place both on the client and server, every 32 milliseconds (1 tick).
  79. ///
  80. /// @see ProcessList
  81. /// @param move Move event corresponding to this tick, or NULL.
  82. virtual void processTick( const Move *move ) {}
  83. /// Interpolates between tick events. This takes place on the CLIENT ONLY.
  84. ///
  85. /// @param delta Time since last call to interpolate
  86. virtual void interpolateTick( F32 delta ) {}
  87. /// Advances simulation time for animations. This is called every frame.
  88. ///
  89. /// @param dt Time since last advance call
  90. virtual void advanceTime( F32 dt ) {}
  91. /// Allow object to modify the Move before it is ticked or sent to the server.
  92. /// This is only called for the control object on the client-side.
  93. virtual void preprocessMove( Move *move ) {}
  94. //protected:
  95. struct Link
  96. {
  97. ProcessObject *next;
  98. ProcessObject *prev;
  99. };
  100. // Processing interface
  101. void plUnlink();
  102. void plLinkAfter(ProcessObject*);
  103. void plLinkBefore(ProcessObject*);
  104. void plJoin(ProcessObject*);
  105. U32 mProcessTag; // Tag used during sort
  106. U32 mOrderGUID; // UID for keeping order synced (e.g., across network or runs of sim)
  107. Link mProcessLink; // Ordered process queue
  108. bool mProcessTick;
  109. bool mIsGameBase;
  110. };
  111. //----------------------------------------------------------------------------
  112. typedef Signal<void()> PreTickSignal;
  113. typedef Signal<void(SimTime)> PostTickSignal;
  114. class GameBase;
  115. /// List of ProcessObjects.
  116. class ProcessList
  117. {
  118. public:
  119. ProcessList();
  120. virtual ~ProcessList() {}
  121. void markDirty() { mDirty = true; }
  122. bool isDirty() { return mDirty; }
  123. SimTime getLastTime() { return mLastTime; }
  124. F32 getLastDelta() { return mLastDelta; }
  125. F32 getLastInterpDelta() { return mLastDelta / F32(TickMs); }
  126. U32 getTotalTicks() { return mTotalTicks; }
  127. void dumpToConsole();
  128. PreTickSignal& preTickSignal() { return mPreTick; }
  129. PostTickSignal& postTickSignal() { return mPostTick; }
  130. virtual void addObject( ProcessObject *obj );
  131. /// Returns true if a tick was processed.
  132. virtual bool advanceTime( SimTime timeDelta );
  133. protected:
  134. void orderList();
  135. GameBase* getGameBase( ProcessObject *obj );
  136. virtual void advanceObjects();
  137. virtual void onAdvanceObjects() { advanceObjects(); }
  138. virtual void onPreTickObject( ProcessObject* ) {}
  139. virtual void onTickObject( ProcessObject* ) {}
  140. protected:
  141. ProcessObject mHead;
  142. U32 mCurrentTag;
  143. bool mDirty;
  144. U32 mTotalTicks;
  145. SimTime mLastTick;
  146. SimTime mLastTime;
  147. F32 mLastDelta;
  148. PreTickSignal mPreTick;
  149. PostTickSignal mPostTick;
  150. // JTF: still needed?
  151. public:
  152. ProcessObject* findNearestToEnd(Vector<ProcessObject*>& objs) const;
  153. };
  154. #endif // _PROCESSLIST_H_