From 580da61289da2d27e865452493fa9e2c1370c7cd Mon Sep 17 00:00:00 2001
From: dg <dg@51575b47-30f0-44d4-a5cc-537603b46e54>
Date: Sat, 11 Sep 2010 00:30:21 +0000
Subject: [PATCH] The money achievements will pop no matter the mean of
 acquiring money

git-svn-id: http://svn.net-core.org/repos/t-engine4@1180 51575b47-30f0-44d4-a5cc-537603b46e54
---
 game/modules/tome/class/Actor.lua                    | 12 ++++++++++++
 game/modules/tome/class/Store.lua                    |  4 ++--
 game/modules/tome/data/chats/jewelry-store.lua       |  4 ++--
 game/modules/tome/data/chats/magic-store.lua         |  2 +-
 .../tome/data/chats/minas-tirith-weapon-store.lua    |  4 ++--
 game/modules/tome/data/general/objects/money.lua     |  7 +------
 game/modules/tome/data/quests/lost-merchant.lua      |  2 +-
 7 files changed, 21 insertions(+), 14 deletions(-)

diff --git a/game/modules/tome/class/Actor.lua b/game/modules/tome/class/Actor.lua
index d32a8596d3..43e819962d 100644
--- a/game/modules/tome/class/Actor.lua
+++ b/game/modules/tome/class/Actor.lua
@@ -357,6 +357,18 @@ function _M:magicMap(radius, x, y)
 	end
 end
 
+function _M:incMoney(v)
+	self.money = self.money + v
+	if self.money < 0 then self.money = 0 end
+	self.changed = true
+
+	if self.player then
+		world:gainAchievement("TREASURE_HUNTER", self)
+		world:gainAchievement("TREASURE_HOARDER", self)
+		world:gainAchievement("DRAGON_GREED", self)
+	end
+end
+
 function _M:getRankStatAdjust()
 	if self.rank == 1 then return -1
 	elseif self.rank == 2 then return -0.5
diff --git a/game/modules/tome/class/Store.lua b/game/modules/tome/class/Store.lua
index 02aab50dcd..09daded689 100644
--- a/game/modules/tome/class/Store.lua
+++ b/game/modules/tome/class/Store.lua
@@ -73,7 +73,7 @@ end
 function _M:onBuy(who, o, item, nb)
 	local price = o:getPrice() * self.sell_percent / 100
 	if who.money >= price * nb then
-		who.money = who.money - price * nb
+		who:incMoney(- price * nb)
 	end
 end
 
@@ -86,7 +86,7 @@ end
 function _M:onSell(who, o, item, nb)
 	local price = o:getPrice() * self.buy_percent / 100
 	if price <= 0 or nb <= 0 then return end
-	who.money = who.money + price * nb
+	who:incMoney(price * nb)
 	o:identify(true)
 end
 
