From bd60335b14942b693f14d524167e39bd35c26240 Mon Sep 17 00:00:00 2001
From: Tim Felgentreff <timfelgentreff@gmail.com>
Date: Thu, 3 Mar 2022 22:43:07 +0100
Subject: [PATCH] make changes work with tolua++, trying to be backwards
 compatible

---
 src/include/fow.h         |  2 +-
 src/include/net_message.h | 38 ++++++++++++++++++++++++++++++++++++++
 src/include/settings.h    |  3 ++-
 src/tolua/game.pkg        | 39 ++++++++++++++++++++++++++++++++++-----
 src/tolua/map.pkg         |  2 +-
 src/tolua/network.pkg     | 34 ++++++++++++++++++++++------------
 src/tolua/player.pkg      |  2 +-
 7 files changed, 99 insertions(+), 21 deletions(-)

diff --git a/src/include/fow.h b/src/include/fow.h
index 82835dda3..a1f1f335f 100644
--- a/src/include/fow.h
+++ b/src/include/fow.h
@@ -44,7 +44,7 @@
 /*----------------------------------------------------------------------------
 --  Declarations
 ----------------------------------------------------------------------------*/
-ENUM_CLASS FogOfWarTypes { cTiled, cEnhanced, cTiledLegacy, cNumOfTypes };  /// Types of the fog of war
+enum class FogOfWarTypes { cTiled, cEnhanced, cTiledLegacy, cNumOfTypes };  /// Types of the fog of war
 class CFogOfWar
 {
 public:
diff --git a/src/include/net_message.h b/src/include/net_message.h
index 2d75569fb..ed3e21c36 100644
--- a/src/include/net_message.h
+++ b/src/include/net_message.h
@@ -74,6 +74,16 @@ ENUM_CLASS SlotOption : uint8_t {
 	Closed
 };
 
+#if USING_TOLUAPP
+class ServerSetupStateRacesArray {
+public:
+	ServerSetupStateRacesArray() : p(nullptr) {}
+	int8_t& operator[](int idx) { return p[idx].Race; }
+	int8_t& operator[](int idx) const { return p[idx].Race; }
+	SettingsPresets *p;
+};
+#endif
+
 /**
 **  Multiplayer game setup menu state
 */
@@ -93,6 +103,34 @@ public:
 	SlotOption CompOpt[PlayerMax];    /// Free slot option selection  {"Available", "Computer", "Closed" }
 	uint8_t Ready[PlayerMax];      /// Client ready state
 	// Fill in here...
+
+#if USING_TOLUAPP
+	// TODO: can be removed once tolua++ is gone
+	inline char get_ResourcesOption() { return (char)ServerGameSettings.Resources; }
+	inline char get_UnitsOption() { return (char)ServerGameSettings.NumUnits; }
+	inline char get_FogOfWar() { return (char)!ServerGameSettings.NoFogOfWar; }
+	inline char get_Inside() { return (char)ServerGameSettings.Inside; }
+	inline char get_RevealMap() { return (char)ServerGameSettings.RevealMap; }
+	inline char get_GameTypeOption() { return (char)ServerGameSettings.GameType; }
+	inline char get_Difficulty() { return (char)ServerGameSettings.Difficulty; }
+	inline char get_Opponents() { return (char)ServerGameSettings.Opponents; }
+	inline char set_ResourcesOption(char v) { return ServerGameSettings.Resources = v; }
+	inline char set_UnitsOption(char v) { return ServerGameSettings.NumUnits = v; }
+	inline char set_FogOfWar(char v) { return ServerGameSettings.NoFogOfWar = !v; }
+	inline char set_Inside(char v) { return ServerGameSettings.Inside = v; }
+	inline char set_RevealMap(char v) { return ServerGameSettings.RevealMap = (MapRevealModes)v; }
+	inline char set_GameTypeOption(char v) { return ServerGameSettings.GameType = (GameTypes)v; }
+	inline char set_Difficulty(char v) { return ServerGameSettings.Difficulty = v; }
+	inline char set_Opponents(char v) { return ServerGameSettings.Opponents = v; }
+
+	ServerSetupStateRacesArray racesArray;
+	inline ServerSetupStateRacesArray *get_Race() {
+		if (racesArray.p == nullptr) {
+			racesArray.p = ((SettingsPresets*)ServerGameSettings.Presets);
+		}
+		return &racesArray;
+	}
+#endif
 };
 
 /**
diff --git a/src/include/settings.h b/src/include/settings.h
index 347313dd4..794df0ad8 100644
--- a/src/include/settings.h
+++ b/src/include/settings.h
@@ -46,7 +46,8 @@
 /*----------------------------------------------------------------------------
 --  TODO: Remove this once we move off of tolua++ and can properly support strongly typed enums
 ----------------------------------------------------------------------------*/
