init.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. -- Minetest 0.4 mod: stairs
  2. -- See README.txt for licensing and other information.
  3. stairs = {}
  4. -- Node will be called stairs:stair_<subname>
  5. function stairs.register_stair(subname, recipeitem, groups, images, description, sounds)
  6. minetest.register_node(":stairs:stair_" .. subname, {
  7. description = description,
  8. drawtype = "nodebox",
  9. tiles = images,
  10. paramtype = "light",
  11. paramtype2 = "facedir",
  12. is_ground_content = true,
  13. groups = groups,
  14. sounds = sounds,
  15. node_box = {
  16. type = "fixed",
  17. fixed = {
  18. {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
  19. {-0.5, 0, 0, 0.5, 0.5, 0.5},
  20. },
  21. },
  22. on_place = function(itemstack, placer, pointed_thing)
  23. if pointed_thing.type ~= "node" then
  24. return itemstack
  25. end
  26. local p0 = pointed_thing.under
  27. local p1 = pointed_thing.above
  28. local param2 = 0
  29. local placer_pos = placer:getpos()
  30. if placer_pos then
  31. local dir = {
  32. x = p1.x - placer_pos.x,
  33. y = p1.y - placer_pos.y,
  34. z = p1.z - placer_pos.z
  35. }
  36. param2 = minetest.dir_to_facedir(dir)
  37. end
  38. if p0.y-1 == p1.y then
  39. param2 = param2 + 20
  40. if param2 == 21 then
  41. param2 = 23
  42. elseif param2 == 23 then
  43. param2 = 21
  44. end
  45. end
  46. return minetest.item_place(itemstack, placer, pointed_thing, param2)
  47. end,
  48. })
  49. -- for replace ABM
  50. minetest.register_node(":stairs:stair_" .. subname.."upside_down", {
  51. replace_name = "stairs:stair_" .. subname,
  52. groups = {slabs_replace=1},
  53. })
  54. minetest.register_craft({
  55. output = 'stairs:stair_' .. subname .. ' 4',
  56. recipe = {
  57. {recipeitem, "", ""},
  58. {recipeitem, recipeitem, ""},
  59. {recipeitem, recipeitem, recipeitem},
  60. },
  61. })
  62. -- Flipped recipe for the silly minecrafters
  63. minetest.register_craft({
  64. output = 'stairs:stair_' .. subname .. ' 4',
  65. recipe = {
  66. {"", "", recipeitem},
  67. {"", recipeitem, recipeitem},
  68. {recipeitem, recipeitem, recipeitem},
  69. },
  70. })
  71. end
  72. -- Node will be called stairs:slab_<subname>
  73. function stairs.register_slab(subname, recipeitem, groups, images, description, sounds)
  74. minetest.register_node(":stairs:slab_" .. subname, {
  75. description = description,
  76. drawtype = "nodebox",
  77. tiles = images,
  78. paramtype = "light",
  79. paramtype2 = "facedir",
  80. is_ground_content = true,
  81. groups = groups,
  82. sounds = sounds,
  83. node_box = {
  84. type = "fixed",
  85. fixed = {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
  86. },
  87. on_place = function(itemstack, placer, pointed_thing)
  88. if pointed_thing.type ~= "node" then
  89. return itemstack
  90. end
  91. -- If it's being placed on an another similar one, replace it with
  92. -- a full block
  93. local slabpos = nil
  94. local slabnode = nil
  95. local p0 = pointed_thing.under
  96. local p1 = pointed_thing.above
  97. local n0 = minetest.get_node(p0)
  98. local n1 = minetest.get_node(p1)
  99. local param2 = 0
  100. local n0_is_upside_down = (n0.name == "stairs:slab_" .. subname and
  101. n0.param2 >= 20)
  102. if n0.name == "stairs:slab_" .. subname and not n0_is_upside_down and p0.y+1 == p1.y then
  103. slabpos = p0
  104. slabnode = n0
  105. elseif n1.name == "stairs:slab_" .. subname then
  106. slabpos = p1
  107. slabnode = n1
  108. end
  109. if slabpos then
  110. -- Remove the slab at slabpos
  111. minetest.remove_node(slabpos)
  112. -- Make a fake stack of a single item and try to place it
  113. local fakestack = ItemStack(recipeitem)
  114. fakestack:set_count(itemstack:get_count())
  115. pointed_thing.above = slabpos
  116. local success
  117. fakestack, success = minetest.item_place(fakestack, placer, pointed_thing)
  118. -- If the item was taken from the fake stack, decrement original
  119. if success then
  120. itemstack:set_count(fakestack:get_count())
  121. -- Else put old node back
  122. else
  123. minetest.set_node(slabpos, slabnode)
  124. end
  125. return itemstack
  126. end
  127. -- Upside down slabs
  128. if p0.y-1 == p1.y then
  129. -- Turn into full block if pointing at a existing slab
  130. if n0_is_upside_down then
  131. -- Remove the slab at the position of the slab
  132. minetest.remove_node(p0)
  133. -- Make a fake stack of a single item and try to place it
  134. local fakestack = ItemStack(recipeitem)
  135. fakestack:set_count(itemstack:get_count())
  136. pointed_thing.above = p0
  137. local success
  138. fakestack, success = minetest.item_place(fakestack, placer, pointed_thing)
  139. -- If the item was taken from the fake stack, decrement original
  140. if success then
  141. itemstack:set_count(fakestack:get_count())
  142. -- Else put old node back
  143. else
  144. minetest.set_node(p0, n0)
  145. end
  146. return itemstack
  147. end
  148. -- Place upside down slab
  149. param2 = 20
  150. end
  151. -- If pointing at the side of a upside down slab
  152. if n0_is_upside_down and p0.y+1 ~= p1.y then
  153. param2 = 20
  154. end
  155. return minetest.item_place(itemstack, placer, pointed_thing, param2)
  156. end,
  157. })
  158. -- for replace ABM
  159. minetest.register_node(":stairs:slab_" .. subname.."upside_down", {
  160. replace_name = "stairs:slab_"..subname,
  161. groups = {slabs_replace=1},
  162. })
  163. minetest.register_craft({
  164. output = 'stairs:slab_' .. subname .. ' 6',
  165. recipe = {
  166. {recipeitem, recipeitem, recipeitem},
  167. },
  168. })
  169. end
  170. -- Replace old "upside_down" nodes with new param2 versions
  171. minetest.register_abm({
  172. nodenames = {"group:slabs_replace"},
  173. interval = 1,
  174. chance = 1,
  175. action = function(pos, node)
  176. node.name = minetest.registered_nodes[node.name].replace_name
  177. node.param2 = node.param2 + 20
  178. if node.param2 == 21 then
  179. node.param2 = 23
  180. elseif node.param2 == 23 then
  181. node.param2 = 21
  182. end
  183. minetest.set_node(pos, node)
  184. end,
  185. })
  186. -- Nodes will be called stairs:{stair,slab}_<subname>
  187. function stairs.register_stair_and_slab(subname, recipeitem, groups, images, desc_stair, desc_slab, sounds)
  188. stairs.register_stair(subname, recipeitem, groups, images, desc_stair, sounds)
  189. stairs.register_slab(subname, recipeitem, groups, images, desc_slab, sounds)
  190. end
  191. stairs.register_stair_and_slab("wood", "default:wood",
  192. {choppy=2,oddly_breakable_by_hand=2,flammable=3},
  193. {"default_wood.png"},
  194. "Wooden Stair",
  195. "Wooden Slab",
  196. default.node_sound_wood_defaults())
  197. stairs.register_stair_and_slab("tree", "default:tree",
  198. {choppy=2,oddly_breakable_by_hand=1,flammable=2},
  199. {"default_tree_top.png", "default_tree_top.png", "default_tree.png",
  200. "default_tree.png", "default_tree.png", "default_tree_stair.png"},
  201. "Tree Stair",
  202. "Tree Slab",
  203. default.node_sound_wood_defaults())
  204. stairs.register_stair_and_slab("stone", "default:stone",
  205. {cracky=3},
  206. {"default_stone.png"},
  207. "Stone Stair",
  208. "Stone Slab",
  209. default.node_sound_stone_defaults())
  210. stairs.register_stair_and_slab("cobble", "default:cobble",
  211. {cracky=3},
  212. {"default_cobble.png"},
  213. "Cobble Stair",
  214. "Cobble Slab",
  215. default.node_sound_stone_defaults())
  216. stairs.register_stair_and_slab("brick", "default:brick",
  217. {cracky=3},
  218. {"default_brick.png"},
  219. "Brick Stair",
  220. "Brick Slab",
  221. default.node_sound_stone_defaults())
  222. stairs.register_stair_and_slab("sandstone", "default:sandstone",
  223. {crumbly=2,cracky=2},
  224. {"default_sandstone.png"},
  225. "Sandstone Stair",
  226. "Sandstone Slab",
  227. default.node_sound_stone_defaults())
  228. stairs.register_stair_and_slab("desert_sandstone", "default:desert_sandstone",
  229. {crumbly=2,cracky=2},
  230. {"default_desert_sandstone.png"},
  231. "Desert Sandstone Stair",
  232. "Desert Sandstone Slab",
  233. default.node_sound_stone_defaults())
  234. stairs.register_stair_and_slab("silver_sandstone", "default:silver_sandstone",
  235. {crumbly=2,cracky=2},
  236. {"default_silver_sandstone.png"},
  237. "Silver Sandstone Stair",
  238. "Silver Sandstone Slab",
  239. default.node_sound_stone_defaults())
  240. stairs.register_stair_and_slab("junglewood", "default:junglewood",
  241. {choppy=2,oddly_breakable_by_hand=2,flammable=3},
  242. {"default_junglewood.png"},
  243. "Junglewood Stair",
  244. "Junglewood Slab",
  245. default.node_sound_wood_defaults())
  246. stairs.register_stair_and_slab("jungletree", "default:jungletree",
  247. {choppy=2,oddly_breakable_by_hand=1,flammable=2},
  248. {"default_jungletree_top.png", "default_jungletree_top.png", "default_jungletree.png",
  249. "default_jungletree.png", "default_jungletree.png", "default_jungletree_stair.png"},
  250. "Jungle Tree Stair",
  251. "Jungle Tree Slab",
  252. default.node_sound_wood_defaults())
  253. stairs.register_stair_and_slab("stonebrick", "default:stonebrick",
  254. {cracky=3},
  255. {"default_stone_brick.png"},
  256. "Stone Brick Stair",
  257. "Stone Brick Slab",
  258. default.node_sound_stone_defaults())
  259. stairs.register_stair_and_slab("desert_stonebrick", "default:desert_stonebrick",
  260. {cracky=3},
  261. {"default_desert_stone_brick.png"},
  262. "Desert Stone Brick Stair",
  263. "Desert Stone Brick Slab",
  264. default.node_sound_stone_defaults())
  265. stairs.register_stair_and_slab("sandstone_brick", "default:sandstonebrick",
  266. {cracky=3},
  267. {"default_sandstone_brick.png"},
  268. "Sandstone Brick Stair",
  269. "Sandstone Brick Slab",
  270. default.node_sound_stone_defaults())
  271. stairs.register_stair_and_slab("desert_sandstone_brick", "default:desert_sandstonebrick",
  272. {cracky=3},
  273. {"default_desert_sandstone_brick.png"},
  274. "Desert Sandstone Brick Stair",
  275. "Desert Sandstone Brick Slab",
  276. default.node_sound_stone_defaults())
  277. stairs.register_stair_and_slab("silver_sandstone_brick", "default:silver_sandstonebrick",
  278. {cracky=3},
  279. {"default_silver_sandstone_brick.png"},
  280. "Silver Sandstone Brick Stair",
  281. "Silver Sandstone Brick Slab",
  282. default.node_sound_stone_defaults())
  283. stairs.register_stair_and_slab("stone_block", "default:stone_block",
  284. {cracky=3},
  285. {"default_stone_block.png"},
  286. "Stone Block Stair",
  287. "Stone Block Slab",
  288. default.node_sound_stone_defaults())
  289. stairs.register_stair_and_slab("desert_stone_block", "default:desert_stone_block",
  290. {cracky=3},
  291. {"default_desert_stone_block.png"},
  292. "Desert Stone Block Stair",
  293. "Desert Stone Block Slab",
  294. default.node_sound_stone_defaults())
  295. stairs.register_stair_and_slab("sandstone_block", "default:sandstone_block",
  296. {cracky=3},
  297. {"default_sandstone_block.png"},
  298. "Sandstone Block Stair",
  299. "Sandstone Block Slab",
  300. default.node_sound_stone_defaults())
  301. stairs.register_stair_and_slab("desert_sandstone_block", "default:desert_sandstone_block",
  302. {cracky=3},
  303. {"default_desert_sandstone_block.png"},
  304. "Desert Sandstone Block Stair",
  305. "Desert Sandstone Block Slab",
  306. default.node_sound_stone_defaults())
  307. stairs.register_stair_and_slab("silver_sandstone_block", "default:silver_sandstone_block",
  308. {cracky=3},
  309. {"default_silver_sandstone_block.png"},
  310. "Silver Sandstone Block Stair",
  311. "Silver Sandstone Block Slab",
  312. default.node_sound_stone_defaults())
  313. stairs.register_stair_and_slab("desert_stone", "default:desert_stone",
  314. {cracky=3},
  315. {"default_desert_stone.png"},
  316. "Desert Stone Stair",
  317. "Desert Stone Slab",
  318. default.node_sound_stone_defaults())
  319. stairs.register_stair_and_slab("desert_cobble", "default:desert_cobble",
  320. {cracky=3},
  321. {"default_desert_cobble.png"},
  322. "Desert Cobble Stair",
  323. "Desert Cobble Slab",
  324. default.node_sound_stone_defaults())