Created
September 6, 2015 00:27
-
-
Save jacobcase/ee9f45cc993fb6dd07a5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local GRID_X = 10 | |
local GRID_Y = 10 | |
local COMPARE_SLOT = 1 | |
local FUEL_SLOT = 2 | |
local SAPLING_SLOT = 3 | |
local x_pos = 0 | |
local y_pos = -1 | |
local function force_move(num) | |
print("Force moving ", num, " blocks") | |
local i = 0 | |
while i < num do | |
if turtle.detect() then | |
turtle.dig() | |
end | |
local res = turtle.forward() | |
if res then | |
i = i + 1 | |
end | |
end | |
end | |
local function move_to_column(column_num) | |
local to_move = math.abs(column_num - x_pos) | |
print("Moving ", to_move, " columns over") | |
if column_num < x_pos then | |
turtle.turnLeft() | |
force_move(to_move) | |
x_pos = x_pos - to_move | |
turtle.turnRight() | |
elseif column_num > x_pos then | |
turtle.turnRight() | |
force_move(to_move) | |
x_pos = x_pos + to_move | |
turtle.turnLeft() | |
end | |
end | |
local function restock() | |
move_to_column(0) | |
-- Drop off everything collected first | |
for i=1,16 do | |
if((i ~= COMPARE_SLOT) and (i ~= FUEL_SLOT) and (i ~= SAPLING_SLOT)) then | |
turtle.select(i) | |
turtle.dropDown() | |
end | |
end | |
-- drop off all but one of the comparison items | |
turtle.select(COMPARE_SLOT) | |
if turtle.getItemCount() > 1 then | |
turtle.dropDown(turtle.getItemCount() - 1) | |
end | |
-- refuel | |
turtle.turnRight() | |
force_move(1) | |
x_pos = x_pos + 1 | |
turtle.turnLeft() | |
turtle.select(FUEL_SLOT) | |
local slots_left = turtle.getItemSpace() | |
turtle.suckDown(slots_left) | |
if turtle.getItemCount() > 1 then | |
turtle.refuel(turtle.getItemCount() - 1) | |
end | |
-- re-sapling | |
turtle.turnRight() | |
force_move(1) | |
x_pos = x_pos + 1 | |
turtle.turnLeft() | |
turtle.select(SAPLING_SLOT) | |
slots_left = turtle.getItemSpace() | |
turtle.suckDown(slots_left) | |
turtle.turnLeft() | |
force_move(2) | |
x_pos = x_pos - 2 | |
turtle.turnRight() | |
end | |
local function place_sapling() | |
turtle.select(SAPLING_SLOT) | |
if not turtle.compareDown() then | |
turtle.digDown() | |
end | |
turtle.placeDown() | |
end | |
local function harvest_move() | |
local height_diff = 0 | |
turtle.select(COMPARE_SLOT) | |
if turtle.compare() then | |
print("Tree found, harvesting") | |
turtle.dig() | |
turtle.forward() | |
turtle.digDown() | |
while turtle.compareUp() do | |
turtle.digUp() | |
turtle.up() | |
height_diff = height_diff + 1 | |
end | |
while height_diff > 0 do | |
turtle.digDown() | |
local r = turtle.down() | |
if r == true then | |
height_diff = height_diff - 1 | |
end | |
end | |
else | |
force_move(1) | |
end | |
place_sapling() | |
end | |
local function harvest_column(column_num) | |
-- Harvests a single column of trees. Assumes it is right outside | |
-- the grid below the "X axis". Travels to the row, faces it, and | |
-- harvests the row | |
move_to_column(column_num) | |
-- Go down the column and harvest wood | |
print("Harvesting down row") | |
for y = 0, GRID_Y - 1 do | |
harvest_move() | |
y_pos = y_pos + 1 | |
end | |
-- Go back the same way, harvest incase any tree grew | |
turtle.turnRight() | |
turtle.turnRight() | |
print("Harvesting back row") | |
for y = 0, GRID_Y - 2 do | |
harvest_move() | |
y_pos = y_pos - 1 | |
end | |
force_move(1) | |
y_pos = y_pos - 1 | |
turtle.turnRight() | |
turtle.turnRight() | |
end | |
local function start_harvest_grid() | |
turtle.select(FUEL_SLOT) | |
turtle.refuel(turtle.getItemCount() - 1) | |
while true do | |
for x=0, GRID_X - 1 do | |
print("harvesting column ", x) | |
restock() | |
harvest_column(x) | |
end | |
end | |
end | |
start_harvest_grid() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment