sim.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. #include "console/console.h"
  23. #include "console/engineAPI.h"
  24. #include "console/sim.h"
  25. #include "console/simEvents.h"
  26. #include "console/simObject.h"
  27. #include "console/simSet.h"
  28. #include "core/module.h"
  29. MODULE_BEGIN( Sim )
  30. // Note: tho the SceneGraphs are created after the Manager, delete them after, rather
  31. // than before to make sure that all the objects are removed from the graph.
  32. MODULE_INIT_AFTER( GFX )
  33. MODULE_SHUTDOWN_BEFORE( GFX )
  34. MODULE_INIT
  35. {
  36. Sim::init();
  37. }
  38. MODULE_SHUTDOWN
  39. {
  40. Sim::shutdown();
  41. }
  42. MODULE_END;
  43. namespace Sim
  44. {
  45. // Don't forget to InstantiateNamed* in simManager.cc - DMM
  46. ImplementNamedSet(ActiveActionMapSet)
  47. ImplementNamedSet(GhostAlwaysSet)
  48. ImplementNamedSet(WayPointSet)
  49. ImplementNamedSet(fxReplicatorSet)
  50. ImplementNamedSet(fxFoliageSet)
  51. ImplementNamedSet(BehaviorSet)
  52. ImplementNamedSet(MaterialSet)
  53. ImplementNamedSet(SFXSourceSet)
  54. ImplementNamedSet(SFXDescriptionSet)
  55. ImplementNamedSet(SFXTrackSet)
  56. ImplementNamedSet(SFXEnvironmentSet)
  57. ImplementNamedSet(SFXStateSet)
  58. ImplementNamedSet(SFXAmbienceSet)
  59. ImplementNamedSet(TerrainMaterialSet)
  60. ImplementNamedSet(DataBlockSet);
  61. ImplementNamedSet(ForestBrushSet);
  62. ImplementNamedSet(ForestItemDataSet);
  63. ImplementNamedGroup(ActionMapGroup)
  64. ImplementNamedGroup(ClientGroup)
  65. ImplementNamedGroup(GuiGroup)
  66. ImplementNamedGroup(GuiDataGroup)
  67. ImplementNamedGroup(TCPGroup)
  68. ImplementNamedGroup(SFXParameterGroup);
  69. //groups created on the client
  70. ImplementNamedGroup(ClientConnectionGroup)
  71. ImplementNamedSet(sgMissionLightingFilterSet)
  72. }
  73. //-----------------------------------------------------------------------------
  74. // Console Functions
  75. //-----------------------------------------------------------------------------
  76. ConsoleFunctionGroupBegin ( SimFunctions, "Functions relating to Sim.");
  77. DefineEngineFunction( nameToID, S32, (const char * objectName), ,"nameToID(object)")
  78. {
  79. SimObject *obj = Sim::findObject(objectName);
  80. if(obj)
  81. return obj->getId();
  82. else
  83. return -1;
  84. }
  85. DefineEngineFunction( isObject, bool, (const char * objectName), ,"isObject(object)")
  86. {
  87. if (!String::compare(objectName, "0") || !String::compare(objectName, ""))
  88. return false;
  89. else
  90. return (Sim::findObject(objectName) != NULL);
  91. }
  92. ConsoleDocFragment _spawnObject1(
  93. "@brief Global function used for spawning any type of object.\n\n"
  94. "Note: This is separate from SpawnSphere::spawnObject(). This function is not called off any "
  95. "other class and uses different parameters than the SpawnSphere's function. In the source, "
  96. "SpawnSphere::spawnObject() actually calls this function and passes its properties "
  97. "(spawnClass, spawnDatablock, etc).\n\n"
  98. "@param class Mandatory field specifying the object class, such as Player or TSStatic.\n\n"
  99. "@param datablock Field specifying the object's datablock, optional for objects such as TSStatic, mandatory for game objects like Player.\n\n"
  100. "@param name Optional field specifying a name for this instance of the object.\n\n"
  101. "@param properties Optional set of parameters applied to the spawn object during creation.\n\n"
  102. "@param script Optional command(s) to execute when spawning an object.\n\n"
  103. "@tsexample\n"
  104. "// Set the parameters for the spawn function\n"
  105. "%objectClass = \"Player\";\n"
  106. "%objectDatablock = \"DefaultPlayerData\";\n"
  107. "%objectName = \"PlayerName\";\n"
  108. "%additionalProperties = \"health = \\\"0\\\";\"; // Note the escape sequence \\ in front of quotes\n"
  109. "%spawnScript = \"echo(\\\"Player Spawned\\\");\" // Note the escape sequence \\ in front of quotes\n"
  110. "// Spawn with the engine's Sim::spawnObject() function\n"
  111. "%player = spawnObject(%objectClass, %objectDatablock, %objectName, %additionalProperties, %spawnScript);\n"
  112. "@endtsexample\n\n"
  113. "@ingroup Game",
  114. NULL,
  115. "bool spawnObject(class [, dataBlock, name, properties, script]);"
  116. );
  117. DefineEngineFunction( spawnObject, S32, ( const char * spawnClass
  118. , const char * spawnDataBlock
  119. , const char * spawnName
  120. , const char * spawnProperties
  121. , const char * spawnScript
  122. ),("","","","") ,"spawnObject(class [, dataBlock, name, properties, script])"
  123. "@hide")
  124. {
  125. SimObject* spawnObject = Sim::spawnObject(spawnClass, spawnDataBlock, spawnName, spawnProperties, spawnScript);
  126. if (spawnObject)
  127. return spawnObject->getId();
  128. else
  129. return -1;
  130. }
  131. DefineEngineFunction( cancel, void, (S32 eventId), ,"cancel(eventId)")
  132. {
  133. Sim::cancelEvent(eventId);
  134. }
  135. DefineEngineFunction( cancelAll, void, (const char * objectId), ,"cancelAll(objectId): cancel pending events on the specified object. Events will be automatically cancelled if object is deleted.")
  136. {
  137. Sim::cancelPendingEvents(Sim::findObject(objectId));
  138. }
  139. DefineEngineFunction( isEventPending, bool, (S32 scheduleId), ,"isEventPending(%scheduleId);")
  140. {
  141. return Sim::isEventPending(scheduleId);
  142. }
  143. DefineEngineFunction( getEventTimeLeft, S32, (S32 scheduleId), ,"getEventTimeLeft(scheduleId) Get the time left in ms until this event will trigger.")
  144. {
  145. return Sim::getEventTimeLeft(scheduleId);
  146. }
  147. DefineEngineFunction( getScheduleDuration, S32, (S32 scheduleId), ,"getScheduleDuration(%scheduleId);" )
  148. {
  149. S32 ret = Sim::getScheduleDuration(scheduleId);
  150. return ret;
  151. }
  152. DefineEngineFunction( getTimeSinceStart, S32, (S32 scheduleId), ,"getTimeSinceStart(%scheduleId);" )
  153. {
  154. S32 ret = Sim::getTimeSinceStart(scheduleId);
  155. return ret;
  156. }
  157. DefineEngineStringlyVariadicFunction(schedule, S32, 4, 0, "schedule(time, refobject|0, command, <arg1...argN>)")
  158. {
  159. U32 timeDelta = U32(dAtof(argv[1]));
  160. SimObject *refObject = Sim::findObject(argv[2]);
  161. if(!refObject)
  162. {
  163. if(argv[2][0] != '0')
  164. return 0;
  165. refObject = Sim::getRootGroup();
  166. }
  167. SimConsoleEvent *evt = new SimConsoleEvent(argc - 3, argv + 3, false);
  168. S32 ret = Sim::postEvent(refObject, evt, Sim::getCurrentTime() + timeDelta);
  169. // #ifdef DEBUG
  170. // Con::printf("ref %s schedule(%s) = %d", argv[2], argv[3], ret);
  171. // Con::executef( "backtrace");
  172. // #endif
  173. return ret;
  174. }
  175. DefineEngineFunction( getUniqueName, const char*, (const char * baseName), ,
  176. "( String baseName )\n"
  177. "@brief Returns a unique unused SimObject name based on a given base name.\n\n"
  178. "@baseName Name to conver to a unique string if another instance exists\n"
  179. "@note Currently only used by editors\n"
  180. "@ingroup Editors\n"
  181. "@internal")
  182. {
  183. String outName = Sim::getUniqueName( baseName );
  184. if ( outName.isEmpty() )
  185. return NULL;
  186. char *buffer = Con::getReturnBuffer( outName.size() );
  187. dStrcpy( buffer, outName, outName.size() );
  188. return buffer;
  189. }
  190. DefineEngineFunction( getUniqueInternalName, const char*, (const char * baseName, const char * setString, bool searchChildren), ,
  191. "( String baseName, SimSet set, bool searchChildren )\n"
  192. "@brief Returns a unique unused internal name within the SimSet/Group based on a given base name.\n\n"
  193. "@note Currently only used by editors\n"
  194. "@ingroup Editors\n"
  195. "@internal")
  196. {
  197. SimSet *set;
  198. if ( !Sim::findObject( setString, set ) )
  199. {
  200. Con::errorf( "getUniqueInternalName() - invalid parameter for SimSet." );
  201. return NULL;
  202. }
  203. String outName = Sim::getUniqueInternalName( baseName, set, searchChildren );
  204. if ( outName.isEmpty() )
  205. return NULL;
  206. char *buffer = Con::getReturnBuffer( outName.size() );
  207. dStrcpy( buffer, outName, outName.size() );
  208. return buffer;
  209. }
  210. DefineEngineFunction( isValidObjectName, bool, (const char * name), , "( string name )"
  211. "@brief Return true if the given name makes for a valid object name.\n\n"
  212. "@param name Name of object\n"
  213. "@return True if name is allowed, false if denied (usually because it starts with a number, _, or invalid character"
  214. "@ingroup Console")
  215. {
  216. return Sim::isValidObjectName( name );
  217. }
  218. ConsoleFunctionGroupEnd( SimFunctions );