From 289dd7a1a9478715222af870793c93a5200496f2 Mon Sep 17 00:00:00 2001
From: dg <dg@51575b47-30f0-44d4-a5cc-537603b46e54>
Date: Tue, 15 Jun 2010 20:31:31 +0000
Subject: [PATCH] Ego items powers are based on the material level of the base
 object. A mithril weapon will get better bonuses than an iron one

git-svn-id: http://svn.net-core.org/repos/t-engine4@787 51575b47-30f0-44d4-a5cc-537603b46e54
---
 game/engine/Zone.lua                          |  7 +--
 game/engine/resolvers.lua                     |  3 +-
 game/engine/utils.lua                         | 30 +++++++++++
 .../data/general/objects/egos/amulets.lua     |  6 +--
 .../tome/data/general/objects/egos/armor.lua  | 10 ++--
 .../tome/data/general/objects/egos/rings.lua  | 50 +++++++++----------
 .../tome/data/general/objects/egos/robe.lua   | 30 +++++------
 .../tome/data/general/objects/egos/shield.lua | 22 ++++----
 .../tome/data/general/objects/egos/staves.lua | 24 ++++-----
 .../tome/data/general/objects/egos/weapon.lua | 26 +++++-----
 game/modules/tome/resolvers.lua               | 14 ++++++
 11 files changed, 134 insertions(+), 88 deletions(-)

diff --git a/game/engine/Zone.lua b/game/engine/Zone.lua
index 61190f239c..f52c1e327d 100644
--- a/game/engine/Zone.lua
+++ b/game/engine/Zone.lua
@@ -260,17 +260,18 @@ function _M:finishEntity(level, type, e, ego_chance)
 			for ie, ego in ipairs(egos_list) do
 				print("ego", ego.__CLASSNAME, ego.name, getmetatable(ego))
 				ego = ego:clone()
-				ego:resolve()
-				ego:resolve(nil, true)
 				local newname
 				if ego.prefix then newname = ego.name .. e.name
 				else newname = e.name .. ego.name end
 				print("applying ego", ego.name, "to ", e.name, "::", newname)
 				ego.unided_name = nil
-				table.mergeAdd(e, ego, true)
+				-- Merge additively but with array appending, so that nameless resolvers are not lost
+				table.mergeAddAppendArray(e, ego, true)
 				e.name = newname
 				e.egoed = true
 			end
+			-- Re-resolve with the (possibly) new resolvers
+			e:resolve()
 		end
 		e.egos = nil e.egos_chance = nil e.force_ego = nil
 	end
diff --git a/game/engine/resolvers.lua b/game/engine/resolvers.lua
index 9ebb19146c..0123317f26 100644
--- a/game/engine/resolvers.lua
+++ b/game/engine/resolvers.lua
@@ -38,11 +38,12 @@ end
 
 --- Random bonus based on level
 resolvers.current_level = 1
+resolvers.mbonus_max_level = 50
 function resolvers.mbonus(max, add)
 	return {__resolver="mbonus", max, add}
 end
 function resolvers.calc.mbonus(t)
-	return rng.mbonus(t[1], resolvers.current_level, 50) + (t[2] or 0)
+	return rng.mbonus(t[1], resolvers.current_level, resolvers.mbonus_max_level) + (t[2] or 0)
 end
 
 --- Talents resolver
diff --git a/game/engine/utils.lua b/game/engine/utils.lua
index f30458a6ce..1b9bf47f5a 100644
--- a/game/engine/utils.lua
+++ b/game/engine/utils.lua
@@ -62,6 +62,36 @@ function table.mergeAdd(dst, src, deep)
 	end
 end
 
+--- Merges additively the named fields and append the array part
+-- Yes this is weird and you'll probably not need it, but the engine does :)
+function table.mergeAddAppendArray(dst, src, deep)
+	-- Append the array part
+	for i = 1, #src do
+		local b = src[i]
+		if deep and type(b) == "table" and not b.__CLASSNAME then b = table.clone(b, true)
+		elseif deep and type(b) == "table" and b.__CLASSNAME then b = b:clone()
+		end
+		table.insert(dst, b)
+	end
+
+	-- Copy the table part
+	for k, e in pairs(src) do
+		if type(k) ~= "number" then
+			if deep and dst[k] and type(e) == "table" and type(dst[k]) == "table" and not e.__CLASSNAME then
+				-- WARNING we do not recurse on ourself but instead of the simple mergeAdd, we do not want to do the array stuff for subtables
+				-- yes I warned you this is weird
+				table.mergeAdd(dst[k], e, true)
+			elseif deep and not dst[k] and type(e) == "table" and not e.__CLASSNAME then
+				dst[k] = table.clone(e, true)
+			elseif dst[k] and type(e) == "number" then
+				dst[k] = dst[k] + e
+			else
+				dst[k] = e
+			end
+		end
+	end
+end
+
 function table.append(dst, src)
 	for i = 1, #src do dst[#dst+1] = src[i] end
 end
diff --git a/game/modules/tome/data/general/objects/egos/amulets.lua b/game/modules/tome/data/general/objects/egos/amulets.lua
index 5daa7694f7..4243c83ace 100644
--- a/game/modules/tome/data/general/objects/egos/amulets.lua
+++ b/game/modules/tome/data/general/objects/egos/amulets.lua
@@ -26,7 +26,7 @@ newEntity{
 	rarity = 6,
 	cost = 4,
 	wielder = {
-		inc_stats = { [Stats.STAT_CUN] = resolvers.mbonus(8, 2) },
+		inc_stats = { [Stats.STAT_CUN] = resolvers.mbonus_material(8, 2) },
 	},
 }
 newEntity{
@@ -35,7 +35,7 @@ newEntity{
 	rarity = 6,
 	cost = 4,
 	wielder = {
-		inc_stats = { [Stats.STAT_WIL] = resolvers.mbonus(8, 2) },
+		inc_stats = { [Stats.STAT_WIL] = resolvers.mbonus_material(8, 2) },
 	},
 }
 newEntity{
@@ -87,7 +87,7 @@ newEntity{
 		local tt = tts[rng.range(1, #tts)]
 
 		e.wielder.talents_types_mastery = {}
-		e.wielder.talents_types_mastery[tt] = (10 + rng.mbonus(30, resolvers.current_level, 50)) / 100
+		e.wielder.talents_types_mastery[tt] = (10 + rng.mbonus(math.ceil(30 * e.material_level / 5), resolvers.current_level, 50)) / 100
 	end),
 }
 newEntity{
diff --git a/game/modules/tome/data/general/objects/egos/armor.lua b/game/modules/tome/data/general/objects/egos/armor.lua
index 790c18cc0c..f85588a371 100644
--- a/game/modules/tome/data/general/objects/egos/armor.lua
+++ b/game/modules/tome/data/general/objects/egos/armor.lua
@@ -23,7 +23,7 @@ newEntity{
 	rarity = 5,
 	cost = 6,
 	wielder = {
-		resists={[DamageType.FIRE] = resolvers.mbonus(30, 10)},
+		resists={[DamageType.FIRE] = resolvers.mbonus_material(30, 10)},
 	},
 }
 newEntity{
@@ -32,7 +32,7 @@ newEntity{
 	rarity = 5,
 	cost = 6,
 	wielder = {
-		resists={[DamageType.COLD] = resolvers.mbonus(30, 10)},
+		resists={[DamageType.COLD] = resolvers.mbonus_material(30, 10)},
 	},
 }
 newEntity{
@@ -41,7 +41,7 @@ newEntity{
 	rarity = 5,
 	cost = 6,
 	wielder = {
-		resists={[DamageType.ACID] = resolvers.mbonus(30, 10)},
+		resists={[DamageType.ACID] = resolvers.mbonus_material(30, 10)},
 	},
 }
 newEntity{
@@ -50,7 +50,7 @@ newEntity{
 	rarity = 5,
 	cost = 6,
 	wielder = {
-		resists={[DamageType.LIGHTNING] = resolvers.mbonus(30, 10)},
+		resists={[DamageType.LIGHTNING] = resolvers.mbonus_material(30, 10)},
 	},
 }
 newEntity{
@@ -59,7 +59,7 @@ newEntity{
 	rarity = 5,
 	cost = 6,
 	wielder = {
-		resists={[DamageType.NATURE] = resolvers.mbonus(30, 10)},
+		resists={[DamageType.NATURE] = resolvers.mbonus_material(30, 10)},
 	},
 }
 
diff --git a/game/modules/tome/data/general/objects/egos/rings.lua b/game/modules/tome/data/general/objects/egos/rings.lua
index 1cf1de9b5a..dd7caaaecd 100644
--- a/game/modules/tome/data/general/objects/egos/rings.lua
+++ b/game/modules/tome/data/general/objects/egos/rings.lua
@@ -26,7 +26,7 @@ newEntity{
 	rarity = 4,
 	cost = 2,
 	wielder = {
-		see_invisible = resolvers.mbonus(20, 5),
+		see_invisible = resolvers.mbonus_material(20, 5),
 	},
 }
 
@@ -38,7 +38,7 @@ Beware, you should take off your light, otherwise you will still be easily spott
 	rarity = 4,
 	cost = 16,
 	wielder = {
-		invisible = resolvers.mbonus(10, 5),
+		invisible = resolvers.mbonus_material(10, 5),
 	},
 }
 
@@ -48,7 +48,7 @@ newEntity{
 	rarity = 10,
 	cost = 8,
 	wielder = {
-		life_regen = resolvers.mbonus(3, 1),
+		life_regen = resolvers.mbonus_material(3, 1),
 	},
 }
 
@@ -58,7 +58,7 @@ newEntity{
 	rarity = 8,
 	cost = 3,
 	wielder = {
-		mana_regen = resolvers.mbonus(3, 1),
+		mana_regen = resolvers.mbonus_material(3, 1),
 	},
 }
 
@@ -68,7 +68,7 @@ newEntity{
 	rarity = 6,
 	cost = 2,
 	wielder = {
-		combat_atk = resolvers.mbonus(15, 5),
+		combat_atk = resolvers.mbonus_material(15, 5),
 	},
 }
 
@@ -78,7 +78,7 @@ newEntity{
 	rarity = 6,
 	cost = 2,
 	wielder = {
-		combat_def = resolvers.mbonus(15, 5),
+		combat_def = resolvers.mbonus_material(15, 5),
 	},
 }
 
@@ -88,7 +88,7 @@ newEntity{
 	rarity = 6,
 	cost = 2,
 	wielder = {
-		resists = { [DamageType.FIRE] = resolvers.mbonus(15, 20), }
+		resists = { [DamageType.FIRE] = resolvers.mbonus_material(15, 20), }
 	},
 }
 
@@ -98,7 +98,7 @@ newEntity{
 	rarity = 6,
 	cost = 2,
 	wielder = {
-		resists = { [DamageType.COLD] = resolvers.mbonus(15, 20), }
+		resists = { [DamageType.COLD] = resolvers.mbonus_material(15, 20), }
 	},
 }
 
@@ -108,7 +108,7 @@ newEntity{
 	rarity = 6,
 	cost = 2,
 	wielder = {
-		resists = { [DamageType.NATURE] = resolvers.mbonus(15, 20), }
+		resists = { [DamageType.NATURE] = resolvers.mbonus_material(15, 20), }
 	},
 }
 
@@ -118,7 +118,7 @@ newEntity{
 	rarity = 6,
 	cost = 2,
 	wielder = {
-		resists = { [DamageType.LIGHTNING] = resolvers.mbonus(15, 20), }
+		resists = { [DamageType.LIGHTNING] = resolvers.mbonus_material(15, 20), }
 	},
 }
 
@@ -128,7 +128,7 @@ newEntity{
 	rarity = 6,
 	cost = 2,
 	wielder = {
-		resists = { [DamageType.ACID] = resolvers.mbonus(15, 20), }
+		resists = { [DamageType.ACID] = resolvers.mbonus_material(15, 20), }
 	},
 }
 
@@ -138,7 +138,7 @@ newEntity{
 	rarity = 6,
 	cost = 2,
 	wielder = {
-		combat_spellresist = resolvers.mbonus(15, 5),
+		combat_spellresist = resolvers.mbonus_material(15, 5),
 	},
 }
 
@@ -148,7 +148,7 @@ newEntity{
 	rarity = 6,
 	cost = 2,
 	wielder = {
-		combat_physresist = resolvers.mbonus(15, 5),
+		combat_physresist = resolvers.mbonus_material(15, 5),
 	},
 }
 
@@ -158,7 +158,7 @@ newEntity{
 	rarity = 6,
 	cost = 4,
 	wielder = {
-		inc_stats = { [Stats.STAT_STR] = resolvers.mbonus(8, 2) },
+		inc_stats = { [Stats.STAT_STR] = resolvers.mbonus_material(8, 2) },
 	},
 }
 newEntity{
@@ -167,7 +167,7 @@ newEntity{
 	rarity = 6,
 	cost = 4,
 	wielder = {
-		inc_stats = { [Stats.STAT_DEX] = resolvers.mbonus(8, 2) },
+		inc_stats = { [Stats.STAT_DEX] = resolvers.mbonus_material(8, 2) },
 	},
 }
 newEntity{ define_as = "RING_MAGIC",
@@ -176,7 +176,7 @@ newEntity{ define_as = "RING_MAGIC",
 	rarity = 6,
 	cost = 4,
 	wielder = {
-		inc_stats = { [Stats.STAT_MAG] = resolvers.mbonus(8, 2) },
+		inc_stats = { [Stats.STAT_MAG] = resolvers.mbonus_material(8, 2) },
 	},
 }
 newEntity{
@@ -185,7 +185,7 @@ newEntity{
 	rarity = 6,
 	cost = 4,
 	wielder = {
-		inc_stats = { [Stats.STAT_CON] = resolvers.mbonus(8, 2) },
+		inc_stats = { [Stats.STAT_CON] = resolvers.mbonus_material(8, 2) },
 	},
 }
 
@@ -196,7 +196,7 @@ newEntity{
 	rarity = 4,
 	cost = 4,
 	wielder = {
-		inc_damage = { [DamageType.PHYSICAL] = resolvers.mbonus(15, 5) },
+		inc_damage = { [DamageType.PHYSICAL] = resolvers.mbonus_material(15, 5) },
 	},
 }
 newEntity{ define_as = "RING_ARCANE_POWER",
@@ -205,7 +205,7 @@ newEntity{ define_as = "RING_ARCANE_POWER",
 	rarity = 4,
 	cost = 4,
 	wielder = {
-		inc_damage = { [DamageType.ARCANE] = resolvers.mbonus(15, 5) },
+		inc_damage = { [DamageType.ARCANE] = resolvers.mbonus_material(15, 5) },
 	},
 }
 newEntity{ define_as = "RING_BURNING",
@@ -214,7 +214,7 @@ newEntity{ define_as = "RING_BURNING",
 	rarity = 4,
 	cost = 4,
 	wielder = {
-		inc_damage = { [DamageType.FIRE] = resolvers.mbonus(15, 5) },
+		inc_damage = { [DamageType.FIRE] = resolvers.mbonus_material(15, 5) },
 	},
 }
 newEntity{ define_as = "RING_FREEZING",
@@ -223,7 +223,7 @@ newEntity{ define_as = "RING_FREEZING",
 	rarity = 4,
 	cost = 4,
 	wielder = {
-		inc_damage = { [DamageType.COLD] = resolvers.mbonus(15, 5) },
+		inc_damage = { [DamageType.COLD] = resolvers.mbonus_material(15, 5) },
 	},
 }
 newEntity{ define_as = "RING_NATURE_BLESSING",
@@ -232,7 +232,7 @@ newEntity{ define_as = "RING_NATURE_BLESSING",
 	rarity = 4,
 	cost = 4,
 	wielder = {
-		inc_damage = { [DamageType.NATURE] = resolvers.mbonus(15, 5) },
+		inc_damage = { [DamageType.NATURE] = resolvers.mbonus_material(15, 5) },
 	},
 }
 newEntity{ define_as = "RING_BLIGHT",
@@ -241,7 +241,7 @@ newEntity{ define_as = "RING_BLIGHT",
 	rarity = 4,
 	cost = 4,
 	wielder = {
-		inc_damage = { [DamageType.BLIGHT] = resolvers.mbonus(15, 5) },
+		inc_damage = { [DamageType.BLIGHT] = resolvers.mbonus_material(15, 5) },
 	},
 }
 newEntity{ define_as = "RING_SHOCK",
@@ -250,7 +250,7 @@ newEntity{ define_as = "RING_SHOCK",
 	rarity = 4,
 	cost = 4,
 	wielder = {
-		inc_damage = { [DamageType.LIGHTNING] = resolvers.mbonus(15, 5) },
+		inc_damage = { [DamageType.LIGHTNING] = resolvers.mbonus_material(15, 5) },
 	},
 }
 newEntity{ define_as = "RING_CORROSION",
@@ -259,6 +259,6 @@ newEntity{ define_as = "RING_CORROSION",
 	rarity = 4,
 	cost = 4,
 	wielder = {
-		inc_damage = { [DamageType.ACID] = resolvers.mbonus(15, 5) },
+		inc_damage = { [DamageType.ACID] = resolvers.mbonus_material(15, 5) },
 	},
 }
diff --git a/game/modules/tome/data/general/objects/egos/robe.lua b/game/modules/tome/data/general/objects/egos/robe.lua
index 33a1ad021c..b1d2941ed6 100644
--- a/game/modules/tome/data/general/objects/egos/robe.lua
+++ b/game/modules/tome/data/general/objects/egos/robe.lua
@@ -23,7 +23,7 @@ newEntity{
 	rarity = 5,
 	cost = 6,
 	wielder = {
-		resists={[DamageType.FIRE] = resolvers.mbonus(30, 10)},
+		resists={[DamageType.FIRE] = resolvers.mbonus_material(30, 10)},
 	},
 }
 newEntity{
@@ -32,7 +32,7 @@ newEntity{
 	rarity = 5,
 	cost = 6,
 	wielder = {
-		resists={[DamageType.COLD] = resolvers.mbonus(30, 10)},
+		resists={[DamageType.COLD] = resolvers.mbonus_material(30, 10)},
 	},
 }
 newEntity{
@@ -41,7 +41,7 @@ newEntity{
 	rarity = 5,
 	cost = 6,
 	wielder = {
-		resists={[DamageType.ACID] = resolvers.mbonus(30, 10)},
+		resists={[DamageType.ACID] = resolvers.mbonus_material(30, 10)},
 	},
 }
 newEntity{
@@ -50,7 +50,7 @@ newEntity{
 	rarity = 5,
 	cost = 6,
 	wielder = {
-		resists={[DamageType.LIGHTNING] = resolvers.mbonus(30, 10)},
+		resists={[DamageType.LIGHTNING] = resolvers.mbonus_material(30, 10)},
 	},
 }
 newEntity{
@@ -59,7 +59,7 @@ newEntity{
 	rarity = 5,
 	cost = 6,
 	wielder = {
-		resists={[DamageType.NATURE] = resolvers.mbonus(30, 10)},
+		resists={[DamageType.NATURE] = resolvers.mbonus_material(30, 10)},
 	},
 }
 
@@ -69,7 +69,7 @@ newEntity{
 	rarity = 7,
 	cost = 6,
 	wielder = {
-		max_mana = resolvers.mbonus(100, 10),
+		max_mana = resolvers.mbonus_material(100, 10),
 	},
 }
 
@@ -79,7 +79,7 @@ newEntity{
 	rarity = 7,
 	cost = 6,
 	wielder = {
-		on_melee_hit={[DamageType.SLIME] = resolvers.mbonus(7, 3)},
+		on_melee_hit={[DamageType.SLIME] = resolvers.mbonus_material(7, 3)},
 	},
 }
 
@@ -90,14 +90,14 @@ newEntity{
 	cost = 15,
 	wielder = {
 		inc_damage = {
-			[DamageType.ARCANE] = resolvers.mbonus(15, 5),
-			[DamageType.FIRE] = resolvers.mbonus(15, 5),
-			[DamageType.COLD] = resolvers.mbonus(15, 5),
-			[DamageType.ACID] = resolvers.mbonus(15, 5),
-			[DamageType.LIGHTNING] = resolvers.mbonus(15, 5),
-			[DamageType.NATURE] = resolvers.mbonus(15, 5),
-			[DamageType.BLIGHT] = resolvers.mbonus(15, 5),
-			[DamageType.PHYSICAL] = resolvers.mbonus(15, 5),
+			[DamageType.ARCANE] = resolvers.mbonus_material(15, 5),
+			[DamageType.FIRE] = resolvers.mbonus_material(15, 5),
+			[DamageType.COLD] = resolvers.mbonus_material(15, 5),
+			[DamageType.ACID] = resolvers.mbonus_material(15, 5),
+			[DamageType.LIGHTNING] = resolvers.mbonus_material(15, 5),
+			[DamageType.NATURE] = resolvers.mbonus_material(15, 5),
+			[DamageType.BLIGHT] = resolvers.mbonus_material(15, 5),
+			[DamageType.PHYSICAL] = resolvers.mbonus_material(15, 5),
 		},
 		combat_spellpower = 4,
 	},
diff --git a/game/modules/tome/data/general/objects/egos/shield.lua b/game/modules/tome/data/general/objects/egos/shield.lua
index 489a45b2dd..f5976d3578 100644
--- a/game/modules/tome/data/general/objects/egos/shield.lua
+++ b/game/modules/tome/data/general/objects/egos/shield.lua
@@ -23,7 +23,7 @@ newEntity{
 	rarity = 5,
 	cost = 4,
 	wielder = {
-		resists={[DamageType.FIRE] = resolvers.mbonus(20, 10)},
+		resists={[DamageType.FIRE] = resolvers.mbonus_material(20, 10)},
 	},
 }
 newEntity{
@@ -32,7 +32,7 @@ newEntity{
 	rarity = 5,
 	cost = 4,
 	wielder = {
-		resists={[DamageType.COLD] = resolvers.mbonus(20, 10)},
+		resists={[DamageType.COLD] = resolvers.mbonus_material(20, 10)},
 	},
 }
 newEntity{
@@ -41,7 +41,7 @@ newEntity{
 	rarity = 5,
 	cost = 4,
 	wielder = {
-		resists={[DamageType.ACID] = resolvers.mbonus(20, 10)},
+		resists={[DamageType.ACID] = resolvers.mbonus_material(20, 10)},
 	},
 }
 newEntity{
@@ -50,7 +50,7 @@ newEntity{
 	rarity = 5,
 	cost = 4,
 	wielder = {
-		resists={[DamageType.LIGHTNING] = resolvers.mbonus(20, 10)},
+		resists={[DamageType.LIGHTNING] = resolvers.mbonus_material(20, 10)},
 	},
 }
 newEntity{
@@ -59,7 +59,7 @@ newEntity{
 	rarity = 5,
 	cost = 4,
 	wielder = {
-		resists={[DamageType.NATURE] = resolvers.mbonus(20, 10)},
+		resists={[DamageType.NATURE] = resolvers.mbonus_material(20, 10)},
 	},
 }
 
@@ -70,7 +70,7 @@ newEntity{
 	rarity = 8,
 	cost = 8,
 	wielder = {
-		on_melee_hit={[DamageType.FIRE] = resolvers.mbonus(7, 3)},
+		on_melee_hit={[DamageType.FIRE] = resolvers.mbonus_material(7, 3)},
 	},
 }
 newEntity{
@@ -79,7 +79,7 @@ newEntity{
 	rarity = 8,
 	cost = 10,
 	wielder = {
-		on_melee_hit={[DamageType.ICE] = resolvers.mbonus(4, 3)},
+		on_melee_hit={[DamageType.ICE] = resolvers.mbonus_material(4, 3)},
 	},
 }
 newEntity{
@@ -88,7 +88,7 @@ newEntity{
 	rarity = 8,
 	cost = 8,
 	wielder = {
-		on_melee_hit={[DamageType.ACID] = resolvers.mbonus(7, 3)},
+		on_melee_hit={[DamageType.ACID] = resolvers.mbonus_material(7, 3)},
 	},
 }
 newEntity{
@@ -97,7 +97,7 @@ newEntity{
 	rarity = 8,
 	cost = 8,
 	wielder = {
-		on_melee_hit={[DamageType.LIGHTNING] = resolvers.mbonus(7, 3)},
+		on_melee_hit={[DamageType.LIGHTNING] = resolvers.mbonus_material(7, 3)},
 	},
 }
 
@@ -107,7 +107,7 @@ newEntity{
 	rarity = 15,
 	cost = 20,
 	wielder = {
-		combat_def=resolvers.mbonus(15, 4),
+		combat_def=resolvers.mbonus_material(15, 4),
 	},
 }
 
@@ -117,6 +117,6 @@ newEntity{
 	rarity = 15,
 	cost = 20,
 	wielder = {
-		max_life=resolvers.mbonus(100, 10),
+		max_life=resolvers.mbonus_material(100, 10),
 	},
 }
diff --git a/game/modules/tome/data/general/objects/egos/staves.lua b/game/modules/tome/data/general/objects/egos/staves.lua
index 6ea42aaf30..e055ca49c0 100644
--- a/game/modules/tome/data/general/objects/egos/staves.lua
+++ b/game/modules/tome/data/general/objects/egos/staves.lua
@@ -25,7 +25,7 @@ newEntity{
 	rarity = 4,
 	cost = 8,
 	wielder = {
-		combat_spellpower = resolvers.mbonus(30, 3),
+		combat_spellpower = resolvers.mbonus_material(30, 3),
 	},
 }
 
@@ -35,7 +35,7 @@ newEntity{
 	rarity = 3,
 	cost = 8,
 	wielder = {
-		max_mana = resolvers.mbonus(100, 10),
+		max_mana = resolvers.mbonus_material(100, 10),
 	},
 }
 
@@ -45,7 +45,7 @@ newEntity{
 	rarity = 3,
 	cost = 8,
 	wielder = {
-		combat_spellcrit = resolvers.mbonus(15, 4),
+		combat_spellcrit = resolvers.mbonus_material(15, 4),
 	},
 }
 
@@ -55,9 +55,9 @@ newEntity{
 	rarity = 12,
 	cost = 45,
 	wielder = {
-		combat_spellpower = resolvers.mbonus(30, 3),
-		max_mana = resolvers.mbonus(100, 10),
-		inc_stats = { [Stats.STAT_MAG] = resolvers.mbonus(5, 1), [Stats.STAT_WIL] = resolvers.mbonus(5, 1) },
+		combat_spellpower = resolvers.mbonus_material(30, 3),
+		max_mana = resolvers.mbonus_material(100, 10),
+		inc_stats = { [Stats.STAT_MAG] = resolvers.mbonus_material(5, 1), [Stats.STAT_WIL] = resolvers.mbonus_material(5, 1) },
 	},
 }
 
@@ -67,7 +67,7 @@ newEntity{
 	rarity = 3,
 	cost = 5,
 	wielder = {
-		inc_damage={ [DamageType.FIRE] = resolvers.mbonus(25, 8), },
+		inc_damage={ [DamageType.FIRE] = resolvers.mbonus_material(25, 8), },
 	},
 }
 
@@ -77,7 +77,7 @@ newEntity{
 	rarity = 3,
 	cost = 5,
 	wielder = {
-		inc_damage={ [DamageType.COLD] = resolvers.mbonus(25, 8), },
+		inc_damage={ [DamageType.COLD] = resolvers.mbonus_material(25, 8), },
 	},
 }
 
@@ -87,7 +87,7 @@ newEntity{
 	rarity = 3,
 	cost = 5,
 	wielder = {
-		inc_damage={ [DamageType.ACID] = resolvers.mbonus(25, 8), },
+		inc_damage={ [DamageType.ACID] = resolvers.mbonus_material(25, 8), },
 	},
 }
 
@@ -97,7 +97,7 @@ newEntity{
 	rarity = 3,
 	cost = 5,
 	wielder = {
-		inc_damage={ [DamageType.LIGHTNING] = resolvers.mbonus(25, 8), },
+		inc_damage={ [DamageType.LIGHTNING] = resolvers.mbonus_material(25, 8), },
 	},
 }
 
@@ -107,7 +107,7 @@ newEntity{
 	rarity = 3,
 	cost = 5,
 	wielder = {
-		inc_damage={ [DamageType.NATURE] = resolvers.mbonus(25, 8), },
+		inc_damage={ [DamageType.NATURE] = resolvers.mbonus_material(25, 8), },
 	},
 }
 
@@ -117,6 +117,6 @@ newEntity{
 	rarity = 3,
 	cost = 5,
 	wielder = {
-		inc_damage={ [DamageType.BLIGHT] = resolvers.mbonus(25, 8), },
+		inc_damage={ [DamageType.BLIGHT] = resolvers.mbonus_material(25, 8), },
 	},
 }
diff --git a/game/modules/tome/data/general/objects/egos/weapon.lua b/game/modules/tome/data/general/objects/egos/weapon.lua
index 8570e46234..e0e6d2ea6b 100644
--- a/game/modules/tome/data/general/objects/egos/weapon.lua
+++ b/game/modules/tome/data/general/objects/egos/weapon.lua
@@ -22,7 +22,7 @@ newEntity{
 	level_range = {1, 50},
 	rarity = 5,
 	wielder = {
-		melee_project={[DamageType.FIRE] = resolvers.mbonus(25, 4)},
+		melee_project={[DamageType.FIRE] = resolvers.mbonus_material(25, 4)},
 	},
 }
 newEntity{
@@ -30,7 +30,7 @@ newEntity{
 	level_range = {15, 50},
 	rarity = 5,
 	wielder = {
-		melee_project={[DamageType.ICE] = resolvers.mbonus(15, 4)},
+		melee_project={[DamageType.ICE] = resolvers.mbonus_material(15, 4)},
 	},
 }
 newEntity{
@@ -38,7 +38,7 @@ newEntity{
 	level_range = {1, 50},
 	rarity = 5,
 	wielder = {
-		melee_project={[DamageType.ACID] = resolvers.mbonus(25, 4)},
+		melee_project={[DamageType.ACID] = resolvers.mbonus_material(25, 4)},
 	},
 }
 newEntity{
@@ -46,7 +46,7 @@ newEntity{
 	level_range = {1, 50},
 	rarity = 5,
 	wielder = {
-		melee_project={[DamageType.LIGHTNING] = resolvers.mbonus(25, 4)},
+		melee_project={[DamageType.LIGHTNING] = resolvers.mbonus_material(25, 4)},
 	},
 }
 newEntity{
@@ -54,7 +54,7 @@ newEntity{
 	level_range = {1, 50},
 	rarity = 5,
 	wielder = {
-		melee_project={[DamageType.POISON] = resolvers.mbonus(45, 6)},
+		melee_project={[DamageType.POISON] = resolvers.mbonus_material(45, 6)},
 	},
 }
 
@@ -63,7 +63,7 @@ newEntity{
 	level_range = {10, 50},
 	rarity = 5,
 	wielder = {
-		melee_project={[DamageType.SLIME] = resolvers.mbonus(45, 6)},
+		melee_project={[DamageType.SLIME] = resolvers.mbonus_material(45, 6)},
 	},
 }
 
@@ -72,7 +72,7 @@ newEntity{
 	level_range = {1, 50},
 	rarity = 3,
 	cost = 4,
-	combat={atk = resolvers.mbonus(20, 2)},
+	combat={atk = resolvers.mbonus_material(20, 2)},
 }
 
 newEntity{
@@ -80,7 +80,7 @@ newEntity{
 	level_range = {1, 50},
 	rarity = 3,
 	cost = 6,
-	combat={apr = resolvers.mbonus(15, 1)},
+	combat={apr = resolvers.mbonus_material(15, 1)},
 }
 
 newEntity{
@@ -90,10 +90,10 @@ newEntity{
 	cost = 35,
 	wielder = {
 		melee_project={
-			[DamageType.FIRE] = resolvers.mbonus(25, 4),
-			[DamageType.ICE] = resolvers.mbonus(15, 4),
-			[DamageType.ACID] = resolvers.mbonus(25, 4),
-			[DamageType.LIGHTNING] = resolvers.mbonus(25, 4),
+			[DamageType.FIRE] = resolvers.mbonus_material(25, 4),
+			[DamageType.ICE] = resolvers.mbonus_material(15, 4),
+			[DamageType.ACID] = resolvers.mbonus_material(25, 4),
+			[DamageType.LIGHTNING] = resolvers.mbonus_material(25, 4),
 		},
 	},
 }
@@ -104,6 +104,6 @@ newEntity{
 	rarity = 3,
 	cost = 4,
 	wielder = {
-		inc_damage={ [DamageType.PHYSICAL] = resolvers.mbonus(25, 8), },
+		inc_damage={ [DamageType.PHYSICAL] = resolvers.mbonus_material(25, 8), },
 	},
 }
diff --git a/game/modules/tome/resolvers.lua b/game/modules/tome/resolvers.lua
index a5d9dce349..6cd61e1621 100644
--- a/game/modules/tome/resolvers.lua
+++ b/game/modules/tome/resolvers.lua
@@ -158,3 +158,17 @@ function resolvers.calc.chatfeature(t, e)
 	-- Delete the origin field
 	return nil
 end
+
+--- Random bonus based on level (sets the mbonus max level, we use 60 instead of 50 to get some forced randomness at high level)
+resolvers.mbonus_max_level = 60
+
+--- Random bonus based on level and material quality
+resolvers.current_level = 1
+function resolvers.mbonus_material(max, add)
+	return {__resolver="mbonus_material", max, add}
+end
+function resolvers.calc.mbonus_material(t, e)
+	local ml = e.material_level or 1
+	print("RESOLVER MBONUS MATERIAL", ml, math.ceil(t[1] * ml / 5))
+	return math.ceil(rng.mbonus(t[1], resolvers.current_level, resolvers.mbonus_max_level) * ml / 5) + (t[2] or 0)
+end
-- 
GitLab