moveManager.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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/gameBase/moveManager.h"
  23. #include "core/stream/bitStream.h"
  24. #include "core/module.h"
  25. #include "console/consoleTypes.h"
  26. #include "core/strings/stringFunctions.h"
  27. #include "math/mConstants.h"
  28. MODULE_BEGIN( MoveManager )
  29. MODULE_INIT
  30. {
  31. MoveManager::init();
  32. }
  33. MODULE_END;
  34. bool MoveManager::mDeviceIsKeyboardMouse = false;
  35. F32 MoveManager::mForwardAction = 0;
  36. F32 MoveManager::mBackwardAction = 0;
  37. F32 MoveManager::mUpAction = 0;
  38. F32 MoveManager::mDownAction = 0;
  39. F32 MoveManager::mLeftAction = 0;
  40. F32 MoveManager::mRightAction = 0;
  41. bool MoveManager::mFreeLook = false;
  42. F32 MoveManager::mPitch = 0;
  43. F32 MoveManager::mYaw = 0;
  44. F32 MoveManager::mRoll = 0;
  45. F32 MoveManager::mPitchUpSpeed = 0;
  46. F32 MoveManager::mPitchDownSpeed = 0;
  47. F32 MoveManager::mYawLeftSpeed = 0;
  48. F32 MoveManager::mYawRightSpeed = 0;
  49. F32 MoveManager::mRollLeftSpeed = 0;
  50. F32 MoveManager::mRollRightSpeed = 0;
  51. F32 MoveManager::mXAxis_L = 0;
  52. F32 MoveManager::mYAxis_L = 0;
  53. F32 MoveManager::mXAxis_R = 0;
  54. F32 MoveManager::mYAxis_R = 0;
  55. U32 MoveManager::mTriggerCount[MaxTriggerKeys] = { 0, };
  56. U32 MoveManager::mPrevTriggerCount[MaxTriggerKeys] = { 0, };
  57. const Move NullMove =
  58. {
  59. /*px=*/16, /*py=*/16, /*pz=*/16,
  60. /*pyaw=*/0, /*ppitch=*/0, /*proll=*/0,
  61. /*x=*/0, /*y=*/0,/*z=*/0,
  62. /*yaw=*/0, /*pitch=*/0, /*roll=*/0,
  63. /*id=*/0,
  64. /*sendCount=*/0,
  65. /*checksum=*/false,
  66. /*deviceIsKeyboardMouse=*/false,
  67. /*freeLook=*/false,
  68. /*triggers=*/{false,false,false,false,false,false}
  69. };
  70. void MoveManager::init()
  71. {
  72. Con::addVariable("mvForwardAction", TypeF32, &mForwardAction,
  73. "Forwards movement speed for the active player.\n"
  74. "@ingroup Game");
  75. Con::addVariable("mvBackwardAction", TypeF32, &mBackwardAction,
  76. "Backwards movement speed for the active player.\n"
  77. "@ingroup Game");
  78. Con::addVariable("mvUpAction", TypeF32, &mUpAction,
  79. "Upwards movement speed for the active player.\n"
  80. "@ingroup Game");
  81. Con::addVariable("mvDownAction", TypeF32, &mDownAction,
  82. "Downwards movement speed for the active player.\n"
  83. "@ingroup Game");
  84. Con::addVariable("mvLeftAction", TypeF32, &mLeftAction,
  85. "Left movement speed for the active player.\n"
  86. "@ingroup Game");
  87. Con::addVariable("mvRightAction", TypeF32, &mRightAction,
  88. "Right movement speed for the active player.\n"
  89. "@ingroup Game");
  90. Con::addVariable("mvFreeLook", TypeBool, &mFreeLook,
  91. "Boolean state for if freelook is active or not.\n"
  92. "@ingroup Game");
  93. Con::addVariable("mvDeviceIsKeyboardMouse", TypeBool, &mDeviceIsKeyboardMouse,
  94. "Boolean state for it the system is using a keyboard and mouse or not.\n"
  95. "@ingroup Game");
  96. Con::addVariable("mvPitch", TypeF32, &mPitch,
  97. "Current pitch value, typically applied through input devices, such as a mouse.\n"
  98. "@ingroup Game");
  99. Con::addVariable("mvYaw", TypeF32, &mYaw,
  100. "Current yaw value, typically applied through input devices, such as a mouse.\n"
  101. "@ingroup Game");
  102. Con::addVariable("mvRoll", TypeF32, &mRoll,
  103. "Current roll value, typically applied through input devices, such as a mouse.\n"
  104. "@ingroup Game");
  105. Con::addVariable("mvPitchUpSpeed", TypeF32, &mPitchUpSpeed,
  106. "Upwards pitch speed.\n"
  107. "@ingroup Game");
  108. Con::addVariable("mvPitchDownSpeed", TypeF32, &mPitchDownSpeed,
  109. "Downwards pitch speed.\n"
  110. "@ingroup Game");
  111. Con::addVariable("mvYawLeftSpeed", TypeF32, &mYawLeftSpeed,
  112. "Left Yaw speed.\n"
  113. "@ingroup Game");
  114. Con::addVariable("mvYawRightSpeed", TypeF32, &mYawRightSpeed,
  115. "Right Yaw speed.\n"
  116. "@ingroup Game");
  117. Con::addVariable("mvRollLeftSpeed", TypeF32, &mRollLeftSpeed,
  118. "Left roll speed.\n"
  119. "@ingroup Game");
  120. Con::addVariable("mvRollRightSpeed", TypeF32, &mRollRightSpeed,
  121. "Right roll speed.\n"
  122. "@ingroup Game");
  123. // Dual-analog
  124. Con::addVariable( "mvXAxis_L", TypeF32, &mXAxis_L,
  125. "Left thumbstick X axis position on a dual-analog gamepad.\n"
  126. "@ingroup Game" );
  127. Con::addVariable( "mvYAxis_L", TypeF32, &mYAxis_L,
  128. "Left thumbstick Y axis position on a dual-analog gamepad.\n"
  129. "@ingroup Game" );
  130. Con::addVariable( "mvXAxis_R", TypeF32, &mXAxis_R,
  131. "Right thumbstick X axis position on a dual-analog gamepad.\n"
  132. "@ingroup Game" );
  133. Con::addVariable( "mvYAxis_R", TypeF32, &mYAxis_R,
  134. "Right thumbstick Y axis position on a dual-analog gamepad.\n"
  135. "@ingroup Game");
  136. for(U32 i = 0; i < MaxTriggerKeys; i++)
  137. {
  138. char varName[256];
  139. dSprintf(varName, sizeof(varName), "mvTriggerCount%d", i);
  140. Con::addVariable(varName, TypeS32, &mTriggerCount[i],
  141. "Used to determine the trigger counts of buttons. Namely used for input actions such as jumping and weapons firing.\n"
  142. "@ingroup Game");
  143. }
  144. }
  145. static inline F32 clampFloatWrap(F32 val)
  146. {
  147. return val - F32(S32(val));
  148. }
  149. static inline S32 clampRangeClamp(F32 val)
  150. {
  151. if(val < -1)
  152. return 0;
  153. if(val > 1)
  154. return 32;
  155. // 0.5 / 16 = 0.03125 ... this forces a round up to
  156. // make the precision near zero equal in the negative
  157. // and positive directions. See...
  158. //
  159. // http://www.garagegames.com/community/forums/viewthread/49714
  160. return (S32)((val + 1.03125) * 16);
  161. }
  162. #define FANG2IANG(x) ((U32)((S16)((F32(0x10000) / M_2PI) * x)) & 0xFFFF)
  163. #define IANG2FANG(x) (F32)((M_2PI / F32(0x10000)) * (F32)((S16)x))
  164. void Move::unclamp()
  165. {
  166. yaw = IANG2FANG(pyaw);
  167. pitch = IANG2FANG(ppitch);
  168. roll = IANG2FANG(proll);
  169. x = (px - 16) / F32(16);
  170. y = (py - 16) / F32(16);
  171. z = (pz - 16) / F32(16);
  172. }
  173. static inline F32 clampAngleClamp( F32 angle )
  174. {
  175. const F32 limit = ( M_PI_F / 180.0f ) * 179.999f;
  176. if ( angle < -limit )
  177. return -limit;
  178. if ( angle > limit )
  179. return limit;
  180. return angle;
  181. }
  182. void Move::clamp()
  183. {
  184. // If yaw/pitch/roll goes equal or greater than -PI/+PI it
  185. // flips the direction of the rotation... we protect against
  186. // that by clamping before the conversion.
  187. yaw = clampAngleClamp( yaw );
  188. pitch = clampAngleClamp( pitch );
  189. roll = clampAngleClamp( roll );
  190. // angles are all 16 bit.
  191. pyaw = FANG2IANG(yaw);
  192. ppitch = FANG2IANG(pitch);
  193. proll = FANG2IANG(roll);
  194. px = clampRangeClamp(x);
  195. py = clampRangeClamp(y);
  196. pz = clampRangeClamp(z);
  197. unclamp();
  198. }
  199. void Move::pack(BitStream *stream, const Move * basemove)
  200. {
  201. bool alwaysWriteAll = basemove!=NULL;
  202. if (!basemove)
  203. basemove = &NullMove;
  204. S32 i;
  205. bool triggerDifferent = false;
  206. for (i=0; i < MaxTriggerKeys; i++)
  207. if (trigger[i] != basemove->trigger[i])
  208. triggerDifferent = true;
  209. bool somethingDifferent = (pyaw!=basemove->pyaw) ||
  210. (ppitch!=basemove->ppitch) ||
  211. (proll!=basemove->proll) ||
  212. (px!=basemove->px) ||
  213. (py!=basemove->py) ||
  214. (pz!=basemove->pz) ||
  215. (deviceIsKeyboardMouse!=basemove->deviceIsKeyboardMouse) ||
  216. (freeLook!=basemove->freeLook) ||
  217. triggerDifferent;
  218. if (alwaysWriteAll || stream->writeFlag(somethingDifferent))
  219. {
  220. if(stream->writeFlag(pyaw != basemove->pyaw))
  221. stream->writeInt(pyaw, 16);
  222. if(stream->writeFlag(ppitch != basemove->ppitch))
  223. stream->writeInt(ppitch, 16);
  224. if(stream->writeFlag(proll != basemove->proll))
  225. stream->writeInt(proll, 16);
  226. if (stream->writeFlag(px != basemove->px))
  227. stream->writeInt(px, 6);
  228. if (stream->writeFlag(py != basemove->py))
  229. stream->writeInt(py, 6);
  230. if (stream->writeFlag(pz != basemove->pz))
  231. stream->writeInt(pz, 6);
  232. stream->writeFlag(freeLook);
  233. stream->writeFlag(deviceIsKeyboardMouse);
  234. if (stream->writeFlag(triggerDifferent))
  235. for(i = 0; i < MaxTriggerKeys; i++)
  236. stream->writeFlag(trigger[i]);
  237. }
  238. }
  239. void Move::unpack(BitStream *stream, const Move * basemove)
  240. {
  241. bool alwaysReadAll = basemove!=NULL;
  242. if (!basemove)
  243. basemove=&NullMove;
  244. if (alwaysReadAll || stream->readFlag())
  245. {
  246. pyaw = stream->readFlag() ? stream->readInt(16) : basemove->pyaw;
  247. ppitch = stream->readFlag() ? stream->readInt(16) : basemove->ppitch;
  248. proll = stream->readFlag() ? stream->readInt(16) : basemove->proll;
  249. px = stream->readFlag() ? stream->readInt(6) : basemove->px;
  250. py = stream->readFlag() ? stream->readInt(6) : basemove->py;
  251. pz = stream->readFlag() ? stream->readInt(6) : basemove->pz;
  252. freeLook = stream->readFlag();
  253. deviceIsKeyboardMouse = stream->readFlag();
  254. bool triggersDiffer = stream->readFlag();
  255. for (S32 i = 0; i< MaxTriggerKeys; i++)
  256. trigger[i] = triggersDiffer ? stream->readFlag() : basemove->trigger[i];
  257. unclamp();
  258. }
  259. else
  260. *this = *basemove;
  261. }