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