From f0af99529687f1b8a3b519dff0f6f1720ecf96bb Mon Sep 17 00:00:00 2001
From: dg <dg@51575b47-30f0-44d4-a5cc-537603b46e54>
Date: Sun, 28 Feb 2010 22:03:39 +0000
Subject: [PATCH] max encumberance

git-svn-id: http://svn.net-core.org/repos/t-engine4@370 51575b47-30f0-44d4-a5cc-537603b46e54
---
 game/engine/Object.lua                   |  1 +
 game/engine/dialogs/ShowInventory.lua    |  2 +-
 game/engine/interface/ActorInventory.lua | 12 +++++++
 game/modules/tome/class/Actor.lua        | 40 ++++++++++++++++++++++++
 4 files changed, 54 insertions(+), 1 deletion(-)

diff --git a/game/engine/Object.lua b/game/engine/Object.lua
index bf4424e8ca..442d32949c 100644
--- a/game/engine/Object.lua
+++ b/game/engine/Object.lua
@@ -122,6 +122,7 @@ end
 --- Applies a function to all items of the stack
 function _M:forAllStack(fct)
 	fct(self)
+	if not self.stacked then return end
 	for i, so in ipairs(self.stacked) do
 		fct(so)
 	end
diff --git a/game/engine/dialogs/ShowInventory.lua b/game/engine/dialogs/ShowInventory.lua
index 165f0a5501..5b1af69a61 100644
--- a/game/engine/dialogs/ShowInventory.lua
+++ b/game/engine/dialogs/ShowInventory.lua
@@ -8,7 +8,7 @@ function _M:init(title, inven, filter, action, actor)
 	self.filter = filter
 	self.action = action
 	self.actor = actor
-	engine.Dialog.init(self, title or "Inventory", game.w * 0.8, game.h * 0.8, nil, nil, nil, core.display.newFont("/data/font/VeraMono.ttf", 10))
+	engine.Dialog.init(self, title or "Inventory", game.w * 0.8, game.h * 0.8, nil, nil, nil, core.display.newFont("/data/font/VeraMono.ttf", 12))
 
 	self:generateList()
 
diff --git a/game/engine/interface/ActorInventory.lua b/game/engine/interface/ActorInventory.lua
index 1a0599b554..9884d67efa 100644
--- a/game/engine/interface/ActorInventory.lua
+++ b/game/engine/interface/ActorInventory.lua
@@ -77,9 +77,15 @@ function _M:addObject(inven_id, o)
 		self:onWear(o)
 	end
 
+	self:onAddObject(o)
+
 	return true
 end
 
+--- Called upon adding an object
+function _M:onAddObject(o)
+end
+
 --- Picks an object from the floor
 function _M:pickupFloor(i, vocal)
 	local o = game.level.map:getObject(self.x, self.y, i)
@@ -118,9 +124,15 @@ function _M:removeObject(inven, item, no_unstack)
 		self:onTakeoff(o)
 	end
 
+	self:onRemoveObject(o)
+
 	return o
 end
 
+--- Called upon removing an object
+function _M:onRemoveObject(o)
+end
+
 --- Drop an object on the floor
 -- @param inven the inventory to drop from
 -- @param item the item id to drop
diff --git a/game/modules/tome/class/Actor.lua b/game/modules/tome/class/Actor.lua
index 353cd4a28a..7932ae2716 100644
--- a/game/modules/tome/class/Actor.lua
+++ b/game/modules/tome/class/Actor.lua
@@ -330,6 +330,46 @@ function _M:attack(target)
 	self:bumpInto(target)
 end
 
+function _M:getMaxEncumberance()
+	return math.floor(40 + self:getStr() * 1.8)
+end
+
+function _M:checkEncumberance()
+	-- Compute encumberance
+	local enc, max = 0, self:getMaxEncumberance()
+	for inven_id, inven in pairs(self.inven) do
+		for item, o in ipairs(inven) do
+			o:forAllStack(function(so) enc = enc + so.encumber end)
+		end
+	end
+	print("Total encumberance", enc, max)
+
+	-- We are pinned to the ground if we carry too much
+	if not self.encumbered and enc > max then
+		game.logPlayer(self, "#FF0000#You carry too much, you are encumbered!")
+		game.logPlayer(self, "#FF0000#Drop some of your items.")
+		self.encumbered = self:addTemporaryValue("never_move", 1)
+	elseif self.encumbered and enc <= max then
+		self:removeTemporaryValue("never_move", self.encumbered)
+		self.encumbered = nil
+		game.logPlayer(self, "#00FF00#You are no longer encumbered.")
+	end
+end
+
+--- Call when an object is added
+function _M:onAddObject(o)
+	engine.interface.ActorInventory.onAddObject(self, o)
+
+	self:checkEncumberance()
+end
+
+--- Call when an object is removed
+function _M:onRemoveObject(o)
+	engine.interface.ActorInventory.onRemoveObject(self, o)
+
+	self:checkEncumberance()
+end
+
 --- Actor learns a talent
 -- @param t_id the id of the talent to learn
 -- @return true if the talent was learnt, nil and an error message otherwise
-- 
GitLab