--- deliantra/server/common/region.C 2007/01/29 14:46:01 1.19 +++ deliantra/server/common/region.C 2009/01/01 20:49:48 1.42 @@ -1,38 +1,39 @@ /* - * CrossFire, A Multiplayer game for X-windows + * This file is part of Deliantra, the Roguelike Realtime MMORPG. * - * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team - * Copyright (C) 2001-2003 Mark Wedel & Crossfire Development Team - * Copyright (C) 1992 Frank Tore Johansen + * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team + * Copyright (©) 2001-2003,2007 Mark Wedel & Crossfire Development Team + * Copyright (©) 1992,2007 Frank Tore Johansen * - * This program is free software; you can redistribute it and/or modify + * Deliantra 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 + * the Free Software Foundation, either version 3 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 + * 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 + * along with this program. If not, see . + * + * The authors can be reached via e-mail to */ #include #include +regionvec regions; + region * region::default_region () { - for (region *reg = first_region; reg; reg = reg->next) - if (reg->fallback) - return reg; + for_all_regions (rgn) + if (rgn->fallback) + return rgn; - return first_region; + return regions [0]; } /* @@ -44,92 +45,25 @@ * used by the map parsing code. */ region * -region::find (const char *name) +region::find (shstr_cmp name) { - for (region *reg = first_region; reg; reg = reg->next) - if (!strcmp (reg->name, name)) - return reg; + for_all_regions (rgn) + if (rgn->name == name) + return rgn; - LOG (llevError, "region called %s requested, but not found, using fallback.\n", name); + LOG (llevError, "region called %s requested, but not found, using fallback.\n", &name); return default_region (); } -/* - * Tries to find a region that 'name' corresponds to. - * It looks, in order, for: - * an exact match to region name (case insensitive) - * an exact match to longname (case insensitive) - * a substring that matches to the longname (eg Kingdom) - * a substring that matches to the region name (eg nav) - * if it can find none of these it returns the first parentless region - * (there should be only one of these - the top level one) - * If we got a NULL, then just return the top level region - * - */ -region * -region::find_fuzzy (const char *name) +void +region::do_destroy () { - region *reg; - char *substr; - char *p; + regions.erase (this); - if (name == NULL) - { - for (reg = first_region; reg->parent; reg = reg->parent) - ; - - return reg; - } + attachable::do_destroy (); - p = strchr (name, '\n'); - if (p) - *p = '\0'; - - for (reg = first_region; reg; reg = reg->next) - if (!strcasecmp (reg->name, name)) - return reg; - - for (reg = first_region; reg; reg = reg->next) - if (reg->longname) - if (!strcasecmp (reg->longname, name)) - return reg; - - substr = NULL; - for (reg = first_region; reg; reg = reg->next) - if (reg->longname) - { - substr = strstr (reg->longname, name); - if (substr) - return reg; - } - - for (reg = first_region; reg; reg = reg->next) - if (reg->longname) - { - /* - * This is not a bug, we want the region that is most identifiably a discrete - * area in the game, eg if we have 'scor', we want to return 'scorn' and not - * 'scornarena', regardless of their order on the list so we only look at those - * regions with a longname set. - */ - substr = strstr (reg->name, name); - if (substr) - return reg; - } - - for (reg = first_region; reg; reg = reg->next) - { - substr = strstr (reg->name, name); - if (substr) - return reg; - } - - /* if we are still here, we are going to have to give up, and give the top level region */ - for (reg = first_region; reg->parent; reg = reg->parent) - ; - - return reg; + refcnt_dec (); } /* @@ -137,24 +71,22 @@ * otherwise returns 0 * if passed a NULL region returns -1 */ - static int region_is_child_of_region (const region * child, const region * r) { - - if (r == NULL) + if (!r) return -1; - if (child == NULL) + if (!child) return 0; - if (!strcmp (child->name, r->name)) + if (child->name == r->name) return 1; - else if (child->parent != NULL) + if (child->parent) return region_is_child_of_region (child->parent, r); - else - return 0; + + return 0; } /** Returns an object which is an exit through which the player represented by op should be @@ -165,21 +97,18 @@ object * get_jail_exit (object *op) { - region *reg; - object *exit; - if (op->type != PLAYER) { LOG (llevError, "region.c: get_jail_exit called against non-player object.\n"); return NULL; } - reg = op->region (); + region *reg = op->region (); while (reg) { if (reg->jailmap) { - exit = object::create (); + object *exit = object::create (); EXIT_PATH (exit) = reg->jailmap; /* damned exits reset savebed and remove teleports, so the prisoner can't escape */ SET_FLAG (exit, FLAG_DAMNED); @@ -191,204 +120,93 @@ reg = reg->parent; } - LOG (llevDebug, "No suitable jailmap for region %s was found.\n", ®->name); - return NULL; + LOG (llevError, "No suitable jailmap for region %s was found.\n", ®->name); + + return 0; } -static void -assign_region_parents (void) +region * +region::read (object_thawer &f) { - region *reg; - uint32 parent_count = 0; - uint32 region_count = 0; + assert (f.kw == KW_region); + + region *rgn = new region; + rgn->refcnt_inc (); + + f.get (rgn->name); + f.next (); - for (reg = first_region; reg && reg->next; reg = reg->next) + for (;;) { - if (reg->parent_name) + switch (f.kw) { - reg->parent = region::find (reg->parent_name); - parent_count++; + case KW_parent: + rgn->parent = region::find (f.get_str ()); + break; + + case KW_msg: f.get_ml (KW_endmsg, rgn->msg); break; + case KW_longname: f.get (rgn->longname); break; + case KW_jail_map: f.get (rgn->jailmap); break; + case KW_jail_x: f.get (rgn->jailx); break; + case KW_jail_y: f.get (rgn->jaily); break; + case KW_portal_map: f.get (rgn->portalmap);break; + case KW_portal_x: f.get (rgn->portalx); break; + case KW_portal_y: f.get (rgn->portaly); break; + case KW_fallback: f.get (rgn->fallback); break; + case KW_chance: f.get (rgn->treasure_density); break; + + case KW_randomitems: + rgn->treasure = treasurelist::get (f.get_str ()); + break; + + case KW_end: + f.next (); + + // cannot use find as that will request the default region + for_all_regions (old) + if (old->name == rgn->name) + { + old->destroy (); + break; + } + + // just append + regions.push_back (rgn); + return rgn; + + case KW_ERROR: + rgn->set_key_text (f.kw_str, f.value); + //fprintf (stderr, "region addkv(%s,%s)\n", f.kw_str, f.value);//D + break; + + default: + if (!f.parse_error ("region", rgn->name)) + { + rgn->destroy (); + return 0; + } + break; } - region_count++; + f.next (); } - - LOG (llevDebug, "Assigned %u regions with %u parents.\n", region_count, parent_count); } /* * First initialises the archtype hash-table (init_archetable()). * Reads and parses the archetype file (with the first and second-pass * functions). - * Then initialises treasures by calling load_treasures(). */ void init_regions (void) { - FILE *fp; - char filename[MAX_BUF]; - int comp; - - if (first_region != NULL) /* Only do this once */ - return; - - // make sure one region is always available - first_region = new region; - first_region->name = ""; - first_region->longname = strdup ("Built-in Region"); - - sprintf (filename, "%s/%s/%s", settings.datadir, settings.mapdir, settings.regions); - LOG (llevDebug, "Reading regions from %s...\n", filename); - if ((fp = open_and_uncompress (filename, 0, &comp)) == NULL) + if (!regions.size ()) { - LOG (llevError, " Can't open regions file %s in init_regions.\n", filename); - return; - } - - parse_regions (fp); - assign_region_parents (); - LOG (llevDebug, " done\n"); - - close_and_delete (fp, comp); -} - -/* - * Reads/parses the region file, and copies into a linked list - * of region structs. - */ -void -parse_regions (FILE * fp) -{ - region *newreg; - region *reg; - - char buf[HUGE_BUF], msgbuf[HUGE_BUF], *key = NULL, *value, *end; - int msgpos = 0; - - newreg = NULL; - while (fgets (buf, HUGE_BUF - 1, fp) != NULL) - { - buf[HUGE_BUF - 1] = 0; - key = buf; - while (isspace (*key)) - key++; - if (*key == 0) - continue; /* empty line */ - value = strchr (key, ' '); - if (!value) - { - end = strchr (key, '\n'); - *end = 0; - } - else - { - *value = 0; - value++; - while (isspace (*value)) - value++; - end = strchr (value, '\n'); - } - - /* - * This is a bizzare mutated form of the map and archetype parser - * rolled into one. Key is the field name, value is what it should - * be set to. - * We've already done the work to null terminate key, - * and strip off any leading spaces for both of these. - * We have not touched the newline at the end of the line - - * these might be needed for some values. the end pointer - * points to the first of the newlines. - * value could be NULL! It would be easy enough to just point - * this to "" to prevent cores, but that would let more errors slide - * through. - */ - if (!strcmp (key, "region")) - { - *end = 0; - newreg = new region; - newreg->name = value; - } - else if (!strcmp (key, "parent")) - { - /* - * Note that this is in the initialisation code, so we don't actually - * assign the pointer to the parent yet, because it might not have been - * parsed. - */ - *end = 0; - newreg->parent_name = value; - } - else if (!strcmp (key, "longname")) - { - *end = 0; - newreg->longname = strdup (value); - } - else if (!strcmp (key, "jail")) - { - /* jail entries are of the form: /path/to/map x y */ - char path[MAX_BUF]; - int x, y; - - if (sscanf (value, "%[^ ] %d %d\n", path, &x, &y) != 3) - { - LOG (llevError, "region.c: malformated regions entry: jail %s\n", value); - continue; - } - newreg->jailmap = strdup (path); - newreg->jailx = x; - newreg->jaily = y; - } - else if (!strcmp (key, "msg")) - { - while (fgets (buf, HUGE_BUF - 1, fp) != NULL) - { - if (!strcmp (buf, "endmsg\n")) - break; - else - { - strcpy (msgbuf + msgpos, buf); - msgpos += strlen (buf); - } - } - /* - * There may be regions with empty messages (eg, msg/endmsg - * with nothing between). When maps are loaded, this is done - * so better do it here too... - */ - if (msgpos != 0) - newreg->msg = strdup (msgbuf); - - /* we have to reset msgpos, or the next region will store both msg blocks. */ - msgpos = 0; - } - else if (!strcmp (key, "fallback")) - { - *end = 0; - newreg->fallback = atoi (value); - } - else if (!strcmp (key, "end")) - { - /* Place this new region last on the list, if the list is empty put it first */ - for (reg = first_region; reg != NULL && reg->next != NULL; reg = reg->next); - - if (reg == NULL) - first_region = newreg; - else - reg->next = newreg; - newreg = NULL; - } - else if (!strcmp (key, "nomore")) - { - /* we have reached the end of the region specs.... */ - break; - } - else - { - /* we should never get here, if we have, then something is wrong */ - LOG (llevError, "Got unknown value in region file: %s %s\n", key, value); - } + // make sure one region is always available + region *rgn = new region; + rgn->name = ""; + rgn->longname = "Built-in Region"; + regions.push_back (rgn); } - if (!key || strcmp (key, "nomore")) - LOG (llevError, "Got premature eof on regions file!\n"); }