EdgeLordKirito

Slave Eval

May 30th, 2023 (edited)
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 17.71 KB | None | 0 0
  1. -- Slot1 = Fuel
  2. -- Slot2 = Light
  3. -- Slot3 = Blocks
  4.  
  5. --still error Retreat does not work
  6.  
  7. --Globals Start
  8. FUELVALUE = 0
  9. Facing = 0
  10. MASTERID = 0
  11. ACTIVE = false
  12. StartX,StartY,StartZ = 0,0,0
  13. DISTANCETOHOMEBASE = 0
  14. TOTALFUEL = 0
  15. TotalFuelStart = 0
  16. FUELSLOT = 16
  17. EMERGENCYFUELSLOT = 15
  18. COMMAND = ""
  19. --Globals End
  20. function Main()
  21.     print("Slave Script now Running")
  22.     OpenRednetSpecificForDevice()
  23.     os.setComputerLabel("Slave")
  24.     InitTurtle()
  25.     EvaluateCommand(COMMAND)
  26.     print("Programm terminated with Code 0 " .. tostring(os.epoch("utc")))
  27. end
  28.  
  29. -- Important Functions Start
  30. function Retreat()
  31.     PathfindToCoordinateNonBlocking(StartX,StartY,StartZ)
  32.     ACTIVE = true
  33. end
  34.  
  35. -- Important Functions End
  36. -- Rotation Logic Start
  37.     -- facing = 0 means North Neg Z
  38.     -- facing = 1 means East Pos X
  39.     -- facing = 2 means South Pos Z
  40.     -- facing = 3 means West Neg x
  41. function RotateToNegZ()
  42.     TurnUntilFacingMatches(0)
  43. end
  44.  
  45. function RotateToPosX()
  46.     TurnUntilFacingMatches(1)
  47. end
  48.  
  49. function RotateToPosZ()
  50.     TurnUntilFacingMatches(2)
  51. end
  52.  
  53. function RotateToNegX()
  54.     TurnUntilFacingMatches(3)
  55. end
  56.  
  57. function TurnUntilFacingMatches(facing)
  58.     if Facing == facing then
  59.         return true
  60.     end
  61.     TurnRight()
  62.     if Facing == facing then
  63.         return true
  64.     end
  65.     TurnRight()
  66.     if Facing == facing then
  67.         return true
  68.     end
  69.     TurnRight()
  70.     if Facing == facing then
  71.         return true
  72.     end
  73.     TurnRight()
  74.     if Facing == facing then
  75.         return true
  76.     end
  77.  
  78. end
  79.  
  80. function MatchFacing(facing)
  81.     if Facing == facing then
  82.         return true
  83.     end
  84. end
  85.  
  86. function TurnRight()
  87.     turtle.turnRight()
  88.     Facing = math.fmod(Facing + 1,4)
  89. end
  90.  
  91. function RotateUnitlFacingId(input)
  92.     for i = 0, 3 do
  93.         if FacingId(input) then
  94.             return true
  95.         else
  96.             TurnRight()
  97.         end
  98.     end
  99. end
  100.  
  101. function FacingId(input)
  102.     local succes, data = turtle.inspect()
  103.     if succes then
  104.         if data.name == input then
  105.             return true
  106.         else
  107.             return false
  108.         end
  109.     else
  110.         return false
  111.     end
  112. end
  113.  
  114. function InvertHeading()
  115.     if Facing == 0 then
  116.         RotateToPosZ()
  117.     end
  118.     if Facing == 1 then
  119.         RotateToNegX()
  120.     end
  121.     if Facing == 2 then
  122.         RotateToNegZ()
  123.     end
  124.     if Facing == 3 then
  125.         RotateToPosX()
  126.     end
  127. end
  128. -- Rotation Logic End
  129.  
  130. function CheckRetreatParam()
  131.     if LowFuel() then
  132.         return true
  133.     end
  134.     if InventoryHasfreeSpace() then
  135.         return true
  136.     end
  137.     return false
  138. end
  139.  
  140. -- Movement Logic Start
  141. function MoveForwardByOne()
  142.     CriticallyLowFuelCheck()
  143.     if CheckRetreatParam() then
  144.         local x,y,z = gps.locate()
  145.         SetSavedCoords(x,y,z)
  146.         ReturnHome()
  147.         return
  148.     end
  149.     if CheckForBedrockBeneath() then
  150.         BedrockFound()
  151.     end
  152.     if turtle.getFuelLevel() < 20 then
  153.         Refuel()
  154.     end
  155.     local existence = turtle.inspect()
  156.     if existence then
  157.         turtle.dig()
  158.     end
  159.     UpdateTurtleValues()
  160.     turtle.forward()
  161. end
  162.  
  163. function MoveForwardByXBlocks(x)
  164.     for i = 1, x do
  165.         MoveForwardByOne()
  166.     end
  167. end
  168.  
  169. function MoveUpwardByOne()
  170.     CriticallyLowFuelCheck()
  171.     if CheckRetreatParam() then
  172.         local x,y,z = gps.locate()
  173.         SetSavedCoords(x,y,z)
  174.         ReturnHome()
  175.         return
  176.     end
  177.     if CheckForBedrockBeneath() then
  178.         BedrockFound()
  179.     end
  180.     if turtle.getFuelLevel() < 20 then
  181.         Refuel()
  182.     end
  183.     local existence = turtle.inspectUp()
  184.     if existence then
  185.         turtle.digUp()
  186.     end
  187.     UpdateTurtleValues()
  188.     turtle.up()
  189. end
  190.  
  191. function MoveUpwardByXBlocks(x)
  192.     for i = 1, x do
  193.         MoveUpwardByOne()
  194.     end
  195. end
  196.  
  197. function MoveDownwardByOne()
  198.     CriticallyLowFuelCheck()
  199.     if CheckRetreatParam() then
  200.         local x,y,z = gps.locate()
  201.         SetSavedCoords(x,y,z)
  202.         ReturnHome()
  203.         return
  204.     end
  205.     if CheckForBedrockBeneath() then
  206.         BedrockFound()
  207.     end
  208.     if turtle.getFuelLevel() < 20 then
  209.         Refuel()
  210.     end
  211.     local existence = turtle.inspectDown()
  212.     if existence then
  213.         turtle.digDown()
  214.     end
  215.     UpdateTurtleValues()
  216.     turtle.down()
  217. end
  218.  
  219. function MoveDownwardByXBlocks(x)
  220.     for i = 1, x do
  221.         MoveDownwardByOne()
  222.     end
  223. end
  224. -- Movement Logic End
  225.  
  226. -- Pathfinding Logic Start
  227. function PathfindToCoordinate(destinationX,destinationY,destinationZ)
  228.     local currentX,currentY,currentZ = gps.locate()
  229.     local xDistance = CalculateDistance(destinationX,currentX)
  230.     local yDistance = CalculateDistance(destinationY,currentY)
  231.     local zDistance = CalculateDistance(destinationZ,currentZ)
  232.     local totalDistance = CalculateTotalDistance(xDistance,yDistance,zDistance)
  233.     local totalFuel = CalculateTotalFuel()
  234.     if totalFuel < totalDistance then
  235.         --Send Distress Signal To Low Fuel for Retreat
  236.         os.setComputerLabel("Fuel Critical")
  237.     else
  238.         CorrectYAxis(destinationY,currentY,yDistance)
  239.         CorrectXAxis(destinationX,currentX,xDistance)
  240.         CorrectZAxis(destinationZ,currentZ,zDistance)
  241.         if CompareCoordinate(destinationX,destinationY,destinationZ) then
  242.             print("Location has been Successfully reached")
  243.         else
  244.             print("Location has not been reached")
  245.         end
  246.     end
  247. end
  248.  
  249. function CompareCoordinate(destinationX,destinationY,destinationZ)
  250.     local currentX,currentY,currentZ = gps.locate()
  251.     if currentX == destinationX and currentY == destinationY and currentZ == destinationZ then
  252.         return true
  253.     else
  254.         return false
  255.     end
  256. end
  257.  
  258. function CorrectXAxis(destinationX,currentX,xDistance)
  259.     local comparison = CompareTo(currentX,destinationX)
  260.     local xDistance = xDistance
  261.     if comparison == 1 then
  262.         RotateToNegX()
  263.         MoveForwardByXBlocks(xDistance)
  264.     end
  265.     if comparison == -1 then
  266.         RotateToPosX()
  267.         MoveForwardByXBlocks(xDistance)
  268.     end
  269. end
  270.  
  271. function CorrectZAxis(destinationZ,currentZ,zDistance)
  272.     local comparison = CompareTo(currentZ,destinationZ)
  273.     local zDistance = zDistance
  274.     if comparison == 1 then
  275.         RotateToNegZ()
  276.         MoveForwardByXBlocks(zDistance)
  277.     end
  278.     if comparison == -1 then
  279.         RotateToPosZ()
  280.         MoveForwardByXBlocks(zDistance)
  281.     end
  282. end
  283.  
  284. function CorrectYAxis(destinationY,currentY,yDistance)
  285.     local comparison = CompareTo(currentY,destinationY)
  286.     local yDistance = yDistance
  287.     if comparison == 1 then
  288.         MoveDownwardByXBlocks(yDistance)
  289.     end
  290.     if comparison == -1 then
  291.         MoveUpwardByXBlocks(yDistance)
  292.     end
  293. end
  294.  
  295. -- Pathfinding Logic End
  296.  
  297. -- Non Blocking Pathfind Logic Start
  298. function MoveNonBlockingForwardByOne()
  299.     CriticallyLowFuelCheck()
  300.     if turtle.getFuelLevel() < 20 then
  301.         Refuel()
  302.     end
  303.     local existence = turtle.inspect()
  304.     if existence then
  305.         turtle.dig()
  306.     end
  307.     turtle.forward()
  308.     UpdateTurtleValues()
  309. end
  310.  
  311. function MoveNonBlockingForwardByXBlocks(x)
  312.     for i = 1, x do
  313.         MoveNonBlockingForwardByOne()
  314.     end
  315. end
  316.  
  317. function MoveNonBlockingUpByOne()
  318.     CriticallyLowFuelCheck()
  319.     if turtle.getFuelLevel() < 20 then
  320.         Refuel()
  321.     end
  322.     local existence = turtle.inspectUp()
  323.     if existence then
  324.         turtle.digUp()
  325.     end
  326.     turtle.up()
  327.     UpdateTurtleValues()
  328. end
  329.  
  330. function MoveNonBlockingUpByXBlocks(x)
  331.     for i = 1, x do
  332.         MoveNonBlockingUpByOne()
  333.     end
  334. end
  335.  
  336. function MoveNonBlockingDownbyOne()
  337.     CriticallyLowFuelCheck()
  338.     if turtle.getFuelLevel() < 20 then
  339.         Refuel()
  340.     end
  341.     local existence = turtle.inspectDown()
  342.     if existence then
  343.         turtle.digDown()
  344.     end
  345.     turtle.down()
  346.     UpdateTurtleValues()
  347. end
  348.  
  349. function MoveNonBlockingDownByXBlocks(x)
  350.     for i = 1, x do
  351.         MoveNonBlockingDownbyOne()
  352.     end
  353. end
  354.  
  355. function CorrectYAxisNonBlocking(destinationY,currentY,yDistance)
  356.     local comparison = CompareTo(currentY,destinationY)
  357.     local yDistance = yDistance
  358.     if comparison == 1 then
  359.         MoveNonBlockingDownByXBlocks(yDistance)
  360.     end
  361.     if comparison == -1 then
  362.         MoveNonBlockingUpByXBlocks(yDistance)
  363.     end
  364. end
  365.  
  366. function CorrectZAxisNonBlocking(destinationZ,currentZ,zDistance)
  367.     local comparison = CompareTo(currentZ,destinationZ)
  368.     local zDistance = zDistance
  369.     if comparison == 1 then
  370.         RotateToNegZ()
  371.         MoveNonBlockingForwardByXBlocks(zDistance)
  372.     end
  373.     if comparison == -1 then
  374.         RotateToPosZ()
  375.         MoveNonBlockingForwardByXBlocks(zDistance)
  376.     end
  377. end
  378.  
  379. function CorrectXAxisNonBlocking(destinationX,currentX,xDistance)
  380.     local comparison = CompareTo(currentX,destinationX)
  381.     local xDistance = xDistance
  382.     if comparison == 1 then
  383.         RotateToNegX()
  384.         MoveNonBlockingForwardByXBlocks(xDistance)
  385.     end
  386.     if comparison == -1 then
  387.         RotateToPosX()
  388.         MoveNonBlockingForwardByXBlocks(xDistance)
  389.     end
  390. end
  391.  
  392. function PathfindToCoordinateNonBlocking(destinationX,destinationY,destinationZ)
  393.     local currentX,currentY,currentZ = gps.locate()
  394.     local xDistance = CalculateDistance(destinationX,currentX)
  395.     local yDistance = CalculateDistance(destinationY,currentY)
  396.     local zDistance = CalculateDistance(destinationZ,currentZ)
  397.     local totalDistance = CalculateTotalDistance(xDistance,yDistance,zDistance)
  398.     local totalFuel = CalculateTotalFuel()
  399.     if totalFuel < totalDistance then
  400.     else
  401.         CorrectYAxisNonBlocking(destinationY,currentY,yDistance)
  402.         CorrectXAxisNonBlocking(destinationX,currentX,xDistance)
  403.         CorrectZAxisNonBlocking(destinationZ,currentZ,zDistance)
  404.         if CompareCoordinate(destinationX,destinationY,destinationZ) then
  405.             print("Successfully Reached Coords")
  406.         else
  407.             print("UnSuccessfully Reached Coords")
  408.         end
  409.     end
  410. end
  411. -- Non Blocking PathFind Logic End
  412.  
  413. -- Math Logic Start
  414. function CompareTo(num1,num2)
  415.     if num1 == nil then
  416.         print("num1 is nil")
  417.     end
  418.     if num2 == nil then
  419.         print("num2 is nil")
  420.     end
  421.     local val1 = tonumber(num1)
  422.     local val2 = tonumber(num2)
  423.     if val1 > val2 then
  424.         return 1
  425.     else if val1 < val2 then
  426.         return -1
  427.     else if val1 == val2 then
  428.         return 0
  429.     end
  430.     end
  431.     end
  432. end
  433.  
  434. function CalculateDistance(value1,value2)
  435.     return math.abs(value1 - value2)
  436. end
  437.  
  438. function CalculateTotalDistance(xDistance,yDistance,zDistance)
  439.     local result = xDistance + yDistance + zDistance
  440.     return result
  441. end
  442.  
  443. function CalculateHomebaseDistance(baseX,BaseY,BaseZ)
  444.     local x,y,z = gps.locate()
  445.     local result = CalculateTotalDistance(CalculateDistance(baseX,x),CalculateDistance(BaseY,y),CalculateDistance(BaseZ,z))
  446.     return result
  447. end
  448.  
  449. function CalculateTotalFuel()
  450.     local result = turtle.getFuelLevel() + turtle.getItemCount(FUELSLOT) * FUELVALUE
  451.     return result
  452. end
  453. --Math Logic End
  454.  
  455. -- Send and Receive Logic Start
  456. function EnterWaitModeAndReturnMessage()
  457.     local id,message = rednet.receive()
  458.     return id,message
  459. end
  460.  
  461. function SendImportantMessage()
  462.     SendMessage("IMPORTANT")
  463. end
  464.  
  465. function SendMessage(input)
  466.     rednet.send(MASTERID,input)
  467.     PrintSysMessage(("%s Message has been sent to Computer %d"):format(input,MASTERID) .. tostring(os.epoch("utc")))
  468. end
  469. -- Send and Receive Logic End
  470.  
  471. -- Terminal Logic Start
  472. function PrintStringInColor(string,color)
  473.     term.setTextColor(color)
  474.     print(string)
  475.     ResetTerminal()
  476. end
  477.  
  478. function PrintSysMessage(string)
  479.     PrintStringInColor(string,colors.magenta)
  480. end
  481.  
  482. function ResetTerminal()
  483.     term.setTextColor(colors.white)
  484. end
  485. -- Terminal Logic End
  486.  
  487. -- Turtle Functions Start
  488. function UnloadInventory()
  489.     local success,data = turtle.inspect()
  490.     if success then
  491.         if data.name == "minecraft:chest" or "minecraft:ender_chest" then
  492.             for i = 1, 16 do
  493.                 turtle.select(i)
  494.                 turtle.drop()
  495.             end
  496.         end
  497.     end
  498. end
  499.  
  500. function FuelUp()
  501.     local success,data = turtle.inspect()
  502.     if success then
  503.         turtle.select(FUELSLOT)
  504.         turtle.suck()
  505.         turtle.select(EMERGENCYFUELSLOT) --Emergency FuelSlot
  506.         turtle.suck()
  507.         turtle.select(1)
  508.     end
  509. end
  510.  
  511. function SetFuelValue(input)
  512.     FUELVALUE = input
  513. end
  514.  
  515. function CheckForBedrockBeneath()
  516.     local success,data = turtle.inspectDown()
  517.     if success then
  518.         if data.name == "minecraft:bedrock" then
  519.             return true
  520.         else
  521.             return false
  522.         end
  523.     else
  524.         return false
  525.     end
  526. end
  527.  
  528. function Refuel()
  529.     local success = false
  530.     if turtle.getItemCount(FUELSLOT) > 0 then
  531.             turtle.select(FUELSLOT)
  532.             success = turtle.refuel(1)
  533.             turtle.select(1)
  534.     end
  535.     return success
  536. end
  537.  
  538. function EmergencyRefuel()
  539.     local success = false
  540.     if turtle.getItemCount(EMERGENCYFUELSLOT) > 0 then
  541.             turtle.select(EMERGENCYFUELSLOT)
  542.             success = turtle.refuel()
  543.             turtle.select(1)
  544.     end
  545.     return success
  546. end
  547.  
  548. function LowFuel()
  549.     if DISTANCETOHOMEBASE == TOTALFUEL then
  550.         print(("Distance equals Totalfuel %d"):format(TOTALFUEL))
  551.         return true
  552.     end
  553.  
  554.     if DISTANCETOHOMEBASE < TOTALFUEL then
  555.         print("I can Still reach Home")
  556.         return false
  557.     end
  558.  
  559.     if DISTANCETOHOMEBASE > TOTALFUEL then
  560.         print("Home is to faraway")
  561.         print("Using Emergency Fuel")
  562.         EmergencyRefuel()
  563.         return true
  564.     end
  565. end
  566.  
  567.  
  568. function InventoryHasfreeSpace()
  569.     local count = turtle.getItemCount(14)
  570.     if count == 0 then
  571.         return false
  572.     else
  573.         return true
  574.     end
  575. end
  576.  
  577. function InitTurtle()
  578.     print("initializing Turtle")
  579.     local id,fuelType = EnterWaitModeAndReturnMessage()
  580.     print(fuelType)
  581.     MASTERID = id
  582.     local currentX,currentY,currentZ = gps.locate()
  583.     EvaluateFuelType(fuelType)
  584.     SetStartCoords(currentX,currentY,currentZ)
  585.     Refuel()
  586.     UpdateTurtleValues()
  587.     GetCommand()
  588. end
  589.  
  590. function GetCommand()
  591.     local id, message = EnterWaitModeAndReturnMessage()
  592.     COMMAND = message
  593. end
  594.  
  595. function EvaluateCommand(input)
  596.     if ACTIVE then
  597.         return
  598.     end
  599.     print("Evaluating Command")
  600.     local command = MySplit(input)
  601.     if command[1] == "line" then
  602.         print("Mining at Height")
  603.         ExecuteMineCommand(tonumber(command[2]),tonumber(command[3]))
  604.     end
  605. end
  606.  
  607. function EvaluateFuelType(input)
  608.     if input == "coal" then
  609.         SetFuelValue(80)
  610.     else if input == "coal_block" then
  611.         SetFuelValue(800)
  612.     end
  613.     end
  614.  end
  615.  
  616.  function CriticallyLowFuelCheck()
  617.     if turtle.getFuelLevel() == 0 then
  618.         os.setComputerLabel("Critical Fuel level")
  619.         --os.shutdown()
  620.     end
  621.  end
  622.  
  623.  function BedrockFound()
  624.     os.setComputerLabel("Bedrock Found")
  625.     PathfindToCoordinateNonBlocking(StartX,StartY,StartZ)
  626.     RotateToNegZ()
  627.     os.shutdown()
  628.  end
  629.  
  630.  function UpdateTotalFuel()
  631.     TOTALFUEL = CalculateTotalFuel()
  632.  end
  633.  
  634.  function UpdateHomeBaseDistance()
  635.     local dis = CalculateHomebaseDistance(StartX,StartY,StartZ)
  636.     SetHomebaseDistance(dis)
  637.  end
  638.  
  639.  function UpdateTurtleValues()
  640.     print("Starting Update of values")
  641.     UpdateHomeBaseDistance()
  642.     print(DISTANCETOHOMEBASE)
  643.     UpdateTotalFuel()
  644.     print(TOTALFUEL)
  645.     print("End of Value Update")
  646.  end
  647.  
  648.  function ReturnHome()
  649.     Retreat()
  650.  end
  651. -- Turtle Functions End
  652.  
  653. -- Mining
  654.  
  655. function ExecuteMineCommand(level, length)
  656.     if ACTIVE then
  657.         return
  658.     end
  659.     MineToLevel(level)
  660.     if ACTIVE then
  661.         return
  662.     end
  663.     MineLines(length)
  664.     if ACTIVE then
  665.         return
  666.     end
  667. end
  668.  
  669. function MineToLevel(level)
  670.     if ACTIVE then
  671.         return
  672.     end
  673.     local x,y,z = gps.locate()
  674.     local distance = CalculateDistance(level,y)
  675.     if CompareTo(level,y) == 1 then
  676.         MoveUpwardByXBlocks(distance)
  677.     end
  678.     if CompareTo(level,y) == -1 then
  679.         MoveDownwardByXBlocks(distance)
  680.     end
  681. end
  682.  
  683. function MineLine(length)
  684.     MoveForwardByXBlocks(length)
  685.     MoveDownwardByOne()
  686.     TurnRight()
  687.     TurnRight()
  688. end
  689.  
  690. function MineLines(length)
  691.     if ACTIVE then
  692.         return
  693.     end
  694.     while true do
  695.         MineLine(length)
  696.     end
  697. end
  698. -- Mining
  699.  
  700. -- Os Functions Start
  701. function WhatDeviceAmIRunningOn()
  702.     return getDeviceType()
  703. end
  704.  
  705. function getDeviceType()
  706.     if turtle then
  707.      return "turtle"
  708.    elseif pocket then
  709.      return "pocket"
  710.    elseif commands then
  711.      return "command_computer"
  712.    else
  713.      return "computer"
  714.    end
  715.  end
  716.  
  717.  function OpenRednetSpecificForDevice()
  718.     local device = WhatDeviceAmIRunningOn()
  719.     device = string.upper(device)
  720.     if device == "TURTLE"then
  721.         rednet.open("left")
  722.     else if device == "COMPUTER" or device == "COMMAND_COMPUTER" or device == "PAD" then
  723.         rednet.open("back")
  724.     end
  725.     end
  726.  end
  727.  
  728. -- Os Functions End
  729.  
  730. -- General Functions Start
  731. function SetChestCoords(x,y,z)
  732.     ChestX = x
  733.     ChestY = y
  734.     ChestZ = z
  735. end
  736.  
  737. function SetFuelCoords(x,y,z)
  738.     FuelX = x
  739.     FuelY = y
  740.     FuelZ = z
  741. end
  742.  
  743. function SetSavedCoords(x,y,z)
  744.     SavedX = x
  745.     SavedY = y
  746.     SavedZ = z
  747. end
  748.  
  749. function SetStartCoords(x,y,z)
  750.     StartX = x
  751.     StartY = y
  752.     StartZ = z
  753. end
  754.  
  755. function SetHomebaseDistance(distance)
  756.     DISTANCETOHOMEBASE = distance
  757. end
  758. -- General Start End
  759.  
  760. -- String Funtion Start
  761. function MySplit (inputstr, sep)
  762.     if sep == nil then
  763.        sep = "%s"
  764.     end
  765.     local t={}
  766.     for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  767.        table.insert(t, str)
  768.     end
  769.     return t
  770.  end
  771. -- String funtion End
  772.  
  773.  
  774. Main()
Advertisement
Add Comment
Please, Sign In to add comment