Cleanup code.

Autoload now loads normal saves, quicksaves, or autosaves (whatever was most recent).
This commit is contained in:
Eric Wasylishen 2010-12-16 23:47:15 -07:00
parent 516ba61710
commit 4f70e43b95
2 changed files with 71 additions and 69 deletions

View file

@ -644,75 +644,38 @@ void Host_ServerFrame (void)
// send all messages to the clients
SV_SendClientMessages ();
// Autosave stuff
static double oldhealth = 0;
static double lastautosave = 0;
static int autosaveindex = 0;
static char *autosavemap[2];
static double autosavetime[2];
static const float AUTOSAVE_DURATION = 30;
static const float AUTOSAVE_SAFETY_MARGIN = 10;
static const float AUTOSAVE_SAFE_HEALTH = 65;
if (!deathmatch.value && !coop.value && !sv.paused &&
sv.active && !cl.intermission && svs.maxclients == 1)
// autosave
qboolean autosaveAllowed = (!deathmatch.value &&
!coop.value &&
!sv.paused &&
sv.active &&
!cl.intermission &&
svs.maxclients == 1 &&
sv_player->v.deadflag == DEAD_NO);
if (autosaveAllowed)
{
// Reset the last autosave time if it is out of bounds (i.e. we changed
// maps)
static double oldhealth = 0;
static double lastautosave = 0;
// Reset the last autosave time if it is out of bounds (i.e. we changed maps)
if (lastautosave > sv.time)
{
lastautosave = 0;
}
if (oldhealth > 0 && sv_player->v.health <= 0 &&
(sv.time - lastautosave) <= AUTOSAVE_SAFETY_MARGIN)
{
// Player just died within a few seconds of an autosave, so
// replace that autosave with the backup (if one exists)
fprintf(stderr, "Invalidate last autosave");
}
if ((sv.time - lastautosave) > AUTOSAVE_DURATION)
{
// Eligible for autosave
if (sv_player->v.health >= AUTOSAVE_SAFE_HEALTH)
{
// If we have plenty of health, save right now
fprintf(stderr, "Healthy autosave");
Cmd_ExecuteString ("save quick", src_command);
lastautosave = sv.time;
}
else if (sv_player->v.health > oldhealth)
{
fprintf(stderr, "Unhealthy, just got health autosave");
// If we have less than AUTOSAVE_SAFE_HEALTH, only save if we just
// picked up a healthpack
Cmd_ExecuteString ("save quick", src_command);
lastautosave = sv.time;
// FIXME: picking up 15 health when we have 1 might not warrant
// an autosave
}
}
}
oldhealth = sv_player->v.health;
/*
if (ericw % 100 == 0)
{
fprintf(stderr, "1000 frames. server time: %lf\n", sv.time);
if (ericw % 300 == 0)
if ((sv.time - lastautosave) > 30
&& (sv_player->v.health >= 25 || sv_player->v.health > oldhealth))
{
if (!sv.paused)
{
fprintf(stderr, "autosaving...\n");
Cmd_ExecuteString ("save quick", src_command);
}
char command[MAX_QPATH + 10];
sprintf(command, "save auto_%s", sv.name);
Cmd_ExecuteString (command, src_command);
lastautosave = sv.time;
}
}*/
oldhealth = sv_player->v.health;
}
}
/*

View file

@ -33,6 +33,9 @@ extern int com_nummissionpacks; //johnfitz
int current_skill;
char lastsavename[MAX_QPATH]; // autosave
char lastsavemapname[MAX_QPATH]; // autosave
void Mod_Print (void);
/*
@ -889,6 +892,42 @@ void Host_Changelevel_f (void)
SV_SpawnServer (level);
}
static qboolean _autoloadIfPossible()
{
if (!deathmatch.value &&
!coop.value &&
sv.active &&
!cl.intermission &&
svs.maxclients == 1 &&
sv_player->v.deadflag != DEAD_NO)
{
if (0 == strcmp(sv.name, lastsavemapname))
{
char command[MAX_QPATH + 5];
sprintf(command, "load %s", lastsavename);
Cmd_ExecuteString (command, src_command);
return true;
}
}
return false;
}
/*
==================
Host_Autoload_f
Automatically loads either the
==================
*/
void Host_Autoload_f (void)
{
if (!_autoloadIfPossible())
{
Host_Restart_f();
}
}
/*
==================
Host_Restart_f
@ -908,18 +947,14 @@ void Host_Restart_f (void)
strcpy (mapname, sv.name); // must copy out, because it gets cleared
// in sv_spawnserver
fprintf(stderr, "Host_Restart_f '%s' health %lf\n", mapname, sv_player->v.health);
// Autosave stuff
if (!deathmatch.value && !coop.value && !sv.paused &&
sv.active && !cl.intermission && svs.maxclients == 1)
if (_autoloadIfPossible()) // autosave
{
//if (0 == strcmp(mapname, autosavemap[autosaveindex]))
Cmd_ExecuteString ("load quick", src_command);
return;
}
lastsavemapname[0] = '\0'; // autosave - we are explicitly restarting the level, so don't autoload
lastsavename[0] = '\0';
SV_SpawnServer (mapname);
}
@ -1051,6 +1086,9 @@ void Host_Savegame_f (void)
}
}
strcpy(lastsavename, Cmd_Argv(1)); // autosave
strcpy(lastsavemapname, sv.name); // autosave
sprintf (name, "%s/%s", com_gamedir, Cmd_Argv(1));
COM_DefaultExtension (name, ".sav");
@ -2304,6 +2342,7 @@ void Host_InitCommands (void)
Cmd_AddCommand ("load", Host_Loadgame_f);
Cmd_AddCommand ("save", Host_Savegame_f);
Cmd_AddCommand ("give", Host_Give_f);
Cmd_AddCommand ("autoload", Host_Autoload_f); //autosave
Cmd_AddCommand ("startdemos", Host_Startdemos_f);
Cmd_AddCommand ("demos", Host_Demos_f);