ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/arch.C
Revision: 1.117
Committed: Sun Nov 18 00:37:11 2018 UTC (5 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.116: +10 -14 lines
Log Message:
some range based for loops

File Contents

# User Rev Content
1 elmex 1.1 /*
2 root 1.71 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 root 1.111 *
4 root 1.116 * Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team
5 root 1.112 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
6 root 1.100 * Copyright (©) 2002 Mark Wedel & Crossfire Development Team
7     * Copyright (©) 1992 Frank Tore Johansen
8 root 1.111 *
9 root 1.89 * Deliantra is free software: you can redistribute it and/or modify it under
10     * the terms of the Affero GNU General Public License as published by the
11     * Free Software Foundation, either version 3 of the License, or (at your
12     * option) any later version.
13 root 1.111 *
14 root 1.68 * This program is distributed in the hope that it will be useful,
15     * but WITHOUT ANY WARRANTY; without even the implied warranty of
16     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17     * GNU General Public License for more details.
18 root 1.111 *
19 root 1.89 * You should have received a copy of the Affero GNU General Public License
20     * and the GNU General Public License along with this program. If not, see
21     * <http://www.gnu.org/licenses/>.
22 root 1.111 *
23 root 1.71 * The authors can be reached via e-mail to <support@deliantra.net>
24 pippijn 1.37 */
25 elmex 1.1
26 root 1.12 #include <cassert>
27 root 1.115 #include <functional>
28    
29     #include <flat_hash_map.hpp>
30 root 1.12
31 elmex 1.1 #include <global.h>
32    
33 root 1.74 archetype *loading_arch; // ugly flag to object loader etc. to suppress/request special processing
34     arch_ptr archetype::empty;
35 root 1.46
36 root 1.12 // the hashtable
37 root 1.115 typedef ska::flat_hash_map<
38     const char *,
39     arch_ptr,
40     str_hash,
41     str_equal,
42     slice_allocator< std::pair<const char *const, arch_ptr> >
43     > HT;
44 root 1.18
45 root 1.74 static HT ht (10000);
46 root 1.62 archvec archetypes;
47 root 1.74 static unordered_vector<archetype *> allarch;
48     static int dirtycnt;
49 root 1.12
50 root 1.73 // the vector of other_arch references to be resolved
51     static std::vector< std::pair<arch_ptr *, shstr> > postponed_arch_ref;
52     // the vector of loaded but not yet committed archetypes
53     static std::vector<archetype *> postponed_arch;
54    
55 root 1.92 //+GPL
56    
57 root 1.93 /*
58 root 1.108 * Creates an object. This function is called by archetype::get ()
59 root 1.93 * if it fails to find the appropriate archetype.
60 root 1.108 * Thus archetype::get() will be guaranteed to always return
61 root 1.93 * an object, and never NULL.
62     */
63 root 1.94 static object *
64 root 1.93 create_singularity (const char *name)
65     {
66     LOG (llevError | logBacktrace, "FATAL: creating singularity for '%s'.\n", name);
67    
68     if (!strcmp (name, "bug"))
69     abort ();
70    
71     object *op = archetype::get (shstr_bug);
72     op->name = op->name_pl = format ("bug, please report (missing archetype %s)", name);
73    
74     return op;
75     }
76    
77 elmex 1.1 /**
78     * GROS - This function retrieves an archetype given the name that appears
79     * during the game (for example, "writing pen" instead of "stylus").
80     * It does not use the hashtable system, but browse the whole archlist each time.
81     * I suggest not to use it unless you really need it because of performance issue.
82     * It is currently used by scripting extensions (create-object).
83     * Params:
84     * - name: the name we're searching for (ex: "writing pen");
85     * Return value:
86     * - the archetype found or null if nothing was found.
87     */
88 root 1.12 archetype *
89     find_archetype_by_object_name (const char *name)
90     {
91 root 1.39 shstr_cmp name_cmp (name);
92 elmex 1.1
93 root 1.63 for_all_archetypes (at)
94 root 1.66 if (at->name == name_cmp)
95 root 1.39 return at;
96 elmex 1.1
97 root 1.39 return 0;
98 elmex 1.1 }
99    
100     /**
101     * This function retrieves an archetype by type and name that appears during
102     * the game. It is basically the same as find_archetype_by_object_name()
103     * except that it considers only items of the given type.
104     */
105 root 1.12 archetype *
106     find_archetype_by_object_type_name (int type, const char *name)
107     {
108 root 1.39 shstr_cmp name_cmp (name);
109 elmex 1.1
110 root 1.63 for_all_archetypes (at)
111 root 1.66 if (at->name == name_cmp && at->type == type)
112 root 1.39 return at;
113 elmex 1.1
114 root 1.39 return 0;
115 elmex 1.1 }
116    
117     /* This is a lot like the above function. Instead, we are trying to match
118     * the arch->skill values. type is the type of object to match
119     * against (eg, to only match against skills or only skill objects for example).
120     * If type is -1, ew don't match on type.
121     */
122 root 1.12 object *
123     get_archetype_by_skill_name (const char *skill, int type)
124     {
125 root 1.39 shstr_cmp skill_cmp (skill);
126 elmex 1.1
127 root 1.63 for_all_archetypes (at)
128     if (at->skill == skill_cmp && (type == -1 || type == at->type))
129 root 1.80 return at->instance ();
130 root 1.35
131     return 0;
132 elmex 1.1 }
133    
134     /* similiar to above - this returns the first archetype
135     * that matches both the type and subtype. type and subtype
136     * can be -1 to say ignore, but in this case, the match it does
137     * may not be very useful. This function is most useful when
138     * subtypes are known to be unique for a particular type
139     * (eg, skills)
140     */
141 root 1.12 archetype *
142     get_archetype_by_type_subtype (int type, int subtype)
143     {
144 root 1.63 for_all_archetypes (at)
145     if ((type == -1 || type == at->type) && (subtype == -1 || subtype == at->subtype))
146 root 1.39 return at;
147 elmex 1.1
148 root 1.39 return 0;
149 elmex 1.1 }
150    
151     /**
152     * GROS - this returns a new object given the name that appears during the game
153     * (for example, "writing pen" instead of "stylus").
154     * Params:
155     * - name: The name we're searching for (ex: "writing pen");
156     * Return value:
157     * - a corresponding object if found; a singularity object if not found.
158     * Note by MSW - it appears that it takes the full name and keeps
159     * shortening it until it finds a match. I re-wrote this so that it
160 root 1.78 * doesn't allocate it each time - not that this function is used much,
161 elmex 1.1 * but it otherwise had a big memory leak.
162     */
163 root 1.12 object *
164     get_archetype_by_object_name (const char *name)
165     {
166 root 1.17 char tmpname[MAX_BUF];
167    
168     assign (tmpname, name);
169 root 1.12
170 root 1.90 for (int i = strlen (tmpname); i > 0; i--)
171 root 1.12 {
172     tmpname[i] = 0;
173 root 1.17
174 root 1.39 if (archetype *at = find_archetype_by_object_name (tmpname))
175 root 1.80 return at->instance ();
176 elmex 1.1 }
177 root 1.17
178 root 1.12 return create_singularity (name);
179 elmex 1.1 }
180    
181 root 1.65 /* This is a subset of the parse_id command. Basically, name can be
182     * a string seperated lists of things to match, with certain keywords.
183     * pl is the player (only needed to set count properly)
184     * op is the item we are trying to match. Calling function takes care
185     * of what action might need to be done and if it is valid
186     * (pickup, drop, etc.) Return NONZERO if we have a match. A higher
187     * value means a better match. 0 means no match.
188     *
189     * Brief outline of the procedure:
190     * We take apart the name variable into the individual components.
191     * cases for 'all' and unpaid are pretty obvious.
192     * Next, we check for a count (either specified in name, or in the
193     * player object.)
194     * If count is 1, make a quick check on the name.
195     * IF count is >1, we need to make plural name. Return if match.
196     * Last, make a check on the full name.
197     */
198 root 1.12 int
199 root 1.15 item_matched_string (object *pl, object *op, const char *name)
200 elmex 1.1 {
201 root 1.22 char *cp, local_name[MAX_BUF];
202     int count, retval = 0;
203 root 1.15
204 root 1.40 assign (local_name, name); /* strtok is destructive to name */
205 root 1.12
206     for (cp = strtok (local_name, ","); cp; cp = strtok (NULL, ","))
207     {
208     while (cp[0] == ' ')
209     ++cp; /* get rid of spaces */
210    
211     /* LOG(llevDebug,"Trying to match %s\n", cp); */
212     /* All is a very generic match - low match value */
213     if (!strcmp (cp, "all"))
214     return 1;
215    
216     /* unpaid is a little more specific */
217 root 1.102 if (!strcmp (cp, "unpaid") && op->flag [FLAG_UNPAID])
218 root 1.12 return 2;
219 root 1.90
220 root 1.102 if (!strcmp (cp, "cursed") && op->flag [FLAG_KNOWN_CURSED] && (op->flag [FLAG_CURSED] || op->flag [FLAG_DAMNED]))
221 root 1.12 return 2;
222    
223 root 1.102 if (!strcmp (cp, "unlocked") && !op->flag [FLAG_INV_LOCKED])
224 root 1.12 return 2;
225    
226     /* Allow for things like '100 arrows' */
227     if ((count = atoi (cp)) != 0)
228     {
229     cp = strchr (cp, ' ');
230     while (cp && cp[0] == ' ')
231     ++cp; /* get rid of spaces */
232     }
233     else
234     {
235     if (pl->type == PLAYER)
236     count = pl->contr->count;
237     else
238     count = 0;
239     }
240    
241     if (!cp || cp[0] == '\0' || count < 0)
242     return 0;
243    
244    
245     /* The code here should go from highest retval to lowest. That
246     * is because of the 'else' handling - we don't want to match on
247     * something and set a low retval, even though it may match a higher retcal
248     * later. So keep it in descending order here, so we try for the best
249     * match first, and work downward.
250     */
251 root 1.91 const char *qbn0, *qbn1, *qsn; // query base name/short name caches
252    
253 root 1.12 if (!strcasecmp (cp, query_name (op)))
254     retval = 20;
255 root 1.91 else if (!strcasecmp (cp, qsn = query_short_name (op)))
256 root 1.12 retval = 18;
257 root 1.91 else if (!strcasecmp (cp, qbn0 = query_base_name (op, 0)))
258 root 1.12 retval = 16;
259 root 1.91 else if (!strcasecmp (cp, qbn1 = query_base_name (op, 1)))
260 root 1.12 retval = 16;
261     else if (op->custom_name && !strcasecmp (cp, op->custom_name))
262     retval = 15;
263 root 1.91 else if (!strncasecmp (cp, qbn0, strlen (cp)))
264 root 1.12 retval = 14;
265 root 1.91 else if (!strncasecmp (cp, qbn1, strlen (cp)))
266 root 1.12 retval = 14;
267     /* Do substring checks, so things like 'Str+1' will match.
268     * retval of these should perhaps be lower - they are lower
269     * then the specific strcasecmp aboves, but still higher than
270     * some other match criteria.
271     */
272 root 1.91 else if (strstr (qbn1, cp))
273 root 1.12 retval = 12;
274 root 1.91 else if (strstr (qbn0, cp))
275 root 1.12 retval = 12;
276 root 1.91 else if (strstr (qsn, cp))
277 root 1.12 retval = 12;
278     /* Check against plural/non plural based on count. */
279     else if (count > 1 && !strcasecmp (cp, op->name_pl))
280 root 1.23 retval = 6;
281 root 1.12 else if (count == 1 && !strcasecmp (op->name, cp))
282 root 1.23 retval = 6;
283 root 1.12 /* base name matched - not bad */
284     else if (strcasecmp (cp, op->name) == 0 && !count)
285     retval = 4;
286     /* Check for partial custom name, but give a real low priority */
287 root 1.88 else if (op->custom_name.contains (cp))
288 root 1.12 retval = 3;
289    
290     if (retval)
291     {
292     if (pl->type == PLAYER)
293     pl->contr->count = count;
294 root 1.23
295 root 1.12 return retval;
296 root 1.7 }
297 elmex 1.1 }
298 root 1.23
299 root 1.12 return 0;
300 elmex 1.1 }
301    
302 root 1.92 //-GPL
303    
304 root 1.104 void
305     archetype::do_delete ()
306     {
307     delete this;
308     }
309    
310 root 1.63 archetype::archetype (const char *name)
311 root 1.12 {
312 root 1.63 arch = this;
313     this->archname = this->name = this->name_pl = name;
314 root 1.46 }
315 root 1.12
316 root 1.46 archetype::~archetype ()
317     {
318 root 1.63 unlink ();
319 elmex 1.1 }
320    
321 root 1.63 void
322     archetype::link ()
323 elmex 1.1 {
324 root 1.63 if (!archetypes.contains (this))
325 root 1.74 {
326     archetypes.insert (this);
327     ht.insert (std::make_pair (archname, this));
328     }
329 elmex 1.1 }
330    
331 root 1.46 void
332 root 1.63 archetype::unlink ()
333     {
334     if (archetypes.contains (this))
335 root 1.74 {
336     archetypes.erase (this);
337     ht.erase (archname);
338     }
339 root 1.63 }
340    
341     /*
342     * Finds, using the hashtable, which archetype matches the given name.
343     * returns a pointer to the found archetype, otherwise NULL.
344     */
345     archetype *
346     archetype::find (const char *name)
347     {
348     if (!name)
349     return 0;
350    
351     auto (i, ht.find (name));
352    
353     if (i == ht.end ())
354     return 0;
355     else
356     return i->second;
357     }
358    
359 root 1.97 void
360     archetype::post_load_check ()
361     {
362 root 1.105 assert (("obj_original MUST NOT be set for archetypes", !flag [FLAG_OBJ_ORIGINAL]));
363    
364     if (type == SKILL)
365     add_skill_archetype (this);
366    
367 root 1.97 object::post_load_check ();
368     }
369    
370 root 1.63 archetype *
371 root 1.53 archetype::read (object_thawer &f)
372 root 1.12 {
373 root 1.43 assert (f.kw == KW_object);
374    
375 root 1.63 std::vector<archetype *> parts;
376 root 1.12
377 root 1.72 coroapi::cede_to_tick ();
378 root 1.52
379 root 1.49 for (;;)
380 root 1.43 {
381 root 1.73 archetype *at = new archetype (f.get_str ());
382 root 1.63
383 root 1.54 f.next ();
384 root 1.49
385 root 1.56 #if 0
386 root 1.63 // implementing it here in the server does neither allow multiple inheritence
387     // nor does it cleanly "just override". it would allow use in map files, though,
388 root 1.103 // and other resource files dynamically loaded (as opposed to being preprocessed).
389 root 1.63 // not that any of this is relevant as of yet...
390 root 1.55 if (f.kw == KW_inherit)
391     {
392     if (archetype *at = find (f.get_str ()))
393     *op = at->clone;
394     else
395     LOG (llevError, "archetype '%s' tries to inherit from non-existent archetype '%s'.\n",
396 root 1.63 &at->archname, f.get_str ());
397 root 1.55
398     f.next ();
399     }
400 root 1.56 #endif
401 root 1.55
402 root 1.69 loading_arch = at; // hack to tell parse_kv et al. to behave
403     bool parse_ok = at->parse_kv (f);
404     loading_arch = 0;
405    
406     if (!parse_ok)
407 root 1.49 goto fail;
408    
409 root 1.69 loading_arch = at; // hack to tell parse_kv et al. to behave
410 root 1.63 at->post_load_check ();
411 root 1.69 loading_arch = 0;
412 root 1.59
413 root 1.63 parts.push_back (at);
414 root 1.12
415 root 1.49 if (f.kw != KW_more)
416     break;
417 root 1.46
418 root 1.43 f.next ();
419 root 1.54
420     if (f.kw != KW_object)
421     {
422     f.parse_error ("more object");
423     goto fail;
424     }
425 root 1.49 }
426 root 1.43
427 root 1.49 {
428 root 1.63 archetype *new_head = parts.front ();
429     archetype *old_head = find (new_head->archname);
430    
431 root 1.64 if (old_head && !old_head->is_head ())
432     {
433     LOG (llevError, "%s: unable to overwrite non-head archetype '%s' with head archetype, skipping.\n",
434     &new_head->archname, &old_head->archname);
435     goto fail;
436     }
437 root 1.63
438     // check that all archetypes belong to the same old object or are new
439 root 1.117 for (auto &&at : parts)
440 root 1.49 {
441 root 1.117 archetype *new_part = at;
442 root 1.63 archetype *old_part = find (new_part->archname);
443 root 1.49
444 root 1.63 if (old_part && old_part->head_ () != old_head)
445 root 1.49 {
446 root 1.63 LOG (llevError, "%s: unable to overwrite archetype '%s' with archetype of different object, skipping.\n",
447     &new_part->archname, &((archetype *)old_part->head_ ())->archname);
448 root 1.49 goto fail;
449     }
450     }
451    
452 root 1.64 // assemble new chain
453 root 1.99 new_head->max_x = new_head->x;
454 root 1.49
455 root 1.63 archetype *less = new_head;
456 root 1.117 for (auto &&p = parts.begin () + 1; p != parts.end (); ++p)
457 root 1.49 {
458 root 1.63 archetype *at = *p;
459 root 1.49
460 root 1.74 // some flags get inherited from the head (probably a lot more)
461 root 1.67 // doing it here doesn't feel too cozy, but it allows code
462     // to ignore head checks for these flags, which saves time
463     at->flag [FLAG_ALIVE] = new_head->flag [FLAG_ALIVE];
464     at->flag [FLAG_NO_PICK] = new_head->flag [FLAG_NO_PICK];
465     at->flag [FLAG_MONSTER] = new_head->flag [FLAG_MONSTER];
466     at->flag [FLAG_IS_FLOOR] = new_head->flag [FLAG_IS_FLOOR];
467    
468 root 1.74 new_head->max_x = max (new_head->max_x, at->x);
469 root 1.49
470 root 1.64 at->head = new_head;
471     less->more = at;
472     less = at;
473 root 1.63 }
474 root 1.49
475 root 1.73 postponed_arch.insert (postponed_arch.end (), parts.begin (), parts.end ());
476 root 1.53
477 root 1.63 return new_head;
478 root 1.49 }
479 root 1.43
480 root 1.49 fail:
481 root 1.117 for (auto &&at : parts)
482     at->destroy ();
483 root 1.43
484 root 1.53 return 0;
485 root 1.43 }
486    
487 root 1.53 void
488 root 1.73 archetype::postpone_arch_ref (arch_ptr &ref, const_utf8_string other_arch)
489     {
490     ref = 0;
491     postponed_arch_ref.push_back (std::pair<arch_ptr *, shstr>(&ref, shstr (other_arch)));
492     }
493    
494     void
495     archetype::commit_load ()
496 root 1.43 {
497 root 1.73 // unlink old archetypes and link in new ones */
498 root 1.117 for (auto &&at : postponed_arch)
499 root 1.73 {
500     if (archetype *old = find (at->archname))
501     old->unlink ();
502    
503 root 1.74 allarch.push_back (at);
504    
505 root 1.73 at->link ();
506 root 1.74 ++dirtycnt;
507 root 1.73 }
508    
509     postponed_arch.clear ();
510    
511     // now resolve arch references
512 root 1.117 for (auto &&p : postponed_arch_ref) // not yet C++17
513 root 1.73 {
514 root 1.117 arch_ptr *ap = p.first;
515     archetype *at = find (p.second);
516 root 1.73
517     if (!at)
518 root 1.117 LOG (llevError, "unable to resolve postponed arch reference to '%s'", &p.second);
519 root 1.73
520     *ap = at;
521     }
522    
523     postponed_arch_ref.clear ();
524    
525     empty = find (shstr_empty_archetype);
526 elmex 1.1 }
527    
528 root 1.74 void
529     archetype::gc ()
530     {
531 root 1.109 int cnt = max (1, min<int> (allarch.size () / 128, dirtycnt));
532 root 1.74 dirtycnt = max (0, dirtycnt - cnt);
533    
534     do
535     {
536     static int idx;
537    
538     if (idx >= allarch.size ())
539     if (idx)
540     idx = 0;
541     else
542     return;
543    
544     archetype *at = allarch [idx];
545    
546     if (at->refcnt_cnt () > 1) // all arches have ONE refcount from their object
547     ++idx;
548     else
549     {
550 root 1.106 //LOG (llevDebug, "garbage collect arch %s", &at->archname);
551 root 1.79 assert (at->arch == at); // verify that refcnt == 1 is truly valid
552 root 1.74 allarch.erase (idx);
553 root 1.77
554     // break chain
555     for (object *op = at->head_ (); op; )
556     {
557     object *next = op->more;
558     op->head = 0;
559     op->more = 0;
560     op = next;
561     }
562    
563 root 1.85 at->destroy ();
564 root 1.74 at->arch = 0;
565     }
566     }
567     while (--cnt);
568     }
569    
570 root 1.70 object *
571     archetype::instance ()
572     {
573 root 1.80 object *op = clone ();
574     op->instantiate ();
575     return op;
576 root 1.70 }
577    
578 root 1.92 //+GPL
579    
580 elmex 1.1 /*
581     * Finds which archetype matches the given name, and returns a new
582     * object containing a copy of the archetype.
583     */
584 root 1.12 object *
585 root 1.76 archetype::get (const char *name)
586     {
587 root 1.80 archetype *at = find (name);
588    
589     if (!at)
590     return create_singularity (name);
591    
592     return at->instance ();
593 root 1.76 }
594    
595 elmex 1.1 /*
596     * Returns the first archetype using the given type.
597     * Used in treasure-generation.
598     */
599 root 1.95 static archetype *
600 root 1.12 type_to_archetype (int type)
601     {
602 root 1.63 for_all_archetypes (at)
603     if (at->type == type && at->head_ () != at)
604 elmex 1.1 return at;
605 root 1.20
606     return 0;
607 elmex 1.1 }
608    
609     /*
610     * Returns a new object copied from the first archetype matching
611     * the given type.
612     * Used in treasure-generation.
613     */
614 root 1.12 object *
615     clone_arch (int type)
616     {
617 root 1.80 archetype *at = type_to_archetype (type);
618 elmex 1.1
619 root 1.80 if (!at)
620 root 1.12 {
621     LOG (llevError, "Can't clone archetype %d\n", type);
622 root 1.30 return 0;
623 root 1.12 }
624 root 1.20
625 root 1.80 return at->instance ();
626 elmex 1.1 }
627    
628     /*
629     * member: make instance from class
630     */
631 root 1.12 object *
632 root 1.15 object_create_arch (archetype *at)
633 elmex 1.1 {
634 root 1.20 object *op, *prev = 0, *head = 0;
635 elmex 1.1
636 root 1.12 while (at)
637     {
638 root 1.80 op = at->instance ();
639 root 1.63
640     op->x = at->x;
641     op->y = at->y;
642 root 1.20
643 root 1.12 if (head)
644     op->head = head, prev->more = op;
645 root 1.20
646 root 1.12 if (!head)
647     head = op;
648 root 1.20
649 root 1.12 prev = op;
650 root 1.63 at = (archetype *)at->more;
651 elmex 1.1 }
652 root 1.20
653 root 1.63 return head;
654 elmex 1.1 }
655    
656 root 1.92 //-GPL
657