2011-08-24 14:14:44 +02:00
|
|
|
/*
|
|
|
|
Copyright (C) 2003 Parallel Realities
|
2015-03-01 21:37:32 +01:00
|
|
|
Copyright (C) 2011, 2012, 2013 Guus Sliepen
|
2016-01-08 03:32:40 +01:00
|
|
|
Copyright (C) 2015, 2016 onpon4 <onpon4@riseup.net>
|
2011-08-24 14:14:44 +02:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
2015-02-26 17:20:36 +01:00
|
|
|
as published by the Free Software Foundation; either version 3
|
2011-08-24 14:14:44 +02:00
|
|
|
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
|
2015-02-26 17:20:36 +01:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2011-08-24 14:14:44 +02:00
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2015-02-26 17:20:36 +01:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2011-08-24 14:14:44 +02:00
|
|
|
*/
|
|
|
|
|
2011-08-26 21:29:04 +02:00
|
|
|
#include "Starfighter.h"
|
|
|
|
|
|
|
|
static char saveSlot[10][25];
|
2011-08-24 14:14:44 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
Reads in each save game that it finds and gives it an appropriate
|
|
|
|
description using the area variable contained in the game binary
|
|
|
|
data. It returns the slot number (1 - 10) of the most recently
|
|
|
|
used file. On the title screen, this is used to determine whether
|
|
|
|
a player can "Continue Current Game" and "Load Saved Game".
|
|
|
|
*/
|
|
|
|
int initSaveSlots()
|
|
|
|
{
|
|
|
|
char fileName[PATH_MAX];
|
2016-01-04 00:19:20 +01:00
|
|
|
int imagePos = screen->h / 3 + 50;
|
2011-08-24 14:14:44 +02:00
|
|
|
Game tempGame;
|
|
|
|
struct stat fileInfo;
|
|
|
|
int modTime = 0;
|
|
|
|
int continueSaveIndex = 0;
|
|
|
|
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
//READ SAVE GAME DATA
|
2015-03-03 05:32:48 +01:00
|
|
|
for (int i = 0 ; i <= 5 ; i++)
|
2011-08-24 14:14:44 +02:00
|
|
|
{
|
2016-01-02 22:59:48 +01:00
|
|
|
sprintf(fileName, "%ssave%.2d.dat", engine.configDirectory, i);
|
2015-03-03 05:32:48 +01:00
|
|
|
|
2011-08-24 14:14:44 +02:00
|
|
|
fp = fopen(fileName, "rb");
|
|
|
|
if (fp == NULL)
|
|
|
|
{
|
2015-03-03 05:32:48 +01:00
|
|
|
sprintf(saveSlot[i], (i == 0 ? "AUTOSAVE (Empty)" : "Empty"));
|
2011-08-24 14:14:44 +02:00
|
|
|
if (engine.gameSection == SECTION_TITLE)
|
2016-01-04 00:19:20 +01:00
|
|
|
gfx_createTextObject(TS_SAVESLOT_0 + i, saveSlot[i],
|
|
|
|
-1, imagePos, FONT_WHITE);
|
2011-08-24 14:14:44 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-03-03 05:32:48 +01:00
|
|
|
if (i == 0)
|
2011-08-24 14:14:44 +02:00
|
|
|
{
|
2015-03-03 05:32:48 +01:00
|
|
|
sprintf(saveSlot[i], "AUTOSAVE");
|
2011-08-24 14:14:44 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-03-03 05:32:48 +01:00
|
|
|
if (fread(&tempGame, sizeof(Game), 1, fp) != 1)
|
|
|
|
{
|
|
|
|
sprintf(saveSlot[i], "Corrupt Game Data");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sprintf(saveSlot[i], "%s, %s", systemNames[tempGame.system],
|
|
|
|
tempGame.stationedName);
|
|
|
|
}
|
2011-08-24 14:14:44 +02:00
|
|
|
}
|
|
|
|
|
2015-03-03 05:32:48 +01:00
|
|
|
if (engine.gameSection == SECTION_TITLE)
|
2015-12-31 16:47:27 +01:00
|
|
|
gfx_createTextObject(TS_SAVESLOT_0 + i, saveSlot[i], -1,
|
2015-03-03 05:32:48 +01:00
|
|
|
imagePos, FONT_WHITE);
|
|
|
|
|
2011-08-24 14:14:44 +02:00
|
|
|
if (stat(fileName, &fileInfo) != -1)
|
|
|
|
{
|
|
|
|
if (fileInfo.st_mtime > modTime)
|
2015-03-03 05:32:48 +01:00
|
|
|
{modTime = fileInfo.st_mtime; continueSaveIndex = i;}
|
2011-08-24 14:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
imagePos += 20;
|
|
|
|
}
|
|
|
|
|
|
|
|
return continueSaveIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Fill in later...
|
|
|
|
*/
|
2011-08-26 16:55:46 +02:00
|
|
|
bool loadGame(int slot)
|
2011-08-24 14:14:44 +02:00
|
|
|
{
|
|
|
|
char filename[PATH_MAX];
|
|
|
|
FILE *fp;
|
2016-01-02 22:59:48 +01:00
|
|
|
sprintf(filename, "%ssave%.2d.dat", engine.configDirectory, slot);
|
2011-08-24 14:14:44 +02:00
|
|
|
|
|
|
|
fp = fopen(filename, "rb");
|
|
|
|
|
|
|
|
if (fp == NULL)
|
2011-08-26 16:55:46 +02:00
|
|
|
return false;
|
2011-08-24 14:14:44 +02:00
|
|
|
|
2015-05-21 01:41:43 +02:00
|
|
|
if (fread(&game, sizeof(Game), 1, fp) != 1)
|
2011-08-24 14:14:44 +02:00
|
|
|
{
|
|
|
|
printf("Save game error. The file was not of the expected format.\n");
|
|
|
|
fclose(fp);
|
2011-08-26 16:55:46 +02:00
|
|
|
return false;
|
2011-08-24 14:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
2015-05-21 01:41:43 +02:00
|
|
|
if (game.saveFormat < 2)
|
|
|
|
game.difficulty = DIFFICULTY_NORMAL;
|
2012-12-09 16:11:55 +01:00
|
|
|
|
2015-05-21 01:41:43 +02:00
|
|
|
weapon[W_PLAYER_WEAPON] = game.playerWeapon;
|
Fixed more magic numbers.
God, this one was definitely the biggest headache of all of the
magic number erasing. Never did I expect such cryptic problems.
The first problem was that the entirety of the player's weapon
struct was a part of the save file, *including the weapon's "image
indexes"*. Since the indexes have been changed, and the originally
used one is now unavailable when it's requested, this was causing
a segfault later on. Had to fix this by setting the image index
when the game is loaded.
The second problem was related to another bug I've been confused
about for years: the one that causes mobile rays to fire 5 green
shots. The entire reason those shots were green was because
the weapon's image indexes were undefined, and *that was causing
them to default to 0*. 0 was simply the index of green plasma.
Of course, though, now attempting to use that image causes a
segfault, so for now, I've fixed this by changing the image index
of the mobile rays to the red plasma bolts.
There are still some magic numbers left, related to the intermission
screen. But the hardest part is now done, thank God.
2016-01-06 04:12:29 +01:00
|
|
|
weapon[W_PLAYER_WEAPON].imageIndex[0] = SP_PLASMA_GREEN;
|
|
|
|
weapon[W_PLAYER_WEAPON].imageIndex[1] = SP_PLASMA_GREEN;
|
2015-05-21 01:41:43 +02:00
|
|
|
player = game.thePlayer;
|
2011-08-24 14:14:44 +02:00
|
|
|
|
|
|
|
// Re-init all the planets in this system...
|
2015-05-21 01:41:43 +02:00
|
|
|
initPlanetMissions(game.system);
|
2011-08-24 14:14:44 +02:00
|
|
|
|
|
|
|
// ... and then override with completition status
|
|
|
|
for (int i = 0 ; i < 10 ; i++)
|
2015-05-21 01:41:43 +02:00
|
|
|
systemPlanet[i].missionCompleted = game.missionCompleted[i];
|
2011-08-24 14:14:44 +02:00
|
|
|
|
2011-08-26 16:55:46 +02:00
|
|
|
return true;
|
2011-08-24 14:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void saveGame(int slot)
|
|
|
|
{
|
2015-03-31 21:23:17 +02:00
|
|
|
FILE *fp;
|
|
|
|
char fileName[PATH_MAX];
|
|
|
|
|
2015-03-03 05:32:48 +01:00
|
|
|
if ((slot < 0) || (slot > 5))
|
2011-08-24 14:14:44 +02:00
|
|
|
{
|
2015-03-03 05:32:48 +01:00
|
|
|
printf("Error - Saves may only be 0 to 5\n");
|
2011-08-24 14:14:44 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-02 22:59:48 +01:00
|
|
|
sprintf(fileName, "%ssave%.2d.dat", engine.configDirectory, slot);
|
2011-08-24 14:14:44 +02:00
|
|
|
fp = fopen(fileName, "wb");
|
|
|
|
|
2015-05-21 01:41:43 +02:00
|
|
|
game.saveFormat = 3;
|
|
|
|
game.playerWeapon = weapon[W_PLAYER_WEAPON];
|
|
|
|
game.thePlayer = player;
|
2011-08-24 14:14:44 +02:00
|
|
|
for (int i = 0 ; i < 10 ; i++)
|
2015-05-21 01:41:43 +02:00
|
|
|
game.missionCompleted[i] = systemPlanet[i].missionCompleted;
|
2011-08-24 14:14:44 +02:00
|
|
|
|
|
|
|
if (fp != NULL)
|
|
|
|
{
|
2015-05-21 01:41:43 +02:00
|
|
|
if (fwrite(&game, sizeof(Game), 1, fp) != 1)
|
2011-08-24 14:14:44 +02:00
|
|
|
{
|
|
|
|
printf("Error Saving Game to Slot %d\n", slot);
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("Error Saving Game to Slot %d\n", slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recall to update the save slots... (lazy, yes)
|
|
|
|
initSaveSlots();
|
2013-09-30 16:52:43 +02:00
|
|
|
engine.keyState[KEY_FIRE] = 0;
|
2011-08-24 14:14:44 +02:00
|
|
|
}
|
|
|
|
|
2016-01-07 02:35:37 +01:00
|
|
|
void createSavesSurface(SDL_Surface *savesSurface, int clickedSlot)
|
2011-08-24 14:14:44 +02:00
|
|
|
{
|
2015-03-31 21:23:17 +02:00
|
|
|
int y = 10;
|
2011-08-24 14:14:44 +02:00
|
|
|
|
2015-12-31 16:47:27 +01:00
|
|
|
gfx_drawRect(savesSurface, 0, 0, 348, 298, 0x00, 0x00, 0x00);
|
2011-08-24 14:14:44 +02:00
|
|
|
|
2015-03-03 05:32:48 +01:00
|
|
|
for (int i = 1 ; i <= 5 ; i++)
|
2011-08-24 14:14:44 +02:00
|
|
|
{
|
|
|
|
if (clickedSlot == i)
|
2015-12-31 16:47:27 +01:00
|
|
|
gfx_drawRect(savesSurface, 5, y, 338, 25, 0x99, 0x00, 0x00);
|
2011-08-24 14:14:44 +02:00
|
|
|
else
|
2015-12-31 16:47:27 +01:00
|
|
|
gfx_drawRect(savesSurface, 5, y, 338, 25, 0x00, 0x00, 0x99);
|
2015-11-06 16:12:57 +01:00
|
|
|
gfx_renderString(saveSlot[i], 70, y + 5, FONT_WHITE, 0, savesSurface);
|
2011-08-24 14:14:44 +02:00
|
|
|
y += 30;
|
|
|
|
}
|
|
|
|
|
2015-11-06 16:12:57 +01:00
|
|
|
gfx_renderString("*** HELP ***", 120, 170, FONT_WHITE, 0, savesSurface);
|
2011-08-24 14:14:44 +02:00
|
|
|
|
2015-03-03 05:32:48 +01:00
|
|
|
switch (clickedSlot)
|
2011-08-24 14:14:44 +02:00
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
case 3:
|
|
|
|
case 4:
|
2015-03-03 05:32:48 +01:00
|
|
|
case 5:
|
2015-12-31 16:47:27 +01:00
|
|
|
gfx_drawRect(savesSurface, 5, 265, 100, 25, 0x00, 0x99, 0x00);
|
|
|
|
gfx_drawRect(savesSurface, 125, 265, 100, 25, 0x99, 0x99, 0x00);
|
|
|
|
gfx_drawRect(savesSurface, 243, 265, 100, 25, 0x99, 0x00, 0x00);
|
2015-11-06 16:12:57 +01:00
|
|
|
gfx_renderString("SAVE", 40, 270, FONT_WHITE, 0, savesSurface);
|
|
|
|
gfx_renderString("CANCEL", 150, 270, FONT_WHITE, 0, savesSurface);
|
|
|
|
gfx_renderString("DELETE", 270, 270, FONT_WHITE, 0, savesSurface);
|
2011-08-26 23:53:46 +02:00
|
|
|
|
2015-11-06 16:12:57 +01:00
|
|
|
gfx_renderString("SAVE will save the game", 17, 200, FONT_WHITE, 0,
|
2015-03-03 05:32:48 +01:00
|
|
|
savesSurface);
|
2015-11-06 16:12:57 +01:00
|
|
|
gfx_renderString("CANCEL will unselect that slot", 17, 220,
|
|
|
|
FONT_WHITE, 0, savesSurface);
|
|
|
|
gfx_renderString("DELETE will remove the save", 17, 240,
|
|
|
|
FONT_WHITE, 0, savesSurface);
|
2011-08-24 14:14:44 +02:00
|
|
|
break;
|
|
|
|
case -1:
|
2015-11-06 16:12:57 +01:00
|
|
|
gfx_renderString("First click a Save game slot to use", 17, 200,
|
|
|
|
FONT_WHITE, 0, savesSurface);
|
2011-08-24 14:14:44 +02:00
|
|
|
break;
|
|
|
|
case -10:
|
2015-11-06 16:12:57 +01:00
|
|
|
gfx_renderString("Game Saved", 130, 200, FONT_WHITE, 0,
|
|
|
|
savesSurface);
|
2011-08-24 14:14:44 +02:00
|
|
|
break;
|
|
|
|
case -11:
|
2015-11-06 16:12:57 +01:00
|
|
|
gfx_renderString("Save Deleted", 130, 200, FONT_WHITE, 0,
|
|
|
|
savesSurface);
|
2011-08-24 14:14:44 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-09-30 16:52:43 +02:00
|
|
|
engine.keyState[KEY_FIRE] = 0;
|
2011-08-24 14:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Displays the save slot available. For use with an interface that
|
|
|
|
has the cursor enabled. It returns the index number of the slot clicked
|
|
|
|
so that the function invoking it can perform a load or save on that slot.
|
|
|
|
*/
|
2016-01-07 02:35:37 +01:00
|
|
|
int showSaveSlots(SDL_Surface *savesSurface, int saveSlot)
|
2011-08-24 14:14:44 +02:00
|
|
|
{
|
2015-03-31 21:23:17 +02:00
|
|
|
int clickedSlot = -1;
|
|
|
|
|
2011-08-24 14:14:44 +02:00
|
|
|
SDL_Rect r;
|
|
|
|
r.x = 201;
|
|
|
|
r.y = 115;
|
|
|
|
r.w = 348;
|
|
|
|
r.h = 25;
|
|
|
|
|
2013-09-30 16:52:43 +02:00
|
|
|
if ((engine.keyState[KEY_FIRE]))
|
2011-08-24 14:14:44 +02:00
|
|
|
{
|
2015-03-03 05:32:48 +01:00
|
|
|
for (int i = 1 ; i <= 5 ; i++)
|
2011-08-24 14:14:44 +02:00
|
|
|
{
|
2015-09-25 22:28:09 +02:00
|
|
|
if (game_collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6,
|
2015-03-03 05:32:48 +01:00
|
|
|
r.x, r.y, r.w, r.h))
|
2011-08-24 14:14:44 +02:00
|
|
|
{
|
|
|
|
clickedSlot = i;
|
|
|
|
createSavesSurface(savesSurface, i);
|
|
|
|
}
|
|
|
|
r.y += 30;
|
|
|
|
}
|
|
|
|
|
2015-09-25 22:28:09 +02:00
|
|
|
if (game_collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 215,
|
2015-03-03 05:32:48 +01:00
|
|
|
365, 100, 25))
|
2011-08-24 14:14:44 +02:00
|
|
|
{
|
2015-03-03 05:32:48 +01:00
|
|
|
saveGame(saveSlot);
|
2011-08-24 14:14:44 +02:00
|
|
|
createSavesSurface(savesSurface, -10);
|
|
|
|
}
|
|
|
|
|
2015-09-25 22:28:09 +02:00
|
|
|
if (game_collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 335,
|
2015-03-03 05:32:48 +01:00
|
|
|
365, 100, 25))
|
2011-08-24 14:14:44 +02:00
|
|
|
createSavesSurface(savesSurface, -1);
|
|
|
|
|
2015-09-25 22:28:09 +02:00
|
|
|
if (game_collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 453,
|
2015-03-03 05:32:48 +01:00
|
|
|
365, 100, 25))
|
2011-08-24 14:14:44 +02:00
|
|
|
{
|
|
|
|
char filename[PATH_MAX];
|
2016-01-02 22:59:48 +01:00
|
|
|
sprintf(filename, "%ssave%.2d.dat", engine.configDirectory,
|
2015-03-03 05:32:48 +01:00
|
|
|
saveSlot);
|
2011-08-24 14:14:44 +02:00
|
|
|
remove(filename);
|
|
|
|
initSaveSlots();
|
|
|
|
createSavesSurface(savesSurface, -11);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clickedSlot > -1)
|
|
|
|
saveSlot = clickedSlot;
|
|
|
|
|
|
|
|
return saveSlot;
|
|
|
|
}
|