Add Lua functions to save/get mission variables
authorWuzzy <Wuzzy2@mail.ru>
Tue, 18 Dec 2018 15:21:23 +0100
changeset 14468 bd1db668b7c0
parent 14467 4c743ef80b1b
child 14469 ead8928a59f8
Add Lua functions to save/get mission variables
ChangeLog.txt
QTfrontend/game.cpp
QTfrontend/mission.cpp
hedgewars/uCommandHandlers.pas
hedgewars/uIO.pas
hedgewars/uScript.pas
hedgewars/uVariables.pas
--- a/ChangeLog.txt	Tue Dec 18 14:24:23 2018 +0100
+++ b/ChangeLog.txt	Tue Dec 18 15:21:23 2018 +0100
@@ -16,6 +16,8 @@
  * Don't show "F1", "F2", etc. in ammo menu if these aren't the actual slot keys
 
 Lua API:
+ + New call: SaveMissionVar(varname, value): Save value to mission variable (variable for non-campaign mission)
+ + 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
  + Params explode, poison in the SpawnFake*Crate functions now optional and default to false
--- a/QTfrontend/game.cpp	Tue Dec 18 14:24:23 2018 +0100
+++ b/QTfrontend/game.cpp	Tue Dec 18 15:21:23 2018 +0100
@@ -48,7 +48,7 @@
 QString lastGameAmmo = NULL;
 TeamSelWidget * lastGameTeamSel = NULL;
 
-QString training, trainingTeam, campaign, campaignScript, campaignTeam; // TODO: Cleaner solution?
+QString trainingName, trainingScript, trainingTeam, campaign, campaignScript, campaignTeam; // TODO: Cleaner solution?
 
 HWGame::HWGame(GameUIConfig * config, GameCFGWidget * gamecfg, QString ammo, TeamSelWidget* pTeamSelWidget) :
     TCPBase(true, 0),
@@ -184,7 +184,7 @@
     QByteArray traincfg;
     HWProto::addStringToBuffer(traincfg, "TL");
     HWProto::addStringToBuffer(traincfg, "eseed " + QUuid::createUuid().toString());
-    HWProto::addStringToBuffer(traincfg, "escript " + training);
+    HWProto::addStringToBuffer(traincfg, "escript " + trainingScript);
 
     RawSendIPC(traincfg);
 }
@@ -517,7 +517,8 @@
 
     gameType = gtTraining;
 
-    training = "Missions/" + subFolder + "/" + file + ".lua";
+    trainingScript  = "Missions/" + subFolder + "/" + file + ".lua";
+    trainingName = file;
     trainingTeam = trainTeam;
     demo.clear();
     Start(false);
@@ -588,7 +589,7 @@
     QString varToFind = QString::fromUtf8(varToSend);
     QSettings teamfile(QString(cfgdir->absolutePath() + "/Teams/%1.hwt").arg(trainingTeam), QSettings::IniFormat, 0);
     teamfile.setIniCodec("UTF-8");
-    QString varValue = teamfile.value("Mission " + training + "/" + varToFind, "").toString();
+    QString varValue = teamfile.value("Mission " + trainingName + "/" + varToFind, "").toString();
     QByteArray command;
     HWProto::addStringToBuffer(command, "v." + varValue);
     RawSendIPC(command);
@@ -605,6 +606,6 @@
 
     QSettings teamfile(QString(cfgdir->absolutePath() + "/Teams/%1.hwt").arg(trainingTeam), QSettings::IniFormat, 0);
     teamfile.setIniCodec("UTF-8");
-    teamfile.setValue("Mission " + training + "/" + varToWrite, varValue);
+    teamfile.setValue("Mission " + trainingName + "/" + varToWrite, varValue);
 }
 
