2 -- This file contains built-in stuff in Minetest implemented in Lua.
4 -- It is always loaded and executed after registration of the C API,
5 -- before loading and running any mods.
8 function basic_dump2(o)
9 if type(o) == "number" then
11 elseif type(o) == "string" then
12 return string.format("%q", o)
13 elseif type(o) == "boolean" then
15 elseif type(o) == "function" then
17 elseif type(o) == "userdata" then
19 elseif type(o) == "nil" then
22 error("cannot dump a " .. type(o))
27 function dump2(o, name, dumped)
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
37 io.write(dumped[o], "\n")
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)
47 error("cannot dump a " .. type(o))
52 function dump(o, dumped)
54 if type(o) == "number" then
56 elseif type(o) == "string" then
57 return string.format("%q", o)
58 elseif type(o) == "table" then
60 return "<circular reference>"
64 for k,v in pairs(o) do
65 t[#t+1] = "" .. k .. " = " .. dump(v, dumped)
67 return "{" .. table.concat(t, ", ") .. "}"
68 elseif type(o) == "boolean" then
70 elseif type(o) == "function" then
72 elseif type(o) == "userdata" then
74 elseif type(o) == "nil" then
77 error("cannot dump a " .. type(o))
83 -- Built-in node definitions. Also defined in C.
86 minetest.register_nodedef_defaults({
87 -- name intentionally not defined here
90 tile_images = {"unknown_block.png"},
91 inventory_image = "unknown_block.png",
93 {image="", backface_culling=true},
94 {image="", backface_culling=true},
97 post_effect_color = {a=0, r=0, g=0, b=0},
99 is_ground_content = false,
100 sunlight_propagates = false,
105 buildable_to = false,
106 wall_mounted = false,
107 often_contains_mineral = false,
110 extra_dug_item_rarity = 2,
113 liquid_alternative_flowing = "",
114 liquid_alternative_source = "",
115 liquid_viscosity = 0,
117 damage_per_second = 0,
118 selection_box = {type="regular"},
120 diggablity = "normal",
127 cookresult_item = "", -- Cannot be cooked
128 furnace_cooktime = 3.0,
129 furnace_burntime = -1, -- Cannot be used as fuel
132 minetest.register_node("air", {
133 drawtype = "airlike",
135 sunlight_propagates = true,
140 air_equivalent = true,
143 minetest.register_node("ignore", {
144 drawtype = "airlike",
146 sunlight_propagates = false,
150 buildable_to = true, -- A way to remove accidentally placed ignores
151 air_equivalent = true,
155 -- stackstring manipulation functions
156 -- example stackstring: 'craft "apple" 4'
157 -- example item: {type="craft", name="apple"}
158 -- example item: {type="tool", name="SteelPick", wear="23272"}
161 function stackstring_take_item(stackstring)
162 if stackstring == nil then
165 local stacktype = nil
166 stacktype = string.match(stackstring,
168 if stacktype == "node" or stacktype == "craft" then
171 local itemcount = nil
172 itemtype, itemname, itemcount = string.match(stackstring,
173 '([%a%d]+) "([^"]*)" (%d+)')
174 itemcount = tonumber(itemcount)
175 if itemcount == 0 then
177 elseif itemcount == 1 then
178 return '', {type=itemtype, name=itemname}
180 return itemtype.." \""..itemname.."\" "..(itemcount-1),
181 {type=itemtype, name=itemname}
183 elseif stacktype == "tool" then
187 itemtype, itemname, itemwear = string.match(stackstring,
188 '([%a%d]+) "([^"]*)" (%d+)')
189 itemwear = tonumber(itemwear)
190 return '', {type=itemtype, name=itemname, wear=itemwear}
194 function stackstring_put_item(stackstring, item)
196 return stackstring, false
198 stackstring = stackstring or ''
199 local stacktype = nil
200 stacktype = string.match(stackstring,
202 stacktype = stacktype or ''
203 if stacktype ~= '' and stacktype ~= item.type then
204 return stackstring, false
206 if item.type == "node" or item.type == "craft" then
209 local itemcount = nil
210 itemtype, itemname, itemcount = string.match(stackstring,
211 '([%a%d]+) "([^"]*)" (%d+)')
212 itemtype = itemtype or item.type
213 itemname = itemname or item.name
214 if itemcount == nil then
217 itemcount = itemcount + 1
218 return itemtype.." \""..itemname.."\" "..itemcount, true
219 elseif item.type == "tool" then
220 if stacktype ~= nil then
221 return stackstring, false
226 itemtype, itemname, itemwear = string.match(stackstring,
227 '([%a%d]+) "([^"]*)" (%d+)')
228 itemwear = tonumber(itemwear)
229 return itemtype.." \""..itemname.."\" "..itemwear, true
231 return stackstring, false
234 function stackstring_put_stackstring(stackstring, src)
236 --print("src="..dump(src))
237 src, item = stackstring_take_item(src)
238 --print("src="..dump(src).." item="..dump(item))
240 stackstring, success = stackstring_put_item(stackstring, item)
242 return stackstring, false
245 return stackstring, true
248 function test_stackstring()
253 stack, item = stackstring_take_item('node "TNT" 3')
254 assert(stack == 'node "TNT" 2')
255 assert(item.type == 'node')
256 assert(item.name == 'TNT')
258 stack, item = stackstring_take_item('craft "with spaces" 2')
259 assert(stack == 'craft "with spaces" 1')
260 assert(item.type == 'craft')
261 assert(item.name == 'with spaces')
263 stack, item = stackstring_take_item('craft "with spaces" 1')
265 assert(item.type == 'craft')
266 assert(item.name == 'with spaces')
268 stack, item = stackstring_take_item('craft "s8df2kj3" 0')
272 stack, item = stackstring_take_item('tool "With Spaces" 32487')
274 assert(item.type == 'tool')
275 assert(item.name == 'With Spaces')
276 assert(item.wear == 32487)
278 stack, success = stackstring_put_item('node "With Spaces" 40',
279 {type='node', name='With Spaces'})
280 assert(stack == 'node "With Spaces" 41')
281 assert(success == true)
283 stack, success = stackstring_put_item('craft "With Spaces" 40',
284 {type='craft', name='With Spaces'})
285 assert(stack == 'craft "With Spaces" 41')
286 assert(success == true)
288 stack, success = stackstring_put_item('tool "With Spaces" 32487',
289 {type='tool', name='With Spaces'})
290 assert(stack == 'tool "With Spaces" 32487')
291 assert(success == false)
293 stack, success = stackstring_put_item('node "With Spaces" 40',
294 {type='tool', name='With Spaces'})
295 assert(stack == 'node "With Spaces" 40')
296 assert(success == false)
298 assert(stackstring_put_stackstring('node "With Spaces" 2',
299 'node "With Spaces" 1') == 'node "With Spaces" 3')
307 minetest.inventorycube = function(img1, img2, img3)
310 return "[inventorycube"
311 .. "{" .. img1:gsub("%^", "&")
312 .. "{" .. img2:gsub("%^", "&")
313 .. "{" .. img3:gsub("%^", "&")
320 minetest.craftitem_place_item = function(item, placer, pos)
321 --print("craftitem_place_item")
322 --print("item: " .. dump(item))
323 --print("placer: " .. dump(placer))
324 --print("pos: " .. dump(pos))
325 minetest.env:add_item(pos, 'craft "' .. item .. '" 1')
329 minetest.craftitem_eat = function(hp_change)
330 return function(item, user, pointed_thing) -- closure
331 --print("craftitem_eat(" .. hp_change .. ")")
332 --print("item: " .. dump(item))
333 --print("user: " .. dump(user))
334 --print("pointed_thing: " .. dump(pointed_thing))
335 user:set_hp(user:get_hp() + hp_change)
341 -- Default material types
344 function minetest.digprop_constanttime(time)
346 diggability = "constant",
347 constant_time = time,
351 function minetest.digprop_stonelike(toughness)
353 diggablity = "normal",
354 weight = toughness * 5,
361 function minetest.digprop_dirtlike(toughness)
363 diggablity = "normal",
364 weight = toughness * 1.2,
371 function minetest.digprop_gravellike(toughness)
373 diggablity = "normal",
374 weight = toughness * 2,
381 function minetest.digprop_woodlike(toughness)
383 diggablity = "normal",
384 weight = toughness * 1.0,
391 function minetest.digprop_leaveslike(toughness)
393 diggablity = "normal",
394 weight = toughness * (-0.5),
401 function minetest.digprop_glasslike(toughness)
403 diggablity = "normal",
404 weight = toughness * 0.1,
412 -- Creative inventory
415 minetest.creative_inventory = {}
417 minetest.add_to_creative_inventory = function(itemstring)
418 table.insert(minetest.creative_inventory, itemstring)
422 -- Callback registration
425 function make_registration()
427 local registerfunc = function(func) table.insert(t, func) end
428 return t, registerfunc
431 minetest.registered_on_chat_messages, minetest.register_on_chat_message = make_registration()
432 minetest.registered_globalsteps, minetest.register_globalstep = make_registration()
433 minetest.registered_on_placenodes, minetest.register_on_placenode = make_registration()
434 minetest.registered_on_dignodes, minetest.register_on_dignode = make_registration()
435 minetest.registered_on_punchnodes, minetest.register_on_punchnode = make_registration()
436 minetest.registered_on_generateds, minetest.register_on_generated = make_registration()
437 minetest.registered_on_newplayers, minetest.register_on_newplayer = make_registration()
438 minetest.registered_on_respawnplayers, minetest.register_on_respawnplayer = make_registration()