Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Falling Anvil Game
- http://www.youtube.com/user/foooman
- ]]--
- --Number of Anvils to be dropped at once
- local difficulty = {}
- local difficultyInt
- --Time between Anvils dropping
- local anvilInterval = 2
- --Maximum number of Anvils to Drop
- local anvilMax = 10
- local anvilsDropped = 0
- --Maximum of 16 for the 16 bundled cable colors
- local anvilQuantity = 16
- --Table of anvils to be dropped
- local anvils = {}
- --Table containing decimal color values for Bundled cable
- local colors = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768}
- --Variable to keep loop running
- local listening = true
- local running = true
- --Loser Detection Side
- local loserSide = "bottom"
- --Bundled Cable Side
- local bundledSide = "left"
- --Timer for dropping anvils
- local anvilTimer
- local anvilSum = 0
- local colorSum = 0
- --Side with modem
- local rednetSide = "top"
- --Table of locked doors
- local doorLock = {}
- --Bundled Cable Side for Doors
- local doorSide = "right"
- --Player ready status, 2 is a go
- local playerReady = {0, 0}
- --Player IDs
- local playerIDOne = 783
- local playerIDTwo = 784
- --monitor Side
- local monitorSide = "back"
- local w,h = term.getSize()
- --Feed Cobble on Door Check
- local cobbleFeed = false
- rednet.open("top")
- m = peripheral.wrap(monitorSide)
- m.setTextScale(5)
- local wm,hm = m.getSize()
- term.redirect(peripheral.wrap(monitorSide))
- --Create Seed for RNG
- math.randomseed(os.time())
- function aSum(array,int)
- arraySum = 0
- for i=1,int do
- arraySum = arraySum + array[i]
- end
- return arraySum
- end
- function printCentered(str,ypos)
- term.setCursorPos(wm/2 - #str/2, ypos)
- term.write(str)
- end
- --Resets table to zero values
- function resetAnvils()
- for i= 1, anvilQuantity do
- anvils[i] = 0
- end
- end
- --Pulses all cables
- function dropAnvils()
- --If Maximum reached shut down program
- anvilsDropped = anvilsDropped + 1
- if anvilsDropped >= anvilMax then
- print("Anvil Maximum reached")
- running = false
- return
- end
- print("Dropping Anvil #"..anvilsDropped)
- --Resets Color Sum
- colorSum = 0
- --Creates Sum of Bundled Cable Colors
- for i= 1, anvilQuantity do
- if anvils[i] == 1 then
- colorSum = colorSum + colors[i]
- end
- end
- rs.setBundledOutput(bundledSide, colorSum)
- --Hold Signal for 1 seconds
- os.sleep(1)
- --Resets bundled output to false
- rs.setBundledOutput(bundledSide, 0)
- end
- --Creates Random Anvil Table
- function createAnvils()
- --Resets anvilSum
- anvilSum = 0
- --Choose random anvils until current difficulty is met
- while anvilSum < difficultyInt do
- anvils[math.random(anvilQuantity)] = 1
- sumAnvils()
- end
- end
- --Sums Anvil Array
- function sumAnvils()
- anvilSum = 0
- for i= 1,anvilQuantity do
- anvilSum = anvilSum + anvils[i]
- end
- end
- --Rednet Interpreter
- function rednetInterp()
- --Unlock Door
- if string.find(b, "N") then
- --Resets Ready Status when client reboots
- if a == playerIDOne then
- playerReady[1] = 0
- elseif a == playerIDTwo then
- playerReady[2] = 0
- end
- --Sets Doors to Unlock
- for i = 1,2 do
- if string.find(b, tostring(i)) then
- doorLock[i] = 0
- doorLock[i+2] = 0
- end
- end
- end
- --Lock Door
- if string.find(b, "S") then
- for i = 1,2 do
- if string.find(b,tostring(i)) then
- doorLock[i] = 1
- doorLock[i+2] = 1
- end
- end
- end
- --Player
- if string.find(b, "P") then
- for i = 1,2 do
- --One Player Mode
- if string.find(b,"1") then
- if a == playerIDOne then
- playerReady[1] = 2
- playerReady[2] = 0
- print("Player 1 Ready")
- elseif a == playerIdTwo then
- playerReady[1] = 0
- playerReady[2] = 2
- print("Player 2 Ready")
- end
- --Two Player Mode
- elseif string.find(b, "2") then
- if a == playerIDOne then
- playerReady[1] = 1
- print("Player 1 Ready")
- elseif a == playerIDTwo then
- playerReady[2] = 1
- print("Player 2 Ready")
- end
- end
- end
- end
- --Difficulty
- if string.find(b, "D") then
- for i = 16,1,-1 do
- if string.find(b,tostring(i)) then
- if a == playerIDOne then
- difficulty[1] = i
- print("P1Difficulty: "..i)
- break
- elseif a ==playerIDTwo then
- difficulty[2] = i
- print("P2Difficulty: "..i)
- break
- end
- end
- end
- end
- end
- --Checks doors and opens/closes them
- function doorCheck()
- local doorBundle = 0
- for i = 1,4 do
- if doorLock[i] == 1 then
- doorBundle = doorBundle + colors[i]
- end
- end
- --Close Doors and sleep before feeding cobble
- rs.setBundledOutput(doorSide, doorBundle)
- if cobbleFeed then
- os.sleep(1)
- for i = 1,2 do
- if playerReady[i] > 0 then
- doorBundle = doorBundle + colors[i+4]
- cobbleFeed = false
- end
- end
- end
- rs.setBundledOutput(doorSide, doorBundle)
- end
- --Ready to Rollout
- function rollout()
- --If Players are ready
- if aSum(playerReady,2) == 2 then
- print("Game Starting")
- --Calculate Average Difficulty if 2 Player
- for k,v in pairs(playerReady) do
- if v == 1 then
- difficultyInt = aSum(difficulty,2)/2
- elseif v == 2 then
- difficultyInt = difficulty[k]
- end
- end
- print("Difficulty: "..difficultyInt)
- --Lock Doors
- doorLock = {1, 1, 1, 1}
- --Spit Out Cobble and Door Check
- cobbleFeed = true
- doorCheck()
- --5Sec Countdown on Monitor
- for i = 5,1,-1 do
- os.sleep(1)
- print(i)
- end
- --set listening to false to break listen loop
- rednet.broadcast("reboot")
- listening = false
- end
- end
- --INITIALIZATION
- term.clear()
- printCentered(" Dodge", 2)
- printCentered(" the", 3)
- printCentered(" Anvils!",4)
- printCentered(" --ready--",7)
- while listening do
- event, a, b, c = os.pullEvent()
- if event == "rednet_message" then
- rednetInterp()
- doorCheck()
- rollout()
- end
- end
- --OPERATION--
- resetAnvils()
- --Open Doors
- doorLock = {0, 0, 0, 0}
- doorCheck()
- --Start 1s Timer of open Doors
- anvilTimer = os.startTimer(1)
- while running do
- event, a, b, c = os.pullEvent()
- --If loser redstone triggers stops operation
- if event == "redstone" then
- if rs.getInput(loserSide) then
- print("Death detected")
- running = false
- end
- end
- --If timer hits send more anvils
- if event == "timer" then
- --Lock Doors
- doorLock = {1, 1, 1, 1}
- doorCheck()
- resetAnvils()
- createAnvils()
- dropAnvils()
- anvilTimer = os.startTimer(anvilInterval)
- end
- end
- --END OF PROGRAM
- --Unlocks doors and reboots
- print("Game Over! Thanks for playing")
- doorLock = {0, 0, 0, 0}
- doorCheck()
- --Activate Rose Stealer for 1s then turn off
- rs.setBundledOutput(doorSide, colors[7])
- os.sleep(1)
- rs.setBundledOutput(doorSide, 0)
- os.sleep(9)
- os.reboot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement