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