Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Slot1 = Fuel
- -- Slot2 = Light
- -- Slot3 = Blocks
- --Globals Start
- FUELVALUE = 0
- Facing = 0
- MASTERID = 0
- ACTIVE = true
- ChestX,ChestY,ChestZ = 0,0,0
- FuelX,FuelY,FuelZ = 0,0,0
- SavedX,SavedY,SavedZ = 0,0,0
- --Globals End
- function main()
- OpenRednetSpecificForDevice()
- os.setComputerLabel("Slave")
- local x,y,z = gps.locate()
- SetFuelValue(1)
- SetFuelCoords(-261,63,560)
- SetChestCoords(-262,63,560)
- SetSavedCoords(x,y,z)
- HomeBase()
- print("Programm terminated with Code 0 " .. tostring(os.epoch("utc")))
- end
- -- Important Functions Start
- function Retreat()
- -- navigate back to saved location
- PathfindToCoordinate(ChestX,ChestY,ChestZ)
- end
- function Terminate()
- 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
- for i = 0, 3 do
- TurnRight()
- if Facing == facing then
- return true
- end
- 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
- -- Rotation Logic End
- -- Movement Logic Start
- function MoveForwardByOne()
- local existence = turtle.inspect()
- if existence then
- turtle.dig()
- end
- turtle.forward()
- end
- function MoveForwardByXBlocks(x)
- for i = 1, x do
- MoveForwardByOne()
- end
- end
- function MoveUpwardByOne()
- local existence = turtle.inspectUp()
- if existence then
- turtle.digUp()
- end
- turtle.up()
- end
- function MoveUpwardByXBlocks(x)
- for i = 1, x do
- MoveUpwardByOne()
- end
- end
- function MoveDownwardByOne()
- local existence = turtle.inspectDown()
- if existence then
- turtle.digDown()
- end
- turtle.down()
- end
- function MoveDownwardByXBlocks(x)
- for i = 1, x do
- MoveDownwardByOne()
- end
- end
- function MoveNonDestructiveForwardByOne()
- local existence = turtle.inspect()
- if existence then
- return true
- end
- turtle.forward()
- end
- function MoveNonDestructiveForwardByXBlocks(x)
- for i = 1, x do
- local block = MoveNonDestructiveForwardByOne()
- if block then
- SendMessage("Blocked at the Front")
- end
- end
- end
- function MoveNonDestructiveUpByOne()
- local existence = turtle.inspectUp()
- if existence then
- return true
- end
- turtle.up()
- end
- function MoveNonDestructiveUpByXBlocks(x)
- for i = 1, x do
- local block = MoveNonDestructiveForwardByOne()
- if block then
- SendMessage("Blocked at the Top")
- end
- end
- end
- function MoveNonDestructiveDownbyOne()
- local existence = turtle.inspectDown()
- if existence then
- return true
- end
- turtle.down()
- end
- function MoveNonDestructiveDownByXBlocks(x)
- for i = 1, x do
- local block = MoveNonDestructiveForwardByOne()
- if block then
- SendMessage("Blocked at the Bottom")
- end
- 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 = turtle.getFuelLevel() + turtle.getItemCount(1) * FUELVALUE
- if totalFuel < totalDistance then
- --Send Distress Signal To Low Fuel for Retreat
- SendMessage("Urgent Message Incoming")
- local message = ("%d %d %d"):format(currentX,currentY,currentZ)
- SendMessage(message)
- 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 PathfindToCoordinateNonDestructive(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 = turtle.getFuelLevel() + turtle.getItemCount(1) * FUELVALUE
- if totalFuel < totalDistance then
- --Send Distress Signal To Low Fuel for Retreat
- SendMessage("Urgent Message Incoming")
- local message = ("%d %d %d"):format(currentX,currentY,currentZ)
- SendMessage(message)
- else
- CorrectYAxisNonDestructive(destinationY,currentY,yDistance)
- CorrectXAxisNonDestructive(destinationX,currentX,xDistance)
- CorrectZAxisNondestructive(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 CorrectXAxisNonDestructive(destinationX,currentX,xDistance)
- local comparison = CompareTo(currentX,destinationX)
- local xDistance = xDistance
- if comparison == 1 then
- RotateToNegX()
- MoveNonDestructiveForwardByXBlocks(xDistance)
- end
- if comparison == -1 then
- RotateToPosX()
- MoveNonDestructiveForwardByXBlocks(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 CorrectZAxisNondestructive(destinationZ,currentZ,zDistance)
- local comparison = CompareTo(currentZ,destinationZ)
- local zDistance = zDistance
- if comparison == 1 then
- RotateToNegZ()
- MoveNonDestructiveForwardByXBlocks(zDistance)
- end
- if comparison == -1 then
- RotateToPosZ()
- MoveNonDestructiveForwardByXBlocks(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
- function CorrectYAxisNonDestructive(destinationY,currentY,yDistance)
- local comparison = CompareTo(currentY,destinationY)
- local yDistance = yDistance
- if comparison == 1 then
- MoveNonDestructiveDownByXBlocks(yDistance)
- end
- if comparison == -1 then
- MoveNonDestructiveUpByXBlocks(yDistance)
- end
- end
- -- Pathfinding Logic End
- -- Mining Logic Start
- -- Mining Logic End
- -- Math Logic Start
- function CompareTo(num1,num2)
- if num1 > num2 then
- return 1
- else if num1 < num2 then
- return -1
- else if num1 == num2 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
- --Math Logic End
- -- Send and Receive Logic Start
- function EnterWaitModeAndReturnMessage()
- local id,message,protocol = rednet.receive()
- return id,message,protocol
- 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
- function EvaluateResponse(id,str)
- local message = string.upper(str)
- if (message == "GO")
- then
- print(("Computer %d sent message %s"):format(id, message) .. tostring(os.epoch("utc")))
- Mine()
- end
- if (message == "RETREAT")
- then
- print(("Computer %d sent message %s"):format(id, message) .. tostring(os.epoch("utc")))
- end
- if(message == "TERMINATE")
- then
- print(("Computer %d sent message %s"):format(id, message) .. tostring(os.epoch("utc")))
- Terminate()
- end
- 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(1)
- turtle.suck()
- end
- end
- function SetFuelValue(input)
- if input == 1 then
- FUELVALUE = 80
- end
- if input == 2 then
- FUELVALUE = 800
- end
- 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(1) > 0 then
- turtle.select(1)
- success = turtle.refuel(1)
- end
- return success
- end
- -- Turtle Functions End
- -- HomeBase Operations Start
- function ArrivedAtFuelHub()
- if CompareCoordinate(FuelX,FuelY,FuelZ) then
- return true
- else
- return false
- end
- end
- function RefuelAtFuelHub()
- RotateUnitlFacingId("functionalstorage:oak_1")
- FuelUp()
- end
- function MoveToFuelHub()
- PathfindToCoordinate(FuelX,FuelY,FuelZ)
- if ArrivedAtFuelHub() then
- return true
- end
- end
- function MoveAwayFromRefuelHub()
- PathfindToCoordinateNonDestructive(SavedX,SavedY,SavedZ)
- end
- function RefuelAtTheFuelHub()
- MoveToFuelHub()
- end
- function FuelHub()
- MoveToFuelHub()
- RefuelAtFuelHub()
- MoveAwayFromRefuelHub()
- end
- function OutputChest()
- MoveToOutputChest()
- OutputInventory()
- end
- function MoveToOutputChest()
- PathfindToCoordinate(ChestX,ChestY,ChestZ)
- RotateUnitlFacingId("minecraft:chest")
- end
- function OutputInventory()
- UnloadInventory()
- end
- 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 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
- main()
Advertisement
Add Comment
Please, Sign In to add comment