-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathp_base.c
More file actions
323 lines (275 loc) · 8.06 KB
/
p_base.c
File metadata and controls
323 lines (275 loc) · 8.06 KB
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
/*
CALICO
Core mobj thinking, movement, and clipping checks.
Derived from Doom 64 EX, used under MIT by permission.
The MIT License (MIT)
Copyright (C) 2016 Samuel Villarreal, James Haley
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "doomdef.h"
#include "p_local.h"
#include "mars.h"
void P_ApplyFriction(mobj_t *mo) ATTR_DATA_CACHE_ALIGN;
static void P_FloatChange(mobj_t* mo) ATTR_DATA_CACHE_ALIGN;
void P_ZMovement(mobj_t* mo) ATTR_DATA_CACHE_ALIGN;
void P_MobjThinker(mobj_t* mobj) ATTR_DATA_CACHE_ALIGN;
void P_XYMovement(mobj_t* mo) ATTR_DATA_CACHE_ALIGN;
#define STOPSPEED 0x1000
#define FRICTION 0xd240
//
// Do horizontal movement.
//
void P_ApplyFriction(mobj_t *mo)
{
if(mo->momx > -STOPSPEED && mo->momx < STOPSPEED &&
mo->momy > -STOPSPEED && mo->momy < STOPSPEED)
{
mo->momx = 0;
mo->momy = 0;
}
else
{
#if 0
mo->momx = (mo->momx>>8)*(FRICTION>>8);
mo->momy = (mo->momy>>8)*(FRICTION>>8);
#else
// the original code doesn't produce identical
// results in most cases, but is much slower on
// the SH-2 as it involves calling gcc's builtin
// functions for the >> 8's
mo->momx = FixedMul(mo->momx, FRICTION);
mo->momy = FixedMul(mo->momy, FRICTION);
#endif
}
}
//
// Do horizontal movement.
//
void P_XYMovement(mobj_t *mo)
{
fixed_t xleft, yleft, xuse, yuse;
xleft = xuse = mo->momx & ~7;
yleft = yuse = mo->momy & ~7;
while(xuse > MAXMOVE || xuse < -MAXMOVE || yuse > MAXMOVE || yuse < -MAXMOVE)
{
xuse >>= 1;
yuse >>= 1;
}
while(xleft || yleft)
{
pmovework_t w;
xleft -= xuse;
yleft -= yuse;
if(!P_TryMove(&w, mo, mo->x + xuse, mo->y + yuse))
{
// blocked move
// flying skull?
if(mo->flags & MF_SKULLFLY)
{
mo->extradata = LPTR_TO_SPTR(w.hitthing);
mo->latecall = LC_SKULL_BASH;
return;
}
// explode a missile?
if(mo->flags & MF_MISSILE)
{
if(w.ceilingline && w.ceilingline->sidenum[1] >= 0 && *(int8_t *)&LD_BACKSECTOR(w.ceilingline)->ceilingpic == -1)
{
mo->latecall = LC_REMOVE_MOBJ;
return;
}
mo->extradata = LPTR_TO_SPTR(w.hitthing);
mo->latecall = LC_MISSILE_HIT;
return;
}
mo->momx = mo->momy = 0;
return;
}
}
// slow down
if(mo->flags & (MF_MISSILE|MF_SKULLFLY))
return; // no friction for missiles or flying skulls ever
if(mo->z > mo->floorz)
return; // no friction when airborne
if(mo->flags & MF_CORPSE && mo->floorz != SSEC_SECTOR(mo->subsector)->floorheight)
return; // sliding corpse: don't stop halfway off a step
P_ApplyFriction(mo);
}
//
// Float a flying monster up or down.
//
static void P_FloatChange(mobj_t *mo)
{
mobj_t *target;
fixed_t dist, delta;
target = mo->target; // get the target object
delta = (target->z + ((mo->height*FRACUNIT) >> 1)) - mo->z; // get the height difference
dist = P_AproxDistance(target->x - mo->x, target->y - mo->y);
delta *= 3;
if(delta < 0)
{
if(dist < -delta)
mo->z -= FLOATSPEED; // adjust height downward
}
else if(dist < delta)
mo->z += FLOATSPEED; // adjust height upward
}
//
// Do movement on the z axis.
//
void P_ZMovement(mobj_t *mo)
{
mo->z += mo->momz;
if((mo->flags & MF_FLOAT) && mo->target)
{
// float toward target if too close
P_FloatChange(mo);
}
// clip movement
if(mo->z <= mo->floorz)
{
if (mo->flags & MF_SKULLFLY)
{
// the skull slammed into something
mo->momz = -mo->momz;
}
if(mo->momz < 0)
mo->momz = 0;
mo->z = mo->floorz; // hit the floor
if((mo->flags & MF_MISSILE) && !(mo->flags & MF_NOCLIP))
{
mo->latecall = LC_EXPLODE_MISSILE;
return;
}
}
else if(!(mo->flags & MF_NOGRAVITY))
{
// apply gravity
fixed_t gravity = (GRAVITY * vblsinframe) / TICVBLS;
if(!mo->momz)
mo->momz = -gravity;
else
mo->momz -= gravity/2;
}
if(mo->z + (mo->height*FRACUNIT) > mo->ceilingz)
{
if(mo->momz > 0)
mo->momz = 0;
mo->z = mo->ceilingz - (mo->height*FRACUNIT); // hit the ceiling
if (mo->flags & MF_SKULLFLY)
{
// the skull slammed into something
mo->momz = -mo->momz;
}
if((mo->flags & MF_MISSILE) && !(mo->flags & MF_NOCLIP))
mo->latecall = LC_EXPLODE_MISSILE;
}
}
//
// Perform main thinking logic for a single mobj per tic.
//
void P_MobjThinker(mobj_t *mobj)
{
if (!(mobj->flags & MF_STATIC))
{
// momentum movement
if(mobj->momx || mobj->momy)
P_XYMovement(mobj);
// removed or has a special action to perform?
if(mobj->latecall != LC_NONE)
return;
if(mobj->z != mobj->floorz || mobj->momz)
P_ZMovement(mobj);
// removed or has a special action to perform?
if(mobj->latecall != LC_NONE)
return;
}
// cycle through states
if (mobj->tics != -1)
{
mobj->tics--;
// you can cycle through multiple states in a tic
if (!mobj->tics)
P_SetMobjState(mobj, states[mobj->state].nextstate);
}
}
//
// Do the main thinking logic for mobjs during a gametic.
//
void P_RunMobjBase2(void)
{
mobj_t* mo;
mobj_t* next;
for (mo = mobjhead.next; mo != (void*)&mobjhead; mo = next)
{
#ifdef MARS
// clear cache for mobj flags following the sight check as
// the other CPU might have modified the MF_SEETARGET state
if (mo->tics == 1)
Mars_ClearCacheLine(&mo->flags);
#endif
next = mo->next; /* in case mo is removed this time */
if (mo->type != MT_PLAYER)
P_MobjThinker(mo);
}
}
/*
===================
=
= P_RunMobjLate
=
= Run stuff that doesn't happen every tick
===================
*/
void P_RunMobjLate(void)
{
mobj_t* mo, *targ;
mobj_t* next;
for (mo = mobjhead.next; mo != (void*)&mobjhead; mo = next)
{
next = mo->next; /* in case mo is removed this time */
if (mo->flags & MF_STATIC)
continue;
if (mo->latecall != LC_NONE)
{
switch (mo->latecall)
{
case LC_SKULL_BASH:
targ = SPTR_TO_LPTR(mo->extradata);
L_SkullBash(mo, targ);
break;
case LC_MISSILE_HIT:
targ = SPTR_TO_LPTR(mo->extradata);
L_MissileHit(mo, targ);
break;
case LC_EXPLODE_MISSILE:
P_ExplodeMissile(mo);
break;
case LC_REMOVE_MOBJ:
P_RemoveMobj(mo);
break;
}
mo->latecall = LC_NONE;
}
}
/* move entities, removed this frame, from limbo to free list */
for (mo = limbomobjhead.next; mo != (void*)&limbomobjhead; mo = next)
{
next = mo->next;
P_FreeMobj(mo);
}
}