summaryrefslogtreecommitdiff
path: root/lua
diff options
context:
space:
mode:
authorLNJ <git@lnj.li>2017-04-17 16:04:16 +0200
committerLNJ <git@lnj.li>2017-06-03 17:06:12 +0200
commitf3dafb5b6a82255a795972637377320f64c019b9 (patch)
treecb47078e207b244f8427a4dd54f429417c45ee12 /lua
parent1f0fdf832b5a24ee80df01b0f08a0902bad8e978 (diff)
Add Drawer Upgradesorigin/drawer-upgrades
Yey, they're finally there! :) This will add steel, gold, obsidian and diamond upgrades for MTG and iron, gold, obsidian, diamond and emerald upgrades for MCL2. You can add them to a drawer by rightclicking it and adding them to the new drawer's upgrade inventory.
Diffstat (limited to 'lua')
-rwxr-xr-xlua/api.lua81
-rwxr-xr-xlua/helpers.lua44
-rwxr-xr-xlua/visual.lua140
3 files changed, 212 insertions, 53 deletions
diff --git a/lua/api.lua b/lua/api.lua
index 9d54f53..736a8fb 100755
--- a/lua/api.lua
+++ b/lua/api.lua
@@ -37,6 +37,14 @@ drawers.node_box_simple = {
{-0.4375, -0.5, -0.5, 0.4375, -0.4375, -0.4375},
}
+drawers.drawer_formspec = "size[9,7]" ..
+ "list[context;upgrades;2,0.5;5,1;]" ..
+ "list[current_player;main;0,3;9,4;]" ..
+ drawers.gui_bg ..
+ drawers.gui_bg_img ..
+ drawers.gui_slots ..
+ drawers.get_upgrade_slots_bg(2, 0.5)
+
-- construct drawer
function drawers.drawer_on_construct(pos)
local node = core.get_node(pos)
@@ -66,7 +74,14 @@ function drawers.drawer_on_construct(pos)
i = i + 1
end
+ -- spawn all visuals
drawers.spawn_visuals(pos)
+
+ -- create drawer upgrade inventory
+ meta:get_inventory():set_size("upgrades", 5)
+
+ -- set the formspec
+ meta:set_string("formspec", drawers.drawer_formspec)
end
-- destruct drawer
@@ -113,17 +128,55 @@ function drawers.drawer_on_dig(pos, node, player)
k = k + 1
end
+ -- drop all drawer upgrades
+ local upgrades = meta:get_inventory():get_list("upgrades")
+ if upgrades then
+ for _,itemStack in pairs(upgrades) do
+ if itemStack:get_count() > 0 then
+ local rndpos = drawers.randomize_pos(pos)
+ core.add_item(rndpos, itemStack:get_name())
+ end
+ end
+ end
+
-- remove node
core.node_dig(pos, node, player)
end
+function drawers.drawer_allow_metadata_inventory_put(pos, listname, index, stack, player)
+ if listname ~= "upgrades" then
+ return 0
+ end
+ if stack:get_count() > 1 then
+ return 0
+ end
+ if core.get_item_group(stack:get_name(), "drawer_upgrade") < 1 then
+ return 0
+ end
+ return 1
+end
+
+function drawers.add_drawer_upgrade(pos, listname, index, stack, player)
+ -- only do anything if adding to upgrades
+ if listname ~= "upgrades" then return end
+
+ drawers.update_drawer_upgrades(pos)
+end
+
+function drawers.remove_drawer_upgrade(pos, listname, index, stack, player)
+ -- only do anything if adding to upgrades
+ if listname ~= "upgrades" then return end
+
+ drawers.update_drawer_upgrades(pos)
+end
+
function drawers.drawer_insert_object(pos, node, stack, direction)
local drawer_visuals = drawers.drawer_visuals[core.serialize(pos)]
if not drawer_visuals then return stack end
local leftover = stack
for _, visual in pairs(drawer_visuals) do
- leftover = visual.try_insert_stack(visual, leftover, true)
+ leftover = visual:try_insert_stack(leftover, true)
end
return leftover
end
@@ -144,6 +197,9 @@ function drawers.register_drawer(name, def)
def.on_construct = drawers.drawer_on_construct
def.on_destruct = drawers.drawer_on_destruct
def.on_dig = drawers.drawer_on_dig
+ def.allow_metadata_inventory_put = drawers.drawer_allow_metadata_inventory_put
+ def.on_metadata_inventory_put = drawers.add_drawer_upgrade
+ def.on_metadata_inventory_take = drawers.remove_drawer_upgrade
if minetest.get_modpath("screwdriver") and screwdriver then
def.on_rotate = def.on_rotate or screwdriver.disallow
@@ -231,3 +287,26 @@ function drawers.register_drawer(name, def)
end
end
end
+
+function drawers.register_drawer_upgrade(name, def)
+ def.groups = def.groups or {}
+ def.groups.drawer_upgrade = def.groups.drawer_upgrade or 100
+ def.inventory_image = def.inventory_image or "drawers_upgrade_template.png"
+ def.stack_max = 1
+
+ local recipe_item = def.recipe_item or "air"
+ def.recipe_item = nil
+
+ core.register_craftitem(name, def)
+
+ if not def.no_craft then
+ core.register_craft({
+ output = name,
+ recipe = {
+ {recipe_item, "group:stick", recipe_item},
+ {"group:stick", "drawers:upgrade_template", "group:stick"},
+ {recipe_item, "group:stick", recipe_item}
+ }
+ })
+ end
+end
diff --git a/lua/helpers.lua b/lua/helpers.lua
index 89785a8..04ea38d 100755
--- a/lua/helpers.lua
+++ b/lua/helpers.lua
@@ -29,6 +29,15 @@ SOFTWARE.
local MP = core.get_modpath(core.get_current_modname())
local S, NS = dofile(MP.."/intllib.lua")
+-- GUI
+function drawers.get_upgrade_slots_bg(x,y)
+ local out = ""
+ for i = 0, 4, 1 do
+ out = out .."image["..x+i..","..y..";1,1;drawers_upgrade_slot_bg.png]"
+ end
+ return out
+end
+
function drawers.gen_info_text(basename, count, factor, stack_max)
local maxCount = stack_max * factor
local percent = count / maxCount * 100
@@ -199,6 +208,41 @@ function drawers.remove_visuals(pos)
end
end
+function drawers.update_drawer_upgrades(pos)
+ local node = core.get_node(pos)
+ local ndef = core.registered_nodes[node.name]
+ local drawerType = ndef.groups.drawer
+
+ -- default number of slots/stacks
+ local stackMaxFactor = ndef.drawer_stack_max_factor
+
+ -- storage percent with all upgrades
+ local storagePercent = 100
+
+ -- get info of all upgrades
+ local inventory = core.get_meta(pos):get_inventory():get_list("upgrades")
+ for _,itemStack in pairs(inventory) do
+ local iname = itemStack:get_name()
+ local idef = core.registered_items[iname]
+ local addPercent = idef.groups.drawer_upgrade or 0
+
+ storagePercent = storagePercent + addPercent
+ end
+
+ -- i.e.: 150% / 100 => 1.50
+ stackMaxFactor = math.floor(stackMaxFactor * (storagePercent / 100))
+ -- calculate stack_max factor for a single drawer
+ stackMaxFactor = stackMaxFactor / drawerType
+
+ -- set the new stack max factor in all visuals
+ local drawer_visuals = drawers.drawer_visuals[core.serialize(pos)]
+ if not drawer_visuals then return end
+
+ for _,visual in pairs(drawer_visuals) do
+ visual:setStackMaxFactor(stackMaxFactor)
+ end
+end
+
function drawers.randomize_pos(pos)
local rndpos = table.copy(pos)
local x = math.random(-50, 50) * 0.01
diff --git a/lua/visual.lua b/lua/visual.lua
index 5098a63..3287f65 100755
--- a/lua/visual.lua
+++ b/lua/visual.lua
@@ -90,10 +90,11 @@ core.register_entity("drawers:visual", {
drawers.drawer_visuals[posstr][vId] = self
end
-
- local node = core.get_node(self.drawer_pos)
+ -- get meta
+ self.meta = core.get_meta(self.drawer_pos)
-- collisionbox
+ local node = core.get_node(self.drawer_pos)
local colbox
if self.drawerType ~= 2 then
if node.param2 == 1 or node.param2 == 3 then
@@ -123,17 +124,16 @@ core.register_entity("drawers:visual", {
-- drawer values
- local meta = core.get_meta(self.drawer_pos)
local vid = self.visualId
- self.count = meta:get_int("count"..vid)
- self.itemName = meta:get_string("name"..vid)
- self.maxCount = meta:get_int("max_count"..vid)
- self.itemStackMax = meta:get_int("base_stack_max"..vid)
- self.stackMaxFactor = meta:get_int("stack_max_factor"..vid)
+ self.count = self.meta:get_int("count"..vid)
+ self.itemName = self.meta:get_string("name"..vid)
+ self.maxCount = self.meta:get_int("max_count"..vid)
+ self.itemStackMax = self.meta:get_int("base_stack_max"..vid)
+ self.stackMaxFactor = self.meta:get_int("stack_max_factor"..vid)
-- infotext
- local infotext = meta:get_string("entity_infotext"..vid) .. "\n\n\n\n\n"
+ local infotext = self.meta:get_string("entity_infotext"..vid) .. "\n\n\n\n\n"
self.object:set_properties({
collisionbox = colbox,
@@ -200,35 +200,10 @@ core.register_entity("drawers:visual", {
-- update the drawer count
self.count = self.count - removeCount
- -- clean up drawer, if empty
- if self.count <= 0 then
- self.itemName = ""
- meta:set_string("name"..self.visualId, self.itemName)
- self.texture = "blank.png"
- end
-
-
- -- build info
- local itemDescription = ""
- if self.count <= 0 then
- itemDescription = S("Empty")
- elseif core.registered_items[self.itemName] then
- itemDescription = core.registered_items[self.itemName].description
- end
-
- local infotext = drawers.gen_info_text(itemDescription,
- self.count, self.stackMaxFactor, self.itemStackMax)
-
- -- set new infotext and texture
- self.object:set_properties({
- infotext = infotext .. "\n\n\n\n\n",
- textures = {self.texture}
- })
-
- -- save everything to meta
- meta:set_string("entity_infotext"..self.visualId, infotext)
- meta:set_int("count"..self.visualId, self.count)
+ self:updateInfotext()
+ self:updateTexture()
+ self:saveMetaData()
-- return the stack that was removed from the drawer
return stack
@@ -277,32 +252,93 @@ core.register_entity("drawers:visual", {
itemstack:set_count(itemstack:get_count() - stackCount)
end
- -- get meta
- local meta = core.get_meta(self.drawer_pos)
+ -- update infotext, texture
+ self:updateInfotext()
+ self:updateTexture()
+
+ self:saveMetaData()
- -- update infotext
- local itemDescription
+ if itemstack:get_count() == 0 then itemstack = ItemStack("") end
+ return itemstack
+ end,
+
+ updateInfotext = function(self)
+ local itemDescription = ""
if core.registered_items[self.itemName] then
itemDescription = core.registered_items[self.itemName].description
- else
+ end
+
+ if self.count <= 0 then
+ self.itemName = ""
+ self.meta:set_string("name"..self.visualId, self.itemName)
+ self.texture = "blank.png"
itemDescription = S("Empty")
end
+
local infotext = drawers.gen_info_text(itemDescription,
self.count, self.stackMaxFactor, self.itemStackMax)
- meta:set_string("entity_infotext"..self.visualId, infotext)
+ self.meta:set_string("entity_infotext"..self.visualId, infotext)
+ self.object:set_properties({
+ infotext = infotext .. "\n\n\n\n\n"
+ })
+ end,
+
+ updateTexture = function(self)
-- texture
self.texture = drawers.get_inv_image(self.itemName)
self.object:set_properties({
- infotext = infotext .. "\n\n\n\n\n",
textures = {self.texture}
})
+ end,
- self.saveMetaData(self, meta)
+ dropStack = function(self, itemStack)
+ -- print warning if dropping higher stack counts than allowed
+ if itemStack:get_count() > itemStack:get_stack_max() then
+ core.log("warning", "[drawers] Dropping item stack with higher count than allowed")
+ end
+ -- find a position containing air
+ local dropPos = core.find_node_near(self.drawer_pos, 1, {"air"}, false)
+ -- if no pos found then drop on the top of the drawer
+ if not dropPos then
+ dropPos = self.pos
+ dropPos.y = dropPos.y + 1
+ end
+ -- drop the item stack
+ core.item_drop(itemStack, nil, dropPos)
+ end,
- if itemstack:get_count() == 0 then itemstack = ItemStack("") end
- return itemstack
+ dropItemOverload = function(self)
+ -- drop stacks until there are no more items than allowed
+ while self.count > self.maxCount do
+ -- remove the overflow
+ local removeCount = self.count - self.maxCount
+ -- if this is too much for a single stack, only take the
+ -- stack limit
+ if removeCount > self.itemStackMax then
+ removeCount = self.itemStackMax
+ end
+ -- remove this count from the drawer
+ self.count = self.count - removeCount
+ -- create a new item stack having the size of the remove
+ -- count
+ local stack = ItemStack(self.itemName)
+ stack:set_count(removeCount)
+ print(stack:to_string())
+ -- drop the stack
+ self:dropStack(stack)
+ end
+ end,
+
+ setStackMaxFactor = function(self, stackMaxFactor)
+ self.stackMaxFactor = stackMaxFactor
+ self.maxCount = self.stackMaxFactor * self.itemStackMax
+
+ -- will drop possible overflowing items
+ self:dropItemOverload()
+ self:updateInfotext()
+ self:saveMetaData()
end,
play_interact_sound = function(self)
@@ -314,11 +350,11 @@ core.register_entity("drawers:visual", {
end,
saveMetaData = function(self, meta)
- meta:set_int("count"..self.visualId, self.count)
- meta:set_string("name"..self.visualId, self.itemName)
- meta:set_int("max_count"..self.visualId, self.maxCount)
- meta:set_int("base_stack_max"..self.visualId, self.itemStackMax)
- meta:set_int("stack_max_factor"..self.visualId, self.stackMaxFactor)
+ self.meta:set_int("count"..self.visualId, self.count)
+ self.meta:set_string("name"..self.visualId, self.itemName)
+ self.meta:set_int("max_count"..self.visualId, self.maxCount)
+ self.meta:set_int("base_stack_max"..self.visualId, self.itemStackMax)
+ self.meta:set_int("stack_max_factor"..self.visualId, self.stackMaxFactor)
end
})