wires.lua 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. -- naming scheme: wire:(xp)(zp)(xm)(zm)(xpyp)(zpyp)(xmyp)(zmyp)_on/off
  2. -- where x= x direction, z= z direction, y= y direction, p = +1, m = -1, e.g. xpym = {x=1, y=-1, z=0}
  3. -- The (xp)/(zpyp)/.. statements shall be replaced by either 0 or 1
  4. -- Where 0 means the wire has no visual connection to that direction and
  5. -- 1 means that the wire visually connects to that other node.
  6. -- #######################
  7. -- ## Update wire looks ##
  8. -- #######################
  9. -- self_pos = pos of any mesecon node, from_pos = pos of conductor to getconnect for
  10. local wire_getconnect = function (from_pos, self_pos)
  11. local node = minetest.get_node(self_pos)
  12. if minetest.registered_nodes[node.name]
  13. and minetest.registered_nodes[node.name].mesecons then
  14. -- rules of node to possibly connect to
  15. local rules = {}
  16. if (minetest.registered_nodes[node.name].mesecon_wire) then
  17. rules = mesecon.rules.default
  18. else
  19. rules = mesecon.get_any_rules(node)
  20. end
  21. for _, r in ipairs(mesecon.flattenrules(rules)) do
  22. if (mesecon.cmpPos(mesecon.addPosRule(self_pos, r), from_pos)) then
  23. return true
  24. end
  25. end
  26. end
  27. return false
  28. end
  29. -- Update this node
  30. local wire_updateconnect = function (pos)
  31. local connections = {}
  32. for _, r in ipairs(mesecon.rules.default) do
  33. if wire_getconnect(pos, mesecon.addPosRule(pos, r)) then
  34. table.insert(connections, r)
  35. end
  36. end
  37. local nid = {}
  38. for _, vec in ipairs(connections) do
  39. -- flat component
  40. if vec.x == 1 then nid[0] = "1" end
  41. if vec.z == 1 then nid[1] = "1" end
  42. if vec.x == -1 then nid[2] = "1" end
  43. if vec.z == -1 then nid[3] = "1" end
  44. -- slopy component
  45. if vec.y == 1 then
  46. if vec.x == 1 then nid[4] = "1" end
  47. if vec.z == 1 then nid[5] = "1" end
  48. if vec.x == -1 then nid[6] = "1" end
  49. if vec.z == -1 then nid[7] = "1" end
  50. end
  51. end
  52. local nodeid = (nid[0] or "0")..(nid[1] or "0")..(nid[2] or "0")..(nid[3] or "0")
  53. ..(nid[4] or "0")..(nid[5] or "0")..(nid[6] or "0")..(nid[7] or "0")
  54. local state_suffix = string.find(minetest.get_node(pos).name, "_off") and "_off" or "_on"
  55. minetest.set_node(pos, {name = "mesecons:wire_"..nodeid..state_suffix})
  56. end
  57. local update_on_place_dig = function (pos, node)
  58. -- Update placed node (get_node again as it may have been dug)
  59. local nn = minetest.get_node(pos)
  60. if (minetest.registered_nodes[nn.name])
  61. and (minetest.registered_nodes[nn.name].mesecon_wire) then
  62. wire_updateconnect(pos)
  63. end
  64. -- Update nodes around it
  65. local rules = {}
  66. if minetest.registered_nodes[node.name]
  67. and minetest.registered_nodes[node.name].mesecon_wire then
  68. rules = mesecon.rules.default
  69. else
  70. rules = mesecon.get_any_rules(node)
  71. end
  72. if (not rules) then return end
  73. for _, r in ipairs(mesecon.flattenrules(rules)) do
  74. local np = mesecon.addPosRule(pos, r)
  75. if minetest.registered_nodes[minetest.get_node(np).name]
  76. and minetest.registered_nodes[minetest.get_node(np).name].mesecon_wire then
  77. wire_updateconnect(np)
  78. end
  79. end
  80. end
  81. function mesecon.update_autoconnect(pos, node)
  82. if (not node) then node = minetest.get_node(pos) end
  83. update_on_place_dig(pos, node)
  84. end
  85. -- ############################
  86. -- ## Wire node registration ##
  87. -- ############################
  88. -- Nodeboxes:
  89. local box_center = {-1/16, -.5, -1/16, 1/16, -.5+1/16, 1/16}
  90. local box_bump1 = { -2/16, -8/16, -2/16, 2/16, -13/32, 2/16 }
  91. local nbox_nid =
  92. {
  93. [0] = {1/16, -.5, -1/16, 8/16, -.5+1/16, 1/16}, -- x positive
  94. [1] = {-1/16, -.5, 1/16, 1/16, -.5+1/16, 8/16}, -- z positive
  95. [2] = {-8/16, -.5, -1/16, -1/16, -.5+1/16, 1/16}, -- x negative
  96. [3] = {-1/16, -.5, -8/16, 1/16, -.5+1/16, -1/16}, -- z negative
  97. [4] = {.5-1/16, -.5+1/16, -1/16, .5, .4999+1/16, 1/16}, -- x positive up
  98. [5] = {-1/16, -.5+1/16, .5-1/16, 1/16, .4999+1/16, .5}, -- z positive up
  99. [6] = {-.5, -.5+1/16, -1/16, -.5+1/16, .4999+1/16, 1/16}, -- x negative up
  100. [7] = {-1/16, -.5+1/16, -.5, 1/16, .4999+1/16, -.5+1/16} -- z negative up
  101. }
  102. local tiles_off = { "mesecons_wire_off.png" }
  103. local tiles_on = { "mesecons_wire_on.png" }
  104. local selectionbox =
  105. {
  106. type = "fixed",
  107. fixed = {-.5, -.5, -.5, .5, -.5+4/16, .5}
  108. }
  109. -- go to the next nodeid (ex.: 01000011 --> 01000100)
  110. local nid_inc = function() end
  111. nid_inc = function (nid)
  112. local i = 0
  113. while nid[i-1] ~= 1 do
  114. nid[i] = (nid[i] ~= 1) and 1 or 0
  115. i = i + 1
  116. end
  117. -- BUT: Skip impossible nodeids:
  118. if ((nid[0] == 0 and nid[4] == 1) or (nid[1] == 0 and nid[5] == 1)
  119. or (nid[2] == 0 and nid[6] == 1) or (nid[3] == 0 and nid[7] == 1)) then
  120. return nid_inc(nid)
  121. end
  122. return i <= 8
  123. end
  124. register_wires = function()
  125. local nid = {}
  126. while true do
  127. -- Create group specifiction and nodeid string (see note above for details)
  128. local nodeid = (nid[0] or "0")..(nid[1] or "0")..(nid[2] or "0")..(nid[3] or "0")
  129. ..(nid[4] or "0")..(nid[5] or "0")..(nid[6] or "0")..(nid[7] or "0")
  130. -- Calculate nodebox
  131. local nodebox = {type = "fixed", fixed={box_center}}
  132. for i=0,7 do
  133. if nid[i] == 1 then
  134. table.insert(nodebox.fixed, nbox_nid[i])
  135. end
  136. end
  137. -- Add bump to nodebox if curved
  138. if (nid[0] == 1 and nid[1] == 1) or (nid[1] == 1 and nid[2] == 1)
  139. or (nid[2] == 1 and nid[3] == 1) or (nid[3] == 1 and nid[0] == 1) then
  140. table.insert(nodebox.fixed, box_bump1)
  141. end
  142. -- If nothing to connect to, still make a nodebox of a straight wire
  143. if nodeid == "00000000" then
  144. nodebox.fixed = {-8/16, -.5, -1/16, 8/16, -.5+1/16, 1/16}
  145. end
  146. local rules = {}
  147. if (nid[0] == 1) then table.insert(rules, vector.new( 1, 0, 0)) end
  148. if (nid[1] == 1) then table.insert(rules, vector.new( 0, 0, 1)) end
  149. if (nid[2] == 1) then table.insert(rules, vector.new(-1, 0, 0)) end
  150. if (nid[3] == 1) then table.insert(rules, vector.new( 0, 0, -1)) end
  151. if (nid[0] == 1) then table.insert(rules, vector.new( 1, -1, 0)) end
  152. if (nid[1] == 1) then table.insert(rules, vector.new( 0, -1, 1)) end
  153. if (nid[2] == 1) then table.insert(rules, vector.new(-1, -1, 0)) end
  154. if (nid[3] == 1) then table.insert(rules, vector.new( 0, -1, -1)) end
  155. if (nid[4] == 1) then table.insert(rules, vector.new( 1, 1, 0)) end
  156. if (nid[5] == 1) then table.insert(rules, vector.new( 0, 1, 1)) end
  157. if (nid[6] == 1) then table.insert(rules, vector.new(-1, 1, 0)) end
  158. if (nid[7] == 1) then table.insert(rules, vector.new( 0, 1, -1)) end
  159. local meseconspec_off = { conductor = {
  160. rules = rules,
  161. state = mesecon.state.off,
  162. onstate = "mesecons:wire_"..nodeid.."_on"
  163. }}
  164. local meseconspec_on = { conductor = {
  165. rules = rules,
  166. state = mesecon.state.on,
  167. offstate = "mesecons:wire_"..nodeid.."_off"
  168. }}
  169. local groups_on = {dig_immediate = 3, mesecon_conductor_craftable = 1,
  170. not_in_creative_inventory = 1}
  171. local groups_off = {dig_immediate = 3, mesecon_conductor_craftable = 1}
  172. if nodeid ~= "00000000" then
  173. groups_off["not_in_creative_inventory"] = 1
  174. end
  175. mesecon.register_node("mesecons:wire_"..nodeid, {
  176. description = "Mesecon",
  177. drawtype = "nodebox",
  178. inventory_image = "mesecons_wire_inv.png",
  179. wield_image = "mesecons_wire_inv.png",
  180. paramtype = "light",
  181. paramtype2 = "facedir",
  182. sunlight_propagates = true,
  183. selection_box = selectionbox,
  184. node_box = nodebox,
  185. walkable = false,
  186. drop = "mesecons:wire_00000000_off",
  187. mesecon_wire = true
  188. }, {tiles = tiles_off, mesecons = meseconspec_off, groups = groups_off},
  189. {tiles = tiles_on, mesecons = meseconspec_on, groups = groups_on})
  190. if (nid_inc(nid) == false) then return end
  191. end
  192. end
  193. register_wires()
  194. -- ##############
  195. -- ## Crafting ##
  196. -- ##############
  197. minetest.register_craft({
  198. type = "cooking",
  199. output = "mesecons:wire_00000000_off 2",
  200. recipe = "default:mese_crystal_fragment",
  201. cooktime = 3,
  202. })
  203. minetest.register_craft({
  204. type = "cooking",
  205. output = "mesecons:wire_00000000_off 18",
  206. recipe = "default:mese_crystal",
  207. cooktime = 15,
  208. })
  209. minetest.register_craft({
  210. type = "cooking",
  211. output = "mesecons:wire_00000000_off 162",
  212. recipe = "default:mese",
  213. cooktime = 30,
  214. })