[WIP] cleanup, remove old 0.4 code (#6)

This commit is contained in:
Niklp 2023-01-06 21:50:44 +01:00 committed by GitHub
parent cd67eb2d19
commit 3c2618437a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 105 deletions

View File

@ -9,7 +9,5 @@ read_globals = {
"vector", "ItemStack", "vector", "ItemStack",
-- Mod deps -- Mod deps
"wardzones",
"minetestd",
"player_monoids" "player_monoids"
} }

144
init.lua
View File

@ -53,56 +53,20 @@
-- 2022-05-16 -- 2022-05-16
-- Add Z-index to theoretically be behind hotbar and practically behind other HUDs -- Add Z-index to theoretically be behind hotbar and practically behind other HUDs
local HUD_Overlay = true --show glider struts as overlay on HUD local HUD_Overlay = true -- show glider struts as overlay on HUD
local debug = false --show debug info in top-center of hud local debug = false -- show debug info in top-center of hud
local moveModelUp = false hangglider = {} -- Make this global, so other mods can tell if hangglider exists.
if tonumber(string.sub(minetest.get_version().string, 1, 1)) and
tonumber(string.sub(minetest.get_version().string, 1, 1)) > 4 then
moveModelUp = true
end
hangglider = {} --Make this global, so other mods can tell if hangglider exists.
hangglider.use = {} hangglider.use = {}
if HUD_Overlay then if HUD_Overlay then
hangglider.id = {} -- hud id for displaying overlay with struts hangglider.id = {} -- hud id for displaying overlay with struts
end end
if debug then hangglider.debug = {} end -- hud id for debug data
--hangglider.airbreak = {} -- true if falling fast when equip
--[[
minetest.register_entity("hangglider:airstopper", { --A one-instant entity that catches the player and stops them.
is_visible = false,
physical = false,
immortal = true,
attach = nil,
on_step = function(self, _)
local canExist = false
if self.attach then
local player = self.attach
if player:is_player() then
local pname = player:get_player_name()
canExist = true
if player:get_player_velocity().y < 0.5 and player:get_player_velocity().y > -0.5 then
--Let go when the player actually stops, as that's the whole point.
if hangglider.use[pname] then
if moveModelUp then
minetest.add_entity(player:get_pos(), "hangglider:glider"):set_attach(player, "", {x=0,y=10,z=0}, {x=0,y=0,z=0})
else
minetest.add_entity(player:get_pos(), "hangglider:glider"):set_attach(player, "", {x=0,y=0,z=0}, {x=0,y=0,z=0})
end
end
canExist = false
end
end
if not canExist then
player:set_detach()
end
end
if not canExist then
self.object:remove()
end
end
})]]
if minetest.global_exists("areas") then if debug then -- hud id for debug data
hangglider.debug = {}
end
if minetest.get_modpath("areas") then
hangglider.flak = true hangglider.flak = true
-- chat command definition essentially copied from areas mod. -- chat command definition essentially copied from areas mod.
minetest.register_chatcommand("area_flak",{ minetest.register_chatcommand("area_flak",{
@ -115,8 +79,7 @@ if minetest.global_exists("areas") then
end end
if not areas:isAreaOwner(id, name) then if not areas:isAreaOwner(id, name) then
return false, "Area "..id.." does not exist" return false, "Area "..id.." does not exist or is not owned by you."
.." or is not owned by you."
end end
local open = not areas.areas[id].flak local open = not areas.areas[id].flak
-- Save false as nil to avoid inflating the DB. -- Save false as nil to avoid inflating the DB.
@ -150,12 +113,14 @@ hangglider.shot_sound = function (pos)
pos = pos, pos = pos,
max_hear_distance = 30, max_hear_distance = 30,
gain = 10.0, gain = 10.0,
}) }, true)
end end
local has_player_monoids = minetest.get_modpath("player_monoids")
local physics_attrs = {"jump", "speed", "gravity"} local physics_attrs = {"jump", "speed", "gravity"}
local function apply_physics_override(player, overrides) local function apply_physics_override(player, overrides)
if minetest.get_modpath("player_monoids") then if has_player_monoids then
for _, attr in pairs(physics_attrs) do for _, attr in pairs(physics_attrs) do
if overrides[attr] then if overrides[attr] then
player_monoids[attr]:add_change(player, overrides[attr], "hangglider:glider") player_monoids[attr]:add_change(player, overrides[attr], "hangglider:glider")
@ -169,7 +134,7 @@ end
local function remove_physics_override(player, overrides) local function remove_physics_override(player, overrides)
for _, attr in pairs(physics_attrs) do for _, attr in pairs(physics_attrs) do
if overrides[attr] then if overrides[attr] then
if minetest.global_exists("player_monoids") then if has_player_monoids then
player_monoids[attr]:del_change(player, "hangglider:glider") player_monoids[attr]:del_change(player, "hangglider:glider")
else else
player:set_physics_override({[attr] = 1}) player:set_physics_override({[attr] = 1})
@ -186,7 +151,7 @@ minetest.register_entity("hangglider:glider", {
mesh = "glider.obj", mesh = "glider.obj",
immortal = true, immortal = true,
static_save = false, static_save = false,
textures = {"wool_white.png","default_wood.png"}, textures = {"wool_white.png", "default_wood.png"},
on_step = function(self, dtime) on_step = function(self, dtime)
local canExist = false local canExist = false
if self.object:get_attach() then if self.object:get_attach() then
@ -199,20 +164,19 @@ minetest.register_entity("hangglider:glider", {
if mrn_name then if mrn_name then
if not (mrn_name.walkable or mrn_name.liquidtype ~= "none") then if not (mrn_name.walkable or mrn_name.liquidtype ~= "none") then
canExist = true canExist = true
step_v = player:get_velocity().y
step_v = player:get_velocity().y if step_v < 0 and step_v > -3 then
if step_v < 0 and step_v > -3 then apply_physics_override(player, {speed = math.abs(step_v/2) + 0.75})
apply_physics_override(player, {speed=math.abs(step_v/2) + 0.75}) elseif step_v <= -3 then -- Cap our gliding movement speed.
elseif step_v <= -3 then --Cap our gliding movement speed. apply_physics_override(player, {speed = 2.25})
apply_physics_override(player, {speed=2.25}) else
else remove_physics_override(player, {speed = 1})
remove_physics_override(player, {speed=1}) end
end if debug then
if debug then player:hud_change(hangglider.debug[pname].id, "text", step_v..', '..
player:hud_change(hangglider.debug[pname].id, "text", step_v..', '.. player:get_physics_override().gravity..', '..tostring(hangglider.airbreak[pname]))
player:get_physics_override().gravity..', '..tostring(hangglider.airbreak[pname])) end
end apply_physics_override(player, {gravity = ((step_v + 3)/20)})
apply_physics_override(player, {gravity=((step_v + 3)/20)})
end end
end end
end end
@ -231,13 +195,11 @@ minetest.register_entity("hangglider:glider", {
end end
end end
if not canExist then if not canExist then
remove_physics_override(player, {gravity = 1, jump = 1, speed = 1})
remove_physics_override(player, { gravity=1, jump = 1, speed = 1,})
hangglider.use[pname] = false hangglider.use[pname] = false
if HUD_Overlay then if HUD_Overlay then
player:hud_change(hangglider.id[pname], "text", "blank.png") player:hud_change(hangglider.id[pname], "text", "blank.png")
end end
--hangglider.airbreak[pname] = false
end end
end end
end end
@ -256,7 +218,6 @@ minetest.register_on_dieplayer(function(player)
hangglider.use[player:get_player_name()] = false hangglider.use[player:get_player_name()] = false
end) end)
minetest.register_on_joinplayer(function(player) minetest.register_on_joinplayer(function(player)
local pname = player:get_player_name() local pname = player:get_player_name()
remove_physics_override(player, { remove_physics_override(player, {
@ -268,22 +229,21 @@ minetest.register_on_joinplayer(function(player)
hangglider.id[pname] = player:hud_add({ hangglider.id[pname] = player:hud_add({
hud_elem_type = "image", hud_elem_type = "image",
text = "blank.png", text = "blank.png",
position = {x=0, y=0}, position = {x = 0, y = 0},
scale = {x=-100, y=-100}, scale = {x = -100, y = -100},
alignment = {x=1, y=1}, alignment = {x = 1, y = 1},
offset = {x=0, y=0}, offset = {x = 0, y = 0},
z_index = -150 z_index = -150
}) })
end end
if debug then if debug then
hangglider.debug[pname] = {id = player:hud_add({hud_elem_type = "text", hangglider.debug[pname] = {id = player:hud_add({hud_elem_type = "text",
position = {x=0.5, y=0.1}, position = {x = 0.5, y= 0.1 },
text = "-", text = "-",
number = 0xFF0000}), -- red text number = 0xFF0000}), -- red text
-- ht = {50,50,50}, -- ht = {50,50,50},
} }
end end
--hangglider.airbreak[pname] = false
end) end)
minetest.register_on_leaveplayer(function(player) minetest.register_on_leaveplayer(function(player)
@ -291,13 +251,12 @@ minetest.register_on_leaveplayer(function(player)
hangglider.use[pname] = nil hangglider.use[pname] = nil
if HUD_Overlay then hangglider.id[pname] = nil end if HUD_Overlay then hangglider.id[pname] = nil end
if debug then hangglider.debug[pname] = nil end if debug then hangglider.debug[pname] = nil end
--hangglider.airbreak[pname] = nil
end) end)
minetest.register_tool("hangglider:hangglider", { minetest.register_tool("hangglider:hangglider", {
description = "Glider", description = "Glider",
inventory_image = "glider_item.png", inventory_image = "glider_item.png",
stack_max=1, stack_max = 1,
on_use = function(itemstack, player) on_use = function(itemstack, player)
if not player or player.is_fake_player then if not player or player.is_fake_player then
-- player does not exist or is created from an automated machine (fake_player) -- player does not exist or is created from an automated machine (fake_player)
@ -305,36 +264,15 @@ minetest.register_tool("hangglider:hangglider", {
end end
local pos = player:get_pos() local pos = player:get_pos()
local pname = player:get_player_name() local pname = player:get_player_name()
if not hangglider.use[pname] then --Equip if not hangglider.use[pname] then -- Equip
minetest.sound_play("bedsheet", {pos=pos, max_hear_distance = 8, gain = 1.0}) minetest.sound_play("bedsheet", {pos = pos, max_hear_distance = 8, gain = 1.0}, true)
if HUD_Overlay then player:hud_change(hangglider.id[pname], "text", "glider_struts.png") end if HUD_Overlay then player:hud_change(hangglider.id[pname], "text", "glider_struts.png") end
local airbreak = false minetest.add_entity(pos, "hangglider:glider"):set_attach(player, "", {x=0,y=10,z=0}, {x=0,y=0,z=0})
--[[if vel < -1.5 then -- engage mid-air, falling fast, so stop but ramp velocity more quickly
--hangglider.airbreak[pname] = true
airbreak = true
local stopper = minetest.add_entity(pos, "hangglider:airstopper")
minetest.after(0, function(stopper, player) --"Extreme Measures"
stopper:set_pos(player:get_pos())
stopper:get_luaentity().attach = player
player:set_attach( stopper, "", {x=0,y=0,z=0}, {x=0,y=0,z=0})
end, stopper, player)
end]]
if not airbreak then
if moveModelUp then
minetest.add_entity(pos, "hangglider:glider"):set_attach(player, "", {x=0,y=10,z=0}, {x=0,y=0,z=0})
else
minetest.add_entity(pos, "hangglider:glider"):set_attach(player, "", {x=0,y=0,z=0}, {x=0,y=0,z=0})
end
end
hangglider.use[pname] = true hangglider.use[pname] = true
apply_physics_override(player, {jump = 0}) apply_physics_override(player, {jump = 0})
-- if minetest 0.4.x use this:
-- if minetest 5.x use this:
-- minetest.add_entity(player:get_pos(), "hangglider:glider"):set_attach(player, "", {x=0,y=10,z=0}, {x=0,y=0,z=0})
itemstack:set_wear(itemstack:get_wear() + 255) itemstack:set_wear(itemstack:get_wear() + 255)
return itemstack return itemstack
elseif hangglider.use[pname] then --Unequip elseif hangglider.use[pname] then -- Unequip
if HUD_Overlay then player:hud_change(hangglider.id[pname], "text", "default_wood.png^[colorize:#0000:255") end if HUD_Overlay then player:hud_change(hangglider.id[pname], "text", "default_wood.png^[colorize:#0000:255") end
hangglider.use[pname] = false hangglider.use[pname] = false
end end