From 3e894ec8a31437dee8f25d2ec8cf8781689e53b8 Mon Sep 17 00:00:00 2001
From: DarkGod <darkgod@net-core.org>
Date: Thu, 27 Aug 2020 15:49:23 +0200
Subject: [PATCH] Skeletons, Bone Giants, Ghouls and Dreads will unsummon if
 removing points in their talents so taht they are not supported anymore
 Swapped Putrescent Liquifaction and Corpse Explosion

---
 .../tome/data/talents/spells/dreadmaster.lua  | 18 +++++
 .../data/talents/spells/master-of-bones.lua   | 35 +++++++-
 .../data/talents/spells/master-of-flesh.lua   | 79 +++++++++++--------
 game/modules/tome/dialogs/LevelupDialog.lua   | 11 +++
 4 files changed, 109 insertions(+), 34 deletions(-)

diff --git a/game/modules/tome/data/talents/spells/dreadmaster.lua b/game/modules/tome/data/talents/spells/dreadmaster.lua
index ae0aac8b2c..37319be246 100644
--- a/game/modules/tome/data/talents/spells/dreadmaster.lua
+++ b/game/modules/tome/data/talents/spells/dreadmaster.lua
@@ -121,6 +121,15 @@ newTalent{
 	},
 	getLevel = function(self, t) return math.floor(self:combatScale(self:getTalentLevel(t), -6, 0.9, 2, 5)) end, -- -6 @ 1, +2 @ 5, +5 @ 8
 	on_pre_use = function(self, t) return not necroArmyStats(self).dread end,
+	-- Fucking respec.
+	on_levelup_changed = function(self, t, lvl, old_lvl, lvl_raw, old_lvl_raw)
+		if lvl >= old_lvl then return end
+		local stats = necroArmyStats(self)
+		if stats.dread then
+			game.party:removeMember(stats.dread, true)
+			stats.dread:disappear(self)
+		end
+	end,
 	action = function(self, t)
 		local lev = t.getLevel(self, t)
 
