Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- ER9 - Optical and high speed melon turtles
- Design of a melon farm:
- F: Farming tile, remain unchanged.
- C: Charging point.
- U: Unloading point
- L: Loading point
- #################
- #F F F F F F F ##
- #F F F F F F F L#
- #F F F F F F F ##
- #F F F F F F F ##
- #F F F F F F F C#
- #F F F F F F F ##
- #F F F F F F F ##
- #F F F F F F F U#
- #F F F F F F F ##
- #################
- "Fake" GPS sytem. Coordinates:
- Z+
- | /Y+
- | /
- | /
- | /
- | /
- X ________|____________ X
- - /| +
- / |
- / |
- / |
- / Z-
- Y-
- North = x+
- South = x-
- West = x-
- East = x+
- Up = y+
- Down = y-
- ]]--
- -- GLOBAL DEFINITIONS
- direction_north = 1
- direction_east = 2
- direction_south = 3
- direction_west = 4
- direction_down = 5;
- direction_up = 6;
- x_index = 1
- y_index = 2
- z_index = 3
- -- TIMER SETUP ETC
- system_harvestinterval = 600 --600 sec = 10 min
- -- MAP DEFINITIONS
- map_column_height = 9
- map_totalColumns = 7
- -- TURTLE DEFINITIONS
- turtle_position = {0, 0, 0}
- turtle_direction = 1
- -- SYSTEM FUNCTIONS - NO VISIBLE CHANGES
- --GPS methods
- function updateGPS(direction, distance)
- direction = tonumber(direction)
- distance = tonumber(distance)
- --gps setup: {x, y, z}
- if (direction == direction_down) then --Y
- turtle_position[y_index] = tonumber(turtle_position[y_index]) + distance
- elseif (direction == direction_up) then --Y
- turtle_position[y_index] = tonumber(turtle_position[y_index]) - distance
- elseif(direction == direction_north) then
- turtle_position[x_index] = tonumber(turtle_position[x_index]) + distance --X
- elseif(direction == direction_south) then --X
- turtle_position[x_index] = tonumber(turtle_position[x_index]) - distance
- elseif(direction == direction_east) then --Z
- turtle_position[z_index] = tonumber(turtle_position[z_index]) + distance
- elseif(direction == direction_west) then
- turtle_position[z_index] = tonumber(turtle_position[z_index]) - distance
- end
- end
- function setGPS(x, y, z)
- turtle_position = {x, y, z}
- end
- function resetGPS()
- turtle_position = {0, 0, 0}
- end
- -- MOVEMENT FUNCTIONS
- -- BASIC MOVEMENT FUNCTIONS
- function move(direction, distance)
- -- DETERMINE DIRECTION (for fake GPS)
- actDirection = turtle_direction
- if (direction == 'back') then
- if (turtle_direction == direction_north) then
- turtle_direction = direction_south
- elseif (turtle_direction == direction_south) then
- turtle_direction = direction_north
- elseif (turtle_direction == direction_west) then
- turtle_direction = direction_east
- elseif (turtle_direction == direction_east) then
- turtle_direction = direction_west
- end
- actDirection = turtle_direction
- elseif (direction == 'down') then
- actDirection = direction_down
- elseif (direction == 'up') then
- actDirection = direction_up
- end
- -- START MOVEMENT CODE
- if (direction == 'forward') then
- for i = 1, distance do
- while (turtle.detect()) do
- turtle.dig()
- end
- turtle.forward()
- updateGPS(actDirection, 1)
- end
- elseif (direction == 'back') then
- turtle.turnRight() --need to rotate twice
- turtle.turnRight()
- for i = 1, distance do
- if (turtle.detect()) then
- turtle.dig()
- end
- turtle.forward()
- updateGPS(actDirection, 1)
- end
- elseif (direction == 'up') then
- for i = 1, distance do
- if (turtle.detectUp()) then
- turtle.digUp()
- end
- turtle.up()
- updateGPS(actDirection, 1)
- end
- elseif (direction == 'down') then
- for i = 1, distance do
- if (turtle.detectDown()) then
- turtle.digDown()
- end
- turtle.down()
- updateGPS(actDirection, 1)
- end
- end
- print('x: ' .. turtle_position[x_index] .. ' y: ' .. turtle_position[y_index] .. ' z: ' .. turtle_position[z_index])
- end
- function rotateOnce(direction)
- --north = 1, east = 2, south = 3, west = 4
- local newDir_left = {4, 1, 2, 3} --north = west, east = north, south = east, west = south
- local newDir_right = {2, 3, 4, 1} --north = east, east = south, south = west, west = north
- if (direction == 'left') then
- turtle_direction = newDir_left[turtle_direction]
- turtle.turnLeft()
- elseif (direction == 'right') then
- turtle_direction = newDir_right[turtle_direction]
- turtle.turnRight()
- end
- end
- function rotate(direction, times)
- for i = 1, times do
- rotateOnce(direction)
- end
- end
- --ADVANCED MOVEMENT FUNCTIONS
- function findOriginalPosition()
- local originalPosition = {0, 0, 0}
- --first process x, then z, then y.
- --make turtle go 10 blocks up.
- move('up', 10)
- -- Go back to correct z
- if (turtle_position[z_index] > originalPosition[z_index]) then
- while (turtle_position[z_index] > originalPosition[z_index]) do
- --need to move WEST
- if (turtle_direction == direction_north) then
- rotateOnce('left')
- elseif (turtle_direction == direction_east) then
- rotate('right', 2)
- elseif (turtle_direction == direction_south) then
- rotateOnce('right')
- end
- move('forward', (turtle_position[z_index] - originalPosition[z_index]))
- end
- elseif (turtle_position[z_index] < originalPosition[z_index]) then
- while (turtle_position[z_index] < originalPosition[z_index]) do
- --need to move EAST
- if (turtle_direction == direction_north) then
- rotateOnce('right')
- elseif (turtle_direction == direction_west) then
- rotate('left', 2)
- elseif (turtle_direction == direction_south) then
- rotateOnce('left')
- end
- move('forward', ((turtle_position[z_index]) * -1))
- end
- end
- -- Go back to correct x
- if (turtle_position[x_index] > originalPosition[x_index]) then
- while (turtle_position[x_index] > originalPosition[x_index]) do
- --need to move SOUTH
- if (turtle_direction == direction_north) then
- rotate('left', 2)
- elseif (turtle_direction == direction_east) then
- rotateOnce('right')
- elseif (turtle_direction == direction_west) then
- rotateOnce('left')
- end
- move('forward', (turtle_position[x_index] - originalPosition[x_index]))
- end
- elseif (turtle_position[x_index] < originalPosition[x_index]) then
- while (turtle_position[x_index] < originalPosition[x_index]) do
- --need to move NORTH
- if (turtle_direction == direction_south) then
- rotate('right', 2)
- elseif (turtle_direction == direction_west) then
- rotateOnce('right')
- elseif (turtle_direction == direction_east) then
- rotateOnce('left')
- end
- move('forward', ((turtle_position[x_index]) * -1))
- end
- end
- -- Go back to correct Y
- if (turtle_position[y_index] > originalPosition[y_index]) then
- while (turtle_position[y_index] > originalPosition[y_index]) do
- --need to go DOWN
- move('down', (turtle_position[y_index] - originalPosition[y_index]))
- end
- elseif (turtle_position[y_index] < originalPosition[y_index]) then
- while (turtle_position[y_index] < originalPosition[y_index]) do
- --need to go UP
- move('down', (turtle_position[y_index] * -1))
- end
- end
- -- Rotate to correct location
- if (turtle_direction == direction_east) then
- rotateOnce('left')
- elseif (turtle_direction == direction_south) then
- rotate('right', 2)
- elseif (turtle_direction == direction_west) then
- rotateOnce('right')
- end
- end
- -- FARM METHODS
- function initFarm()
- local a = 0
- end
- -- MAIN SYSTEM
- -- phase: init
- while (turtle.detect() == true) do
- rotateOnce('right')
- end
- -- phase: move to load station
- move('forward', 1) --exit charge point
- rotateOnce('right') --rotate towards load station
- move('forward', 3) --navigate towards load station
- rotateOnce('right') --rotate to enter load station
- move('forward', 1) --enter load station
- rotate('right', 2) --rotate to exit position / prepare for load
- -- phase: load from load station
- print ('Script done')
- print('x: ' .. turtle_position[x_index] .. ' y: ' .. turtle_position[y_index] .. ' z: ' .. turtle_position[z_index])
- findOriginalPosition()
Advertisement
Add Comment
Please, Sign In to add comment