sim.h 6.8 KB

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