Advertisement
guitarplayer616

Multisetup TEST

Jan 29th, 2017
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 = {}
  129.     for i,v in pairs(reference) do
  130.          ref[i] = v
  131.     end
  132.     assert(ref)
  133.     ref.starty = v.starty
  134.     ref.startz = v.startz
  135.     ref.finaly = v.finaly
  136.     ref.finalz = v.finalz
  137.     assert(ref.startx)
  138.     assert(v.starty)
  139.     assert(ref.finalx)
  140.     assert(v.finaly)
  141.     local ins = blueprint(ref.startx,v.starty,v.startz,ref.finalx,v.finaly,v.finalz)
  142.     if tArgs[2] == "tsp" then
  143.         ins = improveBlueprint(ins,ref.startx,ref.starty,ref.startz,ref.finalx,ref.finaly,ref.finalz)
  144.     end
  145.     local fname = saveBlueprint(ref,slots,ins,uniqueblocks,nTurtle)
  146.     print(fname," saved")
  147.  
  148.     for i,v in pairs(fs.list(shell.dir())) do
  149.         if v:find("["..tostring(nTurtle).."]") then
  150.             shell.run("copy "..tostring(v).." disk")
  151.             if nTurtle ~= 1 then
  152.                 shell.run("rm "..tostring(v))
  153.             end
  154.         end
  155.     end
  156. end
  157.  
  158. function setPosition(nTurtle,heightPos,widthPos,lengthPos,face)
  159.     nTurtle = tostring(nTurtle)
  160.     local h = fs.open("pos"..nTurtle,"w")
  161.     h.writeLine("heightPos = "..tostring(heightPos)..";")
  162.     h.writeLine("widthPos = "..tostring(widthPos)..";")
  163.     h.writeLine("lengthPos = "..tostring(lengthPos)..";")
  164.     h.writeLine("face = "..tostring(face)..";")
  165.     h.close()
  166.     shell.run("copy pos"..nTurtle.." disk")
  167. end
  168.  
  169. function Main()
  170.     shell.run("turtle reset")
  171.     if turtle.getFuelLevel() < 200 then
  172.         error("more fuel required")
  173.     end
  174.  
  175.     shell.run(tArgs[1])
  176.  
  177.     print("Please put ...\n"..tonumber(reference.multiturtle).." turtles(any type),\n1 disk, and \n1 diskDrive in inventory\n")
  178.     print("press any key to continue")
  179.     os.pullEvent("char")
  180.     for i = 2,reference.multiturtle do
  181.         turtle.select(find("turtle"))
  182.         local nTurtle = i
  183.         goto(reference.startx,reference.starty+i-1,reference.startz-1)
  184.         turn("south")
  185.         turtle.place()
  186.         turtle.select(find("peripheral"))
  187.         goto(reference.startx+1,reference.starty+i-1,reference.startz-1)
  188.         turn("south")
  189.         turtle.place()
  190.         turtle.select(find("computercraft:disk"))
  191.         turtle.drop()
  192.         --copyFiles(i)
  193.         do
  194.             files2disk()
  195.             transferBlueprint(nTurtle)
  196.             --setPosition(nTurtle,heightPos-1,widthPos,lengthPos-1,face)
  197.             local disk = fs.open("disk/startup","w")
  198.             disk.write([[
  199.             local function disk2turtle(nTurtle)
  200.                 local files = {"refill","setup","textutilsFIX","time","turtle"}
  201.                 for i,v in pairs(fs.list("disk")) do
  202.                     if v:find("["..tostring(nTurtle).."]") then
  203.                         shell.run("copy disk/"..tostring(v).." "..tostring(v))
  204.                         shell.run("rm disk/"..tostring(v))
  205.                     end
  206.                 end
  207.                 for i,v in pairs(files) do
  208.                     if fs.exists("disk/"..tostring(v)) then
  209.                         shell.run("copy disk/"..tostring(v).." "..tostring(v))
  210.                     end
  211.                 end
  212.             end
  213.            
  214.             local function checkWrench()
  215.                 for i = 1,16 do
  216.                     local item = turtle.getItemDetail(i)
  217.                     if item then
  218.                         local possible = item.name:lower()
  219.                         print(possible)
  220.                         if possible:find("wrench") or possible:find("hammer") then
  221.                             return true
  222.                         end
  223.                     end
  224.                 end
  225.                 return false
  226.             end
  227.  
  228.             function countBlocks(instructions)
  229.                 uniqueblocks={}
  230.                 for n = 1,#instructions do
  231.                     local blockID,data = unpack(instructions[n],4,5)
  232.                     found = false
  233.                     for j,w in ipairs(uniqueblocks) do
  234.              
  235.                         if (w.blockID==blockID) and (w.data==data) then
  236.                             found = true
  237.                             w.amount = w.amount + 1
  238.                             break
  239.                         end
  240.                     end
  241.              
  242.                     if found==false then
  243.                         uniqueblocks[#uniqueblocks+1] = {}
  244.                         uniqueblocks[#uniqueblocks].blockID = blockID
  245.                         uniqueblocks[#uniqueblocks].data = data
  246.                         uniqueblocks[#uniqueblocks].amount = 1
  247.                     end
  248.                 end
  249.                 return uniqueblocks
  250.             end
  251.  
  252.             function printMaterials(uniqueblocks,slots)
  253.                 local w,h = term.getSize()
  254.                 local buffer = {}
  255.  
  256.                 buffer[#buffer+1] = "Controls:"
  257.                 buffer[#buffer+1] = ""
  258.                 buffer[#buffer+1] = "use arrowkeys or scrollwheel to scroll"
  259.                 buffer[#buffer+1] = "press x to refuel"
  260.                 buffer[#buffer+1] = "press a when all materials are accounted for"
  261.                 buffer[#buffer+1] = ""
  262.  
  263.                 for i,v in pairs(uniqueblocks) do
  264.                     --print(v.blockID,",",v.data,"=",v.amount)
  265.                     local t = slots[v.blockID][v.data]
  266.                     buffer[#buffer+1] = "- "..tostring(t[1])..","..tostring(t[2])
  267.                     buffer[#buffer+1] = "    "..tostring(v.amount)
  268.                     if v.amount > 64 then
  269.                         buffer[#buffer] = buffer[#buffer] .. " ("..tostring(math.ceil(v.amount/64)).." stacks)"
  270.                     end
  271.                 end
  272.  
  273.                 buffer[#buffer+1] = ""
  274.                 if reference.wrench then
  275.                     buffer[#buffer+1] = "make sure I have a wrench"
  276.                 else
  277.                     buffer[#buffer+1] = "no wrench required"
  278.                 end
  279.  
  280.                 local function display(buffer,n)
  281.                     shell.run("clr")
  282.                     local a = 1
  283.                     for i = n,#buffer do
  284.                         term.setCursorPos(1,a)
  285.                         --term.clearLine()
  286.                         term.write(buffer[i])
  287.                         a = a + 1
  288.                     end
  289.                 end
  290.  
  291.                 local n = 1
  292.                 display(buffer,n)
  293.                 while true do
  294.                     local event = {os.pullEvent()}
  295.                     if event[1] == "mouse_scroll" then
  296.                         if event[2] == 1 and n < #buffer then
  297.                             n = n + 1
  298.                             display(buffer,n)
  299.                         elseif event[2] == -1 and n > 1 then
  300.                             n = n - 1
  301.                             display(buffer,n)
  302.                         end
  303.                     end
  304.                     if event[2] == "x" then
  305.                         shell.run("refuel 64")
  306.                         buffer[#buffer+1] = "fuel: "..tostring(turtle.getFuelLevel())
  307.                         display(buffer,n)
  308.                     end
  309.                     if event[2] == "a" then
  310.                         local ready = true
  311.                         if turtle.getFuelLevel() < 200 then
  312.                             buffer[#buffer+1] = "needs more fuel to start"
  313.                             ready = false
  314.                         end
  315.                         if not checkWrench() then
  316.                             buffer[#buffer+1] = "needs a wrench"
  317.                             ready = false
  318.                         end
  319.                         if ready then
  320.                             buffer[#buffer+1] = "ready to go!, waiting on rs signal"
  321.                         end
  322.                         display(buffer,n)
  323.                     end
  324.                     if event[1] == "redstone" then
  325.                         shell.run("clr")
  326.                         break
  327.                     end
  328.                 end
  329.             end
  330.             disk2turtle(]]..nTurtle..[[)
  331.             shell.run("turtle ]]..tostring(heightPos-1).." "..tostring(widthPos).." "..tostring(lengthPos+1).." "..tostring(face)..[[")
  332.            
  333.            
  334.             rs.setOutput("back",true)
  335.             sleep(5)
  336.             rs.setOutput("back",false)
  337.             local filename
  338.             for _,v in pairs(fs.list(shell.dir())) do
  339.                 if v:find("blueprint") then
  340.                     filename = v
  341.                     shell.run(v)
  342.                 end
  343.             end
  344.             updateVar(filename,"reference.returnx",]]..tostring(heightPos-1)..[[)
  345.             updateVar(filename,"reference.returny",]]..tostring(widthPos)..[[)
  346.             updateVar(filename,"reference.returnz",]]..tostring(lengthPos)..[[)
  347.            
  348.             local ublocks = countBlocks(instructions)
  349.             printMaterials(ublocks,slots)
  350.            
  351.             --send pulse through turtles to start
  352.             --turtles only start if all are ready
  353.            
  354.             shell.run("setup "..filename)
  355.             rs.setOutput("left",true)
  356.             shell.run("startup")
  357.             ]])
  358.             disk.close()
  359.         end
  360.        
  361.         goto(reference.startx,reference.starty+i-1,reference.startz-1)
  362.         turn("south")
  363.         peripheral.call("front","turnOn")
  364.         os.pullEvent("redstone")
  365.         goto(reference.startx+1,reference.starty+i-1,reference.startz-1)
  366.         turn("south")
  367.         turtle.suck()
  368.         turtle.dig()
  369.     end
  370.     goto(reference.startx,reference.starty,reference.startz-1)
  371.     turn("south")
  372.     goto(reference.startx,reference.starty,reference.startz)
  373. end
  374.  
  375. function start()
  376.     --shell.run(tArgs[1])
  377.     --fuelcheck
  378.     --materialscheck
  379.     shell.run("setup "..tArgs[1])
  380.     rs.setOutput("left",false)
  381.     rs.setOutput("left",true)
  382.     shell.run("startup")
  383. end
  384.  
  385. Main()
  386. local ublocks = countBlocks(instructions)
  387. printMaterials(ublocks,slots)
  388. print("type \"start\" when all turtles are ready")
  389. local s = read()
  390. if s == "start" then
  391.     start()
  392. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement