Engine:
authorsmxx
Sat, 06 Mar 2010 13:22:52 +0000
changeset 2949 d137a9da7701
parent 2948 3f21a9dc93d0
child 2950 81dd71096b1f
Engine: * Added localisation support to LUA (see example hwt files)
hedgewars/uScript.pas
share/hedgewars/Data/Missions/Bazooka Training.hwt
share/hedgewars/Data/Missions/Shotgun Training.hwt
--- a/hedgewars/uScript.pas	Sat Mar 06 10:59:20 2010 +0000
+++ b/hedgewars/uScript.pas	Sat Mar 06 13:22:52 2010 +0000
@@ -333,77 +333,83 @@
 end;
 
 procedure ScriptOnGameInit;
+var s, t : ansistring;
 begin
-    // not required if there's no script to run
-    if not ScriptLoaded then
-        exit;
-            
-    // push game variables so they may be modified by the script
-    ScriptSetInteger('GameFlags', GameFlags);
-    ScriptSetString('Seed', cSeed);
-    ScriptSetInteger('TurnTime', cHedgehogTurnTime);
-    ScriptSetInteger('CaseFreq', cCaseFactor);
-    ScriptSetInteger('LandAdds', cLandAdditions);
-    ScriptSetInteger('Delay', cInactDelay);
-    ScriptSetString('Map', '');
-    ScriptSetString('Theme', '');
+// not required if there's no script to run
+if not ScriptLoaded then
+    exit;
+        
+// push game variables so they may be modified by the script
+ScriptSetInteger('GameFlags', GameFlags);
+ScriptSetString('Seed', cSeed);
+ScriptSetInteger('TurnTime', cHedgehogTurnTime);
+ScriptSetInteger('CaseFreq', cCaseFactor);
+ScriptSetInteger('LandAdds', cLandAdditions);
+ScriptSetInteger('Delay', cInactDelay);
+ScriptSetString('Map', '');
+ScriptSetString('Theme', '');
 
-    ScriptCall('onGameInit');
-    
-    // pop game variables
-    ParseCommand('seed ' + ScriptGetString('Seed'), true);
-    ParseCommand('$gmflags ' + ScriptGetString('GameFlags'), true);
-    ParseCommand('$turntime ' + ScriptGetString('TurnTime'), true);
-    ParseCommand('$casefreq ' + ScriptGetString('CaseFreq'), true);
-    ParseCommand('$landadds ' + ScriptGetString('LandAdds'), true);
-    ParseCommand('$delay ' + ScriptGetString('Delay'), true);
-    if ScriptGetString('Map') <> '' then
-        ParseCommand('map ' + ScriptGetString('Map'), true);
-    if ScriptGetString('Theme') <> '' then
-        ParseCommand('theme ' + ScriptGetString('Theme'), true);    
+// import locale
+s:= cLocaleFName;
+SplitByChar(s, t, '.');
+ScriptSetString('L', s);
+
+ScriptCall('onGameInit');
 
-    ScriptPrepareAmmoStore;
-    ScriptCall('onAmmoStoreInit');
-    ScriptApplyAmmoStore;
+// pop game variables
+ParseCommand('seed ' + ScriptGetString('Seed'), true);
+ParseCommand('$gmflags ' + ScriptGetString('GameFlags'), true);
+ParseCommand('$turntime ' + ScriptGetString('TurnTime'), true);
+ParseCommand('$casefreq ' + ScriptGetString('CaseFreq'), true);
+ParseCommand('$landadds ' + ScriptGetString('LandAdds'), true);
+ParseCommand('$delay ' + ScriptGetString('Delay'), true);
+if ScriptGetString('Map') <> '' then
+    ParseCommand('map ' + ScriptGetString('Map'), true);
+if ScriptGetString('Theme') <> '' then
+    ParseCommand('theme ' + ScriptGetString('Theme'), true);    
+
+ScriptPrepareAmmoStore;
+ScriptCall('onAmmoStoreInit');
+ScriptApplyAmmoStore;
 end;
 
 procedure ScriptLoad(name : shortstring);
 var ret : LongInt;
 begin
-    ret:= luaL_loadfile(luaState, Str2PChar(name));
-    if ret <> 0 then
-        WriteLnToConsole('LUA: Failed to load ' + name + '(error ' + IntToStr(ret) + ')')
-    else
-        begin
-        WriteLnToConsole('LUA: ' + name + ' loaded');
-        // call the script file
-        lua_pcall(luaState, 0, 0, 0);
-        ScriptLoaded:= true
-        end
+ret:= luaL_loadfile(luaState, Str2PChar(name));
+if ret <> 0 then
+    WriteLnToConsole('LUA: Failed to load ' + name + '(error ' + IntToStr(ret) + ')')
+else
+    begin
+    WriteLnToConsole('LUA: ' + name + ' loaded');
+    // call the script file
+    lua_pcall(luaState, 0, 0, 0);
+    ScriptLoaded:= true
+    end
 end;
 
 procedure SetGlobals;
 begin
