summaryrefslogtreecommitdiff
path: root/technic/machines
diff options
context:
space:
mode:
Diffstat (limited to 'technic/machines')
-rw-r--r--technic/machines/HV/quarry.lua268
-rw-r--r--technic/machines/switching_station.lua85
2 files changed, 51 insertions, 302 deletions
diff --git a/technic/machines/HV/quarry.lua b/technic/machines/HV/quarry.lua
index c1ee1bb..e69de29 100644
--- a/technic/machines/HV/quarry.lua
+++ b/technic/machines/HV/quarry.lua
@@ -1,268 +0,0 @@
-
-local S = technic.getter
-
-local tube_entry = "^pipeworks_tube_connection_metallic.png"
-local cable_entry = "^technic_cable_connection_overlay.png"
-
-minetest.register_craft({
- recipe = {
- {"technic:carbon_plate", "pipeworks:filter", "technic:composite_plate"},
- {"technic:motor", "technic:machine_casing", "technic:diamond_drill_head"},
- {"technic:carbon_steel_block", "technic:hv_cable", "technic:carbon_steel_block"}},
- output = "technic:quarry",
-})
-
-local quarry_dig_above_nodes = 3 -- How far above the quarry we will dig nodes
-local quarry_max_depth = 100
-local quarry_demand = 10000
-local quarry_eject_dir = vector.new(0, 1, 0)
-
-local function set_quarry_formspec(meta)
- local radius = meta:get_int("size")
- local formspec = "size[6,4.3]"..
- "list[context;cache;0,1;4,3;]"..
- "item_image[4.8,0;1,1;technic:quarry]"..
- "label[0,0.2;"..S("%s Quarry"):format("HV").."]"..
- "field[4.3,3.5;2,1;size;"..S("Radius:")..";"..radius.."]"
- if meta:get_int("enabled") == 0 then
- formspec = formspec.."button[4,1;2,1;enable;"..S("Disabled").."]"
- else
- formspec = formspec.."button[4,1;2,1;disable;"..S("Enabled").."]"
- end
- local diameter = radius*2 + 1
- local nd = meta:get_int("dug")
- local rel_y = quarry_dig_above_nodes - math.floor(nd / (diameter*diameter))
- formspec = formspec.."label[0,4;"..minetest.formspec_escape(
- nd == 0 and S("Digging not started") or
- (rel_y < -quarry_max_depth and S("Digging finished") or
- (meta:get_int("purge_on") == 1 and S("Purging cache") or
- S("Digging %d m "..(rel_y > 0 and "above" or "below").." machine")
- :format(math.abs(rel_y))))
- ).."]"
- formspec = formspec.."button[4,2;2,1;restart;"..S("Restart").."]"
- meta:set_string("formspec", formspec)
-end
-
-local function set_quarry_demand(meta)
- local radius = meta:get_int("size")
- local diameter = radius*2 + 1
- local machine_name = S("%s Quarry"):format("HV")
- if meta:get_int("enabled") == 0 or meta:get_int("purge_on") == 1 then
- meta:set_string("infotext", S(meta:get_int("purge_on") == 1 and "%s purging cache" or "%s Disabled"):format(machine_name))
- meta:set_int("HV_EU_demand", 0)
- elseif meta:get_int("dug") == diameter*diameter * (quarry_dig_above_nodes+1+quarry_max_depth) then
- meta:set_string("infotext", S("%s Finished"):format(machine_name))
- meta:set_int("HV_EU_demand", 0)
- else
- meta:set_string("infotext", S(meta:get_int("HV_EU_input") >= quarry_demand and "%s Active" or "%s Unpowered"):format(machine_name))
- meta:set_int("HV_EU_demand", quarry_demand)
- end
-end
-
-local function quarry_receive_fields(pos, formname, fields, sender)
- local meta = minetest.get_meta(pos)
- if fields.size and string.find(fields.size, "^[0-9]+$") then
- local size = tonumber(fields.size)
- if size >= 2 and size <= 8 and size ~= meta:get_int("size") then
- meta:set_int("size", size)
- meta:set_int("dug", 0)
- end
- end
- if fields.enable then meta:set_int("enabled", 1) end
- if fields.disable then meta:set_int("enabled", 0) end
- if fields.restart then
- meta:set_int("dug", 0)
- meta:set_int("purge_on", 1)
- end
- set_quarry_formspec(meta)
- set_quarry_demand(meta)
-end
-
-local function quarry_handle_purge(pos)
- local meta = minetest.get_meta(pos)
- local inv = meta:get_inventory()
- local i = 0
- for _,stack in ipairs(inv:get_list("cache")) do
- i = i + 1
- if stack then
- local item = stack:to_table()
- if item then
- technic.tube_inject_item(pos, pos, quarry_eject_dir, item)
- stack:clear()
- inv:set_stack("cache", i, stack)
- break
- end
- end
- end
- if inv:is_empty("cache") then
- meta:set_int("purge_on", 0)
- end
-end
-
-local function quarry_run(pos, node)
- local meta = minetest.get_meta(pos)
- local inv = meta:get_inventory()
- -- initialize cache for the case we load an older world
- inv:set_size("cache", 12)
- -- toss a coin whether we do an automatic purge. Chance 1:200
- local purge_rand = math.random()
- if purge_rand <= 0.005 then
- meta:set_int("purge_on", 1)
- end
-
- if meta:get_int("enabled") and meta:get_int("HV_EU_input") >= quarry_demand and meta:get_int("purge_on") == 0 then
- local pdir = minetest.facedir_to_dir(node.param2)
- local qdir = pdir.x == 1 and vector.new(0,0,-1) or
- (pdir.z == -1 and vector.new(-1,0,0) or
- (pdir.x == -1 and vector.new(0,0,1) or
- vector.new(1,0,0)))
- local radius = meta:get_int("size")
- local diameter = radius*2 + 1
- local startpos = vector.add(vector.add(vector.add(pos,
- vector.new(0, quarry_dig_above_nodes, 0)),
- pdir),
- vector.multiply(qdir, -radius))
- local endpos = vector.add(vector.add(vector.add(startpos,
- vector.new(0, -quarry_dig_above_nodes-quarry_max_depth, 0)),
- vector.multiply(pdir, diameter-1)),
- vector.multiply(qdir, diameter-1))
- local vm = VoxelManip()
- local minpos, maxpos = vm:read_from_map(startpos, endpos)
- local area = VoxelArea:new({MinEdge=minpos, MaxEdge=maxpos})
- local data = vm:get_data()
- local c_air = minetest.get_content_id("air")
- local owner = meta:get_string("owner")
- local nd = meta:get_int("dug")
- while nd ~= diameter*diameter * (quarry_dig_above_nodes+1+quarry_max_depth) do
- local ry = math.floor(nd / (diameter*diameter))
- local ndl = nd % (diameter*diameter)
- if ry % 2 == 1 then
- ndl = diameter*diameter - 1 - ndl
- end
- local rq = math.floor(ndl / diameter)
- local rp = ndl % diameter
- if rq % 2 == 1 then rp = diameter - 1 - rp end
- local digpos = vector.add(vector.add(vector.add(startpos,
- vector.new(0, -ry, 0)),
- vector.multiply(pdir, rp)),
- vector.multiply(qdir, rq))
- local can_dig = true
- if can_dig and minetest.is_protected and minetest.is_protected(digpos, owner) then
- can_dig = false
- end
- local dignode
- if can_dig then
- dignode = technic.get_or_load_node(digpos) or minetest.get_node(digpos)
- local dignodedef = minetest.registered_nodes[dignode.name] or {diggable=false}
- if not dignodedef.diggable or (dignodedef.can_dig and not dignodedef.can_dig(digpos, nil)) then
- can_dig = false
- end
- end
-
- if can_dig then
- for ay = startpos.y, digpos.y+1, -1 do
- local checkpos = {x=digpos.x, y=ay, z=digpos.z}
- local checknode = technic.get_or_load_node(checkpos) or minetest.get_node(checkpos)
- if checknode.name ~= "air" then
- can_dig = false
- break
- end
- end
- end
- nd = nd + 1
- if can_dig then
- minetest.remove_node(digpos)
- local drops = minetest.get_node_drops(dignode.name, "")
- for _, dropped_item in ipairs(drops) do
- local left = inv:add_item("cache", dropped_item)
- while not left:is_empty() do
- meta:set_int("purge_on", 1)
- quarry_handle_purge(pos)
- left = inv:add_item("cache", left)
- end
- end
- break
- end
- end
- if nd == diameter*diameter * (quarry_dig_above_nodes+1+quarry_max_depth) then
- -- if a quarry is finished, we enable purge mode
- meta:set_int("purge_on", 1)
- end
- meta:set_int("dug", nd)
- else
- -- if a quarry is disabled or has no power, we enable purge mode
- meta:set_int("purge_on", 1)
- end
- -- if something triggered a purge, we handle it
- if meta:get_int("purge_on") == 1 then
- quarry_handle_purge(pos)
- end
- set_quarry_formspec(meta)
- set_quarry_demand(meta)
-end
-
-local function send_move_error(player)
- minetest.chat_send_player(player:get_player_name(),
- S("Manually taking/removing from cache by hand is not possible. "..
- "If you can't wait, restart or disable the quarry to start automatic purge."))
- return 0
-end
-
-minetest.register_node("technic:quarry", {
- description = S("%s Quarry"):format("HV"),
- tiles = {
- "technic_carbon_steel_block.png"..tube_entry,
- "technic_carbon_steel_block.png"..cable_entry,
- "technic_carbon_steel_block.png"..cable_entry,
- "technic_carbon_steel_block.png"..cable_entry,
- "technic_carbon_steel_block.png^default_tool_mesepick.png",
- "technic_carbon_steel_block.png"..cable_entry
- },
- paramtype2 = "facedir",
- groups = {cracky=2, tubedevice=1, technic_machine=1, technic_hv=1},
- connect_sides = {"bottom", "front", "left", "right"},
- tube = {
- connect_sides = {top = 1},
- -- lower priority than other tubes, so that quarries will prefer any
- -- other tube to another quarry, which could lead to server freezes
- -- in certain quarry placements (2x2 for example would never eject)
- priority = 10,
- can_go = function(pos, node, velocity, stack)
- -- always eject the same, even if items came in another way
- -- this further mitigates loops and generally avoids random sideway movement
- -- that can be expected in certain quarry placements
- return { quarry_eject_dir }
- end
- },
- on_construct = function(pos)
- local meta = minetest.get_meta(pos)
- meta:set_string("infotext", S("%s Quarry"):format("HV"))
- meta:set_int("size", 4)
- set_quarry_formspec(meta)
- set_quarry_demand(meta)
- end,
- after_place_node = function(pos, placer, itemstack)
- local meta = minetest.get_meta(pos)
- meta:set_string("owner", placer:get_player_name())
- pipeworks.scan_for_tube_objects(pos)
- end,
- can_dig = function(pos,player)
- local meta = minetest.get_meta(pos);
- local inv = meta:get_inventory()
- return inv:is_empty("cache")
- end,
- after_dig_node = pipeworks.scan_for_tube_objects,
- on_receive_fields = quarry_receive_fields,
- technic_run = quarry_run,
- allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
- return send_move_error(player)
- end,
- allow_metadata_inventory_put = function(pos, listname, index, stack, player)
- return send_move_error(player)
- end,
- allow_metadata_inventory_take = function(pos, listname, index, stack, player)
- return send_move_error(player)
- end
-})
-
-technic.register_machine("HV", "technic:quarry", technic.receiver)
diff --git a/technic/machines/switching_station.lua b/technic/machines/switching_station.lua
index dcc0520..21d394b 100644
--- a/technic/machines/switching_station.lua
+++ b/technic/machines/switching_station.lua
@@ -91,48 +91,57 @@ minetest.register_node("technic:switching_station",{
--------------------------------------------------
-- Functions to traverse the electrical network
--------------------------------------------------
+local function flatten(map)
+ local list = {}
+ for key, value in pairs(map) do
+ list[#list + 1] = value
+ end
+ return list
+end
-- Add a wire node to the LV/MV/HV network
-local add_new_cable_node = function(nodes, pos, network_id)
- technic.cables[minetest.hash_node_position(pos)] = network_id
- -- Ignore if the node has already been added
- for i = 1, #nodes do
- if pos.x == nodes[i].x and
- pos.y == nodes[i].y and
- pos.z == nodes[i].z then
- return false
- end
+local function add_network_node(nodes, pos, network_id)
+ local node_id = minetest.hash_node_position(pos)
+ technic.cables[node_id] = network_id
+ if nodes[node_id] then
+ return false
end
- table.insert(nodes, {x=pos.x, y=pos.y, z=pos.z, visited=1})
+ nodes[node_id] = pos
return true
end
+local function add_cable_node(nodes, pos, network_id, queue)
+ if add_network_node(nodes, pos, network_id) then
+ queue[#queue + 1] = pos
+ end
+end
+
-- Generic function to add found connected nodes to the right classification array
-local check_node_subp = function(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nodes, pos, machines, tier, sw_pos, from_below, network_id)
+local check_node_subp = function(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nodes, pos, machines, tier, sw_pos, from_below, network_id, queue)
technic.get_or_load_node(pos)
local meta = minetest.get_meta(pos)
local name = minetest.get_node(pos).name
if technic.is_tier_cable(name, tier) then
- add_new_cable_node(all_nodes, pos,network_id)
+ add_cable_node(all_nodes, pos,network_id, queue)
elseif machines[name] then
--dprint(name.." is a "..machines[name])
meta:set_string(tier.."_network",minetest.pos_to_string(sw_pos))
if machines[name] == technic.producer then
- add_new_cable_node(PR_nodes, pos, network_id)
+ add_network_node(PR_nodes, pos, network_id)
elseif machines[name] == technic.receiver then
- add_new_cable_node(RE_nodes, pos, network_id)
+ add_network_node(RE_nodes, pos, network_id)
elseif machines[name] == technic.producer_receiver then
- add_new_cable_node(PR_nodes, pos, network_id)
- add_new_cable_node(RE_nodes, pos, network_id)
+ add_network_node(PR_nodes, pos, network_id)
+ add_network_node(RE_nodes, pos, network_id)
elseif machines[name] == "SPECIAL" and
(pos.x ~= sw_pos.x or pos.y ~= sw_pos.y or pos.z ~= sw_pos.z) and
from_below then
-- Another switching station -> disable it
- add_new_cable_node(SP_nodes, pos, network_id)
+ add_network_node(SP_nodes, pos, network_id)
meta:set_int("active", 0)
elseif machines[name] == technic.battery then
- add_new_cable_node(BA_nodes, pos, network_id)
+ add_network_node(BA_nodes, pos, network_id)
end
meta:set_int(tier.."_EU_timeout", 2) -- Touch node
@@ -140,8 +149,7 @@ local check_node_subp = function(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nod
end
-- Traverse a network given a list of machines and a cable type name
-local traverse_network = function(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nodes, i, machines, tier, sw_pos, network_id)
- local pos = all_nodes[i]
+local traverse_network = function(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nodes, pos, machines, tier, sw_pos, network_id, queue)
local positions = {
{x=pos.x+1, y=pos.y, z=pos.z},
{x=pos.x-1, y=pos.y, z=pos.z},
@@ -149,9 +157,8 @@ local traverse_network = function(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_no
{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}}
- --print("ON")
for i, cur_pos in pairs(positions) do
- check_node_subp(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nodes, cur_pos, machines, tier, sw_pos, i == 3, network_id)
+ check_node_subp(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nodes, cur_pos, machines, tier, sw_pos, i == 3, network_id, queue)
end
end
@@ -163,7 +170,8 @@ local touch_nodes = function(list, tier)
end
local get_network = function(sw_pos, pos1, tier)
- local cached = technic.networks[minetest.hash_node_position(pos1)]
+ local network_id = minetest.hash_node_position(pos1)
+ local cached = technic.networks[network_id]
if cached and cached.tier == tier then
touch_nodes(cached.PR_nodes, tier)
touch_nodes(cached.BA_nodes, tier)
@@ -175,19 +183,28 @@ local get_network = function(sw_pos, pos1, tier)
end
return cached.PR_nodes, cached.BA_nodes, cached.RE_nodes
end
- local i = 1
local PR_nodes = {}
local BA_nodes = {}
local RE_nodes = {}
local SP_nodes = {}
- local all_nodes = {pos1}
- repeat
- traverse_network(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nodes,
- i, technic.machines[tier], tier, sw_pos, minetest.hash_node_position(pos1))
- i = i + 1
- until all_nodes[i] == nil
- technic.networks[minetest.hash_node_position(pos1)] = {tier = tier, PR_nodes = PR_nodes,
- RE_nodes = RE_nodes, BA_nodes = BA_nodes, SP_nodes = SP_nodes, all_nodes = all_nodes}
+ local all_nodes = {}
+ local queue = {}
+ add_cable_node(all_nodes, pos1, network_id, queue)
+ while next(queue) do
+ local to_visit = {}
+ for _, pos in ipairs(queue) do
+ traverse_network(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nodes,
+ pos, technic.machines[tier], tier, sw_pos, network_id, to_visit)
+ end
+ queue = to_visit
+ end
+ PR_nodes = flatten(PR_nodes)
+ BA_nodes = flatten(BA_nodes)
+ RE_nodes = flatten(RE_nodes)
+ SP_nodes = flatten(SP_nodes)
+ all_nodes = flatten(all_nodes)
+ technic.networks[network_id] = {tier = tier, all_nodes = all_nodes, SP_nodes = SP_nodes,
+ PR_nodes = PR_nodes, RE_nodes = RE_nodes, BA_nodes = BA_nodes}
return PR_nodes, BA_nodes, RE_nodes
end
@@ -242,8 +259,8 @@ minetest.register_abm({
local poshash = minetest.hash_node_position(pos)
- if not technic.redundant_warn.poshash then
- technic.redundant_warn.poshash = true
+ if not technic.redundant_warn[poshash] then
+ technic.redundant_warn[poshash] = true
print("[TECHNIC] Warning: redundant switching station found near "..minetest.pos_to_string(pos))
end
return