Advertisement
Guest User

Untitled

a guest
Oct 9th, 2015
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.66 KB | None | 0 0
  1.  
  2.  
  3.  
  4.  
  5.  
  6. swConfig = { version = "1.1",
  7. fastAnimationSteps = 12,
  8. mediumAnimationSteps = 10
  9. }
  10.  
  11. hwConfig = { enableSide = "left" , -- side of cable for en/disabling lanes
  12. electedSide = "right" , -- side of cable for e.g. illuminating the elected lane
  13. triggerSide = "back" , -- side of trigger to initiate a new election
  14. lanes = { -- max. 16 lanes possible
  15. { name = "Rot" , cableColor = colors.red },
  16. { name = "Orange" , cableColor = colors.orange },
  17. { name = "Gelb" , cableColor = colors.yellow },
  18. { name = "Grün" , cableColor = colors.green },
  19. { name = "Blau" , cableColor = colors.blue },
  20. { name = "Violett", cableColor = colors.magenta },
  21. { name = "Türkis" , cableColor = colors.cyan },
  22. { name = "Weiss" , cableColor = colors.white },
  23. -- Special, no lanes.
  24. { name = "CHOICE" , cableColor = colors.black },
  25. { name = "BACK" , cableColor = colors.pink },
  26. { name = "DOUBLE" , cableColor = colors.brown }
  27. }
  28. }
  29.  
  30.  
  31. numLanes = 0 -- number of existing lanes
  32. enabledLanes = { } -- table of the indices of enabled lanes
  33. electedLane = 0
  34.  
  35.  
  36. -- ------------------------------
  37. -- selfCheck
  38. --
  39. -- Returns true on success, false on fail.
  40. -- ------------------------------
  41. function selfCheck( )
  42. local sides = redstone.getSides( )
  43.  
  44. return true
  45. end
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52. -- ------------------------------
  53. -- checkEnabled
  54. --
  55. --
  56. -- ------------------------------
  57. function checkEnabled( )
  58. local input = redstone.getBundledInput( hwConfig.enableSide )
  59. enabledLanes = { }
  60.  
  61. for lane = 1, numLanes do
  62. color = hwConfig.lanes[ lane ].cableColor
  63. if bit.band( input, color ) ~= 0 then
  64. table.insert( enabledLanes, lane )
  65. end
  66. end
  67.  
  68. end
  69.  
  70.  
  71.  
  72. -- ------------------------------
  73. -- updateElected
  74. --
  75. -- Set signal for elected lane (or none if lane is 0).
  76. -- ------------------------------
  77. function updateElected( )
  78. local output = 0
  79. if electedLane > 0 then
  80. output = hwConfig.lanes[ electedLane ].cableColor
  81. end
  82. redstone.setBundledOutput( hwConfig.electedSide, output )
  83. end
  84.  
  85.  
  86.  
  87. -- ------------------------------
  88. -- setup
  89. --
  90. --
  91. -- ------------------------------
  92. function setup( )
  93. print( "" )
  94. print( "" )
  95. print( "" )
  96. print( "" )
  97. print( "" )
  98. print( " Don Quijote" )
  99. print( " Version ", swConfig.version )
  100. print( "" )
  101. print( "BR Technologies" )
  102. print( " 12/2014" )
  103. print( "" )
  104. print( "" )
  105. print( "" )
  106. os.sleep( 1 )
  107.  
  108. -- initialize random number generator
  109. math.randomseed( os.time( ) )
  110.  
  111. -- initialize soft~ and hardware
  112. local lanes = hwConfig.lanes
  113. numLanes = table.getn( lanes )
  114. print( string.format( "Es sind %i Bahnen konfiguriert.", numLanes ) )
  115. for i = 1, numLanes do
  116. print( string.format( " %i. %s", i, hwConfig.lanes[ i ].name ) )
  117. end
  118. checkEnabled( )
  119. electedLane = 0
  120. updateElected( )
  121. end
  122.  
  123.  
  124.  
  125.  
  126. -- ------------------------------
  127. -- runElection
  128. --
  129. --
  130. -- ------------------------------
  131. function runElection( )
  132. electedLane = 0
  133. updateElected( )
  134.  
  135. checkEnabled( )
  136. local numEnabled = table.getn( enabledLanes )
  137. if numEnabled < 1 then
  138. return
  139. elseif numEnabled == 1 then
  140. electedLane = enabledLanes[ 1 ]
  141. updateElected( )
  142. return
  143. end
  144.  
  145. -- Determine the line finally elected.
  146. local finalIdx = math.random( numEnabled )
  147.  
  148. -- Animate election.
  149. local idx = 0
  150. local lane = 0
  151.  
  152. -- Fast Stage: minimum sleep time
  153. for i = 1, swConfig.fastAnimationSteps do
  154. idx = idx % numEnabled +1
  155. electedLane = enabledLanes[ idx ]
  156. updateElected( )
  157. os.sleep( 0.1 )
  158. end
  159.  
  160. -- Medium Stage: increasing sleep time
  161. local medInc = 0.2 / swConfig.mediumAnimationSteps
  162. local medSleep = 0.1;
  163. for i = 1, swConfig.mediumAnimationSteps do
  164. idx = idx % numEnabled +1
  165. electedLane = enabledLanes[ idx ]
  166. updateElected( )
  167. os.sleep( medSleep )
  168. medSleep = medSleep + medInc
  169. end
  170.  
  171. -- Final Stage: Advancing to Target Image
  172. while idx ~= finalIdx do
  173. idx = idx % numEnabled +1
  174. electedLane = enabledLanes[ idx ]
  175. updateElected( )
  176. os.sleep( 0.2 )
  177. end
  178.  
  179. end
  180.  
  181.  
  182. -- ==============================
  183. -- main
  184. -- ==============================
  185.  
  186.  
  187. if not selfCheck( ) then
  188. print( "Selbsttest fehlgeschlagen. Es liegt ein Aufbau~ oder Konfigurationsfehler vor." )
  189. else
  190. setup( )
  191.  
  192. while true do
  193. local triggered = redstone.getInput( hwConfig.triggerSide )
  194. local electedOn = true
  195. while not triggered do
  196. os.sleep( 0.5 )
  197. triggered = redstone.getInput( hwConfig.triggerSide )
  198.  
  199. if electedLane ~= 0 then
  200. electedOn = not electedOn
  201. local output = 0
  202. if electedOn then
  203. output = hwConfig.lanes[ electedLane ].cableColor
  204. end
  205. redstone.setBundledOutput( hwConfig.electedSide, output )
  206. end
  207. end
  208.  
  209. runElection( )
  210. end
  211. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement