init.lua 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. -- Minetest 0.4 mod: stairs
  2. -- See README.txt for licensing and other information.
  3. -- Localize for performance.
  4. local math_floor = math.floor
  5. local math_random = math.random
  6. -- Global namespace for functions
  7. local S = function(str) return str end
  8. stairs = {}
  9. circular_saw = circular_saw or {}
  10. circular_saw.known_nodes = circular_saw.known_nodes or {}
  11. -- Also used by walls.
  12. function stairs.setup_nodedef_callbacks(subname, def)
  13. if string.find(subname, "ice") or string.find(subname, "snow") then
  14. assert(not def.on_construct)
  15. def.on_construct = function(pos)
  16. if rc.ice_melts_at_pos(pos) then
  17. minetest.get_node_timer(pos):start(math_random(ice.minmax_time()))
  18. end
  19. end
  20. assert(not def.on_timer)
  21. def.on_timer = function(pos, elapsed)
  22. if rc.ice_melts_at_pos(pos) then
  23. minetest.add_node(pos, {name="default:water_flowing"})
  24. end
  25. end
  26. end
  27. end
  28. function stairs.rotate_and_place(itemstack, placer, pt)
  29. if pt.type == "node" then
  30. local node = minetest.get_node(pt.under)
  31. if node.name == itemstack:get_name() then
  32. local param2 = node.param2
  33. return minetest.item_place(itemstack, placer, pt, param2)
  34. end
  35. end
  36. return minetest.rotate_node(itemstack, placer, pt)
  37. end
  38. dofile(minetest.get_modpath("stairs") .. "/slopes.lua")
  39. local slabs_defs = {
  40. ["_quarter"] = {num=4, light=0.25, is_flat=true},
  41. ["_three_quarter"] = {num=12, light=0.75, is_flat=true},
  42. ["_1"] = {num=1, light=1/16, is_flat=true},
  43. ["_2"] = {num=2, light=2/16, is_flat=true},
  44. ["_14"] = {num=14, light=14/16, is_flat=true},
  45. ["_15"] = {num=15, light=15/16, is_flat=true},
  46. ["_two_sides"] = {
  47. nodebox = {
  48. { -0.5, -0.5, -0.5, 0.5, -7/16, 7/16 },
  49. { -0.5, -0.5, 7/16, 0.5, 0.5, 0.5 },
  50. }
  51. },
  52. ["_three_sides"] = {
  53. nodebox = {
  54. { -7/16, -0.5, -0.5, 0.5, -7/16, 7/16 },
  55. { -7/16, -0.5, 7/16, 0.5, 0.5, 0.5 },
  56. { -0.5, -0.5, -0.5, -7/16, 0.5, 0.5 },
  57. }
  58. },
  59. ["_three_sides_u"] = {
  60. nodebox = {
  61. { -0.5, -0.5, -0.5, 0.5, 0.5, -7/16 },
  62. { -0.5, -0.5, -7/16, 0.5, -7/16, 7/16 },
  63. { -0.5, -0.5, 7/16, 0.5, 0.5, 0.5 },
  64. }
  65. },
  66. ["_four_sides"] = {
  67. nodebox = {
  68. { -0.5, -0.5, -0.5, 0.5, 0.5, -7/16 },
  69. { -0.5, -0.5, -7/16, 0.5, -7/16, 7/16 },
  70. { -0.5, -0.5, 7/16, 0.5, 0.5, 0.5 },
  71. { -0.5, -7/16, -7/16, -7/16, 0.5, 7/16 },
  72. }
  73. },
  74. ["_hole"] = {
  75. nodebox = {
  76. {-0.5, -0.5, -0.5, 0.5, 0.5, -7/16},
  77. {-0.5, -0.5, 0.5, 0.5, 0.5, 7/16},
  78. {-0.5, -0.5, -7/16, -7/16, 0.5, 7/16},
  79. {0.5, -0.5, -7/16, 7/16, 0.5, 7/16},
  80. }
  81. },
  82. ["_two_opposite"] = {
  83. nodebox = {
  84. {-0.5, -0.5, -0.5, 0.5, 0.5, -7/16},
  85. {-0.5, -0.5, 0.5, 0.5, 0.5, 7/16},
  86. }
  87. },
  88. ["_pit"] = {
  89. nodebox = {
  90. { -0.5, -0.5, -0.5, 0.5, 0.5, -7/16 },
  91. { -0.5, -0.5, -7/16, 0.5, -7/16, 7/16 },
  92. { -0.5, -0.5, 7/16, 0.5, 0.5, 0.5 },
  93. { -0.5, -7/16, -7/16, -7/16, 0.5, 7/16 },
  94. { -7/16, 0.5, -7/16, 0.5, 7/16, 7/16 },
  95. }
  96. },
  97. ["_pit_half"] = {
  98. nodebox = {
  99. { -0.5, -0.5, -0.5, 0.0, 0.5, -7/16 },
  100. { -0.5, -0.5, -7/16, 0.0, -7/16, 7/16 },
  101. { -0.5, -0.5, 7/16, 0.0, 0.5, 0.5 },
  102. { -0.5, -7/16, -7/16, -7/16, 0.5, 7/16 },
  103. { -7/16, 0.5, -7/16, 0.0, 7/16, 7/16 },
  104. }
  105. },
  106. ["_hole_half"] = {
  107. nodebox = {
  108. {-0.5, -0.5, -0.5, 0.5, 0, -7/16},
  109. {-0.5, -0.5, 0.5, 0.5, 0, 7/16},
  110. {-0.5, -0.5, -7/16, -7/16, 0, 7/16},
  111. {0.5, -0.5, -7/16, 7/16, 0, 7/16},
  112. }
  113. },
  114. }
  115. function stairs.register_extra_slabs(subname, recipeitem, groups, images, description, sounds)
  116. local stair_images = {}
  117. for i, image in ipairs(images) do
  118. if type(image) == "string" then
  119. stair_images[i] = {
  120. name = image,
  121. backface_culling = true,
  122. }
  123. elseif image.backface_culling == nil then -- override using any other value
  124. stair_images[i] = table.copy(image)
  125. stair_images[i].backface_culling = true
  126. end
  127. end
  128. local defs = table.copy(slabs_defs)
  129. -- Do not modify function argument.
  130. local groups = table.copy(groups)
  131. groups.not_in_craft_guide = 1
  132. groups.stairs_slab = 1
  133. groups.stairs_node = 1
  134. local ndef = minetest.registered_items[recipeitem]
  135. assert(ndef)
  136. for alternate, num in pairs(defs) do
  137. local def
  138. if num.num then
  139. def = {
  140. node_box = {
  141. type = "fixed",
  142. fixed = {-0.5, -0.5, -0.5, 0.5, (num.num/16)-0.5, 0.5},
  143. }
  144. }
  145. elseif num.nodebox then
  146. def = {
  147. node_box = {
  148. type = "fixed",
  149. fixed = num.nodebox,
  150. }
  151. }
  152. end
  153. def.drawtype = "nodebox"
  154. def.paramtype = "light"
  155. def.paramtype2 = "facedir"
  156. def.on_place = function(...) return stairs.rotate_and_place(...) end
  157. def.groups = groups
  158. def.sounds = sounds
  159. def.description = description
  160. def.tiles = stair_images
  161. def.light_source = math.ceil(ndef.light_source*(num.light or 0))
  162. -- Only for flat slabs.
  163. if num.is_flat then
  164. def.movement_speed_depends = recipeitem
  165. end
  166. stairs.setup_nodedef_callbacks(subname, def)
  167. minetest.register_node(":stairs:slab_" .. subname .. alternate, def)
  168. end
  169. if recipeitem then
  170. circular_saw.known_nodes[recipeitem] = {"stairs", subname}
  171. end
  172. end
  173. local stairs_defs = {
  174. ["_half"] = {
  175. node_box = {
  176. type = "fixed",
  177. fixed = {
  178. {-0.5, -0.5, -0.5, 0, 0, 0.5},
  179. {-0.5, 0, 0, 0, 0.5, 0.5},
  180. },
  181. },
  182. light=3/8,
  183. },
  184. ["_right_half" ]= {
  185. node_box = {
  186. type = "fixed",
  187. fixed = {
  188. {0, -0.5, -0.5, 0.5, 0, 0.5},
  189. {0, 0, 0, 0.5, 0.5, 0.5},
  190. },
  191. },
  192. light=3/8,
  193. },
  194. ["_half_1"] = {
  195. node_box = {
  196. type = "fixed",
  197. fixed = {
  198. {-0.5, -0.5, -0.5, -7/16, 0, 0.5},
  199. {-0.5, 0, 0, -7/16, 0.5, 0.5},
  200. },
  201. },
  202. light=1/8,
  203. },
  204. ["_right_half_1" ]= {
  205. node_box = {
  206. type = "fixed",
  207. fixed = {
  208. {7/16, -0.5, -0.5, 0.5, 0, 0.5},
  209. {7/16, 0, 0, 0.5, 0.5, 0.5},
  210. },
  211. },
  212. light=1/8,
  213. },
  214. ["_inner"] = {
  215. node_box = {
  216. type = "fixed",
  217. fixed = {
  218. {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
  219. {-0.5, 0, 0, 0.5, 0.5, 0.5},
  220. {-0.5, 0, -0.5, 0, 0.5, 0},
  221. },
  222. },
  223. light=7/8,
  224. },
  225. ["_outer"] = {
  226. node_box = {
  227. type = "fixed",
  228. fixed = {
  229. {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
  230. {-0.5, 0, 0, 0, 0.5, 0.5},
  231. },
  232. },
  233. light=5/8,
  234. },
  235. ["_alt"] = {
  236. node_box = {
  237. type = "fixed",
  238. fixed = {
  239. {-0.5, -0.5, -0.5, 0.5, 0, 0},
  240. {-0.5, 0, 0, 0.5, 0.5, 0.5},
  241. },
  242. },
  243. light=1/2,
  244. },
  245. ["_alt_1"] = {
  246. node_box = {
  247. type = "fixed",
  248. fixed = {
  249. {-0.5, -0.0625, -0.5, 0.5, 0, 0},
  250. {-0.5, 0.4375, 0, 0.5, 0.5, 0.5},
  251. },
  252. },
  253. light=1/16,
  254. },
  255. ["_alt_5"] = {
  256. node_box = {
  257. type = "fixed",
  258. fixed = {
  259. {-0.5, -0.0625, 0.0, 0.5, 0, 0.5},
  260. {-0.5, 0.4375, 0, 0.5, 0.5, 0.5},
  261. },
  262. },
  263. light=1/16,
  264. },
  265. ["_alt_6"] = {
  266. node_box = {
  267. type = "fixed",
  268. fixed = {
  269. {-0.5, -0.0625, -0.5, 0.5, 0, 0.5},
  270. {-0.5, 0.4375, -0.5, 0.5, 0.5, 0.5},
  271. },
  272. },
  273. light=1/16,
  274. },
  275. ["_alt_2"] = {
  276. node_box = {
  277. type = "fixed",
  278. fixed = {
  279. {-0.5, -0.125, -0.5, 0.5, 0, 0},
  280. {-0.5, 0.375, 0, 0.5, 0.5, 0.5},
  281. },
  282. },
  283. light=2/16,
  284. },
  285. ["_alt_4"] = {
  286. node_box = {
  287. type = "fixed",
  288. fixed = {
  289. {-0.5, -0.25, -0.5, 0.5, 0, 0},
  290. {-0.5, 0.25, 0, 0.5, 0.5, 0.5},
  291. },
  292. },
  293. light=4/16,
  294. },
  295. }
  296. function stairs.register_extra_stairs(subname, recipeitem, groups, images, description, sounds)
  297. local stair_images = {}
  298. for i, image in ipairs(images) do
  299. if type(image) == "string" then
  300. stair_images[i] = {
  301. name = image,
  302. backface_culling = true,
  303. }
  304. elseif image.backface_culling == nil then -- override using any other value
  305. stair_images[i] = table.copy(image)
  306. stair_images[i].backface_culling = true
  307. end
  308. end
  309. local defs = table.copy(stairs_defs)
  310. -- Do not modify function argument.
  311. local groups = table.copy(groups)
  312. groups.not_in_craft_guide = 1
  313. groups.stairs_stair = 1
  314. groups.stairs_node = 1
  315. local ndef = minetest.registered_items[recipeitem]
  316. assert(ndef)
  317. for alternate, def in pairs(defs) do
  318. def.drawtype = "nodebox"
  319. def.paramtype = "light"
  320. def.paramtype2 = "facedir"
  321. def.on_place = function(...) return stairs.rotate_and_place(...) end
  322. def.groups = groups
  323. def.sounds = sounds
  324. def.description = description
  325. def.tiles = stair_images
  326. def.light_source = math.ceil(ndef.light_source*def.light)
  327. def.light = nil
  328. stairs.setup_nodedef_callbacks(subname, def)
  329. minetest.register_node(":stairs:stair_" ..subname..alternate, def)
  330. if recipeitem then
  331. if alternate == "_inner" then
  332. minetest.register_craft({
  333. output = "stairs:stair_" .. subname .. alternate .. ' 8',
  334. recipe = {
  335. {recipeitem, recipeitem, recipeitem},
  336. {recipeitem, recipeitem, ''},
  337. {recipeitem, '', recipeitem},
  338. },
  339. })
  340. elseif alternate == "_outer" then
  341. minetest.register_craft({
  342. output = "stairs:stair_" .. subname .. alternate .. ' 8',
  343. recipe = {
  344. {recipeitem, recipeitem, recipeitem},
  345. {recipeitem, '', ''},
  346. {recipeitem, '', ''},
  347. },
  348. })
  349. end
  350. end
  351. end
  352. minetest.register_alias("stairs:stair_" ..subname.. "_bottom", "stairs:stair_" ..subname)
  353. if recipeitem then
  354. circular_saw.known_nodes[recipeitem] = {"stairs", subname}
  355. end
  356. end
  357. local panels_defs = {
  358. [""] = {
  359. node_box = {
  360. type = "fixed",
  361. fixed = {-0.5, -0.5, 0, 0.5, 0, 0.5},
  362. },
  363. light=1/4,
  364. },
  365. ["_1"] = {
  366. node_box = {
  367. type = "fixed",
  368. fixed = {-0.5, -0.5, 0, 0.5, -0.4375, 0.5},
  369. },
  370. light=1/32,
  371. },
  372. ["_2"] = {
  373. node_box = {
  374. type = "fixed",
  375. fixed = {-0.5, -0.5, 0, 0.5, -0.375, 0.5},
  376. },
  377. light=2/32,
  378. },
  379. ["_4"] = {
  380. node_box = {
  381. type = "fixed",
  382. fixed = {-0.5, -0.5, 0, 0.5, -0.25, 0.5},
  383. },
  384. light=4/32,
  385. },
  386. ["_12"] = {
  387. node_box = {
  388. type = "fixed",
  389. fixed = {-0.5, -0.5, 0, 0.5, 0.25, 0.5},
  390. },
  391. light=12/32,
  392. },
  393. ["_14"] = {
  394. node_box = {
  395. type = "fixed",
  396. fixed = {-0.5, -0.5, 0, 0.5, 0.375, 0.5},
  397. },
  398. light=14/32,
  399. },
  400. ["_15"] = {
  401. node_box = {
  402. type = "fixed",
  403. fixed = {-0.5, -0.5, 0, 0.5, 0.4375, 0.5},
  404. },
  405. light=15/32,
  406. },
  407. ["_16"] = {
  408. node_box = {
  409. type = "fixed",
  410. fixed = {-0.5, -0.5, 0, 0.5, 0.5, 0.5},
  411. },
  412. light=16/32,
  413. }
  414. }
  415. function stairs.register_panel(subname, recipeitem, groups, images, description, sounds)
  416. local stair_images = {}
  417. for i, image in ipairs(images) do
  418. if type(image) == "string" then
  419. stair_images[i] = {
  420. name = image,
  421. backface_culling = true,
  422. }
  423. elseif image.backface_culling == nil then -- override using any other value
  424. stair_images[i] = table.copy(image)
  425. stair_images[i].backface_culling = true
  426. end
  427. end
  428. local defs = table.copy(panels_defs)
  429. -- Do not modify function argument.
  430. local groups = table.copy(groups)
  431. groups.not_in_craft_guide = 1
  432. groups.stairs_panel = 1
  433. groups.stairs_node = 1
  434. local ndef = minetest.registered_items[recipeitem]
  435. assert(ndef)
  436. for alternate, def in pairs(defs) do
  437. def.drawtype = "nodebox"
  438. def.paramtype = "light"
  439. def.paramtype2 = "facedir"
  440. def.on_place = function(...) return stairs.rotate_and_place(...) end
  441. def.groups = groups
  442. def.sounds = sounds
  443. def.description = description
  444. def.tiles = stair_images
  445. def.light_source = math.ceil(ndef.light_source*def.light)
  446. def.light = nil
  447. stairs.setup_nodedef_callbacks(subname, def)
  448. minetest.register_node(":stairs:panel_" ..subname..alternate, def)
  449. end
  450. minetest.register_alias("stairs:panel_" ..subname.. "_bottom", "stairs:panel_" ..subname)
  451. if recipeitem then
  452. circular_saw.known_nodes[recipeitem] = {"stairs", subname}
  453. end
  454. end
  455. local microblocks_defs = {
  456. [""] = {
  457. node_box = {
  458. type = "fixed",
  459. fixed = {-0.5, -0.5, 0, 0, 0, 0.5},
  460. },
  461. light=8/64,
  462. },
  463. ["_c"] = {
  464. node_box = {
  465. type = "fixed",
  466. fixed = {-0.25, -0.5, -0.25, 0.25, 0, 0.25},
  467. },
  468. light=8/64,
  469. },
  470. ["_1c"] = {
  471. node_box = {
  472. type = "fixed",
  473. fixed = {-0.25, -0.5, -0.25, 0.25, -0.4375, 0.25},
  474. },
  475. light=1/64,
  476. },
  477. ["_1"] = {
  478. node_box = {
  479. type = "fixed",
  480. fixed = {-0.5, -0.5, 0, 0, -0.4375, 0.5},
  481. },
  482. light=1/64,
  483. },
  484. ["_2"] = {
  485. node_box = {
  486. type = "fixed",
  487. fixed = {-0.5, -0.5, 0, 0, -0.375, 0.5},
  488. },
  489. light=2/64,
  490. },
  491. ["_4"] = {
  492. node_box = {
  493. type = "fixed",
  494. fixed = {-0.5, -0.5, 0, 0, -0.25, 0.5},
  495. },
  496. light=4/64,
  497. },
  498. ["_12"] = {
  499. node_box = {
  500. type = "fixed",
  501. fixed = {-0.5, -0.5, 0, 0, 0.25, 0.5},
  502. },
  503. light=12/64,
  504. },
  505. ["_14"] = {
  506. node_box = {
  507. type = "fixed",
  508. fixed = {-0.5, -0.5, 0, 0, 0.375, 0.5},
  509. },
  510. light=14/64,
  511. },
  512. ["_15"] = {
  513. node_box = {
  514. type = "fixed",
  515. fixed = {-0.5, -0.5, 0, 0, 0.4375, 0.5},
  516. },
  517. light=15/64,
  518. },
  519. ["_16"] = {
  520. node_box = {
  521. type = "fixed",
  522. fixed = {-0.5, -0.5, 0, 0, 0.5, 0.5},
  523. },
  524. light=16/64,
  525. },
  526. ["_1s"] = {
  527. node_box = {
  528. type = "fixed",
  529. fixed = {
  530. {-0.5, -0.5, -0.5, 0, -0.4375, 0},
  531. {0, -0.5, 0, 0.5, -0.4375, 0.5},
  532. },
  533. },
  534. light=2/64,
  535. },
  536. ["_16s"] = {
  537. node_box = {
  538. type = "fixed",
  539. fixed = {
  540. {-0.5, -0.5, -0.5, 0, 0, 0},
  541. {0, -0.5, 0, 0.5, 0, 0.5},
  542. },
  543. },
  544. light=16/64,
  545. }
  546. }
  547. function stairs.register_micro(subname, recipeitem, groups, images, description, sounds)
  548. local stair_images = {}
  549. for i, image in ipairs(images) do
  550. if type(image) == "string" then
  551. stair_images[i] = {
  552. name = image,
  553. backface_culling = true,
  554. }
  555. elseif image.backface_culling == nil then -- override using any other value
  556. stair_images[i] = table.copy(image)
  557. stair_images[i].backface_culling = true
  558. end
  559. end
  560. local defs = table.copy(microblocks_defs)
  561. -- Do not modify function argument.
  562. local groups = table.copy(groups)
  563. groups.not_in_craft_guide = 1
  564. groups.stairs_microblock = 1
  565. groups.stairs_node = 1
  566. local ndef = minetest.registered_items[recipeitem]
  567. assert(ndef)
  568. for alternate, def in pairs(defs) do
  569. def.drawtype = "nodebox"
  570. def.paramtype = "light"
  571. def.paramtype2 = "facedir"
  572. def.on_place = function(...) return stairs.rotate_and_place(...) end
  573. def.groups = groups
  574. def.sounds = sounds
  575. def.description = description
  576. def.tiles = stair_images
  577. def.light_source = math.ceil(ndef.light_source*def.light)
  578. def.light = nil
  579. stairs.setup_nodedef_callbacks(subname, def)
  580. minetest.register_node(":stairs:micro_" .. subname .. alternate, def)
  581. end
  582. minetest.register_alias("stairs:micro_" ..subname.. "_bottom", "stairs:micro_" ..subname)
  583. if recipeitem then
  584. circular_saw.known_nodes[recipeitem] = {"stairs", subname}
  585. end
  586. end
  587. -- Register aliases for new pine node names
  588. minetest.register_alias("stairs:stair_pinewood", "stairs:stair_pine_wood")
  589. minetest.register_alias("stairs:slab_pinewood", "stairs:slab_pine_wood")
  590. -- Register stairs.
  591. -- Node will be called stairs:stair_<subname>
  592. function stairs.register_stair(subname, recipeitem, groups, images, description, sounds)
  593. local stair_images = {}
  594. for i, image in ipairs(images) do
  595. if type(image) == "string" then
  596. stair_images[i] = {
  597. name = image,
  598. backface_culling = true,
  599. }
  600. elseif image.backface_culling == nil then -- override using any other value
  601. stair_images[i] = table.copy(image)
  602. stair_images[i].backface_culling = true
  603. end
  604. end
  605. -- Do not modify function argument.
  606. groups = table.copy(groups)
  607. groups.stair = 1
  608. groups.stairs_stair = 1
  609. groups.not_in_craft_guide = 1
  610. groups.stairs_node = 1
  611. local ndef = minetest.registered_items[recipeitem]
  612. assert(ndef)
  613. local stair_def = {
  614. description = description,
  615. drawtype = "mesh",
  616. mesh = "stairs_stair.obj",
  617. tiles = stair_images,
  618. paramtype = "light",
  619. paramtype2 = "facedir",
  620. is_ground_content = false,
  621. groups = groups,
  622. sounds = sounds,
  623. light_source = math.ceil(ndef.light_source*0.75),
  624. selection_box = {
  625. type = "fixed",
  626. fixed = {
  627. {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
  628. {-0.5, 0, 0, 0.5, 0.5, 0.5},
  629. },
  630. },
  631. collision_box = {
  632. type = "fixed",
  633. fixed = {
  634. {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
  635. {-0.5, 0, 0, 0.5, 0.5, 0.5},
  636. },
  637. },
  638. on_place = function(itemstack, placer, pointed_thing)
  639. if pointed_thing.type ~= "node" then
  640. return itemstack
  641. end
  642. local p0 = pointed_thing.under
  643. local p1 = pointed_thing.above
  644. local param2 = 0
  645. local placer_pos = placer:getpos()
  646. if placer_pos then
  647. local dir = {
  648. x = p1.x - placer_pos.x,
  649. y = p1.y - placer_pos.y,
  650. z = p1.z - placer_pos.z
  651. }
  652. param2 = minetest.dir_to_facedir(dir)
  653. end
  654. if p0.y - 1 == p1.y then
  655. param2 = param2 + 20
  656. if param2 == 21 then
  657. param2 = 23
  658. elseif param2 == 23 then
  659. param2 = 21
  660. end
  661. end
  662. return minetest.item_place(itemstack, placer, pointed_thing, param2)
  663. end,
  664. }
  665. stairs.setup_nodedef_callbacks(subname, stair_def)
  666. minetest.register_node(":stairs:stair_" .. subname, stair_def)
  667. if recipeitem then
  668. assert(type(recipeitem) == "string")
  669. circular_saw.known_nodes[recipeitem] = {"stairs", subname}
  670. -- Use stairs to craft full blocks again (1:1)
  671. minetest.register_craft({
  672. output = recipeitem .. ' 3',
  673. recipe = {
  674. {'stairs:stair_' .. subname, 'stairs:stair_' .. subname},
  675. {'stairs:stair_' .. subname, 'stairs:stair_' .. subname},
  676. },
  677. })
  678. -- Recipe matches appearence in inventory
  679. minetest.register_craft({
  680. output = 'stairs:stair_' .. subname .. ' 8',
  681. recipe = {
  682. {"", "", recipeitem},
  683. {"", recipeitem, recipeitem},
  684. {recipeitem, recipeitem, recipeitem},
  685. },
  686. })
  687. end
  688. end
  689. -- Slab facedir to placement 6d matching table
  690. local slab_trans_dir = {[0] = 8, 0, 2, 1, 3, 4}
  691. -- Slab facedir when placing initial slab against other surface
  692. local slab_trans_dir_place = {[0] = 0, 20, 12, 16, 4, 8}
  693. -- Register slabs.
  694. -- Node will be called stairs:slab_<subname>
  695. function stairs.register_slab(subname, recipeitem, groups, images, description, sounds)
  696. -- Do not modify function argument.
  697. groups = table.copy(groups)
  698. groups.slab = 1
  699. groups.stairs_slab = 1
  700. groups.not_in_craft_guide = 1
  701. groups.stairs_node = 1
  702. local ndef = minetest.registered_items[recipeitem]
  703. assert(ndef)
  704. local slab_def = {
  705. description = description,
  706. drawtype = "nodebox",
  707. tiles = images,
  708. paramtype = "light",
  709. paramtype2 = "facedir",
  710. is_ground_content = false,
  711. groups = groups,
  712. sounds = sounds,
  713. movement_speed_depends = recipeitem,
  714. light_source = math.ceil(ndef.light_source*0.5),
  715. node_box = {
  716. type = "fixed",
  717. fixed = {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
  718. },
  719. on_place = function(itemstack, placer, pointed_thing)
  720. local under = minetest.get_node(pointed_thing.under)
  721. local wield_item = itemstack:get_name()
  722. if under and wield_item == under.name then
  723. -- place slab using under node orientation
  724. local dir = minetest.dir_to_facedir(vector.subtract(
  725. pointed_thing.above, pointed_thing.under), true)
  726. local p2 = under.param2
  727. -- combine two slabs if possible
  728. if slab_trans_dir[math_floor(p2 / 4)] == dir then
  729. if not recipeitem then
  730. return itemstack
  731. end
  732. local player_name = placer:get_player_name()
  733. if minetest.is_protected(pointed_thing.under, player_name) and not
  734. minetest.check_player_privs(placer, "protection_bypass") then
  735. minetest.record_protection_violation(pointed_thing.under,
  736. player_name)
  737. return
  738. end
  739. minetest.add_node(pointed_thing.under, {name = recipeitem, param2 = p2})
  740. if not minetest.setting_getbool("creative_mode") then
  741. itemstack:take_item()
  742. end
  743. return itemstack
  744. end
  745. -- Placing a slab on an upside down slab should make it right-side up.
  746. if p2 >= 20 and dir == 8 then
  747. p2 = p2 - 20
  748. -- same for the opposite case: slab below normal slab
  749. elseif p2 <= 3 and dir == 4 then
  750. p2 = p2 + 20
  751. end
  752. -- else attempt to place node with proper param2
  753. minetest.item_place_node(ItemStack(wield_item), placer, pointed_thing, p2)
  754. if not minetest.setting_getbool("creative_mode") then
  755. itemstack:take_item()
  756. end
  757. return itemstack
  758. else
  759. -- place slab using look direction of player
  760. local dir = minetest.dir_to_wallmounted(vector.subtract(
  761. pointed_thing.above, pointed_thing.under), true)
  762. local rot = slab_trans_dir_place[dir]
  763. if rot == 0 or rot == 20 then
  764. rot = rot + minetest.dir_to_facedir(placer:get_look_dir())
  765. end
  766. return minetest.item_place(itemstack, placer, pointed_thing, rot)
  767. end
  768. end,
  769. }
  770. stairs.setup_nodedef_callbacks(subname, slab_def)
  771. minetest.register_node(":stairs:slab_" .. subname, slab_def)
  772. if recipeitem then
  773. -- Use 2 slabs to craft a full block again (1:1)
  774. minetest.register_craft({
  775. output = recipeitem,
  776. recipe = {
  777. {'stairs:slab_' .. subname},
  778. {'stairs:slab_' .. subname},
  779. },
  780. })
  781. minetest.register_craft({
  782. output = 'stairs:slab_' .. subname .. ' 6',
  783. recipe = {
  784. {recipeitem, recipeitem, recipeitem},
  785. },
  786. })
  787. end
  788. end
  789. -- Stair/slab registration function.
  790. -- Now includes all extra blocks.
  791. function stairs.register_stair_and_slab(subname, recipeitem, groups, images, desc, sounds)
  792. stairs.register_micro(subname, recipeitem, groups, images, desc .. " Microblock", sounds)
  793. stairs.register_panel(subname, recipeitem, groups, images, desc .. " Panel", sounds)
  794. stairs.register_stair(subname, recipeitem, groups, images, desc .. " Stair", sounds)
  795. stairs.register_extra_stairs(subname, recipeitem, groups, images, desc .. " Stair", sounds)
  796. stairs.register_slab(subname, recipeitem, groups, images, desc .. " Slab", sounds)
  797. stairs.register_extra_slabs(subname, recipeitem, groups, images, desc .. " Slab", sounds)
  798. stairs.register_slopes(subname, recipeitem, groups, images, desc .. " Slope", sounds)
  799. end