From b383231c40d133681ab526c65952232e0c76f2aa Mon Sep 17 00:00:00 2001 From: mrkubax10 Date: Tue, 15 Apr 2025 22:47:44 +0200 Subject: [PATCH] Refactor electric sabers --- ...ndustrialtest_advanced_electric_saber.png} | Bin tools/activated_electric_tool.lua | 64 ++++-- tools/electric_hoe.lua | 10 +- tools/electric_saber.lua | 213 ++++++------------ tools/tool.lua | 24 +- 5 files changed, 141 insertions(+), 170 deletions(-) rename textures/{industrialtest_diamond_electric_saber.png => industrialtest_advanced_electric_saber.png} (100%) diff --git a/textures/industrialtest_diamond_electric_saber.png b/textures/industrialtest_advanced_electric_saber.png similarity index 100% rename from textures/industrialtest_diamond_electric_saber.png rename to textures/industrialtest_advanced_electric_saber.png diff --git a/tools/activated_electric_tool.lua b/tools/activated_electric_tool.lua index 426373b..8e8796e 100644 --- a/tools/activated_electric_tool.lua +++ b/tools/activated_electric_tool.lua @@ -24,22 +24,32 @@ function industrialtest.ActivatedElectricTool.createDefinitionTable(self) if self.digs then self:defineToolCapabilities(def,nil,self.digSpeed) end + if self.hits then + def.tool_capabilities.damage_groups={} + for _,hitType in ipairs(self.hits) do + def.tool_capabilities.damage_groups[hitType]=1 + end + end return def end function industrialtest.ActivatedElectricTool.createActiveDefinitionTable(self) local def=self:createDefinitionTable() - if self.digs then + + if self.digs or self.hits then self:defineToolCapabilities(def,self.active.times,self.active.digSpeed) end + def.groups.not_in_creative_inventory=1 + def.after_use=function(itemstack,user,node,digparams) if self:afterUse(itemstack,user,node,digparams) then return itemstack end return nil end + return def end @@ -55,9 +65,11 @@ function industrialtest.ActivatedElectricTool.beforeUse(self,itemstack,node) if not self.isActive(itemstack) then itemstack:set_name(itemstack:get_name().."_active") end + return true else itemstack:set_name(self.name) end + return false end function industrialtest.ActivatedElectricTool.afterUse(self,itemstack,user,node,digparams) @@ -95,21 +107,41 @@ function industrialtest.ActivatedElectricTool.defineToolCapabilities(self,def,ti end end end + if self.hits then + for _,hitType in ipairs(self.hits) do + if hitType=="fleshy" then + def.groups.sword=1 + end + end + end elseif industrialtest.mclAvailable then - local digTypeMapping={ - choppy="axey", - cracky="pickaxey", - crumbly="shovely", - hoey="hoey" - } - def.groups.dig_speed_class=self.digSpeedClass - def._mcl_diggroups={} - for _,digType in ipairs(self.digs) do - def._mcl_diggroups[digTypeMapping[digType]]={ - speed=digSpeed, - level=5-self.digLevel, - uses=-1 + if self.digs then + local digTypeMapping={ + choppy={"axey"}, + cracky={"pickaxey"}, + crumbly={"shovely"}, + hoey={"hoey"}, + snappy={"swordy","swordy_cobweb"} } + def.groups.dig_speed_class=self.digSpeedClass + def._mcl_diggroups={} + for _,digType in ipairs(self.digs) do + for _,mappedDigType in ipairs(digTypeMapping[digType]) do + def._mcl_diggroups[mappedDigType]={ + speed=digSpeed, + level=5-self.digLevel, + uses=-1 + } + end + end + end + if self.hits then + for _,hitType in ipairs(self.hits) do + if hitType=="fleshy" then + def.groups.weapon=1 + def.groups.sword=1 + end + end end end end @@ -121,8 +153,8 @@ minetest.register_on_punchnode(function(pos,node,user) local itemstack=user:get_wielded_item() local def=itemstack:get_definition() - if def and def._industrialtest_self and def.groups and def.groups._industrialtest_activatedElectricTool then - def._industrialtest_self:beforeUse(itemstack,node) + if def and def._industrialtest_self and def.groups and def.groups._industrialtest_activatedElectricTool and + def._industrialtest_self:beforeUse(itemstack,node) then user:set_wielded_item(itemstack) end end) diff --git a/tools/electric_hoe.lua b/tools/electric_hoe.lua index 24e4c20..4b3b116 100644 --- a/tools/electric_hoe.lua +++ b/tools/electric_hoe.lua @@ -22,16 +22,14 @@ industrialtest.internal.unpackTableInto(industrialtest.ElectricHoeBase,{ flow=industrialtest.api.lvPowerFlow }) -function industrialtest.ElectricHoeBase.createDefinitionTable(self) - local def=industrialtest.ActivatedElectricTool.createDefinitionTable(self) - +function industrialtest.ElectricHoeBase.register(self) if industrialtest.mtgAvailable then - def.on_place=minetest.item_place + self.define.onUse=true elseif industrialtest.mclAvailable then - def.on_use=nil + self.define.onPlace=true end - return def + industrialtest.ActivatedElectricTool.register(self) end function industrialtest.ElectricHoeBase.hitUse(self,itemstack,user,pointed) diff --git a/tools/electric_saber.lua b/tools/electric_saber.lua index ce2a9a4..d149812 100644 --- a/tools/electric_saber.lua +++ b/tools/electric_saber.lua @@ -14,162 +14,95 @@ -- You should have received a copy of the GNU General Public License -- along with this program. If not, see . +-- This entire code is super hacky, I'm aware. + local S=minetest.get_translator("industrialtest") +industrialtest.ElectricSaberBase=table.copy(industrialtest.ActivatedElectricTool) +industrialtest.internal.unpackTableInto(industrialtest.ElectricSaberBase,{ + fullPunchInterval=0.5, + digs={"snappy"}, + hits={"fleshy"}, + capacity=100000, + flow=industrialtest.api.hvPowerFlow +}) -local electricSaber={} - -electricSaber.afterUse=function(itemstack,config) - local meta=itemstack:get_meta() - industrialtest.api.addPowerToItem(itemstack,-20) - if meta:get_int("industrialtest.powerAmount")<20 then - itemstack:set_name("industrialtest:"..config.name) - end - return itemstack +function industrialtest.ElectricSaberBase.createActiveDefinitionTable(self) + self.define.onUse=false + return industrialtest.ActivatedElectricTool.createActiveDefinitionTable(self) end -local function registerElectricSaber(config) - local definition={ - description=config.displayName, - inventory_image="industrialtest_"..config.name..".png", - after_use=function() - -- Hack to make sure that saber won't be destroyed when has 0 EU - return nil - end, - _industrialtest_powerStorage=true, - _industrialtest_powerCapacity=10000, - _industrialtest_powerFlow=industrialtest.api.lvPowerFlow, - _industrialtest_inactiveName="industrialtest:"..config.name +function industrialtest.ElectricSaberBase.hitUse(self,itemstack,user,pointed) + if self:canActivate(itemstack) and pointed.type=="object" then + -- pointed is a ObjectRef + industrialtest.api.addPowerToItem(itemstack,-self:getOpPower(itemstack)) + local toolCapabilities={damage_groups={}} + for _,hitType in ipairs(self.hits) do + toolCapabilities.damage_groups[hitType]=self.active.damage + end + pointed.ref:punch(user,nil,toolCapabilities) + return true + end + return false +end + +function industrialtest.ElectricSaberBase.afterUse(self,itemstack,user,node,digparams) + industrialtest.ActivatedElectricTool.afterUse(self,itemstack,user,node,digparams) + itemstack:set_name(self.name) + return true +end + +function industrialtest.ElectricSaberBase.getOpPower(self,itemstack) + return 500 +end + +industrialtest.ElectricSaber=table.copy(industrialtest.ElectricSaberBase) +industrialtest.internal.unpackTableInto(industrialtest.ElectricSaber,{ + name="industrialtest:electric_saber", + description=S("Electric Saber"), + inventoryImage="industrialtest_electric_saber.png", + digLevel=0, + digSpeedClass=4, + active={ + digSpeed=7, + times={[1]=0.7,[2]=0.5,[3]=0.2,[4]=0.15}, + damage=3 } - if industrialtest.mtgAvailable then - definition.groups={ - sword=1 - } - definition.tool_capabilities={ - full_punch_interval=0.5, - max_drop_level=config.maxDropLevel, - groupcaps={ - snappy={ - times=config.inactiveTimes, - maxlevel=config.maxLevel - } - }, - damage_groups={fleshy=config.inactiveDamage} - } - elseif industrialtest.mclAvailable then - definition.groups={ - weapon=1, - sword=1, - dig_speed_class=config.digSpeedClass - } - definition.tool_capabilities={ - full_punch_interval=0.5, - max_drop_level=config.maxDropLevel, - damage_groups={fleshy=config.inactiveDamage} - } - definition._mcl_toollike_wield=true - definition._mcl_diggroups={ - swordy={ - speed=config.inactiveDigSpeed, - level=config.digLevel, - uses=-1 - }, - swordy_cobweb={ - speed=config.inactiveDigSpeed, - level=config.digLevel, - uses=-1 - } - } - end - minetest.register_tool("industrialtest:"..config.name,definition) - definition=table.copy(definition) - if industrialtest.mtgAvailable then - definition.tool_capabilities.groupcaps.snappy.times=config.activeTimes - definition.tool_capabilities.damage_groups.fleshy=config.activeDamage - elseif industrialtest.mclAvailable then - definition.tool_capabilities.damage_groups.fleshy=config.activeDamage - definition._mcl_diggroups.swordy.speed=config.activeDigSpeed - definition._mcl_diggroups.swordy_cobweb.speed=config.activeDigSpeed - end - definition.groups.not_in_creative_inventory=1 - definition.after_use=function(itemstack) - return electricSaber.afterUse(itemstack,config) - end - minetest.register_tool("industrialtest:"..config.name.."_active",definition) - industrialtest.internal.registeredElectricSabers["industrialtest:"..config.name]=true - industrialtest.internal.registeredElectricSabers["industrialtest:"..config.name.."_active"]=true -end -local definition={ - name="electric_saber", - displayName=S("Electric Saber") -} -if industrialtest.mtgAvailable then - definition.maxDropLevel=1 - definition.inactiveTimes={[1]=10,[2]=5.6,[3]=4} - definition.maxLevel=3 - definition.inactiveDamage=1 - definition.activeTimes={[1]=2.0,[2]=0.8,[3]=0.4} - definition.activeDamage=6 -elseif industrialtest.mclAvailable then - definition.digSpeedClass=4 - definition.maxDropLevel=4 - definition.inactiveDamage=1 - definition.inactiveDigSpeed=1 - definition.digLevel=4 - definition.activeDamage=6 - definition.activeDigSpeed=7 -end -registerElectricSaber(definition) +}) + +industrialtest.ElectricSaber:register() + minetest.register_craft({ type="shaped", output="industrialtest:electric_saber", recipe={ - {"industrialtest:refined_iron_ingot"}, - {"industrialtest:refined_iron_ingot"}, - {"industrialtest:re_battery"} + {industrialtest.elementKeys.yellowDust,"industrialtest:advanced_alloy",""}, + {industrialtest.elementKeys.yellowDust,"industrialtest:advanced_alloy",""}, + {"industrialtest:carbon_plate","industrialtest:energy_crystal","industrialtest:carbon_plate"} } }) -definition={ - name="diamond_electric_saber", - displayName=S("Diamond Electric Saber") -} -if industrialtest.mtgAvailable then - definition.maxDropLevel=1 - definition.inactiveTimes={[1]=10,[2]=5.6,[3]=4} - definition.maxLevel=3 - definition.inactiveDamage=1 - definition.activeTimes={[1]=2.0,[2]=0.8,[3]=0.4} - definition.activeDamage=9 -elseif industrialtest.mclAvailable then - definition.digSpeedClass=5 - definition.maxDropLevel=5 - definition.inactiveDamage=1 - definition.inactiveDigSpeed=1 - definition.digLevel=5 - definition.activeDamage=9 - definition.activeDigSpeed=9 -end -registerElectricSaber(definition) + +industrialtest.AdvancedElectricSaber=table.copy(industrialtest.ElectricSaberBase) +industrialtest.internal.unpackTableInto(industrialtest.AdvancedElectricSaber,{ + name="industrialtest:advanced_electric_saber", + description=S("Advanced Electric Saber"), + inventoryImage="industrialtest_advanced_electric_saber.png", + digLevel=0, + digSpeedClass=5, + active={ + digSpeed=8, + times={[1]=0.5,[2]=0.2,[3]=0.15,[4]=0.1}, + damage=5 + } +}) + +industrialtest.AdvancedElectricSaber:register() + minetest.register_craft({ type="shapeless", - output="industrialtest:diamond_electric_saber", + output="industrialtest:advanced_electric_saber", recipe={ "industrialtest:electric_saber", industrialtest.elementKeys.diamond, industrialtest.elementKeys.diamond } }) - -minetest.register_on_punchplayer(function(player,hitter) - local itemstack=hitter:get_wielded_item() - if industrialtest.internal.registeredElectricSabers[itemstack:get_name()] then - local meta=itemstack:get_meta() - if meta:get_int("industrialtest.powerAmount")>=20 then - industrialtest.api.addPowerToItem(itemstack,-20) - hitter:set_wielded_item(itemstack) - local def=minetest.registered_tools[itemstack:get_name().."_active"] - player:set_hp(player:get_hp()-def.tool_capabilites.damage_groups.fleshy) - return true - end - end - return false -end) diff --git a/tools/tool.lua b/tools/tool.lua index ff9f1f7..6c2c502 100644 --- a/tools/tool.lua +++ b/tools/tool.lua @@ -15,6 +15,9 @@ -- along with this program. If not, see . industrialtest.Tool=table.copy(industrialtest.Item) +industrialtest.internal.unpackTableInto(industrialtest.Tool,{ + define={} +}) function industrialtest.Tool.use(self,itemstack,user,pointed) -- dummy function @@ -22,6 +25,7 @@ function industrialtest.Tool.use(self,itemstack,user,pointed) end function industrialtest.Tool.hitUse(self,itemstack,user,pointed) + -- Note: this method, when used, onUse must be set in define table -- dummy function return false end @@ -59,17 +63,21 @@ function industrialtest.Tool.createDefinitionTable(self) def.tool_capabilities={ uses=self.uses } - def.on_place=function(itemstack,user,pointed) - if self:onPlace(itemstack,user,pointed) then - return itemstack + if self.define.onPlace then + def.on_place=function(itemstack,user,pointed) + if self:onPlace(itemstack,user,pointed) then + return itemstack + end + return nil end - return nil end - def.on_use=function(itemstack,user,pointed) - if self:onUse(itemstack,user,pointed) then - return itemstack + if self.define.onUse then + def.on_use=function(itemstack,user,pointed) + if self:onUse(itemstack,user,pointed) then + return itemstack + end + return nil end - return nil end if industrialtest.mclAvailable then