Pinkishu

tbm2

Jul 1st, 2012
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.08 KB | None | 0 0
  1. local side = "left"
  2. local funcState = {}
  3. funcState.step = 0
  4. funcState.func = runLeft
  5. local rsDelay = 0.1
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12. local colMap = {
  13.   headSliderBreaker = { color = colors.orange, delay = 0.3 },
  14.   headSliderDeployer = { color = colors.yellow, delay = 0.3 },
  15.   headSliderLeft = { color = colors.lightBlue, delay = 1.1 },
  16.   headSliderRight = { color = colors.magenta, delay = 1.1 },
  17.   checkColLeft = { color = colors.white },
  18.   checkColRight = { color = colors.gray },
  19.   checkForward = { color = colors.brown },
  20.   fwdBreaker = { color = colors.purple, delay = 0.3 },
  21.   fwdDeployer = { color = colors.cyan, delay = 0.3 },
  22.   battery = { color = colors.lime, delay = 1 },
  23.   engineMotorPush = { color=colors.pink, delay = 1 },
  24.   engineMotorPull = { color=colors.lightGray, delay= 1.3 },
  25.   headMiningBreaker = { color = colors.blue, delay = 0.35 },
  26.   kill = { color = colors.green }
  27. }
  28.  
  29. function redclear()
  30.   rs.setBundledOutput(side,0)
  31. end
  32.  
  33. function redset(color)
  34.   rs.setBundledOutput(side,color)
  35. end
  36.  
  37. function forward()
  38.   redpulse( colMap.fwdDeployer )
  39.   local check = false
  40.   for k=1,2,1 do
  41.   --while not check do
  42.     redpulse(colMap.engineMotorPush)
  43.     redpulse(colMap.engineMotorPull)
  44.    
  45.     for i = 1, 5, 1 do
  46.       if redtest(colMap.checkForward.color) then
  47.         check = true
  48.         delay(1)
  49.       end
  50.       if check then break end
  51.       delay(0.5)
  52.     end
  53.     if check then break end
  54.   end
  55.   redpulse( colMap.fwdBreaker )
  56.   redpulse(colMap.battery)
  57.   return "#END"
  58. end
  59.  
  60. function redadd(color)
  61.   --print("add " .. color )
  62.   local nCol = colors.combine( rs.getBundledOutput( side ), color )
  63.   rs.setBundledOutput(side,nCol)
  64. end
  65.  
  66. function reddel(color)
  67.   --print("sub " .. color )
  68.   local nCol = colors.subtract( rs.getBundledOutput( side ), color )
  69.   rs.setBundledOutput(side,nCol)
  70. end
  71.  
  72. function redtest(color)
  73.   return colors.test( rs.getBundledInput( side ), color )
  74. end
  75.  
  76. function delay(delayt)
  77.   funcState.timer = os.startTimer( delayt )
  78.   coroutine.yield()
  79. end
  80.  
  81. function redpulse(opts)
  82.   redadd(opts.color)
  83.   delay( opts.delay )
  84.   reddel(opts.color)
  85.   delay(rsDelay)
  86. end
  87.  
  88. function mineHeadLeft()
  89.   redpulse(colMap.headSliderBreaker)
  90.   redpulse(colMap.headSliderLeft)
  91. end
  92.  
  93. function mineHeadRight()
  94.   redpulse(colMap.headSliderDeployer)
  95.   redpulse(colMap.headSliderRight)
  96. end
  97.  
  98. redclear()
  99.  
  100. function runLeft()
  101.   while not redtest(colors.white) do
  102.     mineHeadLeft()
  103.   end
  104.   return "#END"
  105. end
  106. funcState.func = runLeft
  107.  
  108.  
  109.  
  110. function mine()
  111.   local tCol = colMap.checkColRight.color
  112.   local cFunc = mineHeadRight
  113.   if not funcState.dir then tCol = colMap.checkColLeft.color cFunc = mineHeadLeft end
  114.   local counter = 0
  115.   redpulse(colMap.headMiningBreaker)
  116.   delay(1)
  117.   redpulse(colMap.headMiningBreaker)
  118.   while not redtest(tCol) do
  119.     --redpulse(colMap.headMiningBreaker)
  120.     --delay(1)
  121.     redpulse(colMap.headMiningBreaker)
  122.     cFunc()
  123.    
  124.    
  125.     counter = counter + 1
  126.     if counter > 12 then redpulse(colMap.headMiningBreaker) delay(1) redpulse(colMap.headMiningBreaker)
  127.     elseif counter > 20 then error("ERROR: Mine head too far/broken") end
  128.   end
  129.   redpulse(colMap.headMiningBreaker)
  130.   return "#END"
  131. end
  132.  
  133. function funcDo()
  134.   while true do
  135.     if redtest(colMap.kill.color) then return end
  136.     local res = funcState.func()
  137.     if res == "#END" then
  138.       redclear()
  139.       if funcState.func == runLeft then
  140.         funcState.dir = true
  141.         funcState.func = mine
  142.       elseif funcState.func == mine then
  143.         print("mine end")
  144.         funcState.func = forward
  145.       elseif funcState.func == forward then
  146.         funcState.func = mine
  147.         funcState.dir = not funcState.dir
  148.       end
  149.     end
  150.  
  151.     delay(rsDelay)
  152.   end
  153. end
  154. local funcCo = coroutine.create(funcDo)
  155. function timerCallback(ev,p1)
  156.   if redtest( colMap.kill.color) then return end
  157.   if p1 == funcState.timer then
  158.     funcState.timer = nil
  159.     coroutine.resume(funcCo)
  160.   end
  161. end
  162.  
  163. events.registerEvent("timer",timerCallback)
  164. coroutine.resume(funcCo)
  165. while true do
  166.   if redtest(colMap.kill.color) then return end
  167.   coroutine.yield()
  168.  
  169. end
Advertisement
Add Comment
Please, Sign In to add comment