Refactor batpacks
This commit is contained in:
parent
6147074e05
commit
dfdce73376
4
init.lua
4
init.lua
@ -64,8 +64,12 @@ dofile(modpath.."/machines/solar_panel_generator.lua")
|
||||
dofile(modpath.."/machines/wind_mill.lua")
|
||||
|
||||
dofile(modpath.."/tools/common.lua")
|
||||
dofile(modpath.."/tools/item.lua")
|
||||
dofile(modpath.."/tools/electric_item.lua")
|
||||
dofile(modpath.."/tools/tool.lua")
|
||||
dofile(modpath.."/tools/gear_tool.lua")
|
||||
dofile(modpath.."/tools/electric_tool.lua")
|
||||
dofile(modpath.."/tools/electric_gear_tool.lua")
|
||||
dofile(modpath.."/tools/batpack.lua")
|
||||
dofile(modpath.."/tools/electric_chainsaw.lua")
|
||||
dofile(modpath.."/tools/electric_drill.lua")
|
||||
|
@ -15,72 +15,37 @@
|
||||
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
local S=minetest.get_translator("industrialtest")
|
||||
industrialtest.BatPackBase=table.copy(industrialtest.ElectricGearTool)
|
||||
industrialtest.internal.unpackTableInto(industrialtest.BatPackBase,{
|
||||
part="torso"
|
||||
})
|
||||
|
||||
local updateDelta=0
|
||||
|
||||
local function registerBatpack(config)
|
||||
if industrialtest.mtgAvailable then
|
||||
armor:register_armor("industrialtest:"..config.name,{
|
||||
description=config.displayName,
|
||||
inventory_image="industrialtest_"..config.name.."_inv.png",
|
||||
groups={
|
||||
armor_torso=1,
|
||||
armor_heal=0,
|
||||
_industrialtest_batpack=1
|
||||
},
|
||||
_industrialtest_powerStorage=true,
|
||||
_industrialtest_powerCapacity=config.powerCapacity,
|
||||
_industrialtest_powerFlow=config.powerFlow
|
||||
})
|
||||
elseif industrialtest.mclAvailable then
|
||||
minetest.register_tool("industrialtest:"..config.name,{
|
||||
description=config.displayName,
|
||||
inventory_image="industrialtest_"..config.name.."_inv.png",
|
||||
groups={
|
||||
armor=1,
|
||||
non_combat_armor=1,
|
||||
armor_torso=1,
|
||||
non_combat_torso=1,
|
||||
_industrialtest_batpack=1
|
||||
},
|
||||
sounds={
|
||||
_mcl_armor_equip="mcl_armor_equip_iron",
|
||||
_mcl_armor_unequip="mcl_armor_unequip_iron"
|
||||
},
|
||||
on_place=mcl_armor.equip_on_use,
|
||||
on_secondary_use=mcl_armor.equip_on_use,
|
||||
_mcl_armor_element="torso",
|
||||
_mcl_armor_texture="industrialtest_"..config.name..".png",
|
||||
_industrialtest_powerStorage=true,
|
||||
_industrialtest_powerCapacity=config.powerCapacity,
|
||||
_industrialtest_powerFlow=config.powerFlow
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
local function onGlobalStep(player,inv,stack,index,def)
|
||||
if not def.groups or not def.groups._industrialtest_batpack then
|
||||
return false
|
||||
end
|
||||
function industrialtest.BatPackBase.update(self,player,itemstack)
|
||||
local wielded=player:get_wielded_item()
|
||||
local wieldedMeta=wielded:get_meta()
|
||||
if not industrialtest.api.hasPowerStorage(wieldedMeta) or wieldedMeta:get_int("industrialtest.powerFlow")>def._industrialtest_powerFlow then
|
||||
return true
|
||||
if not industrialtest.api.hasPowerStorage(wieldedMeta) or wieldedMeta:get_int("industrialtest.powerFlow")>self.flow then
|
||||
return false
|
||||
end
|
||||
if industrialtest.api.transferPowerFromItem(stack,wieldedMeta,def._industrialtest_powerFlow)>0 then
|
||||
if industrialtest.api.transferPowerFromItem(itemstack,wieldedMeta,self.flow)>0 then
|
||||
industrialtest.api.updateItemPowerText(wielded)
|
||||
player:set_wielded_item(wielded)
|
||||
inv:set_stack("armor",index,stack)
|
||||
return true
|
||||
end
|
||||
return true
|
||||
return false
|
||||
end
|
||||
|
||||
registerBatpack({
|
||||
name="batpack_v",
|
||||
displayName=S("BatPack"),
|
||||
powerCapacity=60000,
|
||||
powerFlow=industrialtest.api.lvPowerFlow
|
||||
industrialtest.BatPack=table.copy(industrialtest.BatPackBase)
|
||||
industrialtest.internal.unpackTableInto(industrialtest.BatPack,{
|
||||
name="industrialtest:batpack_v",
|
||||
description=S("BatPack"),
|
||||
inventoryImage="industrialtest_batpack_v_inv.png",
|
||||
modelImage="industrialtest_batpack_v.png",
|
||||
capacity=60000,
|
||||
flow=industrialtest.api.lvPowerFlow
|
||||
})
|
||||
|
||||
industrialtest.BatPack:register()
|
||||
|
||||
minetest.register_craft({
|
||||
type="shaped",
|
||||
output="industrialtest:batpack_v",
|
||||
@ -91,12 +56,18 @@ minetest.register_craft({
|
||||
}
|
||||
})
|
||||
|
||||
registerBatpack({
|
||||
name="lappack_v",
|
||||
displayName=S("LapPack"),
|
||||
powerCapacity=300000,
|
||||
powerFlow=industrialtest.api.hvPowerFlow
|
||||
industrialtest.LapPack=table.copy(industrialtest.BatPackBase)
|
||||
industrialtest.internal.unpackTableInto(industrialtest.LapPack,{
|
||||
name="industrialtest:lappack_v",
|
||||
description=S("LapPack"),
|
||||
inventoryImage="industrialtest_lappack_v_inv.png",
|
||||
modelImage="industrialtest_lappack_v.png",
|
||||
capacity=60000,
|
||||
flow=industrialtest.api.lvPowerFlow
|
||||
})
|
||||
|
||||
industrialtest.LapPack:register()
|
||||
|
||||
minetest.register_craft({
|
||||
type="shaped",
|
||||
output="industrialtest:lappack_v",
|
||||
@ -106,33 +77,3 @@ minetest.register_craft({
|
||||
{industrialtest.elementKeys.powerCarrier,"",industrialtest.elementKeys.powerCarrier}
|
||||
}
|
||||
})
|
||||
|
||||
minetest.register_globalstep(function(dtime)
|
||||
updateDelta=updateDelta+dtime
|
||||
if updateDelta<industrialtest.updateDelay then
|
||||
return
|
||||
end
|
||||
updateDelta=0
|
||||
local players=minetest.get_connected_players()
|
||||
for _,player in ipairs(players) do
|
||||
if industrialtest.mtgAvailable then
|
||||
local _,inv=armor:get_valid_player(player,"")
|
||||
if inv then
|
||||
local armorList=inv:get_list("armor")
|
||||
assert(armorList)
|
||||
for i=1,#armorList do
|
||||
local stack=armorList[i]
|
||||
local def=stack:get_definition()
|
||||
if onGlobalStep(player,inv,stack,i,def) then
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
elseif industrialtest.mclAvailable then
|
||||
local inv=player:get_inventory()
|
||||
local stack=inv:get_stack("armor",3)
|
||||
local def=stack:get_definition()
|
||||
onGlobalStep(player,inv,stack,3,def)
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
20
tools/electric_gear_tool.lua
Normal file
20
tools/electric_gear_tool.lua
Normal file
@ -0,0 +1,20 @@
|
||||
-- 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.ElectricGearTool=table.copy(industrialtest.GearTool)
|
||||
industrialtest.internal.unpackTableInto(industrialtest.ElectricGearTool,{
|
||||
prepare=industrialtest.ElectricItem.prepare
|
||||
})
|
26
tools/electric_item.lua
Normal file
26
tools/electric_item.lua
Normal file
@ -0,0 +1,26 @@
|
||||
-- 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.ElectricItem=table.copy(industrialtest.Item)
|
||||
|
||||
function industrialtest.ElectricItem.prepare(self,itemstack)
|
||||
local meta=itemstack:get_meta()
|
||||
if industrialtest.api.hasPowerStorage(meta) then
|
||||
return
|
||||
end
|
||||
industrialtest.api.addPowerStorage(meta,self.capacity,self.flow,"n/a")
|
||||
industrialtest.api.updateItemPowerText(itemstack)
|
||||
end
|
@ -15,9 +15,12 @@
|
||||
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
industrialtest.ElectricTool=table.copy(industrialtest.Tool)
|
||||
industrialtest.internal.unpackTableInto(industrialtest.ElectricTool,{
|
||||
prepare=industrialtest.ElectricItem.prepare
|
||||
})
|
||||
|
||||
function industrialtest.ElectricTool.onPlace(self,itemstack,user,pointed)
|
||||
self:prepareTool(itemstack)
|
||||
self:prepare(itemstack)
|
||||
|
||||
local meta=itemstack:get_meta()
|
||||
local opPower=self:getOpPower()
|
||||
@ -25,18 +28,10 @@ function industrialtest.ElectricTool.onPlace(self,itemstack,user,pointed)
|
||||
return false
|
||||
end
|
||||
|
||||
self:use(itemstack,user,pointed)
|
||||
industrialtest.api.addPowerToItem(itemstack,-opPower)
|
||||
return true
|
||||
end
|
||||
|
||||
function industrialtest.ElectricTool.prepareTool(self,itemstack)
|
||||
local meta=itemstack:get_meta()
|
||||
if industrialtest.api.hasPowerStorage(meta) then
|
||||
return
|
||||
if self:use(itemstack,user,pointed) then
|
||||
industrialtest.api.addPowerToItem(itemstack,-opPower)
|
||||
end
|
||||
industrialtest.api.addPowerStorage(meta,self.capacity,self.flow,"n/a")
|
||||
industrialtest.api.updateItemPowerText(itemstack)
|
||||
return true
|
||||
end
|
||||
|
||||
function industrialtest.ElectricTool.getOpPower(self,itemstack)
|
||||
|
79
tools/gear_tool.lua
Normal file
79
tools/gear_tool.lua
Normal file
@ -0,0 +1,79 @@
|
||||
-- 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.GearTool=table.copy(industrialtest.Item)
|
||||
|
||||
function industrialtest.GearTool.createDefinitionTable(self)
|
||||
local def=industrialtest.Item.createDefinitionTable(self)
|
||||
|
||||
def.groups._industrialtest_gearTool=1
|
||||
def.groups["armor_"..self.part]=1
|
||||
if industrialtest.mtgAvailable then
|
||||
def.groups.armor_heal=1
|
||||
elseif industrialtest.mclAvailable then
|
||||
def.groups.armor=1
|
||||
def.groups.non_combat_armor=1
|
||||
def.sounds={
|
||||
_mcl_armor_equip="mcl_armor_equip_iron",
|
||||
_mcl_armor_unequip="mcl_armor_unequip_iron"
|
||||
}
|
||||
def.on_place=mcl_armor.equip_on_use
|
||||
def.on_secondary_use=mcl_armor.equip_on_use
|
||||
def._mcl_armor_element=self.part
|
||||
def._mcl_armor_texture=self.modelImage
|
||||
end
|
||||
|
||||
return def
|
||||
end
|
||||
|
||||
function industrialtest.GearTool.register(self)
|
||||
local def=self:createDefinitionTable()
|
||||
if industrialtest.mtgAvailable then
|
||||
armor:register_armor(self.name,def)
|
||||
elseif industrialtest.mclAvailable then
|
||||
minetest.register_tool(self.name,def)
|
||||
end
|
||||
end
|
||||
|
||||
local updateDelta=0
|
||||
minetest.register_globalstep(function(dtime)
|
||||
updateDelta=updateDelta+dtime
|
||||
if updateDelta<industrialtest.updateDelay then
|
||||
return
|
||||
end
|
||||
updateDelta=0
|
||||
|
||||
local players=minetest.get_connected_players()
|
||||
for _,player in ipairs(players) do
|
||||
local inv
|
||||
if industrialtest.mtgAvailable then
|
||||
_,inv=armor:get_valid_player(player,"")
|
||||
elseif industrialtest.mclAvailable then
|
||||
inv=player:get_inventory()
|
||||
end
|
||||
if inv then
|
||||
local armorList=inv:get_list("armor")
|
||||
assert(armorList)
|
||||
for i,itemstack in ipairs(armorList) do
|
||||
local def=itemstack:get_definition()
|
||||
if def and def.groups._industrialtest_gearTool and def._industrialtest_self and def._industrialtest_self.update and
|
||||
def._industrialtest_self:update(player,itemstack) then
|
||||
inv:set_stack("armor",i,itemstack)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end)
|
54
tools/item.lua
Normal file
54
tools/item.lua
Normal file
@ -0,0 +1,54 @@
|
||||
-- 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.Item={}
|
||||
|
||||
function industrialtest.Item.createDefinitionTable(self)
|
||||
local def={
|
||||
description=self.description,
|
||||
inventory_image=self.inventoryImage,
|
||||
groups={},
|
||||
_industrialtest_self=self
|
||||
}
|
||||
|
||||
if industrialtest.mtgAvailable then
|
||||
def.groups.flammable=self.flammable
|
||||
end
|
||||
|
||||
return def
|
||||
end
|
||||
|
||||
function industrialtest.Item.register(self)
|
||||
local def=self:createDefinitionTable()
|
||||
minetest.register_tool(self.name,def)
|
||||
end
|
||||
|
||||
-- Item callbacks
|
||||
minetest.register_on_player_inventory_action(function(player,action,inventory,info)
|
||||
if action=="put" then
|
||||
local def=info.stack:get_definition()
|
||||
if def and def._industrialtest_self and def._industrialtest_self.prepare then
|
||||
def._industrialtest_self:prepare(info.stack)
|
||||
inventory:set_stack(info.listname,info.index,info.stack)
|
||||
end
|
||||
end
|
||||
end)
|
||||
minetest.register_on_craft(function(itemstack)
|
||||
local def=itemstack:get_definition()
|
||||
if def and def._industrialtest_self and def._industrialtest_self.prepare then
|
||||
def._industrialtest_self:prepare(itemstack)
|
||||
end
|
||||
end)
|
@ -14,7 +14,7 @@
|
||||
-- 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.Tool={}
|
||||
industrialtest.Tool=table.copy(industrialtest.Item)
|
||||
|
||||
function industrialtest.Tool.use(self,itemstack,user,pointed)
|
||||
-- dummy function
|
||||
@ -25,7 +25,7 @@ function industrialtest.Tool.onPlace(self,itemstack,user,pointed)
|
||||
if self:use(itemstack,user,pointed) then
|
||||
local meta=itemstack:get_meta()
|
||||
if not meta:contains("industrialtest.uses") then
|
||||
self:prepareTool(itemstack)
|
||||
self:prepare(itemstack)
|
||||
end
|
||||
local uses=meta:get_int("industrialtest.uses")-1
|
||||
if uses==0 then
|
||||
@ -44,33 +44,25 @@ function industrialtest.Tool.onPlace(self,itemstack,user,pointed)
|
||||
return false
|
||||
end
|
||||
|
||||
function industrialtest.Tool.prepareTool(self,itemstack)
|
||||
function industrialtest.Tool.prepare(self,itemstack)
|
||||
local meta=itemstack:get_meta()
|
||||
meta:set_int("industrialtest.uses",self.uses)
|
||||
end
|
||||
|
||||
function industrialtest.Tool.createDefinitionTable(self)
|
||||
local def={
|
||||
description=self.description,
|
||||
inventory_image=self.inventoryImage,
|
||||
tool_capabilities={
|
||||
full_punch_interval=1,
|
||||
uses=self.uses
|
||||
},
|
||||
on_place=function(itemstack,user,pointed)
|
||||
if self:onPlace(itemstack,user,pointed) then
|
||||
return itemstack
|
||||
end
|
||||
return nil
|
||||
end,
|
||||
_industrialtest_self=self
|
||||
local def=industrialtest.Item.createDefinitionTable(self)
|
||||
def.tool_capabilities={
|
||||
full_punch_interval=1,
|
||||
uses=self.uses
|
||||
}
|
||||
def.on_place=function(itemstack,user,pointed)
|
||||
if self:onPlace(itemstack,user,pointed) then
|
||||
return itemstack
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
if industrialtest.mtgAvailable then
|
||||
def.groups={
|
||||
flammable=self.flammable
|
||||
}
|
||||
elseif industrialtest.mclAvailable then
|
||||
if industrialtest.mclAvailable then
|
||||
def.groups={
|
||||
tool=1
|
||||
}
|
||||
@ -80,25 +72,3 @@ function industrialtest.Tool.createDefinitionTable(self)
|
||||
|
||||
return def
|
||||
end
|
||||
|
||||
function industrialtest.Tool.register(self)
|
||||
local def=self:createDefinitionTable()
|
||||
minetest.register_tool(self.name,def)
|
||||
end
|
||||
|
||||
-- Item callbacks
|
||||
minetest.register_on_player_inventory_action(function(player,action,inventory,info)
|
||||
if action=="put" then
|
||||
local def=info.stack:get_definition()
|
||||
if def and def._industrialtest_self then
|
||||
def._industrialtest_self:prepareTool(info.stack)
|
||||
inventory:set_stack(info.listname,info.index,info.stack)
|
||||
end
|
||||
end
|
||||
end)
|
||||
minetest.register_on_craft(function(itemstack)
|
||||
local def=itemstack:get_definition()
|
||||
if def and def._industrialtest_self then
|
||||
def._industrialtest_self:prepareTool(itemstack)
|
||||
end
|
||||
end)
|
||||
|
Loading…
x
Reference in New Issue
Block a user