--- deliantra/server/common/player.C 2007/01/06 14:42:29 1.13 +++ deliantra/server/common/player.C 2007/01/07 02:39:13 1.14 @@ -1,26 +1,26 @@ /* - CrossFire, A Multiplayer game for X-windows - - Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team - Copyright (C) 2002 Mark Wedel & Crossfire Development Team - Copyright (C) 1992 Frank Tore Johansen - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - - The authors can be reached via e-mail at -*/ + * CrossFire, A Multiplayer game for X-windows + * + * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team + * Copyright (C) 2002 Mark Wedel & Crossfire Development Team + * Copyright (C) 1992 Frank Tore Johansen + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * The authors can be reached via e-mail at + */ #include #include @@ -51,3 +51,217 @@ return 1; return 0; } + +/* + * If final is set, it a clean/final save, not a backup, ie dont remove objects from inventory + */ +bool +player::save_pl (object_freezer &freezer) +{ + INVOKE_PLAYER (SAVE, ob->contr); + + int wiz = ob->flag [FLAG_WIZ]; + + /* Eneq(@csd.uu.se): If we have an open container hide it. */ + object *container = ob->container; + ob->container = 0; + +#define PL_OUT(k) freezer.put (KW_ ## k, k) +#define PL_OUT2(k,v) freezer.put (KW_ ## k, v) + + PL_OUT (password); + PL_OUT2 (title, own_title); + PL_OUT (gen_hp); + PL_OUT (gen_sp); + PL_OUT (gen_grace); + PL_OUT (listening); + PL_OUT (shoottype); + PL_OUT (bowtype); + PL_OUT (petmode); + PL_OUT (peaceful); + PL_OUT (digestion); + PL_OUT2 (pickup, mode); + PL_OUT (outputs_sync); + PL_OUT (outputs_count); + + freezer.put (KW_usekeys, usekeys == key_inventory ? "key_inventory" : (usekeys == keyrings ? "keyrings" : "containers")); + freezer.put (KW_unapply, unapply == unapply_nochoice ? "unapply_nochoice" : (unapply == unapply_never ? "unapply_never" : "unapply_always")); + + if (ob->map) PL_OUT2 (map, ob->map->path); + + PL_OUT (savebed_map); + PL_OUT (bed_x); + PL_OUT (bed_y); + PL_OUT (weapon_sp); + PL_OUT2 (Str, orig_stats.Str); + PL_OUT2 (Dex, orig_stats.Dex); + PL_OUT2 (Con, orig_stats.Con); + PL_OUT2 (Int, orig_stats.Int); + PL_OUT2 (Pow, orig_stats.Pow); + PL_OUT2 (Wis, orig_stats.Wis); + PL_OUT2 (Cha, orig_stats.Cha); + + PL_OUT2 (lev_array, min (ob->level, 10)); + + for (int i = 1; i <= last_level && i <= 10; i++) + { + fprintf (freezer, "%d\n", levhp[i]); + fprintf (freezer, "%d\n", levsp[i]); + fprintf (freezer, "%d\n", levgrace[i]); + } + + freezer.put (ob->contr); + freezer.put (KW_endplst); + + SET_FLAG (ob, FLAG_NO_FIX_PLAYER); + CLEAR_FLAG (ob, FLAG_WIZ); + save_object (freezer, ob, 1); /* don't check and don't remove */ + + CLEAR_FLAG (ob, FLAG_NO_FIX_PLAYER); + + /* Eneq(@csd.uu.se): Reveal the container if we have one. */ + ob->container = container; + + ob->flag [FLAG_WIZ] = wiz; + + INVOKE_PLAYER (SAVE_DONE, ob->contr); + + return true; +} + +bool +player::save_pl (const char *path) +{ + object_freezer freezer; + + if (!save_pl (freezer)) + return false; + + return freezer.save (path); +} + +player * +player::load_pl (object_thawer &thawer) +{ + player *pl = new player; + + pl->last_save_time = time (0); + pl->savebed_map = first_map_path; + + /* Loop through the file, loading the rest of the values */ + for (;;) + { + keyword kw = thawer.get_kv (); + + switch (kw) + { + case KW_EOF: + LOG (llevError, "%s: unexpected EOF while reading player header\n", thawer.name); + delete pl; + return 0; + + case KW_ERROR: + LOG (llevError, "%s: error while reading player header, skipping (%s,%s)\n", thawer.name, thawer.last_keyword, thawer.last_value); + break; + + case KW_endplst: + goto done; + + case KW_oid: thawer.get (pl, thawer.get_sint32 ()); break; + case KW_password: assign (pl->password , thawer.get_str ()); break; + case KW_title: assign (pl->own_title, thawer.get_str ()); break; + case KW_shoottype: pl->shoottype = (rangetype) thawer.get_sint32 (); break; + case KW_bowtype: pl->bowtype = (bowtype_t) thawer.get_sint32 (); break; + case KW_petmode: pl->petmode = (petmode_t) thawer.get_sint32 (); break; + case KW_listening: thawer.get (pl->listening); break; + case KW_peaceful: thawer.get (pl->peaceful); break; + case KW_digestion: thawer.get (pl->digestion); break; + case KW_pickup: thawer.get (pl->mode); break; + case KW_outputs_sync: thawer.get (pl->outputs_sync); break; + case KW_outputs_count: thawer.get (pl->outputs_count); break; + case KW_map: thawer.get (pl->maplevel); break; + case KW_savebed_map: thawer.get (pl->savebed_map); break; + case KW_bed_x: thawer.get (pl->bed_x); break; + case KW_bed_y: thawer.get (pl->bed_y); break; + case KW_weapon_sp: thawer.get (pl->weapon_sp); break; + case KW_Str: thawer.get (pl->orig_stats.Str); break; + case KW_Dex: thawer.get (pl->orig_stats.Dex); break; + case KW_Con: thawer.get (pl->orig_stats.Con); break; + case KW_Int: thawer.get (pl->orig_stats.Int); break; + case KW_Pow: thawer.get (pl->orig_stats.Pow); break; + case KW_Wis: thawer.get (pl->orig_stats.Wis); break; + case KW_Cha: thawer.get (pl->orig_stats.Cha); break; + case KW_gen_hp: thawer.get (pl->gen_hp); break; + case KW_gen_sp: thawer.get (pl->gen_sp); break; + case KW_gen_grace: thawer.get (pl->gen_grace); break; + + case KW_usekeys: + if (!strcmp (thawer.get_str (), "key_inventory")) + pl->usekeys = key_inventory; + else if (!strcmp (thawer.get_str (), "keyrings")) + pl->usekeys = keyrings; + else if (!strcmp (thawer.get_str (), "containers")) + pl->usekeys = containers; + else + LOG (llevDebug, "load_player: got unknown usekeys type: %s\n", thawer.get_str ()); + break; + + case KW_unapply: + if (!strcmp (thawer.get_str (), "unapply_nochoice")) + pl->unapply = unapply_nochoice; + else if (!strcmp (thawer.get_str (), "unapply_never")) + pl->unapply = unapply_never; + else if (!strcmp (thawer.get_str (), "unapply_always")) + pl->unapply = unapply_always; + else + LOG (llevDebug, "load_player: got unknown unapply type: %s\n", thawer.get_str ()); + break; + + case KW_lev_array: + { + int count = thawer.get_sint32 (); + + for (int i = 1; i <= count; i++) + { + char line[128]; + + fgets (line, 128, thawer); pl->levhp [i] = atoi (line); + fgets (line, 128, thawer); pl->levsp [i] = atoi (line); + fgets (line, 128, thawer); pl->levgrace[i] = atoi (line); + } + } + break; + + default: + LOG (llevError, "%s: skipping unknown key in player header: %s\n", thawer.name, keyword_str [kw]); + break; + } + } + +done: + /* this loads the standard objects values. */ + pl->set_object (object::create ()); + + if (!load_object (thawer, pl->ob, 0)) + { + pl->ob->destroy (); + delete pl; + return 0; + } + + INVOKE_PLAYER (LOAD, pl); + + return pl; +} + +player * +player::load_pl (const char *path) +{ + object_thawer thawer (path); + + if (!thawer) + return 0; + + return load_pl (thawer); +} +