ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/login.C
Revision: 1.37
Committed: Sun Dec 31 21:02:05 2006 UTC (17 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.36: +14 -4 lines
Log Message:
more use of shstr where it makes sense naturally

File Contents

# Content
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 The authors can be reached via e-mail at <crossfire@schmorp.de>
22 */
23
24 #include <global.h>
25 #include <sproto.h>
26 #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 /* Delete character with name. if new is set, also delete the new
35 * style directory, otherwise, just delete the old style playfile
36 * (needed for transition)
37 */
38 void
39 delete_character (const char *name, int newchar)
40 {
41 char buf[MAX_BUF];
42
43 sprintf (buf, "%s/%s/%s.pl", settings.localdir, settings.playerdir, name);
44
45 if (unlink (buf) == -1)
46 LOG (llevDebug, "Cannot delete character file %s: %s\n", buf, strerror (errno));
47
48 if (newchar)
49 {
50 sprintf (buf, "%s/%s/%s", settings.localdir, settings.playerdir, name);
51 /* this effectively does an rm -rf on the directory */
52 remove_directory (buf);
53 }
54 }
55
56 int
57 create_savedir_if_needed (char *savedir)
58 {
59 struct stat *buf;
60
61 if ((buf = (struct stat *) malloc (sizeof (struct stat))) == NULL)
62 {
63 LOG (llevError, "Unable to save playerfile... out of memory.\n");
64 return 0;
65 }
66 else
67 {
68 stat (savedir, buf);
69 if (!S_ISDIR (buf->st_mode))
70 if (mkdir (savedir, SAVE_DIR_MODE))
71 {
72 LOG (llevError, "Unable to create player savedir %s: %s\n", savedir, strerror (errno));
73 return 0;
74 }
75 free (buf);
76 }
77
78 return 1;
79 }
80
81 /*
82 * If final is set, it a clean/final save, not a backup, ie dont remove objects from inventory
83 */
84 void
85 player::save (bool final)
86 {
87 object *tmp, *container = 0;
88
89 /* Sanity check - some stuff changes this when player is exiting */
90 if (ob->type != PLAYER || !enable_save || !ns)
91 return;
92
93 INVOKE_PLAYER (SAVE, ob->contr);
94
95 object_freezer freezer;
96
97 int wiz = ob->flag [FLAG_WIZ];
98
99 /* Eneq(@csd.uu.se): If we have an open container hide it. */
100 container = ob->container;
101 ob->container = 0;
102
103 fprintf (freezer, "password %s\n", password);
104
105 if (own_title[0] != '\0')
106 fprintf (freezer, "title %s\n", own_title);
107
108 fprintf (freezer, "explore %d\n", explore);
109 fprintf (freezer, "gen_hp %d\n", gen_hp);
110 fprintf (freezer, "gen_sp %d\n", gen_sp);
111 fprintf (freezer, "gen_grace %d\n", gen_grace);
112 fprintf (freezer, "listening %d\n", listening);
113 fprintf (freezer, "shoottype %d\n", shoottype);
114 fprintf (freezer, "bowtype %d\n", bowtype);
115 fprintf (freezer, "petmode %d\n", petmode);
116 fprintf (freezer, "peaceful %d\n", peaceful);
117 fprintf (freezer, "digestion %d\n", digestion);
118 fprintf (freezer, "pickup %d\n", mode);
119 fprintf (freezer, "outputs_sync %d\n", outputs_sync);
120 fprintf (freezer, "outputs_count %d\n", outputs_count);
121 /* Match the enumerations but in string form */
122 fprintf (freezer, "usekeys %s\n", usekeys == key_inventory ? "key_inventory" : (usekeys == keyrings ? "keyrings" : "containers"));
123 /* Match the enumerations but in string form */
124 fprintf (freezer, "unapply %s\n", unapply == unapply_nochoice ? "unapply_nochoice" :
125 (unapply == unapply_never ? "unapply_never" : "unapply_always"));
126
127 if (ob->map)
128 fprintf (freezer, "map %s\n", &ob->map->path);
129
130 fprintf (freezer, "savebed_map %s\n", &savebed_map);
131 fprintf (freezer, "bed_x %d\nbed_y %d\n", bed_x, bed_y);
132 fprintf (freezer, "weapon_sp %f\n", weapon_sp);
133 fprintf (freezer, "Str %d\n", orig_stats.Str);
134 fprintf (freezer, "Dex %d\n", orig_stats.Dex);
135 fprintf (freezer, "Con %d\n", orig_stats.Con);
136 fprintf (freezer, "Int %d\n", orig_stats.Int);
137 fprintf (freezer, "Pow %d\n", orig_stats.Pow);
138 fprintf (freezer, "Wis %d\n", orig_stats.Wis);
139 fprintf (freezer, "Cha %d\n", orig_stats.Cha);
140
141 fprintf (freezer, "lev_array %d\n", ob->level > 10 ? 10 : ob->level);
142
143 for (int i = 1; i <= last_level && i <= 10; i++)
144 {
145 fprintf (freezer, "%d\n", levhp[i]);
146 fprintf (freezer, "%d\n", levsp[i]);
147 fprintf (freezer, "%d\n", levgrace[i]);
148 }
149
150 freezer.put (ob->contr);
151
152 fprintf (freezer, "endplst\n");
153
154 SET_FLAG (ob, FLAG_NO_FIX_PLAYER);
155 CLEAR_FLAG (ob, FLAG_WIZ);
156 save_object (freezer, ob, 1); /* don't check and don't remove */
157
158 char filename[MAX_BUF];
159
160 sprintf (filename, "%s/%s/%s/%s.pl", settings.localdir, settings.playerdir, &ob->name, &ob->name);
161 make_path_to_file (filename);
162 freezer.save (filename);
163
164 CLEAR_FLAG (ob, FLAG_NO_FIX_PLAYER);
165
166 /* Eneq(@csd.uu.se): Reveal the container if we have one. */
167 ob->container = container;
168
169 ob->flag [FLAG_WIZ] = wiz;
170
171 enable_save = !final;
172 }
173
174 player *
175 player::load (const char *path)
176 {
177 object_thawer thawer (path);
178
179 /* If no file, must be a new player, so lets get confirmation of
180 * the password. Return control to the higher level dispatch,
181 * since the rest of this just deals with loading of the file.
182 */
183 if (!thawer)
184 return 0;
185
186 player *pl = new player;
187
188 char buf[MAX_BUF], bufall[MAX_BUF];
189
190 pl->set_object (object::create ());
191 pl->last_save_time = time (0);
192
193 pl->savebed_map = first_map_path;
194
195 /* Loop through the file, loading the rest of the values */
196 while (fgets (bufall, MAX_BUF, thawer))
197 {
198 int value;
199 sscanf (bufall, "%s %d\n", buf, &value);
200
201 if (!strcmp (buf, "endplst"))
202 break;
203 else if (!strcmp (buf, "oid"))
204 thawer.get (pl, value);
205 else if (!strcmp (buf, "password"))
206 sscanf (bufall, "password %[^\n]", pl->password);
207 else if (!strcmp (buf, "title"))
208 sscanf (bufall, "title %[^\n]", pl->own_title);
209 else if (!strcmp (buf, "explore"))
210 pl->explore = value;
211 else if (!strcmp (buf, "gen_hp"))
212 pl->gen_hp = value;
213 else if (!strcmp (buf, "shoottype"))
214 pl->shoottype = (rangetype) value;
215 else if (!strcmp (buf, "bowtype"))
216 pl->bowtype = (bowtype_t) value;
217 else if (!strcmp (buf, "petmode"))
218 pl->petmode = (petmode_t) value;
219 else if (!strcmp (buf, "gen_sp"))
220 pl->gen_sp = value;
221 else if (!strcmp (buf, "gen_grace"))
222 pl->gen_grace = value;
223 else if (!strcmp (buf, "listening"))
224 pl->listening = value;
225 else if (!strcmp (buf, "peaceful"))
226 pl->peaceful = value;
227 else if (!strcmp (buf, "digestion"))
228 pl->digestion = value;
229 else if (!strcmp (buf, "pickup"))
230 pl->mode = value;
231 else if (!strcmp (buf, "outputs_sync"))
232 pl->outputs_sync = value;
233 else if (!strcmp (buf, "outputs_count"))
234 pl->outputs_count = value;
235 else if (!strcmp (buf, "map"))
236 {
237 //TODO: use thawer
238 char buf[8192];
239 sscanf (bufall, "map %s", buf);
240 pl->maplevel = buf;
241 }
242 else if (!strcmp (buf, "savebed_map"))
243 {
244 //TODO: use thawer
245 char buf[8192];
246 sscanf (bufall, "savebed_map %s", buf);
247 pl->savebed_map = buf;
248 }
249 else if (!strcmp (buf, "bed_x"))
250 pl->bed_x = value;
251 else if (!strcmp (buf, "bed_y"))
252 pl->bed_y = value;
253 else if (!strcmp (buf, "weapon_sp"))
254 sscanf (buf, "weapon_sp %f", &pl->weapon_sp);
255 else if (!strcmp (buf, "Str"))
256 pl->orig_stats.Str = value;
257 else if (!strcmp (buf, "Dex"))
258 pl->orig_stats.Dex = value;
259 else if (!strcmp (buf, "Con"))
260 pl->orig_stats.Con = value;
261 else if (!strcmp (buf, "Int"))
262 pl->orig_stats.Int = value;
263 else if (!strcmp (buf, "Pow"))
264 pl->orig_stats.Pow = value;
265 else if (!strcmp (buf, "Wis"))
266 pl->orig_stats.Wis = value;
267 else if (!strcmp (buf, "Cha"))
268 pl->orig_stats.Cha = value;
269 else if (!strcmp (buf, "usekeys"))
270 {
271 if (!strcmp (bufall + 8, "key_inventory\n"))
272 pl->usekeys = key_inventory;
273 else if (!strcmp (bufall + 8, "keyrings\n"))
274 pl->usekeys = keyrings;
275 else if (!strcmp (bufall + 8, "containers\n"))
276 pl->usekeys = containers;
277 else
278 LOG (llevDebug, "load_player: got unknown usekeys type: %s\n", bufall + 8);
279 }
280 else if (!strcmp (buf, "unapply"))
281 {
282 if (!strcmp (bufall + 8, "unapply_nochoice\n"))
283 pl->unapply = unapply_nochoice;
284 else if (!strcmp (bufall + 8, "unapply_never\n"))
285 pl->unapply = unapply_never;
286 else if (!strcmp (bufall + 8, "unapply_always\n"))
287 pl->unapply = unapply_always;
288 else
289 LOG (llevDebug, "load_player: got unknown unapply type: %s\n", bufall + 8);
290 }
291 else if (!strcmp (buf, "lev_array"))
292 {
293 for (int i = 1; i <= value; i++)
294 {
295 char line[128];
296
297 fgets (line, 128, thawer);
298 pl->levhp[i] = atoi (line);
299 fgets (line, 128, thawer);
300 pl->levsp[i] = atoi (line);
301 fgets (line, 128, thawer);
302 pl->levgrace[i] = atoi (line);
303 }
304 /* spell_array code removed - don't know when that was last used.
305 * Even the load code below will someday be replaced by spells being
306 * objects.
307 */
308 }
309 else
310 LOG (llevDebug, "unparseable line in player file %s: %s\n", path, bufall);
311 }
312
313 /* this loads the standard objects values. */
314 load_object (thawer, pl->ob, 0);
315
316 pl->last_save_tick = pticks;
317
318 INVOKE_PLAYER (LOAD, pl, ARG_STRING (path));
319
320 return pl;
321 }
322