--- deliantra/server/common/treasure.C 2006/12/10 20:15:57 1.22 +++ deliantra/server/common/treasure.C 2007/01/20 22:09:51 1.34 @@ -1,25 +1,26 @@ /* - CrossFire, A Multiplayer game for X-windows - - Copyright (C) 2002 Mark Wedel & Crossfire Development Team - Copyright (C) 1992 Frank Tore Johansen - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - - The authors can be reached via e-mail at -*/ + * CrossFire, A Multiplayer game for X-windows + * + * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team + * Copyright (C) 2002 Mark Wedel & Crossfire Development Team + * Copyright (C) 1992 Frank Tore Johansen + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * The authors can be reached via e-mail at + */ #define ALLOWED_COMBINATION @@ -158,13 +159,11 @@ static void check_treasurelist (const treasure *t, const treasurelist * tl) { - if (t->item == NULL && t->name == NULL) - LOG (llevError, "Treasurelist %s has element with no name or archetype\n", &tl->name); 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) - (void) find_treasurelist (t->name); + find_treasurelist (t->name); if (t->next) check_treasurelist (t->next, tl); if (t->next_yes) @@ -260,7 +259,7 @@ if (!name_) return 0; - for (treasurelist * tl = first_treasurelist; tl != 0; tl = tl->next) + for (treasurelist *tl = first_treasurelist; tl != 0; tl = tl->next) if (name_ == tl->name) return tl; @@ -282,8 +281,6 @@ * abilities. This is used by summon spells, thus no summoned monsters * start with equipment, but only their abilities). */ - - static void put_treasure (object *op, object *creator, int flags) { @@ -296,17 +293,17 @@ */ if (flags & GT_ENVIRONMENT && op->type != SPELL) { - op->x = creator->x; - op->y = creator->y; SET_FLAG (op, FLAG_OBJ_ORIGINAL); - insert_ob_in_map (op, creator->map, op, INS_NO_MERGE | INS_NO_WALK_ON); + op->insert_at (creator, creator, INS_NO_MERGE | INS_NO_WALK_ON); } else { - op = insert_ob_in_ob (op, creator); + op = creator->insert (op); + if ((flags & GT_APPLY) && QUERY_FLAG (creator, FLAG_MONSTER)) monster_check_apply (creator, op); - if ((flags & GT_UPDATE_INV) && (tmp = is_player_inv (creator)) != NULL) + + if ((flags & GT_UPDATE_INV) && (tmp = creator->in_player ())) esrv_send_item (tmp, op); } } @@ -336,16 +333,20 @@ { object *tmp; - if ((int) t->chance >= 100 || (RANDOM () % 100 + 1) < (int) t->chance) + if ((int) t->chance >= 100 || (rndm (100) + 1) < (int) t->chance) { if (t->name) { - if (strcmp (t->name, "NONE") && difficulty >= t->magic) - create_treasure (find_treasurelist (t->name), op, flag, difficulty, tries); + if (difficulty >= t->magic) + { + treasurelist *tl = find_treasurelist (t->name); + if (tl) + create_treasure (tl, op, flag, difficulty, tries); + } } else { - if (t->item->clone.invisible != 0 || !(flag & GT_INVISIBLE)) + if (t->item && (t->item->clone.invisible != 0 || !(flag & GT_INVISIBLE))) { tmp = arch_to_object (t->item); if (t->nrof && tmp->nrof <= 1) @@ -395,18 +396,19 @@ if (t->name) { - if (!strcmp (t->name, "NONE")) - return; - if (difficulty >= t->magic) - create_treasure (find_treasurelist (t->name), op, flag, difficulty, tries); + { + treasurelist *tl = find_treasurelist (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) + if (t->item && (t->item->clone.invisible != 0 || flag != GT_INVISIBLE)) { object *tmp = arch_to_object (t->item); @@ -430,7 +432,7 @@ * to do that. */ void -create_treasure (treasurelist * t, object *op, int flag, int difficulty, int tries) +create_treasure (treasurelist *tl, object *op, int flag, int difficulty, int tries) { if (tries++ > 100) @@ -438,10 +440,10 @@ LOG (llevDebug, "createtreasure: tries exceeded 100, returning without making treasure\n"); return; } - if (t->total_chance) - create_one_treasure (t, op, flag, difficulty, tries); + if (tl->total_chance) + create_one_treasure (tl, op, flag, difficulty, tries); else - create_all_treasures (t->items, op, flag, difficulty, tries); + create_all_treasures (tl->items, op, flag, difficulty, tries); } /* This is similar to the old generate treasure function. However, @@ -450,21 +452,23 @@ * inserted into, and then return that treausre */ object * -generate_treasure (treasurelist * t, int difficulty) +generate_treasure (treasurelist *tl, int difficulty) { - object *ob = get_object (), *tmp; + difficulty = clamp (difficulty, 1, settings.max_level); + + object *ob = object::create (), *tmp; - create_treasure (t, ob, 0, difficulty, 0); + 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) - remove_ob (tmp); + tmp->remove (); if (ob->inv) LOG (llevError, "In generate treasure, created multiple objects.\n"); - free_object (ob); + ob->destroy (); return tmp; } @@ -568,7 +572,7 @@ if (scaled_diff >= DIFFLEVELS) scaled_diff = DIFFLEVELS - 1; - percent = RANDOM () % 100; + percent = rndm (100); for (magic = 0; magic < (MAXMAGIC + 1); magic++) { @@ -584,7 +588,7 @@ magic = 0; } - magic = (RANDOM () % 3) ? magic : -magic; + magic = (rndm (3)) ? magic : -magic; /* LOG(llevDebug, "Chose magic %d for difficulty (scaled %d) %d\n", magic, scaled_diff, difficulty); */ return magic; @@ -609,7 +613,7 @@ if (op->type == ARMOUR) ARMOUR_SPEED (op) = (ARMOUR_SPEED (&op->arch->clone) * (100 + magic * 10)) / 100; - if (magic < 0 && !(RANDOM () % 3)) /* You can't just check the weight always */ + if (magic < 0 && !(rndm (3))) /* You can't just check the weight always */ magic = (-magic); op->weight = (op->arch->clone.weight * (100 - magic * 10)) / 100; } @@ -617,7 +621,7 @@ { if (op->type == ARMOUR) ARMOUR_SPEED (op) = (ARMOUR_SPEED (op) * (100 + magic * 10)) / 100; - if (magic < 0 && !(RANDOM () % 3)) /* You can't just check the weight always */ + if (magic < 0 && !(rndm (3))) /* You can't just check the weight always */ magic = (-magic); op->weight = (op->weight * (100 - magic * 10)) / 100; } @@ -660,14 +664,14 @@ if (op->type == AMULET) { - if (!(RANDOM () % 21)) - r = 20 + RANDOM () % 2; + if (!(rndm (21))) + r = 20 + rndm (2); else { if (RANDOM () & 2) r = 10; else - r = 11 + RANDOM () % 9; + r = 11 + rndm (9); } } @@ -714,10 +718,10 @@ case 18: case 19: { - int b = 5 + FABS (bonus), val, resist = RANDOM () % num_resist_table; + int b = 5 + abs (bonus), val, resist = rndm (num_resist_table); /* Roughly generate a bonus between 100 and 35 (depending on the bonus) */ - val = 10 + RANDOM () % b + RANDOM () % b + RANDOM () % b + RANDOM () % b; + val = 10 + rndm (b) + rndm (b) + rndm (b) + rndm (b); /* Cursed items need to have higher negative values to equal out with * positive values for how protections work out. Put another @@ -729,12 +733,13 @@ if (val > 35) val = 35; /* Upper limit */ b = 0; + while (op->resist[resist_table[resist]] != 0 && b < 4) - { - resist = RANDOM () % num_resist_table; - } + resist = rndm (num_resist_table); + if (b == 4) return; /* Not able to find a free resistance */ + op->resist[resist_table[resist]] = val; /* We should probably do something more clever here to adjust value * based on how good a resistance we gave. @@ -772,6 +777,7 @@ op->value = (op->value * 2) / 3; break; } + if (bonus > 0) op->value *= 2 * bonus; else @@ -801,7 +807,7 @@ } #define DICE2 (get_magic(2)==2?2:1) -#define DICESPELL (RANDOM()%3+RANDOM()%3+RANDOM()%3+RANDOM()%3+RANDOM()%3) +#define DICESPELL (rndm (3)+RANDOM()%3+RANDOM()%3+RANDOM()%3+RANDOM()%3) /* * fix_generated_item(): This is called after an item is generated, in @@ -940,7 +946,7 @@ case SHIELD: case HELMET: case CLOAK: - if (QUERY_FLAG (op, FLAG_CURSED) && !(RANDOM () % 4)) + if (QUERY_FLAG (op, FLAG_CURSED) && !(rndm (4))) set_ring_bonus (op, -DICE2); break; @@ -989,7 +995,7 @@ op->name_pl = "potions"; } - if (!(flags & GT_ONLY_GOOD) && RANDOM () % 2) + if (!(flags & GT_ONLY_GOOD) && rndm (2)) SET_FLAG (op, FLAG_CURSED); break; } @@ -1001,16 +1007,15 @@ case RING: if (op->arch == NULL) { - remove_ob (op); - free_object (op); - op = NULL; + op->destroy (); + op = 0; break; } if (op->arch != ring_arch && op->arch != amulet_arch) /* It's a special artifact! */ break; - if (!(flags & GT_ONLY_GOOD) && !(RANDOM () % 3)) + if (!(flags & GT_ONLY_GOOD) && !(rndm (3))) SET_FLAG (op, FLAG_CURSED); set_ring_bonus (op, QUERY_FLAG (op, FLAG_CURSED) ? -DICE2 : DICE2); @@ -1018,18 +1023,18 @@ if (op->type != RING) /* Amulets have only one ability */ break; - if (!(RANDOM () % 4)) + if (!(rndm (4))) { - int d = (RANDOM () % 2 || QUERY_FLAG (op, FLAG_CURSED)) ? -DICE2 : DICE2; + int d = (rndm (2) || QUERY_FLAG (op, FLAG_CURSED)) ? -DICE2 : DICE2; if (d > 0) op->value *= 3; set_ring_bonus (op, d); - if (!(RANDOM () % 4)) + if (!(rndm (4))) { - int d = (RANDOM () % 3 || QUERY_FLAG (op, FLAG_CURSED)) ? -DICE2 : DICE2; + int d = (rndm (3) || QUERY_FLAG (op, FLAG_CURSED)) ? -DICE2 : DICE2; if (d > 0) op->value *= 5; @@ -1047,15 +1052,15 @@ * msg for it, and tailor its properties based on theĀ· * creator and/or map level we found it on. */ - if (!op->msg && RANDOM () % 10) + if (!op->msg && rndm (10)) { /* set the book level properly */ if (creator->level == 0 || QUERY_FLAG (creator, FLAG_ALIVE)) { if (op->map && op->map->difficulty) - op->level = RANDOM () % (op->map->difficulty) + RANDOM () % 10 + 1; + op->level = RANDOM () % (op->map->difficulty) + rndm (10) + 1; else - op->level = RANDOM () % 20 + 1; + op->level = rndm (20) + 1; } else op->level = RANDOM () % creator->level; @@ -1171,14 +1176,14 @@ static artifactlist * get_empty_artifactlist (void) { - artifactlist *tl = (artifactlist *) malloc (sizeof (artifactlist)); + artifactlist *al = (artifactlist *) malloc (sizeof (artifactlist)); - if (tl == NULL) + if (al == NULL) fatal (OUT_OF_MEMORY); - tl->next = NULL; - tl->items = NULL; - tl->total_chance = 0; - return tl; + al->next = NULL; + al->items = NULL; + al->total_chance = 0; + return al; } /* @@ -1188,16 +1193,16 @@ static artifact * get_empty_artifact (void) { - artifact *t = (artifact *) malloc (sizeof (artifact)); + artifact *a = (artifact *) malloc (sizeof (artifact)); - if (t == NULL) + if (a == NULL) fatal (OUT_OF_MEMORY); - t->item = NULL; - t->next = NULL; - t->chance = 0; - t->difficulty = 0; - t->allowed = NULL; - return t; + a->item = NULL; + a->next = NULL; + a->chance = 0; + a->difficulty = 0; + a->allowed = NULL; + return a; } /* @@ -1257,15 +1262,16 @@ if (depth > 100) return; - while (t != NULL) + while (t) { - if (t->name != NULL) + if (t->name) { for (i = 0; i < depth; i++) fprintf (logfile, " "); fprintf (logfile, "{ (list: %s)\n", &t->name); tl = find_treasurelist (t->name); - dump_monster_treasure_rec (name, tl->items, depth + 2); + 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); @@ -1274,25 +1280,28 @@ { for (i = 0; i < depth; i++) fprintf (logfile, " "); - if (t->item->clone.type == FLESH) + if (t->item && t->item->clone.type == FLESH) fprintf (logfile, "%s's %s\n", name, &t->item->clone.name); else fprintf (logfile, "%s\n", &t->item->clone.name); } - if (t->next_yes != NULL) + + if (t->next_yes) { for (i = 0; i < depth; i++) fprintf (logfile, " "); fprintf (logfile, " (if yes)\n"); dump_monster_treasure_rec (name, t->next_yes, depth + 1); } - if (t->next_no != NULL) + + if (t->next_no) { for (i = 0; i < depth; i++) fprintf (logfile, " "); fprintf (logfile, " (if no)\n"); dump_monster_treasure_rec (name, t->next_no, depth + 1); } + t = t->next; } } @@ -1392,7 +1401,7 @@ art->difficulty = (uint8) value; else if (!strncmp (cp, "Object", 6)) { - art->item = get_object (); + art->item = object::create (); if (!load_object (thawer, art->item, 0)) LOG (llevError, "Init_Artifacts: Could not load object.\n"); @@ -1491,7 +1500,7 @@ if (!QUERY_FLAG (op, FLAG_ALIVE)) op->speed = 0.0; - update_ob_speed (op); + op->set_speed (op->speed); } if (change->nrof) @@ -1513,11 +1522,7 @@ /* Remove any spells this object currently has in it */ while (op->inv) - { - tmp_obj = op->inv; - remove_ob (tmp_obj); - free_object (tmp_obj); - } + op->inv->destroy (); tmp_obj = arch_to_object (change->other_arch); insert_ob_in_ob (tmp_obj, op); @@ -1806,16 +1811,13 @@ int special_potion (object *op) { - - int i; - if (op->attacktype) return 1; if (op->stats.Str || op->stats.Dex || op->stats.Con || op->stats.Pow || op->stats.Wis || op->stats.Int || op->stats.Cha) return 1; - for (i = 0; i < NROFATTACKS; i++) + for (int i = 0; i < NROFATTACKS; i++) if (op->resist[i]) return 1;