/
gameinput.cpp
580 lines (492 loc) · 15.7 KB
/
gameinput.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
//-------------------------------------------------------------------------
/*
Copyright (C) 2019 Christoph Oelckers
Copyright (C) 2020 Mitchell Richters
This is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
//-------------------------------------------------------------------------
#include "gamecontrol.h"
#include "gameinput.h"
#include "gamestruct.h"
#include "serializer.h"
#include "build.h"
CVARD(Bool, invertmousex, false, CVAR_ARCHIVE | CVAR_GLOBALCONFIG, "invert horizontal mouse movement")
CVARD(Bool, invertmouse, false, CVAR_ARCHIVE | CVAR_GLOBALCONFIG, "invert vertical mouse movement")
//---------------------------------------------------------------------------
//
// code fron gameexec/conrun
//
//---------------------------------------------------------------------------
int getincangle(int a, int na)
{
a &= 2047;
na &= 2047;
if(abs(a-na) >= 1024)
{
if(na > 1024) na -= 2048;
if(a > 1024) a -= 2048;
}
return na-a;
}
binangle getincanglebam(binangle a, binangle na)
{
int64_t cura = a.asbam();
int64_t newa = na.asbam();
if(abs(cura-newa) > INT32_MAX)
{
if(newa > INT32_MAX) newa -= UINT32_MAX;
if(cura > INT32_MAX) cura -= UINT32_MAX;
}
return bamang(uint32_t(newa-cura));
}
//---------------------------------------------------------------------------
//
// Input scale helper functions.
//
//---------------------------------------------------------------------------
inline static double getTicrateScale(const double value)
{
return value * (1. / GameTicRate);
}
inline static double getPushScale(const double scaleAdjust)
{
return (2. / 9.) * (scaleAdjust < 1. ? (1. - scaleAdjust * 0.5) * 1.5 : 1.);
}
inline static fixedhoriz getscaledhoriz(const double value, const double scaleAdjust, const fixedhoriz& object, const double push)
{
return buildfhoriz(scaleAdjust * ((object.asbuildf() * getTicrateScale(value)) + push));
}
inline static binangle getscaledangle(const double value, const double scaleAdjust, const binangle& object, const double push)
{
return buildfang(scaleAdjust * ((object.signedbuildf() * getTicrateScale(value)) + push));
}
inline static void scaletozero(fixedhoriz& object, const double value, const double scaleAdjust, const double push = DBL_MAX)
{
if (auto sgn = Sgn(object.asq16()))
{
object -= getscaledhoriz(value, scaleAdjust, object, push == DBL_MAX ? sgn * getPushScale(scaleAdjust) : push);
if (sgn != Sgn(object.asq16())) object = q16horiz(0);
}
}
inline static void scaletozero(binangle& object, const double value, const double scaleAdjust, const double push = DBL_MAX)
{
if (auto sgn = Sgn(object.signedbam()))
{
object -= getscaledangle(value, scaleAdjust, object, push == DBL_MAX ? sgn * getPushScale(scaleAdjust) : push);
if (sgn != Sgn(object.signedbam())) object = bamang(0);
}
}
//---------------------------------------------------------------------------
//
// Functions for determining whether its turbo turn time (turn key held for a number of tics).
//
//---------------------------------------------------------------------------
/*
// Turbo turn time.
Blood: 24 * 30 = 720;
Duke: 120 / 8 * 30 = 450;
SW: 120 / 8 * 40 = 600;
Exhumed: N/A;
Average: 590.;
*/
enum
{
BUILDTICRATE = 120,
TURBOTURNBASE = 590,
};
static double turnheldtime;
void updateTurnHeldAmt(double const scaleAdjust)
{
turnheldtime += getTicrateScale(BUILDTICRATE) * scaleAdjust;
}
bool isTurboTurnTime()
{
return turnheldtime >= getTicrateScale(TURBOTURNBASE);
}
void resetTurnHeldAmt()
{
turnheldtime = 0;
}
//---------------------------------------------------------------------------
//
// Player's movement function, called from game's ticker or from gi->GetInput() as required.
//
//---------------------------------------------------------------------------
/*
// Running speed.
Blood: 92 / 4 * 2 * 30 = 1380;
Duke: 15 * 2 * 2 * 30 = 1800;
SW: 28 * 1.40625 * 40 = 1575; // Precisely, ((((28 * 12) + ((28 * 12) / 4)) * 3) / 32) * 40
Exhumed: 12 * 4 * 30 = 1440;
Average: 1548.75;
// Normal speed.
Blood: 92 / 4 * 30 = 690;
Duke: 15 * 2 * 30 = 900;
SW: 18 * 1.40625 * 40 = 1012.5; // Precisely, (((((12 + 6) * 12) + (((12 + 6) * 12) / 4)) * 3) / 32) * 40
Exhumed: 8 * 4 * 30 = 960;
Average: 890.625;
// Preamble.
Blood: N/A;
Exhumed: N/A;
Duke: 5 * 2 * 30 = 300;
SW: 3 * 1.40625 * 40 = 168.75; // Precisely, ((((3 * 12) + ((3 * 12) / 4)) * 3) / 32) * 40
Average: 234.375;
*/
enum
{
RUNNINGTURNBASE = 1549,
NORMALTURNBASE = 891,
PREAMBLEBASE = 234,
};
void processMovement(InputPacket* const currInput, InputPacket* const inputBuffer, ControlInfo* const hidInput, double const scaleAdjust, int const drink_amt, bool const allowstrafe, double const turnscale)
{
// set up variables
int const running = !!(inputBuffer->actions & SB_RUN);
int const keymove = gi->playerKeyMove() << running;
float const mousevelscale = keymove * (1.f / 160.f);
double const hidprescale = g_gameType & GAMEFLAG_PSEXHUMED ? 5. : 1.;
double const hidspeed = getTicrateScale(running ? RUNNINGTURNBASE : NORMALTURNBASE) * BAngToDegree;
// process mouse and initial controller input.
if (buttonMap.ButtonDown(gamefunc_Strafe) && allowstrafe)
currInput->svel -= xs_CRoundToInt(((hidInput->mousemovex * mousevelscale) + (scaleAdjust * hidInput->dyaw * keymove)) * hidprescale);
else
currInput->avel += float(hidInput->mouseturnx + (scaleAdjust * hidInput->dyaw * hidspeed * turnscale));
if (!(inputBuffer->actions & SB_AIMMODE))
currInput->horz -= hidInput->mouseturny;
else
currInput->fvel -= xs_CRoundToInt(hidInput->mousemovey * mousevelscale * hidprescale);
if (invertmouse)
currInput->horz = -currInput->horz;
if (invertmousex)
currInput->avel = -currInput->avel;
// process remaining controller input.
currInput->horz -= float(scaleAdjust * hidInput->dpitch * hidspeed);
currInput->svel += xs_CRoundToInt(scaleAdjust * hidInput->dx * keymove * hidprescale);
currInput->fvel += xs_CRoundToInt(scaleAdjust * hidInput->dz * keymove * hidprescale);
// process keyboard turning keys.
if (buttonMap.ButtonDown(gamefunc_Strafe) && allowstrafe)
{
if (abs(inputBuffer->svel) < keymove)
{
if (buttonMap.ButtonDown(gamefunc_Turn_Left))
currInput->svel += keymove;
if (buttonMap.ButtonDown(gamefunc_Turn_Right))
currInput->svel -= keymove;
}
}
else
{
bool const turnleft = buttonMap.ButtonDown(gamefunc_Turn_Left) || (buttonMap.ButtonDown(gamefunc_Strafe_Left) && !allowstrafe);
bool const turnright = buttonMap.ButtonDown(gamefunc_Turn_Right) || (buttonMap.ButtonDown(gamefunc_Strafe_Right) && !allowstrafe);
if (turnleft || turnright)
{
updateTurnHeldAmt(scaleAdjust);
float const turnamount = float(scaleAdjust * hidspeed * turnscale * (isTurboTurnTime() ? 1. : double(PREAMBLEBASE) / double(NORMALTURNBASE)));
if (turnleft)
currInput->avel -= turnamount;
if (turnright)
currInput->avel += turnamount;
}
else
{
resetTurnHeldAmt();
}
}
// process keyboard forward/side velocity keys.
if (abs(inputBuffer->svel) < keymove)
{
if (buttonMap.ButtonDown(gamefunc_Strafe_Left) && allowstrafe)
currInput->svel += keymove;
if (buttonMap.ButtonDown(gamefunc_Strafe_Right) && allowstrafe)
currInput->svel -= keymove;
}
if (abs(inputBuffer->fvel) < keymove)
{
if (isRR() && drink_amt >= 66 && drink_amt <= 87)
{
if (buttonMap.ButtonDown(gamefunc_Move_Forward))
{
currInput->fvel += keymove;
currInput->svel += drink_amt & 1 ? keymove : -keymove;
}
if (buttonMap.ButtonDown(gamefunc_Move_Backward))
{
currInput->fvel -= keymove;
currInput->svel -= drink_amt & 1 ? keymove : -keymove;
}
}
else
{
if (buttonMap.ButtonDown(gamefunc_Move_Forward))
currInput->fvel += keymove;
if (buttonMap.ButtonDown(gamefunc_Move_Backward))
currInput->fvel -= keymove;
}
}
// add collected input to game's local input accumulation packet.
inputBuffer->fvel = clamp(inputBuffer->fvel + currInput->fvel, -keymove, keymove);
inputBuffer->svel = clamp(inputBuffer->svel + currInput->svel, -keymove, keymove);
inputBuffer->avel += currInput->avel;
inputBuffer->horz += currInput->horz;
}
//---------------------------------------------------------------------------
//
// Player's horizon function, called from game's ticker or from gi->GetInput() as required.
//
//---------------------------------------------------------------------------
/*
// Aim speed.
Duke: 6 * 30 = 180;
SW: (16 / 2) * 40 = 320;
Average: 250.;
// Look speed.
Duke: 12 * 30 = 360;
SW: 16 * 40 = 640;
Average: 500.;
// Return to centre speed.
Duke: (1 / 3) * 30 = 10;
SW: (1 / 4) * 40 = 10;
Average: 10.;
*/
enum
{
AIMSPEED = 250,
LOOKSPEED = 500,
CNTRSPEED = 10,
};
void PlayerHorizon::applyinput(float const horz, ESyncBits* actions, double const scaleAdjust)
{
// Process only if movewment isn't locked.
if (!movementlocked())
{
// Test if we have input to process.
if (horz || *actions & (SB_AIM_UP | SB_AIM_DOWN | SB_LOOK_UP | SB_LOOK_DOWN))
{
// Store current horizon as true pitch.
double pitch = horiz.aspitch();
// Process mouse input.
if (horz)
{
*actions &= ~SB_CENTERVIEW;
pitch += horz;
}
// Process keyboard input.
auto doKbdInput = [&](ESyncBits_ const up, ESyncBits_ const down, double const rate, bool const lock)
{
if (*actions & (up | down))
{
if (lock) *actions &= ~SB_CENTERVIEW; else *actions |= SB_CENTERVIEW;
double const amount = scaleAdjust * HorizToPitch(getTicrateScale(rate));
if (*actions & down)
pitch -= amount;
if (*actions & up)
pitch += amount;
}
};
doKbdInput(SB_AIM_UP, SB_AIM_DOWN, AIMSPEED, true);
doKbdInput(SB_LOOK_UP, SB_LOOK_DOWN, LOOKSPEED, false);
// clamp before converting back to horizon
horiz = q16horiz(clamp(PitchToHoriz(pitch), gi->playerHorizMin(), gi->playerHorizMax()));
}
// return to center if conditions met.
if ((*actions & SB_CENTERVIEW) && !(*actions & (SB_LOOK_UP|SB_LOOK_DOWN)))
{
scaletozero(horiz, CNTRSPEED, scaleAdjust);
if (!horiz.asq16()) *actions &= ~SB_CENTERVIEW;
}
}
else
{
*actions &= ~SB_CENTERVIEW;
}
}
//---------------------------------------------------------------------------
//
// Player's angle function, called from game's ticker or from gi->GetInput() as required.
//
//---------------------------------------------------------------------------
/*
// Rotate return speed.
Duke: (1 / 2) * 30 = 15;
// Look return speed.
Duke: (1 / 4) * 30 = 7.5;
// Rotating speed.
Duke: 24 * 30 = 720;
// Looking speed.
Duke: 152 * 30 = 4560;
// Spin standing speed.
Duke: 128 * 30 = 3840;
Blood: 128 * 30 = 3840;
// Looking speed.
Blood: 64 * 30 = 1920;
*/
enum
{
LOOKROTRETBASE = 15,
ROTATESPEED = 720,
LOOKINGSPEED = 4560,
SPINSTAND = 3840,
SPINCROUCH = 1920,
};
void PlayerAngle::applyinput(float const avel, ESyncBits* actions, double const scaleAdjust)
{
// Process angle return to zeros.
scaletozero(rotscrnang, LOOKROTRETBASE, scaleAdjust);
scaletozero(look_ang, +LOOKROTRETBASE * 0.5, scaleAdjust);
// Process keyboard input.
auto doLookKeys = [&](ESyncBits_ const key, double const direction)
{
if (*actions & key)
{
look_ang += buildfang(getTicrateScale(LOOKINGSPEED) * scaleAdjust * direction);
rotscrnang -= buildfang(getTicrateScale(ROTATESPEED) * scaleAdjust * direction);
}
};
doLookKeys(SB_LOOK_LEFT, -1);
doLookKeys(SB_LOOK_RIGHT, 1);
if (!movementlocked())
{
if (*actions & SB_TURNAROUND)
{
if (spin == 0)
{
// currently not spinning, so start a spin
spin = -1024.;
}
*actions &= ~SB_TURNAROUND;
}
if (avel)
{
// add player's input
ang += degang(avel);
}
if (spin < 0)
{
// return spin to 0
double add = getTicrateScale(!(*actions & SB_CROUCH) ? SPINSTAND : SPINCROUCH) * scaleAdjust;
spin += add;
if (spin > 0)
{
// Don't overshoot our target. With variable factor this is possible.
add -= spin;
spin = 0;
}
ang += buildfang(add);
}
}
else
{
spin = 0;
}
}
//---------------------------------------------------------------------------
//
// Player's slope tilt when playing without a mouse and on a slope.
//
//---------------------------------------------------------------------------
/*
// Horizoff centre speed.
Duke: (1 / 8) * 30 = 3.75;
SW: (1 / 8) * 40 = 5;
Average: 4.375;
*/
enum
{
// Values used by Duke/SW, where this function originated from.
DEFSINSHIFT = 5,
DEFVIEWPITCH = 160,
// Values used by Blood since it calculates differently to Duke/SW.
BLOODSINSHIFT = 8,
SINSHIFTDELTA = BLOODSINSHIFT - DEFSINSHIFT,
BLOODVIEWPITCH = (0x4000 >> SINSHIFTDELTA) - (DEFVIEWPITCH << (SINSHIFTDELTA - 1)), // 1408.
};
void PlayerHorizon::calcviewpitch(vec2_t const pos, binangle const ang, bool const aimmode, bool const canslopetilt, sectortype* const cursectnum, double const scaleAdjust, bool const climbing)
{
if (cl_slopetilting && cursectnum != nullptr)
{
if (aimmode && canslopetilt) // If the floor is sloped
{
// Get a point, 512 (64 for Blood) units ahead of player's position
int const shift = -(isBlood() ? BLOODSINSHIFT : DEFSINSHIFT);
int const x = pos.X + ang.bcos(shift);
int const y = pos.Y + ang.bsin(shift);
auto tempsect = cursectnum;
updatesector(x, y, &tempsect);
if (tempsect != nullptr) // If the new point is inside a valid sector...
{
// Get the floorz as if the new (x,y) point was still in
// your sector
int const j = getflorzofslopeptr(cursectnum, pos.X, pos.Y);
int const k = getflorzofslopeptr(tempsect, x, y);
// If extended point is in same sector as you or the slopes
// of the sector of the extended point and your sector match
// closely (to avoid accidently looking straight out when
// you're at the edge of a sector line) then adjust horizon
// accordingly
if (cursectnum == tempsect || (!isBlood() && abs(getflorzofslopeptr(tempsect, x, y) - k) <= (4 << 8)))
{
horizoff += q16horiz(xs_CRoundToInt(scaleAdjust * ((j - k) * (!isBlood() ? DEFVIEWPITCH : BLOODVIEWPITCH))));
}
}
}
if (climbing)
{
// tilt when climbing but you can't even really tell it.
if (horizoff.asq16() < IntToFixed(100)) horizoff += getscaledhoriz(4.375, scaleAdjust, buildhoriz(100) - horizoff, 1.);
}
else
{
// Make horizoff grow towards 0 since horizoff is not modified when you're not on a slope.
scaletozero(horizoff, 4.375, scaleAdjust, Sgn(horizoff.asq16()));
}
}
}
FSerializer& Serialize(FSerializer& arc, const char* keyname, PlayerAngle& w, PlayerAngle* def)
{
if (arc.BeginObject(keyname))
{
arc("ang", w.ang)
("lookang", w.look_ang)
("rotscrnang", w.rotscrnang)
("spin", w.spin)
("inputdisabled", w.inputdisabled)
.EndObject();
if (arc.isReading())
{
w.oang = w.ang;
w.olook_ang = w.look_ang;
w.orotscrnang = w.rotscrnang;
w.inputdisabled = w.inputdisabled;
w.resetadjustment();
}
}
return arc;
}
FSerializer& Serialize(FSerializer& arc, const char* keyname, PlayerHorizon& w, PlayerHorizon* def)
{
if (arc.BeginObject(keyname))
{
arc("horiz", w.horiz)
("horizoff", w.horizoff)
("inputdisabled", w.inputdisabled)
.EndObject();
if (arc.isReading())
{
w.ohoriz = w.horiz;
w.ohorizoff = w.horizoff;
w.inputdisabled = w.inputdisabled;
w.resetadjustment();
}
}
return arc;
}