Advertisement
denvys5

Farmer 2.0

Feb 17th, 2014
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.77 KB | None | 0 0
  1. local tArgs = {...}
  2.  
  3. local direction = {
  4.         ["forw"]  =1;
  5.         ["right"] =2;
  6.         ["back"]  =3;
  7.         ["left"]  =4;
  8.  
  9. }
  10.  
  11. local pos = {
  12.         ["x"]=0;
  13.         ["y"]=0;
  14. }
  15. dir=direction.forw;
  16.  
  17. local lenght
  18. local width
  19.  
  20. local function saveState()
  21.         name = shell.getRunningProgram()
  22.         local file = fs.open(name..".save","w")
  23.  
  24.         file.writeLine(pos.x)
  25.         file.writeLine(pos.y)
  26.         file.writeLine(dir)
  27.         file.writeLine(lenght)
  28.         file.writeLine(width)
  29.        
  30.         file.close()
  31. end
  32.  
  33. local function readState()
  34.         name = shell.getRunningProgram()
  35.         if not fs.exists(name..".save") then
  36.                         return false
  37.         end
  38.         local file = fs.open(name..".save","r")
  39.  
  40.         pos.x =         tonumber(file.readLine())
  41.         pos.y =         tonumber(file.readLine())
  42.         dir =           tonumber(file.readLine())
  43.         lenght =        tonumber(file.readLine())
  44.         width =         tonumber(file.readLine())
  45.  
  46.         file.close()
  47.         return true
  48. end
  49.  
  50. local function turnRight()
  51.         dir=dir+1      
  52.         if dir==5 then dir = direction.forw end
  53.         saveState()
  54.         turtle.turnRight()
  55. end
  56.  
  57. local function turnLeft()
  58.         dir=dir-1      
  59.         if dir==0 then dir = direction.left end
  60.         saveState()
  61.         turtle.turnLeft()
  62. end
  63.  
  64. local function forward()
  65.         while true do
  66.                         if turtle.detect() then --обнаружен блок, сломать
  67.                                         if turtle.dig() then
  68. --                              print("detect block")
  69.                                         else
  70.                                                         print("detect bedrock or unbreaking block")
  71.                                                         return false
  72.                                         end
  73.                         elseif turtle.attack() then -- Впереди нет блока, проверяем есть ли там существо
  74.                                         print("detect monster or player")
  75.                                        
  76.                         else    -- Передвигаемся вперед
  77.                                         local ty,tx = pos.y,pos.x
  78.                                         if dir == direction.forw then
  79.                                                         pos.x=pos.x+1
  80.                                         elseif dir == direction.right then
  81.                                                         pos.y=pos.y+1
  82.                                         elseif dir == direction.back then
  83.                                                         pos.x=pos.x-1
  84.                                         elseif dir == direction.left then
  85.                                                         pos.y=pos.y-1
  86.                                         end
  87.                                         saveState()
  88.                                         print("--")
  89.                                         print(pos.y)
  90.                                         if turtle.forward() then
  91. --                              print("it's ok!")
  92.                                                         return true
  93.                                         else
  94.                                                         -- backup
  95.                                                         if not turtle.detect() then    
  96.                                                                         print("detect unknowing object")
  97.                                                         end
  98.                                                         pos.y = ty
  99.                                                         pos.x = tx
  100.                                                         saveState()
  101.                                                         print(pos.y)
  102.                                                         os.sleep(0.5)
  103.                                         end
  104.                         end
  105.         end
  106. end
  107. local t=turtle
  108. if tArgs == 0 then
  109.  tArgs[1] = 8
  110. end
  111. function refuel()
  112.  t.select(16)
  113.   while t.getFuelLevel()<100 do
  114.     if not turtle.refuel(1) then
  115.       print("Put fuel in slot 16")
  116.       os.sleep(10)
  117.     end
  118.   end
  119. end
  120.  
  121. function cleaninv()
  122.  for i=1,15 do
  123.   t.select(i)
  124.   t.drop()
  125.  end
  126.  t.select(1)
  127. end
  128.  
  129. function suck()
  130.  turnRight()
  131.  t.suck()
  132.  t.suckUp()
  133.  turnLeft()
  134.  t.suck()
  135.  t.suckUp()
  136.  turnLeft()
  137.  t.suck()
  138.  t.suckUp()
  139.  turnRight()
  140. end
  141.  
  142. function main()
  143.  while true do
  144.   for i=1,tArgs[1] do
  145.    suck()
  146.    refuel()
  147.    forward()
  148.   end
  149.   turnRight()
  150.   turnRight()
  151.   for i=1,tArgs[1] do
  152.    suck()
  153.    refuel()
  154.    forward()
  155.   end
  156.   cleaninv()
  157.   turnRight()
  158.   turnRight()
  159.   refuel()
  160.   sleep(120)
  161.  end
  162. end
  163.  
  164. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement