123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878 |
- #include "Precompiled.h"
- #include "globaldata.h"
- #include <stdlib.h>
- #include "m_bbox.h"
- #include "doomdef.h"
- #include "p_local.h"
- #include "r_state.h"
- fixed_t
- P_AproxDistance
- ( fixed_t dx,
- fixed_t dy )
- {
- dx = abs(dx);
- dy = abs(dy);
- if (dx < dy)
- return dx+dy-(dx>>1);
- return dx+dy-(dy>>1);
- }
- int
- P_PointOnLineSide
- ( fixed_t x,
- fixed_t y,
- line_t* line )
- {
- fixed_t dx;
- fixed_t dy;
- fixed_t left;
- fixed_t right;
-
- if (!line->dx)
- {
- if (x <= line->v1->x)
- return line->dy > 0;
-
- return line->dy < 0;
- }
- if (!line->dy)
- {
- if (y <= line->v1->y)
- return line->dx < 0;
-
- return line->dx > 0;
- }
-
- dx = (x - line->v1->x);
- dy = (y - line->v1->y);
-
- left = FixedMul ( line->dy>>FRACBITS , dx );
- right = FixedMul ( dy , line->dx>>FRACBITS );
-
- if (right < left)
- return 0;
- return 1;
- }
- int
- P_BoxOnLineSide
- ( fixed_t* tmbox,
- line_t* ld )
- {
- int p1 = 0;
- int p2 = 0;
-
- switch (ld->slopetype)
- {
- case ST_HORIZONTAL:
- p1 = tmbox[BOXTOP] > ld->v1->y;
- p2 = tmbox[BOXBOTTOM] > ld->v1->y;
- if (ld->dx < 0)
- {
- p1 ^= 1;
- p2 ^= 1;
- }
- break;
-
- case ST_VERTICAL:
- p1 = tmbox[BOXRIGHT] < ld->v1->x;
- p2 = tmbox[BOXLEFT] < ld->v1->x;
- if (ld->dy < 0)
- {
- p1 ^= 1;
- p2 ^= 1;
- }
- break;
-
- case ST_POSITIVE:
- p1 = P_PointOnLineSide (tmbox[BOXLEFT], tmbox[BOXTOP], ld);
- p2 = P_PointOnLineSide (tmbox[BOXRIGHT], tmbox[BOXBOTTOM], ld);
- break;
-
- case ST_NEGATIVE:
- p1 = P_PointOnLineSide (tmbox[BOXRIGHT], tmbox[BOXTOP], ld);
- p2 = P_PointOnLineSide (tmbox[BOXLEFT], tmbox[BOXBOTTOM], ld);
- break;
- }
- if (p1 == p2)
- return p1;
- return -1;
- }
- int
- P_PointOnDivlineSide
- ( fixed_t x,
- fixed_t y,
- divline_t* line )
- {
- fixed_t dx;
- fixed_t dy;
- fixed_t left;
- fixed_t right;
-
- if (!line->dx)
- {
- if (x <= line->x)
- return line->dy > 0;
-
- return line->dy < 0;
- }
- if (!line->dy)
- {
- if (y <= line->y)
- return line->dx < 0;
- return line->dx > 0;
- }
-
- dx = (x - line->x);
- dy = (y - line->y);
-
-
- if ( (line->dy ^ line->dx ^ dx ^ dy)&0x80000000 )
- {
- if ( (line->dy ^ dx) & 0x80000000 )
- return 1;
- return 0;
- }
-
- left = FixedMul ( line->dy>>8, dx>>8 );
- right = FixedMul ( dy>>8 , line->dx>>8 );
-
- if (right < left)
- return 0;
- return 1;
- }
- void
- P_MakeDivline
- ( line_t* li,
- divline_t* dl )
- {
- dl->x = li->v1->x;
- dl->y = li->v1->y;
- dl->dx = li->dx;
- dl->dy = li->dy;
- }
- fixed_t
- P_InterceptVector
- ( divline_t* v2,
- divline_t* v1 )
- {
- #if 1
- fixed_t frac;
- fixed_t num;
- fixed_t den;
-
- den = FixedMul (v1->dy>>8,v2->dx) - FixedMul(v1->dx>>8,v2->dy);
- if (den == 0)
- return 0;
-
-
- num =
- FixedMul ( (v1->x - v2->x)>>8 ,v1->dy )
- +FixedMul ( (v2->y - v1->y)>>8, v1->dx );
- frac = FixedDiv (num , den);
- return frac;
- #else
- float frac;
- float num;
- float den;
- float v1x;
- float v1y;
- float v1dx;
- float v1dy;
- float v2x;
- float v2y;
- float v2dx;
- float v2dy;
- v1x = (float)v1->x/FRACUNIT;
- v1y = (float)v1->y/FRACUNIT;
- v1dx = (float)v1->dx/FRACUNIT;
- v1dy = (float)v1->dy/FRACUNIT;
- v2x = (float)v2->x/FRACUNIT;
- v2y = (float)v2->y/FRACUNIT;
- v2dx = (float)v2->dx/FRACUNIT;
- v2dy = (float)v2->dy/FRACUNIT;
-
- den = v1dy*v2dx - v1dx*v2dy;
- if (den == 0)
- return 0;
-
- num = (v1x - v2x)*v1dy + (v2y - v1y)*v1dx;
- frac = num / den;
- return frac*FRACUNIT;
- #endif
- }
- void P_LineOpening (line_t* maputil_linedef)
- {
- sector_t* front;
- sector_t* back;
-
- if (maputil_linedef->sidenum[1] == -1)
- {
-
- ::g->openrange = 0;
- return;
- }
-
- front = maputil_linedef->frontsector;
- back = maputil_linedef->backsector;
-
- if (front->ceilingheight < back->ceilingheight)
- ::g->opentop = front->ceilingheight;
- else
- ::g->opentop = back->ceilingheight;
- if (front->floorheight > back->floorheight)
- {
- ::g->openbottom = front->floorheight;
- ::g->lowfloor = back->floorheight;
- }
- else
- {
- ::g->openbottom = back->floorheight;
- ::g->lowfloor = front->floorheight;
- }
-
- ::g->openrange = ::g->opentop - ::g->openbottom;
- }
- void P_UnsetThingPosition (mobj_t* thing)
- {
- int blockx;
- int blocky;
- if ( ! (thing->flags & MF_NOSECTOR) )
- {
-
-
- if (thing->snext)
- thing->snext->sprev = thing->sprev;
- if (thing->sprev)
- thing->sprev->snext = thing->snext;
- else
- thing->subsector->sector->thinglist = thing->snext;
- }
-
- if ( ! (thing->flags & MF_NOBLOCKMAP) )
- {
-
-
- if (thing->bnext)
- thing->bnext->bprev = thing->bprev;
-
- if (thing->bprev)
- thing->bprev->bnext = thing->bnext;
- else
- {
- blockx = (thing->x - ::g->bmaporgx)>>MAPBLOCKSHIFT;
- blocky = (thing->y - ::g->bmaporgy)>>MAPBLOCKSHIFT;
- if (blockx>=0 && blockx < ::g->bmapwidth
- && blocky>=0 && blocky < ::g->bmapheight)
- {
- ::g->blocklinks[blocky*::g->bmapwidth+blockx] = thing->bnext;
- }
- }
- }
- }
- void
- P_SetThingPosition (mobj_t* thing)
- {
- subsector_t* ss;
- sector_t* sec;
- int blockx;
- int blocky;
- mobj_t** link;
-
-
- ss = R_PointInSubsector (thing->x,thing->y);
- thing->subsector = ss;
-
- if ( ! (thing->flags & MF_NOSECTOR) )
- {
-
- sec = ss->sector;
-
- thing->sprev = NULL;
- thing->snext = sec->thinglist;
- if (sec->thinglist)
- sec->thinglist->sprev = thing;
- sec->thinglist = thing;
- }
-
-
- if ( ! (thing->flags & MF_NOBLOCKMAP) )
- {
-
- blockx = (thing->x - ::g->bmaporgx)>>MAPBLOCKSHIFT;
- blocky = (thing->y - ::g->bmaporgy)>>MAPBLOCKSHIFT;
- if (blockx>=0
- && blockx < ::g->bmapwidth
- && blocky>=0
- && blocky < ::g->bmapheight)
- {
- link = &::g->blocklinks[blocky*::g->bmapwidth+blockx];
- thing->bprev = NULL;
- thing->bnext = *link;
- if (*link)
- (*link)->bprev = thing;
- *link = thing;
- }
- else
- {
-
- thing->bnext = thing->bprev = NULL;
- }
- }
- }
- qboolean
- P_BlockLinesIterator
- ( int x,
- int y,
- qboolean(*func)(line_t*) )
- {
- int offset;
- short* list;
- line_t* ld;
-
- if (x<0
- || y<0
- || x>=::g->bmapwidth
- || y>=::g->bmapheight)
- {
- return true;
- }
-
- offset = y*::g->bmapwidth+x;
-
- offset = *(::g->blockmap+offset);
- for ( list = ::g->blockmaplump+offset ; *list != -1 ; list++)
- {
- ld = &::g->lines[*list];
- if (ld->validcount == ::g->validcount)
- continue;
- ld->validcount = ::g->validcount;
- if ( !func(ld) )
- return false;
- }
- return true;
- }
- qboolean
- P_BlockThingsIterator
- ( int x,
- int y,
- qboolean(*func)(mobj_t*) )
- {
- mobj_t* mobj;
-
- if ( x<0
- || y<0
- || x>=::g->bmapwidth
- || y>=::g->bmapheight)
- {
- return true;
- }
-
- for (mobj = ::g->blocklinks[y*::g->bmapwidth+x] ;
- mobj ;
- mobj = mobj->bnext)
- {
- if (!func( mobj ) )
- return false;
- }
- return true;
- }
- qboolean
- PIT_AddLineIntercepts (line_t* ld)
- {
- int s1;
- int s2;
- fixed_t frac;
- divline_t dl;
-
-
- if ( ::g->trace.dx > FRACUNIT*16
- || ::g->trace.dy > FRACUNIT*16
- || ::g->trace.dx < -FRACUNIT*16
- || ::g->trace.dy < -FRACUNIT*16)
- {
- s1 = P_PointOnDivlineSide (ld->v1->x, ld->v1->y, &::g->trace);
- s2 = P_PointOnDivlineSide (ld->v2->x, ld->v2->y, &::g->trace);
- }
- else
- {
- s1 = P_PointOnLineSide (::g->trace.x, ::g->trace.y, ld);
- s2 = P_PointOnLineSide (::g->trace.x+::g->trace.dx, ::g->trace.y+::g->trace.dy, ld);
- }
-
- if (s1 == s2)
- return true;
-
-
- P_MakeDivline (ld, &dl);
- frac = P_InterceptVector (&::g->trace, &dl);
- if (frac < 0)
- return true;
-
-
- if (::g->earlyout
- && frac < FRACUNIT
- && !ld->backsector)
- {
- return false;
- }
-
-
- ::g->intercept_p->frac = frac;
- ::g->intercept_p->isaline = true;
- ::g->intercept_p->d.line = ld;
- ::g->intercept_p++;
- return true;
- }
- qboolean PIT_AddThingIntercepts (mobj_t* thing)
- {
- fixed_t x1;
- fixed_t y1;
- fixed_t x2;
- fixed_t y2;
-
- int s1;
- int s2;
-
- qboolean tracepositive;
- divline_t dl;
-
- fixed_t frac;
-
- tracepositive = (::g->trace.dx ^ ::g->trace.dy)>0;
-
-
- if (tracepositive)
- {
- x1 = thing->x - thing->radius;
- y1 = thing->y + thing->radius;
-
- x2 = thing->x + thing->radius;
- y2 = thing->y - thing->radius;
- }
- else
- {
- x1 = thing->x - thing->radius;
- y1 = thing->y - thing->radius;
-
- x2 = thing->x + thing->radius;
- y2 = thing->y + thing->radius;
- }
-
- s1 = P_PointOnDivlineSide (x1, y1, &::g->trace);
- s2 = P_PointOnDivlineSide (x2, y2, &::g->trace);
- if (s1 == s2)
- return true;
-
- dl.x = x1;
- dl.y = y1;
- dl.dx = x2-x1;
- dl.dy = y2-y1;
-
- frac = P_InterceptVector (&::g->trace, &dl);
- if (frac < 0)
- return true;
- ::g->intercept_p->frac = frac;
- ::g->intercept_p->isaline = false;
- ::g->intercept_p->d.thing = thing;
- ::g->intercept_p++;
- return true;
- }
- qboolean
- P_TraverseIntercepts
- ( traverser_t func,
- fixed_t maxfrac )
- {
- int count;
- fixed_t dist;
- intercept_t* scan;
- intercept_t* in;
-
- count = ::g->intercept_p - ::g->intercepts;
-
- in = 0;
-
- while (count--)
- {
- dist = MAXINT;
- for (scan = ::g->intercepts ; scan < ::g->intercept_p ; scan++)
- {
- if (scan->frac < dist)
- {
- dist = scan->frac;
- in = scan;
- }
- }
-
- if (dist > maxfrac)
- return true;
- #if 0
- {
-
- in = scan = ::g->intercepts;
- for ( scan = ::g->intercepts ; scan<::g->intercept_p ; scan++)
- if (scan->frac > maxfrac)
- *in++ = *scan;
- ::g->intercept_p = in;
- return false;
- }
- #endif
- if ( !func (in) )
- return false;
- in->frac = MAXINT;
- }
-
- return true;
- }
- qboolean
- P_PathTraverse
- ( fixed_t x1,
- fixed_t y1,
- fixed_t x2,
- fixed_t y2,
- int flags,
- qboolean (*trav) (intercept_t *))
- {
- fixed_t xt1;
- fixed_t yt1;
- fixed_t xt2;
- fixed_t yt2;
-
- fixed_t xstep;
- fixed_t ystep;
-
- fixed_t partial;
-
- fixed_t xintercept;
- fixed_t yintercept;
-
- int mapx;
- int mapy;
-
- int mapxstep;
- int mapystep;
- int count;
-
- ::g->earlyout = flags & PT_EARLYOUT;
-
- ::g->validcount++;
- ::g->intercept_p = ::g->intercepts;
-
- if ( ((x1-::g->bmaporgx)&(MAPBLOCKSIZE-1)) == 0)
- x1 += FRACUNIT;
-
- if ( ((y1-::g->bmaporgy)&(MAPBLOCKSIZE-1)) == 0)
- y1 += FRACUNIT;
- ::g->trace.x = x1;
- ::g->trace.y = y1;
- ::g->trace.dx = x2 - x1;
- ::g->trace.dy = y2 - y1;
- x1 -= ::g->bmaporgx;
- y1 -= ::g->bmaporgy;
- xt1 = x1>>MAPBLOCKSHIFT;
- yt1 = y1>>MAPBLOCKSHIFT;
- x2 -= ::g->bmaporgx;
- y2 -= ::g->bmaporgy;
- xt2 = x2>>MAPBLOCKSHIFT;
- yt2 = y2>>MAPBLOCKSHIFT;
- if (xt2 > xt1)
- {
- mapxstep = 1;
- partial = FRACUNIT - ((x1>>MAPBTOFRAC)&(FRACUNIT-1));
- ystep = FixedDiv (y2-y1,abs(x2-x1));
- }
- else if (xt2 < xt1)
- {
- mapxstep = -1;
- partial = (x1>>MAPBTOFRAC)&(FRACUNIT-1);
- ystep = FixedDiv (y2-y1,abs(x2-x1));
- }
- else
- {
- mapxstep = 0;
- partial = FRACUNIT;
- ystep = 256*FRACUNIT;
- }
- yintercept = (y1>>MAPBTOFRAC) + FixedMul (partial, ystep);
-
- if (yt2 > yt1)
- {
- mapystep = 1;
- partial = FRACUNIT - ((y1>>MAPBTOFRAC)&(FRACUNIT-1));
- xstep = FixedDiv (x2-x1,abs(y2-y1));
- }
- else if (yt2 < yt1)
- {
- mapystep = -1;
- partial = (y1>>MAPBTOFRAC)&(FRACUNIT-1);
- xstep = FixedDiv (x2-x1,abs(y2-y1));
- }
- else
- {
- mapystep = 0;
- partial = FRACUNIT;
- xstep = 256*FRACUNIT;
- }
- xintercept = (x1>>MAPBTOFRAC) + FixedMul (partial, xstep);
-
-
-
-
- mapx = xt1;
- mapy = yt1;
-
- for (count = 0 ; count < 64 ; count++)
- {
- if (flags & PT_ADDLINES)
- {
- if (!P_BlockLinesIterator (mapx, mapy,PIT_AddLineIntercepts))
- return false;
- }
-
- if (flags & PT_ADDTHINGS)
- {
- if (!P_BlockThingsIterator (mapx, mapy,PIT_AddThingIntercepts))
- return false;
- }
-
- if (mapx == xt2
- && mapy == yt2)
- {
- break;
- }
-
- if ( (yintercept >> FRACBITS) == mapy)
- {
- yintercept += ystep;
- mapx += mapxstep;
- }
- else if ( (xintercept >> FRACBITS) == mapx)
- {
- xintercept += xstep;
- mapy += mapystep;
- }
-
- }
-
- return P_TraverseIntercepts ( trav, FRACUNIT );
- }
|