Stairs: Stair recipe returns 8 stairs not 6
[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
5 -- Global namespace for functions
6
7 stairs = {}
8
9
10 -- Register aliases for new pine node names
11
12 minetest.register_alias("stairs:stair_pinewood", "stairs:stair_pine_wood")
13 minetest.register_alias("stairs:slab_pinewood", "stairs:slab_pine_wood")
14
15
16 -- Get setting for replace ABM
17
18 local replace = minetest.setting_getbool("enable_stairs_replace_abm")
19
20
21 -- Register stairs.
22 -- Node will be called stairs:stair_<subname>
23
24 function stairs.register_stair(subname, recipeitem, groups, images, description, sounds)
25         groups.stair = 1
26         minetest.register_node(":stairs:stair_" .. subname, {
27                 description = description,
28                 drawtype = "mesh",
29                 mesh = "stairs_stair.obj",
30                 tiles = images,
31                 paramtype = "light",
32                 paramtype2 = "facedir",
33                 is_ground_content = false,
34                 groups = groups,
35                 sounds = sounds,
36                 selection_box = {
37                         type = "fixed",
38                         fixed = {
39                                 {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
40                                 {-0.5, 0, 0, 0.5, 0.5, 0.5},
41                         },
42                 },
43                 collision_box = {
44                         type = "fixed",
45                         fixed = {
46                                 {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
47                                 {-0.5, 0, 0, 0.5, 0.5, 0.5},
48                         },
49                 },
50                 on_place = function(itemstack, placer, pointed_thing)
51                         if pointed_thing.type ~= "node" then
52                                 return itemstack
53                         end
54
55                         local p0 = pointed_thing.under
56                         local p1 = pointed_thing.above
57                         local param2 = 0
58
59                         local placer_pos = placer:getpos()
60                         if placer_pos then
61                                 local dir = {
62                                         x = p1.x - placer_pos.x,
63                                         y = p1.y - placer_pos.y,
64                                         z = p1.z - placer_pos.z
65                                 }
66                                 param2 = minetest.dir_to_facedir(dir)
67                         end
68
69                         if p0.y - 1 == p1.y then
70                                 param2 = param2 + 20
71                                 if param2 == 21 then
72                                         param2 = 23
73                                 elseif param2 == 23 then
74                                         param2 = 21
75                                 end
76                         end
77
78                         return minetest.item_place(itemstack, placer, pointed_thing, param2)
79                 end,
80         })
81
82         -- for replace ABM
83         if replace then
84                 minetest.register_node(":stairs:stair_" .. subname .. "upside_down", {
85                         replace_name = "stairs:stair_" .. subname,
86                         groups = {slabs_replace = 1},
87                 })
88         end
89
90         if recipeitem then
91                 minetest.register_craft({
92                         output = 'stairs:stair_' .. subname .. ' 8',
93                         recipe = {
94                                 {recipeitem, "", ""},
95                                 {recipeitem, recipeitem, ""},
96                                 {recipeitem, recipeitem, recipeitem},
97                         },
98                 })
99
100                 -- Flipped recipe for the silly minecrafters
101                 minetest.register_craft({
102                         output = 'stairs:stair_' .. subname .. ' 8',
103                         recipe = {
104                                 {"", "", recipeitem},
105                                 {"", recipeitem, recipeitem},
106                                 {recipeitem, recipeitem, recipeitem},
107                         },
108                 })
109         end
110 end
111
112
113 -- Register slabs.
114 -- Node will be called stairs:slab_<subname>
115
116 function stairs.register_slab(subname, recipeitem, groups, images, description, sounds)
117         groups.slab = 1
118         minetest.register_node(":stairs:slab_" .. subname, {
119                 description = description,
120                 drawtype = "nodebox",
121                 tiles = images,
122                 paramtype = "light",
123                 paramtype2 = "facedir",
124                 is_ground_content = false,
125                 groups = groups,
126                 sounds = sounds,
127                 node_box = {
128                         type = "fixed",
129                         fixed = {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
130                 },
131                 on_place = function(itemstack, placer, pointed_thing)
132                         if pointed_thing.type ~= "node" then
133                                 return itemstack
134                         end
135
136                         -- If it's being placed on an another similar one, replace it with
137                         -- a full block
138                         local slabpos = nil
139                         local slabnode = nil
140                         local p0 = pointed_thing.under
141                         local p1 = pointed_thing.above
142                         local n0 = minetest.get_node(p0)
143                         local n1 = minetest.get_node(p1)
144                         local param2 = 0
145
146                         local n0_is_upside_down = (n0.name == "stairs:slab_" .. subname and
147                                         n0.param2 >= 20)
148
149                         if n0.name == "stairs:slab_" .. subname and not n0_is_upside_down and
150                                         p0.y + 1 == p1.y then
151                                 slabpos = p0
152                                 slabnode = n0
153                         elseif n1.name == "stairs:slab_" .. subname then
154                                 slabpos = p1
155                                 slabnode = n1
156                         end
157                         if slabpos then
158                                 -- Remove the slab at slabpos
159                                 minetest.remove_node(slabpos)
160                                 -- Make a fake stack of a single item and try to place it
161                                 local fakestack = ItemStack(recipeitem)
162                                 fakestack:set_count(itemstack:get_count())
163
164                                 pointed_thing.above = slabpos
165                                 local success
166                                 fakestack, success = minetest.item_place(fakestack, placer,
167                                         pointed_thing)
168                                 -- If the item was taken from the fake stack, decrement original
169                                 if success then
170                                         itemstack:set_count(fakestack:get_count())
171                                 -- Else put old node back
172                                 else
173                                         minetest.set_node(slabpos, slabnode)
174                                 end
175                                 return itemstack
176                         end
177                         
178                         -- Upside down slabs
179                         if p0.y - 1 == p1.y then
180                                 -- Turn into full block if pointing at a existing slab
181                                 if n0_is_upside_down  then
182                                         -- Remove the slab at the position of the slab
183                                         minetest.remove_node(p0)
184                                         -- Make a fake stack of a single item and try to place it
185                                         local fakestack = ItemStack(recipeitem)
186                                         fakestack:set_count(itemstack:get_count())
187
188                                         pointed_thing.above = p0
189                                         local success
190                                         fakestack, success = minetest.item_place(fakestack, placer,
191                                                 pointed_thing)
192                                         -- If the item was taken from the fake stack, decrement original
193                                         if success then
194                                                 itemstack:set_count(fakestack:get_count())
195                                         -- Else put old node back
196                                         else
197                                                 minetest.set_node(p0, n0)
198                                         end
199                                         return itemstack
200                                 end
201
202                                 -- Place upside down slab
203                                 param2 = 20
204                         end
205
206                         -- If pointing at the side of a upside down slab
207                         if n0_is_upside_down and p0.y + 1 ~= p1.y then
208                                 param2 = 20
209                         end
210
211                         return minetest.item_place(itemstack, placer, pointed_thing, param2)
212                 end,
213         })
214
215         -- for replace ABM
216         if replace then
217                 minetest.register_node(":stairs:slab_" .. subname .. "upside_down", {
218                         replace_name = "stairs:slab_".. subname,
219                         groups = {slabs_replace = 1},
220                 })
221         end
222
223         if recipeitem then
224                 minetest.register_craft({
225                         output = 'stairs:slab_' .. subname .. ' 6',
226                         recipe = {
227                                 {recipeitem, recipeitem, recipeitem},
228                         },
229                 })
230         end
231 end
232
233
234 -- Optionally replace old "upside_down" nodes with new param2 versions.
235 -- Disabled by default.
236
237 if replace then
238         minetest.register_abm({
239                 nodenames = {"group:slabs_replace"},
240                 interval = 16,
241                 chance = 1,
242                 action = function(pos, node)
243                         node.name = minetest.registered_nodes[node.name].replace_name
244                         node.param2 = node.param2 + 20
245                         if node.param2 == 21 then
246                                 node.param2 = 23
247                         elseif node.param2 == 23 then
248                                 node.param2 = 21
249                         end
250                         minetest.set_node(pos, node)
251                 end,
252         })
253 end
254
255
256 -- Stair/slab registration function.
257 -- Nodes will be called stairs:{stair,slab}_<subname>
258
259 function stairs.register_stair_and_slab(subname, recipeitem, groups, images,
260                 desc_stair, desc_slab, sounds)
261         stairs.register_stair(subname, recipeitem, groups, images, desc_stair, sounds)
262         stairs.register_slab(subname, recipeitem, groups, images, desc_slab, sounds)
263 end
264
265
266 -- Register default stairs and slabs
267
268 stairs.register_stair_and_slab("wood", "default:wood",
269                 {snappy = 2, choppy = 2, oddly_breakable_by_hand = 2, flammable = 3},
270                 {"default_wood.png"},
271                 "Wooden Stair",
272                 "Wooden Slab",
273                 default.node_sound_wood_defaults())
274
275 stairs.register_stair_and_slab("junglewood", "default:junglewood",
276                 {snappy = 2, choppy = 2, oddly_breakable_by_hand = 2, flammable = 3},
277                 {"default_junglewood.png"},
278                 "Junglewood Stair",
279                 "Junglewood Slab",
280                 default.node_sound_wood_defaults())
281
282 stairs.register_stair_and_slab("pine_wood", "default:pine_wood",
283                 {snappy = 2, choppy = 2, oddly_breakable_by_hand = 2, flammable = 3},
284                 {"default_pine_wood.png"},
285                 "Pine Wood Stair",
286                 "Pine Wood Slab",
287                 default.node_sound_wood_defaults())
288
289 stairs.register_stair_and_slab("acacia_wood", "default:acacia_wood",
290                 {snappy = 2, choppy = 2, oddly_breakable_by_hand = 2, flammable = 3},
291                 {"default_acacia_wood.png"},
292                 "Acacia Wood Stair",
293                 "Acacia Wood Slab",
294                 default.node_sound_wood_defaults())
295
296 stairs.register_stair_and_slab("aspen_wood", "default:aspen_wood",
297                 {snappy = 2, choppy = 2, oddly_breakable_by_hand = 2, flammable = 3},
298                 {"default_aspen_wood.png"},
299                 "Aspen Wood Stair",
300                 "Aspen Wood Slab",
301                 default.node_sound_wood_defaults())
302
303 stairs.register_stair_and_slab("stone", "default:stone",
304                 {cracky = 3},
305                 {"default_stone.png"},
306                 "Stone Stair",
307                 "Stone Slab",
308                 default.node_sound_stone_defaults())
309
310 stairs.register_stair_and_slab("cobble", "default:cobble",
311                 {cracky = 3},
312                 {"default_cobble.png"},
313                 "Cobblestone Stair",
314                 "Cobblestone Slab",
315                 default.node_sound_stone_defaults())
316
317 stairs.register_stair_and_slab("mossycobble", nil,
318                 {cracky = 3},
319                 {"default_mossycobble.png"},
320                 "Mossy Cobblestone Stair",
321                 "Mossy Cobblestone Slab",
322                 default.node_sound_stone_defaults())
323
324 stairs.register_stair_and_slab("stonebrick", "default:stonebrick",
325                 {cracky = 3},
326                 {"default_stone_brick.png"},
327                 "Stone Brick Stair",
328                 "Stone Brick Slab",
329                 default.node_sound_stone_defaults())
330
331 stairs.register_stair_and_slab("desert_stone", "default:desert_stone",
332                 {cracky = 3},
333                 {"default_desert_stone.png"},
334                 "Desertstone Stair",
335                 "Desertstone Slab",
336                 default.node_sound_stone_defaults())
337
338 stairs.register_stair_and_slab("desert_cobble", "default:desert_cobble",
339                 {cracky = 3},
340                 {"default_desert_cobble.png"},
341                 "Desert Cobblestone Stair",
342                 "Desert Cobblestone Slab",
343                 default.node_sound_stone_defaults())
344
345 stairs.register_stair_and_slab("desert_stonebrick", "default:desert_stonebrick",
346                 {cracky = 3},
347                 {"default_desert_stone_brick.png"},
348                 "Desert Stone Brick Stair",
349                 "Desert Stone Brick Slab",
350                 default.node_sound_stone_defaults())
351
352 stairs.register_stair_and_slab("sandstone", "default:sandstone",
353                 {crumbly = 1, cracky = 3},
354                 {"default_sandstone.png"},
355                 "Sandstone Stair",
356                 "Sandstone Slab",
357                 default.node_sound_stone_defaults())
358                 
359 stairs.register_stair_and_slab("sandstonebrick", "default:sandstonebrick",
360                 {cracky = 2},
361                 {"default_sandstone_brick.png"},
362                 "Sandstone Brick Stair",
363                 "Sandstone Brick Slab",
364                 default.node_sound_stone_defaults())
365
366 stairs.register_stair_and_slab("obsidian", "default:obsidian",
367                 {cracky = 1, level = 2},
368                 {"default_obsidian.png"},
369                 "Obsidian Stair",
370                 "Obsidian Slab",
371                 default.node_sound_stone_defaults())
372
373 stairs.register_stair_and_slab("obsidianbrick", "default:obsidianbrick",
374                 {cracky = 1, level = 2},
375                 {"default_obsidian_brick.png"},
376                 "Obsidian Brick Stair",
377                 "Obsidian Brick Slab",
378                 default.node_sound_stone_defaults())
379
380 stairs.register_stair_and_slab("brick", "default:brick",
381                 {cracky = 3},
382                 {"default_brick.png"},
383                 "Brick Stair",
384                 "Brick Slab",
385                 default.node_sound_stone_defaults())
386
387 stairs.register_stair_and_slab("straw", "farming:straw",
388                 {snappy = 3, flammable = 4},
389                 {"farming_straw.png"},
390                 "Straw Stair",
391                 "Straw Slab",
392                 default.node_sound_leaves_defaults())
393
394 stairs.register_stair_and_slab("steelblock", "default:steelblock",
395                 {cracky = 1, level = 2},
396                 {"default_steel_block.png"},
397                 "Steel Block Stair",
398                 "Steel Block Slab",
399                 default.node_sound_stone_defaults())
400
401 stairs.register_stair_and_slab("copperblock", "default:copperblock",
402                 {cracky = 1, level = 2},
403                 {"default_copper_block.png"},
404                 "Copper Block Stair",
405                 "Copper Block Slab",
406                 default.node_sound_stone_defaults())
407
408 stairs.register_stair_and_slab("bronzeblock", "default:bronzeblock",
409                 {cracky = 1, level = 2},
410                 {"default_bronze_block.png"},
411                 "Bronze Block Stair",
412                 "Bronze Block Slab",
413                 default.node_sound_stone_defaults())
414
415 stairs.register_stair_and_slab("goldblock", "default:goldblock",
416                 {cracky = 1},
417                 {"default_gold_block.png"},
418                 "Gold Block Stair",
419                 "Gold Block Slab",
420                 default.node_sound_stone_defaults())