simBase.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 _SIMBASE_H_
  23. #define _SIMBASE_H_
  24. #ifndef _VECTOR_H_
  25. #include "collection/vector.h"
  26. #endif
  27. #ifndef _FIND_ITERATOR_H_
  28. #include "collection/findIterator.h"
  29. #endif
  30. #ifndef _BITSET_H_
  31. #include "collection/bitSet.h"
  32. #endif
  33. #ifndef _CONSOLEOBJECT_H_
  34. #include "console/consoleObject.h"
  35. #endif
  36. #ifndef _SIMDICTIONARY_H_
  37. #include "sim/simDictionary.h"
  38. #endif
  39. #ifndef _PLATFORMMUTEX_H_
  40. #include "platform/threads/mutex.h"
  41. #endif
  42. #ifndef _PLATFORMSEMAPHORE_H_
  43. #include "platform/platformSemaphore.h"
  44. #endif
  45. #ifndef _TAML_CALLBACKS_H_
  46. #include "persistence/taml/tamlCallbacks.h"
  47. #endif
  48. #ifndef _SIM_OBJECT_LIST_H_
  49. #include "sim/SimObjectList.h"
  50. #endif
  51. #ifndef _SIM_OBJECT_H_
  52. #include "sim/simObject.h"
  53. #endif
  54. #ifndef _SIM_EVENT_H_
  55. #include "simEvent.h"
  56. #endif
  57. #ifndef _SIM_CONSOLE_EVENT_H_
  58. #include "simConsoleEvent.h"
  59. #endif
  60. #ifndef _SIM_CONSOLE_THREAD_EXEC_EVENT_H_
  61. #include "simConsoleThreadExecEvent.h"
  62. #endif
  63. #ifndef _SIM_OBJECT_PTR_H_
  64. #include "simObjectPtr.h"
  65. #endif
  66. #ifndef _SIM_DATABLOCK_H_
  67. #include "simDatablock.h"
  68. #endif
  69. #ifndef _SIM_DATABLOCK_GROUP_H_
  70. #include "simDatablockGroup.h"
  71. #endif
  72. //---------------------------------------------------------------------------
  73. /// Definition of some basic Sim system constants.
  74. ///
  75. /// These constants define the range of ids assigned to datablocks
  76. /// (DataBlockObjectIdFirst - DataBlockObjectIdLast), and the number
  77. /// of bits used to store datablock IDs.
  78. ///
  79. /// Normal Sim objects are given the range of IDs starting at
  80. /// DynamicObjectIdFirst and going to infinity. Sim objects use
  81. /// a SimObjectId to represent their ID; this is currently a U32.
  82. ///
  83. /// The RootGroupId is assigned to gRootGroup, in which most SimObjects
  84. /// are addded as child members. See simManager.cc for details, particularly
  85. /// Sim::initRoot() and following.
  86. enum SimObjectsConstants
  87. {
  88. DataBlockObjectIdFirst = 3,
  89. DataBlockObjectIdBitSize = 10,
  90. DataBlockObjectIdLast = DataBlockObjectIdFirst + (1 << DataBlockObjectIdBitSize) - 1,
  91. MessageObjectIdFirst = DataBlockObjectIdLast + 1,
  92. MessageObjectIdBitSize = 6,
  93. MessageObjectIdLast = MessageObjectIdFirst + (1 << MessageObjectIdBitSize) - 1,
  94. DynamicObjectIdFirst = MessageObjectIdLast + 1,
  95. InvalidEventId = 0,
  96. RootGroupId = 0xFFFFFFFF,
  97. };
  98. class SimEvent;
  99. class SimObject;
  100. class SimGroup;
  101. class SimManager;
  102. class Namespace;
  103. class BitStream;
  104. class Stream;
  105. typedef U32 SimTime;
  106. //---------------------------------------------------------------------------
  107. /// @defgroup simbase_helpermacros Helper Macros
  108. ///
  109. /// These are used for named sets and groups in the manager.
  110. /// @{
  111. #define DeclareNamedSet(set) extern SimSet *g##set;inline SimSet *get##set() { return g##set; }
  112. #define DeclareNamedGroup(set) extern SimGroup *g##set;inline SimGroup *get##set() { return g##set; }
  113. #define ImplementNamedSet(set) SimSet *g##set;
  114. #define ImplementNamedGroup(set) SimGroup *g##set;
  115. /// @}
  116. //---------------------------------------------------------------------------
  117. namespace Sim
  118. {
  119. DeclareNamedSet(ActiveActionMapSet)
  120. DeclareNamedSet(GhostAlwaysSet)
  121. DeclareNamedSet(BehaviorSet)
  122. DeclareNamedSet(AchievementSet)
  123. DeclareNamedGroup(ActionMapGroup)
  124. DeclareNamedGroup(ClientGroup)
  125. DeclareNamedGroup(GuiGroup)
  126. DeclareNamedGroup(GuiDataGroup)
  127. DeclareNamedGroup(TCPGroup)
  128. DeclareNamedGroup(ClientConnectionGroup)
  129. DeclareNamedGroup(ChunkFileGroup);
  130. void init();
  131. void shutdown();
  132. SimDataBlockGroup *getDataBlockGroup();
  133. SimGroup* getRootGroup();
  134. SimObject* findObject(SimObjectId);
  135. SimObject* findObject(const char* name);
  136. template<class T> inline bool findObject(SimObjectId id,T*&t)
  137. {
  138. t = dynamic_cast<T*>(findObject(id));
  139. return t != NULL;
  140. }
  141. template<class T> inline bool findObject(const char* pObjectName,T*&t)
  142. {
  143. t = dynamic_cast<T*>(findObject(pObjectName));
  144. return t != NULL;
  145. }
  146. template<class T> inline T* findObject(SimObjectId id)
  147. {
  148. return dynamic_cast<T*>(findObject(id));
  149. }
  150. template<class T> inline T* findObject(const char* pObjectName)
  151. {
  152. return dynamic_cast<T*>(findObject(pObjectName));
  153. }
  154. void advanceToTime(SimTime time);
  155. void advanceTime(SimTime delta);
  156. SimTime getCurrentTime();
  157. SimTime getTargetTime();
  158. /// a target time of 0 on an event means current event
  159. U32 postEvent(SimObject*, SimEvent*, U32 targetTime);
  160. inline U32 postEvent(SimObjectId iD,SimEvent*evt, U32 targetTime)
  161. {
  162. return postEvent(findObject(iD), evt, targetTime);
  163. }
  164. inline U32 postEvent(const char *objectName,SimEvent*evt, U32 targetTime)
  165. {
  166. return postEvent(findObject(objectName), evt, targetTime);
  167. }
  168. inline U32 postCurrentEvent(SimObject*obj, SimEvent*evt)
  169. {
  170. return postEvent(obj,evt,getCurrentTime());
  171. }
  172. inline U32 postCurrentEvent(SimObjectId obj,SimEvent*evt)
  173. {
  174. return postEvent(obj,evt,getCurrentTime());
  175. }
  176. inline U32 postCurrentEvent(const char *obj,SimEvent*evt)
  177. {
  178. return postEvent(obj,evt,getCurrentTime());
  179. }
  180. void cancelEvent(U32 eventId);
  181. bool isEventPending(U32 eventId);
  182. U32 getEventTimeLeft(U32 eventId);
  183. U32 getTimeSinceStart(U32 eventId);
  184. U32 getScheduleDuration(U32 eventId);
  185. bool saveObject(SimObject *obj, Stream *stream);
  186. SimObject *loadObjectStream(Stream *stream);
  187. bool saveObject(SimObject *obj, const char *filename);
  188. SimObject *loadObjectStream(const char *filename);
  189. }
  190. //----------------------------------------------------------------------------
  191. #define DECLARE_CONSOLETYPE(T) \
  192. DefineConsoleType( Type##T##Ptr )
  193. #define IMPLEMENT_CONSOLETYPE(T) \
  194. DatablockConsoleType( T##Ptr, Type##T##Ptr, sizeof(T*), T )
  195. #define IMPLEMENT_SETDATATYPE(T) \
  196. ConsoleSetType( Type##T##Ptr ) \
  197. { \
  198. if (argc == 1) { \
  199. *reinterpret_cast<T**>(dptr) = NULL; \
  200. if (argv[0] && argv[0][0] && !Sim::findObject(argv[0],*reinterpret_cast<T**>(dptr))) \
  201. Con::printf("Object '%s' is not a member of the '%s' data block class", argv[0], #T); \
  202. } \
  203. else \
  204. Con::printf("Cannot set multiple args to a single pointer."); \
  205. }
  206. #define IMPLEMENT_GETDATATYPE(T) \
  207. ConsoleGetType( Type##T##Ptr ) \
  208. { \
  209. T** obj = reinterpret_cast<T**>(dptr); \
  210. char* returnBuffer = Con::getReturnBuffer(16); \
  211. dSprintf(returnBuffer, 16, "%s", *obj ? (*obj)->getIdString() : ""); \
  212. return returnBuffer; \
  213. }
  214. //---------------------------------------------------------------------------
  215. #endif