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.11 by root, Thu Dec 21 23:37:05 2006 UTC vs.
Revision 1.63 by root, Sat Jan 30 23:30:26 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 (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
4 Copyright (C) 2002 Mark Wedel & Crossfire Development Team 5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
5 Copyright (C) 1992 Frank Tore Johansen 6 * Copyright (©) 1992,2007 Frank Tore Johansen
6 7 *
7 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
8 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
9 the Free Software Foundation; either version 2 of the License, or 10 * Free Software Foundation, either version 3 of the License, or (at your
10 (at your option) any later version. 11 * 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 Affero GNU General Public License
18 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
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 * <http://www.gnu.org/licenses/>.
20 21 *
21 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>
22*/ 23 */
23 24
24#include <global.h> 25#include <global.h>
25#include <funcpoint.h>
26 26
27void 27//+GPL
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->ns)
55 {
56 pl->ns->send_packet ("goodbye");
57 pl->ns->flush ();
58 pl->ns->pl = 0;
59 pl->ns->destroy ();
60 }
61
62 /* Clear item stack */
63 if (pl->stack_items)
64 free (pl->stack_items);
65
66 delete pl;
67}
68
69 28
70/* Determine if the attacktype represented by the 29/* Determine if the attacktype represented by the
71 * specified attack-number is enabled for dragon players. 30 * specified attack-number is enabled for dragon players.
72 * A dragon player (quetzal) can gain resistances for 31 * A dragon player (quetzal) can gain resistances for
73 * all enabled attacktypes. 32 * all enabled attacktypes.
81 40
82 return 0; 41 return 0;
83} 42}
84 43
85/* 44/*
86 * returns true if the adressed object 'ob' is a player 45 * If final is set, it a clean/final save, not a backup, ie dont remove objects from inventory
87 * of the dragon race.
88 */ 46 */
89int 47bool
90is_dragon_pl (const object *op) 48player::save_pl (object_freezer &freezer)
91{ 49{
92 if (op != NULL && op->type == PLAYER && op->arch != NULL && op->arch->clone.race != NULL && strcmp (op->arch->clone.race, "dragon") == 0) 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 CLEAR_FLAG (ob, 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
123bool
124player::save_pl (const char *path)
125{
126 object_freezer freezer;
127
128 if (!save_pl (freezer))
93 return 1; 129 return false;
130
131 return freezer.save (path);
132}
133
134player *
135player::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 case KW_Str: f.get (pl->orig_stats.Str); break;
194 case KW_Dex: f.get (pl->orig_stats.Dex); break;
195 case KW_Con: f.get (pl->orig_stats.Con); break;
196 case KW_Int: f.get (pl->orig_stats.Int); break;
197 case KW_Pow: f.get (pl->orig_stats.Pow); break;
198 case KW_Wis: f.get (pl->orig_stats.Wis); break;
199 case KW_Cha: f.get (pl->orig_stats.Cha); break;
200 case KW_gen_hp: f.get (pl->gen_hp); break;
201 case KW_gen_sp: f.get (pl->gen_sp); break;
202 case KW_gen_grace: f.get (pl->gen_grace); break;
203
204 case KW_usekeys:
205 if (!strcmp (f.get_str (), "key_inventory"))
206 pl->usekeys = key_inventory;
207 else if (!strcmp (f.get_str (), "keyrings"))
208 pl->usekeys = keyrings;
209 else if (!strcmp (f.get_str (), "containers"))
210 pl->usekeys = containers;
211 else
212 LOG (llevDebug, "load_player: got unknown usekeys type: %s\n", f.get_str ());
213 break;
214
215 case KW_unapply:
216 if (!strcmp (f.get_str (), "unapply_nochoice"))
217 pl->unapply = unapply_nochoice;
218 else if (!strcmp (f.get_str (), "unapply_never"))
219 pl->unapply = unapply_never;
220 else if (!strcmp (f.get_str (), "unapply_always"))
221 pl->unapply = unapply_always;
222 else
223 LOG (llevDebug, "load_player: got unknown unapply type: %s\n", f.get_str ());
224 break;
225
226 case KW_lev_array:
227 {
228 int count = f.get_sint32 ();
229
230 for (int i = 1; i <= count; i++)
231 {
232 char line [32];
233
234 fgets (line, 32, f); pl->levhp [i] = atoi (line);
235 fgets (line, 32, f); pl->levsp [i] = atoi (line);
236 fgets (line, 32, f); pl->levgrace[i] = atoi (line);
237 }
238 }
239 break;
240 }
241
242 f.next ();
243 }
244
245failure:
246 LOG (llevError, "%s: too many or too grave errors, aborting player load.\n", f.name);
247
248 if (pl->ob)
249 pl->ob->destroy ();
250
251 pl->destroy ();
252
94 return 0; 253 return 0;
95} 254}
255
256const_utf8_string
257player::killer_name () const
258{
259 static char buf [2048];
260
261 if (!killer)
262 return "a dungeon collapse";
263 else if (killer->contr)
264 snprintf (buf, sizeof (buf), "%s the %s", &killer->name,
265 *killer->contr->own_title ? killer->contr->own_title : killer->contr->title);
266 else if (killer->type == DEEP_SWAMP)
267 snprintf (buf, sizeof (buf), "drowning in a %s", &killer->name);
268 else if (killer->owner)
269 snprintf (buf, sizeof (buf), "%s's %s", &killer->outer_owner ()->name, &killer->name);
270 else
271 return &killer->name;
272
273 return buf;
274}
275
276// expand one level of cfpod
277// end == -2, till '\n'
278// end == -1, till '>'
279// end == 0, till eos
280// end >= 1, levels of " >>>"
281static const char *
282expand_cfpod (const player *pl, dynbuf_text &buf, const char *cfpod, int end = 0)
283{
284 static dynbuf_text nest(128, 128);
285
286 for (;;)
287 switch (char ch = *cfpod++)
288 {
289 case 0:
290 return cfpod - 1;
291
292 case ' ':
293 // verbatim block?
294 if (!buf.size () && !end)
295 {
296 --cfpod;
297 goto verbatim;
298 }
299
300 if (end < 1)
301 goto passthrough;
302
303 for (int i = 0; i < end; ++i)
304 if (cfpod [i] != '>')
305 goto passthrough;
306
307 return cfpod + end;
308
309 case '>':
310 if (end != -1)
311 {
312 buf << "&gt;";
313 break;
314 }
315
316 return cfpod;
317
318 case '&':
319 buf << "&amp;";
320 break;
321
322 case '<':
323 buf << "&lt;";
324 break;
325
326 case '\r':
327 buf << '\n';
328 /* \r\n+ becomes one or more newlines */
329 while (*cfpod == '\n')
330 buf << *cfpod++;
331 break;
332
333 case '\n':
334 if (end == -2)
335 return cfpod;
336
337 // \n\ => newline, tt
338 // \n\n => \n\n
339 // \n => single space
340
341 // two or more newlines will be copied verbatim, one is replaced by space
342 if (*cfpod == '\n')
343 {
344 --cfpod;
345 do
346 buf << *cfpod++;
347 while (*cfpod == '\n');
348 }
349 else if (*cfpod == ' ')
350 buf << '\n';
351 else
352 buf << ' ';
353
354 // verbatim block?
355 if (*cfpod == ' ')
356 goto verbatim;
357
358 break;
359
360 case 'B': case 'C': case 'E': case 'G':
361 case 'H': case 'I': case 'T': case 'U':
362 case 'Z':
363 {
364 int end = 0;
365
366 while (cfpod [end] == '<')
367 ++end;
368
369 if (!end)
370 goto passthrough;
371
372 if (end == 1 || cfpod [end] != ' ')
373 {
374 end = -1;
375 ++cfpod;
376 }
377 else
378 cfpod += end + 1;
379
380 const char *pfx, *sfx;
381
382 switch (ch)
383 {
384 case 'B': pfx = "<b>" ; sfx = "</b>" ; break;
385 case 'C': pfx = "<tt>" ; sfx = "</tt>" ; break;
386 case 'E': pfx = "&" ; sfx = ";" ; break;
387 case 'I': pfx = "<i>" ; sfx = "</i>" ; break;
388 case 'U': pfx = "<u>" ; sfx = "</u>" ; break;
389 case 'T': pfx = "<big><b>"; sfx = "</b></big>"; break;
390 case 'Z': pfx = "" ; sfx = "" ; break;
391
392 case 'H': // hint
393 {
394 if (pl->hintmode)
395 {
396 cfpod = expand_cfpod (pl, nest, cfpod, end);
397 nest.clear ();
398
399 if (pl->hintmode == 1)
400 buf << "<fg name=\"lightblue\">[Hint available]</fg>";
401 else
402 while (*cfpod <= ' '&& *cfpod) // eat trailing whitespace
403 ++cfpod;
404
405 goto skip;
406 }
407 else
408 {
409 pfx = "<fg name=\"lightblue\">[Hint: ";
410 sfx = "]</fg>";
411 }
412 }
413 break;
414
415 case 'G': // gender
416 {
417 cfpod = expand_cfpod (pl, nest, cfpod, end);
418 nest << '\0'; // zero-terminate
419
420 const char *str = nest.linearise ();
421 const char *sep = strchr (str, '|');
422
423 if (sep)
424 {
425 if (pl->gender)
426 buf.add (sep + 1, nest.size () - (sep - str) - 2);
427 else
428 buf.add (str, sep - str);
429 }
430
431 nest.clear ();
432
433 goto skip;
434 }
435
436 default:
437 pfx = "UNKNOWN_DIRECTIVE{";
438 sfx = "}";
439 break;
440 }
441
442 buf << pfx;
443 cfpod = expand_cfpod (pl, buf, cfpod, end);
444 buf << sfx;
445 }
446
447 break;
448
449 default:
450 passthrough:
451 buf << ch;
452 skip:
453 break;
454
455 verbatim:
456 // elements cannot currently span multiple lines
457 do
458 {
459 buf << "<tt>";
460 cfpod = expand_cfpod (pl, buf, cfpod, -2);
461 buf << "</tt>\n";
462
463 while (*cfpod == '\n')
464 buf << *cfpod++;
465 }
466 while (*cfpod == ' ');
467
468 break;
469 }
470}
471
472dynbuf_text *
473player::expand_cfpod (const char *cfpod) const
474{
475 static dynbuf_text buf(1024, 1024);
476 buf.clear ();
477
478 ::expand_cfpod (this, buf, cfpod);
479
480 return &buf;
481}
482

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines