Coordinate management library (beta)

Started by Valvate, Oct 04, 2018, 02:13 AM

Previous topic - Next topic

Valvate

Hey guys, this is my first forum post so my formatting is going to suck (sorry!)

i wrote a library / api / module for coordinate management, here is the github,
you can download it to your turtle with

wget https://raw.githubusercontent.com/itsyourboychipsahoy/Random/master/lib.lua lib.luaimport it with
local t = require('lib') or
local t = dofile('lib.lua')
It has all the normal turtle movement functions turning functions and digging functions, except it keeps track of coordinates and blocks dug.
also it can save positions and go back to them later
and move to coordinates (turtle starts at 0,0,0 facing north)
t.saveCurrentPos('home')
t.forward()
t.digUp()
t.turnRight()
t.up()
print('I have dug '..t.blocks_dug..' Blocks!')
t.gotoPos('home')
all the functions return true when they worked and return false when they failed (just like the turtle api!)
if you have any questions post a comment or ask me on discord :D

this is still a work in progress so please tell me how i can make it better :))

EDIT: if someone could tell me how to make the look function better that would be great, right now it just turns right until its at the correct orientation.
EDIT2: thanks Incin :D
EDIT3: added a bunch of test stuff that might not work

Incin

First of all, it's spelled 'coordinate' :P

I've used this in the past:

function t.look(direction)
  if direction == orientation then return end

  if (direction - orientation) % 2 == 0 then
    t.turnLeft()
    t.turnLeft()
  elseif (direction - orientation) % 4 == 1 then
    t.turnRight()
  else
    t.turnLeft()
  end
end

The way it works is the following:
  • If we are already facing the correct direction, don't do anything
  • If there is a distance of two between the orientation we want and the one we have, that means that we are facing backwards, in this case just turn twice in either direction (I picked left here)
  • If the distance between where we are and where we want to go is positive (i.e. we are to the left of it), then turn right
  • Otherwise, turn left
To understand recursion, you must first understand recursion.

Valvate

thanks Incin, i'm going to use what you said. :D
(it was hardcore triggering me when he turned right 3 times to get the correct orientation :P)