From 65f5ac8520a286ae062824147c2f0e3b0253c8ff Mon Sep 17 00:00:00 2001 From: FaceDeer Date: Thu, 12 Oct 2017 03:06:24 -0600 Subject: [PATCH 1/3] add initial support for tapping technic power cables --- class_layout.lua | 28 ++++++++++++-- init.lua | 2 + nodes/node_crate.lua | 11 +++++- nodes/node_power_connector.lua | 69 ++++++++++++++++++++++++++++++++++ util_execute_cycle.lua | 31 +++++++++++---- 5 files changed, 130 insertions(+), 11 deletions(-) create mode 100644 nodes/node_power_connector.lua diff --git a/class_layout.lua b/class_layout.lua index 6094a7d..4ada5b2 100644 --- a/class_layout.lua +++ b/class_layout.lua @@ -36,7 +36,8 @@ function DigtronLayout.create(pos, player) self.all = {} self.inventories = {} self.fuelstores = {} - self.battery_holders = {} + self.battery_holders = {} -- technic batteries + self.power_connectors = {} -- technic power cable self.diggers = {} self.builders = {} self.extents = {} @@ -123,6 +124,8 @@ function DigtronLayout.create(pos, player) table.insert(self.fuelstores, node_image) elseif group_number == 7 then table.insert(self.battery_holders, node_image) + elseif group_number == 8 then + table.insert(self.power_connectors, node_image) end if is_protected then @@ -320,6 +323,14 @@ function DigtronLayout.write_layout_image(self, player) local old_meta = minetest.get_meta(oldpos) local old_def = minetest.registered_nodes[old_node.name] minetest.remove_node(oldpos) + + for _, callback in ipairs(minetest.registered_on_dignodes) do + -- Copy pos and node because callback can modify them + local pos_copy = {x=oldpos.x, y=oldpos.y, z=oldpos.z} + local oldnode_copy = {name=old_node.name, param1=old_node.param1, param2=old_node.param2} + callback(pos_copy, oldnode_copy, player) + end + minetest.log("action", string.format("%s removes Digtron component %s at (%d, %d, %d)", player:get_player_name(), old_node.name, oldpos.x, oldpos.y, oldpos.z)) if old_def.after_dig_node ~= nil then old_def.after_dig_node(oldpos, old_node, old_meta, player) @@ -329,10 +340,21 @@ function DigtronLayout.write_layout_image(self, player) -- create the new one for k, node_image in pairs(self.all) do - minetest.set_node(node_image.pos, node_image.node) - minetest.get_meta(node_image.pos):from_table(node_image.meta) + local new_pos = node_image.pos + local new_node = node_image.node + local old_node = minetest.get_node(new_pos) + minetest.set_node(new_pos, new_node) + minetest.get_meta(new_pos):from_table(node_image.meta) minetest.log("action", string.format("%s adds Digtron component %s at (%d, %d, %d)", player:get_player_name(), node_image.node.name, node_image.pos.x, node_image.pos.y, node_image.pos.z)) + for _, callback in ipairs(minetest.registered_on_placenodes) do + -- Copy pos and node because callback can modify them + local pos_copy = {x=new_pos.x, y=new_pos.y, z=new_pos.z} + local oldnode_copy = {name=old_node.name, param1=old_node.param1, param2=old_node.param2} + local newnode_copy = {name=new_node.name, param1=new_node.param1, param2=new_node.param2} + callback(pos_copy, newnode_copy, player, oldnode_copy) + end + local new_def = minetest.registered_nodes[node_image.node.name] if new_def.after_place_node ~= nil then new_def.after_place_node(node_image.pos, player) diff --git a/init.lua b/init.lua index d3c0a36..5e93454 100644 --- a/init.lua +++ b/init.lua @@ -39,6 +39,8 @@ dofile( digtron_modpath .. "/nodes/node_axle.lua" ) -- Rotation controller dofile( digtron_modpath .. "/nodes/node_crate.lua" ) -- Digtron portability support dofile( digtron_modpath .. "/nodes/recipes.lua" ) +dofile( digtron_modpath .. "/nodes/node_power_connector.lua") + dofile( digtron_modpath .. "/upgrades.lua" ) -- various LBMs for upgrading older versions of Digtron. -- digtron group numbers: diff --git a/nodes/node_crate.lua b/nodes/node_crate.lua index 7e30207..080fe68 100644 --- a/nodes/node_crate.lua +++ b/nodes/node_crate.lua @@ -30,7 +30,16 @@ minetest.register_node("digtron:empty_crate", { -- destroy everything. Note that this includes the empty crate, which will be bundled up with the layout. for _, node_image in pairs(layout.all) do - minetest.remove_node(node_image.pos) + local old_pos = node_image.pos + local old_node = node_image.node + minetest.remove_node(old_pos) + + for _, callback in ipairs(minetest.registered_on_dignodes) do + -- Copy pos and node because callback can modify them + local pos_copy = {x=old_pos.x, y=old_pos.y, z=old_pos.z} + local oldnode_copy = {name=old_node.name, param1=old_node.param1, param2=old_node.param2} + callback(pos_copy, oldnode_copy, clicker) + end end -- Create the loaded crate node diff --git a/nodes/node_power_connector.lua b/nodes/node_power_connector.lua new file mode 100644 index 0000000..4e51e36 --- /dev/null +++ b/nodes/node_power_connector.lua @@ -0,0 +1,69 @@ +-- internationalization boilerplate +local MP = minetest.get_modpath(minetest.get_current_modname()) +local S, NS = dofile(MP.."/intllib.lua") + +local size = 3/16 + +local max_dig_cost = math.max(digtron.config.dig_cost_cracky, digtron.config.dig_cost_crumbly, digtron.config.dig_cost_choppy, digtron.config.dig_cost_default) + +minetest.register_node("digtron:power_connector", { + description = S("DPC"), + _doc_items_longdesc = digtron.doc.power_connector_longdesc, + _doc_items_usagehelp = digtron.doc.power_connector_usagehelp, + groups = {cracky = 3, oddly_breakable_by_hand=3, digtron = 8, technic_machine=1, technic_hv=1}, + tiles = {"digtron_plate.png"}, + connect_sides = {"bottom", "top", "left", "right", "front", "back"}, + drawtype = "nodebox", + sounds = digtron.metal_sounds, + paramtype = "light", + paramtype2 = "facedir", + is_ground_content = false, +-- node_box = { +-- type = "fixed", +-- fixed = { +-- {-0.5, 0.5, -0.5, 0.5, 0, 0.5}, -- NodeBox1 +-- {-0.1875, 0, -0.1875, 0.1875, -0.5, 0.1875}, -- NodeBox2 +-- {-0.3125, -0.0625, -0.3125, 0.3125, -0.1875, 0.3125}, -- NodeBox3 +-- {-0.3125, -0.25, -0.3125, 0.3125, -0.375, 0.3125}, -- NodeBox4 +-- } +-- }, + + connects_to = {"group:technic_hv_cable"}, + node_box = { + type = "connected", + fixed = { + {-0.5, -0.5, -0.5, 0.5, 0, 0.5}, -- NodeBox1 + {-0.1875, 0, -0.1875, 0.1875, 0.5, 0.1875}, -- NodeBox2 + {-0.3125, 0.0625, -0.3125, 0.3125, 0.1875, 0.3125}, -- NodeBox3 + {-0.3125, 0.25, -0.3125, 0.3125, 0.375, 0.3125}, -- NodeBox4 + }, + connect_front = {-size, -size, -0.5, size, size, size}, -- z- + connect_back = {-size, -size, size, size, size, 0.5 }, -- z+ + connect_left = {-0.5, -size, -size, size, size, size}, -- x- + connect_right = {-size, -size, -size, 0.5, size, size}, -- x+ + }, + + technic_run = function(pos, node) + local meta = minetest.get_meta(pos) + local eu_input = meta:get_int("HV_EU_input") + local demand = meta:get_int("HV_EU_demand") + meta:set_string("infotext", S("Digtron Power @1/@2\nRight-click to update", eu_input, demand)) + end, + + on_rightclick = function(pos, node, player, itemstack, pointed_thing) + local layout = DigtronLayout.create(pos, player) + local max_cost = 0 + for _, node_image in pairs(layout.builders) do + max_cost = max_cost + digtron.config.build_cost + end + for _, node_image in pairs(layout.diggers) do + max_cost = max_cost + max_dig_cost + end + local meta = minetest.get_meta(pos) + meta:set_int("HV_EU_demand", max_cost * digtron.config.power_ratio) + end, + +}) + + +technic.register_machine("HV", "digtron:power_connector", technic.receiver) \ No newline at end of file diff --git a/util_execute_cycle.lua b/util_execute_cycle.lua index 29edcdb..d5f23f0 100644 --- a/util_execute_cycle.lua +++ b/util_execute_cycle.lua @@ -207,10 +207,25 @@ digtron.execute_dig_cycle = function(pos, clicker) local test_fuel_needed = test_build_fuel_cost + digging_fuel_cost - fuel_burning local test_fuel_burned = 0 - - if test_fuel_needed > 0 then - -- check for the available electrical power - test_fuel_burned = digtron.tap_batteries(layout.battery_holders, test_fuel_needed, true) + + local power_from_cables = 0 + if minetest.get_modpath("technic") then + local power_inputs = {} + for _, power_connector in pairs(layout.power_connectors) do + if power_connector.meta.fields.HV_network and power_connector.meta.fields.HV_EU_input then + power_inputs[power_connector.meta.fields.HV_network] = tonumber(power_connector.meta.fields.HV_EU_input) + end + end + for _, power in pairs(power_inputs) do + power_from_cables = power_from_cables + power + end + power_from_cables = power_from_cables / digtron.config.power_ratio + test_fuel_burned = power_from_cables + + if test_fuel_needed - test_fuel_burned > 0 then + -- check for the available electrical power + test_fuel_burned = test_fuel_burned + digtron.tap_batteries(layout.battery_holders, test_fuel_needed, true) + end end if (test_fuel_needed < test_fuel_burned) then exhaust = 0 -- all power needs met by electricity, don't blow smoke @@ -312,11 +327,13 @@ digtron.execute_dig_cycle = function(pos, clicker) minetest.sound_play("buzzer", {gain=0.5, pos=pos}) status_text = S("Digtron unexpectedly failed to execute one or more build operations, likely due to an inventory error.") .. "\n" end - + + local total_fuel_cost = math.max(digging_fuel_cost + building_fuel_cost - power_from_cables, 0) + -- actually burn the fuel needed - fuel_burning = fuel_burning - digging_fuel_cost + fuel_burning = fuel_burning - total_fuel_cost if digtron.config.particle_effects and exhaust == 1 then - table.insert(particle_systems, burn_smoke(pos, digging_fuel_cost)) + table.insert(particle_systems, burn_smoke(pos, total_fuel_cost)) end if fuel_burning < 0 then -- we tap into the batteries either way From d582f106aa4f1be3bda41113a4893c39d35223a7 Mon Sep 17 00:00:00 2001 From: FaceDeer Date: Thu, 12 Oct 2017 22:53:30 -0600 Subject: [PATCH 2/3] finishing up technic power tap, other misc fixes --- class_layout.lua | 19 +++++- nodes/node_crate.lua | 9 +++ nodes/node_power_connector.lua | 74 +++++++++++++++------- nodes/recipes.lua | 9 +++ textures/digtron_power_connector_side.png | Bin 0 -> 390 bytes textures/digtron_power_connector_top.png | Bin 0 -> 387 bytes util_execute_cycle.lua | 8 +-- 7 files changed, 91 insertions(+), 28 deletions(-) create mode 100644 textures/digtron_power_connector_side.png create mode 100644 textures/digtron_power_connector_top.png diff --git a/class_layout.lua b/class_layout.lua index 4ada5b2..c340060 100644 --- a/class_layout.lua +++ b/class_layout.lua @@ -1,6 +1,8 @@ DigtronLayout = {} DigtronLayout.__index = DigtronLayout +local modpath_awards = minetest.get_modpath("awards") + ------------------------------------------------------------------------- -- Creation @@ -147,8 +149,9 @@ function DigtronLayout.create(pos, player) to_test:set_if_not_in(tested, testpos.x, testpos.y - 1, testpos.z, true) to_test:set_if_not_in(tested, testpos.x, testpos.y, testpos.z + 1, true) to_test:set_if_not_in(tested, testpos.x, testpos.y, testpos.z - 1, true) - elseif minetest.registered_nodes[node.name].buildable_to ~= true then + elseif not minetest.registered_nodes[node.name] or minetest.registered_nodes[node.name].buildable_to ~= true then -- Tracks whether the digtron is hovering in mid-air. If any part of the digtron array touches something solid it gains traction. + -- Allowing unknown nodes to provide traction, since they're not buildable_to either self.traction = self.traction + 1 end @@ -324,6 +327,13 @@ function DigtronLayout.write_layout_image(self, player) local old_def = minetest.registered_nodes[old_node.name] minetest.remove_node(oldpos) + if modpath_awards then + -- We're about to tell the awards mod that we're digging a node, but we + -- don't want it to count toward any actual awards. Pre-decrement. + local data = awards.players[player:get_player_name()] + awards.increment_item_counter(data, "count", old_node.name, -1) + end + for _, callback in ipairs(minetest.registered_on_dignodes) do -- Copy pos and node because callback can modify them local pos_copy = {x=oldpos.x, y=oldpos.y, z=oldpos.z} @@ -347,6 +357,13 @@ function DigtronLayout.write_layout_image(self, player) minetest.get_meta(new_pos):from_table(node_image.meta) minetest.log("action", string.format("%s adds Digtron component %s at (%d, %d, %d)", player:get_player_name(), node_image.node.name, node_image.pos.x, node_image.pos.y, node_image.pos.z)) + if modpath_awards then + -- We're about to tell the awards mod that we're placing a node, but we + -- don't want it to count toward any actual awards. Pre-decrement. + local data = awards.players[player:get_player_name()] + awards.increment_item_counter(data, "place", new_node.name, -1) + end + for _, callback in ipairs(minetest.registered_on_placenodes) do -- Copy pos and node because callback can modify them local pos_copy = {x=new_pos.x, y=new_pos.y, z=new_pos.z} diff --git a/nodes/node_crate.lua b/nodes/node_crate.lua index 080fe68..101d7bb 100644 --- a/nodes/node_crate.lua +++ b/nodes/node_crate.lua @@ -2,6 +2,8 @@ local MP = minetest.get_modpath(minetest.get_current_modname()) local S, NS = dofile(MP.."/intllib.lua") +local modpath_awards = minetest.get_modpath("awards") + minetest.register_node("digtron:empty_crate", { description = S("Digtron Crate (Empty)"), _doc_items_longdesc = digtron.doc.empty_crate_longdesc, @@ -34,6 +36,13 @@ minetest.register_node("digtron:empty_crate", { local old_node = node_image.node minetest.remove_node(old_pos) + if modpath_awards then + -- We're about to tell the awards mod that we're digging a node, but we + -- don't want it to count toward any actual awards. Pre-decrement. + local data = awards.players[clicker:get_player_name()] + awards.increment_item_counter(data, "count", old_node.name, -1) + end + for _, callback in ipairs(minetest.registered_on_dignodes) do -- Copy pos and node because callback can modify them local pos_copy = {x=old_pos.x, y=old_pos.y, z=old_pos.z} diff --git a/nodes/node_power_connector.lua b/nodes/node_power_connector.lua index 4e51e36..6c4f903 100644 --- a/nodes/node_power_connector.lua +++ b/nodes/node_power_connector.lua @@ -6,36 +6,48 @@ local size = 3/16 local max_dig_cost = math.max(digtron.config.dig_cost_cracky, digtron.config.dig_cost_crumbly, digtron.config.dig_cost_choppy, digtron.config.dig_cost_default) +local get_formspec_string = function(current_val, current_max) + return "size[4.5,0.6]" .. + default.gui_bg .. + default.gui_bg_img .. + default.gui_slots .. + "field[0.2,0.3;1,1;value;;".. current_val .. "]" .. + "button[1,0;1,1;maximize;" .. S("Maximize\nPower") .."]" .. + "label[2,0;"..S("Maximum Power\nRequired: @1", current_max) .."]".. + "button[3.5,0;1,1;refresh;" .. S("Refresh\nMax") .."]" +end + +local connector_groups = {cracky = 3, oddly_breakable_by_hand=3, digtron = 8, technic_machine=1, technic_hv=1} +if not minetest.get_modpath("technic") then + -- Technic is not installed, hide this away. + connector_groups.not_in_creative_inventory = 1 +end + minetest.register_node("digtron:power_connector", { - description = S("DPC"), + description = S("Digtron HV Power Connector"), _doc_items_longdesc = digtron.doc.power_connector_longdesc, _doc_items_usagehelp = digtron.doc.power_connector_usagehelp, - groups = {cracky = 3, oddly_breakable_by_hand=3, digtron = 8, technic_machine=1, technic_hv=1}, - tiles = {"digtron_plate.png"}, + _digtron_formspec = get_formspec_string(0,0), + groups = connector_groups, + tiles = {"digtron_plate.png^digtron_power_connector_top.png^digtron_digger_yb_frame.png", "digtron_plate.png^digtron_digger_yb_frame.png", + "digtron_plate.png^digtron_digger_yb_frame.png^digtron_power_connector_side.png", "digtron_plate.png^digtron_digger_yb_frame.png^digtron_power_connector_side.png", + "digtron_plate.png^digtron_digger_yb_frame.png^digtron_power_connector_side.png", "digtron_plate.png^digtron_digger_yb_frame.png^digtron_power_connector_side.png", + }, connect_sides = {"bottom", "top", "left", "right", "front", "back"}, drawtype = "nodebox", sounds = digtron.metal_sounds, paramtype = "light", paramtype2 = "facedir", is_ground_content = false, --- node_box = { --- type = "fixed", --- fixed = { --- {-0.5, 0.5, -0.5, 0.5, 0, 0.5}, -- NodeBox1 --- {-0.1875, 0, -0.1875, 0.1875, -0.5, 0.1875}, -- NodeBox2 --- {-0.3125, -0.0625, -0.3125, 0.3125, -0.1875, 0.3125}, -- NodeBox3 --- {-0.3125, -0.25, -0.3125, 0.3125, -0.375, 0.3125}, -- NodeBox4 --- } --- }, connects_to = {"group:technic_hv_cable"}, node_box = { type = "connected", fixed = { - {-0.5, -0.5, -0.5, 0.5, 0, 0.5}, -- NodeBox1 - {-0.1875, 0, -0.1875, 0.1875, 0.5, 0.1875}, -- NodeBox2 - {-0.3125, 0.0625, -0.3125, 0.3125, 0.1875, 0.3125}, -- NodeBox3 - {-0.3125, 0.25, -0.3125, 0.3125, 0.375, 0.3125}, -- NodeBox4 + {-0.5, -0.5, -0.5, 0.5, 0, 0.5}, -- Main body + {-0.1875, 0, -0.1875, 0.1875, 0.5, 0.1875}, -- post + {-0.3125, 0.0625, -0.3125, 0.3125, 0.1875, 0.3125}, -- vane + {-0.3125, 0.25, -0.3125, 0.3125, 0.375, 0.3125}, -- vane }, connect_front = {-size, -size, -0.5, size, size, size}, -- z- connect_back = {-size, -size, size, size, size, 0.5 }, -- z+ @@ -43,15 +55,20 @@ minetest.register_node("digtron:power_connector", { connect_right = {-size, -size, -size, 0.5, size, size}, -- x+ }, + on_construct = function(pos) + local meta = minetest.get_meta(pos) + meta:set_string("formspec", get_formspec_string(0,0)) + end, + technic_run = function(pos, node) local meta = minetest.get_meta(pos) local eu_input = meta:get_int("HV_EU_input") local demand = meta:get_int("HV_EU_demand") - meta:set_string("infotext", S("Digtron Power @1/@2\nRight-click to update", eu_input, demand)) + meta:set_string("infotext", S("Digtron Power @1/@2", eu_input, demand)) end, - on_rightclick = function(pos, node, player, itemstack, pointed_thing) - local layout = DigtronLayout.create(pos, player) + on_receive_fields = function(pos, formname, fields, sender) + local layout = DigtronLayout.create(pos, sender) local max_cost = 0 for _, node_image in pairs(layout.builders) do max_cost = max_cost + digtron.config.build_cost @@ -59,11 +76,22 @@ minetest.register_node("digtron:power_connector", { for _, node_image in pairs(layout.diggers) do max_cost = max_cost + max_dig_cost end + local current_max = max_cost * digtron.config.power_ratio + local meta = minetest.get_meta(pos) - meta:set_int("HV_EU_demand", max_cost * digtron.config.power_ratio) + + if fields.maximize then + meta:set_int("HV_EU_demand", current_max) + elseif fields.value ~= nil then + local number = tonumber(fields.value) or 0 + local number = math.min(math.max(number, 0), current_max) + meta:set_int("HV_EU_demand", number) + end + + meta:set_string("formspec", get_formspec_string(meta:get_int("HV_EU_demand"), current_max)) end, - }) - -technic.register_machine("HV", "digtron:power_connector", technic.receiver) \ No newline at end of file +if minetest.get_modpath("technic") then + technic.register_machine("HV", "digtron:power_connector", technic.receiver) +end \ No newline at end of file diff --git a/nodes/recipes.lua b/nodes/recipes.lua index 942fa69..87b0945 100644 --- a/nodes/recipes.lua +++ b/nodes/recipes.lua @@ -100,6 +100,15 @@ if minetest.get_modpath("technic") then {"","default:steel_ingot",""} } }) + + minetest.register_craft({ + output = "digtron:power_connector", + recipe = { + {"","technic:hv_cable",""}, + {"technic:hv_cable","digtron:digtron_core","technic:hv_cable"}, + {"","technic:hv_cable",""} + } + }) end minetest.register_craft({ diff --git a/textures/digtron_power_connector_side.png b/textures/digtron_power_connector_side.png new file mode 100644 index 0000000000000000000000000000000000000000..d4ad519367226412adedd57a6665f494b81d2401 GIT binary patch literal 390 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`Y)RhkE)4%caKYZ?lYt_f1s;*b z3=G`DAk4@xYmNj^kiEpy*OmPt7mo;=_sW7EcbbH6h^bmDSouy1cJGu) zi4rSb|B|d=j_WmKZ>ZlEsp%RyZAqq=%yACI8Ou7?y^d>3T(D}@a{mja54uCwY>mji z81=YPF82Dy_tvL31%zJx?JQVx-(B70zo> zzxFN9joy~K+v=Gng94B9CLQIzs{Q7HMyqVM>E9(!$ bGt@A=m|v0ltIOpdC^$S_{an^LB{Ts5($teR literal 0 HcmV?d00001 diff --git a/textures/digtron_power_connector_top.png b/textures/digtron_power_connector_top.png new file mode 100644 index 0000000000000000000000000000000000000000..8ae47b184e0f58cc49d9ae137c8794622cac37d7 GIT binary patch literal 387 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`Y)RhkE)4%caKYZ?lYt_f1s;*b z3=G`DAk4@xYmNj^kiEpy*OmPt7cZl_@!5mY`+-8TnIRD+&iT2ysd*(pE(3#eQEFmI zYKlU6W=V#EyQgnJie4%^Q2e>4i(`nz>7|oTceNS_xcINGo!X&YuJ={@&hP90CTTP+ z`r(mg*i(>h^x$Yy)Y76$Qw0joeT-IW^1t_{zCpz^AoP-nDhJcC!X@ioYhT``%P{f% z6N^60)01>g8yr(m^11ACy|m}CM`-B# Date: Thu, 12 Oct 2017 23:07:48 -0600 Subject: [PATCH 3/3] allow digtrons to dig undefined nodes without crashing --- util.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util.lua b/util.lua index 07d41ba..05c6c79 100644 --- a/util.lua +++ b/util.lua @@ -46,7 +46,7 @@ digtron.mark_diggable = function(pos, nodes_dug) end local targetdef = minetest.registered_nodes[target.name] - if targetdef.can_dig == nil or targetdef.can_dig(pos, player) then + if targetdef == nil or targetdef.can_dig == nil or targetdef.can_dig(pos, player) then nodes_dug:set(pos.x, pos.y, pos.z, true) if target.name ~= "air" then local in_known_group = false