Advertisement
Guest User

[TFS 1.x] Tron Event by Printer V0.5

a guest
Jan 6th, 2016
661
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.84 KB | None | 0 0
  1. local RED_TRON = 1
  2. local YELLOW_TRON = 2
  3. local PURPLE_TRON = 3
  4.  
  5. local config = {
  6.     playerSpeed = 1500,
  7.     tronSpeed = 100,
  8.     reward = {
  9.         {id = 2160, count = 1},
  10.         {id = 2152, count = 5},
  11.         {id = 2148, count = 10}
  12.     },
  13.     position = {
  14.         playerPosition = { -- Position of the tiles, when they press the lever
  15.             Position(159, 386, 5),
  16.             Position(160, 386, 5),
  17.             Position(161, 386, 5)
  18.         },
  19.         newPosition = { -- Starter position, when they get teleported inside
  20.             [RED_TRON] = Position(142, 404, 15),
  21.             [YELLOW_TRON] = Position(142, 364, 15),
  22.             [PURPLE_TRON] = Position(196, 364, 15)
  23.         },
  24.         arenaPosition = {
  25.             fromPosition = Position(134, 356, 15), -- Top left of the area
  26.             toPosition = Position(202, 411, 15) -- Bottom right of the area
  27.         }
  28.     }
  29. }
  30.  
  31.  
  32. if not tronEvent then
  33.     tronEvent = {
  34.         players = {},
  35.  
  36.         joinCount = 0,
  37.         running = false,
  38.  
  39.         eventStorage = 2000,
  40.         colorStorage = 2001,
  41.         lifeStorage = 2002,
  42.  
  43.         color = {
  44.             [RED_TRON] = {field = 1492, colorName = "Red"},
  45.             [YELLOW_TRON] = {field = 1500, colorName = "Yellow"},
  46.             [PURPLE_TRON] = {field = 1506, colorName = "Purple"}
  47.         },
  48.         tronDirection = {
  49.             [DIRECTION_NORTH] = 7132,
  50.             [DIRECTION_EAST] = 7131,
  51.             [DIRECTION_SOUTH] = 7132,
  52.             [DIRECTION_WEST] = 7131
  53.         },
  54.     }
  55. end
  56.  
  57. function tronEvent.getPlayerColor(self, player)
  58.     return player:getStorageValue(self.colorStorage)
  59. end
  60.  
  61. function tronEvent.addReward(self, player)
  62.     for i = 1, #config.reward do
  63.         local reward = config.reward[i]
  64.         player:addItem(reward.id, reward.count)
  65.     end
  66.  
  67.     player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have been rewarded for winning, Tron event.")
  68. end
  69.  
  70. function tronEvent.onUseLever(self, player)
  71.     if self.running then
  72.         player:sendTextMessage(MESSAGE_STATUS_SMALL, "There is already someone in the event.")
  73.         return false
  74.     end
  75.  
  76.     -- Are the players on the tiles?
  77.     local storePlayers = {}
  78.     for i = 1, #config.position.playerPosition do
  79.         local playerTile = Tile(config.position.playerPosition[i]):getTopCreature()
  80.         if not playerTile or not playerTile:isPlayer() then
  81.             player:sendTextMessage(MESSAGE_STATUS_SMALL, "You need 3 players.")
  82.             return false
  83.         end
  84.  
  85.         storePlayers[#storePlayers + 1] = playerTile
  86.     end
  87.  
  88.     for i = 1, #storePlayers do
  89.         self:addPlayer(storePlayers[i])
  90.     end
  91.  
  92.     self:broadcastMessage(MESSAGE_INFO_DESCR, "Welcome to Tron, the following commands are available:\n!tron boost -- To speed up.\n!tron jump -- To join over fields.\nIt is strongly recommended that you bind these commands to your hotkeys.")
  93.     addEvent(tronEvent.startCountdown, 13 * 1000, self, 3)
  94.     return true
  95. end
  96.  
  97. local tronOutfitCondition = Condition(CONDITION_OUTFIT, CONDITIONID_COMBAT)
  98. tronOutfitCondition:setTicks(-1)
  99.  
  100. function tronEvent.addPlayer(self, player)
  101.     -- Increase the join count by "+1"
  102.     self.joinCount = self.joinCount + 1
  103.  
  104.     -- Add Storages
  105.     player:setStorageValue(self.eventStorage, 1)
  106.     player:setStorageValue(self.colorStorage, self.joinCount)
  107.     player:setStorageValue(self.lifeStorage, 3)
  108.  
  109.     -- Teleport
  110.     local startPosition = config.position.newPosition[self.joinCount]
  111.     player:getPosition():sendMagicEffect(CONST_ME_POFF)
  112.     player:teleportTo(startPosition)
  113.     startPosition:sendMagicEffect(CONST_ME_TELEPORT)
  114.  
  115.     -- Remove Speed
  116.     player:changeSpeed(-player:getSpeed())
  117.  
  118.     -- Store player
  119.     self.players[#self.players + 1] = player:getId()
  120. end
  121.  
  122. function tronEvent.moveTron(self, cid)
  123.     local player = Player(cid)
  124.     if not player then
  125.         return
  126.     end
  127.  
  128.     -- Has no event storage, then stop it
  129.     if player:getStorageValue(self.eventStorage) ~= 1 then
  130.         return
  131.     end
  132.  
  133.     -- Store Last Position, so we can add the fields behind the tron
  134.     local lastPosition = player:getPosition()
  135.  
  136.     -- Direction to move
  137.     local direction = player:getDirection()
  138.     doMoveCreature(cid, direction)
  139.  
  140.     -- Lets add the color field, behind the tron, with player id as attribute
  141.     local colorId = self:getPlayerColor(player)
  142.     if colorId == -1 then
  143.         return
  144.     end
  145.  
  146.     local colorField = Game.createItem(self.color[colorId].field, 1, lastPosition)
  147.     if colorField then
  148.         colorField:setAttribute(ITEM_ATTRIBUTE_TEXT, cid)
  149.     end
  150.  
  151.     -- Outfit
  152.     tronOutfitCondition:setOutfit(self.tronDirection[direction])
  153.     player:addCondition(tronOutfitCondition)
  154.  
  155.     addEvent(tronEvent.moveTron, config.tronSpeed, self, cid)
  156. end
  157.  
  158. function tronEvent.addTron(self, player)
  159.     -- Add speed
  160.     player:changeSpeed(config.playerSpeed)
  161.  
  162.     -- Move the tron
  163.     self:moveTron(player:getId())
  164. end
  165.  
  166. function tronEvent.onPlayerDeath(self, player, killerId)
  167.     -- Clear the field of this tron
  168.     local arenaPosition = config.position.arenaPosition
  169.     for x = arenaPosition.fromPosition.x, arenaPosition.toPosition.x do
  170.         for y = arenaPosition.fromPosition.y, arenaPosition.toPosition.y do
  171.             for z = arenaPosition.fromPosition.z, arenaPosition.toPosition.z do
  172.                 local tile = Tile(x, y, z)
  173.                 if tile then
  174.                     for _, item in ipairs(tile:getItems()) do
  175.                         -- Remove the fields
  176.                         if item:getType():getType() == ITEM_TYPE_MAGICFIELD then
  177.                             if item:getId() == self.color[self:getPlayerColor(player)].field then
  178.                                 item:remove()
  179.                             end
  180.                         end
  181.                     end
  182.                 end
  183.             end
  184.         end
  185.     end
  186.  
  187.     -- Remove life point
  188.     local life = math.max(1, player:getStorageValue(self.lifeStorage))
  189.     player:setStorageValue(self.lifeStorage, life - 1)
  190.  
  191.     -- Teleport starting position
  192.     local position = config.position.newPosition[self:getPlayerColor(player)]
  193.     player:teleportTo(position)
  194.     position:sendMagicEffect(CONST_ME_TELEPORT)
  195.  
  196.     local playerName, killerName = player:getName(), "undefined"
  197.     local playerColor, killerColor = self.color[self:getPlayerColor(player)].colorName, "undefined"
  198.  
  199.     local killer = Player(killerId)
  200.     if killer then
  201.         killerName = killer:getName()
  202.         killerColor = self.color[self:getPlayerColor(killer)].colorName
  203.  
  204.         -- Send Message
  205.         player:sendTextMessage(MESSAGE_EVENT_ADVANCE, string.format("You were killed by %s[%s] - %d life left.", killerName, killerColor, life - 1))
  206.         killer:sendTextMessage(MESSAGE_EVENT_ADVANCE, string.format("You killed %s[%s]", playerName, playerColor))
  207.     end
  208.  
  209.     -- Broadcast "Death"
  210.     if life > 1 then
  211.         self:broadcastMessage(MESSAGE_INFO_DESCR, string.format("[Tron Event]: %s[%s] were killed by %s[%s].", playerName, playerColor, killerName, killerColor))
  212.     else
  213.         self:broadcastMessage(MESSAGE_INFO_DESCR, string.format("[Tron Event]: %s[%s]! Has been eliminated by %s[%s].", playerName, playerColor, killerName, killerColor))
  214.         self:releasePlayer(player)
  215.     end
  216. end
  217.  
  218. function tronEvent.onPlayerCommands(self, player, keyword)
  219.     if keyword == "jump" then
  220.         -- Do action
  221.     elseif keyword == "boost" then
  222.         -- Do action
  223.     else
  224.         player:sendCancelMessage("Insufficient parameters.")
  225.     end
  226. end
  227.  
  228. function tronEvent.broadcastMessage(self, messageType, message)
  229.     if #self.players == 0 then
  230.         return
  231.     end
  232.  
  233.     if messageType == nil then
  234.         messageType = MESSAGE_STATUS_WARNING
  235.     end
  236.  
  237.     for i = 1, #self.players do
  238.         local tmpPlayer = Player(self.players[i])
  239.         if tmpPlayer then
  240.             tmpPlayer:sendTextMessage(messageType, message)
  241.         end
  242.     end
  243. end
  244.  
  245. function tronEvent.removePlayer(self, playerId)
  246.     if #self.players == 0 then
  247.         return
  248.     end
  249.  
  250.     for i = 1, #self.players do
  251.         if self.players[i] == playerId then
  252.             table.remove(self.players, i)
  253.             return
  254.         end
  255.     end
  256. end
  257.  
  258. function tronEvent.releasePlayer(self, player)
  259.     -- Remove Storages
  260.     player:setStorageValue(self.eventStorage, -1)
  261.     player:setStorageValue(self.colorStorage, -1)
  262.     player:setStorageValue(self.lifeStorage, -1)
  263.  
  264.     -- Remove outfit
  265.     player:removeCondition(CONDITION_OUTFIT, CONDITIONID_COMBAT)
  266.  
  267.     -- Change back speed
  268.     player:changeSpeed(-player:getSpeed())
  269.     player:changeSpeed(player:getBaseSpeed())
  270.  
  271.     -- Teleport player to temple
  272.     local templePosition = player:getTown():getTemplePosition()
  273.     player:teleportTo(templePosition)
  274.     templePosition:sendMagicEffect(CONST_ME_TELEPORT, player)
  275.  
  276.     -- Remove from player table
  277.     self:removePlayer(player:getId())
  278.  
  279.     -- Last player, then stop the event. We got a winner
  280.     if #self.players == 1 then
  281.         -- Fetch winner
  282.         for i = 1, #self.players do
  283.             local tmpPlayer = Player(self.players[i])
  284.             if tmpPlayer then
  285.                 self:addReward(tmpPlayer)
  286.                 self:releasePlayer(tmpPlayer)
  287.             end
  288.         end
  289.  
  290.         self:reset()
  291.     end
  292.            
  293. end
  294.  
  295. function tronEvent.startCountdown(self, count)
  296.     for i = 1, #self.players do
  297.         local tmpPlayer = Player(self.players[i])
  298.         if tmpPlayer then
  299.             tmpPlayer:say(string.format("%s", count == 0 and "GO!" or count), TALKTYPE_MONSTER_SAY)
  300.             if count == 0 then
  301.                 self:start()
  302.                 return
  303.             end
  304.         end
  305.     end
  306.  
  307.     addEvent(tronEvent.startCountdown, 1000, self, count - 1)
  308. end
  309.    
  310. function tronEvent.start(self)
  311.     self.running = true
  312.  
  313.     for i = 1, #self.players do
  314.         local tmpPlayer = Player(self.players[i])
  315.         if tmpPlayer then
  316.             self:addTron(tmpPlayer)
  317.         end
  318.     end
  319. end
  320.  
  321. function tronEvent.reset(self)
  322.     self.players = {}
  323.     self.running = false
  324.     self.joinCount = 0
  325. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement