Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Slot1 = Fuel
- -- Slot2 = Light
- -- Slot3 = Blocks
- --still error Retreat does not work
- --Globals Start
- FUELVALUE = 0
- Facing = 0
- MASTERID = 0
- ACTIVE = true
- ChestX,ChestY,ChestZ = 0,0,0
- FuelX,FuelY,FuelZ = 0,0,0
- StartX,StartY,StartZ = 0,0,0
- SavedX,SavedY,SavedZ = 0,0,0
- DISTANCETOHOMEBASE = 0
- TOTALFUEL = 0
- FUELSLOT = 16
- EMERGENCYFUELSLOT = 15
- COMMAND = ""
- --Globals End
- function Main()
- print("Slave Script now Running")
- OpenRednetSpecificForDevice()
- os.setComputerLabel("Slave")
- InitTurtle()
- CriticallyLowFuelCheck()
- GetCommand()
- print("Programm terminated with Code 0 " .. tostring(os.epoch("utc")))
- end
- -- Print Functions Start
- function PrintChestLocation()
- print(("Chest is at X %d Y %d Z %d"):format(ChestX,ChestY,ChestZ))
- end
- function PrintFuelLocation()
- print(("Fuel is at X %d Y %d Z %d"):format(FuelX,FuelY,FuelZ))
- end
- function PrintStartLocation()
- print(("Start is at X %d Y %d Z %d"):format(StartX,StartY,StartZ))
- end
- function PrintSavedLocation()
- print(("Saved is at X %d Y %d Z %d"):format(SavedX,SavedY,SavedZ))
- end
- function PrintAllLocations()
- PrintChestLocation()
- PrintFuelLocation()
- PrintStartLocation()
- PrintSavedLocation()
- end
- --Print Functions End
- -- Important Functions Start
- function Retreat()
- os.setComputerLabel("Retreating")
- print("Fuel: " .. turtle.getFuelLevel())
- print("Home: " .. DISTANCETOHOMEBASE)
- PrintSysMessage("About To Pathfind NonBlocking 1")
- PathfindToCoordinateNonBlocking(StartX,StartY,StartZ)
- PrintSysMessage("About To Homebase")
- HomeBase()
- PrintSysMessage("About To Pathfind NonBlocking 2")
- PathfindToCoordinateNonBlocking(StartX,StartY,StartZ)
- RotateToNegZ()
- PrintSysMessage("About To Pathfind NonBlocking 3")
- PathfindToCoordinateNonBlocking(SavedX,SavedY,SavedZ)
- os.setComputerLabel("Slave")
- PrintSysMessage("Resume Action")
- EvaluateCommand(COMMAND)
- end
- -- Important Functions End
- -- Rotation Logic Start
- -- facing = 0 means North Neg Z
- -- facing = 1 means East Pos X
- -- facing = 2 means South Pos Z
- -- facing = 3 means West Neg x
- function RotateToNegZ()
- TurnUntilFacingMatches(0)
- end
- function RotateToPosX()
- TurnUntilFacingMatches(1)
- end
- function RotateToPosZ()
- TurnUntilFacingMatches(2)
- end
- function RotateToNegX()
- TurnUntilFacingMatches(3)
- end
- function TurnUntilFacingMatches(facing)
- if Facing == facing then
- return true
- end
- TurnRight()
- if Facing == facing then
- return true
- end
- TurnRight()
- if Facing == facing then
- return true
- end
- TurnRight()
- if Facing == facing then
- return true
- end
- TurnRight()
- if Facing == facing then
- return true
- end
- end
- function MatchFacing(facing)
- if Facing == facing then
- return true
- end
- end
- function TurnRight()
- turtle.turnRight()
- Facing = math.fmod(Facing + 1,4)
- end
- function RotateUnitlFacingId(input)
- for i = 0, 3 do
- if FacingId(input) then
- return true
- else
- TurnRight()
- end
- end
- end
- function FacingId(input)
- local succes, data = turtle.inspect()
- if succes then
- if data.name == input then
- return true
- else
- return false
- end
- else
- return false
- end
- end
- function InvertHeading()
- if Facing == 0 then
- RotateToPosZ()
- end
- if Facing == 1 then
- RotateToNegX()
- end
- if Facing == 2 then
- RotateToNegZ()
- end
- if Facing == 3 then
- RotateToPosX()
- end
- end
- -- Rotation Logic End
- -- Movement Logic Start
- function MoveForwardByOne()
- CriticallyLowFuelCheck()
- if CheckRetreatParam() then
- local x,y,z = gps.locate()
- SetSavedCoords(x,y,z)
- ReturnHome()
- return
- end
- if CheckForBedrockBeneath() then
- BedrockFound()
- end
- if turtle.getFuelLevel() < 20 then
- Refuel()
- end
- local existence = turtle.inspect()
- if existence then
- turtle.dig()
- end
- UpdateTurtleValues()
- turtle.forward()
- end
- function MoveForwardByXBlocks(x)
- for i = 1, x do
- MoveForwardByOne()
- end
- end
- function MoveUpwardByOne()
- CriticallyLowFuelCheck()
- if CheckRetreatParam() then
- local x,y,z = gps.locate()
- SetSavedCoords(x,y,z)
- ReturnHome()
- return
- end
- if CheckForBedrockBeneath() then
- BedrockFound()
- end
- if turtle.getFuelLevel() < 20 then
- Refuel()
- end
- local existence = turtle.inspectUp()
- if existence then
- turtle.digUp()
- end
- UpdateTurtleValues()
- turtle.up()
- end
- function MoveUpwardByXBlocks(x)
- for i = 1, x do
- MoveUpwardByOne()
- end
- end
- function MoveDownwardByOne()
- CriticallyLowFuelCheck()
- if CheckRetreatParam() then
- local x,y,z = gps.locate()
- SetSavedCoords(x,y,z)
- ReturnHome()
- return
- end
- if CheckForBedrockBeneath() then
- BedrockFound()
- end
- if turtle.getFuelLevel() < 20 then
- Refuel()
- end
- local existence = turtle.inspectDown()
- if existence then
- turtle.digDown()
- end
- UpdateTurtleValues()
- turtle.down()
- end
- function MoveDownwardByXBlocks(x)
- for i = 1, x do
- MoveDownwardByOne()
- end
- end
- -- Movement Logic End
- -- Pathfinding Logic Start
- function PathfindToCoordinate(destinationX,destinationY,destinationZ)
- local currentX,currentY,currentZ = gps.locate()
- local xDistance = CalculateDistance(destinationX,currentX)
- local yDistance = CalculateDistance(destinationY,currentY)
- local zDistance = CalculateDistance(destinationZ,currentZ)
- local totalDistance = CalculateTotalDistance(xDistance,yDistance,zDistance)
- local totalFuel = CalculateTotalFuel()
- if totalFuel < totalDistance then
- --Send Distress Signal To Low Fuel for Retreat
- os.setComputerLabel("Fuel Critical")
- else
- CorrectYAxis(destinationY,currentY,yDistance)
- CorrectXAxis(destinationX,currentX,xDistance)
- CorrectZAxis(destinationZ,currentZ,zDistance)
- if CompareCoordinate(destinationX,destinationY,destinationZ) then
- print("Location has been Successfully reached")
- else
- print("Location has not been reached")
- end
- end
- end
- function CompareCoordinate(destinationX,destinationY,destinationZ)
- local currentX,currentY,currentZ = gps.locate()
- if currentX == destinationX and currentY == destinationY and currentZ == destinationZ then
- return true
- else
- return false
- end
- end
- function CorrectXAxis(destinationX,currentX,xDistance)
- local comparison = CompareTo(currentX,destinationX)
- local xDistance = xDistance
- if comparison == 1 then
- RotateToNegX()
- MoveForwardByXBlocks(xDistance)
- end
- if comparison == -1 then
- RotateToPosX()
- MoveForwardByXBlocks(xDistance)
- end
- end
- function CorrectZAxis(destinationZ,currentZ,zDistance)
- local comparison = CompareTo(currentZ,destinationZ)
- local zDistance = zDistance
- if comparison == 1 then
- RotateToNegZ()
- MoveForwardByXBlocks(zDistance)
- end
- if comparison == -1 then
- RotateToPosZ()
- MoveForwardByXBlocks(zDistance)
- end
- end
- function CorrectYAxis(destinationY,currentY,yDistance)
- local comparison = CompareTo(currentY,destinationY)
- local yDistance = yDistance
- if comparison == 1 then
- MoveDownwardByXBlocks(yDistance)
- end
- if comparison == -1 then
- MoveUpwardByXBlocks(yDistance)
- end
- end
- -- Pathfinding Logic End
- -- Non Blocking Pathfind Logic Start
- function MoveNonBlockingForwardByOne()
- CriticallyLowFuelCheck()
- if turtle.getFuelLevel() < 20 then
- Refuel()
- end
- local existence = turtle.inspect()
- if existence then
- turtle.dig()
- end
- turtle.forward()
- UpdateTurtleValues()
- end
- function MoveNonBlockingForwardByXBlocks(x)
- for i = 1, x do
- MoveNonBlockingForwardByOne()
- end
- end
- function MoveNonBlockingUpByOne()
- CriticallyLowFuelCheck()
- if turtle.getFuelLevel() < 20 then
- Refuel()
- end
- local existence = turtle.inspectUp()
- if existence then
- turtle.digUp()
- end
- turtle.up()
- UpdateTurtleValues()
- end
- function MoveNonBlockingUpByXBlocks(x)
- for i = 1, x do
- MoveNonBlockingUpByOne()
- end
- end
- function MoveNonBlockingDownbyOne()
- CriticallyLowFuelCheck()
- if turtle.getFuelLevel() < 20 then
- Refuel()
- end
- local existence = turtle.inspectDown()
- if existence then
- turtle.digDown()
- end
- turtle.down()
- UpdateTurtleValues()
- end
- function MoveNonBlockingDownByXBlocks(x)
- for i = 1, x do
- MoveNonBlockingDownbyOne()
- end
- end
- function CorrectYAxisNonBlocking(destinationY,currentY,yDistance)
- local comparison = CompareTo(currentY,destinationY)
- local yDistance = yDistance
- if comparison == 1 then
- MoveNonBlockingDownByXBlocks(yDistance)
- end
- if comparison == -1 then
- MoveNonBlockingUpByXBlocks(yDistance)
- end
- end
- function CorrectZAxisNonBlocking(destinationZ,currentZ,zDistance)
- local comparison = CompareTo(currentZ,destinationZ)
- local zDistance = zDistance
- if comparison == 1 then
- RotateToNegZ()
- MoveNonBlockingForwardByXBlocks(zDistance)
- end
- if comparison == -1 then
- RotateToPosZ()
- MoveNonBlockingForwardByXBlocks(zDistance)
- end
- end
- function CorrectXAxisNonBlocking(destinationX,currentX,xDistance)
- local comparison = CompareTo(currentX,destinationX)
- local xDistance = xDistance
- if comparison == 1 then
- RotateToNegX()
- MoveNonBlockingForwardByXBlocks(xDistance)
- end
- if comparison == -1 then
- RotateToPosX()
- MoveNonBlockingForwardByXBlocks(xDistance)
- end
- end
- function PathfindToCoordinateNonBlocking(destinationX,destinationY,destinationZ)
- local currentX,currentY,currentZ = gps.locate()
- local xDistance = CalculateDistance(destinationX,currentX)
- local yDistance = CalculateDistance(destinationY,currentY)
- local zDistance = CalculateDistance(destinationZ,currentZ)
- local totalDistance = CalculateTotalDistance(xDistance,yDistance,zDistance)
- local totalFuel = CalculateTotalFuel()
- if totalFuel < totalDistance then
- else
- CorrectYAxisNonBlocking(destinationY,currentY,yDistance)
- CorrectXAxisNonBlocking(destinationX,currentX,xDistance)
- CorrectZAxisNonBlocking(destinationZ,currentZ,zDistance)
- if CompareCoordinate(destinationX,destinationY,destinationZ) then
- print("Successfully Reached Coords")
- else
- print("UnSuccessfully Reached Coords")
- end
- end
- end
- -- Non Blocking PathFind Logic End
- -- Mining Logic Start
- function MineLine(length)
- if CheckRetreatParam() then
- local x,y,z = gps.locate()
- SetSavedCoords(x,y,z)
- ReturnHome()
- end
- if CheckForBedrockBeneath() then
- BedrockFound()
- end
- MoveForwardByXBlocks(length)
- MoveDownwardByOne()
- TurnRight()
- TurnRight()
- end
- function MineDownToSpecifiedLevel(level)
- local x,y,z = gps.locate()
- local newY = tonumber(y)
- local height = tonumber(level)
- local distance = CalculateDistance(height,newY)
- MoveDownwardByXBlocks(distance)
- end
- function ExecuteMineCommand(level,length)
- MineDownToSpecifiedLevel(level)
- while true do
- MineLine(length)
- end
- print("Ended MineLine if you reach this big error")
- end
- function CheckRetreatParam()
- if LowFuel() then
- print("Fuel")
- return true
- end
- if InventoryHasfreeSpace() then
- print("Inventory")
- return true
- end
- return false
- end
- -- always call CheckRetreatParameter and Checkforbedrock
- -- Mining Logic End
- -- Math Logic Start
- function CompareTo(num1,num2)
- if num1 == nil then
- print("num1 is nil")
- end
- if num2 == nil then
- print("num2 is nil")
- end
- local val1 = tonumber(num1)
- local val2 = tonumber(num2)
- if val1 > val2 then
- return 1
- else if val1 < val2 then
- return -1
- else if val1 == val2 then
- return 0
- end
- end
- end
- end
- function CalculateDistance(value1,value2)
- return math.abs(value1 - value2)
- end
- function CalculateTotalDistance(xDistance,yDistance,zDistance)
- local result = xDistance + yDistance + zDistance
- return result
- end
- function CalculateHomebaseDistance(baseX,BaseY,BaseZ)
- local x,y,z = gps.locate()
- local result = CalculateTotalDistance(CalculateDistance(baseX,x),CalculateDistance(BaseY,y),CalculateDistance(BaseZ,z))
- return result
- end
- function CalculateTotalFuel()
- local result = turtle.getFuelLevel() + turtle.getItemCount(FUELSLOT) * FUELVALUE
- return result
- end
- --Math Logic End
- -- Send and Receive Logic Start
- function EnterWaitModeAndReturnMessage()
- local id,message = rednet.receive()
- return id,message
- end
- function SendImportantMessage()
- SendMessage("IMPORTANT")
- end
- function SendMessage(input)
- rednet.send(MASTERID,input)
- PrintSysMessage(("%s Message has been sent to Computer %d"):format(input,MASTERID) .. tostring(os.epoch("utc")))
- end
- -- Send and Receive Logic End
- -- Terminal Logic Start
- function PrintStringInColor(string,color)
- term.setTextColor(color)
- print(string)
- ResetTerminal()
- end
- function PrintSysMessage(string)
- PrintStringInColor(string,colors.magenta)
- end
- function ResetTerminal()
- term.setTextColor(colors.white)
- end
- -- Terminal Logic End
- -- Turtle Functions Start
- function UnloadInventory()
- local success,data = turtle.inspect()
- if success then
- if data.name == "minecraft:chest" or "minecraft:ender_chest" then
- for i = 1, 16 do
- turtle.select(i)
- turtle.drop()
- end
- end
- end
- end
- function FuelUp()
- local success,data = turtle.inspect()
- if success then
- turtle.select(FUELSLOT)
- turtle.suck()
- turtle.select(EMERGENCYFUELSLOT) --Emergency FuelSlot
- turtle.suck()
- turtle.select(1)
- end
- end
- function SetFuelValue(input)
- FUELVALUE = input
- end
- function CheckForBedrockBeneath()
- local success,data = turtle.inspectDown()
- if success then
- if data.name == "minecraft:bedrock" then
- return true
- else
- return false
- end
- else
- return false
- end
- end
- function Refuel()
- local success = false
- if turtle.getItemCount(FUELSLOT) > 0 then
- turtle.select(FUELSLOT)
- success = turtle.refuel(1)
- turtle.select(1)
- end
- return success
- end
- function EmergencyRefuel()
- local success = false
- if turtle.getItemCount(EMERGENCYFUELSLOT) > 0 then
- turtle.select(EMERGENCYFUELSLOT)
- success = turtle.refuel()
- turtle.select(1)
- end
- return success
- end
- function LowFuel()
- if DISTANCETOHOMEBASE == TOTALFUEL then
- print(("Distance equals Totalfuel %d"):format(TOTALFUEL))
- return true
- end
- if DISTANCETOHOMEBASE < TOTALFUEL then
- print("I can Still reach Home")
- return false
- end
- if DISTANCETOHOMEBASE > TOTALFUEL then
- print("Home is to faraway")
- print("Using Emergency Fuel")
- EmergencyRefuel()
- return true
- end
- end
- function InventoryHasfreeSpace()
- local count = turtle.getItemCount(14)
- if count == 0 then
- return false
- else
- return true
- end
- end
- function InitTurtle()
- print("initializing Turtle")
- local id, chestMessage = EnterWaitModeAndReturnMessage()
- print(chestMessage)
- local id, fuelMessage = EnterWaitModeAndReturnMessage()
- print(fuelMessage)
- local id,fuelType = EnterWaitModeAndReturnMessage()
- print(fuelType)
- local fuelCoords = MySplit(fuelMessage)
- local chestCoords = MySplit(chestMessage)
- local currentX,currentY,currentZ = gps.locate()
- EvaluateFuelType(fuelType)
- SetFuelCoords(fuelCoords[1],fuelCoords[2],fuelCoords[3])
- SetChestCoords(chestCoords[1],chestCoords[2],chestCoords[3])
- SetStartCoords(currentX,currentY,currentZ)
- SetSavedCoords(currentX,currentY,currentZ)
- Refuel()
- UpdateTurtleValues()
- end
- function GetCommand()
- local id, message = EnterWaitModeAndReturnMessage()
- COMMAND = message
- EvaluateCommand(message)
- end
- function EvaluateCommand(input)
- print("Evaluating Command")
- local command = MySplit(input)
- if command[1] == "line" then
- if command[2] ~= nil then
- print("Mining at Height")
- ExecuteMineCommand(tonumber(command[2]),tonumber(command[3]))
- else
- print("Mining from the Top")
- local x,y,z = gps.locate()
- ExecuteMineCommand(y,tonumber(command[3]))
- end
- end
- end
- function EvaluateFuelType(input)
- if input == "coal" then
- SetFuelValue(80)
- else if input == "coal_block" then
- SetFuelValue(800)
- end
- end
- end
- function CriticallyLowFuelCheck()
- if turtle.getFuelLevel() == 0 then
- os.setComputerLabel("Critical Fuel level")
- --os.shutdown()
- end
- end
- function BedrockFound()
- os.setComputerLabel("Bedrock Found")
- PathfindToCoordinateNonBlocking(StartX,StartY,StartZ)
- RotateToNegZ()
- os.shutdown()
- end
- function UpdateTotalFuel()
- TOTALFUEL = CalculateTotalFuel()
- end
- function UpdateHomeBaseDistance()
- local dis = CalculateHomebaseDistance(FuelX,FuelY,FuelZ)
- SetHomebaseDistance(dis)
- end
- function UpdateTurtleValues()
- print("Starting Update of values")
- UpdateHomeBaseDistance()
- print(DISTANCETOHOMEBASE)
- UpdateTotalFuel()
- print(TOTALFUEL)
- print("End of Value Update")
- end
- function ReturnHome()
- print("Going Home")
- Retreat()
- end
- function IncreaseRetreatCounter()
- INTERNALRETREATCOUNTER = INTERNALRETREATCOUNTER + 1
- end
- function CheckMaximumRetreats()
- if INTERNALRETREATCOUNTER == MAXRETREATS then
- os.setComputerLabel("I retreated The Max Amount")
- os.shutdown()
- end
- end
- -- Turtle Functions End
- -- HomeBase Operations Start
- function ArrivedAtFuelHub()
- if CompareCoordinate(FuelX,FuelY,FuelZ) then
- return true
- else
- return false
- end
- end
- function RefuelAtFuelHub()
- RotateToPosZ()
- --RotateUnitlFacingId("functionalstorage:oak_1")
- FuelUp()
- end
- function MoveToFuelHub()
- PathfindToCoordinateNonBlocking(FuelX,FuelY,FuelZ)
- if ArrivedAtFuelHub() then
- return true
- end
- end
- function MoveAwayFromRefuelHub()
- PathfindToCoordinateNonBlocking(StartX,StartY,StartZ)
- end
- function RefuelAtTheFuelHub()
- MoveToFuelHub()
- end
- function FuelHub()
- MoveToFuelHub()
- RefuelAtFuelHub()
- MoveAwayFromRefuelHub()
- end
- function OutputChest()
- MoveToOutputChest()
- OutputInventory()
- end
- function MoveToOutputChest()
- PathfindToCoordinateNonBlocking(ChestX,ChestY,ChestZ)
- RotateToPosZ()
- --RotateUnitlFacingId("minecraft:chest")
- end
- function OutputInventory()
- UnloadInventory()
- end
- function HomeBase()
- OutputChest()
- FuelHub()
- end
- -- Homebase Operations End
- -- Os Functions Start
- function WhatDeviceAmIRunningOn()
- return getDeviceType()
- end
- function getDeviceType()
- if turtle then
- return "turtle"
- elseif pocket then
- return "pocket"
- elseif commands then
- return "command_computer"
- else
- return "computer"
- end
- end
- function OpenRednetSpecificForDevice()
- local device = WhatDeviceAmIRunningOn()
- device = string.upper(device)
- if device == "TURTLE"then
- rednet.open("left")
- else if device == "COMPUTER" or device == "COMMAND_COMPUTER" or device == "PAD" then
- rednet.open("back")
- end
- end
- end
- -- Os Functions End
- -- General Functions Start
- function SetChestCoords(x,y,z)
- ChestX = x
- ChestY = y
- ChestZ = z
- end
- function SetFuelCoords(x,y,z)
- FuelX = x
- FuelY = y
- FuelZ = z
- end
- function SetSavedCoords(x,y,z)
- SavedX = x
- SavedY = y
- SavedZ = z
- end
- function SetStartCoords(x,y,z)
- StartX = x
- StartY = y
- StartZ = z
- end
- function SetHomebaseDistance(distance)
- DISTANCETOHOMEBASE = distance
- end
- -- General Start End
- -- String Funtion Start
- function MySplit (inputstr, sep)
- if sep == nil then
- sep = "%s"
- end
- local t={}
- for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
- table.insert(t, str)
- end
- return t
- end
- -- String funtion End
- Main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement