Add power handling to the controller and require power for the remote.

On the remote, ensure that reset updates the formspec.
This commit is contained in:
Mike Stump 2024-01-09 14:33:00 -08:00
parent 69e2f8b565
commit e6ffb906e4
3 changed files with 99 additions and 6 deletions

@ -42,7 +42,25 @@ me.register_node("ctrl", {
{-0.1875, -0.5, -0.1875, 0.1875, -0.25, 0.1875}, -- Bottom2 {-0.1875, -0.5, -0.1875, 0.1875, -0.25, 0.1875}, -- Bottom2
}, },
}, },
groups = { cracky = 1, me_connect = 1, }, groups = { cracky = 1, me_connect = 1,
-- for technic integration
technic_machine = 1, technic_hv = 1,
-- for on/off switch to conserve power
mesecon_effector_off = 1, mesecon = 2,
},
--HV_EU_demand = 10,
-- typical technic connections:
-- connect_sides = {"bottom", "front", "left", "right"},
--HV_EU_input = 23,
--HV_EU_supply = 144,
--HV_EU_demand = 44,
technic_run = function(pos, node)
--local meta = minetest.get_meta(pos)
-- quick cheat sheet for how to wire:
--meta:set_int("HV_EU_input", 23)
--meta:set_int("HV_EU_demand", 45)
--meta:set_int("HV_EU_supply", 1045)
end,
connect_sides = "nobottom", connect_sides = "nobottom",
me_update = function(pos,_,ev) me_update = function(pos,_,ev)
local net = me.get_network(pos) local net = me.get_network(pos)
@ -52,13 +70,30 @@ me.register_node("ctrl", {
end end
net:update() net:update()
end, end,
mesecons = {effector = {
action_on = function (pos, node)
local net = me.get_network(pos)
-- turn OFF on mese power
local meta = minetest.get_meta(pos)
meta:set_int("enabled", 0)
net:update_demand()
end,
action_off = function (pos, node)
local net = me.get_network(pos)
-- turn ON without mesepower
local meta = minetest.get_meta(pos)
meta:set_int("enabled", 1)
net:update_demand()
end
}},
on_construct = function(pos) on_construct = function(pos)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local net = network.new({controller_pos = pos}) local net = network.new({controller_pos = pos})
table.insert(me.networks,net) table.insert(me.networks,net)
me.send_event(pos,"connect",{net=net}) me.send_event(pos,"connect",{net=net})
meta:set_string("infotext", "Network Controller") meta:set_int("enabled", 0)
net:update_demand()
end, end,
after_place_node = function(pos, player) after_place_node = function(pos, player)
local name = player:get_player_name() local name = player:get_player_name()
@ -168,3 +203,9 @@ me.register_machine("cable", {
type = "conductor", type = "conductor",
}, },
}) })
if technic then
-- quick cheat sheet for how to wire:
-- producer receiver, producer_receiver, battery
technic.register_machine("HV", "microexpansion:ctrl", technic.receiver)
end

