8ec5aa7a190b29ac9c7991a4d06d7244c059218c
[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.settings:get_bool("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                 -- Recipe matches appearence in inventory
93                 minetest.register_craft({
94                         output = 'stairs:stair_' .. subname .. ' 8',
95                         recipe = {
96                                 {"", "", recipeitem},
97                                 {"", recipeitem, recipeitem},
98                                 {recipeitem, recipeitem, recipeitem},
99                         },
100                 })
101
102                 -- Use stairs to craft full blocks again (1:1)
103                 minetest.register_craft({
104                         output = recipeitem .. ' 3',
105                         recipe = {
106                                 {'stairs:stair_' .. subname, 'stairs:stair_' .. subname},
107                                 {'stairs:stair_' .. subname, 'stairs:stair_' .. subname},
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                 -- Use 2 slabs to craft a full block again (1:1)
220                 minetest.register_craft({
221                         output = recipeitem,
222                         recipe = {
223                                 {'stairs:slab_' .. subname},
224                                 {'stairs:slab_' .. subname},
225                         },
226                 })
227
228                 -- Fuel
229                 local baseburntime = minetest.get_craft_result({
230                         method = "fuel",
231                         width = 1,
232                         items = {recipeitem}
233                 }).time
234                 if baseburntime > 0 then
235                         minetest.register_craft({
236                                 type = "fuel",
237                                 recipe = 'stairs:slab_' .. subname,
238                                 burntime = math.floor(baseburntime * 0.5),
239                         })
240                 end
241         end
242 end
243
244
245 -- Optionally replace old "upside_down" nodes with new param2 versions.
246 -- Disabled by default.
247
248 if replace then
249         minetest.register_abm({
250                 label = "Slab replace",
251                 nodenames = {"group:slabs_replace"},
252                 interval = 16,
253                 chance = 1,
254                 action = function(pos, node)
255                         node.name = minetest.registered_nodes[node.name].replace_name
256                         node.param2 = node.param2 + 20
257                         if node.param2 == 21 then
258                                 node.param2 = 23
259                         elseif node.param2 == 23 then
260                                 node.param2 = 21
261                         end
262                         minetest.set_node(pos, node)
263                 end,
264         })
265 end
266
267
268 -- Stair/slab registration function.
269 -- Nodes will be called stairs:{stair,slab}_<subname>
270
271 function stairs.register_stair_and_slab(subname, recipeitem,
272                 groups, images, desc_stair, desc_slab, sounds)
273         stairs.register_stair(subname, recipeitem, groups, images, desc_stair, sounds)
274         stairs.register_slab(subname, recipeitem, groups, images, desc_slab, sounds)
275 end
276
277
278 -- Register default stairs and slabs
279
280 stairs.register_stair_and_slab(
281         "wood",
282         "default:wood",
283         {choppy = 2, oddly_breakable_by_hand = 2, flammable = 2},
284         {"default_wood.png"},
285         "Wooden Stair",
286         "Wooden Slab",
287         default.node_sound_wood_defaults()
288 )
289
290 stairs.register_stair_and_slab(
291         "junglewood",
292         "default:junglewood",
293         {choppy = 2, oddly_breakable_by_hand = 2, flammable = 2},
294         {"default_junglewood.png"},
295         "Jungle Wood Stair",
296         "Jungle Wood Slab",
297         default.node_sound_wood_defaults()
298 )
299
300 stairs.register_stair_and_slab(
301         "pine_wood",
302         "default:pine_wood",
303         {choppy = 3, oddly_breakable_by_hand = 2, flammable = 3},
304         {"default_pine_wood.png"},
305         "Pine Wood Stair",
306         "Pine Wood Slab",
307         default.node_sound_wood_defaults()
308 )
309
310 stairs.register_stair_and_slab(
311         "acacia_wood",
312         "default:acacia_wood",
313         {choppy = 2, oddly_breakable_by_hand = 2, flammable = 2},
314         {"default_acacia_wood.png"},
315         "Acacia Wood Stair",
316         "Acacia Wood Slab",
317         default.node_sound_wood_defaults()
318 )
319
320 stairs.register_stair_and_slab(
321         "aspen_wood",
322         "default:aspen_wood",
323         {choppy = 3, oddly_breakable_by_hand = 2, flammable = 3},
324         {"default_aspen_wood.png"},
325         "Aspen Wood Stair",
326         "Aspen Wood Slab",
327         default.node_sound_wood_defaults()
328 )
329
330 stairs.register_stair_and_slab(
331         "stone",
332         "default:stone",
333         {cracky = 3},
334         {"default_stone.png"},
335         "Stone Stair",
336         "Stone Slab",
337         default.node_sound_stone_defaults()
338 )
339
340 stairs.register_stair_and_slab(
341         "cobble",
342         "default:cobble",
343         {cracky = 3},
344         {"default_cobble.png"},
345         "Cobblestone Stair",
346         "Cobblestone Slab",
347         default.node_sound_stone_defaults()
348 )
349
350 stairs.register_stair_and_slab(
351         "mossycobble",
352         nil,
353         {cracky = 3},
354         {"default_mossycobble.png"},
355         "Mossy Cobblestone Stair",
356         "Mossy Cobblestone Slab",
357         default.node_sound_stone_defaults()
358 )
359
360 stairs.register_stair_and_slab(
361         "stonebrick",
362         "default:stonebrick",
363         {cracky = 2},
364         {"default_stone_brick.png"},
365         "Stone Brick Stair",
366         "Stone Brick Slab",
367         default.node_sound_stone_defaults()
368 )
369
370 stairs.register_stair_and_slab(
371         "stone_block",
372         "default:stone_block",
373         {cracky = 2},
374         {"default_stone_block.png"},
375         "Stone Block Stair",
376         "Stone Block Slab",
377         default.node_sound_stone_defaults()
378 )
379
380 stairs.register_stair_and_slab(
381         "desert_stone",
382         "default:desert_stone",
383         {cracky = 3},
384         {"default_desert_stone.png"},
385         "Desert Stone Stair",
386         "Desert Stone Slab",
387         default.node_sound_stone_defaults()
388 )
389
390 stairs.register_stair_and_slab(
391         "desert_cobble",
392         "default:desert_cobble",
393         {cracky = 3},
394         {"default_desert_cobble.png"},
395         "Desert Cobblestone Stair",
396         "Desert Cobblestone Slab",
397         default.node_sound_stone_defaults()
398 )
399
400 stairs.register_stair_and_slab(
401         "desert_stonebrick",
402         "default:desert_stonebrick",
403         {cracky = 2},
404         {"default_desert_stone_brick.png"},
405         "Desert Stone Brick Stair",
406         "Desert Stone Brick Slab",
407         default.node_sound_stone_defaults()
408 )
409
410 stairs.register_stair_and_slab(
411         "desert_stone_block",
412         "default:desert_stone_block",
413         {cracky = 2},
414         {"default_desert_stone_block.png"},
415         "Desert Stone Block Stair",
416         "Desert Stone Block Slab",
417         default.node_sound_stone_defaults()
418 )
419
420 stairs.register_stair_and_slab(
421         "sandstone",
422         "default:sandstone",
423         {crumbly = 1, cracky = 3},
424         {"default_sandstone.png"},
425         "Sandstone Stair",
426         "Sandstone Slab",
427         default.node_sound_stone_defaults()
428 )
429
430 stairs.register_stair_and_slab(
431         "sandstonebrick",
432         "default:sandstonebrick",
433         {cracky = 2},
434         {"default_sandstone_brick.png"},
435         "Sandstone Brick Stair",
436         "Sandstone Brick Slab",
437         default.node_sound_stone_defaults()
438 )
439
440 stairs.register_stair_and_slab(
441         "sandstone_block",
442         "default:sandstone_block",
443         {cracky = 2},
444         {"default_sandstone_block.png"},
445         "Sandstone Block Stair",
446         "Sandstone Block Slab",
447         default.node_sound_stone_defaults()
448 )
449
450 stairs.register_stair_and_slab(
451         "desert_sandstone",
452         "default:desert_sandstone",
453         {crumbly = 1, cracky = 3},
454         {"default_desert_sandstone.png"},
455         "Desert Sandstone Stair",
456         "Desert Sandstone Slab",
457         default.node_sound_stone_defaults()
458 )
459
460 stairs.register_stair_and_slab(
461         "desert_sandstone_brick",
462         "default:desert_sandstone_brick",
463         {cracky = 2},
464         {"default_desert_sandstone_brick.png"},
465         "Desert Sandstone Brick Stair",
466         "Desert Sandstone Brick Slab",
467         default.node_sound_stone_defaults()
468 )
469
470 stairs.register_stair_and_slab(
471         "desert_sandstone_block",
472         "default:desert_sandstone_block",
473         {cracky = 2},
474         {"default_desert_sandstone_block.png"},
475         "Desert Sandstone Block Stair",
476         "Desert Sandstone Block Slab",
477         default.node_sound_stone_defaults()
478 )
479
480 stairs.register_stair_and_slab(
481         "silver_sandstone",
482         "default:silver_sandstone",
483         {crumbly = 1, cracky = 3},
484         {"default_silver_sandstone.png"},
485         "Silver Sandstone Stair",
486         "Silver Sandstone Slab",
487         default.node_sound_stone_defaults()
488 )
489
490 stairs.register_stair_and_slab(
491         "silver_sandstone_brick",
492         "default:silver_sandstone_brick",
493         {cracky = 2},
494         {"default_silver_sandstone_brick.png"},
495         "Silver Sandstone Brick Stair",
496         "Silver Sandstone Brick Slab",
497         default.node_sound_stone_defaults()
498 )
499
500 stairs.register_stair_and_slab(
501         "silver_sandstone_block",
502         "default:silver_sandstone_block",
503         {cracky = 2},
504         {"default_silver_sandstone_block.png"},
505         "Silver Sandstone Block Stair",
506         "Silver Sandstone Block Slab",
507         default.node_sound_stone_defaults()
508 )
509
510 stairs.register_stair_and_slab(
511         "obsidian",
512         "default:obsidian",
513         {cracky = 1, level = 2},
514         {"default_obsidian.png"},
515         "Obsidian Stair",
516         "Obsidian Slab",
517         default.node_sound_stone_defaults()
518 )
519
520 stairs.register_stair_and_slab(
521         "obsidianbrick",
522         "default:obsidianbrick",
523         {cracky = 1, level = 2},
524         {"default_obsidian_brick.png"},
525         "Obsidian Brick Stair",
526         "Obsidian Brick Slab",
527         default.node_sound_stone_defaults()
528 )
529
530 stairs.register_stair_and_slab(
531         "obsidian_block",
532         "default:obsidian_block",
533         {cracky = 1, level = 2},
534         {"default_obsidian_block.png"},
535         "Obsidian Block Stair",
536         "Obsidian Block Slab",
537         default.node_sound_stone_defaults()
538 )
539
540 stairs.register_stair_and_slab(
541         "brick",
542         "default:brick",
543         {cracky = 3},
544         {"default_brick.png"},
545         "Brick Stair",
546         "Brick Slab",
547         default.node_sound_stone_defaults()
548 )
549
550 stairs.register_stair_and_slab(
551         "straw",
552         "farming:straw",
553         {snappy = 3, flammable = 4},
554         {"farming_straw.png"},
555         "Straw Stair",
556         "Straw Slab",
557         default.node_sound_leaves_defaults()
558 )
559
560 stairs.register_stair_and_slab(
561         "steelblock",
562         "default:steelblock",
563         {cracky = 1, level = 2},
564         {"default_steel_block.png"},
565         "Steel Block Stair",
566         "Steel Block Slab",
567         default.node_sound_metal_defaults()
568 )
569
570 stairs.register_stair_and_slab(
571         "copperblock",
572         "default:copperblock",
573         {cracky = 1, level = 2},
574         {"default_copper_block.png"},
575         "Copper Block Stair",
576         "Copper Block Slab",
577         default.node_sound_metal_defaults()
578 )
579
580 stairs.register_stair_and_slab(
581         "bronzeblock",
582         "default:bronzeblock",
583         {cracky = 1, level = 2},
584         {"default_bronze_block.png"},
585         "Bronze Block Stair",
586         "Bronze Block Slab",
587         default.node_sound_metal_defaults()
588 )
589
590 stairs.register_stair_and_slab(
591         "goldblock",
592         "default:goldblock",
593         {cracky = 1},
594         {"default_gold_block.png"},
595         "Gold Block Stair",
596         "Gold Block Slab",
597         default.node_sound_metal_defaults()
598 )
599
600 stairs.register_stair_and_slab(
601         "ice",
602         "default:ice",
603         {cracky = 3, puts_out_fire = 1, cools_lava = 1},
604         {"default_ice.png"},
605         "Ice Stair",
606         "Ice Slab",
607         default.node_sound_glass_defaults()
608 )
609
610 stairs.register_stair_and_slab(
611         "snowblock",
612         "default:snowblock",
613         {crumbly = 3, puts_out_fire = 1, cools_lava = 1, snowy = 1},
614         {"default_snow.png"},
615         "Snow Block Stair",
616         "Snow Block Slab",
617         default.node_sound_dirt_defaults({
618                 footstep = {name = "default_snow_footstep", gain = 0.15},
619                 dug = {name = "default_snow_footstep", gain = 0.2},
620                 dig = {name = "default_snow_footstep", gain = 0.2}
621         })
622 )