Advertisement
Guest User

startup

a guest
Apr 17th, 2014
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.45 KB | None | 0 0
  1. local rows = {}
  2. for i = 1, 25 do
  3. table.insert(rows,18)
  4. end
  5. local types = {}
  6. for i = 1, 9 do
  7. table.insert(types,14)
  8. end
  9. for i = 1, 2 do
  10. table.insert(types,15)
  11. end
  12. for i = 1, 14 do
  13. table.insert(types,16)
  14. end
  15. local travelDist = 10
  16. local harvestTime = 2400
  17. local skipFirstFarm = not(...)
  18. local home = {x = -247, y = 64, z = 136}
  19.  
  20. farmerStatus = "Standby"
  21. farmerNextHarvest = 0
  22.  
  23. local function main()
  24.     while not gps.locate() do
  25.         farmerStatus = "Error: No GPS signal"
  26.         print("Error: No GPS signal")
  27.         sleep(30)
  28.     end
  29.     local function goHomeByGPS(turned)
  30.         farmerStatus = "Going home..."
  31.         local function travel(dist, positive)
  32.             if ((math.abs(dist) == dist) and positive) or ((math.abs(dist) ~= dist) and not positive) then
  33.             else
  34.                 turtle.turnLeft()
  35.                 turtle.turnLeft()
  36.                 turtle.forward()
  37.                 turtle.forward()
  38.             end
  39.             for i = 1, math.abs(dist)-1 do
  40.                 if not turtle.forward() then
  41.                     if turned then
  42.                         if diffY ~= 0 then
  43.                             print("Maybe if I go up/down...")
  44.                             if math.abs(diffY) == diffY then
  45.                                 for i = 1, diffY do
  46.                                     turtle.up()
  47.                                 end
  48.                             else
  49.                                 for i = 1, math.abs(diffY) do
  50.                                     turtle.down()
  51.                                 end
  52.                             end
  53.                             local ret = goHomeByGPS(true)
  54.                             return ret
  55.                         else
  56.                             return false
  57.                         end
  58.                     end
  59.                     turtle.turnLeft()
  60.                     local ret = goHomeByGPS(true)
  61.                     return ret
  62.                 end
  63.             end
  64.             turtle.turnLeft()
  65.             return goHomeByGPS()
  66.         end
  67.         sleep(0.1)
  68.         local initX, initY, initZ = gps.locate()
  69.         print("I am currently at: "..initX..", "..initY..", "..initZ)
  70.         local diffX = (initX - home.x)*-1
  71.         local diffY = (initY - home.y)*-1
  72.         local diffZ = (initZ - home.z)*-1
  73.         print("I'm X:"..diffX.." and Z:"..diffZ.." away from home!")
  74.         if (diffX == 0) and (diffZ == 0) then
  75.             if math.abs(diffY) == diffY then
  76.                 for i = 1, diffY do
  77.                     turtle.up()
  78.                 end
  79.             else
  80.                 for i = 1, math.abs(diffY) do
  81.                     turtle.down()
  82.                 end
  83.             end
  84.             local initX, initY, initZ = gps.locate()
  85.             print("I am now at: "..initX..", "..initY..", "..initZ)
  86.             if home.x == initX and home.y == initY and home.z == initZ then
  87.                 for i = 1, 4 do
  88.                     turtle.forward()
  89.                     local x = gps.locate()
  90.                     turtle.back()
  91.                     if x > home.x then
  92.                         print("Alright, I'm home! Awesome!")
  93.                         return true
  94.                     end
  95.                     turtle.turnLeft()
  96.                 end
  97.                 return false
  98.             else
  99.                 print("Ah crap, I miscalculated :/")
  100.                 print("Well, I'm screwed")
  101.                 return false
  102.             end
  103.         elseif turtle.forward() then
  104.             secondX, _, secondZ = gps.locate()
  105.             if secondX > initX then
  106.                 if diffX == 0 then
  107.                     turtle.back()
  108.                     turtle.turnLeft()
  109.                     local ret = goHomeByGPS()
  110.                 else
  111.                     return travel(diffX, true)
  112.                 end
  113.             elseif secondX < initX then
  114.                 if diffX == 0 then
  115.                     turtle.back()
  116.                     turtle.turnLeft()
  117.                     local ret = goHomeByGPS()
  118.                 else
  119.                     return travel(diffX, false)
  120.                 end
  121.             elseif secondZ > initZ then
  122.                 if diffZ == 0 then
  123.                     turtle.back()
  124.                     turtle.turnLeft()
  125.                     local ret = goHomeByGPS()
  126.                 else
  127.                     return travel(diffZ, true)
  128.                 end
  129.             elseif secondZ < initZ then
  130.                 if diffZ == 0 then
  131.                     turtle.back()
  132.                     turtle.turnLeft()
  133.                     local ret = goHomeByGPS()
  134.                 else
  135.                     return travel(diffZ, false)
  136.                 end
  137.             end
  138.         elseif turtle.back() then
  139.             turtle.forward()
  140.             turtle.turnLeft()
  141.             turtle.turnLeft()
  142.             local ret = goHomeByGPS()
  143.             return ret
  144.         elseif turned then
  145.             print("Crap, I'm stuck!")
  146.             print("D: Turtle is sad")
  147.             if diffY ~= 0 then
  148.                 print("Maybe if I go up/down...")
  149.                 if math.abs(diffY) == diffY then
  150.                     for i = 1, diffY do
  151.                         turtle.up()
  152.                     end
  153.                 else
  154.                     for i = 1, math.abs(diffY) do
  155.                         turtle.down()
  156.                     end
  157.                 end
  158.                 local ret = goHomeByGPS(true)
  159.                 return ret
  160.             else
  161.                 return false
  162.             end
  163.         elseif turtle.getFuelLevel() < 1 then
  164.             print("Crap, out of fuel!")
  165.             return false
  166.         else
  167.             turtle.turnLeft()
  168.             local ret = goHomeByGPS(true)
  169.             return ret
  170.         end
  171.     end
  172.  
  173. --[[
  174.     local storage = {}
  175.     local storageData = {}
  176.     storage.save = function()
  177.             for i = 1, 16 do
  178.                     storageData[i] = turtle.getItemCount(i)
  179.             end
  180.     end
  181.  
  182.     storage.getSlotDiff = function()
  183.             local diff = {}
  184.             for i = 1, 16 do
  185.                     if storageData[i] ~= turtle.getItemCount(i) then
  186.                             table.insert(diff,i)
  187.                     end
  188.             end
  189.             return diff
  190.     end
  191. ]]--
  192.  
  193.     local enoughFuel = function()
  194.             local total = travelDist
  195.             for _, row in pairs(rows) do
  196.                     total = total+row
  197.             end
  198.             total = total*1.2
  199.             return turtle.getFuelLevel() > total
  200.     end
  201.  
  202.     local function farm()
  203.         local direction = "down"
  204.  
  205.         for rowNumber, rowLength in pairs(rows) do
  206.             for block = 1, rowLength do
  207.                 turtle.select(1)
  208.                 turtle.digDown()
  209.                 for slot = 1, 4 do
  210.                     turtle.select(slot)
  211.                     if turtle.compareTo(types[rowNumber]) then
  212.                         turtle.placeDown()
  213.                     end
  214.                 end
  215.                 if block ~= rowLength then
  216.                     if not turtle.forward() then
  217.                         print("There is an obstruction in the way!")
  218.                         print("Bailing: Returning to base")
  219.                         local ret = goHomeByGPS()
  220.                         return false, ret
  221.                     end
  222.                 end
  223.             end
  224.             if rowNumber == #rows then
  225.                 local ret = goHomeByGPS()
  226.                 return true, ret
  227.             elseif direction == "down" then
  228.                 turtle.turnRight()
  229.                 turtle.turnRight()
  230.                 if rowNumber == #rows then
  231.                         break
  232.                 end
  233.                 for i = 1, rows[rowNumber]-rows[rowNumber+1] do
  234.                         turtle.forward()
  235.                 end
  236.                 turtle.turnLeft()
  237.                 turtle.forward()
  238.                 turtle.turnRight()
  239.                 direction = "up"
  240.             elseif direction == "up" then
  241.                 turtle.turnLeft()
  242.                 turtle.forward()
  243.                 turtle.turnLeft()
  244.                 direction = "down"
  245.             end
  246.         end
  247.     end
  248.  
  249.     local function farmingMonitor()
  250.         while true do
  251.             if not farmerStatus:find("Lost") then
  252.                 if enoughFuel() then
  253.                     print("Time to farm!")
  254.                     if not skipFirstFarm then
  255.                         farmerStatus = "Farming..."
  256.                         local ret, stat = farm()
  257.                     else
  258.                         print("Skipping initial farm")
  259.                         skipFirstFarm = true
  260.                     end
  261.                     if stat == false then
  262.                         print("I'm lost! D: Help me!")
  263.                         farmerStatus = "Lost! \\[Save me pwease :(\\]"
  264.                     else
  265.                         farmerStatus = "Waiting for next harvest..."
  266.                         for i = 1, harvestTime/5 do
  267.                             sleep(5)
  268.                             farmerNextHarvest = harvestTime-(i*5)
  269.                             print("Waiting for next harvest: "..farmerNextHarvest)
  270.                         end
  271.                     end
  272.                 else
  273.                     farmerStatus = "Outta fuel D:!"
  274.                     print("I'm out of fuel! Please place fuel")
  275.                     print("In slot 13 (Bottom left corner)")
  276.                     print("and press any key to continue")
  277.                     os.pullEvent("key")
  278.                     turtle.select(13)
  279.                     if not turtle.refuel() then
  280.                         print("D: That's not fuel! Try again.")
  281.                     else
  282.                         print("Thanks! Now farming...")
  283.                         farmerStatus = "Farming..."
  284.                         farm()
  285.                     end
  286.                 end
  287.                
  288.             end
  289.             sleep(2)
  290.         end
  291.     end
  292.  
  293.     if not goHomeByGPS() then
  294.         if turtle.getFuelLevel() > 0 then
  295.                 farmerStatus = "Lost! \\[Save me pwease :(\\]"
  296.         else
  297.             farmerStatus = "Outta fuel D:!"
  298.             while true do
  299.                 print("I'm out of fuel! Please place fuel")
  300.                 print("In slot 13 (Bottom left corner)")
  301.                 print("and press a key to continue")
  302.                 os.pullEvent("key")
  303.                 turtle.select(13)
  304.                 if not turtle.refuel() then
  305.                     print("That's not fuel! D: Try again")
  306.                 else
  307.                     print("Thanks, heading home!")
  308.                     goHomeByGPS()
  309.                     break
  310.                 end
  311.             end
  312.         end
  313.     end
  314.      
  315.     farmingMonitor()
  316. end
  317.  
  318. while true do
  319.         parallel.waitForAny(function() shell.run("/lost") end, main)
  320.         print("Terminate now.")
  321.         sleep(3)
  322. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement