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.17 by root, Tue Jan 9 01:28:32 2007 UTC vs.
Revision 1.61 by root, Sat Jan 16 12:33:46 2010 UTC

1/* 1/*
2 * CrossFire, A Multiplayer game for X-windows 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team 4 * Copyright (©) 2005,2006,2007,2008,2009 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (C) 2002 Mark Wedel & Crossfire Development Team 5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
6 * Copyright (C) 1992 Frank Tore Johansen 6 * Copyright (©) 1992,2007 Frank Tore Johansen
7 * 7 *
8 * This program is free software; you can redistribute it and/or modify 8 * Deliantra is free software: you can redistribute it and/or modify it under
9 * it under the terms of the GNU General Public License as published by 9 * the terms of the Affero GNU General Public License as published by the
10 * the Free Software Foundation; either version 2 of the License, or 10 * Free Software Foundation, either version 3 of the License, or (at your
11 * (at your option) any later version. 11 * option) any later version.
12 * 12 *
13 * 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,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details. 16 * GNU General Public License for more details.
17 * 17 *
18 * You should have received a copy of the GNU General Public License 18 * You should have received a copy of the Affero GNU General Public License
19 * along with this program; if not, write to the Free Software 19 * and the GNU General Public License along with this program. If not, see
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 * <http://www.gnu.org/licenses/>.
21 * 21 *
22 * The authors can be reached via e-mail at <crossfire@schmorp.de> 22 * The authors can be reached via e-mail to <support@deliantra.net>
23 */ 23 */
24 24
25#include <global.h> 25#include <global.h>
26#include <funcpoint.h> 26
27//+GPL
27 28
28/* Determine if the attacktype represented by the 29/* Determine if the attacktype represented by the
29 * specified attack-number is enabled for dragon players. 30 * specified attack-number is enabled for dragon players.
30 * A dragon player (quetzal) can gain resistances for 31 * A dragon player (quetzal) can gain resistances for
31 * all enabled attacktypes. 32 * all enabled attacktypes.
45 * of the dragon race. 46 * of the dragon race.
46 */ 47 */
47int 48int
48is_dragon_pl (const object *op) 49is_dragon_pl (const object *op)
49{ 50{
50 if (op != NULL && op->type == PLAYER && op->arch != NULL && op->arch->clone.race != NULL && strcmp (op->arch->clone.race, "dragon") == 0) 51 if (op
52 && op->type == PLAYER
53 && op->arch
54 && op->arch->race == shstr_dragon)
51 return 1; 55 return 1;
56
52 return 0; 57 return 0;
53} 58}
54 59
55/* 60/*
56 * If final is set, it a clean/final save, not a backup, ie dont remove objects from inventory 61 * If final is set, it a clean/final save, not a backup, ie dont remove objects from inventory
61 INVOKE_PLAYER (SAVE, ob->contr); 66 INVOKE_PLAYER (SAVE, ob->contr);
62 67
63 int wiz = ob->flag [FLAG_WIZ]; 68 int wiz = ob->flag [FLAG_WIZ];
64 69
65 /* Eneq(@csd.uu.se): If we have an open container hide it. */ 70 /* Eneq(@csd.uu.se): If we have an open container hide it. */
66 object *container = ob->container; 71 object *container = ob->container_ ();
67 ob->container = 0; 72 ob->container = 0;
68 73
74 if (ob->map)
75 maplevel = ob->map->path;
76
69#define PL_OUT(k) freezer.put (KW_ ## k, k) 77#define PL_OUT(k) freezer.put (KW(k), k)
70#define PL_OUT2(k,v) freezer.put (KW_ ## k, v) 78#define PL_OUT2(k,v) freezer.put (KW(k), v)
71 79
72 PL_OUT (password); 80 PL_OUT (password);
73 PL_OUT2 (title, own_title); 81 PL_OUT2 (title, own_title);
82 PL_OUT (gender);
83 PL_OUT (hintmode);
74 PL_OUT (gen_hp); 84 PL_OUT (gen_hp);
75 PL_OUT (gen_sp); 85 PL_OUT (gen_sp);
76 PL_OUT (gen_grace); 86 PL_OUT (gen_grace);
77 PL_OUT (listening);
78 PL_OUT (shoottype);
79 PL_OUT (bowtype); 87 PL_OUT (bowtype);
80 PL_OUT (petmode); 88 PL_OUT (petmode);
81 PL_OUT (peaceful); 89 PL_OUT (peaceful);
82 PL_OUT (digestion); 90 PL_OUT (digestion);
83 PL_OUT2 (pickup, mode); 91 PL_OUT2 (pickup, mode);
84 PL_OUT (outputs_sync); 92 PL_OUT (outputs_sync);
85 PL_OUT (outputs_count); 93 PL_OUT (outputs_count);
86 94
87 freezer.put (KW_usekeys, usekeys == key_inventory ? "key_inventory" : (usekeys == keyrings ? "keyrings" : "containers")); 95 freezer.put (KW(usekeys), usekeys == key_inventory ? CS(key_inventory) : (usekeys == keyrings ? CS(keyrings) : CS(containers)));
88 freezer.put (KW_unapply, unapply == unapply_nochoice ? "unapply_nochoice" : (unapply == unapply_never ? "unapply_never" : "unapply_always")); 96 freezer.put (KW(unapply), unapply == unapply_nochoice ? CS(unapply_nochoice) : (unapply == unapply_never ? CS(unapply_never) : CS(unapply_always)));
89 97
90 PL_OUT2 (map, maplevel); 98 PL_OUT2 (map, maplevel);
91 PL_OUT (savebed_map); 99 PL_OUT (savebed_map);
92 PL_OUT (bed_x); 100 PL_OUT (bed_x);
93 PL_OUT (bed_y); 101 PL_OUT (bed_y);
94 PL_OUT (weapon_sp); 102 PL_OUT (weapon_sp);
103 PL_OUT (weapon_sp_left);
95 PL_OUT2 (Str, orig_stats.Str); 104 PL_OUT2 (Str, orig_stats.Str);
96 PL_OUT2 (Dex, orig_stats.Dex); 105 PL_OUT2 (Dex, orig_stats.Dex);
97 PL_OUT2 (Con, orig_stats.Con); 106 PL_OUT2 (Con, orig_stats.Con);
98 PL_OUT2 (Int, orig_stats.Int); 107 PL_OUT2 (Int, orig_stats.Int);
99 PL_OUT2 (Pow, orig_stats.Pow); 108 PL_OUT2 (Pow, orig_stats.Pow);
102 111
103 PL_OUT2 (lev_array, 10); 112 PL_OUT2 (lev_array, 10);
104 113
105 for (int i = 1; i <= 10; i++) 114 for (int i = 1; i <= 10; i++)
106 { 115 {
107 fprintf (freezer, "%d\n", levhp [i]); 116 freezer.add (levhp [i]); freezer.add ('\n');
108 fprintf (freezer, "%d\n", levsp [i]); 117 freezer.add (levsp [i]); freezer.add ('\n');
109 fprintf (freezer, "%d\n", levgrace[i]); 118 freezer.add (levgrace[i]); freezer.add ('\n');
110 } 119 }
111 120
112 freezer.put (ob->contr); 121 freezer.put (ob->contr);
113 freezer.put (KW_endplst); 122 freezer.put (KW(endplst));
114 123
115 SET_FLAG (ob, FLAG_NO_FIX_PLAYER);
116 CLEAR_FLAG (ob, FLAG_WIZ); 124 CLEAR_FLAG (ob, FLAG_WIZ);
117 save_object (freezer, ob, 1); /* don't check and don't remove */ 125 ob->write (freezer);
118
119 CLEAR_FLAG (ob, FLAG_NO_FIX_PLAYER);
120 126
121 /* Eneq(@csd.uu.se): Reveal the container if we have one. */ 127 /* Eneq(@csd.uu.se): Reveal the container if we have one. */
122 ob->container = container; 128 ob->container = container;
123 129
124 ob->flag [FLAG_WIZ] = wiz; 130 ob->flag [FLAG_WIZ] = wiz;
126 INVOKE_PLAYER (SAVE_DONE, ob->contr); 132 INVOKE_PLAYER (SAVE_DONE, ob->contr);
127 133
128 return true; 134 return true;
129} 135}
130 136
137//-GPL
138
131bool 139bool
132player::save_pl (const char *path) 140player::save_pl (const char *path)
133{ 141{
134 object_freezer freezer; 142 object_freezer freezer;
135 143
138 146
139 return freezer.save (path); 147 return freezer.save (path);
140} 148}
141 149
142player * 150player *
143player::load_pl (object_thawer &thawer) 151player::load_pl (object_thawer &f)
144{ 152{
153 int maxerrs = 20;
154
145 player *pl = new player; 155 player *pl = new player;
146 156
147 pl->savebed_map = first_map_path; 157 pl->savebed_map = first_map_path;
148
149 /* this loads the standard objects values. */
150 pl->set_object (object::create ());
151 158
152 /* Loop through the file, loading the rest of the values */ 159 /* Loop through the file, loading the rest of the values */
153 for (;;) 160 for (;;)
154 { 161 {
155 keyword kw = thawer.get_kv ();
156
157 switch (kw) 162 switch (f.kw)
158 { 163 {
159 case KW_EOF: 164 case KW_EOF:
160 LOG (llevError, "%s: unexpected EOF while reading player header\n", thawer.name); 165 LOG (llevError, "%s: unexpected EOF while reading player header\n", f.name);
161 pl->ob->destroy (); 166 goto failure;
162 pl->destroy (); 167
168 default:
169 LOG (llevError, "%s: skipping unknown key in player header: %s\n", f.name, f.kw_str);
170 if (!--maxerrs) goto failure;
163 return 0; 171 break;
164 172
165 case KW_ERROR: 173 case KW_ERROR:
166 LOG (llevError, "%s: error while reading player header, skipping (%s,%s)\n", thawer.name, thawer.last_keyword, thawer.last_value); 174 LOG (llevError, "%s: parse error while reading player header, skipping (%s,%s).\n", f.name, f.kw_str, f.value);
175 if (!--maxerrs) goto failure;
167 break; 176 break;
168 177
169 case KW_endplst: 178 case KW_endplst:
179 f.next ();
180
181 if (object *op = pl->ob->read (f))
182 pl->set_object (op);
183 else
170 goto done; 184 goto failure;
171 185
186 INVOKE_PLAYER (LOAD, pl);
187
188 return pl;
189
172 case KW_oid: thawer.get (pl, thawer.get_sint32 ()); break; 190 case KW_oid: f.get (pl, f.get_sint32 ()); break;
173 case KW_password: assign (pl->password , thawer.get_str ()); break; 191 case KW_password: assign (pl->password , f.get_str ()); break;
174 case KW_title: assign (pl->own_title, thawer.get_str ()); break; 192 case KW_title: assign (pl->own_title, f.get_str ()); break;
175 case KW_shoottype: pl->shoottype = (rangetype) thawer.get_sint32 (); break;
176 case KW_bowtype: pl->bowtype = (bowtype_t) thawer.get_sint32 (); break; 193 case KW_bowtype: pl->bowtype = (bowtype_t) f.get_sint32 (); break;
177 case KW_petmode: pl->petmode = (petmode_t) thawer.get_sint32 (); break; 194 case KW_petmode: pl->petmode = (petmode_t) f.get_sint32 (); break;
178 case KW_listening: thawer.get (pl->listening); break; 195 case KW_gender: f.get (pl->gender); break;
196 case KW_hintmode: f.get (pl->hintmode); break;
197 case KW_listening: /*TODO*/; break;
179 case KW_peaceful: thawer.get (pl->peaceful); break; 198 case KW_peaceful: f.get (pl->peaceful); break;
180 case KW_digestion: thawer.get (pl->digestion); break; 199 case KW_digestion: f.get (pl->digestion); break;
181 case KW_pickup: thawer.get (pl->mode); break; 200 case KW_pickup: f.get (pl->mode); break;
182 case KW_outputs_sync: thawer.get (pl->outputs_sync); break; 201 case KW_outputs_sync: f.get (pl->outputs_sync); break;
183 case KW_outputs_count: thawer.get (pl->outputs_count); break; 202 case KW_outputs_count: f.get (pl->outputs_count); break;
184 case KW_map: thawer.get (pl->maplevel); break; 203 case KW_map: f.get (pl->maplevel); break;
185 case KW_savebed_map: thawer.get (pl->savebed_map); break; 204 case KW_savebed_map: f.get (pl->savebed_map); break;
186 case KW_bed_x: thawer.get (pl->bed_x); break; 205 case KW_bed_x: f.get (pl->bed_x); break;
187 case KW_bed_y: thawer.get (pl->bed_y); break; 206 case KW_bed_y: f.get (pl->bed_y); break;
188 case KW_weapon_sp: thawer.get (pl->weapon_sp); break; 207 case KW_weapon_sp: f.get (pl->weapon_sp); break;
208 case KW_weapon_sp_left:f.get (pl->weapon_sp_left); break;
189 case KW_Str: thawer.get (pl->orig_stats.Str); break; 209 case KW_Str: f.get (pl->orig_stats.Str); break;
190 case KW_Dex: thawer.get (pl->orig_stats.Dex); break; 210 case KW_Dex: f.get (pl->orig_stats.Dex); break;
191 case KW_Con: thawer.get (pl->orig_stats.Con); break; 211 case KW_Con: f.get (pl->orig_stats.Con); break;
192 case KW_Int: thawer.get (pl->orig_stats.Int); break; 212 case KW_Int: f.get (pl->orig_stats.Int); break;
193 case KW_Pow: thawer.get (pl->orig_stats.Pow); break; 213 case KW_Pow: f.get (pl->orig_stats.Pow); break;
194 case KW_Wis: thawer.get (pl->orig_stats.Wis); break; 214 case KW_Wis: f.get (pl->orig_stats.Wis); break;
195 case KW_Cha: thawer.get (pl->orig_stats.Cha); break; 215 case KW_Cha: f.get (pl->orig_stats.Cha); break;
196 case KW_gen_hp: thawer.get (pl->gen_hp); break; 216 case KW_gen_hp: f.get (pl->gen_hp); break;
197 case KW_gen_sp: thawer.get (pl->gen_sp); break; 217 case KW_gen_sp: f.get (pl->gen_sp); break;
198 case KW_gen_grace: thawer.get (pl->gen_grace); break; 218 case KW_gen_grace: f.get (pl->gen_grace); break;
199 219
200 case KW_usekeys: 220 case KW_usekeys:
201 if (!strcmp (thawer.get_str (), "key_inventory")) 221 if (!strcmp (f.get_str (), "key_inventory"))
202 pl->usekeys = key_inventory; 222 pl->usekeys = key_inventory;
203 else if (!strcmp (thawer.get_str (), "keyrings")) 223 else if (!strcmp (f.get_str (), "keyrings"))
204 pl->usekeys = keyrings; 224 pl->usekeys = keyrings;
205 else if (!strcmp (thawer.get_str (), "containers")) 225 else if (!strcmp (f.get_str (), "containers"))
206 pl->usekeys = containers; 226 pl->usekeys = containers;
207 else 227 else
208 LOG (llevDebug, "load_player: got unknown usekeys type: %s\n", thawer.get_str ()); 228 LOG (llevDebug, "load_player: got unknown usekeys type: %s\n", f.get_str ());
209 break; 229 break;
210 230
211 case KW_unapply: 231 case KW_unapply:
212 if (!strcmp (thawer.get_str (), "unapply_nochoice")) 232 if (!strcmp (f.get_str (), "unapply_nochoice"))
213 pl->unapply = unapply_nochoice; 233 pl->unapply = unapply_nochoice;
214 else if (!strcmp (thawer.get_str (), "unapply_never")) 234 else if (!strcmp (f.get_str (), "unapply_never"))
215 pl->unapply = unapply_never; 235 pl->unapply = unapply_never;
216 else if (!strcmp (thawer.get_str (), "unapply_always")) 236 else if (!strcmp (f.get_str (), "unapply_always"))
217 pl->unapply = unapply_always; 237 pl->unapply = unapply_always;
218 else 238 else
219 LOG (llevDebug, "load_player: got unknown unapply type: %s\n", thawer.get_str ()); 239 LOG (llevDebug, "load_player: got unknown unapply type: %s\n", f.get_str ());
220 break; 240 break;
221 241
222 case KW_lev_array: 242 case KW_lev_array:
223 { 243 {
224 int count = thawer.get_sint32 (); 244 int count = f.get_sint32 ();
225 245
226 for (int i = 1; i <= count; i++) 246 for (int i = 1; i <= count; i++)
227 { 247 {
228 char line [32]; 248 char line [32];
229 249
230 fgets (line, 32, thawer); pl->levhp [i] = atoi (line); 250 fgets (line, 32, f); pl->levhp [i] = atoi (line);
231 fgets (line, 32, thawer); pl->levsp [i] = atoi (line); 251 fgets (line, 32, f); pl->levsp [i] = atoi (line);
232 fgets (line, 32, thawer); pl->levgrace[i] = atoi (line); 252 fgets (line, 32, f); pl->levgrace[i] = atoi (line);
233 } 253 }
234 } 254 }
235 break; 255 break;
236
237 default:
238 LOG (llevError, "%s: skipping unknown key in player header: %s\n", thawer.name, keyword_str [kw]);
239 break;
240 } 256 }
257
258 f.next ();
241 } 259 }
242 260
243done: 261failure:
244 if (!load_object (thawer, pl->ob, 0)) 262 LOG (llevError, "%s: too many or too grave errors, aborting player load.\n", f.name);
245 { 263
264 if (pl->ob)
246 pl->ob->destroy (); 265 pl->ob->destroy ();
266
247 pl->destroy (); 267 pl->destroy ();
248 return 0;
249 }
250 268
251 INVOKE_PLAYER (LOAD, pl);
252
253 return pl;
254}
255
256player *
257player::load_pl (const char *path)
258{
259 object_thawer thawer (path);
260
261 if (!thawer)
262 return 0; 269 return 0;
263
264 return load_pl (thawer);
265} 270}
266 271
272const_utf8_string
273player::killer_name () const
274{
275 static char buf [2048];
276
277 if (!killer)
278 return "a dungeon collapse";
279 else if (killer->contr)
280 snprintf (buf, sizeof (buf), "%s the %s", &killer->name,
281 *killer->contr->own_title ? killer->contr->own_title : killer->contr->title);
282 else if (killer->type == DEEP_SWAMP)
283 snprintf (buf, sizeof (buf), "drowning in a %s", &killer->name);
284 else if (killer->owner)
285 snprintf (buf, sizeof (buf), "%s's %s", &killer->outer_owner ()->name, &killer->name);
286 else
287 return &killer->name;
288
289 return buf;
290}
291
292// expand one level of cfpod
293// end == -2, till '\n'
294// end == -1, till '>'
295// end == 0, till eos
296// end >= 1, levels of " >>>"
297static const char *
298expand_cfpod (const player *pl, dynbuf_text &buf, const char *cfpod, int end = 0)
299{
300 static dynbuf_text nest(128, 128);
301
302 for (;;)
303 switch (char ch = *cfpod++)
304 {
305 case 0:
306 return cfpod - 1;
307
308 case ' ':
309 // verbatim block?
310 if (!buf.size () && !end)
311 {
312 --cfpod;
313 goto verbatim;
314 }
315
316 if (end < 1)
317 goto passthrough;
318
319 for (int i = 0; i < end; ++i)
320 if (cfpod [i] != '>')
321 goto passthrough;
322
323 return cfpod + end;
324
325 case '>':
326 if (end != -1)
327 {
328 buf << "&gt;";
329 break;
330 }
331
332 return cfpod;
333
334 case '&':
335 buf << "&amp;";
336 break;
337
338 case '<':
339 buf << "&lt;";
340 break;
341
342 case '\r':
343 buf << '\n';
344 /* \r\n+ becomes one or more newlines */
345 while (*cfpod == '\n')
346 buf << *cfpod++;
347 break;
348
349 case '\n':
350 if (end == -2)
351 return cfpod;
352
353 // \n\ => newline, tt
354 // \n\n => \n\n
355 // \n => single space
356
357 // two or more newlines will be copied verbatim, one is replaced by space
358 if (*cfpod == '\n')
359 {
360 --cfpod;
361 do
362 buf << *cfpod++;
363 while (*cfpod == '\n');
364 }
365 else if (*cfpod == ' ')
366 buf << '\n';
367 else
368 buf << ' ';
369
370 // verbatim block?
371 if (*cfpod == ' ')
372 goto verbatim;
373
374 break;
375
376 case 'B': case 'C': case 'E': case 'G':
377 case 'H': case 'I': case 'T': case 'U':
378 case 'Z':
379 {
380 int end = 0;
381
382 while (cfpod [end] == '<')
383 ++end;
384
385 if (!end)
386 goto passthrough;
387
388 if (end == 1 || cfpod [end] != ' ')
389 {
390 end = -1;
391 ++cfpod;
392 }
393 else
394 cfpod += end + 1;
395
396 const char *pfx, *sfx;
397
398 switch (ch)
399 {
400 case 'B': pfx = "<b>" ; sfx = "</b>" ; break;
401 case 'C': pfx = "<tt>" ; sfx = "</tt>" ; break;
402 case 'E': pfx = "&" ; sfx = ";" ; break;
403 case 'I': pfx = "<i>" ; sfx = "</i>" ; break;
404 case 'U': pfx = "<u>" ; sfx = "</u>" ; break;
405 case 'T': pfx = "<big><b>"; sfx = "</b></big>"; break;
406 case 'Z': pfx = "" ; sfx = "" ; break;
407
408 case 'H': // hint
409 {
410 if (pl->hintmode)
411 {
412 cfpod = expand_cfpod (pl, nest, cfpod, end);
413 nest.clear ();
414
415 if (pl->hintmode == 1)
416 buf << "<fg name=\"lightblue\">[Hint available]</fg>";
417 else
418 while (*cfpod <= ' '&& *cfpod) // eat trailing whitespace
419 ++cfpod;
420
421 goto skip;
422 }
423 else
424 {
425 pfx = "<fg name=\"lightblue\">[Hint: ";
426 sfx = "]</fg>";
427 }
428 }
429 break;
430
431 case 'G': // gender
432 {
433 cfpod = expand_cfpod (pl, nest, cfpod, end);
434 nest << '\0'; // zero-terminate
435
436 const char *str = nest.linearise ();
437 const char *sep = strchr (str, '|');
438
439 if (sep)
440 {
441 if (pl->gender)
442 buf.add (sep + 1, nest.size () - (sep - str) - 2);
443 else
444 buf.add (str, sep - str);
445 }
446
447 nest.clear ();
448
449 goto skip;
450 }
451
452 default:
453 pfx = "UNKNOWN_DIRECTIVE{";
454 sfx = "}";
455 break;
456 }
457
458 buf << pfx;
459 cfpod = expand_cfpod (pl, buf, cfpod, end);
460 buf << sfx;
461 }
462
463 break;
464
465 default:
466 passthrough:
467 buf << ch;
468 skip:
469 break;
470
471 verbatim:
472 // elements cannot currently span multiple lines
473 do
474 {
475 buf << "<tt>";
476 cfpod = expand_cfpod (pl, buf, cfpod, -2);
477 buf << "</tt>\n";
478
479 while (*cfpod == '\n')
480 buf << *cfpod++;
481 }
482 while (*cfpod == ' ');
483
484 break;
485 }
486}
487
488dynbuf_text *
489player::expand_cfpod (const char *cfpod) const
490{
491 static dynbuf_text buf(1024, 1024);
492 buf.clear ();
493
494 ::expand_cfpod (this, buf, cfpod);
495
496 return &buf;
497}
498

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines