Advertisement
Guest User

Untitled

a guest
Jan 24th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.76 KB | None | 0 0
  1. local config = {
  2.     positions = {
  3.         teleports = {
  4.             back_to_temple = Position(120,127, 7),
  5.             tp_to_event =  Position(128, 125, 7)
  6.         },
  7.         event_area = {
  8.             player_spawn = Position(128, 127, 7),
  9.             from = Position(120, 127, 7),
  10.             to = Position(138, 136, 7)
  11.         },
  12.         rooms = {
  13.             [1] = {
  14.                 entrance = Position(122, 131, 7),
  15.                 from = Position(120, 132, 7),
  16.                 to = Position(123, 136, 7)
  17.             },
  18.             [2] = {
  19.                 entrance = Position(127, 131, 7),
  20.                 from = Position(125, 132, 7),
  21.                 to = Position(128, 136, 7)
  22.             },
  23.             [3] = {
  24.                 entrance = Position(132, 131, 7),
  25.                 from = Position(130, 132, 7),
  26.                 to = Position(133, 136, 7)
  27.             },
  28.             [4] = {
  29.                 entrance = Position(137, 131, 7),
  30.                 from = Position(135, 132, 7),
  31.                 to = Position(138, 136, 7)
  32.             }
  33.         }
  34.     },
  35.     storages = {
  36.         event_status = 2020 -- on/off
  37.     },
  38.     messages = {
  39.         start = "Room event has started, find the room in Thais DP to access the event. %d seconds before teleport dissappears",
  40.         time_to_pick = "Pick a room, you only have %d seconds before the gates close!",
  41.         picking_room = "Choosing room randomly...",
  42.         room_clear = "Room %d has been cleared.",
  43.         event_end = "Room event has ended.",
  44.         winner = "%s is the winner!",
  45.         no_winner = "Nobody won the event.",
  46.         out_of_room = "You did not get into a room in time, too slow!",
  47.         lost = "You chose the wrong room."
  48.     },
  49.     times = {
  50.         start = 120, --event will start in 120 seconds after saying the command
  51.         clean_room = 10, --how much time players will have to pick a room
  52.         suspense_time = 3 --how much time before room gets cleaned after pick
  53.     },
  54.     other = {
  55.         gate = 1547, --id of gate
  56.         rewards = {{2160, 6}, {2159, 3}} --itemi id, amount
  57.     }
  58. }
  59. local rooms = config.positions.rooms
  60. function onThink(interval, lastExecution)
  61.     if Game.getStorageValue(config.storages.event_status) == nil then
  62.         Game.setStorageValue(config.storages.event_status, 0)
  63.     end
  64.     if Game.getStorageValue(config.storages.event_status) > 0 then
  65.         print("[Room event]: Prevented event overlapping")
  66.         return true
  67.     end
  68.     Game.setStorageValue(config.storages.event_status, 1)
  69.     Game.broadcastMessage(string.format('[Room event]: ' .. config.messages.start, config.times.start), MESSAGE_EVENT_ADVANCE)
  70.     local tp = Game.createItem(1387, 1, config.positions.teleports.tp_to_event)
  71.     local teleport = Teleport(tp.uid):setDestination(config.positions.event_area.player_spawn)
  72.     addEvent(Game.broadcastMessage, (config.times.start / 2) * 1000, string.format('[Room event]: ' .. config.messages.start, config.times.start / 2), MESSAGE_EVENT_ADVANCE)
  73.     addEvent(function() Item(tp.uid):remove() startEvent() end, config.times.start * 1000)
  74.     return true
  75. end
  76.  
  77. function startEvent()
  78.     messagePlayers(getPlayersInEvent(), string.format(config.messages.time_to_pick, config.times.clean_room))
  79.     addEvent(closeGates, config.times.clean_room * 1000)
  80. end
  81. function closeGates()
  82.     removePlayersOutOfRoom(getPlayersInEvent())
  83.     for i = 1, #rooms do
  84.         Game.createItem(config.other.gate, 1, rooms[i].entrance)
  85.     end
  86.     messagePlayers(getPlayersInEvent(), config.messages.picking_room)
  87.     addEvent(chooseRoom, config.times.suspense_time * 1000)
  88. end
  89. function chooseRoom()
  90.     local chosen_room = math.random(1, #rooms)
  91.     for _, loser in ipairs(getPlayersInRoom(rooms[chosen_room])) do
  92.         loser:sendTextMessage(MESSAGE_EVENT_ADVANCE, "[Room event]: " .. config.messages.lost)
  93.         loser:teleportTo(getTownTemplePosition(loser:getTown():getId()))
  94.     end
  95.     messagePlayers(getPlayersInEvent(), string.format(config.messages.room_clear, chosen_room))
  96.     for i = 1, #rooms do
  97.         local gate = getTileItemById(rooms[i].entrance, config.other.gate)
  98.         if gate.itemid == config.other.gate then
  99.             Item(gate.uid):remove()
  100.         end
  101.     end
  102.     local game_stat = checkForWinner()
  103.     if game_stat == nil then
  104.         startEvent()
  105.     elseif not game_stat then
  106.         Game.broadcastMessage('[Room event]: ' .. config.messages.no_winner, MESSAGE_EVENT_ADVANCE)
  107.         Game.setStorageValue(config.storages.event_status, 0)
  108.     else
  109.         Game.setStorageValue(config.storages.event_status, 0)
  110.         for _, i in pairs(config.other.rewards) do
  111.             game_stat:addItem(i[1], i[2])
  112.         end
  113.         game_stat:teleportTo(getTownTemplePosition(game_stat:getTown():getId()))
  114.         Game.broadcastMessage(string.format('[Room event]: ' .. config.messages.winner, game_stat:getName()), MESSAGE_EVENT_ADVANCE)
  115.     end
  116.    
  117. end
  118. function checkForWinner()
  119.     local count = 0
  120.     local last_player = nil
  121.     for _, i in ipairs(getPlayersInEvent()) do
  122.         count = count + 1
  123.         last_player = i
  124.     end
  125.     if count > 1 then
  126.         return nil
  127.     elseif count == 0 then
  128.         return false
  129.     else
  130.         return last_player
  131.     end
  132. end
  133. function getPlayersInEvent()
  134.     local players = {}
  135.     for _, pid in ipairs(getOnlinePlayers()) do
  136.         if Player(pid):getPosition():isInRange(config.positions.event_area.from, config.positions.event_area.to) then
  137.             table.insert(players, Player(pid))
  138.         end
  139.     end
  140.     return players
  141. end
  142. function getPlayersInRoom(room)
  143.     local players = {}
  144.     for _, pid in ipairs(getOnlinePlayers()) do
  145.         if Player(pid):getPosition():isInRange(room.from, room.to) then
  146.             table.insert(players, Player(pid))
  147.         end
  148.     end
  149.     return players
  150. end
  151. function removePlayersOutOfRoom(players_in_event)
  152.     local players = {}
  153.     for i = 1, #rooms do
  154.         for _, player_in_room in ipairs(getPlayersInRoom(rooms[i])) do
  155.             table.insert(players, player_in_room)
  156.         end
  157.     end
  158.     for _, player_in_event in ipairs(players_in_event) do
  159.         if not isInArray(players, player_in_event) then
  160.             player_in_event:teleportTo(getTownTemplePosition(player_in_event:getTown():getId()))
  161.             player_in_event:sendTextMessage(MESSAGE_EVENT_ADVANCE, "[Room event]: " .. config.messages.out_of_room)
  162.         end
  163.     end
  164. end
  165. function messagePlayers(players, message)
  166.     for _, player in ipairs(players) do
  167.         player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "[Room event]: " .. message)
  168.     end
  169. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement