Advertisement
YK7942

Potion Turtle v1.2

Apr 14th, 2014
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 23.05 KB | None | 0 0
  1. --[[TODO LIST
  2. 1. make prompts blink/countdown to make them stand out to the user --temp solution implemented
  3. 4. modify menu routine to have +duration/+effect/splash sub menus for each of the primary potions
  4. 5. make the apothecary double sided for increased capacity
  5. 6. allow for choosing of build position above/in/below ground
  6. 7. break up Main() into simpler functions ie select, build, brew
  7. 8. turtle identifies a fully stocked apothecary and sleeps longer
  8. ]]
  9.  
  10. --[[THOUGHTS
  11. 5. need to give instruction for usage RE: end chest (probably on the CC page)
  12. ]]
  13.  
  14. function Main() --Control center for the program DONE
  15.     --potion selection
  16.     local ApothecarySize
  17.     if DoesOldSaveExist() then
  18.         if IsNewSetWanted() then
  19.             ApothecarySize = GetSize()
  20.             potionList = RunMenu(ApothecarySize)
  21.             SaveFile(potionList)
  22.         else
  23.             potionList = LoadFile()
  24.             ApothecarySize = #potionList
  25.         end
  26.     else
  27.         ApothecarySize = GetSize()
  28.         potionList = RunMenu(ApothecarySize)
  29.         SaveFile(potionList)
  30.     end
  31.     --construction
  32.     if IsBuilderWanted() then
  33.         WaitForInventory(ApothecarySize)
  34.         SurveySite(ApothecarySize)
  35.         AutoBuilder(ApothecarySize)
  36.     end
  37.     inventory, reservedSlots = DefineInventory(potionList)
  38.     --potion brewing
  39.     FindHome(ApothecarySize)
  40.     CleanUp(reservedSlots)
  41.     shelfNo = 1
  42.     while true do
  43.         if IsShelfFull(shelfNo,reservedSlots) then
  44.             --Do nothing
  45.         else
  46.             if IsEnoughIngredients(potionList[shelfNo],inventory) then
  47.                 FillBottles(reservedSlots)
  48.                 BrewPotion(potionList[shelfNo],inventory)
  49.                 RestockShelf(shelfNo)
  50.             else
  51.                 RestockTurtle(reservedSlots)
  52.             end
  53.         end
  54.        
  55.         if shelfNo >= ApothecarySize then
  56.             Rest(60,"sleep","Cycle complete.")
  57.             shelfNo = 1
  58.         else
  59.             shelfNo = shelfNo + 1
  60.         end
  61.     end
  62. end
  63.  
  64.  
  65. function DoesOldSaveExist() --Is there an old save file existing? DONE
  66.     print("Searching for existing save file..")
  67.     local fileName = os.getComputerID()..".potions" --computer ID with the extension .potions
  68.     local file = io.open(fileName,"r")
  69.     if file == nil then
  70.         return false
  71.     else
  72.         file:close()
  73.         return true
  74.     end
  75. end
  76.  
  77.  
  78. function IsNewSetWanted() --do you need to choose a new potion set? DONE
  79.     term.clear() term.setCursorPos(1,1)
  80.     print("Would you like to select a new set of potions?? \nYou have 20 seconds to respond (Y/N)")
  81.     local timer = os.startTimer(20) --#The interval that you would like to wait
  82.     while true do
  83.         local event, result = os.pullEvent()
  84.         if event == "timer" then --#If a timer event is pulled and its ID matches the one returned by startTimer
  85.             return false
  86.         elseif event == "key" then
  87.             if result == keys.enter or result == keys.y then return true
  88.             elseif result == keys.n then return false              
  89.             end
  90.         end
  91.     end
  92. end
  93.  
  94.  
  95. function GetSize() --ask user what size the apothecary should be DONE
  96.     term.clear() term.setCursorPos(1,1)
  97.     local maximum = 9 --maximum due to building materials is 17
  98.     local minimum = 4
  99.     local default = minimum
  100.     while true do
  101.         print("How many different types of potion do you wish to have in stock?")
  102.         print("NOTE: this will change the size of the apothecary")
  103.         print("Max = "..maximum..". Min = "..minimum..". Default = "..default)
  104.         local userInput = string.lower(read())
  105.         if tonumber(userInput) ~= nil then
  106.             if tonumber(userInput) >= minimum and tonumber(userInput) <= maximum then
  107.                 return tonumber(userInput)
  108.             else
  109.                 term.clear() term.setCursorPos(1,1)
  110.                 print("Outside of limits.  Try again")
  111.             end
  112.         else
  113.             term.clear() term.setCursorPos(1,1)
  114.             print("Not an integer.  Try again")
  115.         end
  116.     end
  117. end
  118.  
  119.  
  120. function RunMenu(size) --run the menu program to read a users selection DONE
  121.     local potion = {}
  122.     potion[01] = {"N.V.  3:00","g.carrot "}  --Night Vision
  123.     potion[02] = {"N.V.  8:00","g.carrot ","redstone "}  --Night Vision
  124.     potion[03] = {"Inv.  3:00","g.carrot ","f.s.eye  "}  --Invisibility
  125.     potion[04] = {"Inv.  8:00","g.carrot ","f.s.eye  ","redstone "}  --Invisibility
  126.     potion[05] = {"F.R.  3:00","m. cream "}  --Fire Resist
  127.     potion[06] = {"F.R.  8:00","m. cream ","redstone "}  --Fire Resist
  128.     potion[07] = {"Sl(s) 1:30","m. cream ","f.s.eye  ","gunpowder"}  --Slowness
  129.     potion[08] = {"Sl(s) 4:00","m. cream ","f.s.eye  ","redstone ","gunpowder"}  --Slowness
  130.     potion[09] = {"Sw I  3:00","sugar    "}  --Swiftness
  131.     potion[10] = {"Sw I  8:00","sugar    ","redstone "}  --Swiftness
  132.     potion[11] = {"Sw II 1:30","sugar    ","glowstone"}  --Swiftness
  133.     potion[12] = {"Hp I  +6  ","gl.melon "}  --Healing
  134.     potion[13] = {"Hp II +12 ","gl.melon ","glowstone"}  --Healing
  135.     potion[14] = {"Hm1(s)-6  ","spidereye","f.s.eye  ","gunpowder"}  --Harming
  136.     potion[15] = {"Hm2(s)-12 ","spidereye","f.s.eye  ","glowstone","gunpowder"} --Harming
  137.     potion[16] = {"Ps1(s)0:45","spidereye","redstone ","gunpowder"}  --Poison
  138.     potion[17] = {"Ps1(s)2:00","spidereye","glowstone","gunpowder"}  --Poison
  139.     potion[18] = {"Ps2(s)0:22","spidereye","glowstone","f.s.eye  ","gunpowder"}
  140.     potion[19] = {"RgnI  0:45","ghastTear"}
  141.     potion[20] = {"RgnI  2:00","ghastTear","redstone "}
  142.     potion[21] = {"RgnII 0.22","ghastTear","glowstone"}
  143.     potion[22] = {"Weak  1:30","f.s.eye  ","gunpowder"}
  144.     potion[23] = {"Weak  4:00","f.s.eye  ","redstone ","gunpowder"}
  145.     potion[24] = {"St I  3:00","blz.pwdr "}
  146.     potion[25] = {"St I  8:00","blz.pwdr ","glowstone"}
  147.     potion[26] = {"St II 1:30","blz.pwdr ","redstone "}
  148.  
  149.     local choice={}     --vector storing the users selection
  150.     for i = 1,size do choice[i] = 1 end
  151.     local itemsPP = 9       --number of potions that can fit in a column
  152.     local column = 1        --starting column (i think it needs a better variable name)
  153.     local pageStart = 4     --the first row that you can print a potion on
  154.  
  155.     repeat
  156.         term.clear() term.setCursorPos(1,1)
  157.         --Print the headings
  158.         print("Select potions from the choice: (UDLR)")
  159.         print("          <<< Shelf "..column.." of "..#choice.." >>>         ")
  160.        
  161.         --Print the potions
  162.         for potionID = 1,#potion do
  163.             colIndex = math.ceil(potionID/itemsPP)-1
  164.             rowIndex = potionID - colIndex*itemsPP
  165.             term.setCursorPos(colIndex*13 + 1, rowIndex + pageStart-1)
  166.             if choice[column] == potionID then
  167.                 print(">>"..potion[potionID][1].."|")
  168.             else
  169.                 print("  "..potion[potionID][1].."|")
  170.             end
  171.         end
  172.                
  173.                 --Print indicators
  174.         for i = #choice,1,-1 do
  175.             colIndex = math.ceil(choice[i]/itemsPP)-1
  176.             rowIndex = choice[i] - colIndex*itemsPP
  177.             term.setCursorPos(colIndex*13 + 1, rowIndex + pageStart-1)
  178.             print(i)
  179.         end
  180.        
  181.         --Recieve and process keyboard input
  182.         local eventtype, key = os.pullEvent()  --get keyboard input
  183.         if key == 200 and choice[column] == 1 then choice[column] = #potion  --up
  184.             elseif key == 200 then choice[column] = choice[column] - 1 --up
  185.             elseif key == 208 and choice[column] == #potion then choice[column] = 1  --down
  186.             elseif key == 208 then choice[column] = choice[column] + 1 --down
  187.             elseif key == 205 and column == size then column = 1 --right
  188.             elseif key == 205 then column = column + 1 --right
  189.             elseif key == 203 and column == 1 then column = size --left
  190.             elseif key == 203 then column = column - 1 --left
  191.         end
  192.     until key == 28  --if keypress = 'enter' then continue the program
  193.  
  194.     --print the user choice
  195.     term.clear() term.setCursorPos(1,1)
  196.     print("You have selected:")
  197.     for i = 1,#choice do
  198.         print("  Shelf "..i..": "..potion[choice[i]][1])
  199.     end
  200.     print("Are you happy with this selection?\n(Y/N)")
  201.     local input = read()
  202.     if input == "n" or input == "N" then
  203.         return RunMenu()
  204.     else
  205.         --reduce the potionlist to just the 4 choices stored in a 2D array
  206.         local potionShortList = {}
  207.         for i = 1,#choice do
  208.             potionShortList[i] = potion[choice[i]]
  209.         end
  210.        
  211.         term.clear() term.setCursorPos(1,1)
  212.         return potionShortList
  213.     end
  214. end
  215.  
  216.  
  217. function SaveFile(potionShortList) --saves the potion list as an 8 bit number in a file DONE
  218.     print("Saving potion set to file..")
  219.     local fileName = os.getComputerID()..".potions" --computer ID with the extension .potions
  220.     local file = fs.open(fileName,"w")
  221.     for potionNo = 1,#potionShortList do
  222.         local potionString = ""
  223.         for potionItem = 1,#potionShortList[potionNo],1 do
  224.             potionString = potionString..potionShortList[potionNo][potionItem]
  225.         end
  226.         file.writeLine(potionString)
  227.     end
  228.     file:close()
  229. end
  230.  
  231.  
  232. function LoadFile() --loads the data from file and returns the potion list DONE
  233.     print("Loading potions set from existing file..")
  234.     local fileName = os.getComputerID()..".potions" --computer ID with the extension .potions
  235.     local file = fs.open(fileName,"r")
  236.     local potionShortList = {}
  237.     local dataString = file.readLine()
  238.     local potionNumber = 0
  239.    
  240.     while dataString ~= null do
  241.         potionNumber = potionNumber + 1
  242.         potionShortList[potionNumber] = {}
  243.         potionShortList[potionNumber][1] = dataString:sub(1,10) --record the potion name
  244.         for potionItem = 1,(dataString:len()-10)/9 do
  245.             local startChar = potionItem*9 + 1 + 1
  246.             local endChar =   potionItem*9 + 1 + 9
  247.             potionShortList[potionNumber][potionItem+1] = dataString:sub(startChar,endChar)
  248.         end
  249.         dataString = file.readLine()
  250.     end    
  251.    
  252.     file:close()
  253.     return potionShortList
  254. end
  255.  
  256.  
  257. function IsBuilderWanted() --Ask the user if they require instructions DONE
  258.     term.clear() term.setCursorPos(1,1)
  259.     print("Would you like to use the builder program??\nYou have 20 seconds to respond (Y/N)")
  260.     local timer = os.startTimer(20) --#The interval that you would like to wait
  261.     while true do
  262.         local event, result = os.pullEvent()
  263.         if event=="timer" then --#If a timer event is pulled and its ID matches the one returned by startTimer
  264.             return false
  265.         elseif event == "key" then
  266.             if result == keys.enter or result == keys.y then return true
  267.             elseif result == keys.n then return false              
  268.             end
  269.         end
  270.     end
  271. end
  272.  
  273.  
  274. function WaitForInventory(size) --Waits for the required building materials DONE
  275.     repeat
  276.         term.clear() term.setCursorPos(1,1)
  277.         local inventory = "good"
  278.         turtle.select(1)  --building material
  279.         if turtle.getItemCount(1) ~= 11 + 3*size then
  280.             print(" - Put exactly "..(11+3*size).." blocks of a building material in slot 1")
  281.             inventory = "bad"
  282.         end
  283.         turtle.select(2)  --dispensers
  284.         if turtle.getItemCount(2) ~= size then
  285.             print(" - Put exactly "..size.." dispensers or droppers in slot 2")
  286.             inventory = "bad"
  287.         end
  288.         turtle.select(3)  --chest
  289.         if turtle.getItemCount(3) ~= 2 then
  290.             print(" - Put exactly 2 chests in slot 3")
  291.             inventory = "bad"
  292.         end
  293.         turtle.select(4)  --brewing stand
  294.         if turtle.getItemCount(4) ~= 1 then
  295.             print(" - Put exactly one brewing stand in slot 4")
  296.             inventory = "bad"
  297.         end
  298.         turtle.select(5)  --fuel
  299.         turtle.refuel()
  300.         if turtle.getFuelLevel() < 90 then
  301.             print(" - Put at least 2 coal worth of fuel in slot 5")
  302.             inventory = "bad"
  303.         end
  304.         if inventory == "bad" then sleep(3) end
  305.     until inventory == "good"
  306. end
  307.  
  308.  
  309. --[[possible graphics for possible build styles
  310.      [][][][]
  311. _____[][][][]_____  above ground
  312.  
  313. _____[][][][]_____  in ground
  314.      [][][][]
  315.  
  316. _____        _____  below ground
  317.      [][][][]
  318.      [][][][]
  319. ]]
  320.  
  321.  
  322. function SurveySite(size) --starting at brewing stand end DONE
  323.     term.clear() term.setCursorPos(1,1)
  324.     print("Surveying site.  Stand well back.")
  325.     --survey the main volume
  326.     for layer = 1,2 do
  327.         for i = 1,size+1 do
  328.             while turtle.detect() == true do
  329.                 print("Remove this block. TimeStamp = " ..os.clock())
  330.                 term.clear() term.setCursorPos(1,1)
  331.                 sleep(2)
  332.             end
  333.             turtle.turnLeft()
  334.             while turtle.detect() == true do
  335.                 print("Remove this block. TimeStamp = " ..os.clock())
  336.                 term.clear() term.setCursorPos(1,1)
  337.                 sleep(2)
  338.             end
  339.             TurnAround()
  340.             while turtle.detect() == true do
  341.                 print("Remove this block. TimeStamp = " ..os.clock())
  342.                 term.clear() term.setCursorPos(1,1)
  343.                 sleep(2)
  344.             end
  345.             turtle.turnLeft()
  346.             turtle.forward()
  347.         end
  348.         turtle.forward()
  349.         turtle.turnLeft()
  350.         while turtle.detect() == true do
  351.             print("Remove this block. TimeStamp = " ..os.clock())
  352.                 term.clear() term.setCursorPos(1,1)
  353.             sleep(2)
  354.         end
  355.         TurnAround()
  356.         while turtle.detect() == true do
  357.             print("Remove this block. TimeStamp = " ..os.clock())
  358.                 term.clear() term.setCursorPos(1,1)
  359.             sleep(2)
  360.         end
  361.         turtle.turnLeft()
  362.        
  363.         if layer == 1 then
  364.             while turtle.detectUp() == true do
  365.                 print("Remove the block above. TimeStamp = " ..os.clock())
  366.                 term.clear() term.setCursorPos(1,1)
  367.                 sleep(2)
  368.             end
  369.             turtle.up()
  370.             TurnAround()
  371.         end
  372.     end
  373.     turtle.down()
  374.     TurnAround()
  375.    
  376.     --survey the water well
  377.     turtle.forward() turtle.forward()
  378.     while turtle.detectDown() == true do
  379.         print("Remove the block below and fill it with water. TimeStamp = " ..os.clock())
  380.         term.clear() term.setCursorPos(1,1)
  381.         sleep(2)
  382.     end
  383.     turtle.back() turtle.back()
  384. end
  385.  
  386.  
  387. function AutoBuilder(size) --starting at brewing stand end DONE
  388.     BM = 1 DISP = 2 CH = 3 BS = 4
  389.     print("Laying bottom layer")
  390.     turtle.select(BM)
  391.     turtle.turnLeft() --build brewing end
  392.     turtle.place()
  393.     TurnAround()
  394.     turtle.place()
  395.     turtle.turnRight()
  396.     turtle.back()
  397.     turtle.place()
  398.     turtle.turnRight()
  399.     turtle.place()
  400.     TurnAround()
  401.     turtle.place()
  402.     turtle.turnRight()
  403.     turtle.back()
  404.     turtle.select(BS)
  405.     turtle.place()
  406.     turtle.select(BM)
  407.     turtle.turnRight()
  408.     turtle.place()
  409.     TurnAround()
  410.     turtle.place()
  411.     turtle.turnLeft()
  412.     for i = 1,size-1 do
  413.         turtle.forward()
  414.         turtle.turnLeft()
  415.         turtle.place()
  416.         TurnAround()
  417.         turtle.place()
  418.         turtle.turnLeft()
  419.     end
  420.     print("Laying top layer layer")
  421.     turtle.up()
  422.     turtle.placeDown()
  423.     turtle.turnLeft()
  424.     turtle.place()
  425.     TurnAround()
  426.     turtle.place()
  427.     turtle.turnLeft()
  428.     turtle.back()
  429.     turtle.select(CH)
  430.     turtle.place()
  431.     turtle.placeDown()
  432.     TurnAround()
  433.     for i = 1,size do
  434.         turtle.turnLeft()
  435.         turtle.select(DISP)
  436.         turtle.place()
  437.         TurnAround()
  438.         turtle.select(BM)
  439.         turtle.place()
  440.         turtle.turnLeft()
  441.         turtle.forward()
  442.     end
  443.     turtle.turnLeft()
  444.     turtle.place()
  445.     TurnAround()
  446.     turtle.place()
  447.     turtle.turnLeft()
  448.     turtle.back()
  449.     turtle.place()
  450. end
  451.  
  452.  
  453. function DefineInventory(potionShortList) --gives instruction on inventory layout and returns the inventory array DONE
  454.     local inventory = {"bottles  ","n. wart  "} --establish the inventory
  455.     for potID = 1,#potionShortList do --loop through all potions
  456.         for ingredient = 2,#potionShortList[potID] do --loop through all the ingredients
  457.             local listed = false  --assume the current ingredient not in the inventory array
  458.             for slot = 1,#inventory do
  459.                 if inventory[slot] == potionShortList[potID][ingredient] then
  460.                     listed = true
  461.                 end
  462.             end
  463.             if listed == false then
  464.                 inventory[#inventory+1] = potionShortList[potID][ingredient] --append the array
  465.             end
  466.         end
  467.     end
  468.     local resSlotNum = #inventory --calculates the number of reserved slots
  469.     for slot = resSlotNum+1,16 do inventory[slot] = "[empty]  " end --mark the empty slots
  470.  
  471.     --prints the inventory and scans the inventory ensuring it is at least minimally stocked
  472.     local stocked
  473.     repeat
  474.         term.clear() term.setCursorPos(1,1)
  475.         print("Stock the inventory to continue.\n\n")
  476.         for x = 0,3 do
  477.             print(inventory[x*4+1].." "..inventory[x*4+2].." "..inventory[x*4+3].." "..inventory[x*4+4].."")
  478.         end
  479.         sleep(1)
  480.         stocked = true
  481.         for slot = 1,resSlotNum do
  482.             turtle.select(slot)
  483.             if turtle.getItemCount(slot) == 0 then
  484.                 stocked = false
  485.             end
  486.         end
  487.     until stocked
  488.     term.clear() term.setCursorPos(1,1)
  489.    
  490.     return inventory, resSlotNum
  491. end
  492.  
  493.  
  494. function FindHome(size) --Navigates to home base DONE
  495.     print("\nGetting my bearings...")
  496.     --turtle will find the position above the water and then move to be above the brewing stand
  497.     local runCount = 4
  498.     --orient turtle in the axis of the apothecary
  499.     while turtle.detect() == true do
  500.         turtle.turnLeft()
  501.         runCount = runCount - 1
  502.         if runCount == 0 then error("NAVIGATION ERROR\nTurtle is lost\nForcing quit\n") end
  503.     end
  504.     --check if it on the top layer or bottom layer based on the length
  505.     for i = 1,size do turtle.forward() end --go to end
  506.     TurnAround()
  507.     for i = 1,size-3 do turtle.forward() end
  508.     if turtle.detect() == false then --the turtle is on the top layer
  509.         turtle.down()
  510.         turtle.forward()
  511.     end
  512.     if turtle.detectDown() == true then --if it is at the far end
  513.         TurnAround()
  514.         for i = 1,size-3 do turtle.forward() end
  515.     end
  516.     if turtle.detectDown() == true then error("NAVIGATION ERROR\nTurtle is lost!\nForcing quit\n") end
  517.     turtle.up()
  518.     turtle.forward()
  519. end
  520.  
  521.  
  522. function CleanUp(reservedSlotCount) --Clears away any leftovers from the previous run DONE
  523.     print("Cleaning up from last time")
  524.     turtle.suckDown()
  525.     turtle.back()
  526.     turtle.down()
  527.     for i = 1,3 do turtle.suck() end
  528.     TurnAround()
  529.     GoToEnd()
  530.     for slot = reservedSlotCount+1,16 do
  531.         turtle.select(slot)
  532.         turtle.drop(64)
  533.     end
  534.     TurnAround()
  535.     turtle.up()
  536.     GoToEnd()
  537.     term.clear() term.setCursorPos(1,1)
  538. end
  539.  
  540.  
  541. function IsShelfFull(shelfID,reservedSlotCount) --counts the contents of a shelf and returns true or false DONE
  542.     print("Inspecting shelf #"..shelfID.."..")
  543.     --move into position and face the shelf
  544.     while shelfID > 1 do
  545.         turtle.back()
  546.         shelfID = shelfID - 1
  547.     end
  548.     turtle.turnLeft()
  549.     --attempt to withdraw as many potions as possible
  550.     turtle.select(reservedSlotCount+1)
  551.     for slot = reservedSlotCount+1,16 do
  552.         turtle.suck()
  553.     end
  554.     --count the potions and put them back
  555.     local potionCount = 0
  556.     for slot = reservedSlotCount+1,16 do
  557.         turtle.select(slot)
  558.         if turtle.getItemCount(slot) == 1 then --if there's a potion
  559.             potionCount = potionCount + 1
  560.         elseif turtle.getItemCount(slot) == 0 then --if there's nothing
  561.             break
  562.         else
  563.             print("ERROR: Detected non-potion item.  Proceeding regardless")
  564.         end
  565.         turtle.drop()
  566.     end
  567.     --go home
  568.     turtle.turnRight()
  569.     GoToEnd()
  570.     --return a result
  571.     local emptySlotCount = 16-reservedSlotCount
  572.     if potionCount < emptySlotCount then --if less than maximum count
  573.         if potionCount > 6 then --if no room for more potions (max:9)
  574.             print("Shelf is full.")
  575.             return true
  576.         else
  577.             print("Shelf is NOT full.")
  578.             return false
  579.         end
  580.     else
  581.         print("Shelf is full.")
  582.         return true
  583.     end
  584. end
  585.  
  586.  
  587. function IsEnoughIngredients(potionRecipe, inventoryList) --ensures enough supply of raw materials for brewing DONE
  588.     print("Checking internal storage..")
  589.     turtle.select(1) --contains bottles
  590.     if turtle.getItemCount(1) <= 3 then return false end
  591.     turtle.select(2)
  592.     if turtle.getItemCount(2) <= 1 then return false end
  593.     for slot = 3,16 do
  594.         turtle.select(slot)
  595.         if turtle.getItemCount(slot) <= 1 then --check item count first.  more likely to be false.  saves time
  596.             for ingredient = 2,#potionRecipe do
  597.                 if inventoryList[slot] == potionRecipe[ingredient] then
  598.                     print("Insufficient internal storage.")
  599.                     return false
  600.                 end
  601.             end
  602.         end
  603.     end
  604.     if turtle.getFuelLevel() < 200 then return false end
  605.     print("Internal storage is sufficient.")
  606.     return true
  607. end
  608.  
  609.  
  610. function FillBottles(reservedSlotCount) --DONE
  611.     print("Filling bottles..")
  612.     turtle.back()
  613.     turtle.down()
  614.     for i = 1,3 do
  615.         turtle.select(1) --slot 1 contains empty bottles
  616.         turtle.placeDown()
  617.         turtle.select(reservedSlotCount + 1)
  618.         turtle.drop()
  619.     end
  620.     turtle.up()
  621.     turtle.forward()
  622. end
  623.  
  624.  
  625. function BrewPotion(potionRecipe, inventoryList) --brews the potion corresponding to the shelfID DONE
  626.     local potionSize = #potionRecipe
  627.     turtle.select(2) --this slot contains netherwart
  628.     turtle.dropDown(1)
  629.     sleep(1) --this is just a buffer in case it takes longer than 20s to brew a step
  630.     term.clear() term.setCursorPos(1,1)
  631.     Rest(20,"brew","Brewing step 1 of "..potionSize.." for "..potionRecipe[1])
  632.     --find the next ingredient in the recipe in the inventory and put it in the stand
  633.     for ingredient = 2,potionSize do --for each ingredient in the potion
  634.         for slot = 3,16 do --for each slot in the inventory
  635.             if inventoryList[slot] == potionRecipe[ingredient] then --compare the recipe step to the inventory slot
  636.                 turtle.select(slot)
  637.                 turtle.dropDown(1)
  638.                 sleep(1) --this is just a buffer in case it takes longer than 20s to brew a step
  639.                 term.clear() term.setCursorPos(1,1)
  640.                 Rest(20,"brew","Brewing step "..ingredient.." of "..potionSize.." for "..potionRecipe[1])
  641.             end
  642.         end
  643.     end
  644. end
  645.  
  646.  
  647. function RestockShelf(shelfID)  --puts freshly brewed potions on the shelf DONE
  648.     print("Restocking Shelves...")
  649.     --move to the brewing stand
  650.     turtle.back()
  651.     turtle.down()
  652.     --extract potions
  653.     for i = 1,3 do
  654.         turtle.suck()
  655.     end
  656.     --move to home
  657.     turtle.up()
  658.     turtle.forward()
  659.     --move to shelf
  660.     while shelfID > 1 do
  661.         turtle.back()
  662.         shelfID = shelfID - 1
  663.     end
  664.     turtle.turnLeft()
  665.     --identify which slots the potions are stored in
  666.     local slot = 16
  667.     turtle.select(slot)
  668.     while turtle.getItemCount(slot) == 0 do
  669.         slot = slot - 1
  670.         turtle.select(slot)
  671.     end
  672.     --put the potions away
  673.     for i = 1,3 do
  674.         turtle.drop()
  675.         slot = slot - 1
  676.         turtle.select(slot)
  677.     end
  678.     --move to home
  679.     turtle.turnRight()
  680.     GoToEnd()
  681. end
  682.  
  683.  
  684. function RestockTurtle(reservedSlotCount) --restocks the turtle from the storage chests DONE
  685.     print("Restocking turtle...")
  686.     TurnAround()
  687.     GoToEnd()
  688.    
  689.     turtle.select(reservedSlotCount+1)
  690.     while turtle.suck() == true do --get a stack of items
  691.         for slot = 1,reservedSlotCount do turtle.transferTo(slot,64) end  --shove it in the working inventory
  692.         turtle.refuel()   --attempt to refuel with the item
  693.         turtle.dropDown()  --dump the item in the temp chest
  694.     end
  695.     while turtle.suckDown() == true do  --move items from temp to perm chest
  696.       turtle.drop()
  697.     end
  698.    
  699.     TurnAround()
  700.     GoToEnd()
  701.     if turtle.getFuelLevel() < 100 then
  702.         Rest(300,"sleep","Low on fuel.  Slowing down work rate..")
  703.     elseif turtle.getFuelLevel() < 200 then
  704.         Rest(100,"sleep","Low on fuel.  Slowing down work rate..")
  705.     end
  706. end
  707.  
  708.  
  709. function Rest(restTime, restType, heading) --makes the turtle sleep for a time with animations DONE
  710.     term.clear() term.setCursorPos(1,1)
  711.     print(heading)
  712.     local restStep
  713.     while restTime > 0 do
  714.         term.setCursorPos(1,2)
  715.         if restType == "brew" then
  716.             restStep = 1 --must be an odd number
  717.             print("Brewing for "..restTime.."s  \n")
  718.             if math.fmod(restTime,2) == 0 then
  719.                 print[[
  720.                   \      
  721.                  } \   {
  722.                 |O  \o  |
  723.                 |  o \  |
  724.                  \_____/
  725.                 ]]
  726.             else
  727.                 print[[
  728.                       /  
  729.                  }   / {
  730.                 | o / O |
  731.                 |  / o  |
  732.                  \_____/
  733.                 ]]
  734.             end
  735.         elseif restType == "sleep" then
  736.             restStep = 1 --must be an odd number
  737.             print("Sleeping for "..restTime.."s   \n")
  738.             if math.fmod(restTime,2) == 0 then
  739.                 print[[
  740.                 ==== ====          
  741.                   //   // ===  ===  
  742.                  //   //   //   //  
  743.                 ==== ====  ===  ===
  744.                 ]]
  745.             else
  746.                 print[[
  747.                 ==== ====  ===  ===
  748.                   //   //   //   //
  749.                  //   //    ===  ===
  750.                 ==== ====          
  751.                 ]]
  752.             end
  753.         end
  754.         restTime = restTime - restStep
  755.         sleep(restStep)
  756.     end
  757.     term.clear() term.setCursorPos(1,1)
  758. end
  759.  
  760.  
  761. function TurnAround()  --turtle performs a 180 leftwise turn DONE
  762.     turtle.turnLeft() turtle.turnLeft()
  763. end
  764.  
  765.  
  766. function GoToEnd() --turtle moves until it hits something (break after 8 paces)
  767.     local runCount = 8
  768.     while turtle.forward() == true do
  769.         turtle.forward()
  770.         runCount = runCount - 1
  771.         if runCount == 0 then
  772.             error("\nTurtle is lost!\nForcing quit\n")
  773.         end
  774.     end
  775. end
  776.  
  777. term.clear() term.setCursorPos(1,1)
  778. print("NAVIGATION ERROR\nStarting program 'Potion Robot v1.2' by YardKing42 aka YK7942\n")
  779. Main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement