ksbd

BatteryBox refill dead battery (placement side)

Feb 24th, 2013
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.55 KB | None | 0 0
  1. --Variables:
  2.  
  3. local firstTimeSetup = "yes"
  4. local taskDone = "yes"
  5. local task = "waiting"
  6. local waitTime = 30
  7. local waitTimeMax = 300
  8. local emptyCount
  9.  
  10.  
  11. local varStorage = {}
  12.  
  13. --Functions:
  14.  
  15. function storeVariables()
  16.     varStorage.task = task
  17.     varStorage.taskDone = taskDone
  18.     --The above stores the variables in the table
  19.    
  20.     local output = textutils.serialize(varStorage)
  21.     --This converts the table to a string
  22.  
  23.     local handle = assert(fs.open("vars", "w"), "Couldn't save vars")
  24.     handle.write(output)
  25.     handle.close()
  26. end
  27.  
  28. function getVariables()
  29.     local handle = assert(fs.open("vars", "r"), "Couldn't load vars")
  30.     local input = handle.readAll()
  31.     handle.close()
  32.  
  33.     local destination = textutils.unserialize(input)
  34.     task = destination.task
  35.     taskDone = destination.taskDone
  36. end
  37.  
  38. function clear()
  39.     term.clear()
  40.     term.setCursorPos(1,1)
  41. end
  42.  
  43. function getBat()
  44.     task = "gettingBattery"
  45.     taskDone = "no"
  46.     storeVariables()
  47.     turtle.select(1)
  48.     turtle.suck()
  49.     emptyCount = turtle.getItemCount(1)
  50.     if emptyCount == 0 then print("No more than 0")
  51.         while emptyCount == 0 do
  52.             turtle.select(1)
  53.             turtle.suck()
  54.             emptyCount = turtle.getItemCount(1)
  55.             sleep(waitTime)
  56.         end
  57.     end
  58.     if emptyCount > 0 then print("Got more than 0")
  59.         if emptyCount == 1 then print("Got 1") elseif emptyCount > 1 then print("More than 1")
  60.             emptyCount = emptyCount - 1
  61.             turtle.drop(emptyCount)
  62.         end
  63.     end
  64.     taskDone = "yes"
  65. end
  66.  
  67. function putBat()
  68.     task = "puttingBattery"
  69.     taskDone = "no"
  70.     storeVariables()
  71.     turtle.select(1)
  72.     turtle.dropUp()
  73.     taskDone = "yes"
  74. end
  75.  
  76. function wait()
  77.     task = "waiting"
  78.     taskDone = "no"
  79.     storeVariables()
  80.     sleep(waitTime)
  81.     taskDone = "yes"
  82. end
  83.  
  84. function waitMax()
  85.     task = "waiting"
  86.     taskDone = "no"
  87.     storeVariables()
  88.     sleep(waitTimeMax)
  89.     taskDone = "yes"
  90. end
  91.  
  92. function pickUp()
  93.     task = "pickingUp"
  94.     taskDone = "no"
  95.     storeVariables()
  96.     turtle.select(1)
  97.     turtle.digDown()
  98.     taskDone = "yes"
  99. end
  100.  
  101. function place()
  102.     task = "placing"
  103.     taskDone = "no"
  104.     storeVariables()
  105.     turtle.select(1)
  106.     turtle.placeDown()
  107.     taskDone = "yes"
  108. end
  109.  
  110. function operation()
  111.     pickUp()
  112.     putBat()
  113.     getBat()
  114.     place()
  115.     wait()
  116.     waitMax()
  117. end
  118.  
  119. --Main Program:
  120.  
  121. --First startup run:
  122.  
  123. if firstTimeSetup == "yes" then
  124.     clear()
  125.     print("Don't forget to change the *firstTimeSetup = 'yes'* variable to 'no'!")
  126.     print("Do this by typing 'edit FileName'")
  127.     print("(FileName = what you named the program)")
  128.     print("into the console after reboot.")
  129.     print()
  130.     textutils.slowPrint("Saving StartUp variables...")
  131.     print()
  132.     print()
  133.     term.write("Press any key to reboot:") io.read()
  134.     storeVariables()
  135.     sleep(1)
  136.     os.reboot()
  137. end
  138.  
  139. --StartUp:
  140.  
  141. getVariables()
  142.  
  143. if task == "pickingUp" then
  144.     if taskDone == "yes" then
  145.         putBat()
  146.         getBat()
  147.         place()
  148.         wait()
  149.         waitMax()
  150.     elseif taskDone == "no" then
  151.         pickUp()
  152.         putBat()
  153.         getBat()
  154.         place()
  155.         wait()
  156.         waitMax()
  157.     end
  158. elseif task == "puttingBattery" then
  159.     if taskDone == "yes" then
  160.         getBat()
  161.         place()
  162.         wait()
  163.         waitMax()
  164.     elseif taskDone == "no" then
  165.         putBat()
  166.         getBat()
  167.         place()
  168.         wait()
  169.         waitMax()
  170.     end
  171. elseif task == "gettingBattery" then
  172.     if taskDone == "yes" then
  173.         place()
  174.         wait()
  175.         waitMax()
  176.     elseif taskDone == "no" then
  177.         getBat()
  178.         place()
  179.         wait()
  180.         waitMax()
  181.     end
  182. elseif task == "placing" then
  183.     if taskDone == "yes" then
  184.         wait()
  185.         waitMax()
  186.     elseif taskDone == "no" then
  187.         place()
  188.         wait()
  189.         waitMax()
  190.     end
  191. elseif task == "waiting" then
  192.     if taskDone == "no" then
  193.         wait()
  194.     end
  195. end
  196. --End of StartUp
  197.  
  198. while true do
  199.     operation()
  200. end
Advertisement
Add Comment
Please, Sign In to add comment