Refactor jetpacks

This commit is contained in:
mrkubax10 2025-04-17 13:49:06 +02:00
parent b383231c40
commit 000cff6940
5 changed files with 81 additions and 135 deletions

View File

@ -66,6 +66,7 @@ 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/fluid_container_item.lua")
dofile(modpath.."/tools/tool.lua")
dofile(modpath.."/tools/gear_tool.lua")
dofile(modpath.."/tools/electric_tool.lua")

View File

@ -20,7 +20,14 @@ industrialtest.internal.unpackTableInto(industrialtest.BatPackBase,{
part="torso"
})
function industrialtest.BatPackBase.update(self,player,itemstack)
local updateDelta=0
function industrialtest.BatPackBase.update(self,player,itemstack,dtime)
updateDelta=updateDelta+dtime
if updateDelta<industrialtest.updateDelay then
return false
end
updateDelta=0
local wielded=player:get_wielded_item()
local wieldedMeta=wielded:get_meta()
if not industrialtest.api.hasPowerStorage(wieldedMeta) or wieldedMeta:get_int("industrialtest.powerFlow")>self.flow then

View File

@ -0,0 +1,27 @@
-- 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.FluidContainerItem=table.copy(industrialtest.Item)
function industrialtest.FluidContainerItem.prepare(self,itemstack)
if industrialtest.api.itemHasFluidStorage(itemstack) then
return
end
local meta=itemstack:get_meta()
meta:set_int("industrialtest.fluidAmount",0)
meta:set_int("industrialtest.fluidCapacity",self.capacity)
industrialtest.api.updateItemFluidText(itemstack)
end

View File

