Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. local tankChestSide = "left"
  2. local drumName = "thermalexpansion_tank"
  3. local drumRefillDuration = 8
  4. local sleepDuration = 4
  5. local drumRefillTrigger = 0.9
  6. local redstoneSide = ""
  7. local redstoneColor = colors.red
  8.  
  9. local pDrums = peripheral.getNames()
  10. local pChest = peripheral.wrap(tankChestSide)
  11.  
  12. -- in world tanks
  13. local drums = {}
  14. --refill Tanks
  15. local tanksChest = {}
  16.  
  17. local refillingFluid
  18. local isRefilling = false
  19.  
  20.  
  21. function scanDrums()
  22. print("scanning connected drums...")
  23. for i,j in pairs(pDrums) do
  24. if peripheral.getType(j) == drumName then
  25. local tankInfo = peripheral.call(j,"getTankInfo")[1]
  26. local fluid = tankInfo["contents"]
  27. if fluid ~= nil then
  28. print("...found: ", fluid["rawName"] , " Drum")
  29. drums[i] = fluid["rawName"]
  30. else
  31. print("...found empty drum-ignore!")
  32. end
  33. end
  34. end
  35. end
  36.  
  37. function scanTanksChest()
  38. print("scanning chest for refill tanks...")
  39. if pChest ~= nil then
  40. local stackCount = pChest.getInventorySize()
  41. --print(stackCount)
  42. for i=1,stackCount do
  43. local stack = pChest.getStackInSlot(i)
  44. if stack ~= nil then
  45. local fluidC = stack["fluid_container"]
  46. if fluidC ~= nil then
  47. local fluid = fluidC["contents"]
  48. if fluid == nil then
  49. print("...found an empty tank in slot ", i)
  50. else
  51. print("...found a Tank with ", fluid["rawName"], " in slot ",i)
  52. tanksChest[fluid["rawName"]] = i
  53. end
  54. end
  55. end
  56. end
  57. else
  58. print("...no chest found")
  59. end
  60. end
  61.  
  62. local function round(num, n)
  63. local mult = 10^(n or 0)
  64. return math.floor(num * mult + 0.5) / mult
  65. end
  66.  
  67. local function persistRefill(name)
  68. local file = fs.open("refill", "w")
  69. file.write(name)
  70. file.close()
  71. end
  72.  
  73. local function unpersistRefill()
  74. local file = fs.open("refill", "w")
  75. file.write("")
  76. file.close()
  77. end
  78.  
  79. local function readPersistendRefill()
  80. local file = fs.open("refill", "r")
  81. local fluidName = fs.readLine()
  82. file.close()
  83. return fluidName
  84. end
  85.  
  86. local function moveToRefillChest(name)
  87. local slot = tonumber(tanksChest[name])
  88. print(slot)
  89. pChest.pushItem("up", slot)
  90. refillingFluid = name
  91. isRefilling = true
  92. persistRefill(name)
  93. end
  94.  
  95. print("init tanks and drums...")
  96. print("")
  97. scanDrums()
  98. scanTanksChest()
  99.  
  100. print("")
  101. print("...init done")
  102.  
  103. local function testFluid(pName)
  104. local tankInfo = peripheral.call(j, "getTankInfo")[1]
  105. local fluidMax = tankInfo["capacity"]
  106. local fluid = tankInfo["contents"]
  107. local fluidCur = 0.01
  108. if fluid ~= nil then
  109. fluidCur = fluid["amount"]
  110. end
  111. return round(fluidCur / fluidMax, 2) <= drumRefillTrigger
  112. end
  113.  
  114.  
  115. while true do
  116. term.clear()
  117. term.setCursorPos(1,1)
  118. if isRefilling == false then
  119. print("checking fluids...")
  120. for i,j in pairs(pDrums) do
  121. if peripheral.getType(j) == drumName then
  122. local tankInfo = peripheral.call(j, "getTankInfo")[1]
  123. local fluidMax = tankInfo["capacity"]
  124. local fluid = tankInfo["contents"]
  125. local fluidCur = 0.01
  126. if fluid ~= nil then
  127. fluidCur = fluid["amount"]
  128. end
  129. local fillLevel = round(fluidCur / fluidMax, 2)
  130. print("...fluid: ", fluid["rawName"] , " ", fluidCur, " / ", fluidMax, " (", fillLevel*100, "%)")
  131. if fillLevel <= drumRefillTrigger then
  132. moveToRefillChest(fluid["rawName"])
  133. break
  134. end
  135. end
  136. end
  137. sleep(sleepDuration)
  138. else
  139. print("refilling ", refillingFluid, "...")
  140. while testFluid(refillingFluid) == false do
  141. print("...filling drum")
  142. sleep(drumRefillDuration)
  143. end
  144. refillingFluid = ""
  145. unpersistRefill()
  146. isRefilling = false
  147. --break tank via redstone signal
  148. rs.setBundledOuput(redstoneSide, redstoneColor)
  149. sleep(1.5)
  150. rs.setBundledOuput(redstoneSide, 0)
  151. print("...drum filled!")
  152. sleep(sleepDuration)
  153. end
  154. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement