Clean up g_spawn.c and add sanity checks

This commit is contained in:
Yamagi Burmeister 2013-01-05 13:24:38 +01:00
parent 437c52de42
commit 214bd0a6f5

View file

@ -1,3 +1,9 @@
/* =======================================================================
*
* Item spawning.
*
* =======================================================================
*/
#include "header/local.h"
@ -7,7 +13,6 @@ typedef struct
void (*spawn)(edict_t *ent);
} spawn_t;
void SP_item_health(edict_t *self);
void SP_item_health_small(edict_t *self);
void SP_item_health_large(edict_t *self);
@ -125,7 +130,6 @@ void SP_turret_breach (edict_t *self);
void SP_turret_base(edict_t *self);
void SP_turret_driver(edict_t *self);
// RAFAEL 14-APR-98
void SP_monster_soldier_hypergun(edict_t *self);
void SP_monster_soldier_lasergun(edict_t *self);
void SP_monster_soldier_ripper(edict_t *self);
@ -141,11 +145,9 @@ void SP_misc_viper_missile (edict_t *self);
void SP_misc_amb4(edict_t *ent);
void SP_target_mal_laser(edict_t *ent);
void SP_misc_transport(edict_t *ent);
// END 14-APR-98
void SP_misc_nuke(edict_t *ent);
spawn_t spawns[] = {
{"item_health", SP_item_health},
{"item_health_small", SP_item_health_small},
@ -174,7 +176,6 @@ spawn_t spawns[] = {
{"func_explosive", SP_func_explosive},
{"func_killbox", SP_func_killbox},
// RAFAEL
{"func_object_repair", SP_object_repair},
{"rotating_light", SP_rotating_light},
@ -208,11 +209,8 @@ spawn_t spawns[] = {
{"target_earthquake", SP_target_earthquake},
{"target_character", SP_target_character},
{"target_string", SP_target_string},
// RAFAEL 15-APR-98
{"target_mal_laser", SP_target_mal_laser},
{"worldspawn", SP_worldspawn},
{"viewthing", SP_viewthing},
@ -244,14 +242,10 @@ spawn_t spawns[] = {
{"misc_eastertank", SP_misc_eastertank},
{"misc_easterchick", SP_misc_easterchick},
{"misc_easterchick2", SP_misc_easterchick2},
// RAFAEL
{"misc_crashviper", SP_misc_crashviper},
{"misc_viper_missile", SP_misc_viper_missile},
{"misc_amb4", SP_misc_amb4},
// RAFAEL 17-APR-98
{"misc_transport", SP_misc_transport},
// END 17-APR-98
// RAFAEL 12-MAY-98
{"misc_nuke", SP_misc_nuke},
{"monster_berserk", SP_monster_berserk},
@ -276,10 +270,7 @@ spawn_t spawns[] = {
{"monster_boss2", SP_monster_boss2},
{"monster_boss3_stand", SP_monster_boss3_stand},
{"monster_jorg", SP_monster_jorg},
{"monster_commander_body", SP_monster_commander_body},
// RAFAEL 14-APR-98
{"monster_soldier_hypergun", SP_monster_soldier_hypergun},
{"monster_soldier_lasergun", SP_monster_soldier_lasergun},
{"monster_soldier_ripper", SP_monster_soldier_ripper},
@ -288,7 +279,6 @@ spawn_t spawns[] = {
{"monster_chick_heat", SP_monster_chick_heat},
{"monster_gladb", SP_monster_gladb},
{"monster_boss5", SP_monster_boss5},
// END 14-APR-98
{"turret_breach", SP_turret_breach},
{"turret_base", SP_turret_base},
@ -298,54 +288,59 @@ spawn_t spawns[] = {
};
/*
===============
ED_CallSpawn
Finds the spawn function for the entity and calls it
===============
* Finds the spawn function for
* the entity and calls it
*/
void ED_CallSpawn (edict_t *ent)
void
ED_CallSpawn(edict_t *ent)
{
spawn_t *s;
gitem_t *item;
int i;
if (!ent)
{
return;
}
if (!ent->classname)
{
gi.dprintf("ED_CallSpawn: NULL classname\n");
return;
}
// check item spawn functions
/* check item spawn functions */
for (i = 0, item = itemlist; i < game.num_items; i++, item++)
{
if (!item->classname)
{
continue;
}
if (!strcmp(item->classname, ent->classname))
{ // found it
{
/* found it */
SpawnItem(ent, item);
return;
}
}
// check normal spawn functions
/* check normal spawn functions */
for (s = spawns; s->name; s++)
{
if (!strcmp(s->name, ent->classname))
{ // found it
{
/* found it */
s->spawn(ent);
return;
}
}
gi.dprintf("%s doesn't have a spawn function\n", ent->classname);
}
/*
=============
ED_NewString
=============
*/
char *ED_NewString (const char *string)
char *
ED_NewString(const char *string)
{
char *newb, *new_p;
int i, l;
@ -358,47 +353,58 @@ char *ED_NewString (const char *string)
for (i = 0; i < l; i++)
{
if (string[i] == '\\' && i < l-1)
if ((string[i] == '\\') && (i < l - 1))
{
i++;
if (string[i] == 'n')
{
*new_p++ = '\n';
else
*new_p++ = '\\';
}
else
{
*new_p++ = '\\';
}
}
else
{
*new_p++ = string[i];
}
}
return newb;
}
/*
===============
ED_ParseField
Takes a key/value pair and sets the binary values
in an edict
===============
* Takes a key/value pair and sets
* the binary values in an edict
*/
void ED_ParseField (const char *key, const char *value, edict_t *ent)
void
ED_ParseField(const char *key, const char *value, edict_t *ent)
{
field_t *f;
byte *b;
float v;
vec3_t vec;
if (!key || !value)
{
return;
}
for (f = fields; f->name; f++)
{
if (!(f->flags & FFL_NOSPAWN) && !Q_stricmp(f->name, key))
{ // found it
{
/* found it */
if (f->flags & FFL_SPAWNTEMP)
{
b = (byte *)&st;
}
else
{
b = (byte *)ent;
}
switch (f->type)
{
@ -428,21 +434,21 @@ void ED_ParseField (const char *key, const char *value, edict_t *ent)
default:
break;
}
return;
}
}
gi.dprintf("%s is not a field\n", key);
}
/*
====================
ED_ParseEdict
Parses an edict out of the given string, returning the new position
ed should be a properly initialized empty edict.
====================
* Parses an edict out of the given string,
* returning the new position. ed should be
* a properly initialized empty edict.
*/
char *ED_ParseEdict (char *data, edict_t *ent)
char *
ED_ParseEdict(char *data, edict_t *ent)
{
qboolean init;
char keyname[256];
@ -451,54 +457,66 @@ char *ED_ParseEdict (char *data, edict_t *ent)
init = false;
memset(&st, 0, sizeof(st));
// go through all the dictionary pairs
/* go through all the dictionary pairs */
while (1)
{
// parse key
/* parse key */
com_token = COM_Parse(&data);
if (com_token[0] == '}')
{
break;
}
if (!data)
{
gi.error("ED_ParseEntity: EOF without closing brace");
}
strncpy(keyname, com_token, sizeof(keyname) - 1);
// parse value
/* parse value */
com_token = COM_Parse(&data);
if (!data)
{
gi.error("ED_ParseEntity: EOF without closing brace");
}
if (com_token[0] == '}')
{
gi.error("ED_ParseEntity: closing brace without data");
}
init = true;
// keynames with a leading underscore are used for utility comments,
// and are immediately discarded by quake
/* keynames with a leading underscore are
used for utility comments, and are
immediately discarded by quake */
if (keyname[0] == '_')
{
continue;
}
ED_ParseField(keyname, com_token, ent);
}
if (!init)
{
memset(ent, 0, sizeof(*ent));
}
return data;
}
/*
================
G_FindTeams
Chain together all entities with a matching team field.
All but the first will have the FL_TEAMSLAVE flag set.
All but the last will have the teamchain field set to the next one
================
* Chain together all entities with a matching team field.
*
* All but the first will have the FL_TEAMSLAVE flag set.
* All but the last will have the teamchain field set to the next one
*/
void G_FindTeams (void)
void
G_FindTeams(void)
{
edict_t *e, *e2, *chain;
int i, j;
@ -506,26 +524,46 @@ void G_FindTeams (void)
c = 0;
c2 = 0;
for (i = 1, e = g_edicts + i; i < globals.num_edicts; i++, e++)
{
if (!e->inuse)
{
continue;
}
if (!e->team)
{
continue;
}
if (e->flags & FL_TEAMSLAVE)
{
continue;
}
chain = e;
e->teammaster = e;
c++;
c2++;
for (j = i + 1, e2 = e + 1; j < globals.num_edicts; j++, e2++)
{
if (!e2->inuse)
{
continue;
}
if (!e2->team)
{
continue;
}
if (e2->flags & FL_TEAMSLAVE)
{
continue;
}
if (!strcmp(e->team, e2->team))
{
c2++;
@ -541,14 +579,11 @@ void G_FindTeams (void)
}
/*
==============
SpawnEntities
Creates a server's entity / program execution context by
parsing textual entity definitions out of an ent file.
==============
* Creates a server's entity / program execution context by
* parsing textual entity definitions out of an ent file.
*/
void SpawnEntities (const char *mapname, char *entities, const char *spawnpoint)
void
SpawnEntities(const char *mapname, char *entities, const char *spawnpoint)
{
edict_t *ent;
int inhibit;
@ -556,13 +591,27 @@ void SpawnEntities (const char *mapname, char *entities, const char *spawnpoint)
int i;
float skill_level;
if (!mapname || !entities || !spawnpoint)
{
return;
}
skill_level = floor(skill->value);
if (skill_level < 0)
{
skill_level = 0;
}
if (skill_level > 3)
{
skill_level = 3;
}
if (skill->value != skill_level)
{
gi.cvar_forceset("skill", va("%f", skill_level));
}
SaveClientData();
@ -574,34 +623,52 @@ void SpawnEntities (const char *mapname, char *entities, const char *spawnpoint)
strncpy(level.mapname, mapname, sizeof(level.mapname) - 1);
strncpy(game.spawnpoint, spawnpoint, sizeof(game.spawnpoint) - 1);
// set client fields on player ents
/* set client fields on player ents */
for (i = 0; i < game.maxclients; i++)
{
g_edicts[i + 1].client = game.clients + i;
}
ent = NULL;
inhibit = 0;
// parse ents
/* parse ents */
while (1)
{
// parse the opening brace
/* parse the opening brace */
com_token = COM_Parse(&entities);
if (!entities)
{
break;
}
if (com_token[0] != '{')
{
gi.error("ED_LoadFromFile: found %s when expecting {", com_token);
}
if (!ent)
{
ent = g_edicts;
}
else
{
ent = G_Spawn();
}
entities = ED_ParseEdict(entities, ent);
// yet another map hack
if (!Q_stricmp(level.mapname, "command") && !Q_stricmp(ent->classname, "trigger_once") && !Q_stricmp(ent->model, "*27"))
/* yet another map hack */
if (!Q_stricmp(level.mapname, "command") &&
!Q_stricmp(ent->classname, "trigger_once") &&
!Q_stricmp(ent->model, "*27"))
{
ent->spawnflags &= ~SPAWNFLAG_NOT_HARD;
}
// remove things (except the world) from different skill levels or deathmatch
/* remove things (except the world) from
different skill levels or deathmatch */
if (ent != g_edicts)
{
if (deathmatch->value)
@ -615,10 +682,14 @@ void SpawnEntities (const char *mapname, char *entities, const char *spawnpoint)
}
else
{
if ( /* ((coop->value) && (ent->spawnflags & SPAWNFLAG_NOT_COOP)) || */
((skill->value == 0) && (ent->spawnflags & SPAWNFLAG_NOT_EASY)) ||
((skill->value == 1) && (ent->spawnflags & SPAWNFLAG_NOT_MEDIUM)) ||
(((skill->value == 2) || (skill->value == 3)) && (ent->spawnflags & SPAWNFLAG_NOT_HARD))
if (
((skill->value == 0) &&
(ent->spawnflags & SPAWNFLAG_NOT_EASY)) ||
((skill->value == 1) &&
(ent->spawnflags & SPAWNFLAG_NOT_MEDIUM)) ||
(((skill->value == 2) ||
(skill->value == 3)) &&
(ent->spawnflags & SPAWNFLAG_NOT_HARD))
)
{
G_FreeEdict(ent);
@ -627,7 +698,10 @@ void SpawnEntities (const char *mapname, char *entities, const char *spawnpoint)
}
}
ent->spawnflags &= ~(SPAWNFLAG_NOT_EASY|SPAWNFLAG_NOT_MEDIUM|SPAWNFLAG_NOT_HARD|SPAWNFLAG_NOT_COOP|SPAWNFLAG_NOT_DEATHMATCH);
ent->spawnflags &=
~(SPAWNFLAG_NOT_EASY | SPAWNFLAG_NOT_MEDIUM |
SPAWNFLAG_NOT_HARD |
SPAWNFLAG_NOT_COOP | SPAWNFLAG_NOT_DEATHMATCH);
}
ED_CallSpawn(ent);
@ -640,19 +714,18 @@ void SpawnEntities (const char *mapname, char *entities, const char *spawnpoint)
PlayerTrail_Init();
}
//===================================================================
/* =================================================================== */
char *single_statusbar =
"yb -24 "
// health
/* health */
"xv 0 "
"hnum "
"xv 50 "
"pic 0 "
// ammo
/* ammo */
"if 2 "
" xv 100 "
" anum "
@ -660,7 +733,7 @@ char *single_statusbar =
" pic 2 "
"endif "
// armor
/* armor */
"if 4 "
" xv 200 "
" rnum "
@ -668,7 +741,7 @@ char *single_statusbar =
" pic 4 "
"endif "
// selected item
/* selected item */
"if 6 "
" xv 296 "
" pic 6 "
@ -676,7 +749,7 @@ char *single_statusbar =
"yb -50 "
// picked up item
/* picked up item */
"if 7 "
" xv 0 "
" pic 7 "
@ -686,7 +759,7 @@ char *single_statusbar =
" yb -50 "
"endif "
// timer
/* timer */
"if 9 "
" xv 262 "
" num 2 10 "
@ -694,7 +767,7 @@ char *single_statusbar =
" pic 9 "
"endif "
// help / weapon icon
/* help / weapon icon */
"if 11 "
" xv 148 "
" pic 11 "
@ -704,13 +777,13 @@ char *single_statusbar =
char *dm_statusbar =
"yb -24 "
// health
/* health */
"xv 0 "
"hnum "
"xv 50 "
"pic 0 "
// ammo
/* ammo */
"if 2 "
" xv 100 "
" anum "
@ -718,7 +791,7 @@ char *dm_statusbar =
" pic 2 "
"endif "
// armor
/* armor */
"if 4 "
" xv 200 "
" rnum "
@ -726,7 +799,7 @@ char *dm_statusbar =
" pic 4 "
"endif "
// selected item
/* selected item */
"if 6 "
" xv 296 "
" pic 6 "
@ -734,7 +807,7 @@ char *dm_statusbar =
"yb -50 "
// picked up item
/* picked up item */
"if 7 "
" xv 0 "
" pic 7 "
@ -744,7 +817,7 @@ char *dm_statusbar =
" yb -50 "
"endif "
// timer
/* timer */
"if 9 "
" xv 246 "
" num 2 10 "
@ -752,25 +825,25 @@ char *dm_statusbar =
" pic 9 "
"endif "
// help / weapon icon
/* help / weapon icon */
"if 11 "
" xv 148 "
" pic 11 "
"endif "
// frags
/* frags */
"xr -50 "
"yt 2 "
"num 3 14 "
// spectator
/* spectator */
"if 17 "
"xv 0 "
"yb -58 "
"string2 \"SPECTATOR MODE\" "
"endif "
// chase camera
/* chase camera */
"if 16 "
"xv 0 "
"yb -68 "
@ -780,80 +853,96 @@ char *dm_statusbar =
"endif "
;
/*QUAKED worldspawn (0 0 0) ?
Only used for the world.
"sky" environment map name
"skyaxis" vector axis for rotating sky
"skyrotate" speed of rotation in degrees/second
"sounds" music cd track number
"gravity" 800 is default gravity
"message" text to print at user logon
*
* Only used for the world.
* "sky" environment map name
* "skyaxis" vector axis for rotating sky
* "skyrotate" speed of rotation in degrees/second
* "sounds" music cd track number
* "gravity" 800 is default gravity
* "message" text to print at user logon
*/
void SP_worldspawn (edict_t *ent)
void
SP_worldspawn(edict_t *ent)
{
if (!ent)
{
return;
}
ent->movetype = MOVETYPE_PUSH;
ent->solid = SOLID_BSP;
ent->inuse = true; // since the world doesn't use G_Spawn()
ent->s.modelindex = 1; // world model is always index 1
ent->inuse = true; /* since the world doesn't use G_Spawn() */
ent->s.modelindex = 1; /* world model is always index 1 */
//---------------
// reserve some spots for dead player bodies for coop / deathmatch
/* reserve some spots for dead player
bodies for coop / deathmatch */
InitBodyQue();
// set configstrings for items
/* set configstrings for items */
SetItemNames();
if (st.nextmap)
{
strcpy(level.nextmap, st.nextmap);
}
// make some data visible to the server
/* make some data visible to the server */
if (ent->message && ent->message[0])
{
gi.configstring(CS_NAME, ent->message);
strncpy(level.level_name, ent->message, sizeof(level.level_name));
}
else
{
strncpy(level.level_name, level.mapname, sizeof(level.level_name));
}
if (st.sky && st.sky[0])
{
gi.configstring(CS_SKY, st.sky);
}
else
{
gi.configstring(CS_SKY, "unit1_");
}
gi.configstring(CS_SKYROTATE, va("%f", st.skyrotate));
gi.configstring (CS_SKYAXIS, va("%f %f %f",
st.skyaxis[0], st.skyaxis[1], st.skyaxis[2]) );
gi.configstring(CS_SKYAXIS, va("%f %f %f", st.skyaxis[0],
st.skyaxis[1], st.skyaxis[2]));
gi.configstring(CS_CDTRACK, va("%i", ent->sounds));
gi.configstring(CS_MAXCLIENTS, va("%i", (int)(maxclients->value)));
// status bar program
/* status bar program */
if (deathmatch->value)
{
gi.configstring(CS_STATUSBAR, dm_statusbar);
}
else
{
gi.configstring(CS_STATUSBAR, single_statusbar);
}
//---------------
// help icon for statusbar
/* help icon for statusbar */
gi.imageindex("i_help");
level.pic_health = gi.imageindex("i_health");
gi.imageindex("help");
gi.imageindex("field_3");
if (!st.gravity)
{
gi.cvar_set("sv_gravity", "800");
}
else
{
gi.cvar_set("sv_gravity", st.gravity);
}
snd_fry = gi.soundindex ("player/fry.wav"); // standing in lava / slime
snd_fry = gi.soundindex("player/fry.wav"); /* standing in lava / slime */
PrecacheItem(FindItem("Blaster"));
@ -865,19 +954,19 @@ void SP_worldspawn (edict_t *ent)
gi.soundindex("misc/udeath.wav");
// gibs
/* gibs */
gi.soundindex("items/respawn1.wav");
// sexed sounds
/* sexed sounds */
gi.soundindex("*death1.wav");
gi.soundindex("*death2.wav");
gi.soundindex("*death3.wav");
gi.soundindex("*death4.wav");
gi.soundindex("*fall1.wav");
gi.soundindex("*fall2.wav");
gi.soundindex ("*gurp1.wav"); // drowning damage
gi.soundindex("*gurp1.wav"); /* drowning damage */
gi.soundindex("*gurp2.wav");
gi.soundindex ("*jump1.wav"); // player jump
gi.soundindex("*jump1.wav"); /* player jump */
gi.soundindex("*pain25_1.wav");
gi.soundindex("*pain25_2.wav");
gi.soundindex("*pain50_1.wav");
@ -887,10 +976,10 @@ void SP_worldspawn (edict_t *ent)
gi.soundindex("*pain100_1.wav");
gi.soundindex("*pain100_2.wav");
// sexed models
// THIS ORDER MUST MATCH THE DEFINES IN g_local.h
// you can add more, max 19 (pete change)
// these models are only loaded in coop or deathmatch. not singleplayer.
/* sexed models. you can add more, max 19
THIS ORDER MUST MATCH THE DEFINES IN g_local.h
these models are only loaded in coop or deathmatch.
not singleplayer. */
if (coop->value || deathmatch->value)
{
gi.modelindex("#w_blaster.md2");
@ -909,22 +998,22 @@ void SP_worldspawn (edict_t *ent)
gi.modelindex("#w_ripper.md2");
}
//-------------------
/* ------------------- */
gi.soundindex ("player/gasp1.wav"); // gasping for air
gi.soundindex ("player/gasp2.wav"); // head breaking surface, not gasping
gi.soundindex("player/gasp1.wav"); /* gasping for air */
gi.soundindex("player/gasp2.wav"); /* head breaking surface, not gasping */
gi.soundindex ("player/watr_in.wav"); // feet hitting water
gi.soundindex ("player/watr_out.wav"); // feet leaving water
gi.soundindex("player/watr_in.wav"); /* feet hitting water */
gi.soundindex("player/watr_out.wav"); /* feet leaving water */
gi.soundindex ("player/watr_un.wav"); // head going underwater
gi.soundindex("player/watr_un.wav"); /* head going underwater */
gi.soundindex("player/u_breath1.wav");
gi.soundindex("player/u_breath2.wav");
gi.soundindex ("items/pkup.wav"); // bonus item pickup
gi.soundindex ("world/land.wav"); // landing thud
gi.soundindex ("misc/h2ohit1.wav"); // landing splash
gi.soundindex("items/pkup.wav"); /* bonus item pickup */
gi.soundindex("world/land.wav"); /* landing thud */
gi.soundindex("misc/h2ohit1.wav"); /* landing splash */
gi.soundindex("items/damage.wav");
gi.soundindex("items/protect.wav");
@ -941,49 +1030,47 @@ void SP_worldspawn (edict_t *ent)
gi.modelindex("models/objects/gibs/skull/tris.md2");
gi.modelindex("models/objects/gibs/head2/tris.md2");
//
// Setup light animation tables. 'a' is total darkness, 'z' is doublebright.
//
/* Setup light animation tables. 'a' is total darkness, 'z' is doublebright. */
// 0 normal
/* 0 normal */
gi.configstring(CS_LIGHTS + 0, "m");
// 1 FLICKER (first variety)
/* 1 FLICKER (first variety) */
gi.configstring(CS_LIGHTS + 1, "mmnmmommommnonmmonqnmmo");
// 2 SLOW STRONG PULSE
/* 2 SLOW STRONG PULSE */
gi.configstring(CS_LIGHTS + 2, "abcdefghijklmnopqrstuvwxyzyxwvutsrqponmlkjihgfedcba");
// 3 CANDLE (first variety)
/* 3 CANDLE (first variety) */
gi.configstring(CS_LIGHTS + 3, "mmmmmaaaaammmmmaaaaaabcdefgabcdefg");
// 4 FAST STROBE
/* 4 FAST STROBE */
gi.configstring(CS_LIGHTS + 4, "mamamamamama");
// 5 GENTLE PULSE 1
/* 5 GENTLE PULSE 1 */
gi.configstring(CS_LIGHTS + 5, "jklmnopqrstuvwxyzyxwvutsrqponmlkj");
// 6 FLICKER (second variety)
/* 6 FLICKER (second variety) */
gi.configstring(CS_LIGHTS + 6, "nmonqnmomnmomomno");
// 7 CANDLE (second variety)
/* 7 CANDLE (second variety) */
gi.configstring(CS_LIGHTS + 7, "mmmaaaabcdefgmmmmaaaammmaamm");
// 8 CANDLE (third variety)
/* 8 CANDLE (third variety) */
gi.configstring(CS_LIGHTS + 8, "mmmaaammmaaammmabcdefaaaammmmabcdefmmmaaaa");
// 9 SLOW STROBE (fourth variety)
/* 9 SLOW STROBE (fourth variety) */
gi.configstring(CS_LIGHTS + 9, "aaaaaaaazzzzzzzz");
// 10 FLUORESCENT FLICKER
/* 10 FLUORESCENT FLICKER */
gi.configstring(CS_LIGHTS + 10, "mmamammmmammamamaaamammma");
// 11 SLOW PULSE NOT FADE TO BLACK
/* 11 SLOW PULSE NOT FADE TO BLACK */
gi.configstring(CS_LIGHTS + 11, "abcdefghijklmnopqrrqponmlkjihgfedcba");
// styles 32-62 are assigned by the light program for switchable lights
/* styles 32-62 are assigned by the light program for switchable lights */
// 63 testing
/* 63 testing */
gi.configstring(CS_LIGHTS + 63, "a");
}