1 |
elmex |
1.1 |
/* |
2 |
|
|
CrossFire, A Multiplayer game for X-windows |
3 |
|
|
|
4 |
|
|
Copyright (C) 2002 Mark Wedel & Crossfire Development Team |
5 |
|
|
Copyright (C) 1992 Frank Tore Johansen |
6 |
|
|
|
7 |
|
|
This program is free software; you can redistribute it and/or modify |
8 |
|
|
it under the terms of the GNU General Public License as published by |
9 |
|
|
the Free Software Foundation; either version 2 of the License, or |
10 |
|
|
(at your option) any later version. |
11 |
|
|
|
12 |
|
|
This program is distributed in the hope that it will be useful, |
13 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
|
|
GNU General Public License for more details. |
16 |
|
|
|
17 |
|
|
You should have received a copy of the GNU General Public License |
18 |
|
|
along with this program; if not, write to the Free Software |
19 |
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
20 |
|
|
|
21 |
root |
1.18 |
The authors can be reached via e-mail at <crossfire@schmorp.de> |
22 |
elmex |
1.1 |
*/ |
23 |
|
|
|
24 |
|
|
#include <global.h> |
25 |
root |
1.30 |
#include <sproto.h> |
26 |
elmex |
1.1 |
#include <spells.h> |
27 |
|
|
#include <loader.h> |
28 |
|
|
#include <define.h> |
29 |
|
|
|
30 |
|
|
extern void sub_weight (object *, signed long); |
31 |
|
|
extern void add_weight (object *, signed long); |
32 |
|
|
extern long pticks; |
33 |
|
|
|
34 |
|
|
/* If flag is non zero, it means that we want to try and save everyone, but |
35 |
|
|
* keep the game running. Thus, we don't want to free any information. |
36 |
|
|
*/ |
37 |
root |
1.15 |
void |
38 |
|
|
emergency_save (int flag) |
39 |
|
|
{ |
40 |
elmex |
1.1 |
player *pl; |
41 |
root |
1.15 |
|
42 |
elmex |
1.1 |
trying_emergency_save = 1; |
43 |
root |
1.30 |
|
44 |
root |
1.15 |
LOG (llevError, "Emergency save: "); |
45 |
root |
1.31 |
for (pl = first_player; pl; pl = pl->next) |
46 |
root |
1.15 |
{ |
47 |
|
|
if (!pl->ob) |
48 |
|
|
{ |
49 |
|
|
LOG (llevError, "No name, ignoring this.\n"); |
50 |
|
|
continue; |
51 |
|
|
} |
52 |
root |
1.30 |
|
53 |
|
|
LOG (llevError, "%s ", &pl->ob->name); |
54 |
root |
1.15 |
new_draw_info (NDI_UNIQUE, 0, pl->ob, "Emergency save..."); |
55 |
elmex |
1.1 |
|
56 |
root |
1.30 |
/* If we are not exiting the game (ie, this is sort of a backup save), then |
57 |
|
|
* don't change the location back to the village. Note that there are other |
58 |
|
|
* options to have backup saves be done at the starting village |
59 |
|
|
*/ |
60 |
root |
1.15 |
if (!flag) |
61 |
|
|
{ |
62 |
|
|
strcpy (pl->maplevel, first_map_path); |
63 |
root |
1.30 |
|
64 |
root |
1.31 |
pl->ob->map = 0; |
65 |
root |
1.15 |
pl->ob->x = -1; |
66 |
|
|
pl->ob->y = -1; |
67 |
|
|
} |
68 |
root |
1.30 |
|
69 |
|
|
pl->save (1); |
70 |
elmex |
1.1 |
} |
71 |
root |
1.30 |
|
72 |
root |
1.15 |
LOG (llevError, "\n"); |
73 |
elmex |
1.1 |
} |
74 |
|
|
|
75 |
|
|
/* Delete character with name. if new is set, also delete the new |
76 |
|
|
* style directory, otherwise, just delete the old style playfile |
77 |
|
|
* (needed for transition) |
78 |
|
|
*/ |
79 |
root |
1.15 |
void |
80 |
|
|
delete_character (const char *name, int newchar) |
81 |
|
|
{ |
82 |
|
|
char buf[MAX_BUF]; |
83 |
elmex |
1.1 |
|
84 |
root |
1.15 |
sprintf (buf, "%s/%s/%s.pl", settings.localdir, settings.playerdir, name); |
85 |
root |
1.30 |
|
86 |
root |
1.15 |
if (unlink (buf) == -1) |
87 |
|
|
LOG (llevDebug, "Cannot delete character file %s: %s\n", buf, strerror (errno)); |
88 |
root |
1.30 |
|
89 |
root |
1.15 |
if (newchar) |
90 |
|
|
{ |
91 |
|
|
sprintf (buf, "%s/%s/%s", settings.localdir, settings.playerdir, name); |
92 |
|
|
/* this effectively does an rm -rf on the directory */ |
93 |
|
|
remove_directory (buf); |
94 |
elmex |
1.1 |
} |
95 |
|
|
} |
96 |
|
|
|
97 |
root |
1.15 |
int |
98 |
|
|
create_savedir_if_needed (char *savedir) |
99 |
elmex |
1.1 |
{ |
100 |
|
|
struct stat *buf; |
101 |
|
|
|
102 |
root |
1.15 |
if ((buf = (struct stat *) malloc (sizeof (struct stat))) == NULL) |
103 |
|
|
{ |
104 |
|
|
LOG (llevError, "Unable to save playerfile... out of memory.\n"); |
105 |
|
|
return 0; |
106 |
|
|
} |
107 |
|
|
else |
108 |
|
|
{ |
109 |
|
|
stat (savedir, buf); |
110 |
|
|
if (!S_ISDIR (buf->st_mode)) |
111 |
|
|
if (mkdir (savedir, SAVE_DIR_MODE)) |
112 |
|
|
{ |
113 |
|
|
LOG (llevError, "Unable to create player savedir %s: %s\n", savedir, strerror (errno)); |
114 |
|
|
return 0; |
115 |
|
|
} |
116 |
|
|
free (buf); |
117 |
|
|
} |
118 |
root |
1.30 |
|
119 |
root |
1.15 |
return 1; |
120 |
elmex |
1.1 |
} |
121 |
|
|
|
122 |
|
|
/* |
123 |
root |
1.30 |
* If final is set, it a clean/final save, not a backup, ie dont remove objects from inventory |
124 |
elmex |
1.1 |
*/ |
125 |
root |
1.30 |
void |
126 |
|
|
player::save (bool final) |
127 |
root |
1.15 |
{ |
128 |
root |
1.30 |
object *tmp, *container = 0; |
129 |
root |
1.15 |
|
130 |
|
|
/* Sanity check - some stuff changes this when player is exiting */ |
131 |
root |
1.30 |
if (ob->type != PLAYER || !enable_save || !ns) |
132 |
|
|
return; |
133 |
elmex |
1.1 |
|
134 |
root |
1.30 |
INVOKE_PLAYER (SAVE, ob->contr); |
135 |
root |
1.5 |
|
136 |
root |
1.30 |
object_freezer freezer; |
137 |
elmex |
1.1 |
|
138 |
root |
1.30 |
int wiz = ob->flags [FLAG_WIZ]; |
139 |
root |
1.3 |
|
140 |
root |
1.9 |
/* Eneq(@csd.uu.se): If we have an open container hide it. */ |
141 |
root |
1.30 |
container = ob->container; |
142 |
|
|
ob->container = 0; |
143 |
|
|
|
144 |
|
|
fprintf (freezer, "password %s\n", password); |
145 |
elmex |
1.1 |
|
146 |
root |
1.30 |
if (own_title[0] != '\0') |
147 |
|
|
fprintf (freezer, "title %s\n", own_title); |
148 |
root |
1.3 |
|
149 |
root |
1.30 |
fprintf (freezer, "explore %d\n", explore); |
150 |
|
|
fprintf (freezer, "gen_hp %d\n", gen_hp); |
151 |
|
|
fprintf (freezer, "gen_sp %d\n", gen_sp); |
152 |
|
|
fprintf (freezer, "gen_grace %d\n", gen_grace); |
153 |
|
|
fprintf (freezer, "listening %d\n", listening); |
154 |
|
|
fprintf (freezer, "shoottype %d\n", shoottype); |
155 |
|
|
fprintf (freezer, "bowtype %d\n", bowtype); |
156 |
|
|
fprintf (freezer, "petmode %d\n", petmode); |
157 |
|
|
fprintf (freezer, "peaceful %d\n", peaceful); |
158 |
|
|
fprintf (freezer, "digestion %d\n", digestion); |
159 |
|
|
fprintf (freezer, "pickup %d\n", mode); |
160 |
|
|
fprintf (freezer, "outputs_sync %d\n", outputs_sync); |
161 |
|
|
fprintf (freezer, "outputs_count %d\n", outputs_count); |
162 |
elmex |
1.1 |
/* Match the enumerations but in string form */ |
163 |
root |
1.30 |
fprintf (freezer, "usekeys %s\n", usekeys == key_inventory ? "key_inventory" : (usekeys == keyrings ? "keyrings" : "containers")); |
164 |
elmex |
1.1 |
/* Match the enumerations but in string form */ |
165 |
root |
1.30 |
fprintf (freezer, "unapply %s\n", unapply == unapply_nochoice ? "unapply_nochoice" : |
166 |
|
|
(unapply == unapply_never ? "unapply_never" : "unapply_always")); |
167 |
elmex |
1.1 |
|
168 |
root |
1.30 |
if (ob->map) |
169 |
|
|
fprintf (freezer, "map %s\n", ob->map->path); |
170 |
elmex |
1.1 |
else |
171 |
root |
1.15 |
fprintf (freezer, "map %s\n", settings.emergency_mapname); |
172 |
|
|
|
173 |
root |
1.30 |
fprintf (freezer, "savebed_map %s\n", savebed_map); |
174 |
|
|
fprintf (freezer, "bed_x %d\nbed_y %d\n", bed_x, bed_y); |
175 |
|
|
fprintf (freezer, "weapon_sp %f\n", weapon_sp); |
176 |
|
|
fprintf (freezer, "Str %d\n", orig_stats.Str); |
177 |
|
|
fprintf (freezer, "Dex %d\n", orig_stats.Dex); |
178 |
|
|
fprintf (freezer, "Con %d\n", orig_stats.Con); |
179 |
|
|
fprintf (freezer, "Int %d\n", orig_stats.Int); |
180 |
|
|
fprintf (freezer, "Pow %d\n", orig_stats.Pow); |
181 |
|
|
fprintf (freezer, "Wis %d\n", orig_stats.Wis); |
182 |
|
|
fprintf (freezer, "Cha %d\n", orig_stats.Cha); |
183 |
|
|
|
184 |
|
|
fprintf (freezer, "lev_array %d\n", ob->level > 10 ? 10 : ob->level); |
185 |
|
|
|
186 |
|
|
for (int i = 1; i <= last_level && i <= 10; i++) |
187 |
|
|
{ |
188 |
|
|
fprintf (freezer, "%d\n", levhp[i]); |
189 |
|
|
fprintf (freezer, "%d\n", levsp[i]); |
190 |
|
|
fprintf (freezer, "%d\n", levgrace[i]); |
191 |
root |
1.15 |
} |
192 |
root |
1.4 |
|
193 |
root |
1.30 |
freezer.put (ob->contr); |
194 |
root |
1.4 |
|
195 |
root |
1.15 |
fprintf (freezer, "endplst\n"); |
196 |
elmex |
1.1 |
|
197 |
root |
1.30 |
SET_FLAG (ob, FLAG_NO_FIX_PLAYER); |
198 |
|
|
CLEAR_FLAG (ob, FLAG_WIZ); |
199 |
|
|
save_object (freezer, ob, 3); /* don't check and don't remove */ |
200 |
elmex |
1.1 |
|
201 |
root |
1.9 |
char filename[MAX_BUF]; |
202 |
root |
1.15 |
|
203 |
root |
1.30 |
sprintf (filename, "%s/%s/%s/%s.pl", settings.localdir, settings.playerdir, &ob->name, &ob->name); |
204 |
root |
1.15 |
make_path_to_file (filename); |
205 |
root |
1.9 |
freezer.save (filename); |
206 |
|
|
|
207 |
root |
1.30 |
CLEAR_FLAG (ob, FLAG_NO_FIX_PLAYER); |
208 |
elmex |
1.1 |
|
209 |
|
|
/* Eneq(@csd.uu.se): Reveal the container if we have one. */ |
210 |
root |
1.30 |
ob->container = container; |
211 |
root |
1.5 |
|
212 |
root |
1.30 |
ob->flags [FLAG_WIZ] = wiz; |
213 |
elmex |
1.1 |
|
214 |
root |
1.30 |
enable_save = !final; |
215 |
elmex |
1.1 |
} |
216 |
|
|
|
217 |
root |
1.30 |
player * |
218 |
|
|
player::load (const char *path) |
219 |
root |
1.15 |
{ |
220 |
root |
1.30 |
object_thawer thawer (path); |
221 |
root |
1.15 |
|
222 |
|
|
/* If no file, must be a new player, so lets get confirmation of |
223 |
|
|
* the password. Return control to the higher level dispatch, |
224 |
|
|
* since the rest of this just deals with loading of the file. |
225 |
|
|
*/ |
226 |
|
|
if (!thawer) |
227 |
root |
1.30 |
return 0; |
228 |
root |
1.3 |
|
229 |
root |
1.30 |
player *pl = new player; |
230 |
root |
1.16 |
|
231 |
root |
1.30 |
char buf[MAX_BUF], bufall[MAX_BUF]; |
232 |
root |
1.19 |
|
233 |
root |
1.30 |
pl->set_object (object::create ()); |
234 |
|
|
pl->last_save_time = time (0); |
235 |
elmex |
1.1 |
|
236 |
root |
1.30 |
assign (pl->savebed_map, first_map_path); |
237 |
root |
1.15 |
|
238 |
|
|
/* Loop through the file, loading the rest of the values */ |
239 |
root |
1.30 |
while (fgets (bufall, MAX_BUF, thawer)) |
240 |
root |
1.15 |
{ |
241 |
root |
1.30 |
int value; |
242 |
root |
1.15 |
sscanf (bufall, "%s %d\n", buf, &value); |
243 |
root |
1.30 |
|
244 |
root |
1.15 |
if (!strcmp (buf, "endplst")) |
245 |
|
|
break; |
246 |
|
|
else if (!strcmp (buf, "oid")) |
247 |
|
|
thawer.get (pl, value); |
248 |
root |
1.30 |
else if (!strcmp (buf, "password")) |
249 |
|
|
sscanf (bufall, "password %[^\n]", pl->password); |
250 |
|
|
else if (!strcmp (buf, "title")) |
251 |
root |
1.15 |
sscanf (bufall, "title %[^\n]", pl->own_title); |
252 |
|
|
else if (!strcmp (buf, "explore")) |
253 |
|
|
pl->explore = value; |
254 |
|
|
else if (!strcmp (buf, "gen_hp")) |
255 |
|
|
pl->gen_hp = value; |
256 |
|
|
else if (!strcmp (buf, "shoottype")) |
257 |
|
|
pl->shoottype = (rangetype) value; |
258 |
|
|
else if (!strcmp (buf, "bowtype")) |
259 |
|
|
pl->bowtype = (bowtype_t) value; |
260 |
|
|
else if (!strcmp (buf, "petmode")) |
261 |
|
|
pl->petmode = (petmode_t) value; |
262 |
|
|
else if (!strcmp (buf, "gen_sp")) |
263 |
|
|
pl->gen_sp = value; |
264 |
|
|
else if (!strcmp (buf, "gen_grace")) |
265 |
|
|
pl->gen_grace = value; |
266 |
|
|
else if (!strcmp (buf, "listening")) |
267 |
|
|
pl->listening = value; |
268 |
|
|
else if (!strcmp (buf, "peaceful")) |
269 |
|
|
pl->peaceful = value; |
270 |
|
|
else if (!strcmp (buf, "digestion")) |
271 |
|
|
pl->digestion = value; |
272 |
|
|
else if (!strcmp (buf, "pickup")) |
273 |
|
|
pl->mode = value; |
274 |
|
|
else if (!strcmp (buf, "outputs_sync")) |
275 |
|
|
pl->outputs_sync = value; |
276 |
|
|
else if (!strcmp (buf, "outputs_count")) |
277 |
|
|
pl->outputs_count = value; |
278 |
|
|
else if (!strcmp (buf, "map")) |
279 |
|
|
sscanf (bufall, "map %s", pl->maplevel); |
280 |
|
|
else if (!strcmp (buf, "savebed_map")) |
281 |
|
|
sscanf (bufall, "savebed_map %s", pl->savebed_map); |
282 |
|
|
else if (!strcmp (buf, "bed_x")) |
283 |
|
|
pl->bed_x = value; |
284 |
|
|
else if (!strcmp (buf, "bed_y")) |
285 |
|
|
pl->bed_y = value; |
286 |
|
|
else if (!strcmp (buf, "weapon_sp")) |
287 |
|
|
sscanf (buf, "weapon_sp %f", &pl->weapon_sp); |
288 |
|
|
else if (!strcmp (buf, "Str")) |
289 |
|
|
pl->orig_stats.Str = value; |
290 |
|
|
else if (!strcmp (buf, "Dex")) |
291 |
|
|
pl->orig_stats.Dex = value; |
292 |
|
|
else if (!strcmp (buf, "Con")) |
293 |
|
|
pl->orig_stats.Con = value; |
294 |
|
|
else if (!strcmp (buf, "Int")) |
295 |
|
|
pl->orig_stats.Int = value; |
296 |
|
|
else if (!strcmp (buf, "Pow")) |
297 |
|
|
pl->orig_stats.Pow = value; |
298 |
|
|
else if (!strcmp (buf, "Wis")) |
299 |
|
|
pl->orig_stats.Wis = value; |
300 |
|
|
else if (!strcmp (buf, "Cha")) |
301 |
|
|
pl->orig_stats.Cha = value; |
302 |
|
|
else if (!strcmp (buf, "usekeys")) |
303 |
|
|
{ |
304 |
|
|
if (!strcmp (bufall + 8, "key_inventory\n")) |
305 |
|
|
pl->usekeys = key_inventory; |
306 |
|
|
else if (!strcmp (bufall + 8, "keyrings\n")) |
307 |
|
|
pl->usekeys = keyrings; |
308 |
|
|
else if (!strcmp (bufall + 8, "containers\n")) |
309 |
|
|
pl->usekeys = containers; |
310 |
|
|
else |
311 |
|
|
LOG (llevDebug, "load_player: got unknown usekeys type: %s\n", bufall + 8); |
312 |
root |
1.6 |
} |
313 |
root |
1.15 |
else if (!strcmp (buf, "unapply")) |
314 |
|
|
{ |
315 |
|
|
if (!strcmp (bufall + 8, "unapply_nochoice\n")) |
316 |
|
|
pl->unapply = unapply_nochoice; |
317 |
|
|
else if (!strcmp (bufall + 8, "unapply_never\n")) |
318 |
|
|
pl->unapply = unapply_never; |
319 |
|
|
else if (!strcmp (bufall + 8, "unapply_always\n")) |
320 |
|
|
pl->unapply = unapply_always; |
321 |
|
|
else |
322 |
|
|
LOG (llevDebug, "load_player: got unknown unapply type: %s\n", bufall + 8); |
323 |
|
|
} |
324 |
|
|
else if (!strcmp (buf, "lev_array")) |
325 |
|
|
{ |
326 |
root |
1.30 |
for (int i = 1; i <= value; i++) |
327 |
root |
1.15 |
{ |
328 |
|
|
char line[128]; |
329 |
|
|
|
330 |
|
|
fgets (line, 128, thawer); |
331 |
|
|
pl->levhp[i] = atoi (line); |
332 |
|
|
fgets (line, 128, thawer); |
333 |
|
|
pl->levsp[i] = atoi (line); |
334 |
|
|
fgets (line, 128, thawer); |
335 |
|
|
pl->levgrace[i] = atoi (line); |
336 |
|
|
} |
337 |
|
|
/* spell_array code removed - don't know when that was last used. |
338 |
|
|
* Even the load code below will someday be replaced by spells being |
339 |
|
|
* objects. |
340 |
|
|
*/ |
341 |
|
|
} |
342 |
root |
1.30 |
else |
343 |
|
|
LOG (llevDebug, "unparseable line in player file %s: %s\n", path, bufall); |
344 |
|
|
} |
345 |
root |
1.15 |
|
346 |
|
|
/* this loads the standard objects values. */ |
347 |
root |
1.30 |
load_object (thawer, pl->ob, 0); |
348 |
root |
1.15 |
|
349 |
|
|
/* If the map where the person was last saved does not exist, |
350 |
|
|
* restart them on their home-savebed. This is good for when |
351 |
|
|
* maps change between versions |
352 |
|
|
* First, we check for partial path, then check to see if the full |
353 |
|
|
* path (for unique player maps) |
354 |
|
|
*/ |
355 |
root |
1.26 |
if (!has_been_loaded (pl->maplevel) |
356 |
|
|
&& check_path (pl->maplevel, 1) == -1 |
357 |
|
|
&& check_path (pl->maplevel, 0) == -1) |
358 |
root |
1.15 |
{ |
359 |
root |
1.26 |
strcpy (pl->maplevel, pl->savebed_map); |
360 |
root |
1.30 |
pl->ob->x = pl->bed_x, pl->ob->y = pl->bed_y; |
361 |
elmex |
1.1 |
} |
362 |
|
|
|
363 |
root |
1.15 |
pl->last_save_tick = pticks; |
364 |
|
|
|
365 |
root |
1.30 |
INVOKE_PLAYER (LOAD, pl, ARG_STRING (path)); |
366 |
root |
1.15 |
|
367 |
root |
1.30 |
return pl; |
368 |
|
|
} |
369 |
root |
1.15 |
|