init.lua 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. -- lpanes: leaded glass panes -- from xPanes mod by xyz, customized by davedevils, reworked and converted to leaded/stained panes by Britt. Textures by Britt
  2. --Leaded glass uses a 'leading' (pronounced "led-ing") material to secure the glass pieces in place. This is traditionally the metal lead. Specify which 'leading' metal will be used in crafting the panes. Lead or Tin might be appropriate, but for the default game, these are not available:
  3. local leadingMetal = "default:steel_ingot"
  4. local function split(string, separator)
  5. local separator, fields = separator or ":", {}
  6. local pattern = string.format("([^%s]+)", separator)
  7. self:gsub(pattern, function(c) fields[#fields+1] = c end)
  8. return fields
  9. end
  10. local directions = {
  11. {x = 1, y = 0, z = 0},
  12. {x = 0, y = 0, z = 1},
  13. {x = -1, y = 0, z = 0},
  14. {x = 0, y = 0, z = -1},
  15. }
  16. local function update_pane(pos)
  17. local nodeName = minetest.env:get_node(pos).name
  18. if nodeName:find("lpanes:pane_") == nil then
  19. return
  20. end
  21. --print("Found: "..nodeName)
  22. --Determine the shade/color of the node
  23. local shade
  24. local nodeNameParts = nodeName:split("_")
  25. local partCount = table.getn(nodeNameParts)
  26. local lastPart = nodeNameParts[partCount]
  27. --If the last part is simply a number, then its a 'shape' designator and not part of the color name
  28. if tonumber(lastPart) ~= nil then
  29. shade = table.concat(nodeNameParts, "_", 2, partCount - 1) --take off the 'shape' part of the node name
  30. else
  31. shade = table.concat(nodeNameParts, "_", 2) --start with the second entry and go to the end
  32. end
  33. --print("Shade: "..shade)
  34. local sum = 0
  35. for i = 1, 4 do
  36. local node = minetest.env:get_node({x = pos.x + directions[i].x, y = pos.y + directions[i].y, z = pos.z + directions[i].z})
  37. if minetest.registered_nodes[node.name].walkable ~= false then
  38. sum = sum + 2 ^ (i - 1)
  39. end
  40. end
  41. if sum == 0 then
  42. sum = 15
  43. end
  44. print("Adding node: lpanes:pane_"..shade.."_"..sum)
  45. minetest.env:add_node(pos, {name = "lpanes:pane_"..shade.."_"..sum})
  46. end
  47. local function update_nearby(pos)
  48. for i = 1,4 do
  49. update_pane({x = pos.x + directions[i].x, y = pos.y + directions[i].y, z = pos.z + directions[i].z})
  50. end
  51. end
  52. function pane(node, desc, dropitem, shade)
  53. local function rshift(x, by)
  54. return math.floor(x / 2 ^ by)
  55. end
  56. local half_blocks = {
  57. {0, -0.5, -0.06, 0.5, 0.5, 0.06},
  58. {-0.06, -0.5, 0, 0.06, 0.5, 0.5},
  59. {-0.5, -0.5, -0.06, 0, 0.5, 0.06},
  60. {-0.06, -0.5, -0.5, 0.06, 0.5, 0}
  61. }
  62. local full_blocks = {
  63. {-0.5, -0.5, -0.06, 0.5, 0.5, 0.06},
  64. {-0.06, -0.5, -0.5, 0.06, 0.5, 0.5}
  65. }
  66. for i = 1, 15 do
  67. local need = {}
  68. local cnt = 0
  69. for j = 1, 4 do
  70. if rshift(i, j - 1) % 2 == 1 then
  71. need[j] = true
  72. cnt = cnt + 1
  73. end
  74. end
  75. local take = {}
  76. if need[1] == true and need[3] == true then
  77. need[1] = nil
  78. need[3] = nil
  79. table.insert(take, full_blocks[1])
  80. end
  81. if need[2] == true and need[4] == true then
  82. need[2] = nil
  83. need[4] = nil
  84. table.insert(take, full_blocks[2])
  85. end
  86. for k in pairs(need) do
  87. table.insert(take, half_blocks[k])
  88. end
  89. local texture = "lpanes_"..shade..".png"
  90. if cnt == 1 then
  91. texture = "lpanes_half_"..shade..".png"
  92. end
  93. -- Position-appropriate node. One of these nodes will replace the generic node immediately upon placement
  94. minetest.register_node("lpanes:pane_"..shade.."_"..i, {
  95. drawtype = "nodebox",
  96. --tiles = {"xpanes_top_"..node..""..shade..".png", "xpanes_top_"..node..""..shade..".png", texture},
  97. tiles = {"lpanes_top.png", "lpanes_top.png", texture},
  98. paramtype = "light",
  99. use_texture_alpha = true,
  100. groups = {snappy=2,cracky=3,oddly_breakable_by_hand=3},
  101. drop = dropitem,
  102. node_box = {
  103. type = "fixed",
  104. fixed = take
  105. },
  106. selection_box = {
  107. type = "fixed",
  108. fixed = take
  109. }
  110. })
  111. end
  112. --This is the node that is originally placed. It will be replaced immediately by one
  113. -- of the position-appropriate nodes
  114. minetest.register_node("lpanes:pane_"..shade, {
  115. description = desc,
  116. tiles = {"lpanes_"..shade..".png"},
  117. inventory_image = "lpanes_"..shade..".png",
  118. paramtype = "light",
  119. stack_max = 64,
  120. use_texture_alpha = true,
  121. wield_image = "lpanes_"..shade..".png",
  122. node_placement_prediction = "",
  123. on_construct = update_pane,
  124. drop = "",
  125. })
  126. --Crafing
  127. --The Clear, Black, and Grey shades need special handling.
  128. if shade == "clear" then
  129. minetest.register_craft({
  130. type = "shapeless",
  131. output = 'lpanes:pane_'..shade..' 6',
  132. recipe = {"default:glass", leadingMetal},
  133. cooktime = 3,
  134. })
  135. elseif shade == "black" then
  136. minetest.register_craft({
  137. type = "shapeless",
  138. output = 'lpanes:pane_'..shade..' 6',
  139. recipe = {"default:glass", leadingMetal, "dye:black"},
  140. cooktime = 3,
  141. })
  142. elseif shade == "white" then
  143. minetest.register_craft({
  144. type = "shapeless",
  145. output = 'lpanes:pane_'..shade..' 6',
  146. recipe = {"default:glass", leadingMetal, "dye:white"},
  147. cooktime = 3,
  148. })
  149. elseif shade == "grey" then
  150. minetest.register_craft({
  151. type = "shapeless",
  152. output = 'lpanes:pane_'..shade..' 6',
  153. recipe = {"default:glass", leadingMetal, "dye:grey"},
  154. cooktime = 3,
  155. })
  156. elseif shade == "light_grey" then
  157. minetest.register_craft({
  158. type = "shapeless",
  159. output = 'lpanes:pane_'..shade..' 6',
  160. recipe = {"default:glass", leadingMetal, "dye:light_grey"},
  161. cooktime = 3,
  162. })
  163. elseif shade == "dark_grey" then
  164. minetest.register_craft({
  165. type = "shapeless",
  166. output = 'lpanes:pane_'..shade..' 6',
  167. recipe = {"default:glass", leadingMetal, "dye:dark_grey"},
  168. cooktime = 3,
  169. })
  170. else
  171. minetest.register_craft({
  172. type = "shapeless",
  173. output = 'lpanes:pane_'..shade..' 6',
  174. recipe = {"default:glass", leadingMetal, "unifieddyes:"..shade},
  175. cooktime = 3,
  176. })
  177. end
  178. end
  179. minetest.register_on_placenode(update_nearby)
  180. minetest.register_on_dignode(update_nearby)
  181. colors = {
  182. { "aqua", true },
  183. --{ "black", true},
  184. { "blue", true },
  185. { "cyan", true },
  186. { "green", true },
  187. { "lime", true },
  188. { "magenta", true },
  189. { "orange", true },
  190. { "red", true },
  191. { "redviolet", true },
  192. { "skyblue", true },
  193. { "violet", true },
  194. { "yellow", true },
  195. }
  196. --Do the colors that follow Unified Dye's naming pattern
  197. for i in ipairs(colors) do
  198. local hue = colors[i][1]
  199. -- node, desc, dropItem, shade
  200. pane("glass", "Glass Pane "..hue, "", hue)
  201. pane("glass", "Glass Pane Medium "..hue, "", "medium_"..hue)
  202. pane("glass", "Glass Pane Medium "..hue.." s50", "", "medium_"..hue.."_s50")
  203. pane("glass", "Glass Pane Dark "..hue, "", "dark_"..hue)
  204. pane("glass", "Glass Pane Dark "..hue.." s50", "", "dark_"..hue.."_s50")
  205. end
  206. --Do the colors that don't follow Unified Dye's naming pattern
  207. pane("glass", "Glass Pane white", "", "white")
  208. pane("glass", "Glass Pane light grey", "", "light_grey")
  209. pane("glass", "Glass Pane grey", "", "grey")
  210. pane("glass", "Glass Pane dark grey", "", "dark_grey")
  211. pane("glass", "Glass Pane black", "", "black")
  212. pane("glass", "Glass Pane clear", "", "clear")
  213. print("[LPanes] Loaded!")