From 274a729ef59e5807c3bee050f9ef2d0554748040 Mon Sep 17 00:00:00 2001 From: David Leal Date: Wed, 24 Mar 2021 12:32:23 -0600 Subject: [PATCH] Major improvements for flying entities Previously, if the entity had the `can_fly` option enabled, when going upwards or downwards, it went too fast, extremely fast, and its speed increases over time. With this update, this no longer happens: you can customize your own upwards and downwards maximum speed for any entity you want. --- README.md | 10 +++++----- init.lua | 42 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 73e9860..047bbd7 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ -# Library Mount [![Build status](https://github.com/Panquesito7/lib_mount/workflows/build/badge.svg)](https://github.com/Panquesito7/lib_mount/actions) [![License](https://img.shields.io/badge/license-LGPLv2.1%2B-blue.svg)](https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html) [![ContentDB](https://content.minetest.net/packages/Panquesito7/lib_mount/shields/downloads/)](https://content.minetest.net/packages/Panquesito7/lib_mount/) +# Library Mount [![Build status](https://github.com/minetest-mods/lib_mount/workflows/build/badge.svg)](https://github.com/minetest-mods/lib_mount/actions) [![License](https://img.shields.io/badge/license-LGPLv2.1%2B-blue.svg)](https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html) [![ContentDB](https://content.minetest.net/packages/Panquesito7/lib_mount/shields/downloads/)](https://content.minetest.net/packages/Panquesito7/lib_mount/) Made by [blert2112](https://github.com/blert2112).\ Handed over to [Panquesito7](https://github.com/Panquesito7). -Current version: 1.2 +Current version: 1.3 ## Dependencies @@ -29,11 +29,11 @@ See [`LICENSE.md`](LICENSE.md) for information. ## Installation -- Unzip the archive, rename the folder to `lib_mount` and place it in ../minetest/mods/ +- Unzip the archive, rename the folder to `lib_mount` and place it in `../minetest/mods/`. -- GNU/Linux: If you use a system-wide installation place it in ~/.minetest/mods/. +- GNU/Linux: If you use a system-wide installation, place it in `~/.minetest/mods/`. -- If you only want this to be used in a single world, place the folder in worldmods/ in your world directory. +- If you only want this to be used in a single world, place the folder in `worldmods/` in your world directory. For further information or help, see:\ . diff --git a/init.lua b/init.lua index 8f27efc..8a6f80b 100644 --- a/init.lua +++ b/init.lua @@ -278,13 +278,13 @@ function lib_mount.drive(entity, dtime, is_mob, moving_anim, stand_anim, jump_he velo.y = velo.y + (jump_height * 3) + 1 acce_y = acce_y + (acce_y * 3) + 1 end - if can_go_down and can_go_up and can_fly and can_fly == true then + if can_go_up and can_fly and can_fly == true then velo.y = velo.y + 1 acce_y = acce_y + 1 end end if ctrl.sneak then - if can_go_down and can_go_up and can_fly and can_fly == true then + if can_go_down and can_fly and can_fly == true then velo.y = velo.y - 1 acce_y = acce_y - 1 end @@ -317,6 +317,24 @@ function lib_mount.drive(entity, dtime, is_mob, moving_anim, stand_anim, jump_he return end + -- Stop! (upwards and downwards; applies only if `can_fly` is enabled) + if can_fly == true then + local s2 = get_sign(velo.y) + local s3 = get_sign(acce_y) + velo.y = velo.y - 0.02 * s2 + acce_y = acce_y - 0.02 * s3 + if s2 ~= get_sign(velo.y) then + entity.object:set_velocity({x=0, y=0, z=0}) + velo.y = 0 + return + end + if s3 ~= get_sign(acce_y) then + entity.object:set_velocity({x=0, y=0, z=0}) + acce_y = 0 -- luacheck: ignore + return + end + end + -- enforce speed limit forward and reverse local max_spd = entity.max_speed_reverse if get_sign(entity.v) >= 0 then @@ -326,6 +344,25 @@ function lib_mount.drive(entity, dtime, is_mob, moving_anim, stand_anim, jump_he entity.v = entity.v - get_sign(entity.v) end + -- Enforce speed limit when going upwards or downwards (applies only if `can_fly` is enabled) + if can_fly == true then + local max_spd_flying = entity.max_speed_downwards + if get_sign(velo.y) >= 0 or get_sign(acce_y) >= 0 then + max_spd_flying = entity.max_speed_upwards + end + + if math.abs(velo.y) > max_spd_flying then + velo.y = velo.y - get_sign(velo.y) + end + if velo.y > max_spd_flying then -- This check is to prevent exceeding the maximum speed; but the above check also prevents that. + velo.y = velo.y - get_sign(velo.y) + end + + if math.abs(acce_y) > max_spd_flying then + acce_y = acce_y - get_sign(acce_y) + end + end + -- Set position, velocity and acceleration local p = entity.object:get_pos() local new_velo = {x=0, y=0, z=0} @@ -337,6 +374,7 @@ function lib_mount.drive(entity, dtime, is_mob, moving_anim, stand_anim, jump_he if ni == "air" then if can_fly == true then new_acce.y = 0 + acce_y = acce_y - get_sign(acce_y) -- When going down, this will prevent from exceeding the maximum speed. end elseif ni == "liquid" then if entity.terrain_type == 2 or entity.terrain_type == 3 then