Advertisement
RLPGhost

Lua Tank Control

Mar 17th, 2015
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.54 KB | None | 0 0
  1. local tank = peripheral.wrap("back")
  2.  
  3. local cap                -- Tank capacity
  4. local amount             -- Amount liquid in tank
  5. local percentfull        -- Percent liquid in tank
  6.  
  7. function getTank(tankPeriph)
  8.     local tableInfo = tankPeriph.getTankInfo("unknown") -- Local to the getTank function.
  9.  
  10.     fluidRaw = nil
  11.     fluidName = nil
  12.     fluidAmount = nil
  13.     fluidCapacity = nil
  14.      
  15.     for k,v in pairs(tableInfo) do
  16.       fluidCapacity = v.capacity
  17.  
  18.       if v.contents then
  19.         for i,w in pairs(v.contents) do
  20.           if i == "rawName" then
  21.             fluidRaw = w
  22.           elseif i == "amount" then
  23.             fluidAmount = w
  24.           elseif i == "name" then
  25.             fluidName = w
  26.           end
  27.         end
  28.       end
  29.     end
  30.  
  31.     return fluidRaw, fluidName, fluidAmount, fluidCapacity -- Returning the values of global variables (which are nil).
  32. end
  33.  
  34. while true do
  35.  
  36. local fluidRaw, fluidName, fluidAmount, fluidCapacity = getTank(tank)
  37.  
  38. if fluidName then
  39.     -- Get values for tank capacity and amount
  40.     fluidName = string.gsub(fluidName, "^%l", string.upper)
  41.     cap = fluidCapacity / 1000
  42.     amount = fluidAmount
  43. end
  44.  
  45. if amount == nil then
  46.       amount = 0
  47.       percentfull = 0
  48.     else
  49.       -- Use math.floor to convert to integers
  50.       amount = math.floor(amount / 1000)
  51.       percentfull = math.floor(100 * amount / cap)
  52. end
  53.    
  54. if percentfull < 40 then
  55.     redstone.setOutput("bottom", true)
  56. end
  57.  
  58. if percentfull > 60 then
  59.     redstone.setOutput("bottom", false)
  60. end
  61.  
  62. sleep(2)
  63.  
  64. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement