Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tankChestSide = "left"
- local drumName = "thermalexpansion_tank"
- local drumRefillDuration = 8
- local sleepDuration = 4
- local drumRefillTrigger = 0.9
- local redstoneSide = ""
- local redstoneColor = colors.red
- local pDrums = peripheral.getNames()
- local pChest = peripheral.wrap(tankChestSide)
- -- in world tanks
- local drums = {}
- --refill Tanks
- local tanksChest = {}
- local refillingFluid
- local isRefilling = false
- function scanDrums()
- print("scanning connected drums...")
- for i,j in pairs(pDrums) do
- if peripheral.getType(j) == drumName then
- local tankInfo = peripheral.call(j,"getTankInfo")[1]
- local fluid = tankInfo["contents"]
- if fluid ~= nil then
- print("...found: ", fluid["rawName"] , " Drum")
- drums[i] = fluid["rawName"]
- else
- print("...found empty drum-ignore!")
- end
- end
- end
- end
- function scanTanksChest()
- print("scanning chest for refill tanks...")
- if pChest ~= nil then
- local stackCount = pChest.getInventorySize()
- --print(stackCount)
- for i=1,stackCount do
- local stack = pChest.getStackInSlot(i)
- if stack ~= nil then
- local fluidC = stack["fluid_container"]
- if fluidC ~= nil then
- local fluid = fluidC["contents"]
- if fluid == nil then
- print("...found an empty tank in slot ", i)
- else
- print("...found a Tank with ", fluid["rawName"], " in slot ",i)
- tanksChest[fluid["rawName"]] = i
- end
- end
- end
- end
- else
- print("...no chest found")
- end
- end
- local function round(num, n)
- local mult = 10^(n or 0)
- return math.floor(num * mult + 0.5) / mult
- end
- local function persistRefill(name)
- local file = fs.open("refill", "w")
- file.write(name)
- file.close()
- end
- local function unpersistRefill()
- local file = fs.open("refill", "w")
- file.write("")
- file.close()
- end
- local function readPersistendRefill()
- local file = fs.open("refill", "r")
- local fluidName = fs.readLine()
- file.close()
- return fluidName
- end
- local function moveToRefillChest(name)
- local slot = tonumber(tanksChest[name])
- print(slot)
- pChest.pushItem("up", slot)
- refillingFluid = name
- isRefilling = true
- persistRefill(name)
- end
- print("init tanks and drums...")
- print("")
- scanDrums()
- scanTanksChest()
- print("")
- print("...init done")
- local function testFluid(pName)
- local tankInfo = peripheral.call(j, "getTankInfo")[1]
- local fluidMax = tankInfo["capacity"]
- local fluid = tankInfo["contents"]
- local fluidCur = 0.01
- if fluid ~= nil then
- fluidCur = fluid["amount"]
- end
- return round(fluidCur / fluidMax, 2) <= drumRefillTrigger
- end
- while true do
- term.clear()
- term.setCursorPos(1,1)
- if isRefilling == false then
- print("checking fluids...")
- for i,j in pairs(pDrums) do
- if peripheral.getType(j) == drumName then
- local tankInfo = peripheral.call(j, "getTankInfo")[1]
- local fluidMax = tankInfo["capacity"]
- local fluid = tankInfo["contents"]
- local fluidCur = 0.01
- if fluid ~= nil then
- fluidCur = fluid["amount"]
- end
- local fillLevel = round(fluidCur / fluidMax, 2)
- print("...fluid: ", fluid["rawName"] , " ", fluidCur, " / ", fluidMax, " (", fillLevel*100, "%)")
- if fillLevel <= drumRefillTrigger then
- moveToRefillChest(fluid["rawName"])
- break
- end
- end
- end
- sleep(sleepDuration)
- else
- print("refilling ", refillingFluid, "...")
- while testFluid(refillingFluid) == false do
- print("...filling drum")
- sleep(drumRefillDuration)
- end
- refillingFluid = ""
- unpersistRefill()
- isRefilling = false
- --break tank via redstone signal
- rs.setBundledOuput(redstoneSide, redstoneColor)
- sleep(1.5)
- rs.setBundledOuput(redstoneSide, 0)
- print("...drum filled!")
- sleep(sleepDuration)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement