Posted 23 October 2012 - 10:44 PM
Unless you couldn't tell, this is a navigation program for a turtle. Although I ran into a problem! When the program is ran, it instantly terminates and prints out a random number generally below 100. So I came here looking for help, make any suggestion please!
(code is incomplete, Y vector isn't coded or collisions)
(code is incomplete, Y vector isn't coded or collisions)
Dir = 0 --1 North, 2 East, 3 South, 4 West
--Ini Vector of turtle
sX = -884
sY = 4
sZ = -1554
--Current Vector of turtle
cX = -884
cY = 4
cZ = -1554
--Vector Adjustements
hX = 0
hY = 0
hZ = 0
--Vector Wanted
wX = -885
wY = 4
wZ = -1557
iniVector()
function Vectoradd(VaX,VaY,VaZ,VbX,VbY,VbZ,RvX,RvY,RvZ)
local VrX = VaX + VbX
local VrY = VaY + VbY
local VrZ = VaZ + VbZ
RvX = VrX
RvY = VrY
RvZ = VrZ
end
function Vectorsous(VaX,VaY,VaZ,VbX,VbY,VbZ,RvX,RvY,RvZ)
local VrX = VaX - VbX
local VrY = VaY - VbY
local VrZ = VaZ - VbZ
RvX = VrX
RvY = VrY
RvZ = VrZ
end
function DirAnalyze(wDir,cDir)
if (wDir == 1) and (cDir == 2) then
left(1)
elseif (wDir == 1) and (cDir == 3) then
left(2)
elseif (wDir == 1) and (cDir == 4) then
right(1)
elseif (wDir == 2) and (cDir == 1) then
right(1)
elseif (wDir == 2) and (cDir == 3) then
left(1)
elseif (wDir == 2) and (cDir == 4) then
left(2)
elseif (wDir == 3) and (cDir == 1) then
right(2)
elseif (wDir == 3) and (cDir == 2) then
right(1)
elseif (wDir == 3) and (cDir == 4) then
left(1)
elseif (wDir == 4) and (cDir == 1) then
left(1)
elseif (wDir == 4) and (cDir == 2) then
right(2)
elseif (wDir == 4) and (cDir == 3) then
right(1)
end
wDir = Dir
end
function left(times)
local done = 0
repeat
turtle.turnLeft()
done = done +1
until done == times
end
function right(times)
local done = 0
repeat
turtle.turnRight()
done = done +1
until done == times
end
function forward()
turtle.forward()
VectorAdjust("forward",Dir,cX,cY,cZ)
end
function VectorAdjust(move,cDir,VaX,VaY,VaZ)
if (move == "forward") then
if (cDir == 1) then
local VaZ = VaZ -1
elseif (cDir == 2) then
local VaX = VaX +1
elseif (cDir == 3) then
local VaZ = VaZ +1
elseif (cDir == 4) then
local VaX = VaX -1
end
elseif (move == "backward") then
if (cDir == 1) then
local VaZ = VaZ +1
elseif (cDir == 2) then
local VaX = VaX -1
elseif (cDir == 3) then
local VaZ = VaZ -1
elseif (cDir == 4) then
local VaX = VaX +1
end
end
end
function iniVector()
Vectorsous(sX,sY,sZ,wX,wY,wZ,hX,hY,hZ)
handler()
end
function handler()
if (Dir == 1) and (hZ > 0) then
forward()
elseif (Dir == 1) and (hZ < 0) then
DirAnalyze(3,1)
elseif (Dir == 2) and (hX > 0) then
forward()
elseif (Dir == 2) and (hX < 0) then
DirAnalyze(4,2)
elseif (Dir == 3) and (hZ < 0) then
forward()
elseif (Dir == 3) and (hZ > 0) then
DirAnalyze(1,3)
elseif (Dir == 4) and (hX < 0) then
forward()
elseif (Dir == 4) and (hX > 0) then
DirAnalyze(2,4)
end
end