Ok, so all I can say is that either ComputerCraft is being stupid or I am being stupid. I lean to the latter than than the former.
Context: The point of the code is to have the turtle mine a quarry, go refuel when needed, and dump out all of its items when full. I haven't gotten to that last one yet, or the code for making the turtle refuel at a certain time. Right now I'm just making the method or the function as Lua likes to call it. Also moveToLocation() is someone else's code. I just borrowed it to make it a bit easier on myself for making turtles go the correct spots.
Here is the class or whatever it's called in lua.
The main methods that need to be focused on are moveToLocation() and goRefuel(). Everything else I know works just fine.
timesMoved = 0
prevDir = 0 -- 0 is left, 1 is right
rows = 0 -- rows is a variable for how many
-- times it has dug a row
homePos = vector.new(-256,77,-333)
suckZone = vector.new(-264,77,-347)
dropZone = vector.new(-259,77,-347)
function goMine()
turtle.refuel(1)
local notVacant = turtle.detect()
if notVacant then
turtle.dig()
turtle.forward()
timesMoved = timesMoved + 1
else
turtle.forward()
timesMoved = timesMoved + 1
end
end
function keepMining()
while timesMoved < 17
do goMine()
end
if rows == 16 then
turtle.digDown()
turtle.down()
turtle.turnLeft()
turtle.turnLeft()
rows = 0
timesMoved = 1
keepMining()
elseif prevDir == 0 then
turtle.turnRight()
turtle.dig()
turtle.forward()
turtle.turnRight()
prevDir = 1
timesMoved = 1
rows = rows + 1
keepMining()
else
turtle.turnLeft()
turtle.dig()
turtle.forward()
turtle.turnLeft()
prevDir = 0
timesMoved = 1
rows = rows + 1
keepMining()
end
end
-- keepMining()
function moveForward(times)
if times then
for i = 1, times do
while not turtle.forward() do
turtle.dig()
end
end
else
while not turtle.forward() do
turtle.dig()
end
end
end
function moveUp(times)
if times then
for i = 1,times do
while not turtle.up() do
turtle.digUp()
end
end
else
while not turtle.up() do
turtle.digUp()
end
end
end
function moveDown(times)
if times then
for i = 1,times do
while not turtle.down() do
turtle.digDown()
end
end
else
while not turtle.down() do
turtle.digDown()
end
end
end
--The "location" parameter needs to be a vector.
function moveToLocation(location)
startingPosition = 0
local currentPosition = 0
if gps.locate(2,false) then
startingPosition = vector.new(gps.locate(2,false))
currentPosition = startingPosition
-- Y Movement
if currentPosition.y > location.y then
-- We need to move down
while currentPosition.y ~= location.y do
moveDown()
currentPosition = vector.new(gps.locate(2,false))
end
else
--We need to move up
while currentPosition.y ~= location.y do
moveUp()
currentPosition = vector.new(gps.locate(2,false))
end
end
turtle.forward()
currentPosition = vector.new(gps.locate(2,false))
local difference = startingPosition - currentPosition
local moving = ""
-- Setting the moving variable
if difference.x == -1 then
-- East
moving = "east"
elseif difference.x == 1 then
-- West
moving = "west"
end
if difference.z == -1 then
-- South
moving = "south"
elseif difference.z == 1 then
-- North
moving = "north"
end
-- X Movement
if currentPosition.x > location.x then
-- We need to move West, so let's face that direction relative to the current facing direction
if moving == "south" then
turtle.turnRight()
elseif moving == "north" then
turtle.turnLeft()
elseif moving == "east" then
turtle.turnRight()
turtle.turnRight()
end
moving = "west"
while currentPosition.x ~= location.x do
moveForward()
currentPosition = vector.new(gps.locate(2,false))
print(moving)
end
else
-- We need to move East, so face that direction relative to the current facing direction
if moving == "south" then
turtle.turnLeft()
elseif moving == "north" then
turtle.turnRight()
elseif moving == "west" then
turtle.turnRight()
turtle.turnRight()
end
moving = "east"
while currentPosition.x ~= location.x do
moveForward()
currentPosition = vector.new(gps.locate(2,false))
print(moving)
end
end
-- Z Movement
if currentPosition.z > location.z then
-- We need to move North, so let's face that direction relative to the current facing direction
if moving == "south" then
turtle.turnRight()
turtle.turnRight()
elseif moving == "west" then
turtle.turnRight()
elseif moving == "east" then
turtle.turnLeft()
end
moving = "north"
while currentPosition.z ~= location.z do
moveForward()
currentPosition = vector.new(gps.locate(2,false))
print(moving)
end
else
-- We need to move South, so let's face that direction relative to the current facing direction
if moving == "north" then
turtle.turnRight()
turtle.turnRight()
elseif moving == "west" then
turtle.turnLeft()
elseif moving == "east" then
turtle.turnRight()
end
moving = "south"
while currentPosition.z ~= location.z do
moveForward()
currentPosition = vector.new(gps.locate(2,false))
print(moving)
end
end
else
print("Could not get turtle's location.")
return
end
end
rednet.open("left")
function goRefuel()
local quarryPosition = 0
quarryPosition = vector.new(-264,73,-336)
moveToLocation(suckZone)
turtle.suckDown()
turtle.back()
moveToLocation(quarryPosition)
end
function goHome()
moveToLocation(homePos)
end
goRefuel()
Basically, goRefuel() doesn't want to work correctly. If it is very obvious, please just tell me (I suck at Lua).
Here is a list of what I have done in the goRefuel() method:
- Changed the quarryPosition parameter in the second call of moveToLocation() to be a vector.new with actual coordinates. (worked)
- Set completely different coordinates altogether for both calls. (didn't work) But then I left the world and rejoined (single player btw) and it did work.
Do you see my predicament here? I don't understand what is going on and I don't know where to begin. This is why I am asking for someone that knows Lua and this mod better than me to tell me that I am either sane or insane. I don't know what the heck is happening.
Sorry for bad formatting. I just don't know what to share about this. So my main question is, where is the bug at, mod, game, or code?
Oh completely forgot to mention this. I am using Minecraft 1.16.5, with the latest ComputerCraft: Tweaked version.
[SOLVED]
I moved all of the code to a different class and referenced it as an API code works perfectly now for me. Still don't know where the bug lied though.