2015-04-16 16:55:32 +00:00
|
|
|
/*
|
|
|
|
===========================================================================
|
|
|
|
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 bteral 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 bteral Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU bteral 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 "BspLoader.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
char filename[1024];
|
|
|
|
char *buffer, *script_p, *end_p;
|
|
|
|
int line;
|
2015-04-16 16:55:32 +00:00
|
|
|
} BSPScript;
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
#define MAX_INCLUDES 8
|
|
|
|
BSPScript scriptstack[MAX_INCLUDES];
|
|
|
|
BSPScript *script;
|
|
|
|
int scriptline;
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
char token[BSPMAXTOKEN];
|
2015-04-16 16:55:32 +00:00
|
|
|
bool endofscript;
|
2018-09-23 21:17:31 +00:00
|
|
|
bool tokenready; // only true if UnGetToken was just called
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
//loadBSPFile
|
|
|
|
//
|
|
|
|
|
|
|
|
int extrasize = 100;
|
|
|
|
|
|
|
|
BspLoader::BspLoader()
|
2018-09-23 21:17:31 +00:00
|
|
|
: m_num_entities(0)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
|
|
|
m_Endianness = getMachineEndianness();
|
|
|
|
if (m_Endianness == BSP_BIG_ENDIAN)
|
|
|
|
{
|
|
|
|
printf("Machine is BIG_ENDIAN\n");
|
2018-09-23 21:17:31 +00:00
|
|
|
}
|
|
|
|
else
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
|
|
|
printf("Machine is Little Endian\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
bool BspLoader::loadBSPFile(void *memoryBuffer)
|
|
|
|
{
|
|
|
|
BSPHeader *header = (BSPHeader *)memoryBuffer;
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
// load the file header
|
|
|
|
if (header)
|
|
|
|
{
|
|
|
|
// swap the header
|
2018-09-23 21:17:31 +00:00
|
|
|
swapBlock((int *)header, sizeof(*header));
|
|
|
|
|
2015-04-16 16:55:32 +00:00
|
|
|
int length = (header->lumps[BSPLUMP_SHADERS].filelen) / sizeof(BSPShader);
|
2018-09-23 21:17:31 +00:00
|
|
|
m_dshaders.resize(length + extrasize);
|
|
|
|
m_numShaders = copyLump(header, BSPLUMP_SHADERS, &m_dshaders[0], sizeof(BSPShader));
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
length = (header->lumps[LUMP_MODELS].filelen) / sizeof(BSPModel);
|
2018-09-23 21:17:31 +00:00
|
|
|
m_dmodels.resize(length + extrasize);
|
|
|
|
m_nummodels = copyLump(header, LUMP_MODELS, &m_dmodels[0], sizeof(BSPModel));
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
length = (header->lumps[BSPLUMP_PLANES].filelen) / sizeof(BSPPlane);
|
2018-09-23 21:17:31 +00:00
|
|
|
m_dplanes.resize(length + extrasize);
|
|
|
|
m_numplanes = copyLump(header, BSPLUMP_PLANES, &m_dplanes[0], sizeof(BSPPlane));
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
length = (header->lumps[BSPLUMP_LEAFS].filelen) / sizeof(BSPLeaf);
|
2018-09-23 21:17:31 +00:00
|
|
|
m_dleafs.resize(length + extrasize);
|
|
|
|
m_numleafs = copyLump(header, BSPLUMP_LEAFS, &m_dleafs[0], sizeof(BSPLeaf));
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
length = (header->lumps[BSPLUMP_NODES].filelen) / sizeof(BSPNode);
|
2018-09-23 21:17:31 +00:00
|
|
|
m_dnodes.resize(length + extrasize);
|
|
|
|
m_numnodes = copyLump(header, BSPLUMP_NODES, &m_dnodes[0], sizeof(BSPNode));
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
length = (header->lumps[BSPLUMP_LEAFSURFACES].filelen) / sizeof(m_dleafsurfaces[0]);
|
2018-09-23 21:17:31 +00:00
|
|
|
m_dleafsurfaces.resize(length + extrasize);
|
|
|
|
m_numleafsurfaces = copyLump(header, BSPLUMP_LEAFSURFACES, &m_dleafsurfaces[0], sizeof(m_dleafsurfaces[0]));
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
length = (header->lumps[BSPLUMP_LEAFBRUSHES].filelen) / sizeof(m_dleafbrushes[0]);
|
|
|
|
m_dleafbrushes.resize(length + extrasize);
|
|
|
|
m_numleafbrushes = copyLump(header, BSPLUMP_LEAFBRUSHES, &m_dleafbrushes[0], sizeof(m_dleafbrushes[0]));
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
length = (header->lumps[LUMP_BRUSHES].filelen) / sizeof(BSPBrush);
|
2018-09-23 21:17:31 +00:00
|
|
|
m_dbrushes.resize(length + extrasize);
|
|
|
|
m_numbrushes = copyLump(header, LUMP_BRUSHES, &m_dbrushes[0], sizeof(BSPBrush));
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
length = (header->lumps[LUMP_BRUSHSIDES].filelen) / sizeof(BSPBrushSide);
|
2018-09-23 21:17:31 +00:00
|
|
|
m_dbrushsides.resize(length + extrasize);
|
|
|
|
m_numbrushsides = copyLump(header, LUMP_BRUSHSIDES, &m_dbrushsides[0], sizeof(BSPBrushSide));
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
length = (header->lumps[LUMP_SURFACES].filelen) / sizeof(BSPSurface);
|
2018-09-23 21:17:31 +00:00
|
|
|
m_drawSurfaces.resize(length + extrasize);
|
|
|
|
m_numDrawSurfaces = copyLump(header, LUMP_SURFACES, &m_drawSurfaces[0], sizeof(BSPSurface));
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
length = (header->lumps[LUMP_DRAWINDEXES].filelen) / sizeof(m_drawIndexes[0]);
|
2018-09-23 21:17:31 +00:00
|
|
|
m_drawIndexes.resize(length + extrasize);
|
|
|
|
m_numDrawIndexes = copyLump(header, LUMP_DRAWINDEXES, &m_drawIndexes[0], sizeof(m_drawIndexes[0]));
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
length = (header->lumps[LUMP_VISIBILITY].filelen) / 1;
|
2018-09-23 21:17:31 +00:00
|
|
|
m_visBytes.resize(length + extrasize);
|
|
|
|
m_numVisBytes = copyLump(header, LUMP_VISIBILITY, &m_visBytes[0], 1);
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
length = (header->lumps[LUMP_LIGHTMAPS].filelen) / 1;
|
2018-09-23 21:17:31 +00:00
|
|
|
m_lightBytes.resize(length + extrasize);
|
|
|
|
m_numLightBytes = copyLump(header, LUMP_LIGHTMAPS, &m_lightBytes[0], 1);
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
length = (header->lumps[BSPLUMP_ENTITIES].filelen) / 1;
|
2018-09-23 21:17:31 +00:00
|
|
|
m_dentdata.resize(length + extrasize);
|
|
|
|
m_entdatasize = copyLump(header, BSPLUMP_ENTITIES, &m_dentdata[0], 1);
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
length = (header->lumps[LUMP_LIGHTGRID].filelen) / 1;
|
2018-09-23 21:17:31 +00:00
|
|
|
m_gridData.resize(length + extrasize);
|
|
|
|
m_numGridPoints = copyLump(header, LUMP_LIGHTGRID, &m_gridData[0], 8);
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
// swap everything
|
|
|
|
swapBSPFile();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
const char *BspLoader::getValueForKey(const BSPEntity *ent, const char *key) const
|
|
|
|
{
|
|
|
|
const BSPKeyValuePair *ep;
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
for (ep = ent->epairs; ep; ep = ep->next)
|
|
|
|
{
|
|
|
|
if (!strcmp(ep->key, key))
|
|
|
|
{
|
2015-04-16 16:55:32 +00:00
|
|
|
return ep->value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
float BspLoader::getFloatForKey(const BSPEntity *ent, const char *key)
|
|
|
|
{
|
|
|
|
const char *k;
|
|
|
|
|
|
|
|
k = getValueForKey(ent, key);
|
2015-04-16 16:55:32 +00:00
|
|
|
return float(atof(k));
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
bool BspLoader::getVectorForKey(const BSPEntity *ent, const char *key, BSPVector3 vec)
|
|
|
|
{
|
|
|
|
const char *k;
|
|
|
|
k = getValueForKey(ent, key);
|
2015-04-16 16:55:32 +00:00
|
|
|
if (strcmp(k, ""))
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
sscanf(k, "%f %f %f", &vec[0], &vec[1], &vec[2]);
|
2015-04-16 16:55:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==============
|
|
|
|
parseFromMemory
|
|
|
|
==============
|
|
|
|
*/
|
2018-09-23 21:17:31 +00:00
|
|
|
void BspLoader::parseFromMemory(char *buffer, int size)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
|
|
|
script = scriptstack;
|
|
|
|
script++;
|
|
|
|
if (script == &scriptstack[MAX_INCLUDES])
|
|
|
|
{
|
|
|
|
//printf("script file exceeded MAX_INCLUDES");
|
|
|
|
}
|
2018-09-23 21:17:31 +00:00
|
|
|
strcpy(script->filename, "memory buffer");
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
script->buffer = buffer;
|
|
|
|
script->line = 1;
|
|
|
|
script->script_p = script->buffer;
|
|
|
|
script->end_p = script->buffer + size;
|
|
|
|
|
|
|
|
endofscript = false;
|
|
|
|
tokenready = false;
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
bool BspLoader::isEndOfScript(bool crossline)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
|
|
|
if (!crossline)
|
|
|
|
//printf("Line %i is incomplete\n",scriptline);
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (!strcmp(script->filename, "memory buffer"))
|
|
|
|
{
|
|
|
|
endofscript = true;
|
|
|
|
return false;
|
|
|
|
}
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
//free (script->buffer);
|
2018-09-23 21:17:31 +00:00
|
|
|
if (script == scriptstack + 1)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
|
|
|
endofscript = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
script--;
|
|
|
|
scriptline = script->line;
|
|
|
|
//printf ("returning to %s\n", script->filename);
|
2018-09-23 21:17:31 +00:00
|
|
|
return getToken(crossline);
|
2015-04-16 16:55:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
==============
|
|
|
|
getToken
|
|
|
|
==============
|
|
|
|
*/
|
2018-09-23 21:17:31 +00:00
|
|
|
bool BspLoader::getToken(bool crossline)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
char *token_p;
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (tokenready) // is a token allready waiting?
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
|
|
|
tokenready = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (script->script_p >= script->end_p)
|
2018-09-23 21:17:31 +00:00
|
|
|
return isEndOfScript(crossline);
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// skip space
|
|
|
|
//
|
|
|
|
skipspace:
|
|
|
|
while (*script->script_p <= 32)
|
|
|
|
{
|
|
|
|
if (script->script_p >= script->end_p)
|
2018-09-23 21:17:31 +00:00
|
|
|
return isEndOfScript(crossline);
|
2015-04-16 16:55:32 +00:00
|
|
|
if (*script->script_p++ == '\n')
|
|
|
|
{
|
|
|
|
if (!crossline)
|
|
|
|
{
|
|
|
|
//printf("Line %i is incomplete\n",scriptline);
|
|
|
|
}
|
|
|
|
scriptline = script->line++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (script->script_p >= script->end_p)
|
2018-09-23 21:17:31 +00:00
|
|
|
return isEndOfScript(crossline);
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
// ; # // comments
|
2018-09-23 21:17:31 +00:00
|
|
|
if (*script->script_p == ';' || *script->script_p == '#' || (script->script_p[0] == '/' && script->script_p[1] == '/'))
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
|
|
|
if (!crossline)
|
|
|
|
{
|
|
|
|
//printf("Line %i is incomplete\n",scriptline);
|
|
|
|
}
|
|
|
|
while (*script->script_p++ != '\n')
|
|
|
|
if (script->script_p >= script->end_p)
|
2018-09-23 21:17:31 +00:00
|
|
|
return isEndOfScript(crossline);
|
2015-04-16 16:55:32 +00:00
|
|
|
scriptline = script->line++;
|
|
|
|
goto skipspace;
|
|
|
|
}
|
|
|
|
|
|
|
|
// /* */ comments
|
|
|
|
if (script->script_p[0] == '/' && script->script_p[1] == '*')
|
|
|
|
{
|
|
|
|
if (!crossline)
|
|
|
|
{
|
|
|
|
//printf("Line %i is incomplete\n",scriptline);
|
|
|
|
}
|
2018-09-23 21:17:31 +00:00
|
|
|
script->script_p += 2;
|
2015-04-16 16:55:32 +00:00
|
|
|
while (script->script_p[0] != '*' && script->script_p[1] != '/')
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
if (*script->script_p == '\n')
|
|
|
|
{
|
2015-04-16 16:55:32 +00:00
|
|
|
scriptline = script->line++;
|
|
|
|
}
|
|
|
|
script->script_p++;
|
|
|
|
if (script->script_p >= script->end_p)
|
2018-09-23 21:17:31 +00:00
|
|
|
return isEndOfScript(crossline);
|
2015-04-16 16:55:32 +00:00
|
|
|
}
|
|
|
|
script->script_p += 2;
|
|
|
|
goto skipspace;
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
//
|
|
|
|
// copy token
|
|
|
|
//
|
2015-04-16 16:55:32 +00:00
|
|
|
token_p = token;
|
|
|
|
|
|
|
|
if (*script->script_p == '"')
|
|
|
|
{
|
|
|
|
// quoted token
|
|
|
|
script->script_p++;
|
|
|
|
while (*script->script_p != '"')
|
|
|
|
{
|
|
|
|
*token_p++ = *script->script_p++;
|
|
|
|
if (script->script_p == script->end_p)
|
|
|
|
break;
|
|
|
|
if (token_p == &token[BSPMAXTOKEN])
|
|
|
|
{
|
|
|
|
//printf ("Token too large on line %i\n",scriptline);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
script->script_p++;
|
|
|
|
}
|
2018-09-23 21:17:31 +00:00
|
|
|
else // regular token
|
|
|
|
while (*script->script_p > 32 && *script->script_p != ';')
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
*token_p++ = *script->script_p++;
|
|
|
|
if (script->script_p == script->end_p)
|
|
|
|
break;
|
|
|
|
if (token_p == &token[BSPMAXTOKEN])
|
|
|
|
{
|
|
|
|
//printf ("Token too large on line %i\n",scriptline);
|
|
|
|
}
|
2015-04-16 16:55:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*token_p = 0;
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (!strcmp(token, "$include"))
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
|
|
|
//getToken (false);
|
|
|
|
//AddScriptToStack (token);
|
2018-09-23 21:17:31 +00:00
|
|
|
return false; //getToken (crossline);
|
2015-04-16 16:55:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *BspLoader::copystring(const char *s)
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
char *b;
|
|
|
|
b = (char *)malloc(strlen(s) + 1);
|
|
|
|
strcpy(b, s);
|
2015-04-16 16:55:32 +00:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
void BspLoader::stripTrailing(char *e)
|
|
|
|
{
|
|
|
|
char *s;
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
s = e + strlen(e) - 1;
|
2015-04-16 16:55:32 +00:00
|
|
|
while (s >= e && *s <= 32)
|
|
|
|
{
|
|
|
|
*s = 0;
|
|
|
|
s--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
=================
|
|
|
|
parseEpair
|
|
|
|
=================
|
|
|
|
*/
|
2018-09-23 21:17:31 +00:00
|
|
|
BSPKeyValuePair *BspLoader::parseEpair(void)
|
|
|
|
{
|
|
|
|
BSPKeyValuePair *e;
|
|
|
|
|
|
|
|
e = (struct BSPPair *)malloc(sizeof(BSPKeyValuePair));
|
|
|
|
memset(e, 0, sizeof(BSPKeyValuePair));
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (strlen(token) >= BSPMAX_KEY - 1)
|
|
|
|
{
|
2015-04-16 16:55:32 +00:00
|
|
|
//printf ("ParseEpar: token too long");
|
|
|
|
}
|
2018-09-23 21:17:31 +00:00
|
|
|
e->key = copystring(token);
|
|
|
|
getToken(false);
|
|
|
|
if (strlen(token) >= BSPMAX_VALUE - 1)
|
|
|
|
{
|
2015-04-16 16:55:32 +00:00
|
|
|
//printf ("ParseEpar: token too long");
|
|
|
|
}
|
2018-09-23 21:17:31 +00:00
|
|
|
e->value = copystring(token);
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
// strip trailing spaces that sometimes get accidentally
|
|
|
|
// added in the editor
|
2018-09-23 21:17:31 +00:00
|
|
|
stripTrailing(e->key);
|
|
|
|
stripTrailing(e->value);
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
parseEntity
|
|
|
|
================
|
|
|
|
*/
|
2018-09-23 21:17:31 +00:00
|
|
|
bool BspLoader::parseEntity(void)
|
|
|
|
{
|
|
|
|
BSPKeyValuePair *e;
|
|
|
|
BSPEntity *mapent;
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (!getToken(true))
|
|
|
|
{
|
2015-04-16 16:55:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (strcmp(token, "{"))
|
|
|
|
{
|
2015-04-16 16:55:32 +00:00
|
|
|
//printf ("parseEntity: { not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
BSPEntity bla;
|
|
|
|
bla.brushes = 0;
|
|
|
|
bla.epairs = 0;
|
|
|
|
bla.firstDrawSurf = 0;
|
|
|
|
bla.origin[0] = 0.f;
|
|
|
|
bla.origin[1] = 0.f;
|
|
|
|
bla.origin[2] = 0.f;
|
|
|
|
bla.patches = 0;
|
|
|
|
|
|
|
|
m_entities.push_back(bla);
|
2018-09-23 21:17:31 +00:00
|
|
|
mapent = &m_entities[m_entities.size() - 1];
|
2015-04-16 16:55:32 +00:00
|
|
|
m_num_entities++;
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
if (!getToken(true))
|
|
|
|
{
|
2015-04-16 16:55:32 +00:00
|
|
|
//printf("parseEntity: EOF without closing brace");
|
|
|
|
}
|
2018-09-23 21:17:31 +00:00
|
|
|
if (!strcmp(token, "}"))
|
|
|
|
{
|
2015-04-16 16:55:32 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-09-23 21:17:31 +00:00
|
|
|
e = (struct BSPPair *)parseEpair();
|
2015-04-16 16:55:32 +00:00
|
|
|
e->next = mapent->epairs;
|
|
|
|
mapent->epairs = e;
|
|
|
|
} while (1);
|
2018-09-23 21:17:31 +00:00
|
|
|
|
2015-04-16 16:55:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
parseEntities
|
|
|
|
|
|
|
|
Parses the dentdata string into entities
|
|
|
|
================
|
|
|
|
*/
|
2018-09-23 21:17:31 +00:00
|
|
|
void BspLoader::parseEntities(void)
|
|
|
|
{
|
2015-04-16 16:55:32 +00:00
|
|
|
m_num_entities = 0;
|
|
|
|
m_entities.clear();
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
parseFromMemory(&m_dentdata[0], m_entdatasize);
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
while (parseEntity())
|
|
|
|
{
|
|
|
|
}
|
2015-04-16 16:55:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int BspLoader::getMachineEndianness()
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
long int i = 1;
|
|
|
|
const char *p = (const char *)&i;
|
|
|
|
if (p[0] == 1) // Lowest address contains the least significant byte
|
|
|
|
return BSP_LITTLE_ENDIAN;
|
|
|
|
else
|
|
|
|
return BSP_BIG_ENDIAN;
|
2015-04-16 16:55:32 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
short BspLoader::isLittleShort(short l)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
|
|
|
if (machineEndianness() == BSP_BIG_ENDIAN)
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
unsigned char b1, b2;
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
b1 = l & 255;
|
|
|
|
b2 = (l >> 8) & 255;
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
return (b1 << 8) + b2;
|
2015-04-16 16:55:32 +00:00
|
|
|
}
|
|
|
|
//little endian
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
short BspLoader::isBigShort(short l)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
|
|
|
if (machineEndianness() == BSP_BIG_ENDIAN)
|
|
|
|
{
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
unsigned char b1, b2;
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
b1 = l & 255;
|
|
|
|
b2 = (l >> 8) & 255;
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
return (b1 << 8) + b2;
|
2015-04-16 16:55:32 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
int BspLoader::isLittleLong(int l)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
|
|
|
if (machineEndianness() == BSP_BIG_ENDIAN)
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
unsigned char b1, b2, b3, b4;
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
b1 = l & 255;
|
|
|
|
b2 = (l >> 8) & 255;
|
|
|
|
b3 = (l >> 16) & 255;
|
|
|
|
b4 = (l >> 24) & 255;
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
return ((int)b1 << 24) + ((int)b2 << 16) + ((int)b3 << 8) + b4;
|
2015-04-16 16:55:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//little endian
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
int BspLoader::isBigLong(int l)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
|
|
|
if (machineEndianness() == BSP_BIG_ENDIAN)
|
|
|
|
{
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
unsigned char b1, b2, b3, b4;
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
b1 = l & 255;
|
|
|
|
b2 = (l >> 8) & 255;
|
|
|
|
b3 = (l >> 16) & 255;
|
|
|
|
b4 = (l >> 24) & 255;
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
return ((int)b1 << 24) + ((int)b2 << 16) + ((int)b3 << 8) + b4;
|
2015-04-16 16:55:32 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
float BspLoader::isLittleFloat(float l)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
|
|
|
if (machineEndianness() == BSP_BIG_ENDIAN)
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
union {
|
|
|
|
unsigned char b[4];
|
|
|
|
float f;
|
|
|
|
} in, out;
|
|
|
|
|
2015-04-16 16:55:32 +00:00
|
|
|
in.f = l;
|
|
|
|
out.b[0] = in.b[3];
|
|
|
|
out.b[1] = in.b[2];
|
|
|
|
out.b[2] = in.b[1];
|
|
|
|
out.b[3] = in.b[0];
|
2018-09-23 21:17:31 +00:00
|
|
|
|
2015-04-16 16:55:32 +00:00
|
|
|
return out.f;
|
|
|
|
}
|
|
|
|
|
|
|
|
//little endian
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
float BspLoader::isBigFloat(float l)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
|
|
|
if (machineEndianness() == BSP_BIG_ENDIAN)
|
|
|
|
{
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
//little endian
|
2018-09-23 21:17:31 +00:00
|
|
|
union {
|
|
|
|
unsigned char b[4];
|
|
|
|
float f;
|
|
|
|
} in, out;
|
|
|
|
|
2015-04-16 16:55:32 +00:00
|
|
|
in.f = l;
|
|
|
|
out.b[0] = in.b[3];
|
|
|
|
out.b[1] = in.b[2];
|
|
|
|
out.b[2] = in.b[1];
|
|
|
|
out.b[3] = in.b[0];
|
2018-09-23 21:17:31 +00:00
|
|
|
|
2015-04-16 16:55:32 +00:00
|
|
|
return out.f;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// swapBlock
|
|
|
|
// If all values are 32 bits, this can be used to swap everything
|
|
|
|
//
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
void BspLoader::swapBlock(int *block, int sizeOfBlock)
|
|
|
|
{
|
|
|
|
int i;
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
sizeOfBlock >>= 2;
|
2018-09-23 21:17:31 +00:00
|
|
|
for (i = 0; i < sizeOfBlock; i++)
|
|
|
|
{
|
|
|
|
block[i] = isLittleLong(block[i]);
|
2015-04-16 16:55:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// copyLump
|
|
|
|
//
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
int BspLoader::copyLump(BSPHeader *header, int lump, void *dest, int size)
|
|
|
|
{
|
|
|
|
int length, ofs;
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
length = header->lumps[lump].filelen;
|
|
|
|
ofs = header->lumps[lump].fileofs;
|
2018-09-23 21:17:31 +00:00
|
|
|
|
2015-04-16 16:55:32 +00:00
|
|
|
//if ( length % size ) {
|
|
|
|
// printf ("loadBSPFile: odd lump size");
|
|
|
|
//}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
memcpy(dest, (unsigned char *)header + ofs, length);
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
return length / size;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// swapBSPFile
|
|
|
|
//
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
void BspLoader::swapBSPFile(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
// models
|
|
|
|
swapBlock((int *)&m_dmodels[0], m_nummodels * sizeof(m_dmodels[0]));
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
// shaders (don't swap the name)
|
2018-09-23 21:17:31 +00:00
|
|
|
for (i = 0; i < m_numShaders; i++)
|
|
|
|
{
|
|
|
|
m_dshaders[i].contentFlags = isLittleLong(m_dshaders[i].contentFlags);
|
|
|
|
m_dshaders[i].surfaceFlags = isLittleLong(m_dshaders[i].surfaceFlags);
|
2015-04-16 16:55:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// planes
|
2018-09-23 21:17:31 +00:00
|
|
|
swapBlock((int *)&m_dplanes[0], m_numplanes * sizeof(m_dplanes[0]));
|
|
|
|
|
2015-04-16 16:55:32 +00:00
|
|
|
// nodes
|
2018-09-23 21:17:31 +00:00
|
|
|
swapBlock((int *)&m_dnodes[0], m_numnodes * sizeof(m_dnodes[0]));
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
// leafs
|
2018-09-23 21:17:31 +00:00
|
|
|
swapBlock((int *)&m_dleafs[0], m_numleafs * sizeof(m_dleafs[0]));
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
// leaffaces
|
2018-09-23 21:17:31 +00:00
|
|
|
swapBlock((int *)&m_dleafsurfaces[0], m_numleafsurfaces * sizeof(m_dleafsurfaces[0]));
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
// leafbrushes
|
2018-09-23 21:17:31 +00:00
|
|
|
swapBlock((int *)&m_dleafbrushes[0], m_numleafbrushes * sizeof(m_dleafbrushes[0]));
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
// brushes
|
2018-09-23 21:17:31 +00:00
|
|
|
swapBlock((int *)&m_dbrushes[0], m_numbrushes * sizeof(m_dbrushes[0]));
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
// brushsides
|
2018-09-23 21:17:31 +00:00
|
|
|
swapBlock((int *)&m_dbrushsides[0], m_numbrushsides * sizeof(m_dbrushsides[0]));
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
// vis
|
2018-09-23 21:17:31 +00:00
|
|
|
((int *)&m_visBytes)[0] = isLittleLong(((int *)&m_visBytes)[0]);
|
|
|
|
((int *)&m_visBytes)[1] = isLittleLong(((int *)&m_visBytes)[1]);
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
// drawindexes
|
2018-09-23 21:17:31 +00:00
|
|
|
swapBlock((int *)&m_drawIndexes[0], m_numDrawIndexes * sizeof(m_drawIndexes[0]));
|
2015-04-16 16:55:32 +00:00
|
|
|
|
|
|
|
// drawsurfs
|
2018-09-23 21:17:31 +00:00
|
|
|
swapBlock((int *)&m_drawSurfaces[0], m_numDrawSurfaces * sizeof(m_drawSurfaces[0]));
|
2015-04-16 16:55:32 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
bool BspLoader::findVectorByName(float *outvec, const char *name)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
|
|
|
const char *cl;
|
|
|
|
BSPVector3 origin;
|
2018-09-23 21:17:31 +00:00
|
|
|
|
2015-04-16 16:55:32 +00:00
|
|
|
bool found = false;
|
|
|
|
|
|
|
|
parseEntities();
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
for (int i = 1; i < m_num_entities; i++)
|
|
|
|
{
|
|
|
|
cl = getValueForKey(&m_entities[i], "classname");
|
|
|
|
if (!strcmp(cl, "info_player_start"))
|
|
|
|
{
|
|
|
|
getVectorForKey(&m_entities[i], "origin", origin);
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!strcmp(cl, "info_player_deathmatch"))
|
|
|
|
{
|
|
|
|
getVectorForKey(&m_entities[i], "origin", origin);
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
2015-04-16 16:55:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (found)
|
|
|
|
{
|
|
|
|
outvec[0] = origin[0];
|
|
|
|
outvec[1] = origin[1];
|
|
|
|
outvec[2] = origin[2];
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
const BSPEntity *BspLoader::getEntityByValue(const char *name, const char *value)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
const BSPEntity *entity = NULL;
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
for (int i = 1; i < m_num_entities; i++)
|
|
|
|
{
|
|
|
|
const BSPEntity &ent = m_entities[i];
|
2015-04-16 16:55:32 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
const char *cl = getValueForKey(&m_entities[i], name);
|
|
|
|
if (!strcmp(cl, value))
|
|
|
|
{
|
2015-04-16 16:55:32 +00:00
|
|
|
entity = &ent;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return entity;
|
|
|
|
}
|