Guest User

[CC] quadPipe - with bug

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