red_glazed_terracotta/mods/rgt_inv/init.lua

95 lines
3 KiB
Lua

rgt_inv = {}
local ns = rgt_inv
Inventory = setmetatable({
new = function(p)
local e = setmetatable({
player = p,
state = {
proximate_machines = {}
}
}, {
__index = Inventory,
-- Setting a value on the Inventory instance directly will
-- automatically update the player's inventory formspec.
-- `inv.state` should be used in cases where this is not desirable.
__newindex = function(e, k, v)
e.state[k] = v
e:rebuild()
end
})
e:rebuild()
return e
end,
rebuild = function(e)
local fs = {"\
formspec_version[10]\
size[12,10]\
style_type[button,image_button;border=false]\
"}
for x = 0, 7 do
for y = 0, 3 do
fs[#fs +1] = "\
image["..(x *1.25 +1.125 -0.0625)..","..(y *1.25 +4.5 -0.0625)..";1.14,1.14;rgt_other_button_bg.png;8,8]\
"
end
end
fs[#fs +1] = "\
style_type[image_button;noclip=true;bgimg=rgt_button_bg.png;bgimg_middle=8,8]\
"
local i = 0
for _, x in ipairs(e.state.proximate_machines) do
local y = i > 11 and 10.5 or -1
fs[#fs +1] = "image_button["..(i %11 +0.125)..","..y..";0.75,0.75;rgt_stone.png;blah;]"
i = i +1
end
fs[#fs +1] = "\
list[current_player;main;1.125,4.5;8,4;]\
list[current_player;craft;3,0.5;3,3;]\
listring[]\
list[current_player;craftpreview;7,1;1,1;]\
"
e.player:set_inventory_formspec(table.concat(fs))
end,
on_action = function(e, data)
end
}, {
__call = function(_, ...)
return Inventory.new(...)
end
})
local last_time = 0
minetest.register_globalstep(function()
local time = minetest.get_us_time()
-- Scan for machines every second.
if time -last_time > 1000000 then
for name, m in pairs(rgt.players) do
local pm = {}
local machines = minetest.find_nodes_in_area(m.pos:offset(-7, -7, -7), m.pos:offset(7, 7, 7), "group:rgt_machine", true)
for type, positions in pairs(machines) do
pm[#pm +1] = {
type = type,
pos = positions[math.random(1, #positions)]
}
end
if not (#pm <= 0 and #m.inv.state.proximate_machines <= 0) then
-- Give the machines list a predictable order by sorting it alphabetically prior to submission.
table.sort(pm, function(a, b) return a.type < b.type end)
m.inv.proximate_machines = pm
end
end
last_time = time
end
end)
minetest.register_chatcommand("/lua", {
privs = {server = true},
func = function(name, args)
xpcall(function()
loadstring(args)()
end, say)
end
})