Skip to content

Commit 54ed904

Browse files
authored
Refactor: terrain improvement replacement logic (#844)
Minor readability improvement
1 parent 01d75b4 commit 54ed904

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

C7/Game.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -809,11 +809,11 @@ private void ProcessAction(string currentAction) {
809809
|| terraform == null || !CurrentlySelectedUnit.canPerformTerraformAction(terraform))
810810
return;
811811

812-
TerrainImprovement currentImprovement = CurrentlySelectedUnit.location.overlays.ImprovementAtLayer(terraform);
813-
if (currentImprovement != null && terraform.Improvement.upgradesFrom != currentImprovement) {
812+
TerrainImprovement replacementTarget = CurrentlySelectedUnit.location.overlays.GetReplacementTarget(terraform);
813+
if (replacementTarget != null) {
814814
popupOverlay.ShowPopup(
815815
new ConfirmationPopup(
816-
$"A previous terrain enhancement ({currentImprovement.key.Capitalize()}) will be replaced \nby this operation. Do you wish to continue?",
816+
$"A previous terrain enhancement ({replacementTarget.key.Capitalize()}) will be replaced \nby this operation. Do you wish to continue?",
817817
"Continue.",
818818
"Cancel action.",
819819
() => {

C7Engine/C7GameData/Tile.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -772,8 +772,19 @@ public TerrainImprovement ImprovementAtLayer(TerrainImprovement.Layer layer) {
772772
return ti;
773773
}
774774

775-
public TerrainImprovement ImprovementAtLayer(Terraform terraform) {
776-
return terraform.Improvement == null ? null : ImprovementAtLayer(terraform.Improvement.layer);
775+
// Returns an existing improvement that would be replaced by the given terraform.
776+
// Returns null if there is no such improvement,
777+
// or the new improvement upgrades from the existing one (upgrades don't count as replacements)
778+
public TerrainImprovement GetReplacementTarget(Terraform terraform) {
779+
var newImp = terraform.Improvement;
780+
if (newImp == null)
781+
return null;
782+
783+
var current = ImprovementAtLayer(newImp.layer);
784+
if (current == null)
785+
return null;
786+
787+
return newImp.upgradesFrom != current ? current : null;
777788
}
778789

779790
public bool HasImprovement(TerrainImprovement improvement) {

0 commit comments

Comments
 (0)