--- a/QTfrontend/mission.cpp	Tue Dec 18 14:24:23 2018 +0100
+++ b/QTfrontend/mission.cpp	Tue Dec 18 15:21:23 2018 +0100
@@ -27,20 +27,16 @@
 {
     QSettings* teamfile = new QSettings(cfgdir->absolutePath() + "/Teams/" + teamName + ".hwt", QSettings::IniFormat, 0);
     teamfile->setIniCodec("UTF-8");
-    // if entry not found check if there is written without _
-    // if then is found rename it to use _
-    QString cleanedMissionName = missionName;
-    cleanedMissionName = cleanedMissionName.replace(QString("_"),QString(" "));
-    if (!teamfile->childGroups().contains("Mission " + cleanedMissionName) &&
-            teamfile->childGroups().contains("Mission " + cleanedMissionName)){
-        teamfile->beginGroup("Mission " + cleanedMissionName);
+    if (!teamfile->childGroups().contains("Mission " + missionName) &&
+            teamfile->childGroups().contains("Mission " + missionName)){
+        teamfile->beginGroup("Mission " + missionName);
         QStringList keys = teamfile->childKeys();
         teamfile->endGroup();
         for (int i=0;i<keys.size();i++) {
-            QVariant value = teamfile->value("Mission " + cleanedMissionName + "/" + keys[i]);
+            QVariant value = teamfile->value("Mission " + missionName + "/" + keys[i]);
             teamfile->setValue("Mission " + missionName + "/" + keys[i], value);
         }
-        teamfile->remove("Mission " + cleanedMissionName);
+        teamfile->remove("Mission " + missionName);
     }
 
     return teamfile;
--- a/hedgewars/uCommandHandlers.pas	Tue Dec 18 14:24:23 2018 +0100
+++ b/hedgewars/uCommandHandlers.pas	Tue Dec 18 15:21:23 2018 +0100
@@ -853,6 +853,11 @@
   CampaignVariable := s;
 end;
 
+procedure chMissVar(var s:shortstring);
+begin
+  MissionVariable := s;
+end;
+
 procedure chWorldEdge(var s: shortstring);
 begin
 WorldEdge:= TWorldEdge(StrToInt(s))
@@ -963,6 +968,7 @@
     RegisterVariable('+cur_r'  , @chCurR_p       , true );
     RegisterVariable('-cur_r'  , @chCurR_m       , true );
     RegisterVariable('campvar' , @chCampVar      , true );
+    RegisterVariable('missvar' , @chMissVar      , true );
     RegisterVariable('record'  , @chRecord       , true );
     RegisterVariable('worldedge',@chWorldEdge    , false);
     RegisterVariable('advmapgen',@chAdvancedMapGenMode, false);
--- a/hedgewars/uIO.pas	Tue Dec 18 14:24:23 2018 +0100
+++ b/hedgewars/uIO.pas	Tue Dec 18 15:21:23 2018 +0100
@@ -171,6 +171,10 @@
               if s[2] = '.' then
                   ParseCommand('campvar ' + copy(s, 3, length(s) - 2), true);
           end;
+     'v': begin
+              if s[2] = '.' then
+                  ParseCommand('missvar ' + copy(s, 3, length(s) - 2), true);
+          end;
      'I': ParseCommand('pause server', true);
      's': if gameType = gmtNet then
              ParseChatCommand('chatmsg ', s, 2)
--- a/hedgewars/uScript.pas	Tue Dec 18 14:24:23 2018 +0100
+++ b/hedgewars/uScript.pas	Tue Dec 18 15:21:23 2018 +0100
@@ -3007,6 +3007,21 @@
     lc_getcampaignvar := 1;
 end;
 
+function lc_savemissionvar(L : Plua_State): LongInt; Cdecl;
+begin
+    if CheckLuaParamCount(L, 2, 'SaveMissionVar', 'varname, value') then
+        SendIPC('v!' + lua_tostring(L, 1) + ' ' + lua_tostring(L, 2) + #0);
+    lc_savemissionvar := 0;
+end;
+
+function lc_getmissionvar(L : Plua_State): LongInt; Cdecl;
+begin
+    if CheckLuaParamCount(L, 1, 'GetMissionVar', 'varname') then
+        SendIPCAndWaitReply('v?' + lua_tostring(L, 1) + #0);
+    lua_pushstring(L, str2pchar(MissionVariable));
+    lc_getmissionvar := 1;
+end;
+
 function lc_hidehog(L: Plua_State): LongInt; Cdecl;
 var gear: PGear;
 begin
@@ -4208,6 +4223,8 @@
 lua_register(luaState, _P'IsHogHidden', @lc_ishoghidden);
 lua_register(luaState, _P'SaveCampaignVar', @lc_savecampaignvar);
 lua_register(luaState, _P'GetCampaignVar', @lc_getcampaignvar);
+lua_register(luaState, _P'SaveMissionVar', @lc_savemissionvar);
+lua_register(luaState, _P'GetMissionVar', @lc_getmissionvar);
 lua_register(luaState, _P'band', @lc_band);
 lua_register(luaState, _P'bor', @lc_bor);
 lua_register(luaState, _P'bnot', @lc_bnot);
--- a/hedgewars/uVariables.pas	Tue Dec 18 14:24:23 2018 +0100
+++ b/hedgewars/uVariables.pas	Tue Dec 18 15:21:23 2018 +0100
@@ -88,6 +88,7 @@
 
     CheckSum        : LongWord;
     CampaignVariable: shortstring;
+    MissionVariable : shortstring;
     GameTicks       : LongWord;
     OuchTauntTimer  : LongWord; // Timer which blocks sndOuch from being played too often and fast
     GameState       : TGameState;