Skip to content

Commit eedeac5

Browse files
authored
Merge pull request #897 from ajhalme/world-sizes-ui
Add dynamic world size selectors in New Game
2 parents 3f928d5 + d95c906 commit eedeac5

10 files changed

Lines changed: 176 additions & 173 deletions

File tree

C7/Lua/game_modes/base-ruleset.json

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8890,5 +8890,58 @@
88908890
"corruptionPercentage": 100,
88918891
"militaryLaw": 1
88928892
}
8893+
],
8894+
"worldSizes" : [
8895+
{
8896+
"name": "Tiny",
8897+
"length": 80,
8898+
"height": 60,
8899+
"width": 60,
8900+
"optimalNumberOfCities": 14,
8901+
"techRate": 160,
8902+
"distanceBetweenCivs": 11,
8903+
"numberOfCivs": 4
8904+
},
8905+
{
8906+
"name": "Small",
8907+
"length": 80,
8908+
"height": 80,
8909+
"width": 80,
8910+
"optimalNumberOfCities": 17,
8911+
"techRate": 200,
8912+
"distanceBetweenCivs": 11,
8913+
"numberOfCivs": 6
8914+
},
8915+
{
8916+
"name": "Standard",
8917+
"length": 80,
8918+
"height": 100,
8919+
"width": 100,
8920+
"optimalNumberOfCities": 20,
8921+
"techRate": 240,
8922+
"distanceBetweenCivs": 12,
8923+
"numberOfCivs": 8,
8924+
"isDefault" : true
8925+
},
8926+
{
8927+
"name": "Large",
8928+
"length": 80,
8929+
"height": 130,
8930+
"width": 130,
8931+
"optimalNumberOfCities": 28,
8932+
"techRate": 320,
8933+
"distanceBetweenCivs": 18,
8934+
"numberOfCivs": 12
8935+
},
8936+
{
8937+
"name": "Huge",
8938+
"length": 80,
8939+
"height": 160,
8940+
"width": 160,
8941+
"optimalNumberOfCities": 36,
8942+
"techRate": 400,
8943+
"distanceBetweenCivs": 24,
8944+
"numberOfCivs": 16
8945+
}
88938946
]
88948947
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
uid://dfc5qvfimbmfk

C7/UIElements/NewGame/PlayerSetup.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ public partial class PlayerSetup : Control {
3636
Civilization selectedCivilization;
3737
Difficulty selectedDifficulty;
3838

39-
// TODO: read this from the rules based on the world size
40-
const int NUM_OPPONENTS = 7;
41-
4239
// Called when the node enters the scene tree for the first time.
4340
public override void _Ready() {
4441
GlobalSingleton global = GetNode<GlobalSingleton>("/root/GlobalSingleton");
@@ -75,7 +72,7 @@ public override void _Ready() {
7572
DisplaySelectedLeader();
7673

7774
// Set up the options for opponents.
78-
AddOpponentSelectors();
75+
AddOpponentSelectors(global.WorldCharacteristics.worldSize.numberOfCivs);
7976

8077
// Set up the difficulty buttons
8178
difficultyContainer.Columns = save.Difficulties.Count;
@@ -106,9 +103,10 @@ private void BackToMainMenu() {
106103
GetTree().ChangeSceneToFile("res://UIElements/MainMenu/main_menu.tscn");
107104
}
108105

109-
private void AddOpponentSelectors() {
110-
// TODO: The number of opponents should come from the rule set.
111-
for (int i = 0; i < NUM_OPPONENTS; ++i) {
106+
private void AddOpponentSelectors(int numberOfCivs) {
107+
int numOpponents = numberOfCivs - 1;
108+
109+
for (int i = 0; i < numOpponents; ++i) {
112110
OptionButton optionButton = new();
113111
StyleBoxFlat styleBox = new() {
114112
BorderColor = Color.Color8(150, 150, 150, 220),
@@ -136,7 +134,7 @@ private void AddOpponentSelectors() {
136134
opponentListContainer.AddChild(container);
137135
container.AddChild(optionButton);
138136

139-
container.CustomMinimumSize = new Vector2(312.0f / opponentListContainer.Columns, 315.0f / NUM_OPPONENTS);
137+
container.CustomMinimumSize = new Vector2(312.0f / opponentListContainer.Columns, 315.0f / numOpponents);
140138
optionButton.CustomMinimumSize = new Vector2(290.0f / opponentListContainer.Columns, optionButton.CustomMinimumSize.Y);
141139

142140
foreach (Civilization civ in save.Civilizations) {

C7/UIElements/NewGame/WorldSetup.cs

Lines changed: 62 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
using System;
33
using C7Engine;
44
using C7Engine.Lua;
5+
using C7GameData;
56
using C7GameData.Save;
67
using Serilog;
78

89
[Tool]
910
public partial class WorldSetup : Control {
10-
private static ILogger log = LogManager.ForContext<WorldSetup>();
11+
private ILogger log = LogManager.ForContext<WorldSetup>();
1112

1213
[Export] TextureRect background;
1314

@@ -57,24 +58,25 @@ public partial class WorldSetup : Control {
5758
[Export] TextureRect billion4Large;
5859
[Export] TextureRect billion5Large;
5960

60-
[Export] CheckButton tinySize;
61-
[Export] CheckButton smallSize;
62-
[Export] CheckButton standardSize;
63-
[Export] CheckButton largeSize;
64-
[Export] CheckButton hugeSize;
65-
[Export] CheckButton randomSize;
66-
6761
[Export] TextureButton confirm;
6862
[Export] TextureButton cancel;
6963

7064
[Export] LineEdit seedInput;
7165

66+
[Export] VBoxContainer worldSizeButtonsContainer;
67+
7268
WorldCharacteristics.Landform landform = WorldCharacteristics.Landform.Pangaea;
7369
WorldCharacteristics.OceanCoverage ocean = WorldCharacteristics.OceanCoverage.Percent_70;
7470
WorldCharacteristics.Age age = WorldCharacteristics.Age.Billion_4;
7571
WorldCharacteristics.Temperature temp = WorldCharacteristics.Temperature.Temperate;
7672
WorldCharacteristics.Climate clim = WorldCharacteristics.Climate.Normal;
7773

74+
private WorldSize _worldSize = WorldSize.Generic();
75+
76+
private int GameSeed => int.Parse(seedInput.Text);
77+
78+
private SaveGame _saveGame;
79+
7880
// Called when the node enters the scene tree for the first time.
7981
public override void _Ready() {
8082
background.Texture = TextureLoader.Load("world_setup.background");
@@ -266,14 +268,54 @@ public override void _Ready() {
266268
billion4Large.Visible = true;
267269
billion4.ButtonPressed = true;
268270

269-
// TODO: handle different map sizes properly (including loading the
270-
// optimal city number, etc)
271-
tinySize.Visible = false;
272-
smallSize.Visible = false;
273-
standardSize.Visible = false;
274-
largeSize.Visible = false;
275-
hugeSize.Visible = false;
276-
randomSize.Visible = false;
271+
_saveGame = GameModeLoader.Load(GamePaths.GameModesDir, GamePaths.GameMode);
272+
273+
InitMapSizes();
274+
}
275+
276+
private void InitMapSizes() {
277+
var sizeRandom = new Random();
278+
279+
var worldSizeButtonGroup = new ButtonGroup() { ResourceName = "WorldSizeButtonGroup" };
280+
var randomSizeButton = new Civ3MenuButton
281+
{
282+
Text = "Random",
283+
textPosition = Civ3MenuButton.TextPosition.TextRightOfIcon,
284+
FontSize = 0,
285+
ButtonGroup = worldSizeButtonGroup,
286+
ToggleMode = true
287+
};
288+
289+
randomSizeButton.Pressed += () => {
290+
var wss = _saveGame?.WorldSizes ?? [];
291+
_worldSize = wss.Count > 0 ? wss[sizeRandom.Next(wss.Count)] : WorldSize.Generic();
292+
};
293+
294+
try {
295+
// Dynamically create a new button for each world size in the game
296+
foreach (var ws in _saveGame.WorldSizes) {
297+
var worldSizeButton = new Civ3MenuButton
298+
{
299+
Text = ws.name,
300+
textPosition = Civ3MenuButton.TextPosition.TextRightOfIcon,
301+
FontSize = 0,
302+
ButtonGroup = worldSizeButtonGroup,
303+
ToggleMode = true,
304+
ButtonPressed = ws.isDefault
305+
};
306+
worldSizeButton.Pressed += () => _worldSize = ws;
307+
worldSizeButtonsContainer.AddChild(worldSizeButton);
308+
309+
// apply the default immediately (last isDefault=true wins)
310+
if (ws.isDefault)
311+
_worldSize = ws;
312+
}
313+
314+
// Move random as last in the list and drop default map option and
315+
worldSizeButtonsContainer.AddChild(randomSizeButton);
316+
} catch (Exception ex) {
317+
log.Warning(ex, "Failed to load map sizes from game mode.");
318+
}
277319
}
278320

279321
private void ResetLandformGraphics() {
@@ -313,26 +355,18 @@ private void ResetAgeGraphics() {
313355
private void CreateGame() {
314356
GlobalSingleton Global = GetNode<GlobalSingleton>("/root/GlobalSingleton");
315357
Global.ResetLoadGameFields();
316-
SaveGame save = GameModeLoader.Load(GamePaths.GameModesDir, GamePaths.GameMode);
317358

318-
Global.WorldCharacteristics = new WorldCharacteristics(save) {
359+
Global.WorldCharacteristics = new WorldCharacteristics(_saveGame) {
319360
landform = landform,
320361
oceanCoverage = ocean,
321362
age = age,
322363
climate = clim,
323364
temperature = temp,
324-
worldSize = new WorldSize() {
325-
width = 100,
326-
height = 100,
327-
numberOfCivs = 8,
328-
distanceBetweenCivs = 12,
329-
techRate = 240,
330-
optimalNumberOfCities = 20,
331-
},
332-
mapSeed = Int32.Parse(seedInput.Text),
365+
worldSize = _worldSize,
366+
mapSeed = GameSeed,
333367
};
334368

335-
Global.SaveGame = save;
369+
Global.SaveGame = _saveGame;
336370

337371
GetTree().ChangeSceneToFile("res://UIElements/NewGame/player_setup.tscn");
338372
}

C7/UIElements/NewGame/player_setup.tscn

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ script = ExtResource("1")
3030
background = NodePath("Background")
3131
playerListContainer = NodePath("Background/CenterContainer/PlayerListContainer")
3232
civLabel = NodePath("Background/CivLabel")
33-
opponentListContainer = NodePath("Background/OpponentListContainer")
33+
opponentListContainer = NodePath("Background/OpponentListScroller/OpponentListContainer")
3434
difficultyContainer = NodePath("Background/DifficultyContainer")
3535
confirm = NodePath("Background/Confirm")
3636
cancel = NodePath("Background/Cancel")
@@ -83,13 +83,16 @@ theme_override_font_sizes/font_size = 15
8383
text = "DIFFICULTY"
8484
vertical_alignment = 1
8585

86-
[node name="OpponentListContainer" type="GridContainer" parent="Background"]
86+
[node name="OpponentListScroller" type="ScrollContainer" parent="Background"]
8787
layout_mode = 0
88-
offset_left = 636.0
89-
offset_top = 123.0
90-
offset_right = 948.0
88+
offset_left = 630.0
89+
offset_top = 120.0
90+
offset_right = 951.0
9191
offset_bottom = 458.0
9292

93+
[node name="OpponentListContainer" type="GridContainer" parent="Background/OpponentListScroller"]
94+
layout_mode = 2
95+
9396
[node name="CenterContainer" type="CenterContainer" parent="Background"]
9497
layout_mode = 0
9598
offset_left = 72.0

0 commit comments

Comments
 (0)