Add and edit fuel registrations
[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
110                 -- Fuel
111                 local baseburntime = minetest.get_craft_result({
112                         method = "fuel",
113                         width = 1,
114                         items = {recipeitem}
115                 }).time
116                 if baseburntime > 0 then
117                         minetest.register_craft({
118                                 type = "fuel",
119                                 recipe = 'stairs:stair_' .. subname,
120                                 burntime = math.floor(baseburntime * 0.75),
121                         })
122                 end
123         end
124 end
125
126
127 -- Slab facedir to placement 6d matching table
128 local slab_trans_dir = {[0] = 8, 0, 2, 1, 3, 4}
129 -- Slab facedir when placing initial slab against other surface
130 local slab_trans_dir_place = {[0] = 0, 20, 12, 16, 4, 8}
131
132 -- Register slabs.
133 -- Node will be called stairs:slab_<subname>
134
135 function stairs.register_slab(subname, recipeitem, groups, images, description, sounds)
136         groups.slab = 1
137         minetest.register_node(":stairs:slab_" .. subname, {
138                 description = description,
139                 drawtype = "nodebox",
140                 tiles = images,
141                 paramtype = "light",
142                 paramtype2 = "facedir",
143                 is_ground_content = false,
144                 groups = groups,
145                 sounds = sounds,
146                 node_box = {
147                         type = "fixed",
148                         fixed = {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
149                 },
150                 on_place = function(itemstack, placer, pointed_thing)
151                         local under = minetest.get_node(pointed_thing.under)
152                         local wield_item = itemstack:get_name()
153
154                         if under and wield_item == under.name then
155                                 -- place slab using under node orientation
156                                 local dir = minetest.dir_to_facedir(vector.subtract(
157                                         pointed_thing.above, pointed_thing.under), true)
158
159                                 local p2 = under.param2
160
161                                 -- combine two slabs if possible
162                                 if slab_trans_dir[math.floor(p2 / 4)] == dir then
163                                         if not recipeitem then
164                                                 return itemstack
165                                         end
166                                         local player_name = placer:get_player_name()
167                                         if minetest.is_protected(pointed_thing.under, player_name) and not
168                                                         minetest.check_player_privs(placer, "protection_bypass") then
169                                                 minetest.record_protection_violation(pointed_thing.under,
170                                                         player_name)
171                                                 return
172                                         end
173                                         minetest.set_node(pointed_thing.under, {name = recipeitem, param2 = p2})
174                                         if not minetest.setting_getbool("creative_mode") then
175                                                 itemstack:take_item()
176                                         end
177                                         return itemstack
178                                 end
179
180                                 -- Placing a slab on an upside down slab should make it right-side up.
181                                 if p2 >= 20 and dir == 8 then
182                                         p2 = p2 - 20
183                                 -- same for the opposite case: slab below normal slab
184                                 elseif p2 <= 3 and dir == 4 then
185                                         p2 = p2 + 20
186                                 end
187
188                                 -- else attempt to place node with proper param2
189                                 minetest.item_place_node(ItemStack(wield_item), placer, pointed_thing, p2)
190                                 if not minetest.setting_getbool("creative_mode") then
191                                         itemstack:take_item()
192                                 end
193                                 return itemstack
194                         else
195                                 -- place slab using look direction of player
196                                 local dir = minetest.dir_to_wallmounted(vector.subtract(
197                                         pointed_thing.above, pointed_thing.under), true)
198
199                                 local rot = slab_trans_dir_place[dir]
200                                 if rot == 0 or rot == 20 then
201                                         rot = rot + minetest.dir_to_facedir(placer:get_look_dir())
202                                 end
203
204                                 return minetest.item_place(itemstack, placer, pointed_thing, rot)
205                         end
206                 end,
207         })
208
209         -- for replace ABM
210         if replace then
211                 minetest.register_node(":stairs:slab_" .. subname .. "upside_down", {
212                         replace_name = "stairs:slab_".. subname,
213                         groups = {slabs_replace = 1},
214                 })
215         end
216
217         if recipeitem then
218                 minetest.register_craft({
219                         output = 'stairs:slab_' .. subname .. ' 6',
220                         recipe = {
221                                 {recipeitem, recipeitem, recipeitem},
222                         },
223                 })
224
225                 -- Fuel
226                 local baseburntime = minetest.get_craft_result({
227                         method = "fuel",
228                         width = 1,
229                         items = {recipeitem}
230                 }).time
231                 if baseburntime > 0 then
232                         minetest.register_craft({
233                                 type = "fuel",
234                                 recipe = 'stairs:slab_' .. subname,
235                                 burntime = math.floor(baseburntime * 0.5),
236                         })
237                 end
238         end
239 end
240
241
242 -- Optionally replace old "upside_down" nodes with new param2 versions.
243 -- Disabled by default.
244
245 if replace then
246         minetest.register_abm({
247                 label = "Slab replace",
248                 nodenames = {"group:slabs_replace"},
249                 interval = 16,
250                 chance = 1,
251                 action = function(pos, node)
252                         node.name = minetest.registered_nodes[node.name].replace_name
253                         node.param2 = node.param2 + 20
254                         if node.param2 == 21 then
255                                 node.param2 = 23
256                         elseif node.param2 == 23 then
257                                 node.param2 = 21
258                         end
259                         minetest.set_node(pos, node)
260                 end,
261         })
262 end
263
264
265 -- Stair/slab registration function.
266 -- Nodes will be called stairs:{stair,slab}_<subname>
267
268 function stairs.register_stair_and_slab(subname, recipeitem,
269                 groups, images, desc_stair, desc_slab, sounds)
270         stairs.register_stair(subname, recipeitem, groups, images, desc_stair, sounds)
271         stairs.register_slab(subname, recipeitem, groups, images, desc_slab, sounds)
272 end
273
274
275 -- Register default stairs and slabs
276
277 stairs.register_stair_and_slab(
278         "wood",
279         "default:wood",
280         {choppy = 2, oddly_breakable_by_hand = 2, flammable = 2},
281         {"default_wood.png"},
282         "Wooden Stair",
283         "Wooden Slab",
284         default.node_sound_wood_defaults()
285 )
286
287 stairs.register_stair_and_slab(
288         "junglewood",
289         "default:junglewood",
290         {choppy = 2, oddly_breakable_by_hand = 2, flammable = 2},
291         {"default_junglewood.png"},
292         "Jungle Wood Stair",
293         "Jungle Wood Slab",
294         default.node_sound_wood_defaults()
295 )
296
297 stairs.register_stair_and_slab(
298         "pine_wood",
299         "default:pine_wood",
300         {choppy = 3, oddly_breakable_by_hand = 2, flammable = 3},
301         {"default_pine_wood.png"},
302         "Pine Wood Stair",
303         "Pine Wood Slab",
304         default.node_sound_wood_defaults()
305 )
306
307 stairs.register_stair_and_slab(
308         "acacia_wood",
309         "default:acacia_wood",
310         {choppy = 2, oddly_breakable_by_hand = 2, flammable = 2},
311         {"default_acacia_wood.png"},
312         "Acacia Wood Stair",
313         "Acacia Wood Slab",
314         default.node_sound_wood_defaults()
315 )
316
317 stairs.register_stair_and_slab(
318         "aspen_wood",
319         "default:aspen_wood",
320         {choppy = 3, oddly_breakable_by_hand = 2, flammable = 3},
321         {"default_aspen_wood.png"},
322         "Aspen Wood Stair",
323         "Aspen Wood Slab",
324         default.node_sound_wood_defaults()
325 )
326
327 stairs.register_stair_and_slab(
328         "stone",
329         "default:stone",
330         {cracky = 3},
331         {"default_stone.png"},
332         "Stone Stair",
333         "Stone Slab",
334         default.node_sound_stone_defaults()
335 )
336
337 stairs.register_stair_and_slab(
338         "cobble",
339         "default:cobble",
340         {cracky = 3},
341         {"default_cobble.png"},
342         "Cobblestone Stair",
343         "Cobblestone Slab",
344         default.node_sound_stone_defaults()
345 )
346
347 stairs.register_stair_and_slab(
348         "mossycobble",
349         nil,
350         {cracky = 3},
351         {"default_mossycobble.png"},
352         "Mossy Cobblestone Stair",
353         "Mossy Cobblestone Slab",
354         default.node_sound_stone_defaults()
355 )
356
357 stairs.register_stair_and_slab(
358         "stonebrick",
359         "default:stonebrick",
360         {cracky = 2},
361         {"default_stone_brick.png"},
362         "Stone Brick Stair",
363         "Stone Brick Slab",
364         default.node_sound_stone_defaults()
365 )
366
367 stairs.register_stair_and_slab(
368         "stone_block",
369         "default:stone_block",
370         {cracky = 2},
371         {"default_stone_block.png"},
372         "Stone Block Stair",
373         "Stone Block Slab",
374         default.node_sound_stone_defaults()
375 )
376
377 stairs.register_stair_and_slab(
378         "desert_stone",
379         "default:desert_stone",
380         {cracky = 3},
381         {"default_desert_stone.png"},
382         "Desert Stone Stair",
383         "Desert Stone Slab",
384         default.node_sound_stone_defaults()
385 )
386
387 stairs.register_stair_and_slab(
388         "desert_cobble",
389         "default:desert_cobble",
390         {cracky = 3},
391         {"default_desert_cobble.png"},
392         "Desert Cobblestone Stair",
393         "Desert Cobblestone Slab",
394         default.node_sound_stone_defaults()
395 )
396
397 stairs.register_stair_and_slab(
398         "desert_stonebrick",
399         "default:desert_stonebrick",
400         {cracky = 2},
401         {"default_desert_stone_brick.png"},
402         "Desert Stone Brick Stair",
403         "Desert Stone Brick Slab",
404         default.node_sound_stone_defaults()
405 )
406
407 stairs.register_stair_and_slab(
408         "desert_stone_block",
409         "default:desert_stone_block",
410         {cracky = 2},
411         {"default_desert_stone_block.png"},
412         "Desert Stone Block Stair",
413         "Desert Stone Block Slab",
414         default.node_sound_stone_defaults()
415 )
416
417 stairs.register_stair_and_slab(
418         "sandstone",
419         "default:sandstone",
420         {crumbly = 1, cracky = 3},
421         {"default_sandstone.png"},
422         "Sandstone Stair",
423         "Sandstone Slab",
424         default.node_sound_stone_defaults()
425 )
426
427 stairs.register_stair_and_slab(
428         "sandstonebrick",
429         "default:sandstonebrick",
430         {cracky = 2},
431         {"default_sandstone_brick.png"},
432         "Sandstone Brick Stair",
433         "Sandstone Brick Slab",
434         default.node_sound_stone_defaults()
435 )
436
437 stairs.register_stair_and_slab(
438         "sandstone_block",
439         "default:sandstone_block",
440         {cracky = 2},
441         {"default_sandstone_block.png"},
442         "Sandstone Block Stair",
443         "Sandstone Block Slab",
444         default.node_sound_stone_defaults()
445 )
446
447 stairs.register_stair_and_slab(
448         "obsidian",
449         "default:obsidian",
450         {cracky = 1, level = 2},
451         {"default_obsidian.png"},
452         "Obsidian Stair",
453         "Obsidian Slab",
454         default.node_sound_stone_defaults()
455 )
456
457 stairs.register_stair_and_slab(
458         "obsidianbrick",
459         "default:obsidianbrick",
460         {cracky = 1, level = 2},
461         {"default_obsidian_brick.png"},
462         "Obsidian Brick Stair",
463         "Obsidian Brick Slab",
464         default.node_sound_stone_defaults()
465 )
466
467 stairs.register_stair_and_slab(
468         "obsidian_block",
469         "default:obsidian_block",
470         {cracky = 1, level = 2},
471         {"default_obsidian_block.png"},
472         "Obsidian Block Stair",
473         "Obsidian Block Slab",
474         default.node_sound_stone_defaults()
475 )
476
477 stairs.register_stair_and_slab(
478         "brick",
479         "default:brick",
480         {cracky = 3},
481         {"default_brick.png"},
482         "Brick Stair",
483         "Brick Slab",
484         default.node_sound_stone_defaults()
485 )
486
487 stairs.register_stair_and_slab(
488         "straw",
489         "farming:straw",
490         {snappy = 3, flammable = 4},
491         {"farming_straw.png"},
492         "Straw Stair",
493         "Straw Slab",
494         default.node_sound_leaves_defaults()
495 )
496
497 stairs.register_stair_and_slab(
498         "steelblock",
499         "default:steelblock",
500         {cracky = 1, level = 2},
501         {"default_steel_block.png"},
502         "Steel Block Stair",
503         "Steel Block Slab",
504         default.node_sound_metal_defaults()
505 )
506
507 stairs.register_stair_and_slab(
508         "copperblock",
509         "default:copperblock",
510         {cracky = 1, level = 2},
511         {"default_copper_block.png"},
512         "Copper Block Stair",
513         "Copper Block Slab",
514         default.node_sound_metal_defaults()
515 )
516
517 stairs.register_stair_and_slab(
518         "bronzeblock",
519         "default:bronzeblock",
520         {cracky = 1, level = 2},
521         {"default_bronze_block.png"},
522         "Bronze Block Stair",
523         "Bronze Block Slab",
524         default.node_sound_metal_defaults()
525 )
526
527 stairs.register_stair_and_slab(
528         "goldblock",
529         "default:goldblock",
530         {cracky = 1},
531         {"default_gold_block.png"},
532         "Gold Block Stair",
533         "Gold Block Slab",
534         default.node_sound_metal_defaults()
535 )