2017-04-01 16:28:03 +02:00
|
|
|
--[[
|
|
|
|
Minetest Mod Storage Drawers - A Mod adding storage drawers
|
|
|
|
|
|
|
|
Copyright (C) 2017 LNJ <git@lnj.li>
|
|
|
|
Copyright (C) 2016 Mango Tango <mtango688@gmail.com>
|
|
|
|
|
|
|
|
MIT License
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
SOFTWARE.
|
|
|
|
]]
|
|
|
|
|
2017-04-11 13:02:31 +02:00
|
|
|
-- Load support for intllib.
|
|
|
|
local MP = core.get_modpath(core.get_current_modname())
|
|
|
|
local S, NS = dofile(MP.."/intllib.lua")
|
|
|
|
|
2017-04-17 16:04:16 +02:00
|
|
|
-- 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
|
|
|
|
|
2017-04-01 16:28:03 +02:00
|
|
|
function drawers.gen_info_text(basename, count, factor, stack_max)
|
2017-04-01 20:24:32 +02:00
|
|
|
local maxCount = stack_max * factor
|
|
|
|
local percent = count / maxCount * 100
|
|
|
|
-- round the number (float -> int)
|
|
|
|
percent = math.floor(percent + 0.5)
|
|
|
|
|
2017-04-01 22:30:07 +02:00
|
|
|
if count == 0 then
|
2017-04-11 13:02:31 +02:00
|
|
|
return S("@1 (@2% full)", basename, tostring(percent))
|
2017-04-01 22:30:07 +02:00
|
|
|
else
|
2017-04-11 13:02:31 +02:00
|
|
|
return S("@1 @2 (@3% full)", tostring(count), basename, tostring(percent))
|
2017-04-01 22:30:07 +02:00
|
|
|
end
|
2017-04-01 16:28:03 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function drawers.get_inv_image(name)
|
2017-04-10 17:42:51 +02:00
|
|
|
local texture = "blank.png"
|
2017-04-01 16:28:03 +02:00
|
|
|
local def = core.registered_items[name]
|
2017-04-14 18:59:42 +02:00
|
|
|
if not def then return end
|
|
|
|
|
|
|
|
if def.inventory_image and #def.inventory_image > 0 then
|
|
|
|
texture = def.inventory_image
|
|
|
|
else
|
|
|
|
if not def.tiles then return texture end
|
|
|
|
local tiles = table.copy(def.tiles)
|
|
|
|
|
|
|
|
for k,v in pairs(tiles) do
|
|
|
|
if type(v) == "table" then
|
|
|
|
tiles[k] = v.name
|
2017-04-01 16:28:03 +02:00
|
|
|
end
|
2017-04-14 18:59:42 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- tiles: up, down, right, left, back, front
|
|
|
|
-- inventorycube: up, front, right
|
|
|
|
if #tiles <= 2 then
|
|
|
|
texture = core.inventorycube(tiles[1], tiles[1], tiles[1])
|
|
|
|
elseif #tiles <= 5 then
|
|
|
|
texture = core.inventorycube(tiles[1], tiles[3], tiles[3])
|
|
|
|
else -- full tileset
|
|
|
|
texture = core.inventorycube(tiles[1], tiles[6], tiles[3])
|
2017-04-01 16:28:03 +02:00
|
|
|
end
|
|
|
|
end
|
2017-04-14 18:59:42 +02:00
|
|
|
|
2017-04-01 16:28:03 +02:00
|
|
|
return texture
|
|
|
|
end
|
|
|
|
|
2017-04-05 12:01:23 +02:00
|
|
|
function drawers.spawn_visuals(pos)
|
2017-04-01 16:28:03 +02:00
|
|
|
local node = core.get_node(pos)
|
2017-04-02 21:29:32 +02:00
|
|
|
local ndef = core.registered_nodes[node.name]
|
|
|
|
local drawerType = ndef.groups.drawer
|
2017-04-01 16:28:03 +02:00
|
|
|
|
|
|
|
-- data for the new visual
|
|
|
|
drawers.last_drawer_pos = pos
|
2017-04-02 21:29:32 +02:00
|
|
|
drawers.last_drawer_type = drawerType
|
2017-04-01 16:28:03 +02:00
|
|
|
|
2017-04-02 21:29:32 +02:00
|
|
|
if drawerType == 1 then -- 1x1 drawer
|
|
|
|
drawers.last_visual_id = ""
|
|
|
|
drawers.last_texture = drawers.get_inv_image(core.get_meta(pos):get_string("name"))
|
2017-04-01 16:28:03 +02:00
|
|
|
|
2017-04-02 21:29:32 +02:00
|
|
|
local bdir = core.facedir_to_dir(node.param2)
|
|
|
|
local fdir = vector.new(-bdir.x, 0, -bdir.z)
|
|
|
|
local pos2 = vector.add(pos, vector.multiply(fdir, 0.438))
|
2017-04-01 16:28:03 +02:00
|
|
|
|
2017-04-09 23:31:33 +02:00
|
|
|
local obj = core.add_entity(pos2, "drawers:visual")
|
2017-04-01 16:28:03 +02:00
|
|
|
|
2017-04-02 21:29:32 +02:00
|
|
|
if bdir.x < 0 then obj:setyaw(0.5 * math.pi) end
|
|
|
|
if bdir.z < 0 then obj:setyaw(math.pi) end
|
|
|
|
if bdir.x > 0 then obj:setyaw(1.5 * math.pi) end
|
|
|
|
|
|
|
|
drawers.last_texture = nil
|
2017-04-05 14:52:34 +02:00
|
|
|
elseif drawerType == 2 then
|
|
|
|
local bdir = core.facedir_to_dir(node.param2)
|
|
|
|
|
|
|
|
local fdir1
|
|
|
|
local fdir2
|
|
|
|
if node.param2 == 2 or node.param2 == 0 then
|
|
|
|
fdir1 = vector.new(-bdir.x, 0.5, -bdir.z)
|
|
|
|
fdir2 = vector.new(-bdir.x, -0.5, -bdir.z)
|
|
|
|
else
|
|
|
|
fdir1 = vector.new(-bdir.x, 0.5, -bdir.z)
|
|
|
|
fdir2 = vector.new(-bdir.x, -0.5, -bdir.z)
|
|
|
|
end
|
|
|
|
|
2017-04-09 23:31:33 +02:00
|
|
|
local objs = {}
|
2017-04-05 14:52:34 +02:00
|
|
|
|
|
|
|
drawers.last_visual_id = 1
|
|
|
|
drawers.last_texture = drawers.get_inv_image(core.get_meta(pos):get_string("name1"))
|
|
|
|
local pos1 = vector.add(pos, vector.multiply(fdir1, 0.438))
|
|
|
|
objs[1] = core.add_entity(pos1, "drawers:visual")
|
|
|
|
|
|
|
|
drawers.last_visual_id = 2
|
|
|
|
drawers.last_texture = drawers.get_inv_image(core.get_meta(pos):get_string("name2"))
|
|
|
|
local pos2 = vector.add(pos, vector.multiply(fdir2, 0.438))
|
|
|
|
objs[2] = core.add_entity(pos2, "drawers:visual")
|
|
|
|
|
|
|
|
for i,obj in pairs(objs) do
|
|
|
|
if bdir.x < 0 then obj:setyaw(0.5 * math.pi) end
|
|
|
|
if bdir.z < 0 then obj:setyaw(math.pi) end
|
|
|
|
if bdir.x > 0 then obj:setyaw(1.5 * math.pi) end
|
|
|
|
end
|
2017-04-02 21:29:32 +02:00
|
|
|
else -- 2x2 drawer
|
|
|
|
local bdir = core.facedir_to_dir(node.param2)
|
|
|
|
|
|
|
|
local fdir1
|
|
|
|
local fdir2
|
|
|
|
local fdir3
|
|
|
|
local fdir4
|
|
|
|
if node.param2 == 2 then
|
|
|
|
fdir1 = vector.new(-bdir.x + 0.5, 0.5, -bdir.z)
|
|
|
|
fdir2 = vector.new(-bdir.x - 0.5, 0.5, -bdir.z)
|
|
|
|
fdir3 = vector.new(-bdir.x + 0.5, -0.5, -bdir.z)
|
|
|
|
fdir4 = vector.new(-bdir.x - 0.5, -0.5, -bdir.z)
|
|
|
|
elseif node.param2 == 0 then
|
|
|
|
fdir1 = vector.new(-bdir.x - 0.5, 0.5, -bdir.z)
|
|
|
|
fdir2 = vector.new(-bdir.x + 0.5, 0.5, -bdir.z)
|
|
|
|
fdir3 = vector.new(-bdir.x - 0.5, -0.5, -bdir.z)
|
|
|
|
fdir4 = vector.new(-bdir.x + 0.5, -0.5, -bdir.z)
|
|
|
|
elseif node.param2 == 1 then
|
|
|
|
fdir1 = vector.new(-bdir.x, 0.5, -bdir.z + 0.5)
|
|
|
|
fdir2 = vector.new(-bdir.x, 0.5, -bdir.z - 0.5)
|
|
|
|
fdir3 = vector.new(-bdir.x, -0.5, -bdir.z + 0.5)
|
|
|
|
fdir4 = vector.new(-bdir.x, -0.5, -bdir.z - 0.5)
|
|
|
|
else
|
|
|
|
fdir1 = vector.new(-bdir.x, 0.5, -bdir.z - 0.5)
|
|
|
|
fdir2 = vector.new(-bdir.x, 0.5, -bdir.z + 0.5)
|
|
|
|
fdir3 = vector.new(-bdir.x, -0.5, -bdir.z - 0.5)
|
|
|
|
fdir4 = vector.new(-bdir.x, -0.5, -bdir.z + 0.5)
|
|
|
|
end
|
|
|
|
|
2017-04-09 23:31:33 +02:00
|
|
|
local objs = {}
|
2017-04-02 21:29:32 +02:00
|
|
|
|
|
|
|
drawers.last_visual_id = 1
|
|
|
|
drawers.last_texture = drawers.get_inv_image(core.get_meta(pos):get_string("name1"))
|
|
|
|
local pos1 = vector.add(pos, vector.multiply(fdir1, 0.438))
|
|
|
|
objs[1] = core.add_entity(pos1, "drawers:visual")
|
|
|
|
|
|
|
|
drawers.last_visual_id = 2
|
|
|
|
drawers.last_texture = drawers.get_inv_image(core.get_meta(pos):get_string("name2"))
|
|
|
|
local pos2 = vector.add(pos, vector.multiply(fdir2, 0.438))
|
|
|
|
objs[2] = core.add_entity(pos2, "drawers:visual")
|
|
|
|
|
|
|
|
drawers.last_visual_id = 3
|
|
|
|
drawers.last_texture = drawers.get_inv_image(core.get_meta(pos):get_string("name3"))
|
|
|
|
local pos3 = vector.add(pos, vector.multiply(fdir3, 0.438))
|
|
|
|
objs[3] = core.add_entity(pos3, "drawers:visual")
|
|
|
|
|
|
|
|
drawers.last_visual_id = 4
|
|
|
|
drawers.last_texture = drawers.get_inv_image(core.get_meta(pos):get_string("name4"))
|
|
|
|
local pos4 = vector.add(pos, vector.multiply(fdir4, 0.438))
|
|
|
|
objs[4] = core.add_entity(pos4, "drawers:visual")
|
|
|
|
|
|
|
|
|
|
|
|
for i,obj in pairs(objs) do
|
|
|
|
if bdir.x < 0 then obj:setyaw(0.5 * math.pi) end
|
|
|
|
if bdir.z < 0 then obj:setyaw(math.pi) end
|
|
|
|
if bdir.x > 0 then obj:setyaw(1.5 * math.pi) end
|
|
|
|
end
|
|
|
|
end
|
2017-04-01 16:28:03 +02:00
|
|
|
end
|
2017-04-03 13:00:40 +02:00
|
|
|
|
2017-04-05 12:01:23 +02:00
|
|
|
function drawers.remove_visuals(pos)
|
|
|
|
local objs = core.get_objects_inside_radius(pos, 0.537)
|
|
|
|
if not objs then return end
|
|
|
|
|
|
|
|
for _, obj in pairs(objs) do
|
|
|
|
if obj and obj:get_luaentity() and
|
|
|
|
obj:get_luaentity().name == "drawers:visual" then
|
|
|
|
obj:remove()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-17 16:04:16 +02:00
|
|
|
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
|
|
|
|
|
2017-04-03 13:00:40 +02:00
|
|
|
function drawers.randomize_pos(pos)
|
|
|
|
local rndpos = table.copy(pos)
|
|
|
|
local x = math.random(-50, 50) * 0.01
|
|
|
|
local z = math.random(-50, 50) * 0.01
|
|
|
|
rndpos.x = rndpos.x + x
|
|
|
|
rndpos.y = rndpos.y + 0.25
|
|
|
|
rndpos.z = rndpos.z + z
|
|
|
|
return rndpos
|
|
|
|
end
|
2017-04-10 16:40:52 +02:00
|
|
|
|
|
|
|
function drawers.node_tiles_front_other(front, other)
|
|
|
|
return {other, other, other, other, other, front}
|
|
|
|
end
|