Skip to content

Commit

Permalink
Refactor 2024 01 28 (#279)
Browse files Browse the repository at this point in the history
* Fix crash on game start

* Make ObjectLocation a record type

* Fix a few compile warnings

* Convert Vector2 to generic math

* Update nuget packages
  • Loading branch information
ekolis authored Jan 29, 2024
1 parent ad010ba commit c57ef8a
Show file tree
Hide file tree
Showing 31 changed files with 312 additions and 359 deletions.
6 changes: 3 additions & 3 deletions FrEee.Tests/FrEee.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
<ItemGroup>
<ProjectReference Include="..\FrEee.Assets\FrEee.Assets.csproj" />
<ProjectReference Include="..\FrEee\FrEee.csproj" />
<PackageReference Include="nunit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
</ItemGroup>
<ItemGroup>
<Folder Include="Savegame\" />
Expand Down
14 changes: 7 additions & 7 deletions FrEee.Tests/Utility/VectorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ namespace FrEee.Tests.Utility;
public class VectorTest
{
[Test]
public void IntVector2AddLinearGradientEightWay()
public void AddLinearGradientEightWay()
{
var map = new HeatMap();
map.AddLinearGradientEightWay(new IntVector2(0, 0), 10, 10, -1);
map.AddLinearGradientEightWay(new IntVector2(1, 1), 2, 2, -1);
map.AddLinearGradientEightWay(new Vector2<int>(0, 0), 10, 10, -1);
map.AddLinearGradientEightWay(new Vector2<int>(1, 1), 2, 2, -1);
Assert.AreEqual(11, map[0, 0]);
Assert.AreEqual(11, map[1, 1]);
Assert.AreEqual(0, map[99, 99]);
}

[Test]
public void IntVector2InterpolationEightWay()
public void InterpolationEightWay()
{
var v1 = new IntVector2(-1, 3);
var v2 = new IntVector2(6, 4);
var interp = IntVector2.InterpolateEightWay(v1, v2, 3);
var v1 = new Vector2<int>(-1, 3);
var v2 = new Vector2<int>(6, 4);
var interp = Vector2<int>.InterpolateEightWay(v1, v2, 3);
var trip = v2 - v1;
var traveled = interp - v1;
var togo = v2 - interp;
Expand Down
26 changes: 13 additions & 13 deletions FrEee.WinForms/Controls/BattleView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ public Battle Battle
}
}

public IntVector2 ClickLocation { get; private set; }
public Vector2<int> ClickLocation { get; private set; }

/// <summary>
/// The combat sector which is focused.
/// </summary>
public IntVector2 FocusedLocation
public Vector2<int> FocusedLocation
{
get => focusedLocation;
set
Expand Down Expand Up @@ -138,8 +138,8 @@ public bool UseSquares
private Battle battle;
private List<Boom> booms = new List<Boom>();
private bool combatPhase = false;
private IntVector2 focusedLocation;
private SafeDictionary<ICombatant, IntVector2> locations = new SafeDictionary<ICombatant, IntVector2>();
private Vector2<int> focusedLocation;
private SafeDictionary<ICombatant, Vector2<int>> locations = [];
private List<Pewpew> pewpews = new List<Pewpew>();

/// <summary>
Expand Down Expand Up @@ -201,7 +201,7 @@ protected override void OnPaint(PaintEventArgs pe)
var drawx = drawPoint.X;
var drawy = drawPoint.Y;

var pos = new IntVector2(x, y);
var pos = new Vector2<int>(x, y);

// draw image, owner flag, and name of largest space object (if any)
var here = Battle.Combatants.Where(q => locations.Any(w => (w.Key == q || w.Key == Battle.StartCombatants[q.ID]) && w.Value == pos));
Expand Down Expand Up @@ -331,21 +331,21 @@ private void BattleView_SizeChanged(object sender, EventArgs e)
Invalidate();
}

private IntVector2 GetClickPoint(int x, int y)
private Vector2<int> GetClickPoint(int x, int y)
{
if (AutoZoom)
{
var clickx = (x - SectorBorderSize) / (SectorDrawSize + SectorBorderSize) + Battle.UpperLeft[round].X;
var clicky = (y - SectorBorderSize) / (SectorDrawSize + SectorBorderSize) + Battle.UpperLeft[round].Y;
return new IntVector2(clickx, clicky);
return new Vector2<int>(clickx, clicky);
}
else
{
if (FocusedLocation == null)
FocusedLocation = (Battle.LowerRight[round] - Battle.UpperLeft[round]) / 2;
var clickx = (x - Width / 2 - SectorBorderSize) / (SectorDrawSize + SectorBorderSize) + FocusedLocation.X;
var clicky = (y - Height / 2 - SectorBorderSize) / (SectorDrawSize + SectorBorderSize) + FocusedLocation.Y;
return new IntVector2(clickx, clicky);
return new Vector2<int>(clickx, clicky);
}
}

Expand Down Expand Up @@ -463,28 +463,28 @@ private void UpdateData()

private class Boom
{
public Boom(IntVector2 pos, float size)
public Boom(Vector2<int> pos, float size)
{
Position = pos;
Size = size;
}

public IntVector2 Position { get; set; }
public Vector2<int> Position { get; set; }
public float Size { get; set; }
}

private class Pewpew
{
public Pewpew(IntVector2 start, IntVector2 end, bool isHit = true)
public Pewpew(Vector2<int> start, Vector2<int> end, bool isHit = true)
{
Start = start;
End = end;
IsHit = isHit;
}

public IntVector2 End { get; set; }
public Vector2<int> End { get; set; }
public bool IsHit { get; set; }
public IntVector2 Start { get; set; }
public Vector2<int> Start { get; set; }
}

private HashSet<ICombatant> unarmedCombatants { get; } = new HashSet<ICombatant>();
Expand Down
13 changes: 10 additions & 3 deletions FrEee.WinForms/Controls/GamePanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,17 @@ protected override void OnPaint(PaintEventArgs pe)
// http://support.microsoft.com/kb/953934
protected override void OnSizeChanged(EventArgs e)
{
this.BeginInvoke((MethodInvoker)delegate
try
{
base.OnSizeChanged(e);
});
this.BeginInvoke((MethodInvoker)delegate
{
base.OnSizeChanged(e);
});
}
catch
{
// UI must not be set up yet
}
}

