v2.lua 9.7 KB

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