init.lua 14 KB

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