init.lua 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. cable = cable or {}
  2. cable.modpath = minetest.get_modpath("cable")
  3. cable_hv = cable_hv or {}
  4. cable_mv = cable_mv or {}
  5. cable_lv = cable_lv or {}
  6. cb2_hv = cb2_hv or {}
  7. cb2_mv = cb2_mv or {}
  8. cb2_lv = cb2_lv or {}
  9. -- Ensure tables exist.
  10. stat2 = stat2 or {}
  11. stat2_hv = stat2_hv or {}
  12. stat2_mv = stat2_mv or {}
  13. stat2_lv = stat2_lv or {}
  14. -- API function. Get max length of cable, per tier.
  15. cable.get_max_length =
  16. function(tier)
  17. -- Fixed lengths! Network caching code relies on these values.
  18. if tier == "lv" then
  19. return 16
  20. elseif tier == "mv" then
  21. return 24
  22. elseif tier == "hv" then
  23. return 32
  24. end
  25. return 0
  26. end
  27. -- Register cable functions.
  28. for k, v in ipairs({
  29. {tier="hv"},
  30. {tier="mv"},
  31. {tier="lv"},
  32. }) do
  33. _G["cable_" .. v.tier].after_place_node =
  34. function(pos, placer, itemstack, pointed_thing)
  35. networks.invalidate_hubs(pos, v.tier)
  36. end
  37. _G["cable_" .. v.tier].on_destruct =
  38. function(pos)
  39. networks.invalidate_hubs(pos, v.tier)
  40. end
  41. _G["cable_" .. v.tier].on_rotate =
  42. function(pos, node, user, mode, new_param2)
  43. networks.invalidate_hubs(pos, v.tier)
  44. return nil -- Screwdriver may rotate.
  45. end
  46. end
  47. local function choose_rotation(pos, tier)
  48. local positions = {
  49. {pos={x=pos.x+1, y=pos.y, z=pos.z}, param2=0},
  50. {pos={x=pos.x-1, y=pos.y, z=pos.z}, param2=2},
  51. {pos={x=pos.x, y=pos.y+1, z=pos.z}, param2=5},
  52. {pos={x=pos.x, y=pos.y-1, z=pos.z}, param2=7},
  53. {pos={x=pos.x, y=pos.y, z=pos.z+1}, param2=1},
  54. {pos={x=pos.x, y=pos.y, z=pos.z-1}, param2=3},
  55. }
  56. local rn = "cb2:" .. tier
  57. for k, v in ipairs(positions) do
  58. local node = minetest.get_node(v.pos)
  59. if node.name == rn then
  60. local n2 = minetest.get_node(pos)
  61. n2.param2 = node.param2
  62. minetest.swap_node(pos, n2)
  63. return
  64. end
  65. end
  66. local sn = "stat2:" .. tier
  67. for k, v in ipairs(positions) do
  68. local node = minetest.get_node(v.pos)
  69. if node.name == sn then
  70. local n2 = minetest.get_node(pos)
  71. n2.param2 = v.param2
  72. minetest.swap_node(pos, n2)
  73. return
  74. end
  75. end
  76. end
  77. -- Register NEW cable functions.
  78. for k, v in ipairs({
  79. {tier="hv"},
  80. {tier="mv"},
  81. {tier="lv"},
  82. }) do
  83. _G["cb2_" .. v.tier].after_place_node =
  84. function(pos, placer, itemstack, pointed_thing)
  85. choose_rotation(pos, v.tier)
  86. stat2.invalidate_hubs(pos, v.tier)
  87. end
  88. _G["cb2_" .. v.tier].on_destruct =
  89. function(pos)
  90. stat2.invalidate_hubs(pos, v.tier)
  91. end
  92. _G["cb2_" .. v.tier].on_rotate =
  93. function(pos, node, user, mode, new_param2)
  94. stat2.invalidate_hubs(pos, v.tier)
  95. return nil -- Screwdriver may rotate.
  96. end
  97. end
  98. if not cable.run_once then
  99. for k, v in ipairs({
  100. {tier="hv", name="HV Cable", tile="cable_hv.png", boxd=6},
  101. {tier="mv", name="MV Cable", tile="cable_mv.png", boxd=7},
  102. {tier="lv", name="LV Cable", tile="cable_lv.png", boxd=8},
  103. }) do
  104. -- Which function table do we use?
  105. local functable = _G["cable_" .. v.tier]
  106. minetest.register_node("cable:" .. v.tier, {
  107. description = v.name .. " [Max length: " .. cable.get_max_length(v.tier) .. " meters.]",
  108. tiles = {v.tile},
  109. groups = utility.dig_groups("bigitem", {
  110. immovable = 1,
  111. }),
  112. drop = "cb2:" .. v.tier,
  113. paramtype = "light",
  114. drawtype = "nodebox",
  115. node_box = {
  116. type = "fixed",
  117. fixed = {-0.5, -1/v.boxd, -1/v.boxd, 0.5, 1/v.boxd, 1/v.boxd},
  118. },
  119. paramtype2 = "facedir",
  120. is_ground_content = false,
  121. sounds = default.node_sound_wood_defaults(),
  122. on_rotate = function(...)
  123. return functable.on_rotate(...) end,
  124. after_place_node = function(...)
  125. return functable.after_place_node(...) end,
  126. on_destruct = function(...)
  127. return functable.on_destruct(...) end,
  128. })
  129. end
  130. minetest.register_alias("switching_station:cable", "cable:hv")
  131. minetest.register_craft({
  132. output = "cb2:hv 3",
  133. recipe = {
  134. {'plastic:plastic_sheeting', 'plastic:plastic_sheeting', 'plastic:plastic_sheeting'},
  135. {'cb2:mv', 'cb2:mv', 'cb2:mv'},
  136. {'plastic:plastic_sheeting', 'plastic:plastic_sheeting', 'plastic:plastic_sheeting'},
  137. },
  138. })
  139. minetest.register_craft({
  140. output = "cb2:mv 3",
  141. recipe = {
  142. {'rubber:rubber_fiber', 'rubber:rubber_fiber', 'rubber:rubber_fiber'},
  143. {'cb2:lv', 'cb2:lv', 'cb2:lv'},
  144. {'rubber:rubber_fiber', 'rubber:rubber_fiber', 'rubber:rubber_fiber'},
  145. },
  146. })
  147. minetest.register_craft({
  148. output = "cb2:lv 3",
  149. recipe = {
  150. {'default:paper', 'default:paper', 'default:paper'},
  151. {'default:copper_ingot', 'default:copper_ingot', 'default:copper_ingot'},
  152. {'default:paper', 'default:paper', 'default:paper'},
  153. },
  154. })
  155. -- New cable types.
  156. for k, v in ipairs({
  157. {tier="hv", name="HV Cable", tile="cable_hv.png", w=5},
  158. {tier="mv", name="MV Cable", tile="cable_mv.png", w=5.5},
  159. {tier="lv", name="LV Cable", tile="cable_lv.png", w=6},
  160. }) do
  161. -- Which function table do we use?
  162. local functable = _G["cb2_" .. v.tier]
  163. local function transform(nodebox)
  164. for k, v in ipairs(nodebox) do
  165. for m, n in ipairs(v) do
  166. local p = nodebox[k][m]
  167. p = p / 16
  168. p = p - 0.5
  169. nodebox[k][m] = p
  170. end
  171. end
  172. end
  173. local nodebox = {
  174. {-2, v.w, v.w, 18, 16-v.w, 16-v.w},
  175. }
  176. local selectionbox = {
  177. {0, v.w, v.w, 16, 16-v.w, 16-v.w},
  178. }
  179. transform(nodebox)
  180. transform(selectionbox)
  181. minetest.register_node(":cb2:" .. v.tier, {
  182. description = v.name .. " [Max Length: " .. cable.get_max_length(v.tier) .. " Meters.]",
  183. tiles = {v.tile},
  184. groups = utility.dig_groups("bigitem"),
  185. paramtype = "light",
  186. drawtype = "nodebox",
  187. node_box = {
  188. type = "fixed",
  189. fixed = nodebox,
  190. },
  191. selection_box = {
  192. type = "fixed",
  193. fixed = selectionbox,
  194. },
  195. paramtype2 = "facedir",
  196. is_ground_content = false,
  197. sounds = default.node_sound_wood_defaults(),
  198. on_rotate = function(...)
  199. return functable.on_rotate(...) end,
  200. after_place_node = function(...)
  201. return functable.after_place_node(...) end,
  202. on_destruct = function(...)
  203. return functable.on_destruct(...) end,
  204. })
  205. end
  206. local c = "cable:core"
  207. local f = cable.modpath .. "/init.lua"
  208. reload.register_file(c, f, false)
  209. cable.run_once = true
  210. end