Localize player_attached
[oweals/minetest_game.git] / mods / default / functions.lua
1 -- mods/default/functions.lua
2
3 --
4 -- Sounds
5 --
6
7 function default.node_sound_defaults(table)
8         table = table or {}
9         table.footstep = table.footstep or
10                         {name="", gain=1.0}
11         table.dug = table.dug or
12                         {name="default_dug_node", gain=0.25}
13         table.place = table.place or
14                         {name="default_place_node_hard", gain=1.0}
15         return table
16 end
17
18 function default.node_sound_stone_defaults(table)
19         table = table or {}
20         table.footstep = table.footstep or
21                         {name="default_hard_footstep", gain=0.5}
22         table.dug = table.dug or
23                         {name="default_hard_footstep", gain=1.0}
24         default.node_sound_defaults(table)
25         return table
26 end
27
28 function default.node_sound_dirt_defaults(table)
29         table = table or {}
30         table.footstep = table.footstep or
31                         {name="default_dirt_footstep", gain=1.0}
32         table.dug = table.dug or
33                         {name="default_dirt_footstep", gain=1.5}
34         table.place = table.place or
35                         {name="default_place_node", gain=1.0}
36         default.node_sound_defaults(table)
37         return table
38 end
39
40 function default.node_sound_sand_defaults(table)
41         table = table or {}
42         table.footstep = table.footstep or
43                         {name="default_sand_footstep", gain=0.5}
44         table.dug = table.dug or
45                         {name="default_sand_footstep", gain=1.0}
46         table.place = table.place or
47                         {name="default_place_node", gain=1.0}
48         default.node_sound_defaults(table)
49         return table
50 end
51
52 function default.node_sound_wood_defaults(table)
53         table = table or {}
54         table.footstep = table.footstep or
55                         {name="default_wood_footstep", gain=0.5}
56         table.dug = table.dug or
57                         {name="default_wood_footstep", gain=1.0}
58         default.node_sound_defaults(table)
59         return table
60 end
61
62 function default.node_sound_leaves_defaults(table)
63         table = table or {}
64         table.footstep = table.footstep or
65                         {name="default_grass_footstep", gain=0.35}
66         table.dug = table.dug or
67                         {name="default_grass_footstep", gain=0.85}
68         table.dig = table.dig or
69                         {name="default_dig_crumbly", gain=0.4}
70         table.place = table.place or
71                         {name="default_place_node", gain=1.0}
72         default.node_sound_defaults(table)
73         return table
74 end
75
76 function default.node_sound_glass_defaults(table)
77         table = table or {}
78         table.footstep = table.footstep or
79                         {name="default_glass_footstep", gain=0.5}
80         table.dug = table.dug or
81                         {name="default_break_glass", gain=1.0}
82         default.node_sound_defaults(table)
83         return table
84 end
85
86 --
87 -- Legacy
88 --
89
90 function default.spawn_falling_node(p, nodename)
91         spawn_falling_node(p, nodename)
92 end
93
94 -- Horrible crap to support old code
95 -- Don't use this and never do what this does, it's completely wrong!
96 -- (More specifically, the client and the C++ code doesn't get the group)
97 function default.register_falling_node(nodename, texture)
98         minetest.log("error", debug.traceback())
99         minetest.log('error', "WARNING: default.register_falling_node is deprecated")
100         if minetest.registered_nodes[nodename] then
101                 minetest.registered_nodes[nodename].groups.falling_node = 1
102         end
103 end
104
105 --
106 -- Global callbacks
107 --
108
109 -- Global environment step function
110 function on_step(dtime)
111         -- print("on_step")
112 end
113 minetest.register_globalstep(on_step)
114
115 function on_placenode(p, node)
116         --print("on_placenode")
117 end
118 minetest.register_on_placenode(on_placenode)
119
120 function on_dignode(p, node)
121         --print("on_dignode")
122 end
123 minetest.register_on_dignode(on_dignode)
124
125 function on_punchnode(p, node)
126 end
127 minetest.register_on_punchnode(on_punchnode)
128
129
130 --
131 -- Grow trees
132 --
133
134 minetest.register_abm({
135         nodenames = {"default:sapling"},
136         interval = 10,
137         chance = 50,
138         action = function(pos, node)
139                 local nu =  minetest.get_node({x=pos.x, y=pos.y-1, z=pos.z}).name
140                 local is_soil = minetest.get_item_group(nu, "soil")
141                 if is_soil == 0 then
142                         return
143                 end
144                 
145                 minetest.log("action", "A sapling grows into a tree at "..minetest.pos_to_string(pos))
146                 local vm = minetest.get_voxel_manip()
147                 local minp, maxp = vm:read_from_map({x=pos.x-16, y=pos.y, z=pos.z-16}, {x=pos.x+16, y=pos.y+16, z=pos.z+16})
148                 local a = VoxelArea:new{MinEdge=minp, MaxEdge=maxp}
149                 local data = vm:get_data()
150                 default.grow_tree(data, a, pos, math.random(1, 4) == 1, math.random(1,100000))
151                 vm:set_data(data)
152                 vm:write_to_map(data)
153                 vm:update_map()
154         end
155 })
156
157 minetest.register_abm({
158         nodenames = {"default:junglesapling"},
159         interval = 10,
160         chance = 50,
161         action = function(pos, node)
162                 local nu =  minetest.get_node({x=pos.x, y=pos.y-1, z=pos.z}).name
163                 local is_soil = minetest.get_item_group(nu, "soil")
164                 if is_soil == 0 then
165                         return
166                 end
167                 
168                 minetest.log("action", "A jungle sapling grows into a tree at "..minetest.pos_to_string(pos))
169                 local vm = minetest.get_voxel_manip()
170                 local minp, maxp = vm:read_from_map({x=pos.x-16, y=pos.y-1, z=pos.z-16}, {x=pos.x+16, y=pos.y+16, z=pos.z+16})
171                 local a = VoxelArea:new{MinEdge=minp, MaxEdge=maxp}
172                 local data = vm:get_data()
173                 default.grow_jungletree(data, a, pos, math.random(1,100000))
174                 vm:set_data(data)
175                 vm:write_to_map(data)
176                 vm:update_map()
177         end
178 })
179
180 --
181 -- Lavacooling
182 --
183
184 default.cool_lava_source = function(pos)
185         minetest.set_node(pos, {name="default:obsidian"})
186         minetest.sound_play("default_cool_lava", {pos = pos,  gain = 0.25})
187 end
188
189 default.cool_lava_flowing = function(pos)
190         minetest.set_node(pos, {name="default:stone"})
191         minetest.sound_play("default_cool_lava", {pos = pos,  gain = 0.25})
192 end
193
194 minetest.register_abm({
195         nodenames = {"default:lava_flowing"},
196         neighbors = {"group:water"},
197         interval = 1,
198         chance = 1,
199         action = function(pos, node, active_object_count, active_object_count_wider)
200                 default.cool_lava_flowing(pos, node, active_object_count, active_object_count_wider)
201         end,
202 })
203
204 minetest.register_abm({
205         nodenames = {"default:lava_source"},
206         neighbors = {"group:water"},
207         interval = 1,
208         chance = 1,
209         action = function(pos, node, active_object_count, active_object_count_wider)
210                 default.cool_lava_source(pos, node, active_object_count, active_object_count_wider)
211         end,
212 })
213
214 --
215 -- Papyrus and cactus growing
216 --
217
218 minetest.register_abm({
219         nodenames = {"default:cactus"},
220         neighbors = {"group:sand"},
221         interval = 50,
222         chance = 20,
223         action = function(pos, node)
224                 pos.y = pos.y-1
225                 local name = minetest.get_node(pos).name
226                 if minetest.get_item_group(name, "sand") ~= 0 then
227                         pos.y = pos.y+1
228                         local height = 0
229                         while minetest.get_node(pos).name == "default:cactus" and height < 4 do
230                                 height = height+1
231                                 pos.y = pos.y+1
232                         end
233                         if height < 4 then
234                                 if minetest.get_node(pos).name == "air" then
235                                         minetest.set_node(pos, {name="default:cactus"})
236                                 end
237                         end
238                 end
239         end,
240 })
241
242 minetest.register_abm({
243         nodenames = {"default:papyrus"},
244         neighbors = {"default:dirt", "default:dirt_with_grass"},
245         interval = 50,
246         chance = 20,
247         action = function(pos, node)
248                 pos.y = pos.y-1
249                 local name = minetest.get_node(pos).name
250                 if name == "default:dirt" or name == "default:dirt_with_grass" then
251                         if minetest.find_node_near(pos, 3, {"group:water"}) == nil then
252                                 return
253                         end
254                         pos.y = pos.y+1
255                         local height = 0
256                         while minetest.get_node(pos).name == "default:papyrus" and height < 4 do
257                                 height = height+1
258                                 pos.y = pos.y+1
259                         end
260                         if height < 4 then
261                                 if minetest.get_node(pos).name == "air" then
262                                         minetest.set_node(pos, {name="default:papyrus"})
263                                 end
264                         end
265                 end
266         end,
267 })
268
269 --
270 -- dig upwards
271 --
272
273 function default.dig_up(pos, node, digger)
274         if digger == nil then return end
275         local np = {x = pos.x, y = pos.y + 1, z = pos.z}
276         local nn = minetest.get_node(np)
277         if nn.name == node.name then
278                 minetest.node_dig(np, nn, digger)
279         end
280 end
281
282 --
283 -- Leafdecay
284 --
285
286 -- To enable leaf decay for a node, add it to the "leafdecay" group.
287 --
288 -- The rating of the group determines how far from a node in the group "tree"
289 -- the node can be without decaying.
290 --
291 -- If param2 of the node is ~= 0, the node will always be preserved. Thus, if
292 -- the player places a node of that kind, you will want to set param2=1 or so.
293 --
294 -- If the node is in the leafdecay_drop group then the it will always be dropped
295 -- as an item
296
297 default.leafdecay_trunk_cache = {}
298 default.leafdecay_enable_cache = true
299 -- Spread the load of finding trunks
300 default.leafdecay_trunk_find_allow_accumulator = 0
301
302 minetest.register_globalstep(function(dtime)
303         local finds_per_second = 5000
304         default.leafdecay_trunk_find_allow_accumulator =
305                         math.floor(dtime * finds_per_second)
306 end)
307
308 minetest.register_abm({
309         nodenames = {"group:leafdecay"},
310         neighbors = {"air", "group:liquid"},
311         -- A low interval and a high inverse chance spreads the load
312         interval = 2,
313         chance = 5,
314
315         action = function(p0, node, _, _)
316                 --print("leafdecay ABM at "..p0.x..", "..p0.y..", "..p0.z..")")
317                 local do_preserve = false
318                 local d = minetest.registered_nodes[node.name].groups.leafdecay
319                 if not d or d == 0 then
320                         --print("not groups.leafdecay")
321                         return
322                 end
323                 local n0 = minetest.get_node(p0)
324                 if n0.param2 ~= 0 then
325                         --print("param2 ~= 0")
326                         return
327                 end
328                 local p0_hash = nil
329                 if default.leafdecay_enable_cache then
330                         p0_hash = minetest.hash_node_position(p0)
331                         local trunkp = default.leafdecay_trunk_cache[p0_hash]
332                         if trunkp then
333                                 local n = minetest.get_node(trunkp)
334                                 local reg = minetest.registered_nodes[n.name]
335                                 -- Assume ignore is a trunk, to make the thing work at the border of the active area
336                                 if n.name == "ignore" or (reg and reg.groups.tree and reg.groups.tree ~= 0) then
337                                         --print("cached trunk still exists")
338                                         return
339                                 end
340                                 --print("cached trunk is invalid")
341                                 -- Cache is invalid
342                                 table.remove(default.leafdecay_trunk_cache, p0_hash)
343                         end
344                 end
345                 if default.leafdecay_trunk_find_allow_accumulator <= 0 then
346                         return
347                 end
348                 default.leafdecay_trunk_find_allow_accumulator =
349                                 default.leafdecay_trunk_find_allow_accumulator - 1
350                 -- Assume ignore is a trunk, to make the thing work at the border of the active area
351                 local p1 = minetest.find_node_near(p0, d, {"ignore", "group:tree"})
352                 if p1 then
353                         do_preserve = true
354                         if default.leafdecay_enable_cache then
355                                 --print("caching trunk")
356                                 -- Cache the trunk
357                                 default.leafdecay_trunk_cache[p0_hash] = p1
358                         end
359                 end
360                 if not do_preserve then
361                         -- Drop stuff other than the node itself
362                         itemstacks = minetest.get_node_drops(n0.name)
363                         for _, itemname in ipairs(itemstacks) do
364                                 if minetest.get_item_group(n0.name, "leafdecay_drop") ~= 0 or
365                                                 itemname ~= n0.name then
366                                         local p_drop = {
367                                                 x = p0.x - 0.5 + math.random(),
368                                                 y = p0.y - 0.5 + math.random(),
369                                                 z = p0.z - 0.5 + math.random(),
370                                         }
371                                         minetest.add_item(p_drop, itemname)
372                                 end
373                         end
374                         -- Remove node
375                         minetest.remove_node(p0)
376                         nodeupdate(p0)
377                 end
378         end
379 })
380