1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
|
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.
This file is part of Quake III Arena source code.
Quake III Arena source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
Quake III Arena source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
#include "cmdlib.h"
#include "mathlib.h"
#include "bspfile.h"
#include "scriplib.h"
void GetLeafNums (void);
//=============================================================================
int nummodels;
dmodel_t dmodels[MAX_MAP_MODELS];
int numShaders;
dshader_t dshaders[MAX_MAP_SHADERS];
int entdatasize;
char dentdata[MAX_MAP_ENTSTRING];
int numleafs;
dleaf_t dleafs[MAX_MAP_LEAFS];
int numplanes;
dplane_t dplanes[MAX_MAP_PLANES];
int numnodes;
dnode_t dnodes[MAX_MAP_NODES];
int numleafsurfaces;
int dleafsurfaces[MAX_MAP_LEAFFACES];
int numleafbrushes;
int dleafbrushes[MAX_MAP_LEAFBRUSHES];
int numbrushes;
dbrush_t dbrushes[MAX_MAP_BRUSHES];
int numbrushsides;
dbrushside_t dbrushsides[MAX_MAP_BRUSHSIDES];
int numLightBytes;
byte lightBytes[MAX_MAP_LIGHTING];
int numGridPoints;
byte gridData[MAX_MAP_LIGHTGRID];
int numVisBytes;
byte visBytes[MAX_MAP_VISIBILITY];
int numDrawVerts;
drawVert_t drawVerts[MAX_MAP_DRAW_VERTS];
int numDrawIndexes;
int drawIndexes[MAX_MAP_DRAW_INDEXES];
int numDrawSurfaces;
dsurface_t drawSurfaces[MAX_MAP_DRAW_SURFS];
int numFogs;
dfog_t dfogs[MAX_MAP_FOGS];
// su44: added for MoHAA
int numLeafStaticModels;
short dleafStaticModels[MAX_LEAF_STATICMODELS];
int numStaticModels;
dstaticModel_t staticModels[MAX_STATIC_MODELS];
int numStaticModelVertColors;
byte staticModelVertColors[MAX_MAP_STATICMODELVERTCOLORS];
int numSideEquations;
dsideEquation_t sideEquations[MAX_MAP_SIDEEQATIONS];
int numSphereLights;
dspherel_t sphereLights[MAX_MAP_SPHERELIGHTS];
int numTerraPatches;
dterPatch_t terraPatches[MAX_TERRAIN_PATCHES];
int numLightDefs;
dlightdef_t lightDefs[MAX_LIGHTDEFS];
//=============================================================================
/*
=============
SwapBlock
If all values are 32 bits, this can be used to swap everything
=============
*/
void SwapBlock( int *block, int sizeOfBlock ) {
int i;
sizeOfBlock >>= 2;
for ( i = 0 ; i < sizeOfBlock ; i++ ) {
block[i] = LittleLong( block[i] );
}
}
/*
=============
SwapBSPFile
Byte swaps all data in a bsp file.
=============
*/
void SwapBSPFile( void ) {
int i;
// models
SwapBlock( (int *)dmodels, nummodels * sizeof( dmodels[0] ) );
// shaders (don't swap the name)
for ( i = 0 ; i < numShaders ; i++ ) {
dshaders[i].contentFlags = LittleLong( dshaders[i].contentFlags );
dshaders[i].surfaceFlags = LittleLong( dshaders[i].surfaceFlags );
}
// planes
SwapBlock( (int *)dplanes, numplanes * sizeof( dplanes[0] ) );
// nodes
SwapBlock( (int *)dnodes, numnodes * sizeof( dnodes[0] ) );
// leafs
SwapBlock( (int *)dleafs, numleafs * sizeof( dleafs[0] ) );
// leaffaces
SwapBlock( (int *)dleafsurfaces, numleafsurfaces * sizeof( dleafsurfaces[0] ) );
// leafbrushes
SwapBlock( (int *)dleafbrushes, numleafbrushes * sizeof( dleafbrushes[0] ) );
// brushes
SwapBlock( (int *)dbrushes, numbrushes * sizeof( dbrushes[0] ) );
// brushsides
SwapBlock( (int *)dbrushsides, numbrushsides * sizeof( dbrushsides[0] ) );
// vis
((int *)&visBytes)[0] = LittleLong( ((int *)&visBytes)[0] );
((int *)&visBytes)[1] = LittleLong( ((int *)&visBytes)[1] );
// drawverts (don't swap colors )
for ( i = 0 ; i < numDrawVerts ; i++ ) {
drawVerts[i].lightmap[0] = LittleFloat( drawVerts[i].lightmap[0] );
drawVerts[i].lightmap[1] = LittleFloat( drawVerts[i].lightmap[1] );
drawVerts[i].st[0] = LittleFloat( drawVerts[i].st[0] );
drawVerts[i].st[1] = LittleFloat( drawVerts[i].st[1] );
drawVerts[i].xyz[0] = LittleFloat( drawVerts[i].xyz[0] );
drawVerts[i].xyz[1] = LittleFloat( drawVerts[i].xyz[1] );
drawVerts[i].xyz[2] = LittleFloat( drawVerts[i].xyz[2] );
drawVerts[i].normal[0] = LittleFloat( drawVerts[i].normal[0] );
drawVerts[i].normal[1] = LittleFloat( drawVerts[i].normal[1] );
drawVerts[i].normal[2] = LittleFloat( drawVerts[i].normal[2] );
}
// drawindexes
SwapBlock( (int *)drawIndexes, numDrawIndexes * sizeof( drawIndexes[0] ) );
// drawsurfs
SwapBlock( (int *)drawSurfaces, numDrawSurfaces * sizeof( drawSurfaces[0] ) );
// fogs
for ( i = 0 ; i < numFogs ; i++ ) {
dfogs[i].brushNum = LittleLong( dfogs[i].brushNum );
dfogs[i].visibleSide = LittleLong( dfogs[i].visibleSide );
}
// TODO: swap MoHAA structs
}
/*
=============
CopyLump
=============
*/
int CopyLump( dheader_t *header, int lump, void *dest, int size ) {
int length, ofs;
length = header->lumps[lump].filelen;
ofs = header->lumps[lump].fileofs;
if ( length % size ) {
Error ("LoadBSPFile: odd lump size");
}
memcpy( dest, (byte *)header + ofs, length );
return length / size;
}
/*
=============
LoadBSPFile
=============
*/
void LoadBSPFile( const char *filename ) {
dheader_t *header;
// load the file header
LoadFile (filename, (void **)&header);
// swap the header
SwapBlock( (int *)header, sizeof(*header) );
if ( header->ident != BSP_IDENT ) {
Error( "%s is not a IBSP file", filename );
}
if ( header->version != BSP_VERSION ) {
Error( "%s is version %i, not %i", filename, header->version, BSP_VERSION );
}
numShaders = CopyLump( header, LUMP_SHADERS, dshaders, sizeof(dshader_t) );
nummodels = CopyLump( header, LUMP_MODELS, dmodels, sizeof(dmodel_t) );
numplanes = CopyLump( header, LUMP_PLANES, dplanes, sizeof(dplane_t) );
numleafs = CopyLump( header, LUMP_LEAFS, dleafs, sizeof(dleaf_t) );
numnodes = CopyLump( header, LUMP_NODES, dnodes, sizeof(dnode_t) );
numleafsurfaces = CopyLump( header, LUMP_LEAFSURFACES, dleafsurfaces, sizeof(dleafsurfaces[0]) );
numleafbrushes = CopyLump( header, LUMP_LEAFBRUSHES, dleafbrushes, sizeof(dleafbrushes[0]) );
numbrushes = CopyLump( header, LUMP_BRUSHES, dbrushes, sizeof(dbrush_t) );
numbrushsides = CopyLump( header, LUMP_BRUSHSIDES, dbrushsides, sizeof(dbrushside_t) );
numDrawVerts = CopyLump( header, LUMP_DRAWVERTS, drawVerts, sizeof(drawVert_t) );
numDrawSurfaces = CopyLump( header, LUMP_SURFACES, drawSurfaces, sizeof(dsurface_t) );
//numFogs = CopyLump( header, LUMP_FOGS, dfogs, sizeof(dfog_t) );
numDrawIndexes = CopyLump( header, LUMP_DRAWINDEXES, drawIndexes, sizeof(drawIndexes[0]) );
numVisBytes = CopyLump( header, LUMP_VISIBILITY, visBytes, 1 );
numLightBytes = CopyLump( header, LUMP_LIGHTMAPS, lightBytes, 1 );
entdatasize = CopyLump( header, LUMP_ENTITIES, dentdata, 1);
// numGridPoints = CopyLump( header, LUMP_LIGHTGRID, gridData, 8 );
// su44: MoHAA lumps
numStaticModels = CopyLump( header, LUMP_STATICMODELDEF, staticModels, sizeof(staticModels[0]));
numLeafStaticModels = CopyLump( header, LUMP_STATICMODELINDEXES, dleafStaticModels, sizeof(dleafStaticModels[0]));
numStaticModelVertColors = CopyLump( header, LUMP_STATICMODELDATA, staticModelVertColors, 3);
numTerraPatches = CopyLump( header, LUMP_TERRAIN, terraPatches, sizeof(terraPatches[0]));
numSideEquations = CopyLump( header, LUMP_SIDEEQUATIONS, sideEquations, sizeof(sideEquations[0]));
numSphereLights = CopyLump( header, LUMP_SPHERELIGHTS, sphereLights, sizeof(sphereLights[0]));
numLightDefs = CopyLump( header, LUMP_LIGHTDEFS, lightDefs, sizeof(lightDefs[0]));
free( header ); // everything has been copied out
// swap everything
SwapBSPFile();
}
//============================================================================
/*
=============
AddLump
=============
*/
void AddLump( FILE *bspfile, dheader_t *header, int lumpnum, const void *data, int len ) {
lump_t *lump;
lump = &header->lumps[lumpnum];
lump->fileofs = LittleLong( ftell(bspfile) );
lump->filelen = LittleLong( len );
SafeWrite( bspfile, data, (len+3)&~3 );
}
/*
=============
WriteBSPFile
Swaps the bsp file in place, so it should not be referenced again
=============
*/
void WriteBSPFile( const char *filename ) {
dheader_t outheader, *header;
FILE *bspfile;
header = &outheader;
memset( header, 0, sizeof(dheader_t) );
SwapBSPFile();
header->ident = LittleLong( BSP_IDENT );
header->version = LittleLong( BSP_VERSION );
bspfile = SafeOpenWrite( filename );
SafeWrite( bspfile, header, sizeof(dheader_t) ); // overwritten later
AddLump( bspfile, header, LUMP_SHADERS, dshaders, numShaders*sizeof(dshader_t) );
AddLump( bspfile, header, LUMP_PLANES, dplanes, numplanes*sizeof(dplane_t) );
AddLump( bspfile, header, LUMP_LEAFS, dleafs, numleafs*sizeof(dleaf_t) );
AddLump( bspfile, header, LUMP_NODES, dnodes, numnodes*sizeof(dnode_t) );
AddLump( bspfile, header, LUMP_BRUSHES, dbrushes, numbrushes*sizeof(dbrush_t) );
AddLump( bspfile, header, LUMP_BRUSHSIDES, dbrushsides, numbrushsides*sizeof(dbrushside_t) );
AddLump( bspfile, header, LUMP_LEAFSURFACES, dleafsurfaces, numleafsurfaces*sizeof(dleafsurfaces[0]) );
AddLump( bspfile, header, LUMP_LEAFBRUSHES, dleafbrushes, numleafbrushes*sizeof(dleafbrushes[0]) );
AddLump( bspfile, header, LUMP_MODELS, dmodels, nummodels*sizeof(dmodel_t) );
AddLump( bspfile, header, LUMP_DRAWVERTS, drawVerts, numDrawVerts*sizeof(drawVert_t) );
AddLump( bspfile, header, LUMP_SURFACES, drawSurfaces, numDrawSurfaces*sizeof(dsurface_t) );
AddLump( bspfile, header, LUMP_VISIBILITY, visBytes, numVisBytes );
AddLump( bspfile, header, LUMP_LIGHTMAPS, lightBytes, numLightBytes );
// AddLump( bspfile, header, LUMP_LIGHTGRID, gridData, 8 * numGridPoints );
AddLump( bspfile, header, LUMP_ENTITIES, dentdata, entdatasize );
// AddLump( bspfile, header, LUMP_FOGS, dfogs, numFogs * sizeof(dfog_t) );
AddLump( bspfile, header, LUMP_DRAWINDEXES, drawIndexes, numDrawIndexes * sizeof(drawIndexes[0]) );
// su44: MoHAA lumps
AddLump( bspfile, header, LUMP_STATICMODELDEF, staticModels, numStaticModels * sizeof(staticModels[0]) );
AddLump( bspfile, header, LUMP_STATICMODELINDEXES, staticModels, numLeafStaticModels * sizeof(dleafStaticModels[0]) );
AddLump( bspfile, header, LUMP_STATICMODELDATA, staticModelVertColors, numStaticModelVertColors * 3 );
AddLump( bspfile, header, LUMP_TERRAIN, terraPatches, numTerraPatches * sizeof(terraPatches[0]) );
AddLump( bspfile, header, LUMP_SIDEEQUATIONS, sideEquations, numSideEquations * sizeof(sideEquations[0]) );
AddLump( bspfile, header, LUMP_SPHERELIGHTS, sphereLights, numSphereLights * sizeof(sphereLights[0]) );
AddLump( bspfile, header, LUMP_LIGHTDEFS, lightDefs, numLightDefs * sizeof(lightDefs[0]) );
fseek (bspfile, 0, SEEK_SET);
SafeWrite (bspfile, header, sizeof(dheader_t));
fclose (bspfile);
}
//============================================================================
/*
=============
PrintBSPFileSizes
Dumps info about current file
=============
*/
void PrintBSPFileSizes( void ) {
if ( !num_entities ) {
ParseEntities();
}
printf ("%6i models %7i\n"
,nummodels, (int)(nummodels*sizeof(dmodel_t)));
printf ("%6i shaders %7i\n"
,numShaders, (int)(numShaders*sizeof(dshader_t)));
printf ("%6i brushes %7i\n"
,numbrushes, (int)(numbrushes*sizeof(dbrush_t)));
printf ("%6i brushsides %7i\n"
,numbrushsides, (int)(numbrushsides*sizeof(dbrushside_t)));
printf ("%6i fogs %7i\n"
,numFogs, (int)(numFogs*sizeof(dfog_t)));
printf ("%6i planes %7i\n"
,numplanes, (int)(numplanes*sizeof(dplane_t)));
printf ("%6i entdata %7i\n", num_entities, entdatasize);
printf ("\n");
printf ("%6i nodes %7i\n"
,numnodes, (int)(numnodes*sizeof(dnode_t)));
printf ("%6i leafs %7i\n"
,numleafs, (int)(numleafs*sizeof(dleaf_t)));
printf ("%6i leafsurfaces %7i\n"
,numleafsurfaces, (int)(numleafsurfaces*sizeof(dleafsurfaces[0])));
printf ("%6i leafbrushes %7i\n"
,numleafbrushes, (int)(numleafbrushes*sizeof(dleafbrushes[0])));
printf ("%6i drawverts %7i\n"
,numDrawVerts, (int)(numDrawVerts*sizeof(drawVerts[0])));
printf ("%6i drawindexes %7i\n"
,numDrawIndexes, (int)(numDrawIndexes*sizeof(drawIndexes[0])));
printf ("%6i drawsurfaces %7i\n"
,numDrawSurfaces, (int)(numDrawSurfaces*sizeof(drawSurfaces[0])));
printf ("%6i lightmaps %7i\n"
,numLightBytes / (LIGHTMAP_WIDTH*LIGHTMAP_HEIGHT*3), numLightBytes );
printf (" visibility %7i\n"
, numVisBytes );
printf("\n");
printf ("%6i staticModels %7i\n"
,numStaticModels, (int)(numStaticModels*sizeof(staticModels[0])));
printf ("%6i leafStaticModels %7i\n"
,numLeafStaticModels, (int)(numLeafStaticModels*sizeof(dleafStaticModels[0])));
printf ("%6i staticModelVertCols %7i\n"
,numStaticModelVertColors, (int)(numStaticModelVertColors*sizeof(staticModelVertColors[0])));
printf ("%6i terraPatches %7i\n"
,numTerraPatches, (int)(numTerraPatches*sizeof(terraPatches[0])));
printf ("%6i sphereLights %7i\n"
,numSphereLights, (int)(numSphereLights*sizeof(sphereLights[0])));
printf ("%6i sideEquations %7i\n"
,numSideEquations, (int)(numSideEquations*sizeof(sideEquations[0])));
printf ("%6i lightDefs %7i\n"
,numLightDefs, (int)(numLightDefs*sizeof(lightDefs[0])));
}
//============================================
int num_entities;
entity_t entities[MAX_MAP_ENTITIES];
void StripTrailing( char *e ) {
char *s;
s = e + strlen(e)-1;
while (s >= e && *s <= 32)
{
*s = 0;
s--;
}
}
/*
=================
ParseEpair
=================
*/
epair_t *ParseEpair( void ) {
epair_t *e;
e = malloc( sizeof(epair_t) );
memset( e, 0, sizeof(epair_t) );
if ( strlen(token) >= MAX_KEY-1 ) {
Error ("ParseEpar: token too long");
}
e->key = copystring( token );
GetToken( qfalse );
if ( strlen(token) >= MAX_VALUE-1 ) {
Error ("ParseEpar: token too long");
}
e->value = copystring( token );
// strip trailing spaces that sometimes get accidentally
// added in the editor
StripTrailing( e->key );
StripTrailing( e->value );
return e;
}
/*
================
ParseEntity
================
*/
qboolean ParseEntity( void ) {
epair_t *e;
entity_t *mapent;
if ( !GetToken (qtrue) ) {
return qfalse;
}
if ( strcmp (token, "{") ) {
Error ("ParseEntity: { not found");
}
if ( num_entities == MAX_MAP_ENTITIES ) {
Error ("num_entities == MAX_MAP_ENTITIES");
}
mapent = &entities[num_entities];
num_entities++;
do {
if ( !GetToken (qtrue) ) {
Error ("ParseEntity: EOF without closing brace");
}
if ( !strcmp (token, "}") ) {
break;
}
e = ParseEpair ();
e->next = mapent->epairs;
mapent->epairs = e;
} while (1);
return qtrue;
}
/*
================
ParseEntities
Parses the dentdata string into entities
================
*/
void ParseEntities( void ) {
num_entities = 0;
ParseFromMemory( dentdata, entdatasize );
while ( ParseEntity () ) {
}
}
/*
================
UnparseEntities
Generates the dentdata string from all the entities
This allows the utilities to add or remove key/value pairs
to the data created by the map editor.
================
*/
void UnparseEntities( void ) {
char *buf, *end;
epair_t *ep;
char line[2048];
int i;
char key[1024], value[1024];
buf = dentdata;
end = buf;
*end = 0;
for (i=0 ; i<num_entities ; i++) {
ep = entities[i].epairs;
if ( !ep ) {
continue; // ent got removed
}
strcat (end,"{\n");
end += 2;
for ( ep = entities[i].epairs ; ep ; ep=ep->next ) {
strcpy (key, ep->key);
StripTrailing (key);
strcpy (value, ep->value);
StripTrailing (value);
sprintf (line, "\"%s\" \"%s\"\n", key, value);
strcat (end, line);
end += strlen(line);
}
strcat (end,"}\n");
end += 2;
if (end > buf + MAX_MAP_ENTSTRING) {
Error ("Entity text too long");
}
}
entdatasize = end - buf + 1;
}
void PrintEntity( const entity_t *ent ) {
epair_t *ep;
printf ("------- entity %p -------\n", ent);
for (ep=ent->epairs ; ep ; ep=ep->next) {
printf( "%s = %s\n", ep->key, ep->value );
}
}
void SetKeyValue( entity_t *ent, const char *key, const char *value ) {
epair_t *ep;
for ( ep=ent->epairs ; ep ; ep=ep->next ) {
if ( !strcmp (ep->key, key) ) {
free (ep->value);
ep->value = copystring(value);
return;
}
}
ep = malloc (sizeof(*ep));
ep->next = ent->epairs;
ent->epairs = ep;
ep->key = copystring(key);
ep->value = copystring(value);
}
const char *ValueForKey( const entity_t *ent, const char *key ) {
epair_t *ep;
for (ep=ent->epairs ; ep ; ep=ep->next) {
if (!strcmp (ep->key, key) ) {
return ep->value;
}
}
return "";
}
vec_t FloatForKey( const entity_t *ent, const char *key ) {
const char *k;
k = ValueForKey( ent, key );
return atof(k);
}
void GetVectorForKey( const entity_t *ent, const char *key, vec3_t vec ) {
const char *k;
double v1, v2, v3;
k = ValueForKey (ent, key);
// scanf into doubles, then assign, so it is vec_t size independent
v1 = v2 = v3 = 0;
sscanf (k, "%lf %lf %lf", &v1, &v2, &v3);
vec[0] = v1;
vec[1] = v2;
vec[2] = v3;
}
|