Advertisement
guitarplayer616

Multisetup TEST w / max efficiency

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