microexpansion/modules/power/network.lua

180 lines
4.2 KiB
Lua
Raw Normal View History

2017-07-25 19:47:25 +02:00
-- power/network.lua
local me = microexpansion
local power = me.power
2017-07-25 19:47:25 +02:00
---
--- Helper Functions
---
-- [local function] Get netitem by position
local function get_netitem_by_pos(list, pos)
for _, i in pairs(list) do
if vector.equals(pos, i.pos) then
return i
end
end
end
-- [function] Generate new network ID
function power.new_id()
2019-05-01 13:20:09 +02:00
return "network_"..#me.networks+1
2017-07-25 19:47:25 +02:00
end
-- [function] Add machine to network
function power.add_machine(pos, def)
2019-05-01 13:20:09 +02:00
2017-07-25 19:47:25 +02:00
end
-- [function] Remove machine from network
function power.remove_machine(pos)
local meta = minetest.get_meta(pos)
meta:set_string("network_ignore", "true")
end
-- [function] Trace network
function power.trace(pos)
local netpos = me.networks[minetest.get_meta(pos):get_string("network_id")]
-- if no network, return
if not netpos then
return
end
local meta = minetest.get_meta(netpos)
local netid = meta:get_string("network_id")
local list = {}
local delete = false
if meta:get_string("network_ignore") == "true" then
delete = true
end
-- [local function] Indexed
2020-02-27 15:33:45 +01:00
local function indexed(p)
2017-07-25 19:47:25 +02:00
for _, i in pairs(list) do
2020-02-27 15:33:45 +01:00
if vector.equals(p, i.pos) then
2017-07-25 19:47:25 +02:00
return true
end
end
end
-- [local function] Trace
local function trace(nodes)
2020-02-27 15:33:45 +01:00
for _, p in pairs(nodes) do
if not indexed(p) then
local machine = minetest.get_meta(p)
2017-07-25 19:47:25 +02:00
if machine:get_string("network_ignore") ~= "true" then
2020-02-27 15:33:45 +01:00
local node = me.get_node(p).name
2017-07-25 19:47:25 +02:00
local desc = minetest.registered_nodes[node].description
if delete then
machine:set_string("network_id", nil)
machine:set_string("infotext", desc.."\nNo Network")
2020-02-27 15:33:45 +01:00
me.network_set_demand(p, 0)
2017-07-25 19:47:25 +02:00
else
machine:set_string("network_id", netid)
machine:set_string("infotext", desc.."\nNetwork ID: "..netid)
end
2020-02-27 15:33:45 +01:00
list[#list + 1] = { pos = p, demand = machine:get_int("demand") }
trace(power.get_connected_nodes(p, false))
2017-07-25 19:47:25 +02:00
end
end
end
end
trace(power.get_connected_nodes(netpos))
-- Check original list
local original = minetest.deserialize(meta:get_string("netitems"))
if original then
for _, i in pairs(original) do
if not indexed(i.pos) then
local node = me.get_node(i.pos).name
local desc = minetest.registered_nodes[node].description
local machine = minetest.get_meta(i.pos)
machine:set_string("network_id", nil)
machine:set_string("infotext", desc.."\nNo Network")
me.network_set_demand(pos, 0)
end
end
end
meta:set_string("netitems", minetest.serialize(list))
-- Update infotext
meta:set_string("infotext", "Network Controller (owned by "..
meta:get_string("owner")..")\nNetwork ID: "..meta:get_string("network_id")..
"\nDemand: "..dump(me.network_get_demand(netpos)))
end
---
--- Load Management
---
-- [function] Get load information
function me.network_get_load(pos)
local ctrl = me.networks[minetest.get_meta(pos):get_string("network_id")]
if ctrl then
local meta = minetest.get_meta(ctrl)
local list = minetest.deserialize(meta:get_string("netitems"))
end
end
---- Generators ----
---- Output ----
-- [function] Get total network demand
function me.network_get_demand(pos)
local ctrl = me.networks[minetest.get_meta(pos):get_string("network_id")]
-- if no network, return
if not ctrl then
return
end
local meta = minetest.get_meta(ctrl)
local list = minetest.deserialize(meta:get_string("netitems"))
local demand = 0
for _, i in pairs(list) do
if i.demand then
demand = demand + i.demand
end
end
return demand
end
-- [function] Set demand for machine
function me.network_set_demand(pos, demand)
-- Update original metadata
minetest.get_meta(pos):set_int("demand", demand)
local ctrl = me.networks[minetest.get_meta(pos):get_string("network_id")]
-- if no network, return
if not ctrl then
return
end
local meta = minetest.get_meta(ctrl)
local list = minetest.deserialize(meta:get_string("netitems"))
local item = get_netitem_by_pos(list, pos)
if not item then
return
end
item.demand = demand
meta:set_string("netitems", minetest.serialize(list))
-- Update infotext
meta:set_string("infotext", "Network Controller (owned by "..
meta:get_string("owner")..")\nNetwork ID: "..meta:get_string("network_id")..
"\nDemand: "..dump(me.network_get_demand(pos)))
end
---- Storage ----