1 function basic_dump2(o)
2 if type(o) == "number" then
4 elseif type(o) == "string" then
5 return string.format("%q", o)
6 elseif type(o) == "boolean" then
8 elseif type(o) == "function" then
10 elseif type(o) == "userdata" then
12 elseif type(o) == "nil" then
15 error("cannot dump a " .. type(o))
20 function dump2(o, name, dumped)
24 if type(o) == "number" or type(o) == "string" or type(o) == "boolean"
25 or type(o) == "function" or type(o) == "nil"
26 or type(o) == "userdata" then
27 io.write(basic_dump2(o), "\n")
28 elseif type(o) == "table" then
30 io.write(dumped[o], "\n")
33 io.write("{}\n") -- new table
34 for k,v in pairs(o) do
35 local fieldname = string.format("%s[%s]", name, basic_dump2(k))
36 dump2(v, fieldname, dumped)
40 error("cannot dump a " .. type(o))
45 function dump(o, dumped)
47 if type(o) == "number" then
49 elseif type(o) == "string" then
50 return string.format("%q", o)
51 elseif type(o) == "table" then
53 return "<circular reference>"
57 for k,v in pairs(o) do
58 t[#t+1] = "" .. k .. " = " .. dump(v, dumped)
60 return "{" .. table.concat(t, ", ") .. "}"
61 elseif type(o) == "boolean" then
63 elseif type(o) == "function" then
65 elseif type(o) == "userdata" then
67 elseif type(o) == "nil" then
70 error("cannot dump a " .. type(o))
76 -- Mods should prefix their textures with modname_, eg. given the mod
77 -- name "foomod", a texture could be called "foomod_superfurnace.png"
80 -- minetest.register_entity(name, prototype_table)
81 -- minetest.register_tool(name, {lots of stuff})
82 -- minetest.register_node(name, {lots of stuff})
83 -- minetest.register_craft({output=item, recipe={...})
84 -- minetest.register_globalstep(func)
85 -- minetest.register_on_placenode(func)
86 -- minetest.register_on_dignode(func)
89 -- minetest.env - environment reference
92 -- minetest.registered_entities
93 -- ^ List of registered entity prototypes, indexed by name
94 -- minetest.object_refs
95 -- ^ List of object references, indexed by active object id
96 -- minetest.luaentities
97 -- ^ List of lua entities, indexed by active object id
99 -- EnvRef is basically ServerEnvironment and ServerMap combined.
101 -- - add_node(pos, node)
102 -- - remove_node(pos)
104 -- - add_luaentity(pos, name)
106 -- ObjectRef is basically ServerActiveObject.
107 -- ObjectRef methods:
108 -- - remove(): remove object (after returning from Lua)
109 -- - getpos(): returns {x=num, y=num, z=num}
110 -- - setpos(pos); pos={x=num, y=num, z=num}
111 -- - moveto(pos, continuous=false): interpolated move
112 -- - add_to_inventory(itemstring): add an item to object inventory
114 -- Registered entities:
115 -- - Functions receive a "luaentity" as self:
116 -- - It has the member .object, which is an ObjectRef pointing to the object
117 -- - The original prototype stuff is visible directly via a metatable
119 -- - on_activate(self, staticdata)
120 -- - on_step(self, dtime)
121 -- - on_punch(self, hitter)
122 -- - on_rightclick(self, clicker)
123 -- - get_staticdata(self): return string
125 -- MapNode representation:
126 -- {name="name", param1=num, param2=num}
128 -- Position representation:
129 -- {x=num, y=num, z=num}
132 -- print("minetest dump: "..dump(minetest))
134 minetest.register_tool("WPick", {
135 image = "tool_woodpick.png",
138 dt_crackiness = -0.5,
147 minetest.register_tool("STPick", {
148 image = "tool_stonepick.png",
151 dt_crackiness = -0.5,
154 basedurability = 100,
160 minetest.register_tool("SteelPick", {
161 image = "tool_steelpick.png",
164 dt_crackiness = -0.5,
167 basedurability = 333,
173 minetest.register_tool("MesePick", {
174 image = "tool_mesepick.png",
180 basedurability = 1337,
186 minetest.register_tool("WShovel", {
187 image = "tool_woodshovel.png",
191 dt_crumbliness = -1.5,
192 dt_cuttability = 0.3,
199 minetest.register_tool("STShovel", {
200 image = "tool_stoneshovel.png",
204 dt_crumbliness = -1.5,
205 dt_cuttability = 0.1,
206 basedurability = 100,
212 minetest.register_tool("SteelShovel", {
213 image = "tool_steelshovel.png",
217 dt_crumbliness = -1.5,
218 dt_cuttability = 0.0,
219 basedurability = 330,
225 minetest.register_tool("WAxe", {
226 image = "tool_woodaxe.png",
229 dt_crackiness = -0.2,
231 dt_cuttability = -0.5,
238 minetest.register_tool("STAxe", {
239 image = "tool_stoneaxe.png",
242 dt_crackiness = -0.2,
244 dt_cuttability = -0.5,
245 basedurability = 100,
251 minetest.register_tool("SteelAxe", {
252 image = "tool_steelaxe.png",
255 dt_crackiness = -0.2,
257 dt_cuttability = -0.5,
258 basedurability = 330,
264 minetest.register_tool("WSword", {
265 image = "tool_woodsword.png",
277 minetest.register_tool("STSword", {
278 image = "tool_stonesword.png",
284 basedurability = 100,
290 minetest.register_tool("SteelSword", {
291 image = "tool_steelsword.png",
297 basedurability = 330,
303 minetest.register_tool("", {
318 minetest.register_tool("horribletool", {
333 minetest.register_node("somenode", {
334 tile_images = {"lava.png", "mese.png", "stone.png", "grass.png", "cobble.png", "tree_top.png"},
335 inventory_image = "treeprop.png"
338 minetest.register_node("TNT", {
339 tile_images = {"tnt_top.png", "tnt_bottom.png", "tnt_side.png", "tnt_side.png", "tnt_side.png", "tnt_side.png"},
340 inventory_image = "tnt_side.png"
343 minetest.register_craft({
344 output = 'NodeItem "wood" 4',
350 minetest.register_craft({
351 output = 'CraftItem "Stick" 4',
357 minetest.register_craft({
358 output = 'NodeItem "wooden_fence" 2',
360 {'CraftItem "Stick"', 'CraftItem "Stick"', 'CraftItem "Stick"'},
361 {'CraftItem "Stick"', 'CraftItem "Stick"', 'CraftItem "Stick"'},
365 minetest.register_craft({
366 output = 'NodeItem "sign_wall" 1',
368 {'NodeItem "wood"', 'NodeItem "wood"', 'NodeItem "wood"'},
369 {'NodeItem "wood"', 'NodeItem "wood"', 'NodeItem "wood"'},
370 {'', 'CraftItem "Stick"', ''},
374 minetest.register_craft({
375 output = 'NodeItem "torch" 4',
377 {'CraftItem "lump_of_coal"'},
378 {'CraftItem "Stick"'},
382 minetest.register_craft({
383 output = 'ToolItem "WPick"',
385 {'NodeItem "wood"', 'NodeItem "wood"', 'NodeItem "wood"'},
386 {'', 'CraftItem "Stick"', ''},
387 {'', 'CraftItem "Stick"', ''},
391 minetest.register_craft({
392 output = 'ToolItem "STPick"',
394 {'NodeItem "cobble"', 'NodeItem "cobble"', 'NodeItem "cobble"'},
395 {'', 'CraftItem "Stick"', ''},
396 {'', 'CraftItem "Stick"', ''},
400 minetest.register_craft({
401 output = 'ToolItem "SteelPick"',
403 {'CraftItem "steel_ingot"', 'CraftItem "steel_ingot"', 'CraftItem "steel_ingot"'},
404 {'', 'CraftItem "Stick"', ''},
405 {'', 'CraftItem "Stick"', ''},
409 minetest.register_craft({
410 output = 'ToolItem "MesePick"',
412 {'NodeItem "mese"', 'NodeItem "mese"', 'NodeItem "mese"'},
413 {'', 'CraftItem "Stick"', ''},
414 {'', 'CraftItem "Stick"', ''},
418 minetest.register_craft({
419 output = 'ToolItem "WShovel"',
422 {'CraftItem "Stick"'},
423 {'CraftItem "Stick"'},
427 minetest.register_craft({
428 output = 'ToolItem "STShovel"',
430 {'NodeItem "cobble"'},
431 {'CraftItem "Stick"'},
432 {'CraftItem "Stick"'},
436 minetest.register_craft({
437 output = 'ToolItem "SteelShovel"',
439 {'CraftItem "steel_ingot"'},
440 {'CraftItem "Stick"'},
441 {'CraftItem "Stick"'},
445 minetest.register_craft({
446 output = 'ToolItem "WAxe"',
448 {'NodeItem "wood"', 'NodeItem "wood"'},
449 {'NodeItem "wood"', 'CraftItem "Stick"'},
450 {'', 'CraftItem "Stick"'},
454 minetest.register_craft({
455 output = 'ToolItem "STAxe"',
457 {'NodeItem "cobble"', 'NodeItem "cobble"'},
458 {'NodeItem "cobble"', 'CraftItem "Stick"'},
459 {'', 'CraftItem "Stick"'},
463 minetest.register_craft({
464 output = 'ToolItem "SteelAxe"',
466 {'CraftItem "steel_ingot"', 'CraftItem "steel_ingot"'},
467 {'CraftItem "steel_ingot"', 'CraftItem "Stick"'},
468 {'', 'CraftItem "Stick"'},
472 minetest.register_craft({
473 output = 'ToolItem "WSword"',
477 {'CraftItem "Stick"'},
481 minetest.register_craft({
482 output = 'ToolItem "STSword"',
484 {'NodeItem "cobble"'},
485 {'NodeItem "cobble"'},
486 {'CraftItem "Stick"'},
490 minetest.register_craft({
491 output = 'ToolItem "SteelSword"',
493 {'CraftItem "steel_ingot"'},
494 {'CraftItem "steel_ingot"'},
495 {'CraftItem "Stick"'},
499 minetest.register_craft({
500 output = 'NodeItem "rail" 15',
502 {'CraftItem "steel_ingot"', '', 'CraftItem "steel_ingot"'},
503 {'CraftItem "steel_ingot"', 'CraftItem "Stick"', 'CraftItem "steel_ingot"'},
504 {'CraftItem "steel_ingot"', '', 'CraftItem "steel_ingot"'},
508 minetest.register_craft({
509 output = 'NodeItem "chest" 1',
511 {'NodeItem "wood"', 'NodeItem "wood"', 'NodeItem "wood"'},
512 {'NodeItem "wood"', '', 'NodeItem "wood"'},
513 {'NodeItem "wood"', 'NodeItem "wood"', 'NodeItem "wood"'},
517 minetest.register_craft({
518 output = 'NodeItem "locked_chest" 1',
520 {'NodeItem "wood"', 'NodeItem "wood"', 'NodeItem "wood"'},
521 {'NodeItem "wood"', 'CraftItem "steel_ingot"', 'NodeItem "wood"'},
522 {'NodeItem "wood"', 'NodeItem "wood"', 'NodeItem "wood"'},
526 minetest.register_craft({
527 output = 'NodeItem "furnace" 1',
529 {'NodeItem "cobble"', 'NodeItem "cobble"', 'NodeItem "cobble"'},
530 {'NodeItem "cobble"', '', 'NodeItem "cobble"'},
531 {'NodeItem "cobble"', 'NodeItem "cobble"', 'NodeItem "cobble"'},
535 minetest.register_craft({
536 output = 'NodeItem "steelblock" 1',
538 {'CraftItem "steel_ingot"', 'CraftItem "steel_ingot"', 'CraftItem "steel_ingot"'},
539 {'CraftItem "steel_ingot"', 'CraftItem "steel_ingot"', 'CraftItem "steel_ingot"'},
540 {'CraftItem "steel_ingot"', 'CraftItem "steel_ingot"', 'CraftItem "steel_ingot"'},
544 minetest.register_craft({
545 output = 'NodeItem "sandstone" 1',
547 {'NodeItem "sand"', 'NodeItem "sand"'},
548 {'NodeItem "sand"', 'NodeItem "sand"'},
552 minetest.register_craft({
553 output = 'NodeItem "clay" 1',
555 {'CraftItem "lump_of_clay"', 'CraftItem "lump_of_clay"'},
556 {'CraftItem "lump_of_clay"', 'CraftItem "lump_of_clay"'},
560 minetest.register_craft({
561 output = 'NodeItem "brick" 1',
563 {'CraftItem "clay_brick"', 'CraftItem "clay_brick"'},
564 {'CraftItem "clay_brick"', 'CraftItem "clay_brick"'},
568 minetest.register_craft({
569 output = 'CraftItem "paper" 1',
571 {'NodeItem "papyrus"', 'NodeItem "papyrus"', 'NodeItem "papyrus"'},
575 minetest.register_craft({
576 output = 'CraftItem "book" 1',
578 {'CraftItem "paper"'},
579 {'CraftItem "paper"'},
580 {'CraftItem "paper"'},
584 minetest.register_craft({
585 output = 'NodeItem "bookshelf" 1',
587 {'NodeItem "wood"', 'NodeItem "wood"', 'NodeItem "wood"'},
588 {'CraftItem "book"', 'CraftItem "book"', 'CraftItem "book"'},
589 {'NodeItem "wood"', 'NodeItem "wood"', 'NodeItem "wood"'},
593 minetest.register_craft({
594 output = 'NodeItem "ladder" 1',
596 {'CraftItem "Stick"', '', 'CraftItem "Stick"'},
597 {'CraftItem "Stick"', 'CraftItem "Stick"', 'CraftItem "Stick"'},
598 {'CraftItem "Stick"', '', 'CraftItem "Stick"'},
602 minetest.register_craft({
603 output = 'CraftItem "apple_iron" 1',
605 {'', 'CraftItem "steel_ingot"', ''},
606 {'CraftItem "steel_ingot"', 'CraftItem "apple"', 'CraftItem "steel_ingot"'},
607 {'', 'CraftItem "steel_ingot"', ''},
611 minetest.register_craft({
612 output = 'NodeItem "TNT" 4',
614 {'NodeItem "wood" 1'},
615 {'CraftItem "lump_of_coal" 1'},
616 {'NodeItem "wood" 1'}
621 -- Some common functions
624 function nodeupdate_single(p)
625 n = minetest.env:get_node(p)
626 if n.name == "sand" or n.name == "gravel" then
627 p_bottom = {x=p.x, y=p.y-1, z=p.z}
628 n_bottom = minetest.env:get_node(p_bottom)
629 if n_bottom.name == "air" then
630 minetest.env:remove_node(p)
631 minetest.env:add_luaentity(p, "falling_"..n.name)
637 function nodeupdate(p)
641 p2 = {x=p.x+x, y=p.y+y, z=p.z+z}
642 nodeupdate_single(p2)
649 -- TNT (not functional)
654 physical = true, -- Collides with things
656 collisionbox = {-0.5,-0.5,-0.5, 0.5,0.5,0.5},
658 textures = {"tnt_top.png","tnt_bottom.png","tnt_side.png","tnt_side.png","tnt_side.png","tnt_side.png"},
659 --visual = "single_sprite",
660 --textures = {"mese.png^[forcesingle"},
661 -- Initial value for our timer
663 -- Number of punches required to defuse
667 -- Called when a TNT object is created
668 function TNT:on_activate(staticdata)
669 print("TNT:on_activate()")
670 self.object:setvelocity({x=0, y=2, z=0})
671 self.object:setacceleration({x=0, y=-10, z=0})
674 -- Called periodically
675 function TNT:on_step(dtime)
676 --print("TNT:on_step()")
679 -- Called when object is punched
680 function TNT:on_punch(hitter)
681 print("TNT:on_punch()")
682 self.health = self.health - 1
683 if self.health <= 0 then
685 hitter:add_to_inventory("NodeItem TNT 1")
689 -- Called when object is right-clicked
690 function TNT:on_rightclick(clicker)
691 --pos = self.object:getpos()
692 --pos = {x=pos.x, y=pos.y+0.1, z=pos.z}
693 --self.object:moveto(pos, false)
696 print("TNT dump: "..dump(TNT))
698 print("Registering TNT");
699 minetest.register_entity("TNT", TNT)
705 function register_falling_node(nodename, texture)
706 minetest.register_entity("falling_"..nodename, {
709 collisionbox = {-0.5,-0.5,-0.5, 0.5,0.5,0.5},
711 textures = {texture,texture,texture,texture,texture,texture},
714 on_step = function(self, dtime)
716 self.object:setacceleration({x=0, y=-10, z=0})
717 -- Turn to actual sand when collides to ground or just move
718 pos = self.object:getpos()
719 bcp = {x=pos.x, y=pos.y-0.6, z=pos.z} -- Position of bottom center point
720 bcn = minetest.env:get_node(bcp)
721 if bcn.name ~= "air" then
722 -- Turn to a sand node
723 np = {x=bcp.x, y=bcp.y+1, z=bcp.z}
724 minetest.env:add_node(np, {name=nodename})
733 register_falling_node("sand", "sand.png")
734 register_falling_node("gravel", "gravel.png")
737 minetest.register_entity("falling_sand", {
739 collisionbox = {-0.5,-0.5,-0.5, 0.5,0.5,0.5},
741 textures = {"sand.png","sand.png","sand.png","sand.png","sand.png","sand.png"},
745 on_step = function(self, dtime)
747 self.fallspeed = self.fallspeed + dtime * 5
748 fp = self.object:getpos()
749 fp.y = fp.y - self.fallspeed * dtime
750 self.object:moveto(fp)
751 -- Turn to actual sand when collides to ground or just move
752 bcp = {x=fp.x, y=fp.y-0.5, z=fp.z} -- Position of bottom center point
753 bcn = minetest.env:get_node(bcp)
754 if bcn.name ~= "air" then
755 -- Turn to a sand node
756 np = {x=bcp.x, y=bcp.y+1, z=bcp.z}
757 minetest.env:add_node(np, {name="sand"})
770 -- Global environment step function
771 function on_step(dtime)
774 minetest.register_globalstep(on_step)
776 function on_placenode(p, node)
777 print("on_placenode")
780 minetest.register_on_placenode(on_placenode)
782 function on_dignode(p, node)
786 minetest.register_on_dignode(on_dignode)
788 function on_punchnode(p, node)
789 print("on_punchnode")
790 if node.name == "TNT" then
791 minetest.env:remove_node(p)
792 minetest.env:add_luaentity(p, "TNT")
796 minetest.register_on_punchnode(on_punchnode)
799 -- Done, print some random stuff
802 print("minetest.registered_entities:")
803 dump2(minetest.registered_entities)
806 -- Some random pre-implementation planning and drafting
810 function TNT:on_rightclick(clicker)
811 print("TNT:on_rightclick()")
812 print("self: "..dump(self))
813 print("getmetatable(self): "..dump(getmetatable(self)))
814 print("getmetatable(getmetatable(self)): "..dump(getmetatable(getmetatable(self))))
815 pos = self.object:getpos()
816 print("TNT:on_rightclick(): object position: "..dump(pos))
817 pos = {x=pos.x+0.5+1, y=pos.y+0.5, z=pos.z+0.5}
818 --minetest.env:add_node(pos, {name="stone")
825 textures = "stone.png",
827 get_dig_duration = function(env, pos, digger)
828 -- Check stuff like digger.current_tool
831 on_dig = function(env, pos, digger)
833 digger.inventory.put("MaterialItem2 0");
838 textures = {"grass.png","mud.png","mud_grass_side.png","mud_grass_side.png","mud_grass_side.png","mud_grass_side.png"},
840 get_dig_duration = function(env, pos, digger)
841 -- Check stuff like digger.current_tool
844 on_dig = function(env, pos, digger)
846 digger.inventory.put("MaterialItem2 1");
850 -- Consider the "miscellaneous block namespace" to be 0xc00...0xfff = 3072...4095
851 register_block(3072, {
852 textures = {"tnt_top.png","tnt_bottom.png","tnt_side.png","tnt_side.png","tnt_side.png","tnt_side.png"},
854 get_dig_duration = function(env, pos, digger)
858 -- on_dig = function(env, pos, digger) end, -- Not implemented
859 on_hit = function(env, pos, hitter)
860 -- Replace with TNT object, which will explode after timer, follow gravity, blink and stuff
861 env:add_object("tnt", pos)