Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --tBaseTurtle v1.02
- --startup--
- --variables--
- local port = "right"
- local rednetTimeout =0.5
- local pingListenerEnabled = true
- local unloadPosition = {x=3,y=-2,z=2}
- local sleepTimer = 0.5
- local storageChestCount = 4 --change also if you're using item collectors instead of chests
- --initialize wlan--
- rednet.open(port)
- --functions--
- function GetUnloadPosition()
- if unloadPosition.d ~= nil then
- return unloadPosition.x, unloadPosition.y, unloadPosition.z, unloadPosition.d
- else
- return unloadPosition.x, unloadPosition.y, unloadPosition.z
- end
- end
- --Setup--
- function SetupToNearestMasterPC()
- masterID = nil
- ownerID = -1
- while masterID == nil do
- rednet.broadcast("pa setup locate")
- local startedTime = os.clock()
- while (os.clock() - startedTime) <= rednetTimeout do
- local id, msg, dis = rednet.receive(rednetTimeout)
- print("1")
- if (msg == "pa setup masterHere") then
- print(msg)
- if ownerID == -1 or smallestDis > dis then
- smallestDis = dis
- ownerID = id
- masterID = id
- end
- end
- end
- end
- if (smallestDis == 0 or ownerID == -1) then
- return false
- else return ownerID end
- end
- function GetJob()
- --local msg = nil
- --while msg == nil do
- local msg = SendAndGetResponse(masterID, "pa job request")
- --end
- local splitMessage = SplitMessage(msg)
- if splitMessage[1] == "pa" then
- if splitMessage[2] == "job" then
- if splitMessage[3] == "do" then
- if splitMessage[4] == "TurtlePlacer" then
- print("JobTurtlePlacer()")
- SendMessage(masterID, "pa job confirm")
- while true do pcall(JobTurtlePlacer()) end
- elseif splitMessage[4] == "Digger" or splitMessage[4] == "Diggerx1" or splitMessage[4] == "Miner" or splitMessage[4] == "Minerx1 "then
- print(msg)
- jobx, joby, jobz, jobd, length = tonumber(splitMessage[5]), tonumber(splitMessage[6]), tonumber(splitMessage[7]), splitMessage[8], tonumber(splitMessage[9])
- print("JobDigger(" .. jobx .. ", " .. joby .. ", " .. jobz .. ", " .. jobd .. ", " .. length .. ")")
- SendMessage(masterID, "pa job confirm")
- if splitMessage[4] == "Digger" then
- JobDigger(jobx, joby, jobz, jobd, length)
- elseif splitMessage[4] == "Diggerx1" then
- JobDiggerx1(jobx, joby, jobz, jobd, length)
- elseif splitMessage[4] == "Miner" then
- JobMiner(jobx, joby, jobz, jobd, length)
- elseif splitMessage[4] == "Minerx1" then
- JobMinerx1(jobx, joby, jobz, jobd, length)
- end
- end
- end
- end
- end
- end
- function DetermineDirection()
- if peripheral.getType("front") == "computer" then richtung = "-y"
- elseif peripheral.getType("left") == "computer" then richtung = "-x"
- elseif peripheral.getType("back") == "computer" then richtung = "y"
- elseif peripheral.getType("left") == "turtle" then richtung = "x"
- else
- turtle.turnRight()
- if peripheral.getType("front") == "computer" then richtung = "-y"
- else return
- end
- end
- end
- function GetFreshFuelFromChest(_count) --calcs var fuelvalue
- fuelSlot = 1
- Select(1)
- turtle.suck()
- local fuelLevel1 = turtle.getFuelLevel()
- turtle.refuel(1)
- fuelValue = turtle.getFuelLevel() - fuelLevel1
- fuelCount = turtle.getItemCount(1)
- end
- --communication--
- function SendAndGetResponse(_recID, _message)
- SendMessage(_recID, _message)
- local startedTime = os.clock()
- while (os.clock() - startedTime) <= rednetTimeout do
- local id, msg, dis = rednet.receive(rednetTimeout)
- if id == _recID or _recID == 0 then
- local splitMessage = Split(msg, " ")
- if (splitMessage and splitMessage[1] == "pa") then
- return msg
- end
- end
- end
- return false
- end
- function SendAndGetResponseNoTimeout(_recID, _message)
- sendMessage(_recID ,_message)
- while true do
- local id, msg, dis = rednet.receive(0)
- if id == _recID or _recID == 0 then
- local splitMessage = split(msg, " ")
- if (splitMessage[1] == "pa") then
- return msg
- end
- end
- end
- end
- function SendMessage(_recID, _message)
- rednet.send(_recID, _message)
- end
- --fuelmanagement--
- function Refuel() --inactive--
- --[[if fuelNeeded >= fuelCount * fuelValue * 0.6 then
- turtle.fuelNeeded = true
- GoToStorage()
- TurtleAtStorageManager()
- GoToLastPlace()
- else
- if turtle.getFuelLevel() == 0 then
- Select(fuelSlot)
- if turtle.refuel(1) == true then
- fuelCount = fuelCount - 1
- end
- return true
- else
- return false
- end
- end]]
- local refueled = OnlyRefuel()
- if refueled == true then
- return true
- else
- return false
- end
- end
- function OnlyRefuel()
- if turtle.getFuelLevel() == 0 then
- Select(fuelSlot)
- if turtle.refuel(1) == true then
- fuelCount = fuelCount - 1
- return true
- end
- else
- return false
- end
- end
- --processing--
- function Split(str, pat)
- if str then
- local t = {}
- local fpat = "(.-)" .. pat
- local last_end = 1
- local s, e, cap = str:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(t,cap)
- end
- last_end = e+1
- s, e, cap = str:find(fpat, last_end)
- end
- if last_end <= #str then
- cap = str:sub(last_end)
- table.insert(t, cap)
- end
- return t
- else
- return false
- end
- end
- function SplitMessage(_msg)
- return Split(_msg, " ")
- end
- --movement--
- function Forward()
- while turtle.forward() == false do
- Refuel()
- if turtle.detect() == true then
- sleep(sleepTimer)
- end
- end
- if richtung == "x" then
- xPos = xPos + 1
- elseif richtung == "-x" then
- xPos = xPos - 1
- elseif richtung == "y" then
- yPos = yPos + 1
- elseif richtung == "-y" then
- yPos = yPos - 1
- end
- return not turtle.detect()
- end
- function ForwardDig()
- while turtle.forward() == false do
- Refuel()
- if turtle.detect() == true then
- if peripheral.getType("front") == "turtle" then
- while peripheral.getType("front") == "turtle" do
- sleep(sleepTimer)
- end
- else
- Dig()
- end
- end
- end
- if richtung == "x" then
- xPos = xPos + 1
- elseif richtung == "-x" then
- xPos = xPos - 1
- elseif richtung == "y" then
- yPos = yPos + 1
- elseif richtung == "-y" then
- yPos = yPos - 1
- end
- return not turtle.detect()
- end
- function Backward()
- Refuel()
- if turtle.back() == false then
- sleep(sleepTimer)
- end
- if richtung == "x" then
- xPos = xPos - 1
- elseif richtung == "-x" then
- xPos = xPos + 1
- elseif richtung == "y" then
- yPos = yPos - 1
- elseif richtung == "-y" then
- yPos = yPos + 1
- end
- end
- function BackwardDig()
- Refuel()
- if turtle.back() == false then
- Right()
- Right()
- while turtle.forward() == false do
- if peripheral.getType("front") == "turtle" then
- while peripheral.getType("front") == "turtle" do
- sleep(sleepTimer)
- end
- else
- Dig()
- end
- end
- Right()
- Right()
- end
- if richtung == "x" then
- xPos = xPos - 1
- elseif richtung == "-x" then
- xPos = xPos + 1
- elseif richtung == "y" then
- yPos = yPos - 1
- elseif richtung == "-y" then
- yPos = yPos + 1
- end
- end
- function Up()
- while turtle.up() == false do
- Refuel()
- sleep(sleepTimer)
- end
- zPos = zPos + 1
- end
- function UpDig()
- Refuel()
- while turtle.up() == false do
- Refuel()
- if turtle.detectUp() == true then
- if peripheral.getType("top") == "turtle" then
- while peripheral.getType("top") == "turtle" do
- sleep(sleepTimer)
- end
- else
- DigUp()
- end
- end
- end
- zPos = zPos + 1
- end
- function Down()
- while turtle.down() == false do
- Refuel()
- sleep(sleepTimer)
- end
- zPos = zPos - 1
- end
- function DownDig()
- while turtle.down() == false do
- Refuel()
- if turtle.detectDown() == true then
- if peripheral.getType("bottom") == "turtle" then
- while peripheral.getType("bottom") == "turtle" do
- sleep(sleepTimer)
- end
- else
- DigDown()
- end
- end
- end
- zPos = zPos - 1
- end
- function Left()
- turtle.turnLeft()
- if richtung == "x" then
- richtung = "y"
- elseif richtung == "y" then
- richtung = "-x"
- elseif richtung == "-x" then
- richtung = "-y"
- else richtung = "x"
- end
- end
- function Right()
- turtle.turnRight()
- if richtung == "x" then
- richtung = "-y"
- elseif richtung == "y" then
- richtung = "x"
- elseif richtung == "-x" then
- richtung = "y"
- else richtung = "-x"
- end
- end
- function Dig()
- if peripheral.getType("front") == "turtle" then
- return not turtle.detect()
- else
- while turtle.dig() do
- sleep(0.5)
- if peripheral.getType("front") == "turtle" or turtle.detect() == false then
- break
- end
- end
- return not turtle.detect()
- end
- end
- function DigUp()
- if peripheral.getType("top") == "turtle" then
- return not turtle.detectUp()
- else
- while turtle.digUp() do
- sleep(0.5)
- if peripheral.getType("top") == "turtle" or turtle.detectUp() == false then
- break
- end
- end
- return not turtle.detectUp()
- end
- end
- function DigDown()
- if peripheral.getType("bottom") == "turtle" then
- return not turtle.detectDown()
- else
- turtle.digDown()
- end
- return not turtle.detectDown()
- end
- function LookDirection(_direction)
- if ((_direction == "x" and richtung == "-y") or (_direction == "-x" and richtung == "y") or (_direction == "y" and richtung == "x") or (_direction == "-y" and richtung == "-x")) then
- Left()
- elseif ((_direction == "x" and richtung == "-x") or (_direction == "-x" and richtung == "x") or (_direction == "y" and richtung == "-y") or (_direction == "-y" and richtung == "y")) then
- Left()
- Left()
- elseif((_direction == "x" and richtung == "x") or (_direction == "-x" and richtung == "-x") or (_direction == "y" and richtung == "y") or (_direction == "-y" and richtung == "-y")) then
- else
- Right()
- end
- end
- function GoTo(_destxPos, _destyPos, _destzPos, _destDirection) --only works from 0, -1, 0
- local startPos = {x = xPos, y = yPos, z = zPos}
- print("GoTo(" .. _destxPos .. ", " .. _destyPos .. ", " .. _destzPos .. ")")
- if _destDirection ~= nil then print(_destDirection) end
- if (startPos.x == 0 and startPos.y == -1 and startPos.z == 0) then
- LookDirection("y")
- UpDig()
- ForwardDig()
- end
- if xPos < _destxPos then
- LookDirection("x")
- while xPos < _destxPos do
- ForwardDig()
- end
- elseif xPos > _destxPos then
- LookDirection("-x")
- while xPos > _destxPos do
- ForwardDig()
- end
- else end
- if yPos < _destyPos then
- LookDirection("y")
- while yPos < _destyPos do
- ForwardDig()
- end
- elseif yPos > _destyPos then
- LookDirection("-y")
- while yPos > _destyPos do
- ForwardDig()
- end
- else end
- if zPos < _destzPos then
- while zPos < _destzPos do
- UpDig()
- end
- elseif zPos > _destzPos then
- while zPos > _destzPos do
- DownDig()
- end
- else end
- if _destDirection ~= nil then
- LookDirection(_destDirection)
- end
- end
- --inventory Interaction--
- function Unload(_direction)
- print("UnloadManager...")
- --print("actual coords: " .. xPos .. " " .. yPos .. " " .. zPos)
- local onChest = false
- while yPos > unloadPosition.y-storageChestCount+1 and zPos ~= unloadPosition.z-1 do
- if turtle.detectDown() == false then
- Down()
- --print("go down")
- elseif turtle.detect() == false then
- LookDirection("-y")
- Forward()
- --print("go forward")
- else
- sleep(0.2)
- end
- if yPos == unloadPosition.y-storageChestCount+1 then
- Down()
- --print("go down2")
- end
- end
- --print("do unload invetory")
- for i=2, 16 do
- turtle.select(i)
- if _direction == nil then
- turtle.drop()
- --print("case nil")
- elseif _direction == "up" then
- turtle.dropUp()
- --print("case up")
- elseif _direction == "down" then
- turtle.dropDown()
- --print("case down")
- else
- --print(_direction .. " is not allowed parameter")
- return "not allowed parameter"
- end
- end
- print("reststuff")
- Select(selectedSlot)
- LookDirection("-x")
- Forward()
- Right()
- while yPos < 0 do Forward() end
- end
- function Select(_slot)
- if turtle.select(_slot) == true then
- selectedSlot = _slot
- else return false end
- end
- --jobs--
- function JobTurtlePlacer()
- function TurnWorkerOn()
- local worker = peripheral.wrap("front")
- worker.turnOn()
- end
- local function PlaceAndActivateWorkers()
- while turtle.getItemCount(2) > 0 do
- while (turtle.detect == true) do
- sleep(1)
- end
- sleep(0.5)
- turtle.place()
- sleep(0.4)
- if pcall(TurnWorkerOn) then end
- end
- end
- LookDirection("-x")
- while xPos > -1 do ForwardDig() end
- while zPos > 0 do DownDig() end
- Left()
- if turtle.getItemCount(2) ~= 0 then
- print("Help me get rid of the Items in Slot 2! :( \n After solving this, wait up to 10 seconds before breaking me")
- turtle.drop()
- while turtle.getItemCount(2) ~= 0 do
- rednet.broadcast("pa help " .. xPos .. " " .. yPos .. " " .. zPos .. " TurtlePlacerHasProblem")
- sleep(10)
- end
- end
- Select(2)
- turtle.suck()
- while turtle.getItemCount(2) == 0 do
- sleep(10)
- turtle.suck()
- end
- Left()
- Forward()
- Right()
- PlaceAndActivateWorkers()
- while true do
- Right()
- Forward()
- Left()
- turtle.suck()
- while turtle.getItemCount(2) == 0 do
- sleep(10)
- turtle.suck()
- end
- Left()
- Forward()
- Right()
- PlaceAndActivateWorkers()
- end
- end
- function JobDigger(_jobxPos, _jobyPos, _jobzPos, _jobdirection, _length) --will dig a 3high, 2wide, _lengthlong tunnel
- GoTo(_jobxPos, _jobyPos, _jobzPos, _jobdirection)
- for i=0, _length do
- DigUp()
- DigDown()
- if i < _length then
- ForwardDig()
- end
- end
- Right()
- ForwardDig()
- Right()
- for i=0, _length do
- DigUp()
- DigDown()
- if i < _length then
- ForwardDig()
- end
- end
- Up()
- Backward()
- GoTo(GetUnloadPosition())
- Unload("down")
- end
- function JobDiggerx1(_jobxPos, _jobyPos, _jobzPos, _jobdirection, _length)
- GoTo(_jobxPos, _jobyPos, _jobzPos, _jobdirection)
- for i=0, _length do
- DigUp()
- DigDown()
- if i < _length then
- ForwardDig()
- end
- end
- Right()
- Right()
- Up()
- for i = 0, _length-1 do
- ForwardDig()
- end
- GoTo(GetUnloadPosition())
- Unload("down")
- end
- function JobMiner(_jobxPos, _jobyPos, _jobzPos, _jobdirection, _length) --will dig a 3high, 2wide, _lengthlong tunnel
- GoTo(2,0,-2)
- GoTo(_jobxPos, _jobyPos, _jobzPos, _jobdirection)
- for i=0, _length do
- DigUp()
- DigDown()
- if i < _length then
- ForwardDig()
- end
- end
- Right()
- ForwardDig()
- Right()
- for i=0, _length do
- --if not(yPos == _jobyPos and zPos < -2) then
- DigUp()
- --end
- DigDown()
- if i < _length then
- ForwardDig()
- end
- end
- Up()
- GoTo(2,1,2)
- GoTo(GetUnloadPosition())
- Unload("down")
- end
- function JobMinerx1(_jobxPos, _jobyPos, _jobzPos, _jobdirection, _length)
- GoTo(2,0,-2)
- GoTo(_jobxPos, _jobyPos, _jobzPos, _jobdirection)
- for i=0, _length do
- if yPos ~= 0 or _jobzPos > -2 then
- DigUp()
- end
- if yPos ~= 0 or _jobzPos < -2 then
- DigDown()
- end
- if i < _length then
- ForwardDig()
- end
- end
- Right()
- Right()
- for i = 0, _length-1 do
- ForwardDig()
- end
- GoTo(2,1,2)
- GoTo(GetUnloadPosition())
- Unload("down")
- end
- ---------------------------------------------------------------------------
- --wait for id--
- if SetupToNearestMasterPC() == false then
- print("I cant find the MasterPC! Set him up first")
- return
- end
- DetermineDirection()
- xPos, yPos, zPos = 0, -2, 0
- LookDirection("x")
- GetFreshFuelFromChest(64)
- UpDig()
- Forward()
- Forward()
- Left()
- Forward()
- Forward()
- while true do GetJob() end
Advertisement
Add Comment
Please, Sign In to add comment