ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/player.C
Revision: 1.74
Committed: Wed Nov 21 13:06:10 2012 UTC (11 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-3_1
Changes since 1.73: +1 -0 lines
Log Message:
*** empty log message ***

File Contents

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