Refactor electric chainsaws
This commit is contained in:
parent
dfdce73376
commit
b108ce7c6a
1
init.lua
1
init.lua
@ -69,6 +69,7 @@ dofile(modpath.."/tools/electric_item.lua")
|
|||||||
dofile(modpath.."/tools/tool.lua")
|
dofile(modpath.."/tools/tool.lua")
|
||||||
dofile(modpath.."/tools/gear_tool.lua")
|
dofile(modpath.."/tools/gear_tool.lua")
|
||||||
dofile(modpath.."/tools/electric_tool.lua")
|
dofile(modpath.."/tools/electric_tool.lua")
|
||||||
|
dofile(modpath.."/tools/activated_electric_tool.lua")
|
||||||
dofile(modpath.."/tools/electric_gear_tool.lua")
|
dofile(modpath.."/tools/electric_gear_tool.lua")
|
||||||
dofile(modpath.."/tools/batpack.lua")
|
dofile(modpath.."/tools/batpack.lua")
|
||||||
dofile(modpath.."/tools/electric_chainsaw.lua")
|
dofile(modpath.."/tools/electric_chainsaw.lua")
|
||||||
|
Before Width: | Height: | Size: 10 KiB After Width: | Height: | Size: 10 KiB |
117
tools/activated_electric_tool.lua
Normal file
117
tools/activated_electric_tool.lua
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
-- 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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
industrialtest.ActivatedElectricTool=table.copy(industrialtest.ElectricTool)
|
||||||
|
|
||||||
|
function industrialtest.ActivatedElectricTool.createDefinitionTable(self)
|
||||||
|
local def=industrialtest.ElectricTool.createDefinitionTable(self)
|
||||||
|
|
||||||
|
def.groups._industrialtest_activatedElectricTool=1
|
||||||
|
def.tool_capabilities.full_punch_interval=self.fullPunchInterval
|
||||||
|
if self.digs then
|
||||||
|
self:defineToolCapabilities(def,nil,self.digSpeed)
|
||||||
|
end
|
||||||
|
|
||||||
|
return def
|
||||||
|
end
|
||||||
|
|
||||||
|
function industrialtest.ActivatedElectricTool.createActiveDefinitionTable(self)
|
||||||
|
local def=self:createDefinitionTable()
|
||||||
|
if self.digs 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
|
||||||
|
|
||||||
|
function industrialtest.ActivatedElectricTool.register(self)
|
||||||
|
industrialtest.ElectricTool.register(self)
|
||||||
|
local def=self:createActiveDefinitionTable()
|
||||||
|
minetest.register_tool(self.name.."_active",def)
|
||||||
|
end
|
||||||
|
|
||||||
|
function industrialtest.ActivatedElectricTool.beforeUse(self,itemstack,node)
|
||||||
|
local def=itemstack:get_definition()
|
||||||
|
if self:canActivate(itemstack,node) then
|
||||||
|
if not self.isActive(itemstack) then
|
||||||
|
itemstack:set_name(itemstack:get_name().."_active")
|
||||||
|
end
|
||||||
|
else
|
||||||
|
itemstack:set_name(self.name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function industrialtest.ActivatedElectricTool.afterUse(self,itemstack,user,node,digparams)
|
||||||
|
local meta=itemstack:get_meta()
|
||||||
|
industrialtest.api.addPowerToItem(itemstack,-self:getOpPower(itemstack))
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
function industrialtest.ActivatedElectricTool.canActivate(self,itemstack,node)
|
||||||
|
local meta=itemstack:get_meta()
|
||||||
|
return meta:get_int("industrialtest.powerAmount")>=self:getOpPower(itemstack)
|
||||||
|
end
|
||||||
|
|
||||||
|
function industrialtest.ActivatedElectricTool.isActive(itemstack)
|
||||||
|
return string.sub(itemstack:get_name(),-string.len("_active"),-1)=="_active"
|
||||||
|
end
|
||||||
|
|
||||||
|
function industrialtest.ActivatedElectricTool.defineToolCapabilities(self,def,times,digSpeed)
|
||||||
|
if industrialtest.mtgAvailable then
|
||||||
|
if self.digs then
|
||||||
|
def.tool_capabilities.groupcaps={
|
||||||
|
[self.digs]={
|
||||||
|
times=times,
|
||||||
|
maxlevel=self.digLevel
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if self.digs=="choppy" then
|
||||||
|
def.groups.axe=1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
elseif industrialtest.mclAvailable then
|
||||||
|
local digTypeMapping={
|
||||||
|
choppy="axey"
|
||||||
|
}
|
||||||
|
def.groups.dig_speed_class=self.digSpeedClass
|
||||||
|
def._mcl_diggroups={
|
||||||
|
[digTypeMapping[self.digs]]={
|
||||||
|
speed=digSpeed,
|
||||||
|
level=self.digLevel+4,
|
||||||
|
uses=-1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
minetest.register_on_punchnode(function(pos,node,user)
|
||||||
|
if not user then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
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)
|
||||||
|
user:set_wielded_item(itemstack)
|
||||||
|
end
|
||||||
|
end)
|
@ -19,23 +19,6 @@ industrialtest.internal.registeredElectricDrills={}
|
|||||||
industrialtest.internal.registeredElectricHoes={}
|
industrialtest.internal.registeredElectricHoes={}
|
||||||
industrialtest.internal.registeredElectricSabers={}
|
industrialtest.internal.registeredElectricSabers={}
|
||||||
|
|
||||||
local function isActive(itemstack)
|
|
||||||
return string.sub(itemstack:get_name(),-string.len("_active"),-1)=="_active"
|
|
||||||
end
|
|
||||||
|
|
||||||
local function beforeUse(user,itemstack,canDig)
|
|
||||||
local meta=itemstack:get_meta()
|
|
||||||
local def=itemstack:get_definition()
|
|
||||||
if meta:get_int("industrialtest.powerAmount")>=20 and canDig then
|
|
||||||
if not isActive(itemstack) then
|
|
||||||
itemstack:set_name(itemstack:get_name().."_active")
|
|
||||||
end
|
|
||||||
else
|
|
||||||
itemstack:set_name(def._industrialtest_inactiveName)
|
|
||||||
end
|
|
||||||
user:set_wielded_item(itemstack)
|
|
||||||
end
|
|
||||||
|
|
||||||
minetest.register_on_punchnode(function(pos,node,user,pointed)
|
minetest.register_on_punchnode(function(pos,node,user,pointed)
|
||||||
if not user then
|
if not user then
|
||||||
return
|
return
|
||||||
|
@ -15,107 +15,42 @@
|
|||||||
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
|
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
local S=minetest.get_translator("industrialtest")
|
local S=minetest.get_translator("industrialtest")
|
||||||
|
industrialtest.ElectricChainsawBase=table.copy(industrialtest.ActivatedElectricTool)
|
||||||
|
industrialtest.internal.unpackTableInto(industrialtest.ElectricChainsawBase,{
|
||||||
|
digs="choppy"
|
||||||
|
})
|
||||||
|
|
||||||
local electricChainsaw={}
|
function industrialtest.ElectricChainsawBase.use(self,itemstack,user,pointed)
|
||||||
|
if not industrialtest.mclAvailable then
|
||||||
electricChainsaw.afterUse=function(itemstack,config)
|
return false
|
||||||
|
end
|
||||||
local meta=itemstack:get_meta()
|
local meta=itemstack:get_meta()
|
||||||
industrialtest.api.addPowerToItem(itemstack,-20)
|
local itemstackCopy=itemstack
|
||||||
if meta:get_int("industrialtest.powerAmount")<20 then
|
return itemstack:get_wear()~=industrialtest.internal.mclMakeStrippedTrunk(itemstackCopy,user,pointed,true):get_wear()
|
||||||
itemstack:set_name("industrialtest:"..config.name)
|
|
||||||
end
|
|
||||||
return itemstack
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function registerElectricChainsaw(config)
|
function industrialtest.ElectricChainsawBase.getOpPower(self,itemstack)
|
||||||
local definition={
|
return 50
|
||||||
description=config.displayName,
|
end
|
||||||
inventory_image="industrialtest_"..config.name..".png",
|
|
||||||
after_use=function(itemstack)
|
industrialtest.ElectricChainsaw=table.copy(industrialtest.ElectricChainsawBase)
|
||||||
-- Hack to make sure that chainsaw won't be destroyed when has 0 EU
|
industrialtest.internal.unpackTableInto(industrialtest.ElectricChainsaw,{
|
||||||
return nil
|
name="industrialtest:electric_chainsaw",
|
||||||
end,
|
description=S("Electric Chainsaw"),
|
||||||
_industrialtest_powerStorage=true,
|
inventoryImage="industrialtest_electric_chainsaw.png",
|
||||||
_industrialtest_powerCapacity=10000,
|
capacity=10000,
|
||||||
_industrialtest_powerFlow=industrialtest.api.lvPowerFlow,
|
flow=industrialtest.api.lvPowerFlow,
|
||||||
_industrialtest_inactiveName="industrialtest:"..config.name
|
digLevel=1,
|
||||||
|
digSpeedClass=4,
|
||||||
|
digSpeed=1,
|
||||||
|
active={
|
||||||
|
times={[1]=1.7,[2]=1.2,[3]=1.0,[4]=0.7},
|
||||||
|
digSpeed=7
|
||||||
}
|
}
|
||||||
if industrialtest.mtgAvailable then
|
})
|
||||||
definition.tool_capabilities={
|
|
||||||
full_punch_interval=0.5,
|
industrialtest.ElectricChainsaw:register()
|
||||||
max_drop_level=config.maxDropLevel,
|
|
||||||
groupcaps={
|
|
||||||
choppy={
|
|
||||||
times=config.inactiveTimes,
|
|
||||||
maxlevel=config.maxLevel
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
definition.groups={
|
|
||||||
axe=1
|
|
||||||
}
|
|
||||||
elseif industrialtest.mclAvailable then
|
|
||||||
definition.tool_capabilities={
|
|
||||||
full_punch_interval=0.5,
|
|
||||||
max_drop_level=config.maxDropLevel,
|
|
||||||
}
|
|
||||||
definition.groups={
|
|
||||||
tool=1,
|
|
||||||
dig_speed_class=config.digSpeedClass,
|
|
||||||
}
|
|
||||||
definition.on_place=function(itemstack,user,pointed)
|
|
||||||
local meta=itemstack:get_meta()
|
|
||||||
if meta:get_int("industrialtest.powerAmount")>=20 then
|
|
||||||
local itemstackCopy=itemstack
|
|
||||||
if itemstack:get_wear()~=industrialtest.internal.mclMakeStrippedTrunk(itemstackCopy,user,pointed,true):get_wear() then
|
|
||||||
industrialtest.api.addPowerToItem(itemstack,-20)
|
|
||||||
return itemstack
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
definition._mcl_diggroups={
|
|
||||||
axey={
|
|
||||||
speed=config.inactiveDigSpeed,
|
|
||||||
level=config.digLevel,
|
|
||||||
uses=-1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
definition._mcl_toollike_wield=true
|
|
||||||
end
|
|
||||||
minetest.register_tool("industrialtest:"..config.name,definition)
|
|
||||||
definition=table.copy(definition)
|
|
||||||
if industrialtest.mtgAvailable then
|
|
||||||
definition.tool_capabilities.groupcaps.choppy.times=config.activeTimes
|
|
||||||
elseif industrialtest.mclAvailable then
|
|
||||||
definition._mcl_diggroups.axey.speed=config.activeDigSpeed
|
|
||||||
end
|
|
||||||
definition.groups.not_in_creative_inventory=1
|
|
||||||
definition.on_use=nil
|
|
||||||
definition.after_use=function(itemstack)
|
|
||||||
return electricChainsaw.afterUse(itemstack,config)
|
|
||||||
end
|
|
||||||
minetest.register_tool("industrialtest:"..config.name.."_active",definition)
|
|
||||||
industrialtest.internal.registeredElectricChainsaws["industrialtest:"..config.name]=true
|
|
||||||
industrialtest.internal.registeredElectricChainsaws["industrialtest:"..config.name.."_active"]=true
|
|
||||||
end
|
|
||||||
local definition={
|
|
||||||
name="electric_chainsaw",
|
|
||||||
displayName=S("Electric Chainsaw")
|
|
||||||
}
|
|
||||||
if industrialtest.mtgAvailable then
|
|
||||||
definition.maxDropLevel=1
|
|
||||||
definition.inactiveTimes={[1]=10,[2]=5.6,[3]=4}
|
|
||||||
definition.maxLevel=2
|
|
||||||
definition.activeTimes={[1]=2.2,[2]=1.1,[3]=0.7}
|
|
||||||
elseif industrialtest.mclAvailable then
|
|
||||||
definition.digSpeedClass=4
|
|
||||||
definition.maxDropLevel=4
|
|
||||||
definition.inactiveDigSpeed=1
|
|
||||||
definition.digLevel=4
|
|
||||||
definition.activeDigSpeed=7
|
|
||||||
end
|
|
||||||
registerElectricChainsaw(definition)
|
|
||||||
minetest.register_craft({
|
minetest.register_craft({
|
||||||
type="shaped",
|
type="shaped",
|
||||||
output="industrialtest:electric_chainsaw",
|
output="industrialtest:electric_chainsaw",
|
||||||
@ -125,26 +60,28 @@ minetest.register_craft({
|
|||||||
{"industrialtest:re_battery","industrialtest:refined_iron_ingot",""}
|
{"industrialtest:re_battery","industrialtest:refined_iron_ingot",""}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
definition={
|
|
||||||
name="diamond_electric_chainsaw",
|
industrialtest.AdvancedElectricChainsaw=table.copy(industrialtest.ElectricChainsawBase)
|
||||||
displayName=S("Diamond Electric Chainsaw")
|
industrialtest.internal.unpackTableInto(industrialtest.AdvancedElectricChainsaw,{
|
||||||
}
|
name="industrialtest:advanced_electric_chainsaw",
|
||||||
if industrialtest.mtgAvailable then
|
description=S("Advanced Electric Chainsaw"),
|
||||||
definition.maxDropLevel=1
|
inventoryImage="industrialtest_advanced_electric_chainsaw.png",
|
||||||
definition.inactiveTimes={[1]=10,[2]=5.6,[3]=4}
|
capacity=10000,
|
||||||
definition.maxLevel=3
|
flow=industrialtest.api.lvPowerFlow,
|
||||||
definition.activeTimes={[1]=2.0,[2]=0.8,[3]=0.4}
|
digLevel=1,
|
||||||
elseif industrialtest.mclAvailable then
|
digSpeedClass=5,
|
||||||
definition.digSpeedClass=5
|
digSpeed=1,
|
||||||
definition.maxDropLevel=5
|
active={
|
||||||
definition.inactiveDigSpeed=1
|
times={[1]=1.0,[2]=0.7,[3]=0.5,[4]=0.2},
|
||||||
definition.digLevel=5
|
digSpeed=8
|
||||||
definition.activeDigSpeed=9
|
}
|
||||||
end
|
})
|
||||||
registerElectricChainsaw(definition)
|
|
||||||
|
industrialtest.AdvancedElectricChainsaw:register()
|
||||||
|
|
||||||
minetest.register_craft({
|
minetest.register_craft({
|
||||||
type="shaped",
|
type="shaped",
|
||||||
output="industrialtest:diamond_electric_chainsaw",
|
output="industrialtest:advanced_electric_chainsaw",
|
||||||
recipe={
|
recipe={
|
||||||
{"",industrialtest.elementKeys.diamond,""},
|
{"",industrialtest.elementKeys.diamond,""},
|
||||||
{industrialtest.elementKeys.diamond,"industrialtest:electric_chainsaw",industrialtest.elementKeys.diamond}
|
{industrialtest.elementKeys.diamond,"industrialtest:electric_chainsaw",industrialtest.elementKeys.diamond}
|
||||||
|
@ -19,11 +19,20 @@ industrialtest.internal.unpackTableInto(industrialtest.ElectricTool,{
|
|||||||
prepare=industrialtest.ElectricItem.prepare
|
prepare=industrialtest.ElectricItem.prepare
|
||||||
})
|
})
|
||||||
|
|
||||||
|
function industrialtest.ElectricTool.createDefinitionTable(self)
|
||||||
|
local def=industrialtest.Tool.createDefinitionTable(self)
|
||||||
|
def.after_use=function()
|
||||||
|
-- Hack to make sure that chainsaw won't be destroyed when has 0 EU
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
return def
|
||||||
|
end
|
||||||
|
|
||||||
function industrialtest.ElectricTool.onPlace(self,itemstack,user,pointed)
|
function industrialtest.ElectricTool.onPlace(self,itemstack,user,pointed)
|
||||||
self:prepare(itemstack)
|
self:prepare(itemstack)
|
||||||
|
|
||||||
local meta=itemstack:get_meta()
|
local meta=itemstack:get_meta()
|
||||||
local opPower=self:getOpPower()
|
local opPower=self:getOpPower(itemstack)
|
||||||
if meta:get_int("industrialtest.powerAmount")<opPower then
|
if meta:get_int("industrialtest.powerAmount")<opPower then
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
@ -52,7 +52,6 @@ end
|
|||||||
function industrialtest.Tool.createDefinitionTable(self)
|
function industrialtest.Tool.createDefinitionTable(self)
|
||||||
local def=industrialtest.Item.createDefinitionTable(self)
|
local def=industrialtest.Item.createDefinitionTable(self)
|
||||||
def.tool_capabilities={
|
def.tool_capabilities={
|
||||||
full_punch_interval=1,
|
|
||||||
uses=self.uses
|
uses=self.uses
|
||||||
}
|
}
|
||||||
def.on_place=function(itemstack,user,pointed)
|
def.on_place=function(itemstack,user,pointed)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user