-    ScriptSetInteger('TurnTimeLeft', TurnTimeLeft);
+ScriptSetInteger('TurnTimeLeft', TurnTimeLeft);
 end;
 
 procedure GetGlobals;
 begin
-    TurnTimeLeft:= ScriptGetInteger('TurnTimeLeft');
+TurnTimeLeft:= ScriptGetInteger('TurnTimeLeft');
 end;
 
 procedure ScriptCall(fname : shortstring);
 begin
-    if not ScriptLoaded then
-        exit;
-    SetGlobals;
-    lua_getglobal(luaState, Str2PChar(fname));
-    if lua_pcall(luaState, 0, 0, 0) <> 0 then
-        begin
-        WriteLnToConsole('LUA: Error while calling ' + fname + ': ' + lua_tostring(luaState, -1));
-        lua_pop(luaState, 1)
-        end;
-    GetGlobals;
+if not ScriptLoaded then
+    exit;
+SetGlobals;
+lua_getglobal(luaState, Str2PChar(fname));
+if lua_pcall(luaState, 0, 0, 0) <> 0 then
+    begin
+    WriteLnToConsole('LUA: Error while calling ' + fname + ': ' + lua_tostring(luaState, -1));
+    lua_pop(luaState, 1)
+    end;
+GetGlobals;
 end;
 
 function ScriptCall(fname : shortstring; par1: LongInt) : LongInt;
@@ -423,26 +429,26 @@
 
 function ScriptCall(fname : shortstring; par1, par2, par3, par4 : LongInt) : LongInt;
 begin
-    if not ScriptLoaded then
-        exit;
-    SetGlobals;
-    lua_getglobal(luaState, Str2PChar(fname));
-    lua_pushinteger(luaState, par1);
-    lua_pushinteger(luaState, par2);
-    lua_pushinteger(luaState, par3);
-    lua_pushinteger(luaState, par4);
-    ScriptCall:= 0;
-    if lua_pcall(luaState, 4, 1, 0) <> 0 then
-        begin
-        WriteLnToConsole('LUA: Error while calling ' + fname + ': ' + lua_tostring(luaState, -1));
-        lua_pop(luaState, 1)
-        end
-    else
-        begin
-        ScriptCall:= lua_tointeger(luaState, -1);
-        lua_pop(luaState, 1)
-        end;
-    GetGlobals;
+if not ScriptLoaded then
+    exit;
+SetGlobals;
+lua_getglobal(luaState, Str2PChar(fname));
+lua_pushinteger(luaState, par1);
+lua_pushinteger(luaState, par2);
+lua_pushinteger(luaState, par3);
+lua_pushinteger(luaState, par4);
+ScriptCall:= 0;
+if lua_pcall(luaState, 4, 1, 0) <> 0 then
+    begin
+    WriteLnToConsole('LUA: Error while calling ' + fname + ': ' + lua_tostring(luaState, -1));
+    lua_pop(luaState, 1)
+    end
+else
+    begin
+    ScriptCall:= lua_tointeger(luaState, -1);
+    lua_pop(luaState, 1)
+    end;
+GetGlobals;
 end;
 
 procedure ScriptPrepareAmmoStore;
@@ -501,21 +507,25 @@
 ScriptSetInteger('LAND_HEIGHT', LAND_HEIGHT);
 
 // import game flags
-ScriptSetInteger('gfForts',gfForts);
-ScriptSetInteger('gfMultiWeapon',gfMultiWeapon);
-ScriptSetInteger('gfSolidLand',gfSolidLand);
-ScriptSetInteger('gfBorder',gfBorder);
-ScriptSetInteger('gfDivideTeams',gfDivideTeams);
-ScriptSetInteger('gfLowGravity',gfLowGravity);
-ScriptSetInteger('gfLaserSight',gfLaserSight);
-ScriptSetInteger('gfInvulnerable',gfInvulnerable);
-ScriptSetInteger('gfMines',gfMines);
-ScriptSetInteger('gfVampiric',gfVampiric);
-ScriptSetInteger('gfKarma',gfKarma);
-ScriptSetInteger('gfArtillery',gfArtillery);
-ScriptSetInteger('gfOneClanMode',gfOneClanMode);
-ScriptSetInteger('gfRandomOrder',gfRandomOrder);
-ScriptSetInteger('gfKing',gfKing);
+ScriptSetInteger('gfForts', gfForts);
+ScriptSetInteger('gfMultiWeapon', gfMultiWeapon);
+ScriptSetInteger('gfSolidLand', gfSolidLand);
+ScriptSetInteger('gfBorder', gfBorder);
+ScriptSetInteger('gfDivideTeams', gfDivideTeams);
+ScriptSetInteger('gfLowGravity', gfLowGravity);
+ScriptSetInteger('gfLaserSight', gfLaserSight);
+ScriptSetInteger('gfInvulnerable', gfInvulnerable);
+ScriptSetInteger('gfMines', gfMines);
+ScriptSetInteger('gfVampiric', gfVampiric);
+ScriptSetInteger('gfKarma', gfKarma);
+ScriptSetInteger('gfArtillery', gfArtillery);
+ScriptSetInteger('gfOneClanMode', gfOneClanMode);
+ScriptSetInteger('gfRandomOrder', gfRandomOrder);
+ScriptSetInteger('gfKing', gfKing);
+ScriptSetInteger('gfPlaceHog', gfPlaceHog);
+ScriptSetInteger('gfSharedAmmo', gfSharedAmmo);
+ScriptSetInteger('gfDisableGirders', gfDisableGirders);
+ScriptSetInteger('gfExplosives', gfExplosives);
 
 // register gear types
 for at:= Low(TGearType) to High(TGearType) do
--- a/share/hedgewars/Data/Missions/Bazooka Training.hwt	Sat Mar 06 10:59:20 2010 +0000
+++ b/share/hedgewars/Data/Missions/Bazooka Training.hwt	Sat Mar 06 13:22:52 2010 +0000
@@ -8,6 +8,46 @@
 -- following "--" is ignored.
 
 ---------------------------------------------------------------
+-- At first we put all text we'd like to use in some arrays.
+-- This way we're able to localize the text to be shown without
+-- modifying other files.
+-- The language to be used is stored in the global variable
+-- 'L' that is set by the game (string).
+-- Text may then be accessed using "arrayname[L]".
+local caption = {
+	["en"] = "Bazooka Training",
+	["de"] = "Bazooka-Training"
+	}
+local subcaption = {
+	["en"] = "Aiming Practice",
+	["de"] = "Zielübung"
+	}
+
+local goal = {
+	["en"] = "Eliminate all targets before your time runs out.|You have unlimited ammo for this mission.",
+	["de"] = "Eliminiere alle Ziele bevor die Zeit ausläuft.|Du hast in dieser Mission unbegrenzte Munition."
+	}
+
+local timeout = {
+	["en"] = "Oh no! Time's up! Just try again.",
+	["de"] = "Oh nein! Die Zeit ist um! Versuche es nochmal."
+	}
+
+local success = {
+	["en"] = "Congratulations! You've eliminated all targets|within the allowed time frame.",
+	["de"] = "Gratulation! Du hast alle Ziele innerhalb der|verfügbaren Zeit ausgeschaltet."
+	}
+
+local teamname = {
+	["en"] = "'Zooka Team",
+	["de"] = "Die Knalltüten"
+	}
+
+local hogname = {
+	["en"] = "Hunter",
+	["de"] = "Jäger"
+	}
+---------------------------------------------------------------
 
 -- This variable will hold the number of destroyed targets.
 local score = 0
@@ -73,9 +113,9 @@
 	Theme = "Bamboo"
 
 	-- Create the player team
-	AddTeam("'Zooka Team", 14483456, "Simple", "Island", "Default")
+	AddTeam(teamname[L], 14483456, "Simple", "Island", "Default")
 	-- And add a hog to it
-	player = AddHog("Hunter", 0, 1, "NoHat")
+	player = AddHog(hogname[L], 0, 1, "NoHat")
 	SetGearPosition(player, 1960, 1160);
 end
 
@@ -92,7 +132,7 @@
 	-- A negative icon parameter (-n) represents the n-th weapon icon
 	-- A positive icon paramter (n) represents the (n+1)-th mission icon
 	-- A timeframe of 0 is replaced with the default time to show.
-	ShowMission("Bazooka Training", "Aiming Practice", "Eliminate all targets before your time runs out.|You have unlimited ammo for this mission.", -amBazooka, 0);
+	ShowMission(caption[L], subcaption[L], goal[L], -amBazooka, 0);
 end
 
 -- This function is called every game tick.
@@ -106,7 +146,7 @@
 	if TurnTimeLeft == 1 and score < score_goal then
 		game_lost = true
 		-- ... and show a short message.
-		ShowMission("Bazooka Training", "Aiming Practice", "Oh no! Time's up! Just try again.", -amSkip, 0);
+		ShowMission(caption[L], subcaption[L], timeout[L], -amSkip, 0);
 		-- How about killing our poor hog due to his poor performance?
 		SetHealth(player, 0);
 		-- Just to be sure set the goal time to 1 ms
@@ -155,7 +195,7 @@
 		else
 			if not game_lost then
 			-- Otherwise show that the goal was accomplished
-			ShowMission("Bazooka Training", "Aiming Practice", "Congratulations! You've eliminated all targets|within the allowed time frame.", 0, 0);
+			ShowMission(caption[L], subcaption[L], success[L], 0, 0);
 			-- Also let the hogs shout "victory!"
 			PlaySound(sndVictory)
 			-- Save the time left so we may keep it.
--- a/share/hedgewars/Data/Missions/Shotgun Training.hwt	Sat Mar 06 10:59:20 2010 +0000
+++ b/share/hedgewars/Data/Missions/Shotgun Training.hwt	Sat Mar 06 13:22:52 2010 +0000
@@ -8,6 +8,46 @@
 -- following "--" is ignored.
 
 ---------------------------------------------------------------
+-- At first we put all text we'd like to use in some arrays.
+-- This way we're able to localize the text to be shown without
+-- modifying other files.
+-- The language to be used is stored in the global variable
+-- 'L' that is set by the game (string).
+-- Text may then be accessed using "arrayname[L]".
+local caption = {
+	["en"] = "Bazooka Training",
+	["de"] = "Bazooka-Training"
+	}
+local subcaption = {
+	["en"] = "Aiming Practice",
+	["de"] = "Zielübung"
+	}
+
+local goal = {
+	["en"] = "Eliminate all targets before your time runs out.|You have unlimited ammo for this mission.",
+	["de"] = "Eliminiere alle Ziele bevor die Zeit ausläuft.|Du hast in dieser Mission unbegrenzte Munition."
+	}
+
+local timeout = {
+	["en"] = "Oh no! Time's up! Just try again.",
+	["de"] = "Oh nein! Die Zeit ist um! Versuche es nochmal."
+	}
+
+local success = {
+	["en"] = "Congratulations! You've eliminated all targets|within the allowed time frame.",
+	["de"] = "Gratulation! Du hast alle Ziele innerhalb der|verfügbaren Zeit ausgeschaltet."
+	}
+
+local teamname = {
+	["en"] = "Shotgun Team",
+	["de"] = "Die Knalltüten"
+	}
+
+local hogname = {
+	["en"] = "Hunter",
+	["de"] = "Jäger"
+	}
+---------------------------------------------------------------
 
 -- This variable will hold the number of destroyed targets.
 local score = 0
@@ -73,9 +113,9 @@
 	Theme = "nature"
 
 	-- Create the player team
-	AddTeam("Shotgun Team", 14483456, "Simple", "Island", "Default")
+	AddTeam(teamname[L], 14483456, "Simple", "Island", "Default")
 	-- And add a hog to it
-	player = AddHog("Hunter", 0, 1, "NoHat")
+	player = AddHog(hogname[L], 0, 1, "NoHat")
 	SetGearPosition(player, 2334, 1254);
 end
 
@@ -92,7 +132,7 @@
 	-- A negative icon parameter (-n) represents the n-th weapon icon
 	-- A positive icon paramter (n) represents the (n+1)-th mission icon
 	-- A timeframe of 0 is replaced with the default time to show.
-	ShowMission("Shotgun Training", "Aiming Practice", "Eliminate all targets before your time runs out.|You have unlimited ammo for this mission.", -5, 0);
+	ShowMission(caption[L], subcaption[L], goal[L], -amShotgun, 0);
 end
 
 -- This function is called every game tick.
@@ -106,7 +146,7 @@
 	if TurnTimeLeft == 1 and score < score_goal then
 		game_lost = true
 		-- ... and show a short message.
-		ShowMission("Shotgun Training", "Aiming Practice", "Oh no! Time's up! Just try again.", -amSkip, 0);
+		ShowMission(caption[L], subcaption[L], timeout[L], -amSkip, 0);
 		-- How about killing our poor hog due to his poor performance?
 		SetHealth(player, 0);
 		-- Just to be sure set the goal time to 1 ms
@@ -155,7 +195,7 @@
 		else
 			if not game_lost then
 			-- Otherwise show that the goal was accomplished
-			ShowMission("Shotgun Training", "Aiming Practice", "Congratulations! You've eliminated all targets|within the allowed time frame.", 0, 0);
+			ShowMission(caption[L], subcaption[L], success[L], 0, 0);
 			-- Also let the hogs shout "victory!"
 			PlaySound(sndVictory)
 			-- Save the time left so we may keep it.