techage/liquids/silo.lua

359 lines
11 KiB
Lua
Raw Permalink Normal View History

--[[
TechAge
=======
Copyright (C) 2019-2024 Joachim Stolberg
2020-10-19 20:09:17 +03:00
AGPL v3
See LICENSE.txt for more information
2022-01-03 23:40:31 +03:00
TA3/TA4 Powder Silo
]]--
local S2P = minetest.string_to_pos
local P2S = minetest.pos_to_string
local M = minetest.get_meta
local S = techage.S
local LQD = function(pos) return (minetest.registered_nodes[techage.get_node_lvm(pos).name] or {}).liquid end
local Pipe = techage.LiquidPipe
2021-06-09 23:09:59 +03:00
local liquid = networks.liquid
local INV_SIZE = 8
2020-10-06 22:15:56 +03:00
local STACKMAX = 99
local function allow_metadata_inventory_put(pos, listname, index, stack, player)
if minetest.is_protected(pos, player:get_player_name()) then
return 0
end
2020-08-30 20:16:32 +03:00
-- check if it is powder or techage liquid item (migration function)
2022-01-03 23:40:31 +03:00
local ndef = minetest.registered_craftitems[stack:get_name()] or
2020-08-30 20:16:32 +03:00
minetest.registered_items[stack:get_name()] or {}
if ndef.groups and (ndef.groups.powder == 1 or ndef.groups.ta_liquid == 1) then
2020-07-10 21:18:10 +03:00
local nvm = techage.get_nvm(pos)
nvm.item_name = nil
2020-10-06 22:15:56 +03:00
nvm.item_count = nil
2020-07-10 21:18:10 +03:00
local inv = minetest.get_meta(pos):get_inventory()
if inv:is_empty(listname) then
return stack:get_count()
end
if inv:contains_item(listname, ItemStack(stack:get_name())) then
return stack:get_count()
end
end
return 0
end
local function allow_metadata_inventory_take(pos, listname, index, stack, player)
if minetest.is_protected(pos, player:get_player_name()) then
return 0
end
2020-01-31 21:55:10 +03:00
local nvm = techage.get_nvm(pos)
nvm.item_name = nil
2020-10-06 22:15:56 +03:00
nvm.item_count = nil
return stack:get_count()
end
local function can_dig(pos, player)
if minetest.is_protected(pos, player:get_player_name()) then
return false
end
local inv = minetest.get_meta(pos):get_inventory()
return inv:is_empty("main")
end
2020-01-31 21:55:10 +03:00
local function get_item_name(nvm, inv)
for idx = 1, inv:get_size("main") do
local stack = inv:get_stack("main", idx)
if stack:get_count() > 0 then
2020-01-31 21:55:10 +03:00
nvm.item_name = stack:get_name()
return nvm.item_name
end
end
2022-01-03 23:40:31 +03:00
end
2020-10-06 22:15:56 +03:00
local function get_item_count(pos)
local inv = M(pos):get_inventory()
local count = 0
for idx = 1, inv:get_size("main") do
local stack = inv:get_stack("main", idx)
count = count + stack:get_count()
end
return count
2022-01-03 23:40:31 +03:00
end
2020-10-06 22:15:56 +03:00
local function get_silo_capa(pos)
local inv = M(pos):get_inventory()
for idx = 1, inv:get_size("main") do
local stack = inv:get_stack("main", idx)
if stack:get_count() > 0 then
return inv:get_size("main") * stack:get_stack_max()
end
end
return inv:get_size("main") * STACKMAX
2022-01-03 23:40:31 +03:00
end
2020-10-06 22:15:56 +03:00
local function formspec3()
return "size[8,5]"..
default.gui_bg..
default.gui_bg_img..
default.gui_slots..
"list[context;main;0,0;8,1;]"..
"list[current_player;main;0,1.3;8,4;]"..
"listring[context;main]"..
"listring[current_player;main]"
end
local function formspec4()
return "size[8,6]"..
default.gui_bg..
default.gui_bg_img..
default.gui_slots..
"list[context;main;0,0;8,2;]"..
"list[current_player;main;0,2.3;8,4;]"..
"listring[context;main]"..
"listring[current_player;main]"
end
2019-11-18 22:29:41 +03:00
local tLiquid = {
capa = 0,
peek = function(pos, indir)
2020-01-31 21:55:10 +03:00
local nvm = techage.get_nvm(pos)
2019-11-18 22:29:41 +03:00
local inv = M(pos):get_inventory()
if not inv:is_empty("main") then
2020-01-31 21:55:10 +03:00
return nvm.item_name or get_item_name(nvm, inv)
2019-11-18 22:29:41 +03:00
end
end,
put = function(pos, indir, name, amount)
-- check if it is powder
2020-10-06 22:15:56 +03:00
local nvm = techage.get_nvm(pos)
local ndef = minetest.registered_craftitems[name] or minetest.registered_nodes[name] or {}
if ndef.groups and ndef.groups.powder == 1 then
local inv = M(pos):get_inventory()
local stack = ItemStack(name.." "..amount)
if inv:room_for_item("main", stack) then
2020-10-06 22:15:56 +03:00
nvm.item_count = nvm.item_count or get_item_count(pos)
inv:add_item("main", stack)
2020-10-06 22:15:56 +03:00
nvm.item_count = nvm.item_count + stack:get_count()
return 0
end
2019-11-18 22:29:41 +03:00
end
return amount
end,
take = function(pos, indir, name, amount)
2020-01-31 21:55:10 +03:00
local nvm = techage.get_nvm(pos)
2019-11-18 22:29:41 +03:00
local inv = M(pos):get_inventory()
if not name then
2020-01-31 21:55:10 +03:00
name = nvm.item_name or get_item_name(nvm, inv)
2019-11-18 22:29:41 +03:00
end
if name then
local stack = ItemStack(name.." "..amount)
2020-10-06 22:15:56 +03:00
nvm.item_count = nvm.item_count or get_item_count(pos)
local count = inv:remove_item("main", stack):get_count()
nvm.item_count = nvm.item_count - count
return count, name
2019-11-18 22:29:41 +03:00
end
return 0
end,
untake = function(pos, indir, name, amount)
2020-10-06 22:15:56 +03:00
local nvm = techage.get_nvm(pos)
local inv = M(pos):get_inventory()
local stack = ItemStack(name.." "..amount)
if inv:room_for_item("main", stack) then
2020-10-06 22:15:56 +03:00
nvm.item_count = nvm.item_count or get_item_count(pos)
inv:add_item("main", stack)
2020-10-06 22:15:56 +03:00
nvm.item_count = nvm.item_count + stack:get_count()
return 0
end
return amount
end,
2019-11-18 22:29:41 +03:00
}
minetest.register_node("techage:ta3_silo", {
description = S("TA3 Silo"),
tiles = {
-- up, down, right, left, back, front
"techage_filling_ta3.png^techage_frame_ta3_top.png",
"techage_filling_ta3.png^techage_frame_ta3.png",
"techage_filling_ta3.png^techage_frame_ta3.png^techage_appl_silo.png",
"techage_filling_ta3.png^techage_frame_ta3.png^techage_appl_silo.png",
"techage_filling_ta3.png^techage_frame_ta3.png^techage_appl_silo.png",
"techage_filling_ta3.png^techage_frame_ta3.png^techage_appl_silo.png",
},
on_construct = function(pos)
local inv = M(pos):get_inventory()
inv:set_size('main', INV_SIZE)
end,
after_place_node = function(pos, placer)
local meta = M(pos)
2020-01-31 21:55:10 +03:00
local nvm = techage.get_nvm(pos)
nvm.liquid = {}
local number = techage.add_node(pos, "techage:ta3_silo")
meta:set_string("node_number", number)
meta:set_string("owner", placer:get_player_name())
2020-01-31 21:55:10 +03:00
meta:set_string("formspec", formspec3(nvm))
meta:set_string("infotext", S("TA3 Silo").." "..number)
Pipe:after_place_node(pos)
end,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
Pipe:after_dig_node(pos)
2020-06-14 00:32:00 +03:00
techage.remove_node(pos, oldnode, oldmetadata)
2020-12-21 01:58:49 +03:00
techage.del_mem(pos)
end,
can_dig = can_dig,
allow_metadata_inventory_put = allow_metadata_inventory_put,
allow_metadata_inventory_take = allow_metadata_inventory_take,
paramtype2 = "facedir",
on_rotate = screwdriver.disallow,
groups = {cracky=2},
is_ground_content = false,
sounds = default.node_sound_metal_defaults(),
})
minetest.register_node("techage:ta4_silo", {
description = S("TA4 Silo"),
tiles = {
-- up, down, right, left, back, front
"techage_filling_ta4.png^techage_frame_ta4_top.png",
"techage_filling_ta4.png^techage_frame_ta4.png",
"techage_filling_ta4.png^techage_frame_ta4.png^techage_appl_silo.png",
"techage_filling_ta4.png^techage_frame_ta4.png^techage_appl_silo.png",
"techage_filling_ta4.png^techage_frame_ta4.png^techage_appl_silo.png",
"techage_filling_ta4.png^techage_frame_ta4.png^techage_appl_silo.png",
},
on_construct = function(pos)
local inv = M(pos):get_inventory()
inv:set_size('main', INV_SIZE * 2)
end,
after_place_node = function(pos, placer)
local meta = M(pos)
2020-01-31 21:55:10 +03:00
local nvm = techage.get_nvm(pos)
nvm.liquid = {}
local number = techage.add_node(pos, "techage:ta4_silo")
meta:set_string("node_number", number)
meta:set_string("owner", placer:get_player_name())
2020-01-31 21:55:10 +03:00
meta:set_string("formspec", formspec4(nvm))
meta:set_string("infotext", S("TA4 Silo").." "..number)
Pipe:after_place_node(pos)
end,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
Pipe:after_dig_node(pos)
2020-06-14 00:32:00 +03:00
techage.remove_node(pos, oldnode, oldmetadata)
2020-12-21 01:58:49 +03:00
techage.del_mem(pos)
end,
can_dig = can_dig,
allow_metadata_inventory_put = allow_metadata_inventory_put,
allow_metadata_inventory_take = allow_metadata_inventory_take,
paramtype2 = "facedir",
on_rotate = screwdriver.disallow,
groups = {cracky=2},
is_ground_content = false,
sounds = default.node_sound_metal_defaults(),
})
techage.register_node({"techage:ta3_silo", "techage:ta4_silo"}, {
on_pull_item = function(pos, in_dir, num)
local inv = M(pos):get_inventory()
if not inv:is_empty("main") then
2022-01-03 23:40:31 +03:00
local taken = techage.get_items(pos, inv, "main", num)
2020-12-21 01:58:49 +03:00
local nvm = techage.get_nvm(pos)
nvm.item_count = nvm.item_count or get_item_count(pos)
nvm.item_count = nvm.item_count - taken:get_count()
return taken
end
end,
on_push_item = function(pos, in_dir, stack)
2020-07-10 21:18:10 +03:00
-- check if it is powder
local name = stack:get_name()
local ndef = minetest.registered_craftitems[name] or {}
if ndef.groups and ndef.groups.powder == 1 then
local inv = M(pos):get_inventory()
2022-01-03 23:40:31 +03:00
2020-07-10 21:18:10 +03:00
if inv:is_empty("main") then
inv:add_item("main", stack)
2020-12-21 01:58:49 +03:00
local nvm = techage.get_nvm(pos)
nvm.item_count = nvm.item_count or get_item_count(pos)
nvm.item_count = nvm.item_count + stack:get_count()
2020-07-10 21:18:10 +03:00
return true
end
2022-01-03 23:40:31 +03:00
2020-07-10 21:18:10 +03:00
if inv:contains_item("main", name) and inv:room_for_item("main", stack) then
inv:add_item("main", stack)
2020-12-21 01:58:49 +03:00
local nvm = techage.get_nvm(pos)
nvm.item_count = nvm.item_count or get_item_count(pos)
nvm.item_count = nvm.item_count + stack:get_count()
2020-07-10 21:18:10 +03:00
return true
end
end
return false
end,
on_unpull_item = function(pos, in_dir, stack)
local meta = M(pos)
local inv = meta:get_inventory()
2020-12-21 01:58:49 +03:00
local nvm = techage.get_nvm(pos)
nvm.item_count = nvm.item_count or get_item_count(pos)
nvm.item_count = nvm.item_count + stack:get_count()
return techage.put_items(inv, "main", stack)
end,
on_recv_message = function(pos, src, topic, payload)
if topic == "state" then
local meta = M(pos)
local inv = meta:get_inventory()
return techage.get_inv_state(inv, "main")
2020-10-06 22:15:56 +03:00
elseif topic == "load" then
local inv = M(pos):get_inventory()
local nvm = techage.get_nvm(pos)
nvm.item_count = nvm.item_count or get_item_count(pos)
nvm.capa = nvm.capa or get_silo_capa(pos)
return techage.power.percent(nvm.capa, nvm.item_count), nvm.item_count
else
return "unsupported"
end
end,
2022-06-05 22:20:43 +03:00
on_beduino_request_data = function(pos, src, topic, payload)
2022-06-09 13:59:17 +03:00
if topic == 131 then -- Chest State
2022-06-05 22:20:43 +03:00
local meta = M(pos)
local inv = meta:get_inventory()
2022-06-09 13:59:17 +03:00
return 0, {techage.get_inv_state_num(inv, "main")}
2022-06-05 22:20:43 +03:00
elseif topic == 134 then
local inv = M(pos):get_inventory()
local nvm = techage.get_nvm(pos)
nvm.item_count = nvm.item_count or get_item_count(pos)
nvm.capa = nvm.capa or get_silo_capa(pos)
if payload[1] == 1 then
return 0, {techage.power.percent(nvm.capa, nvm.item_count)}
else
return 0, {nvm.item_count}
end
else
return 2, ""
end
end,
2020-12-21 01:58:49 +03:00
on_node_load = function(pos)
local nvm = techage.get_nvm(pos)
nvm.item_count = nil
end,
2022-01-03 23:40:31 +03:00
})
2021-06-09 23:09:59 +03:00
liquid.register_nodes({"techage:ta3_silo", "techage:ta4_silo"}, Pipe, "tank", nil, tLiquid)
minetest.register_craft({
output = "techage:ta3_silo",
recipe = {
{"", "", ""},
{"techage:tubeS", "techage:chest_ta3", "techage:ta3_pipeS"},
{"", "", ""},
},
})
minetest.register_craft({
output = "techage:ta4_silo",
recipe = {
{"default:tin_ingot", "dye:blue", "default:steel_ingot"},
{"", "techage:ta3_silo", ""},
{"", "", ""},
},
})