Advertisement
guitarplayer616

multisetup Working!

Jan 21st, 2017
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.29 KB | None | 0 0
  1. local tArgs = {...}
  2.  
  3. if #tArgs == 0 then
  4.     error("please select blueprint file")
  5. end
  6.  
  7. if not fs.exists(tArgs[1]) then
  8.     error(tArgs[1].. " does not exists")
  9. end
  10.  
  11. if not fs.exists("turtle") then
  12.     shell.run("pastebin get 7mnsWVwz turtle")
  13. end
  14.  
  15. rs.setOutput("left",false)
  16. local function find(keyword)
  17.     for i = 1,16 do
  18.         local item = turtle.getItemDetail(i)
  19.         if item and item.name:lower():find(keyword:lower()) then
  20.             return i
  21.         end
  22.     end
  23. end
  24.  
  25. function countBlocks(instructions)
  26.     uniqueblocks={}
  27.     for n = 1,#instructions do
  28.         local blockID,data = unpack(instructions[n],4,5)
  29.         found = false
  30.         for j,w in ipairs(uniqueblocks) do
  31.  
  32.             if (w.blockID==blockID) and (w.data==data) then
  33.                 found = true
  34.                 w.amount = w.amount + 1
  35.                 break
  36.             end
  37.         end
  38.  
  39.         if found==false then
  40.             uniqueblocks[#uniqueblocks+1] = {}
  41.             uniqueblocks[#uniqueblocks].blockID = blockID
  42.             uniqueblocks[#uniqueblocks].data = data
  43.             uniqueblocks[#uniqueblocks].amount = 1
  44.         end
  45.     end
  46.     return uniqueblocks
  47. end
  48.  
  49. function printMaterials(uniqueblocks,slots)
  50.     local w,h = term.getSize()
  51.     local buffer = {}
  52.  
  53.     buffer[#buffer+1] = "Controls:"
  54.     buffer[#buffer+1] = ""
  55.     buffer[#buffer+1] = "use arrowkeys or scrollwheel to scroll"
  56.     buffer[#buffer+1] = "press x to refuel"
  57.     buffer[#buffer+1] = "press a when all materials are accounted for"
  58.     buffer[#buffer+1] = ""
  59.  
  60.     for i,v in pairs(uniqueblocks) do
  61.         --print(v.blockID,",",v.data,"=",v.amount)
  62.         local t = slots[v.blockID][v.data]
  63.         buffer[#buffer+1] = "- "..tostring(t[1])..","..tostring(t[2])
  64.         buffer[#buffer+1] = "    "..tostring(v.amount)
  65.         if v.amount > 64 then
  66.             buffer[#buffer] = buffer[#buffer] .. " ("..tostring(math.ceil(v.amount/64)).." stacks)"
  67.         end
  68.     end
  69.  
  70.     buffer[#buffer+1] = ""
  71.     if reference.wrench then
  72.         buffer[#buffer+1] = "make sure I have a wrench"
  73.     else
  74.         buffer[#buffer+1] = "no wrench required"
  75.     end
  76.    
  77.     local function display(buffer,n)
  78.         shell.run("clr")
  79.         local a = 1
  80.         for i = n,#buffer do
  81.             term.setCursorPos(1,a)
  82.             --term.clearLine()
  83.             term.write(buffer[i])
  84.             a = a + 1
  85.         end
  86.     end
  87.  
  88.     local n = 1
  89.     display(buffer,n)
  90.     while true do
  91.         local event = {os.pullEvent()}
  92.         if event[1] == "mouse_scroll" then
  93.             if event[2] == 1 and n < #buffer then
  94.                 n = n + 1
  95.                 display(buffer,n)
  96.             elseif event[2] == -1 and n > 1 then
  97.                 n = n - 1
  98.                 display(buffer,n)
  99.             end
  100.         end
  101.         if event[2] == "x" then
  102.             shell.run("refuel 64")
  103.             buffer[#buffer+1] = "fuel: "..tostring(turtle.getFuelLevel())
  104.             display(buffer,n)
  105.         end
  106.         if event[2] == "a" then
  107.             shell.run("clr")
  108.             break
  109.         end
  110.     end
  111. end
  112.        
  113.  
  114. function files2disk()
  115.     local files = {"refill","setup","textutilsFIX","time","turtle"}
  116.    
  117.     for _,v in pairs(files) do
  118.         if fs.exists(v) and fs.exists("disk/"..tostring(v)) == false then
  119.             shell.run("copy "..tostring(v).." disk")
  120.         end
  121.     end
  122. end
  123.  
  124. function transferBlueprint(nTurtle)
  125.  
  126.     --[[local v = startLocations[nTurtle]
  127.     assert(v)
  128.     local ref = reference
  129.     assert(ref)
  130.     ref.starty = v.starty
  131.     ref.startz = v.startz
  132.     ref.finaly = v.finaly
  133.     ref.finalz = v.finalz
  134.     assert(ref.startx)
  135.     assert(v.starty)
  136.     assert(ref.finalx)
  137.     assert(v.finaly)
  138.     local ins = blueprint(ref.startx,v.starty,v.startz,ref.finalx,v.finaly,v.finalz)
  139.     if tArgs[2] == "tsp" then
  140.         ins = improveBlueprint(ins,ref.startx,ref.starty,ref.startz,ref.finalx,ref.finaly,ref.finalz)
  141.     end
  142.     local fname = saveBlueprint(ref,slots,ins,uniqueblocks,nTurtle)
  143.     print(fname," saved")]]
  144.  
  145.     for i,v in pairs(fs.list(shell.dir())) do
  146.         if v:find("["..tostring(nTurtle).."]") then
  147.             shell.run("copy "..tostring(v).." disk")
  148.             shell.run("rm "..tostring(v))
  149.         end
  150.     end
  151. end
  152.  
  153. function setPosition(nTurtle,heightPos,widthPos,lengthPos,face)
  154.     nTurtle = tostring(nTurtle)
  155.     local h = fs.open("pos"..nTurtle,"w")
  156.     h.writeLine("heightPos = "..tostring(heightPos)..";")
  157.     h.writeLine("widthPos = "..tostring(widthPos)..";")
  158.     h.writeLine("lengthPos = "..tostring(lengthPos)..";")
  159.     h.writeLine("face = "..tostring(face)..";")
  160.     h.close()
  161.     shell.run("copy pos"..nTurtle.." disk")
  162. end
  163.  
  164. function Main()
  165.     shell.run("turtle reset")
  166.     if turtle.getFuelLevel() < 200 then
  167.         error("more fuel required")
  168.     end
  169.  
  170.     shell.run(tArgs[1])
  171.  
  172.     print("Please put ...\n"..tonumber(reference.multiturtle).." turtles(any type),\n1 disk, and \n1 diskDrive in inventory\n")
  173.     print("press any key to continue")
  174.     os.pullEvent("char")
  175.     for i = 2,reference.multiturtle do
  176.         turtle.select(find("turtle"))
  177.         local nTurtle = i
  178.         goto(reference.startx,reference.starty+i-1,reference.startz-1)
  179.         turn("south")
  180.         turtle.place()
  181.         turtle.select(find("peripheral"))
  182.         goto(reference.startx+1,reference.starty+i-1,reference.startz-1)
  183.         turn("south")
  184.         turtle.place()
  185.         turtle.select(find("computercraft:disk"))
  186.         turtle.drop()
  187.         --copyFiles(i)
  188.         do
  189.             files2disk()
  190.             transferBlueprint(nTurtle)
  191.             --setPosition(nTurtle,heightPos-1,widthPos,lengthPos-1,face)
  192.             local disk = fs.open("disk/startup","w")
  193.             disk.write([[
  194.             local function disk2turtle(nTurtle)
  195.                 local files = {"refill","setup","textutilsFIX","time","turtle"}
  196.                 for i,v in pairs(fs.list("disk")) do
  197.                     if v:find("["..tostring(nTurtle).."]") then
  198.                         shell.run("copy disk/"..tostring(v).." "..tostring(v))
  199.                         shell.run("rm disk/"..tostring(v))
  200.                     end
  201.                 end
  202.                 for i,v in pairs(files) do
  203.                     if fs.exists("disk/"..tostring(v)) then
  204.                         shell.run("copy disk/"..tostring(v).." "..tostring(v))
  205.                     end
  206.                 end
  207.             end
  208.            
  209.             local function checkWrench()
  210.                 for i = 1,16 do
  211.                     local item = turtle.getItemDetail(i)
  212.                     if item then
  213.                         local possible = item.name:lower()
  214.                         print(possible)
  215.                         if possible:find("wrench") or possible:find("hammer") then
  216.                             return true
  217.                         end
  218.                     end
  219.                 end
  220.                 return false
  221.             end
  222.  
  223.             function countBlocks(instructions)
  224.                 uniqueblocks={}
  225.                 for n = 1,#instructions do
  226.                     local blockID,data = unpack(instructions[n],4,5)
  227.                     found = false
  228.                     for j,w in ipairs(uniqueblocks) do
  229.              
  230.                         if (w.blockID==blockID) and (w.data==data) then
  231.                             found = true
  232.                             w.amount = w.amount + 1
  233.                             break
  234.                         end
  235.                     end
  236.              
  237.                     if found==false then
  238.                         uniqueblocks[#uniqueblocks+1] = {}
  239.                         uniqueblocks[#uniqueblocks].blockID = blockID
  240.                         uniqueblocks[#uniqueblocks].data = data
  241.                         uniqueblocks[#uniqueblocks].amount = 1
  242.                     end
  243.                 end
  244.                 return uniqueblocks
  245.             end
  246.  
  247.             function printMaterials(uniqueblocks,slots)
  248.                 local w,h = term.getSize()
  249.                 local buffer = {}
  250.  
  251.                 buffer[#buffer+1] = "Controls:"
  252.                 buffer[#buffer+1] = ""
  253.                 buffer[#buffer+1] = "use arrowkeys or scrollwheel to scroll"
  254.                 buffer[#buffer+1] = "press x to refuel"
  255.                 buffer[#buffer+1] = "press a when all materials are accounted for"
  256.                 buffer[#buffer+1] = ""
  257.  
  258.                 for i,v in pairs(uniqueblocks) do
  259.                     --print(v.blockID,",",v.data,"=",v.amount)
  260.                     local t = slots[v.blockID][v.data]
  261.                     buffer[#buffer+1] = "- "..tostring(t[1])..","..tostring(t[2])
  262.                     buffer[#buffer+1] = "    "..tostring(v.amount)
  263.                     if v.amount > 64 then
  264.                         buffer[#buffer] = buffer[#buffer] .. " ("..tostring(math.ceil(v.amount/64)).." stacks)"
  265.                     end
  266.                 end
  267.  
  268.                 buffer[#buffer+1] = ""
  269.                 if reference.wrench then
  270.                     buffer[#buffer+1] = "make sure I have a wrench"
  271.                 else
  272.                     buffer[#buffer+1] = "no wrench required"
  273.                 end
  274.  
  275.                 local function display(buffer,n)
  276.                     shell.run("clr")
  277.                     local a = 1
  278.                     for i = n,#buffer do
  279.                         term.setCursorPos(1,a)
  280.                         --term.clearLine()
  281.                         term.write(buffer[i])
  282.                         a = a + 1
  283.                     end
  284.                 end
  285.  
  286.                 local n = 1
  287.                 display(buffer,n)
  288.                 while true do
  289.                     local event = {os.pullEvent()}
  290.                     if event[1] == "mouse_scroll" then
  291.                         if event[2] == 1 and n < #buffer then
  292.                             n = n + 1
  293.                             display(buffer,n)
  294.                         elseif event[2] == -1 and n > 1 then
  295.                             n = n - 1
  296.                             display(buffer,n)
  297.                         end
  298.                     end
  299.                     if event[2] == "x" then
  300.                         shell.run("refuel 64")
  301.                         buffer[#buffer+1] = "fuel: "..tostring(turtle.getFuelLevel())
  302.                         display(buffer,n)
  303.                     end
  304.                     if event[2] == "a" then
  305.                         local ready = true
  306.                         if turtle.getFuelLevel() < 200 then
  307.                             buffer[#buffer+1] = "needs more fuel to start"
  308.                             ready = false
  309.                         end
  310.                         if not checkWrench() then
  311.                             buffer[#buffer+1] = "needs a wrench"
  312.                             ready = false
  313.                         end
  314.                         if ready then
  315.                             buffer[#buffer+1] = "ready to go!, waiting on rs signal"
  316.                         end
  317.                         display(buffer,n)
  318.                     end
  319.                     if event[1] == "redstone" then
  320.                         shell.run("clr")
  321.                         break
  322.                     end
  323.                 end
  324.             end
  325.             disk2turtle(]]..nTurtle..[[)
  326.             shell.run("turtle ]]..tostring(heightPos-1).." "..tostring(widthPos).." "..tostring(lengthPos+1).." "..tostring(face)..[[")
  327.            
  328.            
  329.             rs.setOutput("back",true)
  330.             sleep(5)
  331.             rs.setOutput("back",false)
  332.             local filename
  333.             for _,v in pairs(fs.list(shell.dir())) do
  334.                 if v:find("blueprint") then
  335.                     filename = v
  336.                     shell.run(v)
  337.                 end
  338.             end
  339.             updateVar(filename,"reference.returnx",]]..tostring(heightPos-1)..[[)
  340.             updateVar(filename,"reference.returny",]]..tostring(widthPos)..[[)
  341.             updateVar(filename,"reference.returnz",]]..tostring(lengthPos)..[[)
  342.            
  343.             local ublocks = countBlocks(instructions)
  344.             printMaterials(ublocks,slots)
  345.            
  346.             --send pulse through turtles to start
  347.             --turtles only start if all are ready
  348.            
  349.             shell.run("setup "..filename)
  350.             rs.setOutput("left",true)
  351.             shell.run("startup")
  352.             ]])
  353.             disk.close()
  354.         end
  355.        
  356.         goto(reference.startx,reference.starty+i-1,reference.startz-1)
  357.         turn("south")
  358.         peripheral.call("front","turnOn")
  359.         os.pullEvent("redstone")
  360.         goto(reference.startx+1,reference.starty+i-1,reference.startz-1)
  361.         turn("south")
  362.         turtle.suck()
  363.         turtle.dig()
  364.     end
  365.     goto(reference.startx,reference.starty,reference.startz-1)
  366.     turn("south")
  367.     goto(reference.startx,reference.starty,reference.startz)
  368. end
  369.  
  370. function start()
  371.     shell.run(tArgs[1])
  372.     --fuelcheck
  373.     --materialscheck
  374.     shell.run("setup "..tArgs[1])
  375.     rs.setOutput("left",false)
  376.     rs.setOutput("left",true)
  377.     shell.run("startup")
  378. end
  379.  
  380. Main()
  381. local ublocks = countBlocks(instructions)
  382. printMaterials(ublocks,slots)
  383. print("type \"start\" when all turtles are ready")
  384. local s = read()
  385. if s == "start" then
  386.     start()
  387. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement