Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API to register custom "can fly checks" #14

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,17 @@ 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, 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
end)
```
46 changes: 33 additions & 13 deletions init.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
hangglider = {}

hangglider = {
translator = minetest.get_translator('hangglider'),
Expand Down Expand Up @@ -80,20 +81,36 @@ 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_flak(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
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, player)
if ret == false then
return false
end
end
return true
end
Expand Down Expand Up @@ -149,7 +166,7 @@ local function hangglider_step(self, dtime)
})
end
end
if not can_fly(pos, name) then
if not can_fly_flak(name, pos) then
if not self.flak_timer then
self.flak_timer = 0
shoot_flak_sound(pos)
Expand All @@ -164,6 +181,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
Expand Down