init.lua 4.6 KB

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