/
a_pusher.cpp
360 lines (321 loc) · 9.94 KB
/
a_pusher.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 1998-1998 Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman
// Copyright 1999-2016 Randy Heit
// Copyright 2002-2016 Christoph Oelckers
//
// This program 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 3 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, see http://www.gnu.org/licenses/
//
//-----------------------------------------------------------------------------
//
// DESCRIPTION:
// Initializes and implements BOOM linedef triggers for
// Wind/Current
//
//-----------------------------------------------------------------------------
#include <stdlib.h>
#include "actor.h"
#include "p_spec.h"
#include "serializer.h"
#include "serialize_obj.h"
#include "p_lnspec.h"
#include "p_maputl.h"
#include "p_local.h"
#include "d_player.h"
#include "g_levellocals.h"
#include "actorinlines.h"
#include "p_spec_thinkers.h"
#include "maploader/maploader.h"
EXTERN_CVAR(Bool, var_pushers);
IMPLEMENT_CLASS(DPusher, false, true)
IMPLEMENT_POINTERS_START(DPusher)
IMPLEMENT_POINTER(m_Source)
IMPLEMENT_POINTERS_END
//-----------------------------------------------------------------------------
//
//
//
//-----------------------------------------------------------------------------
void DPusher::Serialize(FSerializer &arc)
{
Super::Serialize (arc);
arc.Enum("type", m_Type)
("source", m_Source)
("pushvec", m_PushVec)
("magnitude", m_Magnitude)
("radius", m_Radius)
("affectee", m_Affectee);
}
//-----------------------------------------------------------------------------
//
// PUSH/PULL EFFECT
//
// phares 3/20/98: Start of push/pull effects
//
// This is where push/pull effects are applied to objects in the sectors.
//
// There are four kinds of push effects
//
// 1) Pushing Away
//
// Pushes you away from a point source defined by the location of an
// MT_PUSH Thing. The force decreases linearly with distance from the
// source. This force crosses sector boundaries and is felt w/in a circle
// whose center is at the MT_PUSH. The force is felt only if the point
// MT_PUSH can see the target object.
//
// 2) Pulling toward
//
// Same as Pushing Away except you're pulled toward an MT_PULL point
// source. This force crosses sector boundaries and is felt w/in a circle
// whose center is at the MT_PULL. The force is felt only if the point
// MT_PULL can see the target object.
//
// 3) Wind
//
// Pushes you in a constant direction. Full force above ground, half
// force on the ground, nothing if you're below it (water).
//
// 4) Current
//
// Pushes you in a constant direction. No force above ground, full
// force if on the ground or below it (water).
//
// The magnitude of the force is controlled by the length of a controlling
// linedef. The force vector for types 3 & 4 is determined by the angle
// of the linedef, and is constant.
//
// For each sector where these effects occur, the sector special type has
// to have the PUSH_MASK bit set. If this bit is turned off by a switch
// at run-time, the effect will not occur. The controlling sector for
// types 1 & 2 is the sector containing the MT_PUSH/MT_PULL Thing.
//
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//
// Add a push thinker to the thinker list
//
//-----------------------------------------------------------------------------
void DPusher::Construct (DPusher::EPusher type, line_t *l, int magnitude, int angle,
AActor *source, int affectee)
{
m_Source = source;
m_Type = type;
if (l)
{
m_PushVec = l->Delta();
m_Magnitude = m_PushVec.Length();
}
else
{ // [RH] Allow setting magnitude and angle with parameters
ChangeValues (magnitude, angle);
}
if (source) // point source exist?
{
m_Radius = m_Magnitude * 2; // where force goes to zero
}
m_Affectee = affectee;
}
int DPusher::CheckForSectorMatch (EPusher type, int tag)
{
if (m_Type == type && Level->SectorHasTag(m_Affectee, tag))
return m_Affectee;
else
return -1;
}
//-----------------------------------------------------------------------------
//
// T_Pusher looks for all objects that are inside the radius of
// the effect.
//
//-----------------------------------------------------------------------------
void DPusher::Tick ()
{
sector_t *sec;
AActor *thing;
msecnode_t *node;
double ht;
if (!var_pushers)
return;
sec = &Level->sectors[m_Affectee];
// Be sure the special sector type is still turned on. If so, proceed.
// Else, bail out; the sector type has been changed on us.
if (!(sec->Flags & SECF_PUSH))
return;
// For constant pushers (wind/current) there are 3 situations:
//
// 1) Affected Thing is above the floor.
//
// Apply the full force if wind, no force if current.
//
// 2) Affected Thing is on the ground.
//
// Apply half force if wind, full force if current.
//
// 3) Affected Thing is below the ground (underwater effect).
//
// Apply no force if wind, full force if current.
//
// Apply the effect to clipped players only for now.
//
// In Phase II, you can apply these effects to Things other than players.
// [RH] No Phase II, but it works with anything having MF2_WINDTHRUST now.
if (m_Type == p_push)
{
if (m_Source == nullptr)
{
Destroy();
return;
}
// Seek out all pushable things within the force radius of this
// point pusher. Crosses sectors, so use blockmap.
FPortalGroupArray check(FPortalGroupArray::PGA_NoSectorPortals); // no sector portals because this thing is utterly z-unaware.
FMultiBlockThingsIterator it(check, m_Source, m_Radius);
FMultiBlockThingsIterator::CheckResult cres;
while (it.Next(&cres))
{
AActor *thing = cres.thing;
// Normal ZDoom is based only on the WINDTHRUST flag, with the noclip cheat as an exemption.
bool pusharound = ((thing->flags2 & MF2_WINDTHRUST) && !(thing->flags & MF_NOCLIP));
// MBF allows any sentient or shootable thing to be affected, but players with a fly cheat aren't.
if (Level->i_compatflags & COMPATF_MBFMONSTERMOVE)
{
pusharound = ((pusharound || (thing->IsSentient()) || (thing->flags & MF_SHOOTABLE)) // Add categories here
&& (!(thing->player && (thing->flags & (MF_NOGRAVITY))))); // Exclude flying players here
}
if ((pusharound) )
{
DVector2 pos = m_Source->Vec2To(thing);
double dist = pos.Length();
double speed = (m_Magnitude - (dist/2)) / (PUSH_FACTOR * 2);
// If speed <= 0, you're outside the effective radius. You also have
// to be able to see the push/pull source point.
if ((speed > 0) && (P_CheckSight (thing, m_Source, SF_IGNOREVISIBILITY)))
{
DAngle pushangle = pos.Angle();
if (m_Source->GetClass()->TypeName == NAME_PointPuller) pushangle += 180;
thing->Thrust(pushangle, speed);
}
}
}
return;
}
// constant pushers p_wind and p_current
node = sec->touching_thinglist; // things touching this sector
for ( ; node ; node = node->m_snext)
{
thing = node->m_thing;
if (!(thing->flags2 & MF2_WINDTHRUST) || (thing->flags & MF_NOCLIP))
continue;
sector_t *hsec = sec->GetHeightSec();
DVector3 pos = thing->PosRelative(sec);
DVector2 pushvel;
if (m_Type == p_wind)
{
if (hsec == NULL)
{ // NOT special water sector
if (thing->Z() > thing->floorz) // above ground
{
pushvel = m_PushVec; // full force
}
else // on ground
{
pushvel = m_PushVec / 2; // half force
}
}
else // special water sector
{
ht = hsec->floorplane.ZatPoint(pos);
if (thing->Z() > ht) // above ground
{
pushvel = m_PushVec; // full force
}
else if (thing->player && thing->player->viewz < ht) // underwater
{
pushvel.Zero(); // no force
}
else // wading in water
{
pushvel = m_PushVec / 2; // half force
}
}
}
else // p_current
{
const secplane_t *floor;
if (hsec == NULL)
{ // NOT special water sector
floor = &sec->floorplane;
}
else
{ // special water sector
floor = &hsec->floorplane;
}
if (thing->Z() > floor->ZatPoint(pos))
{ // above ground
pushvel.Zero(); // no force
}
else
{ // on ground/underwater
pushvel = m_PushVec; // full force
}
}
thing->Vel += pushvel / PUSH_FACTOR;
}
}
//-----------------------------------------------------------------------------
//
//
//
//-----------------------------------------------------------------------------
void FLevelLocals::AdjustPusher(int tag, int magnitude, int angle, bool wind)
{
struct FThinkerCollection
{
int RefNum;
DThinker *Obj;
};
DPusher::EPusher type = wind? DPusher::p_wind : DPusher::p_current;
// Find pushers already attached to the sector, and change their parameters.
TArray<FThinkerCollection> Collection;
{
auto iterator = GetThinkerIterator<DPusher>();
FThinkerCollection collect;
while ((collect.Obj = iterator.Next()))
{
if ((collect.RefNum = ((DPusher *)collect.Obj)->CheckForSectorMatch(type, tag)) >= 0)
{
((DPusher *)collect.Obj)->ChangeValues(magnitude, angle);
Collection.Push(collect);
}
}
}
size_t numcollected = Collection.Size();
int secnum;
// Now create pushers for any sectors that don't already have them.
auto itr = GetSectorTagIterator(tag);
while ((secnum = itr.Next()) >= 0)
{
unsigned int i;
for (i = 0; i < numcollected; i++)
{
if (Collection[i].RefNum == secnum)
break;
}
if (i == numcollected)
{
CreateThinker<DPusher>(type, nullptr, magnitude, angle, nullptr, secnum);
}
}
}