aiConnection.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 "T3D/aiConnection.h"
  23. #include "console/engineAPI.h"
  24. IMPLEMENT_CONOBJECT( AIConnection );
  25. ConsoleDocClass( AIConnection,
  26. "@brief Special client connection driven by an AI, rather than a human.\n\n"
  27. "Unlike other net connections, AIConnection is intended to run unmanned. Rather than "
  28. "gathering input from a human using a device, move events, triggers, and look events "
  29. "are driven through functions like AIConnection::setMove.\n\n"
  30. "In addition to having its own set of functions for managing client move events, "
  31. "a member variable inherited by GameConnection is toggle: mAIControlled. This is useful "
  32. "for a server to determine if a connection is AI driven via the function GameConnection::isAIControlled\n\n"
  33. "AIConnection is an alternative to manually creating an AI driven game object. When you "
  34. "want the server to manage AI, you will create a specific one from script using a "
  35. "class like AIPlayer. If you do not want the server managing the AI and wish to simulate "
  36. "a complete client connection, you will use AIConnection\n\n."
  37. "To get more specific, if you want a strong alternative to AIPlayer (and wish to make use "
  38. "of the AIConnection structure), consider AIClient. AIClient inherits from AIConnection, "
  39. "contains quite a bit of functionality you will find in AIPlayer, and has its own Player "
  40. "object.\n\n"
  41. "@tsexample\n"
  42. "// Create a new AI client connection\n"
  43. "%botConnection = aiConnect(\"MasterBlaster\" @ %i, -1, 0.5, false, \"SDF\", 1.0);\n\n"
  44. "// In another area of the code, you can locate this and any other AIConnections\n"
  45. "// using the isAIControlled function\n"
  46. "for(%i = 0; %i < ClientGroup.getCount(); %i++)\n"
  47. "{\n"
  48. " %client = ClientGroup.getObject(%i);\n"
  49. " if(%client.isAIControlled())\n"
  50. " {\n"
  51. " // React to this AI controlled client\n"
  52. " }\n"
  53. "}\n"
  54. "@endtsexample\n\n"
  55. "@note This is a legacy class, which you are discouraged from using as it will "
  56. "most likely be deprecated in a future version. For now it has been left in for "
  57. "backwards compatibility with TGE and the old RTS Kit. Use GameConnection "
  58. "and AIPlayer instead.\n\n"
  59. "@see GameConnection, NetConnection, AIClient\n\n"
  60. "@ingroup AI\n"
  61. "@ingroup Networking\n"
  62. );
  63. //-----------------------------------------------------------------------------
  64. AIConnection::AIConnection() {
  65. mAIControlled = true;
  66. mMove = NullMove;
  67. }
  68. //-----------------------------------------------------------------------------
  69. void AIConnection::clearMoves( U32 )
  70. {
  71. // Clear the pending move list. This connection generates moves
  72. // on the fly, so there are never any pending moves.
  73. }
  74. void AIConnection::setMove(Move* m)
  75. {
  76. mMove = *m;
  77. }
  78. const Move& AIConnection::getMove()
  79. {
  80. return mMove;
  81. }
  82. /// Retrive the pending moves
  83. /**
  84. * The GameConnection base class queues moves for delivery to the
  85. * controll object. This function is normally used to retrieve the
  86. * queued moves recieved from the client. The AI connection does not
  87. * have a connected client and simply generates moves on-the-fly
  88. * base on it's current state.
  89. */
  90. U32 AIConnection::getMoveList( Move **lngMove, U32 *numMoves )
  91. {
  92. *numMoves = 1;
  93. *lngMove = &mMove;
  94. return *numMoves;
  95. }
  96. //-----------------------------------------------------------------------------
  97. // Console functions & methods
  98. //-----------------------------------------------------------------------------
  99. //-----------------------------------------------------------------------------
  100. static inline F32 moveClamp(F32 v)
  101. {
  102. // Support function to convert/clamp the input into a move rotation
  103. // which only allows 0 -> M_2PI.
  104. F32 a = mClampF(v, -M_2PI_F, M_2PI_F);
  105. return (a < 0) ? a + M_2PI_F : a;
  106. }
  107. //-----------------------------------------------------------------------------
  108. /// Construct and connect an AI connection object
  109. DefineEngineStringlyVariadicFunction(aiConnect, S32 , 2, 20, "(...)"
  110. "@brief Creates a new AIConnection, and passes arguments to its onConnect script callback.\n\n"
  111. "@returns The newly created AIConnection\n"
  112. "@see GameConnection for parameter information\n"
  113. "@ingroup AI")
  114. {
  115. // Create the connection
  116. AIConnection *aiConnection = new AIConnection();
  117. aiConnection->registerObject();
  118. // Add the connection to the client group
  119. SimGroup *g = Sim::getClientGroup();
  120. g->addObject( aiConnection );
  121. // Prep the arguments for the console exec...
  122. // Make sure and leav args[1] empty.
  123. const char* args[21];
  124. args[0] = "onConnect";
  125. args[1] = NULL; // Filled in later
  126. for (S32 i = 1; i < argc; i++)
  127. args[i + 1] = argv[i];
  128. // Execute the connect console function, this is the same
  129. // onConnect function invoked for normal client connections
  130. Con::execute(aiConnection, argc + 1, args);
  131. return aiConnection->getId();
  132. }
  133. //-----------------------------------------------------------------------------
  134. DefineEngineMethod(AIConnection, setMove, void, (const char * field, F32 value), ,"(string field, float value)"
  135. "Set a field on the current move.\n\n"
  136. "@param field One of {'x','y','z','yaw','pitch','roll'}\n"
  137. "@param value Value to set field to.")
  138. {
  139. Move move = object->getMove();
  140. // Ok, a little slow for now, but this is just an example..
  141. if (!dStricmp(field,"x"))
  142. move.x = mClampF(value,-1,1);
  143. else
  144. if (!dStricmp(field,"y"))
  145. move.y = mClampF(value,-1,1);
  146. else
  147. if (!dStricmp(field,"z"))
  148. move.z = mClampF(value,-1,1);
  149. else
  150. if (!dStricmp(field,"yaw"))
  151. move.yaw = moveClamp(value);
  152. else
  153. if (!dStricmp(field,"pitch"))
  154. move.pitch = moveClamp(value);
  155. else
  156. if (!dStricmp(field,"roll"))
  157. move.roll = moveClamp(value);
  158. //
  159. object->setMove(&move);
  160. }
  161. DefineEngineMethod(AIConnection,getMove,F32, (const char * field), ,"(string field)"
  162. "Get the given field of a move.\n\n"
  163. "@param field One of {'x','y','z','yaw','pitch','roll'}\n"
  164. "@returns The requested field on the current move.")
  165. {
  166. const Move& move = object->getMove();
  167. if (!dStricmp(field,"x"))
  168. return move.x;
  169. if (!dStricmp(field,"y"))
  170. return move.y;
  171. if (!dStricmp(field,"z"))
  172. return move.z;
  173. if (!dStricmp(field,"yaw"))
  174. return move.yaw;
  175. if (!dStricmp(field,"pitch"))
  176. return move.pitch;
  177. if (!dStricmp(field,"roll"))
  178. return move.roll;
  179. return 0;
  180. }
  181. DefineEngineMethod(AIConnection,setFreeLook,void,(bool isFreeLook), ,"(bool isFreeLook)"
  182. "Enable/disable freelook on the current move.")
  183. {
  184. Move move = object->getMove();
  185. move.freeLook = isFreeLook;
  186. object->setMove(&move);
  187. }
  188. DefineEngineMethod(AIConnection, getFreeLook, bool, (), ,"getFreeLook()"
  189. "Is freelook on for the current move?")
  190. {
  191. return object->getMove().freeLook;
  192. }
  193. //-----------------------------------------------------------------------------
  194. DefineEngineMethod(AIConnection,setTrigger,void, (S32 idx, bool set), ,"(int trigger, bool set)"
  195. "Set a trigger.")
  196. {
  197. if (idx >= 0 && idx < MaxTriggerKeys)
  198. {
  199. Move move = object->getMove();
  200. move.trigger[idx] = set;
  201. object->setMove(&move);
  202. }
  203. }
  204. DefineEngineMethod(AIConnection,getTrigger,bool, (S32 idx), ,"(int trigger)"
  205. "Is the given trigger set?")
  206. {
  207. if (idx >= 0 && idx < MaxTriggerKeys)
  208. return object->getMove().trigger[idx];
  209. return false;
  210. }
  211. //-----------------------------------------------------------------------------
  212. DefineEngineMethod(AIConnection,getAddress,const char*,(), ,"")
  213. {
  214. // Override the netConnection method to return to indicate
  215. // this is an ai connection.
  216. return "ai:local";
  217. }