industrialtest/tools/jetpack.lua
2024-01-09 11:07:00 +01:00

143 lines
4.1 KiB
Lua

-- IndustrialTest
-- Copyright (C) 2024 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/>.
local S=minetest.get_translator("industrialtest")
local jetpack={}
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
}
for key,value in pairs(config.groups) do
groups[key]=value
end
minetest.register_tool("industrialtest:"..config.name,{
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
})
elseif industrialtest.mtgAvailable then
local groups={
armor_torso=1,
armor_heal=0,
_industrialtest_jetpack=1
}
for key,value in pairs(config.groups) do
groups[key]=value
end
armor:register_armor("industrialtest:"..config.name, {
description=config.displayName,
inventory_image="industrialtest_"..config.name.."_inv.png",
groups=groups,
_industrialtest_tryFly=config.tryFly
})
end
end
local function 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))
else
player:add_velocity(vector.new(0,vel,0))
end
end
local function onGlobalStep(player,inv,itemstack,index,def)
if def.groups and def.groups._industrialtest_jetpack then
local result=def._industrialtest_tryFly(player,itemstack)
if result then
addYVelocityClamped(player,1,10)
inv:set_stack("armor",index,result)
end
return true
end
return false
end
jetpack.tryFly=function(player,itemstack)
if itemstack:get_wear()>=65533 then
return false
end
itemstack:set_wear(itemstack:get_wear()+2)
minetest.debug(itemstack:get_wear())
return itemstack
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_emptyOnConstruct=1
},
tryFly=jetpack.tryFly
})
minetest.register_craft({
type="shaped",
output="industrialtest:jetpack_v",
recipe={
{"industrialtest:refined_iron_ingot","industrialtest:electronic_circuit","industrialtest:refined_iron_ingot"},
{"industrialtest:refined_iron_ingot","industrialtest:fuel_can","industrialtest:refined_iron_ingot"},
{industrialtest.elementKeys.powerCarrier,"",industrialtest.elementKeys.powerCarrier}
}
})
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)