Refactor electric sabers

This commit is contained in:
mrkubax10 2025-04-15 22:47:44 +02:00
parent 6f864eca78
commit b383231c40
5 changed files with 141 additions and 170 deletions

View File

Before

Width:  |  Height:  |  Size: 686 B

After

Width:  |  Height:  |  Size: 686 B

View File

@ -24,22 +24,32 @@ function industrialtest.ActivatedElectricTool.createDefinitionTable(self)
if self.digs then if self.digs then
self:defineToolCapabilities(def,nil,self.digSpeed) self:defineToolCapabilities(def,nil,self.digSpeed)
end 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 return def
end end
function industrialtest.ActivatedElectricTool.createActiveDefinitionTable(self) function industrialtest.ActivatedElectricTool.createActiveDefinitionTable(self)
local def=self:createDefinitionTable() local def=self:createDefinitionTable()
if self.digs then
if self.digs or self.hits then
self:defineToolCapabilities(def,self.active.times,self.active.digSpeed) self:defineToolCapabilities(def,self.active.times,self.active.digSpeed)
end end
def.groups.not_in_creative_inventory=1 def.groups.not_in_creative_inventory=1
def.after_use=function(itemstack,user,node,digparams) def.after_use=function(itemstack,user,node,digparams)
if self:afterUse(itemstack,user,node,digparams) then if self:afterUse(itemstack,user,node,digparams) then
return itemstack return itemstack
end end
return nil return nil
end end
return def return def
end end
@ -55,9 +65,11 @@ function industrialtest.ActivatedElectricTool.beforeUse(self,itemstack,node)
if not self.isActive(itemstack) then if not self.isActive(itemstack) then
itemstack:set_name(itemstack:get_name().."_active") itemstack:set_name(itemstack:get_name().."_active")
end end
return true
else else
itemstack:set_name(self.name) itemstack:set_name(self.name)
end end
return false
end end
function industrialtest.ActivatedElectricTool.afterUse(self,itemstack,user,node,digparams) function industrialtest.ActivatedElectricTool.afterUse(self,itemstack,user,node,digparams)
@ -95,21 +107,41 @@ function industrialtest.ActivatedElectricTool.defineToolCapabilities(self,def,ti
end end
end 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 elseif industrialtest.mclAvailable then
local digTypeMapping={ if self.digs then
choppy="axey", local digTypeMapping={
cracky="pickaxey", choppy={"axey"},
crumbly="shovely", cracky={"pickaxey"},
hoey="hoey" crumbly={"shovely"},
} hoey={"hoey"},
def.groups.dig_speed_class=self.digSpeedClass snappy={"swordy","swordy_cobweb"}
def._mcl_diggroups={}
for _,digType in ipairs(self.digs) do
def._mcl_diggroups[digTypeMapping[digType]]={
speed=digSpeed,
level=5-self.digLevel,
uses=-1
} }
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 end
end end
@ -121,8 +153,8 @@ minetest.register_on_punchnode(function(pos,node,user)
local itemstack=user:get_wielded_item() local itemstack=user:get_wielded_item()
local def=itemstack:get_definition() local def=itemstack:get_definition()
if def and def._industrialtest_self and def.groups and def.groups._industrialtest_activatedElectricTool then if def and def._industrialtest_self and def.groups and def.groups._industrialtest_activatedElectricTool and
def._industrialtest_self:beforeUse(itemstack,node) def._industrialtest_self:beforeUse(itemstack,node) then
user:set_wielded_item(itemstack) user:set_wielded_item(itemstack)
end end
end) end)

View File

@ -22,16 +22,14 @@ industrialtest.internal.unpackTableInto(industrialtest.ElectricHoeBase,{
flow=industrialtest.api.lvPowerFlow flow=industrialtest.api.lvPowerFlow
}) })
function industrialtest.ElectricHoeBase.createDefinitionTable(self) function industrialtest.ElectricHoeBase.register(self)
local def=industrialtest.ActivatedElectricTool.createDefinitionTable(self)
if industrialtest.mtgAvailable then if industrialtest.mtgAvailable then
def.on_place=minetest.item_place self.define.onUse=true
elseif industrialtest.mclAvailable then elseif industrialtest.mclAvailable then
def.on_use=nil self.define.onPlace=true
end end
return def industrialtest.ActivatedElectricTool.register(self)
end end
function industrialtest.ElectricHoeBase.hitUse(self,itemstack,user,pointed) function industrialtest.ElectricHoeBase.hitUse(self,itemstack,user,pointed)

View File

