Advertisement
Guest User

startup.lua

a guest
Jan 27th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.82 KB | None | 0 0
  1. --patebin code K6TfReHR
  2. -- Custom Variables Changable.
  3. local port = "right"
  4. local timeOutOnRednet = 2
  5. local limitSize = 64 -- I do not reccomend changing this as its very tempermental
  6. local fuelSlots = {}
  7. local chestDir = "down"
  8.  
  9. -- DO NOT CHANGE ANYTHING BELOW
  10. rednet.open(port)
  11.  
  12. -- These are XYZ values relative to the quarry. At default they will all be 0 as quarry is at 0,0
  13. local qu_x = 0
  14. local qu_y = 0
  15. local qu_z = -1
  16. local facing = 2
  17.  
  18. -- Misc Functions
  19. function checkFuel (blocksNeeded)
  20.     if turtle.getFuelLevel() < blocksNeeded then
  21.         local moreFuel = true
  22.         local fuelNeeded = blocksNeeded - turtle.getFuelLevel()
  23.         while fuelNeeded > 0 do
  24.             if turtle.refuel(1) == false then
  25.                 print("REFUELING")
  26.                 if takeItems("front") == false then
  27.                     fuel()
  28.                     return false
  29.                 end
  30.             end
  31.             fuelNeeded = blocksNeeded - turtle.getFuelLevel()
  32.         end
  33.         print("REFUEL COMPLETE")
  34.         turtle.drop()
  35.         return true
  36.     else
  37.         print("FUEL " .. turtle.getFuelLevel() .. " units")
  38.         return true
  39.     end
  40.     return false
  41. end
  42.  
  43. function split(str, pat)
  44.    local t = {}
  45.    local fpat = "(.-)" .. pat
  46.    local last_end = 1
  47.    print(str)
  48.    local s, e, cap = str:find(fpat, 1)
  49.    while s do
  50.       if s ~= 1 or cap ~= "" then
  51.          table.insert(t,cap)
  52.       end
  53.       last_end = e+1
  54.       s, e, cap = str:find(fpat, last_end)
  55.    end
  56.    if last_end <= #str then
  57.       cap = str:sub(last_end)
  58.       table.insert(t, cap)
  59.    end
  60.    return t
  61. end
  62.  
  63. local smallestDis = 0
  64. local mainPCIsLocatedAt = {}
  65. local mainBot = false
  66. -- Function finds and sets up with the nearest quarry
  67. function setupToNearestQuarry ()
  68.     local ownerID = -1
  69.     rednet.broadcast("CCQuarry LOCATE")
  70.     local startedTime = os.clock()
  71.     while (os.clock() - startedTime) <= timeOutOnRednet do
  72.         local id, mess, dis = rednet.receive(timeOutOnRednet)
  73.         if (mess == "CCQuarry PING") then
  74.             if ownerID == -1 or smallestDis >= dis then
  75.                 smallestDis = dis
  76.                 ownerID = id
  77.             end
  78.         end
  79.     end
  80.     if (smallestDis == 0) then
  81.         return false
  82.     else return ownerID end
  83. end
  84.  
  85. function checkIfMainBotWithQuarry (qid)
  86.     local setupInformation = sendAndGetResponse(qid,"SETUP","")
  87.     if setupInformation ~= false then
  88.         if setupInformation == "CCQuarry SETUP MAINBOT" then
  89.             return true
  90.         elseif setupInformation == "CCQuarry SETUP SLAVE" then
  91.             return false
  92.         end
  93.     else
  94.         print("Quarry PC did not respond. Moving self out of way and placing other bots in inventory if any.")
  95.         turtle.up()    
  96.         turtle.digUp()
  97.         turtle.up()    
  98.         endWithError()
  99.     end
  100. end
  101.  
  102. function turn(direction)
  103.     if direction == "left" or direction == "right" then
  104.         local d = 0
  105.         if direction == "left" then d = 1; turtle.turnLeft() else d = -1; turtle.turnRight() end
  106.         facing = facing + d
  107.         if (facing == -1) then facing = 3 elseif (facing == 4) then facing = 0 end
  108.         return true
  109.     end
  110.     return false
  111. end
  112.  
  113. -- Function turns to the specified facing location.
  114. function turnToN (face)
  115.     if face == facing then return true end
  116.     local lOrR = (facing - face + 4) % 4
  117.     if lOrR > 2 then
  118.         turn("left")
  119.     elseif lOrR < 2 then
  120.         turn("right")
  121.     else
  122.         turn("left")
  123.         turn("left")
  124.     end
  125.     return true
  126. end
  127. function turnTo (nesw)
  128.     if nesw == "north" then
  129.         turnToN(0)
  130.     elseif nesw == "south" then
  131.         turnToN(2)
  132.     elseif nesw == "west" then
  133.         turnToN(1)
  134.     elseif nesw == "east" then
  135.         turnToN(3)
  136.     end
  137. end
  138. function move (dir)
  139.     sanityFuelCheck()
  140.     if dir == "up" then
  141.         if turtle.up() then
  142.             qu_y = qu_y + 1
  143.             return true
  144.         end
  145.         return false
  146.     elseif dir == "down" then
  147.         if turtle.down() then
  148.             qu_y = qu_y - 1
  149.             return true
  150.         end
  151.         return false
  152.     elseif dir == "north" or dir == "south" or dir == "west" or dir == "east" then
  153.         turnTo(dir)
  154.         local moved = turtle.forward()
  155.         if moved then
  156.             if dir == "north" then qu_z = qu_z + 1 elseif dir == "south" then qu_z = qu_z - 1 elseif dir == "west" then qu_x = qu_x - 1 elseif dir == "east" then qu_x = qu_x + 1 end
  157.             return true
  158.         else return false end
  159.     end
  160.     return false
  161. end
  162. function moveTo (targetX, targetZ)
  163.     local way = "west"
  164.     if targetX > qu_x then
  165.         way = "east"
  166.     end
  167.     while targetX ~= qu_x do move(way) end
  168.     way = "south"
  169.     if targetZ > qu_z then
  170.         way = "north"
  171.     end
  172.     while targetZ ~= qu_z do move(way) end
  173. end
  174.  
  175. local lengthOfCurrentQuarry = 0
  176. local widthOfCurrentQuarry = 1
  177. function doMainbotFunctions(qid)
  178.     -- Setting new orientation without using turn functions so we get our 0 point.
  179.     for side = 1, 4, 1 do
  180.      if turtle.detect() == false then
  181.         break
  182.      end
  183.      turtle.turnLeft()
  184.      if side == 4 then
  185.       print ("Error, you seem to have not given me anywhere to go :(")
  186.         endWithError()
  187.       end  
  188.     end
  189.     facing = 0
  190.     turnTo("east")
  191.  
  192.     -- Give extra fuel just in case!
  193.     while not checkFuel((limitSize * 2) + 32) do
  194.      print("Need more fuel. Add more fuel then type to continue");  
  195.      read()
  196.     end
  197.    
  198.     while move("north") do
  199.         lengthOfCurrentQuarry = lengthOfCurrentQuarry + 1
  200.         if (lengthOfCurrentQuarry >= limitSize) then
  201.             break
  202.         end
  203.     end
  204.     sendMessage(qid, "LENGTH", lengthOfCurrentQuarry)
  205.     for a = 2, lengthOfCurrentQuarry, 1 do
  206.         move("south")
  207.     end
  208.     while move("east") do
  209.         widthOfCurrentQuarry = widthOfCurrentQuarry +  1
  210.         if (widthOfCurrentQuarry >= limitSize) then
  211.             break
  212.         end
  213.     end
  214.     sendMessage(qid, "WIDTH", widthOfCurrentQuarry)
  215.     print("Quarry is "..lengthOfCurrentQuarry.. " long and ".. widthOfCurrentQuarry.." wide")
  216.     for a = 2, widthOfCurrentQuarry, 1 do
  217.         move("west")   
  218.     end
  219.  
  220.     moveTo(-1,0)
  221.     turnTo("south")
  222.     local items = checkChangedSlot({}, false)
  223.     while takeItems(chestDir) do end
  224.     local changedItems = checkChangedSlot(items, true)
  225.     moveTo(0,0)
  226.     turnTo("south")
  227.     setupNewBots(changedItems, qid)
  228.     move("south")
  229. end
  230. function waitForBotToMove()
  231.     while turtle.detect() do sleep(1) end
  232.     return true
  233. end
  234.  
  235. -- Function assumes  it is positioned in the correct location
  236. function setupNewBots (arr, qid)
  237.     sendMessage(qid, "STARTUP", "")
  238.     for botSlot = 1, table.getn(arr), 1 do
  239.         turtle.select(arr[botSlot])
  240.         while (turtle.getItemCount(arr[botSlot]) ~= 0) do
  241.             turtle.place()
  242.             sendMessage(qid, "TURNONBOT","")
  243.             waitForBotToMove()
  244.         end
  245.     end
  246.     sendMessage(qid, "DONESTARTUP", "")
  247. end
  248. function endWithError () os.exit() end
  249.  
  250. -- Function assumes there will be a response... if not youre fucked!
  251. function sendAndGetResponse (recid, messageID, message)
  252.     sendMessage(recid,messageID,message)
  253.     local startedTime = os.clock()
  254.     while (os.clock() - startedTime) <= timeOutOnRednet do
  255.         local id, mess, dis = rednet.receive(timeOutOnRednet)
  256.         if id == recid or recid == 0 then
  257.             local splitMessage = split(mess, " ")
  258.             if (splitMessage[1] == "CCQuarry") then
  259.                 if (splitMessage[2] == messageID) then
  260.                     return mess
  261.                 end
  262.             end
  263.         end
  264.     end
  265.     return false
  266. end
  267. function sendAndGetResponseNoTimeout (recid, messageID, message)
  268.     sendMessage(recid,messageID,message)
  269.     while true do
  270.         local id, mess, dis = rednet.receive()
  271.         print("Received Message: "..mess)
  272.         if id == recid or recid == -1 then
  273.             local splitMessage = split(mess, " ")
  274.             if (splitMessage[1] == "CCQuarry") then
  275.                 if (splitMessage[2] == messageID) then
  276.                     return mess
  277.                 elseif splitMessage[2] == "WORKERS" then
  278.                     sendMessage(id,"WORKERS","")
  279.                 end
  280.             end
  281.         end
  282.     end
  283.     -- I assume this will never be reached but just in case >.>
  284.     return false
  285. end
  286. function sendMessage (qid, messageID, message)
  287.     rednet.send(qid,"CCQuarry "..messageID.." "..message)
  288. end
  289. -- Function assumes no obsticals. Checks if turtle can get to a specified location without fuel. True if needs fuel false if not
  290. function needFuel(currX, currZ, currY, neededX, neededZ, neededY)
  291.     if currY ~= neededY then return true end
  292.     if (currX == neededX) and ((neededZ - currZ) == 1 or (currZ - neededZ) == 1) then
  293.         return false
  294.     elseif (currZ == neededZ) and ((neededX - currX) == 1 or (currX - neededX) == 1) then
  295.         return false
  296.     end
  297.     return true
  298. end
  299.  
  300. local knowsWhichWayIsX = false;
  301. local directionXIs = 0;
  302.  
  303. -- Function WILL NOT move to block. It will not use fuel and it assumes you can actually face the block without moving and still be next to it.
  304. function faceBlock (currX, currZ, currY, neededX, neededZ, neededY)
  305.     if not needFuel(currX, currZ, currY, neededX, neededZ, neededY) then
  306.     --do nothing
  307.     end
  308. end
  309.  
  310. -- Function returns array of the slots it placed the items from or false if could not take items
  311. function takeItems (direction)
  312.     if direction ~= "up" and direction ~= "down" and direction ~= "front" then print("Error direction "..direction.." matched no valid direction takeItems(direction)");    endWithError(); end
  313.     local emptySlot = getNextEmptySlot()
  314.     local checkArr = {}
  315.     if (emptySlot ~= false) then
  316.         turtle.select(emptySlot)
  317.     else
  318.         checkArr = checkChangedSlot({}, false)
  319.     end
  320.     local success = false
  321.     if direction == "up" then
  322.         success = turtle.suckUp()
  323.     elseif direction == "down" then
  324.         success = turtle.suckDown()
  325.     else
  326.         success = turtle.suck()
  327.     end
  328.     if success then
  329.         if emptySlot ~= false then return {emptySlot}
  330.         else
  331.             return checkChangedSlot(checkArr, true)
  332.         end
  333.     else return false; end
  334. end
  335.  
  336. -- Function gives an array of all the slots and their counts if secondCheck is false and array is just a blank array
  337. -- If secondCheck is true it expects the array and loops through until it finds changes. It will return an array of changed slots
  338. -- Useful for when you take from an inventory and it fills a slot up you can find where it put it.
  339. function checkChangedSlot (array, secondCheck)
  340.     local newArr = {}  
  341.     if secondCheck == false then
  342.         for a = 1, 16, 1 do
  343.             newArr[a] = turtle.getItemCount(a)
  344.         end
  345.         return newArr
  346.     else
  347.         if table.getn(array) == 16 then
  348.             for a = 1, 16, 1 do
  349.                 if array[a] ~= turtle.getItemCount(a) then
  350.                     newArr[(table.getn(newArr) + 1)] = a
  351.                 end
  352.             end
  353.             return newArr
  354.         else
  355.             print("Array given to checkChangedSlot is not 16 in length. Cannot continue.")
  356.         end
  357.     end
  358. end
  359.  
  360. function getNextEmptySlot ()
  361.     for a = 1, 16, 1 do
  362.         if turtle.getItemCount(a) == 0 then
  363.             return a
  364.         end
  365.     end
  366.     return false
  367. end
  368.  
  369. function getWorkingLane (qid)
  370.     local dd = sendAndGetResponseNoTimeout(qid, "LANE","")
  371.     return dd
  372. end
  373.  
  374. function requestServiceLane (qid)
  375.     print("Waiting for quarry response")
  376.     local canGoYet = sendAndGetResponseNoTimeout(qid, "SERVICE", "")
  377.  
  378.     if canGoYet == "CCQuarry SERVICE GO" then
  379.         move("up")
  380.         return true
  381.     end
  382.     endWithError()
  383. end
  384.  
  385. function sanityFuelCheck()
  386.     if turtle.getFuelLevel() <= 50 then
  387.         for i=1,16 do
  388.             turtle.select(i)
  389.             if turtle.refuel(1) then
  390.                 if turtle.getFuelLevel() > 50 then
  391.                     return true
  392.                 end
  393.             end
  394.         end
  395.         turtle.select(1)
  396.         return false
  397.     end
  398.     return true
  399. end
  400.  
  401. function fuel()
  402.     while not sanityFuelCheck() do
  403.         print("NEED FUEL")
  404.         read()
  405.     end
  406. end
  407.  
  408. function goDown(doubl)
  409.     fuel()
  410.     if doubl then turtle.dig() end
  411.     return turtle.digDown() or move("down")
  412. end
  413.  
  414. function startDigging(qid,inpar)
  415.     print("Start digging a lane")
  416.     local doubl = true
  417.     if inpar == "SINGLE" then
  418.         doubl = false
  419.     end
  420.     turtle.digDown()
  421.     while goDown(doubl) do end
  422.     while qu_y ~= 0 do
  423.         move("up")
  424.         fuel()
  425.     end
  426. end
  427.  
  428. function makeDropAtStation (qid)
  429.     print("Going to the station")
  430.     moveTo(0,-1)
  431.     move("down")
  432.     turnTo("west")
  433.     for inv = 1, 16, 1 do
  434.         turtle.select(inv)
  435.         turtle.drop()
  436.     end
  437.     turnTo("east")
  438.     return true
  439. end
  440.  
  441. local doneFirstLane = false
  442. local currentLane = {}
  443. local mainbot = false
  444.  
  445. function processLane (qid)
  446.     local getCurrLane = ""
  447.     if doneFirstLane == false and mainbot == true then
  448.         getCurrLane = "CCQuarry LANE 1 1"; doneFirstLane = true
  449.     else
  450.         getCurrLane = getWorkingLane(qid)
  451.     end
  452.     print(getCurrLane)
  453.     if getCurrLane ~= false  then
  454.         if getCurrLane ~= "CCQuarry LANE ENDLANE" then
  455.             local splitMess = split(getCurrLane, " ")
  456.             currentLane[1] = splitMess[3] + 0
  457.             currentLane[2] = splitMess[4] + 0
  458.             turnTo("north")
  459.             print("Supposed to get to ".. currentLane[1] .. " , "..currentLane[2])
  460.             turnTo("east")
  461.             checkFuel(2 + currentLane[1] + currentLane[2] + 256)
  462.             move("up")
  463.             moveTo(currentLane[1] - 1, currentLane[2] - 1)
  464.             move("down")
  465.             sendMessage(qid, "DONESERVICE", "")
  466.             startDigging(qid,tostring(splitMess[5]))
  467.             requestServiceLane(qid)
  468.             makeDropAtStation(qid)
  469.             return true
  470.         else
  471.             return false
  472.         end
  473.     else
  474.         print("Quarry did not respond in time.")
  475.         endWithError()
  476.     end
  477. end
  478.  
  479.  
  480. local MasterID = setupToNearestQuarry()
  481.  
  482. print("Starting Up")
  483. if MasterID ~= false then
  484.     print("Am I Mainbot?")
  485.     mainbot = checkIfMainBotWithQuarry (MasterID)
  486.     if mainbot then doMainbotFunctions(MasterID) end
  487.     while processLane(MasterID) do end
  488.     print ("Requesting again :D!")
  489.     requestServiceLane(MasterID)
  490.     move("up")
  491.     moveTo(currentLane[1] - 1,currentLane[2] - 1)
  492.     move("up")
  493.     sendMessage(qid, "DONESERVICE", "")
  494. else
  495.     print("No Quarry PC found. Please set that up first before me.")
  496.     endWithError()
  497. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement