Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.96 KB | None | 0 0
  1. os.loadAPI( "brt" )
  2.  
  3. brt.import( "cashio" )
  4. brt.import( "log" )
  5. brt.import( "turtleext" )
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13. --
  14. -- Level 0 Level 1 Level 2
  15. -- +---+---+---+---+---+ +---+---+---+---+---+ +---+---+---+---+---+
  16. -- | | | | | | | | | |M/C| | | | | | | |
  17. -- +---+---+---+---+---+ +---+---+---+---+---+ +---+---+---+---+---+
  18. -- | | | | | | | | | |ACP|WLM| | | | | | |
  19. -- +---+---+---+---+---+ +---+---+---+---+---+ +---+---+---+---+---+
  20. -- | | | | | | | | | | | | | | | | | |
  21. -- +---+---+---+---+---+ +---+---+---+---+---+ +---+---+---+---+---+
  22. -- | | | | P |FMW| | | | | P |FMW| | | | P | P |FMW|
  23. -- +---+---+---+---+---+ +---+---+---+---+---+ +---+---+---+---+---+
  24. -- |CHT|CHT| |ACT| | |PIP|PIP|BAR|AWT|BAR| | S | S | S |MON| S |
  25. -- +---+---+---+---+---+ +---+---+---+---+---+ +---+---+---+---+---+
  26. -- | S | S | S | S | S | | |HOP| S |DRP| S | | | |BTN| | |
  27. -- +---+---+---+---+---+ +---+---+---+---+---+ +---+---+---+---+---+
  28. --
  29. -- ACT = Autonomous Activator
  30. -- Front View AWT = Advanced Wireless Turtle
  31. -- +---+---+---+ BTN = Button
  32. -- | S | S |MON| Level 2 BAR = Barrel
  33. -- +---+---+---+ CHT = Chest
  34. -- |HOP|BTN|DRP| Level 1 ACP = Advanced Computer
  35. -- +---+---+---+ DRP = Dropper
  36. -- | S | S | S | Level 0 FMW = Forge Microblock (west)
  37. -- +---+---+---+ HOP = Hopper
  38. -- MON = Monitor
  39. -- M/C = Modem/Cable
  40. -- P = RedProj. Wire
  41. -- PIP = Pipe or Conduit
  42. -- S = Solid Block
  43. -- WML = Wireless Modem
  44. --
  45. --
  46. --
  47.  
  48.  
  49. -- Target Quote Configuration:
  50. --
  51. -- Quote Calculation: (b + 2*c + 4*d) / (a+b+c+d)
  52. --
  53. -- 0x 1x 2x 3x Average Quote
  54. --
  55. -- 1 1 1 1 ==> 6/4 = 150 %
  56. -- 2 1 1 1 ==> 6/5 = 120 %
  57. -- 3 1 1 1 ==> 6/6 = 100 %
  58. -- 4 1 1 1 ==> 6/7 = 86 %
  59. --
  60. -- 1 2 1 1 ==> 7/5 = 140 %
  61. -- 2 2 1 1 ==> 7/6 = 117 %
  62. -- 3 2 1 1 ==> 7/7 = 100 %
  63. -- 4 2 1 1 ==> 7/8 = 88%
  64. --
  65. -- 1 3 1 1 ==> 8/6 = 133 %
  66. -- 2 3 1 1 ==> 8/7 = 114 %
  67. -- 3 3 1 1 ==> 8/8 = 100 %
  68. -- 4 3 1 1 ==> 8/9 = 89 %
  69. --
  70. -- 1 3 2 1 ==> 10/7 = 143 %
  71. -- 2 3 2 1 ==> 10/8 = 125 %
  72. -- 3 3 2 1 ==> 10/9 = 111 %
  73. -- 4 3 2 1 ==> 10/10 = 100 %
  74. -- 5 3 2 1 ==> 10/11 = 91 %
  75. --
  76. -- 8 17 4 1 ==> 28/29 = 96 %
  77. -- 10 21 7 1 ==> 38/39 = 97 %
  78. -- 13 24 8 2 ==> 46/47 = 98 %
  79. -- 19 62 12 3 ==> 95/96 = 99 %
  80. --
  81.  
  82. swConfig = { version = "0.2",
  83. targetQuotes = { 7, 6, 3, 2 }, -- 100 % average (18 for 18)
  84. logDirName = "logs",
  85. statisticsFilePath = "statistics.dat"
  86. }
  87.  
  88. hwConfig = { firstCashSlot = 1,
  89. lastCashSlot = 12,
  90. cashTemplateSlot = 16 }
  91.  
  92.  
  93. gambleStatistics = { 0, 0, 0, 0 }
  94.  
  95. -- ------------------------------
  96. -- selfcheck( )
  97. -- ------------------------------
  98. -- Returns:
  99. -- true if OK,
  100. -- false on fail.
  101. -- ------------------------------
  102. function selfcheck( )
  103. -- No fuel necessary for just turning!
  104. -- Turn into start orientation (facing the dropper).
  105. local startOrientationFound = false
  106. for i = 1, 4 do
  107. local success, itemData = turtle.inspect( )
  108. if success and itemData.name == "minecraft:dropper" then
  109. startOrientationFound = true
  110. break
  111. else
  112. turtle.turnLeft( )
  113. end
  114. end
  115. if not startOrientationFound then
  116. brt.log.msg( "FEHLER: Kann die erforderliche Ausganslage nicht finden. ", true )
  117. return false
  118. end
  119.  
  120. return true
  121. end
  122.  
  123.  
  124.  
  125. -- ------------------------------
  126. -- deposit( )
  127. -- ------------------------------
  128. -- Collect __all__ cash from input barrel and
  129. -- put it into internal coin storage container.
  130. --
  131. -- Preconditions:
  132. -- Must face the dropper.
  133. --
  134. -- Postconditions:
  135. -- Faces the dropper.
  136. --
  137. -- Returns:
  138. -- Number of coins deposited.
  139. --
  140. function deposit( )
  141.  
  142. -- Turn to input barrel.
  143. turtle.turnRight( )
  144. local collected = brt.cashio.extract( "front", nil,
  145. hwConfig.firstCashSlot,
  146. hwConfig.lastCashSlot,
  147. hwConfig.cashTemplateSlot )
  148.  
  149. -- Turn to dropper.
  150. turtle.turnLeft( )
  151.  
  152. local totalDeposit = 0
  153. while collected > 0 do
  154. -- Turn to private coin storage container.
  155. turtle.turnLeft( )
  156.  
  157. -- Put cash into container.
  158. local deposited = brt.cashio.insert( "front", nil,
  159. hwConfig.firstCashSlot,
  160. hwConfig.lastCashSlot )
  161. totalDeposit = totalDeposit + deposited
  162.  
  163. -- Turn to dropper.
  164. turtle.turnRight( )
  165.  
  166. if deposited < collected then
  167. -- Cannot deposit all the collected cash.
  168. -- Turn to input barrel.
  169. turtle.turnRight( )
  170.  
  171. -- Put back cash that cannot be deposited.
  172. local putBack = brt.cashio.insert( "front", nil,
  173. hwConfig.firstCashSlot,
  174. hwConfig.lastCashSlot )
  175. if putBack < collected - deposited then
  176. -- PROBLEM!
  177. brt.log.msg( "WARNUNG. Münzen die nicht eingelagert werden konnten, konnten auch nicht zurückgelegt werden.", true )
  178. end
  179. -- Turn to dropper.
  180. turtle.turnLeft( )
  181. break;
  182. end
  183.  
  184. -- Turn to input barrel.
  185. turtle.turnRight( )
  186. collected = brt.cashio.extract( "front", nil,
  187. hwConfig.firstCashSlot,
  188. hwConfig.lastCashSlot,
  189. hwConfig.cashTemplateSlot )
  190.  
  191. -- Turn to dropper.
  192. turtle.turnLeft( )
  193. end
  194.  
  195. -- Now facing the dropper.
  196. return totalDeposit
  197. end
  198.  
  199.  
  200.  
  201. -- ------------------------------
  202. -- payout( amount )
  203. -- ------------------------------
  204. -- Preconditions:
  205. -- Must face the dropper.
  206. --
  207. -- Postconditions:
  208. -- Faces the dropper.
  209. --
  210. -- Returns:
  211. -- Number of coins payed.
  212. --
  213. function payout( amount )
  214.  
  215. local totalPayout = 0
  216.  
  217. while totalPayout < amount do
  218. -- Turn to private coin storage container.
  219. turtle.turnLeft( )
  220.  
  221. -- Take cash from private coin storage container.
  222. local collected = brt.cashio.extract( "front", amount - totalPayout,
  223. hwConfig.firstCashSlot,
  224. hwConfig.lastCashSlot )
  225.  
  226. -- Turn to dropper.
  227. turtle.turnRight( )
  228.  
  229. if collected < 1 then
  230. brt.log.msg( "FEHLER. Erforderliche Anzahl an Münzen kann nicht ausgegeben werden.", true )
  231. break
  232. end
  233.  
  234. -- Pay out cash.
  235. local payedOut = brt.cashio.insert( "front", nil,
  236. hwConfig.firstCashSlot,
  237. hwConfig.lastCashSlot )
  238. totalPayout = totalPayout + payedOut
  239.  
  240. if payedOut < collected then
  241. -- PROBLEM! The cash (partitionally) cannot be delivered.
  242. brt.log.msg( "WARNUNG. Münzen konnten (mind. teilweise) nicht ausgegeben werden.", true )
  243.  
  244. -- Return cash that cannot be payed out.
  245. -- Turn to private coin storage container.
  246. turtle.turnLeft( )
  247.  
  248. -- Put cash into container.
  249. local restored = brt.cashio.insert( "front", nil,
  250. hwConfig.firstCashSlot,
  251. hwConfig.lastCashSlot )
  252.  
  253. -- Turn to dropper.
  254. turtle.turnRight( )
  255.  
  256. if restored < collected - payedOut then
  257. -- PROBLEM!
  258. brt.log.msg( "WARNUNG. Münzen die nicht ausgegeben werden konnten, konnten auch nicht eingelagert werden.", true )
  259. end
  260. break;
  261. end
  262.  
  263. end
  264.  
  265. return totalPayout
  266.  
  267. end
  268.  
  269.  
  270.  
  271. -- ------------------------------
  272. -- initializeStatistics( )
  273. -- ------------------------------
  274. function initializeStatistics( )
  275. -- Try to read statistics file.
  276. gambleStatistics = nil --readStatistics( swConfig.statisticsFilePath )
  277. if gambleStatistics == nil then
  278. gambleStatistics = { 0, 0, 0, 0 }
  279. end
  280.  
  281. brt.log.msg( "Statistik initialisiert." )
  282.  
  283. end
  284.  
  285.  
  286.  
  287.  
  288. -- ------------------------------
  289. -- calculateWin( )
  290. -- ------------------------------
  291. function calculateWin( )
  292. local numCases = swConfig.targetQuotes[ 1 ] +
  293. swConfig.targetQuotes[ 2 ] +
  294. swConfig.targetQuotes[ 3 ] +
  295. swConfig.targetQuotes[ 4 ]
  296. if numCases < 1 then
  297. -- Bad configuration.
  298. brt.log.msg( "FEHLER. Ungültige Konfiguration der Ziel-Gewinnqouten. Der Gewinn wird auf 'einfach' festgelegt." )
  299. return 1
  300. end
  301.  
  302. -- 0 <= case < numCases
  303. local case = math.floor( math.random( ) * numCases )
  304.  
  305. local min1 = swConfig.targetQuotes[ 1 ]
  306. local min2 = min1 + swConfig.targetQuotes[ 2 ]
  307. local min3 = min2 + swConfig.targetQuotes[ 3 ]
  308.  
  309. if case >= min3 then
  310. -- Triple.
  311. return 3
  312. elseif case >= min2 then
  313. -- Double.
  314. return 2
  315. elseif case < min1 then
  316. -- Bad luck.
  317. return 0
  318. end
  319.  
  320. -- Single.
  321. return 1
  322. end
  323.  
  324.  
  325.  
  326. --====================
  327. -- M A I N
  328. --
  329. -- The quasi-infinite main loop
  330. -- can be terminated by 'Ctrl+T'
  331. --====================
  332.  
  333. -- Open log file.
  334. shell.run( "mkdir " .. swConfig.logDirName )
  335. local logFilePath = string.format( "%s/%s.log",
  336. swConfig.logDirName,
  337. brt.log.timestamp( ) )
  338. if not brt.log.open( logFilePath ) then
  339. print( "WARNUNG. Logdatei konnte nicht geöffnet werden." )
  340. end
  341.  
  342. brt.log.msg( string.format( "Glücksspielprogramm Version %s gestartet.\nCopyright BRT 2014\nComputer-ID %i",
  343. swConfig.version, os.getComputerID( ) ), true )
  344.  
  345.  
  346. -- Initialize randon generator.
  347. math.randomseed( os.time( ) )
  348.  
  349.  
  350. local statusOK = selfcheck( )
  351.  
  352. monitor = peripheral.wrap( "top" )
  353. if monitor == nil then
  354. brt.log.msg( "FEHLER. Kann den Monitor nicht lokalisieren.", true )
  355. statusOK = false
  356. else
  357. monitor.setTextScale( 0.5 )
  358. monitor.clear( )
  359. monitor.setCursorPos( 1, 1 )
  360. monitor.write( "Maschine" )
  361. monitor.setCursorPos( 1, 2 )
  362. monitor.write( "wird" )
  363. monitor.setCursorPos( 1, 3 )
  364. monitor.write( "vorbereitet..." )
  365. os.sleep( 4 )
  366.  
  367. monitor.clear( )
  368. monitor.setCursorPos( 6, 3 )
  369. monitor.write( "B R" )
  370. monitor.setCursorPos( 2, 5 )
  371. monitor.write( "Technologies" )
  372. monitor.setCursorPos( 5, 8 )
  373. monitor.write( "11/2014" )
  374. os.sleep( 6 )
  375. end
  376.  
  377. redstone.setOutput( "front", false )
  378.  
  379. initializeStatistics( )
  380.  
  381.  
  382. while statusOK do
  383.  
  384. -- Update remote display (if accessable).
  385. --if remote display accessable then
  386. --
  387. --end
  388.  
  389. -- Update local display.
  390. monitor.clear( )
  391. monitor.setCursorPos( 1, 2 )
  392. monitor.write( "Regeln lesen!" )
  393. monitor.setCursorPos( 3, 4 )
  394. monitor.write( "Genau eine" )
  395. monitor.setCursorPos( 1, 5 )
  396. monitor.write( "Münze einwerfen," )
  397. monitor.setCursorPos( 3, 7 )
  398. monitor.write( "dann Knopf" )
  399. monitor.setCursorPos( 4, 8 )
  400. monitor.write( "drücken." )
  401.  
  402. -- Check button.
  403. while not gambleRequest do
  404. os.sleep( 0.5 )
  405. local gambleRequest = redstone.getInput( "back" )
  406. if gambleRequest then
  407. break
  408. end
  409. end
  410.  
  411.  
  412. -- Check input.
  413. local numCoins = deposit( )
  414. if numCoins < 1 then
  415. brt.log.msg( "Spielversuch ohne eingeworfene Münze." )
  416. monitor.clear( )
  417. monitor.setCursorPos( 4, 3 )
  418. monitor.write( "Es wurden" )
  419. monitor.setCursorPos( 2, 4 )
  420. monitor.write( "keine Münzen" )
  421. monitor.setCursorPos( 2, 5 )
  422. monitor.write( "eingeworfen." )
  423. os.sleep( 3 )
  424. else
  425.  
  426. if numCoins > 1 then
  427. brt.log.msg( string.format( "Spiel mit mehr als einer Münze.\n %i Münzen konfisziert.", numCoins-1 ) )
  428. monitor.clear( )
  429. monitor.setCursorPos( 3, 2 )
  430. monitor.write( "Es wurden" )
  431. monitor.setCursorPos( 1, 3 )
  432. monitor.write( "zuviele Münzen." )
  433. monitor.setCursorPos( 2, 4 )
  434. monitor.write( "eingeworfen." )
  435. monitor.setCursorPos( 2, 5 )
  436. monitor.write( "Überzählige" )
  437. monitor.setCursorPos( 4, 6 )
  438. monitor.write( "Münzen" )
  439. monitor.setCursorPos( 2, 7 )
  440. monitor.write( "konfisziert." )
  441. os.sleep( 3 )
  442. end
  443.  
  444. -- Calculate win.
  445. monitor.clear( )
  446. monitor.setCursorPos( 3, 3 )
  447. monitor.write( "Der Gewinn" )
  448. monitor.setCursorPos( 5, 4 )
  449. monitor.write( "wird" )
  450. monitor.setCursorPos( 2, 5 )
  451. monitor.write( "ermittelt..." )
  452. local win = calculateWin( )
  453. brt.log.msg( string.format( "Ausschüttung ermittelt: %i-facher Einsatz.", win ), true )
  454.  
  455. -- Update remote display (if accessable).
  456. --if remote display accessable then
  457. --
  458. --end
  459.  
  460. -- Update local display.
  461. monitor.clear( )
  462. monitor.setCursorPos( 2, 3 )
  463. if win == 0 then
  464. monitor.write( "Leider nicht" )
  465. monitor.setCursorPos( 5, 4 )
  466. monitor.write( "gewonnen." )
  467. else
  468.  
  469. monitor.write( "Ausschüttung:" )
  470. monitor.setCursorPos( 2, 5 )
  471. if win == 2 then
  472. monitor.write( "Doppelter" )
  473. elseif win == 3 then
  474. monitor.write( "Dreifacher" )
  475. else
  476. if win ~= 1 then
  477. brt.log.msg( string.format( "FEHLER. Unerwarteter Gewinn (%i Münzen).\nEs wird nur eine Münze ausgegeben.", win ) )
  478. win = 1
  479. end
  480. monitor.write( "Einfacher" )
  481. end
  482. monitor.setCursorPos( 4, 6 )
  483. monitor.write( " Einsatz." )
  484.  
  485. -- Deliver win.
  486. turtle.turnLeft( )
  487. extracted = brt.cashio.extract( "front", win, 1, 1 )
  488. if extracted ~= win then
  489. brt.log.msg( "FEHLER. Konnte Münzen für die Ausschüttung nicht aus dem internen Container nehmen." )
  490. end
  491.  
  492. turtle.turnRight( )
  493. inserted = brt.cashio.insert( "front", win, 1, 1 )
  494. if inserted ~= win then
  495. brt.log.msg( "FEHLER. Konnte die Münzen zur Ausschüttung nicht in den Dropper laden." )
  496. end
  497. for i = 1, inserted do
  498. redstone.setOutput( "front", true )
  499. os.sleep( 0.7 )
  500. redstone.setOutput( "front", false )
  501. os.sleep( 0.7 )
  502. end
  503.  
  504. end
  505. os.sleep( 3 )
  506. end
  507.  
  508. end
  509.  
  510. brt.log.close( )
  511.  
  512. --
  513. -- EOF
  514. --
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement