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

[Lua] Problem with orientation and position...

Started by lllScorpionlll, 12 March 2013 - 07:30 AM
lllScorpionlll #1
Posted 12 March 2013 - 08:30 AM
My head is burning with this code :D/>

Where is the error ? When I turn forward the turtle.. then it doing strange thinks :)/>

Thanks for the help !


rednet.open ("right")
PCID = 21

local positionx = 0
local positiony = 0
local positionz = 0

while true do
id, command = rednet.receive()
  if command == "go" then
    turtle.forward()
 
  elseif command == "back" then
    turtle.back()
 
  elseif command == "left" then
    turtle.turnLeft()
 
  elseif command == "right" then
    turtle.turnRight()
 
  elseif command == "dig" then
    turtle.dig()
 
  elseif command == "up" then
    turtle.up()
 
  elseif command == "down" then
    turtle.down()
   
  elseif command == "place" then
    turtle.place()
 
  elseif command == "digup" then
    turtle.digUp()
 
  elseif command == "digdown" then
    turtle.digDown()
   
  elseif command == "placeup" then
    turtle.placeUp()
   
  elseif command == "placedown" then
    turtle.placeDown()
  end
 
  function turtleforward()
if turtle.forward() then
  positiony = positiony + 1
elseif turtle.back() then
  positiony = positiony - 1
elseif turtle.up() then
  positionz = positionz + 1
elseif turtle.down() then
  positionz = positionz - 1
end
end
function turtleright()
if turtle.forward() then
  positionx = positionx + 1
elseif turtle.back() then
  positionx = positionx - 1
elseif turtle.up() then
  positionz = positionz + 1
elseif turtle.down() then
  positionz = positionz - 1
end
end
function turtleleft()
if turtle.forward() then
  positionx = positionx - 1
elseif turtle.back() then
  positionx = positionx + 1
elseif turtle.up() then
  positionz = positionz + 1
elseif turtle.down() then
  positionz = positionz - 1
end
end
function turtleback()
if turtle.forward() then
  positiony = positiony - 1
elseif turtle.back() then
  positiony = positiony + 1
elseif turtle.up() then
  positionz = positionz + 1
elseif turtle.down() then
  positionz = positionz - 1
end
end
local turn = turtleforward()
function orientacia()
if turn == turtlerovno() then
  if turtle.turnLeft() then
  turn = turtleleft()
  elseif turtle.turnRight() then
  turn = turtleright()
  end
end
if turn == turtleleft() then
  if turtle.turnLeft() then
  turn = turtleback()
  elseif turtle.turnRight() then
  turn = turtlerovno()
  end
end
if turn == turtleright() then
  if turtle.turnLeft() then
  turn = turtlerovno()
  elseif turtle.turnRight() then
  turn = turtleback()
  end
end
if turn == turtleback() then
  if turtle.turnLeft() then
  turn = turtleright()
  elseif turtle.turnRight() then
  turn = turtleleft()
  end
end
end
print (positionx..""..positiony..""..positionz)
orientacia()
end 
ChunLing #2
Posted 12 March 2013 - 10:36 AM
Well, your functions are all defined inside the command loop (so they get redefined every time the loop runs), but it looks like the only one that ever gets called is turtleforward. So you get a rednet command, execute it if it's in your list, and then you do a turtleforward(). What happens then depends on which direction the turtle can move.

What exactly are you trying to do?
shiphorns #3
Posted 12 March 2013 - 10:43 AM
Also, turtlerovno() seems entirely undefined. Should those calls be turtleforward()?
ChunLing #4
Posted 12 March 2013 - 11:05 AM
Oh, my bad. You're comparing turn to the function returns, so all those functions get called. And all of them return nil, which is what turn is, so all the stuff in them happens every time. That must be quite a dance your turtle performs after every command you give it.

No, wait, you change turn in the first thing. So only that sequence happens. Still a lot of nonsense happening there.
Edited on 12 March 2013 - 10:07 AM
LordIkol #5
Posted 13 March 2013 - 01:41 AM
i tried to get into your code but i dont get why you do the things :D/>

here is a code that may gives you a clue what you should do. i will not guarantee that this code will work, but you should get a clue how it works.
i will check the code when im back home. and give update if not working :)/>

http://pastebin.com/0bRcESc9

edit: note that im using Y as coordinate for the altitude level

Greets
Loki
Edited on 13 March 2013 - 12:56 AM
lllScorpionlll #6
Posted 14 March 2013 - 10:31 AM
Clueless: I dont now but your code doesnt work for me :/

I do this: It is ok ? :)/>


rednet.open ("right")
PCID = 21
local positionx = 0
local positiony = 0
local positionz = 0
local function forwardy()
positiony = positiony + 1
end
local function forwardx()
positionx = positionx + 1
end
local function backy()
positiony = positiony - 1
end
local function backx()
positionx = positionx - 1
end
local function up()
positionz = positionz + 1
end
local function down()
positionz = positionz - 1
end
function celokforward()
while true do
id, command = rednet.receive()
   if turtle.forward() then
	 forwardy()
   elseif turtle.back() then
  backy()
   elseif turtle.left() then
  celokleft()
   elseif turtle.right() then
  celokright()
   elseif turtle.up then
  up()
   elseif turtle.down() then
  down()
   end
   print (positiony.." - "..positionx.." - "..positionz)
end
end
function celokleft()
while true do
id, command = rednet.receive()
   if turtle.forward() then
	 forwardx()
   elseif turtle.back() then
  backx()
   elseif turtle.left() then
  celokback()
   elseif turtle.right() then
  celokforward()
   elseif turtle.up() then
  up()
   elseif turtle.down() then
  down()
   end
   print (positiony.." - "..positionx.." - "..positionz)
end
end 
function celokright()
while true do
id, command = rednet.receive()
   if turtle.forward() then
	 forwardx()
   elseif turtle.back() then
  backx()
   elseif turtle.left() then
  celokforward()
   elseif turtle.right() then
  celokback()
   elseif turtle.up() then
  up()
   elseif turtle.down() then
  down()
   end
print (positiony.." - "..positionx.." - "..positionz)
end
end
function celokback()
while true do
id, command = rednet.receive()
   if turtle.forward() then
	 forwardy()
   elseif turtle.back() then
  backy()
   elseif turtle.left() then
  celokright()
   elseif turtle.right() then
  celokleft()
   elseif turtle.up() then
  up()
   elseif turtle.down() then
  down()
   end
print (positiony.." - "..positionx.." - "..positionz)
end
end
celokforward()
Lyqyd #7
Posted 14 March 2013 - 02:58 PM
Take a look at my location API. It may give you a better sense of how this sort of thing should work.
LordIkol #8
Posted 14 March 2013 - 11:57 PM
i checked my code and its working
if you want the turtle to count up 1 from beginning when you tell him to go forward
just set Orientation to 3 in the script

Greets
Loki
ChunLing #9
Posted 15 March 2013 - 01:05 PM
To the OP, you still haven't explained what you want the turtle to do. If the program runs, then it runs (and hey, same thing if it doesn't run, really). That is to say, it does something, but without knowing what you want it to do we can't well find anything "wrong" with the program aside from things we simply assume you don't want it to do.

If it's now doing what you want, then congrats. If not, then please explain exactly what you want it to do and exactly what it does instead.