init.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. stairs = {mod = "redo"}
  2. -- cache creative
  3. local creative = minetest.settings:get_bool("creative_mode")
  4. local function is_creative_enabled_for(name)
  5. if creative or minetest.check_player_privs(name, {creative = true}) then
  6. return true
  7. end
  8. return false
  9. end
  10. -- process textures
  11. local set_textures = function(images, worldaligntex)
  12. local stair_images = {}
  13. for i, image in ipairs(images) do
  14. stair_images[i] = type(image) == "string" and {name = image} or table.copy(image)
  15. if stair_images[i].backface_culling == nil then
  16. stair_images[i].backface_culling = true
  17. end
  18. if worldaligntex and stair_images[i].align_style == nil then
  19. stair_images[i].align_style = "world"
  20. end
  21. end
  22. return stair_images
  23. end
  24. -- placement helper
  25. local stair_place = function(itemstack, placer, pointed_thing, stair_node)
  26. -- if sneak pressed then use param2 in node pointed at when placing
  27. if placer:is_player() and placer:get_player_control().sneak then
  28. local name = placer:get_player_name()
  29. local pos_a = pointed_thing.above
  30. local node_a = minetest.get_node(pos_a)
  31. local def_a = minetest.registered_nodes[node_a.name]
  32. if not def_a.buildable_to or minetest.is_protected(pos_a, name) then
  33. return itemstack
  34. end
  35. local pos_u = pointed_thing.under
  36. local node_u = minetest.get_node(pos_u)
  37. if minetest.get_item_group(node_u.name, "stair") > 0
  38. or minetest.get_item_group(node_u.name, "slab") > 0 then
  39. minetest.set_node(pos_a, {
  40. name = stair_node,
  41. param2 = node_u.param2
  42. })
  43. if not is_creative_enabled_for(name) then
  44. itemstack:take_item()
  45. end
  46. return itemstack
  47. end
  48. end
  49. core.rotate_and_place(itemstack, placer, pointed_thing,
  50. is_creative_enabled_for(placer:get_player_name()),
  51. {invert_wall = placer:get_player_control().sneak})
  52. return itemstack
  53. end
  54. -- if recipeitem can be burned then stair can also
  55. local function set_burn(recipeitem, stair_name, v)
  56. local burntime = minetest.get_craft_result({
  57. method = "fuel", width = 1, items = {recipeitem} }).time
  58. if burntime > 0 then
  59. minetest.register_craft({
  60. type = "fuel",
  61. recipe = stair_name,
  62. burntime = math.floor(burntime * v)
  63. })
  64. end
  65. end
  66. -- Node will be called stairs:stair_<subname>
  67. function stairs.register_stair(
  68. subname, recipeitem, groups, images, description, snds, wat)
  69. local stair_images = set_textures(images, wat)
  70. local new_groups = table.copy(groups)
  71. new_groups.stair = 1
  72. local def = minetest.registered_nodes[recipeitem] or {}
  73. minetest.register_node(":stairs:stair_" .. subname, {
  74. description = description,
  75. drawtype = "nodebox",
  76. tiles = stair_images,
  77. paramtype = "light",
  78. paramtype2 = "facedir",
  79. is_ground_content = false,
  80. use_texture_alpha = def.use_texture_alpha,
  81. light_source = def.light_source,
  82. sunlight_propagates = def.sunlight_propagates,
  83. groups = new_groups,
  84. sounds = snds or def.sounds,
  85. node_box = {
  86. type = "fixed",
  87. fixed = {
  88. {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
  89. {-0.5, 0, 0, 0.5, 0.5, 0.5}
  90. }
  91. },
  92. on_place = function(itemstack, placer, pointed_thing)
  93. return stair_place(
  94. itemstack, placer, pointed_thing, "stairs:stair_" .. subname)
  95. end
  96. })
  97. -- if no recipe item provided then skip craft recipes
  98. if not recipeitem then
  99. return
  100. end
  101. -- stair recipes
  102. minetest.register_craft({
  103. output = "stairs:stair_" .. subname .. " 8",
  104. recipe = {
  105. {recipeitem, "", ""},
  106. {recipeitem, recipeitem, ""},
  107. {recipeitem, recipeitem, recipeitem}
  108. }
  109. })
  110. minetest.register_craft({
  111. output = "stairs:stair_" .. subname .. " 8",
  112. recipe = {
  113. {"", "", recipeitem},
  114. {"", recipeitem, recipeitem},
  115. {recipeitem, recipeitem, recipeitem}
  116. }
  117. })
  118. -- stair to original material recipe
  119. minetest.register_craft({
  120. output = recipeitem .. " 3",
  121. recipe = {
  122. {"stairs:stair_" .. subname, "stairs:stair_" .. subname},
  123. {"stairs:stair_" .. subname, "stairs:stair_" .. subname}
  124. }
  125. })
  126. set_burn(recipeitem, "stairs:stair_" .. subname, 0.75)
  127. end
  128. -- Node will be called stairs:slab_<subname>
  129. function stairs.register_slab(
  130. subname, recipeitem, groups, images, description, snds, wat)
  131. local slab_images = set_textures(images, wat)
  132. local new_groups = table.copy(groups)
  133. new_groups.slab = 1
  134. local def = minetest.registered_nodes[recipeitem] or {}
  135. minetest.register_node(":stairs:slab_" .. subname, {
  136. description = description,
  137. drawtype = "nodebox",
  138. tiles = slab_images,
  139. paramtype = "light",
  140. paramtype2 = "facedir",
  141. is_ground_content = false,
  142. use_texture_alpha = def.use_texture_alpha,
  143. light_source = def.light_source,
  144. sunlight_propagates = def.sunlight_propagates,
  145. groups = new_groups,
  146. sounds = snds or def.sounds,
  147. node_box = {
  148. type = "fixed",
  149. fixed = {-0.5, -0.5, -0.5, 0.5, 0, 0.5}
  150. },
  151. on_place = function(itemstack, placer, pointed_thing)
  152. return stair_place(
  153. itemstack, placer, pointed_thing, "stairs:slab_" .. subname)
  154. end
  155. })
  156. -- if no recipe item provided then skip craft recipes
  157. if not recipeitem then
  158. return
  159. end
  160. -- slab recipe
  161. minetest.register_craft({
  162. output = "stairs:slab_" .. subname .. " 6",
  163. recipe = {
  164. {recipeitem, recipeitem, recipeitem}
  165. }
  166. })
  167. -- slab to original material recipe
  168. minetest.register_craft({
  169. output = recipeitem,
  170. recipe = {
  171. {"stairs:slab_" .. subname},
  172. {"stairs:slab_" .. subname}
  173. }
  174. })
  175. set_burn(recipeitem, "stairs:slab_" .. subname, 0.5)
  176. end
  177. -- Node will be called stairs:stair_outer_<subname>
  178. function stairs.register_stair_outer(
  179. subname, recipeitem, groups, images, description, snds, wat, fdesc)
  180. local stair_images = set_textures(images, wat)
  181. local new_groups = table.copy(groups)
  182. new_groups.stair = 1
  183. local def = minetest.registered_nodes[recipeitem] or {}
  184. minetest.register_node(":stairs:stair_outer_" .. subname, {
  185. description = fdesc or "Outer " .. description,
  186. drawtype = "nodebox",
  187. tiles = stair_images,
  188. paramtype = "light",
  189. paramtype2 = "facedir",
  190. is_ground_content = false,
  191. use_texture_alpha = def.use_texture_alpha,
  192. light_source = def.light_source,
  193. sunlight_propagates = def.sunlight_propagates,
  194. groups = new_groups,
  195. sounds = snds or def.sounds,
  196. node_box = {
  197. type = "fixed",
  198. fixed = {
  199. {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
  200. {-0.5, 0, 0, 0, 0.5, 0.5}
  201. },
  202. },
  203. on_place = function(itemstack, placer, pointed_thing)
  204. return stair_place(
  205. itemstack, placer, pointed_thing, "stairs:stair_outer_" .. subname)
  206. end
  207. })
  208. -- add alias for old stairs redo name
  209. minetest.register_alias("stairs:corner_" .. subname,
  210. "stairs:stair_outer_" .. subname)
  211. -- if no recipe item provided then skip craft recipes
  212. if not recipeitem then
  213. return
  214. end
  215. -- corner stair recipe
  216. minetest.register_craft({
  217. output = "stairs:stair_outer_" .. subname .. " 6",
  218. recipe = {
  219. {"", "", ""},
  220. {"", recipeitem, ""},
  221. {recipeitem, recipeitem, recipeitem}
  222. },
  223. })
  224. -- corner stair to original material recipe
  225. minetest.register_craft({
  226. output = recipeitem .. " 2",
  227. recipe = {
  228. {"stairs:stair_outer_" .. subname, "stairs:stair_outer_" .. subname},
  229. {"stairs:stair_outer_" .. subname, "stairs:stair_outer_" .. subname}
  230. }
  231. })
  232. set_burn(recipeitem, "stairs:stair_outer_" .. subname, 0.625)
  233. end
  234. -- Node will be called stairs:stair_inner_<subname>
  235. function stairs.register_stair_inner(
  236. subname, recipeitem, groups, images, description, snds, wat, fdesc)
  237. local stair_images = set_textures(images, wat)
  238. local new_groups = table.copy(groups)
  239. new_groups.stair = 1
  240. local def = minetest.registered_nodes[recipeitem] or {}
  241. minetest.register_node(":stairs:stair_inner_" .. subname, {
  242. description = fdesc or "Inner " .. description,
  243. drawtype = "nodebox",
  244. tiles = stair_images,
  245. paramtype = "light",
  246. paramtype2 = "facedir",
  247. is_ground_content = false,
  248. use_texture_alpha = def.use_texture_alpha,
  249. light_source = def.light_source,
  250. sunlight_propagates = def.sunlight_propagates,
  251. groups = new_groups,
  252. sounds = snds or def.sounds,
  253. node_box = {
  254. type = "fixed",
  255. fixed = {
  256. {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
  257. {-0.5, 0, 0, 0.5, 0.5, 0.5},
  258. {-0.5, 0, -0.5, 0, 0.5, 0}
  259. }
  260. },
  261. on_place = function(itemstack, placer, pointed_thing)
  262. return stair_place(
  263. itemstack, placer, pointed_thing, "stairs:stair_inner_" .. subname)
  264. end,
  265. })
  266. -- add alias for old stairs redo name
  267. minetest.register_alias("stairs:invcorner_" .. subname,
  268. "stairs:stair_inner_" .. subname)
  269. -- if no recipe item provided then skip craft recipes
  270. if not recipeitem then
  271. return
  272. end
  273. -- inside corner stair recipe
  274. minetest.register_craft({
  275. output = "stairs:stair_inner_" .. subname .. " 9",
  276. recipe = {
  277. {recipeitem, recipeitem, ""},
  278. {recipeitem, recipeitem, recipeitem},
  279. {recipeitem, recipeitem, recipeitem},
  280. }
  281. })
  282. -- inside corner stair to original material recipe
  283. minetest.register_craft({
  284. output = recipeitem .. " 3",
  285. recipe = {
  286. {"stairs:stair_inner_" .. subname, "stairs:stair_inner_" .. subname},
  287. {"stairs:stair_inner_" .. subname, "stairs:stair_inner_" .. subname}
  288. }
  289. })
  290. set_burn(recipeitem, "stairs:stair_inner_" .. subname, 0.875)
  291. end
  292. -- Node will be called stairs:slope_<subname>
  293. function stairs.register_slope(
  294. subname, recipeitem, groups, images, description, snds, wat)
  295. local stair_images = set_textures(images, wat)
  296. local new_groups = table.copy(groups)
  297. new_groups.stair = 1
  298. local def = minetest.registered_nodes[recipeitem] or {}
  299. minetest.register_node(":stairs:slope_" .. subname, {
  300. description = description,
  301. drawtype = "mesh",
  302. mesh = "stairs_slope.obj",
  303. tiles = stair_images,
  304. paramtype = "light",
  305. paramtype2 = "facedir",
  306. is_ground_content = false,
  307. use_texture_alpha = def.use_texture_alpha,
  308. light_source = def.light_source,
  309. sunlight_propagates = def.sunlight_propagates,
  310. groups = new_groups,
  311. sounds = snds or def.sounds,
  312. selection_box = {
  313. type = "fixed",
  314. fixed = {
  315. {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
  316. {-0.5, 0, 0, 0.5, 0.5, 0.5},
  317. },
  318. },
  319. collision_box = {
  320. type = "fixed",
  321. fixed = {
  322. {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
  323. {-0.5, 0, 0, 0.5, 0.5, 0.5}
  324. },
  325. },
  326. on_place = function(itemstack, placer, pointed_thing)
  327. return stair_place(
  328. itemstack, placer, pointed_thing, "stairs:slope_" .. subname)
  329. end
  330. })
  331. -- slope recipe
  332. minetest.register_craft({
  333. output = "stairs:slope_" .. subname .. " 6",
  334. recipe = {
  335. {recipeitem, "", ""},
  336. {recipeitem, recipeitem, ""}
  337. }
  338. })
  339. -- slope to original material recipe
  340. minetest.register_craft({
  341. output = recipeitem,
  342. recipe = {
  343. {"stairs:slope_" .. subname, "stairs:slope_" .. subname}
  344. }
  345. })
  346. set_burn(recipeitem, "stairs:slope_" .. subname, 0.5)
  347. end
  348. -- Node will be called stairs:slope_inner_<subname>
  349. function stairs.register_slope_inner(
  350. subname, recipeitem, groups, images, description, snds, wat)
  351. local stair_images = set_textures(images, wat)
  352. local new_groups = table.copy(groups)
  353. new_groups.stair = 1
  354. local def = minetest.registered_nodes[recipeitem] or {}
  355. minetest.register_node(":stairs:slope_inner_" .. subname, {
  356. description = description,
  357. drawtype = "mesh",
  358. mesh = "stairs_slope_inner.obj",
  359. tiles = stair_images,
  360. paramtype = "light",
  361. paramtype2 = "facedir",
  362. is_ground_content = false,
  363. use_texture_alpha = def.use_texture_alpha,
  364. light_source = def.light_source,
  365. sunlight_propagates = def.sunlight_propagates,
  366. groups = new_groups,
  367. sounds = snds or def.sounds,
  368. selection_box = {
  369. type = "fixed",
  370. fixed = {
  371. {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
  372. {-0.5, 0, 0, 0.5, 0.5, 0.5},
  373. {-0.5, 0, -0.5, 0, 0.5, 0}
  374. }
  375. },
  376. collision_box = {
  377. type = "fixed",
  378. fixed = {
  379. {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
  380. {-0.5, 0, 0, 0.5, 0.5, 0.5},
  381. {-0.5, 0, -0.5, 0, 0.5, 0}
  382. }
  383. },
  384. on_place = function(itemstack, placer, pointed_thing)
  385. return stair_place(
  386. itemstack, placer, pointed_thing, "stairs:slope_inner_" .. subname)
  387. end
  388. })
  389. -- slope recipe
  390. minetest.register_craft({
  391. output = "stairs:slope_inner_" .. subname .. " 6",
  392. recipe = {
  393. {"", recipeitem, recipeitem},
  394. {recipeitem, recipeitem, recipeitem}
  395. }
  396. })
  397. -- slope to original material recipe
  398. minetest.register_craft({
  399. output = recipeitem,
  400. recipe = {
  401. {"stairs:slope_inner_" .. subname, "stairs:slope_inner_" .. subname}
  402. }
  403. })
  404. set_burn(recipeitem, "stairs:slope_inner_" .. subname, 0.5)
  405. end
  406. -- Node will be called stairs:slope_outer_<subname>
  407. function stairs.register_slope_outer(
  408. subname, recipeitem, groups, images, description, snds, wat)
  409. local stair_images = set_textures(images, wat)
  410. local new_groups = table.copy(groups)
  411. new_groups.stair = 1
  412. local def = minetest.registered_nodes[recipeitem] or {}
  413. minetest.register_node(":stairs:slope_outer_" .. subname, {
  414. description = description,
  415. drawtype = "mesh",
  416. mesh = "stairs_slope_outer.obj",
  417. tiles = stair_images,
  418. paramtype = "light",
  419. paramtype2 = "facedir",
  420. is_ground_content = false,
  421. use_texture_alpha = def.use_texture_alpha,
  422. light_source = def.light_source,
  423. sunlight_propagates = def.sunlight_propagates,
  424. groups = new_groups,
  425. sounds = snds or def.sounds,
  426. selection_box = {
  427. type = "fixed",
  428. fixed = {
  429. {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
  430. {-0.5, 0, 0, 0, 0.5, 0.5}
  431. },
  432. },
  433. collision_box = {
  434. type = "fixed",
  435. fixed = {
  436. {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
  437. {-0.5, 0, 0, 0, 0.5, 0.5}
  438. },
  439. },
  440. on_place = function(itemstack, placer, pointed_thing)
  441. return stair_place(
  442. itemstack, placer, pointed_thing, "stairs:slope_outer_" .. subname)
  443. end
  444. })
  445. -- slope recipe
  446. minetest.register_craft({
  447. output = "stairs:slope_outer_" .. subname .. " 6",
  448. recipe = {
  449. {"", "", recipeitem},
  450. {"", recipeitem, recipeitem}
  451. }
  452. })
  453. -- slope to original material recipe
  454. minetest.register_craft({
  455. output = recipeitem,
  456. recipe = {
  457. {"stairs:slope_outer_" .. subname, "stairs:slope_outer_" .. subname}
  458. }
  459. })
  460. set_burn(recipeitem, "stairs:slope_outer_" .. subname, 0.5)
  461. end
  462. -- Nodes will be called stairs:{stair,slab}_<subname>
  463. function stairs.register_stair_and_slab(
  464. subname, recipeitem, groups, images, desc_stair, desc_slab, sounds, wat)
  465. stairs.register_stair(
  466. subname, recipeitem, groups, images, desc_stair, sounds, wat)
  467. stairs.register_stair_inner(
  468. subname, recipeitem, groups, images, desc_stair, sounds, wat)
  469. stairs.register_stair_outer(
  470. subname, recipeitem, groups, images, desc_stair, sounds, wat)
  471. stairs.register_slab(
  472. subname, recipeitem, groups, images, desc_slab, sounds, wat)
  473. end
  474. -- Nodes will be called stairs:{stair,slab,corner,invcorner,slope}_<subname>
  475. function stairs.register_all(
  476. subname, recipeitem, groups, images, desc, snds, wat)
  477. stairs.register_stair(
  478. subname, recipeitem, groups, images, desc .. " Stair", snds, wat)
  479. stairs.register_slab(
  480. subname, recipeitem, groups, images, desc .. " Slab", snds, wat)
  481. stairs.register_stair_inner(
  482. subname, recipeitem, groups, images, desc .. " Stair", snds, wat)
  483. stairs.register_stair_outer(
  484. subname, recipeitem, groups, images, desc .. " Stair", snds, wat)
  485. stairs.register_slope(
  486. subname, recipeitem, groups, images, desc .. " Slope", snds, wat)
  487. stairs.register_slope_inner(
  488. subname, recipeitem, groups, images, desc .. " Slope", snds, wat)
  489. stairs.register_slope_outer(
  490. subname, recipeitem, groups, images, desc .. " Slope", snds, wat)
  491. end
  492. -- compatibility function for previous stairs:invcorner_<subname>
  493. function stairs.register_invcorner(
  494. subname, recipeitem, groups, images, desc, snds, wat)
  495. stairs.register_stair_inner(
  496. subname, recipeitem, groups, images, desc .. " Stair", snds, wat)
  497. end
  498. -- compatibility function for previous stairs:corner_<subname>
  499. function stairs.register_corner(
  500. subname, recipeitem, groups, images, desc, snds, wat)
  501. stairs.register_stair_outer(
  502. subname, recipeitem, groups, images, desc .. " Stair", snds, wat)
  503. end
  504. -- Register stairs after mods loaded
  505. minetest.register_on_mods_loaded(function()
  506. dofile(minetest.get_modpath("stairs") .. "/stairs.lua")
  507. end)
  508. print ("[MOD] Stairs Redo loaded")