Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
fluxionary committed Mar 24, 2020
1 parent 001ad11 commit b91931e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 27 deletions.
52 changes: 25 additions & 27 deletions init.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
-- Load setting
local suffocation_damage = 10
local setting = minetest.settings:get("real_suffocation_damage")
if tonumber(setting) ~= nil then
suffocation_damage = tonumber(setting)
end
local suffocation_damage = tonumber(minetest.settings:get("real_suffocation_damage")) or 10

-- Skip the rest if suffocation damage is 0, no point in overwriting stuff
if suffocation_damage > 0 then
local function should_suffocate(def)
--[[ Here comes the HUGE conditional deciding whether we use suffocation. We want to catch as many nodes as possible
while avoiding bad nodes. We care mostly about physical properties, we don't care about visual appearance.
Here's what it checks and why:
- Walkable: Must be walkable, which means player can get stuck inside. If player can move freely, suffocation does not make sense
- Drowning and damage: If node has set any of those explicitly, it probably knows why. We don't want to mess with it.
- collision_box, node_box: Checks whether we deal with full-sized standard cubes, since only care about those.
Everything else is probably too small for suffocation to seem real.
- disable_suffocation group: If set to 1, we bail out. This makes it possible for nodes to defend themselves against hacking. :-)
]]

return (def.walkable == nil or def.walkable == true)
and (def.drowning == nil or def.drowning == 0)
and (def.damage_per_second == nil or def.damage_per_second <= 0)
and (def.collision_box == nil or def.collision_box.type == "regular")
and (def.node_box == nil or def.node_box.type == "regular")
and (def.groups.disable_suffocation ~= 1)
end

-- Checks all nodes and adds suffocation (drowning damage) for suitable nodes
local function add_suffocation()
Expand All @@ -15,24 +27,9 @@ local function add_suffocation()
local no_suffocate_nodes = {}
-- Check ALL the nodes!
for itemstring, def in pairs(minetest.registered_nodes) do
--[[ Here comes the HUGE conditional deciding whether we use suffocation. We want to catch as many nodes as possible
while avoiding bad nodes. We care mostly about physical properties, we don't care about visual appearance.
Here's what it checks and why:
- Walkable: Must be walkable, which means player can get stuck inside. If player can move freely, suffocation does not make sense
- Drowning and damage: If node has set any of those explicitly, it probably knows why. We don't want to mess with it.
- collision_box, node_box: Checks whether we deal with full-sized standard cubes, since only care about those.
Everything else is probably too small for suffocation to seem real.
- disable_suffocation group: If set to 1, we bail out. This makes it possible for nodes to defend themselves against hacking. :-)
]]
if (def.walkable == nil or def.walkable == true)
and (def.drowning == nil or def.drowning == 0)
and (def.damage_per_second == nil or def.damage_per_second <= 0)
and (def.collision_box == nil or def.collision_box.type == "regular")
and (def.node_box == nil or def.node_box.type == "regular")
and (def.groups.disable_suffocation ~= 1)
then
if should_suffocate(def) then
-- Add “real_suffocation” group so other mods know this node was touched by this mod
local marked_groups = def.groups
local marked_groups = table.copy(def.groups)
marked_groups.real_suffocation = 1
-- Let's hack the node!
minetest.override_item(itemstring, { drowning = suffocation_damage, groups = marked_groups })
Expand All @@ -46,7 +43,8 @@ local function add_suffocation()
minetest.log("verbose", "[real_suffocation] Suffocation has not been hacked into "..#no_suffocate_nodes.." nodes: "..dump(no_suffocate_nodes))
end

-- This is a minor hack to make sure our loop runs after all nodes have been registered
minetest.after(0, add_suffocation)

-- Skip the rest if suffocation damage is 0, no point in overwriting stuff
if suffocation_damage > 0 then
-- This is a minor hack to make sure our loop runs after all nodes have been registered
minetest.after(0, add_suffocation)
end
1 change: 1 addition & 0 deletions mod.conf
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
name = real_suffocation
description = The player will lose breath inside solid blocks and suffers high damage when losing breath.

0 comments on commit b91931e

Please sign in to comment.