exa-byte

tBaseTurtle v1.02

Jul 1st, 2014
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --tBaseTurtle v1.02
  2.  
  3. --startup--
  4.  
  5. --variables--
  6.  
  7. local port = "right"
  8. local rednetTimeout =0.5
  9. local pingListenerEnabled = true
  10. local unloadPosition = {x=3,y=-2,z=2}
  11. local sleepTimer = 0.5
  12. local storageChestCount = 4 --change also if you're using item collectors instead of chests
  13.  
  14. --initialize wlan--
  15.  
  16. rednet.open(port)
  17.  
  18. --functions--
  19.  
  20. function GetUnloadPosition()
  21.     if unloadPosition.d ~= nil then
  22.         return unloadPosition.x, unloadPosition.y, unloadPosition.z, unloadPosition.d
  23.     else
  24.         return unloadPosition.x, unloadPosition.y, unloadPosition.z
  25.     end
  26. end
  27.  
  28. --Setup--
  29.  
  30. function SetupToNearestMasterPC()
  31.     masterID = nil
  32.     ownerID = -1
  33.     while masterID == nil do
  34.     rednet.broadcast("pa setup locate")
  35.         local startedTime = os.clock()
  36.         while (os.clock() - startedTime) <= rednetTimeout do
  37.             local id, msg, dis = rednet.receive(rednetTimeout)
  38.             print("1")
  39.             if (msg == "pa setup masterHere") then
  40.                 print(msg)
  41.                 if ownerID == -1 or smallestDis > dis then
  42.                     smallestDis = dis
  43.                     ownerID = id
  44.                     masterID = id
  45.                 end
  46.             end
  47.         end
  48.     end
  49.     if (smallestDis == 0 or ownerID == -1) then
  50.         return false
  51.     else return ownerID end
  52. end
  53. function GetJob()
  54.     --local msg = nil
  55.     --while msg == nil do
  56.     local msg = SendAndGetResponse(masterID, "pa job request")
  57.     --end
  58.     local splitMessage = SplitMessage(msg)
  59.     if splitMessage[1] == "pa" then
  60.         if splitMessage[2] == "job" then
  61.             if splitMessage[3] == "do" then
  62.                 if splitMessage[4] == "TurtlePlacer" then
  63.                     print("JobTurtlePlacer()")
  64.                     SendMessage(masterID, "pa job confirm")
  65.                     while true do pcall(JobTurtlePlacer()) end
  66.                
  67.                 elseif splitMessage[4] == "Digger" or splitMessage[4] == "Diggerx1" or splitMessage[4] == "Miner" or splitMessage[4] == "Minerx1 "then
  68.                     print(msg)
  69.                     jobx, joby, jobz, jobd, length = tonumber(splitMessage[5]), tonumber(splitMessage[6]), tonumber(splitMessage[7]), splitMessage[8], tonumber(splitMessage[9])
  70.                     print("JobDigger(" .. jobx .. ", " .. joby .. ", " .. jobz .. ", "  .. jobd .. ", " .. length .. ")")
  71.                     SendMessage(masterID, "pa job confirm")
  72.                     if splitMessage[4] == "Digger" then
  73.                         JobDigger(jobx, joby, jobz, jobd, length)
  74.                     elseif splitMessage[4] == "Diggerx1" then
  75.                         JobDiggerx1(jobx, joby, jobz, jobd, length)
  76.                     elseif splitMessage[4] == "Miner" then
  77.                         JobMiner(jobx, joby, jobz, jobd, length)
  78.                     elseif splitMessage[4] == "Minerx1" then
  79.                         JobMinerx1(jobx, joby, jobz, jobd, length)
  80.                     end
  81.                 end
  82.             end
  83.         end
  84.     end
  85. end
  86. function DetermineDirection()
  87.     if peripheral.getType("front") == "computer" then richtung = "-y"
  88.     elseif peripheral.getType("left") == "computer" then richtung = "-x"
  89.     elseif peripheral.getType("back") == "computer" then richtung = "y"
  90.     elseif peripheral.getType("left") == "turtle" then richtung = "x"
  91.     else
  92.         turtle.turnRight()
  93.         if peripheral.getType("front") == "computer" then richtung = "-y"
  94.         else return
  95.         end
  96.     end
  97. end
  98. function GetFreshFuelFromChest(_count) --calcs var fuelvalue
  99.     fuelSlot = 1
  100.     Select(1)
  101.     turtle.suck()
  102.     local fuelLevel1 = turtle.getFuelLevel()
  103.     turtle.refuel(1)
  104.     fuelValue = turtle.getFuelLevel() - fuelLevel1
  105.     fuelCount = turtle.getItemCount(1)
  106. end
  107.  
  108. --communication--
  109.  
  110. function SendAndGetResponse(_recID, _message)
  111.     SendMessage(_recID, _message)
  112.     local startedTime = os.clock()
  113.     while (os.clock() - startedTime) <= rednetTimeout do
  114.         local id, msg, dis = rednet.receive(rednetTimeout)
  115.         if id == _recID or _recID == 0 then
  116.             local splitMessage = Split(msg, " ")
  117.             if (splitMessage and splitMessage[1] == "pa") then
  118.                 return msg
  119.             end
  120.         end
  121.     end
  122.     return false
  123. end
  124. function SendAndGetResponseNoTimeout(_recID, _message)
  125.     sendMessage(_recID ,_message)
  126.     while true do
  127.         local id, msg, dis = rednet.receive(0)
  128.         if id == _recID or _recID == 0 then
  129.             local splitMessage = split(msg, " ")
  130.             if (splitMessage[1] == "pa") then
  131.                 return msg
  132.             end
  133.         end
  134.     end
  135. end
  136. function SendMessage(_recID, _message)
  137.     rednet.send(_recID, _message)
  138. end
  139.  
  140. --fuelmanagement--
  141.  
  142. function Refuel() --inactive--
  143.     --[[if fuelNeeded >= fuelCount * fuelValue * 0.6 then
  144.         turtle.fuelNeeded = true
  145.         GoToStorage()
  146.         TurtleAtStorageManager()
  147.         GoToLastPlace()
  148.     else
  149.         if turtle.getFuelLevel() == 0 then
  150.             Select(fuelSlot)
  151.             if turtle.refuel(1) == true then
  152.                 fuelCount = fuelCount - 1
  153.             end
  154.             return true
  155.         else
  156.             return false
  157.         end
  158.     end]]
  159.     local refueled = OnlyRefuel()
  160.     if refueled == true then
  161.         return true
  162.     else
  163.         return false
  164.     end
  165. end
  166. function OnlyRefuel()
  167.     if turtle.getFuelLevel() == 0 then
  168.         Select(fuelSlot)
  169.         if turtle.refuel(1) == true then
  170.             fuelCount = fuelCount - 1
  171.         return true
  172.         end
  173.     else
  174.         return false
  175.     end
  176. end
  177.  
  178. --processing--
  179.  
  180. function Split(str, pat)
  181.     if str then
  182.         local t = {}
  183.         local fpat = "(.-)" .. pat
  184.         local last_end = 1
  185.         local s, e, cap = str:find(fpat, 1)
  186.         while s do
  187.             if s ~= 1 or cap ~= "" then
  188.                 table.insert(t,cap)
  189.             end
  190.             last_end = e+1
  191.             s, e, cap = str:find(fpat, last_end)
  192.         end
  193.         if last_end <= #str then
  194.             cap = str:sub(last_end)
  195.             table.insert(t, cap)
  196.         end
  197.         return t
  198.     else
  199.         return false
  200.     end
  201. end
  202. function SplitMessage(_msg)
  203.     return Split(_msg, " ")
  204. end
  205.  
  206. --movement--
  207.  
  208. function Forward()
  209.     while turtle.forward() == false do
  210.         Refuel()
  211.         if turtle.detect() == true then
  212.             sleep(sleepTimer)
  213.         end
  214.     end
  215.     if richtung == "x" then
  216.         xPos = xPos + 1
  217.     elseif richtung == "-x" then
  218.         xPos = xPos - 1
  219.     elseif richtung == "y" then
  220.         yPos = yPos + 1  
  221.     elseif richtung == "-y" then
  222.         yPos = yPos - 1
  223.     end
  224.     return not turtle.detect()
  225. end
  226. function ForwardDig()
  227.     while turtle.forward() == false do
  228.         Refuel()
  229.         if turtle.detect() == true then
  230.             if peripheral.getType("front") == "turtle" then
  231.                 while peripheral.getType("front") == "turtle" do
  232.                     sleep(sleepTimer)
  233.                 end
  234.             else
  235.                 Dig()
  236.             end
  237.         end
  238.     end
  239.     if richtung == "x" then
  240.         xPos = xPos + 1
  241.     elseif richtung == "-x" then
  242.         xPos = xPos - 1
  243.     elseif richtung == "y" then
  244.         yPos = yPos + 1  
  245.     elseif richtung == "-y" then
  246.         yPos = yPos - 1
  247.     end
  248.     return not turtle.detect()
  249. end
  250. function Backward()
  251.     Refuel()
  252.     if turtle.back() == false then
  253.         sleep(sleepTimer)
  254.     end
  255.     if richtung == "x" then
  256.         xPos = xPos - 1
  257.     elseif richtung == "-x" then
  258.         xPos = xPos + 1
  259.     elseif richtung == "y" then
  260.         yPos = yPos - 1  
  261.     elseif richtung == "-y" then
  262.         yPos = yPos + 1
  263.     end
  264. end
  265. function BackwardDig()
  266.     Refuel()
  267.     if turtle.back() == false then
  268.         Right()
  269.         Right()
  270.         while turtle.forward() == false do
  271.             if peripheral.getType("front") == "turtle" then
  272.                 while peripheral.getType("front") == "turtle" do
  273.                     sleep(sleepTimer)
  274.                 end
  275.             else
  276.                 Dig()
  277.             end
  278.         end
  279.         Right()
  280.         Right()
  281.     end
  282.     if richtung == "x" then
  283.         xPos = xPos - 1
  284.     elseif richtung == "-x" then
  285.         xPos = xPos + 1
  286.     elseif richtung == "y" then
  287.         yPos = yPos - 1  
  288.     elseif richtung == "-y" then
  289.         yPos = yPos + 1
  290.     end
  291. end
  292. function Up()
  293.     while turtle.up() == false do
  294.         Refuel()
  295.         sleep(sleepTimer)
  296.     end
  297.     zPos = zPos + 1
  298. end
  299. function UpDig()
  300.     Refuel()
  301.     while turtle.up() == false do
  302.         Refuel()
  303.         if turtle.detectUp() == true then
  304.             if peripheral.getType("top") == "turtle" then
  305.                 while peripheral.getType("top") == "turtle" do
  306.                     sleep(sleepTimer)
  307.                 end
  308.             else
  309.                 DigUp()
  310.             end
  311.         end
  312.     end
  313.     zPos = zPos + 1
  314. end
  315. function Down()
  316.     while turtle.down() == false do
  317.         Refuel()
  318.         sleep(sleepTimer)
  319.     end
  320.     zPos = zPos - 1
  321. end
  322. function DownDig()
  323.     while turtle.down() == false do
  324.         Refuel()
  325.         if turtle.detectDown() == true then
  326.             if peripheral.getType("bottom") == "turtle" then
  327.                 while peripheral.getType("bottom") == "turtle" do
  328.                     sleep(sleepTimer)
  329.                 end
  330.             else
  331.                 DigDown()
  332.             end
  333.         end
  334.     end
  335.     zPos = zPos - 1
  336. end
  337. function Left()
  338.     turtle.turnLeft()
  339.     if richtung == "x" then
  340.         richtung = "y"
  341.     elseif richtung == "y" then
  342.         richtung = "-x"
  343.     elseif richtung == "-x" then
  344.         richtung = "-y"
  345.     else richtung = "x"
  346.     end
  347. end
  348. function Right()
  349.     turtle.turnRight()
  350.     if richtung == "x" then
  351.         richtung = "-y"
  352.     elseif richtung == "y" then
  353.         richtung = "x"
  354.     elseif richtung == "-x" then
  355.         richtung = "y"
  356.     else richtung = "-x"
  357.     end
  358. end
  359. function Dig()
  360.     if peripheral.getType("front") == "turtle" then
  361.         return not turtle.detect()
  362.     else
  363.         while turtle.dig() do
  364.             sleep(0.5)
  365.             if peripheral.getType("front") == "turtle" or turtle.detect() == false then
  366.                 break
  367.             end
  368.         end
  369.         return not turtle.detect()
  370.     end
  371. end
  372. function DigUp()
  373.     if peripheral.getType("top") == "turtle" then
  374.         return not turtle.detectUp()
  375.     else
  376.         while turtle.digUp() do
  377.             sleep(0.5)
  378.             if peripheral.getType("top") == "turtle" or turtle.detectUp() == false then
  379.                 break
  380.             end
  381.         end
  382.         return not turtle.detectUp()
  383.     end
  384. end
  385. function DigDown()
  386.     if peripheral.getType("bottom") == "turtle" then
  387.         return not turtle.detectDown()
  388.     else
  389.         turtle.digDown()
  390.     end
  391.     return not turtle.detectDown()
  392. end
  393. function LookDirection(_direction)
  394.     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
  395.         Left()
  396.     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
  397.         Left()
  398.         Left()
  399.     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
  400.        
  401.     else
  402.         Right()
  403.     end
  404. end
  405. function GoTo(_destxPos, _destyPos, _destzPos, _destDirection) --only works from 0, -1, 0
  406.     local startPos = {x = xPos, y = yPos, z = zPos}
  407.     print("GoTo(" .. _destxPos .. ", " .. _destyPos .. ", " .. _destzPos .. ")")
  408.     if _destDirection ~= nil then print(_destDirection) end
  409.     if (startPos.x == 0 and startPos.y == -1 and startPos.z == 0) then
  410.         LookDirection("y")
  411.         UpDig()
  412.         ForwardDig()
  413.     end
  414.     if xPos < _destxPos then
  415.         LookDirection("x")
  416.         while xPos < _destxPos do
  417.             ForwardDig()
  418.         end
  419.     elseif xPos > _destxPos then
  420.         LookDirection("-x")
  421.         while xPos > _destxPos do
  422.             ForwardDig()
  423.         end
  424.     else end            
  425.     if yPos < _destyPos then
  426.         LookDirection("y")
  427.         while yPos < _destyPos do
  428.             ForwardDig()
  429.         end
  430.     elseif yPos > _destyPos then
  431.         LookDirection("-y")
  432.         while yPos > _destyPos do
  433.             ForwardDig()
  434.         end
  435.     else end
  436.     if zPos < _destzPos then
  437.         while zPos < _destzPos do
  438.             UpDig()
  439.         end
  440.     elseif zPos > _destzPos then
  441.         while zPos > _destzPos do
  442.             DownDig()
  443.         end
  444.     else end        
  445.     if _destDirection ~= nil then
  446.         LookDirection(_destDirection)
  447.     end
  448. end
  449.  
  450. --inventory Interaction--
  451.  
  452. function Unload(_direction)
  453.     print("UnloadManager...")
  454.     --print("actual coords: " .. xPos .. " " .. yPos .. " " .. zPos)
  455.     local onChest = false
  456.     while yPos > unloadPosition.y-storageChestCount+1 and zPos ~= unloadPosition.z-1 do
  457.         if turtle.detectDown() == false then
  458.             Down()
  459.             --print("go down")
  460.         elseif turtle.detect() == false then
  461.             LookDirection("-y")
  462.             Forward()
  463.             --print("go forward")
  464.         else
  465.             sleep(0.2)
  466.         end
  467.         if yPos == unloadPosition.y-storageChestCount+1 then
  468.             Down()
  469.             --print("go down2")
  470.         end
  471.     end
  472.     --print("do unload invetory")
  473.     for i=2, 16 do
  474.         turtle.select(i)
  475.         if _direction == nil then
  476.             turtle.drop()
  477.             --print("case nil")
  478.         elseif _direction == "up" then
  479.             turtle.dropUp()
  480.             --print("case up")
  481.         elseif _direction == "down" then
  482.             turtle.dropDown()
  483.             --print("case down")
  484.         else
  485.             --print(_direction .. " is not allowed parameter")
  486.             return "not allowed parameter"
  487.         end
  488.     end
  489.     print("reststuff")
  490.     Select(selectedSlot)
  491.     LookDirection("-x")
  492.     Forward()
  493.     Right()
  494.     while yPos < 0 do Forward() end
  495. end
  496. function Select(_slot)
  497.     if turtle.select(_slot) == true then
  498.         selectedSlot = _slot
  499.     else return false end
  500. end
  501.  
  502. --jobs--
  503.  
  504. function JobTurtlePlacer()
  505.     function TurnWorkerOn()
  506.         local worker = peripheral.wrap("front")
  507.         worker.turnOn()
  508.     end
  509.     local function PlaceAndActivateWorkers()
  510.         while turtle.getItemCount(2) > 0 do
  511.             while (turtle.detect == true) do
  512.                 sleep(1)
  513.             end
  514.             sleep(0.5)
  515.             turtle.place()
  516.             sleep(0.4)
  517.             if pcall(TurnWorkerOn) then end
  518.         end
  519.     end
  520.     LookDirection("-x")
  521.     while xPos > -1 do ForwardDig() end
  522.     while zPos > 0 do DownDig() end
  523.     Left()
  524.     if turtle.getItemCount(2) ~= 0 then
  525.         print("Help me get rid of the Items in Slot 2! :( \n After solving this, wait up to 10 seconds before breaking me")
  526.         turtle.drop()
  527.         while turtle.getItemCount(2) ~= 0 do
  528.             rednet.broadcast("pa help " .. xPos .. " " .. yPos .. " " .. zPos .. " TurtlePlacerHasProblem")
  529.             sleep(10)
  530.         end
  531.     end
  532.     Select(2)
  533.     turtle.suck()
  534.     while turtle.getItemCount(2) == 0 do
  535.         sleep(10)
  536.         turtle.suck()
  537.     end
  538.     Left()
  539.     Forward()
  540.     Right()
  541.     PlaceAndActivateWorkers()
  542.    
  543.     while true do
  544.         Right()
  545.         Forward()
  546.         Left()
  547.         turtle.suck()
  548.         while turtle.getItemCount(2) == 0 do
  549.             sleep(10)
  550.             turtle.suck()
  551.         end
  552.         Left()
  553.         Forward()
  554.         Right()
  555.         PlaceAndActivateWorkers()
  556.     end
  557. end
  558. function JobDigger(_jobxPos, _jobyPos, _jobzPos, _jobdirection, _length)    --will dig a 3high, 2wide, _lengthlong tunnel
  559.     GoTo(_jobxPos, _jobyPos, _jobzPos, _jobdirection)
  560.     for i=0, _length do
  561.         DigUp()
  562.         DigDown()
  563.         if i < _length then
  564.             ForwardDig()
  565.         end
  566.     end
  567.     Right()
  568.     ForwardDig()
  569.     Right()
  570.     for i=0, _length do
  571.         DigUp()
  572.         DigDown()
  573.         if i < _length then
  574.             ForwardDig()
  575.         end
  576.     end
  577.     Up()
  578.     Backward()
  579.     GoTo(GetUnloadPosition())
  580.     Unload("down")
  581. end
  582. function JobDiggerx1(_jobxPos, _jobyPos, _jobzPos, _jobdirection, _length)
  583.     GoTo(_jobxPos, _jobyPos, _jobzPos, _jobdirection)
  584.     for i=0, _length do
  585.         DigUp()
  586.         DigDown()
  587.         if i < _length then
  588.             ForwardDig()
  589.         end
  590.     end
  591.     Right()
  592.     Right()
  593.     Up()
  594.     for i = 0, _length-1 do
  595.         ForwardDig()
  596.     end
  597.     GoTo(GetUnloadPosition())
  598.     Unload("down")
  599. end
  600. function JobMiner(_jobxPos, _jobyPos, _jobzPos, _jobdirection, _length)     --will dig a 3high, 2wide, _lengthlong tunnel
  601.     GoTo(2,0,-2)
  602.     GoTo(_jobxPos, _jobyPos, _jobzPos, _jobdirection)
  603.     for i=0, _length do
  604.         DigUp()
  605.         DigDown()
  606.         if i < _length then
  607.             ForwardDig()
  608.         end
  609.     end
  610.     Right()
  611.     ForwardDig()
  612.     Right()
  613.     for i=0, _length do
  614.         --if not(yPos == _jobyPos and zPos < -2) then
  615.             DigUp()
  616.         --end
  617.         DigDown()
  618.         if i < _length then
  619.             ForwardDig()
  620.         end
  621.     end
  622.     Up()
  623.     GoTo(2,1,2)
  624.     GoTo(GetUnloadPosition())
  625.     Unload("down")
  626. end
  627. function JobMinerx1(_jobxPos, _jobyPos, _jobzPos, _jobdirection, _length)
  628.     GoTo(2,0,-2)
  629.     GoTo(_jobxPos, _jobyPos, _jobzPos, _jobdirection)
  630.     for i=0, _length do
  631.         if  yPos ~= 0 or _jobzPos > -2 then
  632.             DigUp()
  633.         end
  634.         if yPos ~= 0 or _jobzPos < -2 then
  635.             DigDown()
  636.         end
  637.         if i < _length then
  638.             ForwardDig()
  639.         end
  640.     end
  641.     Right()
  642.     Right()
  643.     for i = 0, _length-1 do
  644.         ForwardDig()
  645.     end
  646.     GoTo(2,1,2)
  647.     GoTo(GetUnloadPosition())
  648.     Unload("down")
  649. end
  650.  
  651. ---------------------------------------------------------------------------
  652. --wait for id--
  653.  
  654. if SetupToNearestMasterPC() == false then
  655.     print("I cant find the MasterPC! Set him up first")
  656.     return
  657. end
  658. DetermineDirection()
  659. xPos, yPos, zPos = 0, -2, 0
  660. LookDirection("x")
  661. GetFreshFuelFromChest(64)
  662. UpDig()
  663. Forward()
  664. Forward()
  665. Left()
  666. Forward()
  667. Forward()
  668. while true do GetJob() end
Advertisement
Add Comment
Please, Sign In to add comment