Pinkishu

tbm4

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