PO_MAN.C 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483
  1. //**************************************************************************
  2. //**
  3. //** PO_MAN.C : Heretic 2 : Raven Software, Corp.
  4. //**
  5. //** $RCSfile: po_man.c,v $
  6. //** $Revision: 1.22 $
  7. //** $Date: 95/09/28 18:20:56 $
  8. //** $Author: cjr $
  9. //**
  10. //**************************************************************************
  11. // HEADER FILES ------------------------------------------------------------
  12. #include "h2def.h"
  13. #include "p_local.h"
  14. #include "r_local.h"
  15. // MACROS ------------------------------------------------------------------
  16. #define PO_MAXPOLYSEGS 64
  17. // TYPES -------------------------------------------------------------------
  18. // EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
  19. boolean PO_MovePolyobj(int num, int x, int y);
  20. boolean PO_RotatePolyobj(int num, angle_t angle);
  21. void PO_Init(int lump);
  22. // PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
  23. static polyobj_t *GetPolyobj(int polyNum);
  24. static int GetPolyobjMirror(int poly);
  25. static void ThrustMobj(mobj_t *mobj, seg_t *seg, polyobj_t *po);
  26. static void UpdateSegBBox(seg_t *seg);
  27. static void RotatePt(int an, fixed_t *x, fixed_t *y, fixed_t startSpotX,
  28. fixed_t startSpotY);
  29. static void UnLinkPolyobj(polyobj_t *po);
  30. static void LinkPolyobj(polyobj_t *po);
  31. static boolean CheckMobjBlocking(seg_t *seg, polyobj_t *po);
  32. static void InitBlockMap(void);
  33. static void IterFindPolySegs(int x, int y, seg_t **segList);
  34. static void SpawnPolyobj(int index, int tag, boolean crush);
  35. static void TranslateToStartSpot(int tag, int originX, int originY);
  36. // EXTERNAL DATA DECLARATIONS ----------------------------------------------
  37. extern seg_t *segs;
  38. // PUBLIC DATA DEFINITIONS -------------------------------------------------
  39. polyblock_t **PolyBlockMap;
  40. polyobj_t *polyobjs; // list of all poly-objects on the level
  41. int po_NumPolyobjs;
  42. // PRIVATE DATA DEFINITIONS ------------------------------------------------
  43. static int PolySegCount;
  44. static fixed_t PolyStartX;
  45. static fixed_t PolyStartY;
  46. // CODE --------------------------------------------------------------------
  47. // ===== Polyobj Event Code =====
  48. //==========================================================================
  49. //
  50. // T_RotatePoly
  51. //
  52. //==========================================================================
  53. void T_RotatePoly(polyevent_t *pe)
  54. {
  55. int absSpeed;
  56. polyobj_t *poly;
  57. if(PO_RotatePolyobj(pe->polyobj, pe->speed))
  58. {
  59. absSpeed = abs(pe->speed);
  60. if(pe->dist == -1)
  61. { // perpetual polyobj
  62. return;
  63. }
  64. pe->dist -= absSpeed;
  65. if(pe->dist <= 0)
  66. {
  67. poly = GetPolyobj(pe->polyobj);
  68. if(poly->specialdata == pe)
  69. {
  70. poly->specialdata = NULL;
  71. }
  72. SN_StopSequence((mobj_t *)&poly->startSpot);
  73. P_PolyobjFinished(poly->tag);
  74. P_RemoveThinker(&pe->thinker);
  75. }
  76. if(pe->dist < absSpeed)
  77. {
  78. pe->speed = pe->dist*(pe->speed < 0 ? -1 : 1);
  79. }
  80. }
  81. }
  82. //==========================================================================
  83. //
  84. // EV_RotatePoly
  85. //
  86. //==========================================================================
  87. boolean EV_RotatePoly(line_t *line, byte *args, int direction, boolean
  88. overRide)
  89. {
  90. int mirror;
  91. int polyNum;
  92. polyevent_t *pe;
  93. polyobj_t *poly;
  94. polyNum = args[0];
  95. if(poly = GetPolyobj(polyNum))
  96. {
  97. if(poly->specialdata && !overRide)
  98. { // poly is already moving
  99. return false;
  100. }
  101. }
  102. else
  103. {
  104. I_Error("EV_RotatePoly: Invalid polyobj num: %d\n", polyNum);
  105. }
  106. pe = Z_Malloc(sizeof(polyevent_t), PU_LEVSPEC, 0);
  107. P_AddThinker(&pe->thinker);
  108. pe->thinker.function = T_RotatePoly;
  109. pe->polyobj = polyNum;
  110. if(args[2])
  111. {
  112. if(args[2] == 255)
  113. {
  114. pe->dist = -1;
  115. }
  116. else
  117. {
  118. pe->dist = args[2]*(ANGLE_90/64); // Angle
  119. }
  120. }
  121. else
  122. {
  123. pe->dist = ANGLE_MAX-1;
  124. }
  125. pe->speed = (args[1]*direction*(ANGLE_90/64))>>3;
  126. poly->specialdata = pe;
  127. SN_StartSequence((mobj_t *)&poly->startSpot, SEQ_DOOR_STONE+
  128. poly->seqType);
  129. while(mirror = GetPolyobjMirror(polyNum))
  130. {
  131. poly = GetPolyobj(mirror);
  132. if(poly && poly->specialdata && !overRide)
  133. { // mirroring poly is already in motion
  134. break;
  135. }
  136. pe = Z_Malloc(sizeof(polyevent_t), PU_LEVSPEC, 0);
  137. P_AddThinker(&pe->thinker);
  138. pe->thinker.function = T_RotatePoly;
  139. poly->specialdata = pe;
  140. pe->polyobj = mirror;
  141. if(args[2])
  142. {
  143. if(args[2] == 255)
  144. {
  145. pe->dist = -1;
  146. }
  147. else
  148. {
  149. pe->dist = args[2]*(ANGLE_90/64); // Angle
  150. }
  151. }
  152. else
  153. {
  154. pe->dist = ANGLE_MAX-1;
  155. }
  156. if(poly = GetPolyobj(polyNum))
  157. {
  158. poly->specialdata = pe;
  159. }
  160. else
  161. {
  162. I_Error("EV_RotatePoly: Invalid polyobj num: %d\n", polyNum);
  163. }
  164. direction = -direction;
  165. pe->speed = (args[1]*direction*(ANGLE_90/64))>>3;
  166. polyNum = mirror;
  167. SN_StartSequence((mobj_t *)&poly->startSpot, SEQ_DOOR_STONE+
  168. poly->seqType);
  169. }
  170. return true;
  171. }
  172. //==========================================================================
  173. //
  174. // T_MovePoly
  175. //
  176. //==========================================================================
  177. void T_MovePoly(polyevent_t *pe)
  178. {
  179. int absSpeed;
  180. polyobj_t *poly;
  181. if(PO_MovePolyobj(pe->polyobj, pe->xSpeed, pe->ySpeed))
  182. {
  183. absSpeed = abs(pe->speed);
  184. pe->dist -= absSpeed;
  185. if(pe->dist <= 0)
  186. {
  187. poly = GetPolyobj(pe->polyobj);
  188. if(poly->specialdata == pe)
  189. {
  190. poly->specialdata = NULL;
  191. }
  192. SN_StopSequence((mobj_t *)&poly->startSpot);
  193. P_PolyobjFinished(poly->tag);
  194. P_RemoveThinker(&pe->thinker);
  195. }
  196. if(pe->dist < absSpeed)
  197. {
  198. pe->speed = pe->dist*(pe->speed < 0 ? -1 : 1);
  199. pe->xSpeed = FixedMul(pe->speed, finecosine[pe->angle]);
  200. pe->ySpeed = FixedMul(pe->speed, finesine[pe->angle]);
  201. }
  202. }
  203. }
  204. //==========================================================================
  205. //
  206. // EV_MovePoly
  207. //
  208. //==========================================================================
  209. boolean EV_MovePoly(line_t *line, byte *args, boolean timesEight, boolean
  210. overRide)
  211. {
  212. int mirror;
  213. int polyNum;
  214. polyevent_t *pe;
  215. polyobj_t *poly;
  216. angle_t an;
  217. polyNum = args[0];
  218. if(poly = GetPolyobj(polyNum))
  219. {
  220. if(poly->specialdata && !overRide)
  221. { // poly is already moving
  222. return false;
  223. }
  224. }
  225. else
  226. {
  227. I_Error("EV_MovePoly: Invalid polyobj num: %d\n", polyNum);
  228. }
  229. pe = Z_Malloc(sizeof(polyevent_t), PU_LEVSPEC, 0);
  230. P_AddThinker(&pe->thinker);
  231. pe->thinker.function = T_MovePoly;
  232. pe->polyobj = polyNum;
  233. if(timesEight)
  234. {
  235. pe->dist = args[3]*8*FRACUNIT;
  236. }
  237. else
  238. {
  239. pe->dist = args[3]*FRACUNIT; // Distance
  240. }
  241. pe->speed = args[1]*(FRACUNIT/8);
  242. poly->specialdata = pe;
  243. an = args[2]*(ANGLE_90/64);
  244. pe->angle = an>>ANGLETOFINESHIFT;
  245. pe->xSpeed = FixedMul(pe->speed, finecosine[pe->angle]);
  246. pe->ySpeed = FixedMul(pe->speed, finesine[pe->angle]);
  247. SN_StartSequence((mobj_t *)&poly->startSpot, SEQ_DOOR_STONE+
  248. poly->seqType);
  249. while(mirror = GetPolyobjMirror(polyNum))
  250. {
  251. poly = GetPolyobj(mirror);
  252. if(poly && poly->specialdata && !overRide)
  253. { // mirroring poly is already in motion
  254. break;
  255. }
  256. pe = Z_Malloc(sizeof(polyevent_t), PU_LEVSPEC, 0);
  257. P_AddThinker(&pe->thinker);
  258. pe->thinker.function = T_MovePoly;
  259. pe->polyobj = mirror;
  260. poly->specialdata = pe;
  261. if(timesEight)
  262. {
  263. pe->dist = args[3]*8*FRACUNIT;
  264. }
  265. else
  266. {
  267. pe->dist = args[3]*FRACUNIT; // Distance
  268. }
  269. pe->speed = args[1]*(FRACUNIT/8);
  270. an = an+ANGLE_180; // reverse the angle
  271. pe->angle = an>>ANGLETOFINESHIFT;
  272. pe->xSpeed = FixedMul(pe->speed, finecosine[pe->angle]);
  273. pe->ySpeed = FixedMul(pe->speed, finesine[pe->angle]);
  274. polyNum = mirror;
  275. SN_StartSequence((mobj_t *)&poly->startSpot, SEQ_DOOR_STONE+
  276. poly->seqType);
  277. }
  278. return true;
  279. }
  280. //==========================================================================
  281. //
  282. // T_PolyDoor
  283. //
  284. //==========================================================================
  285. void T_PolyDoor(polydoor_t *pd)
  286. {
  287. int absSpeed;
  288. polyobj_t *poly;
  289. if(pd->tics)
  290. {
  291. if(!--pd->tics)
  292. {
  293. poly = GetPolyobj(pd->polyobj);
  294. SN_StartSequence((mobj_t *)&poly->startSpot, SEQ_DOOR_STONE+
  295. poly->seqType);
  296. }
  297. return;
  298. }
  299. switch(pd->type)
  300. {
  301. case PODOOR_SLIDE:
  302. if(PO_MovePolyobj(pd->polyobj, pd->xSpeed, pd->ySpeed))
  303. {
  304. absSpeed = abs(pd->speed);
  305. pd->dist -= absSpeed;
  306. if(pd->dist <= 0)
  307. {
  308. poly = GetPolyobj(pd->polyobj);
  309. SN_StopSequence((mobj_t *)&poly->startSpot);
  310. if(!pd->close)
  311. {
  312. pd->dist = pd->totalDist;
  313. pd->close = true;
  314. pd->tics = pd->waitTics;
  315. pd->direction = (ANGLE_MAX>>ANGLETOFINESHIFT)-
  316. pd->direction;
  317. pd->xSpeed = -pd->xSpeed;
  318. pd->ySpeed = -pd->ySpeed;
  319. }
  320. else
  321. {
  322. if(poly->specialdata == pd)
  323. {
  324. poly->specialdata = NULL;
  325. }
  326. P_PolyobjFinished(poly->tag);
  327. P_RemoveThinker(&pd->thinker);
  328. }
  329. }
  330. }
  331. else
  332. {
  333. poly = GetPolyobj(pd->polyobj);
  334. if(poly->crush || !pd->close)
  335. { // continue moving if the poly is a crusher, or is opening
  336. return;
  337. }
  338. else
  339. { // open back up
  340. pd->dist = pd->totalDist-pd->dist;
  341. pd->direction = (ANGLE_MAX>>ANGLETOFINESHIFT)-
  342. pd->direction;
  343. pd->xSpeed = -pd->xSpeed;
  344. pd->ySpeed = -pd->ySpeed;
  345. pd->close = false;
  346. SN_StartSequence((mobj_t *)&poly->startSpot,
  347. SEQ_DOOR_STONE+poly->seqType);
  348. }
  349. }
  350. break;
  351. case PODOOR_SWING:
  352. if(PO_RotatePolyobj(pd->polyobj, pd->speed))
  353. {
  354. absSpeed = abs(pd->speed);
  355. if(pd->dist == -1)
  356. { // perpetual polyobj
  357. return;
  358. }
  359. pd->dist -= absSpeed;
  360. if(pd->dist <= 0)
  361. {
  362. poly = GetPolyobj(pd->polyobj);
  363. SN_StopSequence((mobj_t *)&poly->startSpot);
  364. if(!pd->close)
  365. {
  366. pd->dist = pd->totalDist;
  367. pd->close = true;
  368. pd->tics = pd->waitTics;
  369. pd->speed = -pd->speed;
  370. }
  371. else
  372. {
  373. if(poly->specialdata == pd)
  374. {
  375. poly->specialdata = NULL;
  376. }
  377. P_PolyobjFinished(poly->tag);
  378. P_RemoveThinker(&pd->thinker);
  379. }
  380. }
  381. }
  382. else
  383. {
  384. poly = GetPolyobj(pd->polyobj);
  385. if(poly->crush || !pd->close)
  386. { // continue moving if the poly is a crusher, or is opening
  387. return;
  388. }
  389. else
  390. { // open back up and rewait
  391. pd->dist = pd->totalDist-pd->dist;
  392. pd->speed = -pd->speed;
  393. pd->close = false;
  394. SN_StartSequence((mobj_t *)&poly->startSpot,
  395. SEQ_DOOR_STONE+poly->seqType);
  396. }
  397. }
  398. break;
  399. default:
  400. break;
  401. }
  402. }
  403. //==========================================================================
  404. //
  405. // EV_OpenPolyDoor
  406. //
  407. //==========================================================================
  408. boolean EV_OpenPolyDoor(line_t *line, byte *args, podoortype_t type)
  409. {
  410. int mirror;
  411. int polyNum;
  412. polydoor_t *pd;
  413. polyobj_t *poly;
  414. angle_t an;
  415. polyNum = args[0];
  416. if(poly = GetPolyobj(polyNum))
  417. {
  418. if(poly->specialdata)
  419. { // poly is already moving
  420. return false;
  421. }
  422. }
  423. else
  424. {
  425. I_Error("EV_OpenPolyDoor: Invalid polyobj num: %d\n", polyNum);
  426. }
  427. pd = Z_Malloc(sizeof(polydoor_t), PU_LEVSPEC, 0);
  428. memset(pd, 0, sizeof(polydoor_t));
  429. P_AddThinker(&pd->thinker);
  430. pd->thinker.function = T_PolyDoor;
  431. pd->type = type;
  432. pd->polyobj = polyNum;
  433. if(type == PODOOR_SLIDE)
  434. {
  435. pd->waitTics = args[4];
  436. pd->speed = args[1]*(FRACUNIT/8);
  437. pd->totalDist = args[3]*FRACUNIT; // Distance
  438. pd->dist = pd->totalDist;
  439. an = args[2]*(ANGLE_90/64);
  440. pd->direction = an>>ANGLETOFINESHIFT;
  441. pd->xSpeed = FixedMul(pd->speed, finecosine[pd->direction]);
  442. pd->ySpeed = FixedMul(pd->speed, finesine[pd->direction]);
  443. SN_StartSequence((mobj_t *)&poly->startSpot, SEQ_DOOR_STONE+
  444. poly->seqType);
  445. }
  446. else if(type == PODOOR_SWING)
  447. {
  448. pd->waitTics = args[3];
  449. pd->direction = 1; // ADD: PODOOR_SWINGL, PODOOR_SWINGR
  450. pd->speed = (args[1]*pd->direction*(ANGLE_90/64))>>3;
  451. pd->totalDist = args[2]*(ANGLE_90/64);
  452. pd->dist = pd->totalDist;
  453. SN_StartSequence((mobj_t *)&poly->startSpot, SEQ_DOOR_STONE+
  454. poly->seqType);
  455. }
  456. poly->specialdata = pd;
  457. while(mirror = GetPolyobjMirror(polyNum))
  458. {
  459. poly = GetPolyobj(mirror);
  460. if(poly && poly->specialdata)
  461. { // mirroring poly is already in motion
  462. break;
  463. }
  464. pd = Z_Malloc(sizeof(polydoor_t), PU_LEVSPEC, 0);
  465. memset(pd, 0, sizeof(polydoor_t));
  466. P_AddThinker(&pd->thinker);
  467. pd->thinker.function = T_PolyDoor;
  468. pd->polyobj = mirror;
  469. pd->type = type;
  470. poly->specialdata = pd;
  471. if(type == PODOOR_SLIDE)
  472. {
  473. pd->waitTics = args[4];
  474. pd->speed = args[1]*(FRACUNIT/8);
  475. pd->totalDist = args[3]*FRACUNIT; // Distance
  476. pd->dist = pd->totalDist;
  477. an = an+ANGLE_180; // reverse the angle
  478. pd->direction = an>>ANGLETOFINESHIFT;
  479. pd->xSpeed = FixedMul(pd->speed, finecosine[pd->direction]);
  480. pd->ySpeed = FixedMul(pd->speed, finesine[pd->direction]);
  481. SN_StartSequence((mobj_t *)&poly->startSpot, SEQ_DOOR_STONE+
  482. poly->seqType);
  483. }
  484. else if(type == PODOOR_SWING)
  485. {
  486. pd->waitTics = args[3];
  487. pd->direction = -1; // ADD: same as above
  488. pd->speed = (args[1]*pd->direction*(ANGLE_90/64))>>3;
  489. pd->totalDist = args[2]*(ANGLE_90/64);
  490. pd->dist = pd->totalDist;
  491. SN_StartSequence((mobj_t *)&poly->startSpot, SEQ_DOOR_STONE+
  492. poly->seqType);
  493. }
  494. polyNum = mirror;
  495. }
  496. return true;
  497. }
  498. // ===== Higher Level Poly Interface code =====
  499. //==========================================================================
  500. //
  501. // GetPolyobj
  502. //
  503. //==========================================================================
  504. static polyobj_t *GetPolyobj(int polyNum)
  505. {
  506. int i;
  507. for(i = 0; i < po_NumPolyobjs; i++)
  508. {
  509. if(polyobjs[i].tag == polyNum)
  510. {
  511. return &polyobjs[i];
  512. }
  513. }
  514. return NULL;
  515. }
  516. //==========================================================================
  517. //
  518. // GetPolyobjMirror
  519. //
  520. //==========================================================================
  521. static int GetPolyobjMirror(int poly)
  522. {
  523. int i;
  524. for(i = 0; i < po_NumPolyobjs; i++)
  525. {
  526. if(polyobjs[i].tag == poly)
  527. {
  528. return((*polyobjs[i].segs)->linedef->arg2);
  529. }
  530. }
  531. return 0;
  532. }
  533. //==========================================================================
  534. //
  535. // ThrustMobj
  536. //
  537. //==========================================================================
  538. static void ThrustMobj(mobj_t *mobj, seg_t *seg, polyobj_t *po)
  539. {
  540. int thrustAngle;
  541. int thrustX;
  542. int thrustY;
  543. polyevent_t *pe;
  544. int force;
  545. if(!(mobj->flags&MF_SHOOTABLE) && !mobj->player)
  546. {
  547. return;
  548. }
  549. thrustAngle = (seg->angle-ANGLE_90)>>ANGLETOFINESHIFT;
  550. pe = po->specialdata;
  551. if(pe)
  552. {
  553. if(pe->thinker.function == T_RotatePoly)
  554. {
  555. force = pe->speed>>8;
  556. }
  557. else
  558. {
  559. force = pe->speed>>3;
  560. }
  561. if(force < FRACUNIT)
  562. {
  563. force = FRACUNIT;
  564. }
  565. else if(force > 4*FRACUNIT)
  566. {
  567. force = 4*FRACUNIT;
  568. }
  569. }
  570. else
  571. {
  572. force = FRACUNIT;
  573. }
  574. thrustX = FixedMul(force, finecosine[thrustAngle]);
  575. thrustY = FixedMul(force, finesine[thrustAngle]);
  576. mobj->momx += thrustX;
  577. mobj->momy += thrustY;
  578. if(po->crush)
  579. {
  580. if(!P_CheckPosition(mobj, mobj->x+thrustX, mobj->y+thrustY))
  581. {
  582. P_DamageMobj(mobj, NULL, NULL, 3);
  583. }
  584. }
  585. }
  586. //==========================================================================
  587. //
  588. // UpdateSegBBox
  589. //
  590. //==========================================================================
  591. static void UpdateSegBBox(seg_t *seg)
  592. {
  593. line_t *line;
  594. line = seg->linedef;
  595. if(seg->v1->x < seg->v2->x)
  596. {
  597. line->bbox[BOXLEFT] = seg->v1->x;
  598. line->bbox[BOXRIGHT] = seg->v2->x;
  599. }
  600. else
  601. {
  602. line->bbox[BOXLEFT] = seg->v2->x;
  603. line->bbox[BOXRIGHT] = seg->v1->x;
  604. }
  605. if(seg->v1->y < seg->v2->y)
  606. {
  607. line->bbox[BOXBOTTOM] = seg->v1->y;
  608. line->bbox[BOXTOP] = seg->v2->y;
  609. }
  610. else
  611. {
  612. line->bbox[BOXBOTTOM] = seg->v2->y;
  613. line->bbox[BOXTOP] = seg->v1->y;
  614. }
  615. // Update the line's slopetype
  616. line->dx = line->v2->x - line->v1->x;
  617. line->dy = line->v2->y - line->v1->y;
  618. if(!line->dx)
  619. {
  620. line->slopetype = ST_VERTICAL;
  621. }
  622. else if(!line->dy)
  623. {
  624. line->slopetype = ST_HORIZONTAL;
  625. }
  626. else
  627. {
  628. if(FixedDiv(line->dy, line->dx) > 0)
  629. {
  630. line->slopetype = ST_POSITIVE;
  631. }
  632. else
  633. {
  634. line->slopetype = ST_NEGATIVE;
  635. }
  636. }
  637. }
  638. //==========================================================================
  639. //
  640. // PO_MovePolyobj
  641. //
  642. //==========================================================================
  643. boolean PO_MovePolyobj(int num, int x, int y)
  644. {
  645. int count;
  646. seg_t **segList;
  647. seg_t **veryTempSeg;
  648. polyobj_t *po;
  649. vertex_t *prevPts;
  650. boolean blocked;
  651. if(!(po = GetPolyobj(num)))
  652. {
  653. I_Error("PO_MovePolyobj: Invalid polyobj number: %d\n", num);
  654. }
  655. UnLinkPolyobj(po);
  656. segList = po->segs;
  657. prevPts = po->prevPts;
  658. blocked = false;
  659. validcount++;
  660. for(count = po->numsegs; count; count--, segList++, prevPts++)
  661. {
  662. if((*segList)->linedef->validcount != validcount)
  663. {
  664. (*segList)->linedef->bbox[BOXTOP] += y;
  665. (*segList)->linedef->bbox[BOXBOTTOM] += y;
  666. (*segList)->linedef->bbox[BOXLEFT] += x;
  667. (*segList)->linedef->bbox[BOXRIGHT] += x;
  668. (*segList)->linedef->validcount = validcount;
  669. }
  670. for(veryTempSeg = po->segs; veryTempSeg != segList;
  671. veryTempSeg++)
  672. {
  673. if((*veryTempSeg)->v1 == (*segList)->v1)
  674. {
  675. break;
  676. }
  677. }
  678. if(veryTempSeg == segList)
  679. {
  680. (*segList)->v1->x += x;
  681. (*segList)->v1->y += y;
  682. }
  683. (*prevPts).x += x; // previous points are unique for each seg
  684. (*prevPts).y += y;
  685. }
  686. segList = po->segs;
  687. for(count = po->numsegs; count; count--, segList++)
  688. {
  689. if(CheckMobjBlocking(*segList, po))
  690. {
  691. blocked = true;
  692. }
  693. }
  694. if(blocked)
  695. {
  696. count = po->numsegs;
  697. segList = po->segs;
  698. prevPts = po->prevPts;
  699. validcount++;
  700. while(count--)
  701. {
  702. if((*segList)->linedef->validcount != validcount)
  703. {
  704. (*segList)->linedef->bbox[BOXTOP] -= y;
  705. (*segList)->linedef->bbox[BOXBOTTOM] -= y;
  706. (*segList)->linedef->bbox[BOXLEFT] -= x;
  707. (*segList)->linedef->bbox[BOXRIGHT] -= x;
  708. (*segList)->linedef->validcount = validcount;
  709. }
  710. for(veryTempSeg = po->segs; veryTempSeg != segList;
  711. veryTempSeg++)
  712. {
  713. if((*veryTempSeg)->v1 == (*segList)->v1)
  714. {
  715. break;
  716. }
  717. }
  718. if(veryTempSeg == segList)
  719. {
  720. (*segList)->v1->x -= x;
  721. (*segList)->v1->y -= y;
  722. }
  723. (*prevPts).x -= x;
  724. (*prevPts).y -= y;
  725. segList++;
  726. prevPts++;
  727. }
  728. LinkPolyobj(po);
  729. return false;
  730. }
  731. po->startSpot.x += x;
  732. po->startSpot.y += y;
  733. LinkPolyobj(po);
  734. return true;
  735. }
  736. //==========================================================================
  737. //
  738. // RotatePt
  739. //
  740. //==========================================================================
  741. static void RotatePt(int an, fixed_t *x, fixed_t *y, fixed_t startSpotX, fixed_t startSpotY)
  742. {
  743. fixed_t trx, try;
  744. fixed_t gxt, gyt;
  745. trx = *x;
  746. try = *y;
  747. gxt = FixedMul(trx, finecosine[an]);
  748. gyt = FixedMul(try, finesine[an]);
  749. *x = (gxt-gyt)+startSpotX;
  750. gxt = FixedMul(trx, finesine[an]);
  751. gyt = FixedMul(try, finecosine[an]);
  752. *y = (gyt+gxt)+startSpotY;
  753. }
  754. //==========================================================================
  755. //
  756. // PO_RotatePolyobj
  757. //
  758. //==========================================================================
  759. boolean PO_RotatePolyobj(int num, angle_t angle)
  760. {
  761. int count;
  762. seg_t **segList;
  763. vertex_t *originalPts;
  764. vertex_t *prevPts;
  765. int an;
  766. polyobj_t *po;
  767. boolean blocked;
  768. if(!(po = GetPolyobj(num)))
  769. {
  770. I_Error("PO_RotatePolyobj: Invalid polyobj number: %d\n", num);
  771. }
  772. an = (po->angle+angle)>>ANGLETOFINESHIFT;
  773. UnLinkPolyobj(po);
  774. segList = po->segs;
  775. originalPts = po->originalPts;
  776. prevPts = po->prevPts;
  777. for(count = po->numsegs; count; count--, segList++, originalPts++,
  778. prevPts++)
  779. {
  780. prevPts->x = (*segList)->v1->x;
  781. prevPts->y = (*segList)->v1->y;
  782. (*segList)->v1->x = originalPts->x;
  783. (*segList)->v1->y = originalPts->y;
  784. RotatePt(an, &(*segList)->v1->x, &(*segList)->v1->y, po->startSpot.x,
  785. po->startSpot.y);
  786. }
  787. segList = po->segs;
  788. blocked = false;
  789. validcount++;
  790. for(count = po->numsegs; count; count--, segList++)
  791. {
  792. if(CheckMobjBlocking(*segList, po))
  793. {
  794. blocked = true;
  795. }
  796. if((*segList)->linedef->validcount != validcount)
  797. {
  798. UpdateSegBBox(*segList);
  799. (*segList)->linedef->validcount = validcount;
  800. }
  801. (*segList)->angle += angle;
  802. }
  803. if(blocked)
  804. {
  805. segList = po->segs;
  806. prevPts = po->prevPts;
  807. for(count = po->numsegs; count; count--, segList++, prevPts++)
  808. {
  809. (*segList)->v1->x = prevPts->x;
  810. (*segList)->v1->y = prevPts->y;
  811. }
  812. segList = po->segs;
  813. validcount++;
  814. for(count = po->numsegs; count; count--, segList++, prevPts++)
  815. {
  816. if((*segList)->linedef->validcount != validcount)
  817. {
  818. UpdateSegBBox(*segList);
  819. (*segList)->linedef->validcount = validcount;
  820. }
  821. (*segList)->angle -= angle;
  822. }
  823. LinkPolyobj(po);
  824. return false;
  825. }
  826. po->angle += angle;
  827. LinkPolyobj(po);
  828. return true;
  829. }
  830. //==========================================================================
  831. //
  832. // UnLinkPolyobj
  833. //
  834. //==========================================================================
  835. static void UnLinkPolyobj(polyobj_t *po)
  836. {
  837. polyblock_t *link;
  838. int i, j;
  839. int index;
  840. // remove the polyobj from each blockmap section
  841. for(j = po->bbox[BOXBOTTOM]; j <= po->bbox[BOXTOP]; j++)
  842. {
  843. index = j*bmapwidth;
  844. for(i = po->bbox[BOXLEFT]; i <= po->bbox[BOXRIGHT]; i++)
  845. {
  846. if(i >= 0 && i < bmapwidth && j >= 0 && j < bmapheight)
  847. {
  848. link = PolyBlockMap[index+i];
  849. while(link != NULL && link->polyobj != po)
  850. {
  851. link = link->next;
  852. }
  853. if(link == NULL)
  854. { // polyobj not located in the link cell
  855. continue;
  856. }
  857. link->polyobj = NULL;
  858. }
  859. }
  860. }
  861. }
  862. //==========================================================================
  863. //
  864. // LinkPolyobj
  865. //
  866. //==========================================================================
  867. static void LinkPolyobj(polyobj_t *po)
  868. {
  869. int leftX, rightX;
  870. int topY, bottomY;
  871. seg_t **tempSeg;
  872. polyblock_t **link;
  873. polyblock_t *tempLink;
  874. int i, j;
  875. // calculate the polyobj bbox
  876. tempSeg = po->segs;
  877. rightX = leftX = (*tempSeg)->v1->x;
  878. topY = bottomY = (*tempSeg)->v1->y;
  879. for(i = 0; i < po->numsegs; i++, tempSeg++)
  880. {
  881. if((*tempSeg)->v1->x > rightX)
  882. {
  883. rightX = (*tempSeg)->v1->x;
  884. }
  885. if((*tempSeg)->v1->x < leftX)
  886. {
  887. leftX = (*tempSeg)->v1->x;
  888. }
  889. if((*tempSeg)->v1->y > topY)
  890. {
  891. topY = (*tempSeg)->v1->y;
  892. }
  893. if((*tempSeg)->v1->y < bottomY)
  894. {
  895. bottomY = (*tempSeg)->v1->y;
  896. }
  897. }
  898. po->bbox[BOXRIGHT] = (rightX-bmaporgx)>>MAPBLOCKSHIFT;
  899. po->bbox[BOXLEFT] = (leftX-bmaporgx)>>MAPBLOCKSHIFT;
  900. po->bbox[BOXTOP] = (topY-bmaporgy)>>MAPBLOCKSHIFT;
  901. po->bbox[BOXBOTTOM] = (bottomY-bmaporgy)>>MAPBLOCKSHIFT;
  902. // add the polyobj to each blockmap section
  903. for(j = po->bbox[BOXBOTTOM]*bmapwidth; j <= po->bbox[BOXTOP]*bmapwidth;
  904. j += bmapwidth)
  905. {
  906. for(i = po->bbox[BOXLEFT]; i <= po->bbox[BOXRIGHT]; i++)
  907. {
  908. if(i >= 0 && i < bmapwidth && j >= 0 && j < bmapheight*bmapwidth)
  909. {
  910. link = &PolyBlockMap[j+i];
  911. if(!(*link))
  912. { // Create a new link at the current block cell
  913. *link = Z_Malloc(sizeof(polyblock_t), PU_LEVEL, 0);
  914. (*link)->next = NULL;
  915. (*link)->prev = NULL;
  916. (*link)->polyobj = po;
  917. continue;
  918. }
  919. else
  920. {
  921. tempLink = *link;
  922. while(tempLink->next != NULL && tempLink->polyobj != NULL)
  923. {
  924. tempLink = tempLink->next;
  925. }
  926. }
  927. if(tempLink->polyobj == NULL)
  928. {
  929. tempLink->polyobj = po;
  930. continue;
  931. }
  932. else
  933. {
  934. tempLink->next = Z_Malloc(sizeof(polyblock_t),
  935. PU_LEVEL, 0);
  936. tempLink->next->next = NULL;
  937. tempLink->next->prev = tempLink;
  938. tempLink->next->polyobj = po;
  939. }
  940. }
  941. // else, don't link the polyobj, since it's off the map
  942. }
  943. }
  944. }
  945. //==========================================================================
  946. //
  947. // CheckMobjBlocking
  948. //
  949. //==========================================================================
  950. static boolean CheckMobjBlocking(seg_t *seg, polyobj_t *po)
  951. {
  952. mobj_t *mobj;
  953. int i, j;
  954. int left, right, top, bottom;
  955. int tmbbox[4];
  956. line_t *ld;
  957. boolean blocked;
  958. ld = seg->linedef;
  959. top = (ld->bbox[BOXTOP]-bmaporgy+MAXRADIUS)>>MAPBLOCKSHIFT;
  960. bottom = (ld->bbox[BOXBOTTOM]-bmaporgy-MAXRADIUS)>>MAPBLOCKSHIFT;
  961. left = (ld->bbox[BOXLEFT]-bmaporgx-MAXRADIUS)>>MAPBLOCKSHIFT;
  962. right = (ld->bbox[BOXRIGHT]-bmaporgx+MAXRADIUS)>>MAPBLOCKSHIFT;
  963. blocked = false;
  964. bottom = bottom < 0 ? 0 : bottom;
  965. bottom = bottom >= bmapheight ? bmapheight-1 : bottom;
  966. top = top < 0 ? 0 : top;
  967. top = top >= bmapheight ? bmapheight-1 : top;
  968. left = left < 0 ? 0 : left;
  969. left = left >= bmapwidth ? bmapwidth-1 : left;
  970. right = right < 0 ? 0 : right;
  971. right = right >= bmapwidth ? bmapwidth-1 : right;
  972. for(j = bottom*bmapwidth; j <= top*bmapwidth; j += bmapwidth)
  973. {
  974. for(i = left; i <= right; i++)
  975. {
  976. for(mobj = blocklinks[j+i]; mobj; mobj = mobj->bnext)
  977. {
  978. if(mobj->flags&MF_SOLID || mobj->player)
  979. {
  980. tmbbox[BOXTOP] = mobj->y+mobj->radius;
  981. tmbbox[BOXBOTTOM] = mobj->y-mobj->radius;
  982. tmbbox[BOXLEFT] = mobj->x-mobj->radius;
  983. tmbbox[BOXRIGHT] = mobj->x+mobj->radius;
  984. if (tmbbox[BOXRIGHT] <= ld->bbox[BOXLEFT]
  985. || tmbbox[BOXLEFT] >= ld->bbox[BOXRIGHT]
  986. || tmbbox[BOXTOP] <= ld->bbox[BOXBOTTOM]
  987. || tmbbox[BOXBOTTOM] >= ld->bbox[BOXTOP])
  988. {
  989. continue;
  990. }
  991. if(P_BoxOnLineSide(tmbbox, ld) != -1)
  992. {
  993. continue;
  994. }
  995. ThrustMobj(mobj, seg, po);
  996. blocked = true;
  997. }
  998. }
  999. }
  1000. }
  1001. return blocked;
  1002. }
  1003. //==========================================================================
  1004. //
  1005. // InitBlockMap
  1006. //
  1007. //==========================================================================
  1008. static void InitBlockMap(void)
  1009. {
  1010. int i;
  1011. int j;
  1012. seg_t **segList;
  1013. int area;
  1014. int leftX, rightX;
  1015. int topY, bottomY;
  1016. PolyBlockMap = Z_Malloc(bmapwidth*bmapheight*sizeof(polyblock_t *),
  1017. PU_LEVEL, 0);
  1018. memset(PolyBlockMap, 0, bmapwidth*bmapheight*sizeof(polyblock_t *));
  1019. for(i = 0; i < po_NumPolyobjs; i++)
  1020. {
  1021. LinkPolyobj(&polyobjs[i]);
  1022. // calculate a rough area
  1023. // right now, working like shit...gotta fix this...
  1024. segList = polyobjs[i].segs;
  1025. leftX = rightX = (*segList)->v1->x;
  1026. topY = bottomY = (*segList)->v1->y;
  1027. for(j = 0; j < polyobjs[i].numsegs; j++, segList++)
  1028. {
  1029. if((*segList)->v1->x < leftX)
  1030. {
  1031. leftX = (*segList)->v1->x;
  1032. }
  1033. if((*segList)->v1->x > rightX)
  1034. {
  1035. rightX = (*segList)->v1->x;
  1036. }
  1037. if((*segList)->v1->y < bottomY)
  1038. {
  1039. bottomY = (*segList)->v1->y;
  1040. }
  1041. if((*segList)->v1->y > topY)
  1042. {
  1043. topY = (*segList)->v1->y;
  1044. }
  1045. }
  1046. area = ((rightX>>FRACBITS)-(leftX>>FRACBITS))*
  1047. ((topY>>FRACBITS)-(bottomY>>FRACBITS));
  1048. // fprintf(stdaux, "Area of Polyobj[%d]: %d\n", polyobjs[i].tag, area);
  1049. // fprintf(stdaux, "\t[%d]\n[%d]\t\t[%d]\n\t[%d]\n", topY>>FRACBITS,
  1050. // leftX>>FRACBITS,
  1051. // rightX>>FRACBITS, bottomY>>FRACBITS);
  1052. }
  1053. }
  1054. //==========================================================================
  1055. //
  1056. // IterFindPolySegs
  1057. //
  1058. // Passing NULL for segList will cause IterFindPolySegs to
  1059. // count the number of segs in the polyobj
  1060. //==========================================================================
  1061. static void IterFindPolySegs(int x, int y, seg_t **segList)
  1062. {
  1063. int i;
  1064. if(x == PolyStartX && y == PolyStartY)
  1065. {
  1066. return;
  1067. }
  1068. for(i = 0; i < numsegs; i++)
  1069. {
  1070. if(segs[i].v1->x == x && segs[i].v1->y == y)
  1071. {
  1072. if(!segList)
  1073. {
  1074. PolySegCount++;
  1075. }
  1076. else
  1077. {
  1078. *segList++ = &segs[i];
  1079. }
  1080. IterFindPolySegs(segs[i].v2->x, segs[i].v2->y, segList);
  1081. return;
  1082. }
  1083. }
  1084. I_Error("IterFindPolySegs: Non-closed Polyobj located.\n");
  1085. }
  1086. //==========================================================================
  1087. //
  1088. // SpawnPolyobj
  1089. //
  1090. //==========================================================================
  1091. static void SpawnPolyobj(int index, int tag, boolean crush)
  1092. {
  1093. int i;
  1094. int j;
  1095. int psIndex;
  1096. int psIndexOld;
  1097. seg_t *polySegList[PO_MAXPOLYSEGS];
  1098. for(i = 0; i < numsegs; i++)
  1099. {
  1100. if(segs[i].linedef->special == PO_LINE_START &&
  1101. segs[i].linedef->arg1 == tag)
  1102. {
  1103. if(polyobjs[index].segs)
  1104. {
  1105. I_Error("SpawnPolyobj: Polyobj %d already spawned.\n", tag);
  1106. }
  1107. segs[i].linedef->special = 0;
  1108. segs[i].linedef->arg1 = 0;
  1109. PolySegCount = 1;
  1110. PolyStartX = segs[i].v1->x;
  1111. PolyStartY = segs[i].v1->y;
  1112. IterFindPolySegs(segs[i].v2->x, segs[i].v2->y, NULL);
  1113. polyobjs[index].numsegs = PolySegCount;
  1114. polyobjs[index].segs = Z_Malloc(PolySegCount*sizeof(seg_t *),
  1115. PU_LEVEL, 0);
  1116. *(polyobjs[index].segs) = &segs[i]; // insert the first seg
  1117. IterFindPolySegs(segs[i].v2->x, segs[i].v2->y,
  1118. polyobjs[index].segs+1);
  1119. polyobjs[index].crush = crush;
  1120. polyobjs[index].tag = tag;
  1121. polyobjs[index].seqType = segs[i].linedef->arg3;
  1122. if(polyobjs[index].seqType < 0
  1123. || polyobjs[index].seqType >= SEQTYPE_NUMSEQ)
  1124. {
  1125. polyobjs[index].seqType = 0;
  1126. }
  1127. break;
  1128. }
  1129. }
  1130. if(!polyobjs[index].segs)
  1131. { // didn't find a polyobj through PO_LINE_START
  1132. psIndex = 0;
  1133. polyobjs[index].numsegs = 0;
  1134. for(j = 1; j < PO_MAXPOLYSEGS; j++)
  1135. {
  1136. psIndexOld = psIndex;
  1137. for (i = 0; i < numsegs; i++)
  1138. {
  1139. if(segs[i].linedef->special == PO_LINE_EXPLICIT &&
  1140. segs[i].linedef->arg1 == tag)
  1141. {
  1142. if(!segs[i].linedef->arg2)
  1143. {
  1144. I_Error("SpawnPolyobj: Explicit line missing order number (probably %d) in poly %d.\n",
  1145. j+1, tag);
  1146. }
  1147. if(segs[i].linedef->arg2 == j)
  1148. {
  1149. polySegList[psIndex] = &segs[i];
  1150. polyobjs[index].numsegs++;
  1151. psIndex++;
  1152. if(psIndex > PO_MAXPOLYSEGS)
  1153. {
  1154. I_Error("SpawnPolyobj: psIndex > PO_MAXPOLYSEGS\n");
  1155. }
  1156. }
  1157. }
  1158. }
  1159. // Clear out any specials for these segs...we cannot clear them out
  1160. // in the above loop, since we aren't guaranteed one seg per
  1161. // linedef.
  1162. for(i = 0; i < numsegs; i++)
  1163. {
  1164. if(segs[i].linedef->special == PO_LINE_EXPLICIT &&
  1165. segs[i].linedef->arg1 == tag && segs[i].linedef->arg2 == j)
  1166. {
  1167. segs[i].linedef->special = 0;
  1168. segs[i].linedef->arg1 = 0;
  1169. }
  1170. }
  1171. if(psIndex == psIndexOld)
  1172. { // Check if an explicit line order has been skipped
  1173. // A line has been skipped if there are any more explicit
  1174. // lines with the current tag value
  1175. for(i = 0; i < numsegs; i++)
  1176. {
  1177. if(segs[i].linedef->special == PO_LINE_EXPLICIT &&
  1178. segs[i].linedef->arg1 == tag)
  1179. {
  1180. I_Error("SpawnPolyobj: Missing explicit line %d for poly %d\n",
  1181. j, tag);
  1182. }
  1183. }
  1184. }
  1185. }
  1186. if(polyobjs[index].numsegs)
  1187. {
  1188. PolySegCount = polyobjs[index].numsegs; // PolySegCount used globally
  1189. polyobjs[index].crush = crush;
  1190. polyobjs[index].tag = tag;
  1191. polyobjs[index].segs = Z_Malloc(polyobjs[index].numsegs
  1192. *sizeof(seg_t *), PU_LEVEL, 0);
  1193. for(i = 0; i < polyobjs[index].numsegs; i++)
  1194. {
  1195. polyobjs[index].segs[i] = polySegList[i];
  1196. }
  1197. polyobjs[index].seqType = (*polyobjs[index].segs)->linedef->arg4;
  1198. }
  1199. // Next, change the polyobjs first line to point to a mirror
  1200. // if it exists
  1201. (*polyobjs[index].segs)->linedef->arg2 =
  1202. (*polyobjs[index].segs)->linedef->arg3;
  1203. }
  1204. }
  1205. //==========================================================================
  1206. //
  1207. // TranslateToStartSpot
  1208. //
  1209. //==========================================================================
  1210. static void TranslateToStartSpot(int tag, int originX, int originY)
  1211. {
  1212. seg_t **tempSeg;
  1213. seg_t **veryTempSeg;
  1214. vertex_t *tempPt;
  1215. subsector_t *sub;
  1216. polyobj_t *po;
  1217. int deltaX;
  1218. int deltaY;
  1219. vertex_t avg; // used to find a polyobj's center, and hence subsector
  1220. int i;
  1221. po = NULL;
  1222. for(i = 0; i < po_NumPolyobjs; i++)
  1223. {
  1224. if(polyobjs[i].tag == tag)
  1225. {
  1226. po = &polyobjs[i];
  1227. break;
  1228. }
  1229. }
  1230. if(!po)
  1231. { // didn't match the tag with a polyobj tag
  1232. I_Error("TranslateToStartSpot: Unable to match polyobj tag: %d\n",
  1233. tag);
  1234. }
  1235. if(po->segs == NULL)
  1236. {
  1237. I_Error("TranslateToStartSpot: Anchor point located without a StartSpot point: %d\n", tag);
  1238. }
  1239. po->originalPts = Z_Malloc(po->numsegs*sizeof(vertex_t), PU_LEVEL, 0);
  1240. po->prevPts = Z_Malloc(po->numsegs*sizeof(vertex_t), PU_LEVEL, 0);
  1241. deltaX = originX-po->startSpot.x;
  1242. deltaY = originY-po->startSpot.y;
  1243. tempSeg = po->segs;
  1244. tempPt = po->originalPts;
  1245. avg.x = 0;
  1246. avg.y = 0;
  1247. validcount++;
  1248. for(i = 0; i < po->numsegs; i++, tempSeg++, tempPt++)
  1249. {
  1250. if((*tempSeg)->linedef->validcount != validcount)
  1251. {
  1252. (*tempSeg)->linedef->bbox[BOXTOP] -= deltaY;
  1253. (*tempSeg)->linedef->bbox[BOXBOTTOM] -= deltaY;
  1254. (*tempSeg)->linedef->bbox[BOXLEFT] -= deltaX;
  1255. (*tempSeg)->linedef->bbox[BOXRIGHT] -= deltaX;
  1256. (*tempSeg)->linedef->validcount = validcount;
  1257. }
  1258. for(veryTempSeg = po->segs; veryTempSeg != tempSeg; veryTempSeg++)
  1259. {
  1260. if((*veryTempSeg)->v1 == (*tempSeg)->v1)
  1261. {
  1262. break;
  1263. }
  1264. }
  1265. if(veryTempSeg == tempSeg)
  1266. { // the point hasn't been translated, yet
  1267. (*tempSeg)->v1->x -= deltaX;
  1268. (*tempSeg)->v1->y -= deltaY;
  1269. }
  1270. avg.x += (*tempSeg)->v1->x>>FRACBITS;
  1271. avg.y += (*tempSeg)->v1->y>>FRACBITS;
  1272. // the original Pts are based off the startSpot Pt, and are
  1273. // unique to each seg, not each linedef
  1274. tempPt->x = (*tempSeg)->v1->x-po->startSpot.x;
  1275. tempPt->y = (*tempSeg)->v1->y-po->startSpot.y;
  1276. }
  1277. avg.x /= po->numsegs;
  1278. avg.y /= po->numsegs;
  1279. sub = R_PointInSubsector(avg.x<<FRACBITS, avg.y<<FRACBITS);
  1280. if(sub->poly != NULL)
  1281. {
  1282. I_Error("PO_TranslateToStartSpot: Multiple polyobjs in a single subsector.\n");
  1283. }
  1284. sub->poly = po;
  1285. }
  1286. //==========================================================================
  1287. //
  1288. // PO_Init
  1289. //
  1290. //==========================================================================
  1291. void PO_Init(int lump)
  1292. {
  1293. byte *data;
  1294. int i;
  1295. mapthing_t *mt;
  1296. int numthings;
  1297. int polyIndex;
  1298. polyobjs = Z_Malloc(po_NumPolyobjs*sizeof(polyobj_t), PU_LEVEL, 0);
  1299. memset(polyobjs, 0, po_NumPolyobjs*sizeof(polyobj_t));
  1300. data = W_CacheLumpNum(lump, PU_STATIC);
  1301. numthings = W_LumpLength(lump)/sizeof(mapthing_t);
  1302. mt = (mapthing_t *)data;
  1303. polyIndex = 0; // index polyobj number
  1304. // Find the startSpot points, and spawn each polyobj
  1305. for (i = 0; i < numthings; i++, mt++)
  1306. {
  1307. mt->x = SHORT(mt->x);
  1308. mt->y = SHORT(mt->y);
  1309. mt->angle = SHORT(mt->angle);
  1310. mt->type = SHORT(mt->type);
  1311. // 3001 = no crush, 3002 = crushing
  1312. if(mt->type == PO_SPAWN_TYPE || mt->type == PO_SPAWNCRUSH_TYPE)
  1313. { // Polyobj StartSpot Pt.
  1314. polyobjs[polyIndex].startSpot.x = mt->x<<FRACBITS;
  1315. polyobjs[polyIndex].startSpot.y = mt->y<<FRACBITS;
  1316. SpawnPolyobj(polyIndex, mt->angle, (mt->type == PO_SPAWNCRUSH_TYPE));
  1317. polyIndex++;
  1318. }
  1319. }
  1320. mt = (mapthing_t *)data;
  1321. for (i = 0; i < numthings; i++, mt++)
  1322. {
  1323. mt->x = SHORT(mt->x);
  1324. mt->y = SHORT(mt->y);
  1325. mt->angle = SHORT(mt->angle);
  1326. mt->type = SHORT(mt->type);
  1327. if(mt->type == PO_ANCHOR_TYPE)
  1328. { // Polyobj Anchor Pt.
  1329. TranslateToStartSpot(mt->angle, mt->x<<FRACBITS, mt->y<<FRACBITS);
  1330. }
  1331. }
  1332. Z_Free (data);
  1333. // check for a startspot without an anchor point
  1334. for(i = 0; i < po_NumPolyobjs; i++)
  1335. {
  1336. if(!polyobjs[i].originalPts)
  1337. {
  1338. I_Error("PO_Init: StartSpot located without an Anchor point: %d\n",
  1339. polyobjs[i].tag);
  1340. }
  1341. }
  1342. InitBlockMap();
  1343. }
  1344. //==========================================================================
  1345. //
  1346. // PO_Busy
  1347. //
  1348. //==========================================================================
  1349. boolean PO_Busy(int polyobj)
  1350. {
  1351. polyobj_t *poly;
  1352. poly = GetPolyobj(polyobj);
  1353. if(!poly->specialdata)
  1354. {
  1355. return false;
  1356. }
  1357. else
  1358. {
  1359. return true;
  1360. }
  1361. }