@ -237,6 +237,7 @@ end
function network:update() function network:update()
self:set_storage_space(true) self:set_storage_space(true)
self:update_demand()
end end
function network:get_inventory_name() function network:get_inventory_name()
@ -526,6 +527,52 @@ function network:load()
end end
end end
-- Helper to check to see if the controller is on and powered.
function network:powered(name)
local net = self
local meta = minetest.get_meta(net.controller_pos)
local run = meta:get_int("enabled") == 1
if not run then
minetest.chat_send_player(name, "Please enable by turning controller switch.")
return false
end
--me.log("REMOTE: power level input is "..meta:get_int("HV_EU_input").." and demand is "..meta:get_int("HV_EU_demand"), "error")
run = not technic or (meta:get_int("HV_EU_input") >= meta:get_int("HV_EU_demand"))
if not run then
minetest.chat_send_player(name, "Please provide HV power to ME controller.")
return false
end
return true
end
function network:update_demand()
local demand = 60
local pos = self.controller_pos
local meta = minetest.get_meta(pos)
local net = self
if meta:get_int("enabled") == 0 then
meta:set_int("HV_EU_demand", 0)
meta:set_string("infotext", "Disabled")
return
end
for ipos in me.connected_nodes(pos) do
local name = me.get_node(ipos).name
if name == "microexpansion:cable" then
demand = demand + 1
elseif name == "microexpansion:interface" then
local meta = minetest.get_meta(ipos)
local inventories = minetest.deserialize(meta:get_string("connected"))
demand = demand + #inventories * 10
else
demand = demand + 10
end
local name = meta:get_string("owner")
meta:set_string("infotext", "Network Controller (owned by "..name..")")
end
--me.log("NET: demand is "..demand, "error")
meta:set_int("HV_EU_demand", demand)
end
-- We don't save this data, rather we rewalk upon first use. If 1% of -- We don't save this data, rather we rewalk upon first use. If 1% of
-- the people play per reboot, then this saves 99% of the work. -- the people play per reboot, then this saves 99% of the work.
-- Also, we don't actually read or write any of this data normally, -- Also, we don't actually read or write any of this data normally,
@ -539,10 +586,12 @@ function network:reload_network()
self.autocrafters_by_pos = {} self.autocrafters_by_pos = {}
self.process = {} self.process = {}
for ipos in me.connected_nodes(self.controller_pos) do for ipos in me.connected_nodes(self.controller_pos) do
if me.get_node(ipos).name == "microexpansion:interface" then local name = me.get_node(ipos).name
if name == "microexpansion:interface" then
me.reload_interface(self, ipos, nil) me.reload_interface(self, ipos, nil)
end end
end end
self:update_demand()
end end
function network:serialize() function network:serialize()

@ -11,7 +11,7 @@ end
local function get_metadata(toolstack) local function get_metadata(toolstack)
local m = minetest.deserialize(toolstack:get_metadata()) local m = minetest.deserialize(toolstack:get_metadata())
if not m then m = {} end if not m then m = {} end
if not m.charge then m.charge = 100 end if not m.charge then m.charge = 1000 end
if not m.page then m.page = 1 end if not m.page then m.page = 1 end
if not m.query then m.query = "" end if not m.query then m.query = "" end
if not m.crafts then m.crafts = "false" end if not m.crafts then m.crafts = "false" end
@ -116,6 +116,7 @@ minetest.register_tool("microexpansion:remote", {
pos.z = pos.z - 1 pos.z = pos.z - 1
local net,cpos = me.get_connected_network(pos) local net,cpos = me.get_connected_network(pos)
if net then if net then
if not net:powered(user:get_player_name()) then return end
minetest.chat_send_player(user:get_player_name(), "Connected to ME network, right-click to use.") minetest.chat_send_player(user:get_player_name(), "Connected to ME network, right-click to use.")
toolmeta.terminal = pos toolmeta.terminal = pos
local pinv = user:get_inventory() local pinv = user:get_inventory()
@ -137,8 +138,7 @@ minetest.register_tool("microexpansion:remote", {
minetest.chat_send_player(user:get_player_name(), "Left-click on ME block to connect to ME network.") minetest.chat_send_player(user:get_player_name(), "Left-click on ME block to connect to ME network.")
return return
end end
local net = me.get_connected_network(pos) local net,cpos = me.get_connected_network(pos)
-- if not net then return end
local charge_to_take = 100 local charge_to_take = 100
@ -153,6 +153,8 @@ minetest.register_tool("microexpansion:remote", {
technic.set_RE_wear(toolstack, toolmeta.charge, technic.power_tools[toolstack:get_name()]) technic.set_RE_wear(toolstack, toolmeta.charge, technic.power_tools[toolstack:get_name()])
end end
if net and not net:powered(user:get_player_name()) then return end
local page = toolmeta.page local page = toolmeta.page
local inv_name = toolmeta.inv_name local inv_name = toolmeta.inv_name
local query = toolmeta.query local query = toolmeta.query
@ -252,6 +254,7 @@ minetest.register_on_player_receive_fields(function(user, formname, fields)
toolmeta.crafts = "false" toolmeta.crafts = "false"
toolmeta.page_max = math.floor(inv:get_size(inv_name) / 32) + 1 toolmeta.page_max = math.floor(inv:get_size(inv_name) / 32) + 1
update_search = true update_search = true
did_update = true
elseif field == "tochest" then elseif field == "tochest" then
elseif field == "autocraft" then elseif field == "autocraft" then
if tonumber(value) ~= nil then if tonumber(value) ~= nil then