ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/arch.C
Revision: 1.26
Committed: Fri Nov 17 19:40:53 2006 UTC (17 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.25: +46 -25 lines
Log Message:
* remove arch.h
* use refcounting for archetypes
* cleanup
* strat of generic garbage collector

File Contents

# Content
1 /*
2 CrossFire, A Multiplayer game for X-windows
3
4 Copyright (C) 2002 Mark Wedel & Crossfire Development Team
5 Copyright (C) 1992 Frank Tore Johansen
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 The authors can be reached via e-mail at <crossfire@schmorp.de>
22 */
23
24 #include <cassert>
25
26 #include <global.h>
27 #include <funcpoint.h>
28 #include <loader.h>
29
30 #define USE_UNORDERED_MAP 0
31
32 #if USE_UNORDERED_MAP
33 # include <tr1/functional>
34 # include <tr1/unordered_map>
35 #endif
36
37 int arch_cmp = 0; /* How many strcmp's */
38 int arch_search = 0; /* How many searches */
39 int arch_init; /* True if doing arch initialization */
40
41 /* The naming of these functions is really poor - they are all
42 * pretty much named '.._arch_...', but they may more may not
43 * return archetypes. Some make the arch_to_object call, and thus
44 * return an object. Perhaps those should be called 'archob' functions
45 * to denote they return an object derived from the archetype.
46 * MSW 2003-04-29
47 */
48
49 #if USE_UNORDERED_MAP
50 // the hashtable
51 typedef std::tr1::unordered_map
52 <
53 std::size_t,
54 arch_ptr,
55 std::tr1::hash<size_t>,
56 std::equal_to<size_t>,
57 slice_allocator< std::pair<const std::size_t, archetype *> >
58 true,
59 > HT;
60
61 static HT ht;
62 #else
63 static arch_ptr arch_table[ARCHTABLE];
64 #endif
65
66 /**
67 * GROS - This function retrieves an archetype given the name that appears
68 * during the game (for example, "writing pen" instead of "stylus").
69 * It does not use the hashtable system, but browse the whole archlist each time.
70 * I suggest not to use it unless you really need it because of performance issue.
71 * It is currently used by scripting extensions (create-object).
72 * Params:
73 * - name: the name we're searching for (ex: "writing pen");
74 * Return value:
75 * - the archetype found or null if nothing was found.
76 */
77 archetype *
78 find_archetype_by_object_name (const char *name)
79 {
80 archetype *at;
81
82 if (name == NULL)
83 return (archetype *) NULL;
84
85 for (at = first_archetype; at != NULL; at = at->next)
86 {
87 if (!strcmp (at->clone.name, name))
88 return at;
89 }
90 return NULL;
91 }
92
93 /**
94 * This function retrieves an archetype by type and name that appears during
95 * the game. It is basically the same as find_archetype_by_object_name()
96 * except that it considers only items of the given type.
97 */
98 archetype *
99 find_archetype_by_object_type_name (int type, const char *name)
100 {
101 archetype *at;
102
103 if (name == NULL)
104 return NULL;
105
106 for (at = first_archetype; at != NULL; at = at->next)
107 {
108 if (at->clone.type == type && strcmp (at->clone.name, name) == 0)
109 return at;
110 }
111
112 return NULL;
113 }
114
115 /* This is a lot like the above function. Instead, we are trying to match
116 * the arch->skill values. type is the type of object to match
117 * against (eg, to only match against skills or only skill objects for example).
118 * If type is -1, ew don't match on type.
119 */
120 object *
121 get_archetype_by_skill_name (const char *skill, int type)
122 {
123 archetype *
124 at;
125
126 if (skill == NULL)
127 return NULL;
128
129 for (at = first_archetype; at != NULL; at = at->next)
130 {
131 if (((type == -1) || (type == at->clone.type)) && (!strcmp (at->clone.skill, skill)))
132 return arch_to_object (at);
133 }
134 return NULL;
135 }
136
137 /* similiar to above - this returns the first archetype
138 * that matches both the type and subtype. type and subtype
139 * can be -1 to say ignore, but in this case, the match it does
140 * may not be very useful. This function is most useful when
141 * subtypes are known to be unique for a particular type
142 * (eg, skills)
143 */
144 archetype *
145 get_archetype_by_type_subtype (int type, int subtype)
146 {
147 archetype *
148 at;
149
150 for (at = first_archetype; at != NULL; at = at->next)
151 {
152 if (((type == -1) || (type == at->clone.type)) && (subtype == -1 || subtype == at->clone.subtype))
153 return at;
154 }
155 return NULL;
156 }
157
158 /**
159 * GROS - this returns a new object given the name that appears during the game
160 * (for example, "writing pen" instead of "stylus").
161 * Params:
162 * - name: The name we're searching for (ex: "writing pen");
163 * Return value:
164 * - a corresponding object if found; a singularity object if not found.
165 * Note by MSW - it appears that it takes the full name and keeps
166 * shortening it until it finds a match. I re-wrote this so that it
167 * doesn't malloc it each time - not that this function is used much,
168 * but it otherwise had a big memory leak.
169 */
170 object *
171 get_archetype_by_object_name (const char *name)
172 {
173 archetype *at;
174 char tmpname[MAX_BUF];
175 int i;
176
177 assign (tmpname, name);
178
179 for (i = strlen (tmpname); i > 0; i--)
180 {
181 tmpname[i] = 0;
182 at = find_archetype_by_object_name (tmpname);
183
184 if (at != NULL)
185 return arch_to_object (at);
186 }
187
188 return create_singularity (name);
189 }
190
191 /* This is a subset of the parse_id command. Basically, name can be
192 * a string seperated lists of things to match, with certain keywords.
193 * pl is the player (only needed to set count properly)
194 * op is the item we are trying to match. Calling function takes care
195 * of what action might need to be done and if it is valid
196 * (pickup, drop, etc.) Return NONZERO if we have a match. A higher
197 * value means a better match. 0 means no match.
198 *
199 * Brief outline of the procedure:
200 * We take apart the name variable into the individual components.
201 * cases for 'all' and unpaid are pretty obvious.
202 * Next, we check for a count (either specified in name, or in the
203 * player object.)
204 * If count is 1, make a quick check on the name.
205 * IF count is >1, we need to make plural name. Return if match.
206 * Last, make a check on the full name.
207 */
208 int
209 item_matched_string (object *pl, object *op, const char *name)
210 {
211 char *cp, local_name[MAX_BUF];
212 int count, retval = 0;
213
214 strcpy (local_name, name); /* strtok is destructive to name */
215
216 for (cp = strtok (local_name, ","); cp; cp = strtok (NULL, ","))
217 {
218 while (cp[0] == ' ')
219 ++cp; /* get rid of spaces */
220
221 /* LOG(llevDebug,"Trying to match %s\n", cp); */
222 /* All is a very generic match - low match value */
223 if (!strcmp (cp, "all"))
224 return 1;
225
226 /* unpaid is a little more specific */
227 if (!strcmp (cp, "unpaid") && QUERY_FLAG (op, FLAG_UNPAID))
228 return 2;
229 if (!strcmp (cp, "cursed") && QUERY_FLAG (op, FLAG_KNOWN_CURSED) && (QUERY_FLAG (op, FLAG_CURSED) || QUERY_FLAG (op, FLAG_DAMNED)))
230 return 2;
231
232 if (!strcmp (cp, "unlocked") && !QUERY_FLAG (op, FLAG_INV_LOCKED))
233 return 2;
234
235 /* Allow for things like '100 arrows' */
236 if ((count = atoi (cp)) != 0)
237 {
238 cp = strchr (cp, ' ');
239 while (cp && cp[0] == ' ')
240 ++cp; /* get rid of spaces */
241 }
242 else
243 {
244 if (pl->type == PLAYER)
245 count = pl->contr->count;
246 else
247 count = 0;
248 }
249
250 if (!cp || cp[0] == '\0' || count < 0)
251 return 0;
252
253
254 /* The code here should go from highest retval to lowest. That
255 * is because of the 'else' handling - we don't want to match on
256 * something and set a low retval, even though it may match a higher retcal
257 * later. So keep it in descending order here, so we try for the best
258 * match first, and work downward.
259 */
260 if (!strcasecmp (cp, query_name (op)))
261 retval = 20;
262 else if (!strcasecmp (cp, query_short_name (op)))
263 retval = 18;
264 else if (!strcasecmp (cp, query_base_name (op, 0)))
265 retval = 16;
266 else if (!strcasecmp (cp, query_base_name (op, 1)))
267 retval = 16;
268 else if (op->custom_name && !strcasecmp (cp, op->custom_name))
269 retval = 15;
270 else if (!strncasecmp (cp, query_base_name (op, 0), strlen (cp)))
271 retval = 14;
272 else if (!strncasecmp (cp, query_base_name (op, 1), strlen (cp)))
273 retval = 14;
274 /* Do substring checks, so things like 'Str+1' will match.
275 * retval of these should perhaps be lower - they are lower
276 * then the specific strcasecmp aboves, but still higher than
277 * some other match criteria.
278 */
279 else if (strstr (query_base_name (op, 1), cp))
280 retval = 12;
281 else if (strstr (query_base_name (op, 0), cp))
282 retval = 12;
283 else if (strstr (query_short_name (op), cp))
284 retval = 12;
285 /* Check against plural/non plural based on count. */
286 else if (count > 1 && !strcasecmp (cp, op->name_pl))
287 retval = 6;
288 else if (count == 1 && !strcasecmp (op->name, cp))
289 retval = 6;
290 /* base name matched - not bad */
291 else if (strcasecmp (cp, op->name) == 0 && !count)
292 retval = 4;
293 /* Check for partial custom name, but give a real low priority */
294 else if (op->custom_name && strstr (op->custom_name, cp))
295 retval = 3;
296
297 if (retval)
298 {
299 if (pl->type == PLAYER)
300 pl->contr->count = count;
301
302 return retval;
303 }
304 }
305
306 return 0;
307 }
308
309 /*
310 * Initialises the internal linked list of archetypes (read from file).
311 * Then the global "empty_archetype" pointer is initialised.
312 * Then the blocksview[] array is initialised.
313 */
314
315 void
316 init_archetypes (void)
317 { /* called from add_player() and edit() */
318 if (first_archetype != NULL) /* Only do this once */
319 return;
320
321 arch_init = 1;
322 load_archetypes ();
323 arch_init = 0;
324 empty_archetype = archetype::find ("empty_archetype");
325
326 /* init_blocksview();*/
327 }
328
329 /*
330 * Stores debug-information about how efficient the hashtable
331 * used for archetypes has been in the static errmsg array.
332 */
333
334 void
335 arch_info (object *op)
336 {
337 sprintf (errmsg, "%d searches and %d strcmp()'s", arch_search, arch_cmp);
338 new_draw_info (NDI_BLACK, 0, op, errmsg);
339 }
340
341 /*
342 * Initialise the hashtable used by the archetypes.
343 */
344
345 void
346 clear_archetable (void)
347 {
348 memset ((void *) arch_table, 0, ARCHTABLE * sizeof (archetype *));
349 }
350
351 /*
352 * An alternative way to init the hashtable which is slower, but _works_...
353 */
354
355 void
356 init_archetable (void)
357 {
358 archetype *at;
359
360 LOG (llevDebug, " Setting up archetable...\n");
361
362 for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more)
363 at->hash_add ();
364
365 LOG (llevDebug, "done\n");
366 }
367
368 void
369 free_all_archs (void)
370 {
371 archetype *at, *next;
372 int i = 0, f = 0;
373
374 for (at = first_archetype; at != NULL; at = next)
375 {
376 if (at->more)
377 next = at->more;
378 else
379 next = at->next;
380
381 delete
382 at;
383
384 i++;
385 }
386
387 LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f);
388 }
389
390 archetype::archetype ()
391 {
392 clear_object (&clone); /* to initial state other also */
393 CLEAR_FLAG (&clone, FLAG_FREED); /* This shouldn't matter, since copy_object() */
394 SET_FLAG (&clone, FLAG_REMOVED); /* doesn't copy these flags... */
395 }
396
397 archetype::~archetype ()
398 {
399 }
400
401 /*
402 * Reads/parses the archetype-file, and copies into a linked list
403 * of archetype-structures.
404 */
405 void
406 first_arch_pass (object_thawer & fp)
407 {
408 archetype *at, *head = NULL, *last_more = NULL;
409
410 at->clone.arch = first_archetype = at = new archetype;
411
412 while (int i = load_object (fp, &at->clone, 0))
413 {
414 at->clone.speed_left = (float) (-0.1);
415 /* copy the body_info to the body_used - this is only really
416 * need for monsters, but doesn't hurt to do it for everything.
417 * by doing so, when a monster is created, it has good starting
418 * values for the body_used info, so when items are created
419 * for it, they can be properly equipped.
420 */
421 memcpy (&at->clone.body_used, &at->clone.body_info, sizeof (at->clone.body_info));
422
423 switch (i)
424 {
425 case LL_NORMAL: /* A new archetype, just link it with the previous */
426 if (last_more != NULL)
427 last_more->next = at;
428 if (head != NULL)
429 head->next = at;
430 head = last_more = at;
431 #if 0
432 if (!op->type)
433 LOG (llevDebug, " WARNING: Archetype %s has no type info!\n", op->arch->name);
434 #endif
435 at->tail_x = 0;
436 at->tail_y = 0;
437 break;
438
439 case LL_MORE: /* Another part of the previous archetype, link it correctly */
440
441 at->head = head;
442 at->clone.head = &head->clone;
443 if (last_more != NULL)
444 {
445 last_more->more = at;
446 last_more->clone.more = &at->clone;
447 }
448 last_more = at;
449
450 /* If this multipart image is still composed of individual small
451 * images, don't set the tail_.. values. We can't use them anyways,
452 * and setting these to zero makes the map sending to the client much
453 * easier as just looking at the head, we know what to do.
454 */
455 if (at->clone.face != head->clone.face)
456 {
457 head->tail_x = 0;
458 head->tail_y = 0;
459 }
460 else
461 {
462 if (at->clone.x > head->tail_x)
463 head->tail_x = at->clone.x;
464 if (at->clone.y > head->tail_y)
465 head->tail_y = at->clone.y;
466 }
467 break;
468
469 }
470
471 at = new archetype;
472
473 at->clone.arch = at;
474 }
475
476 delete at;
477 }
478
479 /*
480 * Reads the archetype file once more, and links all pointers between
481 * archetypes.
482 */
483
484 void
485 second_arch_pass (object_thawer & thawer)
486 {
487 char buf[MAX_BUF], *variable = buf, *argument, *cp;
488 archetype *at = NULL, *other;
489
490 while (fgets (buf, MAX_BUF, thawer) != NULL)
491 {
492 if (*buf == '#')
493 continue;
494 if ((argument = strchr (buf, ' ')) != NULL)
495 {
496 *argument = '\0', argument++;
497 cp = argument + strlen (argument) - 1;
498 while (isspace (*cp))
499 {
500 *cp = '\0';
501 cp--;
502 }
503 }
504 if (!strcmp ("Object", variable))
505 {
506 if ((at = archetype::find (argument)) == NULL)
507 LOG (llevError, "Warning: failed to find arch %s\n", argument);
508 }
509 else if (!strcmp ("other_arch", variable))
510 {
511 if (at != NULL && at->clone.other_arch == NULL)
512 {
513 if ((other = archetype::find (argument)) == NULL)
514 LOG (llevError, "Warning: failed to find other_arch %s\n", argument);
515 else if (at != NULL)
516 at->clone.other_arch = other;
517 }
518 }
519 else if (!strcmp ("randomitems", variable))
520 {
521 if (at != NULL)
522 {
523 treasurelist *tl = find_treasurelist (argument);
524
525 if (tl == NULL)
526 LOG (llevError, "Failed to link treasure to arch (%s): %s\n", &at->name, argument);
527 else
528 at->clone.randomitems = tl;
529 }
530 }
531 }
532 }
533
534 #ifdef DEBUG
535 void
536 check_generators (void)
537 {
538 archetype *at;
539
540 for (at = first_archetype; at != NULL; at = at->next)
541 if (QUERY_FLAG (&at->clone, FLAG_GENERATOR) && at->clone.other_arch == NULL)
542 LOG (llevError, "Warning: %s is generator but lacks other_arch.\n", &at->name);
543 }
544 #endif
545
546 /*
547 * First initialises the archtype hash-table (init_archetable()).
548 * Reads and parses the archetype file (with the first and second-pass
549 * functions).
550 * Then initialises treasures by calling load_treasures().
551 */
552
553 void
554 load_archetypes (void)
555 {
556 char filename[MAX_BUF];
557
558 sprintf (filename, "%s/%s", settings.datadir, settings.archetypes);
559 LOG (llevDebug, "Reading archetypes from %s:\n", filename);
560
561 {
562 object_thawer
563 thawer (filename);
564
565 clear_archetable ();
566 LOG (llevDebug, " arch-pass 1...\n");
567 first_arch_pass (thawer);
568 LOG (llevDebug, " done\n");
569 }
570
571 init_archetable ();
572 warn_archetypes = 1;
573
574 {
575 object_thawer
576 thawer (filename);
577
578 LOG (llevDebug, " loading treasure...\n");
579 load_treasures ();
580 LOG (llevDebug, " done\n arch-pass 2...\n");
581 second_arch_pass (thawer);
582 LOG (llevDebug, " done\n");
583 #ifdef DEBUG
584 check_generators ();
585 #endif
586 }
587 LOG (llevDebug, " done\n");
588 }
589
590 /*
591 * Creates and returns a new object which is a copy of the given archetype.
592 * This function returns NULL on failure.
593 */
594
595 object *
596 arch_to_object (archetype *at)
597 {
598 object *op;
599
600 if (at == NULL)
601 {
602 if (warn_archetypes)
603 LOG (llevError, "Couldn't find archetype.\n");
604
605 return NULL;
606 }
607
608 op = get_object ();
609 copy_object (&at->clone, op);
610 op->arch = at;
611 op->instantiate ();
612 return op;
613 }
614
615 /*
616 * Creates an object. This function is called by get_archetype()
617 * if it fails to find the appropriate archetype.
618 * Thus get_archetype() will be guaranteed to always return
619 * an object, and never NULL.
620 */
621
622 object *
623 create_singularity (const char *name)
624 {
625 object *op;
626 char buf[MAX_BUF];
627
628 sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name);
629 op = get_object ();
630 op->name = op->name_pl = buf;
631 SET_FLAG (op, FLAG_NO_PICK);
632 return op;
633 }
634
635 /*
636 * Finds which archetype matches the given name, and returns a new
637 * object containing a copy of the archetype.
638 */
639
640 object *
641 get_archetype (const char *name)
642 {
643 archetype *at = archetype::find (name);
644
645 if (!at)
646 return create_singularity (name);
647
648 return arch_to_object (at);
649 }
650
651 /*
652 * Hash-function used by the arch-hashtable.
653 */
654
655 unsigned long
656 hasharch (const char *str, int tablesize)
657 {
658 unsigned long hash = 0;
659 unsigned int i = 0;
660 const char *p;
661
662 /* use the one-at-a-time hash function, which supposedly is
663 * better than the djb2-like one used by perl5.005, but
664 * certainly is better then the bug used here before.
665 * see http://burtleburtle.net/bob/hash/doobs.html
666 */
667 for (p = str; i < MAXSTRING && *p; p++, i++)
668 {
669 hash += *p;
670 hash += hash << 10;
671 hash ^= hash >> 6;
672 }
673
674 hash += hash << 3;
675 hash ^= hash >> 11;
676 hash += hash << 15;
677
678 return hash % tablesize;
679 }
680
681 /*
682 * Finds, using the hashtable, which archetype matches the given name.
683 * returns a pointer to the found archetype, otherwise NULL.
684 */
685
686 archetype *
687 archetype::find (const char *name)
688 {
689 if (!name)
690 return 0;
691
692 #if USE_UNORDERED_MAP
693 AUTODECL (i, ht.find ((size_t) name));
694
695 if (i == ht.end ())
696 return 0;
697 else
698 return i->second;
699 #endif
700
701 archetype *at;
702 unsigned long index;
703
704 index = hasharch (name, ARCHTABLE);
705 arch_search++;
706 for (;;)
707 {
708 at = arch_table[index];
709
710 if (at == NULL)
711 {
712 if (warn_archetypes)
713 LOG (llevError, "Couldn't find archetype %s\n", name);
714
715 return NULL;
716 }
717
718 arch_cmp++;
719
720 if (!strcmp ((const char *) at->name, name))
721 return at;
722
723 if (++index >= ARCHTABLE)
724 index = 0;
725 }
726 }
727
728 /*
729 * Adds an archetype to the hashtable.
730 */
731 void
732 archetype::hash_add ()
733 {
734 #if USE_UNORDERED_MAP
735 ht.insert (std::make_pair ((size_t) (const char *) name, this));
736 #else
737
738 int index = hasharch ((const char *) name, ARCHTABLE), org_index = index;
739
740 for (;;)
741 {
742 if (!arch_table[index])
743 {
744 arch_table[index] = this;
745 break;
746 }
747
748 if (++index == ARCHTABLE)
749 index = 0;
750
751 if (index == org_index)
752 fatal (ARCHTABLE_TOO_SMALL);
753 }
754 #endif
755 }
756
757 void
758 archetype::hash_del ()
759 {
760 #if USE_UNORDERED_MAP
761 # error remove this from HT
762 #else
763
764 int index = hasharch ((const char *) name, ARCHTABLE), org_index = index;
765
766 for (;;)
767 {
768 if (arch_table[index] == this)
769 {
770 arch_table[index] = 0;
771 break;
772 }
773
774 if (++index == ARCHTABLE)
775 index = 0;
776
777 if (index == org_index)
778 break;
779 }
780 #endif
781 }
782
783 /*
784 * Returns the first archetype using the given type.
785 * Used in treasure-generation.
786 */
787
788 archetype *
789 type_to_archetype (int type)
790 {
791 archetype *at;
792
793 for (at = first_archetype; at; at = at->more == 0 ? at->next : at->more)
794 if (at->clone.type == type)
795 return at;
796
797 return 0;
798 }
799
800 /*
801 * Returns a new object copied from the first archetype matching
802 * the given type.
803 * Used in treasure-generation.
804 */
805
806 object *
807 clone_arch (int type)
808 {
809 archetype *at;
810 object *op = get_object ();
811
812 if ((at = type_to_archetype (type)) == NULL)
813 {
814 LOG (llevError, "Can't clone archetype %d\n", type);
815 free_object (op);
816 return NULL;
817 }
818
819 copy_object (&at->clone, op);
820 op->instantiate ();
821 return op;
822 }
823
824 /*
825 * member: make instance from class
826 */
827
828 object *
829 object_create_arch (archetype *at)
830 {
831 object *op, *prev = 0, *head = 0;
832
833 while (at)
834 {
835 op = arch_to_object (at);
836 op->x = at->clone.x;
837 op->y = at->clone.y;
838
839 if (head)
840 op->head = head, prev->more = op;
841
842 if (!head)
843 head = op;
844
845 prev = op;
846 at = at->more;
847 }
848
849 return (head);
850 }
851