Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --------------------------------------------------------------------------------------------------------------------
- --> GLOBALS
- --------------------------------------------------------------------------------------------------------------------
- -- Position we reside in, relative to time of execution
- local pos = vector.new(0, 0, 0);
- -- Direction we're facing
- DIRECTION_FORWARD = 0x0;
- DIRECTION_LEFT = 0x1;
- DIRECTION_BACK = 0x2;
- DIRECTION_RIGHT = 0x3;
- local dir = DIRECTION_FORWARD;
- --------------------------------------------------------------------------------------------------------------------
- --> TURTLE ABSTRACTION FUNCTIONS
- --------------------------------------------------------------------------------------------------------------------
- ---
- --- Rotates in dir, x times. x will default to one.
- --- This updates the orientation of the turtle.
- ---
- local function rotate(d, x)
- if (not x) then
- x = 1;
- end
- for i = 1, x do
- if (d == DIRECTION_LEFT) then
- turtle.turnLeft();
- -- Increase with clamp
- if (dir == 3) then
- dir = 0;
- else
- dir = dir + 1;
- end
- else
- turtle.turnRight();
- -- Decrease with clamp
- if (dir == 0) then
- dir = 3;
- else
- dir = dir - 1;
- end
- end
- end
- end
- -- Helper functions for above
- local function left(x) rotate(DIRECTION_LEFT, x); end
- local function right(x) rotate(DIRECTION_RIGHT, x); end
- ---
- --- Changes the angle of a turtle to match desired using
- --- as little rotations possible
- ---
- local function setAngle(desired)
- local min = math.min(dir, desired);
- local max = math.max(dir, desired);
- local diff = max - min;
- -- 180 spins / right - forward
- if (diff == 2) then
- return left(2);
- elseif (diff == 3) then
- return right();
- end
- -- Should always be one right/left change
- if (desired > dir) then
- left();
- elseif (desired < dir) then
- right();
- end
- end
- ---
- --- Moves x blocks fowards caving one block above the turtle
- --- and one infront of it. Updates coords.
- ---
- local function move(x)
- for i = 1, x do
- -- Forward can fail, dig until we move
- -- forwards succesfully
- repeat
- turtle.dig();
- turtle.digUp();
- until turtle.forward();
- -- Update coords
- if (dir == DIRECTION_FORWARD) then
- pos.y = pos.y + 1;
- elseif (dir == DIRECTION_LEFT) then
- pos.x = pos.x - 1;
- elseif (dir == DIRECTION_BACK) then
- pos.y = pos.y - 1;
- elseif (dir == DIRECTION_RIGHT) then
- pos.x = pos.x + 1;
- end
- end
- end
- ---
- --- Goes to a vector.
- ---
- local function goto(x, y, preserve)
- if (pos.x ~= x) then
- if (pos.x > x) then
- setAngle(DIRECTION_LEFT);
- else
- setAngle(DIRECTION_RIGHT);
- end
- move(math.abs(pos.x - x));
- end
- if (pos.y ~= y) then
- if (pos.y > y) then
- setAngle(DIRECTION_BACK);
- else
- setAngle(DIRECTION_FORWARD);
- end
- move(math.abs(pos.y - y));
- end
- end
- ---
- --- Returns the turtle so that it's facing the block that it started on
- ---
- local function goHome()
- goto(0, 0, true);
- end
- --[[
- ---
- --
- ---
- local function dump()
- -- Cache selected index
- local selected = ttl("getSelectedSlot");
- -- Dump items
- for i = 1, 16 do
- if (ttl("select", i)) then
- local count = ttl("getItemCount");
- if (count > 0) then
- ttl("drop", count - 1);
- end
- end
- end
- -- Restore selected index
- ttl("select", selected);
- end
- ---
- --- Refuel from chest infront of the turtle
- ---
- function refuel()
- local limit = ttl("getFuelLimit");
- local current = ttl("getFuelLevel");
- local need = limit - current;
- local count = math.ceil(need / 1000);
- local selected = ttl("getSelectedSlot");
- if (count > 64) then
- count = 64;
- end
- -- Attempt to aqquire fuel
- if (not ttl("suck", count)) then
- error("Could not refuel from storage");
- end
- -- Find fuel and refuel from it
- for i = 1, 16 do
- if (ttl("select", i)) then
- local count = ttl("getItemCount");
- if (ttl("refuel", count - 1)) then
- break;
- end
- end
- end
- ttl("select", selected);
- end]]--
- local arg = { ... };
- local x = tonumber(arg[ 1 ]);
- local y = tonumber(arg[ 2 ]);
- goto(x, y);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement