v2.lua 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. distrib2 = distrib2 or {}
  2. distrib2.modpath = minetest.get_modpath("distributer")
  3. distrib2_lv = distrib2_lv or {}
  4. distrib2_mv = distrib2_mv or {}
  5. distrib2_hv = distrib2_hv or {}
  6. for k, v in ipairs({
  7. {tier="lv", up="LV", buffer=tech.distributer_lv.buffer, power=tech.distributer_lv.power},
  8. {tier="mv", up="MV", buffer=tech.distributer_mv.buffer, power=tech.distributer_mv.power},
  9. {tier="hv", up="HV", buffer=tech.distributer_hv.buffer, power=tech.distributer_hv.power},
  10. }) do
  11. -- Which function table are we operating on?
  12. local func = _G["distrib2_" .. v.tier]
  13. func.compose_formspec =
  14. function(pos)
  15. local meta = minetest.get_meta(pos)
  16. local formspec =
  17. "size[5,2.5]" ..
  18. default.gui_bg ..
  19. default.gui_bg_img ..
  20. default.gui_slots ..
  21. "label[0,0.5;Energy Buffer]" ..
  22. "list[context;buffer;0,1;1,1]"
  23. local modename = "Mode: Obtaining EU"
  24. if meta:get_int("toggle") == 1 then
  25. modename = "Mode: Distributing EU"
  26. end
  27. formspec = formspec ..
  28. "label[2,0.5;" .. modename .. "]" ..
  29. "button[2,1;3,1;toggle;Toggle Mode]"
  30. return formspec
  31. end
  32. func.on_receive_fields =
  33. function(pos, formname, fields, sender)
  34. local meta = minetest.get_meta(pos)
  35. if fields.toggle then
  36. if meta:get_int("toggle") == 1 then
  37. meta:set_int("toggle", 0)
  38. else
  39. meta:set_int("toggle", 1)
  40. end
  41. func.trigger_update(pos)
  42. -- Only need update if something changed.
  43. meta:set_string("formspec", func.compose_formspec(pos))
  44. meta:set_string("infotext", func.compose_infotext(pos, false))
  45. end
  46. end
  47. func.compose_infotext =
  48. function(pos, keeprunning)
  49. local active = "Standby"
  50. if keeprunning then
  51. active = "Active"
  52. end
  53. local demand = "Output"
  54. local meta = minetest.get_meta(pos)
  55. if meta:get_int("toggle") == 1 then
  56. demand = "Demand"
  57. end
  58. local amount = v.power
  59. if not keeprunning then
  60. amount = 0
  61. end
  62. local infotext = v.up .. " Grid Splicer (" .. active .. ")\n" ..
  63. demand .. ": " .. amount .. " Per/Sec"
  64. return infotext
  65. end
  66. func.trigger_update =
  67. function(pos)
  68. local timer = minetest.get_node_timer(pos)
  69. -- Restart timer even if already running.
  70. timer:start(1.0)
  71. end
  72. func.on_punch =
  73. function(pos, node, puncher, pointed_thing)
  74. func.trigger_update(pos)
  75. end
  76. func.can_dig =
  77. function(pos, player)
  78. return true
  79. end
  80. func.on_timer =
  81. function(pos, elapsed)
  82. local keeprunning = false
  83. local meta = minetest.get_meta(pos)
  84. local inv = meta:get_inventory()
  85. local owner = meta:get_string("owner")
  86. if meta:get_int("toggle") == 1 then
  87. -- Distribute EU.
  88. -- Get EU from own network and store in buffer.
  89. -- When buffer full, deliver to adjacent consumers.
  90. local energy = inv:get_stack("buffer", 1)
  91. if energy:get_count() < v.buffer then
  92. local gotten = net2.get_energy(pos, owner, v.power, v.tier)
  93. if gotten >= v.power then
  94. keeprunning = true
  95. end
  96. gotten = energy:get_count() + gotten
  97. inv:set_stack("buffer", 1, "atomic:energy " .. gotten)
  98. else
  99. local adjacent = {
  100. {x=pos.x+1, y=pos.y, z=pos.z},
  101. {x=pos.x-1, y=pos.y, z=pos.z},
  102. {x=pos.x, y=pos.y+1, z=pos.z},
  103. {x=pos.x, y=pos.y-1, z=pos.z},
  104. {x=pos.x, y=pos.y, z=pos.z+1},
  105. {x=pos.x, y=pos.y, z=pos.z-1},
  106. }
  107. local targets = {}
  108. local machine = "distrib2:" .. v.tier .. "_machine"
  109. for i, j in ipairs(adjacent) do
  110. if minetest.get_node(j).name == machine then
  111. local m2 = minetest.get_meta(j)
  112. local i2 = m2:get_inventory()
  113. --if m2:get_string("owner") ~= owner then
  114. if i2:get_stack("buffer", 1):get_count() < v.buffer then
  115. targets[#targets+1] = j
  116. end
  117. --end
  118. end
  119. end
  120. if #targets > 0 then
  121. local energy = inv:get_stack("buffer", 1)
  122. local toeach = math.floor(energy:get_count() / (#targets))
  123. toeach = math.floor(toeach * 0.8) -- 80% efficiency.
  124. if toeach > 0 then
  125. for i, j in ipairs(targets) do
  126. local m2 = minetest.get_meta(j)
  127. local i2 = m2:get_inventory()
  128. local e2 = i2:get_stack("buffer", 1)
  129. i2:set_stack("buffer", 1, "atomic:energy " .. (e2:get_count() + toeach))
  130. if i2:get_stack("buffer", 1):get_count() >= v.buffer then
  131. func.trigger_update(j)
  132. end
  133. end
  134. keeprunning = true
  135. end
  136. inv:set_stack("buffer", 1, ItemStack(""))
  137. end
  138. end
  139. else
  140. -- Obtain EU.
  141. -- Deliver EU currently in buffer to own network.
  142. local energy = inv:get_stack("buffer", 1)
  143. if energy:get_count() > 0 then
  144. local give = energy:get_count()
  145. if give > v.power then give = v.power end
  146. local left = net2.put_energy(pos, owner, give, v.tier)
  147. if left == 0 then
  148. keeprunning = true
  149. end
  150. local sub = (give - left)
  151. inv:set_stack("buffer", 1, "atomic:energy " .. (energy:get_count() - sub))
  152. end
  153. end
  154. meta:set_string("infotext", func.compose_infotext(pos, keeprunning))
  155. if keeprunning then
  156. minetest.get_node_timer(pos):start(1.0)
  157. else
  158. minetest.get_node_timer(pos):start(math.random(1, 60*3))
  159. end
  160. end
  161. func.on_construct =
  162. function(pos)
  163. end
  164. func.after_place_node =
  165. function(pos, placer, itemstack, pointed_thing)
  166. local meta = minetest.get_meta(pos)
  167. local node = minetest.get_node(pos)
  168. local owner = placer:get_player_name()
  169. local inv = meta:get_inventory()
  170. meta:set_string("owner", owner)
  171. meta:set_string("nodename", node.name)
  172. inv:set_size("buffer", 1)
  173. net2.clear_caches(pos, owner, v.tier)
  174. meta:set_string("formspec", func.compose_formspec(pos))
  175. meta:set_string("infotext", func.compose_infotext(pos, false))
  176. nodestore.add_node(pos)
  177. local timer = minetest.get_node_timer(pos)
  178. timer:start(1.0)
  179. end
  180. func.on_blast =
  181. function(pos)
  182. local drops = {}
  183. drops[#drops+1] = "distrib2:" .. v.tier .. "_machine"
  184. minetest.remove_node(pos)
  185. return drops
  186. end
  187. func.allow_metadata_inventory_put =
  188. function(pos, listname, index, stack, player)
  189. return 0
  190. end
  191. func.allow_metadata_inventory_move =
  192. function(pos, from_list, from_index, to_list, to_index, count, player)
  193. return 0
  194. end
  195. func.allow_metadata_inventory_take =
  196. function(pos, listname, index, stack, player)
  197. return 0
  198. end
  199. func.on_metadata_inventory_move =
  200. function(pos)
  201. func.trigger_update(pos)
  202. end
  203. func.on_metadata_inventory_put =
  204. function(pos)
  205. func.trigger_update(pos)
  206. end
  207. func.on_metadata_inventory_take =
  208. function(pos, listname, index, stack, player)
  209. func.trigger_update(pos)
  210. end
  211. func.on_destruct =
  212. function(pos)
  213. local meta = minetest.get_meta(pos)
  214. local owner = meta:get_string("owner")
  215. net2.clear_caches(pos, owner, v.tier)
  216. nodestore.del_node(pos)
  217. end
  218. end
  219. if not distrib2.run_once then
  220. for m, n in ipairs({
  221. {tier="lv", up="LV"},
  222. {tier="mv", up="MV"},
  223. {tier="hv", up="HV"},
  224. }) do
  225. -- Which function table are we operating on?
  226. local func = _G["distrib2_" .. n.tier]
  227. minetest.register_node(":distrib2:" .. n.tier .. "_machine", {
  228. description = n.up .. " Grid Splicer\n\nThis machine connects energy grids having different owners.\nNormally all machines and cables in a network must have the same owner.\nThis machine allows to share power between networks with different owners.",
  229. tiles = {
  230. "network_connector_" .. n.tier .. "_top.png", "network_connector_" .. n.tier .. "_top.png",
  231. "network_connector_" .. n.tier .. "_side.png", "network_connector_" .. n.tier .. "_side.png",
  232. "network_connector_" .. n.tier .. "_side.png", "network_connector_" .. n.tier .. "_side.png",
  233. },
  234. groups = utility.dig_groups("machine"),
  235. paramtype2 = "facedir",
  236. is_ground_content = false,
  237. sounds = default.node_sound_metal_defaults(),
  238. drop = "distrib2:" .. n.tier .. "_machine",
  239. on_rotate = function(...)
  240. return screwdriver.rotate_simple(...) end,
  241. allow_metadata_inventory_put = function(...)
  242. return func.allow_metadata_inventory_put(...) end,
  243. allow_metadata_inventory_move = function(...)
  244. return func.allow_metadata_inventory_move(...) end,
  245. allow_metadata_inventory_take = function(...)
  246. return func.allow_metadata_inventory_take(...) end,
  247. on_metadata_inventory_move = function(...)
  248. return func.on_metadata_inventory_move(...) end,
  249. on_metadata_inventory_put = function(...)
  250. return func.on_metadata_inventory_put(...) end,
  251. on_metadata_inventory_take = function(...)
  252. return func.on_metadata_inventory_take(...) end,
  253. on_punch = function(...)
  254. return func.on_punch(...) end,
  255. can_dig = function(...)
  256. return func.can_dig(...) end,
  257. on_timer = function(...)
  258. return func.on_timer(...) end,
  259. on_construct = function(...)
  260. return func.on_construct(...) end,
  261. on_destruct = function(...)
  262. return func.on_destruct(...) end,
  263. on_blast = function(...)
  264. return func.on_blast(...) end,
  265. after_place_node = function(...)
  266. return func.after_place_node(...) end,
  267. on_receive_fields = function(...)
  268. return func.on_receive_fields(...) end,
  269. })
  270. end
  271. minetest.register_craft({
  272. output = 'distrib2:lv_machine',
  273. recipe = {
  274. {'fine_wire:gold', 'rubber:rubber_fiber', 'silicon:doped_wafer'},
  275. {'cb2:lv', 'techcrafts:machine_casing', 'cb2:lv'},
  276. {'techcrafts:control_logic_unit', 'rubber:rubber_fiber', 'fine_wire:silver'},
  277. }
  278. })
  279. minetest.register_craft({
  280. output = 'distrib2:mv_machine',
  281. recipe = {
  282. {'carbon_steel:ingot', 'rubber:rubber_fiber', 'carbon_steel:ingot'},
  283. {'cb2:mv', 'distrib2:lv_machine', 'cb2:mv'},
  284. {'carbon_steel:ingot', 'rubber:rubber_fiber', 'carbon_steel:ingot'},
  285. }
  286. })
  287. minetest.register_craft({
  288. output = 'distrib2:hv_machine',
  289. recipe = {
  290. {'stainless_steel:ingot', 'rubber:rubber_fiber', 'stainless_steel:ingot'},
  291. {'cb2:hv', 'distrib2:mv_machine', 'cb2:hv'},
  292. {'stainless_steel:ingot', 'rubber:rubber_fiber', 'stainless_steel:ingot'},
  293. }
  294. })
  295. local c = "distrib2:core"
  296. local f = distrib2.modpath .. "/v2.lua"
  297. reload.register_file(c, f, false)
  298. distrib2.run_once = true
  299. end