exa-byte

tBaseTurtle

Jun 22nd, 2014
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.86 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.  
  17.  
  18.  
  19.  
  20.  
  21. --functions--
  22.  
  23. function GetUnloadPosition()
  24.     if unloadPosition.d ~= nil then
  25.         return unloadPosition.x, unloadPosition.y, unloadPosition.z, unloadPosition.d
  26.     else
  27.         return unloadPosition.x, unloadPosition.y, unloadPosition.z
  28.     end
  29. end
  30.  
  31. --Setup--
  32.  
  33. function SetupToNearestMasterPC()
  34.     masterID = nil
  35.     ownerID = -1
  36.     while masterID == nil do
  37.     rednet.broadcast("pa setup locate")
  38.         local startedTime = os.clock()
  39.         while (os.clock() - startedTime) <= rednetTimeout do
  40.             local id, msg, dis = rednet.receive(rednetTimeout)
  41.             print("1")
  42.             if (msg == "pa setup masterHere") then
  43.                 print(msg)
  44.                 if ownerID == -1 or smallestDis > dis then
  45.                     smallestDis = dis
  46.                     ownerID = id
  47.                     masterID = id
  48.                 end
  49.             end
  50.         end
  51.     end
  52.     if (smallestDis == 0 or ownerID == -1) then
  53.         return false
  54.     else return ownerID end
  55. end
  56. function GetJob()
  57.     local msg = nil
  58.     while msg == nil do
  59.         local msg = SendAndGetResponse(masterID, "pa job request")
  60.     end
  61.     local splitMessage = SplitMessage(msg)
  62.     if splitMessage[1] == "pa" then
  63.         if splitMessage[2] == "job" then
  64.             if splitMessage[3] == "do" then
  65.                 if splitMessage[4] == "TurtlePlacer" then
  66.                     print("JobTurtlePlacer()")
  67.                     SendMessage(masterID, "pa job confirm")
  68.                     while true do pcall(JobTurtlePlacer()) end
  69.                
  70.                 elseif splitMessage[4] == "Digger" or splitMessage[4] == "Miner" then
  71.                     print(msg)
  72.                     jobx, joby, jobz, jobd, length = tonumber(splitMessage[5]), tonumber(splitMessage[6]), tonumber(splitMessage[7]), splitMessage[8], tonumber(splitMessage[9])
  73.                     print("JobDigger(" .. jobx .. ", " .. joby .. ", " .. jobz .. ", "  .. jobd .. ", " .. length .. ")")
  74.                     SendMessage(masterID, "pa job confirm")
  75.                     if splitMessage[4] == "Digger" then
  76.                         JobDigger(jobx, joby, jobz, jobd, length)
  77.                     else JobMiner(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[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.    local t = {}
  180.    local fpat = "(.-)" .. pat
  181.    local last_end = 1
  182.    local s, e, cap = str:find(fpat, 1)
  183.    while s do
  184.       if s ~= 1 or cap ~= "" then
  185.          table.insert(t,cap)
  186.       end
  187.       last_end = e+1
  188.       s, e, cap = str:find(fpat, last_end)
  189.    end
  190.    if last_end <= #str then
  191.       cap = str:sub(last_end)
  192.       table.insert(t, cap)
  193.    end
  194.    return t
  195. end
  196. function SplitMessage(_msg)
  197.     return Split(_msg, " ")
  198. end
  199.  
  200. --movement--
  201.  
  202. function Forward()
  203.     while turtle.forward() == false do
  204.         Refuel()
  205.         if turtle.detect() == true then
  206.             sleep(sleepTimer)
  207.         end
  208.     end
  209.     if richtung == "x" then
  210.         xPos = xPos + 1
  211.     elseif richtung == "-x" then
  212.         xPos = xPos - 1
  213.     elseif richtung == "y" then
  214.         yPos = yPos + 1  
  215.     elseif richtung == "-y" then
  216.         yPos = yPos - 1
  217.     end
  218.     return not turtle.detect()
  219. end
  220. function ForwardDig()
  221.     while turtle.forward() == false do
  222.         Refuel()
  223.         if turtle.detect() == true then
  224.             if peripheral.getType("front") == "turtle" then
  225.                 while peripheral.getType("front") == "turtle" do
  226.                     sleep(sleepTimer)
  227.                 end
  228.             else
  229.                 Dig()
  230.             end
  231.         end
  232.     end
  233.     if richtung == "x" then
  234.         xPos = xPos + 1
  235.     elseif richtung == "-x" then
  236.         xPos = xPos - 1
  237.     elseif richtung == "y" then
  238.         yPos = yPos + 1  
  239.     elseif richtung == "-y" then
  240.         yPos = yPos - 1
  241.     end
  242.     return not turtle.detect()
  243. end
  244. function Backward()
  245.     Refuel()
  246.     if turtle.back() == false then
  247.         sleep(sleepTimer)
  248.     end
  249.     if richtung == "x" then
  250.         xPos = xPos - 1
  251.     elseif richtung == "-x" then
  252.         xPos = xPos + 1
  253.     elseif richtung == "y" then
  254.         yPos = yPos - 1  
  255.     elseif richtung == "-y" then
  256.         yPos = yPos + 1
  257.     end
  258. end
  259. function BackwardDig()
  260.     Refuel()
  261.     if turtle.back() == false then
  262.         Right()
  263.         Right()
  264.         while turtle.forward() == false do
  265.             if peripheral.getType("front") == "turtle" then
  266.                 while peripheral.getType("front") == "turtle" do
  267.                     sleep(sleepTimer)
  268.                 end
  269.             else
  270.                 Dig()
  271.             end
  272.         end
  273.         Right()
  274.         Right()
  275.     end
  276.     if richtung == "x" then
  277.         xPos = xPos - 1
  278.     elseif richtung == "-x" then
  279.         xPos = xPos + 1
  280.     elseif richtung == "y" then
  281.         yPos = yPos - 1  
  282.     elseif richtung == "-y" then
  283.         yPos = yPos + 1
  284.     end
  285. end
  286. function Up()
  287.     while turtle.up() == false do
  288.         Refuel()
  289.         sleep(sleepTimer)
  290.     end
  291.     zPos = zPos + 1
  292. end
  293. function UpDig()
  294.     Refuel()
  295.     while turtle.up() == false do
  296.         Refuel()
  297.         if turtle.detectUp() == true then
  298.             if peripheral.getType("top") == "turtle" then
  299.                 while peripheral.getType("top") == "turtle" do
  300.                     sleep(sleepTimer)
  301.                 end
  302.             else
  303.                 DigUp()
  304.             end
  305.         end
  306.     end
  307.     zPos = zPos + 1
  308. end
  309. function Down()
  310.     while turtle.down() == false do
  311.         Refuel()
  312.         sleep(sleepTimer)
  313.     end
  314.     zPos = zPos - 1
  315. end
  316. function DownDig()
  317.     while turtle.down() == false do
  318.         Refuel()
  319.         if turtle.detectDown() == true then
  320.             if peripheral.getType("bottom") == "turtle" then
  321.                 while peripheral.getType("bottom") == "turtle" do
  322.                     sleep(sleepTimer)
  323.                 end
  324.             else
  325.                 DigDown()
  326.             end
  327.         end
  328.     end
  329.     zPos = zPos - 1
  330. end
  331. function Left()
  332.     turtle.turnLeft()
  333.     if richtung == "x" then
  334.         richtung = "y"
  335.     elseif richtung == "y" then
  336.         richtung = "-x"
  337.     elseif richtung == "-x" then
  338.         richtung = "-y"
  339.     else richtung = "x"
  340.     end
  341. end
  342. function Right()
  343.     turtle.turnRight()
  344.     if richtung == "x" then
  345.         richtung = "-y"
  346.     elseif richtung == "y" then
  347.         richtung = "x"
  348.     elseif richtung == "-x" then
  349.         richtung = "y"
  350.     else richtung = "-x"
  351.     end
  352. end
  353. function Dig()
  354.     if peripheral.getType("front") == "turtle" then
  355.         return turtle.detect()
  356.     else
  357.         while turtle.dig() do
  358.             if peripheral.getType("front") == "turtle" then
  359.                 break
  360.             end
  361.             sleep(0.5)
  362.             if not turtle.detect() then
  363.                 return true
  364.             end
  365.         end
  366.         return not turtle.detect()
  367.     end
  368. end
  369. function DigUp()
  370.     if peripheral.getType("top") == "turtle" then
  371.         return turtle.detectUp()
  372.     else
  373.         while turtle.digUp() do
  374.             if peripheral.getType("top") == "turtle" then return turtle.detect() end
  375.             sleep(0.5)
  376.             if not turtle.detectUp() then
  377.                 return true
  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 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)
  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.     Up()
  557.     GoTo(GetUnloadPosition())
  558.     Unload("down")
  559. end
  560. function JobMiner(_jobxPos, _jobyPos, _jobzPos, _jobdirection, _length)
  561.     GoTo(2,0,-2)
  562.     GoTo(_jobxPos, _jobyPos, _jobzPos, _jobdirection)
  563.     for i=0, _length do
  564.         if  yPos ~= 0 or _jobzPos > -2 then
  565.             DigUp()
  566.         end
  567.         if yPos ~= 0 or _jobzPos < -2 then
  568.             DigDown()
  569.         end
  570.         if i < _length then
  571.             ForwardDig()
  572.         end
  573.     end
  574.     Up()
  575.     GoTo(3,50,2)
  576.     GoTo(GetUnloadPosition())
  577.     Unload("down")
  578. end
  579.  
  580. ---------------------------------------------------------------------------
  581. --wait for id--
  582.  
  583. if SetupToNearestMasterPC() == false then
  584.     print("I cant find the MasterPC! Set him up first")
  585.     return
  586. end
  587. DetermineDirection()
  588. xPos, yPos, zPos = 0, -1, 0
  589. LookDirection("x")
  590. GetFreshFuelFromChest(32)
  591. UpDig()
  592. ForwardDig()
  593. ForwardDig()
  594. Left()
  595. ForwardDig()
  596. while true do GetJob() end
Advertisement
Add Comment
Please, Sign In to add comment