Advertisement
Tag365

Test API

May 4th, 2015
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.73 KB | None | 0 0
  1. ------------------------
  2. -- Bundt API (Turtle) --
  3. ------------------------
  4.  
  5. -- Set up variables
  6. local modems = peripheral.find("modem")
  7. local tModem = modems
  8. tModem.open(8)
  9. local nServer = rednet.lookup("Map Server")
  10. local sConfigPath = "Bundt.Configuration"
  11. local sPositionPath = "Bundt.pos"
  12. local sMapPath = "BundtMap/"
  13. local sLabel = os.getComputerLabel() or "the turtle"
  14. local tCompass = {[0] = "south", [1] = "west", [2] = "north", [3] = "east"}
  15. local tMap = {}
  16. local tConfig = {}
  17. local tCurrentPos = {}
  18.  
  19. -- localize some functions
  20. local serialize = textutils.serialize
  21. local unserialize = textutils.unserialize
  22.  
  23. -- Local functions. --
  24. local function saveToFile(tTableToSave, sPath)
  25. local hFile = fs.open(sPath, "w")
  26. if type(hFile) == "table" then
  27. hFile.write(serialize(tTableToSave))
  28. hFile.close()
  29. return true
  30. end
  31. return false, hFile
  32. end
  33.  
  34. local function loadFromFile(sPath)
  35. local hFile, value = fs.open(sPath, "r"), false
  36. if type(hFile) == "table" then
  37. value = hFile.readAll()
  38. value = unserialize(value)
  39. hFile.close()
  40. return value
  41. end
  42. return false, hFile
  43. end
  44.  
  45. -- Attempts to obtain the position from the gps.
  46. function obtainPositionFromGPS()
  47. if gps then
  48. print("Obtaining position from the GPS...")
  49. local x, y, z = gps.locate(4)
  50. if x then
  51. print("Detected position from GPS.")
  52. print("X: "..x..", Y:"..y..", Z:"..z)
  53. print("If this is the correct position, press enter to save this value.")
  54. print("If this is incorrect press space to type in the correct position yourself.")
  55. while true do
  56. local event, nKeyPressed = os.pullEvent("key")
  57. if nKeyPressed == keys.enter then
  58. break
  59. elseif nKeyPressed == keys.space then
  60. return false
  61. end
  62. end
  63. tCurrentPos["x"], tCurrentPos["z"] = math.floor(x), math.floor(z)
  64. tCurrentPos["y"] = math.floor(y)
  65. return true
  66. end
  67. end
  68. return false
  69. end
  70.  
  71. function obtainRotationFromGPS()
  72. if turtle.getFuelLevel() > 1 then
  73. if gps then
  74. print("Obtaining rotation from the GPS...")
  75. local x, y, z = gps.locate(4)
  76. if not x then
  77. return false
  78. end
  79. local x2, y2, z2 = 0, 0, 0
  80. for k=1, 4 do
  81. if turtle.forward(1) == true then
  82. x2, y2, z2 = gps.locate(1)
  83. sleep(.5)
  84. turtle.back(1)
  85. break
  86. else
  87. turtle.turnLeft()
  88. end
  89. end
  90. if not x2 then
  91. return false
  92. end
  93. -- Detect rotation.
  94. local nFacing = false
  95. if x ~= x2 and z ~= z2 then
  96. print("The position the GPS sent is incorrect.")
  97. return false
  98. end
  99. if x > x2 then
  100. print("x > x2")
  101. nFacing = 1
  102. elseif x < x2 then
  103. print("x < x2")
  104. nFacing = 3
  105. end
  106. if z > z2 then
  107. print("z > z2")
  108. nFacing = 2
  109. elseif z < z2 then
  110. print("z < z2")
  111. nFacing = 0
  112. end
  113. print("Detected which way "..sLabel.." is facing from GPS.")
  114. print("Facing direction "..nFacing.." ("..tostring(tCompass[nFacing])..")")
  115. print("If this turtle is actually facing "..tCompass[nFacing]..", press enter.")
  116. print("If this is not true press space to type in the correct direction yourself.")
  117. while true do
  118. local event, nKeyPressed = os.pullEvent("key")
  119. if nKeyPressed == keys.enter then
  120. break
  121. elseif nKeyPressed == keys.space then
  122. return false
  123. end
  124. end
  125. tCurrentPos["facing"] = nFacing
  126. return true
  127. end
  128. end
  129. return false
  130. end
  131.  
  132. function askForPosition()
  133. term.setCursorPos(1, 1)
  134. term.clear()
  135. print("Please stand on top of "..(os.getComputerLabel() or " the turtle ").." and press F3.")
  136. print("Type in the position shown in the top left corner of the screen.")
  137. local x, y, z
  138. -- Ask the user to type in the correct position.
  139. term.write("x: ")
  140. while not x do
  141. x = tonumber(read())
  142. end
  143. term.write("y: ")
  144. while not y do
  145. y = tonumber(read())
  146. end
  147. term.write("z: ")
  148. while not z do
  149. z = tonumber(read())
  150. end
  151. tCurrentPos["x"], tCurrentPos["z"] = math.floor(x), math.floor(z)
  152. tCurrentPos["y"] = math.floor(y)
  153. end
  154.  
  155. function askForRotation()
  156. term.setCursorPos(1, 1)
  157. term.clear()
  158. print("Press F3 and face south according to the debug menu.")
  159. tCurrentPos["facing"] = nFacing
  160. while true do
  161. print("If this turtle is facing you, press enter.")
  162. print("Else press space to make the turtle turn left.")
  163. local event, nKeyPressed = os.pullEvent("key")
  164. if nKeyPressed == keys.enter then
  165. break
  166. elseif nKeyPressed == keys.space then
  167. print("Turning left...")
  168. turtle.turnLeft()
  169. end
  170. end
  171. tCurrentPos["facing"] = 2
  172. end
  173.  
  174. -- Global functions. --
  175. -- Reloads the configuration.
  176. function loadConfig()
  177. tConfig = loadFromFile(sConfigPath) or tConfig
  178. return tConfig
  179. end
  180.  
  181. -- Reloads the position.
  182. function loadPosition()
  183. local vLoaded, err = loadFromFile(sPositionPath)
  184. if vLoaded == false then
  185. print(err)
  186. return false
  187. end
  188. tCurrentPos = vLoaded
  189. return tCurrentPos
  190. end
  191.  
  192. -- Saves the configuration to the hard disk.
  193. function saveConfig()
  194. return saveToFile(tConfig, sConfigPath)
  195. end
  196.  
  197. -- Saves the position to the hard disk.
  198. function savePosition()
  199. return saveToFile(tCurrentPos, sPositionPath)
  200. end
  201.  
  202. -- Returns the current turtle position.
  203. function getTurtlePosition()
  204. if tCurrentPos["x"] then
  205. return tCurrentPos["x"], tCurrentPos["y"], tCurrentPos["z"]
  206. else
  207. loadPosition()
  208. if not tCurrentPos["x"] then
  209. if not obtainPositionFromGPS() then
  210. local ok, err = pcall(askForPosition)
  211. if not ok then
  212. error("Failed to ask for position!", 0)
  213. end
  214. end
  215. end
  216. savePosition()
  217. end
  218. return tCurrentPos["x"], tCurrentPos["y"], tCurrentPos["z"]
  219. end
  220.  
  221. -- Returns the current turtle rotation.
  222. function getTurtleRotation()
  223. if tCurrentPos["facing"] then
  224. return tCurrentPos["facing"], tCompass[tCurrentPos["facing"]]
  225. else
  226. loadPosition()
  227. if not tCurrentPos["facing"] then
  228. if not obtainRotationFromGPS() then
  229. local ok, err = pcall(askForRotation)
  230. if not ok then
  231. error("Failed to ask for rotation!", 0)
  232. end
  233. end
  234. end
  235. savePosition()
  236. end
  237. return tCurrentPos["facing"], tCompass[tCurrentPos["facing"]]
  238. end
  239.  
  240. -- Returns what direction the turtle is facing.
  241. function getTurtleDirection()
  242. local v1, v2 = getTurtleRotation()
  243. return v2
  244. end
  245.  
  246. function faceDirection(direction)
  247. if getTurtleRotation() - direction < 0 then
  248. while getTurtleRotation() ~= direction do
  249. turtle.turnLeft()
  250. end
  251. else
  252. while getTurtleRotation() ~= direction do
  253. turtle.turnRight()
  254. end
  255. end
  256. end
  257.  
  258. -- Moves the turtle north.
  259. function moveNorth(spaces)
  260. if not tCurrentPos["facing"] then
  261. getTurtleRotation()
  262. end
  263. faceDirection(2)
  264. turtle.forward(spaces)
  265. end
  266.  
  267. -- Moves the turtle south.
  268. function moveSouth(spaces)
  269. if not tCurrentPos["facing"] then
  270. getTurtleRotation()
  271. end
  272. faceDirection(0)
  273. turtle.forward(spaces)
  274. end
  275.  
  276. -- Moves the turtle west.
  277. function moveWest(spaces)
  278. if not tCurrentPos["facing"] then
  279. getTurtleRotation()
  280. end
  281. faceDirection(1)
  282. turtle.forward(spaces)
  283. end
  284.  
  285. -- Moves the turtle east.
  286. function moveEast(spaces)
  287. if not tCurrentPos["facing"] then
  288. getTurtleRotation()
  289. end
  290. faceDirection(3)
  291. turtle.forward(spaces)
  292. end
  293.  
  294. function goTo(x, y, z)
  295. local xOld, yOld, zOld = getTurtlePosition()
  296. if y > yOld then
  297. turtle.up(y - yOld)
  298. elseif y < yOld then
  299. turtle.down(yOld - y)
  300. end
  301. if x > xOld then
  302. moveEast(x - xOld)
  303. elseif x < xOld then
  304. moveWest(xOld - x)
  305. end
  306. if z > zOld then
  307. moveNorth(z - zOld)
  308. elseif x < xOld then
  309. moveSouth(zOld - z)
  310. end
  311. end
  312.  
  313. function getBlockPosInFront()
  314. local direction = getTurtleRotation()
  315. local x, y, z = getTurtlePosition()
  316. if direction == 3 then
  317. x = x + 1
  318. elseif direction == 1 then
  319. x = x - 1
  320. elseif direction == 2 then
  321. z = z + 1
  322. elseif direction == 0 then
  323. z = z - 1
  324. end
  325. return x, y, z
  326. end
  327.  
  328. function getBlockPosInBack()
  329. local direction = getTurtleRotation()
  330. local x, y, z = getTurtlePosition()
  331. if direction == 3 then
  332. x = x - 1
  333. elseif direction == 1 then
  334. x = x + 1
  335. elseif direction == 2 then
  336. z = z - 1
  337. elseif direction == 0 then
  338. z = z + 1
  339. end
  340. return x, y, z
  341. end
  342.  
  343. -- Map API --
  344. local nDiv16 = 1/16
  345. tMap.terrain = {}
  346. tMap.legend = {}
  347. tMap.serializeLegend = {}
  348.  
  349. function makeNewEntryInLegend(sCodeName, sMeta, sName, drawColor)
  350. if not tMap.legend[sCodeName] then
  351. tMap.legend[sCodeName] = {}
  352. end
  353. tMap.legend[sCodeName][tonumber(sMeta) or 0] = {#tMap.serializeLegend + 1, sName, drawColor}
  354. tMap.serializeLegend[#tMap.serializeLegend + 1] = {sCodeName, sMeta, sName, drawColor}
  355. end
  356.  
  357. local white, orange, magenta, lightBlue, yellow, lime, pink, gray, lightGray, cyan, purple, blue, brown, green, red, black = colors.white, colors.orange, colors.magenta, colors.lightBlue, colors.yellow, colors.lime, colors.pink, colors.gray, colors.lightGray, colors.cyan, colors.purple, colors.blue, colors.brown, colors.green, colors.red, colors.black
  358.  
  359. local buildEntries = {
  360. {"minecraft:stone", 0, "Stone", lightGray},
  361. {"minecraft:grass", 0, "Grass Block", green},
  362. {"minecraft:dirt", 0, "Dirt", brown},
  363. {"minecraft:dirt", 2, "Podzol", brown},
  364. {"minecraft:cobblestone", 0, "Cobblestone", gray},
  365. {"minecraft:planks", 0, "Oak Wood Planks", yellow},
  366. {"minecraft:planks", 1, "Spruce Wood Planks", brown},
  367. {"minecraft:planks", 2, "Birch Wood Planks", yellow},
  368. {"minecraft:planks", 3, "Jungle Wood Planks", brown},
  369. {"minecraft:planks", 4, "Acacia Wood Planks", red},
  370. {"minecraft:planks", 5, "Dark Oak Wood Planks", brown},
  371. {"minecraft:sapling", 0, "Oak Sapling", yellow},
  372. {"minecraft:bedrock", 0, "Bedrock", gray},
  373. {"minecraft:flowing_water", 0, "Water", blue},
  374. {"minecraft:flowing_lava", 0, "Lava", red},
  375. {"minecraft:sand", 0, "Sand", yellow},
  376. {"minecraft:sand", 1, "Red Sand", red},
  377. {"minecraft:gravel", 0, "Gravel", lightGray},
  378. {"minecraft:gold_ore", 0, "Gold Ore", yellow},
  379. {"minecraft:iron_ore", 0, "Iron Ore", brown},
  380. {"minecraft:coal_ore", 0, "Coal Ore", black},
  381. {"minecraft:log", 0, "Oak Wood", brown},
  382. {"minecraft:log", 1, "Spruce Wood", brown},
  383. {"minecraft:log", 2, "Birch Wood", white},
  384. {"minecraft:log", 3, "Jungle Wood", brown},
  385. {"minecraft:leaves", 0, "Oak Leaves", green},
  386. {"minecraft:leaves", 1, "Spruce Leaves", green},
  387. {"minecraft:leaves", 2, "Birch Leaves", green},
  388. {"minecraft:leaves", 3, "Jungle Leaves", green},
  389. {"minecraft:sponge", 0, "Sponge", yellow},
  390. {"minecraft:glass", 0, "Glass", lightBlue},
  391. {"minecraft:lapis_ore", 0, "Lapis Lazuli Ore", blue},
  392. {"minecraft:lapis_block", 0, "Lapis Lazuli Block", blue},
  393. {"minecraft:dispenser", 0, "Dispenser", yellow},
  394. {"minecraft:diamond_ore", 0, "Diamond Ore", cyan},
  395. {"ComputerCraft:CC-Computer", 0, "Computer", lightGray},
  396. {"ComputerCraft:CC-Computer", 13, "Advanced Computer", yellow},
  397. {"ComputerCraft:command_computer", 0, "Command Computer", brown},
  398. }
  399.  
  400. for k, v in ipairs(buildEntries) do
  401. makeNewEntryInLegend(v[1], v[2], v[3], v[4])
  402. end
  403.  
  404. function getChunk(x, y, z)
  405. if not tMap.terrain[x] then tMap.terrain[x] = {} end
  406. if not tMap.terrain[x][y] then tMap.terrain[x][y] = {} end
  407. if not tMap.terrain[x][y][z] then loadChunk(x, y, z) end
  408. if not tMap.terrain[x][y][z] then tMap.terrain[x][y][z] = {} end
  409. return tMap.terrain[x][y][z]
  410. end
  411.  
  412. function getChunkWithBlock(x, y, z)
  413. x, y, z = x - (math.floor(x*nDiv16)*16), y - (math.floor(y*nDiv16)*16), z - (math.floor(z*nDiv16)*16)
  414. if not tMap.terrain[x] then tMap.terrain[x] = {} end
  415. if not tMap.terrain[x][y] then tMap.terrain[x][y] = {} end
  416. if not tMap.terrain[x][y][z] then loadChunk(x, y, z) end
  417. if not tMap.terrain[x][y][z] then tMap.terrain[x][y][z] = {} end
  418. return tMap.terrain[x][y][z]
  419. end
  420.  
  421. function getChunkCorWithBlock(x, y, z)
  422. return math.floor(x*nDiv16), math.floor(y*nDiv16), math.floor(z*nDiv16)
  423. end
  424.  
  425. function getBlock(x, y, z)
  426. local tChunk = getChunk(math.floor(x*nDiv16), math.floor(y*nDiv16), math.floor(z*nDiv16))
  427. local x2, y2, z2 = x - (math.floor(x*nDiv16)*16), y - (math.floor(y*nDiv16)*16), z - (math.floor(z*nDiv16)*16)
  428. if not tChunk[x2] then tChunk[x2] = {} end
  429. if not tChunk[x2][y2] then tChunk[x2][y2] = {} end
  430. if not tChunk[x2][y2][z2] then
  431. tChunk[x2][y2][z2] = 1
  432. end
  433. return tChunk[x2][y2][z2]
  434. end
  435.  
  436. function getBlockWithinChunk(x, y, z, bx, by, bz)
  437. local tChunk = tMap.terrain[x][y][z]
  438. return tChunk[bx][by][bz]
  439. end
  440.  
  441. function getLegendID(t)
  442. local sCodeName, nMetadata = t["name"], t["metadata"]
  443. if tMap.legend[sCodeName] then
  444. if tMap.legend[sCodeName][nMetadata] then
  445. return tMap.legend[sCodeName][nMetadata][1]
  446. end
  447. return tMap.legend[sCodeName][0][1]
  448. end
  449. end
  450.  
  451. function updateBlock(x, y, z, t)
  452. local tChunk = getChunk(math.floor(x*nDiv16), math.floor(y*nDiv16), math.floor(z*nDiv16))
  453. local x2, y2, z2 = x - (math.floor(x*nDiv16)*16), y - (math.floor(y*nDiv16)*16), z - (math.floor(z*nDiv16)*16)
  454. if not tChunk[x2] then tChunk[x2] = {} end
  455. if not tChunk[x2][y2] then tChunk[x2][y2] = {} end
  456. if not tChunk[x2][y2][z2] then
  457. tChunk[x2][y2][z2] = 1
  458. end
  459. tChunk[x2][y2][z2] = getLegendID(t)
  460. return tBlock
  461. end
  462.  
  463. -- Scanning functions --
  464. -- These functions scan blocks and update the map with the new information.
  465. function scanBlockInFront()
  466. local dx, dy, dz = getBlockPosInFront()
  467. local isOk, tInspectedBlock = turtle.inspect()
  468. updateBlock(dx, dy, dz, tInspectedBlock)
  469. local tBlock = getBlock(dx, dy, dz)
  470. return dx, dy, dz, tBlock
  471. end
  472.  
  473. function scanBlockBehind()
  474. local dx, dy, dz = getBlockPosInBack()
  475. turtle.turnLeft()
  476. turtle.turnLeft()
  477. local isOk, tInspectedBlock = turtle.inspect()
  478. updateBlock(dx, dy, dz, tInspectedBlock)
  479. local tBlock = getBlock(dx, dy, dz)
  480. turtle.turnLeft()
  481. turtle.turnLeft()
  482. return dx, dy, dz, tBlock
  483. end
  484.  
  485. function scanBlockAbove()
  486. local dx, dy, dz = getTurtlePosition()
  487. dy = dy + 1
  488. local isOk, tInspectedBlock = turtle.inspectUp()
  489. updateBlock(dx, dy, dz, tInspectedBlock)
  490. local tBlock = getBlock(dx, dy, dz)
  491. return dx, dy, dz, tBlock
  492. end
  493.  
  494. function scanBlockBelow()
  495. local dx, dy, dz = getTurtlePosition()
  496. dy = dy - 1
  497. local isOk, tInspectedBlock = turtle.inspectDown()
  498. updateBlock(dx, dy, dz, tInspectedBlock)
  499. local tBlock = getBlock(dx, dy, dz)
  500. return dx, dy, dz, tBlock
  501. end
  502.  
  503. -- This function scans all six blocks intersecting the turtle.
  504. function scanBlocksNearby(suppressSaving)
  505. for k=1, 4 do
  506. scanBlockInFront()
  507. turtle.turnLeft()
  508. end
  509. scanBlockAbove()
  510. scanBlockBelow()
  511. if not suppressSaving then
  512. saveChunk(getChunkCorWithBlock(getTurtlePosition()))
  513. end
  514. end
  515.  
  516. -- Scans the area.
  517. function scanChunk(x, y, z)
  518. if not x then
  519. x, y, z = getTurtlePosition()
  520. end
  521. local nChunkX, nChunkY, nChunkZ = math.floor(x*nDiv16), math.floor(y*nDiv16), math.floor(z*nDiv16)
  522. local nStartX, nStartY, nStartZ = math.floor(x*nDiv16)*16, math.floor(y*nDiv16)*16, math.floor(z*nDiv16)*16
  523. local tChunk = getChunk(nChunkX, nChunkY, nChunkZ)
  524. goTo(nStartX, nStartY, nStartZ)
  525. for ky=0, 15 do
  526. for kx=0, 15 do
  527. if not tChunk[kx] then
  528. tChunk[kx] = {}
  529. end
  530. if not tChunk[kx][ky] then
  531. tChunk[kx][ky] = {}
  532. end
  533. for kz=0, 15 do
  534. goTo(nStartX + kx, nStartY + ky, nStartZ + kz)
  535. scanBlocksNearby(true)
  536. end
  537. end
  538. end
  539. saveChunk(x, y, z)
  540. end
  541.  
  542. function getBlockName(id, metadata)
  543. if type(id) == "number" then
  544. return tMap.serializeLegend[id][3]
  545. else
  546. return tMap.legend[id][metadata][3]
  547. end
  548. end
  549.  
  550. function getBlockColor(id, metadata)
  551. if type(id) == "number" then
  552. return tMap.serializeLegend[id][4]
  553. else
  554. return tMap.legend[id][metadata][4]
  555. end
  556. end
  557.  
  558. function drawBlockInMap(x, y, z)
  559. local id = tonumber(getBlock(x, y, z)) or 1
  560. if not type(id) == "number" then id = 1 end
  561. if not tMap.serializeLegend[id or 1] then return false end
  562. local color = tMap.serializeLegend[id or 1][4] or colors.blue
  563. term.setBackgroundColor(color)
  564. term.write(" ")
  565. end
  566.  
  567. -- Loads a terrain chunk.
  568. function loadChunk(x, y, z)
  569. local hFile = fs.open(sMapPath..x.."y"..y.."z"..z, "r")
  570. local m1, m2 = 16*16, 16*16*16
  571. if hFile then
  572. local sChunk = hFile.readAll()
  573. hFile.close()
  574. if not tMap.terrain[x] then tMap.terrain[x] = {} end
  575. if not tMap.terrain[x][y] then tMap.terrain[x][y] = {} end
  576. if not tMap.terrain[x][y][z] then tMap.terrain[x][y][z] = {} end
  577. local tChunk = tMap.terrain[x][y][z]
  578. for kx=1, 16 do
  579. tChunk[kx] = {}
  580. for ky=1, 16 do
  581. tChunk[kx][ky] = {}
  582. for kz=1, 16 do
  583. local begin = ((kx - 1)*m2) + (ky - 1)*m1 + kz - 1*4
  584. tChunk[kx][ky][kz] = tonumber(string.sub(sChunk, begin, begin + 4))
  585. end
  586. end
  587. end
  588. elseif nServer then
  589. rednet.send(nServer, textutils.serialize({x, y, z}), "Load Map")
  590. if not tMap.terrain[x] then tMap.terrain[x] = {} end
  591. if not tMap.terrain[x][y] then tMap.terrain[x][y] = {} end
  592. local senderId, message = rednet.recieve("Reply", 20)
  593. tMap.terrain[x][y][z] = textutils.unserialize(message)
  594. end
  595. end
  596.  
  597. function padString(str)
  598. if #str >= 4 then
  599. return string.sub(str, 0, 4)
  600. end
  601. return string.rep("0", 4 - #str)..str
  602. end
  603.  
  604. function saveChunk(x, y, z)
  605. local tChunk = getChunk(x, y, z)
  606. for kx=1, 16 do
  607. if not tChunk[kx] then tChunk[kx] = {} end
  608. for ky=1, 16 do
  609. if not tChunk[kx][ky] then tChunk[kx][ky] = {} end
  610. for kz=1, 16 do
  611. if not tChunk[kx][ky][kz] then tChunk[kx][ky][kz] = 0 end
  612. end
  613. end
  614. end
  615. local hFile = fs.open(sMapPath..x.."y"..y.."z"..z, "w")
  616. if hFile then
  617. for kx, vx in ipairs(tChunk) do
  618. for ky, vy in ipairs(vx) do
  619. for kz, vz in ipairs(vy) do
  620. hFile.write(padString(tostring(vz)) or "0000")
  621. end
  622. end
  623. end
  624. hFile.close()
  625. end
  626. if nServer then
  627. for k=1, 2 do
  628. sleep()
  629. rednet.send(nServer, textutils.serialize({tChunk, x, y, z}), "Update Map")
  630. end
  631. end
  632. end
  633.  
  634. -- Override the turtle API --
  635. local forward = turtle.forward
  636. local back = turtle.back
  637. local up = turtle.up
  638. local down = turtle.down
  639. local turnLeft = turtle.turnLeft
  640. local turnRight = turtle.turnRight
  641.  
  642. function turtle.forward(spaces)
  643. spaces = spaces or 1
  644. if tCurrentPos["x"] and tCurrentPos["facing"] then
  645. local spacesMoved, err = 0
  646. for k=1, spaces do
  647. err = forward(1)
  648. if not err then
  649. break
  650. end
  651. spacesMoved = spacesMoved + 1
  652. end
  653. if tCurrentPos["facing"] == 0 then
  654. tCurrentPos["z"] = tCurrentPos["z"] - spacesMoved
  655. elseif tCurrentPos["facing"] == 2 then
  656. tCurrentPos["z"] = tCurrentPos["z"] + spacesMoved
  657. end
  658. if tCurrentPos["facing"] == 1 then
  659. tCurrentPos["x"] = tCurrentPos["x"] - spacesMoved
  660. elseif tCurrentPos["facing"] == 3 then
  661. tCurrentPos["x"] = tCurrentPos["x"] + spacesMoved
  662. end
  663. savePosition()
  664. return err, spacesMoved
  665. else
  666. return forward(spaces)
  667. end
  668. end
  669.  
  670. function turtle.back(spaces)
  671. spaces = spaces or 1
  672. if tCurrentPos["x"] and tCurrentPos["facing"] then
  673. local spacesMoved, err = 0
  674. for k=1, spaces do
  675. err = back(1)
  676. if not err then
  677. break
  678. end
  679. spacesMoved = spacesMoved + 1
  680. end
  681. if tCurrentPos["facing"] == 0 then
  682. tCurrentPos["z"] = tCurrentPos["z"] + spacesMoved
  683. elseif tCurrentPos["facing"] == 2 then
  684. tCurrentPos["z"] = tCurrentPos["z"] - spacesMoved
  685. end
  686. if tCurrentPos["facing"] == 1 then
  687. tCurrentPos["x"] = tCurrentPos["x"] + spacesMoved
  688. elseif tCurrentPos["facing"] == 3 then
  689. tCurrentPos["x"] = tCurrentPos["x"] - spacesMoved
  690. end
  691. savePosition()
  692. return err, spacesMoved
  693. else
  694. return back(spaces)
  695. end
  696. end
  697.  
  698. function turtle.up(spaces)
  699. spaces = spaces or 1
  700. if tCurrentPos["x"] then
  701. local spacesMoved, err = 0
  702. for k=1, spaces do
  703. err = up(1)
  704. if not err then
  705. break
  706. end
  707. spacesMoved = spacesMoved + 1
  708. end
  709. tCurrentPos["y"] = tCurrentPos["y"] + spacesMoved
  710. savePosition()
  711. return err, spacesMoved
  712. else
  713. return up(spaces)
  714. end
  715. end
  716.  
  717. function turtle.down(spaces)
  718. spaces = spaces or 1
  719. if tCurrentPos["x"] then
  720. local spacesMoved, err = 0
  721. for k=1, spaces do
  722. err = down(1)
  723. if not err then
  724. break
  725. end
  726. spacesMoved = spacesMoved - 1
  727. end
  728. tCurrentPos["y"] = tCurrentPos["y"] + spacesMoved
  729. savePosition()
  730. return err, spacesMoved
  731. else
  732. return down(spaces)
  733. end
  734. end
  735.  
  736. function turtle.turnLeft(spaces)
  737. spaces = spaces or 1
  738. if tCurrentPos["facing"] then
  739. err = turnLeft()
  740. if tCurrentPos["facing"] == 3 then
  741. tCurrentPos["facing"] = 0
  742. else
  743. tCurrentPos["facing"] = tCurrentPos["facing"] + 1
  744. end
  745. savePosition()
  746. return err, spacesMoved
  747. else
  748. return turnLeft(spaces)
  749. end
  750. end
  751.  
  752. function turtle.turnRight(spaces)
  753. spaces = spaces or 1
  754. if tCurrentPos["facing"] then
  755. err = turnRight()
  756. if tCurrentPos["facing"] == 0 then
  757. tCurrentPos["facing"] = 3
  758. else
  759. tCurrentPos["facing"] = tCurrentPos["facing"] - 1
  760. end
  761. savePosition()
  762. return err, spacesMoved
  763. else
  764. return turnRight(spaces)
  765. end
  766. end
  767.  
  768. if fs.isDir("BundtMapModLegend") then
  769. for k, v in ipairs(fs.list("BundtMapModLegend")) do
  770. dofile(v)
  771. end
  772. end
  773.  
  774. -- Finish initalization. --
  775. loadConfig()
  776. getTurtlePosition()
  777. getTurtleRotation()
  778. --loadChunk(getChunkCorWithBlock(getTurtlePosition()))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement