Skip to content

Commit 1cc3933

Browse files
authored
Merge pull request #852 from zack-the-coding-actuary/cycleCityNames
Fixes issue #424 - City name cycling
2 parents 25be85a + edb57f4 commit 1cc3933

5 files changed

Lines changed: 23 additions & 35 deletions

File tree

C7/Text/c7-static-map-save-standalone.json

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71322,7 +71322,6 @@
7132271322
"hasPlayedCurrentTurn": false,
7132371323
"defeated": false,
7132471324
"civilization": "A Barbarian Chiefdom",
71325-
"cityNameIndex": 0,
7132671325
"turnsUntilPriorityReevaluation": 0,
7132771326
"luxuryRate": 0,
7132871327
"scienceRate": 5,
@@ -71340,7 +71339,6 @@
7134071339
"hasPlayedCurrentTurn": false,
7134171340
"defeated": false,
7134271341
"civilization": "Netherlands",
71343-
"cityNameIndex": 0,
7134471342
"tileKnowledge": [
7134571343
{
7134671344
"x": 57,
@@ -71417,7 +71415,6 @@
7141771415
"hasPlayedCurrentTurn": false,
7141871416
"defeated": false,
7141971417
"civilization": "America",
71420-
"cityNameIndex": 0,
7142171418
"tileKnowledge": [
7142271419
{
7142371420
"x": 64,
@@ -71494,7 +71491,6 @@
7149471491
"hasPlayedCurrentTurn": false,
7149571492
"defeated": false,
7149671493
"civilization": "Aztecs",
71497-
"cityNameIndex": 0,
7149871494
"tileKnowledge": [
7149971495
{
7150071496
"x": 59,
@@ -71567,7 +71563,6 @@
7156771563
"hasPlayedCurrentTurn": false,
7156871564
"defeated": false,
7156971565
"civilization": "Inca",
71570-
"cityNameIndex": 0,
7157171566
"tileKnowledge": [
7157271567
{
7157371568
"x": 76,
@@ -71644,7 +71639,6 @@
7164471639
"hasPlayedCurrentTurn": false,
7164571640
"defeated": false,
7164671641
"civilization": "Persia",
71647-
"cityNameIndex": 0,
7164871642
"tileKnowledge": [
7164971643
{
7165071644
"x": 30,
@@ -71725,7 +71719,6 @@
7172571719
"hasPlayedCurrentTurn": false,
7172671720
"defeated": false,
7172771721
"civilization": "Maya",
71728-
"cityNameIndex": 0,
7172971722
"tileKnowledge": [
7173071723
{
7173171724
"x": 9,
@@ -71802,7 +71795,6 @@
7180271795
"hasPlayedCurrentTurn": false,
7180371796
"defeated": false,
7180471797
"civilization": "Iroquois",
71805-
"cityNameIndex": 0,
7180671798
"tileKnowledge": [
7180771799
{
7180871800
"x": 46,

C7/Text/c7-static-map-save.json

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73895,7 +73895,6 @@
7389573895
"hasPlayedCurrentTurn": false,
7389673896
"defeated": false,
7389773897
"civilization": "A Barbarian Chiefdom",
73898-
"cityNameIndex": 0,
7389973898
"turnsUntilPriorityReevaluation": 0,
7390073899
"luxuryRate": 0,
7390173900
"scienceRate": 5,
@@ -73913,7 +73912,6 @@
7391373912
"hasPlayedCurrentTurn": false,
7391473913
"defeated": false,
7391573914
"civilization": "Netherlands",
73916-
"cityNameIndex": 0,
7391773915
"tileKnowledge": [
7391873916
{
7391973917
"x": 57,
@@ -73990,7 +73988,6 @@
7399073988
"hasPlayedCurrentTurn": false,
7399173989
"defeated": false,
7399273990
"civilization": "America",
73993-
"cityNameIndex": 0,
7399473991
"tileKnowledge": [
7399573992
{
7399673993
"x": 64,
@@ -74067,7 +74064,6 @@
7406774064
"hasPlayedCurrentTurn": false,
7406874065
"defeated": false,
7406974066
"civilization": "Aztecs",
74070-
"cityNameIndex": 0,
7407174067
"tileKnowledge": [
7407274068
{
7407374069
"x": 59,
@@ -74140,7 +74136,6 @@
7414074136
"hasPlayedCurrentTurn": false,
7414174137
"defeated": false,
7414274138
"civilization": "Inca",
74143-
"cityNameIndex": 0,
7414474139
"tileKnowledge": [
7414574140
{
7414674141
"x": 76,
@@ -74217,7 +74212,6 @@
7421774212
"hasPlayedCurrentTurn": false,
7421874213
"defeated": false,
7421974214
"civilization": "Persia",
74220-
"cityNameIndex": 0,
7422174215
"tileKnowledge": [
7422274216
{
7422374217
"x": 30,
@@ -74298,7 +74292,6 @@
7429874292
"hasPlayedCurrentTurn": false,
7429974293
"defeated": false,
7430074294
"civilization": "Maya",
74301-
"cityNameIndex": 0,
7430274295
"tileKnowledge": [
7430374296
{
7430474297
"x": 9,
@@ -74375,7 +74368,6 @@
7437574368
"hasPlayedCurrentTurn": false,
7437674369
"defeated": false,
7437774370
"civilization": "Iroquois",
74378-
"cityNameIndex": 0,
7437974371
"tileKnowledge": [
7438074372
{
7438174373
"x": 46,

C7Engine/C7GameData/ImportCiv3.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,6 @@ private void ImportBicLeaders() {
406406
save.Players.Add(MakeSavePlayerFromCiv(save.Civilizations[i],
407407
isBarbarian: i == 0,
408408
isHuman: false,
409-
cityNameIndex: 0,
410409
era: ""));
411410

412411
// Set a government for players not associated with LEAD.
@@ -465,7 +464,6 @@ private void ImportSavLeaders() {
465464
SavePlayer player = MakeSavePlayerFromCiv(civ,
466465
isBarbarian: i == 0,
467466
isHuman: i == 1,
468-
cityNameIndex: leader.FoundedCities,
469467
era: theBiq.Eras[leader.Era].CivilopediaEntry);
470468

471469
// Record what the player is currently researching.
@@ -769,7 +767,7 @@ private void ImportSavLeaders() {
769767
}
770768
}
771769

772-
private SavePlayer MakeSavePlayerFromCiv(Civilization civ, bool isBarbarian, bool isHuman, int cityNameIndex, string era) {
770+
private SavePlayer MakeSavePlayerFromCiv(Civilization civ, bool isBarbarian, bool isHuman, string era) {
773771
return new SavePlayer {
774772
id = ids.CreateID("player"),
775773
colorIndex = civ.colorIndex,
@@ -779,7 +777,6 @@ private SavePlayer MakeSavePlayerFromCiv(Civilization civ, bool isBarbarian, boo
779777
// Never let barbarians play before a real player.
780778
hasPlayedCurrentTurn = isBarbarian,
781779

782-
cityNameIndex = cityNameIndex,
783780
eraCivilopediaName = era,
784781
};
785782
}

C7Engine/C7GameData/Player.cs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public class Player {
2323
public bool defeated = false;
2424

2525
public Civilization civilization;
26-
internal int cityNameIndex = 0;
2726

2827
public List<MapUnit> units = new List<MapUnit>();
2928
public List<City> cities = new List<City>();
@@ -157,18 +156,29 @@ public void AddTechItemToResearchQueue(Tech tech) {
157156
ResearchQueue.Enqueue(tech);
158157
}
159158

160-
public string GetNextCityName() {
161-
string name = civilization.cityNames[cityNameIndex % civilization.cityNames.Count];
162-
int bonusLoops = cityNameIndex / civilization.cityNames.Count;
163-
if (bonusLoops % 2 == 1) {
164-
name = "New " + name;
165-
}
166-
int suffix = (bonusLoops / 2) + 1;
167-
if (suffix > 1) {
168-
name = name + " " + suffix; //e.g. for bonusLoops = 2, we'll have "Athens 2"
159+
private IEnumerable<string> CityNameGenerator() {
160+
List<string> cityNames = civilization.cityNames;
161+
int loopCounter = 0;
162+
163+
// Perpetual generator expression to yield all city names lazily
164+
while (true) {
165+
foreach (string city in cityNames) {
166+
if (loopCounter == 0) yield return city;
167+
else if (loopCounter == 1) yield return $"New {city}";
168+
else {
169+
if (loopCounter % 2 == 0) yield return $"{city} {(loopCounter / 2) + 1}";
170+
else yield return $"New {city} {(loopCounter / 2) + 1}";
171+
}
172+
}
173+
loopCounter++;
169174
}
170-
cityNameIndex++;
171-
return name;
175+
}
176+
177+
public string GetNextCityName() {
178+
// Convert to hashset for faster lookups
179+
HashSet<string> cityNameHashSet = cities.Select(city => city.name).ToHashSet();
180+
181+
return CityNameGenerator().First(x => !cityNameHashSet.Contains(x));
172182
}
173183

174184
public Player() {

C7Engine/C7GameData/Save/SavePlayer.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ public class SavePlayer {
1111
public bool defeated = false;
1212

1313
public string civilization;
14-
public int cityNameIndex = 0;
1514

1615
public List<TileLocation> tileKnowledge = new List<TileLocation>();
1716

@@ -68,7 +67,6 @@ public Player ToPlayer(GameMap map, List<Civilization> civilizations, List<Gover
6867
defeated = defeated,
6968
colorIndex = colorIndex,
7069
civilization = civilization is not null ? civilizations.Find(civ => civ.name == civilization) : null,
71-
cityNameIndex = cityNameIndex,
7270
knownTechs = knownTechs,
7371
eraCivilopediaName = eraCivilopediaName,
7472
luxuryRate = luxuryRate,
@@ -118,7 +116,6 @@ public SavePlayer(Player player) {
118116
civilization = player.civilization?.name;
119117
// TODO: this should be computed by looking at cities defined in the save
120118
// so that adding cities in the save structure doesn't require updating this value
121-
cityNameIndex = player.cityNameIndex;
122119
tileKnowledge = player.tileKnowledge.AllKnownTiles().ConvertAll(tile => new TileLocation(tile));
123120
turnsUntilPriorityReevaluation = player.turnsUntilPriorityReevaluation;
124121
knownTechs = player.knownTechs;

0 commit comments

Comments
 (0)