p_tick.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. #include "doomstat.h"
  34. #include "p_user.h"
  35. #include "p_spec.h"
  36. #include "p_tick.h"
  37. #include "p_map.h"
  38. #include "r_fps.h"
  39. int leveltime;
  40. static boolean newthinkerpresent;
  41. //
  42. // THINKERS
  43. // All thinkers should be allocated by Z_Malloc
  44. // so they can be operated on uniformly.
  45. // The actual structures will vary in size,
  46. // but the first element must be thinker_t.
  47. //
  48. // killough 8/29/98: we maintain several separate threads, each containing
  49. // a special class of thinkers, to allow more efficient searches.
  50. thinker_t thinkerclasscap[th_all+1];
  51. //
  52. // P_InitThinkers
  53. //
  54. void P_InitThinkers(void)
  55. {
  56. int i;
  57. for (i=0; i<NUMTHCLASS; i++) // killough 8/29/98: initialize threaded lists
  58. thinkerclasscap[i].cprev = thinkerclasscap[i].cnext = &thinkerclasscap[i];
  59. thinkercap.prev = thinkercap.next = &thinkercap;
  60. }
  61. //
  62. // killough 8/29/98:
  63. //
  64. // We maintain separate threads of friends and enemies, to permit more
  65. // efficient searches.
  66. //
  67. void P_UpdateThinker(thinker_t *thinker)
  68. {
  69. register thinker_t *th;
  70. // find the class the thinker belongs to
  71. int class =
  72. thinker->function == P_RemoveThinkerDelayed ? th_delete :
  73. thinker->function == P_MobjThinker &&
  74. ((mobj_t *) thinker)->health > 0 &&
  75. (((mobj_t *) thinker)->flags & MF_COUNTKILL ||
  76. ((mobj_t *) thinker)->type == MT_SKULL) ?
  77. ((mobj_t *) thinker)->flags & MF_FRIEND ?
  78. th_friends : th_enemies : th_misc;
  79. {
  80. /* Remove from current thread, if in one */
  81. if ((th = thinker->cnext)!= NULL)
  82. (th->cprev = thinker->cprev)->cnext = th;
  83. }
  84. // Add to appropriate thread
  85. th = &thinkerclasscap[class];
  86. th->cprev->cnext = thinker;
  87. thinker->cnext = th;
  88. thinker->cprev = th->cprev;
  89. th->cprev = thinker;
  90. }
  91. //
  92. // P_AddThinker
  93. // Adds a new thinker at the end of the list.
  94. //
  95. void P_AddThinker(thinker_t* thinker)
  96. {
  97. thinkercap.prev->next = thinker;
  98. thinker->next = &thinkercap;
  99. thinker->prev = thinkercap.prev;
  100. thinkercap.prev = thinker;
  101. thinker->references = 0; // killough 11/98: init reference counter to 0
  102. // killough 8/29/98: set sentinel pointers, and then add to appropriate list
  103. thinker->cnext = thinker->cprev = NULL;
  104. P_UpdateThinker(thinker);
  105. newthinkerpresent = true;
  106. }
  107. //
  108. // killough 11/98:
  109. //
  110. // Make currentthinker external, so that P_RemoveThinkerDelayed
  111. // can adjust currentthinker when thinkers self-remove.
  112. static thinker_t *currentthinker;
  113. //
  114. // P_RemoveThinkerDelayed()
  115. //
  116. // Called automatically as part of the thinker loop in P_RunThinkers(),
  117. // on nodes which are pending deletion.
  118. //
  119. // If this thinker has no more pointers referencing it indirectly,
  120. // remove it, and set currentthinker to one node preceeding it, so
  121. // that the next step in P_RunThinkers() will get its successor.
  122. //
  123. void P_RemoveThinkerDelayed(thinker_t *thinker)
  124. {
  125. if (!thinker->references)
  126. {
  127. { /* Remove from main thinker list */
  128. thinker_t *next = thinker->next;
  129. /* Note that currentthinker is guaranteed to point to us,
  130. * and since we're freeing our memory, we had better change that. So
  131. * point it to thinker->prev, so the iterator will correctly move on to
  132. * thinker->prev->next = thinker->next */
  133. (next->prev = currentthinker = thinker->prev)->next = next;
  134. }
  135. {
  136. /* Remove from current thinker class list */
  137. thinker_t *th = thinker->cnext;
  138. (th->cprev = thinker->cprev)->cnext = th;
  139. }
  140. Z_Free(thinker);
  141. }
  142. }
  143. //
  144. // P_RemoveThinker
  145. //
  146. // Deallocation is lazy -- it will not actually be freed
  147. // until its thinking turn comes up.
  148. //
  149. // killough 4/25/98:
  150. //
  151. // Instead of marking the function with -1 value cast to a function pointer,
  152. // set the function to P_RemoveThinkerDelayed(), so that later, it will be
  153. // removed automatically as part of the thinker process.
  154. //
  155. void P_RemoveThinker(thinker_t *thinker)
  156. {
  157. R_StopInterpolationIfNeeded(thinker);
  158. thinker->function = P_RemoveThinkerDelayed;
  159. P_UpdateThinker(thinker);
  160. }
  161. /* cph 2002/01/13 - iterator for thinker list
  162. * WARNING: Do not modify thinkers between calls to this functin
  163. */
  164. thinker_t* P_NextThinker(thinker_t* th, th_class cl)
  165. {
  166. thinker_t* top = &thinkerclasscap[cl];
  167. if (!th) th = top;
  168. th = cl == th_all ? th->next : th->cnext;
  169. return th == top ? NULL : th;
  170. }
  171. /*
  172. * P_SetTarget
  173. *
  174. * This function is used to keep track of pointer references to mobj thinkers.
  175. * In Doom, objects such as lost souls could sometimes be removed despite
  176. * their still being referenced. In Boom, 'target' mobj fields were tested
  177. * during each gametic, and any objects pointed to by them would be prevented
  178. * from being removed. But this was incomplete, and was slow (every mobj was
  179. * checked during every gametic). Now, we keep a count of the number of
  180. * references, and delay removal until the count is 0.
  181. */
  182. void P_SetTarget(mobj_t **mop, mobj_t *targ)
  183. {
  184. if (*mop) // If there was a target already, decrease its refcount
  185. (*mop)->thinker.references--;
  186. if ((*mop = targ)) // Set new target and if non-NULL, increase its counter
  187. targ->thinker.references++;
  188. }
  189. //
  190. // P_RunThinkers
  191. //
  192. // killough 4/25/98:
  193. //
  194. // Fix deallocator to stop using "next" pointer after node has been freed
  195. // (a Doom bug).
  196. //
  197. // Process each thinker. For thinkers which are marked deleted, we must
  198. // load the "next" pointer prior to freeing the node. In Doom, the "next"
  199. // pointer was loaded AFTER the thinker was freed, which could have caused
  200. // crashes.
  201. //
  202. // But if we are not deleting the thinker, we should reload the "next"
  203. // pointer after calling the function, in case additional thinkers are
  204. // added at the end of the list.
  205. //
  206. // killough 11/98:
  207. //
  208. // Rewritten to delete nodes implicitly, by making currentthinker
  209. // external and using P_RemoveThinkerDelayed() implicitly.
  210. //
  211. static void P_RunThinkers (void)
  212. {
  213. for (currentthinker = thinkercap.next;
  214. currentthinker != &thinkercap;
  215. currentthinker = currentthinker->next)
  216. {
  217. if (newthinkerpresent)
  218. R_ActivateThinkerInterpolations(currentthinker);
  219. if (currentthinker->function)
  220. currentthinker->function(currentthinker);
  221. }
  222. newthinkerpresent = false;
  223. }
  224. //
  225. // P_Ticker
  226. //
  227. void P_Ticker (void)
  228. {
  229. int i;
  230. /* pause if in menu and at least one tic has been run
  231. *
  232. * killough 9/29/98: note that this ties in with basetic,
  233. * since G_Ticker does the pausing during recording or
  234. * playback, and compenates by incrementing basetic.
  235. *
  236. * All of this complicated mess is used to preserve demo sync.
  237. */
  238. if (paused || (menuactive && !demoplayback && !netgame &&
  239. players[consoleplayer].viewz != 1))
  240. return;
  241. R_UpdateInterpolations ();
  242. P_MapStart();
  243. // not if this is an intermission screen
  244. if(gamestate==GS_LEVEL)
  245. for (i=0; i<MAXPLAYERS; i++)
  246. if (playeringame[i])
  247. P_PlayerThink(&players[i]);
  248. P_RunThinkers();
  249. P_UpdateSpecials();
  250. P_RespawnSpecials();
  251. P_MapEnd();
  252. leveltime++; // for par times
  253. }