|
|
1 | |
1 | /* |
2 | /* |
2 | * static char *rcsid_arch_c = |
3 | * static char *rcsid_arch_c = |
3 | * "$Id: arch.C,v 1.12 2006/09/04 17:16:19 root Exp $"; |
4 | * "$Id: arch.C,v 1.16 2006/09/10 16:00:23 root Exp $"; |
4 | */ |
5 | */ |
5 | |
6 | |
6 | /* |
7 | /* |
7 | CrossFire, A Multiplayer game for X-windows |
8 | CrossFire, A Multiplayer game for X-windows |
8 | |
9 | |
… | |
… | |
36 | #include <loader.h> |
37 | #include <loader.h> |
37 | |
38 | |
38 | /* IF set, does a little timing on the archetype load. */ |
39 | /* IF set, does a little timing on the archetype load. */ |
39 | #define TIME_ARCH_LOAD 0 |
40 | #define TIME_ARCH_LOAD 0 |
40 | |
41 | |
41 | static void add_arch (archetype * at); |
42 | static void add_arch (archetype *at); |
42 | |
43 | |
43 | static archetype *arch_table[ARCHTABLE]; |
44 | static archetype *arch_table[ARCHTABLE]; |
44 | int arch_cmp = 0; /* How many strcmp's */ |
45 | int arch_cmp = 0; /* How many strcmp's */ |
45 | int arch_search = 0; /* How many searches */ |
46 | int arch_search = 0; /* How many searches */ |
46 | int arch_init; /* True if doing arch initialization */ |
47 | int arch_init; /* True if doing arch initialization */ |
… | |
… | |
53 | * MSW 2003-04-29 |
54 | * MSW 2003-04-29 |
54 | */ |
55 | */ |
55 | |
56 | |
56 | #if USE_UNORDERED_MAP |
57 | #if USE_UNORDERED_MAP |
57 | // the hashtable |
58 | // the hashtable |
58 | typedef std::tr1::unordered_map<size_t, archetype *> HT; |
59 | typedef |
|
|
60 | std::tr1::unordered_map < |
|
|
61 | size_t, archetype *> |
|
|
62 | HT; |
59 | |
63 | |
60 | static HT ht; |
64 | static |
|
|
65 | HT |
|
|
66 | ht; |
61 | #endif |
67 | #endif |
62 | |
68 | |
63 | /** |
69 | /** |
64 | * GROS - This function retrieves an archetype given the name that appears |
70 | * GROS - This function retrieves an archetype given the name that appears |
65 | * during the game (for example, "writing pen" instead of "stylus"). |
71 | * during the game (for example, "writing pen" instead of "stylus"). |
… | |
… | |
72 | * - the archetype found or null if nothing was found. |
78 | * - the archetype found or null if nothing was found. |
73 | */ |
79 | */ |
74 | archetype * |
80 | archetype * |
75 | find_archetype_by_object_name (const char *name) |
81 | find_archetype_by_object_name (const char *name) |
76 | { |
82 | { |
77 | archetype *at; |
83 | archetype * |
|
|
84 | at; |
78 | |
85 | |
79 | if (name == NULL) |
86 | if (name == NULL) |
80 | return (archetype *) NULL; |
87 | return (archetype *) NULL; |
81 | |
88 | |
82 | for (at = first_archetype; at != NULL; at = at->next) |
89 | for (at = first_archetype; at != NULL; at = at->next) |
… | |
… | |
93 | * except that it considers only items of the given type. |
100 | * except that it considers only items of the given type. |
94 | */ |
101 | */ |
95 | archetype * |
102 | archetype * |
96 | find_archetype_by_object_type_name (int type, const char *name) |
103 | find_archetype_by_object_type_name (int type, const char *name) |
97 | { |
104 | { |
98 | archetype *at; |
105 | archetype * |
|
|
106 | at; |
99 | |
107 | |
100 | if (name == NULL) |
108 | if (name == NULL) |
101 | return NULL; |
109 | return NULL; |
102 | |
110 | |
103 | for (at = first_archetype; at != NULL; at = at->next) |
111 | for (at = first_archetype; at != NULL; at = at->next) |
… | |
… | |
204 | * If count is 1, make a quick check on the name. |
212 | * 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. |
213 | * IF count is >1, we need to make plural name. Return if match. |
206 | * Last, make a check on the full name. |
214 | * Last, make a check on the full name. |
207 | */ |
215 | */ |
208 | int |
216 | int |
209 | item_matched_string (object * pl, object * op, const char *name) |
217 | item_matched_string (object *pl, object *op, const char *name) |
210 | { |
218 | { |
211 | char * |
219 | char * |
212 | cp, |
220 | cp, |
213 | local_name[MAX_BUF]; |
221 | local_name[MAX_BUF]; |
214 | int |
222 | int |
215 | count, |
223 | count, |
216 | retval = 0; |
224 | retval = 0; |
|
|
225 | |
217 | strcpy (local_name, name); /* strtok is destructive to name */ |
226 | strcpy (local_name, name); /* strtok is destructive to name */ |
218 | |
227 | |
219 | for (cp = strtok (local_name, ","); cp; cp = strtok (NULL, ",")) |
228 | for (cp = strtok (local_name, ","); cp; cp = strtok (NULL, ",")) |
220 | { |
229 | { |
221 | while (cp[0] == ' ') |
230 | while (cp[0] == ' ') |
… | |
… | |
326 | return; |
335 | return; |
327 | arch_init = 1; |
336 | arch_init = 1; |
328 | load_archetypes (); |
337 | load_archetypes (); |
329 | arch_init = 0; |
338 | arch_init = 0; |
330 | empty_archetype = find_archetype ("empty_archetype"); |
339 | empty_archetype = find_archetype ("empty_archetype"); |
|
|
340 | |
331 | /* init_blocksview();*/ |
341 | /* init_blocksview();*/ |
332 | } |
342 | } |
333 | |
343 | |
334 | /* |
344 | /* |
335 | * Stores debug-information about how efficient the hashtable |
345 | * Stores debug-information about how efficient the hashtable |
336 | * used for archetypes has been in the static errmsg array. |
346 | * used for archetypes has been in the static errmsg array. |
337 | */ |
347 | */ |
338 | |
348 | |
339 | void |
349 | void |
340 | arch_info (object * op) |
350 | arch_info (object *op) |
341 | { |
351 | { |
342 | sprintf (errmsg, "%d searches and %d strcmp()'s", arch_search, arch_cmp); |
352 | sprintf (errmsg, "%d searches and %d strcmp()'s", arch_search, arch_cmp); |
343 | new_draw_info (NDI_BLACK, 0, op, errmsg); |
353 | new_draw_info (NDI_BLACK, 0, op, errmsg); |
344 | } |
354 | } |
345 | |
355 | |
… | |
… | |
358 | */ |
368 | */ |
359 | |
369 | |
360 | void |
370 | void |
361 | init_archetable (void) |
371 | init_archetable (void) |
362 | { |
372 | { |
363 | archetype *at; |
373 | archetype * |
|
|
374 | at; |
364 | |
375 | |
365 | LOG (llevDebug, " Setting up archetable...\n"); |
376 | LOG (llevDebug, " Setting up archetable...\n"); |
366 | |
377 | |
367 | for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) |
378 | for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) |
368 | add_arch (at); |
379 | add_arch (at); |
… | |
… | |
373 | /* |
384 | /* |
374 | * Dumps an archetype to debug-level output. |
385 | * Dumps an archetype to debug-level output. |
375 | */ |
386 | */ |
376 | |
387 | |
377 | void |
388 | void |
378 | dump_arch (archetype * at) |
389 | dump_arch (archetype *at) |
379 | { |
390 | { |
380 | dump_object (&at->clone); |
391 | dump_object (&at->clone); |
381 | } |
392 | } |
382 | |
393 | |
383 | /* |
394 | /* |
… | |
… | |
389 | void |
400 | void |
390 | dump_all_archetypes (void) |
401 | dump_all_archetypes (void) |
391 | { |
402 | { |
392 | archetype * |
403 | archetype * |
393 | at; |
404 | at; |
|
|
405 | |
394 | for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) |
406 | for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) |
395 | { |
407 | { |
396 | dump_arch (at); |
408 | dump_arch (at); |
397 | fprintf (logfile, "%s\n", errmsg); |
409 | fprintf (logfile, "%s\n", errmsg); |
398 | } |
410 | } |
… | |
… | |
414 | else |
426 | else |
415 | next = at->next; |
427 | next = at->next; |
416 | |
428 | |
417 | delete |
429 | delete |
418 | at; |
430 | at; |
|
|
431 | |
419 | i++; |
432 | i++; |
420 | } |
433 | } |
421 | LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f); |
434 | LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f); |
422 | } |
435 | } |
423 | |
436 | |
424 | /* |
437 | archetype::archetype () |
425 | * Allocates, initialises and returns the pointer to an archetype structure. |
|
|
426 | */ |
|
|
427 | // TODO: should become constructor |
|
|
428 | archetype * |
|
|
429 | get_archetype_struct (void) |
|
|
430 | { |
438 | { |
431 | archetype * |
|
|
432 | arch; |
|
|
433 | |
|
|
434 | arch = new archetype; |
|
|
435 | |
|
|
436 | clear_object (&arch->clone); /* to initial state other also */ |
439 | clear_object (&clone); /* to initial state other also */ |
437 | CLEAR_FLAG (&arch->clone, FLAG_FREED); /* This shouldn't matter, since copy_object() */ |
440 | CLEAR_FLAG (&clone, FLAG_FREED); /* This shouldn't matter, since copy_object() */ |
438 | SET_FLAG (&arch->clone, FLAG_REMOVED); /* doesn't copy these flags... */ |
441 | SET_FLAG (&clone, FLAG_REMOVED); /* doesn't copy these flags... */ |
|
|
442 | } |
439 | |
443 | |
440 | return arch; |
444 | archetype::~archetype () |
|
|
445 | { |
441 | } |
446 | } |
442 | |
447 | |
443 | /* |
448 | /* |
444 | * Reads/parses the archetype-file, and copies into a linked list |
449 | * Reads/parses the archetype-file, and copies into a linked list |
445 | * of archetype-structures. |
450 | * of archetype-structures. |
446 | */ |
451 | */ |
447 | void |
452 | void |
448 | first_arch_pass (object_thawer & fp) |
453 | first_arch_pass (object_thawer & fp) |
449 | { |
454 | { |
450 | object * |
|
|
451 | op; |
|
|
452 | archetype * |
455 | archetype * |
453 | at, * |
456 | at, * |
454 | head = NULL, *last_more = NULL; |
457 | head = NULL, *last_more = NULL; |
455 | int |
|
|
456 | i; |
|
|
457 | |
458 | |
458 | op = get_object (); |
|
|
459 | op->arch = first_archetype = at = get_archetype_struct (); |
459 | at->clone.arch = first_archetype = at = new archetype; |
460 | |
460 | |
461 | while ((i = load_object (fp, op, 0))) |
461 | while (int i = load_object (fp, &at->clone, 0)) |
462 | { |
462 | { |
463 | copy_object (op, &at->clone); |
|
|
464 | at->clone.speed_left = (float) (-0.1); |
463 | at->clone.speed_left = (float) (-0.1); |
465 | /* copy the body_info to the body_used - this is only really |
464 | /* copy the body_info to the body_used - this is only really |
466 | * need for monsters, but doesn't hurt to do it for everything. |
465 | * need for monsters, but doesn't hurt to do it for everything. |
467 | * by doing so, when a monster is created, it has good starting |
466 | * by doing so, when a monster is created, it has good starting |
468 | * values for the body_used info, so when items are created |
467 | * values for the body_used info, so when items are created |
469 | * for it, they can be properly equipped. |
468 | * for it, they can be properly equipped. |
470 | */ |
469 | */ |
471 | memcpy (&at->clone.body_used, &op->body_info, sizeof (op->body_info)); |
470 | memcpy (&at->clone.body_used, &at->clone.body_info, sizeof (at->clone.body_info)); |
472 | |
471 | |
473 | switch (i) |
472 | switch (i) |
474 | { |
473 | { |
475 | case LL_NORMAL: /* A new archetype, just link it with the previous */ |
474 | case LL_NORMAL: /* A new archetype, just link it with the previous */ |
476 | if (last_more != NULL) |
475 | if (last_more != NULL) |
477 | last_more->next = at; |
476 | last_more->next = at; |
478 | if (head != NULL) |
477 | if (head != NULL) |
479 | head->next = at; |
478 | head->next = at; |
480 | head = last_more = at; |
479 | head = last_more = at; |
481 | #if 0 |
480 | #if 0 |
482 | if (!op->type) |
481 | if (!op->type) |
483 | LOG (llevDebug, " WARNING: Archetype %s has no type info!\n", op->arch->name); |
482 | LOG (llevDebug, " WARNING: Archetype %s has no type info!\n", op->arch->name); |
484 | #endif |
483 | #endif |
485 | at->tail_x = 0; |
484 | at->tail_x = 0; |
486 | at->tail_y = 0; |
485 | at->tail_y = 0; |
487 | break; |
486 | break; |
488 | |
487 | |
489 | case LL_MORE: /* Another part of the previous archetype, link it correctly */ |
488 | case LL_MORE: /* Another part of the previous archetype, link it correctly */ |
490 | |
489 | |
491 | at->head = head; |
490 | at->head = head; |
492 | at->clone.head = &head->clone; |
491 | at->clone.head = &head->clone; |
493 | if (last_more != NULL) |
492 | if (last_more != NULL) |
494 | { |
493 | { |
495 | last_more->more = at; |
494 | last_more->more = at; |
496 | last_more->clone.more = &at->clone; |
495 | last_more->clone.more = &at->clone; |
497 | } |
496 | } |
498 | last_more = at; |
497 | last_more = at; |
499 | |
498 | |
500 | /* If this multipart image is still composed of individual small |
499 | /* If this multipart image is still composed of individual small |
501 | * images, don't set the tail_.. values. We can't use them anyways, |
500 | * images, don't set the tail_.. values. We can't use them anyways, |
502 | * and setting these to zero makes the map sending to the client much |
501 | * and setting these to zero makes the map sending to the client much |
503 | * easier as just looking at the head, we know what to do. |
502 | * easier as just looking at the head, we know what to do. |
504 | */ |
503 | */ |
505 | if (at->clone.face != head->clone.face) |
504 | if (at->clone.face != head->clone.face) |
506 | { |
505 | { |
507 | head->tail_x = 0; |
506 | head->tail_x = 0; |
508 | head->tail_y = 0; |
507 | head->tail_y = 0; |
509 | } |
508 | } |
510 | else |
509 | else |
511 | { |
510 | { |
512 | if (at->clone.x > head->tail_x) |
511 | if (at->clone.x > head->tail_x) |
513 | head->tail_x = at->clone.x; |
512 | head->tail_x = at->clone.x; |
514 | if (at->clone.y > head->tail_y) |
513 | if (at->clone.y > head->tail_y) |
515 | head->tail_y = at->clone.y; |
514 | head->tail_y = at->clone.y; |
516 | } |
515 | } |
517 | break; |
516 | break; |
518 | |
517 | |
519 | } |
518 | } |
520 | |
519 | |
521 | at = get_archetype_struct (); |
520 | at = new archetype; |
522 | clear_object (op); |
521 | |
523 | op->arch = at; |
522 | at->clone.arch = at; |
524 | } |
523 | } |
525 | |
524 | |
526 | delete |
525 | delete at; |
527 | at; |
|
|
528 | free_object (op); |
|
|
529 | } |
526 | } |
530 | |
527 | |
531 | /* |
528 | /* |
532 | * Reads the archetype file once more, and links all pointers between |
529 | * Reads the archetype file once more, and links all pointers between |
533 | * archetypes. |
530 | * archetypes. |
… | |
… | |
575 | { |
572 | { |
576 | if (at != NULL) |
573 | if (at != NULL) |
577 | { |
574 | { |
578 | treasurelist * |
575 | treasurelist * |
579 | tl = find_treasurelist (argument); |
576 | tl = find_treasurelist (argument); |
|
|
577 | |
580 | if (tl == NULL) |
578 | if (tl == NULL) |
581 | LOG (llevError, "Failed to link treasure to arch (%s): %s\n", &at->name, argument); |
579 | LOG (llevError, "Failed to link treasure to arch (%s): %s\n", &at->name, argument); |
582 | else |
580 | else |
583 | at->clone.randomitems = tl; |
581 | at->clone.randomitems = tl; |
584 | } |
582 | } |
… | |
… | |
590 | void |
588 | void |
591 | check_generators (void) |
589 | check_generators (void) |
592 | { |
590 | { |
593 | archetype * |
591 | archetype * |
594 | at; |
592 | at; |
|
|
593 | |
595 | for (at = first_archetype; at != NULL; at = at->next) |
594 | for (at = first_archetype; at != NULL; at = at->next) |
596 | if (QUERY_FLAG (&at->clone, FLAG_GENERATOR) && at->clone.other_arch == NULL) |
595 | if (QUERY_FLAG (&at->clone, FLAG_GENERATOR) && at->clone.other_arch == NULL) |
597 | LOG (llevError, "Warning: %s is generator but lacks other_arch.\n", &at->name); |
596 | LOG (llevError, "Warning: %s is generator but lacks other_arch.\n", &at->name); |
598 | } |
597 | } |
599 | #endif |
598 | #endif |
… | |
… | |
608 | void |
607 | void |
609 | load_archetypes (void) |
608 | load_archetypes (void) |
610 | { |
609 | { |
611 | char |
610 | char |
612 | filename[MAX_BUF]; |
611 | filename[MAX_BUF]; |
613 | int |
612 | |
614 | comp; |
|
|
615 | #if TIME_ARCH_LOAD |
613 | #if TIME_ARCH_LOAD |
616 | struct timeval |
614 | struct timeval |
617 | tv1, |
615 | tv1, |
618 | tv2; |
616 | tv2; |
619 | #endif |
617 | #endif |
… | |
… | |
654 | * Creates and returns a new object which is a copy of the given archetype. |
652 | * Creates and returns a new object which is a copy of the given archetype. |
655 | * This function returns NULL on failure. |
653 | * This function returns NULL on failure. |
656 | */ |
654 | */ |
657 | |
655 | |
658 | object * |
656 | object * |
659 | arch_to_object (archetype * at) |
657 | arch_to_object (archetype *at) |
660 | { |
658 | { |
661 | object * |
659 | object * |
662 | op; |
660 | op; |
|
|
661 | |
663 | if (at == NULL) |
662 | if (at == NULL) |
664 | { |
663 | { |
665 | if (warn_archetypes) |
664 | if (warn_archetypes) |
666 | LOG (llevError, "Couldn't find archetype.\n"); |
665 | LOG (llevError, "Couldn't find archetype.\n"); |
|
|
666 | |
667 | return NULL; |
667 | return NULL; |
668 | } |
668 | } |
|
|
669 | |
669 | op = get_object (); |
670 | op = get_object (); |
670 | copy_object (&at->clone, op); |
671 | copy_object (&at->clone, op); |
|
|
672 | op->arch = at; |
671 | op->instantiate (); |
673 | op->instantiate (); |
672 | op->arch = at; |
|
|
673 | return op; |
674 | return op; |
674 | } |
675 | } |
675 | |
676 | |
676 | /* |
677 | /* |
677 | * Creates an object. This function is called by get_archetype() |
678 | * Creates an object. This function is called by get_archetype() |
… | |
… | |
685 | { |
686 | { |
686 | object * |
687 | object * |
687 | op; |
688 | op; |
688 | char |
689 | char |
689 | buf[MAX_BUF]; |
690 | buf[MAX_BUF]; |
|
|
691 | |
690 | sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name); |
692 | sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name); |
691 | op = get_object (); |
693 | op = get_object (); |
692 | op->name = op->name_pl = buf; |
694 | op->name = op->name_pl = buf; |
693 | SET_FLAG (op, FLAG_NO_PICK); |
695 | SET_FLAG (op, FLAG_NO_PICK); |
694 | return op; |
696 | return op; |
… | |
… | |
702 | object * |
704 | object * |
703 | get_archetype (const char *name) |
705 | get_archetype (const char *name) |
704 | { |
706 | { |
705 | archetype * |
707 | archetype * |
706 | at; |
708 | at; |
|
|
709 | |
707 | at = find_archetype (name); |
710 | at = find_archetype (name); |
708 | if (at == NULL) |
711 | if (at == NULL) |
709 | return create_singularity (name); |
712 | return create_singularity (name); |
|
|
713 | |
710 | return arch_to_object (at); |
714 | return arch_to_object (at); |
711 | } |
715 | } |
712 | |
716 | |
713 | /* |
717 | /* |
714 | * Hash-function used by the arch-hashtable. |
718 | * Hash-function used by the arch-hashtable. |
… | |
… | |
755 | name = shstr::find (name); |
759 | name = shstr::find (name); |
756 | |
760 | |
757 | if (!name) |
761 | if (!name) |
758 | return 0; |
762 | return 0; |
759 | |
763 | |
760 | HT::const_iterator i = ht.find ((size_t)name); |
764 | HT::const_iterator i = ht.find ((size_t) name); |
761 | |
765 | |
762 | if (i == ht.end ()) |
766 | if (i == ht.end ()) |
763 | return 0; |
767 | return 0; |
764 | else |
768 | else |
765 | return i->second; |
769 | return i->second; |
766 | #endif |
770 | #endif |
767 | |
771 | |
768 | archetype *at; |
772 | archetype * |
|
|
773 | at; |
769 | unsigned long index; |
774 | unsigned long |
|
|
775 | index; |
770 | |
776 | |
771 | if (name == NULL) |
777 | if (name == NULL) |
772 | return (archetype *) NULL; |
778 | return (archetype *) NULL; |
773 | |
779 | |
774 | index = hasharch (name, ARCHTABLE); |
780 | index = hasharch (name, ARCHTABLE); |
… | |
… | |
796 | |
802 | |
797 | static void |
803 | static void |
798 | add_arch (archetype *at) |
804 | add_arch (archetype *at) |
799 | { |
805 | { |
800 | #if USE_UNORDERED_MAP |
806 | #if USE_UNORDERED_MAP |
801 | ht.insert (std::make_pair ((size_t)(const char *)at->name, at)); |
807 | ht.insert (std::make_pair ((size_t) (const char *) at->name, at)); |
802 | #endif |
808 | #endif |
803 | |
809 | |
|
|
810 | int |
804 | int index = hasharch ((const char *) at->name, ARCHTABLE), org_index = index; |
811 | index = hasharch ((const char *) at->name, ARCHTABLE), org_index = index; |
805 | |
812 | |
806 | for (;;) |
813 | for (;;) |
807 | { |
814 | { |
808 | if (arch_table[index] == NULL) |
815 | if (arch_table[index] == NULL) |
809 | { |
816 | { |
… | |
… | |
864 | /* |
871 | /* |
865 | * member: make instance from class |
872 | * member: make instance from class |
866 | */ |
873 | */ |
867 | |
874 | |
868 | object * |
875 | object * |
869 | object_create_arch (archetype * at) |
876 | object_create_arch (archetype *at) |
870 | { |
877 | { |
871 | object * |
878 | object * |
872 | op, * |
879 | op, * |
873 | prev = NULL, *head = NULL; |
880 | prev = NULL, *head = NULL; |
874 | |
881 | |