Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function gadget:GetInfo()
- return {
- name = "Announcer v1.6 beta",
- desc = "Announces Kills, Deaths and Battle Metal Profits",
- author = "LEDZ",
- date = "Jun 28, 2011",
- license = "GNU GPL, v2 or later",
- layer = 5,
- enabled = true
- }
- end
- --[[
- See forum post http://springrts.com/phpbb/viewtopic.php?f=23&t=26341&p=493108
- for info on Echoing variable-dependent coloured text
- --]]
- --------------------------------------------------------------------------
- -- Localise functions to increase performance
- --------------------------------------------------------------------------
- local announce = Spring.SendCommands
- local Echo = Spring.Echo
- local GetGameSeconds = Spring.GetGameSeconds
- local spGetSelectedUnits = Spring.GetSelectedUnits ----------!!!!!
- local spGetMyPlayerID = Spring.GetMyPlayerID
- local spGetUnitDefID = Spring.GetUnitDefID
- local spGetPlayerInfo = Spring.GetPlayerInfo
- local spGetMyTeamID = Spring.GetMyTeamID
- local spAreTeamsAllied = Spring.AreTeamsAllied
- local teamColor = Spring.GetTeamColor
- local spGetUnitHealth = Spring.GetUnitHealth
- --[[ ( number unitID ) -> nil | number health, number maxHealth, number paralyzeDamage,
- number captureProgress, number buildProgress
- --]]
- local deathcount = 0
- local killcount = 0
- local metaltotalcount = 0
- local metalkillcount = 0
- local metallosscount = 0
- local catchingUp = false
- --------------------------------------------------------------------------
- if gadgetHandler:IsSyncedCode() then
- --------------------------------------------------------------------------------
- -- SYNCED
- --------------------------------------------------------------------------------
- function gadget:GameStart()
- Echo("\255\255\1\1 LEDZ Battle-Metal and Kill/Death Tracker Initialized")
- -- tell the gadgetHandler to react to SendToUnsynced("syncedEvent") by
- -- calling the HandleEvent function
- local eventArgs = {}
- -- Echo("NumInPlayerList: " .. # Spring.GetPlayerList())
- for i=0,(# Spring.GetPlayerList())-1 do
- name, active, spectator, teamID, allyTeamID, pingTime, cpuUsage,
- country, rank = spGetPlayerInfo(i)
- -- Echo("name: " .. name)
- --[[
- for name, active, spectator, teamID, allyTeamID, pingTime, cpuUsage,
- country, rank in pairs(spGetPlayerInfo(i)) do
- --]]
- -- Echo("PlayerInfo to table")
- eventArgs = {
- type = "playersTable" }
- eventArgs[i+2] = { --CANT HAVE ELEMENT 0
- name = name, --SYNCED_NONAME UNTIL 0.83
- active = active,
- spectator = spectator,
- teamID = teamID,
- allyTeamID = allyTeamID,
- pingTime = pingTime,
- cpuUsage = cpuUsage,
- country = country,
- rank = rank,
- }
- end
- -- Echo("Number of Entries in PlayerInfo Table: "..# eventArgs)
- SendEventToUnsynced(eventArgs)
- end
- --[[ Wait until 0.83
- function gadget:GameFrame(frameNum)
- gameFrame = frameNum
- Echo("gameFrame: "..gameFrame)
- end
- function gadget:GameProgress(serverFrameNum) --will work in 0.83
- gameProgress = serverFrameNum
- Echo("gameProgress: "..gameProgress)
- end
- if gameProgress ~= gameFrame then
- catchingUp = true
- catchUpProgress = math.floor(100*gameFrame/gameProgress)
- if catchUpProgress%10 == 0 then
- announce("say "..string.format("is reloading ("..catchUpProgress.."%)"))
- end
- else
- catchingUp = false
- end
- --]]
- function gadget:UnitDestroyed(victimUnitID, victimUnitDefID, victimUnitTeamID, attackerID, attackerDefID, attackerTeamID)
- -- save the event arguments to a table, which we will send to unsynced
- local eventArgs = {
- type = "unitDestroyed",
- victimUnitID = victimUnitID,
- victimUnitDefID = victimUnitDefID,
- victimUnitTeamID = victimUnitTeamID,
- attackerID = attackerID,
- attackerDefID = attackerDefID,
- attackerTeamID = attackerTeamID,
- }
- SendEventToUnsynced(eventArgs)
- end
- function SendEventToUnsynced(eventArgs)
- -- Echo("Sending SYNCED DATA")
- -- what we put in _G ends up in the SYNCED table, in the unsynced part
- _G.eventArgs = eventArgs
- if _G.eventArgs == eventArgs then
- -- Echo("Global Set")
- end
- SendToUnsynced("syncedEvent")
- -- Echo("Sent to Unsynced")
- -- the unsynced part has already read eventArgs by now, we can clear it
- _G.eventArgs = nil
- end
- else
- --------------------------------------------------------------------------------
- -- UNSYNCED
- --------------------------------------------------------------------------------
- function gadget:Initialize()
- gadgetHandler:AddSyncAction("syncedEvent", HandleEvent)
- end
- now = 0
- lastcheck = 0
- freq = 20
- spec = false
- if spec == false then
- function gadget:Update()
- now = now + 1
- if now >= lastcheck + freq then
- lastcheck = now
- playerID = spGetMyPlayerID()
- _, _, spec, _, _, _, _, _ = spGetPlayerInfo(playerID)
- if (spec == true ) then
- Echo("\255\255\1\1 <Announcer> Spectator Mode: LEDZ Battle-Metal and Kill/Death Announcer Disabled")
- gadgetHandler:RemoveGadget(self)
- return
- end
- end
- end
- end
- --------------------------------------------------------------------------------
- -- Metal Multipliers and constants
- --------------------------------------------------------------------------------
- Dk=1
- DM=10
- Kk=1
- KM=10
- firstkill = true
- --------------------------------------------------------------------------------
- --------------------------------------------------------------------------------
- function HandleEvent()
- -- Echo("Handling Event now")
- local eventArgs = {}
- -- copy the event args in SYNCED
- -- spairs() or sipairs() are needed to iterate SYNCED
- -- pairs and ipairs will not work!
- for k, v in spairs(SYNCED.eventArgs) do
- eventArgs[k] = v
- end
- if eventArgs.type == "unitDestroyed" then
- --------------------------------------------------------------------------------
- --Death Counter & Metal Deficit
- --------------------------------------------------------------------------------
- colorizeNames()
- GetPlayerStuff(spGetMyTeamID())
- VUDID = UnitDefs[eventArgs.victimUnitDefID]
- if ( eventArgs.victimUnitTeamID == spGetMyTeamID() and eventArgs.attackerTeamID ~= nil ) then
- unitHealth, unitMaxHealth, paralyzeProgress, captureProgress, buildProgress = spGetUnitHealth(eventArgs.victimUnitID)
- lostUnitMetalCost = VUDID.metalCost * buildProgress
- metallosscount = metallosscount - lostUnitMetalCost
- deathcount = deathcount + 1
- if ( firstkill == true ) then
- announceToAll( badColorizer .. " wants to start again")
- firstkill = false
- end
- metaltracker(-1)
- if (deathcount == DM) then
- announceToAll( messageColorizer .. " has lost " ..badColorizer .. DM .. messageColorizer .. " of his machines")
- Dk = -Dk
- if ( Dk == -1 ) then
- DM = DM*2.5
- else
- DM = DM*4
- end
- end
- --------------------------------------------------------------------------------
- --Commander Lost
- --------------------------------------------------------------------------------
- if ( VUDID.isCommander == true ) then
- announceToAll( badColorizer .. " has been far too wreckless with their Commander")
- -- Add randomiser for colourful array of comments!
- end
- end -- END OF if ( eventArgs.victimUnitTeamID == spGetMyTeamID() )
- --------------------------------------------------------------------------------
- --Kill Counter & Metal Profit
- --------------------------------------------------------------------------------
- VUDID = UnitDefs[eventArgs.victimUnitDefID]
- victimUnitHealthInfo = spGetUnitHealth(eventArgs.victimUnitID)
- if ( eventArgs.attackerTeamID == spGetMyTeamID() ) and ( eventArgs.victimUnitTeamID ~= spGetMyTeamID() ) and not spAreTeamsAllied(eventArgs.victimUnitTeamID, eventArgs.attackerTeamID) then
- unitHealth, unitMaxHealth, paralyzeProgress, captureProgress, buildProgress = spGetUnitHealth(eventArgs.victimUnitID)
- lostUnitMetalCost = VUDID.metalCost * buildProgress
- metalkillcount = metalkillcount + lostUnitMetalCost
- killcount = killcount + 1
- if ( firstkill == true ) then
- announceToAll( goodColorizer .. " is off to a good start")
- firstkill = false
- end
- metaltracker(1)
- if (killcount == KM) then
- announceToAll( messageColorizer .. " has now sent " .. goodColorizer.. KM .. messageColorizer .. " enemies to metalhell")
- Kk = -Kk
- if ( Kk == -1 ) then
- KM = KM*2.5
- else
- KM = KM*4
- end
- end
- --------------------------------------------------------------------------------
- --Commander Found and Killed
- --------------------------------------------------------------------------------
- if ( VUDID.isCommander == true ) then
- announceToAll( goodColorizer .. " is a Commander Killer")
- -- Add randomiser for colourful array of comments!
- end
- end --END OF if ( eventArgs.attackerTeamID == spGetMyTeamID() ) and ( eventArgs.victimUnitTeamID ~= spGetMyTeamID() ) then
- --------------------------------------------------------------------------------
- --GameStartInfo
- --------------------------------------------------------------------------------
- else
- --[[
- PlayerOne = eventArgs[1] --PlayerID = 0
- PlayerTwo = eventArgs[2]
- Echo("name, active, spectator, teamID, allyTeamID, pingTime, cpuUsage, country, rank")
- Echo(PlayerOne.name, PlayerOne.active, PlayerOne.spectator, PlayerOne.teamID,
- PlayerOne.allyTeamID, PlayerOne.pingTime, PlayerOne.cpuUsage, PlayerOne.country, PlayerOne.rank)
- Echo("name, active, spectator, teamID, allyTeamID, pingTime, cpuUsage, country, rank")
- Echo(PlayerTwo.name, PlayerTwo.active, PlayerTwo.spectator, PlayerTwo.teamID,
- PlayerTwo.allyTeamID, PlayerTwo.pingTime, PlayerTwo.cpuUsage, PlayerTwo.country, PlayerTwo.rank)
- --]]
- end -- END OF type ==
- end -- END OF HandleEvent()
- --------------------------------------------------------------------------------
- --Colorizer
- --------------------------------------------------------------------------------
- function colorizeNames()
- nameColorR,nameColorG,nameColorB,nameColorA = teamColor(spGetMyTeamID())
- 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)
- G255 = math.floor(nameColorG*255)
- B255 = math.floor(nameColorB*255)
- if ( R255%10 == 0) then
- R255 = R255+1
- end
- if( G255%10 == 0) then
- G255 = G255+1
- end
- if ( B255%10 == 0) then
- B255 = B255+1
- end
- nameColorizer="\255"..string.char(R255)..string.char(G255)..string.char(B255) --works thanks to zwzsg
- messageColorizer = "\255\253\253\151"
- goodColorizer = "\255\84\255\159"
- neutColorizer = "\255\121\205\205"
- badColorizer = "\255\255\99\71"
- end
- --------------------------------------------------------------------------------
- --Metal Profit Tracker
- --------------------------------------------------------------------------------
- MMDU = -5000 --Metal Deficit Multiplier Upper
- MMDT = -2500 --Metal Deficit Multiplier Trigger
- MMDL = 0 --Metal Deficit Multiplier Lower
- MMDLL = 0
- MMPU = 5000 --Metal Profit Multiplier Upper
- MMPT = 2500 --Metal Profit Multiplier Trigger
- MMPL = 0 --Metal Profit Multiplier Lower
- MMPLL = 0
- metaltotalcountCACHE = 0
- adjustments = 0
- significantMetal = false -- has the profit/loss gone far from 0
- repeatAnnounce = 0
- function metaltracker(x) --when x=1 means going up, x=-1 means going down, --y=1 profit y=-1 deficit
- metaltotalcount = metalkillcount + metallosscount
- if ( metaltotalcount > 5000 or metaltotalcount < -5000 ) then
- significantMetal = true
- end
- if ( metaltotalcount > 0 ) then
- y = 1
- MMDU = -5000
- MMDT = -2500
- MMDL = 0
- MMDLL = 0
- if ( metaltotalcountCACHE < 0 ) and ( significantMetal == true ) then
- announceToAll( goodColorizer .. " has made some good investment choices and broken even!")
- significantMetal = false
- end
- while ( metaltotalcount > MMPU ) do
- MMPLL = MMPL
- MMPU = MMPU*2
- MMPT = MMPT*2
- MMPL = MMPL*2
- if (MMPL == 0) then
- MMPL = 2500
- end
- adjustments = 0
- end
- elseif ( metaltotalcount < 0 ) then
- y = -1
- MMPU = 5000
- MMPT = 2500
- MMPL = 0
- MMPLL = 0
- if ( metaltotalcountCACHE > 0 ) and ( significantMetal == true ) then
- announceToAll( badColorizer .. " has seen profit turn to loss!")
- significantMetal = false
- end
- while ( metaltotalcount < MMDU ) do
- MMDLL = MMDL
- MMDU = MMDU*2
- MMDT = MMDT*2
- MMDL = MMDL*2
- if (MMDL == 0) then
- MMDL = -2500
- end
- adjustments = 0
- end
- else
- announceToAll( neutColorizer .. " is on the dot (0)!")
- end
- --[[DEBUG
- Echo("metal: "..metaltotalcount)
- Echo("--------------------")
- Echo("MMDU: "..MMDU)
- Echo("MMDT: "..MMDT)
- Echo("MMDL: "..MMDL)
- Echo("MMDLL: "..MMDLL)
- Echo("MMPU: "..MMPU)
- Echo("MMPT: "..MMPT)
- Echo("MMPL: "..MMPL)
- Echo("MMPLL: "..MMPLL)
- Echo("--------------------")
- --]]
- if (x == -1) then
- if ( y == -1 ) then
- if (metaltotalcount <= MMDT) and (metaltotalcount >= MMDU) and (repeatAnnounce ~= MMDT) then
- announceToAll( messageColorizer .. " now has a metal deficit of " .. badColorizer .. MMDT)
- repeatAnnounce = MMDT
- adjustments = 0
- end
- elseif ( y == 1 ) then
- while (metaltotalcount <= MMPL ) and (MMPL ~= 0) do
- adjustments = adjustments + 1
- MMPU = MMPU/2
- MMPT = MMPT/2
- MMPL = MMPL/2
- if (MMPL == 2500) then
- MMPL = 0
- end
- MMPLL = MMPL
- end
- if (adjustments >= 2) then
- announceToAll( badColorizer .. " has made some disastrous decisions")
- adjustments = 0
- end
- end
- elseif (x == 1) then
- if ( y == 1 ) then
- if (metaltotalcount >= MMPT) and (metaltotalcount <= MMPU) and (repeatAnnounce ~= MMPT) then
- announceToAll( messageColorizer .. " now has a metal profit of " .. goodColorizer .. MMPT)
- repeatAnnounce = MMPT
- adjustments = 0
- end
- elseif ( y == -1 ) then
- while (metaltotalcount >= MMDL) and (MMDL ~= 0) do
- adjustments = adjustments + 1
- MMDU = MMDU/2
- MMDT = MMDT/2
- MMDL = MMDL/2
- if (MMDL == -2500) then
- MMDL = 0
- end
- MMDLL = MMDL
- end
- if (adjustments >= 2) then
- announceToAll( goodColorizer .. " has made the most of what they have")
- adjustments = 0
- end
- end
- else
- announceToAll("x error!: x= "..x)
- end --end of if x==-1 check
- metaltotalcountCACHE = metaltotalcount
- end --end of metaltracker()
- --------------------------------------------------------------------------------
- --------------------------------------------------------------------------------
- --[[
- function metalmultiplier(MMPTorMMDT, UPorDOWN)
- Echo("MMPTorMMDT: "..MMPTorMMDT)
- Echo("UPorDOWN: "..UPorDOWN)
- if ( UPorDOWN == "UP" ) then
- Echo("times 2")
- MMPTorMMDT = MMPTorMMDT*2
- Echo("MMPTorMMDT: "..MMPTorMMDT)
- if (MMPTorMMDT == MMDL) and (MMDL == 0) then
- Echo("MMDL is zero")
- MMDL = -500
- end
- if (MMPTorMMDT == MMPL) and (MMPL == 0) then
- MMPL = 500
- end
- elseif (UPorDOWN == "DOWN" ) then
- if (MMPTorMMDT == MMDL) and (MMDL == -500) then
- MMDL = 0
- end
- if (MMPTorMMDT == MMPL) and (MMPL == 500) then
- MMPL = 0
- end
- MMPTorMMDT = MMPTorMMDT/2
- else
- Echo("Metalmultiplier recieved no UPorDOWN variable")
- end
- end
- --]]
- --------------------------------------------------------------------------------
- --------------------------------------------------------------------------------
- function GetPlayerStuff(i) --set i to be playerID or something
- for NUM,info in ipairs(Spring.GetPlayerRoster(2)) do
- -- info = Spring.GetPlayerRoster(2)[i] -- 2 = Sort by teamID
- --[[
- Sort codes for Roster:
- 1 = Sort by allies (the default)
- 2 = Sort by teamID
- 3 = Sort by playerName
- 4 = Sort by CPU usage
- 5 = Sort by ping time
- --]]
- -- Echo("GetPlayerStuff"..NUM.." "..type(info))
- if ( info[3] == spGetMyTeamID()) then
- myPlayerName = info[1] ------info is the table here
- myPlayerID = info[2]
- myTeamID = info[3]
- end
- end
- end
- function announceToAll(messagebody)
- if catchingUp or spec then
- return
- else
- announce("say "..string.format(messagebody))
- end
- --[[ numPlayers = 10
- for k=0,numPlayers-1 do
- Echo("Doing Player: "..k)
- announce("say "..string.format(messagebody))
- end
- --]]
- end
- --------------------------------------------------------------------------------
- -- END
- --------------------------------------------------------------------------------
- end --of gadgetIsSynced callin
Advertisement
Add Comment
Please, Sign In to add comment