sim.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 _SIM_H_
  23. #define _SIM_H_
  24. #ifndef _TORQUE_TYPES_H_
  25. #include "platform/types.h"
  26. #endif
  27. #ifndef _TORQUE_STRING_H_
  28. #include "core/util/str.h"
  29. #endif
  30. #ifndef _MODULE_H_
  31. #include "core/module.h"
  32. #endif
  33. #ifndef _CONSOLE_H_
  34. #include "console/console.h"
  35. #endif
  36. // Forward Refs
  37. class SimSet;
  38. class SimGroup;
  39. class SimDataBlockGroup;
  40. class SimObject;
  41. class SimEvent;
  42. class Stream;
  43. // Sim Types
  44. typedef U32 SimTime;
  45. typedef U32 SimObjectId;
  46. /// Definition of some basic Sim system constants.
  47. ///
  48. /// These constants define the range of ids assigned to datablocks
  49. /// (DataBlockObjectIdFirst - DataBlockObjectIdLast), and the number
  50. /// of bits used to store datablock IDs.
  51. ///
  52. /// Normal Sim objects are given the range of IDs starting at
  53. /// DynamicObjectIdFirst and going to infinity. Sim objects use
  54. /// a SimObjectId to represent their ID; this is currently a U32.
  55. ///
  56. /// The RootGroupId is assigned to gRootGroup, in which most SimObjects
  57. /// are addded as child members. See simManager.cc for details, particularly
  58. /// Sim::initRoot() and following.
  59. enum SimObjectsConstants : U32
  60. {
  61. DataBlockObjectIdFirst = 3,
  62. DataBlockObjectIdBitSize = 14,
  63. DataBlockObjectIdLast = DataBlockObjectIdFirst + (1 << DataBlockObjectIdBitSize) - 1,
  64. MessageObjectIdFirst = DataBlockObjectIdLast + 1,
  65. MessageObjectIdBitSize = 6,
  66. MessageObjectIdLast = MessageObjectIdFirst + (1 << MessageObjectIdBitSize) - 1,
  67. DynamicObjectIdFirst = MessageObjectIdLast + 1,
  68. InvalidEventId = 0,
  69. RootGroupId = 0xFFFFFFFF,
  70. };
  71. //---------------------------------------------------------------------------
  72. /// @defgroup simbase_helpermacros Helper Macros
  73. ///
  74. /// These are used for named sets and groups in the manager.
  75. /// @{
  76. #define DeclareNamedSet(set) extern SimSet *g##set;inline SimSet *get##set() { return g##set; }
  77. #define DeclareNamedGroup(set) extern SimGroup *g##set;inline SimGroup *get##set() { return g##set; }
  78. #define ImplementNamedSet(set) SimSet *g##set;
  79. #define ImplementNamedGroup(set) SimGroup *g##set;
  80. /// @}
  81. //---------------------------------------------------------------------------
  82. namespace Sim
  83. {
  84. DeclareNamedSet(ActiveActionMapSet)
  85. DeclareNamedSet(GhostAlwaysSet)
  86. DeclareNamedSet(WayPointSet)
  87. DeclareNamedSet(fxReplicatorSet)
  88. DeclareNamedSet(fxFoliageSet)
  89. DeclareNamedSet(BehaviorSet)
  90. DeclareNamedSet(MaterialSet)
  91. DeclareNamedSet(SFXSourceSet);
  92. DeclareNamedSet(SFXDescriptionSet);
  93. DeclareNamedSet(SFXTrackSet);
  94. DeclareNamedSet(SFXEnvironmentSet);
  95. DeclareNamedSet(SFXStateSet);
  96. DeclareNamedSet(SFXAmbienceSet);
  97. DeclareNamedSet(TerrainMaterialSet);
  98. DeclareNamedSet(DataBlockSet);
  99. DeclareNamedSet(ForestBrushSet);
  100. DeclareNamedSet(ForestItemDataSet);
  101. DeclareNamedGroup(ActionMapGroup)
  102. DeclareNamedGroup(ClientGroup)
  103. DeclareNamedGroup(GuiGroup)
  104. DeclareNamedGroup(GuiDataGroup)
  105. DeclareNamedGroup(TCPGroup)
  106. DeclareNamedGroup(ClientConnectionGroup)
  107. DeclareNamedGroup(SFXParameterGroup);
  108. DeclareNamedSet(sgMissionLightingFilterSet);
  109. void init();
  110. void shutdown();
  111. bool isShuttingDown();
  112. SimDataBlockGroup *getDataBlockGroup();
  113. SimGroup* getRootGroup();
  114. SimObject* findObject(SimObjectId);
  115. SimObject* findObject(const ConsoleValue&);
  116. SimObject* findObject(ConsoleValue*);
  117. SimObject* findObject(const char* name);
  118. SimObject* findObject(const char* fileName, S32 declarationLine);
  119. inline SimObject* findObject(const String& name)
  120. {
  121. return findObject(name.c_str());
  122. }
  123. template<class T> inline bool findObject(SimObjectId iD,T*&t)
  124. {
  125. t = dynamic_cast<T*>(findObject(iD));
  126. return t != NULL;
  127. }
  128. template<class T> inline bool findObject(const char *objectName,T*&t)
  129. {
  130. t = dynamic_cast<T*>(findObject(objectName));
  131. return t != NULL;
  132. }
  133. template<class T> inline bool findObject(const String& objectName, T*& t)
  134. {
  135. t = dynamic_cast<T*>(findObject(objectName));
  136. return t != NULL;
  137. }
  138. SimObject *spawnObject(String spawnClass,
  139. String spawnDataBlock = String::EmptyString,
  140. String spawnName = String::EmptyString,
  141. String spawnProperties = String::EmptyString,
  142. String spawnScript = String::EmptyString);
  143. void advanceToTime(SimTime time);
  144. void advanceTime(SimTime delta);
  145. SimTime getCurrentTime();
  146. SimTime getTargetTime();
  147. /// a target time of 0 on an event means current event
  148. U32 postEvent(SimObject*, SimEvent*, SimTime targetTime);
  149. inline U32 postEvent(SimObjectId iD,SimEvent*evt, SimTime targetTime)
  150. {
  151. return postEvent(findObject(iD), evt, targetTime);
  152. }
  153. inline U32 postEvent(const char *objectName,SimEvent*evt, SimTime targetTime)
  154. {
  155. return postEvent(findObject(objectName), evt, targetTime);
  156. }
  157. inline U32 postCurrentEvent(SimObject*obj, SimEvent*evt)
  158. {
  159. return postEvent(obj,evt,getCurrentTime());
  160. }
  161. inline U32 postCurrentEvent(SimObjectId obj,SimEvent*evt)
  162. {
  163. return postEvent(obj,evt,getCurrentTime());
  164. }
  165. inline U32 postCurrentEvent(const char *obj,SimEvent*evt)
  166. {
  167. return postEvent(obj,evt,getCurrentTime());
  168. }
  169. void cancelEvent(U32 eventId);
  170. void cancelPendingEvents(SimObject *obj);
  171. bool isEventPending(U32 eventId);
  172. U32 getEventTimeLeft(U32 eventId);
  173. U32 getTimeSinceStart(U32 eventId);
  174. U32 getScheduleDuration(U32 eventId);
  175. /// Appends numbers to inName until an unused SimObject name is created
  176. String getUniqueName( const char *inName );
  177. /// Appends numbers to inName until an internal name not taken in the inSet is found.
  178. String getUniqueInternalName( const char *inName, SimSet *inSet, bool searchChildren );
  179. /// Return true if the given name string makes for a valid object name.
  180. /// Empty strings and NULL are also treated as valid names (anonymous objects).
  181. bool isValidObjectName( const char* name );
  182. bool saveObject(SimObject *obj, Stream *stream);
  183. SimObject *loadObjectStream(Stream *stream);
  184. bool saveObject(SimObject *obj, const char *filename);
  185. SimObject *loadObjectStream(const char *filename);
  186. }
  187. #endif // _SIM_H_