private void GamePanel_SizeChanged(object sender, EventArgs e)
Expand Down
10 changes: 5 additions & 5 deletions FrEee.WinForms/FrEee.WinForms.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
</PropertyGroup>
<Import Project="../FrEee.Assets/CommonProjectProperties.xml" />
<ItemGroup>
<PackageReference Include="IronPython" Version="2.7.12" />
<PackageReference Include="Microsoft.AppCenter.Analytics" Version="4.5.1" />
<PackageReference Include="Microsoft.AppCenter.Crashes" Version="4.5.1" />
<PackageReference Include="NAudio" Version="2.1.0" />
<PackageReference Include="IronPython" Version="3.4.1" />
<PackageReference Include="Microsoft.AppCenter.Analytics" Version="5.0.3" />
<PackageReference Include="Microsoft.AppCenter.Crashes" Version="5.0.3" />
<PackageReference Include="NAudio" Version="2.2.1" />
<PackageReference Include="NAudio.Vorbis" Version="1.5.0" />
<PackageReference Include="System.ComponentModel.Composition" Version="6.0.0" />
<PackageReference Include="System.ComponentModel.Composition" Version="8.0.0" />
<ProjectReference Include="..\FrEee.Assets\FrEee.Assets.csproj" />
<ProjectReference Include="..\FrEee\FrEee.csproj" />
</ItemGroup>
Expand Down
10 changes: 5 additions & 5 deletions FrEee/Extensions/AbilityExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ public static IEnumerable<IAbilityObject> Ancestors(this IAbilityObject obj, Fun
/// <returns>true if successful or unnecessary, otherwise false</returns>
public static bool BurnSupplies(this Ability a)
{
if (a.Container is Component)
return (a.Container as Component).BurnSupplies();
if (a.Container is Component comp)
return comp.BurnSupplies();
else
return true; // other ability containers don't use supplies
}
Expand Down Expand Up @@ -261,9 +261,9 @@ private static IEnumerable<Ability> FindSharedAbilities(this IOwnableAbilityObje
var rule = clause.AbilityRule;
if (rule.CanTarget(obj.AbilityTarget))
{
if (rule.CanTarget(AbilityTargets.Sector) && obj is ILocated)
if (rule.CanTarget(AbilityTargets.Sector) && obj is ILocated locObj)
{
var sector = ((ILocated)obj).Sector;
var sector = locObj.Sector;
foreach (var emp in Galaxy.Current.Empires.Where(emp => emp != null))
{
foreach (var abil in sector.EmpireAbilities(emp))
Expand Down Expand Up @@ -307,7 +307,7 @@ private static IEnumerable<Ability> FindSharedAbilities(this IOwnableAbilityObje
/// <param name="index"></param>
/// <param name="filter"></param>
/// <returns></returns>
public static string GetEmpireAbilityValue(this ICommonAbilityObject obj, Empire emp, string name, int index = 1, Func<Ability, bool> filter = null)
public static string? GetEmpireAbilityValue(this ICommonAbilityObject obj, Empire emp, string name, int index = 1, Func<Ability, bool> filter = null)

Check warning on line 310 in FrEee/Extensions/AbilityExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
{
if (obj == null)
return null;
Expand Down
17 changes: 10 additions & 7 deletions FrEee/Extensions/CommonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public static void DealWithMines(this ISpaceObject sobj)
/// <param name="value"></param>
/// <param name=""></param>
/// <returns></returns>
public static T Default<T>(this object value, T def = default, bool throwIfWrongType = false)
public static T? Default<T>(this object value, T? def = default, bool throwIfWrongType = false)
{
if (throwIfWrongType && !(value is T))
throw new InvalidCastException($"Cannot convert {value} to type {typeof(T)}.");
Expand Down Expand Up @@ -701,7 +701,7 @@ public static object Instantiate(this Type type, params object[] args)
if (type.Name == "Battle")
return typeof(SpaceBattle).Instantiate(); // HACK - old savegame compatibility
if (type.GetConstructors().Where(c => c.GetParameters().Length == (args == null ? 0 : args.Length)).Any())
return Activator.CreateInstance(type, args);
return Activator.CreateInstance(type, args) ?? throw new NullReferenceException($"Couldn't create instance of type {type}.");
else
return FormatterServices.GetSafeUninitializedObject(type);
}
Expand Down Expand Up @@ -903,11 +903,10 @@ public static int NormalizeSupplies(this IMobileSpaceObject sobj)
/// <typeparam name="T"></typeparam>
/// <param name="o"></param>
/// <returns></returns>
public static T Parse<T>(this string s)
public static T Parse<T>(this string s, IFormatProvider provider = null)
where T : IParsable<T>
{
var parser = typeof(T).GetMethod("Parse", BindingFlags.Static);
var expr = Expression.Call(parser);
return (T)expr.Method.Invoke(null, new object[] { s });
return T.Parse(s, provider);
}

/// <summary>
Expand Down Expand Up @@ -1054,10 +1053,14 @@ public static string ReadTo(this TextReader r, char c, StringBuilder log)
public static string ReadToEndOfLine(this TextReader r, char c, StringBuilder log)
{
var sb = new StringBuilder();
string data = "";
string? data = "";
do
{
data = r.ReadLine();
if (data is null)
{
throw new Exception($"Found end of text when looking for character '{c}'.");
}
log?.Append(data);
sb.Append(data);
if (data.EndsWith(c.ToString()))
Expand Down
4 changes: 2 additions & 2 deletions FrEee/Extensions/ConversionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public static double AngleTo(this PointF p, PointF target)
/// <typeparam name="T"></typeparam>
/// <param name="o"></param>
/// <returns></returns>
public static T CastTo<T>(this object o, T defaultValue = default(T))
public static T? CastTo<T>(this object o, T? defaultValue = default)
{
return (T)((o ?? defaultValue) ?? default(T));
return (T?)((o ?? defaultValue) ?? default);
}

/// <summary>
Expand Down
11 changes: 6 additions & 5 deletions FrEee/FrEee.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
</PropertyGroup>
<Import Project="../FrEee.Assets/CommonProjectProperties.xml" />
<ItemGroup>
<PackageReference Include="IronPython" Version="2.7.12" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="4.2.0" />
<PackageReference Include="IronPython" Version="3.4.1" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="4.8.0" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="Svg" Version="3.4.3" />
<PackageReference Include="System.ComponentModel.Composition" Version="6.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Svg" Version="3.4.6" />
<PackageReference Include="System.ComponentModel.Composition" Version="8.0.0" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="8.0.0" />
<PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
</ItemGroup>
<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion FrEee/Modding/Templates/ComponentTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ public override int GetHashCode()
return ModID.GetHashCode();
}

public static bool operator ==(ComponentTemplate t1, ComponentTemplate t2)
public static bool operator ==(ComponentTemplate? t1, ComponentTemplate? t2)
{
if (t1 is null && t2 is null)
return true;
Expand Down
2 changes: 1 addition & 1 deletion FrEee/Modding/Templates/GalaxyTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public Galaxy Instantiate(Status status, double desiredProgress, PRNG dice)
sys.Name = unusedNames.PickRandom(dice);
unusedNames.Remove(sys.Name);
NameStellarObjects(sys);
gal.StarSystemLocations.Add(new ObjectLocation<StarSystem> { Location = p.Value, Item = sys });
gal.StarSystemLocations.Add(new(sys, p.Value));
if (status != null)
status.Progress += progressPerStarSystem;
}
Expand Down
8 changes: 4 additions & 4 deletions FrEee/Objects/Civilization/Empire.cs
Original file line number Diff line number Diff line change
Expand Up @@ -835,14 +835,14 @@ public Visibility CheckVisibility(Empire emp)
return Visibility.Unknown;
}

public int CompareTo(Empire other)
public int CompareTo(Empire? other)
{
return Name.CompareTo(other.Name);
return Name.CompareTo(other?.Name);
}

public int CompareTo(object obj)
public int CompareTo(object? obj)
{
return Name.CompareTo(obj.ToString());
return Name.CompareTo(obj?.ToString());
}

/// <summary>
Expand Down
Loading

0 comments on commit c57ef8a

Please sign in to comment.