Pinkishu

tbm3

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