Merge pull request #19 from PilzAdam/slabs
[oweals/minetest_game.git] / mods / stairs / init.lua
1 -- Minetest 0.4 mod: stairs
2 -- See README.txt for licensing and other information.
3
4 stairs = {}
5
6 -- Node will be called stairs:stair_<subname>
7 function stairs.register_stair(subname, recipeitem, groups, images, description)
8         minetest.register_node("stairs:stair_" .. subname, {
9                 description = description,
10                 drawtype = "nodebox",
11                 tiles = images,
12                 paramtype = "light",
13                 paramtype2 = "facedir",
14                 is_ground_content = true,
15                 groups = groups,
16                 node_box = {
17                         type = "fixed",
18                         fixed = {
19                                 {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
20                                 {-0.5, 0, 0, 0.5, 0.5, 0.5},
21                         },
22                 },
23         })
24
25         minetest.register_craft({
26                 output = 'stairs:stair_' .. subname .. ' 4',
27                 recipe = {
28                         {recipeitem, "", ""},
29                         {recipeitem, recipeitem, ""},
30                         {recipeitem, recipeitem, recipeitem},
31                 },
32         })
33
34         -- Flipped recipe for the silly minecrafters
35         minetest.register_craft({
36                 output = 'stairs:stair_' .. subname .. ' 4',
37                 recipe = {
38                         {"", "", recipeitem},
39                         {"", recipeitem, recipeitem},
40                         {recipeitem, recipeitem, recipeitem},
41                 },
42         })
43 end
44
45 -- Node will be called stairs:slab_<subname>
46 function stairs.register_slab(subname, recipeitem, groups, images, description)
47         minetest.register_node("stairs:slab_" .. subname, {
48                 description = description,
49                 drawtype = "nodebox",
50                 tiles = images,
51                 paramtype = "light",
52                 is_ground_content = true,
53                 groups = groups,
54                 node_box = {
55                         type = "fixed",
56                         fixed = {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
57                 },
58                 selection_box = {
59                         type = "fixed",
60                         fixed = {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
61                 },
62                 on_place = function(itemstack, placer, pointed_thing)
63                         if pointed_thing.type ~= "node" then
64                                 return itemstack
65                         end
66
67                         -- If it's being placed on an another similar one, replace it with
68                         -- a full block
69                         local slabpos = nil
70                         local slabnode = nil
71                         local p0 = pointed_thing.under
72                         local p1 = pointed_thing.above
73                         local n0 = minetest.env:get_node(p0)
74                         if n0.name == "stairs:slab_" .. subname and
75                                         p0.y+1 == p1.y then
76                                 slabpos = p0
77                                 slabnode = n0
78                         end
79                         if slabpos then
80                                 -- Remove the slab at slabpos
81                                 minetest.env:remove_node(slabpos)
82                                 -- Make a fake stack of a single item and try to place it
83                                 local fakestack = ItemStack(recipeitem)
84                                 pointed_thing.above = slabpos
85                                 fakestack = minetest.item_place(fakestack, placer, pointed_thing)
86                                 -- If the item was taken from the fake stack, decrement original
87                                 if not fakestack or fakestack:is_empty() then
88                                         itemstack:take_item(1)
89                                 -- Else put old node back
90                                 else
91                                         minetest.env:set_node(slabpos, slabnode)
92                                 end
93                                 return itemstack
94                         end
95                         
96                         -- Otherwise place regularly
97                         return minetest.item_place(itemstack, placer, pointed_thing)
98                 end,
99         })
100
101         minetest.register_craft({
102                 output = 'stairs:slab_' .. subname .. ' 3',
103                 recipe = {
104                         {recipeitem, recipeitem, recipeitem},
105                 },
106         })
107 end
108
109 -- Nodes will be called stairs:{stair,slab}_<subname>
110 function stairs.register_stair_and_slab(subname, recipeitem, groups, images, desc_stair, desc_slab)
111         stairs.register_stair(subname, recipeitem, groups, images, desc_stair)
112         stairs.register_slab(subname, recipeitem, groups, images, desc_slab)
113 end
114
115 stairs.register_stair_and_slab("wood", "default:wood",
116                 {snappy=2,choppy=2,oddly_breakable_by_hand=2},
117                 {"default_wood.png"},
118                 "Wooden stair",
119                 "Wooden slab")
120
121 stairs.register_stair_and_slab("stone", "default:stone",
122                 {cracky=3},
123                 {"default_stone.png"},
124                 "Stone stair",
125                 "Stone slab")
126
127 stairs.register_stair_and_slab("cobble", "default:cobble",
128                 {cracky=3},
129                 {"default_cobble.png"},
130                 "Cobble stair",
131                 "Cobble slab")
132
133 stairs.register_stair_and_slab("brick", "default:brick",
134                 {cracky=3},
135                 {"default_brick.png"},
136                 "Brick stair",
137                 "Brick slab")
138
139 stairs.register_stair_and_slab("sandstone", "default:sandstone",
140                 {crumbly=2,cracky=2},
141                 {"default_sandstone.png"},
142                 "Sandstone stair",
143                 "Sandstone slab")