View difference between Paste ID: fWWr2C5t and jTAkYCCF
SHOW: | | - or go back to the newest paste.
1
2
--------------------------------------------------------------------------------------------------------------------
3
--> GLOBALS
4
--------------------------------------------------------------------------------------------------------------------
5
6
-- Position we reside in, relative to time of execution
7
local pos = vector.new(0, 0, 0);
8
9
-- Direction we're facing
10
DIRECTION_FORWARD = 0x0;
11
DIRECTION_LEFT    = 0x1;
12
DIRECTION_BACK    = 0x2;
13
DIRECTION_RIGHT   = 0x3;
14
local dir = DIRECTION_FORWARD;
15
16
--------------------------------------------------------------------------------------------------------------------
17
--> TURTLE ABSTRACTION FUNCTIONS
18
--------------------------------------------------------------------------------------------------------------------
19
20
---
21
--- Rotates in dir, x times.  x will default to one.
22
--- This updates the orientation of the turtle.
23
--- 
24
local function rotate(d, x)
25
  if (not x) then
26
    x = 1;
27
  end
28
29
  for i = 1, x do
30
    if (d == DIRECTION_LEFT) then
31
      turtle.turnLeft();
32
33
      -- Increase with clamp
34
      if (dir == 3) then
35
        dir = 0;
36
      else
37
        dir = dir + 1;
38
      end
39
    else
40
      turtle.turnRight();
41
      
42
      -- Decrease with clamp
43
      if (dir == 0) then
44
        dir = 3;
45
      else
46
        dir = dir - 1;
47
      end
48
    end
49
  end
50
end
51
52
-- Helper functions for above
53
local function left(x) rotate(DIRECTION_LEFT, x); end
54
local function right(x) rotate(DIRECTION_RIGHT, x); end
55
56
---
57
--- Changes the angle of a turtle to match desired using
58
--- as little rotations possible
59
---
60
local function setAngle(desired)
61
  local min = math.min(dir, desired);
62
  local max = math.max(dir, desired);
63
  local diff = max - min;
64
65
  -- 180 spins / right - forward
66
  if (diff == 2) then
67
    return left(2);
68
  elseif (diff == 3) then
69
    return left();
70
  end
71
72
  -- Should always be one right/left change
73
  if (desired > dir) then
74
    left();
75
  elseif (desired < dir) then
76
    right();
77
  end
78
end
79
80
--- 
81
--- Moves x blocks fowards caving one block above the turtle
82
--- and one infront of it.  Updates coords.
83
---
84
local function move(x)
85
  for i = 1, x do
86
87
    -- Forward can fail, dig until we move
88
    -- forwards succesfully
89
    repeat
90
      turtle.dig();
91
      turtle.digUp();
92
    until turtle.forward();
93
94
    -- Update coords
95
    if (dir == DIRECTION_FORWARD) then
96
      pos.y = pos.y + 1;
97
    elseif (dir == DIRECTION_LEFT) then
98
      pos.x = pos.x + 1;
99
    elseif (dir == DIRECTION_BACK) then
100
      pos.y = pos.y - 1;
101
    elseif (dir == DIRECTION_RIGHT) then
102
      pos.x = pos.x - 1;
103
    end
104
105
  end
106
end
107
108
---
109
--- Returns the turtle so that it's facing the block that it started on
110
---
111
local function goHome()
112
  if (pos.x ~= 0) then
113
114
    -- Point in the direction of home
115
    if (pos.x > 0) then
116
      setAngle(DIRECTION_RIGHT);
117
    else
118
      setAngle(DIRECTION_LEFT);
119
    end
120
121
    move(math.abs(pos.x) - 1);
122
123
    -- Y-Axis will have offset
124
    if (pos.y ~= 0) then
125
      move(1);
126
    end
127
  end
128
129
  if (pos.y ~= 0) then
130
131
    -- Point in the direction of home
132
    if (pos.y > 0) then
133
      setAngle(DIRECTION_BACK);
134
    else
135
      setAngle(DIRECTION_FORWARD);
136
    end
137
138
    -- Always have offset on Y, X will have an offset if Y is aligned
139
    move(math.abs(pos.y) - 1);
140
  end
141
end
142
143
--[[
144
---
145
-- 
146
---
147
local function dump()
148
 
149
  -- Cache selected index
150
  local selected = ttl("getSelectedSlot");
151
  
152
  -- Dump items
153
  for i = 1, 16 do  
154
    if (ttl("select", i)) then
155
      local count = ttl("getItemCount");    
156
     
157
      if (count > 0) then
158
        ttl("drop", count - 1);    
159
      end         
160
    end
161
  end
162
  
163
  -- Restore selected index
164
  ttl("select", selected);
165
end
166
167
---
168
--- Refuel from chest infront of the turtle
169
---
170
function refuel()
171
  
172
  local limit = ttl("getFuelLimit");
173
  local current = ttl("getFuelLevel");
174
  local need = limit - current;
175
  local count = math.ceil(need / 1000);
176
  local selected = ttl("getSelectedSlot");
177
  
178
  if (count > 64) then
179
   count = 64;
180
  end 
181
182
  -- Attempt to aqquire fuel 
183
  if (not ttl("suck", count)) then
184
    error("Could not refuel from storage");
185
  end
186
187
  -- Find fuel and refuel from it  
188
  for i = 1, 16 do
189
    if (ttl("select", i)) then
190
      local count = ttl("getItemCount");
191
      if (ttl("refuel", count - 1)) then
192
        break;
193
      end
194
    end  
195
  end
196
  
197
  ttl("select", selected);
198
end]]--
199
200
move(10);
201
for i = 1, math.random(1, 5) do
202
  left()
203
  move(math.random(1, 15));
204
end
205
goHome();