@@ -235,6 +244,15 @@ newTalent{
 	require = spells_req4,
 	points = 5,
 	mode = "passive",
+	-- Fucking respec.
+	on_levelup_changed = function(self, t, lvl, old_lvl, lvl_raw, old_lvl_raw)
+		if lvl >= old_lvl then return end
+		local stats = necroArmyStats(self)
+		if stats.dread then
+			game.party:removeMember(stats.dread, true)
+			stats.dread:disappear(self)
+		end
+	end,
 	info = function(self, t)
 		return ([[You now summon a Dreadmaster instead of a Dread.
 		Dreadmasters learn to cast silence, disperse magic and mind disruption, making them the ultimate annoyance tool.
diff --git a/game/modules/tome/data/talents/spells/master-of-bones.lua b/game/modules/tome/data/talents/spells/master-of-bones.lua
index 16516a71c7..81e2b04d9b 100644
--- a/game/modules/tome/data/talents/spells/master-of-bones.lua
+++ b/game/modules/tome/data/talents/spells/master-of-bones.lua
@@ -55,7 +55,7 @@ newTalent{
 			poison_immune = 1,
 			undead = 1,
 			rarity = 1,
-			skeleton_minion = "warrior",
+			skeleton_minion = "warrior", basic_skeleton_minion = true,
 
 			max_life = resolvers.rngavg(90,100),
 			combat_armor = 5, combat_def = 1,
@@ -185,6 +185,30 @@ newTalent{
 		if ignore then return max end
 		return math.max(0, max - necroArmyStats(self).nb_skeleton)
 	end,
+	-- Fucking respec.
+	on_levelup_changed = function(self, t, lvl, old_lvl, lvl_raw, old_lvl_raw)
+		local stats = necroArmyStats(self)
+		for i, minion in ipairs(stats.list) do if minion.skeleton_minion then
+			if self:getTalentLevel(t) < 3 and not minion.basic_skeleton_minion then
+				game.party:removeMember(minion, true)
+				minion:disappear(self)
+			elseif self:getTalentLevel(t) < 5 and (minion.skeleton_minion == "mage" or minion.skeleton_minion == "archer") then
+				game.party:removeMember(minion, true)
+				minion:disappear(self)
+			end
+		end end
+		while true do
+			local stats = necroArmyStats(self)
+			local max = t:_getMax(self, true)
+			if lvl_raw <= 0 then max = 0 end
+			if stats.nb_skeleton <= max then break end
+			for i, minion in ipairs(stats.list) do if minion.skeleton_minion then
+				game.party:removeMember(minion, true)
+				minion:disappear(self)
+				break
+			end end
+		end
+	end,
 	getLevel = function(self, t) return math.floor(self:combatScale(self:getTalentLevel(t), -6, 0.9, 2, 5)) end, -- -6 @ 1, +2 @ 5, +5 @ 8
 	on_pre_use = function(self, t) return math.min(t.getMax(self, t), self:getSoul()) >= 1 end,
 	action = function(self, t)
@@ -407,6 +431,15 @@ newTalent{
 	range = 10,
 	getLevel = function(self, t) return math.floor(self:combatScale(self:getTalentLevel(t), -6, 0.9, 2, 5)) end, -- -6 @ 1, +2 @ 5, +5 @ 8
 	on_pre_use = function(self, t) local stats = necroArmyStats(self) return stats.nb_skeleton >= (stats.lord_of_skulls and 4 or 3) end,
+	-- Fucking respec.
+	on_levelup_changed = function(self, t, lvl, old_lvl, lvl_raw, old_lvl_raw)
+		if lvl >= old_lvl then return end
+		local stats = necroArmyStats(self)
+		if stats.bone_giant then
+			game.party:removeMember(stats.bone_giant, true)
+			stats.bone_giant:disappear(self)
+		end
+	end,
 	action = function(self, t)
 		local stats = necroArmyStats(self)
 		if stats.nb_skeleton < (stats.lord_of_skulls and 4 or 3) then return end
diff --git a/game/modules/tome/data/talents/spells/master-of-flesh.lua b/game/modules/tome/data/talents/spells/master-of-flesh.lua
index 66a592482e..c80222e3f9 100644
--- a/game/modules/tome/data/talents/spells/master-of-flesh.lua
+++ b/game/modules/tome/data/talents/spells/master-of-flesh.lua
@@ -58,7 +58,7 @@ newTalent{
 			},
 			ai_state = { talent_in=4, },
 			combat = { dam=resolvers.levelup(10, 1, 1), atk=resolvers.levelup(5, 1, 1), apr=3, dammod={str=0.6} },
-			ghoul_minion = "ghoul",
+			ghoul_minion = "ghoul", basic_ghoul_minion = true,
 		},
 		ghast = {
 			type = "undead", subtype = "ghoul",
@@ -129,6 +129,19 @@ newTalent{
 	getTurns = function(self, t, ignore) return math.floor(self:combatTalentScale(t, 5, 10)) end,
 	getLevel = function(self, t) return math.floor(self:combatScale(self:getTalentLevel(t), -6, 0.9, 2, 5)) end, -- -6 @ 1, +2 @ 5, +5 @ 8
 	on_pre_use = function(self, t) return self:getTalentLevel(t) >= 3 and self:getSoul() >= 1 end,
+	-- Fucking respec.
+	on_levelup_changed = function(self, t, lvl, old_lvl, lvl_raw, old_lvl_raw)
+		local stats = necroArmyStats(self)
+		for i, minion in ipairs(stats.list) do if minion.ghoul_minion then
+			if self:getTalentLevel(t) < 3 and not minion.basic_ghoul_minion then
+				game.party:removeMember(minion, true)
+				minion:disappear(self)
+			elseif self:getTalentLevel(t) < 5 and minion.ghoul_minion == "ghoulking" then
+				game.party:removeMember(minion, true)
+				minion:disappear(self)
+			end
+		end end
+	end,
 	summonGhoul = function(self, t, possible_spots, def)
 		local pos = table.remove(possible_spots, 1)
 		if pos then
@@ -221,10 +234,40 @@ newTalent{
 }
 
 newTalent{
-	name = "Putrescent Liquefaction",
-	type = {"spell/master-of-flesh", 2},
+	name = "Corpse Explosion",
+	type = {"spell/master-of-flesh",2},
 	require = spells_req2,
 	points = 5,
+	cooldown = 20,
+	mana = 30,
+	tactical = { ATTACKAREA = {COLD=2, DARKNESS=2} },
+	requires_target = true,
+	radius = 2,
+	getDur = function(self, t) return math.floor(self:combatTalentScale(t, 3, 8)) end,
+	getDamage = function(self, t) return self:combatTalentSpellDamage(t, 40, 200) end,
+	getDiseasePower = function(self, t) return self:combatTalentSpellDamage(t, 5, 28) end,
+	on_pre_use = function(self, t) local stats = necroArmyStats(self) return stats.nb_ghoul > 0 end,
+	action = function(self, t)
+		self:setEffect(self.EFF_CORPSE_EXPLOSION, t:_getDur(self), {damage=t:_getDamage(self), disease=t:_getDiseasePower(self), radius=self:getTalentRadius(t)})
+		game:playSoundNear(self, "talents/spell_generic2")
+		return true
+	end,
+	info = function(self, t)
+		return ([[Ghouls are nothing but mere tools to you, for %d turns you render them bloated with dark forces.
+		Anytime a ghoul or ghast is hit it will explode in a messy splash of gore, dealing %0.2f frostdusk damage to all foes in radius %d of it.
+		Any creature caught in the blast also receives a random disease that deals %0.2f blight damage over 6 turns and reduces one attribute by %d.
+		Only one ghoul may explode per turn. The one with the least time left to live is always the first to do so.
+		The damage and disease power is increased by your Spellpower.
+		]]):
+		tformat(t:_getDur(self), damDesc(self, DamageType.FROSTDUSK, t:_getDamage(self)), self:getTalentRadius(t), damDesc(self, DamageType.BLIGHT, t:_getDamage(self)), t:_getDiseasePower(self))
+	end,
+}
+
+newTalent{
+	name = "Putrescent Liquefaction",
+	type = {"spell/master-of-flesh", 3},
+	require = spells_req3,
+	points = 5,
 	mode = "sustained",
 	mana = 20, -- This is NOT an error, this is a sustain but with an activation cost
 	soul = 1,
@@ -334,36 +377,6 @@ newTalent{
 	end,
 }
 
-newTalent{
-	name = "Corpse Explosion",
-	type = {"spell/master-of-flesh",3},
-	require = spells_req3,
-	points = 5,
-	cooldown = 20,
-	mana = 30,
-	tactical = { ATTACKAREA = {COLD=2, DARKNESS=2} },
-	requires_target = true,
-	radius = 2,
-	getDur = function(self, t) return math.floor(self:combatTalentScale(t, 3, 8)) end,
-	getDamage = function(self, t) return self:combatTalentSpellDamage(t, 40, 200) end,
-	getDiseasePower = function(self, t) return self:combatTalentSpellDamage(t, 5, 28) end,
-	on_pre_use = function(self, t) local stats = necroArmyStats(self) return stats.nb_ghoul > 0 end,
-	action = function(self, t)
-		self:setEffect(self.EFF_CORPSE_EXPLOSION, t:_getDur(self), {damage=t:_getDamage(self), disease=t:_getDiseasePower(self), radius=self:getTalentRadius(t)})
-		game:playSoundNear(self, "talents/spell_generic2")
-		return true
-	end,
-	info = function(self, t)
-		return ([[Ghouls are nothing but mere tools to you, for %d turns you render them bloated with dark forces.
-		Anytime a ghoul or ghast is hit it will explode in a messy splash of gore, dealing %0.2f frostdusk damage to all foes in radius %d of it.
-		Any creature caught in the blast also receives a random disease that deals %0.2f blight damage over 6 turns and reduces one attribute by %d.
-		Only one ghoul may explode per turn. The one with the least time left to live is always the first to do so.
-		The damage and disease power is increased by your Spellpower.
-		]]):
-		tformat(t:_getDur(self), damDesc(self, DamageType.FROSTDUSK, t:_getDamage(self)), self:getTalentRadius(t), damDesc(self, DamageType.BLIGHT, t:_getDamage(self)), t:_getDiseasePower(self))
-	end,
-}
-
 newTalent{
 	name = "Discarded Refuse",
 	type = {"spell/master-of-flesh", 4},
diff --git a/game/modules/tome/dialogs/LevelupDialog.lua b/game/modules/tome/dialogs/LevelupDialog.lua
index c384321013..6e6176c0a5 100644
--- a/game/modules/tome/dialogs/LevelupDialog.lua
+++ b/game/modules/tome/dialogs/LevelupDialog.lua
@@ -221,6 +221,17 @@ function _M:finish()
 		end
 	end
 
+	for t_id, _ in pairs(self.talents_changed) do
+		local t = self.actor:getTalentFromId(t_id)
+		if t.on_levelup_changed then
+			local lvl = self.actor:getTalentLevel(t_id)
+			local lvl_raw = self.actor:getTalentLevelRaw(t_id)
+			local old_lvl = self.actor_dup:getTalentLevel(t_id)
+			local old_lvl_raw = self.actor_dup:getTalentLevelRaw(t_id)
+			t.on_levelup_changed(self.actor, t, lvl, old_lvl, lvl_raw, old_lvl_raw)
+		end
+	end
+
 	if self.actor.player then
 		if self.actor.descriptor and self.actor.descriptor.race == "Dwarf" then
 			local count_nature, count_spell = 0, 0
-- 
GitLab