This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
macss_'s profile picture

Could somene please find the error and help me?

Started by macss_, 19 May 2014 - 01:19 AM
macss_ #1
Posted 19 May 2014 - 03:19 AM
Well, I was trying to amke turtles smartly go to one specified location but it seems not to be working right, the turtle simply goes up, can someone help me?

I know there is some coding issues in the x,z part but this thing just goes up!
Spoiler

tArgs = { ... }
local function numbering(_table)
for i = 1, #_table do
  _table[i] = tonumber(_table[i])
end
end
local function checkArg()
if #tArgs > 3 then
  print('Too much arguments! Try using "goto <number> <number> <number>"')
  return false
elseif #tArgs == nil then
  print('You MUST have 3 argument" Try using "goto <number> <number> <number>"')
  return false
elseif type(tArgs[1]) ~= "number" or type(tArgs[2]) ~= "number" or type(tArgs[3]) ~= "number" then
  print('You enterend an invalid argument type! Use only numbers!' )
  return false
else
  return true
end
end
local function turn(n)
for i= 1, n do
  turtle.turnRight()
end
end
local function walkDetect()
while turtle.detect() do
  turtle.dig()
end
turtle.forward()
end
local function goTo(x,y,z)
local cx, cy, cz = gps.locate(2)  --get current pos
local dx, dy, dz = math.abs(cx) - math.abs(x), math.abs(cy) - math.abs(y), math.abs(cz) - math.abs(z) -- the actual distance to "walk"
turtle.forward()
local chx, chy, chz = gps.locate(2) --check which way to turn
local dchx, dchy, dchz = math.abs(chx) - math.abs(x), math.abs(chy) - math.abs(y), math.abs(chz) - math.abs(z)
if dchx > dx then --walk in the xCoord
  turn(2)
  local wx =  dx + 1
  for i = 1,wx do
   walkDetect()
  end
elseif dchx < dx then
  local wx = dx - 1
  for i = 1,wx do
   walkDetect()
  end
end
if dchz > dz then --walk in the zCoord
  turn(2)
  local wz = dz + 1
  for i = 1,wz do
   walkDetect()
  end
elseif dchx < dx then
  local wz = dz - 1
  for i = 1,wz do
   walkDetect()
  end
end
if y > cy then
  local wy = y - cy
  for i = 1,wy do
   while turtle.detectDown() do
	turtle.digDown()
   end
   turtle.down()
  end
elseif y < cy then
  local wy = cy-y
  for i = 1, wy do
   while turtle.detectUp() do
	turtle.digUp()
   end
   turtle.up()
  end
end

end
numbering(tArgs)
if checkArg() then
goTo(tArgs[1],tArgs[2],tArgs[3])
end
Edited on 19 May 2014 - 11:55 AM
CCGrimHaxor #2
Posted 19 May 2014 - 02:32 PM
I will start working on a smaller and a working code.

Do you have the gps pcs set up correctly check if they are all working
macss_ #3
Posted 19 May 2014 - 02:55 PM
Yes, the gps system is online and working just fine! Also thanks
macss_ #4
Posted 20 May 2014 - 01:16 AM
No tips at all?!
CCGrimHaxor #5
Posted 20 May 2014 - 11:51 AM
Well you have a complicated code that is hard to navigate in that's why it is hard for me to find the error.
I made one of these programs when I was a noob. I can give it if you want but it is really long and complicated.
macss_ #6
Posted 20 May 2014 - 04:32 PM
If you want you can make a new one for me, what I want is tell the turtle, goto 432 25 90 and it goes, that is what I was trying to do..
BlockSmith #7
Posted 21 May 2014 - 09:08 AM
My best guess from looking at what I have here would be that the y part of your goto function has reversed comparison operators.

Try this:

