]> git.lizzy.rs Git - Crafter.git/blob - mods/new_functions/init.lua
Add player node suffocation
[Crafter.git] / mods / new_functions / init.lua
1 local get_group = minetest.get_node_group
2 local registered_nodes
3 local get_node = minetest.get_node
4 --add nodes that hurt the player into the touch hurt table
5 local hurt_nodes = {}
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)
10                 end
11         end
12         registered_nodes = minetest.registered_nodes
13 end)
14
15 --handle nodes around, inside, and above
16 local player_surroundings_index_table = {}
17
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] = {}
22 end)
23
24 minetest.register_on_leaveplayer(function(player)
25         local name = player:get_player_name()
26         player_surroundings_index_table[name] = nil
27 end)
28 --reset their drowning settings
29 --minetest.register_on_dieplayer(function(ObjectRef, reason))
30
31
32 --handle touching hurt
33 local player_pos
34
35 local c_pos
36
37 local temp_hurt
38 local x1
39 local y1
40 local z1
41
42 local xcompare
43 local ycompare
44 local zcompare
45
46 local c_player
47
48 local subval = vector.subtract
49
50 local abs_it = math.abs
51 local floor_it = math.floor
52
53 local heart
54
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
60                 if temp_hurt then
61                         c_pos = temp_hurt.pos
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)
69                                 return(true)
70                         end
71                 end
72         end
73         return(false)
74 end
75
76 --handle inside hurt
77 local c_player
78 local heart
79 local legs
80 local head
81 local hurt_more
82
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
89                 if legs and head then
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")
93                         end
94                         
95                         heart = player:get_hp()
96                         player:set_hp(heart - hurt_more)
97                         return(true)
98                 end
99         end
100         return(false)
101 end
102
103 --handle player suffocating inside solid node
104 local c_player
105 local heart
106 local legs
107 local head
108 local hurt_more
109 local drawy
110
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
116                 if head then
117                         drawy = registered_nodes[head].drawtype
118
119                         if drawy == "normal" then
120                                 heart = player:get_hp()
121                                 player:set_hp(heart - 1)
122                                 return(true)
123                         end
124                 end
125         end
126         return(false)
127 end
128
129
130 --index specific things in area
131 --declare here for ultra extreme efficiency
132 local get_node = minetest.get_node
133 local pos
134 local node
135 local name
136 local damage_pos
137 local collisionbox
138 local a_min
139 local a_max
140 local v_add = vector.add
141 local v_sub = vector.subtract
142 local get_number = table.getn
143 local hurt_amount
144 local gotten_node
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()
151                         
152                         --under player position (useful for walking on hot stuff)
153                         pos.y = pos.y - 0.1
154                         player_surroundings_index_table[name].under = get_node(pos).name
155                         
156                         --at legs position (useful for pushing a player up)
157                         pos.y = pos.y + 0.6
158                         player_surroundings_index_table[name].legs = get_node(pos).name
159                         
160                         --at camera/head position (useful for drowning/being trapped inside node)
161                         
162                         pos.y = pos.y + 0.940
163                         player_surroundings_index_table[name].head = get_node(pos).name
164                         
165                         handle_player_suffocation(player)
166                         handle_hurt_inside(player)
167
168                         --used for finding a damage node next to the player (centered at player's waist)
169                         pos.y = pos.y - 0.74
170                         a_min = v_sub(pos,1)
171                         a_max = v_add(pos,1)
172                         damage_pos = minetest.find_nodes_in_area(a_min, a_max, hurt_nodes)
173                         
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")
179                                         
180                                         if not collisionbox then
181                                                 collisionbox = {-0.5,-0.5,-0.5,0.5,0.5,0.5}
182                                         end
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
186                                                 break
187                                         end
188                                 end
189                         else
190                                 collisionbox = nil
191                                 player_surroundings_index_table[name].hurt = nil
192                         end
193                                 
194                 end
195         end
196         --4 times a second server tick
197         minetest.after(0.25, function()
198                 index_players_surroundings()
199         end)
200 end
201
202 index_players_surroundings() --begin
203
204 --[[ this is disabled for now
205 --handle water drowning - temp - will be moved to a custom function in a future update
206 local breath
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()
213                                 if breath > 0 then
214                                         player:set_breath(breath - 1)
215                                 end
216                         else
217                                 breath = player:get_breath()
218                                 if breath < 11 then
219                                         player:set_breath(breath + 1)
220                                 end
221                         end
222                 end
223         end
224         minetest.after(0.5, function()
225                 handle_drowning()
226         end)
227 end
228
229 handle_drowning()
230 ]]--
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251