Add SetTeamLabel to Lua API, can set arbitrary string as team label
authorWuzzy <Wuzzy2@mail.ru>
Fri, 16 Feb 2018 13:57:48 +0100
changeset 13023 0248cf0da834
parent 13022 e2e40700c5cf
child 13024 880662cf41ee
Add SetTeamLabel to Lua API, can set arbitrary string as team label
ChangeLog.txt
hedgewars/uScript.pas
hedgewars/uStore.pas
hedgewars/uTeams.pas
hedgewars/uTypes.pas
hedgewars/uWorld.pas
--- a/ChangeLog.txt	Fri Feb 16 12:38:30 2018 +0100
+++ b/ChangeLog.txt	Fri Feb 16 13:57:48 2018 +0100
@@ -64,6 +64,7 @@
  + New call: GetTeamName(teamIdx): Returns name of team with given index (starts at 0)
  + New call: SpawnSupplyCrate(x, y, content, [, amount]): Spawn ammo or utility crate, depending on content
  + New call: HealHog(gearUid, healthBoost[, showMessage[, tint]]): Heal hedgehog with graphical effects and message
+ + New call: SetTeamLabel(teamname[, label]): Set an arbitrary label for a team, will be displayed next to the team bar
  + New callback: onEndTurn(): Called at the end of a turn (when gears have settled)
  * AddAmmo now automatically unselects weapon if it would remove current ammo from current hedgehog
  * Fix call: SetWeapon(amNothing) now unselects weapon
--- a/hedgewars/uScript.pas	Fri Feb 16 12:38:30 2018 +0100
+++ b/hedgewars/uScript.pas	Fri Feb 16 13:57:48 2018 +0100
@@ -2108,6 +2108,48 @@
     lc_addteam:= 0;//1;
 end;
 
+function lc_setteamlabel(L : Plua_State) : LongInt; Cdecl;
+var teamValue: ansistring;
+    i, n: LongInt;
+    success: boolean;
+begin
+    if CheckAndFetchParamCount(L, 1, 2, 'SetTeamLabel', 'teamname[, label]', n) then
+        begin
+        success:= false;
+        // fetch team
+        if TeamsCount > 0 then
+            for i:= 0 to Pred(TeamsCount) do
+                begin
+                // skip teams that don't have matching name
+                if TeamsArray[i]^.TeamName <> lua_tostring(L, 1) then
+                    continue;
+
+                // value of type nil? Then let's clear the team value
+                if (n < 2) or lua_isnil(L, 2) then
+                    begin
+                    FreeAndNilTexture(TeamsArray[i]^.LuaTeamValueTex);
+                    TeamsArray[i]^.hasLuaTeamValue:= false;
+                    success:= true;
+                    end
+                // value of type string? Then let's set the team value
+                else if (lua_isstring(L, 2)) then
+                    begin
+                    teamValue:= lua_tostring(L, 2);
+                    TeamsArray[i]^.LuaTeamValue:= teamValue;
+                    FreeAndNilTexture(TeamsArray[i]^.LuaTeamValueTex);
+                    TeamsArray[i]^.LuaTeamValueTex := RenderStringTex(teamValue, TeamsArray[i]^.Clan^.Color, fnt16);
+                    TeamsArray[i]^.hasLuaTeamValue:= true;
+                    success:= true;
+                    end;
+                // don't change more than one team
+                break;
+                end;
+        end;
+    // return true if operation was successful, false otherwise
+    lua_pushboolean(L, success);
+    lc_setteamlabel:= 1;
+end;
+
 function lc_getteamname(L : Plua_State) : LongInt; Cdecl;
 var t: LongInt;
 begin
@@ -3712,6 +3754,7 @@
 lua_register(luaState, _P'PlaySound', @lc_playsound);
 lua_register(luaState, _P'GetTeamName', @lc_getteamname);
 lua_register(luaState, _P'AddTeam', @lc_addteam);
+lua_register(luaState, _P'SetTeamLabel', @lc_setteamlabel);
 lua_register(luaState, _P'AddHog', @lc_addhog);
 lua_register(luaState, _P'AddAmmo', @lc_addammo);
 lua_register(luaState, _P'GetAmmoCount', @lc_getammocount);
