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

movement API

Started by Jappards, 01 June 2013 - 12:09 PM
Jappards #1
Posted 01 June 2013 - 02:09 PM
when i looked at the amount of movement API`s out there, i saw that there aren`t any proper ones, so i created a proper one.

features:
Spoiler

version()--returns the version
turn(direction,times)--direction accepts:left, right and back,you also need to specify how many times it needs to turn
move(direction,times)--direction accepts all directions except up and down.

code:
Spoiler

function version()
print("1.0.0.0")
end

function turn(direction,times)
if direction == "right" then
  for i=1,times[1] do
   turtle.turnRight()
  end
if direction == "left" then
  for i=1,times[1] then
   turtle.turnleft()
  end
end
end

function move(direction,times)
if direction == "forward" then
  for i=1,times[1] do
   turtle.forward()
  end
if direction == "back" then
  for i=1,times[1] do
   turtle.back()
  end
if direction == "right" then
  turtle.turnRight()
  for i=1,times[1] do
   turtle.forward()
  end
  turtle.turnLeft()
end
if direction == "left" then
  turtle.turnLeft()
  for i=1,times[1] do
   turtle.turnLeft()
  end
  turtle.turnRight()
end
end
if direction == "up" then
for i=1,times[1] do
  turtle.up()
end
end
if direction == "down" then
for i=1,times[1] do
  turtle.down()
end
end
end
or:
http://pastebin.com/Z2hAcYpu

pastebin get Z2hAcYpu move

Upcoming features:
-diagonal movement

note: this code isn`t tested yet, feel free to test it.

bugs
the most helpful way to report a bug is:
What I expected to happen was:

What actually happened was:

Steps to Reproduce:
Yevano #2
Posted 01 June 2013 - 03:22 PM
Just looking at the code, I can see a bunch of logic errors and syntax errors in it. Why post untested code and claim it to be the one proper movement API? In both of your functions, you are indexing the times variable like you are expecting a table. In your move function, you have unmatched 'end's. Your version function won't work. It looks like you are trying to tell the API user if they have the correct version given what they input. You never return true if the version is correct, and you compare version with a number literal which is incorrectly formatted. In your movement functions, you are trying to compare the direction with variables that don't exist. You need put quotes around them to make them string, assuming that's what you meant to do.
Jappards #3
Posted 02 June 2013 - 02:30 AM
Just looking at the code, I can see a bunch of logic errors and syntax errors in it. Why post untested code and claim it to be the one proper movement API? In both of your functions, you are indexing the times variable like you are expecting a table. In your move function, you have unmatched 'end's. Your version function won't work. It looks like you are trying to tell the API user if they have the correct version given what they input. You never return true if the version is correct, and you compare version with a number literal which is incorrectly formatted. In your movement functions, you are trying to compare the direction with variables that don't exist. You need put quotes around them to make them string, assuming that's what you meant to do.
i already fixed it and i made the direction into a string, all bugs in this code should be fixed now.
Yevano #4
Posted 02 June 2013 - 02:18 PM
You still didn't fix the if statements in your movement functions, and there's at least one missing end in your code. You have if statements inside of other if statements, which you probably meant to be next to each other. You didn't fix your times variable either. You still didn't test it, did you? :P/>
Conan1981m #5
Posted 07 June 2013 - 02:07 PM
well thats quite useless, if you want something like that, use rom/programs/turtles/go
robotica34 #6
Posted 18 September 2013 - 01:45 PM
It's pretty cluttered in my opinion, always use TAB for safety.
Now that's something I should write (btw, it's short, because I got tired of typing XD):

function turn(dir, times)
 if dir == "left" then
  for i = 1, times do
   turtle.turnLeft()
  end
 elseif dir == "right" then
  for i = 1, times do
   turtle.turnRight()
  end
end
function refuel(times)
 local i = 0
 while turtle.getFuelLevel() == 0 then
   i = i+1
   turtle.select(i)
   turtle.refuel(times)
 end
end
function go(dir, times)
 for i = 1, times do
  shell.run("go", dir)
 end
end
Dave-ee Jones #7
Posted 19 September 2013 - 03:43 AM
when i looked at the amount of movement API`s out there, i saw that there aren`t any proper ones, so i created a proper one.

-snip-

You know that your API is basically just the basic Turtle API? It doesn't really add anything else in.
robotica34 #8
Posted 19 September 2013 - 04:28 AM
when i looked at the amount of movement API`s out there, i saw that there aren`t any proper ones, so i created a proper one.

-snip-

You know that your API is basically just the basic Turtle API? It doesn't really add anything else in.
I agree with you, but he tried to do something "simpler". I made a turtle API once, but lost it (whatever), it had options like

function dig(dir, tillGravelEnds)
if tillGravelEnds == nil then tillGravelEnds = true end
if dir == nil then dir = "frwd" end
if dir == "left" then
  turtle.turnLeft()
  if tillGravelEnds then
   while turtle.detect() do
	turtle.dig()
	sleep(0.6) -- Was the best time for me
   end
  else
   turtle.dig()
  end
  turtle.turnRight()
end
end
That's a part of my "dig" function. Turtle API should contain every function that interacts with the Minecraft world.
I can see that your code contains critical function bugs (that means it won't function at all because of that bug).
What you wrote:

for i = 1, times[1] do
...
end
You try to access the times variable as a table (a collection of information in one variable). It should be:

for i = 1, times do
...
end
Anyways, I don't really like turtles, because I don't play Minecraft, unless coding :D/> If you're just like me, PM me for some more help. I'd like to help you =)
robotica34 #9
Posted 19 September 2013 - 04:37 AM
SpoilerHow to delete a post? :S