Guest User

[CC] quadPipe - bug has been fixed, still WIP

a guest
Nov 23rd, 2015
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.79 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 that arg number which causes 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. local function printUsage()
  28.   print(table.concat(usage, "\n"))
  29. end
  30.  
  31. local function myError(msg, lvl)
  32.   printUsage()
  33.   error(msg, lvl+1)
  34. end
  35.  
  36. local arg = {...}
  37. --set vaiables to default, we will overide these with the arg later
  38. local quadProgram = defaults.quadProgram
  39. local numOfQuads = defaults.numOfQuads
  40. local delayMult = defaults.delayMult
  41. local ignoreRedstone = defaults.ignoreRedstone
  42. local anyKeyTerminate = defaults.anyKeyTerminate
  43. local bSide = defaults.bSide
  44. local rSide = defaults.rSide
  45.  
  46. if #arg > 0 then
  47.   --[[]]--removed for debug
  48.   --lower case everything
  49.   for i = 1, #arg do
  50.     if not tonumber(arg[i]) then
  51.       arg[i] = string.lower(arg[i])
  52.     end
  53.   end--]]
  54.   --process arg
  55.   local i = 1
  56.   while i <= #arg do
  57.     local doubleInc = false
  58.     if arg[i] == "-h" then
  59.       printUsage()
  60.     elseif arg[i] == "-f" then --quadProgram
  61.       do --make my editor look clearer
  62.       myError("arg "..i.." Can't load file", 2)
  63.       doubleInc = true
  64.       end --make my editor look clearer
  65.     elseif arg[i] == "-n" then --numOfQuads
  66.       do --make my editor look clearer
  67.       local n = tonumber(arg[i+1])
  68.       if n == nil or n < 1 then
  69.         myError("arg "..i.." -n arg must be followed by a whole number greater than 0 (1 or greater are valid)", 2)
  70.       else
  71.         local a,b = math.modf(n)
  72.         if b ~= 0 then
  73.           myError("arg "..i.." -n arg must be followed by a whole number greater than 0 (1 or greater are valid)", 2)
  74.         end
  75.       end
  76.       if n > 64 then
  77.         print("arg "..i.." Warning: numOfQuads is larger than one stack(64). Did the max per base change or are you piping them in?")
  78.       end
  79.       numOfQuads = n
  80.       doubleInc = true
  81.       end --make my editor look clearer
  82.     elseif arg[i] == "-d" then --delayMult
  83.       do --make my editor look clearer
  84.       local d = tonumber(arg[i+1])
  85.       if d == nil then
  86.         myError("arg "..i.." -d arg must be followed by a number", 2)
  87.       end
  88.       delayMult = d
  89.       doubleInc = true
  90.       end --make my editor look clearer
  91.     elseif arg[i] == "-r" then --redstoneSide
  92.       do --make my editor look clearer
  93.       local r = tostring(arg[i+1])
  94.       if r == nil then
  95.        myError("arg "..i.." -r arg must be followed by a string representing a side or equal to 'ignore'", 2)
  96.       end
  97.       if r == "ignore" then
  98.         ignoreRedstone = true
  99.       elseif r == "left" or r == "right" or r == "front" or r == "back" then
  100.         rSide = r
  101.         ignoreRedstone  = false
  102.       else
  103.         myError("arg "..i.." -r arg must be followed by a string representing a side or equal to 'ignore'", 2)
  104.       end
  105.       doubleInc = true
  106.       end --make my editor look clearer
  107.     elseif arg[i] == "-b" then --baseSide
  108.       do --make my editor look clearer
  109.       local b = tostring(arg[i+1])
  110.       if b == nil then
  111.         myError("arg "..i.." -b arg must be followed by a string representing a side", 2)
  112.       end
  113.       if b == "left" or b == "right" or b == "front" or b == "back" then
  114.         bSide = b
  115.       else
  116.         myError("arg "..i.." -b arg must be followed by a string representing a side", 2)
  117.       end
  118.       doubleInc = true
  119.       end --make my editor look clearer
  120.     elseif arg[i] == "-t" then --anyKeyTerminate
  121.       do --make my editor look clearer
  122.       local t = tostring(arg[i+1])
  123.       if t == nil then
  124.         myError("arg "..i.." -t arg must be followed by a string equal to 'true' or 'false'", 2)
  125.       end
  126.       if t == "true" then
  127.         anyKeyTerminate = true
  128.       elseif t == "false" then
  129.         anyKeyTerminate = false
  130.       else
  131.         myError("arg "..i.." -t arg must be followed by a string equal to 'true' or 'false'", 2)
  132.       end
  133.       doubleInc = true
  134.       end --make my editor look clearer
  135.     else
  136.       myError("arg "..i.." unprocessed arg",2)
  137.     end
  138.     if doubleInc then
  139.       i = i +2
  140.     else
  141.       i = i +1
  142.     end
  143.   end
  144. end
  145.  
  146. --main program
  147. local base = peripheral.wrap(bSide)
  148.  
  149.  
  150. print("Lupus590's Quadracopter Program\nLicenced under MIT") --http://opensource.org/licenses/mit-license.php
  151. if anyKeyTerminate then
  152.   print("Press any key to terminate.")
  153. else
  154.   print("Press ctrl-t to terminate.")
  155. end
  156.  
  157. local event
  158. if ignoreRedstone then
  159.   --launch all drones
  160.   local i = 1
  161.   for i = 1, numOfQuads do
  162.     base.flyQuad(quadProgram)
  163.     sleep(delayMult*#quadProgram/numOfQuads)--don't want them all to go at the same time, we want roughly even spacing
  164.   end
  165.   while true do
  166.     event = os.pullEvent()
  167.     --no rest till we are done
  168.     if event == "quad_landed" then
  169.       base.flyQuad(quadProgram)
  170.     elseif anyKeyTerminate and event == "key" then
  171.       return
  172.     end    
  173.   end--while true
  174. else
  175.   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
  176.   while true do
  177.     event = os.pullEvent()
  178.     if event == "redstone" then
  179.       --launch all drones
  180.       local i = 1
  181.       for i = 1, numOfQuads do
  182.         base.flyQuad(quadProgram)
  183.         sleep(delayMult*#quadProgram/numOfQuads)--don't want them all to go at the same time, we want roughly even spacing
  184.       end
  185.       while redstone.getInput(rSide) do
  186.         event = os.pullEvent()
  187.         --no rest till we are done
  188.         if event == "quad_landed" and redstone.getInput(rSide) then
  189.           base.flyQuad(quadProgram)
  190.         elseif anyKeyTerminate and event == "key" then
  191.           return
  192.         end  
  193.       end --while redstone
  194.    
  195.     elseif anyKeyTerminate and event == "key" then
  196.       return
  197.     end --if event == redstone
  198.   end --while true
  199. end--if ignoreRedstone
Advertisement
Add Comment
Please, Sign In to add comment