Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible fix for design duplication bug #199

Merged
merged 1 commit into from
Apr 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Possible fix for design duplication bug
  • Loading branch information
ekolis committed Apr 16, 2022
commit 33bb6cae536aa162a7a2161fc35b4104ad28a4d4
6 changes: 3 additions & 3 deletions FrEee.WinForms/Forms/DesignListForm.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using FrEee.Game.Enumerations;
using FrEee.Game.Enumerations;
using FrEee.Game.Interfaces;
using FrEee.Game.Objects.Civilization;
using FrEee.Game.Objects.Commands;
Expand Down Expand Up @@ -199,7 +199,7 @@ private void btnUpgrade_Click(object sender, EventArgs e)
if (lstDesigns.SelectedItems.Count == 1)
{
var old = (IDesign)lstDesigns.SelectedItems[0].Tag;
var copy = old.IsObsolescent ? old.LatestVersion : CopyDesign(old); // make sure to copy design even if it has no upgradeable parts
var copy = old.Upgrade();
var form = new VehicleDesignForm();
form.Design = copy;
this.ShowChildForm(form);
Expand Down Expand Up @@ -282,4 +282,4 @@ private void btnExportAll_Click(object sender, EventArgs e)
MessageBox.Show($"{designs.Count()} designs exported to library ({count} new).");
}
}
}
}
8 changes: 6 additions & 2 deletions FrEee/Game/Interfaces/IDesign.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using FrEee.Game.Enumerations;
using FrEee.Game.Enumerations;
using FrEee.Game.Objects.Civilization;
using FrEee.Game.Objects.Technology;
using FrEee.Modding.Templates;
Expand Down Expand Up @@ -137,10 +137,14 @@ public interface IDesign : INamed, IPictorial, IOwnableAbilityObject, IConstruct
ICreateDesignCommand CreateCreationCommand();

IVehicle Instantiate();

IDesign Upgrade();
}

public interface IDesign<out T> : IDesign, IPictorial, IReferrable, IUpgradeable<IDesign<T>> where T : IVehicle
{
new T Instantiate();

new IDesign<T> Upgrade();
}
}
}
67 changes: 38 additions & 29 deletions FrEee/Game/Objects/Vehicles/Design.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,41 +308,47 @@ public bool IsObsolescent
public int Iteration { get; set; }

/// <summary>
/// An upgraded version of this design, or this design if it cannot be upgraded.
/// TODO - don't create upgraded design if it already exists, just return it?
/// The latest iteration of this design.
/// </summary>
public IDesign<T> LatestVersion
{
get
{
if (IsObsolescent)
{
var copy = this.CopyAndAssignNewID();
copy.Hull = Hull.LatestVersion;
copy.TurnNumber = Galaxy.Current.TurnNumber;
copy.Owner = Empire.Current;
copy.Iteration = Empire.Current.KnownDesigns.OwnedBy(Empire.Current).Where(x => x.BaseName == BaseName && x.IsUnlocked()).MaxOrDefault(x => x.Iteration) + 1; // auto assign nex available iteration
copy.VehiclesBuilt = 0;
copy.IsObsolete = false;

// use real component templates and mounts from mod, not copies!
copy.Components.Clear();
foreach (var mct in Components)
{
// reuse templates so components appear "condensed" on vehicle designer
var mount = mct.Mount == null ? null : mct.Mount.LatestVersion;
var ct = mct.ComponentTemplate.LatestVersion;
var same = copy.Components.FirstOrDefault(x => x.ComponentTemplate == ct && x.Mount == mount);
if (same == null)
copy.Components.Add(new MountedComponentTemplate(copy, ct, mount));
else
copy.Components.Add(same);
}
return copy;
}
return Owner.KnownDesigns.OfType<IDesign<T>>().Where(q =>
q.Owner == Owner
&& q.Name == Name
).MaxBy(q => q.Iteration) ?? this;
}
}

/// <summary>
/// Creates an upgraded version of this design, with the latest upgraded components, etc.
/// </summary>
/// <returns></returns>
public IDesign<T> Upgrade()
{
var copy = this.CopyAndAssignNewID();
copy.Hull = Hull.LatestVersion;
copy.TurnNumber = Galaxy.Current.TurnNumber;
copy.Owner = Empire.Current;
copy.Iteration = Empire.Current.KnownDesigns.OwnedBy(Empire.Current).Where(x => x.BaseName == BaseName && x.IsUnlocked()).MaxOrDefault(x => x.Iteration) + 1; // auto assign nex available iteration
copy.VehiclesBuilt = 0;
copy.IsObsolete = false;

// use real component templates and mounts from mod, not copies!
copy.Components.Clear();
foreach (var mct in Components)
{
// reuse templates so components appear "condensed" on vehicle designer
var mount = mct.Mount == null ? null : mct.Mount.LatestVersion;
var ct = mct.ComponentTemplate.LatestVersion;
var same = copy.Components.FirstOrDefault(x => x.ComponentTemplate == ct && x.Mount == mount);
if (same == null)
copy.Components.Add(new MountedComponentTemplate(copy, ct, mount));
else
return this;
copy.Components.Add(same);
}
return copy;
}

public ResourceQuantity MaintenanceCost
Expand Down Expand Up @@ -850,5 +856,8 @@ private IEnumerable<string> GetPaths(string pathtype)
}
return paths;
}

IDesign IDesign.Upgrade()
=> Upgrade();
}
}
}
4 changes: 2 additions & 2 deletions FrEee/Utility/Extensions/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -352,4 +352,4 @@ public static IEnumerable<T> UnionSingle<T>(this T t1, T t2)
return list.Where(x => x.Value.SafeEquals(min)).Select(x => x.Item);
}
}
}
}