Lua API: Add functions to add team selected in campaign/mission page
authorWuzzy <Wuzzy2@mail.ru>
Wed, 19 Dec 2018 19:10:42 +0100
changeset 14477 4b678aad50e9
parent 14475 2113296b7a29
child 14478 35724823b0d3
Lua API: Add functions to add team selected in campaign/mission page
ChangeLog.txt
QTfrontend/game.cpp
QTfrontend/team.cpp
QTfrontend/team.h
hedgewars/uScript.pas
hedgewars/uTeams.pas
hedgewars/uVariables.pas
--- a/ChangeLog.txt	Wed Dec 19 05:50:02 2018 +0100
+++ b/ChangeLog.txt	Wed Dec 19 19:10:42 2018 +0100
@@ -23,6 +23,8 @@
  + New call: GetMissionVar(varname): Get value of mission variable
  + New call: SetTurnTimePaused(isPaused): Call with true to pause turn time, false to unpause
  + New call: GetTurnTimePaused(): Returns true if turn time is paused due to Lua
+ + New call: AddMissionTeam(color): Add mission team, i.e. the team selected by player in campaign/mission page
+ + New call: AddMissionHog(health): Add a hedgehog for the mission team
  + Utils library: New calls: getReadableChallengeRecord, updateChallengeRecord
  + New callback: onGameResult(winningClan): Called when the game ends normally. winningClan = index of winning clan or -1 on draw
  + Params explode, poison in the SpawnFake*Crate functions now optional and default to false
--- a/QTfrontend/game.cpp	Wed Dec 19 05:50:02 2018 +0100
+++ b/QTfrontend/game.cpp	Wed Dec 19 19:10:42 2018 +0100
@@ -29,6 +29,8 @@
 
 #include "hwform.h"
 #include "ui/page/pageoptions.h"
+#include "ui/page/pagetraining.h"
+#include "ui/page/pagecampaign.h"
 #include "game.h"
 #include "hwconsts.h"
 #include "gameuiconfig.h"
