SHOW:
|
|
- or go back to the newest paste.
1 | -- positional data | |
2 | x = 0 | |
3 | y = 0 | |
4 | z = 0 | |
5 | facing = 0 -- cardinal directions clockwise | |
6 | ||
7 | -- control data | |
8 | targetX = 0 | |
9 | targetY = 0 | |
10 | targetZ = 0 | |
11 | doDigging = true | |
12 | ||
13 | -- debug flags | |
14 | doPrintPos = false | |
15 | ||
16 | -- events | |
17 | events = {} | |
18 | stopAutoMove = false | |
19 | stopWaypoints = false | |
20 | function runEvents() | |
21 | - | for _, e in ipairs(events) do |
21 | + | for _, e in pairs(events) do |
22 | e() | |
23 | end | |
24 | end | |
25 | ||
26 | -- waypoints | |
27 | waypoints = {} | |
28 | ||
29 | ||
30 | -- util | |
31 | ||
32 | function printPos() | |
33 | print(x, y, z, facing) | |
34 | end | |
35 | ||
36 | function refuelMin() | |
37 | if(turtle.getFuelLevel() == 0) then | |
38 | start = turtle.getSelectedSlot() | |
39 | for i = 0,15,1 do | |
40 | turtle.select((start+i)%16+1) | |
41 | if(turtle.refuel()) then | |
42 | turtle.select(start) | |
43 | return true | |
44 | end | |
45 | end | |
46 | return false | |
47 | end | |
48 | end | |
49 | ||
50 | ||
51 | -- rotation | |
52 | ||
53 | function rotateLeft() | |
54 | if(turtle.turnLeft()) then | |
55 | facing = (facing-1)%4 | |
56 | return true | |
57 | end | |
58 | return false | |
59 | end | |
60 | ||
61 | function rotateRight() | |
62 | if(turtle.turnRight()) then | |
63 | facing = (facing+1)%4 | |
64 | return true | |
65 | end | |
66 | return false | |
67 | end | |
68 | ||
69 | function rotateTo(f) | |
70 | spin = (f-facing)%4 | |
71 | if(spin == 3) then | |
72 | spin = -1 | |
73 | end | |
74 | while(facing ~= f) do | |
75 | if(spin < 0) then | |
76 | rotateLeft() | |
77 | else | |
78 | rotateRight() | |
79 | end | |
80 | end | |
81 | return true | |
82 | end | |
83 | ||
84 | ||
85 | -- simple movement | |
86 | ||
87 | function moveForward() | |
88 | refuelMin() | |
89 | if(doDigging and turtle.detect) then | |
90 | turtle.dig() | |
91 | end | |
92 | if(turtle.forward()) then | |
93 | if(facing == 0) then | |
94 | z = z - 1 | |
95 | elseif(facing == 1) then | |
96 | x = x + 1 | |
97 | elseif(facing == 2) then | |
98 | z = z + 1 | |
99 | elseif(facing == 3) then | |
100 | x = x - 1 | |
101 | else | |
102 | error(facing) | |
103 | end | |
104 | if(doPrintPos) then | |
105 | printPos() | |
106 | end | |
107 | return true | |
108 | end | |
109 | return false | |
110 | end | |
111 | ||
112 | function moveUp() | |
113 | refuelMin() | |
114 | if(doDigging and turtle.detectUp) then | |
115 | turtle.digUp() | |
116 | end | |
117 | if(turtle.up()) then | |
118 | y = y + 1 | |
119 | if(doPrintPos) then | |
120 | printPos() | |
121 | end | |
122 | return true | |
123 | end | |
124 | return false | |
125 | end | |
126 | ||
127 | function moveDown() | |
128 | refuelMin() | |
129 | if(doDigging and turtle.detectDown) then | |
130 | turtle.digDown() | |
131 | end | |
132 | if(turtle.down()) then | |
133 | y = y - 1 | |
134 | if(doPrintPos) then | |
135 | printPos() | |
136 | end | |
137 | return true | |
138 | end | |
139 | return false | |
140 | end | |
141 | ||
142 | ||
143 | -- automated movement | |
144 | ||
145 | function moveAutoStepXZ() | |
146 | if(z > targetZ) then | |
147 | rotateTo(0) | |
148 | moveForward() | |
149 | elseif(x < targetX) then | |
150 | rotateTo(1) | |
151 | moveForward() | |
152 | elseif(z < targetZ) then | |
153 | rotateTo(2) | |
154 | moveForward() | |
155 | elseif(x > targetX) then | |
156 | rotateTo(3) | |
157 | moveForward() | |
158 | else | |
159 | return false | |
160 | end | |
161 | return true | |
162 | end | |
163 | ||
164 | function moveAutoStepY() | |
165 | if(y < targetY) then | |
166 | moveUp() | |
167 | elseif(y > targetY) then | |
168 | moveDown() | |
169 | else | |
170 | return false | |
171 | end | |
172 | return true | |
173 | end | |
174 | ||
175 | function moveAutoXYZ() | |
176 | while(moveAutoStepXZ()) do | |
177 | runEvents() | |
178 | if(stopAutoMove) then | |
179 | return | |
180 | end | |
181 | end | |
182 | while(moveAutoStepY()) do | |
183 | runEvents() | |
184 | if(stopAutoMove) then | |
185 | return | |
186 | end | |
187 | end | |
188 | end | |
189 | ||
190 | ||
191 | -- waypoints | |
192 | ||
193 | function waypointInstruction(array) | |
194 | targetX, targetY, targetZ = table.unpack(array) | |
195 | -- kwargs ignored for now | |
196 | moveAutoXYZ() | |
197 | end | |
198 | ||
199 | function waypointInstructions(array) | |
200 | - | for _, i in ipairs(array) do |
200 | + | for _, i in pairs(array) do |
201 | waypointInstruction(i) | |
202 | if(stopWaypoints) then | |
203 | return | |
204 | end | |
205 | end | |
206 | end | |
207 | ||
208 | - | function calculateWaypointInstructionsFuel(array) |
208 | + | |
209 | - | cost = 0 |
209 | + | |
210 | - | for k, v in ipairs(array) do |
210 | + | |
211 | - | if(k == 1) then |
211 | + | depth = 16 |
212 | - | x1 = x |
212 | + | distance = 16 |
213 | - | y1 = y |
213 | + | for i=0,distance-1,1 do |
214 | - | z1 = z |
214 | + | if(i%2 == 0) then |
215 | table.insert(waypoints, {0, -1, -i}) | |
216 | - | x1 = array[k-1][1] |
216 | + | table.insert(waypoints, {0, -depth, -i}) |
217 | - | y1 = array[k-1][2] |
217 | + | |
218 | - | z1 = array[k-1][3] |
218 | + | table.insert(waypoints, {0, -depth, -i}) |
219 | table.insert(waypoints, {0, -1, -i}) | |
220 | - | x2 = v[1] |
220 | + | |
221 | - | y2 = v[2] |
221 | + | |
222 | - | z2 = v[3] |
222 | + | table.insert(waypoints, {0, 0, 0}) |
223 | - | cost = cost + math.abs(x2-x1) + math.abs(y2-y1) + math.abs(z2-z1) |
223 | + | |
224 | table.insert(events, function() | |
225 | - | return cost |
225 | + | if(turtle.getItemCount(16) > 0) then |
226 | stopWaypoints = true | |
227 | waypointInstruction(waypoints[#waypoints]) | |
228 | end | |
229 | end) | |
230 | ||
231 | - | --depth = 16 |
231 | + | |
232 | - | --distance = 16 |
232 | + | |
233 | - | --for i=0,distance-1,1 do |
233 | + | |
234 | - | -- if(i%2 == 0) then |
234 | + |