init.lua 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. -- xpanes/init.lua
  2. -- Load support for MT game translation.
  3. local S = minetest.get_translator("xpanes")
  4. local function is_pane(pos)
  5. return minetest.get_item_group(minetest.get_node(pos).name, "pane") > 0
  6. end
  7. local function connects_dir(pos, name, dir)
  8. local aside = vector.add(pos, minetest.facedir_to_dir(dir))
  9. if is_pane(aside) then
  10. return true
  11. end
  12. local connects_to = minetest.registered_nodes[name].connects_to
  13. if not connects_to then
  14. return false
  15. end
  16. local list = minetest.find_nodes_in_area(aside, aside, connects_to)
  17. if #list > 0 then
  18. return true
  19. end
  20. return false
  21. end
  22. local function swap(pos, node, name, param2)
  23. if node.name == name and node.param2 == param2 then
  24. return
  25. end
  26. minetest.swap_node(pos, {name = name, param2 = param2})
  27. end
  28. local function update_pane(pos)
  29. if not is_pane(pos) then
  30. return
  31. end
  32. local node = minetest.get_node(pos)
  33. local name = node.name
  34. if name:sub(-5) == "_flat" then
  35. name = name:sub(1, -6)
  36. end
  37. local any = node.param2
  38. local c = {}
  39. local count = 0
  40. for dir = 0, 3 do
  41. c[dir] = connects_dir(pos, name, dir)
  42. if c[dir] then
  43. any = dir
  44. count = count + 1
  45. end
  46. end
  47. if count == 0 then
  48. swap(pos, node, name .. "_flat", any)
  49. elseif count == 1 then
  50. swap(pos, node, name .. "_flat", (any + 1) % 4)
  51. elseif count == 2 then
  52. if (c[0] and c[2]) or (c[1] and c[3]) then
  53. swap(pos, node, name .. "_flat", (any + 1) % 4)
  54. else
  55. swap(pos, node, name, 0)
  56. end
  57. else
  58. swap(pos, node, name, 0)
  59. end
  60. end
  61. minetest.register_on_placenode(function(pos, node)
  62. if minetest.get_item_group(node, "pane") then
  63. update_pane(pos)
  64. end
  65. for i = 0, 3 do
  66. local dir = minetest.facedir_to_dir(i)
  67. update_pane(vector.add(pos, dir))
  68. end
  69. end)
  70. minetest.register_on_dignode(function(pos)
  71. for i = 0, 3 do
  72. local dir = minetest.facedir_to_dir(i)
  73. update_pane(vector.add(pos, dir))
  74. end
  75. end)
  76. xpanes = {}
  77. function xpanes.register_pane(name, def)
  78. for i = 1, 15 do
  79. minetest.register_alias("xpanes:" .. name .. "_" .. i, "xpanes:" .. name .. "_flat")
  80. end
  81. local flatgroups = table.copy(def.groups)
  82. flatgroups.pane = 1
  83. minetest.register_node(":xpanes:" .. name .. "_flat", {
  84. description = def.description,
  85. drawtype = "nodebox",
  86. paramtype = "light",
  87. is_ground_content = false,
  88. sunlight_propagates = true,
  89. inventory_image = def.inventory_image,
  90. wield_image = def.wield_image,
  91. paramtype2 = "facedir",
  92. tiles = {
  93. def.textures[3],
  94. def.textures[3],
  95. def.textures[3],
  96. def.textures[3],
  97. def.textures[1],
  98. def.textures[1]
  99. },
  100. groups = flatgroups,
  101. drop = "xpanes:" .. name .. "_flat",
  102. sounds = def.sounds,
  103. use_texture_alpha = def.use_texture_alpha and "blend" or "clip",
  104. node_box = {
  105. type = "fixed",
  106. fixed = {{-1/2, -1/2, -1/32, 1/2, 1/2, 1/32}},
  107. },
  108. selection_box = {
  109. type = "fixed",
  110. fixed = {{-1/2, -1/2, -1/32, 1/2, 1/2, 1/32}},
  111. },
  112. connect_sides = { "left", "right" },
  113. })
  114. local groups = table.copy(def.groups)
  115. groups.pane = 1
  116. groups.not_in_creative_inventory = 1
  117. minetest.register_node(":xpanes:" .. name, {
  118. drawtype = "nodebox",
  119. paramtype = "light",
  120. is_ground_content = false,
  121. sunlight_propagates = true,
  122. description = def.description,
  123. tiles = {
  124. def.textures[3],
  125. def.textures[3],
  126. def.textures[1]
  127. },
  128. groups = groups,
  129. drop = "xpanes:" .. name .. "_flat",
  130. sounds = def.sounds,
  131. use_texture_alpha = def.use_texture_alpha and "blend" or "clip",
  132. node_box = {
  133. type = "connected",
  134. fixed = {{-1/32, -1/2, -1/32, 1/32, 1/2, 1/32}},
  135. connect_front = {{-1/32, -1/2, -1/2, 1/32, 1/2, -1/32}},
  136. connect_left = {{-1/2, -1/2, -1/32, -1/32, 1/2, 1/32}},
  137. connect_back = {{-1/32, -1/2, 1/32, 1/32, 1/2, 1/2}},
  138. connect_right = {{1/32, -1/2, -1/32, 1/2, 1/2, 1/32}},
  139. },
  140. connects_to = {"group:pane", "group:stone", "group:glass", "group:wood", "group:tree"},
  141. })
  142. minetest.register_craft({
  143. output = "xpanes:" .. name .. "_flat 16",
  144. recipe = def.recipe
  145. })
  146. end
  147. xpanes.register_pane("pane", {
  148. description = S("Glass Pane"),
  149. textures = {"default_glass.png", "", "xpanes_edge.png"},
  150. inventory_image = "default_glass.png",
  151. wield_image = "default_glass.png",
  152. sounds = default.node_sound_glass_defaults(),
  153. groups = {snappy=2, cracky=3, oddly_breakable_by_hand=3},
  154. recipe = {
  155. {"default:glass", "default:glass", "default:glass"},
  156. {"default:glass", "default:glass", "default:glass"}
  157. }
  158. })
  159. xpanes.register_pane("obsidian_pane", {
  160. description = S("Obsidian Glass Pane"),
  161. textures = {"default_obsidian_glass.png", "", "xpanes_edge_obsidian.png"},
  162. inventory_image = "default_obsidian_glass.png",
  163. wield_image = "default_obsidian_glass.png",
  164. sounds = default.node_sound_glass_defaults(),
  165. groups = {snappy=2, cracky=3},
  166. recipe = {
  167. {"default:obsidian_glass", "default:obsidian_glass", "default:obsidian_glass"},
  168. {"default:obsidian_glass", "default:obsidian_glass", "default:obsidian_glass"}
  169. }
  170. })
  171. xpanes.register_pane("bar", {
  172. description = S("Steel Bars"),
  173. textures = {"xpanes_bar.png", "", "xpanes_bar_top.png"},
  174. inventory_image = "xpanes_bar.png",
  175. wield_image = "xpanes_bar.png",
  176. groups = {cracky=2},
  177. sounds = default.node_sound_metal_defaults(),
  178. recipe = {
  179. {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"},
  180. {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"}
  181. }
  182. })
  183. minetest.register_lbm({
  184. name = "xpanes:gen2",
  185. nodenames = {"group:pane"},
  186. action = function(pos, node)
  187. update_pane(pos)
  188. for i = 0, 3 do
  189. local dir = minetest.facedir_to_dir(i)
  190. update_pane(vector.add(pos, dir))
  191. end
  192. end
  193. })
  194. -- Register steel bar doors and trapdoors
  195. if minetest.get_modpath("doors") then
  196. doors.register("xpanes:door_steel_bar", {
  197. tiles = {{name = "xpanes_door_steel_bar.png", backface_culling = true}},
  198. description = S("Steel Bar Door"),
  199. inventory_image = "xpanes_item_steel_bar.png",
  200. protected = true,
  201. groups = {node = 1, cracky = 1, level = 2},
  202. sounds = default.node_sound_metal_defaults(),
  203. sound_open = "xpanes_steel_bar_door_open",
  204. sound_close = "xpanes_steel_bar_door_close",
  205. gain_open = 0.15,
  206. gain_close = 0.13,
  207. recipe = {
  208. {"xpanes:bar_flat", "xpanes:bar_flat"},
  209. {"xpanes:bar_flat", "xpanes:bar_flat"},
  210. {"xpanes:bar_flat", "xpanes:bar_flat"},
  211. },
  212. })
  213. doors.register_trapdoor("xpanes:trapdoor_steel_bar", {
  214. description = S("Steel Bar Trapdoor"),
  215. inventory_image = "xpanes_trapdoor_steel_bar.png",
  216. wield_image = "xpanes_trapdoor_steel_bar.png",
  217. tile_front = "xpanes_trapdoor_steel_bar.png",
  218. tile_side = "xpanes_trapdoor_steel_bar_side.png",
  219. protected = true,
  220. groups = {node = 1, cracky = 1, level = 2, door = 1},
  221. sounds = default.node_sound_metal_defaults(),
  222. sound_open = "xpanes_steel_bar_door_open",
  223. sound_close = "xpanes_steel_bar_door_close",
  224. gain_open = 0.15,
  225. gain_close = 0.13,
  226. })
  227. minetest.register_craft({
  228. output = "xpanes:trapdoor_steel_bar",
  229. recipe = {
  230. {"xpanes:bar_flat", "xpanes:bar_flat"},
  231. {"xpanes:bar_flat", "xpanes:bar_flat"},
  232. }
  233. })
  234. end