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 left();
- 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
- ---
- --- Returns the turtle so that it's facing the block that it started on
- ---
- local function goHome()
- if (pos.x ~= 0) then
- -- Point in the direction of home
- if (pos.x > 0) then
- setAngle(DIRECTION_RIGHT);
- else
- setAngle(DIRECTION_LEFT);
- end
- move(math.abs(pos.x) - 1);
- -- Y-Axis will have offset
- if (pos.y ~= 0) then
- move(1);
- end
- end
- if (pos.y ~= 0) then
- -- Point in the direction of home
- if (pos.y > 0) then
- setAngle(DIRECTION_BACK);
- else
- setAngle(DIRECTION_FORWARD);
- end
- -- Always have offset on Y, X will have an offset if Y is aligned
- move(math.abs(pos.y) - 1);
- end
- 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]]--
- move(10);
- for i = 1, math.random(1, 5) do
- left()
- move(math.random(1, 15));
- end
- goHome();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement