sim.h 7.0 KB

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