-#if 1
+#define USING_TOLUAPP 1
+#if USING_TOLUAPP
 // using tolua++
 #define ENUM_CLASS enum
 #else
diff --git a/src/tolua/game.pkg b/src/tolua/game.pkg
index e085a08a8..444c366e6 100644
--- a/src/tolua/game.pkg
+++ b/src/tolua/game.pkg
@@ -49,11 +49,23 @@ struct SettingsPresets {
 	string AIScript;
 	int Race;
 	int Team;
-	int Type;
+	PlayerTypes Type;
+};
+
+enum RevealTypes { 
+	cNoRevelation, 
+	cAllUnits, 
+	cBuildingsOnly 
+};
+
+enum NetGameTypes {
+	SettingsSinglePlayerGame,
+	SettingsMultiPlayerGame,
+	Unset
 };
 
 class Settings {
-	int NetGameType;
+	NetGameTypes NetGameType;
 
 	SettingsPresets Presets[PlayerMax];
 
@@ -61,17 +73,34 @@ class Settings {
 	int NumUnits;
 	int Opponents;
 	int Difficulty;
-	int GameType;
+	GameTypes GameType;
 	bool NoFogOfWar;
 	bool Inside;
-	int RevealMap;
-	int MapRichness;
+	MapRevealModes RevealMap;
 };
 
+$[
+Settings.MapRichness = 0
+$]
+
+
 extern Settings GameSettings;
 
 #define SettingsPresetMapDefault -1
 
+enum MapRevealModes {
+	cHidden,
+	cKnown,
+	cExplored,
+	cNumOfModes
+};
+
+enum FieldOfViewTypes {
+	cShadowCasting,
+	cSimpleRadial,
+	NumOfTypes
+};
+
 enum GameTypes {
 	SettingsGameTypeMapDefault = SettingsPresetMapDefault,
 	SettingsGameTypeMelee = 0,
diff --git a/src/tolua/map.pkg b/src/tolua/map.pkg
index 6f8705f94..98ecd1d06 100644
--- a/src/tolua/map.pkg
+++ b/src/tolua/map.pkg
@@ -8,7 +8,7 @@ class CMapInfo
         string Postamble;
 	int MapWidth;
 	int MapHeight;
-	int PlayerType[PlayerMax];
+	PlayerTypes PlayerType[PlayerMax];
 };
 
 class CTileset
diff --git a/src/tolua/network.pkg b/src/tolua/network.pkg
index 756a130d6..a6ed44171 100644
--- a/src/tolua/network.pkg
+++ b/src/tolua/network.pkg
@@ -14,20 +14,30 @@ int GetNetworkState();
 void NetworkServerResyncClients(void);
 void NetworkDetachFromServer(void);
 
+class ServerSetupStateRacesArray {
+    int& operator[](int idx) { return p[idx].Race; }
+    int& operator[](int idx) const { return p[idx].Race; }
+}
+
 class CServerSetup {
-	unsigned char ResourcesOption;
-	unsigned char UnitsOption;
-	unsigned char FogOfWar;
-	unsigned char Inside;
-	unsigned char RevealMap;
-	unsigned char GameTypeOption;
-	unsigned char Difficulty;
-	unsigned char MapRichness;
-	unsigned char Opponents;
-	unsigned short CompOpt[PlayerMax]; // cannot use char since tolua interpret variable as string else.
-	unsigned short Ready[PlayerMax];   // cannot use char since tolua interpret variable as string else.
-	unsigned short Race[PlayerMax];    // cannot use char since tolua interpret variable as string else.
+	Settings ServerGameSettings;
+	SlotOption CompOpt[PlayerMax];
+	unsigned short Ready[PlayerMax]; // cannot use char since tolua interpret variable as string.
+	tolua_property unsigned char ResourcesOption;
+	tolua_property unsigned char UnitsOption;
+	tolua_property unsigned char FogOfWar;
+	tolua_property unsigned char Inside;
+	tolua_property unsigned char RevealMap;
+	tolua_property unsigned char GameTypeOption;
+	tolua_property unsigned char Difficulty;
+	tolua_property unsigned char Opponents;
+        tolua_property tolua_readonly ServerSetupStateRacesArray *Race;
 };
+
+$[
+CServerSetup.MapRichness = 0
+$]
+
 extern CServerSetup LocalSetupState;
 extern CServerSetup ServerSetupState;
 extern int NetLocalHostsSlot;
diff --git a/src/tolua/player.pkg b/src/tolua/player.pkg
index 7c85b4442..7379c5618 100644
--- a/src/tolua/player.pkg
+++ b/src/tolua/player.pkg
@@ -12,7 +12,7 @@ class CPlayer
 	int Index;
 	string Name;
 
-	int Type;
+	PlayerTypes Type;
 	int Race;
 	string AiName;