Guest User

Announcer.lua

a guest
Oct 8th, 2011
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.60 KB | None | 0 0
  1. function gadget:GetInfo()
  2.   return {
  3.     name = "Announcer v1.6 beta",
  4.     desc = "Announces Kills, Deaths and Battle Metal Profits",
  5.     author = "LEDZ",
  6.     date = "Jun 28, 2011",
  7.     license = "GNU GPL, v2 or later",
  8.     layer = 5,
  9.     enabled = true
  10.   }
  11. end
  12. --[[
  13. See forum post http://springrts.com/phpbb/viewtopic.php?f=23&t=26341&p=493108
  14. for info on Echoing variable-dependent coloured text
  15. --]]
  16. --------------------------------------------------------------------------
  17. -- Localise functions to increase performance
  18. --------------------------------------------------------------------------
  19. local announce                      = Spring.SendCommands
  20. local Echo                      = Spring.Echo
  21. local GetGameSeconds                    = Spring.GetGameSeconds
  22. local spGetSelectedUnits                = Spring.GetSelectedUnits ----------!!!!!
  23. local spGetMyPlayerID                   = Spring.GetMyPlayerID
  24. local spGetUnitDefID                    = Spring.GetUnitDefID
  25. local spGetPlayerInfo                   = Spring.GetPlayerInfo
  26. local spGetMyTeamID                 = Spring.GetMyTeamID
  27. local spAreTeamsAllied                  = Spring.AreTeamsAllied
  28. local teamColor                     = Spring.GetTeamColor
  29. local spGetUnitHealth                   = Spring.GetUnitHealth
  30. --[[ ( number unitID ) -> nil | number health, number maxHealth, number paralyzeDamage,
  31.                             number captureProgress, number buildProgress
  32. --]]
  33. local deathcount = 0
  34. local killcount = 0
  35. local metaltotalcount = 0
  36. local metalkillcount = 0
  37. local metallosscount = 0
  38. local catchingUp = false
  39.  
  40.  
  41. --------------------------------------------------------------------------
  42. if gadgetHandler:IsSyncedCode() then
  43. --------------------------------------------------------------------------------
  44. -- SYNCED
  45. --------------------------------------------------------------------------------
  46.   function gadget:GameStart()
  47.     Echo("\255\255\1\1 LEDZ Battle-Metal and Kill/Death Tracker Initialized")
  48.     -- tell the gadgetHandler to react to SendToUnsynced("syncedEvent") by
  49.     -- calling the HandleEvent function
  50.     local eventArgs = {}
  51.   --    Echo("NumInPlayerList: " .. # Spring.GetPlayerList())
  52.     for i=0,(# Spring.GetPlayerList())-1 do
  53.       name, active, spectator, teamID, allyTeamID, pingTime, cpuUsage,
  54.   country, rank = spGetPlayerInfo(i)
  55.   --        Echo("name: " .. name)
  56.     --[[
  57.         for name, active, spectator, teamID, allyTeamID, pingTime, cpuUsage,
  58.      country, rank in pairs(spGetPlayerInfo(i)) do
  59.     --]]
  60.   --        Echo("PlayerInfo to table")
  61.     eventArgs = {
  62.     type = "playersTable" }
  63.     eventArgs[i+2] = {      --CANT HAVE ELEMENT 0
  64.       name = name,      --SYNCED_NONAME UNTIL 0.83
  65.       active = active,
  66.       spectator = spectator,
  67.       teamID = teamID,
  68.       allyTeamID = allyTeamID,
  69.       pingTime = pingTime,
  70.       cpuUsage = cpuUsage,
  71.       country = country,
  72.       rank = rank,
  73.       }
  74.     end
  75.   --    Echo("Number of Entries in PlayerInfo Table: "..# eventArgs)       
  76.     SendEventToUnsynced(eventArgs)
  77.   end
  78. --[[ Wait until 0.83
  79.   function gadget:GameFrame(frameNum)
  80.     gameFrame = frameNum
  81.     Echo("gameFrame: "..gameFrame)
  82.   end
  83.   function gadget:GameProgress(serverFrameNum) --will work in 0.83
  84.     gameProgress = serverFrameNum
  85.     Echo("gameProgress: "..gameProgress)
  86.   end
  87.  
  88.   if gameProgress ~= gameFrame then
  89.     catchingUp = true
  90.     catchUpProgress = math.floor(100*gameFrame/gameProgress)
  91.     if catchUpProgress%10 == 0 then
  92.       announce("say "..string.format("is reloading ("..catchUpProgress.."%)"))
  93.     end
  94.   else
  95.     catchingUp = false
  96.   end
  97. --]]
  98.  
  99.   function gadget:UnitDestroyed(victimUnitID, victimUnitDefID, victimUnitTeamID, attackerID, attackerDefID, attackerTeamID)
  100.     -- save the event arguments to a table, which we will send to unsynced
  101.     local eventArgs = {
  102.       type = "unitDestroyed",
  103.       victimUnitID = victimUnitID,
  104.       victimUnitDefID = victimUnitDefID,
  105.       victimUnitTeamID = victimUnitTeamID,
  106.       attackerID = attackerID,
  107.       attackerDefID = attackerDefID,
  108.       attackerTeamID = attackerTeamID,
  109.  
  110.     }
  111.     SendEventToUnsynced(eventArgs)
  112.    
  113.  
  114.   end
  115.  
  116.   function SendEventToUnsynced(eventArgs)
  117.   --    Echo("Sending SYNCED DATA")
  118.     -- what we put in _G ends up in the SYNCED table, in the unsynced part
  119.     _G.eventArgs = eventArgs
  120.     if _G.eventArgs == eventArgs then
  121.   --    Echo("Global Set")
  122.     end
  123.     SendToUnsynced("syncedEvent")
  124.   --  Echo("Sent to Unsynced")
  125.     -- the unsynced part has already read eventArgs by now, we can clear it
  126.     _G.eventArgs = nil
  127.   end
  128.  
  129. else
  130. --------------------------------------------------------------------------------
  131. -- UNSYNCED
  132. --------------------------------------------------------------------------------
  133.  
  134.   function gadget:Initialize()
  135.     gadgetHandler:AddSyncAction("syncedEvent", HandleEvent)
  136.   end
  137.   now = 0
  138.   lastcheck = 0
  139.   freq = 20
  140.   spec = false
  141.   if spec == false then
  142.     function gadget:Update()
  143.       now = now + 1
  144.       if now >= lastcheck + freq then
  145.         lastcheck = now
  146.         playerID = spGetMyPlayerID()
  147.         _, _, spec, _, _, _, _, _ = spGetPlayerInfo(playerID)
  148.         if (spec == true ) then
  149.           Echo("\255\255\1\1 <Announcer> Spectator Mode: LEDZ Battle-Metal and Kill/Death Announcer Disabled")
  150.           gadgetHandler:RemoveGadget(self)
  151.           return
  152.         end
  153.       end
  154.     end
  155.   end
  156.  
  157. --------------------------------------------------------------------------------
  158. -- Metal Multipliers and constants
  159. --------------------------------------------------------------------------------
  160. Dk=1
  161. DM=10
  162. Kk=1
  163. KM=10
  164. firstkill = true
  165. --------------------------------------------------------------------------------
  166. --------------------------------------------------------------------------------
  167.  
  168. function HandleEvent()
  169. --  Echo("Handling Event now")
  170.   local eventArgs = {}
  171.  
  172.   -- copy the event args in SYNCED
  173.   -- spairs() or sipairs() are needed to iterate SYNCED
  174.   -- pairs and ipairs will not work!
  175.   for k, v in spairs(SYNCED.eventArgs) do
  176.     eventArgs[k] = v
  177.   end
  178.   if eventArgs.type == "unitDestroyed" then
  179.  
  180. --------------------------------------------------------------------------------
  181. --Death Counter & Metal Deficit
  182. --------------------------------------------------------------------------------
  183.         colorizeNames()
  184.         GetPlayerStuff(spGetMyTeamID())
  185.         VUDID = UnitDefs[eventArgs.victimUnitDefID]
  186.  
  187.         if ( eventArgs.victimUnitTeamID == spGetMyTeamID() and eventArgs.attackerTeamID ~= nil ) then
  188.             unitHealth, unitMaxHealth, paralyzeProgress, captureProgress, buildProgress = spGetUnitHealth(eventArgs.victimUnitID)
  189.             lostUnitMetalCost = VUDID.metalCost * buildProgress
  190.             metallosscount = metallosscount - lostUnitMetalCost
  191.             deathcount = deathcount + 1
  192.             if ( firstkill == true ) then
  193.               announceToAll( badColorizer .. " wants to start again")
  194.               firstkill = false
  195.             end
  196.             metaltracker(-1)
  197.             if (deathcount == DM) then
  198.                 announceToAll( messageColorizer .. " has lost " ..badColorizer .. DM .. messageColorizer .. " of his machines")
  199.                 Dk = -Dk
  200.                 if ( Dk == -1 ) then
  201.                     DM = DM*2.5
  202.                 else
  203.                     DM = DM*4
  204.                 end
  205.             end
  206.  
  207. --------------------------------------------------------------------------------
  208. --Commander Lost
  209. --------------------------------------------------------------------------------
  210.  
  211.             if ( VUDID.isCommander == true ) then
  212.                 announceToAll( badColorizer .. " has been far too wreckless with their Commander")
  213.                 -- Add randomiser for colourful array of comments!
  214.             end
  215.  
  216.         end --  END OF if ( eventArgs.victimUnitTeamID == spGetMyTeamID() )
  217.  
  218. --------------------------------------------------------------------------------
  219. --Kill Counter & Metal Profit
  220. --------------------------------------------------------------------------------
  221.         VUDID = UnitDefs[eventArgs.victimUnitDefID]
  222.         victimUnitHealthInfo = spGetUnitHealth(eventArgs.victimUnitID)
  223.         if ( eventArgs.attackerTeamID == spGetMyTeamID() ) and ( eventArgs.victimUnitTeamID ~= spGetMyTeamID() ) and not spAreTeamsAllied(eventArgs.victimUnitTeamID, eventArgs.attackerTeamID) then
  224.             unitHealth, unitMaxHealth, paralyzeProgress, captureProgress, buildProgress = spGetUnitHealth(eventArgs.victimUnitID)
  225.             lostUnitMetalCost = VUDID.metalCost * buildProgress
  226.             metalkillcount = metalkillcount + lostUnitMetalCost
  227.             killcount = killcount + 1
  228.             if ( firstkill == true ) then
  229.             announceToAll( goodColorizer .. " is off to a good start")
  230.             firstkill = false
  231.           end
  232.           metaltracker(1)
  233.             if (killcount == KM) then
  234.                 announceToAll( messageColorizer .. " has now sent " .. goodColorizer.. KM .. messageColorizer .. " enemies to metalhell")
  235.                 Kk = -Kk
  236.                 if ( Kk == -1 ) then
  237.                     KM = KM*2.5
  238.                 else
  239.                     KM = KM*4
  240.                 end
  241.             end
  242. --------------------------------------------------------------------------------
  243. --Commander Found and Killed
  244. --------------------------------------------------------------------------------
  245.             if ( VUDID.isCommander == true ) then
  246.                 announceToAll( goodColorizer .. " is a Commander Killer")
  247.                 -- Add randomiser for colourful array of comments!
  248.             end
  249.  
  250.         end --END OF if ( eventArgs.attackerTeamID == spGetMyTeamID() ) and ( eventArgs.victimUnitTeamID ~= spGetMyTeamID() ) then
  251. --------------------------------------------------------------------------------
  252. --GameStartInfo
  253. --------------------------------------------------------------------------------
  254.   else
  255.  
  256. --[[
  257.     PlayerOne = eventArgs[1]  --PlayerID = 0
  258.     PlayerTwo = eventArgs[2]
  259.     Echo("name, active, spectator, teamID, allyTeamID, pingTime, cpuUsage, country, rank")
  260.         Echo(PlayerOne.name, PlayerOne.active, PlayerOne.spectator, PlayerOne.teamID,
  261. PlayerOne.allyTeamID, PlayerOne.pingTime, PlayerOne.cpuUsage, PlayerOne.country, PlayerOne.rank)
  262.         Echo("name, active, spectator, teamID, allyTeamID, pingTime, cpuUsage, country, rank")
  263.         Echo(PlayerTwo.name, PlayerTwo.active, PlayerTwo.spectator, PlayerTwo.teamID,
  264. PlayerTwo.allyTeamID, PlayerTwo.pingTime, PlayerTwo.cpuUsage, PlayerTwo.country, PlayerTwo.rank)
  265. --]]
  266.   end -- END OF type ==
  267.  
  268. end -- END OF HandleEvent()
  269.  
  270. --------------------------------------------------------------------------------
  271. --Colorizer
  272. --------------------------------------------------------------------------------
  273. function colorizeNames()
  274.     nameColorR,nameColorG,nameColorB,nameColorA = teamColor(spGetMyTeamID())
  275.     R255 = math.floor(nameColorR*255)  --the first \255 is just a tag(not color setting) no part can end with a zero due to engine limitation (C)
  276.     G255 = math.floor(nameColorG*255)
  277.     B255 = math.floor(nameColorB*255)
  278.     if ( R255%10 == 0) then
  279.         R255 = R255+1
  280.     end
  281.     if( G255%10 == 0) then
  282.         G255 = G255+1
  283.     end
  284.     if ( B255%10 == 0) then
  285.         B255 = B255+1
  286.     end
  287.  
  288.   nameColorizer="\255"..string.char(R255)..string.char(G255)..string.char(B255) --works thanks to zwzsg
  289.   messageColorizer = "\255\253\253\151"
  290.   goodColorizer = "\255\84\255\159"
  291.   neutColorizer = "\255\121\205\205"
  292.   badColorizer = "\255\255\99\71"
  293.  
  294.  
  295. end
  296.  
  297. --------------------------------------------------------------------------------
  298. --Metal Profit Tracker
  299. --------------------------------------------------------------------------------
  300. MMDU = -5000 --Metal Deficit Multiplier Upper
  301. MMDT = -2500 --Metal Deficit Multiplier Trigger
  302. MMDL = 0 --Metal Deficit Multiplier Lower
  303. MMDLL = 0
  304. MMPU = 5000 --Metal Profit Multiplier Upper
  305. MMPT = 2500 --Metal Profit Multiplier Trigger
  306. MMPL = 0 --Metal Profit Multiplier Lower
  307. MMPLL = 0
  308. metaltotalcountCACHE = 0
  309. adjustments = 0
  310. significantMetal = false -- has the profit/loss gone far from 0
  311. repeatAnnounce = 0
  312.  
  313.  
  314. function metaltracker(x)  --when x=1 means going up, x=-1 means going down, --y=1 profit y=-1 deficit
  315.     metaltotalcount = metalkillcount + metallosscount
  316.     if ( metaltotalcount > 5000 or metaltotalcount < -5000 ) then
  317.       significantMetal = true
  318.     end
  319.     if ( metaltotalcount > 0 ) then
  320.         y = 1
  321.     MMDU = -5000
  322.     MMDT = -2500
  323.     MMDL = 0
  324.     MMDLL = 0
  325.     if ( metaltotalcountCACHE < 0 ) and ( significantMetal == true ) then
  326.       announceToAll( goodColorizer .. " has made some good investment choices and broken even!")
  327.       significantMetal = false
  328.     end
  329.         while ( metaltotalcount > MMPU ) do
  330.           MMPLL = MMPL
  331.       MMPU = MMPU*2
  332.       MMPT = MMPT*2
  333.       MMPL = MMPL*2
  334.       if (MMPL == 0) then
  335.         MMPL = 2500
  336.       end
  337.       adjustments = 0  
  338.     end
  339.    
  340.     elseif ( metaltotalcount < 0 ) then
  341.         y = -1
  342.         MMPU = 5000
  343.     MMPT = 2500
  344.     MMPL = 0
  345.     MMPLL = 0
  346.     if ( metaltotalcountCACHE > 0 ) and ( significantMetal == true ) then
  347.             announceToAll( badColorizer .. " has seen profit turn to loss!")
  348.             significantMetal = false
  349.         end
  350.         while ( metaltotalcount < MMDU ) do
  351.           MMDLL = MMDL
  352.       MMDU = MMDU*2
  353.       MMDT = MMDT*2
  354.       MMDL = MMDL*2
  355.       if (MMDL == 0) then
  356.         MMDL = -2500
  357.       end
  358.       adjustments = 0  
  359.     end
  360.     else
  361.         announceToAll( neutColorizer .. " is on the dot (0)!")
  362.     end
  363. --[[DEBUG
  364.     Echo("metal: "..metaltotalcount)
  365.     Echo("--------------------")
  366.     Echo("MMDU: "..MMDU)
  367.     Echo("MMDT: "..MMDT)
  368.     Echo("MMDL: "..MMDL)
  369.     Echo("MMDLL: "..MMDLL)
  370.     Echo("MMPU: "..MMPU)
  371.     Echo("MMPT: "..MMPT)
  372.     Echo("MMPL: "..MMPL)
  373.     Echo("MMPLL: "..MMPLL)
  374.     Echo("--------------------")
  375. --]]
  376.  
  377.     if (x == -1) then
  378.  
  379.         if ( y == -1 ) then
  380.             if (metaltotalcount <= MMDT) and (metaltotalcount >= MMDU) and (repeatAnnounce ~= MMDT) then
  381.                 announceToAll( messageColorizer .. " now has a metal deficit of " .. badColorizer .. MMDT)
  382.                 repeatAnnounce = MMDT
  383.                 adjustments = 0    
  384.             end
  385.  
  386.         elseif ( y == 1 ) then
  387.  
  388.             while (metaltotalcount <= MMPL ) and (MMPL ~= 0) do
  389.                 adjustments = adjustments + 1
  390.                 MMPU = MMPU/2
  391.                 MMPT = MMPT/2
  392.                 MMPL = MMPL/2
  393.                 if (MMPL == 2500) then
  394.                     MMPL = 0
  395.                 end
  396.                 MMPLL = MMPL
  397.  
  398.             end
  399.             if (adjustments >= 2) then
  400.                 announceToAll( badColorizer .. " has made some disastrous decisions")
  401.                 adjustments = 0
  402.             end
  403.         end
  404.  
  405.     elseif (x == 1) then
  406.  
  407.         if ( y == 1 ) then
  408.  
  409.             if (metaltotalcount >= MMPT) and (metaltotalcount <= MMPU) and (repeatAnnounce ~= MMPT) then
  410.                 announceToAll( messageColorizer .. " now has a metal profit of " .. goodColorizer .. MMPT)
  411.                 repeatAnnounce = MMPT
  412.                 adjustments = 0
  413.             end
  414.  
  415.         elseif ( y == -1 ) then
  416.  
  417.             while (metaltotalcount >= MMDL) and (MMDL ~= 0) do
  418.                 adjustments = adjustments + 1
  419.                 MMDU = MMDU/2
  420.                 MMDT = MMDT/2
  421.                 MMDL = MMDL/2
  422.                 if (MMDL == -2500) then
  423.                     MMDL = 0
  424.                 end
  425.                 MMDLL = MMDL
  426.             end
  427.             if (adjustments >= 2) then
  428.                 announceToAll( goodColorizer .. " has made the most of what they have")
  429.                 adjustments = 0
  430.             end
  431.         end
  432.            
  433.     else
  434.         announceToAll("x error!: x= "..x)
  435.     end --end of if x==-1 check
  436.     metaltotalcountCACHE = metaltotalcount
  437. end --end of metaltracker()
  438.  
  439. --------------------------------------------------------------------------------
  440. --------------------------------------------------------------------------------
  441. --[[
  442. function metalmultiplier(MMPTorMMDT, UPorDOWN)
  443.     Echo("MMPTorMMDT: "..MMPTorMMDT)
  444.     Echo("UPorDOWN: "..UPorDOWN)
  445.     if ( UPorDOWN == "UP" ) then
  446.         Echo("times 2")
  447.         MMPTorMMDT = MMPTorMMDT*2
  448.         Echo("MMPTorMMDT: "..MMPTorMMDT)
  449.         if (MMPTorMMDT == MMDL) and (MMDL == 0) then
  450.             Echo("MMDL is zero")
  451.             MMDL = -500
  452.         end
  453.         if (MMPTorMMDT == MMPL) and (MMPL == 0) then
  454.             MMPL = 500
  455.         end
  456.     elseif (UPorDOWN == "DOWN" ) then
  457.         if (MMPTorMMDT == MMDL) and (MMDL == -500) then
  458.             MMDL = 0
  459.         end
  460.         if (MMPTorMMDT == MMPL) and (MMPL == 500) then
  461.             MMPL = 0
  462.         end
  463.         MMPTorMMDT = MMPTorMMDT/2
  464.     else
  465.         Echo("Metalmultiplier recieved no UPorDOWN variable")
  466.     end
  467. end
  468. --]]
  469.    
  470. --------------------------------------------------------------------------------
  471. --------------------------------------------------------------------------------
  472.  
  473. function GetPlayerStuff(i) --set i to be playerID or something
  474.     for NUM,info in ipairs(Spring.GetPlayerRoster(2)) do
  475. --    info = Spring.GetPlayerRoster(2)[i] --    2 = Sort by teamID
  476. --[[
  477.    Sort codes for Roster:
  478.     1 = Sort by allies  (the default)
  479.     2 = Sort by teamID
  480.     3 = Sort by playerName
  481.     4 = Sort by CPU usage
  482.     5 = Sort by ping time
  483. --]]
  484. --      Echo("GetPlayerStuff"..NUM.." "..type(info))
  485.         if ( info[3] == spGetMyTeamID()) then
  486.             myPlayerName = info[1]      ------info is the table here
  487.             myPlayerID = info[2]
  488.             myTeamID = info[3]
  489.         end
  490.     end
  491. end
  492.  
  493. function announceToAll(messagebody)
  494.   if catchingUp or spec then
  495.     return
  496.   else
  497.     announce("say "..string.format(messagebody))
  498.   end
  499. --[[  numPlayers = 10
  500.   for k=0,numPlayers-1 do
  501.     Echo("Doing Player: "..k)
  502.     announce("say "..string.format(messagebody))
  503.   end
  504. --]]
  505. end
  506.  
  507. --------------------------------------------------------------------------------
  508. -- END
  509. --------------------------------------------------------------------------------
  510. end --of gadgetIsSynced callin
  511.  
Advertisement
Add Comment
Please, Sign In to add comment