--- deliantra/server/common/treasure.C 2007/03/14 00:04:58 1.40 +++ deliantra/server/common/treasure.C 2007/04/17 10:06:32 1.46 @@ -22,8 +22,6 @@ * The authors can be reached via e-mail at */ -#define ALLOWED_COMBINATION - /* TREASURE_DEBUG does some checking on the treasurelists after loading. * It is useful for finding bugs in the treasures file. Since it only * slows the startup some (and not actual game play), it is by default @@ -33,27 +31,40 @@ /* TREASURE_VERBOSE enables copious output concerning artifact generation */ -/* #define TREASURE_VERBOSE */ +//#define TREASURE_VERBOSE #include #include #include #include +extern char *spell_mapping[]; + +static treasurelist *first_treasurelist; static void change_treasure (treasure *t, object *op); /* overrule default values */ -extern char *spell_mapping[]; + +typedef std::tr1::unordered_map< + const char *, + treasurelist *, + str_hash, + str_equal, + slice_allocator< std::pair >, + true +> tl_map_t; + +static tl_map_t tl_map; /* * Initialize global archtype pointers: */ - void init_archetype_pointers () { int prev_warn = warn_archetypes; warn_archetypes = 1; + if (ring_arch == NULL) ring_arch = archetype::find ("ring"); if (amulet_arch == NULL) @@ -62,97 +73,116 @@ staff_arch = archetype::find ("staff"); if (crown_arch == NULL) crown_arch = archetype::find ("crown"); + warn_archetypes = prev_warn; } /* - * Allocate and return the pointer to an empty treasurelist structure. + * Searches for the given treasurelist */ - -static treasurelist * -get_empty_treasurelist (void) +treasurelist * +treasurelist::find (const char *name) { - return new treasurelist; + if (!name) + return 0; + + auto (i, tl_map.find (name)); + + if (i == tl_map.end ()) + return 0; + + return i->second; } /* - * Allocate and return the pointer to an empty treasure structure. + * Searches for the given treasurelist in the globally linked list + * of treasurelists which has been built by load_treasures(). */ -//TODO: make this a constructor -static treasure * -get_empty_treasure (void) +treasurelist * +treasurelist::get (const char *name) { - treasure *t = new treasure; + treasurelist *tl = find (name); + + if (!tl) + { + tl = new treasurelist; + + tl->name = name; + tl->next = first_treasurelist; + first_treasurelist = tl; + + tl_map.insert (std::make_pair (tl->name, tl)); + } + + return tl; +} - t->chance = 100; +//TODO: class method +void +clear (treasurelist *tl) +{ + if (tl->items) + { + free_treasurestruct (tl->items); + tl->items = 0; + } - return t; + tl->total_chance = 0; } /* * Reads the lib/treasure file from disk, and parses the contents * into an internal treasure structure (very linked lists) */ - static treasure * -load_treasure (FILE * fp, int *line) +load_treasure (object_thawer &f) { - char buf[MAX_BUF], *cp, variable[MAX_BUF]; - treasure *t = get_empty_treasure (); + treasure *t = new treasure; int value; nroftreasures++; - while (fgets (buf, MAX_BUF, fp) != NULL) + + for (;;) { - (*line)++; + coroapi::cede_to_tick_every (10); - if (*buf == '#') - continue; - if ((cp = strchr (buf, '\n')) != NULL) - *cp = '\0'; - cp = buf; - while (isspace (*cp)) /* Skip blanks */ - cp++; - - if (sscanf (cp, "arch %s", variable)) - { - if ((t->item = archetype::find (variable)) == NULL) - LOG (llevError, "Treasure lacks archetype: %s\n", variable); - } - else if (sscanf (cp, "list %s", variable)) - t->name = variable; - else if (sscanf (cp, "change_name %s", variable)) - t->change_arch.name = variable; - else if (sscanf (cp, "change_title %s", variable)) - t->change_arch.title = variable; - else if (sscanf (cp, "change_slaying %s", variable)) - t->change_arch.slaying = variable; - else if (sscanf (cp, "chance %d", &value)) - t->chance = (uint8) value; - else if (sscanf (cp, "nrof %d", &value)) - t->nrof = (uint16) value; - else if (sscanf (cp, "magic %d", &value)) - t->magic = (uint8) value; - else if (!strcmp (cp, "yes")) - t->next_yes = load_treasure (fp, line); - else if (!strcmp (cp, "no")) - t->next_no = load_treasure (fp, line); - else if (!strcmp (cp, "end")) - return t; - else if (!strcmp (cp, "more")) + f.next (); + + switch (f.kw) { - t->next = load_treasure (fp, line); - return t; + case KW_arch: + if (!(t->item = archetype::find (f.get_str ()))) + LOG (llevError, "%s:%d treasure references unknown archetype '%s', skipping.\n", f.name, f.linenum, f.get_str ()); + break; + + case KW_list: f.get (t->name); break; + case KW_change_name: f.get (t->change_arch.name); break; + case KW_change_title: f.get (t->change_arch.title); break; + case KW_change_slaying: f.get (t->change_arch.slaying); break; + case KW_chance: f.get (t->chance); break; + case KW_nrof: f.get (t->nrof); break; + case KW_magic: f.get (t->magic); break; + + case KW_yes: t->next_yes = load_treasure (f); break; + case KW_no: t->next_no = load_treasure (f); break; + + case KW_end: + return t; + + case KW_more: + t->next = load_treasure (f); + return t; + + default: + if (!f.parse_error ("treasure list")) + return t; // error + + return t; } - else - LOG (llevError, "Unknown treasure-command: '%s', last entry %s, line %d\n", cp, &t->name, *line); } - LOG (llevError, "treasure lacks 'end'.\n"); - return t; } #ifdef TREASURE_DEBUG - /* recursived checks the linked list. Treasurelist is passed only * so that the treasure name can be printed out */ @@ -161,13 +191,13 @@ { if (t->chance >= 100 && t->next_yes && (t->next || t->next_no)) LOG (llevError, "Treasurelist %s has element that has 100%% generation, next_yes field as well as next or next_no\n", &tl->name); - /* find_treasurelist will print out its own error message */ - if (t->name && *t->name) - find_treasurelist (t->name); + if (t->next) check_treasurelist (t->next, tl); + if (t->next_yes) check_treasurelist (t->next_yes, tl); + if (t->next_no) check_treasurelist (t->next_no, tl); } @@ -177,99 +207,82 @@ * Opens LIBDIR/treasure and reads all treasure-declarations from it. * Each treasure is parsed with the help of load_treasure(). */ - -void -load_treasures (void) +bool +load_treasure_file (const char *filename) { - FILE *fp; - char filename[MAX_BUF], buf[MAX_BUF], name[MAX_BUF]; - treasurelist *previous = NULL; treasure *t; int comp, line = 0; - sprintf (filename, "%s/%s", settings.datadir, settings.treasures); - if ((fp = open_and_uncompress (filename, 0, &comp)) == NULL) + object_thawer f (filename); + + if (!f) { LOG (llevError, "Can't open treasure file.\n"); - return; + return false; } - while (fgets (buf, MAX_BUF, fp) != NULL) - { - line++; - if (*buf == '#' || *buf == '\n') - ; // ignore - else if (sscanf (buf, "treasureone %s\n", name) || sscanf (buf, "treasure %s\n", name)) - { - treasurelist *tl = get_empty_treasurelist (); - - tl->name = name; - if (previous == NULL) - first_treasurelist = tl; - else - previous->next = tl; - previous = tl; - tl->items = load_treasure (fp, &line); + f.next (); - /* This is a one of the many items on the list should be generated. - * Add up the chance total, and check to make sure the yes & no - * fields of the treasures are not being used. - */ - if (!strncmp (buf, "treasureone", 11)) + for (;;) + { + switch (f.kw) + { + case KW_treasure: + case KW_treasureone: { - for (t = tl->items; t != NULL; t = t->next) + bool one = f.kw == KW_treasureone; + treasurelist *tl = treasurelist::get (f.get_str ()); + + clear (tl); + tl->items = load_treasure (f); + + if (!tl->items) + return false; + + /* This is a one of the many items on the list should be generated. + * Add up the chance total, and check to make sure the yes & no + * fields of the treasures are not being used. + */ + if (one) { -#ifdef TREASURE_DEBUG - if (t->next_yes || t->next_no) + for (t = tl->items; t; t = t->next) { - LOG (llevError, "Treasure %s is one item, but on treasure %s\n", &tl->name, t->item ? &t->item->name : &t->name); - LOG (llevError, " the next_yes or next_no field is set\n"); - } +#ifdef TREASURE_DEBUG + if (t->next_yes || t->next_no) + { + LOG (llevError, "Treasure %s is one item, but on treasure %s\n", &tl->name, t->item ? &t->item->name : &t->name); + LOG (llevError, " the next_yes or next_no field is set\n"); + } #endif - tl->total_chance += t->chance; + tl->total_chance += t->chance; + } } } - } - else - LOG (llevError, "Treasure-list %s didn't understand: %s, line %d\n", filename, buf, line); - } - close_and_delete (fp, comp); + break; + case KW_EOF: #ifdef TREASURE_DEBUG - /* Perform some checks on how valid the treasure data actually is. - * verify that list transitions work (ie, the list that it is supposed - * to transition to exists). Also, verify that at least the name - * or archetype is set for each treasure element. - */ - for (previous = first_treasurelist; previous != NULL; previous = previous->next) - check_treasurelist (previous->items, previous); + /* Perform some checks on how valid the treasure data actually is. + * verify that list transitions work (ie, the list that it is supposed + * to transition to exists). Also, verify that at least the name + * or archetype is set for each treasure element. + */ + for (treasurelist *tl = first_treasurelist; tl; tl = tl->next) + check_treasurelist (tl->items, tl); #endif -} + return true; -/* - * Searches for the given treasurelist in the globally linked list - * of treasurelists which has been built by load_treasures(). - */ - -treasurelist * -find_treasurelist (const char *name) -{ - shstr_cmp name_ (name); - - if (!name_) - return 0; - - for (treasurelist *tl = first_treasurelist; tl != 0; tl = tl->next) - if (name_ == tl->name) - return tl; + default: + if (!f.parse_error ("treasure lists")) + return false; - if (first_treasurelist) - LOG (llevError, "Couldn't find treasurelist %s\n", name); + break; + } - return 0; + f.next (); + } } - /* * Generates the objects specified by the given treasure. * It goes recursively through the rest of the linked list. @@ -328,27 +341,22 @@ op->slaying = t->change_arch.slaying; } -void +static void create_all_treasures (treasure *t, object *op, int flag, int difficulty, int tries) { - object *tmp; - if ((int) t->chance >= 100 || (rndm (100) + 1) < (int) t->chance) { if (t->name) { if (difficulty >= t->magic) - { - treasurelist *tl = find_treasurelist (t->name); - if (tl) - create_treasure (tl, op, flag, difficulty, tries); - } + create_treasure (treasurelist::find (t->name), op, flag, difficulty, tries); } else { if (t->item && (t->item->clone.invisible != 0 || !(flag & GT_INVISIBLE))) { - tmp = arch_to_object (t->item); + object *tmp = arch_to_object (t->item); + if (t->nrof && tmp->nrof <= 1) tmp->nrof = rndm (t->nrof) + 1; @@ -358,17 +366,17 @@ } } - if (t->next_yes != NULL) + if (t->next_yes) create_all_treasures (t->next_yes, op, flag, difficulty, tries); } - else if (t->next_no != NULL) + else if (t->next_no) create_all_treasures (t->next_no, op, flag, difficulty, tries); - if (t->next != NULL) + if (t->next) create_all_treasures (t->next, op, flag, difficulty, tries); } -void +static void create_one_treasure (treasurelist *tl, object *op, int flag, int difficulty, int tries) { int value = rndm (tl->total_chance); @@ -380,7 +388,7 @@ return; } - for (t = tl->items; t != NULL; t = t->next) + for (t = tl->items; t; t = t->next) { value -= t->chance; @@ -389,39 +397,30 @@ } if (!t || value >= 0) - { - LOG (llevError, "create_one_treasure: got null object or not able to find treasure\n"); - abort (); - return; - } + cleanup ("create_one_treasure: got null object or not able to find treasure\n", 1); if (t->name) { if (difficulty >= t->magic) { - treasurelist *tl = find_treasurelist (t->name); + treasurelist *tl = treasurelist::find (t->name); if (tl) create_treasure (tl, op, flag, difficulty, tries); } else if (t->nrof) create_one_treasure (tl, op, flag, difficulty, tries); - - return; } - - if (t->item && (t->item->clone.invisible != 0 || flag != GT_INVISIBLE)) + else if (t->item && (t->item->clone.invisible != 0 || flag != GT_INVISIBLE)) { - object *tmp = arch_to_object (t->item); - - if (!tmp) - return; - - if (t->nrof && tmp->nrof <= 1) - tmp->nrof = rndm (t->nrof) + 1; + if (object *tmp = arch_to_object (t->item)) + { + if (t->nrof && tmp->nrof <= 1) + tmp->nrof = rndm (t->nrof) + 1; - fix_generated_item (tmp, op, difficulty, t->magic, flag); - change_treasure (t, tmp); - put_treasure (tmp, op, flag); + fix_generated_item (tmp, op, difficulty, t->magic, flag); + change_treasure (t, tmp); + put_treasure (tmp, op, flag); + } } } @@ -435,6 +434,10 @@ void create_treasure (treasurelist *tl, object *op, int flag, int difficulty, int tries) { + // empty treasurelists are legal + if (!tl->items) + return; + if (tries++ > 100) { LOG (llevDebug, "createtreasure: tries exceeded 100, returning without making treasure\n"); @@ -482,40 +485,39 @@ static int difftomagic_list[DIFFLEVELS][MAXMAGIC + 1] = { -/*chance of magic difficulty*/ - -/* +0 +1 +2 +3 +4 */ - {95, 2, 2, 1, 0}, /*1 */ - {92, 5, 2, 1, 0}, /*2 */ - {85, 10, 4, 1, 0}, /*3 */ - {80, 14, 4, 2, 0}, /*4 */ - {75, 17, 5, 2, 1}, /*5 */ - {70, 18, 8, 3, 1}, /*6 */ - {65, 21, 10, 3, 1}, /*7 */ - {60, 22, 12, 4, 2}, /*8 */ - {55, 25, 14, 4, 2}, /*9 */ - {50, 27, 16, 5, 2}, /*10 */ - {45, 28, 18, 6, 3}, /*11 */ - {42, 28, 20, 7, 3}, /*12 */ - {40, 27, 21, 8, 4}, /*13 */ - {38, 25, 22, 10, 5}, /*14 */ - {36, 23, 23, 12, 6}, /*15 */ - {33, 21, 24, 14, 8}, /*16 */ - {31, 19, 25, 16, 9}, /*17 */ - {27, 15, 30, 18, 10}, /*18 */ - {20, 12, 30, 25, 13}, /*19 */ - {15, 10, 28, 30, 17}, /*20 */ - {13, 9, 27, 28, 23}, /*21 */ - {10, 8, 25, 28, 29}, /*22 */ - {8, 7, 23, 26, 36}, /*23 */ - {6, 6, 20, 22, 46}, /*24 */ - {4, 5, 17, 18, 56}, /*25 */ - {2, 4, 12, 14, 68}, /*26 */ - {0, 3, 7, 10, 80}, /*27 */ - {0, 0, 3, 7, 90}, /*28 */ - {0, 0, 0, 3, 97}, /*29 */ - {0, 0, 0, 0, 100}, /*30 */ - {0, 0, 0, 0, 100}, /*31 */ +// chance of magic difficulty +// +0 +1 +2 +3 +4 + {95, 2, 2, 1, 0}, // 1 + {92, 5, 2, 1, 0}, // 2 + {85, 10, 4, 1, 0}, // 3 + {80, 14, 4, 2, 0}, // 4 + {75, 17, 5, 2, 1}, // 5 + {70, 18, 8, 3, 1}, // 6 + {65, 21, 10, 3, 1}, // 7 + {60, 22, 12, 4, 2}, // 8 + {55, 25, 14, 4, 2}, // 9 + {50, 27, 16, 5, 2}, // 10 + {45, 28, 18, 6, 3}, // 11 + {42, 28, 20, 7, 3}, // 12 + {40, 27, 21, 8, 4}, // 13 + {38, 25, 22, 10, 5}, // 14 + {36, 23, 23, 12, 6}, // 15 + {33, 21, 24, 14, 8}, // 16 + {31, 19, 25, 16, 9}, // 17 + {27, 15, 30, 18, 10}, // 18 + {20, 12, 30, 25, 13}, // 19 + {15, 10, 28, 30, 17}, // 20 + {13, 9, 27, 28, 23}, // 21 + {10, 8, 25, 28, 29}, // 22 + { 8, 7, 23, 26, 36}, // 23 + { 6, 6, 20, 22, 46}, // 24 + { 4, 5, 17, 18, 56}, // 25 + { 2, 4, 12, 14, 68}, // 26 + { 0, 3, 7, 10, 80}, // 27 + { 0, 0, 3, 7, 90}, // 28 + { 0, 0, 0, 3, 97}, // 29 + { 0, 0, 0, 0, 100}, // 30 + { 0, 0, 0, 0, 100}, // 31 }; @@ -558,7 +560,6 @@ * weird integer between 1-31. * */ - int magic_from_difficulty (int difficulty) { @@ -656,7 +657,6 @@ * other bonuses previously rolled and ones the item might natively have. * 2) Add code to deal with new PR method. */ - void set_ring_bonus (object *op, int bonus) { @@ -849,10 +849,10 @@ { create_treasure (op->randomitems, op, flags, difficulty, 0); if (!op->inv) - LOG (llevDebug, "fix_generated_item: Unable to generate treasure for %s\n", &op->name); + LOG (llevDebug, "fix_generated_item: Unable to generate treasure for %s\n", op->debug_desc ()); /* So the treasure doesn't get created again */ - op->randomitems = NULL; + op->randomitems = 0; } if (difficulty < 1) @@ -1178,14 +1178,7 @@ static artifactlist * get_empty_artifactlist (void) { - artifactlist *al = (artifactlist *) malloc (sizeof (artifactlist)); - - if (al == NULL) - fatal (OUT_OF_MEMORY); - al->next = NULL; - al->items = NULL; - al->total_chance = 0; - return al; + return salloc0 (); } /* @@ -1194,17 +1187,7 @@ static artifact * get_empty_artifact (void) { - artifact *a = (artifact *) malloc (sizeof (artifact)); - - if (a == NULL) - fatal (OUT_OF_MEMORY); - - a->item = NULL; - a->next = NULL; - a->chance = 0; - a->difficulty = 0; - a->allowed = NULL; - return a; + return salloc0 (); } /* @@ -1214,9 +1197,7 @@ artifactlist * find_artifactlist (int type) { - artifactlist *al; - - for (al = first_artifactlist; al; al = al->next) + for (artifactlist *al = first_artifactlist; al; al = al->next) if (al->type == type) return al; @@ -1263,24 +1244,30 @@ if (depth > 100) return; + while (t) { if (t->name) { for (i = 0; i < depth; i++) fprintf (logfile, " "); + fprintf (logfile, "{ (list: %s)\n", &t->name); - tl = find_treasurelist (t->name); + + tl = treasurelist::find (t->name); if (tl) dump_monster_treasure_rec (name, tl->items, depth + 2); + for (i = 0; i < depth; i++) fprintf (logfile, " "); + fprintf (logfile, "} (end of list: %s)\n", &t->name); } else { for (i = 0; i < depth; i++) fprintf (logfile, " "); + if (t->item && t->item->clone.type == FLESH) fprintf (logfile, "%s's %s\n", name, &t->item->clone.name); else @@ -1291,6 +1278,7 @@ { for (i = 0; i < depth; i++) fprintf (logfile, " "); + fprintf (logfile, " (if yes)\n"); dump_monster_treasure_rec (name, t->next_yes, depth + 1); } @@ -1299,6 +1287,7 @@ { for (i = 0; i < depth; i++) fprintf (logfile, " "); + fprintf (logfile, " (if no)\n"); dump_monster_treasure_rec (name, t->next_no, depth + 1); } @@ -1311,7 +1300,6 @@ * For debugging purposes. Dumps all treasures for a given monster. * Created originally by Raphael Quinet for debugging the alchemy code. */ - void dump_monster_treasure (const char *name) { @@ -1471,7 +1459,7 @@ if (change->face != blank_face) { #ifdef TREASURE_VERBOSE - LOG (llevDebug, "FACE: %d\n", change->face->number); + LOG (llevDebug, "add_abilities change face: %d\n", change->face); #endif op->face = change->face; } @@ -1662,7 +1650,7 @@ for (tmp = art->allowed; tmp; tmp = tmp->next) { #ifdef TREASURE_VERBOSE - LOG (llevDebug, "legal_art: %s\n", tmp->name); + LOG (llevDebug, "legal_art: %s\n", &tmp->name); #endif if (*tmp->name == '!') name = tmp->name + 1, neg = 1; @@ -1767,10 +1755,11 @@ if (!legal_artifact_combination (op, art)) { #ifdef TREASURE_VERBOSE - LOG (llevDebug, "%s of %s was not a legal combination.\n", op->name, art->item->name); + LOG (llevDebug, "%s of %s was not a legal combination.\n", &op->name, &art->item->name); #endif continue; } + give_artifact_abilities (op, art->item); return; } @@ -1844,12 +1833,9 @@ void free_treasurestruct (treasure *t) { - if (t->next) - free_treasurestruct (t->next); - if (t->next_yes) - free_treasurestruct (t->next_yes); - if (t->next_no) - free_treasurestruct (t->next_no); + if (t->next) free_treasurestruct (t->next); + if (t->next_yes) free_treasurestruct (t->next_yes); + if (t->next_no) free_treasurestruct (t->next_no); delete t; } @@ -1864,21 +1850,18 @@ } void -free_artifact (artifact * at) +free_artifact (artifact *at) { - if (at->next) - free_artifact (at->next); - - if (at->allowed) - free_charlinks (at->allowed); + if (at->next) free_artifact (at->next); + if (at->allowed) free_charlinks (at->allowed); at->item->destroy (1); - delete at; + sfree (at); } void -free_artifactlist (artifactlist * al) +free_artifactlist (artifactlist *al) { artifactlist *nextal; @@ -1889,7 +1872,7 @@ if (al->items) free_artifact (al->items); - free (al); + sfree (al); } } @@ -1898,13 +1881,13 @@ { treasurelist *tl, *next; - - for (tl = first_treasurelist; tl != NULL; tl = next) + for (tl = first_treasurelist; tl; tl = next) { + clear (tl); + next = tl->next; - if (tl->items) - free_treasurestruct (tl->items); delete tl; } + free_artifactlist (first_artifactlist); }