simBase.cc 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. #include "platform/platform.h"
  23. #include "sim/simBase.h"
  24. #include "string/stringTable.h"
  25. #include "console/console.h"
  26. #include "io/fileStream.h"
  27. #include "input/actionMap.h"
  28. #include "io/resource/resourceManager.h"
  29. #include "io/fileObject.h"
  30. #include "console/consoleInternal.h"
  31. #include "debug/profiler.h"
  32. #include "console/consoleTypeValidators.h"
  33. #include "memory/frameAllocator.h"
  34. namespace Sim
  35. {
  36. // Don't forget to InstantiateNamed* in simManager.cc - DMM
  37. ImplementNamedSet(ActiveActionMapSet)
  38. ImplementNamedSet(GhostAlwaysSet)
  39. ImplementNamedSet(BehaviorSet)
  40. ImplementNamedSet(AchievementSet);
  41. ImplementNamedGroup(ActionMapGroup)
  42. ImplementNamedGroup(ClientGroup)
  43. ImplementNamedGroup(GuiGroup)
  44. ImplementNamedGroup(GuiDataGroup)
  45. ImplementNamedGroup(TCPGroup)
  46. //groups created on the client
  47. ImplementNamedGroup(ClientConnectionGroup)
  48. ImplementNamedGroup(ChunkFileGroup)
  49. }
  50. //-----------------------------------------------------------------------------
  51. ConsoleFunction( getSimTime, S32, 1, 1, "() Use the getSimTime function to get the time, in ticks, that has elapsed since the engine started executing.\n"
  52. "@return Returns the time in ticks since the engine was started.\n"
  53. "@sa getRealTime")
  54. {
  55. return Sim::getCurrentTime();
  56. }
  57. //-----------------------------------------------------------------------------
  58. ConsoleFunctionGroupBegin ( SimFunctions, "Functions relating to Sim.");
  59. ConsoleFunction(nameToID, S32, 2, 2, "( objectName ) Use the nameToID function to convert an object name into an object ID.\n"
  60. "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\")\n"
  61. "@param objectName A string containing the name of an object.\n"
  62. "@return Returns a positive non-zero value if the name corresponds to an object, or a -1 if it does not.")
  63. {
  64. SimObject *obj = Sim::findObject(argv[1]);
  65. if(obj)
  66. return obj->getId();
  67. else
  68. return -1;
  69. }
  70. ConsoleFunction(isObject, bool, 2, 2, "( handle ) Use the isObject function to check if the name or ID specified in handle is a valid object.\n"
  71. "@param handle A name or ID of a possible object.\n"
  72. "@return Returns true if handle refers to a valid object, false otherwise")
  73. {
  74. if (!dStrcmp(argv[1], "0") || !dStrcmp(argv[1], ""))
  75. return false;
  76. else
  77. return (Sim::findObject(argv[1]) != NULL);
  78. }
  79. ConsoleFunction(cancel,void,2,2,"( eventID ) Use the cancel function to cancel a previously scheduled event as specified by eventID.\n"
  80. "@param eventID The numeric ID of a previously scheduled event.\n"
  81. "@return No return value.\n"
  82. "@sa getEventTimeLeft, getScheduleDuration, getTimeSinceStart, isEventPending, schedule, obj.schedule")
  83. {
  84. Sim::cancelEvent(dAtoi(argv[1]));
  85. }
  86. ConsoleFunction(isEventPending, bool, 2, 2, "( eventID ) Use the isEventPending function to see if the event associated with eventID is still pending.\n"
  87. "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.\n"
  88. "@param eventID The numeric ID of a previously scheduled event.\n"
  89. "@return Returns true if this event is still outstanding and false if it has passed or eventID is invalid.\n"
  90. "@sa cancel, getEventTimeLeft, getScheduleDuration, getTimeSinceStart, schedule, obj.schedule")
  91. {
  92. return Sim::isEventPending(dAtoi(argv[1]));
  93. }
  94. ConsoleFunction(getEventTimeLeft, S32, 2, 2, "( eventID ) Use the getEventTimeLeft function to determine how much time remains until the event specified by eventID occurs.\n"
  95. "@param eventID The numeric ID of a previously scheduled event.\n"
  96. "@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.\n"
  97. "@sa cancel, getScheduleDuration, getTimeSinceStart, isEventPending, schedule, obj.schedule")
  98. {
  99. return Sim::getEventTimeLeft(dAtoi(argv[1]));
  100. }
  101. ConsoleFunction(getScheduleDuration, S32, 2, 2, " ( eventID ) Use the getScheduleDuration function to determine how long the event associated with eventID was scheduled for.\n"
  102. "@param eventID The numeric ID of a previously scheduled event.\n"
  103. "@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.\n"
  104. "@sa cancel, getEventTimeLeft, getTimeSinceStart, isEventPending, schedule, obj.schedule")
  105. {
  106. S32 ret = Sim::getScheduleDuration(dAtoi(argv[1]));
  107. return ret;
  108. }
  109. ConsoleFunction(getTimeSinceStart, S32, 2, 2, "( eventID ) Use the getTimeSinceStart function to determine how much time has passed since the event specified by eventID was scheduled.\n"
  110. "@param eventID The numeric ID of a previously scheduled event.\n"
  111. "@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.\n"
  112. "@sa cancel, getEventTimeLeft, getScheduleDuration, isEventPending, schedule, obj.schedule")
  113. {
  114. S32 ret = Sim::getTimeSinceStart(dAtoi(argv[1]));
  115. return ret;
  116. }
  117. ConsoleFunction(schedule, S32, 4, 0, "( t , objID || 0 , functionName, arg0, ... , argN ) 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.\n"
  118. "@param t The time to wait (in milliseconds) before executing functionName.\n"
  119. "@param objID An optional ID to associate this event with.\n"
  120. "@param functionName An unadorned (flat) function name.\n"
  121. "@param arg0, .. , argN - Any number of optional arguments to be passed to functionName.\n"
  122. "@return Returns a non-zero integer representing the event ID for the scheduled event.\n"
  123. "@sa cancel, getEventTimeLeft, getScheduleDuration, getTimeSinceStart, isEventPending, obj.schedule")
  124. {
  125. U32 timeDelta = U32(dAtof(argv[1]));
  126. SimObject *refObject = Sim::findObject(argv[2]);
  127. if(!refObject)
  128. {
  129. if(argv[2][0] != '0')
  130. return 0;
  131. refObject = Sim::getRootGroup();
  132. }
  133. SimConsoleEvent *evt = new SimConsoleEvent(argc - 3, argv + 3, false);
  134. S32 ret = Sim::postEvent(refObject, evt, Sim::getCurrentTime() + timeDelta);
  135. // #ifdef DEBUG
  136. // Con::printf("ref %s schedule(%s) = %d", argv[2], argv[3], ret);
  137. // Con::executef(1, "backtrace");
  138. // #endif
  139. return ret;
  140. }
  141. ConsoleFunctionGroupEnd( SimFunctions );