p_floor.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. // Emacs style mode select -*- C++ -*-
  2. //-----------------------------------------------------------------------------
  3. //
  4. // $Id:$
  5. //
  6. // Copyright (C) 1993-1996 by id Software, Inc.
  7. //
  8. // This source is available for distribution and/or modification
  9. // only under the terms of the DOOM Source Code License as
  10. // published by id Software. All rights reserved.
  11. //
  12. // The source is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // FITNESS FOR A PARTICULAR PURPOSE. See the DOOM Source Code License
  15. // for more details.
  16. //
  17. // $Log:$
  18. //
  19. // DESCRIPTION:
  20. // Floor animation: raising stairs.
  21. //
  22. //-----------------------------------------------------------------------------
  23. static const char
  24. rcsid[] = "$Id: p_floor.c,v 1.4 1997/02/03 16:47:54 b1 Exp $";
  25. #include "z_zone.h"
  26. #include "doomdef.h"
  27. #include "p_local.h"
  28. #include "s_sound.h"
  29. // State.
  30. #include "doomstat.h"
  31. #include "r_state.h"
  32. // Data.
  33. #include "sounds.h"
  34. //
  35. // FLOORS
  36. //
  37. //
  38. // Move a plane (floor or ceiling) and check for crushing
  39. //
  40. result_e
  41. T_MovePlane
  42. ( sector_t* sector,
  43. fixed_t speed,
  44. fixed_t dest,
  45. boolean crush,
  46. int floorOrCeiling,
  47. int direction )
  48. {
  49. boolean flag;
  50. fixed_t lastpos;
  51. switch(floorOrCeiling)
  52. {
  53. case 0:
  54. // FLOOR
  55. switch(direction)
  56. {
  57. case -1:
  58. // DOWN
  59. if (sector->floorheight - speed < dest)
  60. {
  61. lastpos = sector->floorheight;
  62. sector->floorheight = dest;
  63. flag = P_ChangeSector(sector,crush);
  64. if (flag == true)
  65. {
  66. sector->floorheight =lastpos;
  67. P_ChangeSector(sector,crush);
  68. //return crushed;
  69. }
  70. return pastdest;
  71. }
  72. else
  73. {
  74. lastpos = sector->floorheight;
  75. sector->floorheight -= speed;
  76. flag = P_ChangeSector(sector,crush);
  77. if (flag == true)
  78. {
  79. sector->floorheight = lastpos;
  80. P_ChangeSector(sector,crush);
  81. return crushed;
  82. }
  83. }
  84. break;
  85. case 1:
  86. // UP
  87. if (sector->floorheight + speed > dest)
  88. {
  89. lastpos = sector->floorheight;
  90. sector->floorheight = dest;
  91. flag = P_ChangeSector(sector,crush);
  92. if (flag == true)
  93. {
  94. sector->floorheight = lastpos;
  95. P_ChangeSector(sector,crush);
  96. //return crushed;
  97. }
  98. return pastdest;
  99. }
  100. else
  101. {
  102. // COULD GET CRUSHED
  103. lastpos = sector->floorheight;
  104. sector->floorheight += speed;
  105. flag = P_ChangeSector(sector,crush);
  106. if (flag == true)
  107. {
  108. if (crush == true)
  109. return crushed;
  110. sector->floorheight = lastpos;
  111. P_ChangeSector(sector,crush);
  112. return crushed;
  113. }
  114. }
  115. break;
  116. }
  117. break;
  118. case 1:
  119. // CEILING
  120. switch(direction)
  121. {
  122. case -1:
  123. // DOWN
  124. if (sector->ceilingheight - speed < dest)
  125. {
  126. lastpos = sector->ceilingheight;
  127. sector->ceilingheight = dest;
  128. flag = P_ChangeSector(sector,crush);
  129. if (flag == true)
  130. {
  131. sector->ceilingheight = lastpos;
  132. P_ChangeSector(sector,crush);
  133. //return crushed;
  134. }
  135. return pastdest;
  136. }
  137. else
  138. {
  139. // COULD GET CRUSHED
  140. lastpos = sector->ceilingheight;
  141. sector->ceilingheight -= speed;
  142. flag = P_ChangeSector(sector,crush);
  143. if (flag == true)
  144. {
  145. if (crush == true)
  146. return crushed;
  147. sector->ceilingheight = lastpos;
  148. P_ChangeSector(sector,crush);
  149. return crushed;
  150. }
  151. }
  152. break;
  153. case 1:
  154. // UP
  155. if (sector->ceilingheight + speed > dest)
  156. {
  157. lastpos = sector->ceilingheight;
  158. sector->ceilingheight = dest;
  159. flag = P_ChangeSector(sector,crush);
  160. if (flag == true)
  161. {
  162. sector->ceilingheight = lastpos;
  163. P_ChangeSector(sector,crush);
  164. //return crushed;
  165. }
  166. return pastdest;
  167. }
  168. else
  169. {
  170. lastpos = sector->ceilingheight;
  171. sector->ceilingheight += speed;
  172. flag = P_ChangeSector(sector,crush);
  173. // UNUSED
  174. #if 0
  175. if (flag == true)
  176. {
  177. sector->ceilingheight = lastpos;
  178. P_ChangeSector(sector,crush);
  179. return crushed;
  180. }
  181. #endif
  182. }
  183. break;
  184. }
  185. break;
  186. }
  187. return ok;
  188. }
  189. //
  190. // MOVE A FLOOR TO IT'S DESTINATION (UP OR DOWN)
  191. //
  192. void T_MoveFloor(floormove_t* floor)
  193. {
  194. result_e res;
  195. res = T_MovePlane(floor->sector,
  196. floor->speed,
  197. floor->floordestheight,
  198. floor->crush,0,floor->direction);
  199. if (!(leveltime&7))
  200. S_StartSound((mobj_t *)&floor->sector->soundorg,
  201. sfx_stnmov);
  202. if (res == pastdest)
  203. {
  204. floor->sector->specialdata = NULL;
  205. if (floor->direction == 1)
  206. {
  207. switch(floor->type)
  208. {
  209. case donutRaise:
  210. floor->sector->special = floor->newspecial;
  211. floor->sector->floorpic = floor->texture;
  212. default:
  213. break;
  214. }
  215. }
  216. else if (floor->direction == -1)
  217. {
  218. switch(floor->type)
  219. {
  220. case lowerAndChange:
  221. floor->sector->special = floor->newspecial;
  222. floor->sector->floorpic = floor->texture;
  223. default:
  224. break;
  225. }
  226. }
  227. P_RemoveThinker(&floor->thinker);
  228. S_StartSound((mobj_t *)&floor->sector->soundorg,
  229. sfx_pstop);
  230. }
  231. }
  232. //
  233. // HANDLE FLOOR TYPES
  234. //
  235. int
  236. EV_DoFloor
  237. ( line_t* line,
  238. floor_e floortype )
  239. {
  240. int secnum;
  241. int rtn;
  242. int i;
  243. sector_t* sec;
  244. floormove_t* floor;
  245. secnum = -1;
  246. rtn = 0;
  247. while ((secnum = P_FindSectorFromLineTag(line,secnum)) >= 0)
  248. {
  249. sec = &sectors[secnum];
  250. // ALREADY MOVING? IF SO, KEEP GOING...
  251. if (sec->specialdata)
  252. continue;
  253. // new floor thinker
  254. rtn = 1;
  255. floor = Z_Malloc (sizeof(*floor), PU_LEVSPEC, 0);
  256. P_AddThinker (&floor->thinker);
  257. sec->specialdata = floor;
  258. floor->thinker.function.acp1 = (actionf_p1) T_MoveFloor;
  259. floor->type = floortype;
  260. floor->crush = false;
  261. switch(floortype)
  262. {
  263. case lowerFloor:
  264. floor->direction = -1;
  265. floor->sector = sec;
  266. floor->speed = FLOORSPEED;
  267. floor->floordestheight =
  268. P_FindHighestFloorSurrounding(sec);
  269. break;
  270. case lowerFloorToLowest:
  271. floor->direction = -1;
  272. floor->sector = sec;
  273. floor->speed = FLOORSPEED;
  274. floor->floordestheight =
  275. P_FindLowestFloorSurrounding(sec);
  276. break;
  277. case turboLower:
  278. floor->direction = -1;
  279. floor->sector = sec;
  280. floor->speed = FLOORSPEED * 4;
  281. floor->floordestheight =
  282. P_FindHighestFloorSurrounding(sec);
  283. if (floor->floordestheight != sec->floorheight)
  284. floor->floordestheight += 8*FRACUNIT;
  285. break;
  286. case raiseFloorCrush:
  287. floor->crush = true;
  288. case raiseFloor:
  289. floor->direction = 1;
  290. floor->sector = sec;
  291. floor->speed = FLOORSPEED;
  292. floor->floordestheight =
  293. P_FindLowestCeilingSurrounding(sec);
  294. if (floor->floordestheight > sec->ceilingheight)
  295. floor->floordestheight = sec->ceilingheight;
  296. floor->floordestheight -= (8*FRACUNIT)*
  297. (floortype == raiseFloorCrush);
  298. break;
  299. case raiseFloorTurbo:
  300. floor->direction = 1;
  301. floor->sector = sec;
  302. floor->speed = FLOORSPEED*4;
  303. floor->floordestheight =
  304. P_FindNextHighestFloor(sec,sec->floorheight);
  305. break;
  306. case raiseFloorToNearest:
  307. floor->direction = 1;
  308. floor->sector = sec;
  309. floor->speed = FLOORSPEED;
  310. floor->floordestheight =
  311. P_FindNextHighestFloor(sec,sec->floorheight);
  312. break;
  313. case raiseFloor24:
  314. floor->direction = 1;
  315. floor->sector = sec;
  316. floor->speed = FLOORSPEED;
  317. floor->floordestheight = floor->sector->floorheight +
  318. 24 * FRACUNIT;
  319. break;
  320. case raiseFloor512:
  321. floor->direction = 1;
  322. floor->sector = sec;
  323. floor->speed = FLOORSPEED;
  324. floor->floordestheight = floor->sector->floorheight +
  325. 512 * FRACUNIT;
  326. break;
  327. case raiseFloor24AndChange:
  328. floor->direction = 1;
  329. floor->sector = sec;
  330. floor->speed = FLOORSPEED;
  331. floor->floordestheight = floor->sector->floorheight +
  332. 24 * FRACUNIT;
  333. sec->floorpic = line->frontsector->floorpic;
  334. sec->special = line->frontsector->special;
  335. break;
  336. case raiseToTexture:
  337. {
  338. int minsize = MAXINT;
  339. side_t* side;
  340. floor->direction = 1;
  341. floor->sector = sec;
  342. floor->speed = FLOORSPEED;
  343. for (i = 0; i < sec->linecount; i++)
  344. {
  345. if (twoSided (secnum, i) )
  346. {
  347. side = getSide(secnum,i,0);
  348. if (side->bottomtexture >= 0)
  349. if (textureheight[side->bottomtexture] <
  350. minsize)
  351. minsize =
  352. textureheight[side->bottomtexture];
  353. side = getSide(secnum,i,1);
  354. if (side->bottomtexture >= 0)
  355. if (textureheight[side->bottomtexture] <
  356. minsize)
  357. minsize =
  358. textureheight[side->bottomtexture];
  359. }
  360. }
  361. floor->floordestheight =
  362. floor->sector->floorheight + minsize;
  363. }
  364. break;
  365. case lowerAndChange:
  366. floor->direction = -1;
  367. floor->sector = sec;
  368. floor->speed = FLOORSPEED;
  369. floor->floordestheight =
  370. P_FindLowestFloorSurrounding(sec);
  371. floor->texture = sec->floorpic;
  372. for (i = 0; i < sec->linecount; i++)
  373. {
  374. if ( twoSided(secnum, i) )
  375. {
  376. if (getSide(secnum,i,0)->sector-sectors == secnum)
  377. {
  378. sec = getSector(secnum,i,1);
  379. if (sec->floorheight == floor->floordestheight)
  380. {
  381. floor->texture = sec->floorpic;
  382. floor->newspecial = sec->special;
  383. break;
  384. }
  385. }
  386. else
  387. {
  388. sec = getSector(secnum,i,0);
  389. if (sec->floorheight == floor->floordestheight)
  390. {
  391. floor->texture = sec->floorpic;
  392. floor->newspecial = sec->special;
  393. break;
  394. }
  395. }
  396. }
  397. }
  398. default:
  399. break;
  400. }
  401. }
  402. return rtn;
  403. }
  404. //
  405. // BUILD A STAIRCASE!
  406. //
  407. int
  408. EV_BuildStairs
  409. ( line_t* line,
  410. stair_e type )
  411. {
  412. int secnum;
  413. int height;
  414. int i;
  415. int newsecnum;
  416. int texture;
  417. int ok;
  418. int rtn;
  419. sector_t* sec;
  420. sector_t* tsec;
  421. floormove_t* floor;
  422. fixed_t stairsize;
  423. fixed_t speed;
  424. secnum = -1;
  425. rtn = 0;
  426. while ((secnum = P_FindSectorFromLineTag(line,secnum)) >= 0)
  427. {
  428. sec = &sectors[secnum];
  429. // ALREADY MOVING? IF SO, KEEP GOING...
  430. if (sec->specialdata)
  431. continue;
  432. // new floor thinker
  433. rtn = 1;
  434. floor = Z_Malloc (sizeof(*floor), PU_LEVSPEC, 0);
  435. P_AddThinker (&floor->thinker);
  436. sec->specialdata = floor;
  437. floor->thinker.function.acp1 = (actionf_p1) T_MoveFloor;
  438. floor->direction = 1;
  439. floor->sector = sec;
  440. switch(type)
  441. {
  442. case build8:
  443. speed = FLOORSPEED/4;
  444. stairsize = 8*FRACUNIT;
  445. break;
  446. case turbo16:
  447. speed = FLOORSPEED*4;
  448. stairsize = 16*FRACUNIT;
  449. break;
  450. }
  451. floor->speed = speed;
  452. height = sec->floorheight + stairsize;
  453. floor->floordestheight = height;
  454. texture = sec->floorpic;
  455. // Find next sector to raise
  456. // 1. Find 2-sided line with same sector side[0]
  457. // 2. Other side is the next sector to raise
  458. do
  459. {
  460. ok = 0;
  461. for (i = 0;i < sec->linecount;i++)
  462. {
  463. if ( !((sec->lines[i])->flags & ML_TWOSIDED) )
  464. continue;
  465. tsec = (sec->lines[i])->frontsector;
  466. newsecnum = tsec-sectors;
  467. if (secnum != newsecnum)
  468. continue;
  469. tsec = (sec->lines[i])->backsector;
  470. newsecnum = tsec - sectors;
  471. if (tsec->floorpic != texture)
  472. continue;
  473. height += stairsize;
  474. if (tsec->specialdata)
  475. continue;
  476. sec = tsec;
  477. secnum = newsecnum;
  478. floor = Z_Malloc (sizeof(*floor), PU_LEVSPEC, 0);
  479. P_AddThinker (&floor->thinker);
  480. sec->specialdata = floor;
  481. floor->thinker.function.acp1 = (actionf_p1) T_MoveFloor;
  482. floor->direction = 1;
  483. floor->sector = sec;
  484. floor->speed = speed;
  485. floor->floordestheight = height;
  486. ok = 1;
  487. break;
  488. }
  489. } while(ok);
  490. }
  491. return rtn;
  492. }