Posted 25 October 2012 - 11:11 PM
Hi, I wrote a program in notepad++. I'm relatively new to programing and what I do know I didn't learn in lua, so don't expect perfect code.
The issue I'm having is when I dropped the file into the programs folder for CC, the file shows up in the computers rom directory but throws an error when I run it and is blank when I edit it.
The error thrown up is:
bios:206:[string "testfile"]:109: 'then' expected
The code is included here:
The issue I'm having is when I dropped the file into the programs folder for CC, the file shows up in the computers rom directory but throws an error when I run it and is blank when I edit it.
The error thrown up is:
bios:206:[string "testfile"]:109: 'then' expected
The code is included here:
--Turtle Navigation Program
--Samuel Beardmore
location = 0
direction = 0
inputs(location, direction)-- Gathers required inputs from user.
processes(location, direction, destination) --Sends inputs to be processed.
function inputs()
location = getLocation()
direction = getDirection()
destination = getDestination()
return location, direction, destination
end
function checkOneNumber(num1)
tonumber(num1)
return num1
end
function checkThreeNumbers(num1, num2, num3)
tonumber(num1)
tonumber(num2)
tonumber(num3)
return num1, num2, num3
end
function getLocation(location) --Gets location in co-ordinates.
isNumber = false
while isnumber == false do
print("Please enter current X co-ordinate: ")
x = read()
print("")
print("Please enter current Y co-ordinate: ")
y = read()
print("")
print("Please enter current Z co-ordinate: ")
z = read()
if pcall(checkThreeNumbers(x, y, z))
then
isNumber = true --Attempts to call checkNumber. If it works, it will return true and end the loop.
else
print("Unrecognised values inputted")
os.sleep(2) --These 3 lines are to allow the user to read the screen before it is cleaned up for another go.
term.clear()
term.setCursorPos(1,1)
end
end
location = {x, y, z}
return location
end
function getDirection(direction) --Gets the direction the turtle is facing.
isNumber = false
while isNumber == false do
print("Please enter the direction the turtle is facing: ")
direction = read()
print("")
if pcall(checkOneNumber(direction))
then
isNumber = true
else
print("Unrecognised values inputted")
os.sleep(2)
term.clear()
term.setCursorPos(1,1)
end
end
return direction
end
function getDestination() --Gets the destination co-ordinates.
isNumber = false
while isnumber == false do
print("Please enter destination X co-ordinate: ")
x = read()
print("")
print("Please enter destination Y co-ordinate: ")
y = read()
print("")
print("Please enter destination Z co-ordinate: ")
z = read()
if pcall(checkThreeNumbers(x, y, z))
then
isNumber = true
else
print("Unrecognised values inputted")
os.sleep(2)
term.clear()
term.setCursorPos(1,1)
end
end
destination = {x, y, z}
return destination
end
function processes(location, direction, destination) --Within this function, all the actual work happens.
while location.y < 255 do --This loop gets the turtle up to cruising altitude.
turtle.up()
end
calcPath(location, destination)--Calculates path to reach destination.
destinationReached = false
while destinationReached and (turtle.getFuelLevel() > 0) == false do --This loop makes the turtle keep going untill the destination is reached.
pathCheck()
followPath(direction, path)
end
while (location.y > destination.y) and (turtle.getFuelLevel() > 0) do
turtle.down()
end
if turtle.getFuelLevel() = 0
then print("Out of fuel")
else print("Destination reached")
end
end
function pathCheck() -- checks that the path ahead is clear, if it isn't, will check for altenatives, if trapped, will throw an error.'
if turtle.detect() == true
then turtle.turnLeft()
trackDirection(-1)
if turtle.detect() == true
then
turtle.turnRight()
turtle.turnRight()
trackDirection(2)
if turtle.detect() == true
then
print("Error: Unable to find a clear path.")
end
end
end
end
function calcPath(location, destination) -- Calculates the path to be taken by the turtle.
xPath = destination.x - location.x
if (destination.x < 0) or (location.x < 0)
then xPath = 0 - xPath
end
zPath = destination.z - location.z
if (destination.z < 0) or (location.z < 0)
then zPath = 0 - zPath
end
path = {xPath, zPath}
return path
end
function followPath(direction, path) -- Causes the turtle to move towards the correct X co-ordinate, then the correct Z co-ordinate.
if path.xPath > 0
then
while direction ~= 3 do
turtle.turnRight()
trackDirection(1)
end
while path.xPath > 0 do
turtle.forward()
path.xPath = path.xPath - 1
end
else
while direction ~= 1 do
turtle.turnRight()
trackDirection(1)
end
while path.xPath > 0 do
turtle.forward()
path.xPath = path.xPath + 1
end
end
if path.zPath > 0
then
while direction ~= 2 do
turtle.turnRight()
trackDirection(1)
end
while path.xPath < 0 do
turtle.forward()
path.xPath = path.xPath - 1
end
else
while direction ~= 0 do
turtle.turnRight()
trackDirection(1)
end
while path.xPath < 0 do
turtle.forward()
path.xPath = path.xPath + 1
end
end
end
function trackDirection(turn) -- Tracks direction, edditing the global variable as required.
direction = direction + turn
if direction > 4
then direction = direction -4
elseif direction < 0
then direction = direction + 4
end
end
Any help is much appreciated, thanks, Sam.