Skip to content

Override map gravity mod rule - #3283

Open
lostsquirrel1 wants to merge 3 commits into
masterfrom
BAR106-override_map_gravity
Open

Override map gravity mod rule#3283
lostsquirrel1 wants to merge 3 commits into
masterfrom
BAR106-override_map_gravity

Conversation

@lostsquirrel1

@lostsquirrel1 lostsquirrel1 commented Aug 26, 2026

Copy link
Copy Markdown
Collaborator

A clean way to override map gravity. There's aspects of how gravity is used that the game cannot reach itself. Rather than case down all the corner cases and have override code in Lua, a simple mod rule override could be used to set the gravity without any ambiguity.

Comment thread rts/Sim/Misc/ModInfo.cpp
Comment on lines +204 to +205
overrideMapGravity = system.GetBool("overrideMapGravity", overrideMapGravity);
forcedMapGravityStrength = system.GetFloat("forcedMapGravityStrength", forcedMapGravityStrength);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not have just forcedMapGravityStrength and have it default to the map's gravity if unspecified?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. I will have to check whether the map parameters loaded and available at the time the modrules are initialized.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If modrules are initialized before the map is available then it could also be something like std::optional <float> that is empty if Lua returns nil.

My only concern here is the Lua interface, I don't really mind how the engine resolves things under the hood though.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked the init order: modInfo.Init() runs before mapInfo is constructed (Game.cpp:265 vs 269), so the modrule cannot default to the map value at parse time. The std::optional route works though, since CMapInfo::ReadGlobal already reads modInfo in this PR and can resolve it there. Patch below does that: one system.forcedMapGravityStrength modrule, a number overrides the map gravity, nil keeps the map value. Also adds the doc/pr-changelogs/3283.md entry that PRs are expected to ship now. Both objects compile on mingw, not runtime tested.

diff --git a/doc/pr-changelogs/3283.md b/doc/pr-changelogs/3283.md
new file mode 100644
index 0000000000..3349683a82
--- /dev/null
+++ b/doc/pr-changelogs/3283.md
@@ -0,0 +1 @@
+ * add `system.forcedMapGravityStrength` modrule. If set, it overrides the map's `gravity` (same units, elmo/s^2) everywhere the engine uses it, including `Game.gravity`. Unset (nil) keeps the map value.
diff --git a/rts/Map/MapInfo.cpp b/rts/Map/MapInfo.cpp
index 98530cb27f..6194d30d30 100644
--- a/rts/Map/MapInfo.cpp
+++ b/rts/Map/MapInfo.cpp
@@ -98,7 +98,7 @@ void CMapInfo::ReadGlobal()
 	map.hardness      = topTable.GetFloat("maphardness", 100.0f);
 	map.notDeformable = topTable.GetBool("notDeformable", false);
 
-	map.gravity = (modInfo.overrideMapGravity) ? modInfo.forcedMapGravityStrength : topTable.GetFloat("gravity", CModInfo::DEFAULT_MAP_GRAVITY);
+	map.gravity = modInfo.forcedMapGravityStrength.value_or(topTable.GetFloat("gravity", 130.0f));
 	map.gravity = std::max(0.001f, map.gravity);
 	map.gravity = -map.gravity / (GAME_SPEED * GAME_SPEED);
 
diff --git a/rts/Sim/Misc/ModInfo.cpp b/rts/Sim/Misc/ModInfo.cpp
index 2208b81160..c7bf4e4efc 100644
--- a/rts/Sim/Misc/ModInfo.cpp
+++ b/rts/Sim/Misc/ModInfo.cpp
@@ -138,8 +138,7 @@ void CModInfo::ResetState()
 		qtMaxNodesSearchedRelativeToMapOpenNodes = 0.25;
 
 		enableSmoothMesh = true;
-		overrideMapGravity = false;
-		forcedMapGravityStrength = CModInfo::DEFAULT_MAP_GRAVITY;
+		forcedMapGravityStrength.reset();
 		smoothMeshResDivider = 2;
 		smoothMeshSmoothRadius = 40;
 		quadFieldQuadSizeInElmos = 128;
@@ -201,8 +200,8 @@ void CModInfo::Init(const std::string& modFileName)
 		qtMaxNodesSearchedRelativeToMapOpenNodes = system.GetFloat("qtMaxNodesSearchedRelativeToMapOpenNodes", qtMaxNodesSearchedRelativeToMapOpenNodes);
 
 		enableSmoothMesh = system.GetBool("enableSmoothMesh", enableSmoothMesh);
-		overrideMapGravity = system.GetBool("overrideMapGravity", overrideMapGravity);
-		forcedMapGravityStrength = system.GetFloat("forcedMapGravityStrength", forcedMapGravityStrength);
+		if (system.GetType("forcedMapGravityStrength") == LuaTable::NUMBER)
+			forcedMapGravityStrength = system.GetFloat("forcedMapGravityStrength", 0.0f);
 		smoothMeshResDivider = system.GetInt("smoothMeshResDivider", smoothMeshResDivider);
 		smoothMeshSmoothRadius = system.GetInt("smoothMeshSmoothRadius", smoothMeshSmoothRadius);
 
diff --git a/rts/Sim/Misc/ModInfo.h b/rts/Sim/Misc/ModInfo.h
index 8e5d7b6c89..0c1386e0ba 100644
--- a/rts/Sim/Misc/ModInfo.h
+++ b/rts/Sim/Misc/ModInfo.h
@@ -3,6 +3,7 @@
 #ifndef MOD_INFO_H
 #define MOD_INFO_H
 
+#include <optional>
 #include <string>
 #include "Sim/Misc/Resource.h"
 #include "Sim/Path/PFSTypes.h"
@@ -234,8 +235,8 @@ public:
 
 	bool enableSmoothMesh;
 
-	bool overrideMapGravity; // Enable to override the map gravity, with the value in forcedMapGravityStrength.
-	float forcedMapGravityStrength; // If overrideMapGravity is true, then use this value for the gravity strength. Otherwise, use the map's gravity. Default's to 130.0f, which is the default map gravity.
+	/// If set, overrides the map's gravity (in elmo/s^2, same units as mapinfo `gravity`).
+	std::optional<float> forcedMapGravityStrength;
 
 	/// Reduce the resolution of the smooth mesh by the divider value. Increasing the value reduces
 	/// the accuracy of the smooth mesh, but improves performance. Minimum 1, default 2.
@@ -257,8 +258,6 @@ public:
 
 	// If true, players can select their start position by clicking the map
 	bool useStartPositionSelecter;
-
-	static constexpr float DEFAULT_MAP_GRAVITY = 130.0f;
 };
 
 extern CModInfo modInfo;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, should be in a PR though (can be this one if TK doesn't mind you hijacking it).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah he said he will look at it soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants