Make use of GetEngineString in Lua scripts
authorWuzzy <Wuzzy@disroot.org>
Wed, 28 Jun 2023 23:41:38 +0200
changeset 15970 a803428704fd
parent 15969 9128bb16aaf5
child 15971 cee831693af1
Make use of GetEngineString in Lua scripts
share/hedgewars/Data/Scripts/Multiplayer/Battalion.lua
share/hedgewars/Data/Scripts/Multiplayer/Mutant.lua
share/hedgewars/Data/Scripts/Multiplayer/Racer.lua
share/hedgewars/Data/Scripts/Multiplayer/Space_Invasion.lua
share/hedgewars/Data/Scripts/Multiplayer/TechRacer.lua
share/hedgewars/Data/Scripts/Utils.lua
--- a/share/hedgewars/Data/Scripts/Multiplayer/Battalion.lua	Wed Jun 28 21:16:14 2023 +0200
+++ b/share/hedgewars/Data/Scripts/Multiplayer/Battalion.lua	Wed Jun 28 23:41:38 2023 +0200
@@ -804,7 +804,7 @@
   PlaySound(sndShotgunReload)
 
   if GetRandom(100) < emptyCrateChance then
-    AddCaption(loc("It's empty!"), msgColor, capgrpMessage)
+    AddCaption(GetEngineString("TMsgStrId", sidEmptyCrate), msgColor, capgrpMessage)
     return
   elseif GetRandom(100) < bonusCrateChance then
     factor = 3
@@ -834,7 +834,7 @@
 
   if GetRandom(100) < emptyCrateChance then
     if IsHogLocal(CurHog) then
-      AddCaption(loc("It's empty!"), msgColor, capgrpMessage)
+      AddCaption(GetEngineString("TMsgStrId", sidEmptyCrate), msgColor, capgrpMessage)
     end
     return
   elseif GetRandom(100) < bonusCrateChance then
@@ -880,7 +880,7 @@
 
   if GetRandom(100) < emptyCrateChance then
     if IsHogLocal(CurHog) then
-      AddCaption(loc("It's empty!"), msgColor, capgrpMessage)
+      AddCaption(GetEngineString("TMsgStrId", sidEmptyCrate), msgColor, capgrpMessage)
     end
     return
   elseif GetRandom(100) < bonusCrateChance then
--- a/share/hedgewars/Data/Scripts/Multiplayer/Mutant.lua	Wed Jun 28 21:16:14 2023 +0200
+++ b/share/hedgewars/Data/Scripts/Multiplayer/Mutant.lua	Wed Jun 28 23:41:38 2023 +0200
@@ -21,6 +21,7 @@
 HedgewarsScriptLoad("/Scripts/Locale.lua")
 HedgewarsScriptLoad("/Scripts/Tracker.lua")
 HedgewarsScriptLoad("/Scripts/Params.lua")
+HedgewarsScriptLoad("/Scripts/Utils.lua")
 
 --[[
     MUTANT SCRIPT
@@ -792,8 +793,9 @@
         if not gameOver then
             local winner = createEndGameStats()
             if winner then
-                SendStat(siGameResult, string.format(loc("%s wins!"), winner))
-                AddCaption(string.format(loc("%s wins!"), winner), capcolDefault, capgrpGameState)
+                local winText = formatEngineString(GetEngineString("TMsgStrId", sidWinner), winner)
+                SendStat(siGameResult, winText)
+                AddCaption(winText, capcolDefault, capgrpGameState)
             end
             gameOver = true
         end
--- a/share/hedgewars/Data/Scripts/Multiplayer/Racer.lua	Wed Jun 28 21:16:14 2023 +0200
+++ b/share/hedgewars/Data/Scripts/Multiplayer/Racer.lua	Wed Jun 28 23:41:38 2023 +0200
@@ -32,6 +32,7 @@
 HedgewarsScriptLoad("/Scripts/Locale.lua")
 HedgewarsScriptLoad("/Scripts/OfficialChallenges.lua")
 HedgewarsScriptLoad("/Scripts/Params.lua")
+HedgewarsScriptLoad("/Scripts/Utils.lua")
 
 ------------------
 -- Got Variables?
@@ -508,17 +509,17 @@
 		local roundDraw = false
 		if #clanScores >= 2 and clanScores[1].score == clanScores[2].score and clanScores[1].score ~= MAX_TURN_TIME then
 			roundDraw = true
-                        SendStat(siGameResult, loc("Round draw"))
+                        SendStat(siGameResult, GetEngineString("TMsgStrId", sidDraw))
                         SendStat(siCustomAchievement, loc("The teams are tied for the fastest time."))
                 elseif #sortedTeams >= 1 then
-                        SendStat(siGameResult, string.format(loc("%s wins!"), sortedTeams[1].name))
+                        SendStat(siGameResult, formatEngineString(GetEngineString("TMsgStrId", sidWinner), sortedTeams[1].name))
                         SendStat(siCustomAchievement, string.format(loc("%s wins with a best time of %.1fs."), sortedTeams[1].name, (sortedTeams[1].score/1000)))
                         for i=1,#unfinishedArray do
                                  SendStat(siCustomAchievement, unfinishedArray[i])
                         end
                 else
 			roundDraw = true
-                        SendStat(siGameResult, loc("Round draw"))
+                        SendStat(siGameResult, GetEngineString("TMsgStrId", sidDraw))
                         SendStat(siCustomAchievement, loc("Nobody managed to finish the race. What a shame!"))
                         if specialPointsCount > 0 then
                                 SendStat(siCustomAchievement, loc("Maybe you should try an easier map next time."))
--- a/share/hedgewars/Data/Scripts/Multiplayer/Space_Invasion.lua	Wed Jun 28 21:16:14 2023 +0200
+++ b/share/hedgewars/Data/Scripts/Multiplayer/Space_Invasion.lua	Wed Jun 28 23:41:38 2023 +0200
@@ -2,6 +2,7 @@
 HedgewarsScriptLoad("/Scripts/Locale.lua")
 HedgewarsScriptLoad("/Scripts/Tracker.lua")
 HedgewarsScriptLoad("/Scripts/Params.lua")
+HedgewarsScriptLoad("/Scripts/Utils.lua")
 
 --[[
 Space Invasion
@@ -584,8 +585,10 @@
 
 	if lGameOver then
 		local winnerTeam = teamStats[1].name
-		AddCaption(string.format(loc("%s wins!"), winnerTeam), capcolDefault, capgrpGameState)
-		SendStat(siGameResult, string.format(loc("%s wins!"), winnerTeam))
+		local winText = formatEngineString(GetEngineString("TMsgStrId", sidWinner), winnerTeam)
+
+		AddCaption(winText, capcolDefault, capgrpGameState)
+		SendStat(siGameResult, winText)
 
 		for i = 1, TeamsCount do
 			SendStat(siPointType, "!POINTS")
--- a/share/hedgewars/Data/Scripts/Multiplayer/TechRacer.lua	Wed Jun 28 21:16:14 2023 +0200
+++ b/share/hedgewars/Data/Scripts/Multiplayer/TechRacer.lua	Wed Jun 28 23:41:38 2023 +0200
@@ -470,17 +470,18 @@
 		local roundDraw = false
 		if #clanScores >= 2 and clanScores[1].score == clanScores[2].score and clanScores[1].score ~= MAX_TURN_TIME then
 			roundDraw = true
-			SendStat(siGameResult, loc("Round draw"))
+			SendStat(siGameResult, GetEngineString("TMsgStrId", sidDraw))
 			SendStat(siCustomAchievement, loc("The teams are tied for the fastest time."))
 		elseif #sortedTeams >= 1 then
-			SendStat(siGameResult, string.format(loc("%s wins!"), sortedTeams[1].name))
+
+			SendStat(siGameResult, formatEngineString(GetEngineString("TMsgStrId", sidWinner), sortedTeams[1].name))
 			SendStat(siCustomAchievement, string.format(loc("%s wins with a best time of %.1fs."), sortedTeams[1].name, (sortedTeams[1].score/1000)))
 			for i=1,#unfinishedArray do
 				 SendStat(siCustomAchievement, unfinishedArray[i])
 			end
 		else
 			roundDraw = true
-			SendStat(siGameResult, loc("Round draw"))
+			SendStat(siGameResult, GetEngineString("TMsgStrId", sidDraw))
 			SendStat(siCustomAchievement, loc("Nobody managed to finish the race. What a shame!"))
 			SendStat(siCustomAchievement, loc("Maybe you should try an easier TechRacer map."))
 		end
--- a/share/hedgewars/Data/Scripts/Utils.lua	Wed Jun 28 21:16:14 2023 +0200
+++ b/share/hedgewars/Data/Scripts/Utils.lua	Wed Jun 28 23:41:38 2023 +0200
@@ -127,6 +127,22 @@
 	end
 end
 
+-- Insert parameters %1 to %9 into an engine string and returns the result.
+-- * text: engine string with parameters (from GetEngineString)
+-- * ...: Arguments to insert into the string. The number of arguments MUST match
+--        the number of available arguments of the engine string
+--
+-- Example: formatEngineString(GetEngineString("TMsgStrId", sidWinner), "My Team")
+-- to create a string showing the winning team.
+function formatEngineString(text, ...)
+    local input = text
+    for i=1, 9 do
+       text = string.gsub(text, "%%"..i, "%%s")
+    end
+    text = string.format(text, ...)
+    return text
+end
+
 --[[ GLOBAL VARIABLES ]]
 
 -- Shared common land color values for land sprites.