Posted 17 August 2013 - 05:19 PM
Hello, I got a problem with my code that I am not able to fix. CC tells me that I am trying to call a nil value on line 133(calling the move funciton) in my program, but CC is able to print the value/address of the function
http://pastebin.com/9MvJh9cd
CODE:
http://pastebin.com/9MvJh9cd
CODE:
Spoiler
local args = { ... }
local interval = 5 --60 * 20
--local bogEarthChance = 1/(20*20*20 * 1.5 * 2) --24000
local timer = 0
local x,y,z = 0,0,0
local rot = 0
local width,length = tonumber(args[1]),tonumber(args[2])
os.loadAPI("dataBaseAPI2")
local database = dataBaseAPI2
local data = nil
local rotD = {left = turtle.turnLeft, right = turtle.turnRight}
local moveD = {front = turtle.forward, back = turtle.back, up = turtle.up, down = turtle.down}
local eMoveD = {left = function() turn("left"); return baseMove("front") end, right = function() turn("right"); return baseMove("front") end}
local digD = {front = turtle.dig, up = turtle.digUp, down = turtle.digDown}
local dataBaseFileName = "resumeInfo.dat"
local harvesting, moving = false,false;
local targetX,targetY,movedX, movedY = 0,0,0,0;
local function init()
if fs.exists(dataBaseFileName) then
data = dataBaseAPI2.loadDataBase(dataBaseFileName)
if data then
local pos = data:get("index",1)
if pos then
x,y,z = pos.x, pos.y, pos.z
rot = pos.rot
timer = pos.t
harvesting = pos.h
width = pos.width
length = pos.length
if x and y and z and rot and timer and width then
return true
end
end
end
end
return false
end
local function pastInit()
if not(init()) then
data = dataBaseAPI2.new("turtleInfo")
data:add({index = 1, x=0,y=0,z=0,rot=0,h=false,width=0,length=0,movedX=0,movedY=0,moving=false,targetX=0,targetY=0})
width = tonumber(args[1])
length = tonumber(args[2])
end
end
local function turn(dir)
if rotD[dir] then
local rotated = rotD[dir]()
if dir == "left" then
rot = rot - 1
if rot < 0 then
rot = 3
end
elseif dir == "right" then
rot = rot + 1;
end
rot = (rot % 4)
else
error("Invalid direction!:" .. tostring(dir))
end
end
local function move(dir)
if (eMoveD[dir]) then
return eMoveD[dir]();
else
return baseMove(dir)
end
end
local function baseMove(dir)
if not(moveD[dir]) then
error("Invalid direction!:" .. tostring(dir))
else
local moved = moveD[dir]();
if moved then
local aMove = -1
if dir == "up" then
z = z + 1;
elseif dir == "down" then
z = z - 1;
elseif dir == "back" then
aMove = (rot + 2) % 4
elseif dir == "front" then
aMove = rot
end
if aMove > 0 then
if aMove == 0 then x = x + 1 end
if aMove == 1 then y = y + 1 end
if aMove == 2 then x = x - 1 end
if aMove == 3 then y = y - 1 end
end
end
return moveD[dir]();
end
end
local function dig(side)
if not(digD[side]) then
error("Invalid side!:" .. tostring(side))
else
return digD[dir]();
end
end
local function moveTo(xx,yy)
movedX = 0
movedY = 0
moving = true
targetX = xx
targetY = yy
end
local function nextMove()
if moving then
if not (x == targetX) then
if targetX > x then
while not(rot == 0) do
turn("right")
end
elseif targetX < x then
while not(rot == 2) do
turn("left")
end
end
movedX = movedX + 1
print(move) --Prints ("function: " .. address)
move("front") --Problem: attemt to call nil ################################################ <---------------------------------- PROBLEM
elseif not(y==targetY) then
if targetY > y then
while not(rot == 3) do
turn("left")
end
elseif targetY < y then
while not(rot == 1) do
turn("right")
end
end
movedY = movedY + 1
move("front")
else
targetX = x
targetY = y
moving = false
end
end
end
local function harvest()
local nextX, nextY = 0,0;
local xWr = math.floor(x/width) % 2
if y == length + 1 then
nextX = 0
nextY = 0
elseif x == width then
nextY = y + 1
nextX = x
else
nextY = y
if (xWr % 2) == 0 then
nextX = x + 1
else
nextX = x - 1
end
end
if (nextX == 0) and (nextY == 0) then --TODO: change that condition, in case of empy fuel or full inventory
harvesting = false
else
--TODO: add harvesting and planting stuff here!!!
end
moveTo(nextX,nextY)
end
local function save()
local dat = data:get("index",1);
dat.x, dat.y, dat.z, dat.rot, dat.t,dat.h,dat.width,dat.length,dat.movedY,dat.movedX,dat.moving,dat.targetX,dat.targetY = x,y,z,rot,timer,harvesting,width,length,movedX,movedY,moving,targetX,targetY;
data:save(dataBaseFileName);
end
local function update()
timer = timer + 1
if timer >= interval and not(harvesting) then
harvesting = true
timer = 0
end
if (harvesting) and not(moving) then
harvest();
end
nextMove();
save();
sleep(1)
end
print(move)
pastInit()
while true do
update()
end