@ -48,14 +48,7 @@ function industrialtest.GearTool.register(self)
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
@ -70,7 +63,7 @@ minetest.register_globalstep(function(dtime)
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
def._industrialtest_self:update(player,itemstack,dtime) then
inv:set_stack("armor",i,itemstack)
end
end

View File

@ -15,71 +15,21 @@
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
local S=minetest.get_translator("industrialtest")
industrialtest.JetpackBase=table.copy(industrialtest.GearTool)
industrialtest.internal.unpackTableInto(industrialtest.JetpackBase,{
part="torso"
})
local jetpack={}
local electricJetpack={}
local function registerJetpack(config)
if industrialtest.mclAvailable then
local groups={
armor=1,
non_combat_armor=1,
armor_torso=1,
non_combat_torso=1,
_industrialtest_jetpack=1
}
if config.groups then
for key,value in pairs(config.groups) do
groups[key]=value
end
end
local definition={
description=config.displayName,
inventory_image="industrialtest_"..config.name.."_inv.png",
groups=groups,
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_tryFly=config.tryFly
}
if config.customKeys then
for key,value in pairs(config.customKeys) do
definition[key]=value
end
end
minetest.register_tool("industrialtest:"..config.name,definition)
elseif industrialtest.mtgAvailable then
local groups={
armor_torso=1,
armor_heal=0,
_industrialtest_jetpack=1
}
if config.groups then
for key,value in pairs(config.groups) do
groups[key]=value
end
end
local definition={
description=config.displayName,
inventory_image="industrialtest_"..config.name.."_inv.png",
groups=groups,
_industrialtest_tryFly=config.tryFly
}
if config.customKeys then
for key,value in pairs(config.customKeys) do
definition[key]=value
end
end
armor:register_armor("industrialtest:"..config.name,definition)
function industrialtest.JetpackBase.update(self,player,itemstack,dtime)
local control=player:get_player_control()
if control.jump and self:tryFly(itemstack) then
self.addYVelocityClamped(player,1,10)
return true
end
return false
end
local function addYVelocityClamped(player,vel,max)
function industrialtest.JetpackBase.addYVelocityClamped(player,vel,max)
local playerVel=player:get_velocity()
if playerVel.y+vel>max then
player:add_velocity(vector.new(0,math.max(max-playerVel.y,0),0))
@ -88,39 +38,33 @@ local function addYVelocityClamped(player,vel,max)
end
end
local function onGlobalStep(player,inv,itemstack,index,def)
if def.groups and def.groups._industrialtest_jetpack then
if def._industrialtest_tryFly(itemstack) then
addYVelocityClamped(player,1,10)
inv:set_stack("armor",index,itemstack)
end
return true
end
return false
industrialtest.Jetpack=table.copy(industrialtest.JetpackBase)
industrialtest.internal.unpackTableInto(industrialtest.Jetpack,{
-- _v is hack to suppress "Registered armor doesn't have material at the end of registration name" warning from 3D Armor.
name="industrialtest:jetpack_v",
description=S("Jetpack"),
inventoryImage="industrialtest_jetpack_v_inv.png",
modelImage="industrialtest_jetpack_v.png",
capacity=5000,
prepare=industrialtest.FluidContainerItem.prepare
})
function industrialtest.Jetpack.createDefinitionTable(self)
local def=industrialtest.JetpackBase.createDefinitionTable(self)
def.groups._industrialtest_fueled=1
return def
end
jetpack.tryFly=function(itemstack)
local meta=itemstack:get_meta()
if meta:get_int("industrialtest.fluidAmount")==0 then
function industrialtest.Jetpack.tryFly(self,itemstack)
if industrialtest.api.isItemFluidStorageEmpty(itemstack) then
return false
end
industrialtest.api.addFluidToItem(itemstack,-1)
return true
end
-- _v is hack to suppress "Registered armor doesn't have material at the end of registration name" warning from 3D Armor.
registerJetpack({
name="jetpack_v",
displayName=S("Jetpack"),
groups={
_industrialtest_fueled=1,
_industrialtest_fluidStorage=1
},
tryFly=jetpack.tryFly,
customKeys={
_industrialtest_fluidCapacity=5000
}
})
industrialtest.Jetpack:register()
minetest.register_craft({
type="shaped",
output="industrialtest:jetpack_v",
@ -131,7 +75,18 @@ minetest.register_craft({
}
})
electricJetpack.tryFly=function(itemstack)
industrialtest.ElectricJetpack=table.copy(industrialtest.JetpackBase)
industrialtest.internal.unpackTableInto(industrialtest.ElectricJetpack,{
name="industrialtest:electric_jetpack",
description=S("Electric Jetpack"),
inventoryImage="industrialtest_electric_jetpack_inv.png",
modelImage="industrialtest_electric_jetpack.png",
flow=industrialtest.api.lvPowerFlow,
capacity=30000,
prepare=industrialtest.ElectricItem.prepare
})
function industrialtest.ElectricJetpack.tryFly(self,itemstack)
local meta=itemstack:get_meta()
if meta:get_int("industrialtest.powerAmount")<10 then
return false
@ -140,16 +95,8 @@ electricJetpack.tryFly=function(itemstack)
return true
end
registerJetpack({
name="electric_jetpack",
displayName=S("Electric Jetpack"),
tryFly=electricJetpack.tryFly,
customKeys={
_industrialtest_powerStorage=true,
_industrialtest_powerCapacity=30000,
_industrialtest_powerFlow=industrialtest.api.lvPowerFlow
}
})
industrialtest.ElectricJetpack:register()
minetest.register_craft({
type="shaped",
output="industrialtest:electric_jetpack",
@ -159,32 +106,3 @@ minetest.register_craft({
{industrialtest.elementKeys.yellowDust,"",industrialtest.elementKeys.yellowDust}
}
})
minetest.register_globalstep(function(dtime)
-- FIXME: Maybe this can be optimized?
local players=minetest.get_connected_players()
for _,player in ipairs(players) do
local control=player:get_player_control()
if control.jump then
if 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)
elseif 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
end
end
end
end)