Stairs: Register acacia wood stair and slab
[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 = "mesh",
11                 mesh = "stairs_stair.obj",
12                 tiles = images,
13                 paramtype = "light",
14                 paramtype2 = "facedir",
15                 is_ground_content = false,
16                 groups = groups,
17                 sounds = sounds,
18                 selection_box = {
19                         type = "fixed",
20                         fixed = {
21                                 {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
22                                 {-0.5, 0, 0, 0.5, 0.5, 0.5},
23                         },
24                 },
25                 collision_box = {
26                         type = "fixed",
27                         fixed = {
28                                 {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
29                                 {-0.5, 0, 0, 0.5, 0.5, 0.5},
30                         },
31                 },
32                 on_place = function(itemstack, placer, pointed_thing)
33                         if pointed_thing.type ~= "node" then
34                                 return itemstack
35                         end
36
37                         local p0 = pointed_thing.under
38                         local p1 = pointed_thing.above
39                         local param2 = 0
40
41                         local placer_pos = placer:getpos()
42                         if placer_pos then
43                                 local dir = {
44                                         x = p1.x - placer_pos.x,
45                                         y = p1.y - placer_pos.y,
46                                         z = p1.z - placer_pos.z
47                                 }
48                                 param2 = minetest.dir_to_facedir(dir)
49                         end
50
51                         if p0.y-1 == p1.y then
52                                 param2 = param2 + 20
53                                 if param2 == 21 then
54                                         param2 = 23
55                                 elseif param2 == 23 then
56                                         param2 = 21
57                                 end
58                         end
59
60                         return minetest.item_place(itemstack, placer, pointed_thing, param2)
61                 end,
62         })
63
64         -- for replace ABM
65         minetest.register_node(":stairs:stair_" .. subname.."upside_down", {
66                 replace_name = "stairs:stair_" .. subname,
67                 groups = {slabs_replace=1},
68         })
69
70         minetest.register_craft({
71                 output = 'stairs:stair_' .. subname .. ' 6',
72                 recipe = {
73                         {recipeitem, "", ""},
74                         {recipeitem, recipeitem, ""},
75                         {recipeitem, recipeitem, recipeitem},
76                 },
77         })
78
79         -- Flipped recipe for the silly minecrafters
80         minetest.register_craft({
81                 output = 'stairs:stair_' .. subname .. ' 6',
82                 recipe = {
83                         {"", "", recipeitem},
84                         {"", recipeitem, recipeitem},
85                         {recipeitem, recipeitem, recipeitem},
86                 },
87         })
88 end
89
90 -- Node will be called stairs:slab_<subname>
91 function stairs.register_slab(subname, recipeitem, groups, images, description, sounds)
92         minetest.register_node(":stairs:slab_" .. subname, {
93                 description = description,
94                 drawtype = "nodebox",
95                 tiles = images,
96                 paramtype = "light",
97                 paramtype2 = "facedir",
98                 is_ground_content = false,
99                 groups = groups,
100                 sounds = sounds,
101                 node_box = {
102                         type = "fixed",
103                         fixed = {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
104                 },
105                 on_place = function(itemstack, placer, pointed_thing)
106                         if pointed_thing.type ~= "node" then
107                                 return itemstack
108                         end
109
110                         -- If it's being placed on an another similar one, replace it with
111                         -- a full block
112                         local slabpos = nil
113                         local slabnode = nil
114                         local p0 = pointed_thing.under
115                         local p1 = pointed_thing.above
116                         local n0 = minetest.get_node(p0)
117                         local n1 = minetest.get_node(p1)
118                         local param2 = 0
119
120                         local n0_is_upside_down = (n0.name == "stairs:slab_" .. subname and
121                                         n0.param2 >= 20)
122
123                         if n0.name == "stairs:slab_" .. subname and not n0_is_upside_down and p0.y+1 == p1.y then
124                                 slabpos = p0
125                                 slabnode = n0
126                         elseif n1.name == "stairs:slab_" .. subname then
127                                 slabpos = p1
128                                 slabnode = n1
129                         end
130                         if slabpos then
131                                 -- Remove the slab at slabpos
132                                 minetest.remove_node(slabpos)
133                                 -- Make a fake stack of a single item and try to place it
134                                 local fakestack = ItemStack(recipeitem)
135                                 fakestack:set_count(itemstack:get_count())
136
137                                 pointed_thing.above = slabpos
138                                 local success
139                                 fakestack, success = minetest.item_place(fakestack, placer, pointed_thing)
140                                 -- If the item was taken from the fake stack, decrement original
141                                 if success then
142                                         itemstack:set_count(fakestack:get_count())
143                                 -- Else put old node back
144                                 else
145                                         minetest.set_node(slabpos, slabnode)
146                                 end
147                                 return itemstack
148                         end
149                         
150                         -- Upside down slabs
151                         if p0.y-1 == p1.y then
152                                 -- Turn into full block if pointing at a existing slab
153                                 if n0_is_upside_down  then
154                                         -- Remove the slab at the position of the slab
155                                         minetest.remove_node(p0)
156                                         -- Make a fake stack of a single item and try to place it
157                                         local fakestack = ItemStack(recipeitem)
158                                         fakestack:set_count(itemstack:get_count())
159
160                                         pointed_thing.above = p0
161                                         local success
162                                         fakestack, success = minetest.item_place(fakestack, placer, pointed_thing)
163                                         -- If the item was taken from the fake stack, decrement original
164                                         if success then
165                                                 itemstack:set_count(fakestack:get_count())
166                                         -- Else put old node back
167                                         else
168                                                 minetest.set_node(p0, n0)
169                                         end
170                                         return itemstack
171                                 end
172
173                                 -- Place upside down slab
174                                 param2 = 20
175                         end
176
177                         -- If pointing at the side of a upside down slab
178                         if n0_is_upside_down and p0.y+1 ~= p1.y then
179                                 param2 = 20
180                         end
181
182                         return minetest.item_place(itemstack, placer, pointed_thing, param2)
183                 end,
184         })
185
186         -- for replace ABM
187         minetest.register_node(":stairs:slab_" .. subname.."upside_down", {
188                 replace_name = "stairs:slab_"..subname,
189                 groups = {slabs_replace=1},
190         })
191
192         minetest.register_craft({
193                 output = 'stairs:slab_' .. subname .. ' 6',
194                 recipe = {
195                         {recipeitem, recipeitem, recipeitem},
196                 },
197         })
198 end
199
200 -- Replace old "upside_down" nodes with new param2 versions
201 minetest.register_abm({
202         nodenames = {"group:slabs_replace"},
203         interval = 1,
204         chance = 1,
205         action = function(pos, node)
206                 node.name = minetest.registered_nodes[node.name].replace_name
207                 node.param2 = node.param2 + 20
208                 if node.param2 == 21 then
209                         node.param2 = 23
210                 elseif node.param2 == 23 then
211                         node.param2 = 21
212                 end
213                 minetest.set_node(pos, node)
214         end,
215 })
216
217 -- Nodes will be called stairs:{stair,slab}_<subname>
218 function stairs.register_stair_and_slab(subname, recipeitem, groups, images, desc_stair, desc_slab, sounds)
219         stairs.register_stair(subname, recipeitem, groups, images, desc_stair, sounds)
220         stairs.register_slab(subname, recipeitem, groups, images, desc_slab, sounds)
221 end
222
223 stairs.register_stair_and_slab("wood", "default:wood",
224                 {snappy = 2, choppy = 2, oddly_breakable_by_hand = 2, flammable = 3},
225                 {"default_wood.png"},
226                 "Wooden Stair",
227                 "Wooden Slab",
228                 default.node_sound_wood_defaults())
229
230 stairs.register_stair_and_slab("junglewood", "default:junglewood",
231                 {snappy = 2, choppy = 2, oddly_breakable_by_hand = 2, flammable = 3},
232                 {"default_junglewood.png"},
233                 "Junglewood Stair",
234                 "Junglewood Slab",
235                 default.node_sound_wood_defaults())
236
237 stairs.register_stair_and_slab("pinewood", "default:pinewood",
238                 {snappy = 2, choppy = 2, oddly_breakable_by_hand = 2, flammable = 3},
239                 {"default_pinewood.png"},
240                 "Pinewood Stair",
241                 "Pinewood Slab",
242                 default.node_sound_wood_defaults())
243
244 stairs.register_stair_and_slab("acacia_wood", "default:acacia_wood",
245                 {snappy = 2, choppy = 2, oddly_breakable_by_hand = 2, flammable = 3},
246                 {"default_acacia_wood.png"},
247                 "Acacia Wood Stair",
248                 "Acacia Wood Slab",
249                 default.node_sound_wood_defaults())
250
251 stairs.register_stair_and_slab("stone", "default:stone",
252                 {cracky = 3},
253                 {"default_stone.png"},
254                 "Stone Stair",
255                 "Stone Slab",
256                 default.node_sound_stone_defaults())
257
258 stairs.register_stair_and_slab("cobble", "default:cobble",
259                 {cracky = 3},
260                 {"default_cobble.png"},
261                 "Cobblestone Stair",
262                 "Cobblestone Slab",
263                 default.node_sound_stone_defaults())
264
265 stairs.register_stair_and_slab("stonebrick", "default:stonebrick",
266                 {cracky = 3},
267                 {"default_stone_brick.png"},
268                 "Stone Brick Stair",
269                 "Stone Brick Slab",
270                 default.node_sound_stone_defaults())
271
272 stairs.register_stair_and_slab("desert_stone", "default:desert_stone",
273                 {cracky = 3},
274                 {"default_desert_stone.png"},
275                 "Desertstone Stair",
276                 "Desertstone Slab",
277                 default.node_sound_stone_defaults())
278
279 stairs.register_stair_and_slab("desert_cobble", "default:desert_cobble",
280                 {cracky = 3},
281                 {"default_desert_cobble.png"},
282                 "Desert Cobblestone Stair",
283                 "Desert Cobblestone Slab",
284                 default.node_sound_stone_defaults())
285
286 stairs.register_stair_and_slab("desert_stonebrick", "default:desert_stonebrick",
287                 {cracky = 3},
288                 {"default_desert_stone_brick.png"},
289                 "Desert Stone Brick Stair",
290                 "Desert Stone Brick Slab",
291                 default.node_sound_stone_defaults())
292
293 stairs.register_stair_and_slab("brick", "default:brick",
294                 {cracky = 3},
295                 {"default_brick.png"},
296                 "Brick Stair",
297                 "Brick Slab",
298                 default.node_sound_stone_defaults())
299
300 stairs.register_stair_and_slab("sandstone", "default:sandstone",
301                 {crumbly = 2, cracky = 2},
302                 {"default_sandstone.png"},
303                 "Sandstone Stair",
304                 "Sandstone Slab",
305                 default.node_sound_stone_defaults())
306                 
307 stairs.register_stair_and_slab("sandstonebrick", "default:sandstonebrick",
308                 {crumbly = 2, cracky = 2},
309                 {"default_sandstone_brick.png"},
310                 "Sandstone Brick Stair",
311                 "Sandstone Brick Slab",
312                 default.node_sound_stone_defaults())
313
314 stairs.register_stair_and_slab("obsidian", "default:obsidian",
315                 {cracky = 1, level = 2},
316                 {"default_obsidian.png"},
317                 "Obsidian Stair",
318                 "Obsidian Slab",
319                 default.node_sound_stone_defaults())
320
321 stairs.register_stair_and_slab("obsidianbrick", "default:obsidianbrick",
322                 {cracky = 1, level = 2},
323                 {"default_obsidian_brick.png"},
324                 "Obsidian Brick Stair",
325                 "Obsidian Brick Slab",
326                 default.node_sound_stone_defaults())