simBase_ScriptBinding.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 _SIMBASE_H_
  23. #include "simBase.h"
  24. #endif
  25. //-----------------------------------------------------------------------------
  26. ConsoleFunctionGroupBegin ( SimFunctions, "Functions relating to Sim.");
  27. /*! @addtogroup SimFunctions
  28. simulation engine functions
  29. @{
  30. */
  31. //-----------------------------------------------------------------------------
  32. /*! Use the getSimTime function to get the time, in ticks, that has elapsed since the engine started executing.
  33. @return Returns the time in ticks since the engine was started.
  34. @sa getRealTime
  35. \par From Engine
  36. @copydoc Sim::getCurrentTime
  37. */
  38. ConsoleFunctionWithDocs( getSimTime, ConsoleInt, 1, 1, () )
  39. {
  40. return Sim::getCurrentTime();
  41. }
  42. //-----------------------------------------------------------------------------
  43. /*! Use the nameToID function to convert an object name into an object ID.
  44. This function is a helper for those odd cases where a string will not covert properly, but generally this can be replaced with a statement like: (\"someName\")
  45. @param objectName A string containing the name of an object.
  46. @return Returns a positive non-zero value if the name corresponds to an object, or a -1 if it does not.
  47. \par From Engine
  48. @copydoc Sim::findObject
  49. */
  50. ConsoleFunctionWithDocs( nameToID, ConsoleInt, 2, 2, ( string objectName ) )
  51. {
  52. SimObject *obj = Sim::findObject(argv[1]);
  53. if(obj)
  54. return obj->getId();
  55. else
  56. return -1;
  57. }
  58. /*! Use the isObject function to check if the name or ID specified in handle is a valid object.
  59. @param handle A name or ID of a possible object.
  60. @return Returns true if handle refers to a valid object, false otherwise
  61. */
  62. ConsoleFunctionWithDocs(isObject, ConsoleBool, 2, 2, ( handle ) )
  63. {
  64. if (!dStrcmp(argv[1], "0") || !dStrcmp(argv[1], ""))
  65. return false;
  66. else
  67. return (Sim::findObject(argv[1]) != NULL);
  68. }
  69. /*! cancel a previously scheduled event
  70. @param eventID The numeric ID of a previously scheduled event.
  71. @return No return value.
  72. @sa getEventTimeLeft, getScheduleDuration, getTimeSinceStart, isEventPending, schedule, obj.schedule
  73. @par From Engine
  74. @copydoc Sim::cancelEvent
  75. */
  76. ConsoleFunctionWithDocs(cancel,ConsoleVoid,2,2, ( eventID ) )
  77. {
  78. Sim::cancelEvent(dAtoi(argv[1]));
  79. }
  80. /*! Use the isEventPending function to see if the event associated with eventID is still pending.
  81. When an event passes, the eventID is removed from the event queue, becoming invalid, so there is no discnerable difference between a completed event and a bad event ID.
  82. @param eventID The numeric ID of a previously scheduled event.
  83. @return Returns true if this event is still outstanding and false if it has passed or eventID is invalid.
  84. @sa cancel, getEventTimeLeft, getScheduleDuration, getTimeSinceStart, schedule, obj.schedule
  85. @par From Engine
  86. @copydoc Sim::isEventPending
  87. */
  88. ConsoleFunctionWithDocs(isEventPending, ConsoleBool, 2, 2, ( eventID ) )
  89. {
  90. return Sim::isEventPending(dAtoi(argv[1]));
  91. }
  92. /*!
  93. Use the getEventTimeLeft function to determine how much time remains until the event specified by eventID occurs.
  94. @param eventID The numeric ID of a previously scheduled event.
  95. @return Returns a non-zero integer value equal to the milliseconds until the event specified by eventID will occur. However, if eventID is invalid, or the event has passed, this function will return zero.
  96. @sa cancel, getScheduleDuration, getTimeSinceStart, isEventPending, schedule, obj.schedule
  97. @par From Engine
  98. @copydoc Sim::getEventTimeLeft
  99. */
  100. ConsoleFunctionWithDocs(getEventTimeLeft, ConsoleInt, 2, 2, ( eventID ) )
  101. {
  102. return Sim::getEventTimeLeft(dAtoi(argv[1]));
  103. }
  104. /*!
  105. Use the getScheduleDuration function to determine how long the event associated with eventID was scheduled for.
  106. @param eventID The numeric ID of a previously scheduled event.
  107. @return Returns a non-zero integer value equal to the milliseconds used in the schedule call that created this event. However, if eventID is invalid, this function will return zero.
  108. @sa cancel, getEventTimeLeft, getTimeSinceStart, isEventPending, schedule, obj.schedule
  109. @par From Engine
  110. @copydoc Sim::getScheduleDuration
  111. */
  112. ConsoleFunctionWithDocs(getScheduleDuration, ConsoleInt, 2, 2, ( eventID ) )
  113. {
  114. S32 ret = Sim::getScheduleDuration(dAtoi(argv[1]));
  115. return ret;
  116. }
  117. /*!
  118. Use the getTimeSinceStart function to determine how much time has passed since the event specified by eventID was scheduled.
  119. @param eventID The numeric ID of a previously scheduled event.
  120. @return Returns a non-zero integer value equal to the milliseconds that have passed since this event was scheduled. However, if eventID is invalid, or the event has passed, this function will return zero.
  121. @sa cancel, getEventTimeLeft, getScheduleDuration, isEventPending, schedule, obj.schedule
  122. @par From Engine
  123. @copydoc Sim::getTimeSinceStart
  124. */
  125. ConsoleFunctionWithDocs(getTimeSinceStart, ConsoleInt, 2, 2, ( eventID ) )
  126. {
  127. S32 ret = Sim::getTimeSinceStart(dAtoi(argv[1]));
  128. return ret;
  129. }
  130. /*!
  131. Use the schedule function to schedule functionName to be executed with optional arguments at time t (specified in milliseconds) in the future. This function may be associated with an object ID or not. If it is associated with an object ID and the object is deleted prior to this event occurring, the event is automatically canceled.
  132. @param t The time to wait (in milliseconds) before executing functionName.
  133. @param objID An optional ID to associate this event with.
  134. @param functionName An unadorned (flat) function name.
  135. @param arg0, .. , argN - Any number of optional arguments to be passed to functionName.
  136. @return Returns a non-zero integer representing the event ID for the scheduled event.
  137. @sa cancel, getEventTimeLeft, getScheduleDuration, getTimeSinceStart, isEventPending, obj.schedule
  138. @par From Engine
  139. @copydoc Sim::postEvent
  140. */
  141. ConsoleFunctionWithDocs(schedule, ConsoleInt, 4, 0, ( t , objID || 0 , functionName, arg0, ... , argN ) )
  142. {
  143. U32 timeDelta = U32(dAtof(argv[1]));
  144. SimObject *refObject = Sim::findObject(argv[2]);
  145. if(!refObject)
  146. {
  147. if(argv[2][0] != '0')
  148. return 0;
  149. refObject = Sim::getRootGroup();
  150. }
  151. SimConsoleEvent *evt = new SimConsoleEvent(argc - 3, argv + 3, false);
  152. S32 ret = Sim::postEvent(refObject, evt, Sim::getCurrentTime() + timeDelta);
  153. // #ifdef DEBUG
  154. // Con::printf("ref %s schedule(%s) = %d", argv[2], argv[3], ret);
  155. // Con::executef(1, "backtrace");
  156. // #endif
  157. return ret;
  158. }
  159. //-----------------------------------------------------------------------------
  160. /*! @} */
  161. ConsoleFunctionGroupEnd( SimFunctions );