akihex

coolRun

Apr 26th, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.49 KB | None | 0 0
  1. local reactorDir = "left"
  2. local switchDir = "front"
  3. local interfaceDir = "north"
  4. local turtleDir = "east"
  5. local coolant = "back"
  6. local coolantID = 30099
  7. local maxTemp = 9000
  8. local maxDmg = 9000
  9. local tempMin = 6000
  10. local tempMax = 7500
  11.  
  12. local reactor = peripheral.wrap(reactorDir)
  13.  
  14. local items = nil
  15. local powered = false
  16. local on = true
  17. local off = false
  18. local critical = false
  19.  
  20. local function switch(onOff)
  21.   rs.setOutput(switchDir,onOff)
  22.   powered = onOff   --reactor.isActive()  --onOff
  23.   if onOff and powered then print("Reactor switched on: "..reactor.getHeat())
  24.   else print("Reactor switched off: "..reactor.getHeat()) end
  25. end
  26.  
  27. local function request(direction)
  28.   rs.setOutput(direction,true)
  29.   sleep(0.1)
  30.   rs.setOutput(direction,false)
  31. end
  32.  
  33. local function checkInv(i)
  34.   local result = true
  35.   if turtle.getItemCount(i) > 0 then
  36.     print("Item found in slot " .. i)
  37.     result = false
  38.   end
  39.   return result
  40. end
  41.  
  42. local nMove = 0
  43. local item = nil
  44.  
  45. local function exchangeParts()
  46.   items = reactor.getAllStacks()
  47.   for i,k in pairs(items) do
  48.     if k.id == coolantID then
  49.       if k.dmg >= maxDmg then
  50.         --if powered then switch(off) end
  51.         switch(off)
  52.         --print("Reactor switched off for maintenance")
  53.         nMove = 0
  54.         while nMove ~= 1 do
  55.           print("Attempting to remove cooling cell in slot " .. i)
  56.           nMove = reactor.pushItem(interfaceDir,i,1)
  57.           if nMove ~= 1 then sleep(1) end
  58.         end
  59.         print("Spent coolant cell removed")
  60.         if not checkInv(1) then
  61.        --   print("wtf")
  62.        --   critical = false
  63.        --   break
  64.           reactor.pullItemIntoSlot(turtleDir, 1, 1, i)
  65.           item = reactor.getStackInSlot(i)
  66.           if item and item.id == coolantID then print("Coolant exchanged in slot "..i)
  67.           else
  68.             print("Coolant exchange failed in slot "..i)
  69.             critical = true
  70.             break
  71.           end
  72.         else
  73.           print("Requesting coolant cell for slot "..i)
  74.           item = reactor.getStackInSlot(i)
  75.           while not item do
  76.             if checkInv(1) then request(coolant) end
  77.             reactor.pullItemIntoSlot(turtleDir, 1, 1, i)
  78.             item = reactor.getStackInSlot(i)
  79.           end
  80.           if item and item.id == coolantID then print("Coolant exchanged in slot "..i)
  81.           else
  82.             print("Coolant exchange failed in slot "..i)
  83.             critical = true
  84.             break
  85.           end
  86.         end  
  87.         item = reactor.getStackInSlot(i)
  88.         if not item or item.id ~= coolantID then
  89.           print("no coolant in slot "..i)
  90.           critical = true
  91.           break
  92.         end
  93.       end
  94.     end
  95.   end
  96. end
  97.  
  98. local function checkTemp()
  99.   local T = reactor.getHeat()
  100.   if T < tempMin then
  101.     temperature = 1
  102.   elseif T < tempMax then
  103.     temperature = 0
  104.   elseif T < maxTemp then
  105.     print("Cooldown phase activated: "..T)
  106.     temperature = -1
  107.   else
  108.     switch(off)
  109.     print("Temperature above limit: "..T)
  110.     critical = true
  111.     on = false
  112.   end
  113. end
  114.  
  115. local function changeTemp()
  116.  
  117. end
  118.  
  119. while true do
  120.   if powered and reactor.getHeat() >= maxTemp then switch(off) end
  121.   if not powered and reactor.getHeat() < maxTemp then switch(on) end
  122.   checkTemp()
  123.   exchangeParts()
  124.   if not critical and not powered then
  125.     sleep(0.1)
  126.     switch(on)
  127.   elseif critical then
  128.     switch(off)
  129.     on = false
  130.     print("Critical Error: All systems switched off")
  131.     break
  132.   end
  133.   sleep(1)
  134. end
Advertisement
Add Comment
Please, Sign In to add comment