Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI( "brt" )
- brt.import( "cashio" )
- brt.import( "log" )
- brt.import( "turtleext" )
- --
- -- Level 0 Level 1 Level 2
- -- +---+---+---+---+---+ +---+---+---+---+---+ +---+---+---+---+---+
- -- | | | | | | | | | |M/C| | | | | | | |
- -- +---+---+---+---+---+ +---+---+---+---+---+ +---+---+---+---+---+
- -- | | | | | | | | | |ACP|WLM| | | | | | |
- -- +---+---+---+---+---+ +---+---+---+---+---+ +---+---+---+---+---+
- -- | | | | | | | | | | | | | | | | | |
- -- +---+---+---+---+---+ +---+---+---+---+---+ +---+---+---+---+---+
- -- | | | | P |FMW| | | | | P |FMW| | | | P | P |FMW|
- -- +---+---+---+---+---+ +---+---+---+---+---+ +---+---+---+---+---+
- -- |CHT|CHT| |ACT| | |PIP|PIP|BAR|AWT|BAR| | S | S | S |MON| S |
- -- +---+---+---+---+---+ +---+---+---+---+---+ +---+---+---+---+---+
- -- | S | S | S | S | S | | |HOP| S |DRP| S | | | |BTN| | |
- -- +---+---+---+---+---+ +---+---+---+---+---+ +---+---+---+---+---+
- --
- -- ACT = Autonomous Activator
- -- Front View AWT = Advanced Wireless Turtle
- -- +---+---+---+ BTN = Button
- -- | S | S |MON| Level 2 BAR = Barrel
- -- +---+---+---+ CHT = Chest
- -- |HOP|BTN|DRP| Level 1 ACP = Advanced Computer
- -- +---+---+---+ DRP = Dropper
- -- | S | S | S | Level 0 FMW = Forge Microblock (west)
- -- +---+---+---+ HOP = Hopper
- -- MON = Monitor
- -- M/C = Modem/Cable
- -- P = RedProj. Wire
- -- PIP = Pipe or Conduit
- -- S = Solid Block
- -- WML = Wireless Modem
- --
- --
- --
- -- Target Quote Configuration:
- --
- -- Quote Calculation: (b + 2*c + 4*d) / (a+b+c+d)
- --
- -- 0x 1x 2x 3x Average Quote
- --
- -- 1 1 1 1 ==> 6/4 = 150 %
- -- 2 1 1 1 ==> 6/5 = 120 %
- -- 3 1 1 1 ==> 6/6 = 100 %
- -- 4 1 1 1 ==> 6/7 = 86 %
- --
- -- 1 2 1 1 ==> 7/5 = 140 %
- -- 2 2 1 1 ==> 7/6 = 117 %
- -- 3 2 1 1 ==> 7/7 = 100 %
- -- 4 2 1 1 ==> 7/8 = 88%
- --
- -- 1 3 1 1 ==> 8/6 = 133 %
- -- 2 3 1 1 ==> 8/7 = 114 %
- -- 3 3 1 1 ==> 8/8 = 100 %
- -- 4 3 1 1 ==> 8/9 = 89 %
- --
- -- 1 3 2 1 ==> 10/7 = 143 %
- -- 2 3 2 1 ==> 10/8 = 125 %
- -- 3 3 2 1 ==> 10/9 = 111 %
- -- 4 3 2 1 ==> 10/10 = 100 %
- -- 5 3 2 1 ==> 10/11 = 91 %
- --
- -- 8 17 4 1 ==> 28/29 = 96 %
- -- 10 21 7 1 ==> 38/39 = 97 %
- -- 13 24 8 2 ==> 46/47 = 98 %
- -- 19 62 12 3 ==> 95/96 = 99 %
- --
- swConfig = { version = "0.2",
- targetQuotes = { 7, 6, 3, 2 }, -- 100 % average (18 for 18)
- logDirName = "logs",
- statisticsFilePath = "statistics.dat"
- }
- hwConfig = { firstCashSlot = 1,
- lastCashSlot = 12,
- cashTemplateSlot = 16 }
- gambleStatistics = { 0, 0, 0, 0 }
- -- ------------------------------
- -- selfcheck( )
- -- ------------------------------
- -- Returns:
- -- true if OK,
- -- false on fail.
- -- ------------------------------
- function selfcheck( )
- -- No fuel necessary for just turning!
- -- Turn into start orientation (facing the dropper).
- local startOrientationFound = false
- for i = 1, 4 do
- local success, itemData = turtle.inspect( )
- if success and itemData.name == "minecraft:dropper" then
- startOrientationFound = true
- break
- else
- turtle.turnLeft( )
- end
- end
- if not startOrientationFound then
- brt.log.msg( "FEHLER: Kann die erforderliche Ausganslage nicht finden. ", true )
- return false
- end
- return true
- end
- -- ------------------------------
- -- deposit( )
- -- ------------------------------
- -- Collect __all__ cash from input barrel and
- -- put it into internal coin storage container.
- --
- -- Preconditions:
- -- Must face the dropper.
- --
- -- Postconditions:
- -- Faces the dropper.
- --
- -- Returns:
- -- Number of coins deposited.
- --
- function deposit( )
- -- Turn to input barrel.
- turtle.turnRight( )
- local collected = brt.cashio.extract( "front", nil,
- hwConfig.firstCashSlot,
- hwConfig.lastCashSlot,
- hwConfig.cashTemplateSlot )
- -- Turn to dropper.
- turtle.turnLeft( )
- local totalDeposit = 0
- while collected > 0 do
- -- Turn to private coin storage container.
- turtle.turnLeft( )
- -- Put cash into container.
- local deposited = brt.cashio.insert( "front", nil,
- hwConfig.firstCashSlot,
- hwConfig.lastCashSlot )
- totalDeposit = totalDeposit + deposited
- -- Turn to dropper.
- turtle.turnRight( )
- if deposited < collected then
- -- Cannot deposit all the collected cash.
- -- Turn to input barrel.
- turtle.turnRight( )
- -- Put back cash that cannot be deposited.
- local putBack = brt.cashio.insert( "front", nil,
- hwConfig.firstCashSlot,
- hwConfig.lastCashSlot )
- if putBack < collected - deposited then
- -- PROBLEM!
- brt.log.msg( "WARNUNG. Münzen die nicht eingelagert werden konnten, konnten auch nicht zurückgelegt werden.", true )
- end
- -- Turn to dropper.
- turtle.turnLeft( )
- break;
- end
- -- Turn to input barrel.
- turtle.turnRight( )
- collected = brt.cashio.extract( "front", nil,
- hwConfig.firstCashSlot,
- hwConfig.lastCashSlot,
- hwConfig.cashTemplateSlot )
- -- Turn to dropper.
- turtle.turnLeft( )
- end
- -- Now facing the dropper.
- return totalDeposit
- end
- -- ------------------------------
- -- payout( amount )
- -- ------------------------------
- -- Preconditions:
- -- Must face the dropper.
- --
- -- Postconditions:
- -- Faces the dropper.
- --
- -- Returns:
- -- Number of coins payed.
- --
- function payout( amount )
- local totalPayout = 0
- while totalPayout < amount do
- -- Turn to private coin storage container.
- turtle.turnLeft( )
- -- Take cash from private coin storage container.
- local collected = brt.cashio.extract( "front", amount - totalPayout,
- hwConfig.firstCashSlot,
- hwConfig.lastCashSlot )
- -- Turn to dropper.
- turtle.turnRight( )
- if collected < 1 then
- brt.log.msg( "FEHLER. Erforderliche Anzahl an Münzen kann nicht ausgegeben werden.", true )
- break
- end
- -- Pay out cash.
- local payedOut = brt.cashio.insert( "front", nil,
- hwConfig.firstCashSlot,
- hwConfig.lastCashSlot )
- totalPayout = totalPayout + payedOut
- if payedOut < collected then
- -- PROBLEM! The cash (partitionally) cannot be delivered.
- brt.log.msg( "WARNUNG. Münzen konnten (mind. teilweise) nicht ausgegeben werden.", true )
- -- Return cash that cannot be payed out.
- -- Turn to private coin storage container.
- turtle.turnLeft( )
- -- Put cash into container.
- local restored = brt.cashio.insert( "front", nil,
- hwConfig.firstCashSlot,
- hwConfig.lastCashSlot )
- -- Turn to dropper.
- turtle.turnRight( )
- if restored < collected - payedOut then
- -- PROBLEM!
- brt.log.msg( "WARNUNG. Münzen die nicht ausgegeben werden konnten, konnten auch nicht eingelagert werden.", true )
- end
- break;
- end
- end
- return totalPayout
- end
- -- ------------------------------
- -- initializeStatistics( )
- -- ------------------------------
- function initializeStatistics( )
- -- Try to read statistics file.
- gambleStatistics = nil --readStatistics( swConfig.statisticsFilePath )
- if gambleStatistics == nil then
- gambleStatistics = { 0, 0, 0, 0 }
- end
- brt.log.msg( "Statistik initialisiert." )
- end
- -- ------------------------------
- -- calculateWin( )
- -- ------------------------------
- function calculateWin( )
- local numCases = swConfig.targetQuotes[ 1 ] +
- swConfig.targetQuotes[ 2 ] +
- swConfig.targetQuotes[ 3 ] +
- swConfig.targetQuotes[ 4 ]
- if numCases < 1 then
- -- Bad configuration.
- brt.log.msg( "FEHLER. Ungültige Konfiguration der Ziel-Gewinnqouten. Der Gewinn wird auf 'einfach' festgelegt." )
- return 1
- end
- -- 0 <= case < numCases
- local case = math.floor( math.random( ) * numCases )
- local min1 = swConfig.targetQuotes[ 1 ]
- local min2 = min1 + swConfig.targetQuotes[ 2 ]
- local min3 = min2 + swConfig.targetQuotes[ 3 ]
- if case >= min3 then
- -- Triple.
- return 3
- elseif case >= min2 then
- -- Double.
- return 2
- elseif case < min1 then
- -- Bad luck.
- return 0
- end
- -- Single.
- return 1
- end
- --====================
- -- M A I N
- --
- -- The quasi-infinite main loop
- -- can be terminated by 'Ctrl+T'
- --====================
- -- Open log file.
- shell.run( "mkdir " .. swConfig.logDirName )
- local logFilePath = string.format( "%s/%s.log",
- swConfig.logDirName,
- brt.log.timestamp( ) )
- if not brt.log.open( logFilePath ) then
- print( "WARNUNG. Logdatei konnte nicht geöffnet werden." )
- end
- brt.log.msg( string.format( "Glücksspielprogramm Version %s gestartet.\nCopyright BRT 2014\nComputer-ID %i",
- swConfig.version, os.getComputerID( ) ), true )
- -- Initialize randon generator.
- math.randomseed( os.time( ) )
- local statusOK = selfcheck( )
- monitor = peripheral.wrap( "top" )
- if monitor == nil then
- brt.log.msg( "FEHLER. Kann den Monitor nicht lokalisieren.", true )
- statusOK = false
- else
- monitor.setTextScale( 0.5 )
- monitor.clear( )
- monitor.setCursorPos( 1, 1 )
- monitor.write( "Maschine" )
- monitor.setCursorPos( 1, 2 )
- monitor.write( "wird" )
- monitor.setCursorPos( 1, 3 )
- monitor.write( "vorbereitet..." )
- os.sleep( 4 )
- monitor.clear( )
- monitor.setCursorPos( 6, 3 )
- monitor.write( "B R" )
- monitor.setCursorPos( 2, 5 )
- monitor.write( "Technologies" )
- monitor.setCursorPos( 5, 8 )
- monitor.write( "11/2014" )
- os.sleep( 6 )
- end
- redstone.setOutput( "front", false )
- initializeStatistics( )
- while statusOK do
- -- Update remote display (if accessable).
- --if remote display accessable then
- --
- --end
- -- Update local display.
- monitor.clear( )
- monitor.setCursorPos( 1, 2 )
- monitor.write( "Regeln lesen!" )
- monitor.setCursorPos( 3, 4 )
- monitor.write( "Genau eine" )
- monitor.setCursorPos( 1, 5 )
- monitor.write( "Münze einwerfen," )
- monitor.setCursorPos( 3, 7 )
- monitor.write( "dann Knopf" )
- monitor.setCursorPos( 4, 8 )
- monitor.write( "drücken." )
- -- Check button.
- while not gambleRequest do
- os.sleep( 0.5 )
- local gambleRequest = redstone.getInput( "back" )
- if gambleRequest then
- break
- end
- end
- -- Check input.
- local numCoins = deposit( )
- if numCoins < 1 then
- brt.log.msg( "Spielversuch ohne eingeworfene Münze." )
- monitor.clear( )
- monitor.setCursorPos( 4, 3 )
- monitor.write( "Es wurden" )
- monitor.setCursorPos( 2, 4 )
- monitor.write( "keine Münzen" )
- monitor.setCursorPos( 2, 5 )
- monitor.write( "eingeworfen." )
- os.sleep( 3 )
- else
- if numCoins > 1 then
- brt.log.msg( string.format( "Spiel mit mehr als einer Münze.\n %i Münzen konfisziert.", numCoins-1 ) )
- monitor.clear( )
- monitor.setCursorPos( 3, 2 )
- monitor.write( "Es wurden" )
- monitor.setCursorPos( 1, 3 )
- monitor.write( "zuviele Münzen." )
- monitor.setCursorPos( 2, 4 )
- monitor.write( "eingeworfen." )
- monitor.setCursorPos( 2, 5 )
- monitor.write( "Überzählige" )
- monitor.setCursorPos( 4, 6 )
- monitor.write( "Münzen" )
- monitor.setCursorPos( 2, 7 )
- monitor.write( "konfisziert." )
- os.sleep( 3 )
- end
- -- Calculate win.
- monitor.clear( )
- monitor.setCursorPos( 3, 3 )
- monitor.write( "Der Gewinn" )
- monitor.setCursorPos( 5, 4 )
- monitor.write( "wird" )
- monitor.setCursorPos( 2, 5 )
- monitor.write( "ermittelt..." )
- local win = calculateWin( )
- brt.log.msg( string.format( "Ausschüttung ermittelt: %i-facher Einsatz.", win ), true )
- -- Update remote display (if accessable).
- --if remote display accessable then
- --
- --end
- -- Update local display.
- monitor.clear( )
- monitor.setCursorPos( 2, 3 )
- if win == 0 then
- monitor.write( "Leider nicht" )
- monitor.setCursorPos( 5, 4 )
- monitor.write( "gewonnen." )
- else
- monitor.write( "Ausschüttung:" )
- monitor.setCursorPos( 2, 5 )
- if win == 2 then
- monitor.write( "Doppelter" )
- elseif win == 3 then
- monitor.write( "Dreifacher" )
- else
- if win ~= 1 then
- brt.log.msg( string.format( "FEHLER. Unerwarteter Gewinn (%i Münzen).\nEs wird nur eine Münze ausgegeben.", win ) )
- win = 1
- end
- monitor.write( "Einfacher" )
- end
- monitor.setCursorPos( 4, 6 )
- monitor.write( " Einsatz." )
- -- Deliver win.
- turtle.turnLeft( )
- extracted = brt.cashio.extract( "front", win, 1, 1 )
- if extracted ~= win then
- brt.log.msg( "FEHLER. Konnte Münzen für die Ausschüttung nicht aus dem internen Container nehmen." )
- end
- turtle.turnRight( )
- inserted = brt.cashio.insert( "front", win, 1, 1 )
- if inserted ~= win then
- brt.log.msg( "FEHLER. Konnte die Münzen zur Ausschüttung nicht in den Dropper laden." )
- end
- for i = 1, inserted do
- redstone.setOutput( "front", true )
- os.sleep( 0.7 )
- redstone.setOutput( "front", false )
- os.sleep( 0.7 )
- end
- end
- os.sleep( 3 )
- end
- end
- brt.log.close( )
- --
- -- EOF
- --
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement