Advertisement
Fooman

Anvil Dropping Game

May 16th, 2013
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.71 KB | None | 0 0
  1. --[[
  2. Falling Anvil Game
  3. http://www.youtube.com/user/foooman
  4. ]]--
  5.  
  6. --Number of Anvils to be dropped at once
  7. local difficulty = {}
  8. local difficultyInt
  9. --Time between Anvils dropping
  10. local anvilInterval = 2
  11. --Maximum number of Anvils to Drop
  12. local anvilMax = 10
  13. local anvilsDropped = 0
  14. --Maximum of 16 for the 16 bundled cable colors
  15. local anvilQuantity = 16
  16. --Table of anvils to be dropped
  17. local anvils = {}
  18. --Table containing decimal color values for Bundled cable
  19. local colors = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768}
  20. --Variable to keep loop running
  21. local listening = true
  22. local running = true
  23. --Loser Detection Side
  24. local loserSide = "bottom"
  25. --Bundled Cable Side
  26. local bundledSide = "left"
  27. --Timer for dropping anvils
  28. local anvilTimer
  29. local anvilSum = 0
  30. local colorSum = 0
  31.  
  32. --Side with modem
  33. local rednetSide = "top"
  34. --Table of locked doors
  35. local doorLock = {}
  36. --Bundled Cable Side for Doors
  37. local doorSide = "right"
  38. --Player ready status, 2 is a go
  39. local playerReady = {0, 0}
  40. --Player IDs
  41. local playerIDOne = 783
  42. local playerIDTwo = 784
  43. --monitor Side
  44. local monitorSide = "back"
  45. local w,h = term.getSize()
  46.  
  47. --Feed Cobble on Door Check
  48. local cobbleFeed = false
  49.  
  50. rednet.open("top")
  51. m = peripheral.wrap(monitorSide)
  52. m.setTextScale(5)
  53. local wm,hm = m.getSize()
  54.  
  55. term.redirect(peripheral.wrap(monitorSide))
  56. --Create Seed for RNG
  57. math.randomseed(os.time())
  58.  
  59.  
  60. function aSum(array,int)
  61.     arraySum = 0
  62.     for i=1,int do
  63.         arraySum = arraySum + array[i]
  64.     end
  65.     return arraySum
  66. end
  67.  
  68. function printCentered(str,ypos)
  69.     term.setCursorPos(wm/2 - #str/2, ypos)
  70.     term.write(str)
  71. end
  72.  
  73. --Resets table to zero values
  74. function resetAnvils()
  75.     for i= 1, anvilQuantity do
  76.         anvils[i] = 0
  77.     end
  78. end
  79.  
  80.  
  81. --Pulses all cables
  82. function dropAnvils()
  83.     --If Maximum reached shut down program
  84.     anvilsDropped = anvilsDropped + 1
  85.     if anvilsDropped >= anvilMax then
  86.         print("Anvil Maximum reached")
  87.         running = false
  88.         return
  89.     end
  90.     print("Dropping Anvil #"..anvilsDropped)
  91.     --Resets Color Sum
  92.     colorSum = 0
  93.     --Creates Sum of Bundled Cable Colors
  94.     for i= 1, anvilQuantity do
  95.         if anvils[i] == 1 then
  96.             colorSum = colorSum + colors[i]
  97.         end      
  98.     end
  99.     rs.setBundledOutput(bundledSide, colorSum)
  100.     --Hold Signal for 1 seconds
  101.     os.sleep(1)
  102.     --Resets bundled output to false
  103.     rs.setBundledOutput(bundledSide, 0)
  104. end
  105.  
  106.  
  107. --Creates Random Anvil Table
  108. function createAnvils()
  109.     --Resets anvilSum
  110.     anvilSum = 0
  111.     --Choose random anvils until current difficulty is met
  112.     while anvilSum < difficultyInt do
  113.         anvils[math.random(anvilQuantity)] = 1
  114.         sumAnvils()
  115.     end
  116. end
  117.  
  118. --Sums Anvil Array
  119. function sumAnvils()
  120.     anvilSum = 0
  121.     for i= 1,anvilQuantity do
  122.         anvilSum = anvilSum + anvils[i]
  123.     end
  124. end
  125.  
  126. --Rednet Interpreter
  127. function rednetInterp()
  128.     --Unlock Door
  129.     if string.find(b, "N") then
  130.         --Resets Ready Status when client reboots
  131.         if a == playerIDOne then
  132.             playerReady[1] = 0
  133.         elseif a == playerIDTwo then
  134.             playerReady[2] = 0
  135.         end
  136.         --Sets Doors to Unlock
  137.         for i = 1,2 do
  138.             if string.find(b, tostring(i)) then
  139.                 doorLock[i] = 0
  140.                 doorLock[i+2] = 0
  141.             end
  142.         end
  143.     end
  144.     --Lock Door
  145.     if string.find(b, "S") then
  146.         for i = 1,2 do
  147.             if string.find(b,tostring(i)) then
  148.                 doorLock[i] = 1
  149.                 doorLock[i+2] = 1
  150.             end
  151.         end
  152.     end
  153.     --Player
  154.     if string.find(b, "P") then
  155.         for i = 1,2 do
  156.             --One Player Mode
  157.             if string.find(b,"1") then
  158.                 if a == playerIDOne then
  159.                     playerReady[1] = 2
  160.                     playerReady[2] = 0
  161.                     print("Player 1 Ready")
  162.                 elseif a == playerIdTwo then
  163.                     playerReady[1] = 0
  164.                     playerReady[2] = 2
  165.                     print("Player 2 Ready")
  166.                 end
  167.             --Two Player Mode
  168.             elseif string.find(b, "2") then
  169.                 if a == playerIDOne then
  170.                     playerReady[1] = 1
  171.                     print("Player 1 Ready")
  172.                 elseif a == playerIDTwo then
  173.                     playerReady[2] = 1
  174.                     print("Player 2 Ready")
  175.                 end
  176.             end
  177.         end
  178.     end
  179.     --Difficulty
  180.     if string.find(b, "D") then
  181.         for i = 16,1,-1 do
  182.             if string.find(b,tostring(i)) then
  183.                 if a == playerIDOne then
  184.                     difficulty[1] = i
  185.                     print("P1Difficulty: "..i)
  186.                     break
  187.                 elseif a ==playerIDTwo then
  188.                     difficulty[2] = i
  189.                     print("P2Difficulty: "..i)
  190.                     break
  191.                 end
  192.             end
  193.         end
  194.     end
  195. end
  196.  
  197. --Checks doors and opens/closes them
  198. function doorCheck()
  199.     local doorBundle = 0
  200.     for i = 1,4 do
  201.         if doorLock[i] == 1 then
  202.             doorBundle = doorBundle + colors[i]
  203.         end
  204.     end
  205.     --Close Doors and sleep before feeding cobble
  206.     rs.setBundledOutput(doorSide, doorBundle)
  207.     if cobbleFeed then
  208.         os.sleep(1)
  209.         for i = 1,2 do
  210.             if playerReady[i] > 0 then
  211.                 doorBundle = doorBundle + colors[i+4]
  212.                 cobbleFeed = false
  213.             end
  214.         end
  215.     end
  216.     rs.setBundledOutput(doorSide, doorBundle)
  217. end
  218.  
  219. --Ready to Rollout
  220. function rollout()
  221.     --If Players are ready
  222.     if aSum(playerReady,2) == 2 then
  223.         print("Game Starting")
  224.         --Calculate Average Difficulty if 2 Player
  225.         for k,v in pairs(playerReady) do
  226.             if v == 1 then
  227.                 difficultyInt = aSum(difficulty,2)/2
  228.             elseif v == 2 then
  229.                 difficultyInt = difficulty[k]
  230.             end
  231.         end
  232.         print("Difficulty: "..difficultyInt)
  233.         --Lock Doors
  234.         doorLock = {1, 1, 1, 1}
  235.         --Spit Out Cobble and Door Check
  236.         cobbleFeed = true
  237.         doorCheck()
  238.         --5Sec Countdown on Monitor
  239.         for i = 5,1,-1 do
  240.             os.sleep(1)
  241.             print(i)
  242.         end
  243.         --set listening to false to break listen loop
  244.         rednet.broadcast("reboot")
  245.         listening = false
  246.     end
  247. end
  248.  
  249. --INITIALIZATION
  250. term.clear()
  251. printCentered(" Dodge", 2)
  252. printCentered(" the", 3)
  253. printCentered(" Anvils!",4)
  254. printCentered(" --ready--",7)
  255. while listening do
  256.     event, a, b, c = os.pullEvent()
  257.     if event == "rednet_message" then
  258.         rednetInterp()
  259.         doorCheck()
  260.         rollout()
  261.     end
  262. end
  263.  
  264. --OPERATION--
  265.  
  266. resetAnvils()
  267. --Open Doors
  268. doorLock = {0, 0, 0, 0}
  269. doorCheck()
  270. --Start 1s Timer of open Doors
  271. anvilTimer = os.startTimer(1)
  272.  
  273. while running do
  274.     event, a, b, c = os.pullEvent()
  275.     --If loser redstone triggers stops operation
  276.     if event == "redstone" then
  277.         if rs.getInput(loserSide) then
  278.             print("Death detected")
  279.             running = false
  280.         end
  281.     end
  282.     --If timer hits send more anvils
  283.     if event == "timer" then
  284.         --Lock Doors
  285.         doorLock = {1, 1, 1, 1}
  286.         doorCheck()
  287.         resetAnvils()
  288.         createAnvils()
  289.         dropAnvils()
  290.         anvilTimer = os.startTimer(anvilInterval)
  291.     end
  292. end
  293.  
  294. --END OF PROGRAM
  295. --Unlocks doors and reboots
  296. print("Game Over! Thanks for playing")
  297. doorLock = {0, 0, 0, 0}
  298. doorCheck()
  299. --Activate Rose Stealer for 1s then turn off
  300. rs.setBundledOutput(doorSide, colors[7])
  301. os.sleep(1)
  302. rs.setBundledOutput(doorSide, 0)
  303. os.sleep(9)
  304. os.reboot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement