CONTROLS.CPP 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. #include <stdlib.h>
  2. #include <i86.h>
  3. #include <string.h>
  4. #include "controls.h"
  5. #include "engine.h"
  6. #include "globals.h"
  7. #include "key.h"
  8. #include "debug4g.h"
  9. #include "misc.h"
  10. #include "timer.h"
  11. #include "mouse.h"
  12. #include <memcheck.h>
  13. // virtual key scan codes
  14. #define MOUSE_LBUTTON 0xE0
  15. #define MOUSE_RBUTTON 0xE1
  16. #define MOUSE_MBUTTON 0xE2
  17. #define kAngAccel1 12
  18. #define kAngAccel2 256 //12
  19. // number of ticks at which turning switches to fast turn
  20. #define kTurnThresh 30
  21. //#define KEYS DOOM2
  22. #define DOOM1KEYS
  23. #ifdef DOOM2KEYS
  24. #define kAngSpeed1 92 // Doom II speed
  25. #define kAngSpeed2 184 // Doom II speed
  26. #endif
  27. #ifdef DOOM1KEYS
  28. #define kAngSpeed1 93 // Doom I speed
  29. #define kAngSpeed2 186 // Doom I speed
  30. #endif
  31. INPUT gInput;
  32. // these variables store the keyboard analog input accelerations
  33. static volatile schar iForward, iStrafe;
  34. static volatile uchar iTicks;
  35. static volatile uchar iTurnL = 0, iTurnR = 0;
  36. static volatile int iTurnCount = 0;
  37. static BOOL useMouse = FALSE;
  38. struct {
  39. char *iniKeyName;
  40. BYTE defScanCode;
  41. } controlInfo[] = {
  42. "Forward1", KEY_UP,
  43. "Forward2", KEY_PADUP,
  44. "Backward1", KEY_DOWN,
  45. "Backward2", KEY_PADDOWN,
  46. "Left1", KEY_LEFT,
  47. "Left2", KEY_PADLEFT,
  48. "Right1", KEY_RIGHT,
  49. "Right2", KEY_PADRIGHT,
  50. "Jump1", KEY_X,
  51. "Jump2", 0,
  52. "Crouch1", KEY_C,
  53. "Crouch2", 0,
  54. "StrafeOn1", KEY_LALT,
  55. "StrafeOn2", KEY_RALT,
  56. "StrafeLeft1", KEY_COMMA,
  57. "StrafeLeft2", 0,
  58. "StrafeRight1", KEY_PERIOD,
  59. "StrafeRight2", 0,
  60. "LookUp1", KEY_PAGEUP,
  61. "LookUp2", KEY_PADPAGEUP,
  62. "LookDown1", KEY_PAGEDN,
  63. "LookDown2", KEY_PADPAGEDN,
  64. "AimUp1", KEY_HOME,
  65. "AimUp2", KEY_PADHOME,
  66. "AimDown1", KEY_END,
  67. "AimDown2", KEY_PADEND,
  68. "LookCenter1", KEY_PAD5,
  69. "LookCenter2", 0,
  70. "Action1", KEY_SPACE,
  71. "Action2", 0,
  72. "Fire1", KEY_LCTRL,
  73. "Fire2", KEY_RCTRL,
  74. "AltFire1", KEY_Z,
  75. "AltFire2", 0,
  76. "RunOn1", KEY_LSHIFT,
  77. "RunOn2", KEY_RSHIFT,
  78. "RunToggle1", KEY_SCROLLLOCK,
  79. "RunToggle2", 0,
  80. "Weapon1", KEY_1,
  81. "Weapon2", KEY_2,
  82. "Weapon3", KEY_3,
  83. "Weapon4", KEY_4,
  84. "Weapon5", KEY_5,
  85. "Weapon6", KEY_6,
  86. "Weapon7", KEY_7,
  87. "Weapon8", KEY_8,
  88. "Weapon9", KEY_9,
  89. "Weapon0", KEY_0,
  90. "AutomapToggle", KEY_TAB,
  91. "ItemLeft1", KEY_LBRACE,
  92. "ItemLeft2", 0,
  93. "ItemRight1", KEY_RBRACE,
  94. "ItemRight2", 0,
  95. "ItemDrop1", KEY_BACKSPACE,
  96. "ItemDrop2", 0,
  97. "ItemUse1", KEY_PADENTER,
  98. "ItemUse2", KEY_ENTER,
  99. "Pause", KEY_PAUSE,
  100. };
  101. volatile BYTE *control[ kMaxControls ];
  102. void ctrlStrobeKey( void )
  103. {
  104. BYTE shift = *control[kRunOn1] || *control[kRunOn2];
  105. iTicks++;
  106. iTurnL = iTurnR = 0;
  107. if ( *control[kStrafeOn1] || *control[kStrafeOn2] )
  108. {
  109. if ( *control[kLeft1] || *control[kLeft2] )
  110. iStrafe += 1 + shift;
  111. if ( *control[kRight1] || *control[kRight2] )
  112. iStrafe -= 1 + shift;
  113. }
  114. else
  115. {
  116. if ( *control[kStrafeLeft1] || *control[kStrafeLeft2] )
  117. iStrafe += 1 + shift;
  118. if ( *control[kStrafeRight1] || *control[kStrafeRight2] )
  119. iStrafe -= 1 + shift;
  120. if ( *control[kLeft1] || *control[kLeft2] )
  121. iTurnL = 1;
  122. if ( *control[kRight1] || *control[kRight2] )
  123. iTurnR = 1;
  124. }
  125. if ( *control[kLeft1] || *control[kLeft2] || *control[kRight1] || *control[kRight2] )
  126. iTurnCount++;
  127. else
  128. iTurnCount = 0;
  129. if ( *control[kForward1] || *control[kForward2] )
  130. iForward += 1 + shift;
  131. if ( *control[kBackward1] || *control[kBackward2] )
  132. iForward -= 1 + shift;
  133. }
  134. void ctrlStrobeKey_END( void ) {};
  135. void ctrlInit( void )
  136. {
  137. char inputSettings[128];
  138. strcpy(inputSettings, BloodINI.GetKeyString("Options", "InputSettings", "KeyboardKeys"));
  139. for (int i = 0; i < kMaxControls; i++)
  140. {
  141. int scanId = BloodINI.GetKeyInt(inputSettings, controlInfo[i].iniKeyName, -1);
  142. if ( scanId < 0 )
  143. control[i] = (BYTE *)&keystatus[controlInfo[i].defScanCode];
  144. else
  145. {
  146. if (scanId < 0 || scanId > 255)
  147. scanId = 0;
  148. control[i] = (BYTE *)&keystatus[scanId];
  149. }
  150. }
  151. dpmiLockMemory(FP_OFF(&iForward), sizeof(schar));
  152. dpmiLockMemory(FP_OFF(&iTurnL), sizeof(uchar));
  153. dpmiLockMemory(FP_OFF(&iTurnR), sizeof(uchar));
  154. dpmiLockMemory(FP_OFF(&iTurnCount), sizeof(int));
  155. dpmiLockMemory(FP_OFF(&iStrafe), sizeof(schar));
  156. dpmiLockMemory(FP_OFF(&iTicks), sizeof(uchar));
  157. dpmiLockMemory(FP_OFF(control), sizeof(control));
  158. dpmiLockMemory(FP_OFF(&ctrlStrobeKey), FP_OFF(&ctrlStrobeKey_END) - FP_OFF(&ctrlStrobeKey));
  159. timerRegisterClient(ctrlStrobeKey, kTimerRate);
  160. useMouse = mouseInit() != 0;
  161. Mouse::speedX = BloodINI.GetKeyInt("Mouse", "HSensitivity", 30 );
  162. Mouse::speedY = BloodINI.GetKeyInt("Mouse", "VSensitivity", 10 );
  163. }
  164. void ctrlTerm( void )
  165. {
  166. timerRemoveClient(ctrlStrobeKey);
  167. dpmiUnlockMemory(FP_OFF(&iForward), sizeof(schar));
  168. dpmiUnlockMemory(FP_OFF(&iTurnL), sizeof(uchar));
  169. dpmiUnlockMemory(FP_OFF(&iTurnR), sizeof(uchar));
  170. dpmiUnlockMemory(FP_OFF(&iTurnCount), sizeof(int));
  171. dpmiUnlockMemory(FP_OFF(&iStrafe), sizeof(schar));
  172. dpmiUnlockMemory(FP_OFF(&iTicks), sizeof(uchar));
  173. dpmiUnlockMemory(FP_OFF(control), sizeof(control));
  174. dpmiUnlockMemory(FP_OFF(&ctrlStrobeKey), FP_OFF(&ctrlStrobeKey_END) - FP_OFF(&ctrlStrobeKey));
  175. }
  176. void ctrlGetInput( void )
  177. {
  178. gInput.syncFlags.byte = 0;
  179. gInput.buttonFlags.byte = 0;
  180. gInput.keyFlags.byte = 0;
  181. gInput.newWeapon = 0;
  182. if ( useMouse )
  183. {
  184. Mouse::Read(iTicks);
  185. keystatus[MOUSE_LBUTTON] = (BYTE)(Mouse::buttons & 1);
  186. keystatus[MOUSE_RBUTTON] = (BYTE)(Mouse::buttons & 2);
  187. keystatus[MOUSE_MBUTTON] = (BYTE)(Mouse::buttons & 4);
  188. }
  189. // check buttons
  190. gInput.buttonFlags.jump = (*control[kJump1] || *control[kJump2]) ? 1 : 0;
  191. gInput.buttonFlags.crouch = (*control[kCrouch1] || *control[kCrouch2]) ? 1 : 0;
  192. gInput.buttonFlags.shoot = ( *control[kFire1] || *control[kFire2] ) ? 1 : 0;
  193. gInput.buttonFlags.shoot2 = ( *control[kAltFire1] || *control[kAltFire2] ) ? 1 : 0;
  194. // if (gInput.buttonFlags.byte != gMe->buttonFlags.byte)
  195. gInput.syncFlags.buttonChange = 1;
  196. if (keystatus[KEY_CAPSLOCK])
  197. {
  198. keystatus[KEY_CAPSLOCK] = 0;
  199. gInput.keyFlags.master = 1;
  200. }
  201. if ( *control[kAction1] || *control[kAction2] )
  202. {
  203. *control[kAction1] = 0;
  204. *control[kAction2] = 0;
  205. gInput.keyFlags.action = 1;
  206. }
  207. gInput.buttonFlags.lookup = 0;
  208. gInput.buttonFlags.lookdown = 0;
  209. if ( *control[kAimUp1] || *control[kAimUp2] )
  210. {
  211. gInput.buttonFlags.lookup = 1;
  212. }
  213. if ( *control[kAimDown1] || *control[kAimDown2] )
  214. {
  215. gInput.buttonFlags.lookdown = 1;
  216. }
  217. if ( *control[kLookUp1] || *control[kLookUp2] )
  218. {
  219. gInput.buttonFlags.lookup = 1;
  220. gInput.keyFlags.lookcenter = 1;
  221. }
  222. if ( *control[kLookDown1] || *control[kLookDown2] )
  223. {
  224. gInput.buttonFlags.lookdown = 1;
  225. gInput.keyFlags.lookcenter = 1;
  226. }
  227. if ( *control[kLookCenter1] || *control[kLookCenter2] )
  228. {
  229. *control[kLookCenter1] = 0;
  230. *control[kLookCenter2] = 0;
  231. gInput.keyFlags.lookcenter = 1;
  232. }
  233. if ( keystatus[KEY_F9] )
  234. {
  235. gInput.keyFlags.restart = 1;
  236. keystatus[KEY_F9] = 0;
  237. }
  238. if ( keystatus[KEY_PAUSE] )
  239. {
  240. dprintf("PAUSE PRESSED\n");
  241. gInput.keyFlags.pause = 1;
  242. keystatus[KEY_PAUSE] = 0;
  243. }
  244. // if ( gInput.keyFlags.byte )
  245. gInput.syncFlags.keyChange = 1;
  246. // check weapon change
  247. for (int i = KEY_1; i <= KEY_0; i++)
  248. {
  249. if (keystatus[i])
  250. {
  251. keystatus[i] = 0;
  252. gInput.newWeapon = (uchar)(i - KEY_1 + 1);
  253. gInput.syncFlags.weaponChange = 1;
  254. }
  255. }
  256. gInput.syncFlags.weaponChange = 1;
  257. _disable(); // prevent the values from changing while we're reading them
  258. gInput.forward = iForward;
  259. // if ( gInput.forward != gMe->forward )
  260. gInput.syncFlags.forwardChange = 1;
  261. gInput.turn = 0;
  262. BYTE shift = *control[kRunOn1] || *control[kRunOn2];
  263. if ( iTurnL )
  264. {
  265. if ( shift && iTurnCount > kTurnThresh )
  266. gInput.turn -= (sshort)ClipRange(iTurnCount * kAngAccel2, -kAngSpeed2, kAngSpeed2);
  267. else
  268. gInput.turn -= (sshort)ClipRange(iTurnCount * kAngAccel1, -kAngSpeed1, kAngSpeed1);
  269. }
  270. if ( iTurnR )
  271. {
  272. if ( shift && iTurnCount > kTurnThresh )
  273. gInput.turn += (sshort)ClipRange(iTurnCount * kAngAccel2, -kAngSpeed2, kAngSpeed2);
  274. else
  275. gInput.turn += (sshort)ClipRange(iTurnCount * kAngAccel1, -kAngSpeed1, kAngSpeed1);
  276. }
  277. // if ( gInput.turn != gMe->turn )
  278. gInput.syncFlags.turnChange = 1;
  279. gInput.strafe = iStrafe;
  280. // if ( gInput.strafe != gMe->strafe )
  281. gInput.syncFlags.strafeChange = 1;
  282. gInput.ticks = iTicks;
  283. iForward = iStrafe = iTicks = 0;
  284. _enable();
  285. if ( useMouse )
  286. {
  287. if ( *control[kStrafeOn1] || *control[kStrafeOn2] )
  288. gInput.strafe = (schar)ClipRange(gInput.strafe - Mouse::dX2, -16, 16);
  289. else
  290. gInput.turn = (sshort)ClipRange(gInput.turn + Mouse::dX2, -256, 256);
  291. gInput.forward = (schar)ClipRange(gInput.forward - Mouse::dY2, -16, 16);
  292. }
  293. }