--- deliantra/server/common/treasure.C 2007/04/16 06:23:40 1.41 +++ deliantra/server/common/treasure.C 2007/04/28 17:51:57 1.58 @@ -1,5 +1,5 @@ /* - * CrossFire, A Multiplayer game for X-windows + * CrossFire, A Multiplayer game * * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team * Copyright (C) 2002 Mark Wedel & Crossfire Development Team @@ -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 @@ -58,30 +56,7 @@ 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) - amulet_arch = archetype::find ("amulet"); - if (staff_arch == NULL) - staff_arch = archetype::find ("staff"); - if (crown_arch == NULL) - crown_arch = archetype::find ("crown"); - - warn_archetypes = prev_warn; -} - -/* - * Searches for the given treasurelist in the globally linked list - * of treasurelists which has been built by load_treasures(). + * Searches for the given treasurelist */ treasurelist * treasurelist::find (const char *name) @@ -89,7 +64,7 @@ if (!name) return 0; - AUTODECL (i, tl_map.find (name)); + auto (i, tl_map.find (name)); if (i == tl_map.end ()) return 0; @@ -129,72 +104,11 @@ free_treasurestruct (tl->items); tl->items = 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) -{ - char buf[MAX_BUF], *cp, variable[MAX_BUF]; - treasure *t = new treasure; - int value; - - nroftreasures++; - while (fgets (buf, MAX_BUF, fp) != NULL) - { - (*line)++; - 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")) - { - t->next = load_treasure (fp, line); - 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; + tl->total_chance = 0; } #ifdef TREASURE_DEBUG - /* recursived checks the linked list. Treasurelist is passed only * so that the treasure name can be printed out */ @@ -216,70 +130,90 @@ #endif /* - * Opens LIBDIR/treasure and reads all treasure-declarations from it. - * Each treasure is parsed with the help of load_treasure(). + * Reads the lib/treasure file from disk, and parses the contents + * into an internal treasure structure (very linked lists) */ - -void -load_treasures (void) +static treasure * +read_treasure (object_thawer &f) { - FILE *fp; - char filename[MAX_BUF], buf[MAX_BUF], name[MAX_BUF]; - treasure *t; - int comp, line = 0; + treasure *t = new treasure; - sprintf (filename, "%s/%s", settings.datadir, settings.treasures); - if ((fp = open_and_uncompress (filename, 0, &comp)) == NULL) - { - LOG (llevError, "Can't open treasure file.\n"); - return; - } - 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)) + coroapi::cede_to_tick_every (10); + + switch (f.kw) { - treasurelist *tl = treasurelist::get (name); + case KW_arch: + t->item = archetype::get (f.get_str ()); + break; - clear (tl); - tl->items = load_treasure (fp, &line); + 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 = read_treasure (f); continue; + case KW_no: t->next_no = read_treasure (f); continue; + + case KW_end: + f.next (); + return t; + + case KW_more: + t->next = read_treasure (f); + return t; - /* 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) - { -#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; - } - } + default: + if (!f.parse_error ("treasurelist", t->name)) + return 0; + + return t; } - else - LOG (llevError, "Treasure-list %s didn't understand: %s, line %d\n", filename, buf, line); + + f.next (); } +} + +/* + * Each treasure is parsed with the help of load_treasure(). + */ +treasurelist * +treasurelist::read (object_thawer &f) +{ + assert (f.kw == KW_treasure || f.kw == KW_treasureone); - close_and_delete (fp, comp); + bool one = f.kw == KW_treasureone; + treasurelist *tl = treasurelist::get (f.get_str ()); + clear (tl); + tl->items = read_treasure (f); + if (!tl->items) + return 0; -#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. + /* 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. */ - for (treasurelist *tl = first_treasurelist; tl; tl = tl->next) - check_treasurelist (tl->items, tl); -#endif + if (one) + { + for (treasure *t = tl->items; t; t = t->next) + { + 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"); + } + + tl->total_chance += t->chance; + } + } + + return tl; } /* @@ -296,17 +230,29 @@ static void put_treasure (object *op, object *creator, int flags) { - object *tmp; - - /* Bit of a hack - spells should never be put onto the map. The entire - * treasure stuff is a problem - there is no clear idea of knowing - * this is the original object, or if this is an object that should be created - * by another object. - */ - if (flags & GT_ENVIRONMENT && op->type != SPELL) + if (flags & GT_ENVIRONMENT) { - SET_FLAG (op, FLAG_OBJ_ORIGINAL); - op->insert_at (creator, creator, INS_NO_MERGE | INS_NO_WALK_ON); + /* Bit of a hack - spells should never be put onto the map. The entire + * treasure stuff is a problem - there is no clear idea of knowing + * this is the original object, or if this is an object that should be created + * by another object. + */ + //TODO: flag such as objects... as such (no drop, anybody?) + if (op->type == SPELL) + { + op->destroy (); + return; + } + + op->expand_tail (); + + if (ob_blocked (op, creator->map, creator->x, creator->y)) + op->destroy (); + else + { + SET_FLAG (op, FLAG_OBJ_ORIGINAL); + op->insert_at (creator, creator, INS_NO_MERGE | INS_NO_WALK_ON); + } } else { @@ -315,8 +261,9 @@ if ((flags & GT_APPLY) && QUERY_FLAG (creator, FLAG_MONSTER)) monster_check_apply (creator, op); - if ((flags & GT_UPDATE_INV) && (tmp = creator->in_player ())) - esrv_send_item (tmp, op); + if (flags & GT_UPDATE_INV) + if (object *tmp = creator->in_player ()) + esrv_send_item (tmp, op); } } @@ -348,7 +295,10 @@ if (t->name) { if (difficulty >= t->magic) - create_treasure (treasurelist::find (t->name), op, flag, difficulty, tries); + if (treasurelist *tl = treasurelist::find (t->name)) + create_treasure (tl, op, flag, difficulty, tries); + else + LOG (llevError, "create_all_treasures: undefined reference to treasurelist '%s'.\n", &t->name); } else { @@ -423,6 +373,12 @@ } } +void +object::create_treasure (treasurelist *tl, int flags) +{ + ::create_treasure (tl, this, flags, map ? map->difficulty : 0); +} + /* This calls the appropriate treasure creation function. tries is passed * to determine how many list transitions or attempts to create treasure * have been made. It is really in place to prevent infinite loops with @@ -443,6 +399,9 @@ return; } + if (op->flag [FLAG_TREASURE_ENV]) + flag |= GT_ENVIRONMENT; + if (tl->total_chance) create_one_treasure (tl, op, flag, difficulty, tries); else @@ -459,13 +418,13 @@ { difficulty = clamp (difficulty, 1, settings.max_level); - object *ob = object::create (), *tmp; + object *ob = object::create (); create_treasure (tl, ob, 0, difficulty, 0); /* Don't want to free the object we are about to return */ - tmp = ob->inv; - if (tmp != NULL) + object *tmp = ob->inv; + if (tmp) tmp->remove (); if (ob->inv) @@ -483,51 +442,47 @@ */ 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 }; - /* calculate the appropriate level for wands staves and scrolls. * This code presumes that op has had its spell object created (in op->inv) * * elmex Wed Aug 9 17:44:59 CEST 2006: * Removed multiplicator, too many high-level items were generated on low-difficulty maps. */ - int level_for_item (const object *op, int difficulty) { @@ -847,12 +802,9 @@ if (op->randomitems && op->type != SPELL) { - 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); - + create_treasure (op->randomitems, op, flags & ~GT_ENVIRONMENT, difficulty, 0); /* So the treasure doesn't get created again */ - op->randomitems = NULL; + op->randomitems = 0; } if (difficulty < 1) @@ -1356,10 +1308,7 @@ { case KW_allowed: if (!art) - { - art = get_empty_artifact (); - nrofartifacts++; - } + art = get_empty_artifact (); { if (!strcmp (f.get_str (), "all")) @@ -1369,8 +1318,6 @@ do { - nrofallowedstr++; - if ((next = strchr (cp, ','))) *next++ = '\0'; @@ -1395,6 +1342,8 @@ case KW_object: { art->item = object::create (); + f.get (art->item->name); + f.next (); if (!art->item->parse_kv (f)) LOG (llevError, "Init_Artifacts: Could not load object.\n"); @@ -1430,6 +1379,8 @@ done: for (al = first_artifactlist; al; al = al->next) { + al->total_chance = 0; + for (art = al->items; art; art = art->next) { if (!art->chance) @@ -1445,12 +1396,10 @@ LOG (llevDebug, "done.\n"); } - /* * Used in artifact generation. The bonuses of the first object * is modified by the bonuses of the second object. */ - void add_abilities (object *op, object *change) { @@ -1459,7 +1408,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; } @@ -1639,18 +1588,19 @@ } static int -legal_artifact_combination (object *op, artifact * art) +legal_artifact_combination (object *op, artifact *art) { int neg, success = 0; linked_char *tmp; const char *name; - if (art->allowed == (linked_char *) NULL) + if (!art->allowed) return 1; /* Ie, "all" */ + 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; @@ -1667,6 +1617,7 @@ else if (neg) success = 1; } + return success; } @@ -1755,10 +1706,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; } @@ -1812,7 +1764,6 @@ } /* special_potion() - so that old potion code is still done right. */ - int special_potion (object *op) {