--- a/hedgewars/uStore.pas	Fri Feb 16 12:38:30 2018 +0100
+++ b/hedgewars/uStore.pas	Fri Feb 16 13:57:48 2018 +0100
@@ -261,6 +261,7 @@
         if not allOK then exit;
 
         AIKillsTex := RenderStringTex(ansistring(inttostr(stats.AIKills)), Clan^.Color, fnt16);
+        LuaTeamValueTex := RenderStringTex(LuaTeamValue, Clan^.Color, fnt16);
 
         dec(drY, r.h + 2);
         DrawHealthY:= drY;
--- a/hedgewars/uTeams.pas	Fri Feb 16 12:38:30 2018 +0100
+++ b/hedgewars/uTeams.pas	Fri Feb 16 13:57:48 2018 +0100
@@ -891,6 +891,7 @@
             FreeAndNilTexture(OwnerTex);
             FreeAndNilTexture(GraveTex);
             FreeAndNilTexture(AIKillsTex);
+            FreeAndNilTexture(LuaTeamValueTex);
             FreeAndNilTexture(FlagTex);
             end;
 
--- a/hedgewars/uTypes.pas	Fri Feb 16 12:38:30 2018 +0100
+++ b/hedgewars/uTypes.pas	Fri Feb 16 13:57:48 2018 +0100
@@ -405,6 +405,7 @@
             OwnerTex,
             GraveTex,
             AIKillsTex,
+            LuaTeamValueTex,
             FlagTex: PTexture;
             Flag: shortstring;
             GraveName: shortstring;
@@ -421,6 +422,8 @@
             hasGone: boolean;
             skippedTurns: Longword;
             isGoneFlagPendingToBeSet, isGoneFlagPendingToBeUnset: boolean;
+            luaTeamValue: ansistring;
+            hasLuaTeamValue: boolean;
             end;
 
     TClan = record
--- a/hedgewars/uWorld.pas	Fri Feb 16 12:38:30 2018 +0100
+++ b/hedgewars/uWorld.pas	Fri Feb 16 13:57:48 2018 +0100
@@ -1087,11 +1087,12 @@
                     DrawTexture(15 + h * TeamHealthBarWidth div TeamHealthBarHealth, cScreenHeight + DrawHealthY + smallScreenOffset + 1, SpritesData[sprSlider].Texture);
                 end;
 
-        // draw ai kill counter for gfAISurvival
-        if (GameFlags and gfAISurvival) <> 0 then
-            begin
+        // draw Lua value, if set
+        if (hasLuaTeamValue) then
+            DrawTexture(TeamHealthBarWidth + 22, cScreenHeight + DrawHealthY + smallScreenOffset, LuaTeamValueTex)
+        // otherwise, draw AI kill counter for gfAISurvival
+        else if (GameFlags and gfAISurvival) <> 0 then
             DrawTexture(TeamHealthBarWidth + 22, cScreenHeight + DrawHealthY + smallScreenOffset, AIKillsTex);
-            end;
 
         // if highlighted, draw flag and other contents again to keep their colors
         // this approach should be faster than drawing all borders one by one tinted or not
@@ -1115,7 +1116,13 @@
                 DrawTextureFromRect(-OwnerTex^.w - NameTagTex^.w - 16, cScreenHeight + DrawHealthY + smallScreenOffset + 2, @r, OwnerTex)
                 end;
 
-            if (GameFlags and gfAISurvival) <> 0 then
+            if (hasLuaTeamValue) then
+                begin
+                r.w:= LuaTeamValueTex^.w - 4;
+                r.h:= LuaTeamValueTex^.h - 4;
+                DrawTextureFromRect(TeamHealthBarWidth + 24, cScreenHeight + DrawHealthY + smallScreenOffset + 2, @r, LuaTeamValueTex);
+                end
+            else if (GameFlags and gfAISurvival) <> 0 then
                 begin
                 r.w:= AIKillsTex^.w - 4;
                 r.h:= AIKillsTex^.h - 4;