@@ -183,6 +185,14 @@
 {
     QByteArray traincfg;
     HWProto::addStringToBuffer(traincfg, "TL");
+
+    HWTeam missionTeam = HWTeam();
+    missionTeam.setName(config->Form->ui.pageTraining->CBTeam->currentText());
+    missionTeam.loadFromFile();
+    missionTeam.setNumHedgehogs(HEDGEHOGS_PER_TEAM);
+    missionTeam.setMissionTeam(true);
+    HWProto::addStringListToBuffer(traincfg, missionTeam.teamGameConfig(100));
+
     HWProto::addStringToBuffer(traincfg, "eseed " + QUuid::createUuid().toString());
     HWProto::addStringToBuffer(traincfg, "escript " + trainingScript);
 
@@ -193,8 +203,15 @@
 {
     QByteArray campaigncfg;
     HWProto::addStringToBuffer(campaigncfg, "TL");
+
+    HWTeam missionTeam = HWTeam();
+    missionTeam.setName(config->Form->ui.pageCampaign->CBTeam->currentText());
+    missionTeam.loadFromFile();
+    missionTeam.setNumHedgehogs(HEDGEHOGS_PER_TEAM);
+    missionTeam.setMissionTeam(true);
+    HWProto::addStringListToBuffer(campaigncfg, missionTeam.teamGameConfig(100));
+
     HWProto::addStringToBuffer(campaigncfg, "eseed " + QUuid::createUuid().toString());
-
     HWProto::addStringToBuffer(campaigncfg, "escript " + campaignScript);
 
     RawSendIPC(campaigncfg);
--- a/QTfrontend/team.cpp	Wed Dec 19 05:50:02 2018 +0100
+++ b/QTfrontend/team.cpp	Wed Dec 19 19:10:42 2018 +0100
@@ -35,6 +35,7 @@
     , m_difficulty(0)
     , m_numHedgehogs(4)
     , m_isNetTeam(false)
+    , m_isMissionTeam(false)
 {
     m_name = teamname;
     OldTeamName = m_name;
@@ -61,6 +62,7 @@
     QObject(0)
     , m_numHedgehogs(4)
     , m_isNetTeam(true)
+    , m_isMissionTeam(false)
 {
     // net teams are configured from QStringList
     if(strLst.size() != 23) throw HWTeamConstructException();
@@ -88,6 +90,7 @@
     , m_difficulty(0)
     , m_numHedgehogs(4)
     , m_isNetTeam(false)
+    , m_isMissionTeam(false)
 {
     m_name = QString("Team");
     for (int i = 0; i < HEDGEHOGS_PER_TEAM; i++)
@@ -125,6 +128,7 @@
     , m_numHedgehogs(other.m_numHedgehogs)
     , m_color(other.m_color)
     , m_isNetTeam(other.m_isNetTeam)
+    , m_isMissionTeam(other.m_isMissionTeam)
     , m_owner(other.m_owner)
 //      , AchievementProgress(other.AchievementProgress)
 {
@@ -149,6 +153,7 @@
         m_isNetTeam = other.m_isNetTeam;
         m_owner = other.m_owner;
         m_color = other.m_color;
+        m_isMissionTeam = other.m_isMissionTeam;
     }
 
     return *this;
@@ -243,12 +248,25 @@
 QStringList HWTeam::teamGameConfig(quint32 InitHealth) const
 {
     QStringList sl;
+    QString cmdAddHog = "eaddhh";
+
     if (m_isNetTeam)
     {
         sl.push_back(QString("eaddteam %3 %1 %2").arg(qcolor().rgb() & 0xffffff).arg(m_name).arg(QString(QCryptographicHash::hash(m_owner.toUtf8(), QCryptographicHash::Md5).toHex())));
         sl.push_back("erdriven");
     }
-    else sl.push_back(QString("eaddteam %3 %1 %2").arg(qcolor().rgb() & 0xffffff).arg(m_name).arg(playerHash));
+    else
+    {
+        if (m_isMissionTeam)
+        {
+            sl.push_back(QString("esetmissteam %3 %1 %2").arg(qcolor().rgb() & 0xffffff).arg(m_name).arg(playerHash));
+            cmdAddHog = "eaddmisshh";
+        }
+        else
+        {
+            sl.push_back(QString("eaddteam %3 %1 %2").arg(qcolor().rgb() & 0xffffff).arg(m_name).arg(playerHash));
+        }
+    }
 
     sl.push_back(QString("egrave " + m_grave));
     sl.push_back(QString("efort " + m_fort));
@@ -260,7 +278,7 @@
 
     for (int t = 0; t < m_numHedgehogs; t++)
     {
-        sl.push_back(QString("eaddhh %1 %2 %3")
+        sl.push_back(QString(cmdAddHog + " %1 %2 %3")
                      .arg(QString::number(m_difficulty),
                           QString::number(InitHealth),
                           m_hedgehogs[t].Name));
@@ -281,6 +299,15 @@
     return m_isNetTeam;
 }
 
+void HWTeam::setMissionTeam(bool isMissionTeam)
+{
+    m_isMissionTeam = isMissionTeam;
+}
+
+bool HWTeam::isMissionTeam() const
+{
+    return m_isMissionTeam;
+}
 
 bool HWTeam::operator==(const HWTeam& t1) const
 {
--- a/QTfrontend/team.h	Wed Dec 19 05:50:02 2018 +0100
+++ b/QTfrontend/team.h	Wed Dec 19 19:10:42 2018 +0100
@@ -72,6 +72,7 @@
         QString grave() const;
         const HWHog & hedgehog(unsigned int idx) const;
         bool isNetTeam() const;
+        bool isMissionTeam() const;
         QString keyBind(unsigned int idx) const;
         QString name() const;
         unsigned char numHedgehogs() const;
@@ -89,6 +90,7 @@
         void setNumHedgehogs(unsigned char num);
         void setVoicepack(const QString & voicepack);
         void setNetTeam(bool isNetTeam);
+        void setMissionTeam(bool isMissionTeam);
 
         // convert team info into strings for further computation
         QStringList teamGameConfig(quint32 InitHealth) const;
@@ -119,6 +121,7 @@
         quint8 m_numHedgehogs;
         int m_color;
         bool m_isNetTeam;
+        bool m_isMissionTeam;
         QString m_owner;
 
         // class members that contain statistics, etc.
--- a/hedgewars/uScript.pas	Wed Dec 19 05:50:02 2018 +0100
+++ b/hedgewars/uScript.pas	Wed Dec 19 19:10:42 2018 +0100
@@ -2307,6 +2307,43 @@
     lc_addteam:= 0;//1;
 end;
 
+function lc_addmissionteam(L : Plua_State) : LongInt; Cdecl;
+var colorArg: Int64;
+    colorStr: shortstring;
+begin
+    if CheckLuaParamCount(L, 1, 'AddMissionTeam', 'color') then
+        begin
+        if(MissionTeam = nil) then
+            begin
+            OutError('Lua error: AddMissionTeam: Could not add team. Note: This function only works in singleplayer missions!', true);
+            lc_addmissionteam:= 0;
+            exit;
+            end;
+
+        colorArg:= Trunc(lua_tonumber(L, 1));
+        if (colorArg < 0) and (abs(colorArg) <= cClanColors) then
+            // Pick clan color from settings (recommended)
+            colorStr:= IntToStr(ClanColorArray[Pred(abs(colorArg))])
+        else if (colorArg >= 0) and (colorArg <= $ffffffff) then
+            // Specify color directly
+            colorStr:= IntToStr(colorArg)
+        else
+            begin
+            OutError('Lua error: AddMissionTeam: Invalid ''color'' argument, must be between '+IntToStr(-cClanColors)+' and 0xffffffff!', true);
+            lc_addmissionteam:= 0;
+            exit;
+            end;
+
+        ParseCommand('addteam x ' + colorStr + ' ' + MissionTeam^.TeamName, true, true);
+        ParseCommand('grave ' + MissionTeam^.GraveName, true, true);
+        ParseCommand('fort ' + MissionTeam^.FortName, true, true);
+        ParseCommand('voicepack ' + MissionTeam^.Voicepack^.name, true, true);
+        ParseCommand('flag ' + MissionTeam^.Flag, true, true);
+        CurrentTeam^.Binds:= DefaultBinds
+        end;
+    lc_addmissionteam:= 0;
+end;
+
 function lc_setteamlabel(L : Plua_State) : LongInt; Cdecl;
 var teamValue: ansistring;
     i, n: LongInt;
@@ -2499,13 +2536,13 @@
 
 
 function lc_addhog(L : Plua_State) : LongInt; Cdecl;
-var temp: ShortString;
+var hatName: ShortString;
 begin
     if CheckLuaParamCount(L, 4, 'AddHog', 'hogname, botlevel, health, hat') then
         begin
-        temp:= lua_tostring(L, 4);
+        hatName:= lua_tostring(L, 4);
         ParseCommand('addhh ' + lua_tostring(L, 2) + ' ' + lua_tostring(L, 3) + ' ' + lua_tostring(L, 1), true, true);
-        ParseCommand('hat ' + temp, true, true);
+        ParseCommand('hat ' + hatName, true, true);
         lua_pushnumber(L, CurrentHedgehog^.Gear^.uid);
         end
     else
@@ -2513,6 +2550,31 @@
     lc_addhog:= 1;
 end;
 
+function lc_addmissionhog(L : Plua_State) : LongInt; Cdecl;
+var hatName: ShortString;
+begin
+    if CheckLuaParamCount(L, 1, 'AddMissionHog', 'health') then
+        begin
+        if(MissionTeam = nil) then
+            begin
+            OutError('Lua error: AddMissionHog: Could not add hog. Mission team is not set!', true);
+            lua_pushnil(L);
+            lc_addmissionhog:= 1;
+            exit;
+            end;
+        with MissionTeam^.Hedgehogs[CurrentTeam^.HedgehogsNumber] do
+            begin
+            hatName:= Hat;
+            ParseCommand('addhh ' + IntToStr(BotLevel) + ' ' + lua_tostring(L, 1) + ' ' + Name, true, true);
+            ParseCommand('hat ' + hatName, true, true);
+            end;
+        lua_pushnumber(L, CurrentHedgehog^.Gear^.uid);
+        end
+    else
+        lua_pushnil(L);
+    lc_addmissionhog:= 1;
+end;
+
 function lc_hogturnleft(L : Plua_State) : LongInt; Cdecl;
 var gear: PGear;
 begin
@@ -4289,8 +4351,10 @@
 lua_register(luaState, _P'GetTeamIndex', @lc_getteamindex);
 lua_register(luaState, _P'GetTeamClan', @lc_getteamclan);
 lua_register(luaState, _P'AddTeam', @lc_addteam);
+lua_register(luaState, _P'AddMissionTeam', @lc_addmissionteam);
 lua_register(luaState, _P'SetTeamLabel', @lc_setteamlabel);
 lua_register(luaState, _P'AddHog', @lc_addhog);
+lua_register(luaState, _P'AddMissionHog', @lc_addmissionhog);
 lua_register(luaState, _P'AddAmmo', @lc_addammo);
 lua_register(luaState, _P'GetAmmoCount', @lc_getammocount);
 lua_register(luaState, _P'HealHog', @lc_healhog);
--- a/hedgewars/uTeams.pas	Wed Dec 19 05:50:02 2018 +0100
+++ b/hedgewars/uTeams.pas	Wed Dec 19 19:10:42 2018 +0100
@@ -29,6 +29,7 @@
 procedure freeModule;
 
 function  AddTeam(TeamColor: Longword): PTeam;
+function  SetMissionTeam(TeamColor: Longword): PTeam;
 procedure SwitchHedgehog;
 procedure AfterSwitchHedgehog;
 procedure InitTeams;
@@ -476,6 +477,19 @@
     end;
 end;
 
+function SetMissionTeam(TeamColor: Longword): PTeam;
+var team: PTeam;
+begin
+New(team);
+if checkFails(team <> nil, 'AddTeam: team = nil', true) then exit(nil);
+FillChar(team^, sizeof(TTeam), 0);
+team^.HedgehogsNumber:= 0;
+
+CurrentTeam:= team;
+MissionTeam:= team;
+SetMissionTeam:= team;
+end;
+
 function AddTeam(TeamColor: Longword): PTeam;
 var team: PTeam;
     c: LongInt;
@@ -679,6 +693,30 @@
                     end
 end;
 
+procedure chAddMissionHH(var id: shortstring);
+var s: shortstring;
+    Health: LongInt;
+begin
+s:= '';
+if (not isDeveloperMode) then
+    exit;
+if checkFails((CurrentTeam <> nil), 'Can''t add hedgehogs yet, add a team first!', true) then exit;
+with CurrentTeam^ do
+    begin
+    if checkFails(HedgehogsNumber<=cMaxHHIndex, 'Can''t add hedgehog to "' + TeamName + '"! (already ' + intToStr(HedgehogsNumber) + ' hogs)', true) then exit;
+    SplitBySpace(id, s);
+    CurrentHedgehog:= @Hedgehogs[HedgehogsNumber];
+    CurrentHedgehog^.BotLevel:= StrToInt(id);
+    CurrentHedgehog^.Team:= CurrentTeam;
+    SplitBySpace(s, id);
+    Health:= StrToInt(s);
+    if checkFails((Health > 0) and (Health <= cMaxHogHealth), 'Invalid hedgehog health (must be between 1 and '+IntToStr(cMaxHogHealth)+')', true) then exit;
+    CurrentHedgehog^.Name:= id;
+    CurrentHedgehog^.InitialHealth:= Health;
+    inc(HedgehogsNumber)
+    end
+end;
+
 procedure chAddHH(var id: shortstring);
 var s: shortstring;
     Gear: PGear;
@@ -744,7 +782,7 @@
     // color is always little endian so the mask must be constant also in big endian archs
     Color:= Color or $FF000000;
     AddTeam(Color);
-    
+
     if CurrentTeam <> nil then
         begin
         CurrentTeam^.TeamName:= ts;
@@ -759,6 +797,31 @@
     end
 end;
 
+procedure chSetMissionTeam(var s: shortstring);
+var Color: Longword;
+    ts, cs: shortstring;
+begin
+cs:= '';
+ts:= '';
+if isDeveloperMode then
+    begin
+    SplitBySpace(s, cs);
+    SplitBySpace(cs, ts);
+    Color:= StrToInt(cs);
+
+    // color is always little endian so the mask must be constant also in big endian archs
+    Color:= Color or $FF000000;
+    SetMissionTeam(Color);
+
+    if CurrentTeam <> nil then
+        begin
+        CurrentTeam^.TeamName:= ts;
+        CurrentTeam^.PlayerHash:= s;
+        CurrentTeam^.voicepack:= AskForVoicepack('Default')
+        end
+    end
+end;
+
 procedure chSetHHCoords(var x: shortstring);
 var y: shortstring;
     t: Longint;
@@ -941,7 +1004,9 @@
 procedure initModule;
 begin
 RegisterVariable('addhh', @chAddHH, false);
+RegisterVariable('addmisshh', @chAddMissionHH, false);
 RegisterVariable('addteam', @chAddTeam, false);
+RegisterVariable('setmissteam', @chSetMissionTeam, false);
 RegisterVariable('hhcoords', @chSetHHCoords, false);
 RegisterVariable('bind', @chBind, true );
 RegisterVariable('teamgone', @chTeamGone, true );
--- a/hedgewars/uVariables.pas	Wed Dec 19 05:50:02 2018 +0100
+++ b/hedgewars/uVariables.pas	Wed Dec 19 19:10:42 2018 +0100
@@ -2488,6 +2488,7 @@
 
     CurrentTeam: PTeam;
     PreviousTeam: PTeam;
+    MissionTeam: PTeam;
     CurrentHedgehog: PHedgehog;
     TeamsArray: array[0..Pred(cMaxTeams)] of PTeam;
     TeamsCount: Longword; // number of teams on game start
@@ -2941,6 +2942,7 @@
     GearsList:= nil;
     CurrentTeam:= nil;
     PreviousTeam:= nil;
+    MissionTeam:= nil;
     CurrentHedgehog:= nil;
     FollowGear:= nil;
     lastVisualGearByUID:= nil;