yquake2remaster/src/game/g_main.c

477 lines
8.8 KiB
C
Raw Normal View History

/*
2011-09-28 16:38:01 +00:00
* Copyright (C) 1997-2001 Id Software, Inc.
*
* 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.
*
* 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.
*
* 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.
*
* =======================================================================
*
* Jump in into the game.so and support functions.
*
* =======================================================================
2012-04-29 13:57:33 +00:00
*/
2011-10-06 07:54:45 +00:00
#include "header/local.h"
2011-09-28 16:38:01 +00:00
game_locals_t game;
level_locals_t level;
game_import_t gi;
game_export_t globals;
spawn_temp_t st;
2011-09-28 16:38:01 +00:00
int sm_meat_index;
int snd_fry;
int meansOfDeath;
2011-09-28 16:38:01 +00:00
edict_t *g_edicts;
2011-09-28 16:38:01 +00:00
cvar_t *deathmatch;
cvar_t *coop;
cvar_t *dmflags;
cvar_t *skill;
cvar_t *fraglimit;
cvar_t *timelimit;
cvar_t *password;
cvar_t *spectator_password;
cvar_t *needpass;
cvar_t *maxclients;
cvar_t *maxspectators;
cvar_t *maxentities;
cvar_t *g_select_empty;
cvar_t *dedicated;
2011-09-28 16:38:01 +00:00
cvar_t *filterban;
2011-09-28 16:38:01 +00:00
cvar_t *sv_maxvelocity;
cvar_t *sv_gravity;
2011-09-28 16:38:01 +00:00
cvar_t *sv_rollspeed;
cvar_t *sv_rollangle;
cvar_t *gun_x;
cvar_t *gun_y;
cvar_t *gun_z;
2011-09-28 16:38:01 +00:00
cvar_t *run_pitch;
cvar_t *run_roll;
cvar_t *bob_up;
cvar_t *bob_pitch;
cvar_t *bob_roll;
2011-09-28 16:38:01 +00:00
cvar_t *sv_cheats;
2011-09-28 16:38:01 +00:00
cvar_t *flood_msgs;
cvar_t *flood_persecond;
cvar_t *flood_waitdelay;
2011-09-28 16:38:01 +00:00
cvar_t *sv_maplist;
cvar_t *gib_on;
2011-09-28 16:38:01 +00:00
void SpawnEntities(char *mapname, char *entities, char *spawnpoint);
void ClientThink(edict_t *ent, usercmd_t *cmd);
qboolean ClientConnect(edict_t *ent, char *userinfo);
void ClientUserinfoChanged(edict_t *ent, char *userinfo);
void ClientDisconnect(edict_t *ent);
void ClientBegin(edict_t *ent);
void ClientCommand(edict_t *ent);
void RunEntity(edict_t *ent);
void WriteGame(char *filename, qboolean autosave);
void ReadGame(char *filename);
void WriteLevel(char *filename);
void ReadLevel(char *filename);
void InitGame(void);
void G_RunFrame(void);
/* =================================================================== */
void
ShutdownGame(void)
{
2011-09-28 16:38:01 +00:00
gi.dprintf("==== ShutdownGame ====\n");
2011-09-28 16:38:01 +00:00
gi.FreeTags(TAG_LEVEL);
gi.FreeTags(TAG_GAME);
}
/*
2011-09-28 16:38:01 +00:00
* Returns a pointer to the structure
* with all entry points and global
* variables
*/
game_export_t *
GetGameAPI(game_import_t *import)
{
gi = *import;
globals.apiversion = GAME_API_VERSION;
globals.Init = InitGame;
globals.Shutdown = ShutdownGame;
globals.SpawnEntities = SpawnEntities;
globals.WriteGame = WriteGame;
globals.ReadGame = ReadGame;
globals.WriteLevel = WriteLevel;
globals.ReadLevel = ReadLevel;
globals.ClientThink = ClientThink;
globals.ClientConnect = ClientConnect;
globals.ClientUserinfoChanged = ClientUserinfoChanged;
globals.ClientDisconnect = ClientDisconnect;
globals.ClientBegin = ClientBegin;
globals.ClientCommand = ClientCommand;
globals.RunFrame = G_RunFrame;
globals.ServerCommand = ServerCommand;
globals.edict_size = sizeof(edict_t);
2012-06-26 12:31:01 +00:00
/* Initalize the PRNG */
randk_seed();
return &globals;
}
2012-11-06 19:35:02 +00:00
/*
* this is only here so the functions
* in shared source files can link
*/
2011-09-28 16:38:01 +00:00
void
Sys_Error(char *error, ...)
{
2011-09-28 16:38:01 +00:00
va_list argptr;
char text[1024];
2011-09-28 16:38:01 +00:00
va_start(argptr, error);
vsprintf(text, error, argptr);
va_end(argptr);
2011-09-28 16:38:01 +00:00
gi.error(ERR_FATAL, "%s", text);
}
2011-09-28 16:38:01 +00:00
void
Com_Printf(char *msg, ...)
{
2011-09-28 16:38:01 +00:00
va_list argptr;
char text[1024];
2011-09-28 16:38:01 +00:00
va_start(argptr, msg);
vsprintf(text, msg, argptr);
va_end(argptr);
2011-09-28 16:38:01 +00:00
gi.dprintf("%s", text);
}
2011-09-28 16:38:01 +00:00
/* ====================================================================== */
2011-09-28 16:38:01 +00:00
void
ClientEndServerFrames(void)
{
2011-09-28 16:38:01 +00:00
int i;
edict_t *ent;
2011-09-28 16:38:01 +00:00
/* calc the player views now that all
pushing and damage has been added */
for (i = 0; i < maxclients->value; i++)
{
ent = g_edicts + 1 + i;
2011-09-28 16:38:01 +00:00
if (!ent->inuse || !ent->client)
2011-09-28 16:38:01 +00:00
{
continue;
2011-09-28 16:38:01 +00:00
}
2011-09-28 16:38:01 +00:00
ClientEndServerFrame(ent);
}
}
/*
2011-09-28 16:38:01 +00:00
* Returns the created target changelevel
*/
edict_t *
CreateTargetChangeLevel(char *map)
{
edict_t *ent;
2012-04-29 13:57:33 +00:00
2011-09-28 16:38:01 +00:00
if (!map)
{
return NULL;
2012-04-29 13:57:33 +00:00
}
2011-09-28 16:38:01 +00:00
ent = G_Spawn();
ent->classname = "target_changelevel";
Com_sprintf(level.nextmap, sizeof(level.nextmap), "%s", map);
ent->map = level.nextmap;
return ent;
}
/*
2011-09-28 16:38:01 +00:00
* The timelimit or fraglimit has been exceeded
*/
void
EndDMLevel(void)
{
2011-09-28 16:38:01 +00:00
edict_t *ent;
char *s, *t, *f;
static const char *seps = " ,\n\r";
2011-09-28 16:38:01 +00:00
/* stay on same level flag */
if ((int)dmflags->value & DF_SAME_LEVEL)
{
2011-09-28 16:38:01 +00:00
BeginIntermission(CreateTargetChangeLevel(level.mapname));
return;
}
2011-09-28 16:38:01 +00:00
/* see if it's in the map list */
if (*sv_maplist->string)
{
s = strdup(sv_maplist->string);
f = NULL;
t = strtok(s, seps);
2011-09-28 16:38:01 +00:00
while (t != NULL)
{
if (Q_stricmp(t, level.mapname) == 0)
{
/* it's in the list, go to the next one */
t = strtok(NULL, seps);
2011-09-28 16:38:01 +00:00
if (t == NULL) /* end of list, go to first one */
{
if (f == NULL) /* there isn't a first one, same level */
{
BeginIntermission(CreateTargetChangeLevel(level.mapname));
}
else
2011-09-28 16:38:01 +00:00
{
BeginIntermission(CreateTargetChangeLevel(f));
}
}
else
{
BeginIntermission(CreateTargetChangeLevel(t));
}
free(s);
return;
}
2011-09-28 16:38:01 +00:00
if (!f)
2011-09-28 16:38:01 +00:00
{
f = t;
2011-09-28 16:38:01 +00:00
}
t = strtok(NULL, seps);
}
2011-09-28 16:38:01 +00:00
free(s);
}
2011-09-28 16:38:01 +00:00
if (level.nextmap[0]) /* go to a specific map */
{
BeginIntermission(CreateTargetChangeLevel(level.nextmap));
}
else /* search for a changelevel */
{
ent = G_Find(NULL, FOFS(classname), "target_changelevel");
if (!ent)
2012-04-29 13:57:33 +00:00
{ /* the map designer didn't include a changelevel,
2011-09-28 16:38:01 +00:00
so create a fake ent that goes back to the same level */
BeginIntermission(CreateTargetChangeLevel(level.mapname));
return;
}
2011-09-28 16:38:01 +00:00
BeginIntermission(ent);
}
}
2011-09-28 16:38:01 +00:00
void
CheckNeedPass(void)
{
int need;
2011-09-28 16:38:01 +00:00
/* if password or spectator_password has
changed, update needpass as needed */
if (password->modified || spectator_password->modified)
{
password->modified = spectator_password->modified = false;
need = 0;
if (*password->string && Q_stricmp(password->string, "none"))
2011-09-28 16:38:01 +00:00
{
need |= 1;
2011-09-28 16:38:01 +00:00
}
if (*spectator_password->string &&
Q_stricmp(spectator_password->string, "none"))
{
need |= 2;
2011-09-28 16:38:01 +00:00
}
gi.cvar_set("needpass", va("%d", need));
}
}
2011-09-28 16:38:01 +00:00
void
CheckDMRules(void)
{
2011-09-28 16:38:01 +00:00
int i;
gclient_t *cl;
if (level.intermissiontime)
2011-09-28 16:38:01 +00:00
{
return;
2011-09-28 16:38:01 +00:00
}
if (!deathmatch->value)
2011-09-28 16:38:01 +00:00
{
return;
2011-09-28 16:38:01 +00:00
}
if (timelimit->value)
{
2011-09-28 16:38:01 +00:00
if (level.time >= timelimit->value * 60)
{
2011-09-28 16:38:01 +00:00
gi.bprintf(PRINT_HIGH, "Timelimit hit.\n");
EndDMLevel();
return;
}
}
if (fraglimit->value)
{
2011-09-28 16:38:01 +00:00
for (i = 0; i < maxclients->value; i++)
{
cl = game.clients + i;
2011-09-28 16:38:01 +00:00
if (!g_edicts[i + 1].inuse)
{
continue;
2011-09-28 16:38:01 +00:00
}
if (cl->resp.score >= fraglimit->value)
{
2011-09-28 16:38:01 +00:00
gi.bprintf(PRINT_HIGH, "Fraglimit hit.\n");
EndDMLevel();
return;
}
}
}
}
2011-09-28 16:38:01 +00:00
void
ExitLevel(void)
{
2011-09-28 16:38:01 +00:00
int i;
edict_t *ent;
char command[256];
2011-09-28 16:38:01 +00:00
Com_sprintf(command, sizeof(command), "gamemap \"%s\"\n", level.changemap);
gi.AddCommandString(command);
level.changemap = NULL;
level.exitintermission = 0;
level.intermissiontime = 0;
2011-09-28 16:38:01 +00:00
ClientEndServerFrames();
2011-09-28 16:38:01 +00:00
/* clear some things before going to next level */
for (i = 0; i < maxclients->value; i++)
{
ent = g_edicts + 1 + i;
2011-09-28 16:38:01 +00:00
if (!ent->inuse)
2011-09-28 16:38:01 +00:00
{
continue;
2011-09-28 16:38:01 +00:00
}
if (ent->health > ent->client->pers.max_health)
2011-09-28 16:38:01 +00:00
{
ent->health = ent->client->pers.max_health;
2011-09-28 16:38:01 +00:00
}
}
}
/*
2011-09-28 16:38:01 +00:00
* Advances the world by 0.1 seconds
*/
void
G_RunFrame(void)
{
2011-09-28 16:38:01 +00:00
int i;
edict_t *ent;
level.framenum++;
2011-09-28 16:38:01 +00:00
level.time = level.framenum * FRAMETIME;
2011-09-28 16:38:01 +00:00
/* choose a client for monsters to target this frame */
AI_SetSightClient();
2011-09-28 16:38:01 +00:00
/* exit intermissions */
if (level.exitintermission)
{
2011-09-28 16:38:01 +00:00
ExitLevel();
return;
}
2012-04-29 13:57:33 +00:00
/* treat each object in turn
2011-09-28 16:38:01 +00:00
even the world gets a chance
to think */
ent = &g_edicts[0];
2011-09-28 16:38:01 +00:00
for (i = 0; i < globals.num_edicts; i++, ent++)
{
if (!ent->inuse)
2011-09-28 16:38:01 +00:00
{
continue;
2011-09-28 16:38:01 +00:00
}
level.current_entity = ent;
2011-09-28 16:38:01 +00:00
VectorCopy(ent->s.origin, ent->s.old_origin);
2011-09-28 16:38:01 +00:00
/* if the ground entity moved, make sure we are still on it */
if ((ent->groundentity) &&
(ent->groundentity->linkcount != ent->groundentity_linkcount))
{
ent->groundentity = NULL;
2011-09-28 16:38:01 +00:00
if (!(ent->flags & (FL_SWIM | FL_FLY)) &&
(ent->svflags & SVF_MONSTER))
{
2011-09-28 16:38:01 +00:00
M_CheckGround(ent);
}
}
2011-09-28 16:38:01 +00:00
if ((i > 0) && (i <= maxclients->value))
{
2011-09-28 16:38:01 +00:00
ClientBeginServerFrame(ent);
continue;
}
2011-09-28 16:38:01 +00:00
G_RunEntity(ent);
}
2011-09-28 16:38:01 +00:00
/* see if it is time to end a deathmatch */
CheckDMRules();
2011-09-28 16:38:01 +00:00
/* see if needpass needs updated */
CheckNeedPass();
2011-09-28 16:38:01 +00:00
/* build the playerstate_t structures for all players */
ClientEndServerFrames();
}