Advertisement
Lupus590

[CC] quadPipe

Dec 9th, 2015
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.88 KB | None | 0 0
  1. --default values for command line arguments
  2. --users can change these values if they wish, just keep in mind that command line arguments override these at run time
  3. local defaults = {
  4.   quadProgram = {"east","suck","north","west 3","up 54","east","drop","west","down 54", "east 2", "south"},
  5.   numOfQuads = 3,
  6.   delayMult = 1, --without a delay, all of the quads leave at the same time
  7.   ignoreRedstone = false, --quadracopters will run all the time
  8.   anyKeyTerminate = false, --if false then use ctrl-t
  9.   bSide = "left", --side of quadracopter base
  10.   rSide = "back", --side of redstone input
  11. }
  12. --user editables end here
  13.  
  14. local usage = {
  15.   "multiple different args are accepted",
  16.   "if multiple of the same arg are given then the last one will be used - this is redundent don't do it",
  17.   "we will tell you the arg number which caused any errors",
  18.   "  -h | print this usage information",
  19.   "  -f <filePath> | where filePath points to a file which contains the quadProgram",
  20.   "  -n <interger/number> | numOfQuads",
  21.   "  -d <float/number> | delayMult",
  22.   "  -r <side/'ignore'> | redstoneSide or ignoreRedstone = true",
  23.   "  -b <side> | baseSide",
  24.   "  -t <bool> | anyKeyTerminate",
  25. }
  26.  
  27.  
  28.  
  29.  
  30.  
  31. --validate default values
  32.  
  33.  
  34.  
  35. if type(defaults.quadProgram) ~= "table" then
  36.   error("bad default value: defaults.quadProgram\nExpected table")
  37. end
  38.  
  39.  
  40. if type(defaults.numOfQuads) ~= "number" or defaults.numOfQuads < 1 then
  41.  error("bad default value: defaults.numOfQuads\nExpected a whole number greater than 0 (1 or greater are valid)")
  42. else
  43.   local a,b = math.modf(defaults.numOfQuads)
  44.   if b ~= 0 then
  45.     error("bad default value: defaults.numOfQuads\nExpected a whole number greater than 0 (1 or greater are valid)")
  46.   end
  47.  
  48.   if defaults.numOfQuads > 64 then
  49.     print("Warning: numOfQuads is larger than one stack(64). Did the max per base change or are you piping them in?")
  50.   end
  51. end
  52.  
  53.  
  54. if type(defaults.delayMult) ~= "number" then
  55.   error("bad default value: defaults.delayMult\nExpected a number")
  56. end
  57.  
  58.  
  59. if type(defaults.redstoneSide) ~= "string" then
  60.  error("bad default value: defaults.redstoneSide\nExpected a string  representing a side or equal to 'ignore'")
  61. elseif not (defaults.redstoneSide == "ignore" or defaults.redstoneSide == "left" or defaults.redstoneSide == "right" or defaults.redstoneSide == "front" or defaults.redstoneSide == "back") then
  62.   error("bad default value: defaults.redstoneSide\nExpected a string  representing a side or equal to 'ignore'")
  63. end
  64.  
  65.  
  66. if type(defaults.baseSide) ~= "string" then
  67.   error("bad default value: defaults.redstoneSide\nExpected a string  representing a side")
  68. elseif not (b == "left" or b == "right" or b == "front" or b == "back") then
  69.   error("bad default value: defaults.redstoneSide\nExpected a string  representing a side")
  70. end
  71.  
  72.  
  73. if type(defaults.anyKeyTerminate) ~= "boolean" then
  74.   error("bad default value: defaults.anyKeyTerminate\nExpected a boolean")
  75. end
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83. local function printUsage()
  84.   print(table.concat(usage, "\n"))
  85. end
  86.  
  87. local function myError(msg, lvl)
  88.   printUsage()
  89.   error(msg, lvl+1)
  90. end
  91.  
  92. local arg = {...}
  93. --set vaiables to default, we will overide these with the arg later
  94. local quadProgram = defaults.quadProgram
  95. local numOfQuads = defaults.numOfQuads
  96. local delayMult = defaults.delayMult
  97. local ignoreRedstone = defaults.ignoreRedstone
  98. local anyKeyTerminate = defaults.anyKeyTerminate
  99. local bSide = defaults.bSide
  100. local rSide = defaults.rSide
  101.  
  102. if #arg > 0 then
  103.   --lower case everything
  104.   for i = 1, #arg do
  105.     if not tonumber(arg[i]) then
  106.       arg[i] = string.lower(arg[i])
  107.     end
  108.   end--]]
  109.   --process arg
  110.   local i = 1
  111.   while i <= #arg do
  112.     local doubleInc = false
  113.     if arg[i] == "-h" then
  114.       printUsage()
  115.     elseif arg[i] == "-f" then --quadProgram
  116.       do --make my editor look clearer
  117.       if not (fs.exists(tostring(arg[i+1])) and (not fs.isDir(tostring(arg[i+1])))) then
  118.         myError("arg "..i.." Can't load file, either doesn't exist or is a directory", 2)
  119.       else
  120.         local f = fs.open(arg[i+1], "r")
  121.         local p = f.readAll()
  122.         f.close()
  123.         quadProgram = textutils.unserialize(p)
  124.         if quadProgram == nil then error("unable to understand file: "..tostring(arg[i+1]))
  125.       end
  126.       doubleInc = true
  127.       end --make my editor look clearer
  128.     elseif arg[i] == "-n" then --numOfQuads
  129.       do --make my editor look clearer
  130.       local n = tonumber(arg[i+1])
  131.       if n == nil or n < 1 then
  132.         myError("arg "..i.." -n arg must be followed by a whole number greater than 0 (1 or greater are valid)", 2)
  133.       else
  134.         local a,b = math.modf(n)
  135.         if b ~= 0 then
  136.           myError("arg "..i.." -n arg must be followed by a whole number greater than 0 (1 or greater are valid)", 2)
  137.         end
  138.       end
  139.       if n > 64 then
  140.         print("arg "..i.." Warning: numOfQuads is larger than one stack(64). Did the max per base change or are you piping them in?")
  141.       end
  142.       numOfQuads = n
  143.       doubleInc = true
  144.       end --make my editor look clearer
  145.     elseif arg[i] == "-d" then --delayMult
  146.       do --make my editor look clearer
  147.       local d = tonumber(arg[i+1])
  148.       if d == nil then
  149.         myError("arg "..i.." -d arg must be followed by a number", 2)
  150.       end
  151.       delayMult = d
  152.       doubleInc = true
  153.       end --make my editor look clearer
  154.     elseif arg[i] == "-r" then --redstoneSide
  155.       do --make my editor look clearer
  156.       local r = tostring(arg[i+1])
  157.       if r == nil then
  158.        myError("arg "..i.." -r arg must be followed by a string representing a side or equal to 'ignore'", 2)
  159.       end
  160.       if r == "ignore" then
  161.         ignoreRedstone = true
  162.       elseif r == "left" or r == "right" or r == "front" or r == "back" then
  163.         rSide = r
  164.         ignoreRedstone  = false
  165.       else
  166.         myError("arg "..i.." -r arg must be followed by a string representing a side or equal to 'ignore'", 2)
  167.       end
  168.       doubleInc = true
  169.       end --make my editor look clearer
  170.     elseif arg[i] == "-b" then --baseSide
  171.       do --make my editor look clearer
  172.       local b = tostring(arg[i+1])
  173.       if b == nil then
  174.         myError("arg "..i.." -b arg must be followed by a string representing a side", 2)
  175.       end
  176.       if b == "left" or b == "right" or b == "front" or b == "back" then
  177.         bSide = b
  178.       else
  179.         myError("arg "..i.." -b arg must be followed by a string representing a side", 2)
  180.       end
  181.       doubleInc = true
  182.       end --make my editor look clearer
  183.     elseif arg[i] == "-t" then --anyKeyTerminate
  184.       do --make my editor look clearer
  185.       local t = tostring(arg[i+1])
  186.       if t == nil then
  187.         myError("arg "..i.." -t arg must be followed by a string equal to 'true' or 'false'", 2)
  188.       end
  189.       if t == "true" then
  190.         anyKeyTerminate = true
  191.       elseif t == "false" then
  192.         anyKeyTerminate = false
  193.       else
  194.         myError("arg "..i.." -t arg must be followed by a string equal to 'true' or 'false'", 2)
  195.       end
  196.       doubleInc = true
  197.       end --make my editor look clearer
  198.     else
  199.       myError("arg "..i.." unprocessed arg",2)
  200.     end
  201.     if doubleInc then
  202.       i = i +2
  203.     else
  204.       i = i +1
  205.     end
  206.   end
  207. end
  208.  
  209. --main program
  210. local base = peripheral.wrap(bSide)
  211.  
  212.  
  213. print("Lupus590's Quadracopter Program\nLicenced under MIT") --http://opensource.org/licenses/mit-license.php
  214. if anyKeyTerminate then
  215.   print("Press any key to terminate.")
  216. else
  217.   print("Press ctrl-t to terminate.")
  218. end
  219.  
  220. local event
  221. if ignoreRedstone then
  222.   --launch all drones
  223.   local i = 1
  224.   for i = 1, numOfQuads do
  225.     base.flyQuad(quadProgram)
  226.     sleep(delayMult*#quadProgram/numOfQuads)--don't want them all to go at the same time, we want roughly even spacing
  227.   end
  228.   while true do
  229.     event = os.pullEvent()
  230.     --no rest till we are done
  231.     if event == "quad_landed" then
  232.       base.flyQuad(quadProgram)
  233.     elseif anyKeyTerminate and event == "key" then
  234.       return
  235.     end    
  236.   end--while true
  237. else
  238.   if redstone.getInput(rSide) then os.queueEvent("redstone") end --if redstone is already on then we need to simulate an update to do our stuff
  239.   while true do
  240.     event = os.pullEvent()
  241.     if event == "redstone" then
  242.       --launch all drones
  243.       local i = 1
  244.       for i = 1, numOfQuads do
  245.         base.flyQuad(quadProgram)
  246.         sleep(delayMult*#quadProgram/numOfQuads)--don't want them all to go at the same time, we want roughly even spacing
  247.       end
  248.       while redstone.getInput(rSide) do
  249.         event = os.pullEvent()
  250.         --no rest till we are done
  251.         if event == "quad_landed" and redstone.getInput(rSide) then
  252.           base.flyQuad(quadProgram)
  253.         elseif anyKeyTerminate and event == "key" then
  254.           return
  255.         end  
  256.       end --while redstone
  257.    
  258.     elseif anyKeyTerminate and event == "key" then
  259.       return
  260.     end --if event == redstone
  261.   end --while true
  262. end--if ignoreRedstone
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement