EdgeLordKirito

FuelHub test

May 28th, 2023 (edited)
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.43 KB | None | 0 0
  1. -- Slot1 = Fuel
  2. -- Slot2 = Light
  3. -- Slot3 = Blocks
  4.  
  5. --Globals Start
  6. FUELVALUE = 0
  7. Facing = 0
  8. MASTERID = 0
  9. ACTIVE = true
  10. ChestX,ChestY,ChestZ = 0,0,0
  11. FuelX,FuelY,FuelZ = 0,0,0
  12. SavedX,SavedY,SavedZ = 0,0,0
  13. --Globals End
  14. function main()
  15. OpenRednetSpecificForDevice()
  16. os.setComputerLabel("Slave")
  17. local x,y,z = gps.locate()
  18. SetFuelValue(1)
  19. SetFuelCoords(-261,63,560)
  20. SetChestCoords(-262,63,560)
  21. SetSavedCoords(x,y,z)
  22. HomeBase()
  23. print("Programm terminated with Code 0 " .. tostring(os.epoch("utc")))
  24. end
  25.  
  26.  
  27. -- Important Functions Start
  28. function Retreat()
  29. -- navigate back to saved location
  30. PathfindToCoordinate(ChestX,ChestY,ChestZ)
  31. end
  32.  
  33. function Terminate()
  34. end
  35.  
  36. -- Important Functions End
  37. -- Rotation Logic Start
  38. -- facing = 0 means North Neg Z
  39. -- facing = 1 means East Pos X
  40. -- facing = 2 means South Pos Z
  41. -- facing = 3 means West Neg x
  42. function RotateToNegZ()
  43. TurnUntilFacingMatches(0)
  44. end
  45.  
  46. function RotateToPosX()
  47. TurnUntilFacingMatches(1)
  48. end
  49.  
  50. function RotateToPosZ()
  51. TurnUntilFacingMatches(2)
  52. end
  53.  
  54. function RotateToNegX()
  55. TurnUntilFacingMatches(3)
  56. end
  57.  
  58. function TurnUntilFacingMatches(facing)
  59. if Facing == facing then
  60. return true
  61. end
  62. for i = 0, 3 do
  63. TurnRight()
  64. if Facing == facing then
  65. return true
  66. end
  67. end
  68. end
  69.  
  70. function TurnRight()
  71. turtle.turnRight()
  72. Facing = math.fmod(Facing + 1,4)
  73. end
  74.  
  75. function RotateUnitlFacingId(input)
  76. for i = 0, 3 do
  77. if FacingId(input) then
  78. return true
  79. else
  80. TurnRight()
  81. end
  82. end
  83. end
  84.  
  85. function FacingId(input)
  86. local succes, data = turtle.inspect()
  87. if succes then
  88. if data.name == input then
  89. return true
  90. else
  91. return false
  92. end
  93. else
  94. return false
  95. end
  96. end
  97. -- Rotation Logic End
  98.  
  99. -- Movement Logic Start
  100. function MoveForwardByOne()
  101. local existence = turtle.inspect()
  102. if existence then
  103. turtle.dig()
  104. end
  105. turtle.forward()
  106. end
  107.  
  108. function MoveForwardByXBlocks(x)
  109. for i = 1, x do
  110. MoveForwardByOne()
  111. end
  112. end
  113.  
  114. function MoveUpwardByOne()
  115. local existence = turtle.inspectUp()
  116. if existence then
  117. turtle.digUp()
  118. end
  119. turtle.up()
  120. end
  121.  
  122. function MoveUpwardByXBlocks(x)
  123. for i = 1, x do
  124. MoveUpwardByOne()
  125. end
  126. end
  127.  
  128. function MoveDownwardByOne()
  129. local existence = turtle.inspectDown()
  130. if existence then
  131. turtle.digDown()
  132. end
  133. turtle.down()
  134. end
  135.  
  136. function MoveDownwardByXBlocks(x)
  137. for i = 1, x do
  138. MoveDownwardByOne()
  139. end
  140. end
  141.  
  142. function MoveNonDestructiveForwardByOne()
  143. local existence = turtle.inspect()
  144. if existence then
  145. return true
  146. end
  147. turtle.forward()
  148. end
  149.  
  150. function MoveNonDestructiveForwardByXBlocks(x)
  151. for i = 1, x do
  152. local block = MoveNonDestructiveForwardByOne()
  153. if block then
  154. SendMessage("Blocked at the Front")
  155. end
  156. end
  157. end
  158.  
  159. function MoveNonDestructiveUpByOne()
  160. local existence = turtle.inspectUp()
  161. if existence then
  162. return true
  163. end
  164. turtle.up()
  165. end
  166.  
  167. function MoveNonDestructiveUpByXBlocks(x)
  168. for i = 1, x do
  169. local block = MoveNonDestructiveForwardByOne()
  170. if block then
  171. SendMessage("Blocked at the Top")
  172. end
  173. end
  174. end
  175.  
  176. function MoveNonDestructiveDownbyOne()
  177. local existence = turtle.inspectDown()
  178. if existence then
  179. return true
  180. end
  181. turtle.down()
  182. end
  183.  
  184. function MoveNonDestructiveDownByXBlocks(x)
  185. for i = 1, x do
  186. local block = MoveNonDestructiveForwardByOne()
  187. if block then
  188. SendMessage("Blocked at the Bottom")
  189. end
  190. end
  191. end
  192. -- Movement Logic End
  193.  
  194. -- Pathfinding Logic Start
  195. function PathfindToCoordinate(destinationX,destinationY,destinationZ)
  196. local currentX,currentY,currentZ = gps.locate()
  197. local xDistance = CalculateDistance(destinationX,currentX)
  198. local yDistance = CalculateDistance(destinationY,currentY)
  199. local zDistance = CalculateDistance(destinationZ,currentZ)
  200. local totalDistance = CalculateTotalDistance(xDistance,yDistance,zDistance)
  201. local totalFuel = turtle.getFuelLevel() + turtle.getItemCount(1) * FUELVALUE
  202. if totalFuel < totalDistance then
  203. --Send Distress Signal To Low Fuel for Retreat
  204. SendMessage("Urgent Message Incoming")
  205. local message = ("%d %d %d"):format(currentX,currentY,currentZ)
  206. SendMessage(message)
  207. else
  208. CorrectYAxis(destinationY,currentY,yDistance)
  209. CorrectXAxis(destinationX,currentX,xDistance)
  210. CorrectZAxis(destinationZ,currentZ,zDistance)
  211. if CompareCoordinate(destinationX,destinationY,destinationZ) then
  212. print("Location has been Successfully reached")
  213. else
  214. print("Location has not been reached")
  215. end
  216. end
  217. end
  218.  
  219. function PathfindToCoordinateNonDestructive(destinationX,destinationY,destinationZ)
  220. local currentX,currentY,currentZ = gps.locate()
  221. local xDistance = CalculateDistance(destinationX,currentX)
  222. local yDistance = CalculateDistance(destinationY,currentY)
  223. local zDistance = CalculateDistance(destinationZ,currentZ)
  224. local totalDistance = CalculateTotalDistance(xDistance,yDistance,zDistance)
  225. local totalFuel = turtle.getFuelLevel() + turtle.getItemCount(1) * FUELVALUE
  226. if totalFuel < totalDistance then
  227. --Send Distress Signal To Low Fuel for Retreat
  228. SendMessage("Urgent Message Incoming")
  229. local message = ("%d %d %d"):format(currentX,currentY,currentZ)
  230. SendMessage(message)
  231. else
  232. CorrectYAxisNonDestructive(destinationY,currentY,yDistance)
  233. CorrectXAxisNonDestructive(destinationX,currentX,xDistance)
  234. CorrectZAxisNondestructive(destinationZ,currentZ,zDistance)
  235. if CompareCoordinate(destinationX,destinationY,destinationZ) then
  236. print("Location has been Successfully reached")
  237. else
  238. print("Location has not been reached")
  239. end
  240. end
  241. end
  242.  
  243. function CompareCoordinate(destinationX,destinationY,destinationZ)
  244. local currentX,currentY,currentZ = gps.locate()
  245. if currentX == destinationX and currentY == destinationY and currentZ == destinationZ then
  246. return true
  247. else
  248. return false
  249. end
  250. end
  251.  
  252. function CorrectXAxis(destinationX,currentX,xDistance)
  253. local comparison = CompareTo(currentX,destinationX)
  254. local xDistance = xDistance
  255. if comparison == 1 then
  256. RotateToNegX()
  257. MoveForwardByXBlocks(xDistance)
  258. end
  259. if comparison == -1 then
  260. RotateToPosX()
  261. MoveForwardByXBlocks(xDistance)
  262. end
  263. end
  264.  
  265. function CorrectXAxisNonDestructive(destinationX,currentX,xDistance)
  266. local comparison = CompareTo(currentX,destinationX)
  267. local xDistance = xDistance
  268. if comparison == 1 then
  269. RotateToNegX()
  270. MoveNonDestructiveForwardByXBlocks(xDistance)
  271. end
  272. if comparison == -1 then
  273. RotateToPosX()
  274. MoveNonDestructiveForwardByXBlocks(xDistance)
  275. end
  276. end
  277.  
  278. function CorrectZAxis(destinationZ,currentZ,zDistance)
  279. local comparison = CompareTo(currentZ,destinationZ)
  280. local zDistance = zDistance
  281. if comparison == 1 then
  282. RotateToNegZ()
  283. MoveForwardByXBlocks(zDistance)
  284. end
  285. if comparison == -1 then
  286. RotateToPosZ()
  287. MoveForwardByXBlocks(zDistance)
  288. end
  289. end
  290.  
  291. function CorrectZAxisNondestructive(destinationZ,currentZ,zDistance)
  292. local comparison = CompareTo(currentZ,destinationZ)
  293. local zDistance = zDistance
  294. if comparison == 1 then
  295. RotateToNegZ()
  296. MoveNonDestructiveForwardByXBlocks(zDistance)
  297. end
  298. if comparison == -1 then
  299. RotateToPosZ()
  300. MoveNonDestructiveForwardByXBlocks(zDistance)
  301. end
  302. end
  303.  
  304. function CorrectYAxis(destinationY,currentY,yDistance)
  305. local comparison = CompareTo(currentY,destinationY)
  306. local yDistance = yDistance
  307. if comparison == 1 then
  308. MoveDownwardByXBlocks(yDistance)
  309. end
  310. if comparison == -1 then
  311. MoveUpwardByXBlocks(yDistance)
  312. end
  313. end
  314.  
  315. function CorrectYAxisNonDestructive(destinationY,currentY,yDistance)
  316. local comparison = CompareTo(currentY,destinationY)
  317. local yDistance = yDistance
  318. if comparison == 1 then
  319. MoveNonDestructiveDownByXBlocks(yDistance)
  320. end
  321. if comparison == -1 then
  322. MoveNonDestructiveUpByXBlocks(yDistance)
  323. end
  324. end
  325. -- Pathfinding Logic End
  326.  
  327. -- Mining Logic Start
  328.  
  329. -- Mining Logic End
  330.  
  331. -- Math Logic Start
  332. function CompareTo(num1,num2)
  333. if num1 > num2 then
  334. return 1
  335. else if num1 < num2 then
  336. return -1
  337. else if num1 == num2 then
  338. return 0
  339. end
  340. end
  341. end
  342. end
  343.  
  344. function CalculateDistance(value1,value2)
  345. return math.abs(value1 - value2)
  346. end
  347.  
  348. function CalculateTotalDistance(xDistance,yDistance,zDistance)
  349. local result = xDistance + yDistance + zDistance
  350. return result
  351. end
  352. --Math Logic End
  353.  
  354. -- Send and Receive Logic Start
  355. function EnterWaitModeAndReturnMessage()
  356. local id,message,protocol = rednet.receive()
  357. return id,message,protocol
  358. end
  359.  
  360. function SendImportantMessage()
  361. SendMessage("IMPORTANT")
  362. end
  363.  
  364. function SendMessage(input)
  365. rednet.send(MASTERID,input)
  366. PrintSysMessage(("%s Message has been sent to Computer %d"):format(input,MASTERID) .. tostring(os.epoch("utc")))
  367. end
  368.  
  369. function EvaluateResponse(id,str)
  370. local message = string.upper(str)
  371. if (message == "GO")
  372. then
  373. print(("Computer %d sent message %s"):format(id, message) .. tostring(os.epoch("utc")))
  374. Mine()
  375. end
  376.  
  377. if (message == "RETREAT")
  378. then
  379. print(("Computer %d sent message %s"):format(id, message) .. tostring(os.epoch("utc")))
  380.  
  381. end
  382.  
  383. if(message == "TERMINATE")
  384. then
  385. print(("Computer %d sent message %s"):format(id, message) .. tostring(os.epoch("utc")))
  386. Terminate()
  387. end
  388. end
  389. -- Send and Receive Logic End
  390.  
  391. -- Terminal Logic Start
  392. function PrintStringInColor(string,color)
  393. term.setTextColor(color)
  394. print(string)
  395. ResetTerminal()
  396. end
  397.  
  398. function PrintSysMessage(string)
  399. PrintStringInColor(string,colors.magenta)
  400. end
  401.  
  402. function ResetTerminal()
  403. term.setTextColor(colors.white)
  404. end
  405. -- Terminal Logic End
  406.  
  407. -- Turtle Functions Start
  408. function UnloadInventory()
  409. local success,data = turtle.inspect()
  410. if success then
  411. if data.name == "minecraft:chest" or "minecraft:ender_chest" then
  412. for i = 1, 16 do
  413. turtle.select(i)
  414. turtle.drop()
  415. end
  416. end
  417. end
  418. end
  419.  
  420. function FuelUp()
  421. local success,data = turtle.inspect()
  422. if success then
  423. turtle.select(1)
  424. turtle.suck()
  425. end
  426. end
  427.  
  428. function SetFuelValue(input)
  429. if input == 1 then
  430. FUELVALUE = 80
  431. end
  432. if input == 2 then
  433. FUELVALUE = 800
  434. end
  435. end
  436.  
  437. function CheckForBedrockBeneath()
  438. local success,data = turtle.inspectDown()
  439. if success then
  440. if data.name == "minecraft:bedrock" then
  441. return true
  442. else
  443. return false
  444. end
  445. else
  446. return false
  447. end
  448. end
  449.  
  450. function Refuel()
  451. local success = false
  452. if turtle.getItemCount(1) > 0 then
  453. turtle.select(1)
  454. success = turtle.refuel(1)
  455. end
  456. return success
  457. end
  458. -- Turtle Functions End
  459.  
  460. -- HomeBase Operations Start
  461. function ArrivedAtFuelHub()
  462. if CompareCoordinate(FuelX,FuelY,FuelZ) then
  463. return true
  464. else
  465. return false
  466. end
  467. end
  468.  
  469. function RefuelAtFuelHub()
  470. RotateUnitlFacingId("functionalstorage:oak_1")
  471. FuelUp()
  472. end
  473.  
  474. function MoveToFuelHub()
  475. PathfindToCoordinate(FuelX,FuelY,FuelZ)
  476. if ArrivedAtFuelHub() then
  477. return true
  478. end
  479. end
  480.  
  481. function MoveAwayFromRefuelHub()
  482. PathfindToCoordinateNonDestructive(SavedX,SavedY,SavedZ)
  483. end
  484.  
  485. function RefuelAtTheFuelHub()
  486. MoveToFuelHub()
  487. end
  488.  
  489. function FuelHub()
  490. MoveToFuelHub()
  491. RefuelAtFuelHub()
  492. MoveAwayFromRefuelHub()
  493. end
  494.  
  495. function OutputChest()
  496. MoveToOutputChest()
  497. OutputInventory()
  498. end
  499.  
  500. function MoveToOutputChest()
  501. PathfindToCoordinate(ChestX,ChestY,ChestZ)
  502. RotateUnitlFacingId("minecraft:chest")
  503. end
  504.  
  505. function OutputInventory()
  506. UnloadInventory()
  507. end
  508.  
  509. function SetChestCoords(x,y,z)
  510. ChestX = x
  511. ChestY = y
  512. ChestZ = z
  513. end
  514.  
  515. function SetFuelCoords(x,y,z)
  516. FuelX = x
  517. FuelY = y
  518. FuelZ = z
  519. end
  520.  
  521. function SetSavedCoords(x,y,z)
  522. SavedX = x
  523. SavedY = y
  524. SavedZ = z
  525. end
  526.  
  527. function HomeBase()
  528. OutputChest()
  529. FuelHub()
  530. end
  531. -- Homebase Operations End
  532.  
  533. -- Os Functions Start
  534. function WhatDeviceAmIRunningOn()
  535. return getDeviceType()
  536. end
  537.  
  538. function getDeviceType()
  539. if turtle then
  540. return "turtle"
  541. elseif pocket then
  542. return "pocket"
  543. elseif commands then
  544. return "command_computer"
  545. else
  546. return "computer"
  547. end
  548. end
  549.  
  550. function OpenRednetSpecificForDevice()
  551. local device = WhatDeviceAmIRunningOn()
  552. device = string.upper(device)
  553. if device == "TURTLE"then
  554. rednet.open("left")
  555. else if device == "COMPUTER" or device == "COMMAND_COMPUTER" or device == "PAD" then
  556. rednet.open("back")
  557. end
  558. end
  559. end
  560. -- Os Functions End
  561.  
  562. main()
Advertisement
Add Comment
Please, Sign In to add comment