init.lua 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. local function is_pane(pos)
  2. return minetest.get_item_group(minetest.get_node(pos).name, "pane") > 0
  3. end
  4. local function connects_dir(pos, name, dir)
  5. local aside = vector.add(pos, minetest.facedir_to_dir(dir))
  6. if is_pane(aside) then
  7. return true
  8. end
  9. local connects_to = minetest.registered_nodes[name].connects_to
  10. if not connects_to then
  11. return false
  12. end
  13. local list = minetest.find_nodes_in_area(aside, aside, connects_to)
  14. if #list > 0 then
  15. return true
  16. end
  17. return false
  18. end
  19. local function swap(pos, node, name, param2)
  20. if node.name == name and node.param2 == param2 then
  21. return
  22. end
  23. minetest.set_node(pos, {name = name, param2 = param2})
  24. end
  25. local function update_pane(pos)
  26. if not is_pane(pos) then
  27. return
  28. end
  29. local node = minetest.get_node(pos)
  30. local name = node.name
  31. if name:sub(-5) == "_flat" then
  32. name = name:sub(1, -6)
  33. end
  34. local any = node.param2
  35. local c = {}
  36. local count = 0
  37. for dir = 0, 3 do
  38. c[dir] = connects_dir(pos, name, dir)
  39. if c[dir] then
  40. any = dir
  41. count = count + 1
  42. end
  43. end
  44. if count == 0 then
  45. swap(pos, node, name .. "_flat", any)
  46. elseif count == 1 then
  47. swap(pos, node, name .. "_flat", (any + 1) % 4)
  48. elseif count == 2 then
  49. if (c[0] and c[2]) or (c[1] and c[3]) then
  50. swap(pos, node, name .. "_flat", (any + 1) % 4)
  51. else
  52. swap(pos, node, name, 0)
  53. end
  54. else
  55. swap(pos, node, name, 0)
  56. end
  57. end
  58. minetest.register_on_placenode(function(pos, node)
  59. if minetest.get_item_group(node, "pane") then
  60. update_pane(pos)
  61. end
  62. for i = 0, 3 do
  63. local dir = minetest.facedir_to_dir(i)
  64. update_pane(vector.add(pos, dir))
  65. end
  66. end)
  67. minetest.register_on_dignode(function(pos)
  68. for i = 0, 3 do
  69. local dir = minetest.facedir_to_dir(i)
  70. update_pane(vector.add(pos, dir))
  71. end
  72. end)
  73. xpanes = {}
  74. function xpanes.register_pane(name, def)
  75. for i = 1, 15 do
  76. minetest.register_alias("xpanes:" .. name .. "_" .. i, "xpanes:" .. name .. "_flat")
  77. end
  78. local flatgroups = table.copy(def.groups)
  79. flatgroups.pane = 1
  80. minetest.register_node(":xpanes:" .. name .. "_flat", {
  81. description = def.description,
  82. drawtype = "nodebox",
  83. paramtype = "light",
  84. is_ground_content = false,
  85. sunlight_propagates = true,
  86. inventory_image = def.inventory_image,
  87. wield_image = def.wield_image,
  88. paramtype2 = "facedir",
  89. tiles = {def.textures[3], def.textures[3], def.textures[1]},
  90. groups = flatgroups,
  91. drop = "xpanes:" .. name .. "_flat",
  92. sounds = def.sounds,
  93. use_texture_alpha = def.use_texture_alpha or false,
  94. node_box = {
  95. type = "fixed",
  96. fixed = {{-1/2, -1/2, -1/32, 1/2, 1/2, 1/32}},
  97. },
  98. selection_box = {
  99. type = "fixed",
  100. fixed = {{-1/2, -1/2, -1/32, 1/2, 1/2, 1/32}},
  101. },
  102. connect_sides = { "left", "right" },
  103. })
  104. local groups = table.copy(def.groups)
  105. groups.pane = 1
  106. groups.not_in_creative_inventory = 1
  107. minetest.register_node(":xpanes:" .. name, {
  108. drawtype = "nodebox",
  109. paramtype = "light",
  110. is_ground_content = false,
  111. sunlight_propagates = true,
  112. description = def.description,
  113. tiles = {def.textures[3], def.textures[3], def.textures[1]},
  114. groups = groups,
  115. drop = "xpanes:" .. name .. "_flat",
  116. sounds = def.sounds,
  117. use_texture_alpha = def.use_texture_alpha or false,
  118. node_box = {
  119. type = "connected",
  120. fixed = {{-1/32, -1/2, -1/32, 1/32, 1/2, 1/32}},
  121. connect_front = {{-1/32, -1/2, -1/2, 1/32, 1/2, -1/32}},
  122. connect_left = {{-1/2, -1/2, -1/32, -1/32, 1/2, 1/32}},
  123. connect_back = {{-1/32, -1/2, 1/32, 1/32, 1/2, 1/2}},
  124. connect_right = {{1/32, -1/2, -1/32, 1/2, 1/2, 1/32}},
  125. },
  126. connects_to = {"group:pane", "group:stone", "group:glass", "group:wood", "group:tree"},
  127. })
  128. minetest.register_craft({
  129. output = "xpanes:" .. name .. "_flat 16",
  130. recipe = def.recipe
  131. })
  132. end
  133. xpanes.register_pane("pane", {
  134. description = "Glass Pane",
  135. textures = {"default_glass.png","xpanes_pane_half.png","xpanes_edge.png"},
  136. inventory_image = "default_glass.png",
  137. wield_image = "default_glass.png",
  138. sounds = default.node_sound_glass_defaults(),
  139. groups = {snappy=2, cracky=3, oddly_breakable_by_hand=3},
  140. recipe = {
  141. {"default:glass", "default:glass", "default:glass"},
  142. {"default:glass", "default:glass", "default:glass"}
  143. }
  144. })
  145. xpanes.register_pane("obsidian_pane", {
  146. description = "Obsidian Glass Pane",
  147. textures = {"default_obsidian_glass.png","xpanes_pane_half.png","xpanes_edge_obsidian.png"},
  148. inventory_image = "default_obsidian_glass.png",
  149. wield_image = "default_obsidian_glass.png",
  150. sounds = default.node_sound_glass_defaults(),
  151. groups = {snappy=2, cracky=3},
  152. recipe = {
  153. {"default:obsidian_glass", "default:obsidian_glass", "default:obsidian_glass"},
  154. {"default:obsidian_glass", "default:obsidian_glass", "default:obsidian_glass"}
  155. }
  156. })
  157. xpanes.register_pane("bar", {
  158. description = "Steel Bars",
  159. textures = {"xpanes_bar.png","xpanes_bar.png","xpanes_bar_top.png"},
  160. inventory_image = "xpanes_bar.png",
  161. wield_image = "xpanes_bar.png",
  162. groups = {cracky=2},
  163. sounds = default.node_sound_metal_defaults(),
  164. recipe = {
  165. {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"},
  166. {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"}
  167. }
  168. })
  169. minetest.register_lbm({
  170. name = "xpanes:gen2",
  171. nodenames = {"group:pane"},
  172. action = function(pos, node)
  173. update_pane(pos)
  174. for i = 0, 3 do
  175. local dir = minetest.facedir_to_dir(i)
  176. update_pane(vector.add(pos, dir))
  177. end
  178. end
  179. })