Skip to content

Commit 829b3a9

Browse files
committed
unit and tile IDs, having issues with updating save json when changing format
1 parent 93f6b66 commit 829b3a9

File tree

18 files changed

+110
-107
lines changed

18 files changed

+110
-107
lines changed

C7/AnimationTracker.cs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
using System.Threading;
55
using System.Linq;
66
using C7GameData;
7+
<<<<<<< HEAD
8+
=======
9+
using C7Engine;
10+
using Godot;
11+
>>>>>>> 7b18d7e (unit and tile IDs, having issues with updating save json when changing format)
712

813
public partial class AnimationTracker {
914
private AnimationManager civ3AnimData;
@@ -21,18 +26,11 @@ public struct ActiveAnimation {
2126
public C7Animation anim;
2227
}
2328

24-
public Dictionary<string, ActiveAnimation> activeAnims = new Dictionary<string, ActiveAnimation>();
29+
private Dictionary<ID, ActiveAnimation> activeAnims = new Dictionary<ID, ActiveAnimation>();
2530

2631
public long getCurrentTimeMS() => DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
2732

28-
private string getTileID(Tile tile)
29-
{
30-
// Generate a string to ID this tile that won't conflict with the unit GUIDs. TODO: Eventually we'll implement a common way of ID'ing
31-
// all game objects. Use that here instead.
32-
return String.Format("Tile.{0}.{1}", tile.xCoordinate, tile.yCoordinate);
33-
}
34-
35-
private void startAnimation(string id, C7Animation anim, AutoResetEvent completionEvent, AnimationEnding ending)
33+
private void startAnimation(ID id, C7Animation anim, AutoResetEvent completionEvent, AnimationEnding ending)
3634
{
3735
long currentTimeMS = getCurrentTimeMS();
3836
long animDurationMS = (long)(1000.0 * anim.getDuration());
@@ -55,30 +53,30 @@ private void startAnimation(string id, C7Animation anim, AutoResetEvent completi
5553

5654
public void startAnimation(MapUnit unit, MapUnit.AnimatedAction action, AutoResetEvent completionEvent, AnimationEnding ending)
5755
{
58-
startAnimation(unit.guid, civ3AnimData.forUnit(unit.unitType, action), completionEvent, ending);
56+
startAnimation(unit.id, civ3AnimData.forUnit(unit.unitType, action), completionEvent, ending);
5957
}
6058

6159
public void startAnimation(Tile tile, AnimatedEffect effect, AutoResetEvent completionEvent, AnimationEnding ending)
6260
{
63-
startAnimation(getTileID(tile), civ3AnimData.forEffect(effect), completionEvent, ending);
61+
startAnimation(tile.id, civ3AnimData.forEffect(effect), completionEvent, ending);
6462
}
6563

6664
public void endAnimation(MapUnit unit)
6765
{
6866
ActiveAnimation aa;
69-
if (activeAnims.TryGetValue(unit.guid, out aa)) {
67+
if (activeAnims.TryGetValue(unit.id, out aa)) {
7068
if (aa.completionEvent != null)
7169
aa.completionEvent.Set();
72-
activeAnims.Remove(unit.guid);
70+
activeAnims.Remove(unit.id);
7371
}
7472
}
7573

7674
public bool hasCurrentAction(MapUnit unit)
7775
{
78-
return activeAnims.ContainsKey(unit.guid);
76+
return activeAnims.ContainsKey(unit.id);
7977
}
8078

81-
public (MapUnit.AnimatedAction, float) getCurrentActionAndProgress(string id)
79+
public (MapUnit.AnimatedAction, float) getCurrentActionAndProgress(ID id)
8280
{
8381
ActiveAnimation aa = activeAnims[id];
8482

@@ -97,18 +95,18 @@ public bool hasCurrentAction(MapUnit unit)
9795

9896
public (MapUnit.AnimatedAction, float) getCurrentActionAndProgress(MapUnit unit)
9997
{
100-
return getCurrentActionAndProgress(unit.guid);
98+
return getCurrentActionAndProgress(unit.id);
10199
}
102100

103101
public (MapUnit.AnimatedAction, float) getCurrentActionAndProgress(Tile tile)
104102
{
105-
return getCurrentActionAndProgress(getTileID(tile));
103+
return getCurrentActionAndProgress(tile.id);
106104
}
107105

108106
public void update()
109107
{
110-
long currentTimeMS = !endAllImmediately ? getCurrentTimeMS() : long.MaxValue;
111-
var keysToRemove = new List<string>();
108+
long currentTimeMS = (! endAllImmediately) ? getCurrentTimeMS() : long.MaxValue;
109+
List<ID> keysToRemove = new List<ID>();
112110
foreach (var guidAAPair in activeAnims.Where(guidAAPair => guidAAPair.Value.endTimeMS <= currentTimeMS)) {
113111
var (id, aa) = (guidAAPair.Key, guidAAPair.Value);
114112
if (aa.completionEvent is not null) {
@@ -155,6 +153,6 @@ public MapUnit.Appearance getUnitAppearance(MapUnit unit)
155153
public C7Animation getTileEffect(Tile tile)
156154
{
157155
ActiveAnimation aa;
158-
return activeAnims.TryGetValue(getTileID(tile), out aa) ? aa.anim : null;
156+
return activeAnims.TryGetValue(tile.id, out aa) ? aa.anim : null;
159157
}
160158
}

C7/Game.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public void processEngineMessages(GameData gameData) {
121121
while (EngineStorage.messagesToUI.TryDequeue(out msg)) {
122122
switch (msg) {
123123
case MsgStartUnitAnimation mSUA:
124-
MapUnit unit = gameData.GetUnit(mSUA.unitGUID);
124+
MapUnit unit = gameData.GetUnit(mSUA.unitID);
125125
if (unit != null && (controller.tileKnowledge.isTileKnown(unit.location) || controller.tileKnowledge.isTileKnown(unit.previousLocation))) {
126126
// TODO: This needs to be extended so that the player is shown when AIs found cities, when they move units
127127
// (optionally, depending on preferences) and generalized so that modders can specify whether custom
@@ -333,7 +333,7 @@ public override void _UnhandledInput(InputEvent @event) {
333333
using (var gameDataAccess = new UIGameDataAccess()) {
334334
var tile = mapView.tileAt(gameDataAccess.gameData.map, globalMousePosition);
335335
if (tile != null) {
336-
new MsgSetUnitPath(CurrentlySelectedUnit.guid, tile).send();
336+
new MsgSetUnitPath(CurrentlySelectedUnit.id, tile).send();
337337
}
338338
}
339339
} else {
@@ -441,7 +441,7 @@ private void processActions() {
441441
moveUnit = false;
442442
}
443443
if (moveUnit) {
444-
new MsgMoveUnit(CurrentlySelectedUnit.guid, dir).send();
444+
new MsgMoveUnit(CurrentlySelectedUnit.id, dir).send();
445445
setSelectedUnit(CurrentlySelectedUnit); //also triggers updating the lower-left info box
446446
}
447447
}
@@ -468,18 +468,18 @@ private void processActions() {
468468

469469
// actions with unit buttons
470470
if (Input.IsActionJustPressed(C7Action.UnitHold)) {
471-
new MsgSkipUnitTurn(CurrentlySelectedUnit.guid).send();
471+
new MsgSkipUnitTurn(CurrentlySelectedUnit.id).send();
472472
}
473473

474474
if (Input.IsActionJustPressed(C7Action.UnitWait)) {
475475
using (var gameDataAccess = new UIGameDataAccess()) {
476-
UnitInteractions.waitUnit(gameDataAccess.gameData, CurrentlySelectedUnit.guid);
476+
UnitInteractions.waitUnit(gameDataAccess.gameData, CurrentlySelectedUnit.id);
477477
GetNextAutoselectedUnit(gameDataAccess.gameData);
478478
}
479479
}
480480

481481
if (Input.IsActionJustPressed(C7Action.UnitFortify)) {
482-
new MsgSetFortification(CurrentlySelectedUnit.guid, true).send();
482+
new MsgSetFortification(CurrentlySelectedUnit.id, true).send();
483483
}
484484

485485
if (Input.IsActionJustPressed(C7Action.UnitDisband)) {
@@ -508,7 +508,7 @@ private void processActions() {
508508

509509
if (Input.IsActionJustPressed(C7Action.UnitBuildCity) && CurrentlySelectedUnit.canBuildCity()) {
510510
using (var gameDataAccess = new UIGameDataAccess()) {
511-
MapUnit currentUnit = gameDataAccess.gameData.GetUnit(CurrentlySelectedUnit.guid);
511+
MapUnit currentUnit = gameDataAccess.gameData.GetUnit(CurrentlySelectedUnit.id);
512512
log.Debug(currentUnit.Describe());
513513
if (currentUnit.canBuildCity()) {
514514
PopupOverlay popupOverlay = GetNode<PopupOverlay>(PopupOverlay.NodePath);
@@ -519,7 +519,7 @@ private void processActions() {
519519
}
520520

521521
if (Input.IsActionJustPressed(C7Action.UnitBuildRoad) && CurrentlySelectedUnit.canBuildRoad()) {
522-
new MsgBuildRoad(CurrentlySelectedUnit.guid).send();
522+
new MsgBuildRoad(CurrentlySelectedUnit.id).send();
523523
}
524524
}
525525

@@ -541,7 +541,7 @@ private void _on_SlideToggle_toggled(bool buttonPressed) {
541541

542542
// Called by the disband popup
543543
private void OnUnitDisbanded() {
544-
new MsgDisbandUnit(CurrentlySelectedUnit.guid).send();
544+
new MsgDisbandUnit(CurrentlySelectedUnit.id).send();
545545
}
546546

547547
/**
@@ -555,6 +555,6 @@ public override void _Notification(int what) {
555555
}
556556

557557
private void OnBuildCity(string name) {
558-
new MsgBuildCity(CurrentlySelectedUnit.guid, name).send();
558+
new MsgBuildCity(CurrentlySelectedUnit.id, name).send();
559559
}
560560
}

C7/UIElements/RightClickMenu.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,11 @@ public RightClickTileMenu(Game game, Tile tile) : base(game) {
100100
ResetItems(tile);
101101
}
102102

103-
private bool isUnitFortified(MapUnit unit, Dictionary<string, bool> uiStates) {
104-
if (uiStates is null || !uiStates.ContainsKey(unit.guid)) {
103+
private bool isUnitFortified(MapUnit unit, Dictionary<ID, bool> uiStates) {
104+
if (uiStates is null || !uiStates.ContainsKey(unit.id)) {
105105
return unit.isFortified;
106106
}
107-
return uiStates[unit.guid];
107+
return uiStates[unit.id];
108108
}
109109

110110
private string getUnitAction(MapUnit unit, bool isFortified) {
@@ -118,7 +118,7 @@ private string getUnitAction(MapUnit unit, bool isFortified) {
118118
// and false if they were selected in the previous action. This is to update the UI
119119
// since the actions update the engine asynchronously and otherwise the UI may not
120120
// reflect these changes immediately.
121-
public void ResetItems(Tile tile, Dictionary<string, bool> uiUpdatedUnitStates = null) {
121+
public void ResetItems(Tile tile, Dictionary<ID, bool> uiUpdatedUnitStates = null) {
122122
RemoveAll();
123123

124124
int fortifiedCount = 0;
@@ -128,7 +128,7 @@ public void ResetItems(Tile tile, Dictionary<string, bool> uiUpdatedUnitStates =
128128
bool isFortified = isUnitFortified(unit, uiUpdatedUnitStates);
129129
fortifiedCount += isFortified ? 1 : 0;
130130
string actionName = getUnitAction(unit, isFortified);
131-
AddItem($"{actionName} {unit.Describe()}", () => SelectUnit(unit.guid));
131+
AddItem($"{actionName} {unit.Describe()}", () => SelectUnit(unit.id));
132132
}
133133
int unfortifiedCount = units.Count - fortifiedCount;
134134

@@ -140,13 +140,13 @@ public void ResetItems(Tile tile, Dictionary<string, bool> uiUpdatedUnitStates =
140140
}
141141
}
142142

143-
public void SelectUnit(string guid) {
143+
public void SelectUnit(ID id) {
144144
using (var gameDataAccess = new UIGameDataAccess()) {
145-
MapUnit toSelect = gameDataAccess.gameData.mapUnits.Find(u => u.guid == guid);
145+
MapUnit toSelect = gameDataAccess.gameData.mapUnits.Find(u => u.id == id);
146146
if (toSelect != null && toSelect.owner == game.controller) {
147147
game.setSelectedUnit(toSelect);
148-
new MsgSetFortification(toSelect.guid, false).send();
149-
ResetItems(toSelect.location, new Dictionary<string, bool>() { { toSelect.guid, false } });
148+
new MsgSetFortification(toSelect.id, false).send();
149+
ResetItems(toSelect.location, new Dictionary<ID, bool>() { { toSelect.id, false } });
150150
}
151151
}
152152
if (!Input.IsKeyPressed(Godot.Key.Shift)) {
@@ -158,11 +158,11 @@ public void ForAll(int tileX, int tileY, bool isFortify) {
158158
using (var gameDataAccess = new UIGameDataAccess()) {
159159
bool hasSelectedUnit = false;
160160
Tile tile = gameDataAccess.gameData.map.tileAt(tileX, tileY);
161-
Dictionary<string, bool> modified = new Dictionary<string, bool>();
161+
Dictionary<ID, bool> modified = new Dictionary<ID, bool>();
162162
foreach (MapUnit unit in tile.unitsOnTile) {
163163
if (unit.isFortified != isFortify) {
164-
modified[unit.guid] = isFortify;
165-
new MsgSetFortification(unit.guid, isFortify).send();
164+
modified[unit.id] = isFortify;
165+
new MsgSetFortification(unit.id, isFortify).send();
166166

167167
if (!hasSelectedUnit && !isFortify) {
168168
game.setSelectedUnit(unit);

C7Engine/AI/UnitAI/SettlerAI.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public bool PlayTurn(Player player, MapUnit unit) {
1515
switch (settlerAi.goal) {
1616
case SettlerAIData.SettlerGoal.BUILD_CITY:
1717
if (IsInvalidCityLocation(settlerAi.destination)) {
18-
log.Information("Seeking new destination for settler " + unit.guid + "headed to " + settlerAi.destination);
18+
log.Information("Seeking new destination for settler " + unit.id + "headed to " + settlerAi.destination);
1919
PlayerAI.SetAIForUnit(unit, player);
2020
//Make sure we're using the new settler AI going forward, including this turn
2121
settlerAi = (SettlerAIData)unit.currentAIData;
@@ -35,7 +35,7 @@ public bool PlayTurn(Player player, MapUnit unit) {
3535
else {
3636
//If the settler has no destination, then disband rather than crash later.
3737
if (settlerAi.destination == Tile.NONE) {
38-
log.Information("Disbanding settler " + unit.guid + " with no valid destination");
38+
log.Information("Disbanding settler " + unit.id + " with no valid destination");
3939
unit.disband();
4040
return false;
4141
}

0 commit comments

Comments
 (0)