Advertisement
DomanoSV

testFarm

Jan 19th, 2020
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.78 KB | None | 0 0
  1. -- Variables                        --
  2. local turtleState = {}
  3. local inputChest = {}
  4. local outputChest = {}
  5. local fuelChest = {}
  6.  
  7. turtleStateFilename = "turtleState"
  8.  
  9. -- Functions                        --
  10.  
  11. findItem = function(itemName)
  12.     for i=1,16,1 do
  13.         itemData = turtle.getItemDetail(i)
  14.  
  15.         if itemData ~= nil then
  16.             if itemData.name == itemName then
  17.                 return i
  18.             end
  19.         end
  20.     end
  21.  
  22.     return false
  23. end
  24.  
  25. turnRight = function()
  26.     if not turtle.turnRight() then
  27.         error('Unable to move.') -- Error will never be seen as turtle can never fail to turn left.
  28.     else
  29.         isFacing = isFacing + 1
  30.  
  31.         if isFacing > 3 then
  32.             isFacing = 0
  33.         end
  34.  
  35.         settings.set("isFacing", isFacing)
  36.         settings.save(turtleStateFilename)
  37.     end
  38. end
  39.  
  40. turnLeft = function()
  41.     if not turtle.turnLeft() then
  42.         error('Unable to move.') -- Error will never be seen as turtle can never fail to turn left.
  43.     else
  44.         isFacing = isFacing - 1
  45.  
  46.         if isFacing < 0 then
  47.             isFacing = 3
  48.         end
  49.  
  50.         settings.set("isFacing", isFacing)
  51.         settings.save(turtleStateFilename)
  52.     end
  53. end
  54.  
  55. turnToDir = function(direction)
  56.     if direction == 0 then
  57.         if isFacing == 1 then
  58.             turnLeft()
  59.         elseif isFacing == 2 then
  60.             turnRight()
  61.             turnRight()
  62.         elseif isFacing == 3 then
  63.             turnRight()
  64.         end
  65.     elseif direction == 1 then
  66.         if isFacing == 0 then
  67.             turnRight()
  68.         elseif isFacing == 3 then
  69.             turnLeft()
  70.             turnLeft()
  71.         elseif isFacing == 2 then
  72.             turnLeft()
  73.         end
  74.     elseif direction == 2 then
  75.         if isFacing == 1 then
  76.             turnRight()
  77.         elseif isFacing == 0 then
  78.             turnLeft()
  79.             turnLeft()
  80.         elseif isFacing == 3 then
  81.             turnLeft()
  82.         end
  83.     elseif direction == 3 then
  84.         if isFacing == 0 then
  85.             turnLeft()
  86.         elseif isFacing == 1 then
  87.             turnRight()
  88.             turnRight()
  89.         elseif isFacing == 2 then
  90.             turnRight()
  91.         end
  92.     end
  93.  
  94.     if isFacing == direction then return true end
  95. end
  96.  
  97. moveForward = function()
  98.     turtle.dig()
  99.  
  100.     if not turtle.forward() then
  101.         error("Unable to move.")
  102.     else
  103.         if isFacing == 0 then
  104.             xPos = xPos + 1
  105.             settings.set("xPos", xPos)
  106.         elseif isFacing == 1 then
  107.             yPos = yPos + 1
  108.             settings.set("yPos", yPos)
  109.         elseif isFacing == 2 then
  110.             xPos = xPos - 1
  111.             settings.set("xPos", xPos)
  112.         elseif isFacing == 3 then
  113.             yPos = yPos - 1
  114.             settings.set("yPos", yPos)
  115.         end
  116.         settings.save(turtleStateFilename)
  117.     end
  118. end
  119.  
  120. moveBackward = function()
  121.     if not turtle.back() then
  122.         error("Unable to move.")
  123.     else
  124.         if isFacing == 0 then
  125.             xPos = xPos - 1
  126.             settings.set("xPos", xPos)
  127.         elseif isFacing == 1 then
  128.             yPos = yPos - 1
  129.             settings.set("yPos", yPos)
  130.         elseif isFacing == 2 then
  131.             xPos = xPos + 1
  132.             settings.set("xPos", xPos)
  133.         elseif isFacing == 3 then
  134.             yPos = yPos + 1
  135.             settings.set("yPos", yPos)
  136.         end
  137.         settings.save(turtleStateFilename)
  138.     end
  139. end
  140.  
  141. moveUp = function()
  142.     if not turtle.up() then
  143.         error("Unable to move.")
  144.     else
  145.         zPos = zPos + 1
  146.         settings.set("zPos", zPos)
  147.         settings.save(turtleStateFilename)
  148.     end
  149. end
  150.  
  151. moveDown = function()
  152.     if not turtle.down() then
  153.         error("Unable to move.")
  154.     else
  155.         zPos = zPos - 1
  156.         settings.set("zPos", zPos)
  157.         settings.save(turtleStateFilename)
  158.     end
  159. end
  160.    
  161.  
  162. returnToPos = function(x,y,z,f)
  163.     while yPos ~= y do
  164.         if x >= -1 then
  165.             if xPos == -1 then
  166.                 if isFacing == 1 then
  167.                     turnLeft()
  168.                 elseif isFacing == 2 then
  169.                     turnRight()
  170.                     turnRight()
  171.                 elseif isFacing == 3 then
  172.                     turnRight()
  173.                 end
  174.                 moveForward()
  175.             end
  176.         else
  177.             if isFacing == 1 then
  178.                 turnRight()
  179.             elseif isFacing == 0 then
  180.                 turnRight()
  181.                 turnRight()
  182.             elseif isFacing == 3 then
  183.                 turnLeft()
  184.             end
  185.             moveForward()
  186.         end
  187.  
  188.         if yPos > y then
  189.             if isFacing == 0 then
  190.                 turnLeft()
  191.             elseif isFacing == 1 then
  192.                 turnRight()
  193.                 turnRight()
  194.             elseif isFacing == 2 then
  195.                 turnRight()
  196.             end
  197.             moveForward()
  198.         elseif yPos < y then
  199.             if isFacing == 0 then
  200.                 turnRight()
  201.             elseif isFacing == 3 then
  202.                 turnLeft()
  203.                 turnLeft()
  204.             elseif isFacing == 2 then
  205.                 turnLeft()
  206.             end
  207.             moveForward()
  208.         end
  209.     end
  210.  
  211.     while xPos ~= x do
  212.         if xPos > x then
  213.             if isFacing == 1 then
  214.                 turnRight()
  215.             elseif isFacing == 0 then
  216.                 turnLeft()
  217.                 turnLeft()
  218.             elseif isFacing == 3 then
  219.                 turnLeft()
  220.             end
  221.             moveForward()
  222.         elseif xPos < x then
  223.             if isFacing == 1 then
  224.                 turnLeft()
  225.             elseif isFacing == 2 then
  226.                 turnRight()
  227.                 turnRight()
  228.             elseif isFacing == 3 then
  229.                 turnRight()
  230.             end
  231.             moveForward()
  232.         end
  233.     end
  234.  
  235.     while zPos ~= z do
  236.         if zPos > z then
  237.             moveDown()
  238.         elseif zPos < z then
  239.             moveUp()
  240.         end
  241.     end
  242.  
  243.     turnToDir(f)
  244. end
  245.  
  246. selectgFillDownBlock = function()
  247.     if xPos == -1 or xPos == xSize or yPos == -1 or yPos == ySize then
  248.         fillDownTo("minecraft:stone", "minecraft:stone_bricks")
  249.     else
  250.         fillDownTo("minecraft:stone", "minecraft:dirt")
  251.  
  252.         if (math.fmod(xPos, 5) == 4 or math.fmod(xPos, 5) == -1) and (math.fmod(yPos, 5) == 4 or math.fmod(yPos, 5) == -1) then
  253.             print("water here")
  254.             turtle.digDown()
  255.  
  256.             if not findItem("minecraft:water_bucket") then
  257.                 local x = xPos
  258.                 local y = yPos
  259.                 local z = zPos
  260.                 local f = isFacing
  261.  
  262.                 returnHome()
  263.  
  264.                 while not findItem("minecraft:water_bucket") do
  265.                     print("Waiting for minecraft:water_bucket to be put in inventory.")
  266.                     sleep(2)
  267.                 end
  268.  
  269.                 returnToPos(x,y,z,f)
  270.             end
  271.  
  272.             turtle.select(findItem("minecraft:water_bucket"))
  273.  
  274.             turtle.placeDown()
  275.  
  276.         else
  277.             print("dirt here")
  278.         end
  279.     end
  280. end
  281.  
  282. returnHome = function()
  283.     while zPos ~= 1 do
  284.         if zPos > 1 then
  285.             moveDown()
  286.         elseif zPos < 1 then
  287.             moveUp()
  288.         end
  289.     end
  290.  
  291.     while yPos ~= 0 do
  292.         if xPos == -1 then
  293.             if isFacing == 1 then
  294.                 turnLeft()
  295.             elseif isFacing == 2 then
  296.                 turnRight()
  297.                 turnRight()
  298.             elseif isFacing == 3 then
  299.                 turnRight()
  300.             end
  301.             moveForward()
  302.         end
  303.  
  304.         if yPos > 0 then
  305.             if isFacing == 0 then
  306.                 turnLeft()
  307.             elseif isFacing == 1 then
  308.                 turnRight()
  309.                 turnRight()
  310.             elseif isFacing == 2 then
  311.                 turnRight()
  312.             end
  313.  
  314.             moveForward()
  315.         elseif yPos < 0 then
  316.             if isFacing == 0 then
  317.                 turnRight()
  318.             elseif isFacing == 3 then
  319.                 turnLeft()
  320.                 turnLeft()
  321.             elseif isFacing == 2 then
  322.                 turnLeft()
  323.             end
  324.  
  325.             moveForward()
  326.         end
  327.     end
  328.  
  329.     while xPos ~= -1 do
  330.         if xPos > -1 then
  331.             if isFacing == 1 then
  332.                 turnRight()
  333.             elseif isFacing == 0 then
  334.                 turnLeft()
  335.                 turnLeft()
  336.             elseif isFacing == 3 then
  337.                 turnLeft()
  338.             end
  339.  
  340.             moveForward()
  341.         elseif xPos < -1 then
  342.             if isFacing == 1 then
  343.                 turnLeft()
  344.             elseif isFacing == 2 then
  345.                 turnRight()
  346.                 turnRight()
  347.             elseif isFacing == 3 then
  348.                 turnRight()
  349.             end
  350.  
  351.             moveForward()
  352.         end
  353.     end
  354.  
  355.     if xPos == -1 and yPos == 0 then
  356.         if isFacing == 1 then
  357.             turnLeft()
  358.         elseif isFacing == 2 then
  359.             turnRight()
  360.             turnRight()
  361.         elseif isFacing == 3 then
  362.             turnRight()
  363.         end
  364.     end  
  365.  
  366.     print("I AM HOME!")
  367.  
  368. end
  369.  
  370. restock = function(itemName, xPosPrev, yPosPrev, zPosPrev, isFacingPrev)
  371.     print("xyz: ", xPosPrev, " ", yPosPrev, " ", zPosPrev, "is facing: ", isFacingPrev)
  372.     returnHome()
  373.  
  374.     while not findItem(itemName) do
  375.         print("Waiting for ", itemName, " to be put in inventory.")
  376.         sleep(2)
  377.     end
  378.  
  379.     returnToPos(xPosPrev, yPosPrev, zPosPrev, isFacingPrev)
  380.  
  381.     local selectItem = findItem(itemName)
  382.  
  383.     if selectItem ~= false then
  384.         turtle.select(selectItem)
  385.     else
  386.         print("Did I restock?")
  387.     end
  388. end
  389.  
  390. if not settings.load(turtleStateFilename) then
  391.     settings.get("xSize", 9)
  392.     settings.get("ySize", 9)
  393.     settings.set("xPos", -1)
  394.     settings.set("yPos", 0)
  395.     settings.set("zPos", 1)
  396.     settings.set("isFacing", 0)
  397.     settings.set("firstRun", true)
  398.     settings.set("aboveGround", true)
  399.     settings.set("currentStep", {})
  400.     settings.save(turtleStateFilename)
  401. end
  402.  
  403. xSize = settings.get("xSize", 9)
  404. ySize = settings.get("ySize", 9)
  405. xPos = settings.get("xPos", -1)
  406. yPos = settings.get("yPos", 0)
  407. zPos = settings.get("zPos", 1)
  408. firstRun = settings.get("firstRun", true)
  409. aboveGround = settings.get("aboveGround", true)
  410. isFacing = settings.get("isFacing", 0)
  411.  
  412. if xPos == -1 and yPos == 0 then
  413.     print("I am already home")
  414. else
  415.     returnHome()
  416. end
  417.  
  418. calculateWhatsNeeded = function()
  419.     local wallBlocks = 0
  420.     local dirtBlocks = 0
  421.     local waterBlocks = 0
  422.  
  423.     for x=-1,xSize do
  424.         for y=-1,ySize do
  425.             if x == -1 or y == -1 or x == xSize or y == ySize then
  426.                 wallBlocks = wallBlocks + 1
  427.             else
  428.                 if (math.fmod(x, 5) == 4 or math.fmod(x, 5) == -1) and (math.fmod(y, 5) == 4 or math.fmod(y, 5) == -1) then
  429.                     waterBlocks = waterBlocks + 1
  430.                 else
  431.                     dirtBlocks = dirtBlocks + 1
  432.                 end
  433.             end
  434.         end
  435.     end
  436.  
  437.     print("Minimum blocks needed to create farm:")
  438.     print("Wall Blocks: ", wallBlocks)
  439.     print("Dirt Blocks: ", dirtBlocks)
  440.     print("Water Blocks:", waterBlocks)
  441.     print("These are only an estimate, may use a")
  442.     print("significantly larger number of blocks.")  
  443. end
  444.  
  445. fillDownTo = function(blockA, blockB)
  446.     while true do
  447.         local blockBelow, blockData = turtle.inspectDown()
  448.         local hasStone = findItem(blockB)
  449.  
  450.         if not hasStone then
  451.             break
  452.         else
  453.             turtle.select(hasStone)
  454.         end
  455.  
  456.         if blockBelow then
  457.             if blockData.name == blockA then
  458.                 print("I reached ", blockA, ", Wooo!")
  459.                 break
  460.             else
  461.                 turtle.digDown()
  462.                 moveDown()
  463.             end
  464.         end
  465.        
  466.         if not blockBelow then
  467.             moveDown()
  468.         end
  469.         print("Current zCoord: ", zPos)
  470.     end
  471.  
  472.     while zPos < 0 do
  473.         if not findItem(blockB) then
  474.             print("I have run out of stone")
  475.  
  476.             restock(blockB, xPos, yPos, zPos, isFacing)
  477.         else
  478.             turtle.select(findItem(blockB))
  479.         end
  480.  
  481.         moveUp()
  482.         turtle.placeDown()
  483.     end
  484. end
  485.  
  486. while true do
  487.     local test = turtle.getFuelLevel()
  488.     local solidBlockBelow = false
  489.  
  490.     print("Current fuel level: ", test)
  491.  
  492.     calculateWhatsNeeded()
  493.  
  494.     sleep(20)
  495.  
  496.     if firstRun then
  497.         for i=1,6 do
  498.             if i == 1 then
  499.                 returnToPos(-2, -1, 1, 0)
  500.                 print("Clearing a ", xSize, " x ", ySize, " area for the farm.")
  501.  
  502.                 while true do
  503.                     if (xPos <= -1 or xPos >= xSize) and yPos >= ySize then
  504.                         returnHome()
  505.                         break
  506.                     end
  507.                    
  508.                     if xPos == xSize then
  509.                         turnRight()
  510.                         turtle.dig()
  511.                         moveForward()
  512.                         turtle.digUp()
  513.                         turtle.digDown()
  514.                         turnRight()
  515.                     end
  516.                    
  517.                     if xPos == -1 and yPos ~= -1 then
  518.                         turnLeft()
  519.                         turtle.dig()
  520.                         moveForward()
  521.                         turtle.digUp()
  522.                         turtle.digDown()
  523.                         turnLeft()
  524.                     end
  525.                    
  526.                     turtle.dig()
  527.                     moveForward()
  528.                     turtle.digUp()
  529.                     turtle.digDown()
  530.                 end
  531.             elseif i == 2 then
  532.                 returnToPos(-2, -1, 0, 0)
  533.  
  534.                 while true do
  535.                     if (xPos <= -1 or xPos >= xSize) and yPos >= ySize then
  536.                         returnHome()
  537.                         break
  538.                     end
  539.                    
  540.                     if xPos == xSize then
  541.                         turnRight()
  542.                         moveForward()
  543.                         selectgFillDownBlock()
  544.                         turnRight()
  545.                     end
  546.                    
  547.                     if xPos == -1 and yPos ~= -1 then
  548.                         turnLeft()
  549.                         moveForward()
  550.                         selectgFillDownBlock()
  551.                         turnLeft()
  552.                     end
  553.                    
  554.                     moveForward()
  555.                     selectgFillDownBlock()
  556.                 end
  557.             elseif i == 3 then
  558.                 print("I am, will build walls and roof and place torches. ", i)
  559.                 sleep(10)
  560.             elseif i == 4 then
  561.                 print("Reserved. ", i)
  562.                 sleep(1)
  563.             elseif i == 5 then
  564.                 print("Reserved. ", i)
  565.                 sleep(1)
  566.             elseif i == 6 then
  567.                 print("Reserved. ", i)
  568.                 sleep(1)
  569.             end
  570.         end
  571.     else
  572.  
  573.     exit()
  574.  
  575.     if test <= 80 then
  576.         returnHome()
  577.         break
  578.     end
  579.  
  580.     if xPos >= (xSize - 1) and yPos >= (ySize - 1) then
  581.         returnHome()
  582.     end
  583.    
  584.     if xPos == (xSize - 1) then
  585.         turnRight()
  586.         moveForward()
  587.         turnRight()
  588.     end
  589.    
  590.     if xPos == 0 and yPos ~= 0 then
  591.         turnLeft()
  592.         moveForward()
  593.         turnLeft()
  594.     end
  595.    
  596.     moveForward()
  597.    
  598.     if (math.fmod(xPos, 5) == 4 or math.fmod(xPos, 5) == -1) and (math.fmod(yPos, 5) == 4 or math.fmod(yPos, 5) == -1) then
  599.         print("water here")
  600.         turtle.digDown()
  601.     else
  602.         print("dirt here")
  603.     end
  604.  
  605.    
  606.  
  607.     end
  608.  
  609.     print("Current xCoord: ", xPos)
  610.     print("Current yCoord: ", yPos)
  611.  
  612.     sleep(1)
  613.  
  614.    
  615. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement