Replace spaces with tabs
This commit is contained in:
parent
be54e91335
commit
5a5ca11ecd
39
init.lua
39
init.lua
@ -1,7 +1,11 @@
|
||||
-- microexpansion/init.lua
|
||||
microexpansion = {}
|
||||
microexpansion.modpath = minetest.get_modpath("microexpansion") -- modpath
|
||||
local modpath = microexpansion.modpath -- modpath pointer
|
||||
microexpansion.data = {}
|
||||
microexpansion.modpath = minetest.get_modpath("microexpansion") -- Get modpath
|
||||
microexpansion.worldpath = minetest.get_worldpath() -- Get worldpath
|
||||
|
||||
local modpath = microexpansion.modpath -- Modpath pointer
|
||||
local worldpath = microexpansion.worldpath -- Worldpath pointer
|
||||
|
||||
-- Formspec GUI related stuff
|
||||
microexpansion.gui_bg = "bgcolor[#080808BB;true]background[5,5;1,1;gui_formbg.png;true]"
|
||||
@ -9,6 +13,7 @@ microexpansion.gui_slots = "listcolors[#00000069;#5A5A5A;#141318;#30434C;#FFF]"
|
||||
|
||||
-- logger
|
||||
function microexpansion.log(content, log_type)
|
||||
assert(content, "microexpansion.log: missing content")
|
||||
if not content then return false end
|
||||
if log_type == nil then log_type = "action" end
|
||||
minetest.log(log_type, "[MicroExpansion] "..content)
|
||||
@ -17,6 +22,36 @@ end
|
||||
-- Load API
|
||||
dofile(modpath.."/api.lua")
|
||||
|
||||
-----------------
|
||||
---- ME DATA ----
|
||||
-----------------
|
||||
|
||||
-- [function] Load
|
||||
function microexpansion.load()
|
||||
local res = io.open(worldpath.."/microexpansion.txt", "r")
|
||||
if res then
|
||||
res = minetest.deserialize(res:read("*all"))
|
||||
if type(res) == "table" then
|
||||
microexpansion.networks = res.networks or {}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Load
|
||||
microexpansion.load()
|
||||
|
||||
-- [function] Save
|
||||
function microexpansion.save()
|
||||
local data = {
|
||||
networks = microexpansion.networks,
|
||||
}
|
||||
|
||||
io.open(worldpath.."/microexpansion.txt", "w"):write(minetest.serialize(data))
|
||||
end
|
||||
|
||||
-- [register on] Server Shutdown
|
||||
minetest.register_on_shutdown(microexpansion.save)
|
||||
|
||||
-------------------
|
||||
----- MODULES -----
|
||||
-------------------
|
||||
|
@ -33,11 +33,45 @@ me.register_node("ctrl", {
|
||||
},
|
||||
groups = { cracky = 1, me_connect = 1, },
|
||||
connect_sides = "nobottom",
|
||||
status = "no",
|
||||
status = "unstable",
|
||||
|
||||
after_place_node = function(pos, player)
|
||||
local name = player:get_player_name()
|
||||
local meta = minetest.get_meta(pos)
|
||||
local id = power.new_id()
|
||||
|
||||
meta:set_string("infotext", "Network Controller (owned by "..name..")"
|
||||
.."\nNetwork ID: "..id)
|
||||
meta:set_string("network_id", id)
|
||||
meta:set_string("owner", name)
|
||||
|
||||
-- Initialize other meta
|
||||
meta:set_int("input", 0)
|
||||
meta:set_int("output", 0)
|
||||
meta:set_int("storage", 0)
|
||||
|
||||
me.networks[id] = pos
|
||||
|
||||
-- Trace Network
|
||||
power.trace(pos)
|
||||
end,
|
||||
on_destruct = function(pos, player)
|
||||
local meta = minetest.get_meta(pos)
|
||||
local id = meta:get_string("network_id")
|
||||
me.networks[id] = nil
|
||||
|
||||
-- Remove unit from network
|
||||
me.network_remove(pos)
|
||||
-- Trace/clear network
|
||||
power.trace(pos)
|
||||
end,
|
||||
machine = {
|
||||
type = "transporter",
|
||||
},
|
||||
})
|
||||
|
||||
-- [register node] Cable
|
||||
me.register_node("cable", {
|
||||
me.register_machine("cable", {
|
||||
description = "ME Cable",
|
||||
tiles = {
|
||||
"cable",
|
||||
@ -53,7 +87,10 @@ me.register_node("cable", {
|
||||
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+
|
||||
},
|
||||
connects_to = {"group:me_connect"},
|
||||
groups = { crumbly = 1, me_connect = 1, },
|
||||
status = "no",
|
||||
paramtype = "light",
|
||||
groups = { crumbly = 1, },
|
||||
status = "unstable",
|
||||
machine = {
|
||||
type = "transporter",
|
||||
},
|
||||
})
|
||||
|
@ -3,7 +3,7 @@
|
||||
local me = microexpansion
|
||||
|
||||
-- [register node] Fuel Fired Generator
|
||||
me.register_node("fuel_fired_generator", {
|
||||
me.register_machine("fuel_fired_generator", {
|
||||
description = "Fuel-Fired Generator",
|
||||
tiles = {
|
||||
"machine_sides",
|
||||
@ -11,6 +11,7 @@ me.register_node("fuel_fired_generator", {
|
||||
"machine_sides",
|
||||
"machine_sides",
|
||||
"machine_sides",
|
||||
"machine_sides",
|
||||
"fuelgen_front",
|
||||
},
|
||||
recipe = {
|
||||
@ -21,10 +22,16 @@ me.register_node("fuel_fired_generator", {
|
||||
},
|
||||
}
|
||||
},
|
||||
groups = { cracky = 1, me_connect = 1, },
|
||||
groups = { cracky = 1 },
|
||||
connect_sides = "machine",
|
||||
paramtype2 = "facedir",
|
||||
status = "no",
|
||||
status = "unstable",
|
||||
machine = {
|
||||
type = "provider",
|
||||
on_survey = function(pos)
|
||||
return 5 -- Generate 5 ME/tick
|
||||
end,
|
||||
},
|
||||
})
|
||||
|
||||
-- [register node] Super Smelter
|
||||
@ -49,7 +56,13 @@ me.register_node("super_smelter", {
|
||||
groups = { cracky = 1, me_connect = 1, },
|
||||
connect_sides = "machine",
|
||||
paramtype2 = "facedir",
|
||||
status = "no",
|
||||
status = "unstable",
|
||||
machine = {
|
||||
type = "consumer",
|
||||
on_survey = function(pos)
|
||||
return 5 -- Consume 5 ME/tick
|
||||
end,
|
||||
},
|
||||
})
|
||||
|
||||
-- [register item] Geothermal Generator
|
||||
@ -66,5 +79,11 @@ me.register_node("geo_generator", {
|
||||
groups = { cracky = 1, me_connect = 1, },
|
||||
connect_sides = "machine",
|
||||
paramtype2 = "facedir",
|
||||
status = "no",
|
||||
status = "unstable",
|
||||
machine = {
|
||||
type = "provider",
|
||||
on_survey = function(pos)
|
||||
return 10 -- Generate 10 ME/tick
|
||||
end,
|
||||
},
|
||||
})
|
||||
|
@ -1,10 +1,16 @@
|
||||
-- power/init.lua
|
||||
|
||||
local me = microexpansion
|
||||
|
||||
local networks = me.networks
|
||||
local path = microexpansion.get_module_path("power")
|
||||
|
||||
-- Power generators, wires, etc...
|
||||
me.power = {}
|
||||
local power = me.power
|
||||
|
||||
-- Load Resources
|
||||
|
||||
dofile(path.."/ctrl.lua") -- Controller
|
||||
dofile(path.."/network.lua") -- Network Management
|
||||
dofile(path.."/register.lua") -- Machine Registration
|
||||
dofile(path.."/ctrl.lua") -- Controller/wires
|
||||
dofile(path.."/gen.lua") -- Generators
|
||||
|
244
modules/power/network.lua
Normal file
244
modules/power/network.lua
Normal file
@ -0,0 +1,244 @@
|
||||
-- power/network.lua
|
||||
|
||||
local me = microexpansion
|
||||
|
||||
---
|
||||
--- Helper Functions
|
||||
---
|
||||
|
||||
-- [local function] Renumber table
|
||||
local function renumber_table(t)
|
||||
local result = {}
|
||||
for _, value in pairs(t) do
|
||||
result[#result+1] = value
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
-- [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
|
||||
|
||||
---
|
||||
--- API Functions
|
||||
---
|
||||
|
||||
-- [function] Get node
|
||||
function me.get_node(pos)
|
||||
local node = minetest.get_node_or_nil(pos)
|
||||
if node then return node end
|
||||
local vm = VoxelManip()
|
||||
local MinEdge, MaxEdge = vm:read_from_map(pos, pos)
|
||||
return minetest.get_node(pos)
|
||||
end
|
||||
|
||||
-- [function] Generate new network ID
|
||||
function power.new_id()
|
||||
local count = 1
|
||||
for _, i in pairs(me.networks) do
|
||||
count = count + 1
|
||||
end
|
||||
|
||||
return "network_"..count
|
||||
end
|
||||
|
||||
-- [function] Can connect
|
||||
function power.can_connect(pos)
|
||||
local node = me.get_node(pos)
|
||||
local res = minetest.get_item_group(node.name, "me_connect")
|
||||
|
||||
if res == 1 then
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
-- [function] Get connected nodes
|
||||
function power.get_connected_nodes(pos, include_ctrl)
|
||||
local nodes = {
|
||||
{x=pos.x+1, y=pos.y, z=pos.z},
|
||||
{x=pos.x-1, y=pos.y, z=pos.z},
|
||||
{x=pos.x, y=pos.y+1, z=pos.z},
|
||||
{x=pos.x, y=pos.y-1, z=pos.z},
|
||||
{x=pos.x, y=pos.y, z=pos.z+1},
|
||||
{x=pos.x, y=pos.y, z=pos.z-1},
|
||||
}
|
||||
|
||||
for _, pos in pairs(nodes) do
|
||||
if not power.can_connect(pos) then
|
||||
nodes[_] = nil
|
||||
else
|
||||
if include_ctrl == false then
|
||||
if me.get_node(pos).name == "microexpansion:ctrl" then
|
||||
nodes[_] = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return renumber_table(nodes)
|
||||
end
|
||||
|
||||
-- [function] Add machine to network
|
||||
function power.add_machine(pos, def)
|
||||
|
||||
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 demand
|
||||
|
||||
local delete = false
|
||||
if meta:get_string("network_ignore") == "true" then
|
||||
delete = true
|
||||
end
|
||||
|
||||
-- [local function] Indexed
|
||||
local function indexed(pos)
|
||||
for _, i in pairs(list) do
|
||||
if vector.equals(pos, i.pos) then
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- [local function] Trace
|
||||
local function trace(nodes)
|
||||
for _, pos in pairs(nodes) do
|
||||
if not indexed(pos) then
|
||||
local machine = minetest.get_meta(pos)
|
||||
if machine:get_string("network_ignore") ~= "true" then
|
||||
local node = me.get_node(pos).name
|
||||
local desc = minetest.registered_nodes[node].description
|
||||
if delete then
|
||||
machine:set_string("network_id", nil)
|
||||
machine:set_string("infotext", desc.."\nNo Network")
|
||||
me.network_set_demand(pos, 0)
|
||||
else
|
||||
machine:set_string("network_id", netid)
|
||||
machine:set_string("infotext", desc.."\nNetwork ID: "..netid)
|
||||
end
|
||||
|
||||
list[#list + 1] = { pos = pos, demand = machine:get_int("demand") }
|
||||
trace(power.get_connected_nodes(pos, false))
|
||||
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 ----
|
88
modules/power/register.lua
Normal file
88
modules/power/register.lua
Normal file
@ -0,0 +1,88 @@
|
||||
-- power/register.lua
|
||||
|
||||
--[[ Machine Registration API ]]
|
||||
|
||||
local me = microexpansion
|
||||
local power = me.power
|
||||
|
||||
-- [function] Register machine
|
||||
function me.register_machine(itemstring, def)
|
||||
-- Set after_place_node
|
||||
def.after_place_node = function(pos, player)
|
||||
if def.after_place_node then
|
||||
def.after_place_node(pos, player)
|
||||
end
|
||||
|
||||
local meta = minetest.get_meta(pos)
|
||||
local nodes = me.get_connected_nodes(pos)
|
||||
|
||||
meta:set_string("infotext", def.description.."\nNo Network")
|
||||
|
||||
for _, pos2 in pairs(nodes) do
|
||||
local id = minetest.get_meta(pos2):get_string("network_id")
|
||||
|
||||
if id ~= "" then
|
||||
meta:set_string("infotext", def.description.."\nNetwork ID: "..id)
|
||||
meta:set_string("network_id", id)
|
||||
end
|
||||
end
|
||||
|
||||
-- Trace Network
|
||||
power.trace(pos)
|
||||
|
||||
-- Set demand
|
||||
if def.demand then
|
||||
me.network_set_demand(pos, def.demand)
|
||||
end
|
||||
|
||||
if type(def.machine) == "table" then
|
||||
power.add_machine(pos, def.machine)
|
||||
end
|
||||
end
|
||||
-- Set on_destruct
|
||||
def.on_destruct = function(pos, player)
|
||||
if def.on_destruct then
|
||||
def.on_destruct(pos, player)
|
||||
end
|
||||
|
||||
local meta = minetest.get_meta(pos)
|
||||
|
||||
if meta:get_string("network_id") ~= "" then
|
||||
-- Set demand
|
||||
me.network_set_demand(pos, 0)
|
||||
-- Remove item from network
|
||||
me.network_remove(pos)
|
||||
-- Retrace Network
|
||||
power.trace(pos)
|
||||
end
|
||||
end
|
||||
-- Set connects_to
|
||||
def.connects_to = {"group:me_connect"}
|
||||
-- Set me_connect group
|
||||
def.groups = def.groups or {}
|
||||
def.groups.me_connect = 1
|
||||
|
||||
me.register_node(itemstring, def)
|
||||
end
|
||||
|
||||
-- [function] Get machine definition
|
||||
function me.get_def(name, key)
|
||||
if type(name) == "table" then
|
||||
local node = me.get_node(name)
|
||||
if node then
|
||||
name = node.name
|
||||
end
|
||||
end
|
||||
|
||||
local def = minetest.registered_nodes[name]
|
||||
-- Check name and if registered
|
||||
if not name or not def then
|
||||
return
|
||||
end
|
||||
|
||||
if key then
|
||||
return def[key]
|
||||
else
|
||||
return def
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user