diff --git a/game/modules/tome/data/chats/jewelry-store.lua b/game/modules/tome/data/chats/jewelry-store.lua
index 4308163e1a..6d6316b2c5 100644
--- a/game/modules/tome/data/chats/jewelry-store.lua
+++ b/game/modules/tome/data/chats/jewelry-store.lua
@@ -24,7 +24,7 @@ local imbue_ring = function(npc, player)
 			if price > player.money then require("engine.Dialog"):simplePopup("Not enough money", "This costs "..price.." gold, you need more gold.") return end
 
 			require("engine.Dialog"):yesnoPopup("Imbue cost", "This will cost you "..price.." gold, do you accept?", function(ret) if ret then
-				player.money = player.money - price
+				player:incMoney(-price)
 				player:removeObject(player:getInven("INVEN"), gem_item)
 				ring.wielder = ring.wielder or {}
 				table.mergeAdd(ring.wielder, gem.imbue_powers, true)
@@ -45,7 +45,7 @@ local artifact_imbue_ring = function(npc, player)
 				if price > player.money then require("engine.Dialog"):simplePopup("Not enough money", "Limmir needs more gold for the magical plating.") return end
 
 				require("engine.Dialog"):yesnoPopup("Imbue cost", "You need to use "..price.." gold for the plating, do you accept?", function(ret) if ret then
-					player.money = player.money - price
+					player:incMoney(-price)
 					local gem3 = game.zone:makeEntity(game.level, "object", {type="gem"}, nil, true)
 					print("Imbue third gem", gem3.name)
 
diff --git a/game/modules/tome/data/chats/magic-store.lua b/game/modules/tome/data/chats/magic-store.lua
index 992e61a449..1119ebd37b 100644
--- a/game/modules/tome/data/chats/magic-store.lua
+++ b/game/modules/tome/data/chats/magic-store.lua
@@ -23,7 +23,7 @@ local function recharge(npc, player)
 		if cost > player.money then require("engine.Dialog"):simplePopup("Not enough money", "This costs "..cost.." gold.") return true end
 		require("engine.Dialog"):yesnoPopup("Recharge?", "This will cost you "..cost.." gold.", function(ok) if ok then
 			o.power = o.max_power
-			player.money = player.money - cost
+			player:incMoney(-cost)
 			player.changed = true
 		end end)
 		return true
diff --git a/game/modules/tome/data/chats/minas-tirith-weapon-store.lua b/game/modules/tome/data/chats/minas-tirith-weapon-store.lua
index 98222e38a4..db5834e822 100644
--- a/game/modules/tome/data/chats/minas-tirith-weapon-store.lua
+++ b/game/modules/tome/data/chats/minas-tirith-weapon-store.lua
@@ -34,7 +34,7 @@ newChat{ id="training",
 	answers = {
 		{"Please train me in generic weapons and armour usage.", action=function(npc, player)
 			game.logPlayer(player, "The smith spends some time with you, teaching you the basics of armour and weapon usage.")
-			player.money = player.money - 50
+			player:incMoney(-50)
 			player:learnTalentType("technique/combat-training", true)
 			player.changed = true
 		end, cond=function(npc, player)
@@ -44,7 +44,7 @@ newChat{ id="training",
 		end},
 		{"Please train me in the basic usage of bows and slings.", action=function(npc, player)
 			game.logPlayer(player, "The smith spends some time with you, teaching you the basics of bows and slings.")
-			player.money = player.money - 8
+			player:incMoney(-8)
 			player:learnTalent(player.T_SHOOT, true)
 			player.changed = true
 		end, cond=function(npc, player)
diff --git a/game/modules/tome/data/general/objects/money.lua b/game/modules/tome/data/general/objects/money.lua
index c742152a38..ed48c2be66 100644
--- a/game/modules/tome/data/general/objects/money.lua
+++ b/game/modules/tome/data/general/objects/money.lua
@@ -26,15 +26,10 @@ newEntity{
 	identified = true,
 	desc = [[All that glitters is not gold, all that is gold does not glitter.]],
 	on_prepickup = function(self, who, id)
-		who.money = who.money + self.money_value / 10
+		who:incMoney(self.money_value / 10)
 		game.logPlayer(who, "You pickup %0.2f gold pieces.", self.money_value / 10)
 		-- Remove from the map
 		game.level.map:removeObject(who.x, who.y, id)
-		if who.player then
-			world:gainAchievement("TREASURE_HUNTER", who)
-			world:gainAchievement("TREASURE_HOARDER", who)
-			world:gainAchievement("DRAGON_GREED", who)
-		end
 		return true
 	end,
 	auto_pickup = true,
diff --git a/game/modules/tome/data/quests/lost-merchant.lua b/game/modules/tome/data/quests/lost-merchant.lua
index 727485ffbe..c914c87b34 100644
--- a/game/modules/tome/data/quests/lost-merchant.lua
+++ b/game/modules/tome/data/quests/lost-merchant.lua
@@ -47,7 +47,7 @@ leave_zone = function(self, who)
 	end
 	if merchant_alive then
 		game.logPlayer(who, "#LIGHT_BLUE#The merchant thanks you for saving his life. He gives you 8 gold and asks you to meet him again in Minas Tirith.")
-		who.money = who.money + 8
+		player:incMoney(8)
 		who.changed = true
 		who:setQuestStatus(self.id, engine.Quest.COMPLETED, "saved")
 		world:gainAchievement("LOST_MERCHANT_RESCUE", game.player)
-- 
GitLab