if y < cy then -- Reversed this operator
	local wy = y - cy
	for i = 1,wy do
	  while turtle.detectDown() do
		turtle.digDown()
	  end
	turtle.down()
	end
  elseif y > cy then -- And Reversed this operator
	local wy = cy-y
	for i = 1, wy do
	  while turtle.detectUp() do
		turtle.digUp()
	  end
	turtle.up()
	end
  end

Below I have reformatted your code, could you please rename the variables to something more descriptive. Examples would be xCurrent, xTarget, xPos, etc, etc. Without comments, formatting, and descriptive variable names it's almost impossible to figure heads or tails of someone's code.


tArgs = { ... }
local function numbering(_table)
  for i = 1, #_table do
	_table[i] = tonumber(_table[i])
  end
end

local function checkArg()
  if #tArgs > 3 then
	print('Too much arguments! Try using "goto <number> <number> <number>"')
	return false
  elseif #tArgs == nil then
	print('You MUST have 3 argument" Try using "goto <number> <number> <number>"')
	return false
  elseif type(tArgs[1]) ~= "number" or type(tArgs[2]) ~= "number" or type(tArgs[3]) ~= "number" then
	print('You enterend an invalid argument type! Use only numbers!' )
	return false
  else
	return true
  end
end

local function turn(n)
  for i= 1, n do
	turtle.turnRight()
  end
end

local function walkDetect()
  while turtle.detect() do
	turtle.dig()
  end
  turtle.forward()
end

local function goTo(x,y,z)
  local cx, cy, cz = gps.locate(2)  --get current pos
  local dx, dy, dz = math.abs(cx) - math.abs(x), math.abs(cy) - math.abs(y), math.abs(cz) - math.abs(z) -- the actual distance to "walk"
  turtle.forward()
  local chx, chy, chz = gps.locate(2) --check which way to turn
  local dchx, dchy, dchz = math.abs(chx) - math.abs(x), math.abs(chy) - math.abs(y), math.abs(chz) - math.abs(z)
  if dchx > dx then --walk in the xCoord
	turn(2)
	local wx =  dx + 1
  
	for i = 1,wx do
	 walkDetect()
	end

  elseif dchx < dx then
	local wx = dx - 1
  
	for i = 1,wx do
	  walkDetect()
	end
  end
  if dchz > dz then --walk in the zCoord
	turn(2)
	local wz = dz + 1

	for i = 1,wz do
	  walkDetect()
	end
  elseif dchx < dx then
	local wz = dz - 1

	for i = 1,wz do
	  walkDetect()
	end
  end
  if y > cy then
	local wy = y - cy

	for i = 1,wy do
	  while turtle.detectDown() do
		turtle.digDown()
	  end
	turtle.down()
	end
  elseif y < cy then
	local wy = cy-y

	for i = 1, wy do
	  while turtle.detectUp() do
		turtle.digUp()
	  end
	turtle.up()
	end
  end
end
--[[Program Entry]]--
numbering(tArgs)
if checkArg() then
  goTo(tArgs[1],tArgs[2],tArgs[3])
end
Edited on 21 May 2014 - 07:12 AM
theoriginalbit #8
Posted 21 May 2014 - 09:17 AM
from what I can tell BlockSmith is correct, you've flipped the comparison for y.

Without comments, formatting, and descriptive variable names it's almost impossible to figure heads or tails of someone's code.
false. I can interpret that fine. x y z is the target … cx cy cz is current … dx dy dz is difference between target and current … chx chy chz is the change in current … dchx dchy dchz is the new distance from the target
BlockSmith #9
Posted 21 May 2014 - 09:19 AM
The only way I could figure anything out was by the x z and y in any of the variables then compare it to what I'd do. Oh well. Hope we've helped!
macss_ #10
Posted 21 May 2014 - 02:54 PM
Thank you guys, also thanks for the tips, I'll try to implement the modficications and also build a new code, but anyways thank you!
BlockSmith #11
Posted 21 May 2014 - 09:28 PM
Very welcome!