00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "qbsp.h"
00023
00024 int c_nofaces;
00025 int c_facenodes;
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 int planeused[MAX_MAP_PLANES];
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 void EmitPlanes (void)
00047 {
00048 int i;
00049 dplane_t *dp;
00050 plane_t *mp;
00051
00052
00053
00054 mp = mapplanes;
00055 for (i=0 ; i<nummapplanes ; i++, mp++)
00056 {
00057 dp = &dplanes[numplanes];
00058
00059 VectorCopy ( mp->normal, dp->normal);
00060 dp->dist = mp->dist;
00061 dp->type = mp->type;
00062 numplanes++;
00063 if (numplanes >= MAX_MAP_PLANES)
00064 Error("MAX_MAP_PLANES");
00065 }
00066 }
00067
00068
00069
00070
00071 void EmitMarkFace (dleaf_t *leaf_p, face_t *f)
00072 {
00073 int i;
00074 int facenum;
00075
00076 while (f->merged)
00077 f = f->merged;
00078
00079 if (f->split[0])
00080 {
00081 EmitMarkFace (leaf_p, f->split[0]);
00082 EmitMarkFace (leaf_p, f->split[1]);
00083 return;
00084 }
00085
00086 facenum = f->outputnumber;
00087 if (facenum == -1)
00088 return;
00089
00090 if (facenum < 0 || facenum >= numfaces)
00091 Error ("Bad leafface");
00092 for (i=leaf_p->firstleafface ; i<numleaffaces ; i++)
00093 if (dleaffaces[i] == facenum)
00094 break;
00095 if (i == numleaffaces)
00096 {
00097 if (numleaffaces >= MAX_MAP_LEAFFACES)
00098 Error ("MAX_MAP_LEAFFACES");
00099
00100 dleaffaces[numleaffaces] = facenum;
00101 numleaffaces++;
00102 }
00103
00104 }
00105
00106
00107
00108
00109
00110
00111
00112 void EmitLeaf (node_t *node)
00113 {
00114 dleaf_t *leaf_p;
00115 portal_t *p;
00116 int s;
00117 face_t *f;
00118 bspbrush_t *b;
00119 int i;
00120 int brushnum;
00121
00122
00123 if (numleafs >= MAX_MAP_LEAFS)
00124 Error ("MAX_MAP_LEAFS");
00125
00126 leaf_p = &dleafs[numleafs];
00127 numleafs++;
00128
00129 leaf_p->contents = node->contents;
00130 leaf_p->cluster = node->cluster;
00131 leaf_p->area = node->area;
00132
00133
00134
00135
00136 VectorCopy (node->mins, leaf_p->mins);
00137 VectorCopy (node->maxs, leaf_p->maxs);
00138
00139
00140
00141
00142 leaf_p->firstleafbrush = numleafbrushes;
00143 for (b=node->brushlist ; b ; b=b->next)
00144 {
00145 if (numleafbrushes >= MAX_MAP_LEAFBRUSHES)
00146 Error ("MAX_MAP_LEAFBRUSHES");
00147
00148 brushnum = b->original - mapbrushes;
00149 for (i=leaf_p->firstleafbrush ; i<numleafbrushes ; i++)
00150 if (dleafbrushes[i] == brushnum)
00151 break;
00152 if (i == numleafbrushes)
00153 {
00154 dleafbrushes[numleafbrushes] = brushnum;
00155 numleafbrushes++;
00156 }
00157 }
00158 leaf_p->numleafbrushes = numleafbrushes - leaf_p->firstleafbrush;
00159
00160
00161
00162
00163 if (leaf_p->contents & CONTENTS_SOLID)
00164 return;
00165
00166 leaf_p->firstleafface = numleaffaces;
00167
00168 for (p = node->portals ; p ; p = p->next[s])
00169 {
00170 s = (p->nodes[1] == node);
00171 f = p->face[s];
00172 if (!f)
00173 continue;
00174
00175 EmitMarkFace (leaf_p, f);
00176 }
00177
00178 leaf_p->numleaffaces = numleaffaces - leaf_p->firstleafface;
00179 }
00180
00181
00182
00183
00184
00185
00186
00187 void EmitFace (face_t *f)
00188 {
00189 dface_t *df;
00190 int i;
00191 int e;
00192
00193 f->outputnumber = -1;
00194
00195 if (f->numpoints < 3)
00196 {
00197 return;
00198 }
00199 if (f->merged || f->split[0] || f->split[1])
00200 {
00201 return;
00202 }
00203
00204
00205 f->outputnumber = numfaces;
00206
00207 if (numfaces >= MAX_MAP_FACES)
00208 Error ("numfaces == MAX_MAP_FACES");
00209 df = &dfaces[numfaces];
00210 numfaces++;
00211
00212
00213 df->planenum = f->planenum & (~1);
00214 df->side = f->planenum & 1;
00215
00216 df->firstedge = numsurfedges;
00217 df->numedges = f->numpoints;
00218 df->texinfo = f->texinfo;
00219 for (i=0 ; i<f->numpoints ; i++)
00220 {
00221
00222 e = GetEdge2 (f->vertexnums[i], f->vertexnums[(i+1)%f->numpoints], f);
00223 if (numsurfedges >= MAX_MAP_SURFEDGES)
00224 Error ("numsurfedges == MAX_MAP_SURFEDGES");
00225 dsurfedges[numsurfedges] = e;
00226 numsurfedges++;
00227 }
00228 }
00229
00230
00231
00232
00233
00234
00235 int EmitDrawNode_r (node_t *node)
00236 {
00237 dnode_t *n;
00238 face_t *f;
00239 int i;
00240
00241 if (node->planenum == PLANENUM_LEAF)
00242 {
00243 EmitLeaf (node);
00244 return -numleafs;
00245 }
00246
00247
00248 if (numnodes == MAX_MAP_NODES)
00249 Error ("MAX_MAP_NODES");
00250 n = &dnodes[numnodes];
00251 numnodes++;
00252
00253 VectorCopy (node->mins, n->mins);
00254 VectorCopy (node->maxs, n->maxs);
00255
00256 planeused[node->planenum]++;
00257 planeused[node->planenum^1]++;
00258
00259 if (node->planenum & 1)
00260 Error ("WriteDrawNodes_r: odd planenum");
00261 n->planenum = node->planenum;
00262 n->firstface = numfaces;
00263
00264 if (!node->faces)
00265 c_nofaces++;
00266 else
00267 c_facenodes++;
00268
00269 for (f=node->faces ; f ; f=f->next)
00270 EmitFace (f);
00271
00272 n->numfaces = numfaces - n->firstface;
00273
00274
00275
00276
00277
00278 for (i=0 ; i<2 ; i++)
00279 {
00280 if (node->children[i]->planenum == PLANENUM_LEAF)
00281 {
00282 n->children[i] = -(numleafs + 1);
00283 EmitLeaf (node->children[i]);
00284 }
00285 else
00286 {
00287 n->children[i] = numnodes;
00288 EmitDrawNode_r (node->children[i]);
00289 }
00290 }
00291
00292 return n - dnodes;
00293 }
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303 void WriteBSP (node_t *headnode)
00304 {
00305 int oldfaces;
00306
00307 c_nofaces = 0;
00308 c_facenodes = 0;
00309
00310 qprintf ("--- WriteBSP ---\n");
00311
00312 oldfaces = numfaces;
00313 dmodels[nummodels].headnode = EmitDrawNode_r (headnode);
00314 EmitAreaPortals (headnode);
00315
00316 qprintf ("%5i nodes with faces\n", c_facenodes);
00317 qprintf ("%5i nodes without faces\n", c_nofaces);
00318 qprintf ("%5i faces\n", numfaces-oldfaces);
00319 }
00320
00321
00322
00323
00324
00325
00326
00327
00328 void SetModelNumbers (void)
00329 {
00330 int i;
00331 int models;
00332 char value[10];
00333
00334 models = 1;
00335 for (i=1 ; i<num_entities ; i++)
00336 {
00337 if (entities[i].numbrushes)
00338 {
00339 sprintf (value, "*%i", models);
00340 models++;
00341 SetKeyValue (&entities[i], "model", value);
00342 }
00343 }
00344
00345 }
00346
00347
00348
00349
00350
00351
00352 #define MAX_SWITCHED_LIGHTS 32
00353 void SetLightStyles (void)
00354 {
00355 int stylenum;
00356 char *t;
00357 entity_t *e;
00358 int i, j;
00359 char value[10];
00360 char lighttargets[MAX_SWITCHED_LIGHTS][64];
00361
00362
00363
00364
00365
00366 stylenum = 0;
00367 for (i=1 ; i<num_entities ; i++)
00368 {
00369 e = &entities[i];
00370
00371 t = ValueForKey (e, "classname");
00372 if (Q_strncasecmp (t, "light", 5))
00373 continue;
00374 t = ValueForKey (e, "targetname");
00375 if (!t[0])
00376 continue;
00377
00378
00379 for (j=0 ; j<stylenum ; j++)
00380 if (!strcmp (lighttargets[j], t))
00381 break;
00382 if (j == stylenum)
00383 {
00384 if (stylenum == MAX_SWITCHED_LIGHTS)
00385 Error ("stylenum == MAX_SWITCHED_LIGHTS");
00386 strcpy (lighttargets[j], t);
00387 stylenum++;
00388 }
00389 sprintf (value, "%i", 32 + j);
00390 SetKeyValue (e, "style", value);
00391 }
00392
00393 }
00394
00395
00396
00397
00398
00399
00400
00401
00402 void EmitBrushes (void)
00403 {
00404 int i, j, bnum, s, x;
00405 dbrush_t *db;
00406 mapbrush_t *b;
00407 dbrushside_t *cp;
00408 vec3_t normal;
00409 vec_t dist;
00410 int planenum;
00411
00412 numbrushsides = 0;
00413 numbrushes = nummapbrushes;
00414
00415 for (bnum=0 ; bnum<nummapbrushes ; bnum++)
00416 {
00417 b = &mapbrushes[bnum];
00418 db = &dbrushes[bnum];
00419
00420 db->contents = b->contents;
00421 db->firstside = numbrushsides;
00422 db->numsides = b->numsides;
00423 for (j=0 ; j<b->numsides ; j++)
00424 {
00425 if (numbrushsides == MAX_MAP_BRUSHSIDES)
00426 Error ("MAX_MAP_BRUSHSIDES");
00427 cp = &dbrushsides[numbrushsides];
00428 numbrushsides++;
00429 cp->planenum = b->original_sides[j].planenum;
00430 cp->texinfo = b->original_sides[j].texinfo;
00431 }
00432
00433 #ifdef ME
00434
00435
00436
00437 #endif
00438
00439 for (x=0 ; x<3 ; x++)
00440 for (s=-1 ; s<=1 ; s+=2)
00441 {
00442
00443 VectorCopy (vec3_origin, normal);
00444 normal[x] = s;
00445 if (s == -1)
00446 dist = -b->mins[x];
00447 else
00448 dist = b->maxs[x];
00449 planenum = FindFloatPlane (normal, dist);
00450 for (i=0 ; i<b->numsides ; i++)
00451 if (b->original_sides[i].planenum == planenum)
00452 break;
00453 if (i == b->numsides)
00454 {
00455 if (numbrushsides >= MAX_MAP_BRUSHSIDES)
00456 Error ("MAX_MAP_BRUSHSIDES");
00457
00458 dbrushsides[numbrushsides].planenum = planenum;
00459 dbrushsides[numbrushsides].texinfo =
00460 dbrushsides[numbrushsides-1].texinfo;
00461 numbrushsides++;
00462 db->numsides++;
00463 }
00464 }
00465
00466 }
00467
00468 }
00469
00470
00471
00472
00473
00474
00475
00476
00477 void BeginBSPFile (void)
00478 {
00479
00480
00481 nummodels = 0;
00482 numfaces = 0;
00483 numnodes = 0;
00484 numbrushsides = 0;
00485 numvertexes = 0;
00486 numleaffaces = 0;
00487 numleafbrushes = 0;
00488 numsurfedges = 0;
00489
00490
00491 numedges = 1;
00492
00493
00494 numvertexes = 1;
00495
00496
00497 numleafs = 1;
00498 dleafs[0].contents = CONTENTS_SOLID;
00499 }
00500
00501
00502
00503
00504
00505
00506
00507 void EndBSPFile (void)
00508 {
00509 #if 0
00510 char path[1024];
00511 int len;
00512 byte *buf;
00513 #endif
00514
00515
00516 EmitBrushes ();
00517 EmitPlanes ();
00518 Q2_UnparseEntities ();
00519
00520
00521 #if 0
00522 sprintf (path, "%s/pics/pop.lmp", gamedir);
00523 len = LoadFile (path, &buf);
00524 memcpy (dpop, buf, sizeof(dpop));
00525 FreeMemory(buf);
00526 #endif
00527 }
00528
00529
00530
00531
00532
00533
00534
00535 int firstmodleaf;
00536 extern int firstmodeledge;
00537 extern int firstmodelface;
00538 void BeginModel (void)
00539 {
00540 dmodel_t *mod;
00541 int start, end;
00542 mapbrush_t *b;
00543 int j;
00544 entity_t *e;
00545 vec3_t mins, maxs;
00546
00547 if (nummodels == MAX_MAP_MODELS)
00548 Error ("MAX_MAP_MODELS");
00549 mod = &dmodels[nummodels];
00550
00551 mod->firstface = numfaces;
00552
00553 firstmodleaf = numleafs;
00554 firstmodeledge = numedges;
00555 firstmodelface = numfaces;
00556
00557
00558
00559
00560 e = &entities[entity_num];
00561
00562 start = e->firstbrush;
00563 end = start + e->numbrushes;
00564 ClearBounds (mins, maxs);
00565
00566 for (j=start ; j<end ; j++)
00567 {
00568 b = &mapbrushes[j];
00569 if (!b->numsides)
00570 continue;
00571 AddPointToBounds (b->mins, mins, maxs);
00572 AddPointToBounds (b->maxs, mins, maxs);
00573 }
00574
00575 VectorCopy (mins, mod->mins);
00576 VectorCopy (maxs, mod->maxs);
00577 }
00578
00579
00580
00581
00582
00583
00584
00585 void EndModel (void)
00586 {
00587 dmodel_t *mod;
00588
00589 mod = &dmodels[nummodels];
00590
00591 mod->numfaces = numfaces - mod->firstface;
00592
00593 nummodels++;
00594 }
00595