init.lua 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. -- Pillars by Citorva
  2. -- This mode is licensed as MIT licence. This code can't be edited or copyed
  3. -- outside the terms of this license.
  4. local basic_shape = {
  5. {-0.5, 0.25, -0.5, 0.5, 0.5, 0.5}, -- top
  6. {-0.5, -0.5, -0.5, 0.5, -0.25, 0.5}, -- bot
  7. {-0.25, -0.25, -0.25, 0.25, 0.25, 0.25}, -- body
  8. }
  9. local bottom_shape = {
  10. {-0.5, -0.5, -0.5, 0.5, -0.25, 0.5}, -- bot
  11. {-0.25, -0.25, -0.25, 0.25, 0.5, 0.25}, -- body
  12. }
  13. local top_shape = {
  14. {-0.5, 0.25, -0.5, 0.5, 0.5, 0.5}, -- top
  15. {-0.25, -0.5, -0.25, 0.25, 0.25, 0.25}, -- body
  16. }
  17. local middle_shape = {
  18. {-0.25, -0.5, -0.25, 0.25, 0.5, 0.25}, -- body
  19. }
  20. local function is_pillar(pos)
  21. return minetest.get_item_group(minetest.get_node(pos).name, "pillar") > 0
  22. end
  23. local function connected(pos, name, top)
  24. local v_dir = vector.new(0,-1,0)
  25. if top == true then
  26. v_dir = vector.new(0,1,0)
  27. end
  28. local aside = vector.add(pos, v_dir)
  29. if is_pillar(aside) then
  30. return true
  31. end
  32. return false
  33. end
  34. local function swap(pos, node, name)
  35. if node.name == name then
  36. return
  37. end
  38. minetest.set_node(pos, {name = name})
  39. end
  40. local function update_pillar(pos)
  41. if not is_pillar(pos) then
  42. return
  43. end
  44. local node = minetest.get_node(pos)
  45. local name = node.name
  46. if name:sub(-4) == "_top" or
  47. name:sub(-4) == "_bot" or
  48. name:sub(-4) == "_mid" then
  49. name = name:sub(1, -5)
  50. end
  51. local any = node.param2
  52. local on_top = connected(pos, name, true)
  53. local on_bot = connected(pos, name, false)
  54. if on_top and on_bot then
  55. swap(pos, node, name .. "_mid", any)
  56. elseif on_top then
  57. swap(pos, node, name .. "_bot", any)
  58. elseif on_bot then
  59. swap(pos, node, name .. "_top", any)
  60. else
  61. swap(pos, node, name, any)
  62. end
  63. end
  64. minetest.register_on_placenode(function(pos, node)
  65. if minetest.get_item_group(node, "pillar") then
  66. update_pillar(pos)
  67. end
  68. update_pillar(vector.add(pos, vector.new(0,-1,0)))
  69. update_pillar(vector.add(pos, vector.new(0,1,0)))
  70. end)
  71. minetest.register_on_dignode(function(pos)
  72. update_pillar(vector.add(pos, vector.new(0,-1,0)))
  73. update_pillar(vector.add(pos, vector.new(0,1,0)))
  74. end)
  75. pillars = {}
  76. function pillars.register_pillar(name, def)
  77. local groups = table.copy(def.groups)
  78. groups.pillar = 1
  79. local no_inv_groups = table.copy(groups)
  80. no_inv_groups.not_in_creative_inventory = 1
  81. for k, v in pairs({{"", basic_shape, groups},
  82. {"_top", top_shape, no_inv_groups},
  83. {"_bot", bottom_shape, no_inv_groups},
  84. {"_mid", middle_shape, no_inv_groups}}) do
  85. local sides = { "top", "bottom" }
  86. if v[1] == "_top" then
  87. sides = { "bottom" }
  88. elseif v[1] == "_bot" then
  89. sides = { "top" }
  90. elseif v[1] == "" then
  91. sides = {}
  92. end
  93. minetest.register_node(":pillars:" .. name .. v[1], {
  94. description = def.description,
  95. drawtype = "nodebox",
  96. paramtype = "light",
  97. is_ground_content = false,
  98. sunlight_propagates = def.sunlight_propagates,
  99. inventory_image = def.inventory_image,
  100. wield_image = def.wield_image,
  101. tiles = def.textures,
  102. groups = v[3],
  103. drop = "pillars:" .. name,
  104. sounds = def.sounds,
  105. use_texture_alpha = def.use_texture_alpha or false,
  106. node_box = {
  107. type = "fixed",
  108. fixed = v[2]
  109. },
  110. selection_box = {
  111. type = "fixed",
  112. fixed = v[2]
  113. },
  114. connect_sides = sides
  115. })
  116. end
  117. minetest.register_craft({
  118. output = "pillars:" .. name .. " 7",
  119. recipe = {{def.basenode, def.basenode, def.basenode},
  120. {"",def.basenode,""},
  121. {def.basenode, def.basenode, def.basenode}}
  122. })
  123. minetest.register_craft({
  124. output = def.basenode .. " 1",
  125. recipe = {{"pillars:" .. name}}
  126. })
  127. end
  128. -- Create Pillar from nodes defined in default mod
  129. local default_lib_nodes = {
  130. -- Define all pillars from stone
  131. {"stone", "Stone Pillar"},
  132. {"cobble", "Cobblestone Pillar"},
  133. {"stonebrick", "Stone Brick Pillar", "stone_brick"},
  134. {"stone_block", "Stone Block Pillar"},
  135. {"mossycobble", "Mossy Cobblestone Pillar"},
  136. {"desert_stone", "Desert Stone Pillar"},
  137. {"desert_cobble", "Desert Cobblestone Pillar"},
  138. {"desert_stonebrick", "Desert Stone Brick Pillar", "desert_stone_brick"},
  139. {"desert_stone_block", "Desert Stone Block Pillar"},
  140. {"sandstone", "Sandstone Pillar"},
  141. {"sandstonebrick", "Sandstone Brick Pillar", "sandstone_brick"},
  142. {"sandstone_block", "Sandstone Block Pillar"},
  143. {"desert_sandstone", "Desert Sandstone Pillar"},
  144. {"desert_sandstone_brick", "Desert Sandstone Brick Pillar"},
  145. {"desert_sandstone_block", "Desert Sandstone Block Pillar"},
  146. {"silver_sandstone", "Silver Sandstone Pillar"},
  147. {"silver_sandstone_brick", "Silver Sandstone Brick Pillar"},
  148. {"silver_sandstone_block", "Silver Sandstone Block Pillar"},
  149. {"obsidian", "Obsidian Pillar"},
  150. {"obsidianbrick", "Obsidian Brick Pillar", "obsidian_brick"},
  151. {"obsidian_block", "Obsidian Block Pillar"},
  152. -- Define all pillar from wood planks
  153. {"wood", "Wood Pillar", sound = default.node_sound_wood_defaults()},
  154. {"junglewood", "Jungle Wood Pillar", sound = default.node_sound_wood_defaults()},
  155. {"pine_wood", "Pine Wood Pillar", sound = default.node_sound_wood_defaults()},
  156. {"acacia_wood", "Acacia Wood Pillar", sound = default.node_sound_wood_defaults()},
  157. {"aspen_wood", "Aspen Wood Pillar", sound = default.node_sound_wood_defaults()},
  158. }
  159. for k, v in pairs(default_lib_nodes) do
  160. texture_name = v[1]
  161. sound = default.node_sound_stone_defaults()
  162. if v[3] then
  163. texture_name = v[3]
  164. end
  165. if v.sound then
  166. sound = v.sound
  167. end
  168. pillars.register_pillar(v[1], {
  169. description = v[2],
  170. textures = {"default_" .. texture_name .. ".png"},
  171. sounds = sound,
  172. groups = {snappy=2, cracky=3, oddly_breakable_by_hand=3},
  173. basenode = "default:" .. v[1]
  174. })
  175. end