2019-03-07 19:55:56 +00:00
|
|
|
local S = minetest.get_translator("mcl_void_damage")
|
2020-04-21 23:38:41 +00:00
|
|
|
local enable_damage = minetest.settings:get_bool("enable_damage")
|
2019-03-07 19:55:56 +00:00
|
|
|
|
2021-03-14 22:52:04 +00:00
|
|
|
local pos_to_dim = mcl_worlds.pos_to_dimension
|
|
|
|
local dim_change = mcl_worlds.dimension_change
|
|
|
|
local is_in_void = mcl_worlds.is_in_void
|
|
|
|
local get_spawn_pos = mcl_spawn.get_player_spawn_pos
|
2021-03-14 22:53:56 +00:00
|
|
|
local send_chat = minetest.chat_send_player
|
|
|
|
local get_connected = minetest.get_connected_players
|
2021-03-14 22:52:04 +00:00
|
|
|
|
2018-05-25 11:45:41 +00:00
|
|
|
local voidtimer = 0
|
2020-04-21 23:38:41 +00:00
|
|
|
local VOID_DAMAGE_FREQ = 0.5
|
|
|
|
local VOID_DAMAGE = 4
|
2018-05-25 11:45:41 +00:00
|
|
|
|
2020-04-21 23:38:41 +00:00
|
|
|
-- Remove entities that fall too deep into the void
|
|
|
|
minetest.register_on_mods_loaded(function()
|
|
|
|
-- We do this by overwriting on_step of all entities
|
|
|
|
for entitystring, def in pairs(minetest.registered_entities) do
|
|
|
|
local on_step_old = def.on_step
|
|
|
|
if not on_step_old then
|
|
|
|
on_step_old = function() end
|
|
|
|
end
|
2020-09-21 07:30:29 +00:00
|
|
|
local on_step = function(self, dtime, moveresult)
|
|
|
|
on_step_old(self, dtime, moveresult)
|
2020-04-21 23:38:41 +00:00
|
|
|
local obj = self.object
|
|
|
|
local pos = obj:get_pos()
|
|
|
|
-- Old on_step function might have deleted object,
|
|
|
|
-- so we delete it
|
|
|
|
if not pos then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if not self._void_timer then
|
|
|
|
self._void_timer = 0
|
|
|
|
end
|
|
|
|
self._void_timer = self._void_timer + dtime
|
|
|
|
if self._void_timer <= VOID_DAMAGE_FREQ then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
self._void_timer = 0
|
|
|
|
|
2021-03-14 22:52:04 +00:00
|
|
|
local void, void_deadly = is_in_void(pos)
|
2020-04-21 23:38:41 +00:00
|
|
|
if void_deadly then
|
|
|
|
local ent = obj:get_luaentity()
|
|
|
|
obj:remove()
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
def.on_step = on_step
|
|
|
|
minetest.register_entity(":"..entitystring, def)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
-- Hurt players or teleport them back to spawn if they are too deep in the void
|
2018-05-25 11:45:41 +00:00
|
|
|
minetest.register_globalstep(function(dtime)
|
|
|
|
voidtimer = voidtimer + dtime
|
2020-04-21 23:38:41 +00:00
|
|
|
if voidtimer > VOID_DAMAGE_FREQ then
|
2018-05-25 11:45:41 +00:00
|
|
|
voidtimer = 0
|
|
|
|
local enable_damage = minetest.settings:get_bool("enable_damage")
|
2021-03-14 22:53:56 +00:00
|
|
|
local players = get_connected()
|
2020-04-21 23:38:41 +00:00
|
|
|
for p=1, #players do
|
|
|
|
local player = players[p]
|
|
|
|
local pos = player:get_pos()
|
2021-03-14 22:52:04 +00:00
|
|
|
local void, void_deadly = is_in_void(pos)
|
2018-05-25 11:45:41 +00:00
|
|
|
if void_deadly then
|
2020-04-21 23:38:41 +00:00
|
|
|
local immortal_val = player:get_armor_groups().immortal
|
2018-05-25 11:45:41 +00:00
|
|
|
local is_immortal = false
|
|
|
|
if immortal_val and immortal_val > 0 then
|
|
|
|
is_immortal = true
|
|
|
|
end
|
|
|
|
if is_immortal or not enable_damage then
|
2020-04-21 23:38:41 +00:00
|
|
|
-- If damage is disabled, we can't kill players.
|
|
|
|
-- So we just teleport the player back to spawn.
|
2021-03-14 22:52:04 +00:00
|
|
|
local spawn = get_spawn_pos(player)
|
2020-04-21 23:38:41 +00:00
|
|
|
player:set_pos(spawn)
|
2021-03-14 22:52:04 +00:00
|
|
|
dim_change(player, pos_to_dim(spawn))
|
2021-03-14 22:53:56 +00:00
|
|
|
send_chat(player:get_player_name(), S("The void is off-limits to you!"))
|
2018-05-25 11:45:41 +00:00
|
|
|
elseif enable_damage and not is_immortal then
|
|
|
|
-- Damage enabled, not immortal: Deal void damage (4 HP / 0.5 seconds)
|
2020-04-21 23:38:41 +00:00
|
|
|
if player:get_hp() > 0 then
|
2021-04-14 17:06:30 +00:00
|
|
|
mcl_util.deal_damage(player, VOID_DAMAGE, {type = "out_of_world"})
|
2018-05-25 11:45:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|