initial commit

This commit is contained in:
ducoterra
2017-09-15 14:11:04 -04:00
commit c4a2e890dc
4 changed files with 752 additions and 0 deletions

363
coord Normal file
View File

@@ -0,0 +1,363 @@
--[[
Allows a turtle to keep track of it's
location using a coordinate system.
Use the commands goForward(), goRight(),
goLeft(), goUp(), and goDown() to
control the turtle while adding to
the coordinates.
goHome() will send the turtle back to
it's origin and will point it north:
the direction it was originally facing.
A turtle always faces north when it is placed down
]]--
x = 0
y = 0
z = 0
saveX = 0
saveY = 0
saveZ = 0
saveDirection = NORTH
global map = {}
NORTH = 1
EAST = 2
SOUTH = 3
WEST = 4
UP = 5
DOWN = 6
RIGHT = 7
LEFT = 8
direction = NORTH
--[[sends a turtle forward 1 block
and adds to it's X coordinate]]--
function goForward()
if turtle.forward() then
addToCoords(direction)
return true
else return false
end
end
--[[sends a turtle up 1 block
and adds to it's Z coordinate]]--
function goUp()
if turtle.up() then
addToCoords(UP)
return true
else return false
end
end
--[[sends a turtle down 1 block
and subtracts from it's Z coordinate]]--
function goDown()
if turtle.down() then
addToCoords(DOWN)
return true
else return false
end
end
--[[turns a turtle right and changes
it's direction accordingly]]--
function goRight()
turtle.turnRight()
changeDirection(RIGHT)
end
--[[turns a turtle left and changes
it's direction accordingly]]--
function goLeft()
turtle.turnLeft()
changeDirection(LEFT)
end
--[[digs the block in front of the turtle and
sends it forward, keeping track of it's coordinates]]--
function goDig()
turtle.dig()
goForward()
end
--[[digs the block above the turtle and sends it
up, keeping track of the coordinates]]--
function goDigUp()
turtle.digUp()
goUp()
end
--[[digs the block below the turtle and sends it
down, keeping track of it's coordinates]]--
function goDigDown()
turtle.digDown()
goUp()
end
--[[sends a turtle home and refuels it from a chest
below it's origin]]--
function goRefuel()
goHome()
turtle.suckDown(64)
turtle.select(1)
turtle.refuel()
end
--[[moves a turtle forward a specified number of blocks]]--
function move(distance)
for i=1,distance do
goForward()
end
end
--[[moves a turtle up a specified number of blocks]]--
function moveUp(distance)
for i=1,distance do
goUp()
end
end
--[[moves a turtle down a specified number of blocks]]--
function moveDown(distance)
for i=1,distance do
goDown()
end
end
function autoGoForward()
distanceHome = math.abs(x)+math.abs(y)+math.abs(z)
if distanceHome < turtle.getFuelLevel()-1 then
goForward()
else saveAndFuel() end
end
function autoGoUp()
distanceHome = math.abs(x)+math.abs(y)+math.abs(z)
if distanceHome < turtle.getFuelLevel()-1 then
goUp()
else saveAndFuel() end
end
function autoGoDown()
distanceHome = math.abs(x)+math.abs(y)+math.abs(z)
if distanceHome < turtle.getFuelLevel()-1 then
goDown()
else saveAndFuel() end
end
function autoMove(distance)
for i=1,distance do
autoGoForward()
end
end
function autoMoveUp(distance)
for i=1,distance do
autoGoUp()
end
end
function autoMoveDown(distance)
for i=1,distance do
autoGoDown()
end
end
--[[takes the direction of the turtle and adds 1
to the appropriate coordinate based on that direction]]--
function addToCoords(direction)
if direction == NORTH then
x = x+1
elseif direction == EAST then
y = y+1
elseif direction == SOUTH then
x = x-1
elseif direction == WEST then
y = y-1
elseif direction == UP then
z = z+1
else
z = z-1
end
end
--[[takes a direction turned (right or left) and changes the
direction of the turtle to north, south, east, or west]]--
function changeDirection(turned)
if turned == RIGHT then
if direction == NORTH then
direction = EAST
elseif direction == EAST then
direction = SOUTH
elseif direction == SOUTH then
direction = WEST
else
direction = NORTH
end
else
if direction == NORTH then
direction = WEST
elseif direction == WEST then
direction = SOUTH
elseif direction == SOUTH then
direction = EAST
else
direction = NORTH
end
end
end
--[[takes a direction (north, east, south, or west) and turns
the turtle in that direction]]--
function setDirection(newDirect)
while true do
if newDirect == direction then
break
end
goRight()
end
end
--[[based on the current coordinates of a turtle, send the turtle
back on the x axis, then the y axis, then the z axis]]--
function goHome()
moved = false
attempts = 0
rightDirection = true
leftDirection = false
while x~=0 or y~=0 or z~=0 do
if x > 0 then
setDirection(SOUTH)
else
setDirection(NORTH)
end
while math.abs(x) > 0 and not turtle.detect() do
goForward()
moved = true
end
if y > 0 then
setDirection(WEST)
else
setDirection(EAST)
end
while math.abs(y) > 0 and not turtle.detect() do
goForward()
moved = true
end
while z > 0 and not turtle.detectDown() do
goDown()
end
while z < 0 and not turtle.detectUp() do
goUp()
moved = true
end
if moved == false then
mazeSolve()
end
moved = false
end
setDirection(NORTH)
end
function saveAndFuel()
saveCoords()
goRefuel()
returnToCoords()
end
function saveCoords()
saveX = x
saveY = y
saveZ = z
saveDirection = direction
end
function returnToCoords()
x = -saveX
y = -saveY
z = -saveZ
goHome()
setDirection(saveDirection)
x = saveX
y = saveY
z = saveZ
end
--[[write the main code down here]]--
--[[solves mazes]]--
function mazeSolve()
layer = 1
while not returned() do
try(map(layer,map))
layer = layer + 1
end
end
--[[saves a map of the world around the turtle based on spaces away from the
turtle. all the empty spaces will be marked as true and the spaces blocked
by a block will be marked as false]]--
function map(layer, map)
print("made it")
for i=1,layer do
print("made it")
saveCoords()
move(layer)
map[layer][direction] = turtle.detect()
returnToCoords()
goRight()
end
for i=1,layer do
saveCoords()
moveUp(layer)
map[layer][UP] = turtle.detectUp()
returnToCoords()
end
for i=1,layer do
saveCoords()
moveDown(layer)
map[layer][DOWN] = turtle.detectDown()
returnToCoords()
end
return map
end
--[[tries a layer to see if a turtle can go home by unsticking itself]]--
function try(map)
for i=1,4 do
if map[layer][direction] then
move(layer)
goHome()
if returned() then break end
end
end
end
function returned()
return x==0 and y==0 and z==0
end
autoMove(15)
goRight()
autoMove(30)
autoMoveUp(15)
goHome()