wires.lua 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. -- naming scheme: wire:(xp)(zp)(xm)(zm)_on/off
  2. -- The conditions in brackets define whether there is a mesecon at that place or not
  3. -- 1 = there is one; 0 = there is none
  4. -- y always means y+
  5. box_center = {-1/16, -.5, -1/16, 1/16, -.5+1/16, 1/16}
  6. box_bump1 = { -2/16, -8/16, -2/16, 2/16, -13/32, 2/16 }
  7. box_xp = {1/16, -.5, -1/16, 8/16, -.5+1/16, 1/16}
  8. box_zp = {-1/16, -.5, 1/16, 1/16, -.5+1/16, 8/16}
  9. box_xm = {-8/16, -.5, -1/16, -1/16, -.5+1/16, 1/16}
  10. box_zm = {-1/16, -.5, -8/16, 1/16, -.5+1/16, -1/16}
  11. box_xpy = {.5-1/16, -.5+1/16, -1/16, .5, .4999+1/16, 1/16}
  12. box_zpy = {-1/16, -.5+1/16, .5-1/16, 1/16, .4999+1/16, .5}
  13. box_xmy = {-.5, -.5+1/16, -1/16, -.5+1/16, .4999+1/16, 1/16}
  14. box_zmy = {-1/16, -.5+1/16, -.5, 1/16, .4999+1/16, -.5+1/16}
  15. -- Registering the wires
  16. for xp=0, 1 do
  17. for zp=0, 1 do
  18. for xm=0, 1 do
  19. for zm=0, 1 do
  20. for xpy=0, 1 do
  21. for zpy=0, 1 do
  22. for xmy=0, 1 do
  23. for zmy=0, 1 do
  24. if (xpy == 1 and xp == 0) or (zpy == 1 and zp == 0)
  25. or (xmy == 1 and xm == 0) or (zmy == 1 and zm == 0) then break end
  26. local groups
  27. local nodeid = tostring(xp )..tostring(zp )..tostring(xm )..tostring(zm )..
  28. tostring(xpy)..tostring(zpy)..tostring(xmy)..tostring(zmy)
  29. if nodeid == "00000000" then
  30. groups = {dig_immediate = 3, mesecon_conductor_craftable=1}
  31. wiredesc = "Mesecon"
  32. else
  33. groups = {dig_immediate = 3, not_in_creative_inventory = 1}
  34. wiredesc = "Mesecons Wire (ID: "..nodeid..")"
  35. end
  36. local nodebox = {}
  37. local adjx = false
  38. local adjz = false
  39. if xp == 1 then table.insert(nodebox, box_xp) adjx = true end
  40. if zp == 1 then table.insert(nodebox, box_zp) adjz = true end
  41. if xm == 1 then table.insert(nodebox, box_xm) adjx = true end
  42. if zm == 1 then table.insert(nodebox, box_zm) adjz = true end
  43. if xpy == 1 then table.insert(nodebox, box_xpy) end
  44. if zpy == 1 then table.insert(nodebox, box_zpy) end
  45. if xmy == 1 then table.insert(nodebox, box_xmy) end
  46. if zmy == 1 then table.insert(nodebox, box_zmy) end
  47. if adjx and adjz and (xp + zp + xm + zm > 2) then
  48. table.insert(nodebox, box_bump1)
  49. tiles_off = {
  50. "wires_bump_off.png",
  51. "wires_bump_off.png",
  52. "wires_vertical_off.png",
  53. "wires_vertical_off.png",
  54. "wires_vertical_off.png",
  55. "wires_vertical_off.png"
  56. }
  57. tiles_on = {
  58. "wires_bump_on.png",
  59. "wires_bump_on.png",
  60. "wires_vertical_on.png",
  61. "wires_vertical_on.png",
  62. "wires_vertical_on.png",
  63. "wires_vertical_on.png"
  64. }
  65. else
  66. table.insert(nodebox, box_center)
  67. tiles_off = {
  68. "wires_off.png",
  69. "wires_off.png",
  70. "wires_vertical_off.png",
  71. "wires_vertical_off.png",
  72. "wires_vertical_off.png",
  73. "wires_vertical_off.png"
  74. }
  75. tiles_on = {
  76. "wires_on.png",
  77. "wires_on.png",
  78. "wires_vertical_on.png",
  79. "wires_vertical_on.png",
  80. "wires_vertical_on.png",
  81. "wires_vertical_on.png"
  82. }
  83. end
  84. if nodeid == "00000000" then
  85. nodebox = {-8/16, -.5, -1/16, 8/16, -.5+1/16, 1/16}
  86. end
  87. minetest.register_node("mesecons:wire_"..nodeid.."_off", {
  88. description = wiredesc,
  89. drawtype = "nodebox",
  90. tiles = tiles_off,
  91. -- inventory_image = "wires_inv.png",
  92. -- wield_image = "wires_inv.png",
  93. inventory_image = "jeija_mesecon_off.png",
  94. wield_image = "jeija_mesecon_off.png",
  95. paramtype = "light",
  96. paramtype2 = "facedir",
  97. sunlight_propagates = true,
  98. selection_box = {
  99. type = "fixed",
  100. fixed = {-.5, -.5, -.5, .5, -.5+4/16, .5}
  101. },
  102. node_box = {
  103. type = "fixed",
  104. fixed = nodebox
  105. },
  106. groups = groups,
  107. walkable = false,
  108. stack_max = 99,
  109. drop = "mesecons:wire_00000000_off",
  110. mesecons = {conductor={
  111. state = mesecon.state.off,
  112. onstate = "mesecons:wire_"..nodeid.."_on"
  113. }}
  114. })
  115. minetest.register_node("mesecons:wire_"..nodeid.."_on", {
  116. description = "Wire ID:"..nodeid,
  117. drawtype = "nodebox",
  118. tiles = tiles_on,
  119. -- inventory_image = "wires_inv.png",
  120. -- wield_image = "wires_inv.png",
  121. inventory_image = "jeija_mesecon_off.png",
  122. wield_image = "jeija_mesecon_off.png",
  123. paramtype = "light",
  124. paramtype2 = "facedir",
  125. sunlight_propagates = true,
  126. selection_box = {
  127. type = "fixed",
  128. fixed = {-.5, -.5, -.5, .5, -.5+4/16, .5}
  129. },
  130. node_box = {
  131. type = "fixed",
  132. fixed = nodebox
  133. },
  134. groups = {dig_immediate = 3, mesecon = 2, not_in_creative_inventory = 1},
  135. walkable = false,
  136. stack_max = 99,
  137. drop = "mesecons:wire_00000000_off",
  138. mesecons = {conductor={
  139. state = mesecon.state.on,
  140. offstate = "mesecons:wire_"..nodeid.."_off"
  141. }}
  142. })
  143. end
  144. end
  145. end
  146. end
  147. end
  148. end
  149. end
  150. end
  151. -- Updating the wires:
  152. -- Place the right connection wire
  153. local update_on_place_dig = function (pos, node)
  154. if minetest.registered_nodes[node.name]
  155. and minetest.registered_nodes[node.name].mesecons then
  156. mesecon:update_autoconnect(pos)
  157. end
  158. end
  159. minetest.register_on_placenode(update_on_place_dig)
  160. minetest.register_on_dignode(update_on_place_dig)
  161. function mesecon:update_autoconnect(pos, secondcall, replace_old)
  162. local xppos = {x=pos.x+1, y=pos.y, z=pos.z}
  163. local zppos = {x=pos.x, y=pos.y, z=pos.z+1}
  164. local xmpos = {x=pos.x-1, y=pos.y, z=pos.z}
  165. local zmpos = {x=pos.x, y=pos.y, z=pos.z-1}
  166. local xpympos = {x=pos.x+1, y=pos.y-1, z=pos.z}
  167. local zpympos = {x=pos.x, y=pos.y-1, z=pos.z+1}
  168. local xmympos = {x=pos.x-1, y=pos.y-1, z=pos.z}
  169. local zmympos = {x=pos.x, y=pos.y-1, z=pos.z-1}
  170. local xpypos = {x=pos.x+1, y=pos.y+1, z=pos.z}
  171. local zpypos = {x=pos.x, y=pos.y+1, z=pos.z+1}
  172. local xmypos = {x=pos.x-1, y=pos.y+1, z=pos.z}
  173. local zmypos = {x=pos.x, y=pos.y+1, z=pos.z-1}
  174. if secondcall == nil then
  175. mesecon:update_autoconnect(xppos, true)
  176. mesecon:update_autoconnect(zppos, true)
  177. mesecon:update_autoconnect(xmpos, true)
  178. mesecon:update_autoconnect(zmpos, true)
  179. mesecon:update_autoconnect(xpypos, true)
  180. mesecon:update_autoconnect(zpypos, true)
  181. mesecon:update_autoconnect(xmypos, true)
  182. mesecon:update_autoconnect(zmypos, true)
  183. mesecon:update_autoconnect(xpympos, true)
  184. mesecon:update_autoconnect(zpympos, true)
  185. mesecon:update_autoconnect(xmympos, true)
  186. mesecon:update_autoconnect(zmympos, true)
  187. end
  188. nodename = minetest.env:get_node(pos).name
  189. if string.find(nodename, "mesecons:wire_") == nil and not replace_old then return nil end
  190. if mesecon:rules_link_anydir(pos, xppos) then xp = 1 else xp = 0 end
  191. if mesecon:rules_link_anydir(pos, xmpos) then xm = 1 else xm = 0 end
  192. if mesecon:rules_link_anydir(pos, zppos) then zp = 1 else zp = 0 end
  193. if mesecon:rules_link_anydir(pos, zmpos) then zm = 1 else zm = 0 end
  194. if mesecon:rules_link_anydir(pos, xpympos) then xp = 1 end
  195. if mesecon:rules_link_anydir(pos, xmympos) then xm = 1 end
  196. if mesecon:rules_link_anydir(pos, zpympos) then zp = 1 end
  197. if mesecon:rules_link_anydir(pos, zmympos) then zm = 1 end
  198. if mesecon:rules_link_anydir(pos, xpypos) then xpy = 1 else xpy = 0 end
  199. if mesecon:rules_link_anydir(pos, zpypos) then zpy = 1 else zpy = 0 end
  200. if mesecon:rules_link_anydir(pos, xmypos) then xmy = 1 else xmy = 0 end
  201. if mesecon:rules_link_anydir(pos, zmypos) then zmy = 1 else zmy = 0 end
  202. if xpy == 1 then xp = 1 end
  203. if zpy == 1 then zp = 1 end
  204. if xmy == 1 then xm = 1 end
  205. if zmy == 1 then zm = 1 end
  206. local nodeid = tostring(xp )..tostring(zp )..tostring(xm )..tostring(zm )..
  207. tostring(xpy)..tostring(zpy)..tostring(xmy)..tostring(zmy)
  208. if string.find(nodename, "_off") ~= nil then
  209. minetest.env:set_node(pos, {name = "mesecons:wire_"..nodeid.."_off"})
  210. else
  211. minetest.env:set_node(pos, {name = "mesecons:wire_"..nodeid.."_on" })
  212. end
  213. end
  214. if minetest.registered_nodes["default:stone_with_mese"] == nil then
  215. minetest.register_craft({
  216. output = "mesecons:wire_00000000_off 18",
  217. recipe = {
  218. {"default:mese"},
  219. }
  220. })
  221. else
  222. minetest.register_craft({
  223. type = "cooking",
  224. output = "mesecons:wire_00000000_off 2",
  225. recipe = "default:mese_crystal_fragment",
  226. cooktime = 3,
  227. })
  228. minetest.register_craft({
  229. type = "cooking",
  230. output = "mesecons:wire_00000000_off 18",
  231. recipe = "default:mese_crystal",
  232. cooktime = 15,
  233. })
  234. minetest.register_craft({
  235. type = "cooking",
  236. output = "mesecons:wire_00000000_off 162",
  237. recipe = "default:mese",
  238. cooktime = 30,
  239. })
  240. end
  241. minetest.register_craft({
  242. type = "cooking",
  243. output = "mesecons:wire_00000000_off 16",
  244. recipe = "default:mese_crystal",
  245. })