Advertisement
Guest User

game_messages.lua

a guest
Nov 13th, 2013
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.32 KB | None | 0 0
  1. function widget:GetInfo()
  2.   return {
  3.     name      = "TeamDiedMessages",
  4.     desc      = "Prints a message when a team die",
  5.     author    = "abma & zwzsg",
  6.     date      = "Sep, 2013",
  7.     license   = "GNU GPL, v2 or later",
  8.     layer     = 5,
  9.     enabled   = true
  10.   }
  11. end
  12.  
  13. local sec = "game_messages.lua" -- log section
  14. local msgsfile = "gamedata/messages.tdf"
  15.  
  16. local luaMsgs = {}
  17.  
  18. function widget:Initialize()
  19.     local TDF = VFS.Include('gamedata/parse_tdf.lua')
  20.     if (not VFS.FileExists(msgsfile)) then
  21.         Spring.Log(sec, LOG.INFO, "gamedata/messages.tdf doesn't exist.")
  22.         return
  23.     end
  24.  
  25.     local tdfMsgs, err = TDF.Parse(msgsfile)
  26.     if (tdfMsgs == nil) then
  27.         Spring.Log(sec, LOG.ERROR, 'Error parsing '.. msgsfile  .. err)
  28.     end
  29.     if (type(tdfMsgs.messages) ~= 'table') then
  30.         Spring.Log(sec, LOG.ERROR, 'Missing "messages" section in ' .. msgsfile )
  31.     end
  32.  
  33.     for label, msgs in pairs(tdfMsgs.messages) do
  34.         if ((type(label) == 'string') and (type(msgs)  == 'table')) then
  35.             local msgArray = {}
  36.             for _, msg in pairs(msgs) do
  37.                 if (type(msg) == 'string') then
  38.                     table.insert(msgArray, msg)
  39.                 end
  40.             end
  41.             luaMsgs[label:lower()] = msgArray -- lower case keys
  42.         end
  43.     end
  44. end
  45.  
  46. local function Translate(msg)
  47.     msg = msg:lower()
  48.     if luaMsgs and type(luaMsgs[msg]) == "table" then
  49.         local msgs = luaMsgs[msg]
  50.         return msgs[ math.random( #msgs ) ]
  51.     end
  52.     Spring.Log(sec, LOG.DEBUG, string.format("Missing translation for %s", msg))
  53.     return msg
  54. end
  55.  
  56. function widget:TeamDied(teamID)
  57.     local playerNames
  58.     local _, leaderPlayerId, isDead, isAiTeam = Spring.GetTeamInfo(teamID)
  59.     if isAiTeam then
  60.         local skirmishAIID, aiName, hostingPlayerID, shortName, version, options = Spring.GetAIInfo(teamID)
  61.         if aiName~="UNKNOWN" and aiName:lower():sub(1,3)~="bot" and aiName:len()>1 then
  62.             playerNames=aiName
  63.         elseif shortName~="UNKNOWN" then
  64.             playerNames=shortName
  65.         elseif hostingPlayerID~=Spring.GetMyPlayerID() then
  66.             local ownerName=Spring.GetPlayerInfo(hostingPlayerID)
  67.             if ownerName then
  68.                 playerNames=ownerName.."'s bot"
  69.             end
  70.         end
  71.     end
  72.     for _,playerId in ipairs(Spring.GetPlayerList(teamID,true)) do
  73.         playerNames=(playerNames and playerNames..", " or "")..Spring.GetPlayerInfo(playerId)
  74.     end
  75.     msg = string.format(Translate("Team%i(%s) is no more"), teamID, playerNames or "")
  76.     Spring.Echo(msg)
  77. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement