sim.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. template<class T> inline bool findObject(SimObjectId iD,T*&t)
  120. {
  121. t = dynamic_cast<T*>(findObject(iD));
  122. return t != NULL;
  123. }
  124. template<class T> inline bool findObject(const char *objectName,T*&t)
  125. {
  126. t = dynamic_cast<T*>(findObject(objectName));
  127. return t != NULL;
  128. }
  129. SimObject *spawnObject(String spawnClass,
  130. String spawnDataBlock = String::EmptyString,
  131. String spawnName = String::EmptyString,
  132. String spawnProperties = String::EmptyString,
  133. String spawnScript = String::EmptyString);
  134. void advanceToTime(SimTime time);
  135. void advanceTime(SimTime delta);
  136. SimTime getCurrentTime();
  137. SimTime getTargetTime();
  138. /// a target time of 0 on an event means current event
  139. U32 postEvent(SimObject*, SimEvent*, SimTime targetTime);
  140. inline U32 postEvent(SimObjectId iD,SimEvent*evt, SimTime targetTime)
  141. {
  142. return postEvent(findObject(iD), evt, targetTime);
  143. }
  144. inline U32 postEvent(const char *objectName,SimEvent*evt, SimTime targetTime)
  145. {
  146. return postEvent(findObject(objectName), evt, targetTime);
  147. }
  148. inline U32 postCurrentEvent(SimObject*obj, SimEvent*evt)
  149. {
  150. return postEvent(obj,evt,getCurrentTime());
  151. }
  152. inline U32 postCurrentEvent(SimObjectId obj,SimEvent*evt)
  153. {
  154. return postEvent(obj,evt,getCurrentTime());
  155. }
  156. inline U32 postCurrentEvent(const char *obj,SimEvent*evt)
  157. {
  158. return postEvent(obj,evt,getCurrentTime());
  159. }
  160. void cancelEvent(U32 eventId);
  161. void cancelPendingEvents(SimObject *obj);
  162. bool isEventPending(U32 eventId);
  163. U32 getEventTimeLeft(U32 eventId);
  164. U32 getTimeSinceStart(U32 eventId);
  165. U32 getScheduleDuration(U32 eventId);
  166. /// Appends numbers to inName until an unused SimObject name is created
  167. String getUniqueName( const char *inName );
  168. /// Appends numbers to inName until an internal name not taken in the inSet is found.
  169. String getUniqueInternalName( const char *inName, SimSet *inSet, bool searchChildren );
  170. /// Return true if the given name string makes for a valid object name.
  171. /// Empty strings and NULL are also treated as valid names (anonymous objects).
  172. bool isValidObjectName( const char* name );
  173. bool saveObject(SimObject *obj, Stream *stream);
  174. SimObject *loadObjectStream(Stream *stream);
  175. bool saveObject(SimObject *obj, const char *filename);
  176. SimObject *loadObjectStream(const char *filename);
  177. }
  178. #endif // _SIM_H_