init.lua 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. stack_max = 16,
  85. is_ground_content = false,
  86. sunlight_propagates = true,
  87. inventory_image = def.inventory_image,
  88. wield_image = def.wield_image,
  89. paramtype2 = "facedir",
  90. tiles = {def.textures[3], def.textures[3], def.textures[1]},
  91. groups = flatgroups,
  92. drop = "xpanes:" .. name .. "_flat",
  93. sounds = def.sounds,
  94. use_texture_alpha = def.use_texture_alpha or false,
  95. node_box = {
  96. type = "fixed",
  97. fixed = {{-1/2, -1/2, -1/32, 1/2, 1/2, 1/32}},
  98. },
  99. selection_box = {
  100. type = "fixed",
  101. fixed = {{-1/2, -1/2, -1/32, 1/2, 1/2, 1/32}},
  102. },
  103. connect_sides = { "left", "right" },
  104. })
  105. local groups = table.copy(def.groups)
  106. groups.pane = 1
  107. groups.not_in_creative_inventory = 1
  108. minetest.register_node(":xpanes:" .. name, {
  109. drawtype = "nodebox",
  110. paramtype = "light",
  111. is_ground_content = false,
  112. sunlight_propagates = true,
  113. description = def.description,
  114. stack_max = 16,
  115. tiles = {def.textures[3], def.textures[3], def.textures[1]},
  116. groups = groups,
  117. drop = "xpanes:" .. name .. "_flat",
  118. sounds = def.sounds,
  119. use_texture_alpha = def.use_texture_alpha or false,
  120. node_box = {
  121. type = "connected",
  122. fixed = {{-1/32, -1/2, -1/32, 1/32, 1/2, 1/32}},
  123. connect_front = {{-1/32, -1/2, -1/2, 1/32, 1/2, -1/32}},
  124. connect_left = {{-1/2, -1/2, -1/32, -1/32, 1/2, 1/32}},
  125. connect_back = {{-1/32, -1/2, 1/32, 1/32, 1/2, 1/2}},
  126. connect_right = {{1/32, -1/2, -1/32, 1/2, 1/2, 1/32}},
  127. },
  128. connects_to = {"group:pane", "group:stone", "group:glass", "group:wood", "group:tree"},
  129. })
  130. minetest.register_craft({
  131. output = "xpanes:" .. name .. "_flat 16",
  132. recipe = def.recipe
  133. })
  134. end
  135. xpanes.register_pane("pane", {
  136. description = "Glass Pane",
  137. textures = {"default_glass.png","xpanes_pane_half.png","xpanes_white.png"},
  138. inventory_image = "default_glass.png",
  139. wield_image = "default_glass.png",
  140. sounds = default.node_sound_glass_defaults(),
  141. groups = {snappy=2, cracky=3, oddly_breakable_by_hand=3},
  142. recipe = {
  143. {"default:glass", "default:glass", "default:glass"},
  144. {"default:glass", "default:glass", "default:glass"}
  145. }
  146. })
  147. xpanes.register_pane("bar", {
  148. description = "Iron Bar",
  149. textures = {"xpanes_bar.png","xpanes_bar.png","xpanes_bar_top.png"},
  150. inventory_image = "xpanes_bar.png",
  151. wield_image = "xpanes_bar.png",
  152. groups = {cracky=2},
  153. sounds = default.node_sound_metal_defaults(),
  154. recipe = {
  155. {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"},
  156. {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"}
  157. }
  158. })
  159. minetest.register_lbm({
  160. name = "xpanes:gen2",
  161. nodenames = {"group:pane"},
  162. action = function(pos, node)
  163. update_pane(pos)
  164. for i = 0, 3 do
  165. local dir = minetest.facedir_to_dir(i)
  166. update_pane(vector.add(pos, dir))
  167. end
  168. end
  169. })