]> git.lizzy.rs Git - dragonfireclient.git/blob - data/builtin.lua
More texture naming fixes
[dragonfireclient.git] / data / builtin.lua
1 --
2 -- This file contains built-in stuff in Minetest implemented in Lua.
3 --
4 -- It is always loaded and executed after registration of the C API,
5 -- before loading and running any mods.
6 --
7
8 function basic_dump2(o)
9         if type(o) == "number" then
10                 return tostring(o)
11         elseif type(o) == "string" then
12                 return string.format("%q", o)
13         elseif type(o) == "boolean" then
14                 return tostring(o)
15         elseif type(o) == "function" then
16                 return "<function>"
17         elseif type(o) == "userdata" then
18                 return "<userdata>"
19         elseif type(o) == "nil" then
20                 return "nil"
21         else
22                 error("cannot dump a " .. type(o))
23                 return nil
24         end
25 end
26
27 function dump2(o, name, dumped)
28         name = name or "_"
29         dumped = dumped or {}
30         io.write(name, " = ")
31         if type(o) == "number" or type(o) == "string" or type(o) == "boolean"
32                         or type(o) == "function" or type(o) == "nil"
33                         or type(o) == "userdata" then
34                 io.write(basic_dump2(o), "\n")
35         elseif type(o) == "table" then
36                 if dumped[o] then
37                         io.write(dumped[o], "\n")
38                 else
39                         dumped[o] = name
40                         io.write("{}\n") -- new table
41                         for k,v in pairs(o) do
42                                 local fieldname = string.format("%s[%s]", name, basic_dump2(k))
43                                 dump2(v, fieldname, dumped)
44                         end
45                 end
46         else
47                 error("cannot dump a " .. type(o))
48                 return nil
49         end
50 end
51
52 function dump(o, dumped)
53         dumped = dumped or {}
54         if type(o) == "number" then
55                 return tostring(o)
56         elseif type(o) == "string" then
57                 return string.format("%q", o)
58         elseif type(o) == "table" then
59                 if dumped[o] then
60                         return "<circular reference>"
61                 end
62                 dumped[o] = true
63                 local t = {}
64                 for k,v in pairs(o) do
65                         t[#t+1] = "" .. k .. " = " .. dump(v, dumped)
66                 end
67                 return "{" .. table.concat(t, ", ") .. "}"
68         elseif type(o) == "boolean" then
69                 return tostring(o)
70         elseif type(o) == "function" then
71                 return "<function>"
72         elseif type(o) == "userdata" then
73                 return "<userdata>"
74         elseif type(o) == "nil" then
75                 return "nil"
76         else
77                 error("cannot dump a " .. type(o))
78                 return nil
79         end
80 end
81
82 --
83 -- Built-in node definitions. Also defined in C.
84 --
85
86 minetest.register_nodedef_defaults({
87         -- name intentionally not defined here
88         drawtype = "normal",
89         visual_scale = 1.0,
90         tile_images = {"unknown_block.png"},
91         inventory_image = "unknown_block.png",
92         special_materials = {
93                 {image="", backface_culling=true},
94                 {image="", backface_culling=true},
95         },
96         alpha = 255,
97         post_effect_color = {a=0, r=0, g=0, b=0},
98         paramtype = "none",
99         is_ground_content = false,
100         light_propagates = false,
101         sunlight_propagates = false,
102         walkable = true,
103         pointable = true,
104         diggable = true,
105         climbable = false,
106         buildable_to = false,
107         wall_mounted = false,
108         often_contains_mineral = false,
109         dug_item = "",
110         extra_dug_item = "",
111         extra_dug_item_rarity = 2,
112         metadata_name = "",
113         liquidtype = "none",
114         liquid_alternative_flowing = "",
115         liquid_alternative_source = "",
116         liquid_viscosity = 0,
117         light_source = 0,
118         damage_per_second = 0,
119         selection_box = {type="regular"},
120         material = {
121                 diggablity = "normal",
122                 weight = 0,
123                 crackiness = 0,
124                 crumbliness = 0,
125                 cuttability = 0,
126                 flammability = 0,
127         },
128         cookresult_item = "", -- Cannot be cooked
129         furnace_cooktime = 3.0,
130         furnace_burntime = -1, -- Cannot be used as fuel
131 })
132
133 minetest.register_node("air", {
134         drawtype = "airlike",
135         paramtype = "light",
136         light_propagates = true,
137         sunlight_propagates = true,
138         walkable = false,
139         pointable = false,
140         diggable = false,
141         buildable_to = true,
142         air_equivalent = true,
143 })
144
145 minetest.register_node("ignore", {
146         drawtype = "airlike",
147         paramtype = "none",
148         light_propagates = false,
149         sunlight_propagates = false,
150         walkable = false,
151         pointable = false,
152         diggable = false,
153         buildable_to = true, -- A way to remove accidentally placed ignores
154         air_equivalent = true,
155 })
156
157 --
158 -- stackstring manipulation functions
159 -- example stackstring: 'craft "apple" 4'
160 -- example item: {type="craft", name="apple"}
161 -- example item: {type="tool", name="SteelPick", wear="23272"}
162 --
163
164 function stackstring_take_item(stackstring)
165         if stackstring == nil then
166                 return '', nil
167         end
168         local stacktype = nil
169         stacktype = string.match(stackstring,
170                         '([%a%d]+)')
171         if stacktype == "node" or stacktype == "craft" then
172                 local itemtype = nil
173                 local itemname = nil
174                 local itemcount = nil
175                 itemtype, itemname, itemcount = string.match(stackstring,
176                                 '([%a%d]+) "([^"]*)" (%d+)')
177                 itemcount = tonumber(itemcount)
178                 if itemcount == 0 then
179                         return '', nil
180                 elseif itemcount == 1 then
181                         return '', {type=itemtype, name=itemname}
182                 else
183                         return itemtype.." \""..itemname.."\" "..(itemcount-1),
184                                         {type=itemtype, name=itemname}
185                 end
186         elseif stacktype == "tool" then
187                 local itemtype = nil
188                 local itemname = nil
189                 local itemwear = nil
190                 itemtype, itemname, itemwear = string.match(stackstring,
191                                 '([%a%d]+) "([^"]*)" (%d+)')
192                 itemwear = tonumber(itemwear)
193                 return '', {type=itemtype, name=itemname, wear=itemwear}
194         end
195 end
196
197 function stackstring_put_item(stackstring, item)
198         if item == nil then
199                 return stackstring, false
200         end
201         stackstring = stackstring or ''
202         local stacktype = nil
203         stacktype = string.match(stackstring,
204                         '([%a%d]+)')
205         stacktype = stacktype or ''
206         if stacktype ~= '' and stacktype ~= item.type then
207                 return stackstring, false
208         end
209         if item.type == "node" or item.type == "craft" then
210                 local itemtype = nil
211                 local itemname = nil
212                 local itemcount = nil
213                 itemtype, itemname, itemcount = string.match(stackstring,
214                                 '([%a%d]+) "([^"]*)" (%d+)')
215                 itemtype = itemtype or item.type
216                 itemname = itemname or item.name
217                 if itemcount == nil then
218                         itemcount = 0
219                 end
220                 itemcount = itemcount + 1
221                 return itemtype.." \""..itemname.."\" "..itemcount, true
222         elseif item.type == "tool" then
223                 if stacktype ~= nil then
224                         return stackstring, false
225                 end
226                 local itemtype = nil
227                 local itemname = nil
228                 local itemwear = nil
229                 itemtype, itemname, itemwear = string.match(stackstring,
230                                 '([%a%d]+) "([^"]*)" (%d+)')
231                 itemwear = tonumber(itemwear)
232                 return itemtype.." \""..itemname.."\" "..itemwear, true
233         end
234         return stackstring, false
235 end
236
237 function stackstring_put_stackstring(stackstring, src)
238         while src ~= '' do
239                 --print("src="..dump(src))
240                 src, item = stackstring_take_item(src)
241                 --print("src="..dump(src).." item="..dump(item))
242                 local success
243                 stackstring, success = stackstring_put_item(stackstring, item)
244                 if not success then
245                         return stackstring, false
246                 end
247         end
248         return stackstring, true
249 end
250
251 function test_stackstring()
252         local stack
253         local item
254         local success
255
256         stack, item = stackstring_take_item('node "TNT" 3')
257         assert(stack == 'node "TNT" 2')
258         assert(item.type == 'node')
259         assert(item.name == 'TNT')
260
261         stack, item = stackstring_take_item('craft "with spaces" 2')
262         assert(stack == 'craft "with spaces" 1')
263         assert(item.type == 'craft')
264         assert(item.name == 'with spaces')
265
266         stack, item = stackstring_take_item('craft "with spaces" 1')
267         assert(stack == '')
268         assert(item.type == 'craft')
269         assert(item.name == 'with spaces')
270
271         stack, item = stackstring_take_item('craft "s8df2kj3" 0')
272         assert(stack == '')
273         assert(item == nil)
274
275         stack, item = stackstring_take_item('tool "With Spaces" 32487')
276         assert(stack == '')
277         assert(item.type == 'tool')
278         assert(item.name == 'With Spaces')
279         assert(item.wear == 32487)
280
281         stack, success = stackstring_put_item('node "With Spaces" 40',
282                         {type='node', name='With Spaces'})
283         assert(stack == 'node "With Spaces" 41')
284         assert(success == true)
285
286         stack, success = stackstring_put_item('craft "With Spaces" 40',
287                         {type='craft', name='With Spaces'})
288         assert(stack == 'craft "With Spaces" 41')
289         assert(success == true)
290
291         stack, success = stackstring_put_item('tool "With Spaces" 32487',
292                         {type='tool', name='With Spaces'})
293         assert(stack == 'tool "With Spaces" 32487')
294         assert(success == false)
295
296         stack, success = stackstring_put_item('node "With Spaces" 40',
297                         {type='tool', name='With Spaces'})
298         assert(stack == 'node "With Spaces" 40')
299         assert(success == false)
300         
301         assert(stackstring_put_stackstring('node "With Spaces" 2',
302                         'node "With Spaces" 1') == 'node "With Spaces" 3')
303 end
304 test_stackstring()
305
306 --
307 -- nodeitem helpers
308 --
309
310 minetest.inventorycube = function(img1, img2, img3)
311         img2 = img2 or img1
312         img3 = img3 or img1
313         return "[inventorycube"
314                         .. "{" .. img1:gsub("%^", "&")
315                         .. "{" .. img2:gsub("%^", "&")
316                         .. "{" .. img3:gsub("%^", "&")
317 end
318
319 --
320 -- craftitem helpers
321 --
322
323 minetest.craftitem_place_item = function(item, placer, pos)
324         --print("craftitem_place_item")
325         --print("item: " .. dump(item))
326         --print("placer: " .. dump(placer))
327         --print("pos: " .. dump(pos))
328         minetest.env:add_item(pos, 'craft "' .. item .. '" 1')
329         return true
330 end
331
332 minetest.craftitem_eat = function(hp_change)
333         return function(item, user, pointed_thing)  -- closure
334                 --print("craftitem_eat(" .. hp_change .. ")")
335                 --print("item: " .. dump(item))
336                 --print("user: " .. dump(user))
337                 --print("pointed_thing: " .. dump(pointed_thing))
338                 user:set_hp(user:get_hp() + hp_change)
339                 return true
340         end
341 end
342
343 --
344 -- Default material types
345 --
346
347 function minetest.digprop_constanttime(time)
348         return {
349                 diggability = "constant",
350                 constant_time = time,
351         }
352 end
353
354 function minetest.digprop_stonelike(toughness)
355         return {
356                 diggablity = "normal",
357                 weight = toughness * 5,
358                 crackiness = 1,
359                 crumbliness = -0.1,
360                 cuttability = -0.2,
361         }
362 end
363
364 function minetest.digprop_dirtlike(toughness)
365         return {
366                 diggablity = "normal",
367                 weight = toughness * 1.2,
368                 crackiness = 0,
369                 crumbliness = 1.2,
370                 cuttability = -0.4,
371         }
372 end
373
374 function minetest.digprop_gravellike(toughness)
375         return {
376                 diggablity = "normal",
377                 weight = toughness * 2,
378                 crackiness = 0.2,
379                 crumbliness = 1.5,
380                 cuttability = -1.0,
381         }
382 end
383
384 function minetest.digprop_woodlike(toughness)
385         return {
386                 diggablity = "normal",
387                 weight = toughness * 1.0,
388                 crackiness = 0.75,
389                 crumbliness = -1.0,
390                 cuttability = 1.5,
391         }
392 end
393
394 function minetest.digprop_leaveslike(toughness)
395         return {
396                 diggablity = "normal",
397                 weight = toughness * (-0.5),
398                 crackiness = 0,
399                 crumbliness = 0,
400                 cuttability = 2.0,
401         }
402 end
403
404 function minetest.digprop_glasslike(toughness)
405         return {
406                 diggablity = "normal",
407                 weight = toughness * 0.1,
408                 crackiness = 2.0,
409                 crumbliness = -1.0,
410                 cuttability = -1.0,
411         }
412 end
413
414 --
415 -- Creative inventory
416 --
417
418 minetest.creative_inventory = {}
419
420 minetest.add_to_creative_inventory = function(itemstring)
421         table.insert(minetest.creative_inventory, itemstring)
422 end
423
424 --
425 -- Callback registration
426 --
427
428 function make_registration()
429         local t = {}
430         local registerfunc = function(func) table.insert(t, func) end
431         return t, registerfunc
432 end
433
434 minetest.registered_on_chat_messages, minetest.register_on_chat_message = make_registration()
435 minetest.registered_globalsteps, minetest.register_globalstep = make_registration()
436 minetest.registered_on_placenodes, minetest.register_on_placenode = make_registration()
437 minetest.registered_on_dignodes, minetest.register_on_dignode = make_registration()
438 minetest.registered_on_punchnodes, minetest.register_on_punchnode = make_registration()
439 minetest.registered_on_generateds, minetest.register_on_generated = make_registration()
440 minetest.registered_on_newplayers, minetest.register_on_newplayer = make_registration()
441 minetest.registered_on_respawnplayers, minetest.register_on_respawnplayer = make_registration()
442
443 -- END