Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

l_bsp_sin.c

Go to the documentation of this file.
00001 /*
00002 ===========================================================================
00003 Copyright (C) 1999-2005 Id Software, Inc.
00004 
00005 This file is part of Quake III Arena source code.
00006 
00007 Quake III Arena source code is free software; you can redistribute it
00008 and/or modify it under the terms of the GNU General Public License as
00009 published by the Free Software Foundation; either version 2 of the License,
00010 or (at your option) any later version.
00011 
00012 Quake III Arena source code is distributed in the hope that it will be
00013 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Foobar; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 ===========================================================================
00021 */
00022 
00023 #include "l_cmd.h"
00024 #include "l_math.h"
00025 #include "l_mem.h"
00026 #include "l_log.h"
00027 #include "l_poly.h"
00028 #include "../botlib/l_script.h"
00029 #include "l_bsp_ent.h"
00030 #include "l_bsp_sin.h"
00031 
00032 void GetLeafNums (void);
00033 
00034 //=============================================================================
00035 
00036 int                 sin_nummodels;
00037 sin_dmodel_t        *sin_dmodels;//[SIN_MAX_MAP_MODELS];
00038 
00039 int                 sin_visdatasize;
00040 byte                *sin_dvisdata;//[SIN_MAX_MAP_VISIBILITY];
00041 sin_dvis_t          *sin_dvis;// = (sin_dvis_t *)sin_sin_dvisdata;
00042 
00043 int                 sin_lightdatasize;
00044 byte                *sin_dlightdata;//[SIN_MAX_MAP_LIGHTING];
00045 
00046 int                 sin_entdatasize;
00047 char                *sin_dentdata;//[SIN_MAX_MAP_ENTSTRING];
00048 
00049 int                 sin_numleafs;
00050 sin_dleaf_t         *sin_dleafs;//[SIN_MAX_MAP_LEAFS];
00051 
00052 int                 sin_numplanes;
00053 sin_dplane_t        *sin_dplanes;//[SIN_MAX_MAP_PLANES];
00054 
00055 int                 sin_numvertexes;
00056 sin_dvertex_t       *sin_dvertexes;//[SIN_MAX_MAP_VERTS];
00057 
00058 int                 sin_numnodes;
00059 sin_dnode_t         *sin_dnodes;//[SIN_MAX_MAP_NODES];
00060 
00061 int                 sin_numtexinfo;
00062 sin_texinfo_t       *sin_texinfo;//[SIN_MAX_MAP_sin_texinfo];
00063 
00064 int                 sin_numfaces;
00065 sin_dface_t         *sin_dfaces;//[SIN_MAX_MAP_FACES];
00066 
00067 int                 sin_numedges;
00068 sin_dedge_t         *sin_dedges;//[SIN_MAX_MAP_EDGES];
00069 
00070 int                 sin_numleaffaces;
00071 unsigned short      *sin_dleaffaces;//[SIN_MAX_MAP_LEAFFACES];
00072 
00073 int                 sin_numleafbrushes;
00074 unsigned short      *sin_dleafbrushes;//[SIN_MAX_MAP_LEAFBRUSHES];
00075 
00076 int                 sin_numsurfedges;
00077 int                 *sin_dsurfedges;//[SIN_MAX_MAP_SURFEDGES];
00078 
00079 int                 sin_numbrushes;
00080 sin_dbrush_t        *sin_dbrushes;//[SIN_MAX_MAP_BRUSHES];
00081 
00082 int                 sin_numbrushsides;
00083 sin_dbrushside_t    *sin_dbrushsides;//[SIN_MAX_MAP_BRUSHSIDES];
00084 
00085 int                 sin_numareas;
00086 sin_darea_t         *sin_dareas;//[SIN_MAX_MAP_AREAS];
00087 
00088 int                 sin_numareaportals;
00089 sin_dareaportal_t   *sin_dareaportals;//[SIN_MAX_MAP_AREAPORTALS];
00090 
00091 int                 sin_numlightinfo;
00092 sin_lightvalue_t    *sin_lightinfo;//[SIN_MAX_MAP_LIGHTINFO];
00093 
00094 byte                sin_dpop[256];
00095 
00096 char                sin_dbrushsidetextured[SIN_MAX_MAP_BRUSHSIDES];
00097 
00098 int sin_bspallocated = false;
00099 int sin_allocatedbspmem = 0;
00100 
00101 void Sin_AllocMaxBSP(void)
00102 {
00103     //models
00104     sin_nummodels = 0;
00105     sin_dmodels = (sin_dmodel_t *) GetClearedMemory(SIN_MAX_MAP_MODELS * sizeof(sin_dmodel_t));
00106     sin_allocatedbspmem += SIN_MAX_MAP_MODELS * sizeof(sin_dmodel_t);
00107     //vis data
00108     sin_visdatasize = 0;
00109     sin_dvisdata = (byte *) GetClearedMemory(SIN_MAX_MAP_VISIBILITY * sizeof(byte));
00110     sin_dvis = (sin_dvis_t *) sin_dvisdata;
00111     sin_allocatedbspmem += SIN_MAX_MAP_VISIBILITY * sizeof(byte);
00112     //light data
00113     sin_lightdatasize = 0;
00114     sin_dlightdata = (byte *) GetClearedMemory(SIN_MAX_MAP_LIGHTING * sizeof(byte));
00115     sin_allocatedbspmem += SIN_MAX_MAP_LIGHTING * sizeof(byte);
00116     //entity data
00117     sin_entdatasize = 0;
00118     sin_dentdata = (char *) GetClearedMemory(SIN_MAX_MAP_ENTSTRING * sizeof(char));
00119     sin_allocatedbspmem += SIN_MAX_MAP_ENTSTRING * sizeof(char);
00120     //leafs
00121     sin_numleafs = 0;
00122     sin_dleafs = (sin_dleaf_t *) GetClearedMemory(SIN_MAX_MAP_LEAFS * sizeof(sin_dleaf_t));
00123     sin_allocatedbspmem += SIN_MAX_MAP_LEAFS * sizeof(sin_dleaf_t);
00124     //planes
00125     sin_numplanes = 0;
00126     sin_dplanes = (sin_dplane_t *) GetClearedMemory(SIN_MAX_MAP_PLANES * sizeof(sin_dplane_t));
00127     sin_allocatedbspmem += SIN_MAX_MAP_PLANES * sizeof(sin_dplane_t);
00128     //vertexes
00129     sin_numvertexes = 0;
00130     sin_dvertexes = (sin_dvertex_t *) GetClearedMemory(SIN_MAX_MAP_VERTS * sizeof(sin_dvertex_t));
00131     sin_allocatedbspmem += SIN_MAX_MAP_VERTS * sizeof(sin_dvertex_t);
00132     //nodes
00133     sin_numnodes = 0;
00134     sin_dnodes = (sin_dnode_t *) GetClearedMemory(SIN_MAX_MAP_NODES * sizeof(sin_dnode_t));
00135     sin_allocatedbspmem += SIN_MAX_MAP_NODES * sizeof(sin_dnode_t);
00136     //texture info
00137     sin_numtexinfo = 0;
00138     sin_texinfo = (sin_texinfo_t *) GetClearedMemory(SIN_MAX_MAP_TEXINFO * sizeof(sin_texinfo_t));
00139     sin_allocatedbspmem += SIN_MAX_MAP_TEXINFO * sizeof(sin_texinfo_t);
00140     //faces
00141     sin_numfaces = 0;
00142     sin_dfaces = (sin_dface_t *) GetClearedMemory(SIN_MAX_MAP_FACES * sizeof(sin_dface_t));
00143     sin_allocatedbspmem += SIN_MAX_MAP_FACES * sizeof(sin_dface_t);
00144     //edges
00145     sin_numedges = 0;
00146     sin_dedges = (sin_dedge_t *) GetClearedMemory(SIN_MAX_MAP_EDGES * sizeof(sin_dedge_t));
00147     sin_allocatedbspmem += SIN_MAX_MAP_EDGES * sizeof(sin_dedge_t);
00148     //leaf faces
00149     sin_numleaffaces = 0;
00150     sin_dleaffaces = (unsigned short *) GetClearedMemory(SIN_MAX_MAP_LEAFFACES * sizeof(unsigned short));
00151     sin_allocatedbspmem += SIN_MAX_MAP_LEAFFACES * sizeof(unsigned short);
00152     //leaf brushes
00153     sin_numleafbrushes = 0;
00154     sin_dleafbrushes = (unsigned short *) GetClearedMemory(SIN_MAX_MAP_LEAFBRUSHES * sizeof(unsigned short));
00155     sin_allocatedbspmem += SIN_MAX_MAP_LEAFBRUSHES * sizeof(unsigned short);
00156     //surface edges
00157     sin_numsurfedges = 0;
00158     sin_dsurfedges = (int *) GetClearedMemory(SIN_MAX_MAP_SURFEDGES * sizeof(int));
00159     sin_allocatedbspmem += SIN_MAX_MAP_SURFEDGES * sizeof(int);
00160     //brushes
00161     sin_numbrushes = 0;
00162     sin_dbrushes = (sin_dbrush_t *) GetClearedMemory(SIN_MAX_MAP_BRUSHES * sizeof(sin_dbrush_t));
00163     sin_allocatedbspmem += SIN_MAX_MAP_BRUSHES * sizeof(sin_dbrush_t);
00164     //brushsides
00165     sin_numbrushsides = 0;
00166     sin_dbrushsides = (sin_dbrushside_t *) GetClearedMemory(SIN_MAX_MAP_BRUSHSIDES * sizeof(sin_dbrushside_t));
00167     sin_allocatedbspmem += SIN_MAX_MAP_BRUSHSIDES * sizeof(sin_dbrushside_t);
00168     //areas
00169     sin_numareas = 0;
00170     sin_dareas = (sin_darea_t *) GetClearedMemory(SIN_MAX_MAP_AREAS * sizeof(sin_darea_t));
00171     sin_allocatedbspmem += SIN_MAX_MAP_AREAS * sizeof(sin_darea_t);
00172     //area portals
00173     sin_numareaportals = 0;
00174     sin_dareaportals = (sin_dareaportal_t *) GetClearedMemory(SIN_MAX_MAP_AREAPORTALS * sizeof(sin_dareaportal_t));
00175     sin_allocatedbspmem += SIN_MAX_MAP_AREAPORTALS * sizeof(sin_dareaportal_t);
00176     //light info
00177     sin_numlightinfo = 0;
00178     sin_lightinfo = (sin_lightvalue_t *) GetClearedMemory(SIN_MAX_MAP_LIGHTINFO * sizeof(sin_lightvalue_t));
00179     sin_allocatedbspmem += SIN_MAX_MAP_LIGHTINFO * sizeof(sin_lightvalue_t);
00180     //print allocated memory
00181     Log_Print("allocated ");
00182     PrintMemorySize(sin_allocatedbspmem);
00183     Log_Print(" of BSP memory\n");
00184 } //end of the function Sin_AllocMaxBSP
00185 
00186 void Sin_FreeMaxBSP(void)
00187 {
00188     //models
00189     sin_nummodels = 0;
00190     FreeMemory(sin_dmodels);
00191     sin_dmodels = NULL;
00192     //vis data
00193     sin_visdatasize = 0;
00194     FreeMemory(sin_dvisdata);
00195     sin_dvisdata = NULL;
00196     sin_dvis = NULL;
00197     //light data
00198     sin_lightdatasize = 0;
00199     FreeMemory(sin_dlightdata);
00200     sin_dlightdata = NULL;
00201     //entity data
00202     sin_entdatasize = 0;
00203     FreeMemory(sin_dentdata);
00204     sin_dentdata = NULL;
00205     //leafs
00206     sin_numleafs = 0;
00207     FreeMemory(sin_dleafs);
00208     sin_dleafs = NULL;
00209     //planes
00210     sin_numplanes = 0;
00211     FreeMemory(sin_dplanes);
00212     sin_dplanes = NULL;
00213     //vertexes
00214     sin_numvertexes = 0;
00215     FreeMemory(sin_dvertexes);
00216     sin_dvertexes = NULL;
00217     //nodes
00218     sin_numnodes = 0;
00219     FreeMemory(sin_dnodes);
00220     sin_dnodes = NULL;
00221     //texture info
00222     sin_numtexinfo = 0;
00223     FreeMemory(sin_texinfo);
00224     sin_texinfo = NULL;
00225     //faces
00226     sin_numfaces = 0;
00227     FreeMemory(sin_dfaces);
00228     sin_dfaces = NULL;
00229     //edges
00230     sin_numedges = 0;
00231     FreeMemory(sin_dedges);
00232     sin_dedges = NULL;
00233     //leaf faces
00234     sin_numleaffaces = 0;
00235     FreeMemory(sin_dleaffaces);
00236     sin_dleaffaces = NULL;
00237     //leaf brushes
00238     sin_numleafbrushes = 0;
00239     FreeMemory(sin_dleafbrushes);
00240     sin_dleafbrushes = NULL;
00241     //surface edges
00242     sin_numsurfedges = 0;
00243     FreeMemory(sin_dsurfedges);
00244     sin_dsurfedges = NULL;
00245     //brushes
00246     sin_numbrushes = 0;
00247     FreeMemory(sin_dbrushes);
00248     sin_dbrushes = NULL;
00249     //brushsides
00250     sin_numbrushsides = 0;
00251     FreeMemory(sin_dbrushsides);
00252     sin_dbrushsides = NULL;
00253     //areas
00254     sin_numareas = 0;
00255     FreeMemory(sin_dareas);
00256     sin_dareas = NULL;
00257     //area portals
00258     sin_numareaportals = 0;
00259     FreeMemory(sin_dareaportals);
00260     sin_dareaportals = NULL;
00261     //light info
00262     sin_numlightinfo = 0;
00263     FreeMemory(sin_lightinfo);
00264     sin_lightinfo = NULL;
00265     //
00266     Log_Print("freed ");
00267     PrintMemorySize(sin_allocatedbspmem);
00268     Log_Print(" of BSP memory\n");
00269     sin_allocatedbspmem = 0;
00270 } //end of the function Sin_FreeMaxBSP
00271 
00272 #define WCONVEX_EPSILON     0.5
00273 
00274 //===========================================================================
00275 // returns the amount the face and the winding overlap
00276 //
00277 // Parameter:               -
00278 // Returns:                 -
00279 // Changes Globals:     -
00280 //===========================================================================
00281 float Sin_FaceOnWinding(sin_dface_t *face, winding_t *winding)
00282 {
00283     int i, edgenum, side;
00284     float dist, area;
00285     sin_dplane_t plane;
00286     vec_t *v1, *v2;
00287     vec3_t normal, edgevec;
00288     winding_t *w;
00289 
00290     //
00291     w = CopyWinding(winding);
00292     memcpy(&plane, &sin_dplanes[face->planenum], sizeof(sin_dplane_t));
00293     //check on which side of the plane the face is
00294     if (face->side)
00295     {
00296         VectorNegate(plane.normal, plane.normal);
00297         plane.dist = -plane.dist;
00298     } //end if
00299     for (i = 0; i < face->numedges && w; i++)
00300     {
00301         //get the first and second vertex of the edge
00302         edgenum = sin_dsurfedges[face->firstedge + i];
00303         side = edgenum > 0;
00304         //if the face plane is flipped
00305         v1 = sin_dvertexes[sin_dedges[abs(edgenum)].v[side]].point;
00306         v2 = sin_dvertexes[sin_dedges[abs(edgenum)].v[!side]].point;
00307         //create a plane through the edge vector, orthogonal to the face plane
00308         //and with the normal vector pointing out of the face
00309         VectorSubtract(v1, v2, edgevec);
00310         CrossProduct(edgevec, plane.normal, normal);
00311         VectorNormalize(normal);
00312         dist = DotProduct(normal, v1);
00313         //
00314         ChopWindingInPlace(&w, normal, dist, 0.9); //CLIP_EPSILON
00315     } //end for
00316     if (w)
00317     {
00318         area = WindingArea(w);
00319         FreeWinding(w);
00320         return area;
00321     } //end if
00322     return 0;
00323 } //end of the function Sin_FaceOnWinding
00324 //===========================================================================
00325 // creates a winding for the given brush side on the given brush
00326 //
00327 // Parameter:               -
00328 // Returns:                 -
00329 // Changes Globals:     -
00330 //===========================================================================
00331 winding_t *Sin_BrushSideWinding(sin_dbrush_t *brush, sin_dbrushside_t *baseside)
00332 {
00333     int i;
00334     sin_dplane_t *baseplane, *plane;
00335     sin_dbrushside_t *side;
00336     winding_t *w;
00337     
00338     //create a winding for the brush side with the given planenumber
00339     baseplane = &sin_dplanes[baseside->planenum];
00340     w = BaseWindingForPlane(baseplane->normal, baseplane->dist);
00341     for (i = 0; i < brush->numsides && w; i++)
00342     {
00343         side = &sin_dbrushsides[brush->firstside + i];
00344         //don't chop with the base plane
00345         if (side->planenum == baseside->planenum) continue;
00346         //also don't use planes that are almost equal
00347         plane = &sin_dplanes[side->planenum];
00348         if (DotProduct(baseplane->normal, plane->normal) > 0.999
00349                 && fabs(baseplane->dist - plane->dist) < 0.01) continue;
00350         //
00351         plane = &sin_dplanes[side->planenum^1];
00352         ChopWindingInPlace(&w, plane->normal, plane->dist, 0); //CLIP_EPSILON);
00353     } //end for
00354     return w;
00355 } //end of the function Sin_BrushSideWinding
00356 //===========================================================================
00357 //
00358 // Parameter:               -
00359 // Returns:                 -
00360 // Changes Globals:     -
00361 //===========================================================================
00362 int Sin_HintSkipBrush(sin_dbrush_t *brush)
00363 {
00364     int j;
00365     sin_dbrushside_t *brushside;
00366 
00367     for (j = 0; j < brush->numsides; j++)
00368     {
00369         brushside = &sin_dbrushsides[brush->firstside + j];
00370         if (brushside->texinfo > 0)
00371         {
00372             if (sin_texinfo[brushside->texinfo].flags & (SURF_SKIP|SURF_HINT))
00373             {
00374                 return true;
00375             } //end if
00376         } //end if
00377     } //end for
00378     return false;
00379 } //end of the function Sin_HintSkipBrush
00380 //===========================================================================
00381 // fix screwed brush texture references
00382 //
00383 // Parameter:               -
00384 // Returns:                 -
00385 // Changes Globals:     -
00386 //===========================================================================
00387 qboolean WindingIsTiny(winding_t *w);
00388 
00389 void Sin_FixTextureReferences(void)
00390 {
00391     int i, j, k, we;
00392     sin_dbrushside_t *brushside;
00393     sin_dbrush_t *brush;
00394     sin_dface_t *face;
00395     winding_t *w;
00396 
00397     memset(sin_dbrushsidetextured, false, SIN_MAX_MAP_BRUSHSIDES);
00398     //go over all the brushes
00399    for (i = 0; i < sin_numbrushes; i++)
00400    {
00401         brush = &sin_dbrushes[i];
00402         //hint brushes are not textured
00403         if (Sin_HintSkipBrush(brush)) continue;
00404         //go over all the sides of the brush
00405         for (j = 0; j < brush->numsides; j++)
00406         {
00407             brushside = &sin_dbrushsides[brush->firstside + j];
00408             //
00409             w = Sin_BrushSideWinding(brush, brushside);
00410             if (!w)
00411             {
00412                 sin_dbrushsidetextured[brush->firstside + j] = true;
00413                 continue;
00414             } //end if
00415             else
00416             {
00417                 //RemoveEqualPoints(w, 0.2);
00418                 if (WindingIsTiny(w))
00419                 {
00420                     FreeWinding(w);
00421                     sin_dbrushsidetextured[brush->firstside + j] = true;
00422                     continue;
00423                 } //end if
00424                 else
00425                 {
00426                     we = WindingError(w);
00427                     if (we == WE_NOTENOUGHPOINTS
00428                         || we == WE_SMALLAREA
00429                         || we == WE_POINTBOGUSRANGE
00430 //                      || we == WE_NONCONVEX
00431                         )
00432                     {
00433                         FreeWinding(w);
00434                         sin_dbrushsidetextured[brush->firstside + j] = true;
00435                         continue;
00436                     } //end if
00437                 } //end else
00438             } //end else
00439             if (WindingArea(w) < 20)
00440             {
00441                 sin_dbrushsidetextured[brush->firstside + j] = true;
00442             } //end if
00443             //find a face for texturing this brush
00444             for (k = 0; k < sin_numfaces; k++)
00445             {
00446                 face = &sin_dfaces[k];
00447                 //if the face is in the same plane as the brush side
00448                 if ((face->planenum&~1) != (brushside->planenum&~1)) continue;
00449                 //if the face is partly or totally on the brush side
00450                 if (Sin_FaceOnWinding(face, w))
00451                 {
00452                     brushside->texinfo = face->texinfo;
00453                     sin_dbrushsidetextured[brush->firstside + j] = true;
00454                     break;
00455                 } //end if
00456             } //end for
00457             FreeWinding(w);
00458         } //end for
00459     } //end for
00460 } //end of the function Sin_FixTextureReferences*/
00461 
00462 /*
00463 ===============
00464 CompressVis
00465 
00466 ===============
00467 */
00468 int Sin_CompressVis (byte *vis, byte *dest)
00469 {
00470     int     j;
00471     int     rep;
00472     int     visrow;
00473     byte    *dest_p;
00474     
00475     dest_p = dest;
00476 //  visrow = (r_numvisleafs + 7)>>3;
00477     visrow = (sin_dvis->numclusters + 7)>>3;
00478     
00479     for (j=0 ; j<visrow ; j++)
00480     {
00481         *dest_p++ = vis[j];
00482         if (vis[j])
00483             continue;
00484 
00485         rep = 1;
00486         for ( j++; j<visrow ; j++)
00487             if (vis[j] || rep == 255)
00488                 break;
00489             else
00490                 rep++;
00491         *dest_p++ = rep;
00492         j--;
00493     }
00494     
00495     return dest_p - dest;
00496 } //end of the function Sin_CompressVis
00497 
00498 
00499 /*
00500 ===================
00501 DecompressVis
00502 ===================
00503 */
00504 void Sin_DecompressVis (byte *in, byte *decompressed)
00505 {
00506     int     c;
00507     byte    *out;
00508     int     row;
00509 
00510 //  row = (r_numvisleafs+7)>>3; 
00511     row = (sin_dvis->numclusters+7)>>3; 
00512     out = decompressed;
00513 
00514     do
00515     {
00516         if (*in)
00517         {
00518             *out++ = *in++;
00519             continue;
00520         }
00521     
00522         c = in[1];
00523         if (!c)
00524             Error ("DecompressVis: 0 repeat");
00525         in += 2;
00526         while (c)
00527         {
00528             *out++ = 0;
00529             c--;
00530         }
00531     } while (out - decompressed < row);
00532 } //end of the function Sin_DecompressVis
00533 
00534 //=============================================================================
00535 
00536 /*
00537 =============
00538 Sin_SwapBSPFile
00539 
00540 Byte swaps all data in a bsp file.
00541 =============
00542 */
00543 void Sin_SwapBSPFile (qboolean todisk)
00544 {
00545     int             i, j;
00546     sin_dmodel_t        *d;
00547 
00548     
00549 // models   
00550     for (i=0 ; i<sin_nummodels ; i++)
00551     {
00552         d = &sin_dmodels[i];
00553 
00554         d->firstface = LittleLong (d->firstface);
00555         d->numfaces = LittleLong (d->numfaces);
00556         d->headnode = LittleLong (d->headnode);
00557         
00558         for (j=0 ; j<3 ; j++)
00559         {
00560             d->mins[j] = LittleFloat(d->mins[j]);
00561             d->maxs[j] = LittleFloat(d->maxs[j]);
00562             d->origin[j] = LittleFloat(d->origin[j]);
00563         }
00564     }
00565 
00566 //
00567 // vertexes
00568 //
00569     for (i=0 ; i<sin_numvertexes ; i++)
00570     {
00571         for (j=0 ; j<3 ; j++)
00572             sin_dvertexes[i].point[j] = LittleFloat (sin_dvertexes[i].point[j]);
00573     }
00574         
00575 //
00576 // planes
00577 //  
00578     for (i=0 ; i<sin_numplanes ; i++)
00579     {
00580         for (j=0 ; j<3 ; j++)
00581             sin_dplanes[i].normal[j] = LittleFloat (sin_dplanes[i].normal[j]);
00582         sin_dplanes[i].dist = LittleFloat (sin_dplanes[i].dist);
00583         sin_dplanes[i].type = LittleLong (sin_dplanes[i].type);
00584     }
00585     
00586 //
00587 // sin_texinfos
00588 //  
00589     for (i = 0; i < sin_numtexinfo; i++)
00590     {
00591         for (j=0 ; j<8 ; j++)
00592             sin_texinfo[i].vecs[0][j] = LittleFloat (sin_texinfo[i].vecs[0][j]);
00593 #ifdef SIN
00594       sin_texinfo[i].trans_mag = LittleFloat( sin_texinfo[i].trans_mag );     
00595       sin_texinfo[i].trans_angle = LittleLong( sin_texinfo[i].trans_angle );     
00596       sin_texinfo[i].animtime = LittleFloat( sin_texinfo[i].animtime );     
00597       sin_texinfo[i].nonlit = LittleFloat( sin_texinfo[i].nonlit );     
00598       sin_texinfo[i].translucence = LittleFloat( sin_texinfo[i].translucence );     
00599       sin_texinfo[i].friction = LittleFloat( sin_texinfo[i].friction );     
00600       sin_texinfo[i].restitution = LittleFloat( sin_texinfo[i].restitution );     
00601         sin_texinfo[i].flags = LittleUnsigned (sin_texinfo[i].flags);
00602 #else
00603         sin_texinfo[i].value = LittleLong (sin_texinfo[i].value);
00604         sin_texinfo[i].flags = LittleLong (sin_texinfo[i].flags);
00605 #endif
00606         sin_texinfo[i].nexttexinfo = LittleLong (sin_texinfo[i].nexttexinfo);
00607     }
00608 
00609 #ifdef SIN
00610 //
00611 // lightinfos
00612 //  
00613     for (i = 0; i < sin_numlightinfo; i++)
00614     {
00615         for (j=0 ; j<3 ; j++)
00616          {
00617             sin_lightinfo[i].color[j] = LittleFloat (sin_lightinfo[i].color[j]);
00618          }
00619         sin_lightinfo[i].value = LittleLong (sin_lightinfo[i].value);
00620       sin_lightinfo[i].direct = LittleFloat( sin_lightinfo[i].direct );     
00621       sin_lightinfo[i].directangle = LittleFloat( sin_lightinfo[i].directangle );     
00622       sin_lightinfo[i].directstyle = LittleFloat( sin_lightinfo[i].directstyle );     
00623     }
00624 #endif
00625     
00626 //
00627 // faces
00628 //
00629     for (i=0 ; i<sin_numfaces ; i++)
00630     {
00631         sin_dfaces[i].texinfo = LittleShort (sin_dfaces[i].texinfo);
00632 #ifdef SIN
00633         sin_dfaces[i].lightinfo = LittleLong (sin_dfaces[i].lightinfo);
00634         sin_dfaces[i].planenum = LittleUnsignedShort (sin_dfaces[i].planenum);
00635 #else
00636         sin_dfaces[i].planenum = LittleShort (sin_dfaces[i].planenum);
00637 #endif
00638         sin_dfaces[i].side = LittleShort (sin_dfaces[i].side);
00639         sin_dfaces[i].lightofs = LittleLong (sin_dfaces[i].lightofs);
00640         sin_dfaces[i].firstedge = LittleLong (sin_dfaces[i].firstedge);
00641         sin_dfaces[i].numedges = LittleShort (sin_dfaces[i].numedges);
00642     }
00643 
00644 //
00645 // nodes
00646 //
00647     for (i=0 ; i<sin_numnodes ; i++)
00648     {
00649         sin_dnodes[i].planenum = LittleLong (sin_dnodes[i].planenum);
00650         for (j=0 ; j<3 ; j++)
00651         {
00652             sin_dnodes[i].mins[j] = LittleShort (sin_dnodes[i].mins[j]);
00653             sin_dnodes[i].maxs[j] = LittleShort (sin_dnodes[i].maxs[j]);
00654         }
00655         sin_dnodes[i].children[0] = LittleLong (sin_dnodes[i].children[0]);
00656         sin_dnodes[i].children[1] = LittleLong (sin_dnodes[i].children[1]);
00657 #ifdef SIN
00658         sin_dnodes[i].firstface = LittleUnsignedShort (sin_dnodes[i].firstface);
00659         sin_dnodes[i].numfaces = LittleUnsignedShort (sin_dnodes[i].numfaces);
00660 #else
00661         sin_dnodes[i].firstface = LittleShort (sin_dnodes[i].firstface);
00662         sin_dnodes[i].numfaces = LittleShort (sin_dnodes[i].numfaces);
00663 #endif
00664     }
00665 
00666 //
00667 // leafs
00668 //
00669     for (i=0 ; i<sin_numleafs ; i++)
00670     {
00671         sin_dleafs[i].contents = LittleLong (sin_dleafs[i].contents);
00672         sin_dleafs[i].cluster = LittleShort (sin_dleafs[i].cluster);
00673         sin_dleafs[i].area = LittleShort (sin_dleafs[i].area);
00674         for (j=0 ; j<3 ; j++)
00675         {
00676             sin_dleafs[i].mins[j] = LittleShort (sin_dleafs[i].mins[j]);
00677             sin_dleafs[i].maxs[j] = LittleShort (sin_dleafs[i].maxs[j]);
00678         }
00679 #ifdef SIN
00680         sin_dleafs[i].firstleafface = LittleUnsignedShort (sin_dleafs[i].firstleafface);
00681         sin_dleafs[i].numleaffaces = LittleUnsignedShort (sin_dleafs[i].numleaffaces);
00682         sin_dleafs[i].firstleafbrush = LittleUnsignedShort (sin_dleafs[i].firstleafbrush);
00683         sin_dleafs[i].numleafbrushes = LittleUnsignedShort (sin_dleafs[i].numleafbrushes);
00684 #else
00685         sin_dleafs[i].firstleafface = LittleShort (sin_dleafs[i].firstleafface);
00686         sin_dleafs[i].numleaffaces = LittleShort (sin_dleafs[i].numleaffaces);
00687         sin_dleafs[i].firstleafbrush = LittleShort (sin_dleafs[i].firstleafbrush);
00688         sin_dleafs[i].numleafbrushes = LittleShort (sin_dleafs[i].numleafbrushes);
00689 #endif
00690     }
00691 
00692 //
00693 // leaffaces
00694 //
00695     for (i=0 ; i<sin_numleaffaces ; i++)
00696         sin_dleaffaces[i] = LittleShort (sin_dleaffaces[i]);
00697 
00698 //
00699 // leafbrushes
00700 //
00701     for (i=0 ; i<sin_numleafbrushes ; i++)
00702         sin_dleafbrushes[i] = LittleShort (sin_dleafbrushes[i]);
00703 
00704 //
00705 // surfedges
00706 //
00707     for (i=0 ; i<sin_numsurfedges ; i++)
00708         sin_dsurfedges[i] = LittleLong (sin_dsurfedges[i]);
00709 
00710 //
00711 // edges
00712 //
00713     for (i=0 ; i<sin_numedges ; i++)
00714     {
00715 #ifdef SIN
00716         sin_dedges[i].v[0] = LittleUnsignedShort (sin_dedges[i].v[0]);
00717         sin_dedges[i].v[1] = LittleUnsignedShort (sin_dedges[i].v[1]);
00718 #else
00719         sin_dedges[i].v[0] = LittleShort (sin_dedges[i].v[0]);
00720         sin_dedges[i].v[1] = LittleShort (sin_dedges[i].v[1]);
00721 #endif
00722     }
00723 
00724 //
00725 // brushes
00726 //
00727     for (i=0 ; i<sin_numbrushes ; i++)
00728     {
00729         sin_dbrushes[i].firstside = LittleLong (sin_dbrushes[i].firstside);
00730         sin_dbrushes[i].numsides = LittleLong (sin_dbrushes[i].numsides);
00731         sin_dbrushes[i].contents = LittleLong (sin_dbrushes[i].contents);
00732     }
00733 
00734 //
00735 // areas
00736 //
00737     for (i=0 ; i<sin_numareas ; i++)
00738     {
00739         sin_dareas[i].numareaportals = LittleLong (sin_dareas[i].numareaportals);
00740         sin_dareas[i].firstareaportal = LittleLong (sin_dareas[i].firstareaportal);
00741     }
00742 
00743 //
00744 // areasportals
00745 //
00746     for (i=0 ; i<sin_numareaportals ; i++)
00747     {
00748         sin_dareaportals[i].portalnum = LittleLong (sin_dareaportals[i].portalnum);
00749         sin_dareaportals[i].otherarea = LittleLong (sin_dareaportals[i].otherarea);
00750     }
00751 
00752 //
00753 // brushsides
00754 //
00755     for (i=0 ; i<sin_numbrushsides ; i++)
00756     {
00757 #ifdef SIN
00758         sin_dbrushsides[i].planenum = LittleUnsignedShort (sin_dbrushsides[i].planenum);
00759 #else
00760         sin_dbrushsides[i].planenum = LittleShort (sin_dbrushsides[i].planenum);
00761 #endif
00762         sin_dbrushsides[i].texinfo = LittleShort (sin_dbrushsides[i].texinfo);
00763 #ifdef SIN
00764         sin_dbrushsides[i].lightinfo = LittleLong (sin_dbrushsides[i].lightinfo);
00765 #endif
00766     }
00767 
00768 //
00769 // visibility
00770 //
00771     if (todisk)
00772         j = sin_dvis->numclusters;
00773     else
00774         j = LittleLong(sin_dvis->numclusters);
00775     sin_dvis->numclusters = LittleLong (sin_dvis->numclusters);
00776     for (i=0 ; i<j ; i++)
00777     {
00778         sin_dvis->bitofs[i][0] = LittleLong (sin_dvis->bitofs[i][0]);
00779         sin_dvis->bitofs[i][1] = LittleLong (sin_dvis->bitofs[i][1]);
00780     }
00781 } //end of the function Sin_SwapBSPFile
00782 
00783 
00784 sin_dheader_t   *header;
00785 #ifdef SIN
00786 int Sin_CopyLump (int lump, void *dest, int size, int maxsize)
00787 {
00788     int     length, ofs;
00789 
00790     length = header->lumps[lump].filelen;
00791     ofs = header->lumps[lump].fileofs;
00792     
00793     if (length % size)
00794         Error ("Sin_LoadBSPFile: odd lump size");
00795 
00796    if ((length/size) > maxsize)
00797       Error ("Sin_LoadBSPFile: exceeded max size for lump %d size %d > maxsize %d\n", lump, (length/size), maxsize );
00798     
00799     memcpy (dest, (byte *)header + ofs, length);
00800 
00801     return length / size;
00802 }
00803 #else
00804 int Sin_CopyLump (int lump, void *dest, int size)
00805 {
00806     int     length, ofs;
00807 
00808     length = header->lumps[lump].filelen;
00809     ofs = header->lumps[lump].fileofs;
00810     
00811     if (length % size)
00812         Error ("Sin_LoadBSPFile: odd lump size");
00813     
00814     memcpy (dest, (byte *)header + ofs, length);
00815 
00816     return length / size;
00817 }
00818 #endif
00819 
00820 /*
00821 =============
00822 Sin_LoadBSPFile
00823 =============
00824 */
00825 void    Sin_LoadBSPFile(char *filename, int offset, int length)
00826 {
00827     int         i;
00828     
00829 //
00830 // load the file header
00831 //
00832     LoadFile (filename, (void **)&header, offset, length);
00833 
00834 // swap the header
00835     for (i=0 ; i< sizeof(sin_dheader_t)/4 ; i++)
00836         ((int *)header)[i] = LittleLong ( ((int *)header)[i]);
00837 
00838     if (header->ident != SIN_BSPHEADER && header->ident != SINGAME_BSPHEADER)
00839         Error ("%s is not a IBSP file", filename);
00840     if (header->version != SIN_BSPVERSION && header->version != SINGAME_BSPVERSION)
00841         Error ("%s is version %i, not %i", filename, header->version, SIN_BSPVERSION);
00842 
00843 #ifdef SIN
00844     sin_nummodels = Sin_CopyLump (SIN_LUMP_MODELS, sin_dmodels, sizeof(sin_dmodel_t), SIN_MAX_MAP_MODELS);
00845     sin_numvertexes = Sin_CopyLump (SIN_LUMP_VERTEXES, sin_dvertexes, sizeof(sin_dvertex_t), SIN_MAX_MAP_VERTS);
00846     sin_numplanes = Sin_CopyLump (SIN_LUMP_PLANES, sin_dplanes, sizeof(sin_dplane_t), SIN_MAX_MAP_PLANES);
00847     sin_numleafs = Sin_CopyLump (SIN_LUMP_LEAFS, sin_dleafs, sizeof(sin_dleaf_t), SIN_MAX_MAP_LEAFS);
00848     sin_numnodes = Sin_CopyLump (SIN_LUMP_NODES, sin_dnodes, sizeof(sin_dnode_t), SIN_MAX_MAP_NODES);
00849     sin_numtexinfo = Sin_CopyLump (SIN_LUMP_TEXINFO, sin_texinfo, sizeof(sin_texinfo_t), SIN_MAX_MAP_TEXINFO);
00850     sin_numfaces = Sin_CopyLump (SIN_LUMP_FACES, sin_dfaces, sizeof(sin_dface_t), SIN_MAX_MAP_FACES);
00851     sin_numleaffaces = Sin_CopyLump (SIN_LUMP_LEAFFACES, sin_dleaffaces, sizeof(sin_dleaffaces[0]), SIN_MAX_MAP_LEAFFACES);
00852     sin_numleafbrushes = Sin_CopyLump (SIN_LUMP_LEAFBRUSHES, sin_dleafbrushes, sizeof(sin_dleafbrushes[0]), SIN_MAX_MAP_LEAFBRUSHES);
00853     sin_numsurfedges = Sin_CopyLump (SIN_LUMP_SURFEDGES, sin_dsurfedges, sizeof(sin_dsurfedges[0]), SIN_MAX_MAP_SURFEDGES);
00854     sin_numedges = Sin_CopyLump (SIN_LUMP_EDGES, sin_dedges, sizeof(sin_dedge_t), SIN_MAX_MAP_EDGES);
00855     sin_numbrushes = Sin_CopyLump (SIN_LUMP_BRUSHES, sin_dbrushes, sizeof(sin_dbrush_t), SIN_MAX_MAP_BRUSHES);
00856     sin_numbrushsides = Sin_CopyLump (SIN_LUMP_BRUSHSIDES, sin_dbrushsides, sizeof(sin_dbrushside_t), SIN_MAX_MAP_BRUSHSIDES);
00857     sin_numareas = Sin_CopyLump (SIN_LUMP_AREAS, sin_dareas, sizeof(sin_darea_t), SIN_MAX_MAP_AREAS);
00858     sin_numareaportals = Sin_CopyLump (SIN_LUMP_AREAPORTALS, sin_dareaportals, sizeof(sin_dareaportal_t), SIN_MAX_MAP_AREAPORTALS);
00859     sin_numlightinfo = Sin_CopyLump (SIN_LUMP_LIGHTINFO, sin_lightinfo, sizeof(sin_lightvalue_t), SIN_MAX_MAP_LIGHTINFO);
00860 
00861     sin_visdatasize = Sin_CopyLump (SIN_LUMP_VISIBILITY, sin_dvisdata, 1, SIN_MAX_MAP_VISIBILITY);
00862     sin_lightdatasize = Sin_CopyLump (SIN_LUMP_LIGHTING, sin_dlightdata, 1, SIN_MAX_MAP_LIGHTING);
00863     sin_entdatasize = Sin_CopyLump (SIN_LUMP_ENTITIES, sin_dentdata, 1, SIN_MAX_MAP_ENTSTRING);
00864 
00865     Sin_CopyLump (SIN_LUMP_POP, sin_dpop, 1, sizeof(sin_dpop));
00866 #else
00867     sin_nummodels = Sin_CopyLump (SIN_LUMP_MODELS, sin_dmodels, sizeof(sin_dmodel_t));
00868     sin_numvertexes = Sin_CopyLump (SIN_LUMP_VERTEXES, sin_dvertexes, sizeof(sin_dvertex_t));
00869     sin_numplanes = Sin_CopyLump (SIN_LUMP_PLANES, sin_dplanes, sizeof(sin_dplane_t));
00870     sin_numleafs = Sin_CopyLump (SIN_LUMP_LEAFS, sin_dleafs, sizeof(sin_dleaf_t));
00871     sin_numnodes = Sin_CopyLump (SIN_LUMP_NODES, sin_dnodes, sizeof(sin_dnode_t));
00872     sin_numtexinfo = Sin_CopyLump (SIN_LUMP_TEXINFO, sin_texinfo, sizeof(sin_texinfo_t));
00873     sin_numfaces = Sin_CopyLump (SIN_LUMP_FACES, sin_dfaces, sizeof(sin_dface_t));
00874     sin_numleaffaces = Sin_CopyLump (SIN_LUMP_LEAFFACES, sin_dleaffaces, sizeof(sin_dleaffaces[0]));
00875     sin_numleafbrushes = Sin_CopyLump (SIN_LUMP_LEAFBRUSHES, sin_dleafbrushes, sizeof(sin_dleafbrushes[0]));
00876     sin_numsurfedges = Sin_CopyLump (SIN_LUMP_SURFEDGES, sin_dsurfedges, sizeof(sin_dsurfedges[0]));
00877     sin_numedges = Sin_CopyLump (SIN_LUMP_EDGES, sin_dedges, sizeof(sin_dedge_t));
00878     sin_numbrushes = Sin_CopyLump (SIN_LUMP_BRUSHES, sin_dbrushes, sizeof(sin_dbrush_t));
00879     sin_numbrushsides = Sin_CopyLump (SIN_LUMP_BRUSHSIDES, sin_dbrushsides, sizeof(sin_dbrushside_t));
00880     sin_numareas = Sin_CopyLump (SIN_LUMP_AREAS, sin_dareas, sizeof(sin_darea_t));
00881     sin_numareaportals = Sin_CopyLump (SIN_LUMP_AREAPORTALS, sin_dareaportals, sizeof(sin_dareaportal_t));
00882 
00883     sin_visdatasize = Sin_CopyLump (SIN_LUMP_VISIBILITY, sin_dvisdata, 1);
00884     sin_lightdatasize = Sin_CopyLump (SIN_LUMP_LIGHTING, sin_dlightdata, 1);
00885     sin_entdatasize = Sin_CopyLump (SIN_LUMP_ENTITIES, sin_dentdata, 1);
00886 
00887     Sin_CopyLump (SIN_LUMP_POP, sin_dpop, 1);
00888 #endif
00889 
00890     FreeMemory(header);     // everything has been copied out
00891         
00892 //
00893 // swap everything
00894 //  
00895     Sin_SwapBSPFile (false);
00896 } //end of the function Sin_LoadBSPFile
00897 
00898 /*
00899 =============
00900 Sin_LoadBSPFilesTexinfo
00901 
00902 Only loads the sin_texinfo lump, so qdata can scan for textures
00903 =============
00904 */
00905 void    Sin_LoadBSPFileTexinfo (char *filename)
00906 {
00907     int         i;
00908     FILE        *f;
00909     int     length, ofs;
00910 
00911     header = GetMemory(sizeof(sin_dheader_t));
00912 
00913     f = fopen (filename, "rb");
00914     fread (header, sizeof(sin_dheader_t), 1, f);
00915 
00916 // swap the header
00917     for (i=0 ; i< sizeof(sin_dheader_t)/4 ; i++)
00918         ((int *)header)[i] = LittleLong ( ((int *)header)[i]);
00919 
00920     if (header->ident != SIN_BSPHEADER && header->ident != SINGAME_BSPHEADER)
00921         Error ("%s is not a IBSP file", filename);
00922     if (header->version != SIN_BSPVERSION && header->version != SINGAME_BSPVERSION)
00923         Error ("%s is version %i, not %i", filename, header->version, SIN_BSPVERSION);
00924 
00925 
00926     length = header->lumps[SIN_LUMP_TEXINFO].filelen;
00927     ofs = header->lumps[SIN_LUMP_TEXINFO].fileofs;
00928 
00929     fseek (f, ofs, SEEK_SET);
00930     fread (sin_texinfo, length, 1, f);
00931     fclose (f);
00932 
00933     sin_numtexinfo = length / sizeof(sin_texinfo_t);
00934 
00935     FreeMemory(header);     // everything has been copied out
00936         
00937     Sin_SwapBSPFile (false);
00938 } //end of the function Sin_LoadBSPFilesTexinfo
00939 
00940 
00941 //============================================================================
00942 
00943 FILE        *wadfile;
00944 sin_dheader_t   outheader;
00945 
00946 #ifdef SIN
00947 void Sin_AddLump (int lumpnum, void *data, int len, int size, int maxsize)
00948 {
00949     sin_lump_t *lump;
00950     int totallength;
00951 
00952     totallength = len*size;
00953 
00954     if (len > maxsize)
00955         Error ("Sin_WriteBSPFile: exceeded max size for lump %d size %d > maxsize %d\n", lumpnum, len, maxsize );
00956 
00957     lump = &header->lumps[lumpnum];
00958     
00959     lump->fileofs = LittleLong( ftell(wadfile) );
00960     lump->filelen = LittleLong(totallength);
00961     SafeWrite (wadfile, data, (totallength+3)&~3);
00962 }
00963 #else
00964 void Sin_AddLump (int lumpnum, void *data, int len)
00965 {
00966     sin_lump_t *lump;
00967 
00968     lump = &header->lumps[lumpnum];
00969     
00970     lump->fileofs = LittleLong( ftell(wadfile) );
00971     lump->filelen = LittleLong(len);
00972     SafeWrite (wadfile, data, (len+3)&~3);
00973 }
00974 #endif
00975 /*
00976 =============
00977 Sin_WriteBSPFile
00978 
00979 Swaps the bsp file in place, so it should not be referenced again
00980 =============
00981 */
00982 void    Sin_WriteBSPFile (char *filename)
00983 {       
00984     header = &outheader;
00985     memset (header, 0, sizeof(sin_dheader_t));
00986     
00987     Sin_SwapBSPFile (true);
00988 
00989     header->ident = LittleLong (SIN_BSPHEADER);
00990     header->version = LittleLong (SIN_BSPVERSION);
00991     
00992     wadfile = SafeOpenWrite (filename);
00993     SafeWrite (wadfile, header, sizeof(sin_dheader_t)); // overwritten later
00994 
00995 #ifdef SIN
00996     Sin_AddLump (SIN_LUMP_PLANES, sin_dplanes, sin_numplanes, sizeof(sin_dplane_t), SIN_MAX_MAP_PLANES);
00997     Sin_AddLump (SIN_LUMP_LEAFS, sin_dleafs, sin_numleafs, sizeof(sin_dleaf_t), SIN_MAX_MAP_LEAFS);
00998     Sin_AddLump (SIN_LUMP_VERTEXES, sin_dvertexes, sin_numvertexes, sizeof(sin_dvertex_t), SIN_MAX_MAP_VERTS);
00999     Sin_AddLump (SIN_LUMP_NODES, sin_dnodes, sin_numnodes, sizeof(sin_dnode_t), SIN_MAX_MAP_NODES);
01000     Sin_AddLump (SIN_LUMP_TEXINFO, sin_texinfo, sin_numtexinfo, sizeof(sin_texinfo_t), SIN_MAX_MAP_TEXINFO);
01001     Sin_AddLump (SIN_LUMP_FACES, sin_dfaces, sin_numfaces, sizeof(sin_dface_t), SIN_MAX_MAP_FACES);
01002     Sin_AddLump (SIN_LUMP_BRUSHES, sin_dbrushes, sin_numbrushes, sizeof(sin_dbrush_t), SIN_MAX_MAP_BRUSHES);
01003     Sin_AddLump (SIN_LUMP_BRUSHSIDES, sin_dbrushsides, sin_numbrushsides, sizeof(sin_dbrushside_t), SIN_MAX_MAP_BRUSHSIDES);
01004     Sin_AddLump (SIN_LUMP_LEAFFACES, sin_dleaffaces, sin_numleaffaces, sizeof(sin_dleaffaces[0]), SIN_MAX_MAP_LEAFFACES);
01005     Sin_AddLump (SIN_LUMP_LEAFBRUSHES, sin_dleafbrushes, sin_numleafbrushes, sizeof(sin_dleafbrushes[0]), SIN_MAX_MAP_LEAFBRUSHES);
01006     Sin_AddLump (SIN_LUMP_SURFEDGES, sin_dsurfedges, sin_numsurfedges, sizeof(sin_dsurfedges[0]), SIN_MAX_MAP_SURFEDGES);
01007     Sin_AddLump (SIN_LUMP_EDGES, sin_dedges, sin_numedges, sizeof(sin_dedge_t), SIN_MAX_MAP_EDGES);
01008     Sin_AddLump (SIN_LUMP_MODELS, sin_dmodels, sin_nummodels, sizeof(sin_dmodel_t), SIN_MAX_MAP_MODELS);
01009     Sin_AddLump (SIN_LUMP_AREAS, sin_dareas, sin_numareas, sizeof(sin_darea_t), SIN_MAX_MAP_AREAS);
01010     Sin_AddLump (SIN_LUMP_AREAPORTALS, sin_dareaportals, sin_numareaportals, sizeof(sin_dareaportal_t), SIN_MAX_MAP_AREAPORTALS);
01011     Sin_AddLump (SIN_LUMP_LIGHTINFO, sin_lightinfo, sin_numlightinfo, sizeof(sin_lightvalue_t), SIN_MAX_MAP_LIGHTINFO);
01012 
01013     Sin_AddLump (SIN_LUMP_LIGHTING, sin_dlightdata, sin_lightdatasize, 1, SIN_MAX_MAP_LIGHTING);
01014     Sin_AddLump (SIN_LUMP_VISIBILITY, sin_dvisdata, sin_visdatasize, 1, SIN_MAX_MAP_VISIBILITY);
01015     Sin_AddLump (SIN_LUMP_ENTITIES, sin_dentdata, sin_entdatasize, 1, SIN_MAX_MAP_ENTSTRING);
01016     Sin_AddLump (SIN_LUMP_POP, sin_dpop, sizeof(sin_dpop), 1, sizeof(sin_dpop));
01017 #else
01018     Sin_AddLump (SIN_LUMP_PLANES, sin_dplanes, sin_numplanes*sizeof(sin_dplane_t));
01019     Sin_AddLump (SIN_LUMP_LEAFS, sin_dleafs, sin_numleafs*sizeof(sin_dleaf_t));
01020     Sin_AddLump (SIN_LUMP_VERTEXES, sin_dvertexes, sin_numvertexes*sizeof(sin_dvertex_t));
01021     Sin_AddLump (SIN_LUMP_NODES, sin_dnodes, sin_numnodes*sizeof(sin_dnode_t));
01022     Sin_AddLump (SIN_LUMP_TEXINFO, sin_texinfo, sin_numtexinfo*sizeof(sin_texinfo_t));
01023     Sin_AddLump (SIN_LUMP_FACES, sin_dfaces, sin_numfaces*sizeof(sin_dface_t));
01024     Sin_AddLump (SIN_LUMP_BRUSHES, sin_dbrushes, sin_numbrushes*sizeof(sin_dbrush_t));
01025     Sin_AddLump (SIN_LUMP_BRUSHSIDES, sin_dbrushsides, sin_numbrushsides*sizeof(sin_dbrushside_t));
01026     Sin_AddLump (SIN_LUMP_LEAFFACES, sin_dleaffaces, sin_numleaffaces*sizeof(sin_dleaffaces[0]));
01027     Sin_AddLump (SIN_LUMP_LEAFBRUSHES, sin_dleafbrushes, sin_numleafbrushes*sizeof(sin_dleafbrushes[0]));
01028     Sin_AddLump (SIN_LUMP_SURFEDGES, sin_dsurfedges, sin_numsurfedges*sizeof(sin_dsurfedges[0]));
01029     Sin_AddLump (SIN_LUMP_EDGES, sin_dedges, sin_numedges*sizeof(sin_dedge_t));
01030     Sin_AddLump (SIN_LUMP_MODELS, sin_dmodels, sin_nummodels*sizeof(sin_dmodel_t));
01031     Sin_AddLump (SIN_LUMP_AREAS, sin_dareas, sin_numareas*sizeof(sin_darea_t));
01032     Sin_AddLump (SIN_LUMP_AREAPORTALS, sin_dareaportals, sin_numareaportals*sizeof(sin_dareaportal_t));
01033 
01034     Sin_AddLump (SIN_LUMP_LIGHTING, sin_dlightdata, sin_lightdatasize);
01035     Sin_AddLump (SIN_LUMP_VISIBILITY, sin_dvisdata, sin_visdatasize);
01036     Sin_AddLump (SIN_LUMP_ENTITIES, sin_dentdata, sin_entdatasize);
01037     Sin_AddLump (SIN_LUMP_POP, sin_dpop, sizeof(sin_dpop));
01038 #endif
01039     
01040     fseek (wadfile, 0, SEEK_SET);
01041     SafeWrite (wadfile, header, sizeof(sin_dheader_t));
01042     fclose (wadfile);   
01043 }
01044 
01045 //============================================================================
01046 
01047 
01048 //============================================
01049 
01050 /*
01051 ================
01052 ParseEntities
01053 
01054 Parses the sin_dentdata string into entities
01055 ================
01056 */
01057 void Sin_ParseEntities (void)
01058 {
01059     script_t *script;
01060 
01061     num_entities = 0;
01062     script = LoadScriptMemory(sin_dentdata, sin_entdatasize, "*sin bsp file");
01063     SetScriptFlags(script, SCFL_NOSTRINGWHITESPACES |
01064                                     SCFL_NOSTRINGESCAPECHARS);
01065 
01066     while(ParseEntity(script))
01067     {
01068     } //end while
01069 
01070     FreeScript(script);
01071 } //end of the function Sin_ParseEntities
01072 
01073 
01074 /*
01075 ================
01076 UnparseEntities
01077 
01078 Generates the sin_dentdata string from all the entities
01079 ================
01080 */
01081 void Sin_UnparseEntities (void)
01082 {
01083     char    *buf, *end;
01084     epair_t *ep;
01085     char    line[2048];
01086     int     i;
01087     char    key[1024], value[1024];
01088 
01089     buf = sin_dentdata;
01090     end = buf;
01091     *end = 0;
01092     
01093     for (i=0 ; i<num_entities ; i++)
01094     {
01095         ep = entities[i].epairs;
01096         if (!ep)
01097             continue;   // ent got removed
01098         
01099         strcat (end,"{\n");
01100         end += 2;
01101                 
01102         for (ep = entities[i].epairs ; ep ; ep=ep->next)
01103         {
01104             strcpy (key, ep->key);
01105             StripTrailing (key);
01106             strcpy (value, ep->value);
01107             StripTrailing (value);
01108                 
01109             sprintf (line, "\"%s\" \"%s\"\n", key, value);
01110             strcat (end, line);
01111             end += strlen(line);
01112         }
01113         strcat (end,"}\n");
01114         end += 2;
01115 
01116         if (end > buf + SIN_MAX_MAP_ENTSTRING)
01117             Error ("Entity text too long");
01118     }
01119     sin_entdatasize = end - buf + 1;
01120 } //end of the function Sin_UnparseEntities
01121 
01122 #ifdef SIN
01123 void  FreeValueKeys(entity_t *ent)
01124 {
01125     epair_t *ep,*next;
01126 
01127     for (ep=ent->epairs ; ep ; ep=next)
01128     {
01129         next = ep->next;
01130         FreeMemory(ep->value);
01131         FreeMemory(ep->key);
01132         FreeMemory(ep);
01133     }
01134     ent->epairs = NULL;
01135 }
01136 #endif
01137 
01138 /*
01139 =============
01140 Sin_PrintBSPFileSizes
01141 
01142 Dumps info about current file
01143 =============
01144 */
01145 void Sin_PrintBSPFileSizes (void)
01146 {
01147     if (!num_entities)
01148         Sin_ParseEntities ();
01149 
01150     Log_Print("%6i models       %7i\n"
01151         ,sin_nummodels, (int)(sin_nummodels*sizeof(sin_dmodel_t)));
01152     Log_Print("%6i brushes      %7i\n"
01153         ,sin_numbrushes, (int)(sin_numbrushes*sizeof(sin_dbrush_t)));
01154     Log_Print("%6i brushsides   %7i\n"
01155         ,sin_numbrushsides, (int)(sin_numbrushsides*sizeof(sin_dbrushside_t)));
01156     Log_Print("%6i planes       %7i\n"
01157         ,sin_numplanes, (int)(sin_numplanes*sizeof(sin_dplane_t)));
01158     Log_Print("%6i texinfo      %7i\n"
01159         ,sin_numtexinfo, (int)(sin_numtexinfo*sizeof(sin_texinfo_t)));
01160 #ifdef SIN
01161     Log_Print("%6i lightinfo    %7i\n"
01162         ,sin_numlightinfo, (int)(sin_numlightinfo*sizeof(sin_lightvalue_t)));
01163 #endif
01164     Log_Print("%6i entdata      %7i\n", num_entities, sin_entdatasize);
01165 
01166     Log_Print("\n");
01167 
01168     Log_Print("%6i vertexes     %7i\n"
01169         ,sin_numvertexes, (int)(sin_numvertexes*sizeof(sin_dvertex_t)));
01170     Log_Print("%6i nodes        %7i\n"
01171         ,sin_numnodes, (int)(sin_numnodes*sizeof(sin_dnode_t)));
01172     Log_Print("%6i faces        %7i\n"
01173         ,sin_numfaces, (int)(sin_numfaces*sizeof(sin_dface_t)));
01174     Log_Print("%6i leafs        %7i\n"
01175         ,sin_numleafs, (int)(sin_numleafs*sizeof(sin_dleaf_t)));
01176     Log_Print("%6i leaffaces    %7i\n"
01177         ,sin_numleaffaces, (int)(sin_numleaffaces*sizeof(sin_dleaffaces[0])));
01178     Log_Print("%6i leafbrushes  %7i\n"
01179         ,sin_numleafbrushes, (int)(sin_numleafbrushes*sizeof(sin_dleafbrushes[0])));
01180     Log_Print("%6i surfedges    %7i\n"
01181         ,sin_numsurfedges, (int)(sin_numsurfedges*sizeof(sin_dsurfedges[0])));
01182     Log_Print("%6i edges        %7i\n"
01183         ,sin_numedges, (int)(sin_numedges*sizeof(sin_dedge_t)));
01184     Log_Print("       lightdata    %7i\n", sin_lightdatasize);
01185     Log_Print("       visdata      %7i\n", sin_visdatasize);
01186 }

Generated on Thu Aug 25 12:37:16 2005 for Quake III Arena by  doxygen 1.3.9.1