2017-03-02 06:38:53 +01:00
|
|
|
-- power/ctrl.lua
|
2017-03-02 02:27:00 +01:00
|
|
|
|
|
|
|
local me = microexpansion
|
2019-05-04 13:53:49 +02:00
|
|
|
local network = me.network
|
2017-03-02 02:27:00 +01:00
|
|
|
|
2019-05-18 07:37:46 +02:00
|
|
|
local function update_ctrl(pos)
|
|
|
|
local cnetwork = me.get_network(pos)
|
|
|
|
if cnetwork == nil then
|
2019-05-18 07:16:11 +02:00
|
|
|
minetest.log("error","no network for ctrl at pos "..minetest.pos_to_string(pos))
|
|
|
|
return
|
|
|
|
end
|
2019-05-18 07:37:46 +02:00
|
|
|
local size = cnetwork:get_item_capacity()
|
2019-05-07 17:49:26 +02:00
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local inv = meta:get_inventory()
|
|
|
|
inv:set_size("main", me.int_to_stacks(size))
|
|
|
|
end
|
|
|
|
|
|
|
|
function me.insert_item(stack, inv, listname)
|
|
|
|
if me.settings.huge_stacks == false then
|
|
|
|
inv:add_item(listname, stack)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local stack_name
|
|
|
|
local stack_count
|
|
|
|
if type(stack) == "string" then
|
|
|
|
local split_string = stack:split(" ")
|
|
|
|
stack_name = split_string[1]
|
|
|
|
if (#split_string > 1) then
|
|
|
|
stack_count = tonumber(split_string[2])
|
|
|
|
else
|
|
|
|
stack_count = 1
|
|
|
|
end
|
|
|
|
else
|
|
|
|
stack_name = stack:get_name()
|
|
|
|
stack_count = stack:get_count()
|
|
|
|
end
|
|
|
|
local found = false
|
|
|
|
for i = 0, inv:get_size(listname) do
|
|
|
|
local inside = inv:get_stack(listname, i)
|
|
|
|
if inside:get_name() == stack_name then
|
|
|
|
local total_count = inside:get_count() + stack_count
|
|
|
|
-- bigger item count is not possible we only have unsigned 16 bit
|
|
|
|
if total_count <= math.pow(2,16) then
|
|
|
|
if not inside:set_count(total_count) then
|
|
|
|
minetest.log("error"," adding items to stack in microexpansion network failed")
|
|
|
|
print("stack is now " .. inside:to_string())
|
|
|
|
end
|
|
|
|
inv:set_stack(listname, i, inside)
|
|
|
|
found = true
|
|
|
|
break;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if not found then
|
|
|
|
inv:add_item(listname, stack)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-02 02:27:00 +01:00
|
|
|
-- [register node] Controller
|
|
|
|
me.register_node("ctrl", {
|
2019-05-04 13:53:49 +02:00
|
|
|
description = "ME Controller",
|
2017-07-25 19:47:25 +02:00
|
|
|
tiles = {
|
2017-03-02 02:27:00 +01:00
|
|
|
"ctrl_sides",
|
|
|
|
"ctrl_bottom",
|
|
|
|
"ctrl_sides",
|
|
|
|
"ctrl_sides",
|
|
|
|
"ctrl_sides",
|
|
|
|
"ctrl_sides"
|
|
|
|
},
|
2017-07-25 19:47:25 +02:00
|
|
|
drawtype = "nodebox",
|
|
|
|
paramtype = "light",
|
2017-03-02 02:27:00 +01:00
|
|
|
node_box = {
|
|
|
|
type = "fixed",
|
|
|
|
fixed = {
|
|
|
|
{-0.375, -0.375, -0.375, 0.375, 0.375, 0.375}, -- Core
|
|
|
|
{0.1875, -0.5, -0.5, 0.5, 0.5, -0.1875}, -- Corner1
|
|
|
|
{-0.5, -0.5, -0.5, -0.1875, 0.5, -0.1875}, -- Corner2
|
|
|
|
{-0.5, -0.5, 0.1875, -0.1875, 0.5, 0.5}, -- Corner3
|
|
|
|
{0.1875, -0.5, 0.1875, 0.5, 0.5, 0.5}, -- Corner4
|
|
|
|
{-0.5, -0.4375, -0.5, 0.5, -0.1875, 0.5}, -- Bottom
|
|
|
|
{-0.5, 0.1875, -0.5, 0.5, 0.5, -0.1875}, -- Top1
|
|
|
|
{0.1875, 0.1875, -0.5, 0.5, 0.5, 0.5}, -- Top2
|
|
|
|
{-0.5, 0.1875, -0.5, -0.1875, 0.5, 0.5}, -- Top3
|
|
|
|
{-0.5, 0.1875, 0.1875, 0.5, 0.5, 0.5}, -- Top4
|
|
|
|
{-0.1875, -0.5, -0.1875, 0.1875, -0.25, 0.1875}, -- Bottom2
|
|
|
|
},
|
|
|
|
},
|
2017-07-25 19:47:25 +02:00
|
|
|
groups = { cracky = 1, me_connect = 1, },
|
|
|
|
connect_sides = "nobottom",
|
2019-05-07 17:49:26 +02:00
|
|
|
me_update = update_ctrl,
|
2017-07-25 19:47:25 +02:00
|
|
|
after_place_node = function(pos, player)
|
|
|
|
local name = player:get_player_name()
|
|
|
|
local meta = minetest.get_meta(pos)
|
2019-05-04 13:53:49 +02:00
|
|
|
table.insert(me.networks,network:new({controller_pos = pos}))
|
|
|
|
me.update_connected_machines(pos)
|
2017-07-25 19:47:25 +02:00
|
|
|
|
2019-05-04 13:53:49 +02:00
|
|
|
meta:set_string("infotext", "Network Controller (owned by "..name..")")
|
2017-07-25 19:47:25 +02:00
|
|
|
meta:set_string("owner", name)
|
|
|
|
end,
|
2019-05-18 07:37:46 +02:00
|
|
|
on_destruct = function(pos)
|
2019-05-04 13:53:49 +02:00
|
|
|
local net,idx = me.get_network(pos)
|
|
|
|
if net then
|
|
|
|
net.controller_pos = nil
|
|
|
|
end
|
|
|
|
if idx then
|
|
|
|
table.remove(me.networks,idx)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
after_dig_node = function(pos)
|
|
|
|
me.update_connected_machines(pos)
|
2017-07-25 19:47:25 +02:00
|
|
|
end,
|
2019-05-18 07:37:46 +02:00
|
|
|
allow_metadata_inventory_put = function(pos, listname, index, stack)
|
2019-05-07 17:49:26 +02:00
|
|
|
local inv = minetest.get_meta(pos):get_inventory()
|
|
|
|
local inside_stack = inv:get_stack(listname, index)
|
|
|
|
local stack_name = stack:get_name()
|
|
|
|
-- improve performance by skipping unnessecary calls
|
|
|
|
if inside_stack:get_name() ~= stack_name or inside_stack:get_count() >= inside_stack:get_stack_max() then
|
|
|
|
if inv:get_stack(listname, index+1):get_name() ~= "" then
|
|
|
|
return stack:get_count()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
local max_slots = inv:get_size(listname)
|
|
|
|
local max_items = math.floor(max_slots * 99)
|
|
|
|
|
|
|
|
local slots, items = 0, 0
|
|
|
|
-- Get amount of items in drive
|
|
|
|
for i = 1, max_slots do
|
2019-05-18 07:37:46 +02:00
|
|
|
local dstack = inv:get_stack("main", i)
|
|
|
|
if dstack:get_name() ~= "" then
|
2019-05-07 17:49:26 +02:00
|
|
|
slots = slots + 1
|
2019-05-18 07:37:46 +02:00
|
|
|
local num = dstack:get_count()
|
2019-05-07 17:49:26 +02:00
|
|
|
if num == 0 then num = 1 end
|
|
|
|
items = items + num
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return math.max(math.min(stack:get_count(),max_items-items),0)
|
|
|
|
end,
|
2019-05-18 07:37:46 +02:00
|
|
|
on_metadata_inventory_put = function(pos, listname, _, stack)
|
2019-05-07 17:49:26 +02:00
|
|
|
local inv = minetest.get_meta(pos):get_inventory()
|
|
|
|
inv:remove_item(listname, stack)
|
|
|
|
me.insert_item(stack, inv, listname)
|
|
|
|
end,
|
2019-05-18 07:37:46 +02:00
|
|
|
allow_metadata_inventory_take = function(_, _, _, stack)
|
2019-05-07 17:49:26 +02:00
|
|
|
return math.min(stack:get_count(),stack:get_stack_max())
|
|
|
|
end,
|
2017-07-25 19:47:25 +02:00
|
|
|
machine = {
|
|
|
|
type = "transporter",
|
|
|
|
},
|
2017-03-02 06:38:53 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
-- [register node] Cable
|
2017-07-25 19:47:25 +02:00
|
|
|
me.register_machine("cable", {
|
|
|
|
description = "ME Cable",
|
|
|
|
tiles = {
|
|
|
|
"cable",
|
|
|
|
},
|
|
|
|
drawtype = "nodebox",
|
|
|
|
node_box = {
|
|
|
|
type = "connected",
|
|
|
|
fixed = {-0.25, -0.25, -0.25, 0.25, 0.25, 0.25},
|
2017-03-02 06:38:53 +01:00
|
|
|
connect_top = {-0.25, -0.25, -0.25, 0.25, 0.5, 0.25}, -- y+
|
|
|
|
connect_bottom = {-0.25, -0.5, -0.25, 0.25, 0.25, 0.25}, -- y-
|
|
|
|
connect_front = {-0.25, -0.25, -0.5, 0.25, 0.25, 0.25}, -- z-
|
|
|
|
connect_back = {-0.25, -0.25, 0.25, 0.25, 0.25, 0.5 }, -- z+
|
|
|
|
connect_left = {-0.5, -0.25, -0.25, 0.25, 0.25, 0.25}, -- x-
|
|
|
|
connect_right = {-0.25, -0.25, -0.25, 0.5, 0.25, 0.25}, -- x+
|
2017-07-25 19:47:25 +02:00
|
|
|
},
|
|
|
|
paramtype = "light",
|
|
|
|
groups = { crumbly = 1, },
|
2019-05-04 13:53:49 +02:00
|
|
|
after_place_node = me.update_connected_machines,
|
|
|
|
after_dig_node = me.update_connected_machines,
|
2017-07-25 19:47:25 +02:00
|
|
|
machine = {
|
|
|
|
type = "transporter",
|
|
|
|
},
|
2017-03-02 02:27:00 +01:00
|
|
|
})
|