From 7e8832f52336b43ee73ff246198ab591f57a73ae Mon Sep 17 00:00:00 2001 From: Niklp Date: Wed, 21 Feb 2024 14:22:04 +0100 Subject: [PATCH 1/3] Add API to register custom "can fly checks" --- .luacheckrc | 1 + README.md | 12 ++++++++++++ init.lua | 41 ++++++++++++++++++++++++++++------------- 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/.luacheckrc b/.luacheckrc index 1502aa3..74dfefb 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -1,5 +1,6 @@ globals = { "areas", + "hangglider", } read_globals = { diff --git a/README.md b/README.md index d109a22..ff428cc 100644 --- a/README.md +++ b/README.md @@ -31,3 +31,15 @@ The hang glider will wear out every time you use it. The hang glider can be repa If the `areas` mod is installed, airspace restrictions can be added to areas using the `/area_flak` command. When using a hang glider in an area with flak enabled, you will get shot down a few seconds after entering the area, this reduces your HP to 1 and destroys your hang glider. + +## API + +#### Custom "can fly" checks + +```lua +hangglider.add_fly_check(function(name, pos) + -- Add your code here + -- Must return `true` (can fly) or `false` (can't fly) + return true +end) +``` diff --git a/init.lua b/init.lua index 3953e5d..fd2288e 100644 --- a/init.lua +++ b/init.lua @@ -1,3 +1,4 @@ +hangglider = {} local has_player_monoids = minetest.get_modpath("player_monoids") local has_areas = minetest.get_modpath("areas") @@ -73,20 +74,34 @@ local function remove_physics_overrides(player) end end -local function can_fly(pos, name) - if not enable_flak then - return true - end - local flak = false - local owners = {} - for _, area in pairs(areas:getAreasAtPos(pos)) do - if area.flak then - flak = true +local fly_checks = {} + +function hangglider.add_fly_check(func) + table.insert(fly_checks, func) +end + +local function can_fly(name, pos) + -- Area flak check + if enable_flak then + local flak = false + local owners = {} + for _, area in pairs(areas:getAreasAtPos(pos)) do + if area.flak then + flak = true + end + owners[area.owner] = true + end + if flak and not owners[name] then + return false end - owners[area.owner] = true end - if flak and not owners[name] then - return false + + -- Custom checks set by other mods + for _, func in ipairs(fly_checks) do + local ret = func(name, pos) + if ret == false then + return false + end end return true end @@ -142,7 +157,7 @@ local function hangglider_step(self, dtime) }) end end - if not can_fly(pos, name) then + if not can_fly(name, pos) then if not self.flak_timer then self.flak_timer = 0 shoot_flak_sound(pos) From 4e40e3400c2da1734e971600291071aed69989c4 Mon Sep 17 00:00:00 2001 From: Niklp Date: Fri, 1 Mar 2024 15:31:00 +0100 Subject: [PATCH 2/3] Seperate area flak and custom checks --- README.md | 4 +++- init.lua | 11 ++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ff428cc..134961b 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,9 @@ When using a hang glider in an area with flak enabled, you will get shot down a #### Custom "can fly" checks ```lua -hangglider.add_fly_check(function(name, pos) +hangglider.add_fly_check(function(name, player) + -- `name` is the playername + -- `player` is the PlayerRef -- Add your code here -- Must return `true` (can fly) or `false` (can't fly) return true diff --git a/init.lua b/init.lua index fd2288e..57b50fa 100644 --- a/init.lua +++ b/init.lua @@ -80,7 +80,7 @@ function hangglider.add_fly_check(func) table.insert(fly_checks, func) end -local function can_fly(name, pos) +local function can_fly_area(name, pos) -- Area flak check if enable_flak then local flak = false @@ -95,10 +95,12 @@ local function can_fly(name, pos) return false end end +end +local function can_fly_custom(name, player) -- Custom checks set by other mods for _, func in ipairs(fly_checks) do - local ret = func(name, pos) + local ret = func(name, player) if ret == false then return false end @@ -157,7 +159,7 @@ local function hangglider_step(self, dtime) }) end end - if not can_fly(name, pos) then + if not can_fly_area(name, pos) then if not self.flak_timer then self.flak_timer = 0 shoot_flak_sound(pos) @@ -172,6 +174,9 @@ local function hangglider_step(self, dtime) gliding = false end end + if not can_fly_custom(name, player) then + gliding = false + end if not gliding then remove_physics_overrides(player) hanggliding_players[name] = nil From 104ebd4ee796fb004e5c9e063cbcd95cfff9a9d2 Mon Sep 17 00:00:00 2001 From: Niklp Date: Thu, 11 Apr 2024 13:44:29 +0200 Subject: [PATCH 3/3] Rename function based on feedback --- init.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/init.lua b/init.lua index 57b50fa..6a0a173 100644 --- a/init.lua +++ b/init.lua @@ -80,7 +80,7 @@ function hangglider.add_fly_check(func) table.insert(fly_checks, func) end -local function can_fly_area(name, pos) +local function can_fly_flak(name, pos) -- Area flak check if enable_flak then local flak = false @@ -159,7 +159,7 @@ local function hangglider_step(self, dtime) }) end end - if not can_fly_area(name, pos) then + if not can_fly_flak(name, pos) then if not self.flak_timer then self.flak_timer = 0 shoot_flak_sound(pos)