Create the necessary API for /giveme and /give and implement those commands; also...
[oweals/minetest.git] / data / mods / default / init.lua
1 -- Helper functions defined by builtin.lua:
2 -- dump2(obj, name="_", dumped={})
3 -- dump(obj, dumped={})
4 --
5 -- Textures:
6 -- Mods should prefix their textures with modname_, eg. given the mod
7 -- name "foomod", a texture could be called "foomod_superfurnace.png"
8 -- Due to historical reasons, the default mod does not follow this rule.
9 --
10 -- Global functions:
11 -- minetest.register_entity(name, prototype_table)
12 -- minetest.register_tool(name, {lots of stuff})
13 -- minetest.register_node(name, {lots of stuff})
14 -- minetest.register_craftitem(name, {lots of stuff})
15 -- minetest.register_craft({output=item, recipe={...})
16 -- minetest.register_globalstep(func)
17 -- minetest.register_on_placenode(func(pos, newnode, placer))
18 -- minetest.register_on_dignode(func(pos, oldnode, digger))
19 -- minetest.register_on_punchnode(func(pos, node, puncher))
20 -- minetest.register_on_generated(func(minp, maxp))
21 -- minetest.register_on_newplayer(func(ObjectRef))
22 -- minetest.register_on_respawnplayer(func(ObjectRef))
23 -- ^ return true in func to disable regular player placement
24 -- minetest.register_on_chat_message(func(name, message))
25 -- minetest.setting_get(name)
26 -- minetest.setting_getbool(name)
27 -- minetest.chat_send_all(text)
28 -- minetest.chat_send_player(name, text)
29 -- minetest.get_player_privs(name)
30 --
31 -- Global objects:
32 -- minetest.env - environment reference
33 --
34 -- Global tables:
35 -- minetest.registered_nodes
36 -- ^ List of registered node definitions, indexed by name
37 -- minetest.registered_craftitems
38 -- ^ List of registered craft item definitions, indexed by name
39 -- minetest.registered_entities
40 -- ^ List of registered entity prototypes, indexed by name
41 -- minetest.object_refs
42 -- ^ List of object references, indexed by active object id
43 -- minetest.luaentities
44 -- ^ List of lua entities, indexed by active object id
45 --
46 -- EnvRef is basically ServerEnvironment and ServerMap combined.
47 -- EnvRef methods:
48 -- - add_node(pos, node)
49 -- - remove_node(pos)
50 -- - get_node(pos)
51 -- - add_luaentity(pos, name)
52 -- - add_item(pos, itemstring)
53 -- - add_rat(pos)
54 -- - add_firefly(pos)
55 -- - get_meta(pos) -- Get a NodeMetaRef at that position
56 -- - get_player_by_name(name) -- Get an ObjectRef to a player
57 --
58 -- NodeMetaRef
59 -- - get_type()
60 -- - allows_text_input()
61 -- - set_text(text) -- eg. set the text of a sign
62 -- - get_text()
63 -- - get_owner()
64 -- - set_infotext(infotext)
65 -- - inventory_set_list(name, {item1, item2, ...})
66 -- - inventory_get_list(name)
67 -- - set_inventory_draw_spec(string)
68 -- - set_allow_text_input(bool)
69 -- - set_allow_removal(bool)
70 -- - set_enforce_owner(bool)
71 -- - is_inventory_modified()
72 -- - reset_inventory_modified()
73 -- - is_text_modified()
74 -- - reset_text_modified()
75 -- - set_string(name, value)
76 -- - get_string(name)
77 --
78 -- ObjectRef is basically ServerActiveObject.
79 -- ObjectRef methods:
80 -- - remove(): remove object (after returning from Lua)
81 -- - getpos(): returns {x=num, y=num, z=num}
82 -- - setpos(pos); pos={x=num, y=num, z=num}
83 -- - moveto(pos, continuous=false): interpolated move
84 -- - add_to_inventory(itemstring): add an item to object inventory
85 -- - add_to_inventory_later(itemstring): like above, but after callback returns (only allowed for craftitem callbacks)
86 -- - get_hp(): returns number of hitpoints (2 * number of hearts)
87 -- - set_hp(hp): set number of hitpoints (2 * number of hearts)
88 -- - settexturemod(mod)
89 -- - setsprite(p={x=0,y=0}, num_frames=1, framelength=0.2,
90 -- -           select_horiz_by_yawpitch=false)
91 --
92 -- Registered entities:
93 -- - Functions receive a "luaentity" as self:
94 --   - It has the member .object, which is an ObjectRef pointing to the object
95 --   - The original prototype stuff is visible directly via a metatable
96 -- - Callbacks:
97 --   - on_activate(self, staticdata)
98 --   - on_step(self, dtime)
99 --   - on_punch(self, hitter)
100 --   - on_rightclick(self, clicker)
101 --   - get_staticdata(self): return string
102 --
103 -- MapNode representation:
104 -- {name="name", param1=num, param2=num}
105 --
106 -- Position representation:
107 -- {x=num, y=num, z=num}
108 --
109
110 -- print("minetest dump: "..dump(minetest))
111
112 WATER_ALPHA = 160
113 WATER_VISC = 1
114 LAVA_VISC = 7
115 LIGHT_MAX = 14
116
117 --
118 -- Tool definition
119 --
120
121 minetest.register_tool("WPick", {
122         image = "tool_woodpick.png",
123         basetime = 2.0,
124         dt_weight = 0,
125         dt_crackiness = -0.5,
126         dt_crumbliness = 2,
127         dt_cuttability = 0,
128         basedurability = 30,
129         dd_weight = 0,
130         dd_crackiness = 0,
131         dd_crumbliness = 0,
132         dd_cuttability = 0,
133 })
134 minetest.register_tool("STPick", {
135         image = "tool_stonepick.png",
136         basetime = 1.5,
137         dt_weight = 0,
138         dt_crackiness = -0.5,
139         dt_crumbliness = 2,
140         dt_cuttability = 0,
141         basedurability = 100,
142         dd_weight = 0,
143         dd_crackiness = 0,
144         dd_crumbliness = 0,
145         dd_cuttability = 0,
146 })
147 minetest.register_tool("SteelPick", {
148         image = "tool_steelpick.png",
149         basetime = 1.0,
150         dt_weight = 0,
151         dt_crackiness = -0.5,
152         dt_crumbliness = 2,
153         dt_cuttability = 0,
154         basedurability = 333,
155         dd_weight = 0,
156         dd_crackiness = 0,
157         dd_crumbliness = 0,
158         dd_cuttability = 0,
159 })
160 minetest.register_tool("MesePick", {
161         image = "tool_mesepick.png",
162         basetime = 0,
163         dt_weight = 0,
164         dt_crackiness = 0,
165         dt_crumbliness = 0,
166         dt_cuttability = 0,
167         basedurability = 1337,
168         dd_weight = 0,
169         dd_crackiness = 0,
170         dd_crumbliness = 0,
171         dd_cuttability = 0,
172 })
173 minetest.register_tool("WShovel", {
174         image = "tool_woodshovel.png",
175         basetime = 2.0,
176         dt_weight = 0.5,
177         dt_crackiness = 2,
178         dt_crumbliness = -1.5,
179         dt_cuttability = 0.3,
180         basedurability = 30,
181         dd_weight = 0,
182         dd_crackiness = 0,
183         dd_crumbliness = 0,
184         dd_cuttability = 0,
185 })
186 minetest.register_tool("STShovel", {
187         image = "tool_stoneshovel.png",
188         basetime = 1.5,
189         dt_weight = 0.5,
190         dt_crackiness = 2,
191         dt_crumbliness = -1.5,
192         dt_cuttability = 0.1,
193         basedurability = 100,
194         dd_weight = 0,
195         dd_crackiness = 0,
196         dd_crumbliness = 0,
197         dd_cuttability = 0,
198 })
199 minetest.register_tool("SteelShovel", {
200         image = "tool_steelshovel.png",
201         basetime = 1.0,
202         dt_weight = 0.5,
203         dt_crackiness = 2,
204         dt_crumbliness = -1.5,
205         dt_cuttability = 0.0,
206         basedurability = 330,
207         dd_weight = 0,
208         dd_crackiness = 0,
209         dd_crumbliness = 0,
210         dd_cuttability = 0,
211 })
212 minetest.register_tool("WAxe", {
213         image = "tool_woodaxe.png",
214         basetime = 2.0,
215         dt_weight = 0.5,
216         dt_crackiness = -0.2,
217         dt_crumbliness = 1,
218         dt_cuttability = -0.5,
219         basedurability = 30,
220         dd_weight = 0,
221         dd_crackiness = 0,
222         dd_crumbliness = 0,
223         dd_cuttability = 0,
224 })
225 minetest.register_tool("STAxe", {
226         image = "tool_stoneaxe.png",
227         basetime = 1.5,
228         dt_weight = 0.5,
229         dt_crackiness = -0.2,
230         dt_crumbliness = 1,
231         dt_cuttability = -0.5,
232         basedurability = 100,
233         dd_weight = 0,
234         dd_crackiness = 0,
235         dd_crumbliness = 0,
236         dd_cuttability = 0,
237 })
238 minetest.register_tool("SteelAxe", {
239         image = "tool_steelaxe.png",
240         basetime = 1.0,
241         dt_weight = 0.5,
242         dt_crackiness = -0.2,
243         dt_crumbliness = 1,
244         dt_cuttability = -0.5,
245         basedurability = 330,
246         dd_weight = 0,
247         dd_crackiness = 0,
248         dd_crumbliness = 0,
249         dd_cuttability = 0,
250 })
251 minetest.register_tool("WSword", {
252         image = "tool_woodsword.png",
253         basetime = 3.0,
254         dt_weight = 3,
255         dt_crackiness = 0,
256         dt_crumbliness = 1,
257         dt_cuttability = -1,
258         basedurability = 30,
259         dd_weight = 0,
260         dd_crackiness = 0,
261         dd_crumbliness = 0,
262         dd_cuttability = 0,
263 })
264 minetest.register_tool("STSword", {
265         image = "tool_stonesword.png",
266         basetime = 2.5,
267         dt_weight = 3,
268         dt_crackiness = 0,
269         dt_crumbliness = 1,
270         dt_cuttability = -1,
271         basedurability = 100,
272         dd_weight = 0,
273         dd_crackiness = 0,
274         dd_crumbliness = 0,
275         dd_cuttability = 0,
276 })
277 minetest.register_tool("SteelSword", {
278         image = "tool_steelsword.png",
279         basetime = 2.0,
280         dt_weight = 3,
281         dt_crackiness = 0,
282         dt_crumbliness = 1,
283         dt_cuttability = -1,
284         basedurability = 330,
285         dd_weight = 0,
286         dd_crackiness = 0,
287         dd_crumbliness = 0,
288         dd_cuttability = 0,
289 })
290 -- The hand
291 minetest.register_tool("", {
292         image = "",
293         basetime = 0.5,
294         dt_weight = 1,
295         dt_crackiness = 0,
296         dt_crumbliness = -1,
297         dt_cuttability = 0,
298         basedurability = 50,
299         dd_weight = 0,
300         dd_crackiness = 0,
301         dd_crumbliness = 0,
302         dd_cuttability = 0,
303 })
304
305 --
306 -- Crafting definition
307 --
308
309 minetest.register_craft({
310         output = 'NodeItem "wood" 4',
311         recipe = {
312                 {'NodeItem "tree"'},
313         }
314 })
315
316 minetest.register_craft({
317         output = 'CraftItem "Stick" 4',
318         recipe = {
319                 {'NodeItem "wood"'},
320         }
321 })
322
323 minetest.register_craft({
324         output = 'NodeItem "wooden_fence" 2',
325         recipe = {
326                 {'CraftItem "Stick"', 'CraftItem "Stick"', 'CraftItem "Stick"'},
327                 {'CraftItem "Stick"', 'CraftItem "Stick"', 'CraftItem "Stick"'},
328         }
329 })
330
331 minetest.register_craft({
332         output = 'NodeItem "sign_wall" 1',
333         recipe = {
334                 {'NodeItem "wood"', 'NodeItem "wood"', 'NodeItem "wood"'},
335                 {'NodeItem "wood"', 'NodeItem "wood"', 'NodeItem "wood"'},
336                 {'', 'CraftItem "Stick"', ''},
337         }
338 })
339
340 minetest.register_craft({
341         output = 'NodeItem "torch" 4',
342         recipe = {
343                 {'CraftItem "lump_of_coal"'},
344                 {'CraftItem "Stick"'},
345         }
346 })
347
348 minetest.register_craft({
349         output = 'ToolItem "WPick"',
350         recipe = {
351                 {'NodeItem "wood"', 'NodeItem "wood"', 'NodeItem "wood"'},
352                 {'', 'CraftItem "Stick"', ''},
353                 {'', 'CraftItem "Stick"', ''},
354         }
355 })
356
357 minetest.register_craft({
358         output = 'ToolItem "STPick"',
359         recipe = {
360                 {'NodeItem "cobble"', 'NodeItem "cobble"', 'NodeItem "cobble"'},
361                 {'', 'CraftItem "Stick"', ''},
362                 {'', 'CraftItem "Stick"', ''},
363         }
364 })
365
366 minetest.register_craft({
367         output = 'ToolItem "SteelPick"',
368         recipe = {
369                 {'CraftItem "steel_ingot"', 'CraftItem "steel_ingot"', 'CraftItem "steel_ingot"'},
370                 {'', 'CraftItem "Stick"', ''},
371                 {'', 'CraftItem "Stick"', ''},
372         }
373 })
374
375 minetest.register_craft({
376         output = 'ToolItem "MesePick"',
377         recipe = {
378                 {'NodeItem "mese"', 'NodeItem "mese"', 'NodeItem "mese"'},
379                 {'', 'CraftItem "Stick"', ''},
380                 {'', 'CraftItem "Stick"', ''},
381         }
382 })
383
384 minetest.register_craft({
385         output = 'ToolItem "WShovel"',
386         recipe = {
387                 {'NodeItem "wood"'},
388                 {'CraftItem "Stick"'},
389                 {'CraftItem "Stick"'},
390         }
391 })
392
393 minetest.register_craft({
394         output = 'ToolItem "STShovel"',
395         recipe = {
396                 {'NodeItem "cobble"'},
397                 {'CraftItem "Stick"'},
398                 {'CraftItem "Stick"'},
399         }
400 })
401
402 minetest.register_craft({
403         output = 'ToolItem "SteelShovel"',
404         recipe = {
405                 {'CraftItem "steel_ingot"'},
406                 {'CraftItem "Stick"'},
407                 {'CraftItem "Stick"'},
408         }
409 })
410
411 minetest.register_craft({
412         output = 'ToolItem "WAxe"',
413         recipe = {
414                 {'NodeItem "wood"', 'NodeItem "wood"'},
415                 {'NodeItem "wood"', 'CraftItem "Stick"'},
416                 {'', 'CraftItem "Stick"'},
417         }
418 })
419
420 minetest.register_craft({
421         output = 'ToolItem "STAxe"',
422         recipe = {
423                 {'NodeItem "cobble"', 'NodeItem "cobble"'},
424                 {'NodeItem "cobble"', 'CraftItem "Stick"'},
425                 {'', 'CraftItem "Stick"'},
426         }
427 })
428
429 minetest.register_craft({
430         output = 'ToolItem "SteelAxe"',
431         recipe = {
432                 {'CraftItem "steel_ingot"', 'CraftItem "steel_ingot"'},
433                 {'CraftItem "steel_ingot"', 'CraftItem "Stick"'},
434                 {'', 'CraftItem "Stick"'},
435         }
436 })
437
438 minetest.register_craft({
439         output = 'ToolItem "WSword"',
440         recipe = {
441                 {'NodeItem "wood"'},
442                 {'NodeItem "wood"'},
443                 {'CraftItem "Stick"'},
444         }
445 })
446
447 minetest.register_craft({
448         output = 'ToolItem "STSword"',
449         recipe = {
450                 {'NodeItem "cobble"'},
451                 {'NodeItem "cobble"'},
452                 {'CraftItem "Stick"'},
453         }
454 })
455
456 minetest.register_craft({
457         output = 'ToolItem "SteelSword"',
458         recipe = {
459                 {'CraftItem "steel_ingot"'},
460                 {'CraftItem "steel_ingot"'},
461                 {'CraftItem "Stick"'},
462         }
463 })
464
465 minetest.register_craft({
466         output = 'NodeItem "rail" 15',
467         recipe = {
468                 {'CraftItem "steel_ingot"', '', 'CraftItem "steel_ingot"'},
469                 {'CraftItem "steel_ingot"', 'CraftItem "Stick"', 'CraftItem "steel_ingot"'},
470                 {'CraftItem "steel_ingot"', '', 'CraftItem "steel_ingot"'},
471         }
472 })
473
474 minetest.register_craft({
475         output = 'NodeItem "chest" 1',
476         recipe = {
477                 {'NodeItem "wood"', 'NodeItem "wood"', 'NodeItem "wood"'},
478                 {'NodeItem "wood"', '', 'NodeItem "wood"'},
479                 {'NodeItem "wood"', 'NodeItem "wood"', 'NodeItem "wood"'},
480         }
481 })
482
483 minetest.register_craft({
484         output = 'NodeItem "locked_chest" 1',
485         recipe = {
486                 {'NodeItem "wood"', 'NodeItem "wood"', 'NodeItem "wood"'},
487                 {'NodeItem "wood"', 'CraftItem "steel_ingot"', 'NodeItem "wood"'},
488                 {'NodeItem "wood"', 'NodeItem "wood"', 'NodeItem "wood"'},
489         }
490 })
491
492 minetest.register_craft({
493         output = 'NodeItem "furnace" 1',
494         recipe = {
495                 {'NodeItem "cobble"', 'NodeItem "cobble"', 'NodeItem "cobble"'},
496                 {'NodeItem "cobble"', '', 'NodeItem "cobble"'},
497                 {'NodeItem "cobble"', 'NodeItem "cobble"', 'NodeItem "cobble"'},
498         }
499 })
500
501 minetest.register_craft({
502         output = 'NodeItem "steelblock" 1',
503         recipe = {
504                 {'CraftItem "steel_ingot"', 'CraftItem "steel_ingot"', 'CraftItem "steel_ingot"'},
505                 {'CraftItem "steel_ingot"', 'CraftItem "steel_ingot"', 'CraftItem "steel_ingot"'},
506                 {'CraftItem "steel_ingot"', 'CraftItem "steel_ingot"', 'CraftItem "steel_ingot"'},
507         }
508 })
509
510 minetest.register_craft({
511         output = 'NodeItem "sandstone" 1',
512         recipe = {
513                 {'NodeItem "sand"', 'NodeItem "sand"'},
514                 {'NodeItem "sand"', 'NodeItem "sand"'},
515         }
516 })
517
518 minetest.register_craft({
519         output = 'NodeItem "clay" 1',
520         recipe = {
521                 {'CraftItem "lump_of_clay"', 'CraftItem "lump_of_clay"'},
522                 {'CraftItem "lump_of_clay"', 'CraftItem "lump_of_clay"'},
523         }
524 })
525
526 minetest.register_craft({
527         output = 'NodeItem "brick" 1',
528         recipe = {
529                 {'CraftItem "clay_brick"', 'CraftItem "clay_brick"'},
530                 {'CraftItem "clay_brick"', 'CraftItem "clay_brick"'},
531         }
532 })
533
534 minetest.register_craft({
535         output = 'CraftItem "paper" 1',
536         recipe = {
537                 {'NodeItem "papyrus"', 'NodeItem "papyrus"', 'NodeItem "papyrus"'},
538         }
539 })
540
541 minetest.register_craft({
542         output = 'CraftItem "book" 1',
543         recipe = {
544                 {'CraftItem "paper"'},
545                 {'CraftItem "paper"'},
546                 {'CraftItem "paper"'},
547         }
548 })
549
550 minetest.register_craft({
551         output = 'NodeItem "bookshelf" 1',
552         recipe = {
553                 {'NodeItem "wood"', 'NodeItem "wood"', 'NodeItem "wood"'},
554                 {'CraftItem "book"', 'CraftItem "book"', 'CraftItem "book"'},
555                 {'NodeItem "wood"', 'NodeItem "wood"', 'NodeItem "wood"'},
556         }
557 })
558
559 minetest.register_craft({
560         output = 'NodeItem "ladder" 1',
561         recipe = {
562                 {'CraftItem "Stick"', '', 'CraftItem "Stick"'},
563                 {'CraftItem "Stick"', 'CraftItem "Stick"', 'CraftItem "Stick"'},
564                 {'CraftItem "Stick"', '', 'CraftItem "Stick"'},
565         }
566 })
567
568 minetest.register_craft({
569         output = 'CraftItem "apple_iron" 1',
570         recipe = {
571                 {'', 'CraftItem "steel_ingot"', ''},
572                 {'CraftItem "steel_ingot"', 'CraftItem "apple"', 'CraftItem "steel_ingot"'},
573                 {'', 'CraftItem "steel_ingot"', ''},
574         }
575 })
576
577 --
578 -- Node definitions
579 --
580
581 function digprop_constanttime(time)
582         return {
583                 diggability = "constant",
584                 constant_time = time,
585         }
586 end
587
588 function digprop_stonelike(toughness)
589         return {
590                 diggablity = "normal",
591                 weight = toughness * 5,
592                 crackiness = 1,
593                 crumbliness = -0.1,
594                 cuttability = -0.2,
595         }
596 end
597
598 function digprop_dirtlike(toughness)
599         return {
600                 diggablity = "normal",
601                 weight = toughness * 1.2,
602                 crackiness = 0,
603                 crumbliness = 1.2,
604                 cuttability = -0.4,
605         }
606 end
607
608 function digprop_gravellike(toughness)
609         return {
610                 diggablity = "normal",
611                 weight = toughness * 2,
612                 crackiness = 0.2,
613                 crumbliness = 1.5,
614                 cuttability = -1.0,
615         }
616 end
617
618 function digprop_woodlike(toughness)
619         return {
620                 diggablity = "normal",
621                 weight = toughness * 1.0,
622                 crackiness = 0.75,
623                 crumbliness = -1.0,
624                 cuttability = 1.5,
625         }
626 end
627
628 function digprop_leaveslike(toughness)
629         return {
630                 diggablity = "normal",
631                 weight = toughness * (-0.5),
632                 crackiness = 0,
633                 crumbliness = 0,
634                 cuttability = 2.0,
635         }
636 end
637
638 function digprop_glasslike(toughness)
639         return {
640                 diggablity = "normal",
641                 weight = toughness * 0.1,
642                 crackiness = 2.0,
643                 crumbliness = -1.0,
644                 cuttability = -1.0,
645         }
646 end
647
648 function inventorycube(img1, img2, img3)
649         img2 = img2 or img1
650         img3 = img3 or img1
651         return "[inventorycube"
652                         .. "{" .. img1:gsub("%^", "&")
653                         .. "{" .. img2:gsub("%^", "&")
654                         .. "{" .. img3:gsub("%^", "&")
655 end
656
657 -- Legacy nodes
658
659 minetest.register_node("stone", {
660         tile_images = {"stone.png"},
661         inventory_image = inventorycube("stone.png"),
662         paramtype = "mineral",
663         is_ground_content = true,
664         often_contains_mineral = true, -- Texture atlas hint
665         material = digprop_stonelike(1.0),
666         dug_item = 'NodeItem "cobble" 1',
667 })
668
669 minetest.register_node("dirt_with_grass", {
670         tile_images = {"grass.png", "mud.png", "mud.png^grass_side.png"},
671         inventory_image = inventorycube("mud.png^grass_side.png"),
672         is_ground_content = true,
673         material = digprop_dirtlike(1.0),
674         dug_item = 'NodeItem "dirt" 1',
675 })
676
677 minetest.register_node("dirt_with_grass_footsteps", {
678         tile_images = {"grass_footsteps.png", "mud.png", "mud.png^grass_side.png"},
679         inventory_image = "grass_footsteps.png",
680         is_ground_content = true,
681         material = digprop_dirtlike(1.0),
682         dug_item = 'NodeItem "dirt" 1',
683 })
684
685 minetest.register_node("dirt", {
686         tile_images = {"mud.png"},
687         inventory_image = inventorycube("mud.png"),
688         is_ground_content = true,
689         material = digprop_dirtlike(1.0),
690 })
691
692 minetest.register_node("sand", {
693         tile_images = {"sand.png"},
694         inventory_image = inventorycube("sand.png"),
695         is_ground_content = true,
696         material = digprop_dirtlike(1.0),
697 })
698
699 minetest.register_node("gravel", {
700         tile_images = {"gravel.png"},
701         inventory_image = inventorycube("gravel.png"),
702         is_ground_content = true,
703         material = digprop_gravellike(1.0),
704 })
705
706 minetest.register_node("sandstone", {
707         tile_images = {"sandstone.png"},
708         inventory_image = inventorycube("sandstone.png"),
709         is_ground_content = true,
710         material = digprop_dirtlike(1.0),  -- FIXME should this be stonelike?
711         dug_item = 'NodeItem "sand" 1',  -- FIXME is this intentional?
712 })
713
714 minetest.register_node("clay", {
715         tile_images = {"clay.png"},
716         inventory_image = inventorycube("clay.png"),
717         is_ground_content = true,
718         material = digprop_dirtlike(1.0),
719         dug_item = 'CraftItem "lump_of_clay" 4',
720 })
721
722 minetest.register_node("brick", {
723         tile_images = {"brick.png"},
724         inventory_image = inventorycube("brick.png"),
725         is_ground_content = true,
726         material = digprop_stonelike(1.0),
727         dug_item = 'CraftItem "clay_brick" 4',
728 })
729
730 minetest.register_node("tree", {
731         tile_images = {"tree_top.png", "tree_top.png", "tree.png"},
732         inventory_image = inventorycube("tree_top.png", "tree.png", "tree.png"),
733         is_ground_content = true,
734         material = digprop_woodlike(1.0),
735         cookresult_item = 'CraftItem "lump_of_coal" 1',
736         furnace_burntime = 30,
737 })
738
739 minetest.register_node("jungletree", {
740         tile_images = {"jungletree_top.png", "jungletree_top.png", "jungletree.png"},
741         inventory_image = inventorycube("jungletree_top.png", "jungletree.png", "jungletree.png"),
742         is_ground_content = true,
743         material = digprop_woodlike(1.0),
744         cookresult_item = 'CraftItem "lump_of_coal" 1',
745         furnace_burntime = 30,
746 })
747
748 minetest.register_node("junglegrass", {
749         drawtype = "plantlike",
750         visual_scale = 1.3,
751         tile_images = {"junglegrass.png"},
752         inventory_image = "junglegrass.png",
753         light_propagates = true,
754         paramtype = "light",
755         walkable = false,
756         material = digprop_leaveslike(1.0),
757         furnace_burntime = 2,
758 })
759
760 minetest.register_node("leaves", {
761         drawtype = "allfaces_optional",
762         visual_scale = 1.3,
763         tile_images = {"leaves.png"},
764         inventory_image = "leaves.png",
765         light_propagates = true,
766         paramtype = "light",
767         material = digprop_leaveslike(1.0),
768         extra_dug_item = 'NodeItem "sapling" 1',
769         extra_dug_item_rarity = 20,
770         furnace_burntime = 1,
771 })
772
773 minetest.register_node("cactus", {
774         tile_images = {"cactus_top.png", "cactus_top.png", "cactus_side.png"},
775         inventory_image = inventorycube("cactus_top.png", "cactus_side.png", "cactus_side.png"),
776         is_ground_content = true,
777         material = digprop_woodlike(0.75),
778         furnace_burntime = 15,
779 })
780
781 minetest.register_node("papyrus", {
782         drawtype = "plantlike",
783         tile_images = {"papyrus.png"},
784         inventory_image = "papyrus.png",
785         light_propagates = true,
786         paramtype = "light",
787         is_ground_content = true,
788         walkable = false,
789         material = digprop_leaveslike(0.5),
790         furnace_burntime = 1,
791 })
792
793 minetest.register_node("bookshelf", {
794         tile_images = {"wood.png", "wood.png", "bookshelf.png"},
795         -- FIXME: inventorycube only cares for the first texture
796         --inventory_image = inventorycube("wood.png", "bookshelf.png", "bookshelf.png")
797         inventory_image = inventorycube("bookshelf.png"),
798         is_ground_content = true,
799         material = digprop_woodlike(0.75),
800         furnace_burntime = 30,
801 })
802
803 minetest.register_node("glass", {
804         drawtype = "glasslike",
805         tile_images = {"glass.png"},
806         inventory_image = inventorycube("glass.png"),
807         light_propagates = true,
808         paramtype = "light",
809         sunlight_propagates = true,
810         is_ground_content = true,
811         material = digprop_glasslike(1.0),
812 })
813
814 minetest.register_node("wooden_fence", {
815         drawtype = "fencelike",
816         tile_images = {"wood.png"},
817         inventory_image = "fence.png",
818         light_propagates = true,
819         paramtype = "light",
820         is_ground_content = true,
821         selection_box = {
822                 type = "fixed",
823                 fixed = {-1/7, -1/2, -1/7, 1/7, 1/2, 1/7},
824         },
825         furnace_burntime = 15,
826         material = digprop_woodlike(0.75),
827 })
828
829 minetest.register_node("rail", {
830         drawtype = "raillike",
831         tile_images = {"rail.png", "rail_curved.png", "rail_t_junction.png", "rail_crossing.png"},
832         inventory_image = "rail.png",
833         light_propagates = true,
834         paramtype = "light",
835         is_ground_content = true,
836         walkable = false,
837         selection_box = {
838                 type = "fixed",
839                 --fixed = <default>
840         },
841         material = digprop_dirtlike(0.75),
842 })
843
844 minetest.register_node("ladder", {
845         drawtype = "signlike",
846         tile_images = {"ladder.png"},
847         inventory_image = "ladder.png",
848         light_propagates = true,
849         paramtype = "light",
850         is_ground_content = true,
851         wall_mounted = true,
852         walkable = false,
853         climbable = true,
854         selection_box = {
855                 type = "wallmounted",
856                 --wall_top = = <default>
857                 --wall_bottom = = <default>
858                 --wall_side = = <default>
859         },
860         furnace_burntime = 5,
861         material = digprop_woodlike(0.5),
862 })
863
864 minetest.register_node("coalstone", {
865         tile_images = {"stone.png^mineral_coal.png"},
866         inventory_image = "stone.png^mineral_coal.png",
867         is_ground_content = true,
868         material = digprop_stonelike(1.5),
869 })
870
871 minetest.register_node("wood", {
872         tile_images = {"wood.png"},
873         inventory_image = inventorycube("wood.png"),
874         is_ground_content = true,
875         furnace_burntime = 7,
876         material = digprop_woodlike(0.75),
877 })
878
879 minetest.register_node("mese", {
880         tile_images = {"mese.png"},
881         inventory_image = inventorycube("mese.png"),
882         is_ground_content = true,
883         furnace_burntime = 30,
884         material = digprop_stonelike(0.5),
885 })
886
887 minetest.register_node("cloud", {
888         tile_images = {"cloud.png"},
889         inventory_image = inventorycube("cloud.png"),
890         is_ground_content = true,
891 })
892
893 minetest.register_node("water_flowing", {
894         drawtype = "flowingliquid",
895         tile_images = {"water.png"},
896         alpha = WATER_ALPHA,
897         inventory_image = inventorycube("water.png"),
898         paramtype = "light",
899         light_propagates = true,
900         walkable = false,
901         pointable = false,
902         diggable = false,
903         buildable_to = true,
904         liquidtype = "flowing",
905         liquid_alternative_flowing = "water_flowing",
906         liquid_alternative_source = "water_source",
907         liquid_viscosity = WATER_VISC,
908         post_effect_color = {a=64, r=100, g=100, b=200},
909         special_materials = {
910                 {image="water.png", backface_culling=false},
911                 {image="water.png", backface_culling=true},
912         },
913 })
914
915 minetest.register_node("water_source", {
916         drawtype = "liquid",
917         tile_images = {"water.png"},
918         alpha = WATER_ALPHA,
919         inventory_image = inventorycube("water.png"),
920         paramtype = "light",
921         light_propagates = true,
922         walkable = false,
923         pointable = false,
924         diggable = false,
925         buildable_to = true,
926         liquidtype = "source",
927         liquid_alternative_flowing = "water_flowing",
928         liquid_alternative_source = "water_source",
929         liquid_viscosity = WATER_VISC,
930         post_effect_color = {a=64, r=100, g=100, b=200},
931         special_materials = {
932                 -- New-style water source material (mostly unused)
933                 {image="water.png", backface_culling=false},
934         },
935 })
936
937 minetest.register_node("lava_flowing", {
938         drawtype = "flowingliquid",
939         tile_images = {"lava.png"},
940         inventory_image = inventorycube("lava.png"),
941         paramtype = "light",
942         light_propagates = false,
943         light_source = LIGHT_MAX - 1,
944         walkable = false,
945         pointable = false,
946         diggable = false,
947         buildable_to = true,
948         liquidtype = "flowing",
949         liquid_alternative_flowing = "lava_flowing",
950         liquid_alternative_source = "lava_source",
951         liquid_viscosity = LAVA_VISC,
952         damage_per_second = 4*2,
953         post_effect_color = {a=192, r=255, g=64, b=0},
954         special_materials = {
955                 {image="lava.png", backface_culling=false},
956                 {image="lava.png", backface_culling=true},
957         },
958 })
959
960 minetest.register_node("lava_source", {
961         drawtype = "liquid",
962         tile_images = {"lava.png"},
963         inventory_image = inventorycube("lava.png"),
964         paramtype = "light",
965         light_propagates = false,
966         light_source = LIGHT_MAX - 1,
967         walkable = false,
968         pointable = false,
969         diggable = false,
970         buildable_to = true,
971         liquidtype = "source",
972         liquid_alternative_flowing = "lava_flowing",
973         liquid_alternative_source = "lava_source",
974         liquid_viscosity = LAVA_VISC,
975         damage_per_second = 4*2,
976         post_effect_color = {a=192, r=255, g=64, b=0},
977         special_materials = {
978                 -- New-style lava source material (mostly unused)
979                 {image="lava.png", backface_culling=false},
980         },
981         furnace_burntime = 60,
982 })
983
984 minetest.register_node("torch", {
985         drawtype = "torchlike",
986         tile_images = {"torch_on_floor.png", "torch_on_ceiling.png", "torch.png"},
987         inventory_image = "torch_on_floor.png",
988         paramtype = "light",
989         light_propagates = true,
990         sunlight_propagates = true,
991         walkable = false,
992         wall_mounted = true,
993         light_source = LIGHT_MAX-1,
994         selection_box = {
995                 type = "wallmounted",
996                 wall_top = {-0.1, 0.5-0.6, -0.1, 0.1, 0.5, 0.1},
997                 wall_bottom = {-0.1, -0.5, -0.1, 0.1, -0.5+0.6, 0.1},
998                 wall_side = {-0.5, -0.3, -0.1, -0.5+0.3, 0.3, 0.1},
999         },
1000         material = digprop_constanttime(0.0),
1001         furnace_burntime = 4,
1002 })
1003
1004 minetest.register_node("sign_wall", {
1005         drawtype = "signlike",
1006         tile_images = {"sign_wall.png"},
1007         inventory_image = "sign_wall.png",
1008         paramtype = "light",
1009         light_propagates = true,
1010         sunlight_propagates = true,
1011         walkable = false,
1012         wall_mounted = true,
1013         metadata_name = "sign",
1014         selection_box = {
1015                 type = "wallmounted",
1016                 --wall_top = <default>
1017                 --wall_bottom = <default>
1018                 --wall_side = <default>
1019         },
1020         material = digprop_constanttime(0.5),
1021         furnace_burntime = 10,
1022 })
1023
1024 minetest.register_node("chest", {
1025         tile_images = {"chest_top.png", "chest_top.png", "chest_side.png",
1026                 "chest_side.png", "chest_side.png", "chest_front.png"},
1027         inventory_image = "chest_top.png",
1028         --inventory_image = inventorycube("chest_top.png", "chest_side.png", "chest_front.png"),
1029         paramtype = "facedir_simple",
1030         metadata_name = "chest",
1031         material = digprop_woodlike(1.0),
1032         furnace_burntime = 30,
1033 })
1034
1035 minetest.register_node("locked_chest", {
1036         tile_images = {"chest_top.png", "chest_top.png", "chest_side.png",
1037                 "chest_side.png", "chest_side.png", "chest_lock.png"},
1038         inventory_image = "chest_lock.png",
1039         paramtype = "facedir_simple",
1040         metadata_name = "locked_chest",
1041         material = digprop_woodlike(1.0),
1042         furnace_burntime = 30,
1043 })
1044
1045 minetest.register_node("furnace", {
1046         tile_images = {"furnace_side.png", "furnace_side.png", "furnace_side.png",
1047                 "furnace_side.png", "furnace_side.png", "furnace_front.png"},
1048         inventory_image = "furnace_front.png",
1049         paramtype = "facedir_simple",
1050         metadata_name = "furnace",
1051         material = digprop_stonelike(3.0),
1052 })
1053
1054 minetest.register_node("cobble", {
1055         tile_images = {"cobble.png"},
1056         inventory_image = inventorycube("cobble.png"),
1057         is_ground_content = true,
1058         cookresult_item = 'NodeItem "stone" 1',
1059         material = digprop_stonelike(0.9),
1060 })
1061
1062 minetest.register_node("mossycobble", {
1063         tile_images = {"mossycobble.png"},
1064         inventory_image = inventorycube("mossycobble.png"),
1065         is_ground_content = true,
1066         material = digprop_stonelike(0.8),
1067 })
1068
1069 minetest.register_node("steelblock", {
1070         tile_images = {"steel_block.png"},
1071         inventory_image = inventorycube("steel_block.png"),
1072         is_ground_content = true,
1073         material = digprop_stonelike(5.0),
1074 })
1075
1076 minetest.register_node("nyancat", {
1077         tile_images = {"nc_side.png", "nc_side.png", "nc_side.png",
1078                 "nc_side.png", "nc_back.png", "nc_front.png"},
1079         inventory_image = "nc_front.png",
1080         paramtype = "facedir_simple",
1081         material = digprop_stonelike(3.0),
1082         furnace_burntime = 1,
1083 })
1084
1085 minetest.register_node("nyancat_rainbow", {
1086         tile_images = {"nc_rb.png"},
1087         inventory_image = "nc_rb.png",
1088         material = digprop_stonelike(3.0),
1089         furnace_burntime = 1,
1090 })
1091
1092 minetest.register_node("sapling", {
1093         drawtype = "plantlike",
1094         visual_scale = 1.0,
1095         tile_images = {"sapling.png"},
1096         inventory_image = "sapling.png",
1097         paramtype = "light",
1098         light_propagates = true,
1099         walkable = false,
1100         material = digprop_constanttime(0.0),
1101         furnace_burntime = 10,
1102 })
1103
1104 minetest.register_node("apple", {
1105         drawtype = "plantlike",
1106         visual_scale = 1.0,
1107         tile_images = {"apple.png"},
1108         inventory_image = "apple.png",
1109         paramtype = "light",
1110         light_propagates = true,
1111         sunlight_propagates = true,
1112         walkable = false,
1113         dug_item = 'CraftItem "apple" 1',
1114         material = digprop_constanttime(0.0),
1115         furnace_burntime = 3,
1116 })
1117
1118 --
1119 -- Crafting items
1120 --
1121
1122 local craftitem_place_item = function(item, placer, pos)
1123         --print("craftitem_place_item")
1124         --print("item: " .. dump(item))
1125         --print("placer: " .. dump(placer))
1126         --print("pos: " .. dump(pos))
1127         minetest.env:add_item(pos, 'CraftItem "' .. item .. '" 1')
1128         return true
1129 end
1130
1131 local craftitem_eat = function(hp_change)
1132         return function(item, user, pointed_thing)  -- closure
1133                 --print("craftitem_eat(" .. hp_change .. ")")
1134                 --print("item: " .. dump(item))
1135                 --print("user: " .. dump(user))
1136                 --print("pointed_thing: " .. dump(pointed_thing))
1137                 user:set_hp(user:get_hp() + hp_change)
1138                 return true
1139         end
1140 end
1141
1142 minetest.register_craftitem("Stick", {
1143         image = "stick.png",
1144         --furnace_burntime = ...,
1145         on_place_on_ground = craftitem_place_item,
1146 })
1147
1148 minetest.register_craftitem("paper", {
1149         image = "paper.png",
1150         on_place_on_ground = craftitem_place_item,
1151 })
1152
1153 minetest.register_craftitem("book", {
1154         image = "book.png",
1155         on_place_on_ground = craftitem_place_item,
1156 })
1157
1158 minetest.register_craftitem("lump_of_coal", {
1159         image = "lump_of_coal.png",
1160         furnace_burntime = 40;
1161         on_place_on_ground = craftitem_place_item,
1162 })
1163
1164 minetest.register_craftitem("lump_of_iron", {
1165         image = "lump_of_iron.png",
1166         cookresult_item = 'CraftItem "steel_ingot" 1',
1167         on_place_on_ground = craftitem_place_item,
1168 })
1169
1170 minetest.register_craftitem("lump_of_clay", {
1171         image = "lump_of_clay.png",
1172         cookresult_item = 'CraftItem "clay_brick" 1',
1173         on_place_on_ground = craftitem_place_item,
1174 })
1175
1176 minetest.register_craftitem("steel_ingot", {
1177         image = "steel_ingot.png",
1178         on_place_on_ground = craftitem_place_item,
1179 })
1180
1181 minetest.register_craftitem("clay_brick", {
1182         image = "clay_brick.png",
1183         on_place_on_ground = craftitem_place_item,
1184 })
1185
1186 minetest.register_craftitem("rat", {
1187         image = "rat.png",
1188         cookresult_item = 'CraftItem "cooked_rat" 1',
1189         on_drop = function(item, dropper, pos)
1190                 minetest.env:add_rat(pos)
1191                 return true
1192         end,
1193 })
1194
1195 minetest.register_craftitem("cooked_rat", {
1196         image = "cooked_rat.png",
1197         cookresult_item = 'CraftItem "scorched_stuff" 1',
1198         on_place_on_ground = craftitem_place_item,
1199         on_use = craftitem_eat(6),
1200 })
1201
1202 minetest.register_craftitem("scorched_stuff", {
1203         image = "scorched_stuff.png",
1204         on_place_on_ground = craftitem_place_item,
1205 })
1206
1207 minetest.register_craftitem("firefly", {
1208         image = "firefly.png",
1209         on_drop = function(item, dropper, pos)
1210                 minetest.env:add_firefly(pos)
1211                 return true
1212         end,
1213 })
1214
1215 minetest.register_craftitem("apple", {
1216         image = "apple.png",
1217         on_place_on_ground = craftitem_place_item,
1218         on_use = craftitem_eat(4),
1219 })
1220
1221 minetest.register_craftitem("apple_iron", {
1222         image = "apple_iron.png",
1223         on_place_on_ground = craftitem_place_item,
1224         on_use = craftitem_eat(8),
1225 })
1226
1227 print(dump(minetest.registered_craftitems))
1228
1229
1230 --
1231 -- Some common functions
1232 --
1233
1234 function nodeupdate_single(p)
1235         n = minetest.env:get_node(p)
1236         if n.name == "sand" or n.name == "gravel" then
1237                 p_bottom = {x=p.x, y=p.y-1, z=p.z}
1238                 n_bottom = minetest.env:get_node(p_bottom)
1239                 if n_bottom.name == "air" then
1240                         minetest.env:remove_node(p)
1241                         minetest.env:add_luaentity(p, "falling_"..n.name)
1242                         nodeupdate(p)
1243                 end
1244         end
1245 end
1246
1247 function nodeupdate(p)
1248         for x = -1,1 do
1249         for y = -1,1 do
1250         for z = -1,1 do
1251                 p2 = {x=p.x+x, y=p.y+y, z=p.z+z}
1252                 nodeupdate_single(p2)
1253         end
1254         end
1255         end
1256 end
1257
1258 --
1259 -- Falling stuff
1260 --
1261
1262 function register_falling_node(nodename, texture)
1263         minetest.register_entity("falling_"..nodename, {
1264                 -- Static definition
1265                 physical = true,
1266                 collisionbox = {-0.5,-0.5,-0.5, 0.5,0.5,0.5},
1267                 visual = "cube",
1268                 textures = {texture,texture,texture,texture,texture,texture},
1269                 -- State
1270                 -- Methods
1271                 on_step = function(self, dtime)
1272                         -- Set gravity
1273                         self.object:setacceleration({x=0, y=-10, z=0})
1274                         -- Turn to actual sand when collides to ground or just move
1275                         local pos = self.object:getpos()
1276                         local bcp = {x=pos.x, y=pos.y-0.7, z=pos.z} -- Position of bottom center point
1277                         local bcn = minetest.env:get_node(bcp)
1278                         if bcn.name ~= "air" then
1279                                 -- Turn to a sand node
1280                                 local np = {x=bcp.x, y=bcp.y+1, z=bcp.z}
1281                                 minetest.env:add_node(np, {name=nodename})
1282                                 self.object:remove()
1283                         else
1284                                 -- Do nothing
1285                         end
1286                 end
1287         })
1288 end
1289
1290 register_falling_node("sand", "sand.png")
1291 register_falling_node("gravel", "gravel.png")
1292
1293 --
1294 -- Global callbacks
1295 --
1296
1297 -- Global environment step function
1298 function on_step(dtime)
1299         -- print("on_step")
1300 end
1301 minetest.register_globalstep(on_step)
1302
1303 function on_placenode(p, node)
1304         print("on_placenode")
1305         nodeupdate(p)
1306 end
1307 minetest.register_on_placenode(on_placenode)
1308
1309 function on_dignode(p, node)
1310         print("on_dignode")
1311         nodeupdate(p)
1312 end
1313 minetest.register_on_dignode(on_dignode)
1314
1315 function on_punchnode(p, node)
1316         print("on_punchnode")
1317         if node.name == "TNT" then
1318                 minetest.env:remove_node(p)
1319                 minetest.env:add_luaentity(p, "TNT")
1320                 --minetest.env:add_luaentity(p, "testentity")
1321                 --minetest.env:add_firefly(p)
1322                 nodeupdate(p)
1323         end
1324 end
1325 minetest.register_on_punchnode(on_punchnode)
1326
1327 minetest.register_on_chat_message(function(name, message)
1328         print("default on_chat_message: name="..dump(name).." message="..dump(message))
1329         local cmd = "/giveme"
1330         if message:sub(0, #cmd) == cmd then
1331                 if not minetest.get_player_privs(name)["give"] then
1332                         minetest.chat_send_player(name, "you don't have permission to give")
1333                         return true -- Handled chat message
1334                 end
1335                 stackstring = string.match(message, cmd.." (.*)")
1336                 if stackstring == nil then
1337                         minetest.chat_send_player(name, 'usage: '..cmd..' stackstring')
1338                         return true -- Handled chat message
1339                 end
1340                 print(cmd..' invoked, stackstring="'..stackstring..'"')
1341                 player = minetest.env:get_player_by_name(name)
1342                 added, error_msg = player:add_to_inventory(stackstring)
1343                 if added then
1344                         minetest.chat_send_player(name, '"'..stackstring
1345                                         ..'" added to inventory.');
1346                 else
1347                         minetest.chat_send_player(name, 'Could not give "'..stackstring
1348                                         ..'": '..error_msg);
1349                 end
1350                 return true -- Handled chat message
1351         end
1352         local cmd = "/give"
1353         if message:sub(0, #cmd) == cmd then
1354                 print("minetest.get_player_privs(name)="
1355                                 ..dump(minetest.get_player_privs(name)))
1356                 if not minetest.get_player_privs(name)["give"] then
1357                         minetest.chat_send_player(name, "you don't have permission to give")
1358                         return true -- Handled chat message
1359                 end
1360                 name2, stackstring = string.match(message, cmd.." ([%a%d_-]+) (.*)")
1361                 if name == nil or stackstring == nil then
1362                         minetest.chat_send_player(name, 'usage: '..cmd..' name stackstring')
1363                         return true -- Handled chat message
1364                 end
1365                 print(cmd..' invoked, name2="'..name2
1366                                 ..'" stackstring="'..stackstring..'"')
1367                 player = minetest.env:get_player_by_name(name2)
1368                 if player == nil then
1369                         minetest.chat_send_player(name, name2..' is not a known player')
1370                         return true -- Handled chat message
1371                 end
1372                 added, error_msg = player:add_to_inventory(stackstring)
1373                 if added then
1374                         minetest.chat_send_player(name, '"'..stackstring
1375                                         ..'" added to '..name2..'\'s inventory.');
1376                         minetest.chat_send_player(name2, '"'..stackstring
1377                                         ..'" added to inventory.');
1378                 else
1379                         minetest.chat_send_player(name, 'Could not give "'..stackstring
1380                                         ..'": '..error_msg);
1381                 end
1382                 return true -- Handled chat message
1383         end
1384 end)
1385
1386 --
1387 -- Done, print some random stuff
1388 --
1389
1390 --print("minetest.registered_entities:")
1391 --dump2(minetest.registered_entities)
1392
1393 -- END