Posted 07 June 2015 - 05:42 AM
local rotatingSelf = 0
local orientation
local yDrop
sleep(5)
--get the current location using gps and retrieve the home location from file
local x,y,z = gps.locate()--get current location
local file = io.open("home","r")--get the home location and orientation
local x0 = tonumber(file.read("*line"))
local y0 = tonumber(file.read("*line"))
local z0 = tonumber(file.read("*line"))
--local O0 = tonumber(file.read("*line"))
local O0 = 4 --temporary input TEMP!!
file:close()
local function forward()
while not turtle.forward() do --while attempts to move forward fail.
if turtle.attack() then --it cannot move because of a mob
while turtle.attack() do end --hit the mob until it can move again.
end
end
end
local function up()
while not turtle.up() do --while attempts to move forward fail.
if turtle.attackUp() then --it cannot move because of a mob
while turtle.attackUp() do end --hit the mob until it can move again.
end
end
yDrop = yDrop - 1 --reduce the yDrop as it goes up. --ERROR IS HERE
end
--am i at home?
if (x==x0 and y==y0 and z==z0) then
peripheral.call("right", "transmit", 1972, 1972, "Hey I'm home!")
--begin orienting self, !!!assume!!! out of the 4 adjacent blocks on the same y level only 2 are missing.
while rotatingSelf<2 do --if the rotating turtle spots 2 blocks missing in a row assume it is now facing the correct direction.
turtle.turnRight()--turtle will turn once to the right
if not turtle.detect() then--if there is no block in front
rotatingSelf = rotatingSelf + 1 --increment the number of blocks seen by 1,
else
rotatingSelf = 0 -- else reset to 0 so that it is only 2 spaces in a row that satisfies.
end
--this should satisfy the turtle being at home and oriented correctly
end
else --if its not at home
peripheral.call("right", "transmit", 1972, 1972, "I'm looost!")
while turtle.detect() do --check if there is a block in from of the turtle. breaking random blocks would be a bad idea
turtle.turnRight() --turn to find an empty space. there should always be at least one empty space.
end
--when it is facing an empty space
forward() --move forward using the safe move, no block breaking
local x1,y1,z1 = gps.locate()--get the new location
turtle.turnLeft()
turtle.turnLeft()
forward() --return to previous position.
turtle.turnLeft()
turtle.turnLeft()
--compare the two positions
x = x-x1
z = z-z1
if x==0 then --movement is on the zaxis turtle is facing north or south
if z==1 then --the turtle is facing north
orientation = 1
else --the turtle is facing south
orientation = 3
end
else --movement is on the x axis. the turtle is facing east or west
if x==1 then --the turtle is facing west
orientation = 4
else --the turtle is facing east
orientation = 2
end
end
while orientation ~= O0 do --while the turtle is facing the wrong way
if orientation > O0 then --make the turtle face the correct way would be simpler with modulo but this is fine
turtle.turnLeft()
orientation=orientation-1
else
turtle.turnRight()
orientation=orientation+1
end
end
--return home from wherever it is, for safety no block breaking should be required
yDrop = y0-y --yDrop IS SET HERE
---[[
if O0 == 4 then --depending on orientation the x and z gps need to be oriented
local zMovement = z1 - z0
local xMovement = x0 - x1
elseif O0 == 1 then
elseif O0 == 2 then
elseif O0 == 3 then
end
--start heading home along expected mined paths
peripheral.call("right", "transmit", 1972, 1972, "I'm on my way home ma!")
while yDrop > 0 do --return to Y0
up()
end
turtle.turnRight()
while zMovement > 0 do --return to Z0
forward()
zMovement = zMovement - 1
end
turtle.turnRight()
while xMovement >0 do --return to Y0
forward()
zMovement = zMovement - 1
end
--]]
end
startup:34 attempt to perform arithmetic __sub on nil and number.This is in the up() function.
This function is ONLY being called after line 96 where yDrop is assigned through an arithmetic operation on variables. I just cannot see why yDrop is suddenly nil after being assigned but an arithmetic operation. I Even add a print(yDrop) before line 34 that prints a numerical value that is correct before throwing the same error.
Apologies for bad practice.
After testing some more, yDrop has a value before function up() is called. as soon as up() is called yDrop no longer has any value.
Any now moving the line from inside the function to after the function call has fixed that issue.
Edited on 07 June 2015 - 04:35 AM