From c3b49b68e12365c9ff6095075c93061d6ffde802 Mon Sep 17 00:00:00 2001 From: mrkubax10 Date: Mon, 13 Nov 2023 20:37:50 +0100 Subject: [PATCH] Generate network map while constructing generator for testing --- api.lua | 20 ++++++++++++++++-- cables.lua | 46 +++--------------------------------------- machines/common.lua | 37 ++++++++++++++++++++++++++++++++- machines/generator.lua | 1 + 4 files changed, 58 insertions(+), 46 deletions(-) diff --git a/api.lua b/api.lua index 07df30b..436b29d 100644 --- a/api.lua +++ b/api.lua @@ -269,7 +269,7 @@ local function addNodeToNetwork(pos,networkMasterPos) networks=minetest.deserialize(meta:get_string("industrialtest.networks")) end for _,network in ipairs(networks) do - if network==networkMasterPos then + if network.x==networkMasterPos.x and network.y==networkMasterPos.y and network.z==networkMasterPos.z then return end end @@ -308,7 +308,7 @@ industrialtest.api.createNetworkMap=function(pos) while #workers>0 do for i=1,#workers do local worker=workers[i] - local connections=industrialtest.api.getConnections(worker.position,"i") + connections=industrialtest.api.getConnections(worker.position,"i") if #connections==0 then table.remove(workers,i) break @@ -355,6 +355,22 @@ industrialtest.api.createNetworkMap=function(pos) return map end +industrialtest.api.removeNodeFromNetwork=function(pos,nodePos) + local meta=minetest.get_meta(pos) + local network=minetest.deserialize(meta:get_string("industrialtest.network")) + local removed=false + for key,node in ipairs(network) do + if node.position.x==nodePos.x and node.position.y==nodePos.y and node.position.z==nodePos.z then + table.remove(network,key) + removed=true + break + end + end + if removed then + meta:set_string("industrialtest.network",minetest.serialize(network)) + end +end + -- \brief Creates network map and writes it to node metadata at pos -- \param pos Vector -- \returns nil diff --git a/cables.lua b/cables.lua index 93078b4..83850bb 100644 --- a/cables.lua +++ b/cables.lua @@ -86,47 +86,9 @@ local function registerCable(name,displayName,size,flow,registerInsulated) }, connects_to={ "group:_industrialtest_hasPowerInput", - "group:_industrialtest_hasPowerOutput" - }, - on_construct=function(pos) - local meta=minetest.get_meta(pos) - industrialtest.api.addPowerStorage(meta,flow,flow,"aaaaaa") - end, - on_timer=function(pos) - local meta=minetest.get_meta(pos) - local afterFlow,transferred=industrialtest.api.powerFlow(pos) - meta:set_string("industrialtest.ioConfig","aaaaaa") - if not industrialtest.api.isFullyCharged(meta) then - industrialtest.api.triggerNeighbours(pos) - end - if transferred then - local node=minetest.get_node(pos) - local def=minetest.registered_nodes[node.name] - if def._industrialtest_electrocution then - local players=minetest.get_connected_players() - for _,value in ipairs(players) do - -- Note: don't use vector.distance here because we don't need actual distance between two - -- vectors to determine if player is within range if we use squared range - local playerPos=value:get_pos() - local dx=pos.x-playerPos.x - local dy=pos.y-playerPos.y - local dz=pos.z-playerPos.z - local dist=math.pow(dx,2)+math.pow(dy,2)+math.pow(dz,2) - if dist<=0.60 then - local hp=value:get_hp() - value:set_hp(hp-0.5,"Electrocution") - end - end - end - end - return (afterFlow and meta:get_int("industrialtest.powerAmount")>0) - end, - _industrialtest_electrocution=true, - _industrialtest_onPowerFlow=function(pos,side) - local meta=minetest.get_meta(pos) - industrialtest.api.changeIoConfig(meta,side,"i") - minetest.get_node_timer(pos):start(industrialtest.updateDelay) - end, + "group:_industrialtest_hasPowerOutput", + "group:_industrialtest_cable" + } } if industrialtest.mtgAvailable then definition.groups={ @@ -141,7 +103,6 @@ local function registerCable(name,displayName,size,flow,registerInsulated) definition._mcl_hardness=0.5 definition.sound=mcl_sounds.node_sound_metal_defaults() end - definition.groups._industrialtest_hasPowerInput=1 definition.groups._industrialtest_cable=1 minetest.register_node("industrialtest:"..name.."_cable",definition) if registerInsulated then @@ -150,7 +111,6 @@ local function registerCable(name,displayName,size,flow,registerInsulated) definition.inventory_image="industrialtest_insulated_"..name.."_cable_inv.png" definition.tiles={"industrialtest_insulated_"..name.."_cable.png"} definition.wield_image="industrialtest_insulated_"..name.."_cable_inv.png" - definition._industrialtest_electrocution=nil minetest.register_node("industrialtest:insulated_"..name.."_cable",definition) end end diff --git a/machines/common.lua b/machines/common.lua index 1b05317..138e5cd 100644 --- a/machines/common.lua +++ b/machines/common.lua @@ -61,14 +61,48 @@ end machine.onConstruct=function(pos,config) local meta=minetest.get_meta(pos) local inv=meta:get_inventory() + + industrialtest.api.addPowerStorage(meta,config.capacity,config.flow,config.ioConfig) meta:set_string("formspec",machine.getFormspec(pos,config)) + + if config.groups and config.groups._industrialtest_hasPowerInput then + local connections=industrialtest.api.getConnections(pos) + for _,conn in ipairs(connections) do + local connectionMeta=minetest.get_meta(conn) + if industrialtest.api.isNetworkMaster(connectionMeta) then + industrialtest.api.createNetworkMapForNode(conn) + else + local def=minetest.registered_nodes[minetest.get_node(conn).name] + if def.groups._industrialtest_cable then + local networks=industrialtest.api.isAttachedToNetwork(connectionMeta) + if networks then + for _,network in ipairs(networks) do + industrialtest.api.createNetworkMapForNode(network) + minetest.get_node_timer(network):start(industrialtest.updateDelay) + end + end + end + end + end + end + if config.onConstruct then config.onConstruct(pos,meta,inv) end - industrialtest.api.addPowerStorage(meta,config.capacity,config.flow,config.ioConfig) + minetest.get_node_timer(pos):start(industrialtest.updateDelay) end +machine.onDestruct=function(pos) + local meta=minetest.get_meta(pos) + local networks=industrialtest.api.isAttachedToNetwork(meta) + if networks then + for _,network in ipairs(networks) do + industrialtest.api.removeNodeFromNetwork(network,pos) + end + end +end + machine.onTimer=function(pos,elapsed,config) local meta=minetest.get_meta(pos) local inv=meta:get_inventory() @@ -138,6 +172,7 @@ function industrialtest.internal.registerMachine(config) on_construct=function(pos) machine.onConstruct(pos,config) end, + on_destruct=machine.onDestruct, on_timer=function(pos,elapsed) return machine.onTimer(pos,elapsed,config) end, diff --git a/machines/generator.lua b/machines/generator.lua index 5cdc291..4d9eaaa 100644 --- a/machines/generator.lua +++ b/machines/generator.lua @@ -55,6 +55,7 @@ generator.onConstruct=function(pos,meta,inv) inv:set_size("fuel",1) meta:set_int("fuelTime",0) meta:set_int("maxFuelTime",1) + meta:set_string("industrialtest.network",minetest.serialize(industrialtest.api.createNetworkMap(pos))) end generator.onTimer=function(pos,elapsed,meta,inv)