Advertisement
Guest User

Untitled

a guest
Nov 12th, 2012
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.86 KB | None | 0 0
  1. -- These are two ComputerCraft Lua programs that I've made for a Capture the Flag game in Tekkit.
  2. -- This is a rough program and not considered complete. I'm posting it here for a RFC.
  3. -- Check out my reddit thread at http://www.reddit.com/r/tekkit/comments/131787/work_in_progress_any_ideas_for_a_ctf_map/
  4.  
  5. -- Referee
  6. -- The referee is a ComputerCraft computer that's intended to be in an observation area. It's programmed
  7. -- to read signals from three bundled cables, and then provide a simple display on a monitor.
  8. -- The bundled cable off the right is for the red team, the left is for the blue team, and the back is
  9. -- reserved for a transmitter that signals the start of the game.
  10.  
  11. function checkError()
  12.     -- this will check to see if there's an error for either team's computer and return a bool
  13.     if (redstone.testBundledInput("left", colors.orange)) or (redstone.testBundledInput("right", colors.orange)) then
  14.         return true
  15.     else
  16.         return false
  17.     end
  18. end
  19.  
  20. function checkReady()
  21.     -- this will check to see that both teams are ready to start the game
  22.     if (redstone.testBundledInput("left", colors.lime)) and (redstone.testBundledInput("right", colors.lime)) then
  23.         return true
  24.     else
  25.         return false
  26.     end
  27. end
  28.  
  29. function setStart()
  30.     redstone.setBundledOutput("back", colors.white)
  31. end
  32.  
  33.  
  34. while true do
  35.    
  36.     print("Please wait five seconds while I make sure there are no errors.\n")
  37.     sleep(5)
  38.     if (checkError()) then
  39.         print("There has been an error and I am unable to continue.")
  40.         print("Make sure there is a disk in the left disk drive of each team's computer.")
  41.         break
  42.     end
  43.    
  44.     print("Ready to start.")
  45.     print("Waiting for each team to signal that they are ready.\n")
  46.    
  47.     while true do
  48.         os.pullEvent("redstone")
  49.         if checkReady() then
  50.             print("Both teams ready!")
  51.             print("Starting game!")
  52.             setStart()
  53.             break
  54.         end
  55.     end
  56.    
  57.     local win = false
  58.    
  59.     while true do
  60.    
  61.         monitor = peripheral.wrap("top")
  62.         monitor.setTextScale(1.5)
  63.         term.redirect(monitor)
  64.        
  65.         os.pullEvent("redstone")
  66.        
  67.         term.clear()
  68.         term.setCursorPos(10,1)
  69.        
  70.         term.write("RED")
  71.        
  72.         term.setCursorPos(20,1)
  73.         term.write("BLUE")
  74.        
  75.         term.setCursorPos(1,2)
  76.         term.write("FLAG")
  77.        
  78.         -- stuff for flag here
  79.         if (redstone.testBundledInput("right", colors.magenta)) then
  80.             term.setCursorPos(10,2)
  81.             term.write("STOLEN")
  82.         end
  83.        
  84.         if (redstone.testBundledInput("left", colors.magenta)) then
  85.             term.setCursorPos(20,2)
  86.             term.write("STOLEN")
  87.         end
  88.        
  89.         if (redstone.testBundledInput("left", colors.lightBlue)) then
  90.             -- red team has captured blue flag
  91.             term.setCursorPos(10,2)
  92.             term.write("CAPTURED!")
  93.         end
  94.        
  95.         if (redstone.testBundledInput("right", colors.lightBlue)) then
  96.             term.setCursorPos(20,2)
  97.             term.write("CAPTURED!")
  98.         end
  99.        
  100.         if (redstone.testBundledInput("left", colors.yellow)) then
  101.             term.clear()
  102.             term.setCursorPos(20,4)
  103.             term.write("GAME OVER!")
  104.             term.setCursorPos(20,5)
  105.             term.write("BLUE TEAM WINS!")
  106.             win = true
  107.             break
  108.         end
  109.        
  110.         if (redstone.testBundledInput("right", colors.yellow)) then
  111.             term.clear()
  112.             term.setCursorPos(20,4)
  113.             term.write("GAME OVER!")
  114.             term.setCursorPos(17,5)
  115.             term.write("RED TEAM WINS!")
  116.             win = true
  117.             break
  118.         end    
  119.        
  120.     end
  121.    
  122.     if win then
  123.         term.setCursorPos(1,18)
  124.         term.restore()
  125.         break
  126.     end
  127.    
  128. end
  129.  
  130.  
  131. -- RedTeamBase
  132. -- Each base has a ComputerCraft setup with two disk drives with bundled cable off the back, and
  133. -- transmitters/receivers. The "flags" for the game are labeled floppy disks for ComputerCraft. The
  134. -- program for each base is identical except where the red/blue things have been transposed.
  135.  
  136. function isStolen()
  137.     if disk.hasData("left") then
  138.         label = disk.getLabel("left")
  139.         if label == "Red Flag" then
  140.             return false
  141.         end
  142.     end
  143.     if disk.hasData("right") then
  144.         label = disk.getLabel("right")
  145.         if label == "Red Flag" then
  146.             return false
  147.         end
  148.     end
  149.     return true
  150. end
  151.  
  152. function isCaptured()
  153.     if disk.hasData("left") then
  154.         label = disk.getLabel("left")
  155.         if label == "Blue Flag" then
  156.             return true
  157.         end
  158.     end
  159.     if disk.hasData("right") then
  160.         label = disk.getLabel("right")
  161.         if label == "Blue Flag" then
  162.             return true
  163.         end
  164.     end
  165.     return false
  166. end
  167.  
  168. function amReady()
  169.     if disk.hasData("left") then
  170.         label = disk.getLabel("left")
  171.         if label == "Red Flag" then
  172.             return true
  173.         else
  174.             disk.setLabel("left", "Red Flag")
  175.             return true
  176.         end
  177.     else
  178.         return false
  179.     end
  180. end
  181.  
  182. while true do
  183.     redstone.setBundledOutput("back",0)
  184.     if amReady() then
  185.         print("Ready to start!")
  186.         print("Press any key to signal the referee that your team is ready to start.")
  187.         local event, param1 = os.pullEvent("key")
  188.         print("Regeree signaled. Game will start once other team is ready.")
  189.         curOut = redstone.getBundledOutput("back")
  190.         newOut = colors.combine(curOut, colors.lime)
  191.         redstone.setBundledOutput("back",newOut)
  192.         while true do
  193.             if redstone.testBundledInput("back",colors.white) then -- start signal is on
  194.                 print("Game started!")
  195.                 while true do
  196.                    
  197.                     if (isStolen()) then
  198.                         -- turn on "stolen" signal
  199.                         curOut = redstone.getBundledOutput("back")
  200.                         newOut = colors.combine(curOut, colors.magenta)
  201.                         redstone.setBundledOutput("back",newOut)
  202.                         print("Flag stolen!")
  203.                     else
  204.                         -- turn off the "stolen" signal
  205.                         curOut = redstone.getBundledOutput("back")
  206.                         newOut = colors.subtract(curOut, colors.magenta)
  207.                         redstone.setBundledOutput("back",newOut)
  208.                         if isCaptured() then
  209.                             -- turn on "captured" signal
  210.                             curOut = redstone.getBundledOutput("back")
  211.                             newOut = colors.combine(curOut, colors.lightBlue)
  212.                             redstone.setBundledOutput("back",newOut)
  213.                             print("Enemy flag captured! You must now wait 30 seconds before claiming victory.")
  214.                             sleep(30) -- the team must hold onto the flag for a while before claiming victory
  215.                             if not (isStolen()) and (isCaptured()) then
  216.                                 -- turn on win signal
  217.                                 curOut = redstone.getBundledOutput("back")
  218.                                 newOut = colors.combine(curOut, colors.yellow)
  219.                                 redstone.setBundledOutput("back",newOut)
  220.                                 print("Win conditions met. Check referee for final result.")
  221.                                 break
  222.                             else
  223.                                 -- turn off captured signal
  224.                                 curOut = redstone.getBundledOutput("back")
  225.                                 newOut = colors.subtract(curOut, colors.lightBlue)
  226.                                 redstone.setBundledOutput("back",newOut)
  227.                                 print("Enemy flag retaken! Game continues!")
  228.                             end
  229.                         end
  230.                     end
  231.                     sleep(1)
  232.                    
  233.                 end
  234.                
  235.                 break
  236.                
  237.             else
  238.                 sleep(1)
  239.             end
  240.            
  241.         end
  242.        
  243.         break
  244.        
  245.     else
  246.         -- turn on the error signal
  247.         curOut = redstone.getBundledOutput("back")
  248.         newOut = colors.combine(curOut, colors.orange)
  249.         redstone.setBundledOutput("back",newOut)
  250.         print("ERROR: There must be a disk in the left drive before I can start.")
  251.         print("Insert a disk and restart the computer.")
  252.         break
  253.     end
  254. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement