Fix merging mistake (crash in "stairs")
[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 local function rotate_and_place(itemstack, placer, pointed_thing)
21         local p0 = pointed_thing.under
22         local p1 = pointed_thing.above
23         local param2 = 0
24
25         local placer_pos = placer:getpos()
26         if placer_pos then
27                 param2 = minetest.dir_to_facedir(vector.subtract(p1, placer_pos))
28         end
29
30         local finepos = minetest.pointed_thing_to_face_pos(placer, pointed_thing)
31         local fpos = finepos.y % 1
32
33         if p0.y - 1 == p1.y or (fpos > 0 and fpos < 0.5)
34                         or (fpos < -0.5 and fpos > -0.999999999) then
35                 param2 = param2 + 20
36                 if param2 == 21 then
37                         param2 = 23
38                 elseif param2 == 23 then
39                         param2 = 21
40                 end
41         end
42         return minetest.item_place(itemstack, placer, pointed_thing, param2)
43 end
44
45 -- Register stairs.
46 -- Node will be called stairs:stair_<subname>
47
48 function stairs.register_stair(subname, recipeitem, groups, images, description, sounds)
49         groups.stair = 1
50         minetest.register_node(":stairs:stair_" .. subname, {
51                 description = description,
52                 drawtype = "mesh",
53                 mesh = "stairs_stair.obj",
54                 tiles = images,
55                 paramtype = "light",
56                 paramtype2 = "facedir",
57                 is_ground_content = false,
58                 groups = groups,
59                 sounds = sounds,
60                 selection_box = {
61                         type = "fixed",
62                         fixed = {
63                                 {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
64                                 {-0.5, 0, 0, 0.5, 0.5, 0.5},
65                         },
66                 },
67                 collision_box = {
68                         type = "fixed",
69                         fixed = {
70                                 {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
71                                 {-0.5, 0, 0, 0.5, 0.5, 0.5},
72                         },
73                 },
74                 on_place = function(itemstack, placer, pointed_thing)
75                         if pointed_thing.type ~= "node" then
76                                 return itemstack
77                         end
78
79                         return rotate_and_place(itemstack, placer, pointed_thing)
80                 end,
81         })
82
83         -- for replace ABM
84         if replace then
85                 minetest.register_node(":stairs:stair_" .. subname .. "upside_down", {
86                         replace_name = "stairs:stair_" .. subname,
87                         groups = {slabs_replace = 1},
88                 })
89         end
90
91         if recipeitem then
92                 minetest.register_craft({
93                         output = 'stairs:stair_' .. subname .. ' 8',
94                         recipe = {
95                                 {recipeitem, "", ""},
96                                 {recipeitem, recipeitem, ""},
97                                 {recipeitem, recipeitem, recipeitem},
98                         },
99                 })
100
101                 -- Flipped recipe for the silly minecrafters
102                 minetest.register_craft({
103                         output = 'stairs:stair_' .. subname .. ' 8',
104                         recipe = {
105                                 {"", "", recipeitem},
106                                 {"", recipeitem, recipeitem},
107                                 {recipeitem, recipeitem, recipeitem},
108                         },
109                 })
110
111                 -- Fuel
112                 local baseburntime = minetest.get_craft_result({
113                         method = "fuel",
114                         width = 1,
115                         items = {recipeitem}
116                 }).time
117                 if baseburntime > 0 then
118                         minetest.register_craft({
119                                 type = "fuel",
120                                 recipe = 'stairs:stair_' .. subname,
121                                 burntime = math.floor(baseburntime * 0.75),
122                         })
123                 end
124         end
125 end
126
127
128 -- Slab facedir to placement 6d matching table
129 local slab_trans_dir = {[0] = 8, 0, 2, 1, 3, 4}
130
131 -- Register slabs.
132 -- Node will be called stairs:slab_<subname>
133
134 function stairs.register_slab(subname, recipeitem, groups, images, description, sounds)
135         groups.slab = 1
136         minetest.register_node(":stairs:slab_" .. subname, {
137                 description = description,
138                 drawtype = "nodebox",
139                 tiles = images,
140                 paramtype = "light",
141                 paramtype2 = "facedir",
142                 is_ground_content = false,
143                 groups = groups,
144                 sounds = sounds,
145                 node_box = {
146                         type = "fixed",
147                         fixed = {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
148                 },
149                 on_place = function(itemstack, placer, pointed_thing)
150                         local under = minetest.get_node(pointed_thing.under)
151                         local wield_item = itemstack:get_name()
152                         local creative_enabled = (creative and creative.is_enabled_for
153                                         and creative.is_enabled_for(placer:get_player_name()))
154
155                         if under and under.name:find("stairs:slab_") then
156                                 -- place slab using under node orientation
157                                 local dir = minetest.dir_to_facedir(vector.subtract(
158                                         pointed_thing.above, pointed_thing.under), true)
159
160                                 local p2 = under.param2
161
162                                 -- combine two slabs if possible
163                                 if slab_trans_dir[math.floor(p2 / 4)] == dir
164                                                 and wield_item == under.name then
165
166                                         if not recipeitem then
167                                                 return itemstack
168                                         end
169                                         local player_name = placer:get_player_name()
170                                         if minetest.is_protected(pointed_thing.under, player_name) and not
171                                                         minetest.check_player_privs(placer, "protection_bypass") then
172                                                 minetest.record_protection_violation(pointed_thing.under,
173                                                         player_name)
174                                                 return
175                                         end
176                                         minetest.set_node(pointed_thing.under, {name = recipeitem, param2 = p2})
177                                         if not creative_enabled then
178                                                 itemstack:take_item()
179                                         end
180                                         return itemstack
181                                 end
182
183                                 -- Placing a slab on an upside down slab should make it right-side up.
184                                 if p2 >= 20 and dir == 8 then
185                                         p2 = p2 - 20
186                                 -- same for the opposite case: slab below normal slab
187                                 elseif p2 <= 3 and dir == 4 then
188                                         p2 = p2 + 20
189                                 end
190
191                                 -- else attempt to place node with proper param2
192                                 minetest.item_place_node(ItemStack(wield_item), placer, pointed_thing, p2)
193                                 if not creative_enabled then
194                                         itemstack:take_item()
195                                 end
196                                 return itemstack
197                         else
198                                 return rotate_and_place(itemstack, placer, pointed_thing)
199                         end
200                 end,
201         })
202
203         -- for replace ABM
204         if replace then
205                 minetest.register_node(":stairs:slab_" .. subname .. "upside_down", {
206                         replace_name = "stairs:slab_".. subname,
207                         groups = {slabs_replace = 1},
208                 })
209         end
210
211         if recipeitem then
212                 minetest.register_craft({
213                         output = 'stairs:slab_' .. subname .. ' 6',
214                         recipe = {
215                                 {recipeitem, recipeitem, recipeitem},
216                         },
217                 })
218
219                 -- Fuel
220                 local baseburntime = minetest.get_craft_result({
221                         method = "fuel",
222                         width = 1,
223                         items = {recipeitem}
224                 }).time
225                 if baseburntime > 0 then
226                         minetest.register_craft({
227                                 type = "fuel",
228                                 recipe = 'stairs:slab_' .. subname,
229                                 burntime = math.floor(baseburntime * 0.5),
230                         })
231                 end
232         end
233 end
234
235
236 -- Optionally replace old "upside_down" nodes with new param2 versions.
237 -- Disabled by default.
238
239 if replace then
240         minetest.register_abm({
241                 label = "Slab replace",
242                 nodenames = {"group:slabs_replace"},
243                 interval = 16,
244                 chance = 1,
245                 action = function(pos, node)
246                         node.name = minetest.registered_nodes[node.name].replace_name
247                         node.param2 = node.param2 + 20
248                         if node.param2 == 21 then
249                                 node.param2 = 23
250                         elseif node.param2 == 23 then
251                                 node.param2 = 21
252                         end
253                         minetest.set_node(pos, node)
254                 end,
255         })
256 end
257
258
259 -- Stair/slab registration function.
260 -- Nodes will be called stairs:{stair,slab}_<subname>
261
262 function stairs.register_stair_and_slab(subname, recipeitem,
263                 groups, images, desc_stair, desc_slab, sounds)
264         stairs.register_stair(subname, recipeitem, groups, images, desc_stair, sounds)
265         stairs.register_slab(subname, recipeitem, groups, images, desc_slab, sounds)
266 end
267
268
269 -- Register default stairs and slabs
270
271 stairs.register_stair_and_slab(
272         "wood",
273         "default:wood",
274         {choppy = 2, oddly_breakable_by_hand = 2, flammable = 2},
275         {"default_wood.png"},
276         "Wooden Stair",
277         "Wooden Slab",
278         default.node_sound_wood_defaults()
279 )
280
281 stairs.register_stair_and_slab(
282         "junglewood",
283         "default:junglewood",
284         {choppy = 2, oddly_breakable_by_hand = 2, flammable = 2},
285         {"default_junglewood.png"},
286         "Jungle Wood Stair",
287         "Jungle Wood Slab",
288         default.node_sound_wood_defaults()
289 )
290
291 stairs.register_stair_and_slab(
292         "pine_wood",
293         "default:pine_wood",
294         {choppy = 3, oddly_breakable_by_hand = 2, flammable = 3},
295         {"default_pine_wood.png"},
296         "Pine Wood Stair",
297         "Pine Wood Slab",
298         default.node_sound_wood_defaults()
299 )
300
301 stairs.register_stair_and_slab(
302         "acacia_wood",
303         "default:acacia_wood",
304         {choppy = 2, oddly_breakable_by_hand = 2, flammable = 2},
305         {"default_acacia_wood.png"},
306         "Acacia Wood Stair",
307         "Acacia Wood Slab",
308         default.node_sound_wood_defaults()
309 )
310
311 stairs.register_stair_and_slab(
312         "aspen_wood",
313         "default:aspen_wood",
314         {choppy = 3, oddly_breakable_by_hand = 2, flammable = 3},
315         {"default_aspen_wood.png"},
316         "Aspen Wood Stair",
317         "Aspen Wood Slab",
318         default.node_sound_wood_defaults()
319 )
320
321 stairs.register_stair_and_slab(
322         "stone",
323         "default:stone",
324         {cracky = 3},
325         {"default_stone.png"},
326         "Stone Stair",
327         "Stone Slab",
328         default.node_sound_stone_defaults()
329 )
330
331 stairs.register_stair_and_slab(
332         "cobble",
333         "default:cobble",
334         {cracky = 3},
335         {"default_cobble.png"},
336         "Cobblestone Stair",
337         "Cobblestone Slab",
338         default.node_sound_stone_defaults()
339 )
340
341 stairs.register_stair_and_slab(
342         "mossycobble",
343         nil,
344         {cracky = 3},
345         {"default_mossycobble.png"},
346         "Mossy Cobblestone Stair",
347         "Mossy Cobblestone Slab",
348         default.node_sound_stone_defaults()
349 )
350
351 stairs.register_stair_and_slab(
352         "stonebrick",
353         "default:stonebrick",
354         {cracky = 2},
355         {"default_stone_brick.png"},
356         "Stone Brick Stair",
357         "Stone Brick Slab",
358         default.node_sound_stone_defaults()
359 )
360
361 stairs.register_stair_and_slab(
362         "stone_block",
363         "default:stone_block",
364         {cracky = 2},
365         {"default_stone_block.png"},
366         "Stone Block Stair",
367         "Stone Block Slab",
368         default.node_sound_stone_defaults()
369 )
370
371 stairs.register_stair_and_slab(
372         "desert_stone",
373         "default:desert_stone",
374         {cracky = 3},
375         {"default_desert_stone.png"},
376         "Desert Stone Stair",
377         "Desert Stone Slab",
378         default.node_sound_stone_defaults()
379 )
380
381 stairs.register_stair_and_slab(
382         "desert_cobble",
383         "default:desert_cobble",
384         {cracky = 3},
385         {"default_desert_cobble.png"},
386         "Desert Cobblestone Stair",
387         "Desert Cobblestone Slab",
388         default.node_sound_stone_defaults()
389 )
390
391 stairs.register_stair_and_slab(
392         "desert_stonebrick",
393         "default:desert_stonebrick",
394         {cracky = 2},
395         {"default_desert_stone_brick.png"},
396         "Desert Stone Brick Stair",
397         "Desert Stone Brick Slab",
398         default.node_sound_stone_defaults()
399 )
400
401 stairs.register_stair_and_slab(
402         "desert_stone_block",
403         "default:desert_stone_block",
404         {cracky = 2},
405         {"default_desert_stone_block.png"},
406         "Desert Stone Block Stair",
407         "Desert Stone Block Slab",
408         default.node_sound_stone_defaults()
409 )
410
411 stairs.register_stair_and_slab(
412         "sandstone",
413         "default:sandstone",
414         {crumbly = 1, cracky = 3},
415         {"default_sandstone.png"},
416         "Sandstone Stair",
417         "Sandstone Slab",
418         default.node_sound_stone_defaults()
419 )
420
421 stairs.register_stair_and_slab(
422         "sandstonebrick",
423         "default:sandstonebrick",
424         {cracky = 2},
425         {"default_sandstone_brick.png"},
426         "Sandstone Brick Stair",
427         "Sandstone Brick Slab",
428         default.node_sound_stone_defaults()
429 )
430
431 stairs.register_stair_and_slab(
432         "sandstone_block",
433         "default:sandstone_block",
434         {cracky = 2},
435         {"default_sandstone_block.png"},
436         "Sandstone Block Stair",
437         "Sandstone Block Slab",
438         default.node_sound_stone_defaults()
439 )
440
441 stairs.register_stair_and_slab(
442         "desert_sandstone",
443         "default:desert_sandstone",
444         {crumbly = 1, cracky = 3},
445         {"default_desert_sandstone.png"},
446         "Desert Sandstone Stair",
447         "Desert Sandstone Slab",
448         default.node_sound_stone_defaults()
449 )
450
451 stairs.register_stair_and_slab(
452         "desert_sandstone_brick",
453         "default:desert_sandstone_brick",
454         {cracky = 2},
455         {"default_desert_sandstone_brick.png"},
456         "Desert Sandstone Brick Stair",
457         "Desert Sandstone Brick Slab",
458         default.node_sound_stone_defaults()
459 )
460
461 stairs.register_stair_and_slab(
462         "desert_sandstone_block",
463         "default:desert_sandstone_block",
464         {cracky = 2},
465         {"default_desert_sandstone_block.png"},
466         "Desert Sandstone Block Stair",
467         "Desert Sandstone Block Slab",
468         default.node_sound_stone_defaults()
469 )
470
471 stairs.register_stair_and_slab(
472         "silver_sandstone",
473         "default:silver_sandstone",
474         {crumbly = 1, cracky = 3},
475         {"default_silver_sandstone.png"},
476         "Silver Sandstone Stair",
477         "Silver Sandstone Slab",
478         default.node_sound_stone_defaults()
479 )
480
481 stairs.register_stair_and_slab(
482         "silver_sandstone_brick",
483         "default:silver_sandstone_brick",
484         {cracky = 2},
485         {"default_silver_sandstone_brick.png"},
486         "Silver Sandstone Brick Stair",
487         "Silver Sandstone Brick Slab",
488         default.node_sound_stone_defaults()
489 )
490
491 stairs.register_stair_and_slab(
492         "silver_sandstone_block",
493         "default:silver_sandstone_block",
494         {cracky = 2},
495         {"default_silver_sandstone_block.png"},
496         "Silver Sandstone Block Stair",
497         "Silver Sandstone Block Slab",
498         default.node_sound_stone_defaults()
499 )
500
501 stairs.register_stair_and_slab(
502         "obsidian",
503         "default:obsidian",
504         {cracky = 1, level = 2},
505         {"default_obsidian.png"},
506         "Obsidian Stair",
507         "Obsidian Slab",
508         default.node_sound_stone_defaults()
509 )
510
511 stairs.register_stair_and_slab(
512         "obsidianbrick",
513         "default:obsidianbrick",
514         {cracky = 1, level = 2},
515         {"default_obsidian_brick.png"},
516         "Obsidian Brick Stair",
517         "Obsidian Brick Slab",
518         default.node_sound_stone_defaults()
519 )
520
521 stairs.register_stair_and_slab(
522         "obsidian_block",
523         "default:obsidian_block",
524         {cracky = 1, level = 2},
525         {"default_obsidian_block.png"},
526         "Obsidian Block Stair",
527         "Obsidian Block Slab",
528         default.node_sound_stone_defaults()
529 )
530
531 stairs.register_stair_and_slab(
532         "brick",
533         "default:brick",
534         {cracky = 3},
535         {"default_brick.png"},
536         "Brick Stair",
537         "Brick Slab",
538         default.node_sound_stone_defaults()
539 )
540
541 stairs.register_stair_and_slab(
542         "straw",
543         "farming:straw",
544         {snappy = 3, flammable = 4},
545         {"farming_straw.png"},
546         "Straw Stair",
547         "Straw Slab",
548         default.node_sound_leaves_defaults()
549 )
550
551 stairs.register_stair_and_slab(
552         "steelblock",
553         "default:steelblock",
554         {cracky = 1, level = 2},
555         {"default_steel_block.png"},
556         "Steel Block Stair",
557         "Steel Block Slab",
558         default.node_sound_metal_defaults()
559 )
560
561 stairs.register_stair_and_slab(
562         "copperblock",
563         "default:copperblock",
564         {cracky = 1, level = 2},
565         {"default_copper_block.png"},
566         "Copper Block Stair",
567         "Copper Block Slab",
568         default.node_sound_metal_defaults()
569 )
570
571 stairs.register_stair_and_slab(
572         "bronzeblock",
573         "default:bronzeblock",
574         {cracky = 1, level = 2},
575         {"default_bronze_block.png"},
576         "Bronze Block Stair",
577         "Bronze Block Slab",
578         default.node_sound_metal_defaults()
579 )
580
581 stairs.register_stair_and_slab(
582         "goldblock",
583         "default:goldblock",
584         {cracky = 1},
585         {"default_gold_block.png"},
586         "Gold Block Stair",
587         "Gold Block Slab",
588         default.node_sound_metal_defaults()
589 )
590
591 stairs.register_stair_and_slab(
592         "ice",
593         "default:ice",
594         {cracky = 3, puts_out_fire = 1, cools_lava = 1},
595         {"default_ice.png"},
596         "Ice Stair",
597         "Ice Slab",
598         default.node_sound_glass_defaults()
599 )
600
601 stairs.register_stair_and_slab(
602         "snowblock",
603         "default:snowblock",
604         {crumbly = 3, puts_out_fire = 1, cools_lava = 1, snowy = 1},
605         {"default_snow.png"},
606         "Snow Block Stair",
607         "Snow Block Slab",
608         default.node_sound_dirt_defaults({
609                 footstep = {name = "default_snow_footstep", gain = 0.15},
610                 dug = {name = "default_snow_footstep", gain = 0.2},
611                 dig = {name = "default_snow_footstep", gain = 0.2}
612         })
613 )