Skip to content

Instantly share code, notes, and snippets.

@damien
Created October 25, 2012 06:11
Show Gist options
  • Save damien/3950797 to your computer and use it in GitHub Desktop.
Save damien/3950797 to your computer and use it in GitHub Desktop.
A small collection of ComputerCraft Lua scripts
-- fill: fills a hole with whatever materials are on hand
--
-- The turtle will fill the first hole it runs into with the
-- materials on hand. When it runs out of materials to use
-- as filler, the turtle will return to where it was launched
-- and attempt to pick up more things to fill the hole with.
--
-- The turtle will return to where it was launched from
-- as soon as there is nothing left to fill or if there
-- are no available materials to fill the hole with.
-- Filler class definition
Filler = {}
Filler.__index = Filler
function Filler:new()
local obj = {
-- The coordinate X, Y, Z offset from where the turtle started
tOffsetFromOrigin = { x = 0, y = 0, z = 0 },
-- The direction the turtle is currently facing, relative to
-- the direction it was facing when it started, example:
--
-- (forward)
-- 0
-- (left) 3 T 1 (right)
-- 2
-- (backward)
--
-- Where "T" is the turtle and each number is a direction
iBearing = 0
}
return setmetatable(obj, Filler)
end
-- Prints the current location and bearing of the turtle
function Filler:dump()
print("Location: ", self.tOffsetFromOrigin.x, self.tOffsetFromOrigin.y, self.tOffsetFromOrigin.z)
print("Bearing: ", self.iBearing)
end
-- Called whenever the turtle moves in any direction
-- along the x/y axis to update tOffsetFromOrigin
function Filler:did_move_horizontally()
if self.iBearing == 0 then
self.tOffsetFromOrigin.x = self.tOffsetFromOrigin.x + 1
elseif self.iBearing == 1 then
self.tOffsetFromOrigin.y = self.tOffsetFromOrigin.y + 1
elseif self.iBearing == 2 then
self.tOffsetFromOrigin.x = self.tOffsetFromOrigin.x - 1
else
self.tOffsetFromOrigin.y = self.tOffsetFromOrigin.y - 1
end
end
function Filler:forward()
local bSuccess = turtle.forward()
if bSuccess then
self:did_move_horizontally()
end
if self.tOffsetFromOrigin.x > 20 or self.tOffsetFromOrigin.x < -20 or self.tOffsetFromOrigin.y > 20 or self.tOffsetFromOrigin.y < -20 then
self.tOffsetFromOrigin = { x = 0, y = 0, z = 0 }
end
return bSuccess
end
function Filler:back()
local bSuccess = turtle.back()
if bSuccess then
self:did_move_horizontally()
end
return bSuccess
end
function Filler:up()
local bSuccess = turtle.up()
if bSuccess then
self.tOffsetFromOrigin.z = self.tOffsetFromOrigin.z + 1
end
return bSuccess
end
function Filler:down()
local bSuccess = turtle.down()
if bSuccess then
self.tOffsetFromOrigin.z = self.tOffsetFromOrigin.z - 1
end
return bSuccess
end
function Filler:turnRight()
local bSuccess = turtle.turnRight()
if bSuccess then
if self.iBearing == 3 then
self.iBearing = 0
else
self.iBearing = self.iBearing + 1
end
end
return bSuccess
end
function Filler:turnLeft()
local bSuccess = turtle.turnLeft()
if bSuccess then
if self.iBearing == 0 then
self.iBearing = 3
else
self.iBearing = self.iBearing - 1
end
end
return bSuccess
end
-- May be called at any point. Returns the turtle
-- to the point from where it started.
function Filler:return_to_origin()
-- return to the correct height
while self.tOffsetFromOrigin.z < 0 do
self:up()
end
-- return to the correct y axis
if self.tOffsetFromOrigin.y > 0 then
-- turn until we're facing the correct direction
while self.iBearing ~= 3 do
self:turnLeft()
end
-- ..and then go until we're lined up with our origin!
while self.tOffsetFromOrigin.y > 0 do
self:forward()
end
end
if self.tOffsetFromOrigin.y < 0 then
-- turn until we're facing the correct direction
while self.iBearing ~= 1 do
self:turnRight()
end
-- ..and then go until we're lined up with our origin!
while self.tOffsetFromOrigin.y < 0 do
self:forward()
end
end
-- return to the correct x axis
if self.tOffsetFromOrigin.x > 0 then
-- turn until we're facing the correct direction
while self.iBearing ~= 2 do
self:turnLeft()
end
-- ..and then go until we're lined up with our origin!
while self.tOffsetFromOrigin.x > 0 do
self:forward()
end
end
if self.tOffsetFromOrigin.x < 0 then
-- turn until we're facing the correct direction
while self.iBearing ~= 0 do
self:turnRight()
end
-- ..and then go until we're lined up with our origin!
while self.tOffsetFromOrigin.x < 0 do
self:forward()
end
end
-- we should (theoretically) be at our origin now!
print("Successfully returned to origin!")
end
function Filler:fill()
for i = 1, 5 do self:forward() end
self:turnRight()
for i = 1, 5 do self:forward() end
self:return_to_origin()
for i = 1, 5 do self:forward() end
self:turnLeft()
for i = 1, 5 do self:forward() end
self:return_to_origin()
self:turnRight()
for i = 1, 5 do self:forward() end
self:turnRight()
for i = 1, 5 do self:forward() end
self:return_to_origin()
self:turnRight()
for i = 1, 5 do self:forward() end
self:turnLeft()
for i = 1, 5 do self:forward() end
self:return_to_origin()
self:turnLeft()
for i = 1, 5 do self:forward() end
self:turnRight()
for i = 1, 5 do self:forward() end
self:return_to_origin()
self:turnLeft()
for i = 1, 5 do self:forward() end
self:turnLeft()
for i = 1, 5 do self:forward() end
self:return_to_origin()
self:turnLeft()
self:turnLeft()
for i = 1, 5 do self:forward() end
self:turnRight()
for i = 1, 5 do self:forward() end
self:return_to_origin()
self:turnRight()
self:turnRight()
for i = 1, 5 do self:forward() end
self:turnLeft()
for i = 1, 5 do self:forward() end
self:return_to_origin()
end
-- Main loop
local f = Filler:new()
f:fill()
f:return_to_origin()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment