Advertisement
Guest User

Deepwind.lua

a guest
Jun 17th, 2014
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.79 KB | None | 0 0
  1. -- Deepwind Gorge mod v3.0
  2. -- written by Cenuij
  3.  
  4. local mod       = DBM:NewMod("z1105", "DBM-PvP", 2)
  5. local L         = mod:GetLocalizedStrings()
  6.  
  7. mod:SetRevision(("$Revision: 28 $"):sub(12, -3))
  8. mod:SetZone(DBM_DISABLE_ZONE_DETECTION)
  9.  
  10. mod:RegisterEvents(
  11.     "ZONE_CHANGED_NEW_AREA"
  12. )
  13.  
  14. local capTimer  = mod:NewTimer(60, "TimerCap", "Interface\\Icons\\Spell_Misc_HellifrePVPHonorHoldFavor")
  15. local winTimer  = mod:NewTimer(30, "TimerWin", "Interface\\Icons\\INV_Misc_PocketWatch_01")
  16.  
  17. local bgzone = false
  18. local GetMapLandmarkInfo, GetNumMapLandmarks = GetMapLandmarkInfo, GetNumMapLandmarks
  19.  
  20. mod:RemoveOption("HealthFrame")
  21. mod:RemoveOption("SpeedKillTimer")
  22.  
  23. local ResPerSec = {
  24.     [0] = 1e-300,
  25.     [1] = 8/5,
  26.     [2] = 16/5,
  27.     [3] = 32/5,
  28. }
  29.  
  30. local allyColor = {
  31.     r = 0,
  32.     g = 0,
  33.     b = 1,
  34. }
  35. local hordeColor = {
  36.     r = 1,
  37.     g = 0,
  38.     b = 0,
  39. }
  40.  
  41. local objectives = {}
  42.  
  43. local function get_state_from_texture(id)
  44.     if id == 18 then
  45.         return 1    -- Alliance controlled
  46.     elseif id == 20 then
  47.         return 2    -- Horde controlled
  48.     elseif id == 17 then
  49.         return 3    -- Alliance assaulted
  50.     elseif id == 19 then
  51.         return 4    -- Horde assaulted
  52.     else
  53.         return -1
  54.     end
  55. end
  56.  
  57. local function get_objectives()
  58.     local result = {}
  59.     for i=1, GetNumMapLandmarks(), 1 do
  60.         local name, _, texture = GetMapLandmarkInfo(i)
  61.         if name and texture then
  62.             result[name] = get_state_from_texture(texture)
  63.         end
  64.     end
  65.     return result
  66. end
  67.  
  68. local function get_basecount()
  69.     local alliance = 0
  70.     local horde = 0
  71.     for name,state in pairs(objectives) do
  72.         if 1 == state then
  73.             alliance = alliance + 1
  74.         elseif 2 == state then
  75.             horde = horde + 1
  76.         end
  77.     end
  78.     return alliance, horde
  79. end
  80.  
  81. local function get_score()
  82.     if not bgzone then return 0,0 end
  83.     local AllyScore     = tonumber(string.match((select(4, GetWorldStateUIInfo(1)) or ""), L.ScoreExpr)) or 0
  84.     local HordeScore    = tonumber(string.match((select(4, GetWorldStateUIInfo(2)) or ""), L.ScoreExpr)) or 0
  85.     return AllyScore, HordeScore
  86. end
  87.  
  88. local get_gametime
  89. local update_gametime
  90. do
  91.     local gametime = 0
  92.     function update_gametime()
  93.         gametime = time()
  94.     end
  95.     function get_gametime()
  96.         local systime = GetBattlefieldInstanceRunTime()
  97.         if systime > 0 then
  98.             return systime / 1000
  99.         else
  100.             return time() - gametime
  101.         end
  102.     end
  103. end
  104.  
  105. local function Deepwind_Initialize()
  106.     if 1105 == DBM:GetCurrentArea() then
  107.         bgzone = true
  108.         mod:RegisterShortTermEvents(
  109.             "CHAT_MSG_BG_SYSTEM_HORDE",
  110.             "CHAT_MSG_BG_SYSTEM_ALLIANCE",
  111.             "CHAT_MSG_BG_SYSTEM_NEUTRAL",
  112.             "CHAT_MSG_RAID_BOSS_EMOTE",
  113.             "UPDATE_WORLD_STATES"
  114.         )
  115.         objectives = get_objectives()
  116.     elseif bgzone then
  117.         bgzone = false
  118.         mod:UnregisterShortTermEvents()
  119.     end
  120. end
  121.  
  122. mod.OnInitialize = Deepwind_Initialize
  123.  
  124. function mod:ZONE_CHANGED_NEW_AREA()
  125.     self:Schedule(1, Deepwind_Initialize)
  126. end
  127.  
  128.  
  129. --
  130. -- capTimer
  131. --
  132. do
  133.     local function check_for_updates()
  134.         if not bgzone then return end
  135.         local curobj = get_objectives()
  136.         for name,state in pairs(curobj) do
  137.             if state ~= objectives[name] then
  138.                 capTimer:Stop(name)
  139.                 if 3 == state then
  140.                     capTimer:Start(nil, name)
  141.                     capTimer:SetColor(allyColor, name)
  142.                     capTimer:UpdateIcon("Interface\\Icons\\INV_BannerPVP_02.blp", name)
  143.                 elseif 4 == state then
  144.                     capTimer:Start(nil, name)
  145.                     capTimer:SetColor(hordeColor, name)
  146.                     capTimer:UpdateIcon("Interface\\Icons\\INV_BannerPVP_01.blp", name)
  147.                 end
  148.                 objectives[name] = state
  149.             end
  150.         end
  151.     end
  152.  
  153.     local function schedule_check(self)
  154.         self:Schedule(1, check_for_updates)
  155.     end
  156.  
  157.     mod.CHAT_MSG_BG_SYSTEM_ALLIANCE = schedule_check
  158.     mod.CHAT_MSG_BG_SYSTEM_HORDE = schedule_check
  159.     mod.CHAT_MSG_RAID_BOSS_EMOTE = schedule_check
  160.     mod.CHAT_MSG_BG_SYSTEM_NEUTRAL = schedule_check
  161. end
  162.  
  163. --
  164. -- winTimer
  165. --
  166.  
  167. function mod:UPDATE_WORLD_STATES()
  168.     if not bgzone then return end
  169.  
  170.     local last_alliance_bases, last_horde_bases = get_basecount()
  171.     local last_alliance_score, last_horde_score = get_score()
  172.  
  173.     local AllyTime = (1600 - last_alliance_score) / ResPerSec[last_alliance_bases]
  174.     local HordeTime = (1600 - last_horde_score) / ResPerSec[last_horde_bases]
  175.  
  176.     if AllyTime > 5000 then     AllyTime = 5000 end
  177.     if HordeTime > 5000 then    HordeTime = 5000 end
  178.  
  179.     if AllyTime == HordeTime then
  180.         winner_is = 0
  181.         winTimer:Stop()
  182.         if self.ScoreFrame1Text then
  183.             self.ScoreFrame1Text:SetText("")
  184.             self.ScoreFrame2Text:SetText("")
  185.         end
  186.  
  187.     elseif AllyTime > HordeTime then -- Horde wins
  188.         if self.ScoreFrame1Text and self.ScoreFrame2Text then
  189.             local AllyPoints = math.floor(math.floor(((HordeTime * ResPerSec[last_alliance_bases]) + last_alliance_score) / 10) * 10)
  190.             self.ScoreFrame1Text:SetText("("..AllyPoints..")")
  191.             self.ScoreFrame2Text:SetText("(1600)")
  192.         end
  193.  
  194.         winner_is = 2
  195.         winTimer:Update(get_gametime(), get_gametime()+HordeTime)
  196.         winTimer:DisableEnlarge()
  197.         local title = L.Horde or FACTION_HORDE--L.Horde is nil in english local, unless it's added to non english local, FACTION_HORDE will be used
  198.         winTimer:UpdateName(L.WinBarText:format(title))
  199.         winTimer:SetColor(hordeColor)
  200.         winTimer:UpdateIcon("Interface\\Icons\\INV_BannerPVP_01.blp")
  201.  
  202.     elseif HordeTime > AllyTime then -- Alliance wins
  203.         if self.ScoreFrame1Text and self.ScoreFrame2Text then
  204.             local HordePoints = math.floor(math.floor(((AllyTime * ResPerSec[last_horde_bases]) + last_horde_score) / 10) * 10)
  205.             self.ScoreFrame2Text:SetText("("..HordePoints..")")
  206.             self.ScoreFrame1Text:SetText("(1600)")
  207.         end
  208.  
  209.         winner_is = 1
  210.         winTimer:Update(get_gametime(), get_gametime()+AllyTime)
  211.         winTimer:DisableEnlarge()
  212.         local title = L.Alliance or FACTION_ALLIANCE--L.Alliance is nil in english local, unless it's added to non english local, FACTION_ALLIANCE will be used
  213.         winTimer:UpdateName(L.WinBarText:format(title))
  214.         winTimer:SetColor(allyColor)
  215.         winTimer:UpdateIcon("Interface\\Icons\\INV_BannerPVP_02.blp")
  216.     end
  217. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement