Add junglewood stairs and 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, sounds)
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                 sounds = sounds,
17                 node_box = {
18                         type = "fixed",
19                         fixed = {
20                                 {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
21                                 {-0.5, 0, 0, 0.5, 0.5, 0.5},
22                         },
23                 },
24                 selection_box = {
25                         type = "fixed",
26                         fixed = {
27                                 {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
28                                 {-0.5, 0, 0, 0.5, 0.5, 0.5},
29                         },
30                 },
31                 on_place = function(itemstack, placer, pointed_thing)
32                         if pointed_thing.type ~= "node" then
33                                 return itemstack
34                         end
35                         
36                         local p0 = pointed_thing.under
37                         local p1 = pointed_thing.above
38                         if p0.y-1 == p1.y then
39                                 local fakestack = ItemStack("stairs:stair_" .. subname.."upside_down")
40                                 local ret = minetest.item_place(fakestack, placer, pointed_thing)
41                                 if ret:is_empty() then
42                                         itemstack:take_item()
43                                         return itemstack
44                                 end
45                         end
46                         
47                         -- Otherwise place regularly
48                         return minetest.item_place(itemstack, placer, pointed_thing)
49                 end,
50         })
51         
52         minetest.register_node(":stairs:stair_" .. subname.."upside_down", {
53                 drop = "stairs:stair_" .. subname,
54                 drawtype = "nodebox",
55                 tiles = images,
56                 paramtype = "light",
57                 paramtype2 = "facedir",
58                 is_ground_content = true,
59                 groups = groups,
60                 sounds = sounds,
61                 node_box = {
62                         type = "fixed",
63                         fixed = {
64                                 {-0.5, 0, -0.5, 0.5, 0.5, 0.5},
65                                 {-0.5, -0.5, 0, 0.5, 0, 0.5},
66                         },
67                 },
68                 selection_box = {
69                         type = "fixed",
70                         fixed = {
71                                 {-0.5, 0, -0.5, 0.5, 0.5, 0.5},
72                                 {-0.5, -0.5, 0, 0.5, 0, 0.5},
73                         },
74                 },
75         })
76
77         minetest.register_craft({
78                 output = 'stairs:stair_' .. subname .. ' 4',
79                 recipe = {
80                         {recipeitem, "", ""},
81                         {recipeitem, recipeitem, ""},
82                         {recipeitem, recipeitem, recipeitem},
83                 },
84         })
85
86         -- Flipped recipe for the silly minecrafters
87         minetest.register_craft({
88                 output = 'stairs:stair_' .. subname .. ' 4',
89                 recipe = {
90                         {"", "", recipeitem},
91                         {"", recipeitem, recipeitem},
92                         {recipeitem, recipeitem, recipeitem},
93                 },
94         })
95 end
96
97 -- Node will be called stairs:slab_<subname>
98 function stairs.register_slab(subname, recipeitem, groups, images, description, sounds)
99         minetest.register_node(":stairs:slab_" .. subname, {
100                 description = description,
101                 drawtype = "nodebox",
102                 tiles = images,
103                 paramtype = "light",
104                 is_ground_content = true,
105                 groups = groups,
106                 sounds = sounds,
107                 node_box = {
108                         type = "fixed",
109                         fixed = {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
110                 },
111                 selection_box = {
112                         type = "fixed",
113                         fixed = {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
114                 },
115                 on_place = function(itemstack, placer, pointed_thing)
116                         if pointed_thing.type ~= "node" then
117                                 return itemstack
118                         end
119
120                         -- If it's being placed on an another similar one, replace it with
121                         -- a full block
122                         local slabpos = nil
123                         local slabnode = nil
124                         local p0 = pointed_thing.under
125                         local p1 = pointed_thing.above
126                         local n0 = minetest.env:get_node(p0)
127                         if n0.name == "stairs:slab_" .. subname and
128                                         p0.y+1 == p1.y then
129                                 slabpos = p0
130                                 slabnode = n0
131                         end
132                         if slabpos then
133                                 -- Remove the slab at slabpos
134                                 minetest.env:remove_node(slabpos)
135                                 -- Make a fake stack of a single item and try to place it
136                                 local fakestack = ItemStack(recipeitem)
137                                 pointed_thing.above = slabpos
138                                 fakestack = minetest.item_place(fakestack, placer, pointed_thing)
139                                 -- If the item was taken from the fake stack, decrement original
140                                 if not fakestack or fakestack:is_empty() then
141                                         itemstack:take_item(1)
142                                 -- Else put old node back
143                                 else
144                                         minetest.env:set_node(slabpos, slabnode)
145                                 end
146                                 return itemstack
147                         end
148                         
149                         -- Upside down slabs
150                         if p0.y-1 == p1.y then
151                                 -- Turn into full block if pointing at a existing slab
152                                 if n0.name == "stairs:slab_" .. subname.."upside_down" then
153                                         -- Remove the slab at the position of the slab
154                                         minetest.env:remove_node(p0)
155                                         -- Make a fake stack of a single item and try to place it
156                                         local fakestack = ItemStack(recipeitem)
157                                         pointed_thing.above = p0
158                                         fakestack = minetest.item_place(fakestack, placer, pointed_thing)
159                                         -- If the item was taken from the fake stack, decrement original
160                                         if not fakestack or fakestack:is_empty() then
161                                                 itemstack:take_item(1)
162                                         -- Else put old node back
163                                         else
164                                                 minetest.env:set_node(p0, n0)
165                                         end
166                                         return itemstack
167                                 end
168                                 
169                                 -- Place upside down slab
170                                 local fakestack = ItemStack("stairs:slab_" .. subname.."upside_down")
171                                 local ret = minetest.item_place(fakestack, placer, pointed_thing)
172                                 if ret:is_empty() then
173                                         itemstack:take_item()
174                                         return itemstack
175                                 end
176                         end
177                         
178                         -- If pointing at the side of a upside down slab
179                         if n0.name == "stairs:slab_" .. subname.."upside_down" and
180                                         p0.y+1 ~= p1.y then
181                                 -- Place upside down slab
182                                 local fakestack = ItemStack("stairs:slab_" .. subname.."upside_down")
183                                 local ret = minetest.item_place(fakestack, placer, pointed_thing)
184                                 if ret:is_empty() then
185                                         itemstack:take_item()
186                                         return itemstack
187                                 end
188                         end
189                         
190                         -- Otherwise place regularly
191                         return minetest.item_place(itemstack, placer, pointed_thing)
192                 end,
193         })
194         
195         minetest.register_node(":stairs:slab_" .. subname.."upside_down", {
196                 drop = "stairs:slab_"..subname,
197                 drawtype = "nodebox",
198                 tiles = images,
199                 paramtype = "light",
200                 is_ground_content = true,
201                 groups = groups,
202                 sounds = sounds,
203                 node_box = {
204                         type = "fixed",
205                         fixed = {-0.5, 0, -0.5, 0.5, 0.5, 0.5},
206                 },
207                 selection_box = {
208                         type = "fixed",
209                         fixed = {-0.5, 0, -0.5, 0.5, 0.5, 0.5},
210                 },
211         })
212
213         minetest.register_craft({
214                 output = 'stairs:slab_' .. subname .. ' 6',
215                 recipe = {
216                         {recipeitem, recipeitem, recipeitem},
217                 },
218         })
219 end
220
221 -- Nodes will be called stairs:{stair,slab}_<subname>
222 function stairs.register_stair_and_slab(subname, recipeitem, groups, images, desc_stair, desc_slab, sounds)
223         stairs.register_stair(subname, recipeitem, groups, images, desc_stair, sounds)
224         stairs.register_slab(subname, recipeitem, groups, images, desc_slab, sounds)
225 end
226
227 stairs.register_stair_and_slab("wood", "default:wood",
228                 {snappy=2,choppy=2,oddly_breakable_by_hand=2},
229                 {"default_wood.png"},
230                 "Wooden stair",
231                 "Wooden slab",
232                 default.node_sound_wood_defaults())
233
234 stairs.register_stair_and_slab("stone", "default:stone",
235                 {cracky=3},
236                 {"default_stone.png"},
237                 "Stone stair",
238                 "Stone slab",
239                 default.node_sound_stone_defaults())
240
241 stairs.register_stair_and_slab("cobble", "default:cobble",
242                 {cracky=3},
243                 {"default_cobble.png"},
244                 "Cobble stair",
245                 "Cobble slab",
246                 default.node_sound_stone_defaults())
247
248 stairs.register_stair_and_slab("brick", "default:brick",
249                 {cracky=3},
250                 {"default_brick.png"},
251                 "Brick stair",
252                 "Brick slab",
253                 default.node_sound_stone_defaults())
254
255 stairs.register_stair_and_slab("sandstone", "default:sandstone",
256                 {crumbly=2,cracky=2},
257                 {"default_sandstone.png"},
258                 "Sandstone stair",
259                 "Sandstone slab",
260                 default.node_sound_stone_defaults())
261
262 stairs.register_stair_and_slab("junglewood", "default:junglewood",
263                 {snappy=2,choppy=2,oddly_breakable_by_hand=2},
264                 {"default_junglewood.png"},
265                 "Junglewood stair",
266                 "Junglewood slab",
267                 default.node_sound_wood_defaults())