MattiasBuelens

NetherPVP cell

Aug 12th, 2012
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.27 KB | None | 0 0
  1. --[[
  2.  
  3.     Nether PVP Arena
  4.     Cell program
  5.  
  6. --]]
  7.  
  8. -- Load dependencies
  9. dofile("/common/all")
  10. program.load("../mcnet/message", "../mcnet/names", "../mcnet/device")
  11. program.load("../mcnet/devices/servant")
  12. program.load("msg", "devices/cell")
  13.  
  14. -- Create cell
  15. local configPath = "/cell.conf"
  16. local defaultConfig = {
  17.     name = "cell",
  18.     domain = "netherpvp",
  19.     detectTimeout = 1
  20. }
  21. local config = common.loadStore(configPath, defaultConfig)
  22. local cell = game.Cell:new{
  23.     -- MCNet
  24.     name = config.name,
  25.     domain = config.domain,
  26.     -- Game
  27.     winTimeout = 5,
  28.     -- Redstone
  29.     getColorSide = "front",
  30.     setColorSide = "back",
  31.     setColorBit = colors.black,
  32.     leverSide = "back",
  33.     leverBit = colors.green,
  34.     resetLeverSide = "back",
  35.     resetLeverBit = colors.brown,
  36.     openSide = "back",
  37.     openBit = colors.red
  38. }
  39.  
  40. print("  Address: "..mcnet.address.format(cell.address))
  41. print("  Computer ID: "..tostring(os.computerID()))
  42. print()
  43.  
  44. -- Register handlers
  45. cell:on("registerServer", function(masterId)
  46.     print("Registering master at ", masterId, "...")
  47. end)
  48.  
  49. cell:on("registeredServer", function()
  50.     print("  Successfully registered at master")
  51. end)
  52.  
  53. cell:on("unregisteredServer", function(masterId)
  54.     print("Unregistered master at ", masterId)
  55. end)
  56.  
  57. cell:on("newRound", function()
  58.     print("New round started")
  59. end)
  60.  
  61. cell:on("winRound", function()
  62.     print("Round won!")
  63. end)
  64.  
  65. cell:on("char", function(char)
  66.     if char == "q" then
  67.         -- Confirm quit
  68.         write("Confirm quit? [y/n] > ")
  69.         local event, char = os.pullEvent("char")
  70.         -- Check response
  71.         local response = (string.lower(char) == "y" and "y") or "n"
  72.         print(response)
  73.         if (response == "y") then
  74.             cell:stop()
  75.         end
  76.     end
  77. end)
  78.  
  79. cell:on("error", function(errorType, errorMessage)
  80.     print("[ERROR] "..errorMessage)
  81. end)
  82.  
  83. -- Start
  84. print("Starting...")
  85. cell:start(config.detectTimeout)
  86. write("  Detected:")
  87. for i,deviceId in pairs(cell:neighbours()) do
  88.     write(" "..deviceId)
  89. end
  90. print()
  91. print()
  92.  
  93. -- Main loop
  94. print("Listening...")
  95. print("  Press Q to quit")
  96. print()
  97.  
  98. while cell.isRunning do
  99.     cell:trigger(os.pullEvent())
  100. end
  101.  
  102. -- Stop
  103. print()
  104. print("Stopped")
  105. print()
Advertisement
Add Comment
Please, Sign In to add comment