123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794 |
- #include "cm_local.h"
- #pragma warning (push, 3) //go back down to 3 for the stl include
- #pragma warning (pop)
- using namespace std;
- #ifdef _XBOX
- #include "../renderer/tr_local.h"
- #endif
- class CPoint
- {
- public:
- float x,y,z;
- CPoint(float _x,float _y,float _z):
- x(_x),
- y(_y),
- z(_z)
- {
- }
- bool operator== (const CPoint& _P) const {return((x==_P.x)&&(y==_P.y)&&(z==_P.z));}
- };
- /*
- class CPointComparator
- {
- public:
- bool operator()(const CPoint& _A,const CPoint& _B) const {return((_A.x==_B.x)&&(_A.y==_B.y)&&(_A.z==_B.z));}
- };
- */
- // Fixed memory version of pointToLeaf that doesn't use STL
- // cuts down on memory fragmentation
- struct PointAndLeaf
- {
- // Default constructor for array construction below
- PointAndLeaf() : point(0, 0, 0), leaf(0) { }
- CPoint point;
- int leaf;
- };
- // I think it is a patholoically bad idea to do a 64 item linear search for a cache,
- // so I reduced this to something more manageable.
- // hopefully getting rid of water checks on maps with no water will leave us less
- // reliant on this cache. -gwg
- #define MAX_POINTS_TO_LEAVES 16
- static PointAndLeaf pointToLeaf[MAX_POINTS_TO_LEAVES];
- static int oldestPointToLeaf = 0, sizePointToLeaf = 0;
- //static hlist<pair<CPoint,int> > pointToLeaf;
- //static hlist<pair<CPoint,int> > pointToContents;
- void CM_CleanLeafCache(void)
- {
- oldestPointToLeaf = sizePointToLeaf = 0;
- // pointToLeaf.clear();
- #if 0 // VVFIXME
- hlist<pair<CPoint,int> >::iterator l;
- for(l=pointToLeaf.begin();l!=pointToLeaf.end();l++)
- {
- pointToLeaf.erase(l);
- }
- #endif
- /*
- for(l=pointToContents.begin();l!=pointToContents.end();l++)
- {
- pointToContents.erase(l);
- }
- */
- }
- /*
- ==================
- CM_PointLeafnum_r
- ==================
- */
- int CM_PointLeafnum_r( const vec3_t p, int num, clipMap_t *local ) {
- float d;
- cNode_t *node;
- cplane_t *plane;
- #ifdef _XBOX
- if(!tr.world) {
- return 0;
- }
- #endif
- while (num >= 0)
- {
- node = local->nodes + num;
- #ifdef _XBOX
- plane = cmg.planes + tr.world->nodes[num].planeNum;
- #else
- plane = node->plane;
- #endif
-
- if (plane->type < 3)
- d = p[plane->type] - plane->dist;
- else
- d = DotProduct (plane->normal, p) - plane->dist;
- if (d < 0)
- num = node->children[1];
- else
- num = node->children[0];
- }
- c_pointcontents++; // optimize counter
- return -1 - num;
- }
- int CM_PointLeafnum( const vec3_t p ) {
- if ( !cmg.numNodes ) { // map not loaded
- return 0;
- }
- return CM_PointLeafnum_r (p, 0, &cmg);
- }
- /*
- ======================================================================
- LEAF LISTING
- ======================================================================
- */
- void CM_StoreLeafs( leafList_t *ll, int nodenum ) {
- int leafNum;
- leafNum = -1 - nodenum;
- // store the lastLeaf even if the list is overflowed
- if ( cmg.leafs[ leafNum ].cluster != -1 ) {
- ll->lastLeaf = leafNum;
- }
- if ( ll->count >= ll->maxcount) {
- ll->overflowed = qtrue;
- return;
- }
- ll->list[ ll->count++ ] = leafNum;
- }
- void CM_StoreBrushes( leafList_t *ll, int nodenum ) {
- int i, k;
- int leafnum;
- int brushnum;
- cLeaf_t *leaf;
- cbrush_t *b;
- leafnum = -1 - nodenum;
- leaf = &cmg.leafs[leafnum];
- for ( k = 0 ; k < leaf->numLeafBrushes ; k++ ) {
- brushnum = cmg.leafbrushes[leaf->firstLeafBrush+k];
- b = &cmg.brushes[brushnum];
- if ( b->checkcount == cmg.checkcount ) {
- continue; // already checked this brush in another leaf
- }
- b->checkcount = cmg.checkcount;
- for ( i = 0 ; i < 3 ; i++ ) {
- if ( b->bounds[0][i] >= ll->bounds[1][i] || b->bounds[1][i] <= ll->bounds[0][i] ) {
- break;
- }
- }
- if ( i != 3 ) {
- continue;
- }
- if ( ll->count >= ll->maxcount) {
- ll->overflowed = qtrue;
- return;
- }
- ((cbrush_t **)ll->list)[ ll->count++ ] = b;
- }
- #if 0
- // store patches?
- for ( k = 0 ; k < leaf->numLeafSurfaces ; k++ ) {
- patch = cmg.surfaces[ cmg.leafsurfaces[ leaf->firstleafsurface + k ] ];
- if ( !patch ) {
- continue;
- }
- }
- #endif
- }
- /*
- =============
- CM_BoxLeafnums
- Fills in a list of all the leafs touched
- =============
- */
- void CM_BoxLeafnums_r( leafList_t *ll, int nodenum ) {
- cplane_t *plane;
- cNode_t *node;
- int s;
- #ifdef _XBOX
- if(!tr.world) {
- return;
- }
- #endif
- while (1) {
- if (nodenum < 0) {
- ll->storeLeafs( ll, nodenum );
- return;
- }
-
- node = &cmg.nodes[nodenum];
- #ifdef _XBOX
- plane = cmg.planes + tr.world->nodes[nodenum].planeNum;
- #else
- plane = node->plane;
- #endif
- s = BoxOnPlaneSide( ll->bounds[0], ll->bounds[1], plane );
- if (s == 1) {
- nodenum = node->children[0];
- } else if (s == 2) {
- nodenum = node->children[1];
- } else {
- // go down both
- CM_BoxLeafnums_r( ll, node->children[0] );
- nodenum = node->children[1];
- }
- }
- }
- /*
- ==================
- CM_BoxLeafnums
- ==================
- */
- int CM_BoxLeafnums( const vec3_t mins, const vec3_t maxs, int *boxList, int listsize, int *lastLeaf) {
- leafList_t ll;
- cmg.checkcount++;
- VectorCopy( mins, ll.bounds[0] );
- VectorCopy( maxs, ll.bounds[1] );
- ll.count = 0;
- ll.maxcount = listsize;
- ll.list = boxList;
- ll.storeLeafs = CM_StoreLeafs;
- ll.lastLeaf = 0;
- ll.overflowed = qfalse;
- CM_BoxLeafnums_r( &ll, 0 );
- *lastLeaf = ll.lastLeaf;
- return ll.count;
- }
- /*
- ==================
- CM_BoxBrushes
- ==================
- */
- int CM_BoxBrushes( const vec3_t mins, const vec3_t maxs, cbrush_t **boxlist, int listsize ) {
- leafList_t ll;
- cmg.checkcount++;
- VectorCopy( mins, ll.bounds[0] );
- VectorCopy( maxs, ll.bounds[1] );
- ll.count = 0;
- ll.maxcount = listsize;
- ll.list = (int *)boxlist;
- ll.storeLeafs = CM_StoreBrushes;
- ll.lastLeaf = 0;
- ll.overflowed = qfalse;
-
- CM_BoxLeafnums_r( &ll, 0 );
- return ll.count;
- }
- //====================================================================
- /*
- ==================
- CM_PointContents
- ==================
- */
- #if 1
- int CM_PointContents( const vec3_t p, clipHandle_t model ) {
- int leafnum=0;
- int i, k;
- int brushnum;
- cLeaf_t *leaf;
- cbrush_t *b;
- int contents;
- float d;
- cmodel_t *clipm;
- clipMap_t *local;
- if (!cmg.numNodes) { // map not loaded
- return 0;
- }
- if ( model ) {
- clipm = CM_ClipHandleToModel( model, &local );
- leaf = &clipm->leaf;
- }
- else
- {
- local = &cmg;
- CPoint pt(p[0],p[1],p[2]);
- /* map<CPoint,int,CPointComparator>::iterator l=pointToLeaf.find(pt);
- if(l!=pointToLeaf.end())
- {
- leafnum=(*l).second;
- }
- else
- {
- if(pointToLeaf.size()>=64)
- {
- pointToLeaf.clear();
- Com_Printf("Cleared cache\n");
- }
- leafnum=CM_PointLeafnum_r(p, 0);
- pointToLeaf[pt]=leafnum;
- }*/
- int l = 0;
- for ( ; l < sizePointToLeaf; ++l)
- {
- if (pointToLeaf[l].point == pt)
- {
- leafnum = pointToLeaf[l].leaf;
- break;
- }
- }
- if (l == sizePointToLeaf)
- { // Didn't find it
- if (sizePointToLeaf < MAX_POINTS_TO_LEAVES)
- { // We're adding a new one, rather than replacing
- sizePointToLeaf++;
- }
- else
- { // Put it in the "oldest" slot
- l = oldestPointToLeaf++;
- oldestPointToLeaf &= (MAX_POINTS_TO_LEAVES-1);
- }
- leafnum = CM_PointLeafnum_r(p, 0, local);
- pointToLeaf[l].leaf = leafnum;
- pointToLeaf[l].point = pt;
- }
- /*
- hlist<pair<CPoint,int> >::iterator l;
- for(l=pointToLeaf.begin();l!=pointToLeaf.end();l++)
- {
- if((*l).first==pt)
- {
- leafnum=(*l).second;
- break;
- }
- }
- if(l==pointToLeaf.end())
- {
- if(pointToLeaf.size()>=64)
- {
- pointToLeaf.pop_back();
- }
- leafnum=CM_PointLeafnum_r(p, 0, local);
- pointToLeaf.push_front(pair<CPoint,int>(pt,leafnum));
- }
- */
- leaf = &local->leafs[leafnum];
- }
- contents = 0;
- for (k=0 ; k<leaf->numLeafBrushes ; k++) {
- brushnum = local->leafbrushes[leaf->firstLeafBrush+k];
- b = &local->brushes[brushnum];
- // see if the point is in the brush
- for ( i = 0 ; i < b->numsides ; i++ ) {
- #ifdef _XBOX
- d = DotProduct( p,
- cmg.planes[b->sides[i].planeNum.GetValue()].normal );
- #else
- d = DotProduct( p, b->sides[i].plane->normal );
- #endif
- // FIXME test for Cash
- // if ( d >= b->sides[i].plane->dist ) {
- #ifdef _XBOX
- if ( d > cmg.planes[b->sides[i].planeNum.GetValue()].dist ) {
- #else
- if ( d > b->sides[i].plane->dist ) {
- #endif
- break;
- }
- }
- if ( i == b->numsides ) {
- contents |= b->contents;
- #ifndef _XBOX // Removing terrain from Xbox
- if(cmg.landScape && (contents & CONTENTS_TERRAIN) )
- {
- if(p[2] < cmg.landScape->GetWaterHeight())
- {
- contents |= cmg.landScape->GetWaterContents();
- }
- }
- #endif
- }
- }
- return contents;
- }
- #else
- int CM_PointContents( const vec3_t p, clipHandle_t model ) {
- int leafnum=0;
- int i, k;
- int brushnum;
- cLeaf_t *leaf;
- cbrush_t *b;
- int contents;
- float d;
- cmodel_t *clipm;
- if (!cmg.numNodes) { // map not loaded
- return 0;
- }
- CPoint pt(p[0],p[1],p[2]);
- if ( model )
- {
- clipm = CM_ClipHandleToModel( model );
- leaf = &clipm->leaf;
- }
- else
- {
- hlist<pair<CPoint,int> >::iterator l;
- for(l=pointToContents.begin();l!=pointToContents.end();l++)
- {
- if((*l).first==pt)
- {
- // Breakout early.
- return((*l).second);
- }
- }
- leafnum=CM_PointLeafnum_r(p, 0);
- leaf = &cmg.leafs[leafnum];
- }
- contents = 0;
- for (k=0 ; k<leaf->numLeafBrushes ; k++)
- {
- brushnum = cmg.leafbrushes[leaf->firstLeafBrush+k];
- b = &cmg.brushes[brushnum];
- // see if the point is in the brush
- for ( i = 0 ; i < b->numsides ; i++ )
- {
- d = DotProduct( p, b->sides[i].plane->normal );
- // FIXME test for Cash
- // if ( d >= b->sides[i].plane->dist ) {
- if ( d > b->sides[i].plane->dist )
- {
- break;
- }
- }
- if ( i == b->numsides )
- {
- contents |= b->contents;
- }
- }
- // Cache the result for next time.
- if(!model)
- {
- if(pointToContents.size()>=64)
- {
- pointToContents.pop_back();
- }
- pointToContents.push_front(pair<CPoint,int>(pt,contents));
- }
- return contents;
- }
- #endif
- /*
- ==================
- CM_TransformedPointContents
- Handles offseting and rotation of the end points for moving and
- rotating entities
- ==================
- */
- int CM_TransformedPointContents( const vec3_t p, clipHandle_t model, const vec3_t origin, const vec3_t angles) {
- vec3_t p_l;
- vec3_t temp;
- vec3_t forward, right, up;
- // subtract origin offset
- VectorSubtract (p, origin, p_l);
- // rotate start and end into the models frame of reference
- if ( model != BOX_MODEL_HANDLE &&
- (angles[0] || angles[1] || angles[2]) )
- {
- AngleVectors (angles, forward, right, up);
- VectorCopy (p_l, temp);
- p_l[0] = DotProduct (temp, forward);
- p_l[1] = -DotProduct (temp, right);
- p_l[2] = DotProduct (temp, up);
- }
- return CM_PointContents( p_l, model );
- }
- /*
- ===============================================================================
- PVS
- ===============================================================================
- */
- #ifdef _XBOX
- extern trGlobals_t tr;
- const byte *CM_ClusterPVS (int cluster) {
- if (cluster < 0 || cluster >= cmg.numClusters || !cmg.vised ) {
- return NULL;
- }
- return cmg.visibility->Decompress(cluster * cmg.clusterBytes,
- cmg.numClusters);
- }
- #else
- byte *CM_ClusterPVS (int cluster) {
- if (cluster < 0 || cluster >= cmg.numClusters || !cmg.vised ) {
- return cmg.visibility;
- }
- return cmg.visibility + cluster * cmg.clusterBytes;
- }
- #endif
- /*
- ===============================================================================
- AREAPORTALS
- ===============================================================================
- */
- #ifdef _XBOX
- void CM_FloodArea_r( int areaNum, int floodnum) {
- int i;
- cArea_t *area;
- int *con;
- area = &cmg.areas[ areaNum ];
- if ( area->floodvalid == cmg.floodvalid ) {
- if (area->floodnum == floodnum)
- return;
- Com_Error (ERR_DROP, "FloodArea_r: reflooded");
- }
- area->floodnum = floodnum;
- area->floodvalid = cmg.floodvalid;
- con = cmg.areaPortals + areaNum * cmg.numAreas;
- for ( i=0 ; i < cmg.numAreas ; i++ ) {
- if ( con[i] > 0 ) {
- CM_FloodArea_r( i, floodnum );
- }
- }
- }
- #else // _XBOX
- void CM_FloodArea_r( int areaNum, int floodnum, clipMap_t &cm) {
- int i;
- cArea_t *area;
- int *con;
- area = &cmg.areas[ areaNum ];
- if ( area->floodvalid == cmg.floodvalid ) {
- if (area->floodnum == floodnum)
- return;
- Com_Error (ERR_DROP, "FloodArea_r: reflooded");
- }
- area->floodnum = floodnum;
- area->floodvalid = cmg.floodvalid;
- con = cmg.areaPortals + areaNum * cmg.numAreas;
- for ( i=0 ; i < cmg.numAreas ; i++ ) {
- if ( con[i] > 0 ) {
- CM_FloodArea_r( i, floodnum, cm );
- }
- }
- }
- #endif // XBOX
- /*
- ====================
- CM_FloodAreaConnections
- ====================
- */
- #ifdef _XBOX
- void CM_FloodAreaConnections( void ) {
- int i;
- cArea_t *area;
- int floodnum;
- // all current floods are now invalid
- cmg.floodvalid++;
- floodnum = 0;
- for (i = 0 ; i < cmg.numAreas ; i++) {
- area = &cmg.areas[i];
- if (area->floodvalid == cmg.floodvalid) {
- continue; // already flooded into
- }
- floodnum++;
- CM_FloodArea_r (i, floodnum);
- }
- }
- #else // _XBOX
- void CM_FloodAreaConnections( clipMap_t &cm ) {
- int i;
- cArea_t *area;
- int floodnum;
- // all current floods are now invalid
- cmg.floodvalid++;
- floodnum = 0;
- for (i = 0 ; i < cmg.numAreas ; i++) {
- area = &cmg.areas[i];
- if (area->floodvalid == cmg.floodvalid) {
- continue; // already flooded into
- }
- floodnum++;
- CM_FloodArea_r (i, floodnum, cm);
- }
- }
- #endif // _XBOX
- /*
- ====================
- CM_AdjustAreaPortalState
- ====================
- */
- void CM_AdjustAreaPortalState( int area1, int area2, qboolean open ) {
- if ( area1 < 0 || area2 < 0 ) {
- return;
- }
- if ( area1 >= cmg.numAreas || area2 >= cmg.numAreas ) {
- Com_Error (ERR_DROP, "CM_ChangeAreaPortalState: bad area number");
- }
- if ( open ) {
- cmg.areaPortals[ area1 * cmg.numAreas + area2 ]++;
- cmg.areaPortals[ area2 * cmg.numAreas + area1 ]++;
- } else {
- cmg.areaPortals[ area1 * cmg.numAreas + area2 ]--;
- cmg.areaPortals[ area2 * cmg.numAreas + area1 ]--;
- if ( cmg.areaPortals[ area2 * cmg.numAreas + area1 ] < 0 ) {
- Com_Error (ERR_DROP, "CM_AdjustAreaPortalState: negative reference count");
- }
- }
- #ifdef _XBOX
- CM_FloodAreaConnections ();
- #else
- CM_FloodAreaConnections (cmg);
- #endif
- }
- /*
- ====================
- CM_AreasConnected
- ====================
- */
- qboolean CM_AreasConnected( int area1, int area2 ) {
- #ifndef BSPC
- if ( cm_noAreas->integer ) {
- return qtrue;
- }
- #endif
- if ( area1 < 0 || area2 < 0 ) {
- return qfalse;
- }
- if (area1 >= cmg.numAreas || area2 >= cmg.numAreas) {
- Com_Error (ERR_DROP, "area >= cmg.numAreas");
- }
- if (cmg.areas[area1].floodnum == cmg.areas[area2].floodnum) {
- return qtrue;
- }
- return qfalse;
- }
- /*
- =================
- CM_WriteAreaBits
- Writes a bit vector of all the areas
- that are in the same flood as the area parameter
- Returns the number of bytes needed to hold all the bits.
- The bits are OR'd in, so you can CM_WriteAreaBits from multiple
- viewpoints and get the union of all visible areas.
- This is used to cull non-visible entities from snapshots
- =================
- */
- int CM_WriteAreaBits (byte *buffer, int area)
- {
- int i;
- int floodnum;
- int bytes;
- bytes = (cmg.numAreas+7)>>3;
- #ifndef BSPC
- if (cm_noAreas->integer || area == -1)
- #else
- if ( area == -1)
- #endif
- { // for debugging, send everything
- memset (buffer, 255, bytes);
- }
- else
- {
- floodnum = cmg.areas[area].floodnum;
- for (i=0 ; i<cmg.numAreas ; i++)
- {
- if (cmg.areas[i].floodnum == floodnum || area == -1)
- buffer[i>>3] |= 1<<(i&7);
- }
- }
- return bytes;
- }
- void CM_SnapPVS(vec3_t origin,byte *buffer)
- {
- int clientarea;
- int leafnum;
- int i;
-
- leafnum = CM_PointLeafnum (origin);
- clientarea = CM_LeafArea (leafnum);
- // calculate the visible areas
- memset(buffer,0,MAX_MAP_AREA_BYTES);
- CM_WriteAreaBits(buffer,clientarea);
- for ( i = 0 ; i < MAX_MAP_AREA_BYTES/4 ; i++ ) {
- ((int *)buffer)[i] = ((int *)buffer)[i] ^ -1;
- }
- }
|