From 50565ca604e3ef4e9f1917d9fa492a12999c3e7b Mon Sep 17 00:00:00 2001 From: mrkubax10 Date: Thu, 8 May 2025 22:25:32 +0200 Subject: [PATCH] Implement scanners and add way to use them in Miner --- init.lua | 1 + machines/miner.lua | 40 +++++++++++- tools/scanner.lua | 152 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 190 insertions(+), 3 deletions(-) create mode 100644 tools/scanner.lua diff --git a/init.lua b/init.lua index 6cbe525..b32fd34 100644 --- a/init.lua +++ b/init.lua @@ -86,6 +86,7 @@ dofile(modpath.."/tools/jetpack.lua") dofile(modpath.."/tools/mining_laser.lua") dofile(modpath.."/tools/nano_suit.lua") dofile(modpath.."/tools/power_storage.lua") +dofile(modpath.."/tools/scanner.lua") dofile(modpath.."/tools/solar_helmet.lua") dofile(modpath.."/tools/static_boots.lua") dofile(modpath.."/tools/treetap.lua") diff --git a/machines/miner.lua b/machines/miner.lua index e653eb0..34442f6 100644 --- a/machines/miner.lua +++ b/machines/miner.lua @@ -152,7 +152,8 @@ industrialtest.internal.unpackTableInto(industrialtest.Miner,{ capacity=5000, ioConfig="iiiiii", hasPowerInput=true, - _opPower=1000 + _opPower=1000, + _scannerOpPower=10 }) function industrialtest.Miner.onConstruct(self,pos) @@ -212,7 +213,8 @@ function industrialtest.Miner.canUpdate(self,pos) local drillSlot=inv:get_stack("drill",1) local srcSlot=inv:get_stack("src",1) local level=meta:get_int("level") - return meta:get_int("industrialtest.powerAmount")>=self._opPower and not drillSlot:is_empty() and not srcSlot:is_empty() and + local requiredPower=self:getRequiredPower(pos) + return meta:get_int("industrialtest.powerAmount")>=requiredPower and not drillSlot:is_empty() and not srcSlot:is_empty() and self:canContinue(pos,level) end @@ -284,8 +286,25 @@ function industrialtest.Miner.update(self,pos,elapsed,meta,inv) local drop=self.getNodeDrop(targetPos) self.placeMiningPipe(pos,targetPos) inv:add_item("dst",drop) + + local scannerSlot=inv:get_stack("scanner",1) + if not scannerSlot:is_empty() then + local def=scannerSlot:get_definition() + if def and def._industrialtest_self then + local filtered=def._industrialtest_self:filter(targetPos) + for _,filteredPos in ipairs(filtered) do + drop=self.getNodeDrop(filteredPos) + if inv:room_for_item("dst",drop) then + minetest.remove_node(filteredPos) + inv:add_item("dst",drop) + end + end + end + end + + local requiredPower=self:getRequiredPower(pos) meta:set_int("level",level-1) - industrialtest.api.addPower(meta,-self._opPower) + industrialtest.api.addPower(meta,-requiredPower) return true,true end @@ -324,6 +343,21 @@ function industrialtest.Miner.canContinue(self,pos,level) return not (def and def.groups and def.groups.unbreakable) and inv:room_for_item("dst",drop) end +function industrialtest.Miner.getRequiredPower(self,pos) + local meta=minetest.get_meta(pos) + local inv=meta:get_inventory() + local scannerSlot=inv:get_stack("scanner",1) + local result=self._opPower + if not scannerSlot:is_empty() then + local def=scannerSlot:get_definition() + if def and def._industrialtest_self then + local distance=def._industrialtest_self.minerDistance or 0 + result=result+(distance*distance-1)*self._scannerOpPower + end + end + return result +end + function industrialtest.Miner.getNodeDrop(pos) local node=minetest.get_node(pos) local def=minetest.registered_nodes[node.name] diff --git a/tools/scanner.lua b/tools/scanner.lua new file mode 100644 index 0000000..ee84b5b --- /dev/null +++ b/tools/scanner.lua @@ -0,0 +1,152 @@ +-- IndustrialTest +-- Copyright (C) 2025 mrkubax10 + +-- This program is free software: you can redistribute it and/or modify +-- it under the terms of the GNU General Public License as published by +-- the Free Software Foundation, either version 3 of the License, or +-- (at your option) any later version. + +-- This program is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. + +-- You should have received a copy of the GNU General Public License +-- along with this program. If not, see . + +local S=minetest.get_translator("industrialtest") + +industrialtest.Scanner=table.copy(industrialtest.ElectricTool) +industrialtest.internal.unpackTableInto(industrialtest.Scanner,{ + define={onUse=true} +}) + +function industrialtest.Scanner.createDefinitionTable(self) + local def=industrialtest.ElectricTool.createDefinitionTable(self) + def.groups._industrialtest_scanner=1 + return def +end + +-- Used by Miner to provide Scanner functionality there +function industrialtest.Scanner.filter(self,pos) + -- dummy function + return {} +end + +industrialtest.OreScanner=table.copy(industrialtest.Scanner) + +function industrialtest.OreScanner.hitUse(self,itemstack,user,pointed) + if not user:is_player() then + return false + end + + local positions="" + local pos=user:get_pos() + for x=pos.x-self.distance,pos.x+self.distance do + for y=pos.y-self.distance,pos.y+self.distance do + for z=pos.z-self.distance,pos.z+self.distance do + local targetPos=vector.new(x,y,z) + local node=minetest.get_node(targetPos) + if self.isOre(node) then + positions=positions..string.format("%d %d %d",x,y,z) + positions=positions.."," + end + end + end + end + + local formspec={ + "formspec_version[4]", + "size[10.5,11]", + "label[0.5,0.5;"..self.description.."]", + "label[0.5,1.7;"..S("Found ores").."]", + "textlist[0.5,1.9;9.5,8.6;pos;"..positions.."]" + } + local content=table.concat(formspec,"") + minetest.show_formspec(user:get_player_name(),"industrialtest:scanner",content) + + return true +end + +function industrialtest.OreScanner.filter(self,pos) + local result={} + for x=pos.x-self.minerDistance,pos.x+self.minerDistance do + for z=pos.z-self.minerDistance,pos.z+self.minerDistance do + local nodePos=vector.new(x,pos.y,z) + local node=minetest.get_node(nodePos) + -- This is very hacky, but currently there is no other way of determining if node is ore + if (x~=pos.x or z~=pos.z) and self.isOre(node) then + table.insert(result,nodePos) + end + end + end + return result +end + +function industrialtest.OreScanner.isOre(node) + return string.find(node.name,":stone_with_") +end + +industrialtest.ODScanner=table.copy(industrialtest.OreScanner) +industrialtest.internal.unpackTableInto(industrialtest.ODScanner,{ + name="industrialtest:od_scanner", + description=S("OD Scanner"), + inventoryImage="industrialtest_od_scanner.png", + capacity=100000, + flow=industrialtest.api.lvPowerFlow, + distance=7, + minerDistance=3 +}) + +function industrialtest.ODScanner.getOpPower(self,itemstack) + return 700 +end + +industrialtest.ODScanner:register() + +minetest.register_craft({ + type="shaped", + output="industrialtest:od_scanner", + recipe={ + {"",industrialtest.elementKeys.yellowDust,""}, + {"industrialtest:electronic_circuit","industrialtest:re_battery","industrialtest:electronic_circuit"}, + {"industrialtest:insulated_copper_cable","industrialtest:insulated_copper_cable","industrialtest:insulated_copper_cable"} + } +}) + +industrialtest.OVScanner=table.copy(industrialtest.OreScanner) +industrialtest.internal.unpackTableInto(industrialtest.OVScanner,{ + name="industrialtest:ov_scanner", + description=S("OV Scanner"), + inventoryImage="industrialtest_ov_scanner.png", + capacity=1000000, + flow=industrialtest.api.mvPowerFlow, + distance=11, + minerDistance=5 +}) + +function industrialtest.OVScanner.getOpPower(self,itemstack) + return 7000 +end + +industrialtest.OVScanner:register() + +minetest.register_craft({ + type="shaped", + output="industrialtest:ov_scanner", + recipe={ + {"",industrialtest.elementKeys.yellowDust,""}, + {industrialtest.elementKeys.yellowDust,"industrialtest:electronic_circuit",industrialtest.elementKeys.yellowDust}, + {"industrialtest:insulated_gold_cable","industrialtest:od_scanner","industrialtest:insulated_gold_cable"} + } +}) + +minetest.register_craft({ + type="shaped", + output="industrialtest:ov_scanner", + recipe={ + {"",industrialtest.elementKeys.yellowDust,""}, + {industrialtest.elementKeys.yellowDust,"industrialtest:electronic_circuit",industrialtest.elementKeys.yellowDust}, + {"industrialtest:insulated_gold_cable","industrialtest:re_battery","industrialtest:insulated_gold_cable"} + } +})