Erlendftw

melonfarm!!!

Mar 2nd, 2013
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.90 KB | None | 0 0
  1. --[[
  2.  
  3.     ER9 - Optical and high speed melon turtles
  4.  
  5.  
  6.  
  7.     Design of a melon farm:
  8.         F: Farming tile, remain unchanged.
  9.         C: Charging point.
  10.         U: Unloading point
  11.         L: Loading point
  12.        
  13.     #################
  14.     #F F F F F F F ##
  15.     #F F F F F F F L#
  16.     #F F F F F F F ##
  17.     #F F F F F F F ##
  18.     #F F F F F F F C#
  19.     #F F F F F F F ##
  20.     #F F F F F F F ##
  21.     #F F F F F F F U#
  22.     #F F F F F F F ##
  23.     #################
  24.  
  25.     "Fake" GPS sytem. Coordinates:
  26.  
  27.           Z+
  28.            
  29.           |      /Y+
  30.           |     /
  31.           |    /
  32.           |   /
  33.           |  /
  34. X ________|____________ X
  35. -        /|             +
  36.         / |
  37.        /  |
  38.       /   |
  39.      /    Z-
  40.     Y-
  41.  
  42.     North = x+
  43.     South = x-
  44.  
  45.     West = x-
  46.     East = x+
  47.  
  48.     Up = y+
  49.     Down = y-
  50. ]]--
  51.  
  52.  
  53. -- GLOBAL DEFINITIONS
  54. direction_north = 1
  55. direction_east = 2
  56. direction_south = 3
  57. direction_west = 4
  58. direction_down = 5;
  59. direction_up = 6;
  60.  
  61. x_index = 1
  62. y_index = 2
  63. z_index = 3
  64.  
  65. -- TIMER SETUP ETC
  66. system_harvestinterval = 600 --600 sec = 10 min
  67.  
  68. -- MAP DEFINITIONS
  69. map_column_height = 9
  70. map_totalColumns = 7
  71.  
  72. -- TURTLE DEFINITIONS
  73. turtle_position = {0, 0, 0}
  74. turtle_direction = 1
  75.  
  76. -- SYSTEM FUNCTIONS - NO VISIBLE CHANGES
  77. --GPS methods
  78. function updateGPS(direction, distance)
  79.     direction = tonumber(direction)
  80.     distance = tonumber(distance)
  81.  
  82.     --gps setup: {x, y, z}
  83.     if (direction == direction_down) then --Y
  84.         turtle_position[y_index] = tonumber(turtle_position[y_index]) + distance
  85.     elseif (direction == direction_up) then --Y
  86.         turtle_position[y_index] = tonumber(turtle_position[y_index]) - distance
  87.     elseif(direction == direction_north) then
  88.         turtle_position[x_index] = tonumber(turtle_position[x_index]) + distance --X
  89.     elseif(direction == direction_south) then --X
  90.         turtle_position[x_index] = tonumber(turtle_position[x_index]) - distance
  91.     elseif(direction == direction_east) then --Z
  92.         turtle_position[z_index] = tonumber(turtle_position[z_index]) + distance
  93.     elseif(direction == direction_west) then
  94.         turtle_position[z_index] = tonumber(turtle_position[z_index]) - distance
  95.     end
  96. end
  97.  
  98. function setGPS(x, y, z)
  99.     turtle_position = {x, y, z}
  100. end
  101.  
  102. function resetGPS()
  103.     turtle_position = {0, 0, 0}
  104. end
  105.  
  106.  
  107. -- MOVEMENT FUNCTIONS
  108. -- BASIC MOVEMENT FUNCTIONS
  109. function move(direction, distance)
  110.     -- DETERMINE DIRECTION (for fake GPS)
  111.     actDirection = turtle_direction
  112.  
  113.     if (direction == 'back') then
  114.         if (turtle_direction == direction_north) then
  115.             turtle_direction = direction_south
  116.         elseif (turtle_direction == direction_south) then
  117.             turtle_direction = direction_north
  118.         elseif (turtle_direction == direction_west) then
  119.             turtle_direction = direction_east
  120.         elseif (turtle_direction == direction_east) then
  121.             turtle_direction = direction_west
  122.         end
  123.  
  124.         actDirection = turtle_direction
  125.     elseif (direction == 'down') then
  126.         actDirection = direction_down
  127.     elseif (direction == 'up') then
  128.         actDirection = direction_up
  129.     end
  130.  
  131.     -- START MOVEMENT CODE
  132.     if (direction == 'forward') then
  133.        
  134.         for i = 1, distance do
  135.             while (turtle.detect()) do
  136.                 turtle.dig()
  137.             end
  138.             turtle.forward()
  139.             updateGPS(actDirection, 1)
  140.         end
  141.     elseif (direction == 'back') then
  142.         turtle.turnRight() --need to rotate twice
  143.         turtle.turnRight()
  144.  
  145.         for i = 1, distance do
  146.             if (turtle.detect()) then
  147.                 turtle.dig()
  148.             end
  149.             turtle.forward()
  150.             updateGPS(actDirection, 1)
  151.         end
  152.     elseif (direction == 'up') then
  153.         for i = 1, distance do
  154.             if (turtle.detectUp()) then
  155.                 turtle.digUp()
  156.             end
  157.             turtle.up()
  158.             updateGPS(actDirection, 1)
  159.         end
  160.     elseif (direction == 'down') then
  161.         for i = 1, distance do
  162.             if (turtle.detectDown()) then
  163.                 turtle.digDown()
  164.             end
  165.             turtle.down()
  166.             updateGPS(actDirection, 1)
  167.         end
  168.     end
  169.  
  170.     print('x: ' .. turtle_position[x_index] .. ' y: ' .. turtle_position[y_index] .. ' z: ' .. turtle_position[z_index])
  171. end
  172.  
  173. function rotateOnce(direction)
  174.     --north = 1, east = 2, south = 3, west = 4
  175.     local newDir_left = {4, 1, 2, 3} --north = west, east = north, south = east, west = south
  176.     local newDir_right = {2, 3, 4, 1} --north = east, east = south, south = west, west = north
  177.  
  178.     if (direction == 'left') then
  179.         turtle_direction = newDir_left[turtle_direction]
  180.         turtle.turnLeft()
  181.     elseif (direction == 'right') then
  182.         turtle_direction = newDir_right[turtle_direction]
  183.         turtle.turnRight()
  184.     end
  185. end
  186.  
  187. function rotate(direction, times)
  188.     for i = 1, times do
  189.         rotateOnce(direction)
  190.     end
  191. end
  192.  
  193. --ADVANCED MOVEMENT FUNCTIONS
  194. function findOriginalPosition()
  195.     local originalPosition = {0, 0, 0}
  196.  
  197.     --first process x, then z, then y.
  198.     --make turtle go 10 blocks up.
  199.     move('up', 10)
  200.  
  201.     -- Go back to correct z
  202.  
  203.     if (turtle_position[z_index] > originalPosition[z_index]) then
  204.         while (turtle_position[z_index] > originalPosition[z_index]) do
  205.  
  206.             --need to move WEST
  207.             if (turtle_direction == direction_north) then
  208.                 rotateOnce('left')
  209.             elseif (turtle_direction == direction_east) then
  210.                 rotate('right', 2)
  211.             elseif (turtle_direction == direction_south) then
  212.                 rotateOnce('right')
  213.             end
  214.  
  215.             move('forward', (turtle_position[z_index] - originalPosition[z_index]))
  216.  
  217.         end
  218.     elseif (turtle_position[z_index] < originalPosition[z_index]) then
  219.         while (turtle_position[z_index] < originalPosition[z_index]) do
  220.  
  221.             --need to move EAST
  222.             if (turtle_direction == direction_north) then
  223.                 rotateOnce('right')
  224.             elseif (turtle_direction == direction_west) then
  225.                 rotate('left', 2)
  226.             elseif (turtle_direction == direction_south) then
  227.                 rotateOnce('left')
  228.             end
  229.  
  230.             move('forward', ((turtle_position[z_index]) * -1))
  231.            
  232.         end
  233.     end
  234.  
  235.     -- Go back to correct x
  236.  
  237.     if (turtle_position[x_index] > originalPosition[x_index]) then
  238.         while (turtle_position[x_index] > originalPosition[x_index]) do
  239.  
  240.             --need to move SOUTH
  241.             if (turtle_direction == direction_north) then
  242.                 rotate('left', 2)
  243.             elseif (turtle_direction == direction_east) then
  244.                 rotateOnce('right')
  245.             elseif (turtle_direction == direction_west) then
  246.                 rotateOnce('left')
  247.             end
  248.  
  249.             move('forward', (turtle_position[x_index] - originalPosition[x_index]))
  250.  
  251.         end
  252.     elseif (turtle_position[x_index] < originalPosition[x_index]) then
  253.         while (turtle_position[x_index] < originalPosition[x_index]) do
  254.  
  255.             --need to move NORTH
  256.             if (turtle_direction == direction_south) then
  257.                 rotate('right', 2)
  258.             elseif (turtle_direction == direction_west) then
  259.                 rotateOnce('right')
  260.             elseif (turtle_direction == direction_east) then
  261.                 rotateOnce('left')
  262.             end
  263.  
  264.             move('forward', ((turtle_position[x_index]) * -1))
  265.            
  266.         end
  267.     end
  268.  
  269.     -- Go back to correct Y
  270.  
  271.     if (turtle_position[y_index] > originalPosition[y_index]) then
  272.         while (turtle_position[y_index] > originalPosition[y_index]) do
  273.             --need to go DOWN
  274.             move('down', (turtle_position[y_index] - originalPosition[y_index]))
  275.         end
  276.     elseif (turtle_position[y_index] < originalPosition[y_index]) then
  277.         while (turtle_position[y_index] < originalPosition[y_index]) do
  278.             --need to go UP
  279.             move('down', (turtle_position[y_index] * -1))
  280.         end
  281.     end
  282.  
  283.     -- Rotate to correct location
  284.     if (turtle_direction == direction_east) then
  285.         rotateOnce('left')
  286.     elseif (turtle_direction == direction_south) then
  287.         rotate('right', 2)
  288.     elseif (turtle_direction == direction_west) then
  289.         rotateOnce('right')
  290.     end
  291. end
  292.  
  293.  
  294. -- FARM METHODS
  295. function initFarm()
  296.     local a = 0
  297. end
  298.  
  299. -- MAIN SYSTEM
  300.  
  301. -- phase: init
  302. while (turtle.detect() == true) do
  303.     rotateOnce('right')
  304. end
  305.  
  306. -- phase: move to load station
  307. move('forward', 1) --exit charge point
  308. rotateOnce('right') --rotate towards load station
  309. move('forward', 3)  --navigate towards load station
  310. rotateOnce('right') --rotate to enter load station
  311. move('forward', 1) --enter load station
  312. rotate('right', 2) --rotate to exit position / prepare for load
  313.  
  314. -- phase: load from load station
  315. print ('Script done')
  316.  
  317. print('x: ' .. turtle_position[x_index] .. ' y: ' .. turtle_position[y_index] .. ' z: ' .. turtle_position[z_index])
  318.  
  319. findOriginalPosition()
Advertisement
Add Comment
Please, Sign In to add comment