stair_functions.lua 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. --Copied from MTG. Required as own file for color related changes.
  2. -- Load support for MT game translation.
  3. local function rotate_and_place(itemstack, placer, pointed_thing)
  4. local p0 = pointed_thing.under
  5. local p1 = pointed_thing.above
  6. local param2 = 0
  7. if placer then
  8. local placer_pos = placer:get_pos()
  9. if placer_pos then
  10. param2 = minetest.dir_to_facedir(vector.subtract(p1, placer_pos))
  11. end
  12. local finepos = minetest.pointed_thing_to_face_pos(placer, pointed_thing)
  13. local fpos = finepos.y % 1
  14. if p0.y - 1 == p1.y or (fpos > 0 and fpos < 0.5)
  15. or (fpos < -0.5 and fpos > -0.999999999) then
  16. param2 = param2 + 20
  17. if param2 == 21 then
  18. param2 = 23
  19. elseif param2 == 23 then
  20. param2 = 21
  21. end
  22. end
  23. end
  24. return minetest.item_place(itemstack, placer, pointed_thing, param2)
  25. end
  26. -- Register stair
  27. -- Node will be called stairs:stair_<subname>
  28. function color.register_stair(subname, images, palette, hex, worldaligntex)
  29. -- Set backface culling and world-aligned textures
  30. local stair_images = {}
  31. for i, image in ipairs(images) do
  32. if type(image) == 'string' then
  33. stair_images[i] = {
  34. name = image,
  35. backface_culling = true,
  36. }
  37. if worldaligntex then
  38. stair_images[i].align_style = 'world'
  39. end
  40. else
  41. stair_images[i] = table.copy(image)
  42. if stair_images[i].backface_culling == nil then
  43. stair_images[i].backface_culling = true
  44. end
  45. if worldaligntex and stair_images[i].align_style == nil then
  46. stair_images[i].align_style = 'world'
  47. end
  48. end
  49. end
  50. minetest.register_node(':stairs:stair_' .. subname, {
  51. description = subname..' stair',
  52. drawtype = 'nodebox',
  53. tiles = stair_images,
  54. palette = palette,
  55. color = hex,
  56. paramtype = 'light',
  57. paramtype2 = 'colorfacedir',
  58. is_ground_content = false,
  59. groups = {breakable=1, stainable=1},
  60. node_box = {
  61. type = 'fixed',
  62. fixed = {
  63. {-0.5, -0.5, -0.5, 0.5, 0.0, 0.5},
  64. {-0.5, 0.0, 0.0, 0.5, 0.5, 0.5},
  65. },
  66. },
  67. on_place = function(itemstack, placer, pointed_thing)
  68. if pointed_thing.type ~= 'node' then
  69. return itemstack
  70. end
  71. return rotate_and_place(itemstack, placer, pointed_thing)
  72. end,
  73. })
  74. end
  75. -- Register slab
  76. -- Node will be called stairs:slab_<subname>
  77. function color.register_slab(subname, images, palette, hex, worldaligntex)
  78. -- Set world-aligned textures
  79. local slab_images = {}
  80. for i, image in ipairs(images) do
  81. if type(image) == 'string' then
  82. slab_images[i] = {
  83. name = image,
  84. }
  85. if worldaligntex then
  86. slab_images[i].align_style = 'world'
  87. end
  88. else
  89. slab_images[i] = table.copy(image)
  90. if worldaligntex and image.align_style == nil then
  91. slab_images[i].align_style = 'world'
  92. end
  93. end
  94. end
  95. minetest.register_node(':stairs:slab_' .. subname, {
  96. description = subname..' slab',
  97. drawtype = 'nodebox',
  98. tiles = slab_images,
  99. palette = palette,
  100. color = hex,
  101. paramtype = 'light',
  102. paramtype2 = 'colorfacedir',
  103. is_ground_content = false,
  104. groups = {breakable=1, stainable=1, slab=1},
  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. local under = minetest.get_node(pointed_thing.under)
  111. local wield_item = itemstack:get_name()
  112. local player_name = placer and placer:get_player_name() or ''
  113. local creative_enabled = (creative and creative.is_enabled_for
  114. and creative.is_enabled_for(player_name))
  115. if under and under.name:find('^stairs:slab_') then
  116. -- place slab using under node orientation
  117. local dir = minetest.dir_to_facedir(vector.subtract(
  118. pointed_thing.above, pointed_thing.under), true)
  119. local p2 = under.param2
  120. -- Placing a slab on an upside down slab should make it right-side up.
  121. if p2 >= 20 and dir == 8 then
  122. p2 = p2 - 20
  123. -- same for the opposite case: slab below normal slab
  124. elseif p2 <= 3 and dir == 4 then
  125. p2 = p2 + 20
  126. end
  127. -- else attempt to place node with proper param2
  128. minetest.item_place_node(ItemStack(wield_item), placer, pointed_thing, p2)
  129. if not creative_enabled then
  130. itemstack:take_item()
  131. end
  132. return itemstack
  133. else
  134. return rotate_and_place(itemstack, placer, pointed_thing)
  135. end
  136. end,
  137. })
  138. end
  139. -- Register inner stair
  140. -- Node will be called stairs:stair_inner_<subname>
  141. function color.register_stair_inner(subname, images, palette, hex, worldaligntex)
  142. -- Set backface culling and world-aligned textures
  143. local stair_images = {}
  144. for i, image in ipairs(images) do
  145. if type(image) == 'string' then
  146. stair_images[i] = {
  147. name = image,
  148. backface_culling = true,
  149. }
  150. if worldaligntex then
  151. stair_images[i].align_style = 'world'
  152. end
  153. else
  154. stair_images[i] = table.copy(image)
  155. if stair_images[i].backface_culling == nil then
  156. stair_images[i].backface_culling = true
  157. end
  158. if worldaligntex and stair_images[i].align_style == nil then
  159. stair_images[i].align_style = 'world'
  160. end
  161. end
  162. end
  163. minetest.register_node(':stairs:stair_inner_' .. subname, {
  164. description = subname..' inner stair',
  165. drawtype = 'nodebox',
  166. tiles = stair_images,
  167. palette = palette,
  168. color = hex,
  169. paramtype = 'light',
  170. paramtype2 = 'colorfacedir',
  171. is_ground_content = false,
  172. groups = {breakable=1, stainable=1},
  173. node_box = {
  174. type = 'fixed',
  175. fixed = {
  176. {-0.5, -0.5, -0.5, 0.5, 0.0, 0.5},
  177. {-0.5, 0.0, 0.0, 0.5, 0.5, 0.5},
  178. {-0.5, 0.0, -0.5, 0.0, 0.5, 0.0},
  179. },
  180. },
  181. on_place = function(itemstack, placer, pointed_thing)
  182. if pointed_thing.type ~= 'node' then
  183. return itemstack
  184. end
  185. return rotate_and_place(itemstack, placer, pointed_thing)
  186. end,
  187. })
  188. end
  189. -- Register outer stair
  190. -- Node will be called stairs:stair_outer_<subname>
  191. function color.register_stair_outer(subname, images, palette, hex, worldaligntex)
  192. -- Set backface culling and world-aligned textures
  193. local stair_images = {}
  194. for i, image in ipairs(images) do
  195. if type(image) == 'string' then
  196. stair_images[i] = {
  197. name = image,
  198. backface_culling = true,
  199. }
  200. if worldaligntex then
  201. stair_images[i].align_style = 'world'
  202. end
  203. else
  204. stair_images[i] = table.copy(image)
  205. if stair_images[i].backface_culling == nil then
  206. stair_images[i].backface_culling = true
  207. end
  208. if worldaligntex and stair_images[i].align_style == nil then
  209. stair_images[i].align_style = 'world'
  210. end
  211. end
  212. end
  213. minetest.register_node(':stairs:stair_outer_' .. subname, {
  214. description = subname..' outer stair',
  215. drawtype = 'nodebox',
  216. tiles = stair_images,
  217. palette = palette,
  218. color = hex,
  219. paramtype = 'light',
  220. paramtype2 = 'colorfacedir',
  221. is_ground_content = false,
  222. groups = {breakable=1, stainable=1},
  223. node_box = {
  224. type = 'fixed',
  225. fixed = {
  226. {-0.5, -0.5, -0.5, 0.5, 0.0, 0.5},
  227. {-0.5, 0.0, 0.0, 0.0, 0.5, 0.5},
  228. },
  229. },
  230. on_place = function(itemstack, placer, pointed_thing)
  231. if pointed_thing.type ~= 'node' then
  232. return itemstack
  233. end
  234. return rotate_and_place(itemstack, placer, pointed_thing)
  235. end,
  236. })
  237. end
  238. -- Stair/slab registration function.
  239. -- Nodes will be called stairs:{stair,slab}_<subname>
  240. function color.register_stair_and_slab(subname, images, palette, hex, worldaligntex)
  241. color.register_stair(subname, images, palette, hex, worldaligntex)
  242. color.register_stair_inner(subname, images, palette, hex, worldaligntex)
  243. color.register_stair_outer(subname, images, palette, hex, worldaligntex)
  244. color.register_slab(subname, images, palette, hex, worldaligntex)
  245. end