/
gameinput.cpp
360 lines (306 loc) · 12.4 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
//-------------------------------------------------------------------------
/*
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 "gamefuncs.h"
//---------------------------------------------------------------------------
//
// Static constants used throughout functions.
//
//---------------------------------------------------------------------------
enum
{
BUILDTICRATE = 120,
TURBOTURNBASE = 590,
};
static constexpr double YAW_TURNSPEEDS[3] = { 41.1987304, 156.555175, 272.24121 };
static constexpr double YAW_PREAMBLESCALE = YAW_TURNSPEEDS[0] / YAW_TURNSPEEDS[1];
static constexpr DAngle YAW_LOOKINGSPEED = DAngle::fromDeg(801.5625);
static constexpr DAngle YAW_ROTATESPEED = DAngle::fromDeg(63.28125);
static constexpr DAngle YAW_LOOKRETURN = DAngle::fromDeg(7.5);
static constexpr DAngle YAW_SPINSTAND = DAngle::fromDeg(675.);
static constexpr DAngle YAW_SPINCROUCH = YAW_SPINSTAND * 0.5;
static constexpr DAngle PITCH_LOOKSPEED = DAngle::fromDeg(222.83185);
static constexpr DAngle PITCH_AIMSPEED = PITCH_LOOKSPEED * 0.5;
static constexpr DAngle PITCH_CENTERSPEED = DAngle::fromDeg(10.25);
static constexpr DAngle PITCH_CNTRSINEOFFSET = DAngle::fromDeg(90. + 11.25);
static constexpr DAngle PITCH_HORIZOFFSPEED = DAngle::fromDeg(1.95835);
static constexpr DAngle PITCH_HORIZOFFCLIMB = DAngle::fromDeg(38.);
static constexpr DAngle PITCH_HORIZOFFPUSH = DAngle::fromDeg(0.4476);
//---------------------------------------------------------------------------
//
// Input scale helper functions.
//
//---------------------------------------------------------------------------
template<class T>
inline static T getTicrateScale(const T value)
{
return T(value / GameTicRate);
}
inline static double getCorrectedScale(const double scaleAdjust)
{
// When using the output of I_GetInputFrac() to scale input adjustments at framerate, deviations of over 100 ms can occur.
// Below formula corrects the deviation, with 0.2125 being an average between an ideal value of 0.20 for 40Hz and 0.22 for 30Hz.
// We use the average value here as the difference is under 5 ms and is not worth complicating the algorithm for such precision.
return scaleAdjust < 1. ? scaleAdjust * (1. + 0.21 * (1. - scaleAdjust)) : scaleAdjust;
}
inline static DAngle getscaledangle(const DAngle value, const double scaleAdjust, const DAngle object, const DAngle push)
{
return ((object.Normalized180() * getTicrateScale(value)) + push) * getCorrectedScale(scaleAdjust);
}
inline static void scaletozero(DAngle& object, const DAngle value, const double scaleAdjust, const DAngle push = DAngle::fromDeg(32. / 465.))
{
if (auto sgn = object.Sgn())
{
object -= getscaledangle(value, scaleAdjust, object, push * sgn);
if (sgn != object.Sgn()) object = nullAngle;
}
}
//---------------------------------------------------------------------------
//
// Functions for determining whether its turbo turn time (turn key held for a number of tics).
//
//---------------------------------------------------------------------------
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.
//
//---------------------------------------------------------------------------
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 keymove = 1 << int(!!(inputBuffer->actions & SB_RUN));
float const hidspeed = float(getTicrateScale(YAW_TURNSPEEDS[2]) * turnscale);
float const scaleAdjustf = float(scaleAdjust);
// determine player input.
auto const turning = buttonMap.ButtonDown(gamefunc_Turn_Right) - buttonMap.ButtonDown(gamefunc_Turn_Left);
auto const moving = buttonMap.ButtonDown(gamefunc_Move_Forward) - buttonMap.ButtonDown(gamefunc_Move_Backward) + hidInput->dz * scaleAdjustf;
auto const strafing = buttonMap.ButtonDown(gamefunc_Strafe_Right) - buttonMap.ButtonDown(gamefunc_Strafe_Left) - hidInput->dx * scaleAdjustf;
// process player angle input.
if (!(buttonMap.ButtonDown(gamefunc_Strafe) && allowstrafe))
{
float const turndir = clamp(turning + strafing * !allowstrafe, -1.f, 1.f);
float const turnspeed = float(getTicrateScale(YAW_TURNSPEEDS[keymove]) * turnscale * (isTurboTurnTime() ? 1. : YAW_PREAMBLESCALE));
currInput->avel += hidInput->mouseturnx + (hidInput->dyaw * hidspeed + turndir * turnspeed) * scaleAdjustf;
if (turndir) updateTurnHeldAmt(scaleAdjust); else resetTurnHeldAmt();
}
else
{
currInput->svel += hidInput->mousemovex + (hidInput->dyaw + turning) * keymove * scaleAdjustf;
}
// process player pitch input.
if (!(inputBuffer->actions & SB_AIMMODE))
currInput->horz += hidInput->mouseturny - hidInput->dpitch * hidspeed * scaleAdjustf;
else
currInput->fvel -= hidInput->mousemovey + hidInput->dpitch * keymove * scaleAdjustf;
// process movement input.
currInput->fvel += moving * keymove;
currInput->svel += strafing * keymove * allowstrafe;
// process RR's drunk state.
if (isRR() && drink_amt >= 66 && drink_amt <= 87)
currInput->svel += drink_amt & 1 ? -currInput->fvel : currInput->fvel;
// add collected input to game's local input accumulation packet.
inputBuffer->fvel = clamp(inputBuffer->fvel + currInput->fvel, -(float)keymove, (float)keymove);
inputBuffer->svel = clamp(inputBuffer->svel + currInput->svel, -(float)keymove, (float)keymove);
inputBuffer->avel = clamp(inputBuffer->avel + currInput->avel, -179.f, 179.f);
inputBuffer->horz = clamp(inputBuffer->horz + currInput->horz, -179.f, 179.f);
}
//---------------------------------------------------------------------------
//
// Player's horizon function, called from game's ticker or from gi->GetInput() as required.
//
//---------------------------------------------------------------------------
void PlayerAngles::applyPitch(float const horz, ESyncBits* actions, double const scaleAdjust)
{
// Process only if movement isn't locked.
if (!lockedPitch())
{
// Process mouse input.
if (horz)
{
ZzHORIZON() += DAngle::fromDeg(horz);
*actions &= ~SB_CENTERVIEW;
}
// Process keyboard input.
if (auto aiming = !!(*actions & SB_AIM_DOWN) - !!(*actions & SB_AIM_UP))
{
ZzHORIZON() += getTicrateScale(PITCH_AIMSPEED) * scaleAdjust * aiming;
*actions &= ~SB_CENTERVIEW;
}
if (auto looking = !!(*actions & SB_LOOK_DOWN) - !!(*actions & SB_LOOK_UP))
{
ZzHORIZON() += getTicrateScale(PITCH_LOOKSPEED) * scaleAdjust * looking;
*actions |= SB_CENTERVIEW;
}
// Do return to centre.
if ((*actions & SB_CENTERVIEW) && !(*actions & (SB_LOOK_UP|SB_LOOK_DOWN)))
{
scaletozero(ZzHORIZON(), PITCH_CENTERSPEED * (PITCH_CNTRSINEOFFSET - abs(ZzHORIZON())).Sin(), scaleAdjust);
if (!ZzHORIZON().Sgn()) *actions &= ~SB_CENTERVIEW;
}
// clamp before we finish, even if it's clamped in the drawer.
ZzHORIZON() = ClampViewPitch(ZzHORIZON());
}
else
{
*actions &= ~SB_CENTERVIEW;
}
}
//---------------------------------------------------------------------------
//
// Player's angle function, called from game's ticker or from gi->GetInput() as required.
//
//---------------------------------------------------------------------------
void PlayerAngles::applyYaw(float const avel, ESyncBits* actions, double const scaleAdjust)
{
// Process angle return to zeros.
scaletozero(ZzROTSCRNANG, YAW_LOOKRETURN, scaleAdjust);
scaletozero(ZzLOOKANG, YAW_LOOKRETURN, scaleAdjust);
// Process keyboard input.
if (auto looking = !!(*actions & SB_LOOK_RIGHT) - !!(*actions & SB_LOOK_LEFT))
{
ZzLOOKANG += getTicrateScale(YAW_LOOKINGSPEED) * getCorrectedScale(scaleAdjust) * looking;
ZzROTSCRNANG -= getTicrateScale(YAW_ROTATESPEED) * getCorrectedScale(scaleAdjust) * looking;
}
if (!lockedYaw())
{
// add player's input
ZzANGLE += DAngle::fromDeg(avel);
if (*actions & SB_TURNAROUND)
{
if (YawSpin == nullAngle)
{
// currently not spinning, so start a spin
YawSpin = -DAngle180;
}
*actions &= ~SB_TURNAROUND;
}
if (YawSpin < nullAngle)
{
// return spin to 0
DAngle add = getTicrateScale(!(*actions & SB_CROUCH) ? YAW_SPINSTAND : YAW_SPINCROUCH) * scaleAdjust;
YawSpin += add;
if (YawSpin > nullAngle)
{
// Don't overshoot our target. With variable factor this is possible.
add -= YawSpin;
YawSpin = nullAngle;
}
ZzANGLE += add;
}
}
else
{
YawSpin = nullAngle;
}
}
//---------------------------------------------------------------------------
//
// Player's slope tilt when playing without a mouse and on a slope.
//
//---------------------------------------------------------------------------
void PlayerAngles::doViewPitch(const DVector2& pos, DAngle 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
auto rotpt = pos + ang.ToVector() * (isBlood() ? 4 : 32);
auto tempsect = cursectnum;
updatesector(rotpt, &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
double const j = getflorzofslopeptr(cursectnum, pos);
double const k = getflorzofslopeptr(tempsect, rotpt);
// 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, rotpt) - k) <= 4))
{
ZzHORIZOFF -= maphoriz(scaleAdjust * ((j - k) * (!isBlood() ? 0.625 : 5.5)));
}
}
}
if (climbing)
{
// tilt when climbing but you can't even really tell it.
if (ZzHORIZOFF > PITCH_HORIZOFFCLIMB) ZzHORIZOFF -= getscaledangle(PITCH_HORIZOFFSPEED, scaleAdjust, deltaangle(ZzHORIZOFF, PITCH_HORIZOFFCLIMB), PITCH_HORIZOFFPUSH);
}
else
{
// Make horizoff grow towards 0 since horizoff is not modified when you're not on a slope.
scaletozero(ZzHORIZOFF, PITCH_HORIZOFFSPEED, scaleAdjust, PITCH_HORIZOFFPUSH);
}
// Clamp off against the maximum allowed pitch.
ZzHORIZOFF = ClampViewPitch(ZzHORIZOFF);
}
}
//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------
FSerializer& Serialize(FSerializer& arc, const char* keyname, PlayerAngles& w, PlayerAngles* def)
{
if (arc.BeginObject(keyname))
{
arc("ang", w.ZzANGLE)
("lookang", w.ZzLOOKANG)
("rotscrnang", w.ZzROTSCRNANG)
("spin", w.YawSpin)
("actor", w.pActor)
("inputdisabled", w.legacyDisabledYaw)
("horiz", w.ZzHORIZON())
("horizoff", w.ZzHORIZOFF)
("inputdisabled", w.legacyDisabledPitch)
.EndObject();
if (arc.isReading())
{
w.ZzOLDANGLE = w.ZzANGLE;
w.ZzOLDLOOKANG = w.ZzLOOKANG;
w.ZzOLDROTSCRNANG = w.ZzROTSCRNANG;
w.legacyDisabledYaw = w.legacyDisabledYaw;
w.resetAdjustmentYaw();
w.ZzOLDHORIZON() = w.ZzHORIZON();
w.ZzOHORIZOFF = w.ZzHORIZOFF;
w.legacyDisabledPitch = w.legacyDisabledPitch;
w.resetAdjustmentPitch();
}
}
return arc;
}