--- deliantra/server/common/arch.C 2007/06/04 13:04:00 1.63
+++ deliantra/server/common/arch.C 2008/05/04 11:12:40 1.79
@@ -1,45 +1,36 @@
/*
- * This file is part of Crossfire TRT, the Multiplayer Online Role Playing Game.
+ * This file is part of Deliantra, the Roguelike Realtime MMORPG.
*
- * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team
+ * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
* Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
* Copyright (©) 1992,2007 Frank Tore Johansen
*
- * Crossfire TRT 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.
+ * 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 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 GNU General Public License
- * for more details.
+ * 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 Crossfire TRT; if not, write to the Free Software Foundation, Inc. 51
- * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
*
- * The authors can be reached via e-mail to
+ * The authors can be reached via e-mail to
*/
#include
#include
-#include
#include
#include
#include
-/* The naming of these functions is really poor - they are all
- * pretty much named '.._arch_...', but they may more may not
- * return archetypes. Some make the arch_to_object call, and thus
- * return an object. Perhaps those should be called 'archob' functions
- * to denote they return an object derived from the archetype.
- * MSW 2003-04-29
- */
-
-bool loading_arch; // ugly flag to object laoder etc. to suppress/request special processing
+archetype *loading_arch; // ugly flag to object loader etc. to suppress/request special processing
+arch_ptr archetype::empty;
// the hashtable
typedef std::tr1::unordered_map
@@ -51,8 +42,15 @@
slice_allocator< std::pair >
> HT;
-static HT ht (5000);
+static HT ht (10000);
archvec archetypes;
+static unordered_vector allarch;
+static int dirtycnt;
+
+// the vector of other_arch references to be resolved
+static std::vector< std::pair > postponed_arch_ref;
+// the vector of loaded but not yet committed archetypes
+static std::vector postponed_arch;
/**
* GROS - This function retrieves an archetype given the name that appears
@@ -71,7 +69,7 @@
shstr_cmp name_cmp (name);
for_all_archetypes (at)
- if (at->archname == name_cmp)
+ if (at->name == name_cmp)
return at;
return 0;
@@ -88,7 +86,7 @@
shstr_cmp name_cmp (name);
for_all_archetypes (at)
- if (at->archname == name_cmp && at->type == type)
+ if (at->name == name_cmp && at->type == type)
return at;
return 0;
@@ -137,7 +135,7 @@
* - a corresponding object if found; a singularity object if not found.
* Note by MSW - it appears that it takes the full name and keeps
* shortening it until it finds a match. I re-wrote this so that it
- * doesn't malloc it each time - not that this function is used much,
+ * doesn't allocate it each time - not that this function is used much,
* but it otherwise had a big memory leak.
*/
object *
@@ -159,23 +157,23 @@
return create_singularity (name);
}
- /* This is a subset of the parse_id command. Basically, name can be
- * a string seperated lists of things to match, with certain keywords.
- * pl is the player (only needed to set count properly)
- * op is the item we are trying to match. Calling function takes care
- * of what action might need to be done and if it is valid
- * (pickup, drop, etc.) Return NONZERO if we have a match. A higher
- * value means a better match. 0 means no match.
- *
- * Brief outline of the procedure:
- * We take apart the name variable into the individual components.
- * cases for 'all' and unpaid are pretty obvious.
- * Next, we check for a count (either specified in name, or in the
- * player object.)
- * If count is 1, make a quick check on the name.
- * IF count is >1, we need to make plural name. Return if match.
- * Last, make a check on the full name.
- */
+/* This is a subset of the parse_id command. Basically, name can be
+ * a string seperated lists of things to match, with certain keywords.
+ * pl is the player (only needed to set count properly)
+ * op is the item we are trying to match. Calling function takes care
+ * of what action might need to be done and if it is valid
+ * (pickup, drop, etc.) Return NONZERO if we have a match. A higher
+ * value means a better match. 0 means no match.
+ *
+ * Brief outline of the procedure:
+ * We take apart the name variable into the individual components.
+ * cases for 'all' and unpaid are pretty obvious.
+ * Next, we check for a count (either specified in name, or in the
+ * player object.)
+ * If count is 1, make a quick check on the name.
+ * IF count is >1, we need to make plural name. Return if match.
+ * Last, make a check on the full name.
+ */
int
item_matched_string (object *pl, object *op, const char *name)
{
@@ -279,7 +277,6 @@
archetype::archetype (const char *name)
{
- stub = true;
arch = this;
this->archname = this->name = this->name_pl = name;
}
@@ -292,19 +289,21 @@
void
archetype::link ()
{
- ht.insert (std::make_pair (archname, this));
-
if (!archetypes.contains (this))
- archetypes.insert (this);
+ {
+ archetypes.insert (this);
+ ht.insert (std::make_pair (archname, this));
+ }
}
void
archetype::unlink ()
{
- ht.erase (archname);
-
if (archetypes.contains (this))
- archetypes.erase (this);
+ {
+ archetypes.erase (this);
+ ht.erase (archname);
+ }
}
/*
@@ -326,50 +325,17 @@
}
archetype *
-archetype::get (const char *name)
-{
- if (!name)
- {
- LOG (llevError, "null archetype requested\n");
- name = "(null)";
- }
-
- archetype *at = find (name);
-
- if (loading_arch && at && !at->stub)
- {
- at->unlink ();
- at = 0;
- }
-
- if (!at || (!at->stub && loading_arch))
- {
- at = new archetype (name);
- at->link ();
- }
-
- return at;
-}
-
-archetype *
archetype::read (object_thawer &f)
{
assert (f.kw == KW_object);
- loading_arch = true; // hack to tell parse_kv et al. to behave
-
std::vector parts;
- coroapi::cede_to_tick_every (10);
+ coroapi::cede_to_tick ();
for (;;)
{
- // the archetype might have been referenced earlier
- // so try to find an existing stub archetype first
- archetype *at = find (f.get_str ());
-
- if (!at || !at->stub)
- at = new archetype (f.get_str ());
+ archetype *at = new archetype (f.get_str ());
f.next ();
@@ -390,10 +356,16 @@
}
#endif
- if (!at->parse_kv (f))
+ loading_arch = at; // hack to tell parse_kv et al. to behave
+ bool parse_ok = at->parse_kv (f);
+ loading_arch = 0;
+
+ if (!parse_ok)
goto fail;
+ loading_arch = at; // hack to tell parse_kv et al. to behave
at->post_load_check ();
+ loading_arch = 0;
parts.push_back (at);
@@ -409,16 +381,18 @@
}
}
- loading_arch = false;
-
{
auto (at, parts.begin ());
archetype *new_head = parts.front ();
archetype *old_head = find (new_head->archname);
- for (auto (at, parts.begin ()); at != parts.end (); ++at)
- fprintf (stderr, "%d: %s\n", at - parts.begin(), &(*at)->archname);//D
+ if (old_head && !old_head->is_head ())
+ {
+ LOG (llevError, "%s: unable to overwrite non-head archetype '%s' with head archetype, skipping.\n",
+ &new_head->archname, &old_head->archname);
+ goto fail;
+ }
// check that all archetypes belong to the same old object or are new
for (auto (at, parts.begin ()); at != parts.end (); ++at)
@@ -428,46 +402,40 @@
if (old_part && old_part->head_ () != old_head)
{
- fprintf (stderr, "nhn %s\n", &new_head->archname);//D
- fprintf (stderr, "old_head %p,%p new_head %p,%p ph %p\n", old_head, old_part, new_head, new_part, old_part->head_());
LOG (llevError, "%s: unable to overwrite archetype '%s' with archetype of different object, skipping.\n",
&new_part->archname, &((archetype *)old_part->head_ ())->archname);
goto fail;
}
}
- fprintf (stderr, "oh %p %s\n", old_head, &new_head->archname);//D
-
- // deactivate existing archetype
- for (archetype *at = old_head; at; at = (archetype *)at->more)
- at->unlink ();
-
- // reassemble new chain
+ // assemble new chain
new_head->min_x = new_head->max_x = new_head->x;
new_head->min_y = new_head->max_y = new_head->y;
archetype *less = new_head;
- archetype *head = 0;
for (auto (p, parts.begin () + 1); p != parts.end (); ++p)
{
archetype *at = *p;
- if (at->x < new_head->min_x) new_head->min_x = at->x;
- if (at->y < new_head->min_y) new_head->min_y = at->y;
- if (at->x > new_head->max_x) new_head->max_x = at->x;
- if (at->y > new_head->max_y) new_head->max_y = at->y;
-
- at->head = head; head = new_head;
- less->more = at; less = at;
+ // some flags get inherited from the head (probably a lot more)
+ // doing it here doesn't feel too cozy, but it allows code
+ // to ignore head checks for these flags, which saves time
+ at->flag [FLAG_ALIVE] = new_head->flag [FLAG_ALIVE];
+ at->flag [FLAG_NO_PICK] = new_head->flag [FLAG_NO_PICK];
+ at->flag [FLAG_MONSTER] = new_head->flag [FLAG_MONSTER];
+ at->flag [FLAG_IS_FLOOR] = new_head->flag [FLAG_IS_FLOOR];
+
+ new_head->min_x = min (new_head->min_x, at->x);
+ new_head->min_y = min (new_head->min_y, at->y);
+ new_head->max_x = max (new_head->max_x, at->x);
+ new_head->max_y = max (new_head->max_y, at->y);
+
+ at->head = new_head;
+ less->more = at;
+ less = at;
}
- // now activate it
- for (auto (p, parts.begin ()); p != parts.end (); ++p)
- {
- archetype *at = *p;
- at->stub = false;
- at->link ();
- }
+ postponed_arch.insert (postponed_arch.end (), parts.begin (), parts.end ());
return new_head;
}
@@ -476,21 +444,92 @@
for (auto (p, parts.begin ()); p != parts.end (); ++p)
(*p)->destroy (true);
- loading_arch = false;
return 0;
}
-/*
- * Initialize global archtype pointers:
- */
void
-init_archetype_pointers ()
+archetype::postpone_arch_ref (arch_ptr &ref, const_utf8_string other_arch)
{
- ring_arch = archetype::find ("ring");
- amulet_arch = archetype::find ("amulet");
- staff_arch = archetype::find ("staff");
- crown_arch = archetype::find ("crown");
- empty_archetype = archetype::find ("empty_archetype");
+ ref = 0;
+ postponed_arch_ref.push_back (std::pair(&ref, shstr (other_arch)));
+}
+
+void
+archetype::commit_load ()
+{
+ // unlink old archetypes and link in new ones */
+ for (auto (p, postponed_arch.begin ()); p != postponed_arch.end (); ++p)
+ {
+ archetype *at = *p;
+
+ if (archetype *old = find (at->archname))
+ old->unlink ();
+
+ allarch.push_back (at);
+
+ at->link ();
+ ++dirtycnt;
+ }
+
+ postponed_arch.clear ();
+
+ // now resolve arch references
+ for (auto (p, postponed_arch_ref.begin ()); p != postponed_arch_ref.end (); ++p)
+ {
+ arch_ptr *ap = p->first;
+ archetype *at = find (p->second);
+
+ if (!at)
+ LOG (llevError, "unable to resolve postponed arch reference to '%s'", &p->second);
+
+ *ap = at;
+ }
+
+ postponed_arch_ref.clear ();
+
+ empty = find (shstr_empty_archetype);
+}
+
+void
+archetype::gc ()
+{
+ int cnt = max (1, min (allarch.size () / 128, dirtycnt));
+ dirtycnt = max (0, dirtycnt - cnt);
+
+ do
+ {
+ static int idx;
+
+ if (idx >= allarch.size ())
+ if (idx)
+ idx = 0;
+ else
+ return;
+
+ archetype *at = allarch [idx];
+
+ if (at->refcnt_cnt () > 1) // all arches have ONE refcount from their object
+ ++idx;
+ else
+ {
+ LOG (llevDebug, "garbage collect arch %s", at->debug_desc ());
+ assert (at->arch == at); // verify that refcnt == 1 is truly valid
+ allarch.erase (idx);
+
+ // break chain
+ for (object *op = at->head_ (); op; )
+ {
+ object *next = op->more;
+ op->head = 0;
+ op->more = 0;
+ op = next;
+ }
+
+ at->destroy ();
+ at->arch = 0;
+ }
+ }
+ while (--cnt);
}
/*
@@ -513,6 +552,12 @@
return op;
}
+object *
+archetype::instance ()
+{
+ return arch_to_object (this);
+}
+
/*
* Creates an object. This function is called by get_archetype()
* if it fails to find the appropriate archetype.
@@ -522,13 +567,17 @@
object *
create_singularity (const char *name)
{
- object *op;
+ LOG (llevError | logBacktrace, "FATAL: creating singularity for '%s'.\n", name);
+
+ if (!strcmp (name, "bug"))
+ abort ();
+
char buf[MAX_BUF];
+ sprintf (buf, "bug, please report (%s)", name);
- sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name);
- op = object::create ();
+ object *op = get_archetype ("bug");
op->name = op->name_pl = buf;
- SET_FLAG (op, FLAG_NO_PICK);
+
return op;
}
@@ -547,6 +596,12 @@
return arch_to_object (at);
}
+object *
+archetype::get (const char *name)
+{
+ return get_archetype (name);
+}
+
/*
* Returns the first archetype using the given type.
* Used in treasure-generation.