From 7c8d6fbdafa8f82507640702d642666f0b6d5fac Mon Sep 17 00:00:00 2001 From: OgelGames Date: Fri, 29 May 2020 20:28:49 +1000 Subject: [PATCH] quarry rewrite/optimization --- technic/machines/HV/quarry.lua | 447 ++++++++++++++------------------- 1 file changed, 193 insertions(+), 254 deletions(-) diff --git a/technic/machines/HV/quarry.lua b/technic/machines/HV/quarry.lua index 79f3303..8d6408b 100644 --- a/technic/machines/HV/quarry.lua +++ b/technic/machines/HV/quarry.lua @@ -1,103 +1,78 @@ local S = technic.getter -local tube_entry = "^pipeworks_tube_connection_metallic.png" -local cable_entry = "^technic_cable_connection_overlay.png" +local quarry_dig_above_nodes = tonumber(minetest.settings:get("technic.quarry.dig_above_nodes") or "3") +local quarry_max_depth = tonumber(minetest.settings:get("technic.quarry.maxdepth") or "100") +local quarry_time_limit = tonumber(minetest.settings:get("technic.quarry.time_limit") or "1000") - -minetest.register_craft({ - recipe = { - {"technic:carbon_plate", "pipeworks:filter", "technic:composite_plate"}, - {"basic_materials: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 = tonumber(minetest.settings:get("technic.quarry.maxdepth") or "100") local quarry_demand = 10000 local quarry_eject_dir = vector.new(0, 1, 0) +local machine_name = S("%s Quarry"):format("HV") +local quarry_formspec = + "size[8,9]".. + "item_image[7,0;1,1;technic:quarry]".. + "list[context;cache;1,1;4,3;]".. + "listring[context;cache]".. + "list[current_player;main;0,5;8,4;]".. + "listring[current_player;main]".. + "label[0,0;"..machine_name.."]".. + "button[5,1.9;2,1;restart;"..S("Restart").."]" + +-- hard-coded spiral dig pattern for up to 17x17 dig area +local quarry_dig_pattern = { + 0,1,2,2,3,3,0,0,0,1,1,1,2,2,2,2,3,3,3,3,0,0,0,0,0,1,1,1,1,1,2,2, + 2,2,2,2,3,3,3,3,3,3,0,0,0,0,0,0,0,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2, + 3,3,3,3,3,3,3,3,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2, + 2,2,2,2,3,3,3,3,3,3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1, + 1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,0,0,0,0, + 0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2, + 2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, + 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 +} +local function reset_quarry(pos) + local meta = minetest.get_meta(pos) + local node = technic.get_or_load_node(pos) or minetest.get_node(pos) + meta:set_int("quarry_dir", node.param2) + meta:set_string("quarry_pos", minetest.pos_to_string(pos)) + meta:set_string("dig_pos", "") + meta:set_int("dig_level", pos.y + quarry_dig_above_nodes) + local radius = meta:get_int("size") + local diameter = (radius*2)+1 + meta:set_int("dig_steps", diameter*diameter) + meta:set_int("dig_index", 0) + meta:set_int("purge_on", 1) + meta:set_int("finished", 0) + meta:set_int("dug", 0) +end --- per player quota -local quota_map = {} - -local enable_quota = minetest.settings:get_bool("technic.quarry.quota.enable", false) - --- quota reset timer -if enable_quota then - local timer = 0 - minetest.register_globalstep(function(dtime) - timer = timer + dtime - if timer < 1 then return end - timer=0 - - -- reset quota map - quota_map = {} - - -- this many blocks per second - local init_quota = tonumber(minetest.settings:get("technic.quarry.quota.limit") or "10") - - local players = minetest.get_connected_players() - for i, player in pairs(players) do - local name = player:get_player_name() - quota_map[name] = init_quota +local function set_quarry_status(pos) + local meta = minetest.get_meta(pos) + local formspec = quarry_formspec.."field[5.3,3.4;2,1;size;"..S("Radius:")..";"..meta:get_int("size").."]" + local status = S("Digging not started") + if meta:get_int("enabled") == 1 then + formspec = formspec.."button[5,0.9;2,1;disable;"..S("Enabled").."]" + if meta:get_int("purge_on") == 1 then + status = S("Purging cache") + meta:set_string("infotext", S("%s purging cache"):format(machine_name)) + meta:set_int("HV_EU_demand", 0) + elseif meta:get_int("finished") == 1 then + status = S("Digging finished") + meta:set_string("infotext", S("%s Finished"):format(machine_name)) + meta:set_int("HV_EU_demand", 0) + else + local rel_y = meta:get_int("dig_level") - pos.y + status = S("Digging %d m "..(rel_y > 0 and "above" or "below").." machine"):format(math.abs(rel_y)) + 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) -end - -local cache_timer = 0 -local can_dig_cache = {} -- hash -> bool -minetest.register_globalstep(function(dtime) - cache_timer = cache_timer + dtime - if cache_timer < 30 then return end - cache_timer=0 - - -- clear cache - can_dig_cache = {} -end) - - -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)) + formspec = formspec.."button[5,0.9;2,1;enable;"..S("Disabled").."]" + meta:set_string("infotext", S("%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 + meta:set_string("formspec", formspec.."label[0,4.1;"..minetest.formspec_escape(status).."]") end local function quarry_receive_fields(pos, formname, fields, sender) @@ -110,19 +85,15 @@ 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 + if size >= 1 and size <= 8 and size ~= meta:get_int("size") then meta:set_int("size", size) - meta:set_int("dug", 0) + reset_quarry(pos) 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) + if fields.restart then reset_quarry(pos) end + set_quarry_status(pos) end local function quarry_handle_purge(pos) @@ -130,6 +101,7 @@ local function quarry_handle_purge(pos) local inv = meta:get_inventory() local cache = inv:get_list("cache") if not cache then + inv:set_size("cache", 12) return end local i = 0 @@ -150,164 +122,126 @@ local function quarry_handle_purge(pos) end end -local function quarry_run(pos, node) - - local meta = minetest.get_meta(pos) - local owner = meta:get_string("owner") - - 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) +local function can_dig_node(pos, node, digger) + if node.name == "air" or node.name == "vacuum:vacuum" then + return false end - - local digging_allowed = true - local quota = quota_map[owner] - - if enable_quota then - digging_allowed = quota and quota > 0 + local def = minetest.registered_nodes[node.name] + if not def or not def.diggable then + return false end - - - if digging_allowed and meta:get_int("enabled") and meta:get_int("HV_EU_input") >= quarry_demand and meta:get_int("purge_on") == 0 then - - -- decrement quota - if enable_quota then - quota = quota - 1 - quota_map[owner] = quota - end - - local pdir = minetest.facedir_to_dir(node.param2) - if pdir.y ~= 0 then - -- faces up or down, not valid, otherwise depth-check would run endless and hang up the server - return - end - - 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 owner = meta:get_string("owner") - local nd = meta:get_int("dug") - - local t0 = minetest.get_us_time() - - while nd < diameter*diameter * (quarry_dig_above_nodes+1+quarry_max_depth) do - - local us_used = minetest.get_us_time() - t0 - if us_used > 10000 then - -- abort if this quarry takes too much time - break - end - - 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 - - local hash = minetest.hash_node_position(digpos) - if can_dig_cache[hash] == false then - can_dig = false - end - - if can_dig and minetest.is_protected and minetest.is_protected(digpos, owner) then - can_dig = false - can_dig_cache[hash] = true - 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} - -- doors mod among other thing does NOT like a nil digger... - local fakedigger = pipeworks.create_fake_player({ - name = owner - }) - if not dignodedef.diggable or (dignodedef.can_dig and not dignodedef.can_dig(digpos, fakedigger)) then - can_dig = false - can_dig_cache[hash] = true - end - end - - --[[ - if can_dig then - -- test above blocks if diggable - 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" and checknode.name ~= "vacuum:vacuum" 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) + if def.can_dig and not def.can_dig(pos, digger) then + return false 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) + return true 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 +local function find_ground(quarry_pos, quarry_dir, meta) + local dir = minetest.facedir_to_dir(quarry_dir % 4) + local radius = meta:get_int("size") + local dig_level = meta:get_int("dig_level") + local middle_pos = vector.add(quarry_pos, vector.multiply(dir, radius + 1)) + local pos1 = vector.new(middle_pos.x - radius, dig_level, middle_pos.z - radius) + local pos2 = vector.new(middle_pos.x + radius, dig_level, middle_pos.z + radius) + local nodes = minetest.find_nodes_in_area(pos1, pos2, {"air", "vacuum:vacuum"}) + if #nodes < meta:get_int("dig_steps") then + return vector.new(middle_pos.x, dig_level, middle_pos.z) + end + meta:set_int("dig_level", dig_level - 1) + return nil +end + +local function get_dig_pos(quarry_pos, quarry_dir, dig_pos, dig_index, dig_steps, meta) + if dig_index > 0 and dig_index < dig_steps then + local facedir = (quarry_dir + quarry_dig_pattern[dig_index]) % 4 + dig_pos = vector.add(dig_pos, minetest.facedir_to_dir(facedir)) + elseif dig_index >= dig_steps then + local dig_level = meta:get_int("dig_level") + if (quarry_pos.y - dig_level) >= quarry_max_depth then + return nil, dig_index + end + local dir = minetest.facedir_to_dir(quarry_dir % 4) + dig_pos = vector.add(quarry_pos, vector.multiply(dir, meta:get_int("size") + 1)) + dig_pos.y = dig_level - 1 + meta:set_int("dig_level", dig_pos.y) + dig_index = 0 + end + dig_index = dig_index + 1 + return dig_pos, dig_index +end + +local function execute_dig(pos, node, meta) + local dig_pos = minetest.string_to_pos(meta:get_string("dig_pos")) + local quarry_dir = meta:get_int("quarry_dir") + if not dig_pos then + -- quarry has not hit ground yet + dig_pos = find_ground(pos, quarry_dir, meta) + else + local owner = meta:get_string("owner") + local digger = pipeworks.create_fake_player({name = owner}) + local dig_steps = meta:get_int("dig_steps") + local dig_index = meta:get_int("dig_index") + local t0 = minetest.get_us_time() + -- search for something to dig + while (minetest.get_us_time() - t0) < quarry_time_limit do + dig_pos, dig_index = get_dig_pos(pos, quarry_dir, dig_pos, dig_index, dig_steps, meta) + if not dig_pos then + -- finished digging + meta:set_int("finished", 1) + meta:set_int("purge_on", 1) + break + end + if not minetest.is_protected(dig_pos, owner) then + local dig_node = technic.get_or_load_node(dig_pos) or minetest.get_node(dig_pos) + if can_dig_node(dig_pos, dig_node, digger) then + -- found something to dig, dig it and stop searching + minetest.remove_node(dig_pos) + local inv = meta:get_inventory() + local drops = minetest.get_node_drops(dig_node.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 + meta:set_int("dug", meta:get_int("dug") + 1) + break + end + end + end + meta:set_int("dig_index", dig_index) + end + if dig_pos then + meta:set_string("dig_pos", minetest.pos_to_string(dig_pos)) + end +end + +local function quarry_run(pos, node) + local meta = minetest.get_meta(pos) + if minetest.pos_to_string(pos) ~= meta:get_string("quarry_pos") then + -- quarry has been moved since last dig + reset_quarry(pos) + elseif meta:get_int("purge_on") == 1 then + quarry_handle_purge(pos) + elseif meta:get_int("enabled") and meta:get_int("HV_EU_input") >= quarry_demand and meta:get_int("finished") == 0 then + execute_dig(pos, node, meta) + elseif not meta:get_inventory():is_empty("cache") then + meta:set_int("purge_on", 1) + end + set_quarry_status(pos) 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^pipeworks_tube_connection_metallic.png", + "technic_carbon_steel_block.png^technic_cable_connection_overlay.png", + "technic_carbon_steel_block.png^technic_cable_connection_overlay.png", + "technic_carbon_steel_block.png^technic_cable_connection_overlay.png", "technic_carbon_steel_block.png^default_tool_mesepick.png", - "technic_carbon_steel_block.png"..cable_entry + "technic_carbon_steel_block.png^technic_cable_connection_overlay.png" }, paramtype2 = "facedir", groups = {cracky=2, tubedevice=1, technic_machine=1, technic_hv=1}, @@ -327,33 +261,38 @@ minetest.register_node("technic:quarry", { }, 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) + meta:get_inventory():set_size("cache", 12) + reset_quarry(pos) + set_quarry_status(pos) end, after_place_node = function(pos, placer, itemstack) - local meta = minetest.get_meta(pos) - meta:set_string("owner", placer:get_player_name()) + minetest.get_meta(pos):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") + return minetest.get_meta(pos):get_inventory():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) + return minetest.is_protected(pos, player:get_player_name()) and 0 or count end, allow_metadata_inventory_put = function(pos, listname, index, stack, player) - return send_move_error(player) + return minetest.is_protected(pos, player:get_player_name()) and 0 or stack:get_count() end, allow_metadata_inventory_take = function(pos, listname, index, stack, player) - return send_move_error(player) + return minetest.is_protected(pos, player:get_player_name()) and 0 or stack:get_count() end }) +minetest.register_craft({ + recipe = { + {"technic:carbon_plate", "pipeworks:filter", "technic:composite_plate"}, + {"basic_materials:motor", "technic:machine_casing", "technic:diamond_drill_head"}, + {"technic:carbon_steel_block", "technic:hv_cable", "technic:carbon_steel_block"}}, + output = "technic:quarry" +}) + technic.register_machine("HV", "technic:quarry", technic.receiver)