p_plats.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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 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. * Plats (i.e. elevator platforms) code, raising/lowering.
  31. *
  32. *-----------------------------------------------------------------------------*/
  33. #include "doomstat.h"
  34. #include "m_random.h"
  35. #include "r_main.h"
  36. #include "p_spec.h"
  37. #include "p_tick.h"
  38. #include "s_sound.h"
  39. #include "sounds.h"
  40. platlist_t *activeplats; // killough 2/14/98: made global again
  41. //
  42. // T_PlatRaise()
  43. //
  44. // Action routine to move a plat up and down
  45. //
  46. // Passed a plat structure containing all pertinent information about the move
  47. // No return
  48. //
  49. // jff 02/08/98 all cases with labels beginning with gen added to support
  50. // generalized line type behaviors.
  51. void T_PlatRaise(plat_t* plat)
  52. {
  53. result_e res;
  54. // handle plat moving, up, down, waiting, or in stasis,
  55. switch(plat->status)
  56. {
  57. case up: // plat moving up
  58. res = T_MovePlane(plat->sector,plat->speed,plat->high,plat->crush,0,1);
  59. // if a pure raise type, make the plat moving sound
  60. if (plat->type == raiseAndChange
  61. || plat->type == raiseToNearestAndChange)
  62. {
  63. if (!(leveltime&7))
  64. S_StartSound((mobj_t *)&plat->sector->soundorg, sfx_stnmov);
  65. }
  66. // if encountered an obstacle, and not a crush type, reverse direction
  67. if (res == crushed && (!plat->crush))
  68. {
  69. plat->count = plat->wait;
  70. plat->status = down;
  71. S_StartSound((mobj_t *)&plat->sector->soundorg, sfx_pstart);
  72. }
  73. else // else handle reaching end of up stroke
  74. {
  75. if (res == pastdest) // end of stroke
  76. {
  77. // if not an instant toggle type, wait, make plat stop sound
  78. if (plat->type!=toggleUpDn)
  79. {
  80. plat->count = plat->wait;
  81. plat->status = waiting;
  82. S_StartSound((mobj_t *)&plat->sector->soundorg, sfx_pstop);
  83. }
  84. else // else go into stasis awaiting next toggle activation
  85. {
  86. plat->oldstatus = plat->status;//jff 3/14/98 after action wait
  87. plat->status = in_stasis; //for reactivation of toggle
  88. }
  89. // lift types and pure raise types are done at end of up stroke
  90. // only the perpetual type waits then goes back up
  91. switch(plat->type)
  92. {
  93. case blazeDWUS:
  94. case downWaitUpStay:
  95. case raiseAndChange:
  96. case raiseToNearestAndChange:
  97. case genLift:
  98. P_RemoveActivePlat(plat); // killough
  99. default:
  100. break;
  101. }
  102. }
  103. }
  104. break;
  105. case down: // plat moving down
  106. res = T_MovePlane(plat->sector,plat->speed,plat->low,false,0,-1);
  107. // handle reaching end of down stroke
  108. if (res == pastdest)
  109. {
  110. // if not an instant toggle, start waiting, make plat stop sound
  111. if (plat->type!=toggleUpDn) //jff 3/14/98 toggle up down
  112. { // is silent, instant, no waiting
  113. plat->count = plat->wait;
  114. plat->status = waiting;
  115. S_StartSound((mobj_t *)&plat->sector->soundorg,sfx_pstop);
  116. }
  117. else // instant toggles go into stasis awaiting next activation
  118. {
  119. plat->oldstatus = plat->status;//jff 3/14/98 after action wait
  120. plat->status = in_stasis; //for reactivation of toggle
  121. }
  122. //jff 1/26/98 remove the plat if it bounced so it can be tried again
  123. //only affects plats that raise and bounce
  124. //killough 1/31/98: relax compatibility to demo_compatibility
  125. // remove the plat if its a pure raise type
  126. if (!comp[comp_floors])
  127. {
  128. switch(plat->type)
  129. {
  130. case raiseAndChange:
  131. case raiseToNearestAndChange:
  132. P_RemoveActivePlat(plat);
  133. default:
  134. break;
  135. }
  136. }
  137. }
  138. break;
  139. case waiting: // plat is waiting
  140. if (!--plat->count) // downcount and check for delay elapsed
  141. {
  142. if (plat->sector->floorheight == plat->low)
  143. plat->status = up; // if at bottom, start up
  144. else
  145. plat->status = down; // if at top, start down
  146. // make plat start sound
  147. S_StartSound((mobj_t *)&plat->sector->soundorg,sfx_pstart);
  148. }
  149. break; //jff 1/27/98 don't pickup code added later to in_stasis
  150. case in_stasis: // do nothing if in stasis
  151. break;
  152. }
  153. }
  154. //
  155. // EV_DoPlat
  156. //
  157. // Handle Plat linedef types
  158. //
  159. // Passed the linedef that activated the plat, the type of plat action,
  160. // and for some plat types, an amount to raise
  161. // Returns true if a thinker is started, or restarted from stasis
  162. //
  163. int EV_DoPlat
  164. ( line_t* line,
  165. plattype_e type,
  166. int amount )
  167. {
  168. plat_t* plat;
  169. int secnum;
  170. int rtn;
  171. sector_t* sec;
  172. secnum = -1;
  173. rtn = 0;
  174. // Activate all <type> plats that are in_stasis
  175. switch(type)
  176. {
  177. case perpetualRaise:
  178. P_ActivateInStasis(line->tag);
  179. break;
  180. case toggleUpDn:
  181. P_ActivateInStasis(line->tag);
  182. rtn=1;
  183. break;
  184. default:
  185. break;
  186. }
  187. // act on all sectors tagged the same as the activating linedef
  188. while ((secnum = P_FindSectorFromLineTag(line,secnum)) >= 0)
  189. {
  190. sec = &sectors[secnum];
  191. // don't start a second floor function if already moving
  192. if (P_SectorActive(floor_special,sec)) //jff 2/23/98 multiple thinkers
  193. continue;
  194. // Create a thinker
  195. rtn = 1;
  196. plat = Z_Malloc( sizeof(*plat), PU_LEVSPEC, 0);
  197. memset(plat, 0, sizeof(*plat));
  198. P_AddThinker(&plat->thinker);
  199. plat->type = type;
  200. plat->sector = sec;
  201. plat->sector->floordata = plat; //jff 2/23/98 multiple thinkers
  202. plat->thinker.function = T_PlatRaise;
  203. plat->crush = false;
  204. plat->tag = line->tag;
  205. //jff 1/26/98 Avoid raise plat bouncing a head off a ceiling and then
  206. //going down forever -- default low to plat height when triggered
  207. plat->low = sec->floorheight;
  208. // set up plat according to type
  209. switch(type)
  210. {
  211. case raiseToNearestAndChange:
  212. plat->speed = PLATSPEED/2;
  213. sec->floorpic = sides[line->sidenum[0]].sector->floorpic;
  214. plat->high = P_FindNextHighestFloor(sec,sec->floorheight);
  215. plat->wait = 0;
  216. plat->status = up;
  217. sec->special = 0;
  218. //jff 3/14/98 clear old field as well
  219. sec->oldspecial = 0;
  220. S_StartSound((mobj_t *)&sec->soundorg,sfx_stnmov);
  221. break;
  222. case raiseAndChange:
  223. plat->speed = PLATSPEED/2;
  224. sec->floorpic = sides[line->sidenum[0]].sector->floorpic;
  225. plat->high = sec->floorheight + amount*FRACUNIT;
  226. plat->wait = 0;
  227. plat->status = up;
  228. S_StartSound((mobj_t *)&sec->soundorg,sfx_stnmov);
  229. break;
  230. case downWaitUpStay:
  231. plat->speed = PLATSPEED * 4;
  232. plat->low = P_FindLowestFloorSurrounding(sec);
  233. if (plat->low > sec->floorheight)
  234. plat->low = sec->floorheight;
  235. plat->high = sec->floorheight;
  236. plat->wait = 35*PLATWAIT;
  237. plat->status = down;
  238. S_StartSound((mobj_t *)&sec->soundorg,sfx_pstart);
  239. break;
  240. case blazeDWUS:
  241. plat->speed = PLATSPEED * 8;
  242. plat->low = P_FindLowestFloorSurrounding(sec);
  243. if (plat->low > sec->floorheight)
  244. plat->low = sec->floorheight;
  245. plat->high = sec->floorheight;
  246. plat->wait = 35*PLATWAIT;
  247. plat->status = down;
  248. S_StartSound((mobj_t *)&sec->soundorg,sfx_pstart);
  249. break;
  250. case perpetualRaise:
  251. plat->speed = PLATSPEED;
  252. plat->low = P_FindLowestFloorSurrounding(sec);
  253. if (plat->low > sec->floorheight)
  254. plat->low = sec->floorheight;
  255. plat->high = P_FindHighestFloorSurrounding(sec);
  256. if (plat->high < sec->floorheight)
  257. plat->high = sec->floorheight;
  258. plat->wait = 35*PLATWAIT;
  259. plat->status = P_Random(pr_plats)&1;
  260. S_StartSound((mobj_t *)&sec->soundorg,sfx_pstart);
  261. break;
  262. case toggleUpDn: //jff 3/14/98 add new type to support instant toggle
  263. plat->speed = PLATSPEED; //not used
  264. plat->wait = 35*PLATWAIT; //not used
  265. plat->crush = true; //jff 3/14/98 crush anything in the way
  266. // set up toggling between ceiling, floor inclusive
  267. plat->low = sec->ceilingheight;
  268. plat->high = sec->floorheight;
  269. plat->status = down;
  270. break;
  271. default:
  272. break;
  273. }
  274. P_AddActivePlat(plat); // add plat to list of active plats
  275. }
  276. return rtn;
  277. }
  278. // The following were all rewritten by Lee Killough
  279. // to use the new structure which places no limits
  280. // on active plats. It also avoids spending as much
  281. // time searching for active plats. Previously a
  282. // fixed-size array was used, with NULL indicating
  283. // empty entries, while now a doubly-linked list
  284. // is used.
  285. //
  286. // P_ActivateInStasis()
  287. //
  288. // Activate a plat that has been put in stasis
  289. // (stopped perpetual floor, instant floor/ceil toggle)
  290. //
  291. // Passed the tag of the plat that should be reactivated
  292. // Returns nothing
  293. //
  294. void P_ActivateInStasis(int tag)
  295. {
  296. platlist_t *pl;
  297. for (pl=activeplats; pl; pl=pl->next) // search the active plats
  298. {
  299. plat_t *plat = pl->plat; // for one in stasis with right tag
  300. if (plat->tag == tag && plat->status == in_stasis)
  301. {
  302. if (plat->type==toggleUpDn) //jff 3/14/98 reactivate toggle type
  303. plat->status = plat->oldstatus==up? down : up;
  304. else
  305. plat->status = plat->oldstatus;
  306. plat->thinker.function = T_PlatRaise;
  307. }
  308. }
  309. }
  310. //
  311. // EV_StopPlat()
  312. //
  313. // Handler for "stop perpetual floor" linedef type
  314. //
  315. // Passed the linedef that stopped the plat
  316. // Returns true if a plat was put in stasis
  317. //
  318. // jff 2/12/98 added int return value, fixed return
  319. //
  320. int EV_StopPlat(line_t* line)
  321. {
  322. platlist_t *pl;
  323. for (pl=activeplats; pl; pl=pl->next) // search the active plats
  324. {
  325. plat_t *plat = pl->plat; // for one with the tag not in stasis
  326. if (plat->status != in_stasis && plat->tag == line->tag)
  327. {
  328. plat->oldstatus = plat->status; // put it in stasis
  329. plat->status = in_stasis;
  330. plat->thinker.function = NULL;
  331. }
  332. }
  333. return 1;
  334. }
  335. //
  336. // P_AddActivePlat()
  337. //
  338. // Add a plat to the head of the active plat list
  339. //
  340. // Passed a pointer to the plat to add
  341. // Returns nothing
  342. //
  343. void P_AddActivePlat(plat_t* plat)
  344. {
  345. platlist_t *list = malloc(sizeof *list);
  346. list->plat = plat;
  347. plat->list = list;
  348. if ((list->next = activeplats))
  349. list->next->prev = &list->next;
  350. list->prev = &activeplats;
  351. activeplats = list;
  352. }
  353. //
  354. // P_RemoveActivePlat()
  355. //
  356. // Remove a plat from the active plat list
  357. //
  358. // Passed a pointer to the plat to remove
  359. // Returns nothing
  360. //
  361. void P_RemoveActivePlat(plat_t* plat)
  362. {
  363. platlist_t *list = plat->list;
  364. plat->sector->floordata = NULL; //jff 2/23/98 multiple thinkers
  365. P_RemoveThinker(&plat->thinker);
  366. if ((*list->prev = list->next))
  367. list->next->prev = list->prev;
  368. free(list);
  369. }
  370. //
  371. // P_RemoveAllActivePlats()
  372. //
  373. // Remove all plats from the active plat list
  374. //
  375. // Passed nothing, returns nothing
  376. //
  377. void P_RemoveAllActivePlats(void)
  378. {
  379. while (activeplats)
  380. {
  381. platlist_t *next = activeplats->next;
  382. free(activeplats);
  383. activeplats = next;
  384. }
  385. }