SHOW:
|
|
- or go back to the newest paste.
| 1 | local cposition={0,0,0,0}
| |
| 2 | local home={0,0,0,0}
| |
| 3 | local shaftmode=false | |
| 4 | ||
| 5 | function modposition(axis, value) | |
| 6 | if (axis == 4) then | |
| 7 | cposition[axis] = ((cposition[axis] + value) % 4) | |
| 8 | else | |
| 9 | cposition[axis] = (cposition[axis] + value) | |
| 10 | end | |
| 11 | reportpositions() | |
| 12 | setfilepos("aware/position")
| |
| 13 | end | |
| 14 | ||
| 15 | function setshaftmode(toggle) | |
| 16 | shaftmode=toggle | |
| 17 | end | |
| 18 | ||
| 19 | function moveforward() | |
| 20 | if turtle.forward() then | |
| 21 | if (cposition[4] == 0) then modposition(2,1) | |
| 22 | elseif (cposition[4] == 1) then modposition(1,-1) | |
| 23 | elseif (cposition[4] == 2) then modposition(2,-1) | |
| 24 | elseif (cposition[4] == 3) then modposition(1,1) | |
| 25 | end | |
| 26 | return true | |
| 27 | else | |
| 28 | return false | |
| 29 | end | |
| 30 | end | |
| 31 | ||
| 32 | function moveback() | |
| 33 | if turtle.back() then | |
| 34 | if (cposition[4] == 0) then modposition(2,-1) | |
| 35 | elseif (cposition[4] == 1) then modposition(1,1) | |
| 36 | elseif (cposition[4] == 2) then modposition(2,1) | |
| 37 | elseif (cposition[4] == 3) then modposition(1,-1) | |
| 38 | end | |
| 39 | return true | |
| 40 | else | |
| 41 | return false | |
| 42 | end | |
| 43 | end | |
| 44 | ||
| 45 | function moveup() | |
| 46 | if turtle.up() then | |
| 47 | modposition(3,1) | |
| 48 | return true | |
| 49 | else | |
| 50 | return false | |
| 51 | end | |
| 52 | end | |
| 53 | ||
| 54 | function movedown() | |
| 55 | if turtle.down() then | |
| 56 | modposition(3,-1) | |
| 57 | return true | |
| 58 | else | |
| 59 | return false | |
| 60 | end | |
| 61 | end | |
| 62 | ||
| 63 | function turnright() | |
| 64 | modposition(4,1) | |
| 65 | turtle.turnRight() | |
| 66 | end | |
| 67 | ||
| 68 | function turnleft() | |
| 69 | modposition(4,-1) | |
| 70 | turtle.turnLeft() | |
| 71 | end | |
| 72 | ||
| 73 | function turnto(direction) | |
| 74 | while cposition[4] ~= direction do | |
| 75 | diff = cposition[4] - direction | |
| 76 | if ((diff == 1) or (diff == -3)) then | |
| 77 | turnleft() | |
| 78 | else | |
| 79 | turnright() | |
| 80 | end | |
| 81 | end | |
| 82 | end | |
| 83 | ||
| 84 | function digmoveforward() | |
| 85 | if turtle.detect() then | |
| 86 | turtle.dig() | |
| 87 | end | |
| 88 | if shaftmode then | |
| 89 | if turtle.detectUp() then | |
| 90 | turtle.digUp() | |
| 91 | end | |
| 92 | end | |
| 93 | if moveforward() then | |
| 94 | return true | |
| 95 | else | |
| 96 | return false | |
| 97 | end | |
| 98 | end | |
| 99 | ||
| 100 | function digmoveup() | |
| 101 | if turtle.detectUp() then | |
| 102 | turtle.digUp() | |
| 103 | end | |
| 104 | if shaftmode then | |
| 105 | if turtle.detect() then | |
| 106 | turtle.dig() | |
| 107 | end | |
| 108 | end | |
| 109 | if moveup() then | |
| 110 | return true | |
| 111 | else | |
| 112 | return false | |
| 113 | end | |
| 114 | end | |
| 115 | ||
| 116 | function digmovedown() | |
| 117 | if turtle.detectDown() then | |
| 118 | turtle.digDown() | |
| 119 | end | |
| 120 | if shaftmode then | |
| 121 | if turtle.detect() then | |
| 122 | turtle.dig() | |
| 123 | end | |
| 124 | end | |
| 125 | if movedown() then | |
| 126 | return true | |
| 127 | else | |
| 128 | return false | |
| 129 | end | |
| 130 | end | |
| 131 | ||
| 132 | function movetoz(tarz) | |
| 133 | while(cposition[3] ~= tarz) do | |
| 134 | if cposition[3] < tarz then | |
| 135 | digmoveup() | |
| 136 | else | |
| 137 | digmovedown() | |
| 138 | end | |
| 139 | end | |
| 140 | end | |
| 141 | ||
| 142 | function movetox(tarx) | |
| 143 | while(cposition[1] ~= tarx) do | |
| 144 | if cposition[1] < tarx then | |
| 145 | turnto(3) | |
| 146 | else | |
| 147 | turnto(1) | |
| 148 | end | |
| 149 | digmoveforward() | |
| 150 | end | |
| 151 | end | |
| 152 | ||
| 153 | function movetoy(tary) | |
| 154 | while(cposition[2] ~= tary) do | |
| 155 | if cposition[2] < tary then | |
| 156 | turnto(0) | |
| 157 | else | |
| 158 | turnto(2) | |
| 159 | end | |
| 160 | digmoveforward() | |
| 161 | end | |
| 162 | end | |
| 163 | ||
| 164 | function moveto(tarx, tary, tarz, taro) | |
| 165 | if(cposition[3] > tarz) then | |
| 166 | movetoz(tarz) | |
| 167 | movetoy(tary) | |
| 168 | movetox(tarx) | |
| 169 | else | |
| 170 | movetox(tarx) | |
| 171 | movetoy(tary) | |
| 172 | movetoz(tarz) | |
| 173 | end | |
| 174 | turnto(taro) | |
| 175 | end | |
| 176 | ||
| 177 | function goto(destination) | |
| 178 | termwrite("going to"..textutils.serialize(destination))
| |
| 179 | moveto(destination[1], destination[2], destination[3], destination[4]) | |
| 180 | end | |
| 181 | ||
| 182 | function gohome() | |
| 183 | goto(home) | |
| 184 | end | |
| 185 | ||
| 186 | function isfull() | |
| 187 | for i=1, 9 do | |
| 188 | if turtle.getItemCount(i) == 0 then | |
| 189 | return false | |
| 190 | end | |
| 191 | end | |
| 192 | return true | |
| 193 | end | |
| 194 | ||
| 195 | function isempty() | |
| 196 | for i=1, 9 do | |
| 197 | if turtle.getItemCount(i) > 0 then | |
| 198 | return false | |
| 199 | end | |
| 200 | end | |
| 201 | return true | |
| 202 | end | |
| 203 | ||
| 204 | function dump() | |
| 205 | while (turtle.drop()) do | |
| 206 | sleep(.12) | |
| 207 | end | |
| 208 | end | |
| 209 | ||
| 210 | function setfilepos(filename, coords) | |
| 211 | file = fs.open(filename, "w") | |
| 212 | if (coords == null) then | |
| 213 | file.write(textutils.serialize(cposition)) | |
| 214 | else | |
| 215 | file.write(textutils.serialize(coords)) | |
| 216 | end | |
| 217 | file:close() | |
| 218 | end | |
| 219 | ||
| 220 | function getfilepos(filename) | |
| 221 | file = fs.open(filename, "r") | |
| 222 | pos = textutils.unserialize(file.readLine()) | |
| 223 | file:close() | |
| 224 | return pos | |
| 225 | end | |
| 226 | ||
| 227 | function termwrite(message) | |
| 228 | cx, cy = term.getCursorPos() | |
| 229 | mx, my = term.getSize() | |
| 230 | term.write(message) | |
| 231 | if(cy == my) then | |
| 232 | term.scroll(1) | |
| 233 | term.setCursorPos(1, cy) | |
| 234 | else | |
| 235 | term.setCursorPos(1, cy + 1) | |
| 236 | end | |
| 237 | end | |
| 238 | ||
| 239 | function reportpositions() | |
| 240 | pushpos((os.getComputerID()+10000),"T",cposition) | |
| 241 | pushpos((os.getComputerID()+100),"H",home) | |
| 242 | end | |
| 243 | ||
| 244 | function pushpos(id, class, position) | |
| 245 | fposition=textutils.serialize(position) | |
| 246 | formatted={id,class,fposition}
| |
| 247 | rednet.broadcast(textutils.serialize(formatted)) | |
| 248 | end | |
| 249 | ||
| 250 | function sethome(coords) | |
| 251 | if (coords == null) then | |
| 252 | home = cposition | |
| 253 | else | |
| 254 | home = coords | |
| 255 | end | |
| 256 | setfilepos("aware/home", home)
| |
| 257 | end | |
| 258 | ||
| 259 | function setposition(coords) | |
| 260 | cposition = coords | |
| 261 | setfilepos("aware/position", coords)
| |
| 262 | end | |
| 263 | ||
| 264 | function getposition(axis) | |
| 265 | if (axis == null) then | |
| 266 | return copytable(cposition) | |
| 267 | else | |
| 268 | return cposition[axis] | |
| 269 | end | |
| 270 | end | |
| 271 | ||
| 272 | function gethome() | |
| 273 | return copytable(home) | |
| 274 | end | |
| 275 | ||
| 276 | function gpslocate() | |
| 277 | x,y,z = gps.locate(5) | |
| 278 | if (x ~= null) and (y ~= null) and (z ~= null) then | |
| 279 | setposition(x,y,z,0) | |
| 280 | return true | |
| 281 | - | setfilepos("aware/position")
|
| 281 | + | |
| 282 | - | termwrite("aware: created position")
|
| 282 | + | |
| 283 | end | |
| 284 | end | |
| 285 | ||
| 286 | function loadposition() | |
| 287 | if(fs.exists("aware/position")) then
| |
| 288 | cposition = getfilepos("aware/position")
| |
| 289 | termwrite("aware: loaded position")
| |
| 290 | else | |
| 291 | if gpslocate() then | |
| 292 | termwrite("aware: loaded position from gps")
| |
| 293 | else | |
| 294 | setfilepos("aware/position")
| |
| 295 | termwrite("aware: created position")
| |
| 296 | end | |
| 297 | end | |
| 298 | end | |
| 299 | ||
| 300 | function loadhome() | |
| 301 | if(fs.exists("aware/home")) then
| |
| 302 | home = getfilepos("aware/home")
| |
| 303 | termwrite("aware: loaded home")
| |
| 304 | else | |
| 305 | sethome() | |
| 306 | termwrite("aware: created home")
| |
| 307 | end | |
| 308 | end | |
| 309 | ||
| 310 | function copytable(t) | |
| 311 | local t2 = {}
| |
| 312 | for k,v in pairs(t) do | |
| 313 | t2[k] = v | |
| 314 | end | |
| 315 | return t2 | |
| 316 | end | |
| 317 | ||
| 318 | function init() | |
| 319 | if(fs.isDir("aware") == false) then
| |
| 320 | fs.makeDir("aware")
| |
| 321 | end | |
| 322 | rednet.open("right")
| |
| 323 | loadposition() | |
| 324 | loadhome() | |
| 325 | end | |
| 326 | ||
| 327 | init() |