| 1 |
/* Emacs style mode select -*- C -*- |
| 2 |
*----------------------------------------------------------------------------- |
| 3 |
* |
| 4 |
* |
| 5 |
* PrBoom: a Doom port merged with LxDoom and LSDLDoom |
| 6 |
* based on BOOM, a modified and improved DOOM engine |
| 7 |
* Copyright (C) 1999 by |
| 8 |
* id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman |
| 9 |
* Copyright (C) 1999-2000,2002 by |
| 10 |
* Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze |
| 11 |
* Copyright 2005, 2006 by |
| 12 |
* Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko |
| 13 |
* |
| 14 |
* This program is free software; you can redistribute it and/or |
| 15 |
* modify it under the terms of the GNU General Public License |
| 16 |
* as published by the Free Software Foundation; either version 2 |
| 17 |
* of the License, or (at your option) any later version. |
| 18 |
* |
| 19 |
* This program is distributed in the hope that it will be useful, |
| 20 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 |
* GNU General Public License for more details. |
| 23 |
* |
| 24 |
* You should have received a copy of the GNU General Public License |
| 25 |
* along with this program; if not, write to the Free Software |
| 26 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 27 |
* 02111-1307, USA. |
| 28 |
* |
| 29 |
* DESCRIPTION: |
| 30 |
* Thinker, Ticker. |
| 31 |
* |
| 32 |
*-----------------------------------------------------------------------------*/ |
| 33 |
|
| 34 |
#include "doomstat.h" |
| 35 |
#include "p_user.h" |
| 36 |
#include "p_spec.h" |
| 37 |
#include "p_tick.h" |
| 38 |
#include "p_map.h" |
| 39 |
#include "r_fps.h" |
| 40 |
#include "e6y.h" |
| 41 |
#include "s_advsound.h" |
| 42 |
|
| 43 |
#include "hexen/p_anim.h" |
| 44 |
|
| 45 |
#include "dsda.h" |
| 46 |
#include "dsda/pause.h" |
| 47 |
|
| 48 |
int leveltime; |
| 49 |
|
| 50 |
static dboolean newthinkerpresent; |
| 51 |
|
| 52 |
// |
| 53 |
// THINKERS |
| 54 |
// All thinkers should be allocated by Z_Malloc |
| 55 |
// so they can be operated on uniformly. |
| 56 |
// The actual structures will vary in size, |
| 57 |
// but the first element must be thinker_t. |
| 58 |
// |
| 59 |
|
| 60 |
// killough 8/29/98: we maintain several separate threads, each containing |
| 61 |
// a special class of thinkers, to allow more efficient searches. |
| 62 |
thinker_t thinkerclasscap[th_all+1]; |
| 63 |
int init_thinkers_count = 0; |
| 64 |
|
| 65 |
// |
| 66 |
// P_InitThinkers |
| 67 |
// |
| 68 |
|
| 69 |
void P_InitThinkers(void) |
| 70 |
{ |
| 71 |
int i; |
| 72 |
|
| 73 |
for (i=0; i<NUMTHCLASS; i++) // killough 8/29/98: initialize threaded lists |
| 74 |
thinkerclasscap[i].cprev = thinkerclasscap[i].cnext = &thinkerclasscap[i]; |
| 75 |
|
| 76 |
thinkercap.prev = thinkercap.next = &thinkercap; |
| 77 |
|
| 78 |
init_thinkers_count++; |
| 79 |
} |
| 80 |
|
| 81 |
// |
| 82 |
// killough 8/29/98: |
| 83 |
// |
| 84 |
// We maintain separate threads of friends and enemies, to permit more |
| 85 |
// efficient searches. |
| 86 |
// |
| 87 |
|
| 88 |
void P_UpdateThinker(thinker_t *thinker) |
| 89 |
{ |
| 90 |
register thinker_t *th; |
| 91 |
// find the class the thinker belongs to |
| 92 |
|
| 93 |
int class = |
| 94 |
thinker->function == P_RemoveThinkerDelayed ? th_delete : |
| 95 |
thinker->function == P_MobjThinker && |
| 96 |
((mobj_t *) thinker)->health > 0 && |
| 97 |
(((mobj_t *) thinker)->flags & MF_COUNTKILL || |
| 98 |
((mobj_t *) thinker)->type == MT_SKULL) ? |
| 99 |
((mobj_t *) thinker)->flags & MF_FRIEND ? |
| 100 |
th_friends : th_enemies : th_misc; |
| 101 |
|
| 102 |
{ |
| 103 |
/* Remove from current thread, if in one */ |
| 104 |
if ((th = thinker->cnext)!= NULL) |
| 105 |
(th->cprev = thinker->cprev)->cnext = th; |
| 106 |
} |
| 107 |
|
| 108 |
// Add to appropriate thread |
| 109 |
th = &thinkerclasscap[class]; |
| 110 |
th->cprev->cnext = thinker; |
| 111 |
thinker->cnext = th; |
| 112 |
thinker->cprev = th->cprev; |
| 113 |
th->cprev = thinker; |
| 114 |
} |
| 115 |
|
| 116 |
// |
| 117 |
// P_AddThinker |
| 118 |
// Adds a new thinker at the end of the list. |
| 119 |
// |
| 120 |
|
| 121 |
void P_AddThinker(thinker_t* thinker) |
| 122 |
{ |
| 123 |
thinkercap.prev->next = thinker; |
| 124 |
thinker->next = &thinkercap; |
| 125 |
thinker->prev = thinkercap.prev; |
| 126 |
thinkercap.prev = thinker; |
| 127 |
|
| 128 |
thinker->references = 0; // killough 11/98: init reference counter to 0 |
| 129 |
|
| 130 |
// killough 8/29/98: set sentinel pointers, and then add to appropriate list |
| 131 |
thinker->cnext = thinker->cprev = NULL; |
| 132 |
P_UpdateThinker(thinker); |
| 133 |
newthinkerpresent = true; |
| 134 |
} |
| 135 |
|
| 136 |
// |
| 137 |
// killough 11/98: |
| 138 |
// |
| 139 |
// Make currentthinker external, so that P_RemoveThinkerDelayed |
| 140 |
// can adjust currentthinker when thinkers self-remove. |
| 141 |
|
| 142 |
static thinker_t *currentthinker; |
| 143 |
|
| 144 |
// |
| 145 |
// P_RemoveThinkerDelayed() |
| 146 |
// |
| 147 |
// Called automatically as part of the thinker loop in P_RunThinkers(), |
| 148 |
// on nodes which are pending deletion. |
| 149 |
// |
| 150 |
// If this thinker has no more pointers referencing it indirectly, |
| 151 |
// remove it, and set currentthinker to one node preceeding it, so |
| 152 |
// that the next step in P_RunThinkers() will get its successor. |
| 153 |
// |
| 154 |
|
| 155 |
void P_RemoveThinkerDelayed(thinker_t *thinker) |
| 156 |
{ |
| 157 |
if (!thinker->references) |
| 158 |
{ |
| 159 |
{ /* Remove from main thinker list */ |
| 160 |
thinker_t *next = thinker->next; |
| 161 |
/* Note that currentthinker is guaranteed to point to us, |
| 162 |
* and since we're freeing our memory, we had better change that. So |
| 163 |
* point it to thinker->prev, so the iterator will correctly move on to |
| 164 |
* thinker->prev->next = thinker->next */ |
| 165 |
(next->prev = currentthinker = thinker->prev)->next = next; |
| 166 |
} |
| 167 |
{ |
| 168 |
/* Remove from current thinker class list */ |
| 169 |
thinker_t *th = thinker->cnext; |
| 170 |
(th->cprev = thinker->cprev)->cnext = th; |
| 171 |
} |
| 172 |
Z_Free(thinker); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
// |
| 177 |
// P_RemoveThinker |
| 178 |
// |
| 179 |
// Deallocation is lazy -- it will not actually be freed |
| 180 |
// until its thinking turn comes up. |
| 181 |
// |
| 182 |
// killough 4/25/98: |
| 183 |
// |
| 184 |
// Instead of marking the function with -1 value cast to a function pointer, |
| 185 |
// set the function to P_RemoveThinkerDelayed(), so that later, it will be |
| 186 |
// removed automatically as part of the thinker process. |
| 187 |
// |
| 188 |
|
| 189 |
void P_RemoveThinker(thinker_t *thinker) |
| 190 |
{ |
| 191 |
R_StopInterpolationIfNeeded(thinker); |
| 192 |
thinker->function = P_RemoveThinkerDelayed; |
| 193 |
|
| 194 |
P_UpdateThinker(thinker); |
| 195 |
} |
| 196 |
|
| 197 |
/* cph 2002/01/13 - iterator for thinker list |
| 198 |
* WARNING: Do not modify thinkers between calls to this functin |
| 199 |
*/ |
| 200 |
thinker_t* P_NextThinker(thinker_t* th, th_class cl) |
| 201 |
{ |
| 202 |
thinker_t* top = &thinkerclasscap[cl]; |
| 203 |
if (!th) th = top; |
| 204 |
th = cl == th_all ? th->next : th->cnext; |
| 205 |
return th == top ? NULL : th; |
| 206 |
} |
| 207 |
|
| 208 |
/* |
| 209 |
* P_SetTarget |
| 210 |
* |
| 211 |
* This function is used to keep track of pointer references to mobj thinkers. |
| 212 |
* In Doom, objects such as lost souls could sometimes be removed despite |
| 213 |
* their still being referenced. In Boom, 'target' mobj fields were tested |
| 214 |
* during each gametic, and any objects pointed to by them would be prevented |
| 215 |
* from being removed. But this was incomplete, and was slow (every mobj was |
| 216 |
* checked during every gametic). Now, we keep a count of the number of |
| 217 |
* references, and delay removal until the count is 0. |
| 218 |
*/ |
| 219 |
|
| 220 |
void P_SetTarget(mobj_t **mop, mobj_t *targ) |
| 221 |
{ |
| 222 |
if (*mop) // If there was a target already, decrease its refcount |
| 223 |
(*mop)->thinker.references--; |
| 224 |
if ((*mop = targ)) // Set new target and if non-NULL, increase its counter |
| 225 |
targ->thinker.references++; |
| 226 |
} |
| 227 |
|
| 228 |
// |
| 229 |
// P_RunThinkers |
| 230 |
// |
| 231 |
// killough 4/25/98: |
| 232 |
// |
| 233 |
// Fix deallocator to stop using "next" pointer after node has been freed |
| 234 |
// (a Doom bug). |
| 235 |
// |
| 236 |
// Process each thinker. For thinkers which are marked deleted, we must |
| 237 |
// load the "next" pointer prior to freeing the node. In Doom, the "next" |
| 238 |
// pointer was loaded AFTER the thinker was freed, which could have caused |
| 239 |
// crashes. |
| 240 |
// |
| 241 |
// But if we are not deleting the thinker, we should reload the "next" |
| 242 |
// pointer after calling the function, in case additional thinkers are |
| 243 |
// added at the end of the list. |
| 244 |
// |
| 245 |
// killough 11/98: |
| 246 |
// |
| 247 |
// Rewritten to delete nodes implicitly, by making currentthinker |
| 248 |
// external and using P_RemoveThinkerDelayed() implicitly. |
| 249 |
// |
| 250 |
|
| 251 |
static void P_RunThinkers (void) |
| 252 |
{ |
| 253 |
for (currentthinker = thinkercap.next; |
| 254 |
currentthinker != &thinkercap; |
| 255 |
currentthinker = currentthinker->next) |
| 256 |
{ |
| 257 |
if (newthinkerpresent) |
| 258 |
R_ActivateThinkerInterpolations(currentthinker); |
| 259 |
if (currentthinker->function) |
| 260 |
currentthinker->function(currentthinker); |
| 261 |
} |
| 262 |
newthinkerpresent = false; |
| 263 |
|
| 264 |
// Dedicated thinkers |
| 265 |
T_MAPMusic(); |
| 266 |
} |
| 267 |
|
| 268 |
void P_CleanThinkers (void) |
| 269 |
{ |
| 270 |
for (currentthinker = thinkercap.next; |
| 271 |
currentthinker != &thinkercap; |
| 272 |
currentthinker = currentthinker->next) |
| 273 |
{ |
| 274 |
if (currentthinker->function == P_RemoveThinkerDelayed) |
| 275 |
currentthinker->function(currentthinker); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
static void P_FrozenTicker (void) |
| 280 |
{ |
| 281 |
int i; |
| 282 |
|
| 283 |
P_MapStart(); |
| 284 |
|
| 285 |
if (gamestate == GS_LEVEL) |
| 286 |
{ |
| 287 |
thinker_t* th; |
| 288 |
mobj_t* mo; |
| 289 |
|
| 290 |
for (i = 0; i < g_maxplayers; i++) |
| 291 |
if (playeringame[i]) |
| 292 |
P_PlayerThink(&players[i]); |
| 293 |
|
| 294 |
for (i = 0; i < g_maxplayers; i++) |
| 295 |
if (playeringame[i]) |
| 296 |
P_MobjThinker(players[i].mo); |
| 297 |
|
| 298 |
for (th = thinkercap.next; th != &thinkercap; th = th->next) |
| 299 |
if (th->function == P_MobjThinker || |
| 300 |
th->function == P_BlasterMobjThinker) |
| 301 |
{ |
| 302 |
mo = (mobj_t *) th; |
| 303 |
|
| 304 |
if (mo->player && mo->player == &players[displayplayer]) |
| 305 |
continue; |
| 306 |
|
| 307 |
mo->PrevX = mo->x; |
| 308 |
mo->PrevY = mo->y; |
| 309 |
mo->PrevZ = mo->z; |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
P_MapEnd(); |
| 314 |
} |
| 315 |
|
| 316 |
// |
| 317 |
// P_Ticker |
| 318 |
// |
| 319 |
|
| 320 |
void P_Ticker (void) |
| 321 |
{ |
| 322 |
int i; |
| 323 |
|
| 324 |
/* pause if in menu and at least one tic has been run |
| 325 |
* |
| 326 |
* killough 9/29/98: note that this ties in with basetic, |
| 327 |
* since G_Ticker does the pausing during recording or |
| 328 |
* playback, and compenates by incrementing basetic. |
| 329 |
* |
| 330 |
* All of this complicated mess is used to preserve demo sync. |
| 331 |
*/ |
| 332 |
|
| 333 |
if (dsda_Paused() || (dsda_PausedViaMenu() && players[consoleplayer].viewz != 1)) |
| 334 |
{ |
| 335 |
P_ResetWalkcam(); |
| 336 |
return; |
| 337 |
} |
| 338 |
|
| 339 |
R_UpdateInterpolations (); |
| 340 |
|
| 341 |
if (dsda_FrozenMode()) |
| 342 |
{ |
| 343 |
P_FrozenTicker(); |
| 344 |
} |
| 345 |
else |
| 346 |
{ |
| 347 |
P_MapStart(); |
| 348 |
|
| 349 |
// not if this is an intermission screen |
| 350 |
if (gamestate == GS_LEVEL) |
| 351 |
for (i = 0; i < g_maxplayers; i++) |
| 352 |
if (playeringame[i]) |
| 353 |
P_PlayerThink(&players[i]); |
| 354 |
|
| 355 |
P_RunThinkers(); |
| 356 |
P_UpdateSpecials(); |
| 357 |
P_AnimateSurfaces(); |
| 358 |
P_RespawnSpecials(); |
| 359 |
P_AmbientSound(); |
| 360 |
|
| 361 |
P_MapEnd(); |
| 362 |
|
| 363 |
dsda_WatchPTickCompleted(); |
| 364 |
} |
| 365 |
|
| 366 |
leveltime++; // for par times |
| 367 |
} |