--- deliantra/server/common/treasure.C 2010/04/21 09:30:07 1.106
+++ deliantra/server/common/treasure.C 2018/11/27 18:50:22 1.123
@@ -1,24 +1,25 @@
/*
* This file is part of Deliantra, the Roguelike Realtime MMORPG.
- *
- * Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
+ *
+ * Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team
+ * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
* Copyright (©) 2002 Mark Wedel & Crossfire Development Team
* Copyright (©) 1992 Frank Tore Johansen
- *
+ *
* Deliantra is free software: you can redistribute it and/or modify it under
* the terms of the Affero GNU General Public License as published by the
* Free Software Foundation, either version 3 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 Affero GNU General Public License
* and the GNU General Public License along with this program. If not, see
* .
- *
+ *
* The authors can be reached via e-mail to
*/
@@ -36,19 +37,25 @@
#include
#include
+#include
+
+// used only by treasure.C, does not handle null arch ptrs
+#define IS_ARCH(arch,name) ((arch)->archname == shstr_ ## name)
+
extern char *spell_mapping[];
static treasurelist *first_treasurelist;
static void change_treasure (treasure *t, object *op); /* overrule default values */
-typedef std::tr1::unordered_map<
- const char *,
- treasurelist *,
- str_hash,
- str_equal,
- slice_allocator< std::pair >
-> tl_map_t;
+typedef ska::flat_hash_map
+ <
+ const char *,
+ treasurelist *,
+ str_hash,
+ str_equal,
+ slice_allocator< std::pair >
+ > tl_map_t;
static tl_map_t tl_map;
@@ -492,7 +499,7 @@
{ 0, 0, 0, 0, 100}, // 31
};
-/* calculate the appropriate level for wands staves and scrolls.
+/* 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:
@@ -528,20 +535,13 @@
static int
magic_from_difficulty (int difficulty)
{
- int percent = 0, magic = 0;
- int scaled_diff = (int) (((double) difficulty / settings.max_level) * DIFFLEVELS);
-
- scaled_diff--;
+ int scaled_diff = lerp (difficulty, 0, settings.max_level, 0, DIFFLEVELS - 1);
+ scaled_diff = clamp (scaled_diff, 0, DIFFLEVELS - 1);
- if (scaled_diff < 0)
- scaled_diff = 0;
+ int percent = rndm (100);
+ int magic;
- if (scaled_diff >= DIFFLEVELS)
- scaled_diff = DIFFLEVELS - 1;
-
- percent = rndm (100);
-
- for (magic = 0; magic < (MAXMAGIC + 1); magic++)
+ for (magic = 0; magic <= MAXMAGIC; magic++)
{
percent -= difftomagic_list[scaled_diff][magic];
@@ -549,7 +549,7 @@
break;
}
- if (magic == (MAXMAGIC + 1))
+ if (magic > MAXMAGIC)
{
LOG (llevError, "Warning, table for difficulty (scaled %d) %d bad.\n", scaled_diff, difficulty);
magic = 0;
@@ -567,7 +567,6 @@
* This function doesn't work properly, should add use of archetypes
* to make it truly absolute.
*/
-
void
set_abs_magic (object *op, int magic)
{
@@ -605,13 +604,13 @@
static void
set_magic (int difficulty, object *op, int max_magic, int flags)
{
- int i;
+ int i = magic_from_difficulty (difficulty);
- i = magic_from_difficulty (difficulty);
if ((flags & GT_ONLY_GOOD) && i < 0)
i = -i;
- if (i > max_magic)
- i = max_magic;
+
+ i = min (i, max_magic);
+
set_abs_magic (op, i);
if (i < 0)
op->set_flag (FLAG_CURSED);
@@ -631,24 +630,19 @@
int r = rndm (bonus > 0 ? 25 : 11);
if (op->type == AMULET)
- {
- if (!(rndm (21)))
- r = 20 + rndm (2);
- else
- {
- if (rndm (2))
- r = 10;
- else
- r = 11 + rndm (9);
- }
- }
+ if (!rndm (21))
+ r = 20 + rndm (2);
+ else if (rndm (2))
+ r = 10;
+ else
+ r = 11 + rndm (9);
switch (r)
{
- /* Redone by MSW 2000-11-26 to have much less code. Also,
- * bonuses and penalties will stack and add to existing values.
- * of the item.
- */
+ /* Redone by MSW 2000-11-26 to have much less code. Also,
+ * bonuses and penalties will stack and add to existing values.
+ * of the item.
+ */
case 0:
case 1:
case 2:
@@ -698,8 +692,9 @@
*/
if (bonus < 0)
val = 2 * -val - rndm (b);
- if (val > 35)
- val = 35; /* Upper limit */
+
+ val = min (35, val); /* Upper limit */
+
b = 0;
while (op->resist[resist_table[resist]] != 0 && b < 4)
@@ -742,14 +737,14 @@
case 22:
op->stats.exp += bonus; /* Speed! */
- op->value = (op->value * 2) / 3;
+ op->value = op->value * 2 / 3;
break;
}
if (bonus > 0)
- op->value *= 2 * bonus;
+ op->value = 2 * op->value * bonus;
else
- op->value = -(op->value * 2 * bonus) / 3;
+ op->value = -2 * op->value * bonus / 3;
}
/*
@@ -763,12 +758,9 @@
static int
get_magic (int diff)
{
- int i;
+ diff = min (3, diff);
- if (diff < 3)
- diff = 3;
-
- for (i = 0; i < 4; i++)
+ for (int i = 0; i < 4; i++)
if (rndm (diff))
return i;
@@ -792,7 +784,30 @@
return 0;
}
-#define DICE2 (get_magic(2)==2?2:1)
+static double
+value_factor_from_spell_item (object *spell, object *item)
+{
+ double factor =
+ pow ((spell->value > 0 ? spell->value : 1)
+ * spell->level, 1.5);
+
+ if (item) // this if for: wands/staffs/rods:
+ {
+ /* Old crossfire comment ahead:
+ * Add 50 to both level an divisor to keep prices a little more
+ * reasonable. Otherwise, a high level version of a low level
+ * spell can be worth tons a money (eg, level 20 rod, level 2 spell =
+ * 10 time multiplier). This way, the value are a bit more reasonable.
+ */
+
+ factor *= item->level + 50;
+ factor /= item->inv->level + 50;
+ }
+
+ return factor;
+}
+
+#define DICE2 (get_magic(2) == 2 ? 2 : 1)
#define DICESPELL (rndm (3) + rndm (3) + rndm (3) + rndm (3) + rndm (3))
/*
@@ -802,8 +817,8 @@
*/
/* 4/28/96 added creator object from which op may now inherit properties based on
- * op->type. Right now, which stuff the creator passes on is object type
- * dependant. I know this is a spagetti manuever, but is there a cleaner
+ * op->type. Right now, which stuff the creator passes on is object type
+ * dependant. I know this is a spagetti manuever, but is there a cleaner
* way to do this? b.t. */
/*
@@ -913,7 +928,7 @@
/* Handle healing and magic power potions */
if (op->stats.sp && !op->randomitems)
{
- object *tmp = get_archetype (spell_mapping [op->stats.sp]);
+ object *tmp = archetype::get (spell_mapping [op->stats.sp]);
insert_ob_in_ob (tmp, op);
op->stats.sp = 0;
}
@@ -941,17 +956,17 @@
case POTION:
{
- int too_many_tries = 0, is_special = 0;
+ int too_many_tries = 0;
/* Handle healing and magic power potions */
if (op->stats.sp && !op->randomitems)
{
- object *tmp = get_archetype (spell_mapping[op->stats.sp]);
+ object *tmp = archetype::get (spell_mapping[op->stats.sp]);
insert_ob_in_ob (tmp, op);
op->stats.sp = 0;
}
- while (!(is_special = special_potion (op)) && !op->inv)
+ while (!special_potion (op) && !op->inv)
{
generate_artifact (op, difficulty);
if (too_many_tries++ > 10)
@@ -969,8 +984,8 @@
}
else
{
- op->name = "potion";
- op->name_pl = "potions";
+ op->name = shstr_potion;
+ op->name_pl = shstr_potions;
}
if (!(flags & GT_ONLY_GOOD) && rndm (2))
@@ -995,7 +1010,7 @@
if (op->type != RING) /* Amulets have only one ability */
break;
- if (!(rndm (4)))
+ if (!rndm (4))
{
int d = (rndm (2) || op->flag [FLAG_CURSED]) ? -DICE2 : DICE2;
@@ -1004,12 +1019,13 @@
set_ring_bonus (op, d);
- if (!(rndm (4)))
+ if (!rndm (4))
{
int d = (rndm (3) || op->flag [FLAG_CURSED]) ? -DICE2 : DICE2;
if (d > 0)
op->value *= 5;
+
set_ring_bonus (op, d);
}
}
@@ -1057,10 +1073,8 @@
break;
case SPELLBOOK:
- op->value *=
- pow ((op->inv->value > 0 ? op->inv->value : 1)
- * op->inv->level,
- 1.5);
+ op->value *= value_factor_from_spell_item (op->inv, 0);
+
/* add exp so learning gives xp */
op->level = op->inv->level;
op->stats.exp = op->value;
@@ -1074,30 +1088,22 @@
op->stats.food = op->inv->nrof;
op->nrof = 1;
/* If the spell changes by level, choose a random level
- * for it, and adjust price. If the spell doesn't
- * change by level, just set the wand to the level of
- * the spell, and value calculation is simpler.
+ * for it.
*/
- if (op->inv->duration_modifier || op->inv->dam_modifier || op->inv->range_modifier)
- {
- op->level = level_for_item (op, difficulty);
- op->value = op->value * op->inv->value * (op->level + 50) / (op->inv->level + 50);
- }
+ if (op->inv->duration_modifier
+ || op->inv->dam_modifier
+ || op->inv->range_modifier)
+ op->level = level_for_item (op, difficulty);
else
- {
- op->level = op->inv->level;
- op->value = op->value * op->inv->value;
- }
+ op->level = op->inv->level;
+
+ op->value *= value_factor_from_spell_item (op->inv, op);
break;
case ROD:
op->level = level_for_item (op, difficulty);
- /* Add 50 to both level an divisor to keep prices a little more
- * reasonable. Otherwise, a high level version of a low level
- * spell can be worth tons a money (eg, level 20 rod, level 2 spell =
- * 10 time multiplier). This way, the value are a bit more reasonable.
- */
- op->value = op->value * op->inv->value * (op->level + 50) / (op->inv->level + 50);
+ op->value *= value_factor_from_spell_item (op->inv, op);
+
/* maxhp is used to denote how many 'charges' the rod holds before */
if (op->stats.maxhp)
op->stats.maxhp *= max (op->inv->stats.sp, op->inv->stats.grace);
@@ -1109,7 +1115,7 @@
case SCROLL:
op->level = level_for_item (op, difficulty);
- op->value = op->value * op->inv->value * (op->level + 50) / (op->inv->level + 50);
+ op->value *= value_factor_from_spell_item (op->inv, op);
/* add exp so reading them properly gives xp */
op->stats.exp = op->value / 5;
@@ -1184,7 +1190,6 @@
init_artifacts ()
{
static int has_been_inited = 0;
- char filename[MAX_BUF];
artifact *art = NULL;
artifactlist *al;
@@ -1193,8 +1198,7 @@
else
has_been_inited = 1;
- sprintf (filename, "%s/artifacts", settings.datadir);
- object_thawer f (filename);
+ object_thawer f (settings.datadir, "artifacts");
if (!f)
return;
@@ -1289,8 +1293,6 @@
LOG (llevDebug, "Artifact list type %d has %d total chance\n", al->type, al->total_chance);
#endif
}
-
- LOG (llevDebug, "done.\n");
}
/*
@@ -1300,56 +1302,49 @@
void
add_abilities (object *op, object *change)
{
- int i, tmp;
-
if (change->face != blank_face)
- {
-#ifdef TREASURE_VERBOSE
- LOG (llevDebug, "add_abilities change face: %d\n", change->face);
-#endif
- op->face = change->face;
- }
+ op->face = change->face;
- for (i = 0; i < NUM_STATS; i++)
+ for (int i = 0; i < NUM_STATS; i++)
change_attr_value (&(op->stats), i, change->stats.stat (i));
- op->attacktype |= change->attacktype;
- op->path_attuned |= change->path_attuned;
+ op->attacktype |= change->attacktype;
+ op->path_attuned |= change->path_attuned;
op->path_repelled |= change->path_repelled;
- op->path_denied |= change->path_denied;
- op->move_type |= change->move_type;
- op->stats.luck += change->stats.luck;
+ op->path_denied |= change->path_denied;
+ op->move_type |= change->move_type;
+ op->stats.luck += change->stats.luck;
+
+ static const struct copyflags : object::flags_t
+ {
+ copyflags ()
+ {
+ set (FLAG_CURSED);
+ set (FLAG_DAMNED);
+ set (FLAG_LIFESAVE);
+ set (FLAG_REFL_SPELL);
+ set (FLAG_STEALTH);
+ set (FLAG_XRAYS);
+ set (FLAG_BLIND);
+ set (FLAG_SEE_IN_DARK);
+ set (FLAG_REFL_MISSILE);
+ set (FLAG_MAKE_INVIS);
+ }
+ } copyflags;
+
+ // we might want to just copy, but or'ing is what the original code did
+ op->flag |= change->flag & copyflags;
- if (change->flag [FLAG_CURSED])
- op->set_flag (FLAG_CURSED);
- if (change->flag [FLAG_DAMNED])
- op->set_flag (FLAG_DAMNED);
if ((change->flag [FLAG_CURSED] || change->flag [FLAG_DAMNED]) && op->magic > 0)
set_abs_magic (op, -op->magic);
- if (change->flag [FLAG_LIFESAVE])
- op->set_flag (FLAG_LIFESAVE);
- if (change->flag [FLAG_REFL_SPELL])
- op->set_flag (FLAG_REFL_SPELL);
- if (change->flag [FLAG_STEALTH])
- op->set_flag (FLAG_STEALTH);
- if (change->flag [FLAG_XRAYS])
- op->set_flag (FLAG_XRAYS);
- if (change->flag [FLAG_BLIND])
- op->set_flag (FLAG_BLIND);
- if (change->flag [FLAG_SEE_IN_DARK])
- op->set_flag (FLAG_SEE_IN_DARK);
- if (change->flag [FLAG_REFL_MISSILE])
- op->set_flag (FLAG_REFL_MISSILE);
- if (change->flag [FLAG_MAKE_INVIS])
- op->set_flag (FLAG_MAKE_INVIS);
-
if (change->flag [FLAG_STAND_STILL])
{
op->clr_flag (FLAG_ANIMATE);
+
/* so artifacts will join */
if (!op->flag [FLAG_ALIVE])
- op->speed = 0.0;
+ op->speed = 0.;
op->set_speed (op->speed);
}
@@ -1358,8 +1353,8 @@
op->nrof = rndm (change->nrof) + 1;
op->stats.exp += change->stats.exp; /* Speed modifier */
- op->stats.wc += change->stats.wc;
- op->stats.ac += change->stats.ac;
+ op->stats.wc += change->stats.wc;
+ op->stats.ac += change->stats.ac;
if (change->other_arch)
{
@@ -1401,33 +1396,33 @@
op->stats.maxsp += change->stats.maxsp;
if (change->stats.food < 0)
- op->stats.food = -(change->stats.food);
+ op->stats.food = -change->stats.food;
else
op->stats.food += change->stats.food;
if (change->level < 0)
- op->level = -(change->level);
+ op->level = -change->level;
else
op->level += change->level;
if (change->gen_sp_armour < 0)
- op->gen_sp_armour = -(change->gen_sp_armour);
+ op->gen_sp_armour = -change->gen_sp_armour;
else
- op->gen_sp_armour = (op->gen_sp_armour * (change->gen_sp_armour)) / 100;
+ op->gen_sp_armour = op->gen_sp_armour * (int)change->gen_sp_armour / 100;
op->item_power = change->item_power;
- for (i = 0; i < NROFATTACKS; i++)
- if (change->resist[i])
- op->resist[i] += change->resist[i];
+ for (int i = 0; i < NROFATTACKS; i++)
+ op->resist[i] += change->resist[i];
if (change->stats.dam)
{
if (change->stats.dam < 0)
- op->stats.dam = (-change->stats.dam);
+ op->stats.dam = -change->stats.dam;
else if (op->stats.dam)
{
- tmp = (signed char) (((int) op->stats.dam * (int) change->stats.dam) / 10);
+ int tmp = op->stats.dam * change->stats.dam / 10;
+
if (tmp == op->stats.dam)
{
if (change->stats.dam < 10)
@@ -1443,25 +1438,25 @@
if (change->weight)
{
if (change->weight < 0)
- op->weight = (-change->weight);
+ op->weight = -change->weight;
else
- op->weight = (op->weight * (change->weight)) / 100;
+ op->weight = op->weight * change->weight / 100;
}
if (change->last_sp)
{
if (change->last_sp < 0)
- op->last_sp = (-change->last_sp);
+ op->last_sp = -change->last_sp;
else
- op->last_sp = (signed char) (((int) op->last_sp * (int) change->last_sp) / (int) 100);
+ op->last_sp = op->last_sp * (int)change->last_sp / 100;
}
if (change->gen_sp_armour)
{
if (change->gen_sp_armour < 0)
- op->gen_sp_armour = (-change->gen_sp_armour);
+ op->gen_sp_armour = -change->gen_sp_armour;
else
- op->gen_sp_armour = (signed char) (((int) op->gen_sp_armour * ((int) change->gen_sp_armour)) / (int) 100);
+ op->gen_sp_armour = op->gen_sp_armour * (int)change->gen_sp_armour / 100;
}
op->value *= change->value;
@@ -1503,7 +1498,7 @@
name = tmp->name, neg = 0;
/* If we match name, then return the opposite of 'neg' */
- if (!strcmp (name, op->name) || (op->arch && !strcmp (name, op->arch->archname)))
+ if (!strcmp (name, op->arch->archname))
return !neg;
/* Set success as true, since if the match was an inverse, it means
@@ -1524,10 +1519,8 @@
void
give_artifact_abilities (object *op, object *artifct)
{
- char new_name[MAX_BUF];
+ op->title = format ("of %s", &artifct->name);
- sprintf (new_name, "of %s", &artifct->name);
- op->title = new_name;
add_abilities (op, artifct); /* Give out the bonuses */
#if 0 /* Bit verbose, but keep it here until next time I need it... */
@@ -1557,11 +1550,9 @@
void
generate_artifact (object *op, int difficulty)
{
- artifactlist *al;
artifact *art;
- int i;
- al = find_artifactlist (op->type);
+ artifactlist *al = find_artifactlist (op->type);
if (al == NULL)
{
@@ -1571,7 +1562,7 @@
return;
}
- for (i = 0; i < ARTIFACT_TRIES; i++)
+ for (int i = 0; i < ARTIFACT_TRIES; i++)
{
int roll = rndm (al->total_chance);
@@ -1621,31 +1612,25 @@
void
fix_flesh_item (object *item, object *donor)
{
- char tmpbuf[MAX_BUF];
- int i;
-
if (item->type == FLESH && donor)
{
/* change the name */
- sprintf (tmpbuf, "%s's %s", &donor->name, &item->name);
- item->name = tmpbuf;
- sprintf (tmpbuf, "%s's %s", &donor->name, &item->name_pl);
- item->name_pl = tmpbuf;
+ item->name = format ("%s's %s", &donor->name, &item->name);
+ item->name_pl = format ("%s's %s", &donor->name, &item->name_pl);
/* weight is FLESH weight/100 * donor */
- if ((item->weight = (signed long) (((double) item->weight / (double) 100.0) * (double) donor->weight)) == 0)
- item->weight = 1;
+ item->weight = max (1, item->weight * donor->weight / 100);
/* value is multiplied by level of donor */
item->value *= isqrt (donor->level * 2);
/* food value */
- item->stats.food += (donor->stats.hp / 100) + donor->stats.Con;
+ item->stats.food += donor->stats.hp / 100 + donor->stats.Con;
/* flesh items inherit some abilities of donor, but not
* full effect.
*/
- for (i = 0; i < NROFATTACKS; i++)
+ for (int i = 0; i < NROFATTACKS; i++)
item->resist[i] = donor->resist[i] / 2;
/* item inherits donor's level (important for quezals) */
@@ -1656,7 +1641,7 @@
item->type = POISON;
if (donor->attacktype & AT_ACID)
- item->stats.hp = -1 * item->stats.food;
+ item->stats.hp = -item->stats.food;
item->set_flag (FLAG_NO_STEAL);
}