quakeforge/tools/qfbsp/source/map.c

537 lines
10 KiB
C
Raw Normal View History

2002-09-19 18:51:19 +00:00
/*
Copyright (C) 1996-1997 Id Software, Inc.
2002-09-19 18:51:19 +00:00
This program 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.
2002-09-19 18:51:19 +00:00
This program 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.
2002-09-19 18:51:19 +00:00
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2002-09-19 18:51:19 +00:00
See file, 'COPYING', for details.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
static __attribute__ ((unused)) const char rcsid[] =
"$Id$";
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <stdlib.h>
#include "QF/dstring.h"
#include "QF/quakefs.h"
#include "QF/sys.h"
#include "bsp5.h"
2002-09-19 17:14:23 +00:00
int nummapbrushes;
mbrush_t mapbrushes[MAX_MAP_BRUSHES];
2002-09-19 17:14:23 +00:00
int num_entities;
entity_t entities[MAX_MAP_ENTITIES];
2002-09-19 17:14:23 +00:00
int nummiptex;
char miptex[MAX_MAP_TEXINFO][16];
2002-09-19 17:14:23 +00:00
int
FindMiptex (const char *name)
{
2002-09-19 17:14:23 +00:00
int i;
for (i = 0; i < nummiptex; i++) {
if (!strcmp (name, miptex[i]))
return i;
}
if (nummiptex == MAX_MAP_TEXINFO)
Sys_Error ("nummiptex == MAX_MAP_TEXINFO");
strcpy (miptex[i], name);
nummiptex++;
return i;
}
/*
2002-09-23 16:27:17 +00:00
FindTexinfo
2002-09-23 16:27:17 +00:00
Returns a global texinfo number
*/
static int
2002-09-19 17:14:23 +00:00
FindTexinfo (texinfo_t *t)
{
2002-09-19 17:14:23 +00:00
int i, j;
texinfo_t *tex;
2002-09-23 16:27:17 +00:00
// set the special flag
2002-09-19 17:14:23 +00:00
if (miptex[t->miptex][0] == '*'
2002-09-19 18:51:19 +00:00
|| !strncasecmp (miptex[t->miptex], "sky", 3))
t->flags |= TEX_SPECIAL;
2002-09-19 17:14:23 +00:00
tex = bsp->texinfo;
for (i = 0; i < bsp->numtexinfo; i++, tex++) {
if (t->miptex != tex->miptex)
continue;
if (t->flags != tex->flags)
continue;
2002-09-19 17:14:23 +00:00
for (j = 0; j < 8; j++)
if (t->vecs[0][j] != tex->vecs[0][j])
break;
if (j != 8)
continue;
2002-09-19 17:14:23 +00:00
return i;
}
2002-09-19 17:14:23 +00:00
2002-09-23 16:27:17 +00:00
// allocate a new texture
BSP_AddTexinfo (bsp, t);
return i;
}
#define MAXTOKEN 128
2002-09-19 17:14:23 +00:00
char token[MAXTOKEN];
qboolean unget;
char *script_p;
int scriptline;
static void
2002-09-19 17:14:23 +00:00
StartTokenParsing (char *data)
{
scriptline = 1;
script_p = data;
unget = false;
}
static qboolean
2002-09-19 17:14:23 +00:00
GetToken (qboolean crossline)
{
2002-09-19 17:14:23 +00:00
char *token_p;
2002-09-19 17:14:23 +00:00
if (unget) // is a token allready waiting?
return true;
2002-09-23 16:27:17 +00:00
// skip space
2002-09-19 17:14:23 +00:00
skipspace:
while (*script_p <= 32) {
if (!*script_p) {
if (!crossline)
2002-09-19 17:14:23 +00:00
Sys_Error ("Line %i is incomplete", scriptline);
return false;
}
2002-09-19 17:14:23 +00:00
if (*script_p++ == '\n') {
if (!crossline)
2002-09-19 17:14:23 +00:00
Sys_Error ("Line %i is incomplete", scriptline);
scriptline++;
}
}
2002-09-19 18:51:19 +00:00
if (script_p[0] == '/' && script_p[1] == '/') { // comment field
if (!crossline)
2002-09-19 17:14:23 +00:00
Sys_Error ("Line %i is incomplete\n", scriptline);
while (*script_p++ != '\n')
2002-09-19 17:14:23 +00:00
if (!*script_p) {
if (!crossline)
2002-09-19 17:14:23 +00:00
Sys_Error ("Line %i is incomplete", scriptline);
return false;
}
goto skipspace;
}
2002-09-19 18:51:19 +00:00
2002-09-23 16:27:17 +00:00
// copy token
token_p = token;
2002-09-19 17:14:23 +00:00
if (*script_p == '"') {
script_p++;
2002-09-19 17:14:23 +00:00
while (*script_p != '"') {
if (!*script_p)
Sys_Error ("EOF inside quoted token");
*token_p++ = *script_p++;
2002-09-19 17:14:23 +00:00
if (token_p > &token[MAXTOKEN - 1])
Sys_Error ("Token too large on line %i", scriptline);
}
script_p++;
2002-09-19 17:14:23 +00:00
} else
while (*script_p > 32) {
*token_p++ = *script_p++;
if (token_p > &token[MAXTOKEN - 1])
Sys_Error ("Token too large on line %i", scriptline);
}
*token_p = 0;
2002-09-19 17:14:23 +00:00
return true;
}
2002-09-19 17:14:23 +00:00
entity_t *mapent;
static void
2002-09-19 17:14:23 +00:00
ParseEpair (void)
{
2002-09-19 17:14:23 +00:00
epair_t *e;
e = malloc (sizeof (epair_t));
memset (e, 0, sizeof (epair_t));
e->next = mapent->epairs;
mapent->epairs = e;
2002-09-19 17:14:23 +00:00
if (strlen (token) >= MAX_KEY - 1)
Sys_Error ("ParseEpar: token too long");
e->key = strdup (token);
GetToken (false);
2002-09-19 17:14:23 +00:00
if (strlen (token) >= MAX_VALUE - 1)
Sys_Error ("ParseEpar: token too long");
e->value = strdup (token);
}
2002-09-19 17:14:23 +00:00
vec3_t baseaxis[18] = {
{0, 0, 1}, {1, 0, 0}, {0, -1, 0}, // floor
{0, 0, -1}, {1, 0, 0}, {0, -1, 0}, // ceiling
{1, 0, 0}, {0, 1, 0}, {0, 0, -1}, // west wall
{-1, 0, 0}, {0, 1, 0}, {0, 0, -1}, // east wall
{0, 1, 0}, {1, 0, 0}, {0, 0, -1}, // south wall
{0, -1, 0}, {1, 0, 0}, {0, 0, -1} // north wall
};
static void
2002-09-19 17:14:23 +00:00
TextureAxisFromPlane (plane_t *pln, vec3_t xv, vec3_t yv)
{
2002-09-19 17:14:23 +00:00
float dot, best;
2002-09-19 18:51:19 +00:00
int bestaxis, i;
2002-09-19 17:14:23 +00:00
best = 0;
bestaxis = 0;
2002-09-19 17:14:23 +00:00
for (i = 0; i < 6; i++) {
dot = DotProduct (pln->normal, baseaxis[i * 3]);
if (dot > best) {
best = dot;
bestaxis = i;
}
}
2002-09-19 17:14:23 +00:00
VectorCopy (baseaxis[bestaxis * 3 + 1], xv);
VectorCopy (baseaxis[bestaxis * 3 + 2], yv);
}
static void
2002-09-19 17:14:23 +00:00
ParseBrush (void)
{
2002-09-19 18:51:19 +00:00
float shift[2], rotate, scale[2];
int i, j;
2002-09-19 17:14:23 +00:00
mbrush_t *b;
mface_t *f, *f2;
2002-09-19 18:51:19 +00:00
texinfo_t tx;
2002-09-19 17:14:23 +00:00
vec3_t planepts[3];
vec3_t t1, t2, t3;
vec_t d;
b = &mapbrushes[nummapbrushes];
nummapbrushes++;
b->next = mapent->brushes;
mapent->brushes = b;
2002-09-19 17:14:23 +00:00
do {
if (!GetToken (true))
break;
2002-09-19 17:14:23 +00:00
if (!strcmp (token, "}"))
break;
2002-09-19 17:14:23 +00:00
// read the three point plane definition
for (i = 0; i < 3; i++) {
if (i != 0)
GetToken (true);
2002-09-19 17:14:23 +00:00
if (strcmp (token, "("))
Sys_Error ("parsing brush");
2002-09-19 17:14:23 +00:00
for (j = 0; j < 3; j++) {
GetToken (false);
2002-09-19 17:14:23 +00:00
planepts[i][j] = atoi (token);
}
2002-09-19 17:14:23 +00:00
GetToken (false);
2002-09-19 17:14:23 +00:00
if (strcmp (token, ")"))
Sys_Error ("parsing brush");
}
2002-09-19 17:14:23 +00:00
// read the texturedef
memset (&tx, 0, sizeof (tx));
GetToken (false);
tx.miptex = FindMiptex (token);
GetToken (false);
2002-09-19 17:14:23 +00:00
shift[0] = atoi (token);
GetToken (false);
2002-09-19 17:14:23 +00:00
shift[1] = atoi (token);
GetToken (false);
2002-09-19 17:14:23 +00:00
rotate = atoi (token);
GetToken (false);
2002-09-19 17:14:23 +00:00
scale[0] = atof (token);
GetToken (false);
2002-09-19 17:14:23 +00:00
scale[1] = atof (token);
2002-09-19 18:51:19 +00:00
// if the three points are all on a previous plane, it is a duplicate
// plane
2002-09-19 17:14:23 +00:00
for (f2 = b->faces; f2; f2 = f2->next) {
for (i = 0; i < 3; i++) {
2002-09-19 18:51:19 +00:00
d = DotProduct (planepts[i], f2->plane.normal)
- f2->plane.dist;
if (d < -ON_EPSILON || d > ON_EPSILON)
break;
}
2002-09-19 17:14:23 +00:00
if (i == 3)
break;
}
2002-09-19 17:14:23 +00:00
if (f2) {
printf ("WARNING: brush with duplicate plane\n");
continue;
}
2002-09-19 17:14:23 +00:00
f = malloc (sizeof (mface_t));
f->next = b->faces;
b->faces = f;
2002-09-19 17:14:23 +00:00
// convert to a vector / dist plane
for (j = 0; j < 3; j++) {
t1[j] = planepts[0][j] - planepts[1][j];
t2[j] = planepts[2][j] - planepts[1][j];
t3[j] = planepts[1][j];
}
2002-09-19 17:14:23 +00:00
CrossProduct (t1, t2, f->plane.normal);
if (_VectorCompare (f->plane.normal, vec3_origin)) {
printf ("WARNING: brush plane with no normal\n");
b->faces = f->next;
free (f);
break;
}
_VectorNormalize (f->plane.normal);
f->plane.dist = DotProduct (t3, f->plane.normal);
2002-09-19 17:14:23 +00:00
// fake proper texture vectors from QuakeEd style
{
2002-09-19 18:51:19 +00:00
float ang, sinv, cosv, ns, nt;
2002-09-19 17:14:23 +00:00
int sv, tv;
2002-09-19 18:51:19 +00:00
vec3_t vecs[2];
2002-09-19 17:14:23 +00:00
TextureAxisFromPlane (&f->plane, vecs[0], vecs[1]);
if (!scale[0])
scale[0] = 1;
if (!scale[1])
scale[1] = 1;
2002-09-19 17:14:23 +00:00
// rotate axis
if (rotate == 0) {
sinv = 0;
cosv = 1;
} else if (rotate == 90) {
sinv = 1;
cosv = 0;
} else if (rotate == 180) {
sinv = 0;
cosv = -1;
} else if (rotate == 270) {
sinv = -1;
cosv = 0;
} else {
ang = rotate / 180 * M_PI;
2002-09-19 17:14:23 +00:00
sinv = sin (ang);
cosv = cos (ang);
}
2002-09-19 17:14:23 +00:00
if (vecs[0][0])
sv = 0;
else if (vecs[0][1])
sv = 1;
else
sv = 2;
2002-09-19 17:14:23 +00:00
if (vecs[1][0])
tv = 0;
else if (vecs[1][1])
tv = 1;
else
tv = 2;
2002-09-19 17:14:23 +00:00
for (i = 0; i < 2; i++) {
ns = cosv * vecs[i][sv] - sinv * vecs[i][tv];
2002-09-19 17:14:23 +00:00
nt = sinv * vecs[i][sv] + cosv * vecs[i][tv];
vecs[i][sv] = ns;
vecs[i][tv] = nt;
}
2002-09-19 17:14:23 +00:00
for (i = 0; i < 2; i++)
for (j = 0; j < 3; j++)
tx.vecs[i][j] = vecs[i][j] / scale[i];
2002-09-19 17:14:23 +00:00
tx.vecs[0][3] = shift[0];
tx.vecs[1][3] = shift[1];
}
2002-09-19 17:14:23 +00:00
// unique the texinfo
f->texinfo = FindTexinfo (&tx);
} while (1);
}
static qboolean
2002-09-19 17:14:23 +00:00
ParseEntity (void)
{
if (!GetToken (true))
return false;
2002-09-19 17:14:23 +00:00
if (strcmp (token, "{"))
Sys_Error ("ParseEntity: { not found");
2002-09-19 17:14:23 +00:00
if (num_entities == MAX_MAP_ENTITIES)
Sys_Error ("num_entities == MAX_MAP_ENTITIES");
mapent = &entities[num_entities];
num_entities++;
2002-09-19 17:14:23 +00:00
do {
if (!GetToken (true))
Sys_Error ("ParseEntity: EOF without closing brace");
2002-09-19 17:14:23 +00:00
if (!strcmp (token, "}"))
break;
2002-09-19 17:14:23 +00:00
if (!strcmp (token, "{"))
ParseBrush ();
else
ParseEpair ();
} while (1);
2002-09-19 17:14:23 +00:00
GetVectorForKey (mapent, "origin", mapent->origin);
return true;
}
2002-09-19 17:14:23 +00:00
void
LoadMapFile (const char *filename)
{
2002-09-19 17:14:23 +00:00
char *buf;
QFile *file;
2002-09-19 17:14:23 +00:00
file = Qopen (filename, "rt");
buf = malloc (Qfilesize (file) + 1);
buf[Qfilesize (file)] = 0;
Qread (file, buf, Qfilesize (file));
Qclose (file);
StartTokenParsing (buf);
2002-09-19 17:14:23 +00:00
num_entities = 0;
2002-09-19 17:14:23 +00:00
while (ParseEntity ()) {
}
2002-09-19 17:14:23 +00:00
free (buf);
2002-09-19 17:14:23 +00:00
qprintf ("--- LoadMapFile ---\n");
qprintf ("%s\n", filename);
qprintf ("%5i brushes\n", nummapbrushes);
qprintf ("%5i entities\n", num_entities);
qprintf ("%5i miptex\n", nummiptex);
qprintf ("%5i texinfo\n", bsp->numtexinfo);
}
2002-09-19 17:14:23 +00:00
void
PrintEntity (entity_t *ent)
{
2002-09-19 17:14:23 +00:00
epair_t *ep;
for (ep = ent->epairs; ep; ep = ep->next)
printf ("%20s : %s\n", ep->key, ep->value);
}
const char *
ValueForKey (entity_t *ent, const char *key)
{
2002-09-19 17:14:23 +00:00
epair_t *ep;
for (ep = ent->epairs; ep; ep = ep->next)
if (!strcmp (ep->key, key))
return ep->value;
return "";
}
2002-09-19 17:14:23 +00:00
void
SetKeyValue (entity_t *ent, const char *key, const char *value)
{
2002-09-19 17:14:23 +00:00
epair_t *ep;
for (ep = ent->epairs; ep; ep = ep->next)
if (!strcmp (ep->key, key)) {
free (ep->value);
ep->value = strdup (value);
return;
}
2002-09-19 17:14:23 +00:00
ep = malloc (sizeof (*ep));
ep->next = ent->epairs;
ent->epairs = ep;
ep->key = strdup (key);
ep->value = strdup (value);
}
2002-09-19 17:14:23 +00:00
float
FloatForKey (entity_t *ent, const char *key)
{
const char *k;
2002-09-19 17:14:23 +00:00
k = ValueForKey (ent, key);
2002-09-19 17:14:23 +00:00
return atof (k);
}
2002-09-19 17:14:23 +00:00
void
GetVectorForKey (entity_t *ent, const char *key, vec3_t vec)
{
const char *k;
2002-09-19 17:14:23 +00:00
double v1, v2, v3;
k = ValueForKey (ent, key);
v1 = v2 = v3 = 0;
2002-09-23 16:27:17 +00:00
// scanf into doubles, then assign, so it is vec_t size independent
sscanf (k, "%lf %lf %lf", &v1, &v2, &v3);
vec[0] = v1;
vec[1] = v2;
vec[2] = v3;
}
2002-09-19 17:14:23 +00:00
void
WriteEntitiesToString (void)
{
dstring_t *buf;
2002-09-19 17:14:23 +00:00
char line[128];
2002-09-19 18:51:19 +00:00
epair_t *ep;
2002-09-19 17:14:23 +00:00
int i;
buf = dstring_newstr ();
2002-09-19 17:14:23 +00:00
for (i = 0; i < num_entities; i++) {
ep = entities[i].epairs;
if (!ep)
2002-09-19 17:14:23 +00:00
continue; // ent got removed
dstring_appendstr (buf, "{\n");
2002-09-19 17:14:23 +00:00
for (ep = entities[i].epairs; ep; ep = ep->next) {
sprintf (line, "\"%s\" \"%s\"\n", ep->key, ep->value);
dstring_appendstr (buf, line);
}
dstring_appendstr (buf, "}\n");
}
BSP_AddEntities (bsp, buf->str, buf->size);
}