Guest User

[CC] quadPipe - with bug

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