Advertisement
Kaikaku

aHarvester

Feb 27th, 2021 (edited)
4,971
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 22.74 KB | None | 0 0
  1. --{program="aHarvester",version="1.02c",date="2021-03-05"}
  2. ---------------------------------------
  3. -- aHarvester          by Kaikaku
  4. -- 2021-03-05, v1.02c   info clar. + gfs()
  5. -- 2021-02-28, v1.01   info clarification
  6. -- 2021-02-28, v1.00   initial
  7. ---------------------------------------
  8.  
  9. ---------------------------------------
  10. ---- DESCRIPTION ----------------------
  11. ---------------------------------------
  12. -- This program automates the farming
  13. --   of many different plants like
  14. --   sugarcane, melons, bamboo. Those
  15. --   plants must grow from a source
  16. --   block, that is not to be harvested.
  17. -- Harvested material is droped into
  18. --   chest. Fuel is taken from another.
  19. -- For more feature details see info
  20. --   screens or YouTube.
  21.  
  22.  
  23. ---------------------------------------
  24. ---- ASSUMPTIONS ----------------------
  25. ---------------------------------------
  26. -- Turtle is a mining turtle and a
  27. --   sample of its landmark block in
  28. --   slot 1.
  29.  
  30.  
  31. ---------------------------------------
  32. ---- PARAMETERS -----------------------
  33. ---------------------------------------
  34. local cVersion  ="v1.02c"
  35. local cPrgName  ="aHarvester"
  36. local cCritFuel  =800 -- stops at fuel station / home if lower than this
  37. local cMinFuel  =2000 -- refuels at fuel station if lower than this
  38. local slotFuel=16
  39. local cSleepTime=60
  40. local cMaxTurns =-1
  41. local turnCounter =0
  42. local turnsRemaining =cMaxTurns
  43. local blnCreateStartup=false
  44. local createStartup = "shell.run('"..cPrgName.." "
  45. local blnNoCannibalism=false -- no checks for other turtles (a bit slower)
  46. local blnDetectedTurtle=false
  47. local cNoCannibalism={}
  48. -- Hint: in case of new botonia versions this might need an update!
  49. cNoCannibalism[0]="computercraft:turtle_normal"
  50. cNoCannibalism[1]="computercraft:turtle_advanced"
  51.  
  52. local harvestedStuff=0
  53. local harvestedStuffTotal=0
  54.  
  55. ---------------------------------------
  56. ---- VARIABLES ------------------------
  57. ---------------------------------------
  58. local blnAskForParameters =true
  59.  
  60. local blnDown = true
  61. local blnUp   = true
  62.  
  63. local landmarkU -- up
  64. local landmarkF --forward
  65. local landmarkD --down
  66.  
  67. local nextTrunRight = true
  68. local nextTrunTurn = false
  69. local blnGoOn = true
  70.  
  71.  
  72. ---------------------------------------
  73. ---- Early UI functions ---------------
  74. ---------------------------------------
  75. local function swapColors()     -- <<-- put into template
  76. local backColor=term.getBackgroundColor()
  77. local textColor=term.getTextColor()
  78.  
  79. term.setBackgroundColor(textColor)
  80. term.setTextColor(backColor)
  81. end
  82.  
  83. local function printUI(strInput)    -- <<-- put into template
  84. if strInput==ni then strInput="" end
  85.  
  86.   if strInput=="header" then
  87.     term.write("—ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ") swapColors() print("”") swapColors()  
  88.   elseif strInput=="line" then
  89.      term.write("ŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒŒ") swapColors() print("‘") swapColors()
  90.   elseif strInput=="footer" then
  91.     swapColors() print("Š…") swapColors()
  92.   else
  93.     term.write("•")
  94.     strInput=strInput.."                                     "
  95.     term.write(string.sub(strInput,1,37))
  96.     swapColors() print("•") swapColors()
  97.   end
  98. end
  99.  
  100. ---------------------------------------
  101. ---- tArgs ----------------------------
  102. ---------------------------------------
  103. local tArgs = {...}    -- <<-- transfere concept to template
  104. local paraIdentified=false
  105. local paraNumberCount=0
  106. term.clear() term.setCursorPos(1,1)
  107.  
  108. -- header
  109. if #tArgs~=0 then
  110.   printUI("header")
  111.   printUI(""..cPrgName..", "..cVersion..", by Kaikaku. Enjoy!")
  112.   printUI("line")
  113.   print("Starting...")
  114. end
  115.  
  116. -- check parameters
  117. for i=1,#tArgs do
  118.   blnAskForParameters=false
  119.   paraIdentified=false
  120.   -- tet parameters
  121.   if string.lower(tArgs[i])=="notdown" then
  122.     paraIdentified=true blnDown=false
  123.     createStartup=createStartup..tArgs[i].." "
  124.     print("Option: not dig down!")
  125.   end
  126.   if string.lower(tArgs[i])=="notup" then  
  127.     paraIdentified=true blnUp=false
  128.     createStartup=createStartup..tArgs[i].." "
  129.     print("Option: not dig up!")  
  130.   end
  131.   if string.lower(tArgs[i])=="nocannibalism" then  
  132.     paraIdentified=true blnNoCannibalism=true
  133.     createStartup=createStartup..tArgs[i].." "
  134.     print("Option: no cannibalism!")  
  135.   end
  136.   if string.lower(tArgs[i])=="startup" then
  137.     paraIdentified=true blnCreateStartup=true
  138.     print("Option: creating startup")  
  139.   end
  140.   -- text+number parameters
  141.   if string.sub(string.lower(tArgs[i]),1,7)=="minfuel" then
  142.     term.write("Option: minFuel "..cMinFuel.."->")  
  143.     paraIdentified=true
  144.     createStartup=createStartup..tArgs[i].." "
  145.     cMinFuel=tonumber(string.sub(string.lower(tArgs[i]),8))
  146.     print(cMinFuel)  
  147.   end
  148.   if string.sub(string.lower(tArgs[i]),1,8)=="critfuel" then
  149.     term.write("Option: critFuel "..cCritFuel.."->")  
  150.     paraIdentified=true
  151.     createStartup=createStartup..tArgs[i].." "
  152.     cCritFuel=tonumber(string.sub(string.lower(tArgs[i]),9))
  153.     print(cCritFuel)  
  154.   end
  155.   -- number parameters
  156.   if not paraIdentified  then
  157.     if paraNumberCount==0 then
  158.       cSleepTime=tonumber(tArgs[i]) -- no error handling for text
  159.       paraIdentified=true
  160.       createStartup=createStartup..tArgs[i].." "
  161.       paraNumberCount=paraNumberCount+1
  162.       print("Option: sleep time = "..cSleepTime)
  163.     elseif paraNumberCount==1 then
  164.       cMaxTurns=tonumber(tArgs[i]) -- no error handling for text
  165.       turnsRemaining=cMaxTurns
  166.       paraIdentified=true
  167.       createStartup=createStartup..tArgs[i].." "
  168.       paraNumberCount=paraNumberCount+1
  169.       print("Option: fixed number of turns = "..cMaxTurns)
  170.     end
  171.   end
  172.   if not paraIdentified then
  173.     error("Error: Unknown parameter "..i..":'"..tArgs[i].."'")
  174.   end
  175. sleep(0.3)
  176. end
  177.  
  178. ---------------------------------------    -- <<--- put in template
  179. ---- startup generation ---------------
  180. ---------------------------------------
  181. if blnCreateStartup then
  182. local newFileName
  183.   createStartup=createStartup.."')"
  184.   print("Creating a startup file with: "..createStartup)
  185.  
  186.   -- is there already one?
  187.   if fs.exists("startup.lua") then
  188.     print("  '".."startup.lua".."' already exists.")
  189.     newFileName="startup_old_"..os.date("%Y-%m-%d_%H:%M")
  190.     print("  Renaming to '".."startup.lua".."'")
  191.     shell.run("rename startup.lua "..newFileName)
  192.   end
  193.   if fs.exists("startup") then
  194.     print("  '".."startup".."' already exists.")
  195.     newFileName="startup_old_"..os.date("%Y-%m-%d_%H:%M")
  196.     print("  Renaming to '"..newFileName.."'")
  197.     shell.run("rename startup "..newFileName)
  198.   end
  199.  
  200.   print("Saving new startup...")
  201.    
  202.  
  203. local h = fs.open("startup", "w")
  204. h.writeLine("-- Automatically created startup by:")
  205. h.writeLine("--   "..cPrgName..", "..cVersion..", by Kaikaku")
  206. h.writeLine("-- If you are looking for another")
  207. h.writeLine("--   startup file, check files like:")
  208. h.writeLine("--   'startup_old_yyyy-mm-dd-tttt'")
  209. h.writeLine(createStartup)
  210. h.close()
  211.  
  212. end
  213.  
  214. ---------------------------------------
  215. -- basic functions for turtle control -
  216. ---------------------------------------
  217. local function gf()  while not turtle.forward()   do end end
  218. local function gb()  while not turtle.back()      do end end
  219. local function gu()  while not turtle.up()        do end end
  220. local function gd()  while not turtle.down()      do end end
  221. local function gl()  while not turtle.turnLeft()  do end end
  222. local function gr()  while not turtle.turnRight() do end end
  223. local function df()  local returnValue = turtle.dig() turtle.suck() return returnValue  end
  224. local function gfs(n)  if n==nil then n=1 end  for i=1,n,1 do  while not turtle.forward() do df() end end end
  225. local function du()  turtle.digUp() turtle.suckUp()      end
  226. local function dd()  turtle.digDown() turtle.suckDown()  end
  227. local function pf()  turtle.place()     end
  228. local function pu()  turtle.placeUp()   end
  229. local function pd()  return turtle.placeDown() end
  230. local function sf()  turtle.suck()      end
  231. local function su()  turtle.suckUp()    end
  232. local function sd()  turtle.suckDown()  end
  233. local function Df()  turtle.drop()      end
  234. local function Du(n) turtle.dropUp(n)  end
  235. local function Dd()  turtle.dropDown()  end
  236. local function ss(s) turtle.select(s)   end
  237. local function gic(s) return turtle.getItemCount(s) end
  238.  
  239.  
  240.  
  241. local function waitKey(strText)
  242.   local event, scancode
  243.   write(strText)
  244.   event, scancode = os.pullEvent("key")
  245.   print()
  246. end
  247.  
  248. local function askForInputText(textt)
  249.   local at=""
  250.   -- check prompting texts
  251.   if textt==nil then textt="Enter text:" end
  252.  
  253.   -- ask for input
  254.   write(textt)
  255.   at=read()
  256.   return at
  257. end
  258.  
  259. local function checkFuel()
  260.   local tmp=turtle.getFuelLevel()
  261.   return tmp
  262. end
  263.  
  264. local function checkTurtle()
  265. -- turtle?
  266.   local turtleOk, turtleVal = pcall(checkFuel)
  267.   if not turtleOk then
  268.     term.clear() term.setCursorPos(1,1)
  269.     print("+-------------------------------------+")
  270.     print("  ",cPrgName,", by Kaikaku")
  271.     print("+-------------------------------------+")
  272.     print("| This is a turtle program.           |")
  273.     print("| Please, execute it with a turtle!   |")
  274.     print("+-------------------------------------+")
  275.     return
  276.   end
  277. end
  278.  
  279.  
  280. local function sleepDots(sec)
  281. if sec==nil then sec=10 end
  282. if sec<1 then return end
  283. local sleepDotsCounter=0
  284.  
  285.   for i=1,sec-1 do
  286.     sleepDotsCounter=sleepDotsCounter+1
  287.     sleep(1)
  288.     if sleepDotsCounter<31 then
  289.       term.write(".")
  290.     else
  291.       sleepDotsCounter=1
  292.       print()
  293.       term.write(".")
  294.     end
  295.   end
  296.  
  297.   sleep(1)
  298.   print(".")
  299. end
  300.  
  301.  
  302. ---------------------------------------
  303. -- additional functions               -
  304. ---------------------------------------
  305.  
  306.  
  307. local function dropInventory()
  308.   for i=2,16 do
  309.     if gic(i)>0 then
  310.       ss(i)
  311.       harvestedStuff=harvestedStuff+gic()
  312.       Dd()
  313.     end
  314.   end
  315.   ss(1)
  316. end
  317.  
  318. local function doCompares()
  319.   landmarkU=turtle.compareUp()
  320.   landmarkF=turtle.compare()
  321.   landmarkD=turtle.compareDown()
  322. end
  323.  
  324. local function doRefuel()
  325. local fuelAtStart=turtle.getFuelLevel()
  326.  
  327.   -- call this only at fuel station
  328.   if turtle.getFuelLevel() < cMinFuel then
  329.     -- need to refuel
  330.     term.write("  Refueling:"..turtle.getFuelLevel())
  331.     if gic(slotFuel)==0 then
  332.       ss(slotFuel)
  333.       su()
  334.       turtle.refuel()
  335.       ss(1)
  336.     end
  337.     print(""..turtle.getFuelLevel())
  338.   else
  339.     print("  Fuel is okay.")
  340.   end
  341.   return fuelAtStart<turtle.getFuelLevel()
  342. end
  343.  
  344. local function checkCritFuel()
  345. local currentSlot = turtle.getSelectedSlot()
  346.   while turtle.getFuelLevel()<cCritFuel do
  347.     ss(slotFuel)
  348.     print("Critical fuel level ("..turtle.getFuelLevel().."/"..cCritFuel..")!")
  349.     term.write("  Please, provide fuel in slot "..slotFuel.."!")
  350.     while gic(slotFuel)==0 do
  351.       term.write(".")
  352.       sleep(1)
  353.     end
  354.     turtle.refuel()
  355.   end
  356.   ss(currentSlot)
  357. end
  358.  
  359.  
  360. local function suckAll()
  361.   while turtle.suck() do end
  362.   while turtle.suckUp() do end
  363.   while turtle.suckDOwn() do end
  364. end
  365.  
  366. local function debugPrint(str)
  367.  if false then print(str) end
  368. end
  369.  
  370. local function inspectFor(blockArray, strDirection)
  371. if strDirection==nil then strDirection="f" end
  372. local blnOk, data
  373.  
  374.   -- inspect
  375.   if strDirection=="d" then blnOk, data = turtle.inspectDown()
  376.   elseif strDirection=="u" then blnOk, data = turtle.inspectUp()
  377.   elseif strDirection=="f" then blnOk, data = turtle.inspect()
  378.   else
  379.     print("Warning: Unknown direction '",strDirection,"' in inspectFor, taking (f)orward instead.")
  380.     strDirection="f"
  381.     blnOk, data = turtle.inspect()
  382.   end
  383.   if data.name~=nil then debugPrint("Found:"..string.lower(data.name)) end
  384.   -- compare
  385.   local i=1
  386.   while blockArray[i]~=nil do
  387.   debugPrint("Compare to:"..string.lower(blockArray[i]))
  388.     if data.name~=nil then
  389.       if string.lower(data.name) == string.lower(blockArray[i]) then return true end
  390.     end
  391.     i=i+1
  392.   end
  393.  
  394.   return false -- reached a nil value
  395. end
  396.  
  397. local function evadeStrategy()
  398. local cTurnProbablity=20
  399. local tmpRnd = math.random(0,100)
  400.   term.write("")
  401.   if tmpRnd<=cTurnProbablity then
  402.     gr() term.write("r")
  403.   elseif tmpRnd<=cTurnProbablity*2 then
  404.     gl() term.write("l")
  405.   else
  406.     term.write("s") sleep(0.8)
  407.   end
  408. end
  409.  
  410. local function handleReport()
  411.   print("  Harvested items this turn: "..harvestedStuff)
  412.   harvestedStuffTotal=harvestedStuffTotal+harvestedStuff
  413.   harvestedStuff=0
  414.   print("  Harvested items total: "..harvestedStuffTotal)
  415. end
  416.  
  417. local function handleTurnCounter()
  418. -- return true if cMaxTurns is reached, else false
  419.   handleReport()
  420.   if turnsRemaining==0 then return true end
  421.   turnCounter=turnCounter+1
  422.   if cMaxTurns>=0 then
  423.     turnsRemaining=turnsRemaining-1
  424.     term.write("Turn "..turnCounter.."/"..cMaxTurns..":")
  425.   else
  426.     term.write("Turn "..turnCounter..":")
  427.   end
  428.   return false
  429. end
  430.  
  431. ------------------------------------------------------------------------------
  432. -- main ----------------------------------------------------------------------
  433. ------------------------------------------------------------------------------
  434. checkTurtle()
  435.  
  436. if blnAskForParameters then
  437. term.clear() term.setCursorPos(1,1)
  438. local iPage=0
  439. local iPageMax=10                                    
  440. term.clear() term.setCursorPos(1,1) iPage=iPage+1
  441. repeat
  442. printUI("header")
  443. printUI(""..cPrgName..", "..cVersion..", by Kaikaku ("..iPage.."/"..iPageMax..")")
  444. printUI("line")
  445. --       1234567890123456789012345678901234567
  446. printUI("This program automates the farming")
  447. printUI("  of many different plants like")
  448. printUI("  sugar cane, melons, mushrooms,")
  449. printUI("  bamboo, kelp and many more. These")
  450. printUI("  plants typically grow or spread")
  451. printUI("  from a source block. ")
  452. printUI("Starting it w/o parameter (as now)")
  453. printUI("  shows this info and uses defaults.")
  454. printUI("footer")
  455. until askForInputText("Press enter when ready:")==""
  456.  
  457. term.clear() term.setCursorPos(1,1) iPage=iPage+1
  458. repeat
  459. printUI("header")
  460. printUI(""..cPrgName..", "..cVersion..", by Kaikaku ("..iPage.."/"..iPageMax..")")
  461. printUI("line")
  462. --       1234567890123456789012345678901234567
  463. printUI("Determine a landmark block (not dirt)")
  464. printUI("  and frame the area in where to har-")
  465. printUI("  vest. Place this frame at y level")
  466. printUI("  of the turtle is. Put 1 in slot 1.")
  467. printUI("The turtle will allways move forward")
  468. printUI("  until it hits this block. Then it")
  469. printUI("  makes a u-turn right then left...")
  470. printUI("  In corners it changes the orient.")
  471. printUI("footer")
  472. until askForInputText("Press enter when ready:")==""
  473.  
  474. term.clear() term.setCursorPos(1,1) iPage=iPage+1
  475. repeat
  476. printUI("header")
  477. printUI(""..cPrgName..", "..cVersion..", by Kaikaku ("..iPage.."/"..iPageMax..")")
  478. printUI("line")
  479. --       1234567890123456789012345678901234567
  480. printUI("Put a chest below the turtle level")
  481. printUI("  with a landmark block two blocks")
  482. printUI("  above, this is the home position.")
  483. printUI("Place a chest above the turtle level")
  484. printUI("  with a landmark block two blocks")
  485. printUI("  below, this is the refuel chest.")
  486. printUI("You can have as many home and fuel")
  487. printUI("  stations as you like.")
  488. printUI("footer")
  489. until askForInputText("Press enter when ready:")==""
  490.  
  491. term.clear() term.setCursorPos(1,1) iPage=iPage+1
  492. repeat
  493. printUI("header")
  494. printUI(""..cPrgName..", "..cVersion..", by Kaikaku ("..iPage.."/"..iPageMax..")")
  495. printUI("line")
  496. --       1234567890123456789012345678901234567
  497. printUI("Side view example:")
  498. printUI(" ")
  499. printUI("         L c    T=turtle")
  500. printUI(" Lssssss T  L   L=landmark block")
  501. printUI("  ssssss C L    s=sugar cane")
  502. printUI("  ssssss        C=home chest")
  503. printUI(" XXXXXXXXXXXX   c=fuel chest")
  504. printUI(" XXXXXXXXXXXX   X=dirt/etc.")
  505. printUI("footer")
  506. until askForInputText("Press enter when ready:")==""
  507.  
  508. term.clear() term.setCursorPos(1,1) iPage=iPage+1
  509. repeat
  510. printUI("header")
  511. printUI(""..cPrgName..", "..cVersion..", by Kaikaku ("..iPage.."/"..iPageMax..")")
  512. printUI("line")
  513. --       1234567890123456789012345678901234567
  514. printUI("Number parameters:              ")
  515. printUI("  The first number parameter you use")
  516. printUI("    determines the waiting time at ")
  517. printUI("    the home position (default=60).")
  518. printUI("  A second number parameter deter-")
  519. printUI("    mines the number of turns ")
  520. printUI("    (=reaching home position) if you")
  521. printUI("    want to limit the runs.")
  522. printUI("footer")
  523. until askForInputText("Press enter when ready:")==""
  524. term.clear() term.setCursorPos(1,1) iPage=iPage+1
  525.  
  526. repeat
  527. printUI("header")
  528. printUI(""..cPrgName..", "..cVersion..", by Kaikaku ("..iPage.."/"..iPageMax..")")
  529. printUI("line")
  530. --       1234567890123456789012345678901234567
  531. printUI("Harvest options:              ")
  532. printUI("  Turtle will harvest allways blocks ")
  533. printUI("    in front of it and by default all")
  534. printUI("    blocks above and below it.       ")
  535. printUI("  To disable harvesting of blocks you")
  536. printUI("    use one or both of the following ")
  537. printUI("    not case sensitive parameters:   ")
  538. printUI("  notUp and/or notDown")
  539. printUI("footer")
  540. until askForInputText("Press enter when ready:")==""
  541.  
  542. term.clear() term.setCursorPos(1,1) iPage=iPage+1
  543. repeat
  544. printUI("header")
  545. printUI(""..cPrgName..", "..cVersion..", by Kaikaku ("..iPage.."/"..iPageMax..")")
  546. printUI("line")
  547. --       1234567890123456789012345678901234567
  548. printUI("Fuel parameters: At min fuel level")
  549. printUI("  (2000) the turtle refuels. With")
  550. printUI("  the parameter minFuelxxxx you cust-")
  551. printUI("  omize this value to 'xxxx'.")
  552. printUI("At crit fuel level (800) the turtle")
  553. printUI("  won't move from home or (empty?)")
  554. printUI("  fuel stations: Use parameter ")
  555. printUI("  critFuelxxxx to change this.")
  556. printUI("footer")
  557. until askForInputText("Press enter when ready:")==""
  558.  
  559. term.clear() term.setCursorPos(1,1) iPage=iPage+1
  560. repeat
  561. printUI("header")
  562. printUI(""..cPrgName..", "..cVersion..", by Kaikaku ("..iPage.."/"..iPageMax..")")
  563. printUI("line")
  564. --       1234567890123456789012345678901234567
  565. printUI("Auto startup:")
  566. printUI("  If you want the turtle to allways")
  567. printUI("  start the program when the world or")
  568. printUI("  chunk is loaded, use the parameter:")
  569. printUI("  startup")
  570. printUI("  This will create a startup pro-")
  571. printUI("  gram using the same parameters")
  572. printUI("  as the current call.")
  573. printUI("footer")
  574. until askForInputText("Press enter when ready:")==""
  575.  
  576. term.clear() term.setCursorPos(1,1) iPage=iPage+1
  577. repeat
  578. printUI("header")
  579. printUI(""..cPrgName..", "..cVersion..", by Kaikaku ("..iPage.."/"..iPageMax..")")
  580. printUI("line")
  581. --       1234567890123456789012345678901234567
  582. printUI("Example calls:")
  583. printUI("aHarvester notDown")
  584. printUI("->Turtle starts w/ default values, ")
  585. printUI("  but no harvesting down.")
  586. printUI("aHarvester 120 2 minFuel1000 startup")
  587. printUI("->Creates a startup program, 120 sec")
  588. printUI("  waiting at home, only 2 runs, min")
  589. printUI("  fuel is set to 1000. Turtle starts.")
  590. printUI("footer")
  591. until askForInputText("Press enter when ready:")==""
  592.  
  593. term.clear() term.setCursorPos(1,1) iPage=iPage+1
  594. repeat
  595. printUI("header")
  596. printUI(""..cPrgName..", "..cVersion..", by Kaikaku ("..iPage.."/"..iPageMax..")")
  597. printUI("line")
  598. --       1234567890123456789012345678901234567
  599. printUI("Parameter: noCannibalsm")
  600. printUI("  You can use multiple turtles in one")
  601. printUI("  farm w/o them eating each other.")
  602. printUI("  In very rare cases, the hungry ")
  603. printUI("  turtle will stop until solved.")
  604. printUI("Sorry for this long description, as")
  605. printUI("  fall back you can watch the YouTube")
  606. printUI("  video. Enjoy!")
  607. printUI("footer")
  608. until askForInputText("Press enter to START! (stop w/ ctrl+t)")==""
  609. end
  610.  
  611.  
  612. ---------------------------------------
  613. -- main loop                          -
  614. ---------------------------------------
  615.  
  616. -- step 1: check starting stuff
  617. ss(1)
  618. local tmpSleep=1
  619. while turtle.getItemCount()==0 do
  620.   print("Put 1 landmark block in slot 1!")
  621.   print("Sleeping",tmpSleep,"seconds")
  622.   sleepDots(tmpSleep)
  623.   tmpSleep=tmpSleep+1
  624.   ss(1)
  625. end
  626.  
  627.  
  628. -- step 2: program loop
  629. term.write("Inital turn 0:")
  630.  
  631. while blnGoOn do
  632.  
  633.  
  634.   -- step 2.0: need to turn once more?
  635.   if nextTrunTurn then
  636.     -- need to turn once more, because of wall last turn
  637.     nextTrunTurn=false
  638.     if nextTrunRight then
  639.       gr() print("r") nextTrunRight=false
  640.     else
  641.       gl() print("l") nextTrunRight=true
  642.     end
  643.   end
  644.  
  645.   -- step 2.1: check for landmarks
  646.   doCompares()
  647.  
  648.   -- step 2.2: handle home base
  649.   if landmarkU then
  650.     -- at my base
  651.     if math.random()<0.95 then
  652.       print() term.write("Home. ")
  653.     else
  654.       print() term.write("I'm home, honey! ")
  655.     end
  656.     print("Fuel:",turtle.getFuelLevel().."/"..cMinFuel)
  657.     print("Dropping inventory:")
  658.     dropInventory()
  659.     if handleTurnCounter() then print("Finished all "..cMaxTurns.." turns.") break end
  660.     print(" Sleeping for "..cSleepTime.." seconds")
  661.     sleepDots(cSleepTime)
  662.     checkCritFuel()
  663.    
  664.   end
  665.    
  666.   -- step 2.3: handle fuel station
  667.   if landmarkD then
  668.     -- at fuel station
  669.     print() print("Fuel station. Fuel:",turtle.getFuelLevel().."/"..cMinFuel)
  670.     while doRefuel() do end
  671.     checkCritFuel()
  672.   end
  673.  
  674.   -- step 2.4: handle wall/corner
  675.   if landmarkF then
  676.     -- found wall
  677.     nextTrunTurn=true
  678.     if nextTrunRight then
  679.       gr() term.write("r")
  680.       if turtle.compare() then
  681.         print() term.write("Changing orientation:")
  682.         gl() term.write("l") gl() print("l")
  683.         nextTrunTurn=false
  684.         nextTrunRight=false
  685.       end
  686.     else  
  687.       gl() term.write("l")
  688.       if turtle.compare() then
  689.         print() term.write("Changing orientation:")
  690.         gr() term.write("r") gr() print("r")
  691.         nextTrunTurn=false
  692.         nextTrunRight=true
  693.       end
  694.     end
  695.     landmarkF=turtle.compare()
  696.   end
  697.  
  698.   -- step 2.5: dig'n move
  699.   if blnNoCannibalism then blnDetectedTurtle=inspectFor(cNoCannibalism,"f") end
  700.   if blnDetectedTurtle then
  701.     -- found a turtle!!
  702.     evadeStrategy()
  703.   else
  704.     -- regular move
  705.     if not landmarkF then
  706.       df() while turtle.suck() do end
  707.     end
  708.     if not landmarkU and not landmarkD then
  709.       if blnUp   then du() end while turtle.suckUp()   do end
  710.       if blnDown then dd() end while turtle.suckDown() do end
  711.     end
  712.     if not landmarkF then
  713.       if not landmarkD then while turtle.suckUp() do end end
  714.       if blnNoCannibalism then blnDetectedTurtle=inspectFor(cNoCannibalism,"f") end
  715.       if not blnDetectedTurtle then gfs() term.write("") end
  716.     end
  717.   end
  718.  
  719.   -- step 2.6: safety check
  720.   while gic(1)~=1 do
  721.     print()
  722.     print("Safety violation!")
  723.     print("  I need exactly 1 landmark block in")
  724.     print("  slot 1 (currently: "..gic(1)..")!")
  725.     term.write("Sleeping for 10 sec ")
  726.     sleepDots(10)
  727.   end
  728. end
  729.  
  730.  
  731. print("That was quite a bit of work :)")
  732.  os.sleep(0.4)
  733.  print("***************************************")
  734.  print("* Check out YouTube for more videos   *")
  735.  print("* and turtle programs by Kaikaku :)   *")
  736.  print("***************************************")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement