diff --git a/README.md b/README.md index c836308..a1505cf 100644 --- a/README.md +++ b/README.md @@ -7,8 +7,8 @@ MicroExpansion - ME [microexpansion] * **Licence:** Code: MIT (see LICENSE), Media: CC-BY-SA 3.0 * [Github Repository](https://github.com/octacian/microexpansion) * **Downloads:** - * [Master (Unstable)](https://github.com/octacian/microexpansion/archive/master.zip) - * ...or browse the code on [GitHub](https://github.com/octacian/microexpansion) + * [Master (Unstable)](https://github.com/octacian/microexpansion/archive/master.zip) + * ...or browse the code on [GitHub](https://github.com/octacian/microexpansion) **Note**: MicroExpansion requires that you have `minetest-dev` with [this commit](https://github.com/minetest/minetest/commit/f2f9a923515386d787a245fac52f78e815b3a839) or later. diff --git a/api.lua b/api.lua index 5249ffd..61a55e9 100644 --- a/api.lua +++ b/api.lua @@ -3,150 +3,150 @@ local BASENAME = "microexpansion" -- [function] Register Recipe function microexpansion.register_recipe(output, recipe) - -- Check if disabled - if recipe.disabled == true then - return - end + -- Check if disabled + if recipe.disabled == true then + return + end - local function isint(n) - return n==math.floor(n) - end + local function isint(n) + return n==math.floor(n) + end - local function get_amount(_) - if isint(recipe[_][1]) then - return recipe[_][1] - else return 1 end - end + local function get_amount(_) + if isint(recipe[_][1]) then + return recipe[_][1] + else return 1 end + end - local function get_type(_) - if type(recipe[_][2]) == "string" then - return recipe[_][2] - end - end + local function get_type(_) + if type(recipe[_][2]) == "string" then + return recipe[_][2] + end + end - local function register(_) - local def = { - type = get_type(_), - output = output.." "..tostring(get_amount(_)), - recipe = recipe[_][3] or recipe[_][2] - } - minetest.register_craft(def) - end + local function register(_) + local def = { + type = get_type(_), + output = output.." "..tostring(get_amount(_)), + recipe = recipe[_][3] or recipe[_][2] + } + minetest.register_craft(def) + end - for _, i in ipairs(recipe) do - -- Check if disabled - if recipe.disabled == true then - return - end + for _, i in ipairs(recipe) do + -- Check if disabled + if recipe.disabled == true then + return + end - register(_) - end + register(_) + end end -- [function] Register oredef function microexpansion.register_oredef(ore, def) - -- Check if disabled - if def.disabled == true then - return - end + -- Check if disabled + if def.disabled == true then + return + end - local function register(_) - local def = def[_] - def.ore = "microexpansion:"..ore - minetest.register_ore(def) - end + local function register(_) + local def = def[_] + def.ore = "microexpansion:"..ore + minetest.register_ore(def) + end - for _, i in ipairs(def) do - -- Check if disabled - if def.disabled == true then - return - end + for _, i in ipairs(def) do + -- Check if disabled + if def.disabled == true then + return + end - register(_) - end + register(_) + end end -- [local function] Choose description colour local function desc_colour(status, desc) - if status == "unstable" then - return minetest.colorize("orange", desc) - elseif status == "no" then - return minetest.colorize("red", desc) - else - return minetest.colorize("white", desc) - end + if status == "unstable" then + return minetest.colorize("orange", desc) + elseif status == "no" then + return minetest.colorize("red", desc) + else + return minetest.colorize("white", desc) + end end -- [function] Register Item function microexpansion.register_item(itemstring, def) - -- Check if disabled - if def.disabled == true then - return - end - -- Set usedfor - if def.usedfor then - def.description = def.description .. "\n"..minetest.colorize("grey", def.usedfor) - end - -- Update inventory image - if def.inventory_image then - def.inventory_image = BASENAME.."_"..def.inventory_image..".png" - else - def.inventory_image = BASENAME.."_"..itemstring..".png" - end - -- Colour description - def.description = desc_colour(def.status, def.description) + -- Check if disabled + if def.disabled == true then + return + end + -- Set usedfor + if def.usedfor then + def.description = def.description .. "\n"..minetest.colorize("grey", def.usedfor) + end + -- Update inventory image + if def.inventory_image then + def.inventory_image = BASENAME.."_"..def.inventory_image..".png" + else + def.inventory_image = BASENAME.."_"..itemstring..".png" + end + -- Colour description + def.description = desc_colour(def.status, def.description) - -- Register craftitem - minetest.register_craftitem(BASENAME..":"..itemstring, def) + -- Register craftitem + minetest.register_craftitem(BASENAME..":"..itemstring, def) - -- if recipe, Register recipe - if def.recipe then - microexpansion.register_recipe(BASENAME..":"..itemstring, def.recipe) - end + -- if recipe, Register recipe + if def.recipe then + microexpansion.register_recipe(BASENAME..":"..itemstring, def.recipe) + end end -- [function] Register Node function microexpansion.register_node(itemstring, def) - -- Check if disabled - if def.disabled == true then - return - end - -- Set usedfor - if def.usedfor then - def.description = def.description .. "\n"..minetest.colorize("grey", def.usedfor) - end - -- Update texture - if auto_complete ~= false then - for _,i in ipairs(def.tiles) do - if #def.tiles[_]:split("^") <= 1 then - local prefix = "" - if def.type == "ore" then - prefix = "ore_" - end + -- Check if disabled + if def.disabled == true then + return + end + -- Set usedfor + if def.usedfor then + def.description = def.description .. "\n"..minetest.colorize("grey", def.usedfor) + end + -- Update texture + if auto_complete ~= false then + for _,i in ipairs(def.tiles) do + if #def.tiles[_]:split("^") <= 1 then + local prefix = "" + if def.type == "ore" then + prefix = "ore_" + end - def.tiles[_] = BASENAME.."_"..prefix..i..".png" - end - end - end - -- Colour description - def.description = desc_colour(def.status, def.description) - -- Update connect_sides - if def.connect_sides == "nobottom" then - def.connect_sides = { "top", "front", "left", "back", "right" } - elseif def.connect_sides == "machine" then - def.connect_sides = { "top", "bottom", "left", "back", "right" } - end + def.tiles[_] = BASENAME.."_"..prefix..i..".png" + end + end + end + -- Colour description + def.description = desc_colour(def.status, def.description) + -- Update connect_sides + if def.connect_sides == "nobottom" then + def.connect_sides = { "top", "front", "left", "back", "right" } + elseif def.connect_sides == "machine" then + def.connect_sides = { "top", "bottom", "left", "back", "right" } + end - -- Register node - minetest.register_node(BASENAME..":"..itemstring, def) + -- Register node + minetest.register_node(BASENAME..":"..itemstring, def) - -- if recipe, Register recipe - if def.recipe then - microexpansion.register_recipe(BASENAME..":"..itemstring, def.recipe) - end + -- if recipe, Register recipe + if def.recipe then + microexpansion.register_recipe(BASENAME..":"..itemstring, def.recipe) + end - -- if oredef, Register oredef - if def.oredef then - microexpansion.register_oredef(BASENAME..":"..itemstring, def.oredef) - end + -- if oredef, Register oredef + if def.oredef then + microexpansion.register_oredef(BASENAME..":"..itemstring, def.oredef) + end end diff --git a/init.lua b/init.lua index 0a7130d..0863564 100644 --- a/init.lua +++ b/init.lua @@ -1,7 +1,11 @@ -- microexpansion/init.lua -microexpansion = {} -microexpansion.modpath = minetest.get_modpath("microexpansion") -- modpath -local modpath = microexpansion.modpath -- modpath pointer +microexpansion = {} +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,14 +13,45 @@ microexpansion.gui_slots = "listcolors[#00000069;#5A5A5A;#141318;#30434C;#FFF]" -- logger function microexpansion.log(content, log_type) - if not content then return false end - if log_type == nil then log_type = "action" end - minetest.log(log_type, "[MicroExpansion] "..content) + 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) 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 ----- ------------------- @@ -27,40 +62,40 @@ local settings = Settings(modpath.."/modules.conf"):to_table() -- [function] Get module path function microexpansion.get_module_path(name) - local module_path = modpath.."/modules/"..name + local module_path = modpath.."/modules/"..name - if io.open(module_path.."/init.lua") then - return module_path - end + if io.open(module_path.."/init.lua") then + return module_path + end end -- [function] Load module (overrides modules.conf) function microexpansion.load_module(name) - if loaded_modules[name] ~= false then - local module_init = microexpansion.get_module_path(name).."/init.lua" + if loaded_modules[name] ~= false then + local module_init = microexpansion.get_module_path(name).."/init.lua" - if module_init then - dofile(module_init) - loaded_modules[name] = true - return true - else - microexpansion.log("Invalid module \""..name.."\". The module either does not exist ".. - "or is missing an init.lua file.", "error") - end - else - return true - end + if module_init then + dofile(module_init) + loaded_modules[name] = true + return true + else + microexpansion.log("Invalid module \""..name.."\". The module either does not exist ".. + "or is missing an init.lua file.", "error") + end + else + return true + end end -- [function] Require module (does not override modules.conf) function microexpansion.require_module(name) - if settings[name] and settings[name] ~= false then - return microexpansion.load_module(name) - end + if settings[name] and settings[name] ~= false then + return microexpansion.load_module(name) + end end for name,enabled in pairs(settings) do - if enabled ~= false then - microexpansion.load_module(name) - end + if enabled ~= false then + microexpansion.load_module(name) + end end diff --git a/modules/ores/init.lua b/modules/ores/init.lua index d06965e..89296e6 100644 --- a/modules/ores/init.lua +++ b/modules/ores/init.lua @@ -4,42 +4,42 @@ local me = microexpansion -- [register] Incranium Ore me.register_node("incranium", { - description = "Incranium Ore", - tiles = { "incranium" }, - is_ground_content = true, - groups = { cracky=3, stone=1 }, - type = "ore", - oredef = { - { - ore_type = "blob", - wherein = "default:stone", - clust_scarcity = 4*4*4, - clust_num_ores = 4, - clust_size = 3, - y_min = -300, - y_max = -90, - }, - }, - disabled = true, + description = "Incranium Ore", + tiles = { "incranium" }, + is_ground_content = true, + groups = { cracky=3, stone=1 }, + type = "ore", + oredef = { + { + ore_type = "blob", + wherein = "default:stone", + clust_scarcity = 4*4*4, + clust_num_ores = 4, + clust_size = 3, + y_min = -300, + y_max = -90, + }, + }, + disabled = true, }) -- "Supernatet", pronounced "Super-nat-et" is Latin for "float", this ore will -- float up if there are no blocks above it, so be careful! -- Supernatet ore will be used to craft wings of flight me.register_node("supernatet", { - description = "Supernatant Ore", - tiles = { "default_stone.png^microexpansion_ore_supernatet.png" }, - is_ground_content = true, - type = "ore", - groups = { cracky=3, stone=1 }, - oredef = { - ore_type = "blob", - wherein = "default:stone", - clust_scarcity = 4*4*4, - clust_num_ores = 4, - clust_size = 3, - y_min = -300, - y_max = -90, - }, - status = "unstable", + description = "Supernatant Ore", + tiles = { "default_stone.png^microexpansion_ore_supernatet.png" }, + is_ground_content = true, + type = "ore", + groups = { cracky=3, stone=1 }, + oredef = { + ore_type = "blob", + wherein = "default:stone", + clust_scarcity = 4*4*4, + clust_num_ores = 4, + clust_size = 3, + y_min = -300, + y_max = -90, + }, + status = "unstable", }) diff --git a/modules/power/ctrl.lua b/modules/power/ctrl.lua index f6ca995..9b16943 100644 --- a/modules/power/ctrl.lua +++ b/modules/power/ctrl.lua @@ -4,8 +4,8 @@ local me = microexpansion -- [register node] Controller me.register_node("ctrl", { - description = "Power Controller", - tiles = { + description = "Power Controller", + tiles = { "ctrl_sides", "ctrl_bottom", "ctrl_sides", @@ -13,8 +13,8 @@ me.register_node("ctrl", { "ctrl_sides", "ctrl_sides" }, - drawtype = "nodebox", - paramtype = "light", + drawtype = "nodebox", + paramtype = "light", node_box = { type = "fixed", fixed = { @@ -31,29 +31,66 @@ me.register_node("ctrl", { {-0.1875, -0.5, -0.1875, 0.1875, -0.25, 0.1875}, -- Bottom2 }, }, - groups = { cracky = 1, me_connect = 1, }, - connect_sides = "nobottom", - status = "no", + groups = { cracky = 1, me_connect = 1, }, + connect_sides = "nobottom", + 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", { - description = "ME Cable", - tiles = { - "cable", - }, - drawtype = "nodebox", - node_box = { - type = "connected", - fixed = {-0.25, -0.25, -0.25, 0.25, 0.25, 0.25}, +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}, 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+ - }, - connects_to = {"group:me_connect"}, - groups = { crumbly = 1, me_connect = 1, }, - status = "no", + }, + paramtype = "light", + groups = { crumbly = 1, }, + status = "unstable", + machine = { + type = "transporter", + }, }) diff --git a/modules/power/gen.lua b/modules/power/gen.lua index c80077e..8a0e812 100644 --- a/modules/power/gen.lua +++ b/modules/power/gen.lua @@ -3,34 +3,41 @@ local me = microexpansion -- [register node] Fuel Fired Generator -me.register_node("fuel_fired_generator", { - description = "Fuel-Fired Generator", - tiles = { - "machine_sides", +me.register_machine("fuel_fired_generator", { + description = "Fuel-Fired Generator", + tiles = { + "machine_sides", + "machine_sides", "machine_sides", "machine_sides", "machine_sides", "machine_sides", "fuelgen_front", - }, - recipe = { - { 1, { - { "default:steel_ingot", "default:furnace", "default:steel_ingot" }, - {"default:steel_ingot", "microexpansion:machine_casing", "default:steel_ingot" }, - { "default:steel_ingot", "default:steel_ingot", "default:steel_ingot" }, - }, - } - }, - groups = { cracky = 1, me_connect = 1, }, - connect_sides = "machine", - paramtype2 = "facedir", - status = "no", + }, + recipe = { + { 1, { + { "default:steel_ingot", "default:furnace", "default:steel_ingot" }, + {"default:steel_ingot", "microexpansion:machine_casing", "default:steel_ingot" }, + { "default:steel_ingot", "default:steel_ingot", "default:steel_ingot" }, + }, + } + }, + groups = { cracky = 1 }, + connect_sides = "machine", + paramtype2 = "facedir", + status = "unstable", + machine = { + type = "provider", + on_survey = function(pos) + return 5 -- Generate 5 ME/tick + end, + }, }) -- [register node] Super Smelter me.register_node("super_smelter", { - description = "Super Smelter", - tiles = { + description = "Super Smelter", + tiles = { "machine_sides", "machine_sides", "machine_sides", @@ -38,24 +45,30 @@ me.register_node("super_smelter", { "machine_sides", "super_smelter_front", }, - recipe = { - { 1, { - { "default:furnace", "default:furnace", "default:furnace" }, - { "default:steel_ingot", "microexpansion:machine_casing", "default:steel_ingot" }, - { "default:steel_ingot", "default:steel_ingot", "default:steel_ingot" }, - }, - }, - }, - groups = { cracky = 1, me_connect = 1, }, - connect_sides = "machine", - paramtype2 = "facedir", - status = "no", + recipe = { + { 1, { + { "default:furnace", "default:furnace", "default:furnace" }, + { "default:steel_ingot", "microexpansion:machine_casing", "default:steel_ingot" }, + { "default:steel_ingot", "default:steel_ingot", "default:steel_ingot" }, + }, + }, + }, + groups = { cracky = 1, me_connect = 1, }, + connect_sides = "machine", + paramtype2 = "facedir", + status = "unstable", + machine = { + type = "consumer", + on_survey = function(pos) + return 5 -- Consume 5 ME/tick + end, + }, }) -- [register item] Geothermal Generator me.register_node("geo_generator", { - description = "Geothermal Generator", - tiles = { + description = "Geothermal Generator", + tiles = { "machine_sides", "machine_sides", "machine_sides", @@ -63,8 +76,14 @@ me.register_node("geo_generator", { "machine_sides", "geogen_front", }, - groups = { cracky = 1, me_connect = 1, }, - connect_sides = "machine", - paramtype2 = "facedir", - status = "no", + groups = { cracky = 1, me_connect = 1, }, + connect_sides = "machine", + paramtype2 = "facedir", + status = "unstable", + machine = { + type = "provider", + on_survey = function(pos) + return 10 -- Generate 10 ME/tick + end, + }, }) diff --git a/modules/power/init.lua b/modules/power/init.lua index f2db993..e5ce41a 100644 --- a/modules/power/init.lua +++ b/modules/power/init.lua @@ -1,10 +1,16 @@ -- power/init.lua -local path = microexpansion.get_module_path("power") +local me = microexpansion --- Power generators, wires, etc... +local networks = me.networks +local path = microexpansion.get_module_path("power") + +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 diff --git a/modules/power/network.lua b/modules/power/network.lua new file mode 100644 index 0000000..6d70146 --- /dev/null +++ b/modules/power/network.lua @@ -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 ---- diff --git a/modules/power/register.lua b/modules/power/register.lua new file mode 100644 index 0000000..ebb2d10 --- /dev/null +++ b/modules/power/register.lua @@ -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 diff --git a/modules/shared/init.lua b/modules/shared/init.lua index b68bec4..87e1256 100644 --- a/modules/shared/init.lua +++ b/modules/shared/init.lua @@ -7,24 +7,24 @@ local me = microexpansion -- [register item] Steel Infused Obsidian Ingot me.register_item("steel_infused_obsidian_ingot", { - description = "Steel Infused Obsidian Ingot", - recipe = { - { 1, { - { "default:steel_ingot", "default:obsidian_shard", "default:steel_ingot" }, - }, - }, - }, + description = "Steel Infused Obsidian Ingot", + recipe = { + { 1, { + { "default:steel_ingot", "default:obsidian_shard", "default:steel_ingot" }, + }, + }, + }, }) -- [register item] Machine Casing me.register_item("machine_casing", { - description = "Machine Casing", - recipe = { - { 1, { - {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"}, - {"default:steel_ingot", "default:copper_ingot", "default:steel_ingot"}, - {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"}, - }, - }, - }, + description = "Machine Casing", + recipe = { + { 1, { + {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"}, + {"default:steel_ingot", "default:copper_ingot", "default:steel_ingot"}, + {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"}, + }, + }, + }, }) diff --git a/modules/storage/api.lua b/modules/storage/api.lua index 999d616..05c8ef8 100644 --- a/modules/storage/api.lua +++ b/modules/storage/api.lua @@ -4,97 +4,97 @@ local BASENAME = "microexpansion" -- [function] register cell function microexpansion.register_cell(itemstring, def) - if not def.inventory_image then - def.inventory_image = itemstring - end + if not def.inventory_image then + def.inventory_image = itemstring + end - -- register craftitem - minetest.register_craftitem(BASENAME..":"..itemstring, { - description = def.description, - inventory_image = BASENAME.."_"..def.inventory_image..".png", - groups = {microexpansion_cell = 1}, - stack_max = 1, - microexpansion = { - base_desc = def.description, - drive = { - capacity = def.capacity or 5000, - }, - }, - }) + -- register craftitem + minetest.register_craftitem(BASENAME..":"..itemstring, { + description = def.description, + inventory_image = BASENAME.."_"..def.inventory_image..".png", + groups = {microexpansion_cell = 1}, + stack_max = 1, + microexpansion = { + base_desc = def.description, + drive = { + capacity = def.capacity or 5000, + }, + }, + }) - -- if recipe, register recipe - if def.recipe then - -- if recipe, register recipe - if def.recipe then - microexpansion.register_recipe(BASENAME..":"..itemstring, def.recipe) - end - end + -- if recipe, register recipe + if def.recipe then + -- if recipe, register recipe + if def.recipe then + microexpansion.register_recipe(BASENAME..":"..itemstring, def.recipe) + end + end end -- [function] Get cell size function microexpansion.get_cell_size(name) - local item = minetest.registered_craftitems[name] - if item then - return item.microexpansion.drive.capacity - end + local item = minetest.registered_craftitems[name] + if item then + return item.microexpansion.drive.capacity + end end -- [function] Calculate max stacks function microexpansion.int_to_stacks(int) - return math.floor(int / 99) + return math.floor(int / 99) end -- [function] Calculate number of pages function microexpansion.int_to_pagenum(int) - return math.floor(microexpansion.int_to_stacks(int) / 32) + return math.floor(microexpansion.int_to_stacks(int) / 32) end -- [function] Move items from inv to inv function microexpansion.move_inv(inv1, inv2) - local finv, tinv = inv1.inv, inv2.inv - local fname, tname = inv1.name, inv2.name + local finv, tinv = inv1.inv, inv2.inv + local fname, tname = inv1.name, inv2.name - for i,v in ipairs(finv:get_list(fname) or {}) do - if tinv and tinv:room_for_item(tname, v) then - local leftover = tinv:add_item( tname, v ) - finv:remove_item(fname, v) - if leftover and not(leftover:is_empty()) then - finv:add_item(fname, v) - end - end - end + for i,v in ipairs(finv:get_list(fname) or {}) do + if tinv and tinv:room_for_item(tname, v) then + local leftover = tinv:add_item( tname, v ) + finv:remove_item(fname, v) + if leftover and not(leftover:is_empty()) then + finv:add_item(fname, v) + end + end + end end -- [function] Update cell description function microexpansion.cell_desc(inv, listname, spos) - local stack = inv:get_stack(listname, spos) + local stack = inv:get_stack(listname, spos) - if stack:get_name() ~= "" then - local meta = stack:get_meta() - local base_desc = minetest.registered_craftitems[stack:get_name()].microexpansion.base_desc - local max_slots = inv:get_size("main") - local max_items = math.floor(max_slots * 99) + if stack:get_name() ~= "" then + local meta = stack:get_meta() + local base_desc = minetest.registered_craftitems[stack:get_name()].microexpansion.base_desc + local max_slots = inv:get_size("main") + local max_items = math.floor(max_slots * 99) - local slots, items = 0, 0 - -- Get amount of items in drive - for i = 1, max_items do - local stack = inv:get_stack("main", i) - local item = stack:get_name() - if item ~= "" then - slots = slots + 1 - local num = stack:get_count() - if num == 0 then num = 1 end - items = items + num - end - end + local slots, items = 0, 0 + -- Get amount of items in drive + for i = 1, max_items do + local stack = inv:get_stack("main", i) + local item = stack:get_name() + if item ~= "" then + slots = slots + 1 + local num = stack:get_count() + if num == 0 then num = 1 end + items = items + num + end + end - -- Calculate Percentage - local percent = math.floor(items / max_items * 100) + -- Calculate Percentage + local percent = math.floor(items / max_items * 100) - -- Update description - meta:set_string("description", base_desc.."\n".. - minetest.colorize("grey", tostring(items).."/"..tostring(max_items).." Items ("..tostring(percent).."%)")) - -- Update stack - inv:set_stack(listname, spos, stack) + -- Update description + meta:set_string("description", base_desc.."\n".. + minetest.colorize("grey", tostring(items).."/"..tostring(max_items).." Items ("..tostring(percent).."%)")) + -- Update stack + inv:set_stack(listname, spos, stack) end end diff --git a/modules/storage/storage.lua b/modules/storage/storage.lua index 70badf7..0bc3b76 100644 --- a/modules/storage/storage.lua +++ b/modules/storage/storage.lua @@ -2,24 +2,24 @@ -- [drive] 8k microexpansion.register_cell("cell_8k", { - description = "8k ME Storage Cell", - capacity = 8000, + description = "8k ME Storage Cell", + capacity = 8000, }) -- [drive] 16k microexpansion.register_cell("cell_16k", { - description = "16k ME Storage Cell", - capacity = 16000, + description = "16k ME Storage Cell", + capacity = 16000, }) -- [drive] 32k microexpansion.register_cell("cell_32k", { - description = "32k ME Storage Cell", - capacity = 32000, + description = "32k ME Storage Cell", + capacity = 32000, }) -- [drive] 64k microexpansion.register_cell("cell_64k", { - description = "64k ME Storage Cell", - capacity = 64000, + description = "64k ME Storage Cell", + capacity = 64000, }) diff --git a/modules/tools/xtremo.lua b/modules/tools/xtremo.lua index a613367..91af0e7 100644 --- a/modules/tools/xtremo.lua +++ b/modules/tools/xtremo.lua @@ -2,9 +2,9 @@ -- [register tool] Pickaxe minetest.register_tool("microexpansion:xtremo_pickaxe", { - description = "In Xtremo Pickaxe", - inventory_image = "microexpansion_in_xtremo_pickaxe.png", - tool_capabilities = { + description = "In Xtremo Pickaxe", + inventory_image = "microexpansion_in_xtremo_pickaxe.png", + tool_capabilities = { full_punch_interval = 0.9, max_drop_level=3, groupcaps={ @@ -16,9 +16,9 @@ minetest.register_tool("microexpansion:xtremo_pickaxe", { -- [register tool] Axe minetest.register_tool("microexpansion:xtremo_axe", { - description = "In Xtremo Axe", - inventory_image = "microexpansion_in_xtremo_axe.png", - tool_capabilities = { + description = "In Xtremo Axe", + inventory_image = "microexpansion_in_xtremo_axe.png", + tool_capabilities = { full_punch_interval = 0.9, max_drop_level=3, groupcaps={