1 local get_group = minetest.get_node_group
3 local get_node = minetest.get_node
4 --add nodes that hurt the player into the touch hurt table
6 minetest.register_on_mods_loaded(function()
7 for _,def in pairs(minetest.registered_nodes) do
8 if get_group(def.name, "touch_hurt") > 0 then
9 table.insert(hurt_nodes,def.name)
12 registered_nodes = minetest.registered_nodes
15 --handle nodes around, inside, and above
16 local player_surroundings_index_table = {}
18 --add the player to the index table when they join and remove when they leave
19 minetest.register_on_joinplayer(function(player)
20 local name = player:get_player_name()
21 player_surroundings_index_table[name] = {}
24 minetest.register_on_leaveplayer(function(player)
25 local name = player:get_player_name()
26 player_surroundings_index_table[name] = nil
28 --reset their drowning settings
29 --minetest.register_on_dieplayer(function(ObjectRef, reason))
32 --handle touching hurt
48 local subval = vector.subtract
50 local abs_it = math.abs
51 local floor_it = math.floor
55 local function handle_hurt(player)
56 if player:get_hp() > 0 then
57 player_pos = player:get_pos()
58 name = player:get_player_name()
59 temp_hurt = player_surroundings_index_table[name].hurt
62 x1 = floor_it(abs_it(player_pos.x-c_pos.x)*100)
63 y1 = floor_it(abs_it(player_pos.y-c_pos.y)*100)
64 z1 = floor_it(abs_it(player_pos.z-c_pos.z)*100)
65 --we will assume the player cbox is equal as x=0.8,y=0.5,z=0.8
66 if x1 <= 80 and z1 <= 80 and y1 <= 50 then
67 heart = player:get_hp()
68 player:set_hp(heart - player_surroundings_index_table[name].hurt.hurt_amount)
83 local function handle_hurt_inside(player)
84 if player:get_hp() > 0 then
85 player_pos = player:get_pos()
86 name = player:get_player_name()
87 legs = player_surroundings_index_table[name].legs
88 head = player_surroundings_index_table[name].head
90 hurt_more = get_group(legs, "hurt_inside")
91 if get_group(head, "hurt_inside") > hurt_more then
92 hurt_more = get_group(head, "hurt_inside")
95 heart = player:get_hp()
96 player:set_hp(heart - hurt_more)
103 --handle player suffocating inside solid node
111 local function handle_player_suffocation(player)
112 if player:get_hp() > 0 then
113 player_pos = player:get_pos()
114 name = player:get_player_name()
115 head = player_surroundings_index_table[name].head
117 drawy = registered_nodes[head].drawtype
119 if drawy == "normal" then
120 heart = player:get_hp()
121 player:set_hp(heart - 1)
130 --index specific things in area
131 --declare here for ultra extreme efficiency
132 local get_node = minetest.get_node
140 local v_add = vector.add
141 local v_sub = vector.subtract
142 local get_number = table.getn
145 local function index_players_surroundings()
146 for _,player in ipairs(minetest.get_connected_players()) do
147 if player:get_hp() > 0 then
148 --if not dead begin index
149 name = player:get_player_name()
150 pos = player:get_pos()
152 --under player position (useful for walking on hot stuff)
154 player_surroundings_index_table[name].under = get_node(pos).name
156 --at legs position (useful for pushing a player up)
158 player_surroundings_index_table[name].legs = get_node(pos).name
160 --at camera/head position (useful for drowning/being trapped inside node)
162 pos.y = pos.y + 0.940
163 player_surroundings_index_table[name].head = get_node(pos).name
165 handle_player_suffocation(player)
166 handle_hurt_inside(player)
168 --used for finding a damage node next to the player (centered at player's waist)
172 damage_pos = minetest.find_nodes_in_area(a_min, a_max, hurt_nodes)
174 if get_number(damage_pos) > 0 then
175 for _,found_location in ipairs(damage_pos) do
176 gotten_node = get_node(found_location).name
177 collisionbox = registered_nodes[gotten_node].collision_box
178 hurt_amount = get_group(gotten_node, "touch_hurt")
180 if not collisionbox then
181 collisionbox = {-0.5,-0.5,-0.5,0.5,0.5,0.5}
183 player_surroundings_index_table[name].hurt = {pos=found_location,collisionbox=collisionbox,hurt_amount=hurt_amount}
184 --stop doing damage on player if they got hurt
185 if handle_hurt(player) == true then
191 player_surroundings_index_table[name].hurt = nil
196 --4 times a second server tick
197 minetest.after(0.25, function()
198 index_players_surroundings()
202 index_players_surroundings() --begin
204 --[[ this is disabled for now
205 --handle water drowning - temp - will be moved to a custom function in a future update
207 local function handle_drowning()
208 for _,player in ipairs(minetest.get_connected_players()) do
209 if player:get_hp() > 0 then
210 name = player:get_player_name()
211 if get_group(player_surroundings_index_table[name].head, "drowning") > 0 then
212 breath = player:get_breath()
214 player:set_breath(breath - 1)
217 breath = player:get_breath()
219 player:set_breath(breath + 1)
224 minetest.after(0.5, function()