2
0

init.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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.setting_getbool("enable_stairs_replace_abm")
  10. -- Register stairs.
  11. -- Node will be called stairs:stair_<subname>
  12. function stairs.register_stair(subname, recipeitem, groups, images, description, sounds)
  13. groups.stair = 1
  14. minetest.register_node(":stairs:stair_" .. subname, {
  15. description = description,
  16. drawtype = "mesh",
  17. mesh = "stairs_stair.obj",
  18. tiles = images,
  19. paramtype = "light",
  20. paramtype2 = "facedir",
  21. is_ground_content = false,
  22. groups = groups,
  23. sounds = sounds,
  24. selection_box = {
  25. type = "fixed",
  26. fixed = {
  27. {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
  28. {-0.5, 0, 0, 0.5, 0.5, 0.5},
  29. },
  30. },
  31. collision_box = {
  32. type = "fixed",
  33. fixed = {
  34. {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
  35. {-0.5, 0, 0, 0.5, 0.5, 0.5},
  36. },
  37. },
  38. on_place = function(itemstack, placer, pointed_thing)
  39. if pointed_thing.type ~= "node" then
  40. return itemstack
  41. end
  42. local p0 = pointed_thing.under
  43. local p1 = pointed_thing.above
  44. local param2 = 0
  45. local placer_pos = placer:getpos()
  46. if placer_pos then
  47. local dir = {
  48. x = p1.x - placer_pos.x,
  49. y = p1.y - placer_pos.y,
  50. z = p1.z - placer_pos.z
  51. }
  52. param2 = minetest.dir_to_facedir(dir)
  53. end
  54. if p0.y - 1 == p1.y then
  55. param2 = param2 + 20
  56. if param2 == 21 then
  57. param2 = 23
  58. elseif param2 == 23 then
  59. param2 = 21
  60. end
  61. end
  62. return minetest.item_place(itemstack, placer, pointed_thing, param2)
  63. end,
  64. })
  65. -- for replace ABM
  66. if replace then
  67. minetest.register_node(":stairs:stair_" .. subname .. "upside_down", {
  68. replace_name = "stairs:stair_" .. subname,
  69. groups = {slabs_replace = 1},
  70. })
  71. end
  72. if recipeitem then
  73. minetest.register_craft({
  74. output = 'stairs:stair_' .. subname .. ' 8',
  75. recipe = {
  76. {recipeitem, "", ""},
  77. {recipeitem, recipeitem, ""},
  78. {recipeitem, recipeitem, recipeitem},
  79. },
  80. })
  81. -- Flipped recipe for the silly minecrafters
  82. minetest.register_craft({
  83. output = 'stairs:stair_' .. subname .. ' 8',
  84. recipe = {
  85. {"", "", recipeitem},
  86. {"", recipeitem, recipeitem},
  87. {recipeitem, recipeitem, recipeitem},
  88. },
  89. })
  90. end
  91. end
  92. -- Register slabs.
  93. -- Node will be called stairs:slab_<subname>
  94. function stairs.register_slab(subname, recipeitem, groups, images, description, sounds)
  95. groups.slab = 1
  96. minetest.register_node(":stairs:slab_" .. subname, {
  97. description = description,
  98. drawtype = "nodebox",
  99. tiles = images,
  100. paramtype = "light",
  101. paramtype2 = "facedir",
  102. is_ground_content = false,
  103. groups = groups,
  104. sounds = sounds,
  105. node_box = {
  106. type = "fixed",
  107. fixed = {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
  108. },
  109. on_place = function(itemstack, placer, pointed_thing)
  110. if pointed_thing.type ~= "node" then
  111. return itemstack
  112. end
  113. -- If it's being placed on an another similar one, replace it with
  114. -- a full block
  115. local slabpos = nil
  116. local slabnode = nil
  117. local p0 = pointed_thing.under
  118. local p1 = pointed_thing.above
  119. local n0 = minetest.get_node(p0)
  120. local n1 = minetest.get_node(p1)
  121. local param2 = 0
  122. local n0_is_upside_down = (n0.name == "stairs:slab_" .. subname and
  123. n0.param2 >= 20)
  124. if n0.name == "stairs:slab_" .. subname and not n0_is_upside_down and
  125. p0.y + 1 == p1.y then
  126. slabpos = p0
  127. slabnode = n0
  128. elseif n1.name == "stairs:slab_" .. subname then
  129. slabpos = p1
  130. slabnode = n1
  131. end
  132. if slabpos then
  133. -- Remove the slab at slabpos
  134. minetest.remove_node(slabpos)
  135. -- Make a fake stack of a single item and try to place it
  136. local fakestack = ItemStack(recipeitem)
  137. fakestack:set_count(itemstack:get_count())
  138. pointed_thing.above = slabpos
  139. local success
  140. fakestack, success = minetest.item_place(fakestack, placer,
  141. pointed_thing)
  142. -- If the item was taken from the fake stack, decrement original
  143. if success then
  144. itemstack:set_count(fakestack:get_count())
  145. -- Else put old node back
  146. else
  147. minetest.set_node(slabpos, slabnode)
  148. end
  149. return itemstack
  150. end
  151. -- Upside down slabs
  152. if p0.y - 1 == p1.y then
  153. -- Turn into full block if pointing at a existing slab
  154. if n0_is_upside_down then
  155. -- Remove the slab at the position of the slab
  156. minetest.remove_node(p0)
  157. -- Make a fake stack of a single item and try to place it
  158. local fakestack = ItemStack(recipeitem)
  159. fakestack:set_count(itemstack:get_count())
  160. pointed_thing.above = p0
  161. local success
  162. fakestack, success = minetest.item_place(fakestack, placer,
  163. pointed_thing)
  164. -- If the item was taken from the fake stack, decrement original
  165. if success then
  166. itemstack:set_count(fakestack:get_count())
  167. -- Else put old node back
  168. else
  169. minetest.set_node(p0, n0)
  170. end
  171. return itemstack
  172. end
  173. -- Place upside down slab
  174. param2 = 20
  175. end
  176. -- If pointing at the side of a upside down slab
  177. if n0_is_upside_down and p0.y + 1 ~= p1.y then
  178. param2 = 20
  179. end
  180. return minetest.item_place(itemstack, placer, pointed_thing, param2)
  181. end,
  182. })
  183. -- for replace ABM
  184. if replace then
  185. minetest.register_node(":stairs:slab_" .. subname .. "upside_down", {
  186. replace_name = "stairs:slab_".. subname,
  187. groups = {slabs_replace = 1},
  188. })
  189. end
  190. if recipeitem then
  191. minetest.register_craft({
  192. output = 'stairs:slab_' .. subname .. ' 6',
  193. recipe = {
  194. {recipeitem, recipeitem, recipeitem},
  195. },
  196. })
  197. end
  198. end
  199. -- Optionally replace old "upside_down" nodes with new param2 versions.
  200. -- Disabled by default.
  201. if replace then
  202. minetest.register_abm({
  203. label = "Slab replace",
  204. nodenames = {"group:slabs_replace"},
  205. interval = 16,
  206. chance = 1,
  207. action = function(pos, node)
  208. node.name = minetest.registered_nodes[node.name].replace_name
  209. node.param2 = node.param2 + 20
  210. if node.param2 == 21 then
  211. node.param2 = 23
  212. elseif node.param2 == 23 then
  213. node.param2 = 21
  214. end
  215. minetest.set_node(pos, node)
  216. end,
  217. })
  218. end
  219. -- Stair/slab registration function.
  220. -- Nodes will be called stairs:{stair,slab}_<subname>
  221. function stairs.register_stair_and_slab(subname, recipeitem,
  222. groups, images, desc_stair, desc_slab, sounds)
  223. stairs.register_stair(subname, recipeitem, groups, images, desc_stair, sounds)
  224. stairs.register_slab(subname, recipeitem, groups, images, desc_slab, sounds)
  225. end
  226. -- Register default stairs and slabs
  227. stairs.register_stair_and_slab(
  228. "wood",
  229. "default:wood",
  230. {choppy = 2, oddly_breakable_by_hand = 2, flammable = 2},
  231. {"default_wood.png"},
  232. "Wooden Stair",
  233. "Wooden Slab",
  234. default.node_sound_wood_defaults()
  235. )
  236. stairs.register_stair_and_slab(
  237. "junglewood",
  238. "default:junglewood",
  239. {choppy = 2, oddly_breakable_by_hand = 2, flammable = 2},
  240. {"default_junglewood.png"},
  241. "Jungle Wood Stair",
  242. "Jungle Wood Slab",
  243. default.node_sound_wood_defaults()
  244. )
  245. stairs.register_stair_and_slab(
  246. "pine_wood",
  247. "default:pine_wood",
  248. {choppy = 3, oddly_breakable_by_hand = 2, flammable = 3},
  249. {"default_pine_wood.png"},
  250. "Pine Wood Stair",
  251. "Pine Wood Slab",
  252. default.node_sound_wood_defaults()
  253. )
  254. stairs.register_stair_and_slab(
  255. "acacia_wood",
  256. "default:acacia_wood",
  257. {choppy = 2, oddly_breakable_by_hand = 2, flammable = 2},
  258. {"default_acacia_wood.png"},
  259. "Acacia Wood Stair",
  260. "Acacia Wood Slab",
  261. default.node_sound_wood_defaults()
  262. )
  263. stairs.register_stair_and_slab(
  264. "aspen_wood",
  265. "default:aspen_wood",
  266. {choppy = 3, oddly_breakable_by_hand = 2, flammable = 3},
  267. {"default_aspen_wood.png"},
  268. "Aspen Wood Stair",
  269. "Aspen Wood Slab",
  270. default.node_sound_wood_defaults()
  271. )
  272. stairs.register_stair_and_slab(
  273. "stone",
  274. "default:stone",
  275. {cracky = 3},
  276. {"default_stone.png"},
  277. "Stone Stair",
  278. "Stone Slab",
  279. default.node_sound_stone_defaults()
  280. )
  281. stairs.register_stair_and_slab(
  282. "cobble",
  283. "default:cobble",
  284. {cracky = 3},
  285. {"default_cobble.png"},
  286. "Cobblestone Stair",
  287. "Cobblestone Slab",
  288. default.node_sound_stone_defaults()
  289. )
  290. stairs.register_stair_and_slab(
  291. "mossycobble",
  292. nil,
  293. {cracky = 3},
  294. {"default_mossycobble.png"},
  295. "Mossy Cobblestone Stair",
  296. "Mossy Cobblestone Slab",
  297. default.node_sound_stone_defaults()
  298. )
  299. stairs.register_stair_and_slab(
  300. "stonebrick",
  301. "default:stonebrick",
  302. {cracky = 2},
  303. {"default_stone_brick.png"},
  304. "Stone Brick Stair",
  305. "Stone Brick Slab",
  306. default.node_sound_stone_defaults()
  307. )
  308. stairs.register_stair_and_slab(
  309. "stone_block",
  310. "default:stone_block",
  311. {cracky = 2},
  312. {"default_stone_block.png"},
  313. "Stone Block Stair",
  314. "Stone Block Slab",
  315. default.node_sound_stone_defaults()
  316. )
  317. stairs.register_stair_and_slab(
  318. "desert_stone",
  319. "default:desert_stone",
  320. {cracky = 3},
  321. {"default_desert_stone.png"},
  322. "Desert Stone Stair",
  323. "Desert Stone Slab",
  324. default.node_sound_stone_defaults()
  325. )
  326. stairs.register_stair_and_slab(
  327. "desert_cobble",
  328. "default:desert_cobble",
  329. {cracky = 3},
  330. {"default_desert_cobble.png"},
  331. "Desert Cobblestone Stair",
  332. "Desert Cobblestone Slab",
  333. default.node_sound_stone_defaults()
  334. )
  335. stairs.register_stair_and_slab(
  336. "desert_stonebrick",
  337. "default:desert_stonebrick",
  338. {cracky = 2},
  339. {"default_desert_stone_brick.png"},
  340. "Desert Stone Brick Stair",
  341. "Desert Stone Brick Slab",
  342. default.node_sound_stone_defaults()
  343. )
  344. stairs.register_stair_and_slab(
  345. "desert_stone_block",
  346. "default:desert_stone_block",
  347. {cracky = 2},
  348. {"default_desert_stone_block.png"},
  349. "Desert Stone Block Stair",
  350. "Desert Stone Block Slab",
  351. default.node_sound_stone_defaults()
  352. )
  353. stairs.register_stair_and_slab(
  354. "sandstone",
  355. "default:sandstone",
  356. {crumbly = 1, cracky = 3},
  357. {"default_sandstone.png"},
  358. "Sandstone Stair",
  359. "Sandstone Slab",
  360. default.node_sound_stone_defaults()
  361. )
  362. stairs.register_stair_and_slab(
  363. "sandstonebrick",
  364. "default:sandstonebrick",
  365. {cracky = 2},
  366. {"default_sandstone_brick.png"},
  367. "Sandstone Brick Stair",
  368. "Sandstone Brick Slab",
  369. default.node_sound_stone_defaults()
  370. )
  371. stairs.register_stair_and_slab(
  372. "sandstone_block",
  373. "default:sandstone_block",
  374. {cracky = 2},
  375. {"default_sandstone_block.png"},
  376. "Sandstone Block Stair",
  377. "Sandstone Block Slab",
  378. default.node_sound_stone_defaults()
  379. )
  380. stairs.register_stair_and_slab(
  381. "obsidian",
  382. "default:obsidian",
  383. {cracky = 1, level = 2},
  384. {"default_obsidian.png"},
  385. "Obsidian Stair",
  386. "Obsidian Slab",
  387. default.node_sound_stone_defaults()
  388. )
  389. stairs.register_stair_and_slab(
  390. "obsidianbrick",
  391. "default:obsidianbrick",
  392. {cracky = 1, level = 2},
  393. {"default_obsidian_brick.png"},
  394. "Obsidian Brick Stair",
  395. "Obsidian Brick Slab",
  396. default.node_sound_stone_defaults()
  397. )
  398. stairs.register_stair_and_slab(
  399. "obsidian_block",
  400. "default:obsidian_block",
  401. {cracky = 1, level = 2},
  402. {"default_obsidian_block.png"},
  403. "Obsidian Block Stair",
  404. "Obsidian Block Slab",
  405. default.node_sound_stone_defaults()
  406. )
  407. stairs.register_stair_and_slab(
  408. "brick",
  409. "default:brick",
  410. {cracky = 3},
  411. {"default_brick.png"},
  412. "Brick Stair",
  413. "Brick Slab",
  414. default.node_sound_stone_defaults()
  415. )
  416. stairs.register_stair_and_slab(
  417. "straw",
  418. "farming:straw",
  419. {snappy = 3, flammable = 4},
  420. {"farming_straw.png"},
  421. "Straw Stair",
  422. "Straw Slab",
  423. default.node_sound_leaves_defaults()
  424. )
  425. stairs.register_stair_and_slab(
  426. "steelblock",
  427. "default:steelblock",
  428. {cracky = 1, level = 2},
  429. {"default_steel_block.png"},
  430. "Steel Block Stair",
  431. "Steel Block Slab",
  432. default.node_sound_stone_defaults()
  433. )
  434. stairs.register_stair_and_slab(
  435. "copperblock",
  436. "default:copperblock",
  437. {cracky = 1, level = 2},
  438. {"default_copper_block.png"},
  439. "Copper Block Stair",
  440. "Copper Block Slab",
  441. default.node_sound_stone_defaults()
  442. )
  443. stairs.register_stair_and_slab(
  444. "bronzeblock",
  445. "default:bronzeblock",
  446. {cracky = 1, level = 2},
  447. {"default_bronze_block.png"},
  448. "Bronze Block Stair",
  449. "Bronze Block Slab",
  450. default.node_sound_stone_defaults()
  451. )
  452. stairs.register_stair_and_slab(
  453. "goldblock",
  454. "default:goldblock",
  455. {cracky = 1},
  456. {"default_gold_block.png"},
  457. "Gold Block Stair",
  458. "Gold Block Slab",
  459. default.node_sound_stone_defaults()
  460. )