--- deliantra/server/common/treasure.C 2007/04/16 06:23:40 1.41 +++ deliantra/server/common/treasure.C 2007/04/16 10:14:25 1.42 @@ -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,7 +31,7 @@ /* TREASURE_VERBOSE enables copious output concerning artifact generation */ -/* #define TREASURE_VERBOSE */ +//#define TREASURE_VERBOSE #include #include @@ -135,66 +133,55 @@ * 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 = new treasure; int value; nroftreasures++; - while (fgets (buf, MAX_BUF, fp) != NULL) + + for (;;) { - (*line)++; + coroapi::cede_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 */ @@ -219,67 +206,83 @@ * 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_treasures () { - FILE *fp; - char filename[MAX_BUF], buf[MAX_BUF], name[MAX_BUF]; treasure *t; int comp, line = 0; + char filename[MAX_BUF]; 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) + + f.next (); + + for (;;) { - line++; - if (*buf == '#' || *buf == '\n') - ; // ignore - else if (sscanf (buf, "treasureone %s\n", name) || sscanf (buf, "treasure %s\n", name)) + switch (f.kw) { - treasurelist *tl = treasurelist::get (name); + case KW_treasure: + case KW_treasureone: + { + bool one = f.kw == KW_treasureone; + treasurelist *tl = treasurelist::get (f.get_str ()); - clear (tl); - tl->items = load_treasure (fp, &line); + clear (tl); + tl->items = load_treasure (f); - /* 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 (t = tl->items; t; t = t->next) + 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 (treasurelist *tl = first_treasurelist; tl; tl = tl->next) - check_treasurelist (tl->items, tl); + /* 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; + + default: + if (!f.parse_error ("treasure lists")) + return false; + + break; + } + + f.next (); + } } /* @@ -484,40 +487,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 }; @@ -849,10 +851,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) @@ -1459,7 +1461,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; } @@ -1650,7 +1652,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; @@ -1755,10 +1757,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; }