ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/player.C
(Generate patch)

Comparing deliantra/server/common/player.C (file contents):
Revision 1.10 by root, Sat Dec 16 03:08:25 2006 UTC vs.
Revision 1.23 by root, Sun Apr 29 21:44:34 2007 UTC

1/* 1/*
2 CrossFire, A Multiplayer game for X-windows 2 * CrossFire, A Multiplayer game for X-windows
3 3 *
4 * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team
4 Copyright (C) 2002 Mark Wedel & Crossfire Development Team 5 * Copyright (C) 2002 Mark Wedel & Crossfire Development Team
5 Copyright (C) 1992 Frank Tore Johansen 6 * Copyright (C) 1992 Frank Tore Johansen
6 7 *
7 This program is free software; you can redistribute it and/or modify 8 * 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 * 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 * the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version. 11 * (at your option) any later version.
11 12 *
12 This program is distributed in the hope that it will be useful, 13 * This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details. 16 * GNU General Public License for more details.
16 17 *
17 You should have received a copy of the GNU General Public License 18 * 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 * along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 21 *
21 The authors can be reached via e-mail at <crossfire@schmorp.de> 22 * The authors can be reached via e-mail at <crossfire@schmorp.de>
22*/ 23 */
23 24
24#include <global.h> 25#include <global.h>
25#include <funcpoint.h> 26#include <funcpoint.h>
26
27void
28free_player (player *pl)
29{
30 if (first_player != pl)
31 {
32 player *prev = first_player;
33
34 while (prev != NULL && prev->next != NULL && prev->next != pl)
35 prev = prev->next;
36
37 if (prev->next != pl)
38 {
39 LOG (llevError, "Free_player: Can't find previous player.\n");
40 exit (1);
41 }
42
43 prev->next = pl->next;
44 }
45 else
46 first_player = pl->next;
47
48 if (pl->ob)
49 {
50 pl->ob->contr = 0;
51 pl->ob->destroy ();
52 }
53
54 if (pl->socket)
55 {
56 pl->socket->pl = 0;
57 pl->socket->send_packet ("goodbye");
58 pl->socket->destroy ();
59 }
60
61 /* Clear item stack */
62 if (pl->stack_items)
63 free (pl->stack_items);
64
65 delete pl;
66}
67
68 27
69/* Determine if the attacktype represented by the 28/* Determine if the attacktype represented by the
70 * specified attack-number is enabled for dragon players. 29 * specified attack-number is enabled for dragon players.
71 * A dragon player (quetzal) can gain resistances for 30 * A dragon player (quetzal) can gain resistances for
72 * all enabled attacktypes. 31 * all enabled attacktypes.
88int 47int
89is_dragon_pl (const object *op) 48is_dragon_pl (const object *op)
90{ 49{
91 if (op != NULL && op->type == PLAYER && op->arch != NULL && op->arch->clone.race != NULL && strcmp (op->arch->clone.race, "dragon") == 0) 50 if (op != NULL && op->type == PLAYER && op->arch != NULL && op->arch->clone.race != NULL && strcmp (op->arch->clone.race, "dragon") == 0)
92 return 1; 51 return 1;
52
93 return 0; 53 return 0;
94} 54}
55
56/* Returns TRUE if the range specified (int r) is legal - that is,
57 * the character has an item that is equipped for that range type.
58 * return 0 if there is no item of that range type that is usable.
59 * This function could probably be simplified, eg, everything
60 * should index into the ranges[] array.
61 */
62bool
63player::legal_range (rangetype r) const
64{
65 switch (r)
66 {
67 case range_none: /* "Nothing" is always legal */
68 return 1;
69
70 case range_bow:
71 case range_misc:
72 case range_magic: /* cast spells */
73 case range_golem:
74 return !!ranges [r];
75
76 case range_skill:
77 return !!ob->chosen_skill;
78 }
79
80 /* No match above, must not be valid */
81 return 0;
82}
83
84/*
85 * If final is set, it a clean/final save, not a backup, ie dont remove objects from inventory
86 */
87bool
88player::save_pl (object_freezer &freezer)
89{
90 INVOKE_PLAYER (SAVE, ob->contr);
91
92 int wiz = ob->flag [FLAG_WIZ];
93
94 /* Eneq(@csd.uu.se): If we have an open container hide it. */
95 object *container = ob->container;
96 ob->container = 0;
97
98 if (ob->map)
99 maplevel = ob->map->path;
100
101#define PL_OUT(k) freezer.put (KW_ ## k, k)
102#define PL_OUT2(k,v) freezer.put (KW_ ## k, v)
103
104 PL_OUT (password);
105 PL_OUT2 (title, own_title);
106 PL_OUT (gen_hp);
107 PL_OUT (gen_sp);
108 PL_OUT (gen_grace);
109 PL_OUT (listening);
110 PL_OUT (shoottype);
111 PL_OUT (bowtype);
112 PL_OUT (petmode);
113 PL_OUT (peaceful);
114 PL_OUT (digestion);
115 PL_OUT2 (pickup, mode);
116 PL_OUT (outputs_sync);
117 PL_OUT (outputs_count);
118
119 freezer.put (KW_usekeys, usekeys == key_inventory ? "key_inventory" : (usekeys == keyrings ? "keyrings" : "containers"));
120 freezer.put (KW_unapply, unapply == unapply_nochoice ? "unapply_nochoice" : (unapply == unapply_never ? "unapply_never" : "unapply_always"));
121
122 PL_OUT2 (map, maplevel);
123 PL_OUT (savebed_map);
124 PL_OUT (bed_x);
125 PL_OUT (bed_y);
126 PL_OUT (weapon_sp);
127 PL_OUT2 (Str, orig_stats.Str);
128 PL_OUT2 (Dex, orig_stats.Dex);
129 PL_OUT2 (Con, orig_stats.Con);
130 PL_OUT2 (Int, orig_stats.Int);
131 PL_OUT2 (Pow, orig_stats.Pow);
132 PL_OUT2 (Wis, orig_stats.Wis);
133 PL_OUT2 (Cha, orig_stats.Cha);
134
135 PL_OUT2 (lev_array, 10);
136
137 for (int i = 1; i <= 10; i++)
138 {
139 fprintf (freezer, "%d\n", levhp [i]);
140 fprintf (freezer, "%d\n", levsp [i]);
141 fprintf (freezer, "%d\n", levgrace[i]);
142 }
143
144 freezer.put (ob->contr);
145 freezer.put (KW_endplst);
146
147 SET_FLAG (ob, FLAG_NO_FIX_PLAYER);
148 CLEAR_FLAG (ob, FLAG_WIZ);
149 ob->write (freezer);
150
151 CLEAR_FLAG (ob, FLAG_NO_FIX_PLAYER);
152
153 /* Eneq(@csd.uu.se): Reveal the container if we have one. */
154 ob->container = container;
155
156 ob->flag [FLAG_WIZ] = wiz;
157
158 INVOKE_PLAYER (SAVE_DONE, ob->contr);
159
160 return true;
161}
162
163bool
164player::save_pl (const char *path)
165{
166 object_freezer freezer;
167
168 if (!save_pl (freezer))
169 return false;
170
171 return freezer.save (path);
172}
173
174player *
175player::load_pl (object_thawer &f)
176{
177 int maxerrs = 20;
178
179 player *pl = new player;
180
181 pl->savebed_map = first_map_path;
182
183 /* Loop through the file, loading the rest of the values */
184 for (;;)
185 {
186 switch (f.kw)
187 {
188 case KW_EOF:
189 LOG (llevError, "%s: unexpected EOF while reading player header\n", f.name);
190 goto failure;
191
192 default:
193 LOG (llevError, "%s: skipping unknown key in player header: %s\n", f.name, f.kw_str);
194 if (!--maxerrs) goto failure;
195 break;
196
197 case KW_ERROR:
198 LOG (llevError, "%s: parse error while reading player header, skipping (%s,%s).\n", f.name, f.kw_str, f.value);
199 if (!--maxerrs) goto failure;
200 break;
201
202 case KW_endplst:
203 f.next ();
204
205 if (object *op = pl->ob->read (f))
206 pl->set_object (op);
207 else
208 goto failure;
209
210 INVOKE_PLAYER (LOAD, pl);
211
212 return pl;
213
214 case KW_oid: f.get (pl, f.get_sint32 ()); break;
215 case KW_password: assign (pl->password , f.get_str ()); break;
216 case KW_title: assign (pl->own_title, f.get_str ()); break;
217 case KW_shoottype: pl->shoottype = (rangetype) f.get_sint32 (); break;
218 case KW_bowtype: pl->bowtype = (bowtype_t) f.get_sint32 (); break;
219 case KW_petmode: pl->petmode = (petmode_t) f.get_sint32 (); break;
220 case KW_listening: f.get (pl->listening); break;
221 case KW_peaceful: f.get (pl->peaceful); break;
222 case KW_digestion: f.get (pl->digestion); break;
223 case KW_pickup: f.get (pl->mode); break;
224 case KW_outputs_sync: f.get (pl->outputs_sync); break;
225 case KW_outputs_count: f.get (pl->outputs_count); break;
226 case KW_map: f.get (pl->maplevel); break;
227 case KW_savebed_map: f.get (pl->savebed_map); break;
228 case KW_bed_x: f.get (pl->bed_x); break;
229 case KW_bed_y: f.get (pl->bed_y); break;
230 case KW_weapon_sp: f.get (pl->weapon_sp); break;
231 case KW_Str: f.get (pl->orig_stats.Str); break;
232 case KW_Dex: f.get (pl->orig_stats.Dex); break;
233 case KW_Con: f.get (pl->orig_stats.Con); break;
234 case KW_Int: f.get (pl->orig_stats.Int); break;
235 case KW_Pow: f.get (pl->orig_stats.Pow); break;
236 case KW_Wis: f.get (pl->orig_stats.Wis); break;
237 case KW_Cha: f.get (pl->orig_stats.Cha); break;
238 case KW_gen_hp: f.get (pl->gen_hp); break;
239 case KW_gen_sp: f.get (pl->gen_sp); break;
240 case KW_gen_grace: f.get (pl->gen_grace); break;
241
242 case KW_usekeys:
243 if (!strcmp (f.get_str (), "key_inventory"))
244 pl->usekeys = key_inventory;
245 else if (!strcmp (f.get_str (), "keyrings"))
246 pl->usekeys = keyrings;
247 else if (!strcmp (f.get_str (), "containers"))
248 pl->usekeys = containers;
249 else
250 LOG (llevDebug, "load_player: got unknown usekeys type: %s\n", f.get_str ());
251 break;
252
253 case KW_unapply:
254 if (!strcmp (f.get_str (), "unapply_nochoice"))
255 pl->unapply = unapply_nochoice;
256 else if (!strcmp (f.get_str (), "unapply_never"))
257 pl->unapply = unapply_never;
258 else if (!strcmp (f.get_str (), "unapply_always"))
259 pl->unapply = unapply_always;
260 else
261 LOG (llevDebug, "load_player: got unknown unapply type: %s\n", f.get_str ());
262 break;
263
264 case KW_lev_array:
265 {
266 int count = f.get_sint32 ();
267
268 for (int i = 1; i <= count; i++)
269 {
270 char line [32];
271
272 fgets (line, 32, f); pl->levhp [i] = atoi (line);
273 fgets (line, 32, f); pl->levsp [i] = atoi (line);
274 fgets (line, 32, f); pl->levgrace[i] = atoi (line);
275 }
276 }
277 break;
278 }
279
280 f.next ();
281 }
282
283failure:
284 LOG (llevError, "%s: too many or too grave errors, aborting player load.\n", f.name);
285
286 if (pl->ob)
287 pl->ob->destroy ();
288
289 pl->destroy ();
290
291 return 0;
292}
293
294player *
295player::load_pl (const char *path)
296{
297 object_thawer f (path);
298
299 if (!f)
300 return 0;
301
302 f.next ();
303
304 return load_pl (f);
305}
306

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines