Skip to content
Snippets Groups Projects
Commit 1953737b authored by dg's avatar dg
Browse files

fix inventory stacking on wear/takeoff

git-svn-id: http://svn.net-core.org/repos/t-engine4@363 51575b47-30f0-44d4-a5cc-537603b46e54
parent b979647e
No related branches found
No related tags found
No related merge requests found
......@@ -43,7 +43,12 @@ end
--- Gets the full name of the object
function _M:getName()
return self.name
local qty = self:getNumber()
local name = self.name
if qty == 1 then return name
else return qty.." "..name
end
end
--- Gets the full desc of the object
......@@ -91,8 +96,8 @@ function _M:stack(o)
-- Merge stacks
if o.stacked then
for i, so in ipairs(o.stacked) do
self.stacked[#self.stacked+1] = so
for i = 1, #o.stacked do
self.stacked[#self.stacked+1] = o.stacked[i]
end
o.stacked = nil
end
......
......@@ -57,9 +57,7 @@ function _M:generateList()
for item, o in ipairs(self.actor.inven[inven_id]) do
if not self.filter or self.filter(o) then
local char = string.char(string.byte('a') + i)
local nb = o:getNumber()
nb = (nb == 1) and "" or nb.." "
list[#list+1] = { name=char..") "..nb..o:getName(), object=o, inven=inven_id, item=item }
list[#list+1] = { name=char..") "..o:getName(), object=o, inven=inven_id, item=item }
chars[char] = #list
i = i + 1
end
......
......@@ -97,9 +97,7 @@ function _M:generateList()
local i = 0
for item, o in ipairs(self.inven) do
if not self.filter or self.filter(o) then
local nb = o:getNumber()
nb = (nb == 1) and "" or nb.." "
list[#list+1] = { name=string.char(string.byte('a') + i)..") "..nb..o:getName(), object=o, item=item }
list[#list+1] = { name=string.char(string.byte('a') + i)..") "..o:getName(), object=o, item=item }
i = i + 1
end
end
......
......@@ -60,9 +60,7 @@ function _M:generateList()
local o = game.level.map:getObject(self.x, self.y, idx)
if not o then break end
if not self.filter or self.filter(o) then
local nb = o:getNumber()
nb = (nb == 1) and "" or nb.." "
list[#list+1] = { name=string.char(string.byte('a') + i)..") "..nb..o:getName(), object=o, item=idx }
list[#list+1] = { name=string.char(string.byte('a') + i)..") "..o:getName(), object=o, item=idx }
i = i + 1
end
idx = idx + 1
......
......@@ -96,17 +96,19 @@ end
--- Removes an object from inventory
-- @param inven the inventory to drop from
-- @param item the item id to drop
-- @param no_unstack if the item was a stack takes off the whole stack if true
-- @return the object removed or nil if no item existed
function _M:removeObject(inven, item)
function _M:removeObject(inven, item, no_unstack)
if type(inven) == "number" then inven = self.inven[inven] end
local o = inven[item]
local o, finish = o:unstack()
local o, finish = inven[item], true
if not no_unstack then
o, finish = o:unstack()
end
if finish then
table.remove(inven, item)
end
-- Do whatever is needed when takingoff this object
print("remove object", inven, inven.max, inven.worn, item, o)
if inven.worn then self:onTakeoff(o) end
return o
......@@ -217,8 +219,13 @@ function _M:wearObject(o, replace, vocal)
-- Warning: assume there is now space
self:addObject(self:getInven(o.offslot), o)
elseif replace then
local ro = self:removeObject(inven, 1)
local ro = self:removeObject(inven, 1, true)
if vocal then game.logSeen(self, "%s wears: %s.", self.name:capitalize(), o:getName()) end
-- Can we stack the old and new one ?
if o:stack(ro) then ro = true end
-- Warning: assume there is now space
self:addObject(inven, o)
return ro
......@@ -230,7 +237,7 @@ end
--- Takeoff item
function _M:takeoffObject(inven, item)
local o = self:removeObject(inven, item)
local o = self:removeObject(inven, item, true)
return o
end
......@@ -263,7 +270,9 @@ function _M:sortInven(inven)
for i = #inven, 1, -1 do
-- If it is stackable, look for obejcts before it that it could stack into
if inven[i]:stackable() then
print("sorting", #inven, i, i-1)
for j = i - 1, 1, -1 do
print("j",j)
if inven[j]:stack(inven[i]) then
print("Stacked objects", i, inven[i].name, "into", j, inven[j].name)
table.remove(inven, i)
......
......@@ -74,7 +74,7 @@ end
--- Gets the full name of the object
function _M:getName()
local qty = 1
local qty = self:getNumber()
local name = self.name
if not self:isIdentified() and self:getUnidentifiedName() then name = self:getUnidentifiedName() end
......@@ -90,7 +90,11 @@ function _M:getName()
end)
end
return name
if qty == 1 then
return name
else
return qty.." "..name
end
end
--- Gets the full desc of the object
......
......@@ -234,10 +234,12 @@ function _M:playerWear()
self:showInventory("Wield/wear object", inven, function(o)
return o:wornInven() and true or false
end, function(o, item)
self:removeObject(self.INVEN_INVEN, item, true)
local ro = self:wearObject(o, true, true)
if ro then
if type(ro) == "table" then self:addObject(self.INVEN_INVEN, ro) end
self:removeObject(self.INVEN_INVEN, item)
else
self:addObject(self.INVEN_INVEN, o)
end
self:sortInven()
self:useEnergy()
......
......@@ -154,7 +154,7 @@ newTalent{
o:identify(true)
o:forAllStack(function(so) so.cost = 0 end)
self:addObject(self.INVEN_INVEN, o)
game.logPlayer(self, "You create some ammo: %d %s", o:getNumber(), o:getName())
game.logPlayer(self, "You create some ammo: %s", o:getName())
else
game.logPlayer(self, "You found nothing!")
end
......
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment