Pinkishu

tbmnew

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