Debian Codegraph @

engine/game/savegame.cpp

raw
1 // ____ _ __
2 // / __ )____ _____ | | / /___ ___________
3 // / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/
4 // / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ )
5 // /_____/\____/____/ |__/|__/\__,_/_/ /____/
6 //
7 // A futuristic real-time strategy game.
8 // This file is part of Bos Wars.
9 //
10 /**@name savegame.cpp - Save game. */
11 //
12 // (c) Copyright 2001-2007 by Lutz Sammer, Andreas Arens
13 //
14 // This program is free software; you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation; only version 2 of the License.
17 //
18 // This program is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 // 02111-1307, USA.
27 //
28
29 //@{
30
31 /*----------------------------------------------------------------------------
32 -- Includes
33 ----------------------------------------------------------------------------*/
34
35 #include <string.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <time.h>
39 #include <ctype.h>
40 #include <string>
41
42 #include "stratagus.h"
43 #include "ui.h"
44 #include "unit.h"
45 #include "upgrade.h"
46 #include "missile.h"
47 #include "map.h"
48 #include "player.h"
49 #include "ai.h"
50 #include "trigger.h"
51 #include "iolib.h"
52 #include "replay.h"
53 #include "script.h"
54 #include "actions.h"
55 #include "version.h"
56
57 /*----------------------------------------------------------------------------
58 -- Variables
59 ----------------------------------------------------------------------------*/
60
61 /*----------------------------------------------------------------------------
62 -- Functions
63 ----------------------------------------------------------------------------*/
64
65 /**
66 ** Get the save directory
67 */
68 static std::string GetSaveDir()
69 {
70 std::string dir;
71
72 dir = UserDirectory + "save/";
73
74 return dir;
75 }
76
77 /**
78 ** Save a game to file.
79 **
80 ** @param filename File name to be stored.
81 **
82 ** @note Later we want to store in a more compact binary format.
83 */
84 void SaveGame(const std::string &filename)
85 {
86 time_t now;
87 CFile file;
88 char *s;
89 char *s1;
90 std::string fullpath;
91
92 fullpath = GetSaveDir() + filename;
93 if (file.open(fullpath.c_str(), CL_WRITE_GZ | CL_OPEN_WRITE) == -1) {
94 fprintf(stderr, "Can't save to `%s'\n", filename.c_str());
95 return;
96 }
97
98 time(&now);
99 s = ctime(&now);
100 if ((s1 = strchr(s, '\n'))) {
101 *s1 = '\0';
102 }
103
104 // Parseable header
105 file.printf("SavedGameInfo({\n");
106 file.printf("--- generator = \"Generated by Bos Wars Version " VERSION "\",\n");
107 file.printf("--- comment = \"Visit http://www.boswars.org for more informations\",\n");
108 file.printf("--- type = \"%s\",\n", "single-player");
109 file.printf("--- date = \"%s\",\n", s);
110 file.printf("--- map = \"%s\",\n", Map.Info.Description.c_str());
111 file.printf("--- media-version = \"%s\",\n", "Undefined");
112 file.printf("--- engine = {%d, %d, %d},\n",
113 StratagusMajorVersion, StratagusMinorVersion, StratagusPatchLevel);
114 file.printf(" SyncHash = %d, \n", SyncHash);
115 file.printf(" SyncRandSeed = %d, \n", SyncRandSeed);
116 file.printf(" SaveFile = \"%s\"\n", CurrentMapPath);
117 file.printf("} )\n\n");
118
119 // FIXME: probably not the right place for this
120 file.printf("GameCycle = %lu\n", GameCycle);
121
122 SaveCcl(&file);
123 Map.Save(&file);
124 SavePlayers(&file);
125 SaveUpgrades(&file);
126 SaveUnits(&file);
127 SaveUserInterface(&file);
128 SaveAi(&file);
129 SaveSelections(&file);
130 SaveGroups(&file);
131 SaveMissiles(&file);
132 SaveTriggers(&file);
133 SaveReplayList(&file);
134 // FIXME: find all state information which must be saved.
135 s = SaveGlobal(Lua, true);
136 if (s != NULL) {
137 file.printf("-- Lua state\n\n%s\n", s);
138 delete[] s;
139 }
140 file.close();
141 }
142
143 //@}