Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- swConfig = { version = "1.1",
- fastAnimationSteps = 12,
- mediumAnimationSteps = 10
- }
- hwConfig = { enableSide = "left" , -- side of cable for en/disabling lanes
- electedSide = "right" , -- side of cable for e.g. illuminating the elected lane
- triggerSide = "back" , -- side of trigger to initiate a new election
- lanes = { -- max. 16 lanes possible
- { name = "Rot" , cableColor = colors.red },
- { name = "Orange" , cableColor = colors.orange },
- { name = "Gelb" , cableColor = colors.yellow },
- { name = "Grün" , cableColor = colors.green },
- { name = "Blau" , cableColor = colors.blue },
- { name = "Violett", cableColor = colors.magenta },
- { name = "Türkis" , cableColor = colors.cyan },
- { name = "Weiss" , cableColor = colors.white },
- -- Special, no lanes.
- { name = "CHOICE" , cableColor = colors.black },
- { name = "BACK" , cableColor = colors.pink },
- { name = "DOUBLE" , cableColor = colors.brown }
- }
- }
- numLanes = 0 -- number of existing lanes
- enabledLanes = { } -- table of the indices of enabled lanes
- electedLane = 0
- -- ------------------------------
- -- selfCheck
- --
- -- Returns true on success, false on fail.
- -- ------------------------------
- function selfCheck( )
- local sides = redstone.getSides( )
- return true
- end
- -- ------------------------------
- -- checkEnabled
- --
- --
- -- ------------------------------
- function checkEnabled( )
- local input = redstone.getBundledInput( hwConfig.enableSide )
- enabledLanes = { }
- for lane = 1, numLanes do
- color = hwConfig.lanes[ lane ].cableColor
- if bit.band( input, color ) ~= 0 then
- table.insert( enabledLanes, lane )
- end
- end
- end
- -- ------------------------------
- -- updateElected
- --
- -- Set signal for elected lane (or none if lane is 0).
- -- ------------------------------
- function updateElected( )
- local output = 0
- if electedLane > 0 then
- output = hwConfig.lanes[ electedLane ].cableColor
- end
- redstone.setBundledOutput( hwConfig.electedSide, output )
- end
- -- ------------------------------
- -- setup
- --
- --
- -- ------------------------------
- function setup( )
- print( "" )
- print( "" )
- print( "" )
- print( "" )
- print( "" )
- print( " Don Quijote" )
- print( " Version ", swConfig.version )
- print( "" )
- print( "BR Technologies" )
- print( " 12/2014" )
- print( "" )
- print( "" )
- print( "" )
- os.sleep( 1 )
- -- initialize random number generator
- math.randomseed( os.time( ) )
- -- initialize soft~ and hardware
- local lanes = hwConfig.lanes
- numLanes = table.getn( lanes )
- print( string.format( "Es sind %i Bahnen konfiguriert.", numLanes ) )
- for i = 1, numLanes do
- print( string.format( " %i. %s", i, hwConfig.lanes[ i ].name ) )
- end
- checkEnabled( )
- electedLane = 0
- updateElected( )
- end
- -- ------------------------------
- -- runElection
- --
- --
- -- ------------------------------
- function runElection( )
- electedLane = 0
- updateElected( )
- checkEnabled( )
- local numEnabled = table.getn( enabledLanes )
- if numEnabled < 1 then
- return
- elseif numEnabled == 1 then
- electedLane = enabledLanes[ 1 ]
- updateElected( )
- return
- end
- -- Determine the line finally elected.
- local finalIdx = math.random( numEnabled )
- -- Animate election.
- local idx = 0
- local lane = 0
- -- Fast Stage: minimum sleep time
- for i = 1, swConfig.fastAnimationSteps do
- idx = idx % numEnabled +1
- electedLane = enabledLanes[ idx ]
- updateElected( )
- os.sleep( 0.1 )
- end
- -- Medium Stage: increasing sleep time
- local medInc = 0.2 / swConfig.mediumAnimationSteps
- local medSleep = 0.1;
- for i = 1, swConfig.mediumAnimationSteps do
- idx = idx % numEnabled +1
- electedLane = enabledLanes[ idx ]
- updateElected( )
- os.sleep( medSleep )
- medSleep = medSleep + medInc
- end
- -- Final Stage: Advancing to Target Image
- while idx ~= finalIdx do
- idx = idx % numEnabled +1
- electedLane = enabledLanes[ idx ]
- updateElected( )
- os.sleep( 0.2 )
- end
- end
- -- ==============================
- -- main
- -- ==============================
- if not selfCheck( ) then
- print( "Selbsttest fehlgeschlagen. Es liegt ein Aufbau~ oder Konfigurationsfehler vor." )
- else
- setup( )
- while true do
- local triggered = redstone.getInput( hwConfig.triggerSide )
- local electedOn = true
- while not triggered do
- os.sleep( 0.5 )
- triggered = redstone.getInput( hwConfig.triggerSide )
- if electedLane ~= 0 then
- electedOn = not electedOn
- local output = 0
- if electedOn then
- output = hwConfig.lanes[ electedLane ].cableColor
- end
- redstone.setBundledOutput( hwConfig.electedSide, output )
- end
- end
- runElection( )
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement