ExchangeClone/deconstructor.lua

197 lines
6.9 KiB
Lua
Raw Normal View History

2023-03-26 16:43:44 +02:00
--Renamed "fuel" inventory to "main" to work with hoppers
2021-08-25 18:01:36 +02:00
function get_element_deconstructor_formspec()
2023-03-25 23:29:02 +01:00
if not exchangeclone.mineclone then
2023-03-25 23:37:15 +01:00
local formspec = {
"size[8,9]",
"label[2,1;Fuel]",
2023-03-26 16:43:44 +02:00
"list[context;main;2,2;1,1;]",
"label[5,1;Orb]",
"list[context;dst;5,2;1,1;]",
2023-03-25 23:37:15 +01:00
"list[current_player;main;0,5;8,4;]",
"listring[current_player;main]",
2023-03-26 16:43:44 +02:00
"listring[context;main]",
"listring[current_player;main]",
2023-03-27 01:14:32 +02:00
"listring[context;dst]",
2023-03-25 23:37:15 +01:00
}
return table.concat(formspec, "")
else
2023-03-25 23:37:15 +01:00
local formspec = {
"size[9,10]",
2023-03-25 23:37:15 +01:00
"label[2,1;Fuel]",
2023-03-26 16:43:44 +02:00
"list[context;main;2,2;1,1;]",
2023-03-25 23:37:15 +01:00
mcl_formspec.get_itemslot_bg(2,2,1,1),
"label[5,1;Orb]",
"list[context;dst;5,2;1,1;]",
mcl_formspec.get_itemslot_bg(5,2,1,1),
2023-03-26 00:03:03 +01:00
"list[current_player;main;0,5;9,3;9]",
mcl_formspec.get_itemslot_bg(0,5,9,3),
"list[current_player;main;0,8.5;9,1;]",
mcl_formspec.get_itemslot_bg(0,8.5,9,1),
"listring[current_player;main]",
2023-03-26 16:43:44 +02:00
"listring[context;main]",
"listring[current_player;main]",
"listring[context;dst]"
2023-03-25 23:37:15 +01:00
}
return table.concat(formspec, "")
end
2021-08-25 18:01:36 +02:00
end
local function can_dig(pos, player)
if exchangeclone.mineclone then return true end
2021-08-25 18:40:33 +02:00
local meta = minetest.get_meta(pos);
local inv = meta:get_inventory()
2023-03-26 16:43:44 +02:00
return inv:is_empty("main") and inv:is_empty("dst")
2021-08-25 18:01:36 +02:00
end
local function on_timer(pos, elapsed)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local update = true
while elapsed > 0 and update do
update = false
2023-03-26 16:43:44 +02:00
if not inv:is_empty("dst") and not inv:is_empty("main") then
2021-08-25 18:01:36 +02:00
-- remove one item from fuel inventory
2023-03-26 16:43:44 +02:00
local fuel_stack = inv:get_stack("main", 1)
2023-03-27 23:51:17 +02:00
local energy_value = 0
if fuel_stack:get_name() == "exchangeclone:exchange_orb" then
2023-03-28 00:06:40 +02:00
energy_value = (fuel_stack:get_meta():get_int("stored_charge") or 0) + 8452 --8452 = energy cost of orb
else
2023-03-28 00:06:40 +02:00
energy_value = get_item_energy(fuel_stack:get_name())
end
if energy_value ~= 0 then
local wear = fuel_stack:get_wear()
if wear and wear ~= 0 then
energy_value = math.ceil(energy_value * (wear / 65536))
end
fuel_stack:set_count(fuel_stack:get_count() - 1)
inv:set_stack("main", 1, fuel_stack)
-- only get 1 orb as we can only use one
local dest_orb = inv:get_stack("dst", 1)
local stored = dest_orb:get_meta():get_int("stored_charge") or 0
stored = stored + energy_value
dest_orb:get_meta():set_int("stored_charge", stored)
dest_orb:get_meta():set_string("description", "Exchange Orb\nCurrent Charge: "..tostring(stored))
inv:set_stack("dst", 1, dest_orb)
end
2021-08-25 18:01:36 +02:00
update = true
end
end
minetest.get_node_timer(pos):stop()
return false
end
local function on_construct(pos)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
2023-03-26 16:43:44 +02:00
inv:set_size("main", 1)
2021-08-25 18:01:36 +02:00
inv:set_size("dst", 1)
meta:set_string("formspec", get_element_deconstructor_formspec())
meta:set_string("infotext", "Element Deconstructor")
on_timer(pos, 0)
end
local function allow_metadata_inventory_put(pos, listname, index, stack, player)
2023-03-25 23:02:24 +01:00
2021-08-25 18:40:33 +02:00
if minetest.is_protected(pos, player:get_player_name()) then
return 0
end
2021-08-25 18:01:36 +02:00
local meta = minetest.get_meta(pos)
2021-08-25 18:40:33 +02:00
local inv = meta:get_inventory()
2021-08-25 18:01:36 +02:00
if listname == "dst" then
if stack:get_name() == "exchangeclone:exchange_orb" then
2021-08-25 18:01:36 +02:00
return stack:get_count()
else
return 0
end
2023-03-26 16:43:44 +02:00
elseif listname == "main" then
2021-08-25 18:01:36 +02:00
return stack:get_count()
end
end
local function allow_metadata_inventory_move(pos, from_list, from_index, to_list, to_index, count, player)
2021-08-25 18:40:33 +02:00
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local stack = inv:get_stack(from_list, from_index)
return allow_metadata_inventory_put(pos, to_list, to_index, stack, player)
2021-08-25 18:01:36 +02:00
end
local function allow_metadata_inventory_take(pos, listname, index, stack, player)
2021-08-25 18:40:33 +02:00
if minetest.is_protected(pos, player:get_player_name()) then
return 0
end
return stack:get_count()
2021-08-25 18:01:36 +02:00
end
local function on_blast(pos)
local drops = {}
2023-03-26 16:43:44 +02:00
default.get_inventory_drops(pos, "main", drops)
2021-08-25 18:01:36 +02:00
default.get_inventory_drops(pos, "dst", drops)
drops[#drops+1] = "exchangeclone:element_deconstructor"
2021-08-25 18:01:36 +02:00
minetest.remove_node(pos)
return drops
end
minetest.register_node("exchangeclone:element_deconstructor", {
2021-08-25 18:01:36 +02:00
description = "Element Deconstructor",
2021-12-30 18:51:45 +01:00
tiles = {
"ee_deconstructor_up.png",
"ee_deconstructor_down.png",
"ee_deconstructor_right.png",
"ee_deconstructor_right.png",
"ee_deconstructor_right.png",
"ee_deconstructor_right.png"
},
2023-03-26 16:43:44 +02:00
groups = {cracky = 2, container = 3},
2021-08-25 18:40:33 +02:00
is_ground_content = false,
can_dig = can_dig,
2023-03-27 00:19:19 +02:00
after_dig_node = function(pos, oldnode, oldmetadata, digger)
if exchangeclone.mineclone then
local meta = minetest.get_meta(pos)
local meta2 = meta:to_table()
meta:from_table(oldmetadata)
local inv = meta:get_inventory()
for _, listname in ipairs({"main", "dst"}) do
local stack = inv:get_stack(listname, 1)
if not stack:is_empty() then
local p = {x=pos.x+math.random(0, 10)/10-0.5, y=pos.y, z=pos.z+math.random(0, 10)/10-0.5}
minetest.add_item(p, stack)
end
end
meta:from_table(meta2)
end
2023-03-27 00:19:19 +02:00
end,
2021-08-25 18:01:36 +02:00
on_timer = on_timer,
2021-08-25 18:40:33 +02:00
on_construct = on_construct,
on_metadata_inventory_move = function(pos)
minetest.get_node_timer(pos):start(1.0)
end,
on_metadata_inventory_put = function(pos)
minetest.get_node_timer(pos):start(1.0)
end,
on_metadata_inventory_take = function(pos)
minetest.get_node_timer(pos):start(1.0)
end,
2021-08-25 18:01:36 +02:00
on_blast = on_blast,
allow_metadata_inventory_put = allow_metadata_inventory_put,
allow_metadata_inventory_move = allow_metadata_inventory_move,
allow_metadata_inventory_take = allow_metadata_inventory_take,
})
2021-08-26 16:10:40 +02:00
local recipe_ingredient = "default:furnace"
if exchangeclone.mineclone then
recipe_ingredient = "mcl_furnaces:furnace"
end
2021-08-26 16:10:40 +02:00
minetest.register_craft({
type = "shaped",
output = "exchangeclone:element_deconstructor",
2021-08-26 16:10:40 +02:00
recipe = {
{"", "exchangeclone:exchange_orb",""},
{"exchangeclone:exchange_orb", recipe_ingredient, "exchangeclone:exchange_orb"},
{"", "exchangeclone:exchange_orb", ""}
2021-08-26 16:10:40 +02:00
}
})