Advertisement
ZNZNCOOP

OC_Farmer

Dec 18th, 2014
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.04 KB | None | 0 0
  1. robot= require('robot')
  2. c= require('component')
  3. g= c.generator
  4. i= c.inventory_controller
  5. local cell_fuel= 16
  6. local MAX= 100
  7. SOUTH= 0 -- юг +z
  8. EAST= 3 -- восток +x
  9. NORTH= 2 -- север -z
  10. WEST= 1 -- запад  -x
  11. Max_World_Y= 300
  12. Min_World_Y= 0
  13. local SEEDCELL= 15
  14. local NULLCELL= 16
  15. io.write('Side:')
  16. local sides= io.read()+0
  17. io.write('X:')
  18. local x= io.read()+0
  19. io.write('Y:')
  20. local y= io.read()+0
  21. io.write('Z:')
  22. local z= io.read()+0
  23. function fuel()
  24.    robot.select(cell_fuel)
  25.    count_fuel= robot.count()
  26.    g.insert(count_fuel)
  27. end
  28.  
  29. function turnLeft()
  30.         sides= sides-1
  31.         if (sides<0) then sides= 3 end
  32.         robot.turnLeft()
  33. end
  34. function turnRight()
  35.         sides= sides+1
  36.         if (sides>3) then sides= 0 end
  37.         robot.turnRight()
  38. end
  39. function forward()
  40.         if (robot.detect()) then
  41.        return false
  42.         end
  43.         if (sides == SOUTH) then
  44.            z=z+1
  45.         elseif (sides == EAST) then
  46.            x= x+1
  47.         elseif (sides == NORTH) then
  48.            z= z-1
  49.         elseif(sides == WEST) then
  50.            x= x-1
  51.         end
  52.         robot.forward()
  53. end
  54. function up()
  55.    if (y>=Max_World_Y) then
  56.           y= Max_World_Y
  57.    elseif (robot.detectUp() ~= true) then
  58.           y= y+1
  59.           robot.up()
  60.    end
  61.  
  62. end
  63. function down()
  64.         if (y<=Min_World_Y) then
  65.           y= Min_World_Y
  66.    elseif (robot.detectDown() ~= true) then
  67.           y= y-1
  68.           robot.down()
  69.    end
  70. end
  71.  
  72. function seeds()
  73. while true do
  74.    seeds_list= {}
  75.    for i=1,14 do
  76.       robot.select(i)
  77.       if (robot.compareTo(SEEDCELL)) then
  78.          seeds_list[#seeds_list+1]= i
  79.       end
  80.    end
  81.    if (seeds_list ~= nil) then
  82.       return seeds_list
  83.    else
  84.       print('Need seeds!')
  85.       os.sleep(1)
  86.    end
  87. end
  88. end
  89.  
  90. function turns(side)
  91.    while true do
  92.       if (side == sides) then
  93.          break
  94.       end
  95.       turnRight()
  96.    end
  97. end
  98.  
  99. function right()
  100.    turnRight()
  101.    robot.swing()
  102.    forward()
  103.    turnRight()
  104. end
  105. function left()
  106.    turnLeft()
  107.    robot.swing()
  108.    forward()
  109.    turnLeft()
  110. end
  111.  
  112. function createcell(seedss_list)
  113.    seedss_list= update_seeds(seedss_list)
  114.    local toolcell= seedss_list[1]
  115.    robot.select(toolcell)
  116.    robot.useDown()
  117.    i.equip()
  118.    robot.useDown()
  119.    i.equip()
  120. end
  121.  
  122. function updatecell(seedss_list)
  123.    seedss_list= update_seeds(seedss_list)
  124.    local toolcell= seedss_list[1]
  125.    robot.select(toolcell)
  126.    i.equip()
  127.    robot.swingDown()
  128.    robot.useDown()
  129.    i.equip()
  130. end
  131.  
  132. function gotos(X,Y,Z,side)
  133.    while true do
  134.       if (y < Y) then
  135.          if (robot.detectUp() ~= true) then
  136.              up()
  137.          else
  138.             robot.digUp()
  139.          end
  140.       elseif(y > Y) then
  141.          if(robot.detectDown() ~= true) then
  142.             down()
  143.          else
  144.             robot.digDown()
  145.          end
  146.       elseif(y == Y) then
  147.          break
  148.       end
  149.    end
  150.    while true do
  151.       if (z < Z) then
  152.          turns(0)
  153.          if (robot.detect() ~= true) then
  154.             forward()
  155.          else
  156.             robot.dig()
  157.          end
  158.       end
  159.       if (z > Z) then
  160.          turns(2)
  161.          if (robot.detect() ~= true) then
  162.              forward()  
  163.          else
  164.             robot.dig()
  165.          end
  166.       end
  167.       if (z == Z) then
  168.          break
  169.       end
  170.    end
  171.    while true do
  172.       if (x < X) then
  173.          turns(3)
  174.          if (robot.detect() ~= true) then
  175.              forward()  
  176.          else
  177.             robot.dig()
  178.          end        
  179.       end
  180.       if (x > X) then
  181.          turns(1)
  182.          if (robot.detect() ~= true) then
  183.             forward()
  184.          else
  185.             robot.dig()
  186.          end
  187.       end
  188.       if (x == X) then
  189.          break
  190.       end
  191.    end
  192.   turns(side)
  193. end
  194.  
  195. function update_seeds(seedss_list)
  196.    if (robot.count(seedss_list[1]) <= 1) then
  197.       table.remove(seedss_list,1)
  198.    end
  199.    return seedss_list
  200. end
  201.  
  202. function Allcount(seed1_list)
  203.    count= 0
  204.    for i=1,#seed-list do
  205.       count= count+robot.count(seed1_list[i])
  206.    end
  207.    return count
  208. end
  209.  
  210. function createField(xx,yy)
  211.    local Y= 0
  212.    local stats= 0
  213.    s_l= seeds()
  214.    while Y<=yy do
  215.       local X= 0
  216.       Y= Y+1
  217.       while X<=xx do
  218.          X= X+1
  219.          createcell(s_l)
  220.          if (X ~= xx+1) then
  221.             forward()
  222.          end
  223.       end
  224.       if (stats == 0) then
  225.          right(s_l)
  226.          stats= 1
  227.       elseif (stats == 1) then
  228.          left(s_l)
  229.          stats= 0
  230.       end
  231.    end
  232. end
  233.  
  234. function dropAll(seed1_list)
  235.    for i=3,14 do
  236.       robot.select(i)
  237.          robot.drop(robot.count(i))
  238.    end
  239. end
  240.  
  241. function update_Field(x,y,CHEST_SIDEs)
  242.    local Y= 0
  243.    local stats= 0
  244. while true do
  245.    s_l= seeds()
  246.    while Y<= y do
  247.       local X= 0
  248.       Y= Y+1
  249.       while X<= x do
  250.          X= X+1
  251.          updatecell(s_l)
  252.          if (X ~= x+1) then
  253.             forward()
  254.          end
  255.       end
  256.       if (stats == 0) then
  257.          right()
  258.          stats= 1
  259.       elseif (stats == 1) then
  260.          left()
  261.          stats= 0
  262.       end
  263.    end
  264.    gotos(0,0,0,CHEST_SIDEs)
  265.    dropAll(s_l)
  266.    turnRight()
  267.    turnRight()
  268.    hour= 3600
  269.    now_time= 0
  270.    while true do
  271.       now_time= now_time+1
  272.       print(now_time)
  273.       os.sleep(1)
  274.       if (now_time >= hour) then
  275.          break
  276.       end
  277.    end
  278. end
  279. end
  280.  
  281. local startX= x
  282. local startY= y
  283. local startZ= z
  284. local sidess= sides
  285. local stats= 0
  286. length_x= 0
  287. length_y = 0
  288. fuel()
  289. while true do
  290.    io.write('Enter length X:')
  291.    length_x= io.read()+0
  292.    io.write('Enter length Y:')
  293.    length_y= io.read()+0
  294.    io.write('Enter chest side')
  295.    CHEST_SIDE= io.read()+0
  296.    if (length_x <= MAX and length_y <=MAX) then break end
  297. end
  298. stat= true
  299. while true do
  300.   print('Enter mode 1 - create Farm 2 - update Farm')
  301.   mode= io.read()+0
  302.    if (mode == 1) then
  303.       print(length_x,' ',length_y)  
  304.       createField(length_x,length_y)
  305.       gotos(0,0,0,sidess)
  306.       print('Finish')
  307.       break
  308.    end
  309.    if (mode == 2) then
  310.       update_Field(length_x,length_y,CHEST_SIDE)
  311.    end
  312. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement