Advertisement
remie92

turtleLib

Feb 29th, 2024 (edited)
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.62 KB | None | 0 0
  1. local func = {}
  2.  
  3. minFuel=1000
  4.  
  5. func.ores = {
  6. "minecraft:iron_ore",
  7. "minecraft:coal_ore",
  8. "minecraft:diamond_ore",
  9. "minecraft:gold_ore",
  10. "minecraft:lapis_ore",
  11. "minecraft:redstone_ore",
  12. "minecraft:quartz_ore"
  13. }
  14. world={}
  15.  
  16. function func.inventoryOver()
  17. local totalSpaceOver=0
  18. for i=1,16 do
  19. totalSpaceOver=totalSpaceOver+turtle.getItemSpace(i)
  20. end
  21. return totalSpaceOver
  22. end
  23.  
  24. function func.refreshLoc()
  25. turtle.select(2)
  26. turtle.drop()
  27. turtle.select(1)
  28. shell.run("unequip left")
  29. shell.run("equip 1 left")
  30. xC,yC,zC=gps.locate()
  31. shell.run("unequip left")
  32. shell.run("equip 2 left")
  33. end
  34.  
  35. xC,yC,zC=gps.locate()
  36. func.refreshLoc()
  37. rotC=0
  38. --rot 0 = x+
  39. --rot 1 = z+
  40. --rot 2 = x-
  41. --rot 3 = z-
  42. func.moves=0
  43.  
  44. function func.dropTrashStone()
  45. end
  46.  
  47.  
  48. function func.tableContainsString(tbl, str)
  49. for key, value in pairs(tbl) do
  50. if value == str then
  51. return true
  52. end
  53. end
  54. --print(textutils.serialise(tbl))
  55. --func.saveTableToFile(tbl,"world")
  56. return false
  57. end
  58.  
  59. function func.containsString(table1, table2)
  60. for key1, str1 in pairs(table1) do
  61. for key2, str2 in pairs(table2) do
  62. if tostring(str1) == tostring(str2) then
  63. return true
  64. end
  65. end
  66. end
  67. return false
  68. end
  69.  
  70.  
  71. function func.getFirstKey(tbl)
  72. -- Iterate over the table using next() and return the first key encountered
  73. for key, _ in pairs(tbl) do
  74. return key
  75. end
  76. -- If the table is empty, return nil
  77. return nil
  78. end
  79. function func.getKeyByValue(tbl, searchValue)
  80. for key, value in pairs(tbl) do
  81. if value == searchValue then
  82. return key
  83. end
  84. end
  85. return nil -- Return nil if the value is not found in the table
  86. end
  87.  
  88.  
  89. function func.getFirstKeyWithValueInTable(tbl, valuesTable)
  90. for key, value in pairs(tbl) do
  91. for _, v in ipairs(valuesTable) do
  92. if value == v then
  93. return key
  94. end
  95. end
  96. end
  97. return nil
  98. end
  99.  
  100.  
  101. function func.stringToTable(str)
  102. local tbl = {}
  103. local i = 1
  104. for value in str:gmatch("([^,]+)") do
  105. if i == 1 then
  106. tbl.x = tonumber(value)
  107. elseif i == 2 then
  108. tbl.y = tonumber(value)
  109. elseif i == 3 then
  110. tbl.z = tonumber(value)
  111. else
  112. break -- Only process first three values
  113. end
  114. i = i + 1
  115. end
  116. tbl.rot=0
  117. return tbl
  118. end
  119.  
  120.  
  121. function func.getBlockName()
  122. local frontBlock, frontData = turtle.inspect()
  123. if frontBlock==true then
  124. return frontData.name
  125. end
  126. return "minecraft:air"
  127. end
  128. function func.getBlockNameDown()
  129. local frontBlock, frontData = turtle.inspectDown()
  130. if frontBlock==true then
  131. return frontData.name
  132. end
  133. return "minecraft:air"
  134. end
  135. function func.getBlockNameUp()
  136. local frontBlock, frontData = turtle.inspectUp()
  137. if frontBlock==true then
  138. return frontData.name
  139. end
  140. return "minecraft:air"
  141. end
  142.  
  143.  
  144. function func.scanWorld()
  145. oldRot=rotC
  146. while 0~=rotC do
  147. func.rotate(1)
  148. end
  149. world[(xC+1)..","..yC..","..zC]=tostring(func.getBlockName())
  150. world[xC..","..(yC+1)..","..zC]=tostring(func.getBlockNameUp())
  151. world[xC..","..(yC-1)..","..zC]=tostring(func.getBlockNameDown())
  152. func.rotate(1)
  153. world[xC..","..yC..","..(zC+1)]=tostring(func.getBlockName())
  154. func.rotate(1)
  155. world[(xC-1)..","..yC..","..zC]=tostring(func.getBlockName())
  156. func.rotate(1)
  157. world[xC..","..yC..","..(zC-1)]=tostring(func.getBlockName())
  158. while oldRot~=rotC do
  159. func.rotate(1)
  160. end
  161.  
  162. end
  163.  
  164. function func.mineVeinOre()
  165. oldLoc=func.getLoc()
  166. --print("Starting veinmine "..targetName)
  167. func.scanWorld()
  168.  
  169. --print(textutils.serialise(world))
  170.  
  171. while func.containsString(world,func.ores)==true do
  172. local newVeinCoord=func.stringToTable(func.getFirstKeyWithValueInTable(world,func.ores))
  173. print("Trying to veinmine "..world[func.getFirstKeyWithValueInTable(world,func.ores)])
  174. func.goto(newVeinCoord)
  175. func.scanWorld()
  176. end
  177.  
  178. --print("Stopped veinmining "..targetName)
  179.  
  180. func.goto(oldLoc)
  181. end
  182.  
  183. function func.mineVein(targetName)
  184. oldLoc=func.getLoc()
  185.  
  186. func.scanWorld()
  187. while func.tableContainsString(world,targetName)==true do
  188. local newVeinCoord=func.stringToTable(func.getKeyByValue(world,targetName))
  189. func.goto(newVeinCoord)
  190. func.scanWorld()
  191. end
  192.  
  193. print("Trying to veinmine "..targetName)
  194.  
  195. func.goto(oldLoc)
  196. end
  197.  
  198.  
  199. function func.goto(goal)
  200. print(textutils.serialise(func.getLoc()))
  201. --Get the correct X
  202. if goal.x>xC then
  203. while rotC ~=0 do
  204. func.rotate(1)
  205. end
  206. while goal.x~=xC do
  207. func.forceMove(0)
  208. end
  209. end
  210. if goal.x<xC then
  211. while rotC ~=2 do
  212. func.rotate(1)
  213. end
  214. while goal.x~=xC do
  215. func.forceMove(0)
  216. end
  217. end
  218. --Get the correct Z
  219. if goal.z>zC then
  220. while rotC ~=1 do
  221. func.rotate(1)
  222. end
  223. while goal.z~=zC do
  224. func.forceMove(0)
  225. end
  226. end
  227. if goal.z<zC then
  228. while rotC ~=3 do
  229. func.rotate(1)
  230. end
  231. while goal.z~=zC do
  232. func.forceMove(0)
  233. end
  234. end
  235. --Get the correct Y
  236. if goal.y>yC then
  237. while goal.y~=yC do
  238. func.forceMove(2)
  239. end
  240. end
  241. if goal.y<yC then
  242. while goal.y~=yC do
  243. func.forceMove(3)
  244. end
  245. end
  246. --Get the correct Rot
  247. while goal.rot~=rotC do
  248. func.rotate(1)
  249. end
  250. end
  251.  
  252. function func.getLoc()
  253. local a={}
  254. a.x=xC
  255. a.y=yC
  256. a.z=zC
  257. a.rot=rotC
  258. return a
  259. end
  260.  
  261. function func.testPrint()
  262. print("test")
  263. end
  264.  
  265. function func.autoFuel()
  266. if turtle.getFuelLevel()<minFuel then
  267. func.refuel()
  268. end
  269. end
  270.  
  271. function func.refuel()
  272. for i=1,16 do
  273. turtle.select(i)
  274. turtle.refuel()
  275. end
  276. print("Amount of fuel over: "..turtle.getFuelLevel().."/"..turtle.getFuelLimit())
  277. end
  278.  
  279. --dir 0=forward
  280. --dir 1=back
  281. --dir 2=up
  282. --dir 3=down
  283. function func.move(direction)
  284. if math.fmod(func.moves,10) == 0 then
  285. func.refreshLoc()
  286. end
  287. --print(textutils.serialise(func.getLoc()))
  288. func.moves=func.moves+1
  289. if turtle.getFuelLevel()>0 then
  290. if direction==0 then
  291. if turtle.forward()==true then
  292. if rotC ==0 then
  293. xC=xC+1
  294. end
  295. if rotC ==1 then
  296. zC=zC+1
  297. end
  298. if rotC ==2 then
  299. xC=xC-1
  300. end
  301. if rotC ==3 then
  302. zC=zC-1
  303. end
  304. end
  305. elseif direction==1 then
  306. if turtle.back()==true then
  307. if rotC ==0 then
  308. xC=xC-1
  309. end
  310. if rotC ==1 then
  311. zC=zC-1
  312. end
  313. if rotC ==2 then
  314. xC=xC+1
  315. end
  316. if rotC ==3 then
  317. zC=zC+1
  318. end
  319. end
  320. elseif direction==2 then
  321. if turtle.up()==true then
  322. yC=yC+1
  323. end
  324. elseif direction==3 then
  325. if turtle.down()==true then
  326. yC=yC-1
  327. end
  328. end
  329. end
  330. --print("x: "..xC)
  331. -- print("y: "..yC)
  332. --print("z: "..zC)
  333. --print("Rotation"..rotC)
  334. end
  335. --dir 0=left
  336. --dir 1=right
  337. function func.rotate(direction)
  338. if direction==0 then
  339. turtle.turnLeft()
  340. roC=rotC-1
  341. elseif direction==1 then
  342. turtle.turnRight()
  343. rotC=rotC+1
  344. end
  345. if rotC<0 then
  346. rotC=3
  347. end
  348. if rotC>3 then
  349. rotC=0
  350. end
  351. --print("Rotation"..rotC)
  352. end
  353.  
  354. --Can't force back!
  355. function func.forceMove(direction)
  356.  
  357. if direction==0 then
  358. turtle.dig()
  359. func.move(direction)
  360. world[xC..","..yC..","..zC] = nil
  361. elseif direction==2 then
  362. turtle.digUp()
  363. func.move(direction)
  364. world[xC..","..yC..","..zC] = nil
  365. elseif direction==3 then
  366. turtle.digDown()
  367. func.move(direction)
  368. world[xC..","..yC..","..zC] = nil
  369. end
  370.  
  371. end
  372.  
  373. function func.dropAll()
  374. for i=1,16 do
  375. turtle.select(i)
  376. turtle.drop()
  377. end
  378. end
  379.  
  380. function func.getSlotName(slot)
  381. return (turtle.getItemDetail(slot).name)
  382. end
  383.  
  384. function func.saveTableToFile(tbl, filename)
  385. local file = io.open(filename, "w")
  386. if file then
  387. file:write(func.tableToString(tbl))
  388. file:close()
  389. print("Table saved to " .. filename)
  390. else
  391. print("Error: Unable to open file " .. filename .. " for writing.")
  392. end
  393. end
  394.  
  395. -- Helper function to convert table to string
  396. function func.tableToString(tbl)
  397. local str = "{\n"
  398. for k, v in pairs(tbl) do
  399. if type(k) == "number" then
  400. str = str .. "[" .. k .. "]"
  401. else
  402. str = str .. '["' .. k .. '"]'
  403. end
  404. str = str .. " = "
  405. if type(v) == "table" then
  406. str = str .. tableToString(v)
  407. elseif type(v) == "string" then
  408. str = str .. '"' .. v .. '"'
  409. else
  410. str = str .. tostring(v)
  411. end
  412. str = str .. ",\n"
  413. end
  414. str = str .. "}"
  415. return str
  416. end
  417.  
  418.  
  419.  
  420.  
  421.  
  422. return func
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement