Skip to content

Instantly share code, notes, and snippets.

@b1naryth1ef
Created April 28, 2013 17:20
Show Gist options
  • Save b1naryth1ef/5477582 to your computer and use it in GitHub Desktop.
Save b1naryth1ef/5477582 to your computer and use it in GitHub Desktop.
Computer Craft Square API
require 'middleclass'
Square = class("Square")
Square._mt = {}
function Square:initilaize(size, down)
-- Vars
self.size = size
self.down = down
-- Callbacks
self.onMove = nil
self.onRotate = nil
self.onDown = nil
self.key = {turtle.turnLeft, turtle.turnRight}
self.c = 1
end
function Square:swapDirection()
if self.c == 1 then
self.c = 2
else
self.c = 1
end
end
function Square:dig() -- Dig a line of length self.size
local am = self.size -- I dont know enough about lua to know if this is required
for v=1, am-1, 1 do
if not turtle.dig() then
return false
end
if not turtle.forward() then
return false
end
end
end
function Square:rotate(i)
self.key[self.c]()
if i == self.size then
print("Square Completed!")
return false
end
if not turtle.dig() then
print("Error rotating!")
return false
end
if not turtle.forward() then
print("Error rotating (dig)!")
return false
end
self.key[self.c]()
self:swapDirection()
self.key[self.c]()
end
function Square:run()
for i=1, self.size, 1 do
if not self:dig() then
print("Square exited due to failure of digging/moving!")
return false
end
if not self.rotate(i) then
return true
end
if self.down then
if not turtle.digDown() then
print("Failed to move down!")
return false
end
if not turtle.down() then
print("Failed to move down!")
return false
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment