Skip to content

Instantly share code, notes, and snippets.

@jvantuyl
Last active August 29, 2015 14:01
Show Gist options
  • Save jvantuyl/37f5701d52dc1e33b198 to your computer and use it in GitHub Desktop.
Save jvantuyl/37f5701d52dc1e33b198 to your computer and use it in GitHub Desktop.
ComputerCraft Wall Building Helpers
map = {}
x = 0
y = 0
h = 0
function reset()
map = {}
x = 0
y = 0
end
function mapget(mx, my)
if map[mx] == nil then return nil end
return map[mx][my]
end
function mapset(mx, my, val)
if map[mx] == nil then map[mx] = {} end
map[mx][my] = val
end
function mapupdate(mx, my)
move_to(mx, my)
local val = turtle.detectDown()
mapset(mx, my, val)
return val
end
function right()
turtle.turnRight()
h = (h + 90) % 360
end
function left()
turtle.turnLeft()
h = (h + 270) % 360
end
turnmap = {
[0]={[0]=0, [90]=1, [180]=1, [270]=-1},
[90]={[0]=-1, [90]=0, [180]=1, [270]=1},
[180]={[0]=1, [90]=-1, [180]=0, [270]=1},
[270]={[0]=1, [90]=1, [180]=-1, [270]=0}
}
function heading(new_h)
while true do
local turn = turnmap[h][new_h]
if turn < 0 then
left()
elseif turn > 0 then
right()
else
break
end
end
end
function north() heading(0) end
function south() heading(180) end
function east() heading(90) end
function west() heading(270) end
function distance(new_x, new_y, old_x, old_y)
return math.abs(new_x - old_x) + math.abs(new_y - old_y)
end
function move_to(new_x, new_y)
dist = distance(new_x, new_y, x, y)
if dist > turtle.getFuelLevel() then
error("Out of gas!")
end
while new_x < x do
west()
if not turtle.forward() then error("Obstructed!") end
x = x - 1
end
while new_x > x do
east()
if not turtle.forward() then error("Obstructed!") end
x = x + 1
end
while new_y < y do
south()
if not turtle.forward() then error("Obstructed!") end
y = y - 1
end
while new_y > y do
north()
if not turtle.forward() then error("Obstructed!") end
y = y + 1
end
end
function ensure_item()
for slot=1,16 do
if turtle.getItemCount(slot) > 0 then
turtle.select(slot)
return
end
end
error("Out of stuff!")
end
function test_trace()
mapset(4, -1, true)
mapset(1, 1, true)
mapset(1, 2, false)
mapset(2, 2, true)
mapset(3, 0, true)
end
function trace()
local q = { {x=x, y=y} }
while true do
sleep(0)
n = table.remove(q)
if n == nil then break end
nv = mapget(n.x, n.y)
if nv == nil then
nv = mapupdate(n.x, n.y)
if nv == true then
print("Flood from "..n.x..","..n.y)
table.insert(q, {x=n.x+1, y=n.y-1}) -- SE
table.insert(q, {x=n.x-1, y=n.y-1}) -- SW
table.insert(q, {x=n.x+1, y=n.y+1}) -- NE
table.insert(q, {x=n.x-1, y=n.y+1}) -- NW
table.insert(q, {x=n.x, y=n.y+1}) -- N
table.insert(q, {x=n.x, y=n.y-1}) -- S
table.insert(q, {x=n.x+1, y=n.y}) -- E
table.insert(q, {x=n.x-1, y=n.y}) -- W
end
end
end
end
function getpath()
print("Finding optimal path...")
local points = {}
for ix,row in pairs(map) do
for iy,exist in pairs(row) do
if exist then
table.insert(points, {x=ix,y=iy})
end
end
end
local path = {}
local lx = x
local ly = y
while true do
local ld = nil
local closest
for idx, point in pairs(points) do
local td = distance(lx, ly, point.x, point.y)
-- print(lx..","..ly.."-"..point.x..","..point.y.."="..td)
if ld == nil or td < ld then
closest = idx
ld = td
end
end
if closest == nil then break end
-- print("Closest to "..lx..","..ly.." is "..points[closest].x..","..points[closest].y)
path[#path+1] = points[closest]
lx, ly = points[closest].x, points[closest].y
points[closest] = nil
end
return path
end
function draw()
local path = getpath()
for idx, point in pairs(path) do
print("Placing at "..point.x..","..point.y)
ensure_item()
move_to(point.x, point.y)
turtle.placeDown()
end
end
os.loadAPI("flood")
turtle.refuel(8999)
flood.reset()
flood.trace()
for i=1,3 do
turtle.up()
flood.draw()
done
flood.move_to(0, 0)
flood.north()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment