Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local config = {
- positions = {
- teleports = {
- back_to_temple = Position(120,127, 7),
- tp_to_event = Position(128, 125, 7)
- },
- event_area = {
- player_spawn = Position(128, 127, 7),
- from = Position(120, 127, 7),
- to = Position(138, 136, 7)
- },
- rooms = {
- [1] = {
- entrance = Position(122, 131, 7),
- from = Position(120, 132, 7),
- to = Position(123, 136, 7)
- },
- [2] = {
- entrance = Position(127, 131, 7),
- from = Position(125, 132, 7),
- to = Position(128, 136, 7)
- },
- [3] = {
- entrance = Position(132, 131, 7),
- from = Position(130, 132, 7),
- to = Position(133, 136, 7)
- },
- [4] = {
- entrance = Position(137, 131, 7),
- from = Position(135, 132, 7),
- to = Position(138, 136, 7)
- }
- }
- },
- storages = {
- event_status = 2020 -- on/off
- },
- messages = {
- start = "Room event has started, find the room in Thais DP to access the event. %d seconds before teleport dissappears",
- time_to_pick = "Pick a room, you only have %d seconds before the gates close!",
- picking_room = "Choosing room randomly...",
- room_clear = "Room %d has been cleared.",
- event_end = "Room event has ended.",
- winner = "%s is the winner!",
- no_winner = "Nobody won the event.",
- out_of_room = "You did not get into a room in time, too slow!",
- lost = "You chose the wrong room."
- },
- times = {
- start = 120, --event will start in 120 seconds after saying the command
- clean_room = 10, --how much time players will have to pick a room
- suspense_time = 3 --how much time before room gets cleaned after pick
- },
- other = {
- gate = 1547, --id of gate
- rewards = {{2160, 6}, {2159, 3}} --itemi id, amount
- }
- }
- local rooms = config.positions.rooms
- function onThink(interval, lastExecution)
- if Game.getStorageValue(config.storages.event_status) == nil then
- Game.setStorageValue(config.storages.event_status, 0)
- end
- if Game.getStorageValue(config.storages.event_status) > 0 then
- print("[Room event]: Prevented event overlapping")
- return true
- end
- Game.setStorageValue(config.storages.event_status, 1)
- Game.broadcastMessage(string.format('[Room event]: ' .. config.messages.start, config.times.start), MESSAGE_EVENT_ADVANCE)
- local tp = Game.createItem(1387, 1, config.positions.teleports.tp_to_event)
- local teleport = Teleport(tp.uid):setDestination(config.positions.event_area.player_spawn)
- addEvent(Game.broadcastMessage, (config.times.start / 2) * 1000, string.format('[Room event]: ' .. config.messages.start, config.times.start / 2), MESSAGE_EVENT_ADVANCE)
- addEvent(function() Item(tp.uid):remove() startEvent() end, config.times.start * 1000)
- return true
- end
- function startEvent()
- messagePlayers(getPlayersInEvent(), string.format(config.messages.time_to_pick, config.times.clean_room))
- addEvent(closeGates, config.times.clean_room * 1000)
- end
- function closeGates()
- removePlayersOutOfRoom(getPlayersInEvent())
- for i = 1, #rooms do
- Game.createItem(config.other.gate, 1, rooms[i].entrance)
- end
- messagePlayers(getPlayersInEvent(), config.messages.picking_room)
- addEvent(chooseRoom, config.times.suspense_time * 1000)
- end
- function chooseRoom()
- local chosen_room = math.random(1, #rooms)
- for _, loser in ipairs(getPlayersInRoom(rooms[chosen_room])) do
- loser:sendTextMessage(MESSAGE_EVENT_ADVANCE, "[Room event]: " .. config.messages.lost)
- loser:teleportTo(getTownTemplePosition(loser:getTown():getId()))
- end
- messagePlayers(getPlayersInEvent(), string.format(config.messages.room_clear, chosen_room))
- for i = 1, #rooms do
- local gate = getTileItemById(rooms[i].entrance, config.other.gate)
- if gate.itemid == config.other.gate then
- Item(gate.uid):remove()
- end
- end
- local game_stat = checkForWinner()
- if game_stat == nil then
- startEvent()
- elseif not game_stat then
- Game.broadcastMessage('[Room event]: ' .. config.messages.no_winner, MESSAGE_EVENT_ADVANCE)
- Game.setStorageValue(config.storages.event_status, 0)
- else
- Game.setStorageValue(config.storages.event_status, 0)
- for _, i in pairs(config.other.rewards) do
- game_stat:addItem(i[1], i[2])
- end
- game_stat:teleportTo(getTownTemplePosition(game_stat:getTown():getId()))
- Game.broadcastMessage(string.format('[Room event]: ' .. config.messages.winner, game_stat:getName()), MESSAGE_EVENT_ADVANCE)
- end
- end
- function checkForWinner()
- local count = 0
- local last_player = nil
- for _, i in ipairs(getPlayersInEvent()) do
- count = count + 1
- last_player = i
- end
- if count > 1 then
- return nil
- elseif count == 0 then
- return false
- else
- return last_player
- end
- end
- function getPlayersInEvent()
- local players = {}
- for _, pid in ipairs(getOnlinePlayers()) do
- if Player(pid):getPosition():isInRange(config.positions.event_area.from, config.positions.event_area.to) then
- table.insert(players, Player(pid))
- end
- end
- return players
- end
- function getPlayersInRoom(room)
- local players = {}
- for _, pid in ipairs(getOnlinePlayers()) do
- if Player(pid):getPosition():isInRange(room.from, room.to) then
- table.insert(players, Player(pid))
- end
- end
- return players
- end
- function removePlayersOutOfRoom(players_in_event)
- local players = {}
- for i = 1, #rooms do
- for _, player_in_room in ipairs(getPlayersInRoom(rooms[i])) do
- table.insert(players, player_in_room)
- end
- end
- for _, player_in_event in ipairs(players_in_event) do
- if not isInArray(players, player_in_event) then
- player_in_event:teleportTo(getTownTemplePosition(player_in_event:getTown():getId()))
- player_in_event:sendTextMessage(MESSAGE_EVENT_ADVANCE, "[Room event]: " .. config.messages.out_of_room)
- end
- end
- end
- function messagePlayers(players, message)
- for _, player in ipairs(players) do
- player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "[Room event]: " .. message)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement