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