@ -14,162 +14,95 @@
-- You should have received a copy of the GNU General Public License -- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>. -- along with this program. If not, see <http://www.gnu.org/licenses/>.
-- This entire code is super hacky, I'm aware.
local S=minetest.get_translator("industrialtest") 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={} function industrialtest.ElectricSaberBase.createActiveDefinitionTable(self)
self.define.onUse=false
electricSaber.afterUse=function(itemstack,config) return industrialtest.ActivatedElectricTool.createActiveDefinitionTable(self)
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
end end
local function registerElectricSaber(config) function industrialtest.ElectricSaberBase.hitUse(self,itemstack,user,pointed)
local definition={ if self:canActivate(itemstack) and pointed.type=="object" then
description=config.displayName, -- pointed is a ObjectRef
inventory_image="industrialtest_"..config.name..".png", industrialtest.api.addPowerToItem(itemstack,-self:getOpPower(itemstack))
after_use=function() local toolCapabilities={damage_groups={}}
-- Hack to make sure that saber won't be destroyed when has 0 EU for _,hitType in ipairs(self.hits) do
return nil toolCapabilities.damage_groups[hitType]=self.active.damage
end, end
_industrialtest_powerStorage=true, pointed.ref:punch(user,nil,toolCapabilities)
_industrialtest_powerCapacity=10000, return true
_industrialtest_powerFlow=industrialtest.api.lvPowerFlow, end
_industrialtest_inactiveName="industrialtest:"..config.name 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 industrialtest.ElectricSaber:register()
}
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)
minetest.register_craft({ minetest.register_craft({
type="shaped", type="shaped",
output="industrialtest:electric_saber", output="industrialtest:electric_saber",
recipe={ recipe={
{"industrialtest:refined_iron_ingot"}, {industrialtest.elementKeys.yellowDust,"industrialtest:advanced_alloy",""},
{"industrialtest:refined_iron_ingot"}, {industrialtest.elementKeys.yellowDust,"industrialtest:advanced_alloy",""},
{"industrialtest:re_battery"} {"industrialtest:carbon_plate","industrialtest:energy_crystal","industrialtest:carbon_plate"}
} }
}) })
definition={
name="diamond_electric_saber", industrialtest.AdvancedElectricSaber=table.copy(industrialtest.ElectricSaberBase)
displayName=S("Diamond Electric Saber") industrialtest.internal.unpackTableInto(industrialtest.AdvancedElectricSaber,{
} name="industrialtest:advanced_electric_saber",
if industrialtest.mtgAvailable then description=S("Advanced Electric Saber"),
definition.maxDropLevel=1 inventoryImage="industrialtest_advanced_electric_saber.png",
definition.inactiveTimes={[1]=10,[2]=5.6,[3]=4} digLevel=0,
definition.maxLevel=3 digSpeedClass=5,
definition.inactiveDamage=1 active={
definition.activeTimes={[1]=2.0,[2]=0.8,[3]=0.4} digSpeed=8,
definition.activeDamage=9 times={[1]=0.5,[2]=0.2,[3]=0.15,[4]=0.1},
elseif industrialtest.mclAvailable then damage=5
definition.digSpeedClass=5 }
definition.maxDropLevel=5 })
definition.inactiveDamage=1
definition.inactiveDigSpeed=1 industrialtest.AdvancedElectricSaber:register()
definition.digLevel=5
definition.activeDamage=9
definition.activeDigSpeed=9
end
registerElectricSaber(definition)
minetest.register_craft({ minetest.register_craft({
type="shapeless", type="shapeless",
output="industrialtest:diamond_electric_saber", output="industrialtest:advanced_electric_saber",
recipe={ recipe={
"industrialtest:electric_saber", "industrialtest:electric_saber",
industrialtest.elementKeys.diamond, industrialtest.elementKeys.diamond,
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)

View File

@ -15,6 +15,9 @@
-- along with this program. If not, see <http://www.gnu.org/licenses/>. -- along with this program. If not, see <http://www.gnu.org/licenses/>.
industrialtest.Tool=table.copy(industrialtest.Item) industrialtest.Tool=table.copy(industrialtest.Item)
industrialtest.internal.unpackTableInto(industrialtest.Tool,{
define={}
})
function industrialtest.Tool.use(self,itemstack,user,pointed) function industrialtest.Tool.use(self,itemstack,user,pointed)
-- dummy function -- dummy function
@ -22,6 +25,7 @@ function industrialtest.Tool.use(self,itemstack,user,pointed)
end end
function industrialtest.Tool.hitUse(self,itemstack,user,pointed) function industrialtest.Tool.hitUse(self,itemstack,user,pointed)
-- Note: this method, when used, onUse must be set in define table
-- dummy function -- dummy function
return false return false
end end
@ -59,17 +63,21 @@ function industrialtest.Tool.createDefinitionTable(self)
def.tool_capabilities={ def.tool_capabilities={
uses=self.uses uses=self.uses
} }
def.on_place=function(itemstack,user,pointed) if self.define.onPlace then
if self:onPlace(itemstack,user,pointed) then def.on_place=function(itemstack,user,pointed)
return itemstack if self:onPlace(itemstack,user,pointed) then
return itemstack
end
return nil
end end
return nil
end end
def.on_use=function(itemstack,user,pointed) if self.define.onUse then
if self:onUse(itemstack,user,pointed) then def.on_use=function(itemstack,user,pointed)
return itemstack if self:onUse(itemstack,user,pointed) then
return itemstack
end
return nil
end end
return nil
end end
if industrialtest.mclAvailable then if industrialtest.mclAvailable then