00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "qbsp.h"
00024 #include "l_mem.h"
00025 #include "../botlib/aasfile.h"
00026 #include "aas_store.h"
00027 #include "aas_cfg.h"
00028 #include "aas_map.h"
00029 #include "l_bsp_q3.h"
00030 #include "../qcommon/cm_patch.h"
00031 #include "../game/surfaceflags.h"
00032
00033 #define NODESTACKSIZE 1024
00034
00035
00036
00037
00038
00039
00040
00041 void PrintContents(int contents);
00042
00043 int Q3_BrushContents(mapbrush_t *b)
00044 {
00045 int contents, i, mixed, hint;
00046 side_t *s;
00047
00048 s = &b->original_sides[0];
00049 contents = s->contents;
00050
00051 mixed = false;
00052 hint = false;
00053 for (i = 1; i < b->numsides; i++)
00054 {
00055 s = &b->original_sides[i];
00056 if (s->contents != contents) mixed = true;
00057 if (s->surf & (SURF_HINT|SURF_SKIP)) hint = true;
00058 contents |= s->contents;
00059 }
00060
00061 if (hint)
00062 {
00063 if (contents)
00064 {
00065 Log_Write("WARNING: hint brush with contents: ");
00066 PrintContents(contents);
00067 Log_Write("\r\n");
00068
00069 Log_Write("brush contents is: ");
00070 PrintContents(b->contents);
00071 Log_Write("\r\n");
00072 }
00073 return 0;
00074 }
00075
00076
00077
00078
00079 contents &= ~(CONTENTS_LADDER|CONTENTS_FOG);
00080
00081 if (mixed)
00082 {
00083 Log_Write("Entity %i, Brush %i: mixed face contents "
00084 , b->entitynum, b->brushnum);
00085 PrintContents(contents);
00086 Log_Write("\r\n");
00087
00088 Log_Write("brush contents is: ");
00089 PrintContents(b->contents);
00090 Log_Write("\r\n");
00091
00092 if (contents & CONTENTS_DONOTENTER) return CONTENTS_DONOTENTER;
00093
00094
00095
00096
00097
00098
00099
00100 if (contents & (CONTENTS_LAVA | CONTENTS_SLIME | CONTENTS_WATER))
00101 {
00102 return (contents & (CONTENTS_LAVA | CONTENTS_SLIME | CONTENTS_WATER));
00103 }
00104 if (contents & CONTENTS_PLAYERCLIP) return (contents & CONTENTS_PLAYERCLIP);
00105 return (contents & CONTENTS_SOLID);
00106 }
00107
00108
00109
00110
00111
00112
00113 if (contents == (contents & CONTENTS_STRUCTURAL))
00114 {
00115
00116 contents = 0;
00117 }
00118 if (contents & CONTENTS_DONOTENTER)
00119 {
00120 Log_Print("brush %i is a donotenter brush, c = %X\n", b->brushnum, contents);
00121 }
00122 return contents;
00123 }
00124 #define BBOX_NORMAL_EPSILON 0.0001
00125
00126
00127
00128
00129
00130
00131 void Q3_DPlanes2MapPlanes(void)
00132 {
00133 int i;
00134
00135 for (i = 0; i < q3_numplanes; i++)
00136 {
00137 dplanes2mapplanes[i] = FindFloatPlane(q3_dplanes[i].normal, q3_dplanes[i].dist);
00138 }
00139 }
00140
00141
00142
00143
00144
00145
00146 void Q3_BSPBrushToMapBrush(q3_dbrush_t *bspbrush, entity_t *mapent)
00147 {
00148 mapbrush_t *b;
00149 int i, k, n;
00150 side_t *side, *s2;
00151 int planenum;
00152 q3_dbrushside_t *bspbrushside;
00153 q3_dplane_t *bspplane;
00154
00155 if (nummapbrushes >= MAX_MAPFILE_BRUSHES)
00156 Error ("nummapbrushes >= MAX_MAPFILE_BRUSHES");
00157
00158 b = &mapbrushes[nummapbrushes];
00159 b->original_sides = &brushsides[nummapbrushsides];
00160 b->entitynum = mapent-entities;
00161 b->brushnum = nummapbrushes - mapent->firstbrush;
00162 b->leafnum = dbrushleafnums[bspbrush - q3_dbrushes];
00163
00164 for (n = 0; n < bspbrush->numSides; n++)
00165 {
00166
00167 bspbrushside = &q3_dbrushsides[bspbrush->firstSide + n];
00168
00169 if (nummapbrushsides >= MAX_MAPFILE_BRUSHSIDES)
00170 {
00171 Error ("MAX_MAPFILE_BRUSHSIDES");
00172 }
00173
00174 side = &brushsides[nummapbrushsides];
00175
00176 if (q3_dbrushsidetextured[bspbrush->firstSide + n]) side->flags |= SFL_TEXTURED|SFL_VISIBLE;
00177 else side->flags &= ~SFL_TEXTURED;
00178
00179
00180
00181 if (bspbrushside->shaderNum < 0)
00182 {
00183 side->contents = 0;
00184 side->surf = 0;
00185 }
00186 else
00187 {
00188 side->contents = q3_dshaders[bspbrushside->shaderNum].contentFlags;
00189 side->surf = q3_dshaders[bspbrushside->shaderNum].surfaceFlags;
00190 if (strstr(q3_dshaders[bspbrushside->shaderNum].shader, "common/hint"))
00191 {
00192
00193 side->surf |= SURF_HINT;
00194 }
00195 }
00196
00197 if (side->surf & SURF_NODRAW)
00198 {
00199 side->flags |= SFL_TEXTURED|SFL_VISIBLE;
00200 }
00201
00202
00203
00204
00205
00206
00207
00208 if (side->surf & (SURF_HINT|SURF_SKIP) )
00209 {
00210 side->contents = 0;
00211
00212 }
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222 bspplane = &q3_dplanes[bspbrushside->planeNum];
00223 planenum = FindFloatPlane(bspplane->normal, bspplane->dist);
00224
00225
00226
00227
00228
00229
00230
00231 for (k = 0; k < b->numsides; k++)
00232 {
00233 s2 = b->original_sides + k;
00234
00235
00236
00237 if (s2->planenum == planenum)
00238 {
00239 Log_Print("Entity %i, Brush %i: duplicate plane\n"
00240 , b->entitynum, b->brushnum);
00241 break;
00242 }
00243 if ( s2->planenum == (planenum^1) )
00244 {
00245 Log_Print("Entity %i, Brush %i: mirrored plane\n"
00246 , b->entitynum, b->brushnum);
00247 break;
00248 }
00249 }
00250 if (k != b->numsides)
00251 continue;
00252
00253
00254
00255
00256
00257 side = b->original_sides + b->numsides;
00258
00259 side->planenum = planenum;
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271 nummapbrushsides++;
00272 b->numsides++;
00273 }
00274
00275
00276 b->contents = q3_dshaders[bspbrush->shaderNum].contentFlags;
00277 b->contents &= ~(CONTENTS_LADDER|CONTENTS_FOG|CONTENTS_STRUCTURAL);
00278
00279
00280
00281 if (BrushExists(b))
00282 {
00283 c_squattbrushes++;
00284 b->numsides = 0;
00285 return;
00286 }
00287
00288
00289 if (create_aas)
00290 {
00291
00292 AAS_CreateMapBrushes(b, mapent, false);
00293 return;
00294 }
00295
00296
00297 if (nodetail && (b->contents & CONTENTS_DETAIL) )
00298 {
00299 b->numsides = 0;
00300 return;
00301 }
00302
00303
00304 if (nowater && (b->contents & (CONTENTS_LAVA | CONTENTS_SLIME | CONTENTS_WATER)) )
00305 {
00306 b->numsides = 0;
00307 return;
00308 }
00309
00310
00311 MakeBrushWindings(b);
00312
00313
00314 MarkBrushBevels(b);
00315
00316
00317
00318 if (b->contents & (CONTENTS_PLAYERCLIP|CONTENTS_MONSTERCLIP) )
00319 {
00320 c_clipbrushes++;
00321 for (i = 0; i < b->numsides; i++)
00322 b->original_sides[i].texinfo = TEXINFO_NODE;
00323 }
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365 nummapbrushes++;
00366 mapent->numbrushes++;
00367 }
00368
00369
00370
00371
00372
00373
00374 void Q3_ParseBSPBrushes(entity_t *mapent)
00375 {
00376 int i;
00377
00378 for (i = 0; i < q3_dmodels[mapent->modelnum].numBrushes; i++)
00379 {
00380 Q3_BSPBrushToMapBrush(&q3_dbrushes[q3_dmodels[mapent->modelnum].firstBrush + i], mapent);
00381 }
00382 }
00383
00384
00385
00386
00387
00388
00389 qboolean Q3_ParseBSPEntity(int entnum)
00390 {
00391 entity_t *mapent;
00392 char *model;
00393 int startbrush, startsides;
00394
00395 startbrush = nummapbrushes;
00396 startsides = nummapbrushsides;
00397
00398 mapent = &entities[entnum];
00399 mapent->firstbrush = nummapbrushes;
00400 mapent->numbrushes = 0;
00401 mapent->modelnum = -1;
00402
00403 model = ValueForKey(mapent, "model");
00404 if (model && strlen(model))
00405 {
00406 if (*model == '*')
00407 {
00408
00409 mapent->modelnum = atoi(&model[1]);
00410 }
00411 }
00412
00413 GetVectorForKey(mapent, "origin", mapent->origin);
00414
00415
00416
00417 if (!strcmp("worldspawn", ValueForKey(mapent, "classname")))
00418 {
00419 mapent->modelnum = 0;
00420 }
00421
00422
00423 if (mapent->modelnum >= 0)
00424 {
00425
00426 Q3_ParseBSPBrushes(mapent);
00427 }
00428
00429
00430
00431
00432
00433
00434 if (!strcmp ("func_areaportal", ValueForKey (mapent, "classname")))
00435 {
00436 c_areaportals++;
00437 mapent->areaportalnum = c_areaportals;
00438 return true;
00439 }
00440 return true;
00441 }
00442
00443
00444
00445
00446
00447
00448 #define MAX_PATCH_VERTS 1024
00449
00450 void AAS_CreateCurveBrushes(void)
00451 {
00452 int i, j, n, planenum, numcurvebrushes = 0;
00453 q3_dsurface_t *surface;
00454 q3_drawVert_t *dv_p;
00455 vec3_t points[MAX_PATCH_VERTS];
00456 int width, height, c;
00457 patchCollide_t *pc;
00458 facet_t *facet;
00459 mapbrush_t *brush;
00460 side_t *side;
00461 entity_t *mapent;
00462 winding_t *winding;
00463
00464 qprintf("nummapbrushsides = %d\n", nummapbrushsides);
00465 mapent = &entities[0];
00466 for (i = 0; i < q3_numDrawSurfaces; i++)
00467 {
00468 surface = &q3_drawSurfaces[i];
00469 if ( ! surface->patchWidth ) continue;
00470
00471 if (!(q3_dshaders[surface->shaderNum].contentFlags & (CONTENTS_SOLID|CONTENTS_PLAYERCLIP)))
00472 {
00473
00474 continue;
00475 }
00476
00477 if ( q3_dshaders[surface->shaderNum].contentFlags & CONTENTS_NOBOTCLIP ) {
00478 continue;
00479 }
00480
00481 width = surface->patchWidth;
00482 height = surface->patchHeight;
00483 c = width * height;
00484 if (c > MAX_PATCH_VERTS)
00485 {
00486 Error("ParseMesh: MAX_PATCH_VERTS");
00487 }
00488
00489 dv_p = q3_drawVerts + surface->firstVert;
00490 for ( j = 0 ; j < c ; j++, dv_p++ )
00491 {
00492 points[j][0] = dv_p->xyz[0];
00493 points[j][1] = dv_p->xyz[1];
00494 points[j][2] = dv_p->xyz[2];
00495 }
00496
00497 pc = CM_GeneratePatchCollide(width, height, points);
00498
00499 for (j = 0; j < pc->numFacets; j++)
00500 {
00501 facet = &pc->facets[j];
00502
00503 brush = &mapbrushes[nummapbrushes];
00504 brush->original_sides = &brushsides[nummapbrushsides];
00505 brush->entitynum = 0;
00506 brush->brushnum = nummapbrushes - mapent->firstbrush;
00507
00508 brush->numsides = facet->numBorders + 2;
00509 nummapbrushsides += brush->numsides;
00510 brush->contents = CONTENTS_SOLID;
00511
00512
00513 qprintf("\r%6d curve brushes", ++numcurvebrushes);
00514
00515 planenum = FindFloatPlane(pc->planes[facet->surfacePlane].plane, pc->planes[facet->surfacePlane].plane[3]);
00516
00517 side = &brush->original_sides[0];
00518 side->planenum = planenum;
00519 side->contents = CONTENTS_SOLID;
00520 side->flags |= SFL_TEXTURED|SFL_VISIBLE|SFL_CURVE;
00521 side->surf = 0;
00522
00523 side = &brush->original_sides[1];
00524 if (create_aas)
00525 {
00526
00527
00528 side->planenum = planenum ^ 1;
00529 }
00530 else
00531 {
00532 side->planenum = FindFloatPlane(mapplanes[planenum^1].normal, mapplanes[planenum^1].dist + 1);
00533 side->flags |= SFL_TEXTURED|SFL_VISIBLE;
00534 }
00535 side->contents = CONTENTS_SOLID;
00536 side->flags |= SFL_CURVE;
00537 side->surf = 0;
00538
00539 winding = BaseWindingForPlane(mapplanes[side->planenum].normal, mapplanes[side->planenum].dist);
00540 for (n = 0; n < facet->numBorders; n++)
00541 {
00542
00543 if (facet->borderPlanes[n] == facet->surfacePlane) continue;
00544
00545 side = &brush->original_sides[2 + n];
00546 side->planenum = FindFloatPlane(pc->planes[facet->borderPlanes[n]].plane, pc->planes[facet->borderPlanes[n]].plane[3]);
00547 if (facet->borderInward[n]) side->planenum ^= 1;
00548 side->contents = CONTENTS_SOLID;
00549 side->flags |= SFL_TEXTURED|SFL_CURVE;
00550 side->surf = 0;
00551
00552 if (winding) ChopWindingInPlace(&winding, mapplanes[side->planenum^1].normal, mapplanes[side->planenum^1].dist, 0.1);
00553 }
00554
00555
00556 if (!winding)
00557 {
00558 Log_Print("WARNING: AAS_CreateCurveBrushes: no winding\n");
00559 brush->numsides = 0;
00560 continue;
00561 }
00562 brush->original_sides[0].winding = winding;
00563 WindingBounds(winding, brush->mins, brush->maxs);
00564 for (n = 0; n < 3; n++)
00565 {
00566
00567 if (brush->mins[n] < -MAX_MAP_BOUNDS || brush->maxs[n] > MAX_MAP_BOUNDS)
00568 {
00569 Log_Print("entity %i, brush %i: bounds out of range\n", brush->entitynum, brush->brushnum);
00570 Log_Print("brush->mins[%d] = %f, brush->maxs[%d] = %f\n", n, brush->mins[n], n, brush->maxs[n]);
00571 brush->numsides = 0;
00572 break;
00573 }
00574 if (brush->mins[n] > MAX_MAP_BOUNDS || brush->maxs[n] < -MAX_MAP_BOUNDS)
00575 {
00576 Log_Print("entity %i, brush %i: no visible sides on brush\n", brush->entitynum, brush->brushnum);
00577 Log_Print("brush->mins[%d] = %f, brush->maxs[%d] = %f\n", n, brush->mins[n], n, brush->maxs[n]);
00578 brush->numsides = 0;
00579 break;
00580 }
00581 }
00582 if (create_aas)
00583 {
00584
00585
00586 AAS_CreateMapBrushes(brush, mapent, false);
00587 }
00588 else
00589 {
00590
00591 MakeBrushWindings(brush);
00592 AddBrushBevels(brush);
00593 nummapbrushes++;
00594 mapent->numbrushes++;
00595 }
00596 }
00597 }
00598
00599 qprintf("\r%6d curve brushes\n", numcurvebrushes);
00600 }
00601
00602
00603
00604
00605
00606
00607 void AAS_ExpandMapBrush(mapbrush_t *brush, vec3_t mins, vec3_t maxs);
00608
00609 void Q3_LoadMapFromBSP(struct quakefile_s *qf)
00610 {
00611 int i;
00612 vec3_t mins = {-1,-1,-1}, maxs = {1, 1, 1};
00613
00614 Log_Print("-- Q3_LoadMapFromBSP --\n");
00615
00616 loadedmaptype = MAPTYPE_QUAKE3;
00617
00618 Log_Print("Loading map from %s...\n", qf->filename);
00619
00620 Q3_LoadBSPFile(qf);
00621
00622
00623
00624
00625 for (i = 0; i < MAX_MAPFILE_BRUSHES; i++)
00626 brushmodelnumbers[i] = -1;
00627
00628 nummapbrushsides = 0;
00629 num_entities = 0;
00630
00631 Q3_ParseEntities();
00632
00633 for (i = 0; i < num_entities; i++)
00634 {
00635 Q3_ParseBSPEntity(i);
00636 }
00637
00638 AAS_CreateCurveBrushes();
00639
00640 ClearBounds(map_mins, map_maxs);
00641 for (i = 0; i < entities[0].numbrushes; i++)
00642 {
00643 if (mapbrushes[i].numsides <= 0)
00644 continue;
00645 AddPointToBounds (mapbrushes[i].mins, map_mins, map_maxs);
00646 AddPointToBounds (mapbrushes[i].maxs, map_mins, map_maxs);
00647 }
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666 }
00667
00668
00669
00670
00671
00672
00673 void Q3_ResetMapLoading(void)
00674 {
00675
00676 memset(nodestack, 0, NODESTACKSIZE * sizeof(int));
00677 nodestackptr = NULL;
00678 nodestacksize = 0;
00679 memset(brushmodelnumbers, 0, MAX_MAPFILE_BRUSHES * sizeof(int));
00680 }
00681