krom

MineCraft Woody01

Feb 16th, 2013
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.22 KB | None | 0 0
  1. -- Author: KROM
  2. --
  3. -- --------------------------------
  4. local bUseModem = false
  5. local bBoneMeal = true
  6. local nRuns = 0
  7. local bNoError = true
  8. local bRedNet = false
  9. local nMasterComputerID = nil
  10. -- 30
  11. local vX, vY = 0,-1
  12. local posX, posY, posZ = 0,0,0
  13.  
  14. local stopCause = 0
  15. -- --------------------------------
  16. local function scanForSaplings()
  17.     return turtle.getItemCount(2)
  18. end
  19. -- --------------------------------
  20. local function scanForBoneMeal()
  21.     return turtle.getItemCount(3)
  22. end
  23. -- --------------------------------
  24. local function say(message)
  25.     if bRedNet == true then
  26.         rednet.send(nMasterComputerID, message)
  27.     end
  28.     print(message)
  29. end
  30. -- --------------------------------
  31. local function turnRight()
  32.     turtle.turnRight()
  33.     vX, vY = vY, -vX
  34. end
  35. -- --------------------------------
  36. local function turnLeft()
  37.     turtle.turnLeft()
  38.     vX, vY = -vY, vX
  39. end
  40. -- ----------------------------------------------------------
  41. local function tryDown()
  42.     stopCause = 0
  43.     if posZ == 0 then
  44.         say("tryDown: already at max depth")
  45.         return false
  46.     end
  47.  
  48.     if not turtle.down() then
  49.         if turtle.digDown() then
  50.         end
  51.         if not turtle.down() then
  52.             stopCause = 1
  53.             return false
  54.         end
  55.     end
  56.     posZ = posZ - 1
  57.     return true
  58. end
  59. -- ----------------------------------------------------------
  60. local function tryForwards()
  61.     stopCause = 0
  62.     while not turtle.forward() do
  63.         if turtle.dig() then
  64.             -- all cool
  65.         else
  66.             sleep(0.8)
  67.             if turtle.dig() then
  68.                 -- all cool
  69.             else
  70.                 stopCause = 1
  71.                 return false
  72.             end
  73.         end
  74.     end
  75.  
  76.     return true
  77. end
  78. -- --------------------------------
  79. local function work(nHowDeep, nSize)
  80.     local depth,x,y
  81.     local startZ = posZ
  82.     local bFirstLoop = false
  83.    
  84.     for depth=0,nHowDeep do
  85.         for x=0,(nSize-1) do
  86.        
  87.             -- first row has no turn move
  88.             -- which needs to be offset
  89.             local startY = 1
  90.  
  91.             if x == 0 and startZ == 0 and depth == 0 then
  92.                 startY = 0
  93.             elseif bFirstLoop == true then
  94.                 startY = 0
  95.             end
  96.  
  97.             if x == 0 then
  98.                 say("SY: "..startY)
  99.             end
  100.  
  101.             for y=startY,(nSize-1) do
  102.  
  103.                 local start = posX..","..posY..","..posZ
  104.                
  105.                 v = math.fmod(x,2)
  106.                 -- v == 0 = facing away. like start
  107.                 -- v == 1 = facing to start
  108.                 -- say("V: "..v.." LD: "..layerdirection.." SY: "..startY.." xyz: "..x..","..y..","..depth)
  109.  
  110.  
  111.                 if not tryForwards() then
  112.                     if stopCause == 2 then
  113.                         -- inventory full?!
  114.                         say("Failed to move forward. Inv. full?!")
  115.                         return false
  116.                     else
  117.                         say("Failed to move forward.")
  118.                         return false
  119.                     end
  120.                 end
  121.  
  122.                 if layerdirection == 1 then
  123.                     if v == 0 then
  124.                         posY = posY + 1
  125.                     else
  126.                         posY = posY - 1
  127.                     end
  128.                 else
  129.                     if v == 0 then
  130.                         posY = posY - 1
  131.                     else
  132.                         posY = posY + 1
  133.                     end
  134.                 end
  135.  
  136.                 -- say("P: "..start.." -> "..posX..","..posY..","..posZ)
  137.                 -- say("Dir: "..xDir..","..yDir)
  138.             -- end y loop
  139.             end
  140.  
  141.             bFirstLoop = false;
  142.  
  143.             say("completed one line")
  144.  
  145.             -- if not on last line
  146.             if  x < nSize-1 then
  147.                 -- move to next line (turn, move, turn)
  148.                 if v == 0 then
  149.  
  150.                     say("Moving to next row (1)")
  151.                     turnRight()
  152.  
  153.                     if not tryForwards() then
  154.                         if stopCause == 2 then
  155.                             -- inventory full?!
  156.                             say("Failed to move forward. Inv. full?!")
  157.                             return false
  158.                         else
  159.                             return false
  160.                         end                    
  161.                     end
  162.                     turnRight()
  163.                 else
  164.                     say("Moving to next row (2)")
  165.                     turnLeft()
  166.  
  167.                     if not tryForwards() then
  168.                         if stopCause == 2 then
  169.                             -- inventory full?!
  170.                             say("Failed to move forward. Inv. full?!")
  171.                             return false
  172.                         else
  173.                             return false
  174.                         end
  175.                     end
  176.                     turnLeft()
  177.                 end
  178.  
  179.                 if layerdirection == 1 then
  180.                     posX = posX + 1
  181.                 else
  182.                     posX = posX - 1
  183.                 end
  184.             end
  185.  
  186.         -- end x loop
  187.         end
  188.  
  189.         say("completed one layer")
  190.  
  191.         -- checks if depth reached
  192.         if depth < nHowDeep then
  193.  
  194.             if not tryDown() then
  195.                 -- unable to move down?
  196.                 -- assume we hit bedrock
  197.                 say("! we failed to move down")
  198.                 return false
  199.             end
  200.  
  201.             if layerdirection == 1 then
  202.                 layerdirection = 0
  203.             else
  204.                 layerdirection = 1
  205.             end
  206.  
  207.             turnLeft()
  208.             turnLeft()
  209.         else
  210.             say("finished all layer")
  211.             -- finished
  212.             return true
  213.         end
  214.         -- end z loop
  215.     end
  216.     say("should not hit this code line")
  217. end
  218. -- --------------------------------
  219. local function giantTreeMode(nCurrentHeight)
  220.  
  221.     local startX, startY, startZ = 0,0,nCurrentHeight
  222.     posX, posY, posZ = 0,0,nCurrentHeight
  223.     vX, vY = 0,-1
  224.     local nRadius = 4
  225.     local nMinHeight = 3
  226.     local nMaxHeight = 9
  227.    
  228.     -- go to start position
  229.     while posZ < nCurrentHeight do
  230.         if turtle.detectUp() then
  231.             turtle.digUp()
  232.         end
  233.         turtle.up()
  234.         posZ = posZ + 1
  235.     end
  236.    
  237.     for i=0,nRadius do
  238.         if turtle.detect() then
  239.             turtle.dig()
  240.         end
  241.         turtle.forward()
  242.         posY = posY + 1
  243.     end
  244.    
  245.     turnLeft()
  246.    
  247.     for i=0,nRadius do
  248.         if turtle.detect() then
  249.             turtle.dig()
  250.         end
  251.         turtle.forward()
  252.         posX = posX - 1
  253.     end
  254.    
  255.     turnRight()
  256.     turnRight()
  257.    
  258.     work(nMaxHeight - nMinHeight, nRadius)
  259.    
  260. end
  261. -- --------------------------------
  262. local function harvestTree()
  263.     local nHeight = 0
  264.     say("Harvesting Tree")
  265.     turtle.dig()
  266.     turtle.forward()
  267.     while turtle.detectUp() do
  268.         turtle.digUp()
  269.         turtle.up()
  270.         nHeight = nHeight + 1
  271.     end
  272.    
  273.     -- check if giant tree.
  274.     -- check by height...
  275.     if nHeight > 6 then
  276.         say("*** GIANT TREE MODE ***")
  277.         return giantTreeMode(nHeight)
  278.     else
  279.         -- normal tree. go back down   
  280.         while not turtle.detectDown() do
  281.             turtle.down()
  282.         end
  283.         turtle.back()
  284.     end
  285.        
  286.     say("Finished harvesting tree")
  287. end
  288. -- --------------------------------
  289. local function detectTree()
  290.     if turtle.detect() == false then
  291.         say("No tree detected(1)")
  292.         return false
  293.     end
  294.    
  295.     if turtle.up() == false then
  296.         say("Cannot move up, must be a tree")
  297.         return true
  298.     end
  299.    
  300.     if turtle.detect() == true then
  301.         turtle.down()
  302.         say("Tree detected")       
  303.         return true
  304.     else
  305.         turtle.down()
  306.         say("Possible sapling detected")
  307.         return false
  308.     end
  309.    
  310.     return false
  311. end
  312. -- --------------------------------
  313. local function applyBoneMeal()
  314.     say("Trying to apply BoneMeal")
  315.    
  316.     -- place while there is no tree
  317.     local isatree
  318.     local abort = false
  319.     local nBoneMeal = 0
  320.     local nMax = 5
  321.    
  322.     while abort == false do
  323.    
  324.         say("Looping for BoneMeal")
  325.        
  326.         isatree = detectTree()
  327.         nBoneMeal = scanForBoneMeal()
  328.        
  329.         if isatree == false and nBoneMeal > 0 and turtle.detect() == true then
  330.             turtle.select(3)
  331.             turtle.place() 
  332.             say("Applied BoneMeal")
  333.         end
  334.        
  335.         if isatree == true or nBoneMeal == 0 then
  336.             abort = true
  337.         end    
  338.        
  339.         nMax = nMax - 1
  340.        
  341.         if nMax <= 0 then
  342.             abort = true
  343.             say("BoneMeal: Max tries")
  344.         end
  345.     end
  346. end
  347. -- --------------------------------
  348. local function plantTree(harvested)
  349.     say("Planting new Tree")
  350.     turtle.select(2)
  351.     turtle.place() 
  352.     if bBoneMeal == true and harvested == false then   
  353.         applyBoneMeal()    
  354.     end
  355.     say("Finished planting new Tree")
  356. end
  357. -- --------------------------------
  358. local function spaceLeft()
  359.     local nSpace = 0
  360.     for n=1,16 do
  361.         local x = turtle.getItemCount(n)
  362.         if x == 0 then
  363.             nSpace = nSpace + 1
  364.         end
  365.     end
  366.     return nSpace
  367. end
  368.  
  369. -- --------------------------------
  370. local function moveForward(nSteps, pickupStuff)
  371.     if nSteps == 0 then
  372.         return true
  373.     end
  374.     while nSteps > 0 do
  375.         if pickupStuff == true then
  376.             turtle.suck()          
  377.         end
  378.    
  379.         if turtle.forward() == false then
  380.             return false
  381.         end
  382.         nSteps = nSteps - 1
  383.     end
  384. end
  385.  
  386. -- --------------------------------
  387. local function dump(keepBattery)
  388.     local x = 1
  389.     if keepBattery == true then
  390.         x = 2
  391.     end
  392.     for n=x,16 do
  393.         turtle.select(n)
  394.         turtle.drop()
  395.     end
  396. end
  397. -- --------------------------------
  398. local function restock()
  399.     -- move to first box (energy)
  400.     say("move to first box (energy)");
  401.     turnLeft()
  402.     moveForward(2,true)
  403.     turnRight()
  404.     -- pick up energy cells
  405.     turtle.select(1)
  406.     turtle.suck()
  407.    
  408.     -- move to second box (saplings)
  409.     say("move to second box (saplings)");
  410.     turnLeft()
  411.     moveForward(2,true)
  412.     turnRight()
  413.     -- pick up saplings
  414.     turtle.select(2)
  415.     turtle.suck()
  416.    
  417.     if bBoneMeal then      
  418.         -- move to third box (bone meal)
  419.         say("move to third box (bone meal)");
  420.         turnLeft()
  421.         moveForward(2,true)
  422.         turnRight()
  423.         -- pick up bone meal
  424.         turtle.select(3)
  425.         turtle.suck()
  426.     end
  427.    
  428.     turnRight()
  429.    
  430.     -- return to start
  431.     say("return to start");
  432.     if bBoneMeal then
  433.         moveForward(6,true)
  434.     else
  435.         moveForward(4,true)
  436.     end
  437. end
  438. -- --------------------------------
  439.  
  440.  
  441. -- ----------------------------------------
  442. -- Main -----------------------------------
  443. -- ----------------------------------------
  444.  
  445. local function mainRun()
  446.  
  447.     -- go to restock position
  448.     say("go to restock position")
  449.     turnRight()
  450.  
  451.     -- dump any contents
  452.     say("dump any contents")
  453.     dump(false)
  454.     -- wait?
  455.     -- restock
  456.     say("restock")
  457.     restock()
  458.  
  459.     -- face in original position
  460.     say("face in original position")
  461.     turnRight()
  462.  
  463.  
  464.     -- check fuel
  465.     nFuel = turtle.getFuelLevel()
  466.     if nFuel < 100 then
  467.         turtle.select(1)
  468.         turtle.refuel(2)
  469.         sleep(1)
  470.         nFuel = turtle.getFuelLevel()
  471.         if nFuel < 100 then
  472.             say("Fuel level below minimum treshold of 100")
  473.             return false
  474.         end
  475.     end
  476.  
  477.     -- inital checks if everything is on board
  478.  
  479.     -- check for saplings
  480.     if scanForSaplings() == false then
  481.         say("We have no saplings")
  482.         return false
  483.     end
  484.  
  485.     -- check for BoneMeal
  486.     if bBoneMeal == true and scanForBoneMeal() == 0 then
  487.         say("We have no bonemeal. Continue, but disable feature.")
  488.         bBoneMeal = false
  489.     end
  490.  
  491.     -- check if space in inventory
  492.     if spaceLeft() == 0 then
  493.         say("Inventory is full. Need attention.")
  494.         return false
  495.     end
  496.  
  497.  
  498.  
  499.     -- move to first position ------------------------
  500.     if moveForward(2,true) == false then
  501.         say("Move(1): Failed to move forward")
  502.         return false
  503.     end
  504.  
  505.     turnRight()
  506.  
  507.     if moveForward(3,true) == false then
  508.         say("Move(1,2): Failed to move forward")
  509.         return false
  510.     end
  511.  
  512.     turnLeft()
  513.  
  514.     -- check for tree
  515.     if detectTree() == true then
  516.         harvestTree()
  517.         plantTree(true)
  518.     elseif turtle.detect() == false then
  519.         say("No Tree found. Planting")
  520.         plantTree(false)
  521.     elseif bBoneMeal == true then
  522.         applyBoneMeal()
  523.     end
  524.  
  525.     turnRight()
  526.  
  527.     -- move to second position ------------------------
  528.  
  529.     if moveForward(7,true) == false then
  530.         say("Move(2,1): Failed to move forward")
  531.         return false
  532.     end
  533.  
  534.     turnLeft()
  535.  
  536.     -- check for tree
  537.     if detectTree() == true then
  538.         harvestTree()
  539.         plantTree(true)
  540.     elseif turtle.detect() == false then
  541.         say("No Tree found. Planting")
  542.         plantTree(false)
  543.     elseif bBoneMeal == true then
  544.         applyBoneMeal()
  545.     end
  546.  
  547.     turnRight()
  548.  
  549.     -- move to third position ------------------------
  550.  
  551.  
  552.     if moveForward(1,true) == false then
  553.         say("Move(3,1): Failed to move forward")
  554.         return false
  555.     end
  556.  
  557.     turnLeft()
  558.  
  559.     if moveForward(9,true) == false then
  560.         say("Move(3,2): Failed to move forward")
  561.         return false
  562.     end
  563.  
  564.     turnLeft()
  565.  
  566.     if moveForward(1,true) == false then
  567.         say("Move(3,1): Failed to move forward")
  568.         return false
  569.     end
  570.  
  571.     turnLeft()
  572.  
  573.     -- check for tree
  574.     if detectTree() == true then
  575.         harvestTree()
  576.         plantTree(true)
  577.     elseif turtle.detect() == false then
  578.         say("No Tree found. Planting")
  579.         plantTree(false)
  580.     elseif bBoneMeal == true then
  581.         applyBoneMeal()
  582.     end
  583.  
  584.     turnRight()
  585.  
  586.     -- move to fourth position ------------------------
  587.  
  588.     if moveForward(7,true) == false then
  589.         say("Move(2,1): Failed to move forward")
  590.         return false
  591.     end
  592.  
  593.     turnLeft()
  594.  
  595.     -- check for tree
  596.     if detectTree() == true then
  597.         harvestTree()
  598.         plantTree(true)
  599.     elseif turtle.detect() == false then
  600.         say("No Tree found. Planting")
  601.         plantTree(false)
  602.     elseif bBoneMeal == true then
  603.         applyBoneMeal()
  604.     end
  605.  
  606.     turnRight()
  607.  
  608.     -- return ------------------------
  609.  
  610.     if moveForward(3,true) == false then
  611.         say("Move(4,1): Failed to move forward")
  612.         return false
  613.     end
  614.  
  615.     turnLeft()
  616.  
  617.     if moveForward(11,true) == false then
  618.         say("Move(4,2): Failed to move forward")
  619.         return false
  620.     end
  621.  
  622. --  say("Should be in home position, facing chest. Dumping stuff.")
  623.  
  624.     say("Dumping stuff.")
  625.     dump(false)
  626.     say("Face start position") 
  627.     turnLeft()
  628.     local c = turtle.getFuelLevel()
  629.     c = nFuel - c
  630.     say("==== Run "..nRuns.." complete. "..c.." E consumed ====")
  631.     nRuns = nRuns + 1
  632.     return true
  633. end
  634.  
  635.  
  636. -- -------------------------
  637. -- init wireless
  638. ----------------------------
  639. if bUserModem == true then
  640.   bRedNet = rednet.open("right")
  641.   if not bRedNet == true then
  642.     bRedNet = rednet.open("left")
  643.   end
  644.   bRedNet = true
  645. end
  646.  
  647.  
  648.  
  649. if bRedNet == true then
  650.     say("Rednet enabled")
  651.     rednet.send(nMasterComputerID, "HarvestBot 01 reporting in.")
  652. else
  653.     say("no rednet available")
  654. end
  655.  
  656. while bNoError == true do
  657.     print ("Starting Run "..(nRuns+1))
  658.     bNoError = mainRun()
  659.     nFuel = turtle.getFuelLevel()
  660.     print ("Fuel level: "..nFuel)
  661.     if bNoError == true then
  662.         say("Sleeping for 60 seconds")
  663.         sleep(10)
  664.         say("Sleeping for 50 seconds")
  665.         sleep(10)
  666.         say("Sleeping for 40 seconds")
  667.         sleep(10)
  668.         say("Sleeping for 30 seconds")
  669.         sleep(10)
  670.         say("Sleeping for 20 seconds")
  671.         sleep(10)
  672.         say("Sleeping for 10 seconds")
  673.         sleep(10)
  674.     else
  675.         say("Aborting after "..nRuns.." runs")
  676.     end
  677. end
Advertisement
Add Comment
Please, Sign In to add comment