Advertisement
remie92

remLib

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