exa-byte

tBaseTurtle v1.03

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