Advertisement
Guest User

MagicTurtle API v1.38

a guest
May 27th, 2012
5,015
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 72.56 KB | None | 0 0
  1. --magicTurtle API by Liraal and EatenAlive3
  2. --version 1.38 14-03-2012
  3.  
  4. -- reference: getMatch(block) takes a block (ID or name), returns the slot number of the block in inventory, or false if not in inventory.
  5. -- reference: getSlotMatch(s1,s2,s3,s4,s5,s6,s7,s8,s9) takes a set of nine boolean states, and returns the number of the first state to be true.
  6. -- reference: getSlot(slot) returns the ID of the block contained within the slot.
  7.  
  8. local c=false
  9. local x, y, z --starting pos
  10. local face=2 --make sure that the turtle's pointing north
  11. local autoupdate=false --make this true if you want to autoupdate everytime
  12. local version=1.38
  13. local updateID=11 --ID of terminal sending updates/commands
  14. local armingID=11 --ID of terminal arming the mine
  15. local disarmingID=11 --ID of disarming terminal
  16. local selection = 1
  17. local slot1,slot2,slot3,slot4,slot5,slot6,slot7,slot8,slot9 = 0,0,0,0,0,0,0,0,0
  18. local blockIDs = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129}
  19. local blockNames = {"air","stone","grass","dirt","cobblestone","planks","sapling","bedrock","water","stationary water","lava","stationary lava","sand","gravel","gold ore","iron ore","coal ore","log","leaves","sponge","glass","lapis lazuli ore","lapis lazuli block","dispenser","sandstone","note block","bed","powered rail","detector rail","sticky piston","cobweb","tall grass","dead bush","piston","piston extension","wool","block moved by piston","dandelion","rose","brown mushroom","red mushroom","gold block","iron block","double slab","slab","bricks","tnt","bookshelf","moss stone","obsidian","torch","fire","monster spawner","wooden stairs","chest","redstone wire","diamond ore","diamond block","crafting table","wheat seeds","farmland","furnace","burning furnace","sign post","wooden door","ladder","rails","cobblestone stairs","wall sign","lever","stone pressure plate","iron door","wooden pressure plate","redstone ore","glowing redstone ore","redstone torch off","redstone torch","button","flat snow","ice","snow block","cactus","clay","sugar cane","jukebox","fence","pumpkin","netherrack","soulsand","glowstone","portal","jack-o-lantern","cake","repeater off","repeater on","locked chest","trapdoor","silverfish","stone bricks","huge brown mushroom","huge red mushroom","iron bars","glass pane","melon","pumpkin stem","melon stem","vines","fence gate","brick stairs","stone brick stairs","mycelium","lily pad","nether brick","nether brick fence","nether brick stairs","nether wart","enchantment table","brewing stand","cauldron","end portal","end portal frame","end stone","dragon egg","redstone lamp","redstone lamp on","unknown block","unmapped block","computer","disk drive","monitor"}
  20. air,stone,grass,dirt,cobblestone,cobble,planks,woodenplanks,sapling,bedrock,water,stationarywater,lava,stationarylava,sand,gravel,goldore,ironore,coalore,log,wood,leaves,leaf,sponge,glass,lapislazuliore,lapislazuliblock,dispenser,sandstone,noteblock,bed,poweredrail,detectorrail,stickypiston,cobweb,tallgrass,deadbush,piston,pistonextension,wool,blockmovedbypiston,dandelion,yellowflower,flower,rose,brownmushroom,redmushroom,goldblock,gold,ironblock,iron,doubleslab,slab,step,halfslab,halfstep,brick,bricks,tnt,bookshelf,mossstone,moss,obsidian,torch,fire,mobspawner,monsterspawner,woodenstairs,woodstairs,woodstair,chest,redstonewire,diamondore,diamond,diamondblock,craftingtable,craftingbench,workbench,worktable,wheatseeds,farmland,furnace,burningfurnace,signpost,woodendoor,wooddoor,woodoor,ladder,rails,cobblestonestairs,cobblestair,cobblestairs,wallsign,lever,stonepressureplate,irondoor,woodenpressureplate,redstoneore,glowingredstoneore,redstonetorchoff,redstonetorch,button,flatsnow,snow,ice,snowblock,cactus,clay,sugarcane,reeds,jukebox,fence,pumpkin,netherrack,soulsand,glowstone,portal,jackolantern,cake,repeateroff,repeateron,lockedchest,trapdoor,silverfish,stonebricks,smoothbricks,hugebrownmushroom,hugeredmushroom,ironbars,glasspane,melon,pumpkinstem,melonstem,vines,fencegate,brickstairs,stonebrickstairs,mycelium,lilypad,netherbrick,netherbrickfence,netherbrickstairs,netherwart,enchantmenttable,brewingstand,cauldron,endportal,endportalframe,endstone,dragonegg,redstonelamp,redstonelampon,unknown,unknownblock,unmapped,unmappedblock,computer,diskdrive,monitor = "air","stone","grass","dirt","cobblestone","cobblestone","planks","planks","sapling","bedrock","water","stationary water","lava","stationary lava","sand","gravel","gold ore","iron ore","coal ore","log","log","leaves","leaves","sponge","glass","lapis lazuli ore","lapis lazuli block","dispenser","sandstone","note block","bed","powered rail","detector rail","sticky piston","cobweb","tall grass","dead bush","piston","piston extension","wool","block moved by piston","dandelion","dandelion","dandelion","rose","brown mushroom","red mushroom","gold block","gold block","iron block","iron block","double slab","slab","slab","slab","slab","bricks","bricks","tnt","bookshelf","moss stone","moss stone","obsidian","torch","fire","monster spawner","monster spawner","wooden stairs","wooden stairs","wooden stairs","chest","redstone wire","diamond ore","diamond block","diamond block","crafting table","crafting table","crafting table","crafting table","wheat seeds","farmland","furnace","burning furnace","sign post","wooden door","wooden door","wooden door","ladder","rails","cobblestone stairs","cobblestone stairs","cobblestone stairs","wall sign","lever","stone pressure plate","iron door","wooden pressure plate","redstone ore","glowing redstone ore","redstone torch off","redstone torch","button","flat snow","flat snow","ice","snow block","cactus","clay","sugar cane","sugar cane","jukebox","fence","pumpkin","netherrack","soulsand","glowstone","portal","jack-o-lantern","cake","repeater off","repeater on","locked chest","trapdoor","silverfish","stone bricks","stone bricks","huge brown mushroom","huge red mushroom","iron bars","glass pane","melon","pumpkin stem","melon stem","vines","fence gate","brick stairs","stone brick stairs","mycelium","lily pad","nether brick","nether brick fence","nether brick stairs","nether wart","enchantment table","brewing stand","cauldron","end portal","end portal frame","end stone","dragon egg","redstone lamp","redstone lamp on","unknown block","unknown block","unmapped block","unmapped block","computer","disk drive","monitor"
  21. local map = {}
  22. vertexPaths = {}
  23. nodes = {}
  24. nodeF = {}
  25.  
  26. Vertex = {x=0,y=0,z=0,ID=0,active=true,
  27. setPos = function (self, xs,ys,zs)
  28. self.x=xs self.y=ys self.z=zs
  29. end,
  30.  
  31. getPos = function(self)
  32. return self.x,self.y,self.z
  33. end,
  34.  
  35. setID = function(self,newID)
  36. self.ID = newID
  37. vertexPaths[self.ID] = {}
  38. end,
  39.  
  40. getID = function(self)
  41. return self.ID
  42. end,
  43.  
  44. addPath = function(self,state)
  45. table.insert(vertexPaths[self.ID],#vertexPaths[self.ID]+1,state)
  46. end,
  47.  
  48. getPath = function(self)
  49. return vertexPaths[self.ID]
  50. end,
  51.  
  52. setActive = function(self,state)
  53. self.active = state
  54. end,
  55.  
  56. isActive = function(self)
  57. return self.active
  58. end,
  59.  
  60. getPosString = function(self)
  61. return self.x..","..self.y..","..self.z
  62. end,
  63.  
  64. deletePath = function(self)
  65. vertexPaths[self.ID] = {}
  66. end}
  67.  
  68. function Vertex:new(o)
  69. o = o or {}
  70. setmetatable(o, self)
  71. self.__index = self
  72. return o
  73. end
  74.  
  75. Node = {x=0,y=0,z=0,g=0,h=0,f=0,pid="",ID=0,active=true,
  76.  
  77. setActive = function(self,state)
  78. self.active = state
  79. end,
  80.  
  81. isActive = function(self)
  82. return self.active
  83. end,
  84.  
  85. getPos = function(self)
  86. return self.x,self.y,self.z
  87. end,
  88.  
  89. setPos = function(self,xs,ys,zs)
  90. self.x,self.y,self.z = xs,ys,zs
  91. end,
  92.  
  93. getID = function(self)
  94. return self.ID
  95. end,
  96.  
  97. setID = function(self,id)
  98. self.ID = id
  99. end,
  100.  
  101. getPid = function(self)
  102. return self.pid
  103. end,
  104.  
  105. setPid = function(self,id)
  106. self.pid = id
  107. end,
  108.  
  109. getPosString = function(self)
  110. return self.x..","..self.y..","..self.z
  111. end}
  112.  
  113. function Node:new(xs,ys,zs,xe,ye,ze,id,pid,s,penalty,o)
  114. if not penalty then penalty = 0 end
  115. o = o or {}
  116. setmetatable(o, self)
  117. self.__index = self
  118. if not s then o.g = nodes[pid].g + 10
  119. o.pid = nodes[pid]:getPid()..pid.."," end
  120. o.h = (math.abs(xs-xe)+math.abs(ys-ye)+math.abs(zs-ze)-1)*10+penalty
  121. o.f = o.h + o.g
  122. o.x,o.y,o.z = xs,ys,zs
  123. o.ID = id
  124. nodes[id] = o
  125. nodeF[id] = o.f
  126. return o
  127. end
  128.  
  129. function main()
  130. load()
  131. updateSlots()
  132. end
  133.  
  134. function load()
  135. getPosF()
  136. loadSlots()
  137. if fs.exists("/blockdata") then
  138. local file = io.open("/blockdata","r")
  139. map = textutils.unserialize(file:read())
  140. file:close()
  141. end
  142. end
  143.  
  144. function save()
  145. savePos()
  146. saveSlots()
  147. end
  148.  
  149. function getSelection()
  150. return selection end
  151.  
  152. function saveSlots()
  153. if not fs.isDir("/pos") then fs.makeDir("/pos") end
  154. local slots = io.open("/pos/slots", "w")
  155. slots:write(slot1.." "..slot2.." ".." "..slot3.." "..slot4.." "..slot5.." "..slot6.." "..slot7.." "..slot8.." "..slot9.." "..selection)
  156. slots:close()
  157. return true end
  158.  
  159. function loadSlots()
  160. if not fs.isDir("/pos") then return false end
  161. local slots = io.open("/pos/slots","r")
  162. allSlots = slots:read()
  163. slots:close()
  164. slot1 = pattern(allSlots,"%S+",1)
  165. slot2 = pattern(allSlots,"%S+",slot1:len()+2)
  166. slot3 = pattern(allSlots,"%S+",slot1:len()+slot2:len()+3)
  167. slot4 = pattern(allSlots,"%S+",slot1:len()+slot2:len()+slot3:len()+4)
  168. slot5 = pattern(allSlots,"%S+",slot1:len()+slot2:len()+slot3:len()+slot4:len()+5)
  169. slot6 = pattern(allSlots,"%S+",slot1:len()+slot2:len()+slot3:len()+slot4:len()+slot5:len()+6)
  170. slot7 = pattern(allSlots,"%S+",slot1:len()+slot2:len()+slot3:len()+slot4:len()+slot5:len()+slot6:len()+7)
  171. slot8 = pattern(allSlots,"%S+",slot1:len()+slot2:len()+slot3:len()+slot4:len()+slot5:len()+slot6:len()+slot7:len()+8)
  172. slot9 = pattern(allSlots,"%S+",slot1:len()+slot2:len()+slot3:len()+slot4:len()+slot5:len()+slot6:len()+slot7:len()+slot8:len()+9)
  173. selection = pattern(allSlots,"%S+",slot1:len()+slot2:len()+slot3:len()+slot4:len()+slot5:len()+slot6:len()+slot7:len()+slot8:len()+slot9:len()+10)
  174. slot1 = tonumber(slot1) slot2 = tonumber(slot2) slot3 = tonumber(slot3) slot4 = tonumber(slot4) slot5 = tonumber(slot5) slot6 = tonumber(slot6) slot7 = tonumber(slot7) slot8 = tonumber(slot8) slot9 = tonumber(slot9) selection = tonumber(selection) turtle.select(selection)
  175. return true
  176. end
  177.  
  178. function setSlot(slot,name)
  179. local setTo = 0
  180. local q,position = tableContains(blockNames,name:lower())
  181. if q then setTo = position-1 else return false end
  182. if slot == 1 then slot1 = setTo elseif slot == 2 then slot2 = setTo elseif slot == 3 then slot3 = setTo elseif slot == 4 then slot4 = setTo elseif slot == 5 then slot5 = setTo elseif slot == 6 then slot6 = setTo elseif slot == 7 then slot7 = setTo elseif slot == 8 then slot8 = setTo elseif slot == 9 then slot9 = setTo end
  183. saveSlots()
  184. return true end
  185.  
  186. function getSlot(slot)
  187. if slot == 1 then return slot1 elseif slot == 2 then return slot2 elseif slot == 3 then return slot3 elseif slot == 4 then return slot4 elseif slot == 5 then return slot5 elseif slot == 6 then return slot6 elseif slot == 7 then return slot7 elseif slot == 8 then return slot8 elseif slot == 9 then return slot9 else return false end end
  188.  
  189. function updateSlots()
  190. for i=1,9,1 do
  191. if turtle.getItemCount(i) == 0 then setSlot(i,air) end
  192. if turtle.getItemCount(i) > 0 and getSlot(i) == 0 then setSlot(i, unknown) end
  193. end
  194. saveSlots()
  195. end
  196.  
  197. function select(a)
  198. if tonumber(a) ~= nil then
  199. turtle.select(a)
  200. selection = a
  201. saveSlots()
  202. else
  203. if getMatch(a) then
  204. turtle.select(getMatch(a))
  205. selection = getMatch(a)
  206. saveSlots()
  207. else return false end
  208. end
  209. end
  210.  
  211. function place(a)
  212. local tmp = getSelection()
  213. if tonumber(a) == nil then
  214. select(a)
  215. a = getBlockID(a)
  216. else
  217. select(a)
  218. a = getSlot(a)
  219. end
  220. done = turtle.place()
  221. if face == 0 then
  222. setBlock(x,y,z+1,a)
  223. elseif face == 1 then
  224. setBlock(x-1,y,z,a)
  225. elseif face == 2 then
  226. setBlock(x,y,z-1,a)
  227. elseif face == 3 then
  228. setBlock(x+1,y,z,a) end
  229. select(tmp) updateSlots() return done end
  230.  
  231. function placeUp(a)
  232. local tmp = getSelection()
  233. if tonumber(a) == nil then
  234. select(a)
  235. a = getBlockID(a)
  236. else
  237. select(a)
  238. a = getSlot(a)
  239. end
  240. done = turtle.placeUp()
  241. setBlock(x,y+1,z,a)
  242. select(tmp) updateSlots() return done end
  243.  
  244. function placeDown(a)
  245. local tmp = getSelection()
  246. if tonumber(a) == nil then
  247. select(a)
  248. a = getBlockID(a)
  249. else
  250. select(a)
  251. a = getSlot(a)
  252. end
  253. done = turtle.placeDown()
  254. setBlock(x,y-1,z,a)
  255. select(tmp) updateSlots() return done end
  256.  
  257. function tableContains(table, element)
  258. local count = 0
  259. for _, value in pairs(table) do
  260. count = count + 1
  261. if value == element then
  262. return true,count
  263. end
  264. end
  265. return false,0
  266. end
  267.  
  268. function getBlockName(i)
  269. local final
  270. local names = blockNames[i+1]
  271. if names then final = names
  272. elseif (i/1000)>= 0 and (i/1000) == math.floor(i/1000) then final = "turtle "..(i/1000) end
  273. return final end
  274.  
  275. function getBlockID(name)
  276. if name == false or name == true then name = basicToBlockType(name) end
  277. if tonumber(name) ~= nil then return false end
  278. local q,w = tableContains(blockNames,name:lower())
  279. if q then return tonumber(blockIDs[w])
  280. else return false end end
  281.  
  282. function version()
  283. return version
  284. end
  285.  
  286. function setMineID(arming, disarming)
  287. armingID=arming
  288. disarmingID=disarming
  289. end
  290.  
  291. function setUpdateID(a)
  292. updateID=a
  293. end
  294.  
  295. function savePos()
  296. if not fs.isDir("/pos") then fs.makeDir("/pos") end
  297. local f1=io.open("/pos/posx", "w")
  298. local f2=io.open("/pos/posy", "w")
  299. local f3=io.open("/pos/posz", "w")
  300. local f4=io.open("/pos/face", "w")
  301. f1:write(x)
  302. f2:write(y)
  303. f3:write(z)
  304. f4:write(face)
  305. f1:close()
  306. f2:close()
  307. f3:close()
  308. f4:close()
  309. return true
  310. end
  311.  
  312. function getPosF()
  313. if not fs.exists("/pos/posx") or not fs.exists("/pos/posy") or not fs.exists("/pos/posz") or not fs.exists("/pos/face") then print("You need to set your position with setPos") return false end
  314. local f1=io.open("pos/posx", "r")
  315. local f2=io.open("pos/posy", "r")
  316. local f3=io.open("pos/posz", "r")
  317. local f4=io.open("pos/face", "r")
  318. x=tonumber(f1:read())
  319. y=tonumber(f2:read())
  320. z=tonumber(f3:read())
  321. face=tonumber(f4:read())
  322. f1:close()
  323. f2:close()
  324. f3:close()
  325. f4:close()
  326. end
  327.  
  328. function getPos()
  329. return x,y,z
  330. end
  331.  
  332. function setNorth()
  333. face=2
  334. end
  335.  
  336. function setFacing(f)
  337. face=f
  338. savePos()
  339. return true
  340. end
  341.  
  342. function setPos(a, b, c, ...)
  343. x=a
  344. y=b
  345. z=c
  346. if ...~=nil then face=... end
  347. savePos()
  348. return true
  349. end
  350.  
  351. function faceAdd(a)
  352. if face+a>3 then face=face+a-4 else face=face+a end
  353. end
  354.  
  355. function faceSub(a)
  356. if face-a<0 then face=4-face-a else face=face-a end
  357. end
  358.  
  359. function faceSouth()
  360. return faceSet(0)
  361. end
  362.  
  363. function faceEast()
  364. return faceSet(3)
  365. end
  366.  
  367. function faceNorth()
  368. return faceSet(2)
  369. end
  370.  
  371. function faceWest()
  372. return faceSet(1)
  373. end
  374.  
  375. function toString(a)
  376. local bool = a
  377. if bool == true then
  378. return "true"
  379. else
  380. return "false"
  381. end
  382. end
  383.  
  384. function faceSet(a)
  385. local turn = face-a
  386. local tmp = getFacing()
  387. local done = false
  388. if face == a then
  389. done = true
  390. elseif turn == -1 or turn == 3 then
  391. turnRight()
  392. done = true
  393. elseif turn == 1 or turn == -3 then
  394. turnLeft()
  395. done = true
  396. elseif math.abs(turn) == 2 then
  397. rotate(2)
  398. done = true
  399. end
  400. if done == false then
  401. face = tmp
  402. end
  403. return done
  404. end
  405.  
  406. function turnLeft(a)
  407. if not a then a = 1 end
  408. for i=1,math.abs(a),1 do
  409. turtle.turnLeft()
  410. faceSub(1)
  411. savePos()
  412. end
  413. return true
  414. end
  415.  
  416. function turnRight(a)
  417. if not a then a = 1 end
  418. for i=1,math.abs(a),1 do
  419. turtle.turnRight()
  420. faceAdd(1)
  421. savePos()
  422. end
  423. return true
  424. end
  425.  
  426. function rotate(angle)
  427. if math.abs(math.floor((angle+1)/4)) == math.abs((angle+1)/4) then
  428. turnRight(angle-2)
  429. else
  430. turnLeft(angle)
  431. end
  432. end
  433.  
  434. function checkSides()
  435. local front = turtle.detect()
  436. turnRight()
  437. local right = turtle.detect()
  438. turnRight()
  439. local back = turtle.detect()
  440. turnRight()
  441. local left = turtle.detect()
  442. turnRight()
  443. local top = turtle.detectUp()
  444. local bottom = turtle.detectDown()
  445. return front,right,back,left,top,bottom
  446. end
  447.  
  448. function getPos()
  449. return x, y, z
  450. end
  451.  
  452. function getFacing()
  453. return face
  454. end
  455.  
  456. function up(a)
  457. local state
  458. if not a then a=1 end
  459. if y~=255 then
  460. for i=1,a,1 do
  461. state = turtle.up()
  462. if state then y=y+1
  463. setBlock(x,y-1,z,0)
  464. setBlock(x,y,z,os.getComputerID().."000")
  465. savePos() else break end
  466. end
  467. end
  468. return state
  469. end
  470.  
  471. function down(a)
  472. local state
  473. if not a then a = 1 end
  474. if y~=1 then
  475. for i=1,a,1 do
  476. state = turtle.down()
  477. if state then y=y-1
  478. setBlock(x,y+1,z,0)
  479. setBlock(x,y,z,os.getComputerID().."000")
  480. savePos() else break end
  481. end
  482. end
  483. return state
  484. end
  485.  
  486. function forward(a)
  487. local state=true
  488. if not a then a = 1 end
  489. if face==0 then for i=1, a, 1 do state=turtle.forward() if state then z=z+1 savePos()
  490. setBlock(x,y,z-1,0)
  491. setBlock(x,y,z,os.getComputerID().."000") end end end
  492. if face==1 then for i=1, a, 1 do state=turtle.forward() if state then x=x-1 savePos()
  493. setBlock(x+1,y,z,0)
  494. setBlock(x,y,z,os.getComputerID().."000") end end end
  495. if face==2 then for i=1, a, 1 do state=turtle.forward() if state then z=z-1 savePos()
  496. setBlock(x,y,z+1,0)
  497. setBlock(x,y,z,os.getComputerID().."000") end end end
  498. if face==3 then for i=1, a, 1 do state=turtle.forward() if state then x=x+1 savePos()
  499. setBlock(x-1,y,z,0)
  500. setBlock(x,y,z,os.getComputerID().."000") end end end
  501. return state
  502. end
  503.  
  504. function back(a)
  505. local state=true
  506. if not a then a = 1 end
  507. if face==2 then for i=1, a, 1 do state=turtle.back() if state then z=z+1 savePos()
  508. setBlock(x,y,z-1,0)
  509. setBlock(x,y,z,os.getComputerID().."000") end end end
  510. if face==3 then for i=1, a, 1 do state=turtle.back() if state then x=x-1 savePos()
  511. setBlock(x+1,y,z,0)
  512. setBlock(x,y,z,os.getComputerID().."000") end end end
  513. if face==0 then for i=1, a, 1 do state=turtle.back() if state then z=z-1 savePos()
  514. setBlock(x,y,z+1,0)
  515. setBlock(x,y,z,os.getComputerID().."000") end end end
  516. if face==1 then for i=1, a, 1 do state=turtle.back() if state then x=x+1 savePos()
  517. setBlock(x-1,y,z,0)
  518. setBlock(x,y,z,os.getComputerID().."000") end end end
  519. return state
  520. end
  521.  
  522. function left(a)
  523. if not a then a = 1 end
  524. turnLeft()
  525. return forward(a)
  526. end
  527.  
  528. function right(a)
  529. if not a then a = 1 end
  530. turnRight()
  531. return forward(a)
  532. end
  533.  
  534. function goToPos(a, b, c, mode)
  535. faceEast()
  536. a=a-x
  537. b=b-y
  538. c=c-z
  539. if a>=0 then
  540. for i=1, a, 1 do
  541. while mode and turtle.detect() do dig() sleep(0.2) end
  542. forward(1)
  543. end
  544. else
  545. a=math.abs(a)
  546. rotate(2)
  547. for i=1, a, 1 do
  548. while mode and turtle.detect() do dig() sleep(0.2) end
  549. forward(1)
  550. end
  551. end
  552. if b>=0 then for i=1, b, 1 do
  553. while mode and turtle.detectUp() do digUp() sleep(0.2) end
  554. up()
  555. end
  556. else
  557. b=math.abs(b)
  558. for i=1, b, 1 do
  559. while mode and turtle.detectDown() do digDown() sleep(0.2) end
  560. down()
  561. end
  562. end
  563. faceSouth()
  564. if c>=0 then
  565. for i=1, c, 1 do
  566. while mode and turtle.detect() do dig() sleep(0.2) end
  567. forward(1)
  568. end
  569. else
  570. c=math.abs(c)
  571. rotate(2)
  572. for i=1, c, 1 do
  573. while mode and turtle.detect() do dig() sleep(0.2) end
  574. forward(1)
  575. end
  576. end
  577. return true
  578. end
  579.  
  580. function tunnel(w, h, l)
  581. local xs, ys, zs=getPos()
  582. local faces=face
  583. for k=1, l, 1 do
  584. for j=1, h, 1 do
  585. turnLeft()
  586. for i=1, w, 1 do
  587. dig()
  588. forward(1)
  589. end
  590. for i=1, w, 1 do back(1) end
  591. turnRight()
  592. if j<h then digDown() down() end
  593. end
  594. for j=1, h, 1 do if j<h then up() end end
  595. dig() forward(1)
  596. end
  597. goToPos(xs, ys, zs, false)
  598. faceSet(faces)
  599. return true
  600. end
  601.  
  602. function staircase(w, h, l)
  603. local xs, ys, zs=getPos()
  604. local faces=face
  605. for k=1, l, 1 do
  606. for j=1, h, 1 do
  607. turnLeft()
  608. for i=1, w, 1 do
  609. dig()
  610. forward(1)
  611. end
  612. for i=1, w, 1 do back(1) end
  613. turnRight()
  614. if j<h then digDown() down() end
  615. end
  616. for j=1, h, 1 do up() end
  617. dig() forward(1)
  618. end
  619. goToPos(xs, ys, zs, false)
  620. faceSet(faces)
  621. return true
  622. end
  623.  
  624. function wread()
  625. local _, y=term.getCursorPos()
  626. local tmp=read()
  627. local x, _=term.getCursorPos()
  628. term.setCursorPos(x,y)
  629. return tmp
  630. end
  631.  
  632. function build(blueprint)
  633. local l=#blueprint
  634. local h=#blueprint[1]
  635. local w=#blueprint[1][1]
  636. local xs, ys, zs=getPos()
  637. local faces=face
  638. for k=1, l, 1 do
  639. for j=1, h, 1 do
  640. turnLeft()
  641. for i=1, w, 1 do
  642. dig()
  643. forward(1)
  644. end
  645. for i=1, w, 1 do
  646. local block=blueprint[k][j][i]
  647. print(block)
  648. if block~=0 then select(block) turtle.place() end
  649. back(1)
  650. end
  651. turnRight()
  652. if j<h then digDown() down() end
  653. end
  654. for j=1, h, 1 do if j<h then up() end end
  655. dig() forward(1)
  656. end
  657. goToPos(xs, ys, zs, false)
  658. faceSet(faces)
  659. return true
  660. end
  661.  
  662. function sketch()
  663. print("Blueprint maker")
  664. print("Use turtle's slot IDs for data")
  665. print("Enter dimensions")
  666. write(" Width: ") local w=wread()
  667. write(" Height: ") local h=wread()
  668. write(" Length: ") local l=wread()
  669. local t={}
  670. for i=1, l, 1 do
  671. print("Vertical Layer ",i)
  672. local tt={}
  673. for j=1, h, 1 do
  674. print("Horizontal Row ", j)
  675. local ttt={}
  676. for k=1, w, 1 do
  677. local tmp=tonumber(wread())
  678. table.insert(ttt, tmp)
  679. write(" ")
  680. end
  681. table.insert(tt, ttt)
  682. end
  683. table.insert(t, tt)
  684. end
  685. return t
  686. end
  687.  
  688. function getUpdate(side)
  689. local a,b,c=os.pullEvent()
  690. while a~="rednet_message" or b~=updateID do a,b,c=os.pullEvent() end
  691. local tmp=loadstring(c)
  692. tmp()
  693. return true
  694. end
  695.  
  696. function getUpdateP(side)
  697. if peripheral.getType(side)=="disk" and fs.exists("/"..disk.getMountPath(side).."/magicTurtleUpdate/update") then
  698. shell.run("/"..disk.getMountPath(side).."/magicTurtleUpdate/update")
  699. return true
  700. end
  701. return false
  702. end
  703.  
  704. function event()
  705. local a,b,c=os.pullEvent()
  706. if a=="rednet_message" and b==updateID and c=="update" then getUpdate(b) return true end
  707. if a=="peripheral" then getUpdateP(b) return true end
  708. return false
  709. end
  710.  
  711. function updateSeek()
  712. rednet.open("right")
  713. local result = false
  714. while not result do result = event() end
  715. return true
  716. end
  717.  
  718. function lua()
  719. while true do
  720. write(">")
  721. local com=wread() print("")
  722. if com=="exit" then return true end
  723. if com=="menu" then menu() end
  724. if c then break end
  725. local exec=loadstring(com)
  726. exec()
  727. end
  728. end
  729.  
  730. function autoUpdate()
  731. autoupdate=true
  732. while true do
  733. parallel.waitForAny(lua, updateSeek)
  734. end
  735. return true
  736. end
  737.  
  738. function isSpace()
  739. for i=1, 9, 1 do
  740. if turtle.getItemCount(i)==0 then return true end
  741. end
  742. return false
  743. end
  744.  
  745. function diamondMine(l,...)
  746. local xs,ys,zs=getPos()
  747. local a,b,c=getPos()
  748. while b~=12 do digDown() down() dig() digDown() forward(1) a,b,c=getPos() end
  749. while isSpace() do
  750. if ...~=nil then digSlot(...) else dig() end
  751. forward(1) digDown()
  752. turnLeft() for i=1, l, 1 do
  753. if ...~=nil then digSlot(...) else dig() end
  754. forward(1) end
  755. back(l)
  756. rotate(2)
  757. for i=1, l, 1 do
  758. if ...~=nil then digSlot(...) else dig() end
  759. forward(1) end
  760. back(l)
  761. end
  762. goToPos(xs,ys,zs, true)
  763. return true
  764. end
  765.  
  766. function mine(mode, l,...)
  767. if mode=="diamond" then diamondMine(l,...) end
  768. if mode=="regular" then tunnel(l, l, l) end
  769. if mode=="quarry" then quarry(l, ...) end
  770. return true
  771. end
  772.  
  773. function sendCommand(id, side, exec)
  774. rednet.open(side)
  775. rednet.send(id, "update")
  776. local state=rednet.send(id, exec)
  777. return state
  778. end
  779.  
  780. function fcoord()
  781. if face==0 then z=z+1 end
  782. if face==1 then x=x+1 end
  783. if face==2 then z=z-1 end
  784. if face==3 then x=x-1 end
  785. end
  786.  
  787. function lookAround()
  788. if not turtle.forward() and rs.getInput("front") then return false end
  789. fcoord()
  790. if not rs.getInput("bottom") then
  791. back(1)
  792. turnRight() forward(1)
  793. if not rs.getInput("bottom") then
  794. back(2) rotate(2)
  795. if not rs.getInput("bottom") then
  796. back(1) turnRight() return false
  797. else return true
  798. end
  799. else return true
  800. end
  801. else return true
  802. end
  803. end
  804.  
  805. function road()
  806. while true do
  807. if not lookAround() then
  808. up()
  809. if not lookAround() then
  810. if not turtle.forward() and not rs.getInput("bottom") then turnLeft() write(1)
  811. if not turtle.forward() and not rs.getInput("bottom") then rotate(2) write(2)
  812. if not turtle.forward() and not rs.getInput("bottom") then break end end end
  813. write(13)
  814. fcoord()
  815. down() down()
  816. if not lookAround() then up() break end
  817. end
  818. end
  819. end
  820. end
  821.  
  822. function obs()
  823. while true do
  824. if turtle.detect() then
  825. blow=true
  826. write(1)
  827. back(1)
  828. select(5)
  829. turtle.place()
  830. for i=1, 5, 1 do
  831. write(2)
  832. if not turtle.detectDown() then select(1) turtle.placeDown() end
  833. back(1)
  834. end
  835. forward(5)
  836. for i=1, 5, 1 do
  837. back(1)
  838. select(6)
  839. turtle.place()
  840. end
  841. rs.setOutput("front", true) back(1) rs.setOutput("front", false)
  842. back(3)
  843. sleep(2.5)
  844. forward(9)
  845. blow=false
  846. end
  847. sleep(0.2)
  848. end
  849. end
  850.  
  851. function blowup()
  852. local tmp
  853. local blow=false
  854. while true do
  855. print(blow)
  856. if not blow then tmp=loadstring(read()) else tmp=loadstring("") end
  857. parallel.waitForAll(tmp, obs)
  858. end
  859. return true
  860. end
  861.  
  862. function wander(mode)
  863. if mode == nil then mode = false end
  864. while true do
  865. if (math.random(1,10))>5 then turnLeft()
  866. else turnRight() end
  867. for i=1, math.random(10, 30), 1 do if not turtle.detect() then forward(1) end end
  868. if not mode then
  869. sleep(1) end
  870. end end
  871.  
  872. function wanderer(exec)
  873. local wand=true
  874. while wand do
  875. local tmp=loadstring(exec)
  876. parallel.waitForAny(wander, tmp)
  877. end
  878. end
  879.  
  880. function jack(sap)
  881. local xs,ys,zs=getPos()
  882. select(sap)
  883. turtle.place()
  884. up()
  885. while not turtle.detect() do sleep(0.2) end
  886. back(2) turnRight() back(2) up() up() up() up()
  887. tunnel(5,5,5)
  888. goToPos(xs,ys,zs, true)
  889. end
  890.  
  891. function treefarm(sap)
  892. while true do jack(sap) end
  893. end
  894.  
  895. function watchUp()
  896. while true do
  897. if turtle.detectUp() then rs.setOutput("bottom", true) end
  898. sleep(0.5)
  899. end
  900. end
  901.  
  902. function disarm()
  903. while true do
  904. local a,b,c=os.pullEventRaw()
  905. if a=="rednet_message" and b==disarmingID and c=="disarm" then arm=false end
  906. sleep(0.5)
  907. end
  908. end
  909.  
  910. function landmine(slot, net)
  911. local arm
  912. while not turtle.detectDown() do down() end
  913. digDown() down() digDown()
  914. select(slot)
  915. turtle.placeDown()
  916. while true do
  917. if net then
  918. local a,b,c=os.pullEventRaw()
  919. while a~="rednet_message" or b~=armingID or c~="arming" do a,b,c=os.pullEventRaw() end
  920. end
  921. arm=true
  922. while arm do
  923. if net then
  924. parallel.waitForAny(watchUp, disarm)
  925. else
  926. watchUp()
  927. end
  928. end
  929. end
  930. digdown() up()
  931. end
  932.  
  933. function patch(slot, l, w)
  934. for i=1, l, 1 do
  935. turnLeft()
  936. for j=1, w, 1 do
  937. forward(1)
  938. if (turtle.getItemCount(slot))==0 then return false end
  939. select(slot)
  940. while not turtle.detectDown() do turtle.placeDown() end
  941. end
  942. back(w) turnRight() forward(1)
  943. end
  944. back(l)
  945. end
  946.  
  947. function guardArea(exec, w, h)
  948. local tmp=loadstring(exec)
  949. while true do
  950. for i=1, h, 1 do
  951. turnLeft()
  952. for j=1, w, 1 do
  953. forward(1)
  954. if not turtle.detectDown() then tmp() end
  955. if turtle.detect() then tmp() end
  956. end
  957. back(w) turnRight() forward(1)
  958. end
  959. back(h)
  960. end
  961. end
  962.  
  963. function remoteArm(id)
  964. return rednet.send(id, "arming")
  965. end
  966.  
  967. function remoteDisarm(id)
  968. return rednet.send(id, "disarming")
  969. end
  970.  
  971. function remoteSleep(id, t)
  972. remoteDisarm(id)
  973. sleep(t)
  974. remoteArm(id)
  975. end
  976.  
  977. function cDigDown()
  978. local it={}
  979. for i=1, 9, 1 do
  980. table.insert(it, turtle.getItemCount(i))
  981. end
  982. digDown()
  983. for i=1, 9, 1 do
  984. if it[i]<turtle.getItemCount(i) then return i end
  985. end
  986. return false
  987. end
  988.  
  989. function cDig()
  990. local it={}
  991. for i=1, 9, 1 do
  992. table.insert(it, turtle.getItemCount(i))
  993. end
  994. dig()
  995. for i=1, 9, 1 do
  996. if it[i]<(turtle.getItemCount(i)) then return i end
  997. end
  998. return false
  999. end
  1000.  
  1001. function camoMine(slot)
  1002. local slot2=cDigDown()
  1003. down() digDown()
  1004. placeDown(slot) up()
  1005. placeDown(slot2)
  1006. return true
  1007. end
  1008.  
  1009. function mineGrid(slot)
  1010. for i=1, 5, 1 do
  1011. turnRight()
  1012. for j=1, 5, 1 do
  1013. camoMine(slot)
  1014. forward(3)
  1015. end
  1016. back(15)
  1017. turnLeft()
  1018. forward(3)
  1019. end
  1020. back(9) turnRight() forward(6) turnLeft()
  1021. end
  1022.  
  1023. function playerDetect()
  1024. if not turtle.forward() and not dig() then turtle.back() return true else return false end
  1025. end
  1026.  
  1027. function follow()
  1028. while not playerDetect() do
  1029. turnRight() sleep(0.1)
  1030. end
  1031. forward(1)
  1032. end
  1033.  
  1034. function follower()
  1035. while true do
  1036. follow()
  1037. sleep(0.1)
  1038. end
  1039. end
  1040.  
  1041. function detectLeft()
  1042. turnLeft()
  1043. local a=turtle.detect()
  1044. turnRight()
  1045. return a
  1046. end
  1047.  
  1048. function detectRight()
  1049. turnRight()
  1050. local a=turtle.detect()
  1051. turnLeft()
  1052. return a
  1053. end
  1054.  
  1055. function detectBack()
  1056. rotate(2)
  1057. local a=turtle.detect()
  1058. rotate(2)
  1059. return a
  1060. end
  1061.  
  1062. function detect(side)
  1063. if side=="top" then return turtle.detectUp() end
  1064. if side=="bottom" then return turtle.detectDown() end
  1065. if side=="left" then return detectLeft() end
  1066. if side=="right" then return detectRight() end
  1067. if side=="back" then return detectBack() end
  1068. if side=="front" then return turtle.detect() end
  1069. return false
  1070. end
  1071.  
  1072. function detectDir(f)
  1073. local tmp=getFacing()
  1074. faceSet(f)
  1075. local tmp2=turtle.detect()
  1076. faceSet(tmp)
  1077. return tmp2
  1078. end
  1079.  
  1080. function path(face)
  1081. while true do
  1082. while detectRight() and not turtle.detect() do
  1083. forward(1)
  1084. end
  1085. if detectRight() and turtle.detect() then turnLeft() end
  1086. if not detectRight() then if not forward(1) then turnRight() end elseif not turtle.detectUp() then up() else down() end
  1087. if not detectDir(face) then break end
  1088. end
  1089. end
  1090.  
  1091. function digSlot(slot)
  1092. local s=cDig()
  1093. if s and s~=slot then select(s) turtle.drop() select(slot) else return false end
  1094. return true
  1095. end
  1096.  
  1097. function quarry(w, l)
  1098. local run=true
  1099. while run and isSpace() do
  1100. for i=1, l, 1 do
  1101. while turtle.detect() do dig() end
  1102. turnRight()
  1103. for j=1, w, 1 do
  1104. local s1=digDown() forward(1)
  1105. end
  1106. back(w)
  1107. turnLeft()
  1108. local s2=down()
  1109. end
  1110. if not s1 and not s2 then run=false end
  1111. end
  1112. return true
  1113. end
  1114.  
  1115. function stf(a,b)
  1116. local f=io.open(b,"w")
  1117. for i=1, #a,1 do
  1118. local ai=a[i]
  1119. local tmp2=""
  1120. for j=1, #ai, 1 do
  1121. local tmp=""
  1122. for k=1, #ai[j], 1 do tmp=tmp..ai[j][k] end
  1123. tmp=tmp.." "
  1124. tmp2=tmp2..tmp tmp=""
  1125. end
  1126. f:write(tmp2) f:write("\n")
  1127. end
  1128. f:close()
  1129. return true
  1130. end
  1131.  
  1132. function ctl(a)
  1133. local t={}
  1134. local tt={}
  1135. local ttt={}
  1136. for k=1, #a, 1 do
  1137. tmp2=a[k]
  1138. for i=1, #tmp2, 1 do
  1139. local tmp=tmp2[i]
  1140. for j=1, string.len(tmp), 1 do table.insert(ttt, string.sub(tmp, j,j)) end
  1141. table.insert(tt,ttt) ttt={}
  1142. end
  1143. table.insert(t,tt) tt={}
  1144. end
  1145. return t
  1146. end
  1147.  
  1148. function stt(a)
  1149. local t={}
  1150. local tmp=""
  1151. for i=1, string.len(a), 1 do
  1152. if string.sub(a,i,i)==" " then
  1153. table.insert(t,tmp) tmp=""
  1154. else
  1155. tmp=tmp..string.sub(a,i,i)
  1156. end
  1157. end
  1158. if tmp~="" then table.insert(t,tmp) end
  1159. return t
  1160. end
  1161.  
  1162. function tff(name)
  1163. local tmp=io.open(name,"r")
  1164. local n={}
  1165. for line in tmp:lines() do
  1166. table.insert(n,stt(line))
  1167. end
  1168. return ctl(n)
  1169. end
  1170.  
  1171. function architect(w, h, l)
  1172. local blueprint={}
  1173. local xs, ys, zs=getPos()
  1174. local faces=face
  1175. local row={}
  1176. local layer={}
  1177. for k=1, l, 1 do
  1178. for j=1, h, 1 do
  1179. turnLeft()
  1180. for i=1, w, 1 do
  1181. local tmp=cDig()
  1182. if not tmp then tmp=0 end
  1183. table.insert(row, tmp)
  1184. forward(1)
  1185. end
  1186. for i=1, w, 1 do back(1) end
  1187. turnRight()
  1188. table.insert(layer, row) row={}
  1189. if j<h then
  1190. local tmp=cDigDown()
  1191. if not tmp then tmp=0 end
  1192. table.insert(row, tmp)
  1193. down()
  1194. end
  1195. end
  1196. for j=1, h, 1 do if j<h then up() end end
  1197. local tmp=cDig()
  1198. if not tmp then tmp=0 end
  1199. table.insert(row, tmp)
  1200. forward(1)
  1201. table.insert(blueprint, layer) layer={}
  1202. end
  1203. goToPos(xs, ys, zs, false)
  1204. faceSet(faces)
  1205. return blueprint
  1206. end
  1207.  
  1208. function basicToBlockType(booleanBlock)
  1209. if booleanBlock then
  1210. return 125
  1211. elseif not booleanBlock then
  1212. return 0
  1213. else
  1214. return false
  1215. end
  1216. end
  1217.  
  1218. function mapSides()
  1219. local tmp = getFacing()
  1220. local front,right,back,left,top,bottom = checkSides()
  1221. local xs,ys,zs = getPos()
  1222. setBlock(xs,ys,zs,os.getComputerID()*1000)
  1223. if getFacing() == 2 then
  1224. setBlock(xs,ys,zs-1,basicToBlockType(front))
  1225. setBlock(xs+1,ys,zs,basicToBlockType(right))
  1226. setBlock(xs,ys,zs+1,basicToBlockType(back))
  1227. setBlock(xs-1,ys,zs,basicToBlockType(left))
  1228. setBlock(xs,ys+1,zs,basicToBlockType(top))
  1229. setBlock(xs,ys-1,zs,basicToBlockType(bottom))
  1230. elseif getFacing() == 0 then
  1231. setBlock(xs,ys,zs+1,basicToBlockType(front))
  1232. setBlock(xs-1,ys,zs,basicToBlockType(right))
  1233. setBlock(xs,ys,zs-1,basicToBlockType(back))
  1234. setBlock(xs+1,ys,zs,basicToBlockType(left))
  1235. setBlock(xs,ys+1,zs,basicToBlockType(top))
  1236. setBlock(xs,ys-1,zs,basicToBlockType(bottom))
  1237. elseif getFacing() == 1 then
  1238. setBlock(xs-1,ys,zs,basicToBlockType(front))
  1239. setBlock(xs,ys,zs-1,basicToBlockType(right))
  1240. setBlock(xs+1,ys,zs,basicToBlockType(back))
  1241. setBlock(xs,ys,zs+1,basicToBlockType(left))
  1242. setBlock(xs,ys+1,zs,basicToBlockType(top))
  1243. setBlock(xs,ys-1,zs,basicToBlockType(bottom))
  1244. elseif getFacing() == 3 then
  1245. setBlock(xs+1,ys,zs,basicToBlockType(front))
  1246. setBlock(xs,ys,zs+1,basicToBlockType(right))
  1247. setBlock(xs-1,ys,zs,basicToBlockType(back))
  1248. setBlock(xs,ys,zs-1,basicToBlockType(left))
  1249. setBlock(xs,ys+1,zs,basicToBlockType(top))
  1250. setBlock(xs,ys-1,zs,basicToBlockType(bottom))
  1251. else return false end
  1252. faceSet(tmp)
  1253. return true
  1254. end
  1255.  
  1256. function mapSidesHorizontal()
  1257. local tmp = getFacing()
  1258. local front = turtle.detect() turnRight() local right = turtle.detect() turnRight() local back = turtle.detect() turnRight() local left = turtle.detect() turnRight()
  1259. local xs,ys,zs = getPos()
  1260. setBlock(xs,ys,zs,os.getComputerID()*1000)
  1261. if getFacing() == 2 then
  1262. setBlock(xs,ys,zs-1,basicToBlockType(front))
  1263. setBlock(xs+1,ys,zs,basicToBlockType(right))
  1264. setBlock(xs,ys,zs+1,basicToBlockType(back))
  1265. setBlock(xs-1,ys,zs,basicToBlockType(left))
  1266. elseif getFacing() == 0 then
  1267. setBlock(xs,ys,zs+1,basicToBlockType(front))
  1268. setBlock(xs-1,ys,zs,basicToBlockType(right))
  1269. setBlock(xs,ys,zs-1,basicToBlockType(back))
  1270. setBlock(xs+1,ys,zs,basicToBlockType(left))
  1271. elseif getFacing() == 1 then
  1272. setBlock(xs-1,ys,zs,basicToBlockType(front))
  1273. setBlock(xs,ys,zs-1,basicToBlockType(right))
  1274. setBlock(xs+1,ys,zs,basicToBlockType(back))
  1275. setBlock(xs,ys,zs+1,basicToBlockType(left))
  1276. elseif getFacing() == 3 then
  1277. setBlock(xs+1,ys,zs,basicToBlockType(front))
  1278. setBlock(xs,ys,zs+1,basicToBlockType(right))
  1279. setBlock(xs-1,ys,zs,basicToBlockType(back))
  1280. setBlock(xs,ys,zs-1,basicToBlockType(left))
  1281. else return false end
  1282. faceSet(tmp)
  1283. return true
  1284. end
  1285.  
  1286. function mapSidesVert()
  1287. local tmp = getFacing()
  1288. if getFacing() ~= 0 and getFacing() ~= 2 then faceNorth() end
  1289. local front,top,bottom = turtle.detect(),turtle.detectUp(),turtle.detectDown()
  1290. setBlock(x,y,z,os.getComputerID().."000")
  1291. setBlock(x,y,z-1,basicToBlockType(front))
  1292. setBlock(x,y+1,z,basicToBlockType(top))
  1293. setBlock(x,y-1,z,basicToBlockType(bottom))
  1294. faceSet(tmp)
  1295. return true
  1296. end
  1297.  
  1298. function getBlock(xs,ys,zs)
  1299. local tmp = 126
  1300. if not map[xs..","..ys..","..zs] then
  1301. return tmp
  1302. else
  1303. return map[xs..","..ys..","..zs]
  1304. end end
  1305.  
  1306. function setBlock(xs,ys,zs,type)
  1307. if tonumber(type) == nil then type = getBlockID(type) end
  1308. if tonumber(type) ~= nil then
  1309. local file = io.open("/blockdata","w")
  1310. map[xs..","..ys..","..zs] = type
  1311. file:write(textutils.serialize(map))
  1312. file:close()
  1313. return true
  1314. else return false end
  1315. end
  1316.  
  1317. function compare(slot)
  1318. local done = false
  1319. if tonumber(slot) ~= nil then
  1320. select(slot)
  1321. done = turtle.compare()
  1322. else
  1323. select(getMatch(slot))
  1324. done = turtle.compare()
  1325. end
  1326. return done
  1327. end
  1328.  
  1329. function compareUp(slot)
  1330. local done = false
  1331. if tonumber(slot) ~= nil then
  1332. select(slot)
  1333. done = turtle.compareUp()
  1334. else
  1335. select(getMatch(slot))
  1336. done = turtle.compareUp()
  1337. end
  1338. return done
  1339. end
  1340.  
  1341. function compareDown(slot)
  1342. local done = false
  1343. if tonumber(slot) ~= nil then
  1344. select(slot)
  1345. done = turtle.compareDown()
  1346. else
  1347. select(getMatch(slot))
  1348. done = turtle.compareDown()
  1349. end
  1350. return done
  1351. end
  1352.  
  1353. function stringToBoolean(string) if string == "true" then return true else return false end end
  1354.  
  1355. function pattern(text,pattern,start)
  1356. return string.sub(text,string.find(text,pattern,start)) end
  1357.  
  1358. function compareAll()
  1359. local tmp = ""
  1360. for i=1,9,1 do
  1361. tmp = tmp..toString(compare(i)).." "
  1362. end
  1363. local s1 = pattern(tmp,"%S+",1)
  1364. local s2 = pattern(tmp,"%S+",s1:len()+2)
  1365. local s3 = pattern(tmp,"%S+",s1:len()+s2:len()+3)
  1366. local s4 = pattern(tmp,"%S+",s1:len()+s2:len()+s3:len()+4)
  1367. local s5 = pattern(tmp,"%S+",s1:len()+s2:len()+s3:len()+s4:len()+5)
  1368. local s6 = pattern(tmp,"%S+",s1:len()+s2:len()+s3:len()+s4:len()+s5:len()+6)
  1369. local s7 = pattern(tmp,"%S+",s1:len()+s2:len()+s3:len()+s4:len()+s5:len()+s6:len()+7)
  1370. local s8 = pattern(tmp,"%S+",s1:len()+s2:len()+s3:len()+s4:len()+s5:len()+s6:len()+s7:len()+8)
  1371. local s9 = pattern(tmp,"%S+",s1:len()+s2:len()+s3:len()+s4:len()+s5:len()+s6:len()+s7:len()+s8:len()+9)
  1372. return stringToBoolean(s1),stringToBoolean(s2),stringToBoolean(s3),stringToBoolean(s4),stringToBoolean(s5),stringToBoolean(s6),stringToBoolean(s7),stringToBoolean(s8),stringToBoolean(s9)
  1373. end
  1374.  
  1375. function compareAllUp()
  1376. local tmp = ""
  1377. for i=1,9,1 do
  1378. tmp = tmp..toString(compareUp(i)).." "
  1379. end
  1380. local s1 = pattern(tmp,"%S+",1)
  1381. local s2 = pattern(tmp,"%S+",s1:len()+2)
  1382. local s3 = pattern(tmp,"%S+",s1:len()+s2:len()+3)
  1383. local s4 = pattern(tmp,"%S+",s1:len()+s2:len()+s3:len()+4)
  1384. local s5 = pattern(tmp,"%S+",s1:len()+s2:len()+s3:len()+s4:len()+5)
  1385. local s6 = pattern(tmp,"%S+",s1:len()+s2:len()+s3:len()+s4:len()+s5:len()+6)
  1386. local s7 = pattern(tmp,"%S+",s1:len()+s2:len()+s3:len()+s4:len()+s5:len()+s6:len()+7)
  1387. local s8 = pattern(tmp,"%S+",s1:len()+s2:len()+s3:len()+s4:len()+s5:len()+s6:len()+s7:len()+8)
  1388. local s9 = pattern(tmp,"%S+",s1:len()+s2:len()+s3:len()+s4:len()+s5:len()+s6:len()+s7:len()+s8:len()+9)
  1389. return stringToBoolean(s1),stringToBoolean(s2),stringToBoolean(s3),stringToBoolean(s4),stringToBoolean(s5),stringToBoolean(s6),stringToBoolean(s7),stringToBoolean(s8),stringToBoolean(s9)
  1390. end
  1391.  
  1392. function compareAllDown()
  1393. local tmp = ""
  1394. for i=1,9,1 do
  1395. tmp = tmp..toString(compareDown(i)).." "
  1396. end
  1397. local s1 = pattern(tmp,"%S+",1)
  1398. local s2 = pattern(tmp,"%S+",s1:len()+2)
  1399. local s3 = pattern(tmp,"%S+",s1:len()+s2:len()+3)
  1400. local s4 = pattern(tmp,"%S+",s1:len()+s2:len()+s3:len()+4)
  1401. local s5 = pattern(tmp,"%S+",s1:len()+s2:len()+s3:len()+s4:len()+5)
  1402. local s6 = pattern(tmp,"%S+",s1:len()+s2:len()+s3:len()+s4:len()+s5:len()+6)
  1403. local s7 = pattern(tmp,"%S+",s1:len()+s2:len()+s3:len()+s4:len()+s5:len()+s6:len()+7)
  1404. local s8 = pattern(tmp,"%S+",s1:len()+s2:len()+s3:len()+s4:len()+s5:len()+s6:len()+s7:len()+8)
  1405. local s9 = pattern(tmp,"%S+",s1:len()+s2:len()+s3:len()+s4:len()+s5:len()+s6:len()+s7:len()+s8:len()+9)
  1406. return stringToBoolean(s1),stringToBoolean(s2),stringToBoolean(s3),stringToBoolean(s4),stringToBoolean(s5),stringToBoolean(s6),stringToBoolean(s7),stringToBoolean(s8),stringToBoolean(s9)
  1407. end
  1408.  
  1409. function getMatch(block1)
  1410. local s1 = 1
  1411. if tonumber(block1) == nil then block1 = getBlockID(block1) end
  1412. if getSlot(1) == block1 then s1 = 1 elseif getSlot(2) == block1 then s1 = 2 elseif getSlot(3) == block1 then s1 = 3 elseif getSlot(4) == block1 then s1 = 4 elseif getSlot(5) == block1 then s1 = 5 elseif getSlot(6) == block1 then s1 = 6 elseif getSlot(7) == block1 then s1 = 7 elseif getSlot(8) == block1 then s1 = 8 elseif slot9 == block1 then s1 = 9 else return false end
  1413. return s1 end
  1414.  
  1415. function replace(block1,block2)
  1416. local tmp = getSelection()
  1417. local s1,s2 = 1,1
  1418. local done = false
  1419. if tonumber(block1) ~= nil and tonumber(block2) ~= nil then
  1420. s1 = getMatch(block1)
  1421. s2 = getMatch(block2)
  1422. if compare(s1) then
  1423. dig()
  1424. done = place(s2)
  1425. select(tmp)
  1426. end
  1427. else
  1428. s1 = getMatch(block1)
  1429. s2 = getMatch(block2)
  1430. if compare(s1) then
  1431. dig()
  1432. done = place(s2)
  1433. select(tmp)
  1434. end
  1435. end
  1436. return done
  1437. end
  1438.  
  1439. function replaceUp(block1,block2)
  1440. local tmp = getSelection()
  1441. local s1,s2 = 1,1
  1442. local done = false
  1443. if tonumber(block1) ~= nil and tonumber(block2) ~= nil then
  1444. s1 = getMatch(block1)
  1445. s2 = getMatch(block2)
  1446. if compareUp(s1) then
  1447. digUp()
  1448. done = placeUp(s2)
  1449. select(tmp)
  1450. end
  1451. else
  1452. if compareUp(s1) then
  1453. digUp()
  1454. done = placeUp(s2)
  1455. select(tmp)
  1456. end
  1457. end
  1458. return done
  1459. end
  1460.  
  1461. function replaceDown(block1,block2)
  1462. local tmp = getSelection()
  1463. local s1,s2 = 1,1
  1464. local done = false
  1465. if tonumber(block1) ~= nil and tonumber(block2) ~= nil then
  1466. s1 = getMatch(block1)
  1467. s2 = getMatch(block2)
  1468. if compareDown(s1) then
  1469. digDown()
  1470. done = placeDown(s2)
  1471. select(tmp)
  1472. end
  1473. else
  1474. if compareDown(s1) then
  1475. digDown()
  1476. done = placeDown(s2)
  1477. select(tmp)
  1478. end
  1479. end
  1480. return done
  1481. end
  1482.  
  1483. function dig(block)
  1484. if block == nil then block = false elseif tonumber(block) == nil then block = getBlockID(block) end
  1485. local done = false
  1486. local state = 0
  1487. if not block then state = true else state = compare(getBlockName(block)) end
  1488. if state and turtle.dig() then
  1489. if face == 0 then
  1490. done = setBlock(x,y,z+1,0)
  1491. elseif face == 1 then
  1492. done = setBlock(x-1,y,z,0)
  1493. elseif face == 2 then
  1494. done = setBlock(x,y,z-1,0)
  1495. elseif face == 3 then
  1496. done = setBlock(x+1,y,z,0)
  1497. else done = false end
  1498. else done = false end
  1499. updateSlots()
  1500. return done
  1501. end
  1502.  
  1503. function digUp(block)
  1504. if block == nil then block = false elseif tonumber(block) == nil then block = getBlockID(block) end
  1505. local done = false
  1506. local state = 0
  1507. if not block then state = true else state = compareUp(getBlockName(block)) end
  1508. if state and turtle.digUp() then
  1509. done = setBlock(x,y+1,z,0)
  1510. else done = false end
  1511. updateSlots()
  1512. return done
  1513. end
  1514.  
  1515. function digDown(block)
  1516. if block == nil then block = false elseif tonumber(block) == nil then block = getBlockID(block) end
  1517. local done = false
  1518. local state = 0
  1519. if not block then state = true else state = compareDown(getBlockName(block)) end
  1520. if state and turtle.digDown() then
  1521. done = setBlock(x,y-1,z,0)
  1522. else done = false end
  1523. updateSlots()
  1524. return done
  1525. end
  1526.  
  1527. function getSlotMatch(s1,s2,s3,s4,s5,s6,s7,s8,s9) -- boolean values
  1528. if s1 then return 1 elseif s2 then return 2 elseif s3 then return 3 elseif s4 then return 4 elseif s5 then return 5 elseif s6 then return 6 elseif s7 then return 7 elseif s8 then return 8 elseif s9 then return 9 else return false end end
  1529.  
  1530. function checkSidesType()
  1531. updateSlots()
  1532. local front,right,back,left,top,bottom = 0,0,0,0,0,0
  1533. if turtle.detect() then front = getSlot(getSlotMatch(compareAll())) else front = 0 end if not front then front = 125 end turnRight()
  1534. if turtle.detect() then right = getSlot(getSlotMatch(compareAll())) else right = 0 end if not right then right = 125 end turnRight()
  1535. if turtle.detect() then back = getSlot(getSlotMatch(compareAll())) else back = 0 end if not back then back = 125 end turnRight()
  1536. if turtle.detect() then left = getSlot(getSlotMatch(compareAll())) else left = 0 end if not left then left = 125 end turnRight()
  1537. if turtle.detectUp() then top = getSlot(getSlotMatch(compareAllUp())) else top = 0 end if not top then top = 125 end
  1538. if turtle.detectDown() then bottom = getSlot(getSlotMatch(compareAllDown())) else bottom = 0 end if not bottom then bottom = 125 end
  1539. return front,right,back,left,top,bottom end
  1540.  
  1541. function mapSidesType()
  1542. local tmp = getFacing()
  1543. local front,right,back,left,top,bottom = checkSidesType()
  1544. local xs,ys,zs = getPos()
  1545. setBlock(xs,ys,zs,os.getComputerID().."000")
  1546. if getFacing() == 2 then
  1547. setBlock(xs,ys,zs-1,front)
  1548. setBlock(xs+1,ys,zs,right)
  1549. setBlock(xs,ys,zs+1,back)
  1550. setBlock(xs-1,ys,zs,left)
  1551. setBlock(xs,ys+1,zs,top)
  1552. setBlock(xs,ys-1,zs,bottom)
  1553. elseif getFacing() == 0 then
  1554. setBlock(xs,ys,zs+1,front)
  1555. setBlock(xs-1,ys,zs,right)
  1556. setBlock(xs,ys,zs-1,back)
  1557. setBlock(xs+1,ys,zs,left)
  1558. setBlock(xs,ys+1,zs,top)
  1559. setBlock(xs,ys-1,zs,bottom)
  1560. elseif getFacing() == 1 then
  1561. setBlock(xs-1,ys,zs,front)
  1562. setBlock(xs,ys,zs-1,right)
  1563. setBlock(xs+1,ys,zs,back)
  1564. setBlock(xs,ys,zs+1,left)
  1565. setBlock(xs,ys+1,zs,top)
  1566. setBlock(xs,ys-1,zs,bottom)
  1567. elseif getFacing() == 3 then
  1568. setBlock(xs+1,ys,zs,front)
  1569. setBlock(xs,ys,zs+1,right)
  1570. setBlock(xs-1,ys,zs,back)
  1571. setBlock(xs,ys,zs-1,left)
  1572. setBlock(xs,ys+1,zs,top)
  1573. setBlock(xs,ys-1,zs,bottom)
  1574. else return false end
  1575. faceSet(tmp)
  1576. return true
  1577. end
  1578.  
  1579. function mapSidesTypeHorizontal()
  1580. local tmp = getFacing()
  1581. local front,right,back,left = 126,126,126,126
  1582. if not turtle.detect() then front = getSlot(getSlotMatch(compareAll())) else front = 0 end turnRight()
  1583. if not turtle.detect() then right = getSlot(getSlotMatch(compareAll())) else right = 0 end turnRight()
  1584. if not turtle.detect() then back = getSlot(getSlotMatch(compareAll())) else back = 0 end turnRight()
  1585. if not turtle.detect() then left = getSlot(getSlotMatch(compareAll())) else left = 0 end turnRight()
  1586. local xs,ys,zs = getPos()
  1587. setBlock(xs,ys,zs,os.getComputerID().."000")
  1588. if getFacing() == 2 then
  1589. setBlock(xs,ys,zs-1,front)
  1590. setBlock(xs+1,ys,zs,right)
  1591. setBlock(xs,ys,zs+1,back)
  1592. setBlock(xs-1,ys,zs,left)
  1593. elseif getFacing() == 0 then
  1594. setBlock(xs,ys,zs+1,front)
  1595. setBlock(xs-1,ys,zs,right)
  1596. setBlock(xs,ys,zs-1,back)
  1597. setBlock(xs+1,ys,zs,left)
  1598. elseif getFacing() == 1 then
  1599. setBlock(xs-1,ys,zs,front)
  1600. setBlock(xs,ys,zs-1,right)
  1601. setBlock(xs+1,ys,zs,back)
  1602. setBlock(xs,ys,zs+1,left)
  1603. elseif getFacing() == 3 then
  1604. setBlock(xs+1,ys,zs,front)
  1605. setBlock(xs,ys,zs+1,right)
  1606. setBlock(xs-1,ys,zs,back)
  1607. setBlock(xs,ys,zs-1,left)
  1608. else return false end
  1609. faceSet(tmp)
  1610. return true
  1611. end
  1612.  
  1613. function mapSidesTypeVert()
  1614. local tmp = getFacing()
  1615. if getFacing() ~= 0 and getFacing() ~= 2 then faceNorth() end
  1616. local front,top,bottom = 126,126,126
  1617. if not turtle.detect() then front = 0 else front = getSlot(getSlotMatch(compareAll())) end
  1618. if not turtle.detectUp() then top = 0 else top = getSlot(getSlotMatch(compareAllUp())) end
  1619. if not turtle.detectDown() then bottom = 0 else bottom = getSlot(getSlotMatch(compareAllDown())) end
  1620. setBlock(x,y,z,os.getComputerID().."000")
  1621. if getFacing() == 2 then setBlock(x,y,z-1,front) elseif getFacing() == 0 then setBlock(x,y,z+1,front) else return false end
  1622. setBlock(x,y+1,z,top)
  1623. setBlock(x,y-1,z,bottom)
  1624. faceSet(tmp)
  1625. return true
  1626. end
  1627.  
  1628. function getRow(type,xs,ys,zs,xe,ye,ze)
  1629. if tonumber(type) == nil then
  1630. type = getBlockID(type)
  1631. elseif tonumber(type) ~= nil then
  1632. else return false end
  1633. local dir = 6
  1634. local blocks = 0
  1635. dir = getDirection(xs,ys,zs,xe,ye,ze)
  1636. if dir == 0 then zs = zs + 1 elseif dir == 1 then xs = xs - 1 elseif dir == 2 then zs = zs - 1 elseif dir == 3 then xs = xs + 1 elseif dir == 4 then ys = ys - 1 elseif dir == 5 then ys = ys + 1 elseif dir == 6 then return false else return false end
  1637. if not map[xs..","..ys..","..zs] then return false end
  1638. local temp = getBlock(xs,ys,zs)
  1639. local tmp = tonumber(pattern(temp,"%d+",1))
  1640. local stop = false
  1641. while not stop do
  1642. if tmp == type then
  1643. blocks = blocks + 1
  1644. if dir == 0 then
  1645. temp = getBlock(xs,ys,zs+1)
  1646. if ze > zs then
  1647. tmp = tonumber(pattern(temp,"%d+",1)) --patterns are used incase block save format ever changes.
  1648. zs = zs + 1
  1649. else stop = true end
  1650. elseif dir == 1 then
  1651. temp = getBlock(xs-1,ys,zs)
  1652. if xe < xs then
  1653. tmp = tonumber(pattern(temp,"%d+",1))
  1654. xs = xs - 1
  1655. else stop = true end
  1656. elseif dir == 2 then
  1657. temp = getBlock(xs,ys,zs-1)
  1658. if ze < zs then
  1659. tmp = tonumber(pattern(temp,"%d+",1))
  1660. zs = zs - 1
  1661. else stop = true end
  1662. elseif dir == 3 then
  1663. temp = getBlock(xs+1,ys,zs)
  1664. if xe > xs then
  1665. tmp = tonumber(pattern(temp,"%d+",1))
  1666. xs = xs + 1
  1667. else stop = true end
  1668. elseif dir == 4 then
  1669. temp = getBlock(xs,ys-1,zs)
  1670. if ys > ye then
  1671. tmp = tonumber(pattern(temp,"%d+",1))
  1672. ys = ys - 1
  1673. else stop = true end
  1674. elseif dir == 5 then
  1675. temp = getBlock(xs,ys+1,zs)
  1676. if ys > ye then
  1677. tmp = tonumber(pattern(temp,"%d+",1))
  1678. ys = ys + 1
  1679. else stop = true end
  1680. elseif dir == 6 then
  1681. stop = true
  1682. return false
  1683. else return false end
  1684. else stop = true end
  1685. end
  1686. return blocks
  1687. end
  1688.  
  1689. function getBorderingBlocks(xs,ys,zs)
  1690. local north,east,south,west,up,down = 125,125,125,125,125,125
  1691. north = getBlock(xs,ys,zs-1)
  1692. east = getBlock(xs+1,ys,zs)
  1693. south = getBlock(xs,ys,zs+1)
  1694. west = getBlock(xs-1,ys,zs)
  1695. up = getBlock(xs,ys+1,zs)
  1696. down = getBlock(xs,ys-1,zs)
  1697. return north,east,south,west,up,down
  1698. end
  1699.  
  1700. function getBlockTypeDirection(type,s1,s2,s3,s4,s5,s6) --for finding blocks after a getBorderingBlocks()
  1701. local returnPack = ""
  1702. type = getBlockID(type)
  1703. if s1 == type then returnPack = returnPack.."2 " end
  1704. if s2 == type then returnPack = returnPack.."3 " end
  1705. if s3 == type then returnPack = returnPack.."0 " end
  1706. if s4 == type then returnPack = returnPack.."1 " end
  1707. if s5 == type then returnPack = returnPack.."5 " end
  1708. if s6 == type then returnPack = returnPack.."4 " end
  1709. if returnPack == "" then
  1710. returnPack = returnPack.."6 "
  1711. end
  1712. returnPack = returnPack.."|"
  1713. local q1,q2,q3,q4,q5,q6 = nil,nil,nil,nil,nil,nil
  1714. q1 = pattern(returnPack,"%S+",1)
  1715. if q1 ~= "|" and q1 ~= nil then q2 = pattern(returnPack,"%S+",q1:len()+2) end
  1716. if q2 ~= "|" and q2 ~= nil then q3 = pattern(returnPack,"%S+",q1:len()+q2:len()+3) end
  1717. if q3 ~= "|" and q3 ~= nil then q4 = pattern(returnPack,"%S+",q1:len()+q2:len()+q3:len()+4) end
  1718. if q4 ~= "|" and q4 ~= nil then q5 = pattern(returnPack,"%S+",q1:len()+q2:len()+q3:len()+q4:len()+5) end
  1719. if q5 ~= "|" and q5 ~= nil then q6 = pattern(returnPack,"%S+",q1:len()+q2:len()+q3:len()+q4:len()+q5:len()+6) end
  1720. if q1 then q1 = tonumber(q1) end if q2 then q2 = tonumber(q2) end if q3 then q3 = tonumber(q3) end if q4 then q4 = tonumber(q4) end if q5 then q5 = tonumber(q5) end if q6 then q6 = tonumber(q6) end
  1721. return q1,q2,q3,q4,q5,q6
  1722. end
  1723.  
  1724. function goDirection(dir,spaces)
  1725. local done = false
  1726. if dir == 0 or dir == 1 or dir == 2 or dir == 3 then faceSet(dir) done = forward(spaces)
  1727. elseif dir == 5 then for i=1,spaces,1 do done = up() end
  1728. elseif dir == 4 then for i=1,spaces,1 do done = down() end end
  1729. return done
  1730. end
  1731.  
  1732. function getCoordCount(coord,number)
  1733. local count = 0
  1734. local xo,yo,zo = false,false,false
  1735. local xs,ys,zs = 0,0,0
  1736. if coord:lower() == "x" then xo = true
  1737. elseif coord:lower() == "y" then yo = true
  1738. elseif coord:lower() == "z" then zo = true
  1739. else return false end
  1740. for k,v in pairs(map) do
  1741. xs = pattern(k,"[^,]+",1)
  1742. ys = pattern(k,"[^,]+",xs:len()+2)
  1743. zs = pattern(k,"[^,]+",xs:len()+ys:len()+3)
  1744. xs = tonumber(xs) ys = tonumber(ys) zs = tonumber(zs)
  1745. if xo and xs == number then count = count + 1
  1746. elseif yo and ys == number then count = count + 1
  1747. elseif zo and zs == number then count = count + 1 end
  1748. end
  1749. return count
  1750. end
  1751.  
  1752. function blockToText(blockID)
  1753. blockID = tonumber(blockID)
  1754. if blockID == 0 then return " "
  1755. elseif blockID == 126 then return "~"
  1756. elseif blockID/1000 == math.floor(blockID/1000) then return "T"
  1757. elseif blockID == 3 or blockID == 12 or blockID == 13 or blockID == 18 then return "#"
  1758. elseif blockID == 50 then return "i"
  1759. else return "@" end end
  1760.  
  1761. function coordRow(xs,ys,zs)
  1762. local returnPack = ""
  1763. local count = 0
  1764. local done = false
  1765. zs = zs + 3
  1766. while not done do
  1767. returnPack = returnPack..blockToText(getBlock(xs,ys,zs))
  1768. zs = zs - 1
  1769. if getBlock(xs,ys,zs) == 126 then count = count+1 end
  1770. if count >= 15 then done = true end
  1771. end
  1772. return returnPack
  1773. end
  1774.  
  1775. function getFullRow(xs,ys,zs,xe,ye,ze)
  1776. local tmp = ""
  1777. local done = false
  1778. local dir = getDirection(xs,ys,zs,xe,ye,ze)
  1779. while not done do
  1780. tmp = tmp..getBlock(xs,ys,zs)..","
  1781. if dir == 0 then zs = zs + 1 if zs > ze then done = true end
  1782. elseif dir == 1 then xs = xs - 1 if xs < xe then done = true end
  1783. elseif dir == 2 then zs = zs - 1 if zs < ze then done = true end
  1784. elseif dir == 3 then xs = xs + 1 if xs > xe then done = true end
  1785. elseif dir == 4 then ys = ys - 1 if ys < ye then done = true end
  1786. elseif dir == 5 then ys = ys + 1 if ys > ye then done = true end
  1787. else return false end
  1788. end
  1789. tmp = tmp.."|"
  1790. return tmp
  1791. end
  1792.  
  1793. function getDirection(xs,ys,zs,xe,ye,ze)
  1794. local xf,yf,zf = xe-xs,ye-ys,ze-zs
  1795. local ax,ay,az = math.abs(xf),math.abs(yf),math.abs(zf)
  1796. if xs == xe and ys == ye and zs == ze then return 6 end
  1797. if xs == xe and zs == ze then if yf>=0 then return 5 else return 4 end end
  1798. if xs == xe and ys == ye then if zf >= 0 then return 0 else return 2 end end
  1799. if ys == ye and zs == ze then if xf >= 0 then return 3 else return 1 end end
  1800. if max(ax,ay,az) == ax then if xf >= 0 then return 3 else return 1 end
  1801. elseif max(ax,ay,az) == az then if zf >= 0 then return 0 else return 2 end
  1802. elseif max(ax,ay,az) == ay then if yf>=0 then return 5 else return 4 end
  1803. else return 6 end
  1804. end
  1805.  
  1806. function max(x,...)
  1807. local max=x
  1808. local args={...}
  1809. for i=1,#args,1 do
  1810. if args[i]>max then
  1811. max=args[i]
  1812. end
  1813. end
  1814. return max
  1815. end
  1816.  
  1817. function maxTable(x)
  1818. local max = x[1]
  1819. for i=1,#x,1 do
  1820. if x[i]>max then
  1821. max=x[i]
  1822. end
  1823. end
  1824. return max
  1825. end
  1826.  
  1827. function maxTableTable(x) -- returns the POSITION of max value, NOT the max value!
  1828. local tmp = maxTable(x)
  1829. local max = {}
  1830. for k,v in pairs(x) do
  1831. if v == tmp then table.insert(max,#max+1,k) end
  1832. end
  1833. return max
  1834. end
  1835.  
  1836. function min(x,...)
  1837. local min=x
  1838. local args={...}
  1839. for i=1,#args,1 do
  1840. if args[i]<min then
  1841. min=args[i]
  1842. end
  1843. end
  1844. return min
  1845. end
  1846.  
  1847. function minTable(x)
  1848. local min=x[1]
  1849. for i=1,#x,1 do
  1850. if x[i]<min then
  1851. min=x[i]
  1852. end
  1853. end
  1854. return min
  1855. end
  1856.  
  1857. function minTableTable(x) -- returns the POSITION of min value, NOT the min value!
  1858. local tmp = minTable(x)
  1859. local min = {}
  1860. for k,v in pairs(x) do
  1861. if v == tmp then table.insert(min,#min+1,k) end
  1862. end
  1863. return min
  1864. end
  1865.  
  1866. function getSeperated(x) -- for numbers in string format, returns a table.
  1867. local sep = {}
  1868. local done = false
  1869. local count,prevVars,tmp = 0,0,0
  1870. x = x..""
  1871. while not done do
  1872. count = count + 1
  1873. tmp = pattern(x,"[^,]+",count+prevVars)
  1874. if tmp == "|" then done = true return sep end
  1875. prevVars = prevVars + tmp:len()
  1876. table.insert(sep,count,tonumber(tmp))
  1877. end
  1878. return sep
  1879. end
  1880.  
  1881. function drop(a)
  1882. if tonumber(a) ~= nil then
  1883. select(a)
  1884. turtle.drop()
  1885. updateSlots()
  1886. else
  1887. select(getMatch(a))
  1888. turtle.drop()
  1889. updateSlots()
  1890. end
  1891. end
  1892.  
  1893. function graph(xs,ys,zs,width,height,mode)
  1894. local curX,curZ = 1,1
  1895. if width == nil and height == nil then width,height = term.getSize()
  1896. else height = height + 1 end
  1897. width = width + 1
  1898. local xw,zw = math.floor(width/2),math.floor(height/2)
  1899. local fullRow = 0
  1900. local fullRowY = 0
  1901. local tmp = {}
  1902. local temp = {}
  1903. local tempp = 0
  1904. if mode == nil then mode = false end
  1905. term.clear()
  1906. term.setCursorPos(1,1)
  1907. for i=1,height-1,1 do
  1908. fullRow = getFullRow(xs-xw+curX,ys,zs-zw+curZ,xs+xw,ys,zs+zw)
  1909. if mode then fullRowY = getFullRow(xs-xw+curX,ys-1,zs-zw+curZ,xs+xw,ys,zs+zw) end
  1910. for i=1,width-1,1 do
  1911. tmp = getSeperated(fullRow)
  1912. tempp = blockToText(tmp[i])
  1913. if mode then temp = getSeperated(fullRowY)
  1914. if tempp == " " and blockToText(temp[i]) ~= " " and blockToText(temp[i]) ~= "~" then tempp = "=" end end
  1915. term.write(tempp)
  1916. end
  1917. if curZ <= height then
  1918. curZ = curZ + 1
  1919. term.setCursorPos(1,curZ)
  1920. else break end
  1921. end
  1922. end
  1923.  
  1924. function getBlockInDir(xs,ys,zs,dir)
  1925. if dir == 0 then return getBlock(xs,ys,zs+1)
  1926. elseif dir == 1 then return getBlock(xs-1,ys,zs)
  1927. elseif dir == 2 then return getBlock(xs,ys,zs-1)
  1928. elseif dir == 3 then return getBlock(xs+1,ys,zs)
  1929. elseif dir == 4 then return getBlock(xs,ys-1,zs)
  1930. elseif dir == 5 then return getBlock(xs,ys+1,zs)
  1931. elseif dir == 6 then return getBlock(xs,ys,zs)
  1932. else return false end
  1933. end
  1934.  
  1935. function getCoordsInDir(xs,ys,zs,dir,spaces)
  1936. if spaces == nil then spaces = 1 end
  1937. if dir == 0 then return xs,ys,zs+spaces
  1938. elseif dir == 1 then return xs-spaces,ys,zs
  1939. elseif dir == 2 then return xs,ys,zs-spaces
  1940. elseif dir == 3 then return xs+spaces,ys,zs
  1941. elseif dir == 4 then return xs,ys-spaces,zs
  1942. elseif dir == 5 then return xs,ys+spaces,zs
  1943. elseif dir == 6 then return xs,ys,zs
  1944. else return false end
  1945. end
  1946.  
  1947. function goToCoordsPath(xs,ys,zs)
  1948. local xf,yf,zf = x-xs,y-ys,z-zs
  1949. if xf > 0 then faceWest() return forward(1)
  1950. elseif xf < 0 then faceEast() return forward(1)
  1951. elseif zf > 0 then faceNorth() return forward(1)
  1952. elseif zf < 0 then faceSouth() return forward(1)
  1953. elseif yf > 0 then return down()
  1954. elseif yf < 0 then return up()
  1955. end
  1956. return false
  1957. end
  1958.  
  1959. function pathfindDijkstra(xe,ye,ze)
  1960. local xs,ys,zs = getPos()
  1961. if xe == xs and ye == ys and ze == zs then return true end
  1962. if getBlock(xe,ye,ze,true) ~= 0 then print("You can only pathfind to air.") return false end
  1963. local count,solutionID = 1,0
  1964. local done = false
  1965. local vertices = {}
  1966. local cooldown = 0
  1967. local takenPos = {xs..","..ys..","..zs}
  1968. local q = 0
  1969. vertices[1] = Vertex:new()
  1970. vertices[1]:setID(1)
  1971. vertices[1]:setPos(xs,ys,zs)
  1972. while not done do
  1973. for i=1,#vertices,1 do
  1974. if vertices[i]:isActive() then
  1975. for ii=0,5,1 do
  1976. cooldown = cooldown + 1
  1977. if cooldown >= 50 then sleep(0) cooldown = 0 end
  1978. count = count + 1
  1979. vertices[count] = Vertex:new()
  1980. vertices[count]:setID(count)
  1981. xs,ys,zs = vertices[i]:getPos()
  1982. vertices[count]:setPos(getCoordsInDir(xs,ys,zs,ii))
  1983. for q=1,#takenPos,1 do
  1984. if vertices[count]:getPosString() == takenPos[q] then
  1985. vertices[count]:setActive(false)
  1986. end
  1987. end
  1988. table.insert(takenPos,#takenPos+1,vertices[count]:getPosString())
  1989. if getBlock(vertices[count]:getPos()) ~= 0 then
  1990. vertices[count]:setActive(false)
  1991. elseif vertices[count]:isActive() then
  1992. for iii=1,#vertices[i]:getPath(),1 do
  1993. vertices[count]:addPath(vertices[i]:getPath()[iii])
  1994. end
  1995. vertices[count]:addPath(vertices[count]:getPosString())
  1996. end
  1997. end
  1998. vertices[i]:setActive(false)
  1999. end
  2000. end
  2001. for i=1,#vertices,1 do
  2002. if vertices[i]:getPosString() == xe..","..ye..","..ze then
  2003. solutionID = vertices[i]:getID()
  2004. done = true
  2005. break
  2006. end
  2007. end
  2008. end
  2009. for k,v in ipairs(vertices[solutionID]:getPath()) do
  2010. q = getSeperated(v..",|")
  2011. xs,ys,zs = q[1],q[2],q[3]
  2012. if not goToCoordsPath(xs,ys,zs) then return false end
  2013. end
  2014. return done
  2015. end
  2016.  
  2017. function getCube(xs,ys,zs,radius,type)
  2018. local cube = {}
  2019. local xf,yf,zf,xe,ye,ze = xs-radius,ys-radius,zs-radius,xs+radius,ys+radius,zs+radius
  2020. local block = 125
  2021. local cooldown = 0
  2022. if type then
  2023. if tonumber(type) == nil then type = getBlockID(type) end
  2024. for i=math.floor((math.abs(yf-ye))/-2),math.floor((math.abs(yf-ye))/2),1 do
  2025. for ii=math.floor((math.abs(zf-ze))/-2),math.floor((math.abs(zf-ze))/2),1 do
  2026. for iii=math.floor((math.abs(xf-xe))/-2),math.floor((math.abs(xf-xe))/2),1 do
  2027. block = getBlock(xs+iii,ys+i,zs+ii)
  2028. cooldown = cooldown + 1 if cooldown >=50 then sleep(0.01) cooldown = 0 end
  2029. if block == type then table.insert(cube,#cube+1,(xs+iii)..","..(ys+i)..","..(zs+ii)) end
  2030. end
  2031. end
  2032. end
  2033. else
  2034. for i=math.floor((math.abs(yf-ye))/-2),math.floor((math.abs(yf-ye))/2),1 do
  2035. for ii=math.floor((math.abs(zf-ze))/-2),math.floor((math.abs(zf-ze))/2),1 do
  2036. for iii=math.floor((math.abs(xf-xe))/-2),math.floor((math.abs(xf-xe))/2),1 do
  2037. cooldown = cooldown + 1 if cooldown >=50 then sleep(0.01) cooldown = 0 end
  2038. table.insert(cube,#cube+1,(xs+iii)..","..(ys+i)..","..(zs+ii))
  2039. end
  2040. end
  2041. end
  2042. end
  2043. return cube
  2044. end
  2045.  
  2046. function setCuboid(xs,ys,zs,xe,ye,ze)
  2047. local xb,yb,zb
  2048. xb = min(xs,xe)
  2049. yb = max(ys,ye)
  2050. zb = min(zs,ze)
  2051. return xb,yb,zb
  2052. end
  2053.  
  2054. function getCubeSel(xs,ys,zs,xe,ye,ze,type,axis)
  2055. if not axis then axis = 1 end
  2056. local cube = {}
  2057. local xf,yf,zf = math.abs(xs-xe),math.abs(ys-ye),math.abs(zs-ze)
  2058. local cooldown = 0
  2059. local block = 125
  2060. local xb,yb,zb = setCuboid(xs,ys,zs,xe,ye,ze)
  2061. if type then
  2062. if tonumber(type) == nil then type = getBlockID(type) end
  2063. if axis == 1 then
  2064. for i=0,math.abs(ys-ye),1 do
  2065. for ii=0,math.abs(zs-ze),1 do
  2066. for iii=0,math.abs(xs-xe),1 do
  2067. block = getBlock(xb+iii,yb-i,zb+ii)
  2068. cooldown = cooldown + 1 if cooldown >=50 then sleep(0.01) cooldown = 0 end
  2069. if block == type then table.insert(cube,#cube+1,(xb+iii)..","..(yb-i)..","..(zb+ii)) end
  2070. end
  2071. end
  2072. end
  2073. else
  2074. for i=math.abs(ys-ye),0,-1 do
  2075. for ii=math.abs(zs-ze),0,-1 do
  2076. for iii=math.abs(xs-xe),0,-1 do
  2077. block = getBlock(xb+iii,yb-i,zb+ii)
  2078. cooldown = cooldown + 1 if cooldown >=50 then sleep(0.01) cooldown = 0 end
  2079. if block == type then table.insert(cube,#cube+1,(xb+iii)..","..(yb-i)..","..(zb+ii)) end
  2080. end
  2081. end
  2082. end
  2083. end
  2084. else
  2085. if axis == 1 then
  2086. for i=0,math.abs(ys-ye),1 do
  2087. for ii=0,math.abs(zs-ze),1 do
  2088. for iii=0,math.abs(xs-xe),1 do
  2089. cooldown = cooldown + 1 if cooldown >=50 then sleep(0.01) cooldown = 0 end
  2090. table.insert(cube,#cube+1,(xb+iii)..","..(yb-i)..","..(zb+ii))
  2091. end
  2092. end
  2093. end
  2094. else
  2095. for i=math.abs(ys-ye),0,-1 do
  2096. for ii=math.abs(zs-ze),0,-1 do
  2097. for iii=math.abs(xs-xe),0,-1 do
  2098. cooldown = cooldown + 1 if cooldown >=50 then sleep(0.01) cooldown = 0 end
  2099. table.insert(cube,#cube+1,(xb+iii)..","..(yb-i)..","..(zb+ii))
  2100. end
  2101. end
  2102. end
  2103. end
  2104. end
  2105. return cube
  2106. end
  2107.  
  2108. function dist(xs,ys,zs,xe,ye,ze)
  2109. local d1,d2,d3 = xs-xe,ys-ye,zs-ze
  2110. return math.sqrt(d1^2+d2^2+d3^2)
  2111. end
  2112.  
  2113. function getClosest(xs,ys,zs,max,type) --Returns a table containing the closest matching coordinates.
  2114. if not max then max = 5 end
  2115. if max > 20 then max = 20 print("Too high! Maximum value is 20.") end
  2116. if not type then type = 126 end
  2117. if tonumber(type) == nil then type = getBlockID(type) end
  2118. local min = 0
  2119. local tmp = {}
  2120. local q,w = {},{}
  2121. local xe,ye,ze = 0,0,0
  2122. local cooldown = 0
  2123. if max >= 8 then print("This may take a little while, because the radius was "..max..".") end
  2124. q=getCube(xs,ys,zs,max,type)
  2125. for i=1,#q,1 do
  2126. w = getSeperated(q[i]..",|")
  2127. xe,ye,ze = w[1],w[2],w[3]
  2128. table.insert(tmp,#tmp+1,dist(xs,ys,zs,xe,ye,ze))
  2129. cooldown = cooldown + 1 if cooldown >= 80 then sleep(0.01) cooldown = 0 end
  2130. end
  2131. w = {}
  2132. min = minTable(tmp)
  2133. for k,v in ipairs(tmp) do
  2134. if v == min then
  2135. table.insert(w,#w+1,q[k])
  2136. end
  2137. cooldown = cooldown + 1 if cooldown >= 80 then sleep(0.01) cooldown = 0 end
  2138. end
  2139. return w
  2140. end
  2141.  
  2142. function exploreType(radius,goback,mode)
  2143. mapSidesType()
  2144. if goback == nil then goback = true end
  2145. if mode == nil then mode = true end
  2146. local xs,ys,zs = getPos()
  2147. local q,w
  2148. local xe,ye,ze
  2149. local a
  2150. local done = false
  2151. local loops = 1
  2152. q = getCube(xs,ys,zs,radius,126)
  2153. local tmp = #q
  2154. if #q == 0 then exploreProgress(1,1,4) return true end
  2155. for i=1,2,1 do
  2156. while not done do
  2157. w = getSeperated(q[loops]..",|")
  2158. xe,ye,ze = w[1],w[2],w[3]
  2159. a = getBlockTypeDirection(air,getBorderingBlocks(xe,ye,ze))
  2160. if a and a ~= 6 then
  2161. pathfind(getCoordsInDir(xe,ye,ze,a))
  2162. mapSidesType()
  2163. q = getCube(xs,ys,zs,radius,126)
  2164. tmp = #q
  2165. end
  2166. loops = loops + 1
  2167. if loops > tmp-1 then done = true end
  2168. if mode and not done then
  2169. if i == 0 then
  2170. exploreProgress(loops,tmp,i)
  2171. else
  2172. exploreProgress(loops,tmp,i)
  2173. end
  2174. end
  2175. end
  2176. done = false
  2177. q = getCube(xs,ys,zs,radius,126)
  2178. tmp = #q
  2179. loops = 1
  2180. end
  2181. while not done do --Safety loop, gets any missed spots at the cost of being slower. That's why we did a fast loop before, to clear out most of the unmapped blocks
  2182. w = getSeperated(q[loops]..",|")
  2183. xe,ye,ze = w[1],w[2],w[3]
  2184. a = getBlockTypeDirection(air,getBorderingBlocks(xe,ye,ze))
  2185. if a and a ~= 6 then
  2186. pathfind(getCoordsInDir(xe,ye,ze,a))
  2187. mapSidesType()
  2188. end
  2189. loops = loops + 1
  2190. if loops > tmp then done = true end
  2191. if mode and not done then
  2192. exploreProgress(loops,tmp,3)
  2193. end
  2194. end
  2195. exploreProgress(loops-1,tmp,4)
  2196. if goback then pathfind(xs,ys,zs) end
  2197. return done
  2198. end
  2199.  
  2200. function explore(radius,goback,mode)
  2201. mapSides()
  2202. if goback == nil then goback = true end
  2203. if mode == nil then mode = true end
  2204. local xs,ys,zs = getPos()
  2205. local q,w
  2206. local xe,ye,ze
  2207. local a
  2208. local done = false
  2209. local loops = 1
  2210. q = getCube(xs,ys,zs,radius,126)
  2211. local tmp = #q
  2212. if #q == 0 then exploreProgress(1,1,4) return true end
  2213. for i=1,2,1 do
  2214. while not done do
  2215. w = getSeperated(q[loops]..",|")
  2216. xe,ye,ze = w[1],w[2],w[3]
  2217. a = getBlockTypeDirection(air,getBorderingBlocks(xe,ye,ze))
  2218. if a and a ~= 6 then
  2219. pathfind(getCoordsInDir(xe,ye,ze,a))
  2220. mapSides()
  2221. q = getCube(xs,ys,zs,radius,126)
  2222. tmp = #q
  2223. end
  2224. loops = loops + 1
  2225. if loops > tmp then done = true end
  2226. if mode and not done then
  2227. if i == 0 then
  2228. exploreProgress(loops,tmp,i)
  2229. else
  2230. exploreProgress(loops,tmp,i)
  2231. end
  2232. end
  2233. end
  2234. done = false
  2235. q = getCube(xs,ys,zs,radius,126)
  2236. tmp = #q
  2237. loops = 1
  2238. end
  2239. while not done do --Safety loop, gets any missed spots at the cost of being slower. That's why we did a fast loop before, to clear out most of the unmapped blocks
  2240. w = getSeperated(q[loops]..",|")
  2241. xe,ye,ze = w[1],w[2],w[3]
  2242. a = getBlockTypeDirection(air,getBorderingBlocks(xe,ye,ze))
  2243. if a and a ~= 6 then
  2244. pathfind(getCoordsInDir(xe,ye,ze,a))
  2245. mapSides()
  2246. end
  2247. loops = loops + 1
  2248. if loops > tmp then done = true end
  2249. if mode and not done then
  2250. exploreProgress(loops,tmp,3)
  2251. end
  2252. end
  2253. exploreProgress(loops-1,tmp,4)
  2254. if goback then pathfind(xs,ys,zs) end
  2255. return done
  2256. end
  2257.  
  2258. function exploreSel(xq,yq,zq,xw,yw,zw,goback,mode)
  2259. print("Warning: exploreSel() is in beta testing, expect an occasional bug.")
  2260. mapSides()
  2261. if goback == nil then goback = true end
  2262. if mode == nil then mode = true end
  2263. local xs,ys,zs = getPos()
  2264. local q,w
  2265. local xe,ye,ze
  2266. local t1,t2,t3
  2267. local a
  2268. local done = false
  2269. local loops = 1
  2270. q = getCubeSel(xq,yq,zq,xw,yw,zw,126)
  2271. local tmp = #q
  2272. if #q == 0 then exploreProgress(1,1,4) return true end
  2273. for i=1,2,1 do
  2274. while not done do
  2275. w = getSeperated(q[loops]..",|")
  2276. xe,ye,ze = w[1],w[2],w[3]
  2277. a = getBlockTypeDirection(air,getBorderingBlocks(xe,ye,ze))
  2278. if a and a ~= 6 then
  2279. t1,t2,t3 = getCoordsInDir(xe,ye,ze,a)
  2280. pathfind(t1,t2,t3,10)
  2281. mapSides()
  2282. q = getCubeSel(xq,yq,zq,xw,yw,zw,126,i)
  2283. tmp = #q
  2284. end
  2285. loops = loops + 1
  2286. if loops > tmp then done = true end
  2287. if mode and not done then
  2288. if i == 0 then
  2289. exploreProgress(loops,tmp,i)
  2290. else
  2291. exploreProgress(loops,tmp,i)
  2292. end
  2293. end
  2294. end
  2295. done = false
  2296. q = getCubeSel(xq,yq,zq,xw,yw,zw,126)
  2297. tmp = #q
  2298. loops = 1
  2299. end
  2300. while not done do --Safety loop, gets any missed spots at the cost of being slower. That's why we did a fast loop before, to clear out most of the unmapped blocks
  2301. w = getSeperated(q[loops]..",|")
  2302. xe,ye,ze = w[1],w[2],w[3]
  2303. a = getBlockTypeDirection(air,getBorderingBlocks(xe,ye,ze))
  2304. if a and a ~= 6 then
  2305. t1,t2,t3 = getCoordsInDir(xe,ye,ze,a)
  2306. pathfind(t1,t2,t3,10)
  2307. mapSides()
  2308. end
  2309. loops = loops + 1
  2310. if loops > tmp then done = true end
  2311. if mode and not done then
  2312. exploreProgress(loops,tmp,3)
  2313. end
  2314. end
  2315. exploreProgress(loops-1,tmp,4)
  2316. if goback then pathfind(xs,ys,zs) end
  2317. return done
  2318. end
  2319.  
  2320. function exploreProgress(loops,tmp,stage)
  2321. if not stage then stage = 1 end
  2322. local extra = 10*(stage-1)
  2323. local width,height = term.getSize()
  2324. term.clear()
  2325. term.setCursorPos(1,math.floor(height/2)-3)
  2326. term.write("{[")
  2327. local i = 0
  2328. if stage < 4 then
  2329. while i < math.floor((loops/tmp)*10)+extra do
  2330. i = i + 1
  2331. term.write("=")
  2332. end
  2333. i = 0
  2334. else term.write("==============================") end
  2335. if stage < 4 then
  2336. while i < 30-math.floor((loops/tmp)*10)-extra do
  2337. i = i + 1
  2338. term.write(" ")
  2339. end
  2340. end
  2341. term.write("]}")
  2342. term.setCursorPos(math.floor(width/2)-7,math.floor(height/2)-2)
  2343. if stage < 4 then term.write("|"..math.floor((loops/tmp)*33+(extra*3.3)).."% complete|") else term.write("|100% complete|") end
  2344. term.setCursorPos(1,math.floor(height/2)-1)
  2345. term.write(loops.." of "..tmp.." possible blocks")
  2346. term.setCursorPos(1,math.floor(height/2))
  2347. term.write("explored.")
  2348. term.setCursorPos(1,math.floor(height/2)+1)
  2349. if stage < 3 then term.write("Stage "..stage.." of 3.") else term.write("Stage 3 of 3.") end
  2350. term.setCursorPos(1,math.floor(height/2)+2)
  2351. term.write("Do not ctrl^T, shutdown, or exit")
  2352. term.setCursorPos(1,math.floor(height/2)+3)
  2353. term.write("the game while exploring, or else")
  2354. term.setCursorPos(1,math.floor(height/2)+4)
  2355. term.write("you may experience data corruption.")
  2356. term.setCursorPos(1,height)
  2357. end
  2358.  
  2359. function exploreTypeSel(xq,yq,zq,xw,yw,zw,goback,mode)
  2360. print("Warning: exploreTypeSel() is in beta testing, expect an occasional bug.")
  2361. mapSidesType()
  2362. if goback == nil then goback = true end
  2363. if mode == nil then mode = true end
  2364. local xs,ys,zs = getPos()
  2365. local q,w
  2366. local xe,ye,ze
  2367. local a
  2368. local done = false
  2369. local loops = 1
  2370. q = getCubeSel(xq,yq,zq,xw,yw,zw,126)
  2371. local tmp = #q
  2372. if #q == 0 then exploreProgress(1,1,4) return true end
  2373. for i=1,2,1 do
  2374. while not done do
  2375. w = getSeperated(q[loops]..",|")
  2376. xe,ye,ze = w[1],w[2],w[3]
  2377. a = getBlockTypeDirection(air,getBorderingBlocks(xe,ye,ze))
  2378. if a and a ~= 6 then
  2379. t1,t2,t3 = getCoordsInDir(xe,ye,ze,a)
  2380. pathfind(t1,t2,t3,10)
  2381. mapSidesType()
  2382. q = getCubeSel(xq,yq,zq,xw,yw,zw,126,i)
  2383. tmp = #q
  2384. end
  2385. loops = loops + 1
  2386. if loops > tmp then done = true end
  2387. if mode and not done then
  2388. if i == 0 then
  2389. exploreProgress(loops,tmp,i)
  2390. else
  2391. exploreProgress(loops,tmp,i)
  2392. end
  2393. end
  2394. end
  2395. done = false
  2396. q = getCubeSel(xq,yq,zq,xw,yw,zw,126)
  2397. tmp = #q
  2398. loops = 1
  2399. end
  2400. while not done do --Safety loop, gets any missed spots at the cost of being slower. That's why we did a fast loop before, to clear out most of the unmapped blocks
  2401. w = getSeperated(q[loops]..",|")
  2402. xe,ye,ze = w[1],w[2],w[3]
  2403. a = getBlockTypeDirection(air,getBorderingBlocks(xe,ye,ze))
  2404. if a and a ~= 6 then
  2405. t1,t2,t3 = getCoordsInDir(xe,ye,ze,a)
  2406. pathfind(t1,t2,t3,10)
  2407. mapSidesType()
  2408. end
  2409. loops = loops + 1
  2410. if loops > tmp then done = true end
  2411. if mode and not done then
  2412. exploreProgress(loops,tmp,3)
  2413. end
  2414. end
  2415. exploreProgress(loops-1,tmp,4)
  2416. if goback then pathfind(xs,ys,zs) end
  2417. return done
  2418. end
  2419.  
  2420. function pathfind(xe,ye,ze,tc,up,s,t)
  2421. local ue
  2422. if not tc then ue = false else ue = true end
  2423. if not tc then tc = 10 else tc = math.abs(tc) end
  2424. if not up then up = false end
  2425. if not s then s = 0 end
  2426. if not t then t = false else t = true end
  2427. local xs,ys,zs = getPos()
  2428. waypoints = getWaypoints(xs,ys,zs,xe,ye,ze,ue,up)
  2429. local tmp,done,breaking
  2430. local tries = 0
  2431. prev = waypoints
  2432. local cooldown = 0
  2433. local _,__,___
  2434. while not done do
  2435. tries = tries + 1
  2436. tmp = 0
  2437. for i=1,min(#prev,#waypoints),1 do
  2438. if prev[i]:getPos() ~= waypoints[i]:getPos() then breaking = i else breaking = -1 end
  2439. end
  2440. if not breaking then breaking = -1 end
  2441. if breaking > -1 then _,__,___ = waypoints[breaking - 1]:getPos() pathfind(_,__,___,tc) end
  2442. for k,v in pairs(waypoints) do
  2443. cooldown = cooldown + 1 if cooldown >= 100 then cooldown = 0 sleep(0) end
  2444. if (k >= breaking or breaking == -1) and not goToCoordsPath(v:getPos()) and k ~= 1 then
  2445. tmp = -1
  2446. done = false
  2447. break
  2448. end
  2449. end
  2450. prev = waypoints
  2451. if tmp ~= -1 then
  2452. goToCoordsPath(xe,ye,ze)
  2453. done = true
  2454. else
  2455. if s < 1 then
  2456. if t then mapSidesType() else mapSides() end
  2457. if tries >= tc then tries = 0 xs,ys,zs = getPos() end
  2458. waypoints = getWaypoints(xs,ys,zs,xe,ye,ze,ue,up)
  2459. else
  2460. explore(s)
  2461. if tries >= tc then tries = 0 xs,ys,zs = getPos() end
  2462. waypoints = getWaypoints(xs,ys,zs,xe,ye,ze,ue,up)
  2463. end
  2464. end
  2465. end
  2466. return true
  2467. end
  2468.  
  2469. function getWaypoints(xs,ys,zs,xe,ye,ze,ue,up)
  2470. local returnpack = {}
  2471. local done = false
  2472. local xp,yp,zp = xs,ys,zs
  2473. local penalty = 0
  2474. if xs == xe and ys == ye and zs == ze then return {xs..","..ys..","..zs} end
  2475. local open = {}
  2476. nodes = {} nodeF = {}
  2477. local closest = nil
  2478. if s and getBlock(xe,ye,ze) == 126 then
  2479. closest = getClosest(xe,ye,ze,5,air)
  2480. if #closest ~= 0 then
  2481. local w = getSeperated(closest[1]..",|")
  2482. pathfind(w[1],w[2],w[3])
  2483. end
  2484. end
  2485. local tmp,temp
  2486. local cooldown = 0
  2487. open[1] = Node:new(xs,ys,zs,xe,ye,ze,1,false,true)
  2488. table.sort(nodeF)
  2489. table.sort(open, function(a,b) return a.f<b.f end)
  2490. while not done do
  2491. for ii=0,5,1 do
  2492. tmp = nil
  2493. temp = false
  2494. cooldown = cooldown + 1 if cooldown > 50 then sleep(0) cooldown = 0 end
  2495. xs,ys,zs = open[1]:getPos()
  2496. if (getBlockInDir(xs,ys,zs,ii) == 0 or (ue and getBlockInDir(xs,ys,zs,ii) == 126)) and temp ~= true then
  2497. if getBlockInDir(xs,ys,zs,ii) == 126 then penalty = up end
  2498. xs,ys,zs = getCoordsInDir(xs,ys,zs,ii)
  2499. for iii=1,#open,1 do if open[iii]:getPosString() == xs..","..ys..","..zs then tmp = true temp = open[iii] end end
  2500. if not tmp then
  2501. open[#open+1] = Node:new(xs,ys,zs,xe,ye,ze,#nodes+1,open[1].ID,false,penalty)
  2502. if open[#open]:getPosString() == xe..","..ye..","..ze then done = open[#open] end
  2503. elseif open[1].g > temp.g + 10 then
  2504. open[1].pid = temp.pid
  2505. tmp = getSeperated(open[1].pid..",|")
  2506. open[1].g = nodes[tmp[#tmp]].g + 10
  2507. open[1].f = open[1].g + open[1].h
  2508. nodeF[open[1].ID] = open[1].f
  2509. table.sort(nodeF)
  2510. table.sort(open, function(a,b) return a.f<b.f end)
  2511. end
  2512. end
  2513. penalty = 0
  2514. end
  2515. if temp == true then xs,ys,zs = xp,yp,zp open[1] = Node:new(xp,yp,zp,xe,ye,ze,1,false,true) break end
  2516. nodeF[open[1].ID] = math.huge
  2517. table.remove(open,1)
  2518. table.sort(nodeF)
  2519. table.sort(open, function(a,b) return a.f<b.f end)
  2520. if #open == 0 then done = false end
  2521. end
  2522. table.sort(nodeF)
  2523. table.sort(open, function(a,b) return a.f<b.f end)
  2524. if done then
  2525. tmp = getSeperated(done.pid.."|")
  2526. for i=1,#tmp,1 do
  2527. table.insert(returnpack,nodes[tmp[i]])
  2528. end
  2529. nodes = {} nodeF = {}
  2530. return returnpack
  2531. else nodes = {} nodeF = {} return {} end
  2532. end
  2533.  
  2534. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement