Skip to content

Commit

Permalink
333 put vehicles in their own module (#335)
Browse files Browse the repository at this point in the history
* Move Vehicles up a namespace, out of Objects

* Create FrEee.Vehicles project

* Fix a bunch of code broken by moving concrete vehicle type classes to a separate module. There's still some to fix but I think Design and Hull will be need to migrated to the same or another module as well for that.

* Add VehicleFactory

* Reorganize some things

* Add HullFactory

* Add DesignFactory

* Fix a few simple crashes

* Move WeaponPlatform to correct namespace

* Mark vehicles project as private assets

* Fix compile errors in unit tests caused by referencing newly hidden code

* Fix more unit tests

* Fix compile errors in winforms, clean up some stuff
  • Loading branch information
ekolis authored Nov 18, 2024
1 parent f2a1247 commit e01b3f5
Show file tree
Hide file tree
Showing 145 changed files with 1,348 additions and 1,110 deletions.
2 changes: 1 addition & 1 deletion FrEee.Core.Domain/Extensions/ChecksExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public static bool IsMemoryOfKnownObject(this ISpaceObject sobj)
/// <param name="sobj">The space object to check.</param>
/// <param name="emp">The empire to check against.</param>
/// <returns>The visibility level.</returns>
internal static Visibility CheckSpaceObjectVisibility(this ISpaceObject sobj, Empire emp)
public static Visibility CheckSpaceObjectVisibility(this ISpaceObject sobj, Empire emp)
{
bool hasMemory = false;
if (sobj.IsMemory)
Expand Down
13 changes: 10 additions & 3 deletions FrEee.Core.Domain/Extensions/CommonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using FrEee.Objects.LogMessages;
using FrEee.Objects.Space;
using FrEee.Objects.Technology;
using FrEee.Objects.Vehicles;
using FrEee.Modding;
using FrEee.Utility;
using FrEee.Serialization;
Expand All @@ -24,6 +23,8 @@
using FrEee.Modding.Abilities;
using FrEee.Gameplay.Commands;
using FrEee.Gameplay.Commands.Orders;
using FrEee.Vehicles;
using FrEee.Vehicles.Types;

namespace FrEee.Extensions;

Expand Down Expand Up @@ -170,7 +171,13 @@ public static void DealWithMines(this ISpaceObject sobj)
return;

// shuffle up the mines so they hit in a random order
var mines = sector.SpaceObjects.OfType<Mine>().Concat(sector.SpaceObjects.OfType<Fleet>().SelectMany(f => f.LeafVehicles.OfType<Mine>())).Where(m => m.IsHostileTo(sobj.Owner)).Shuffle().ToList();
var mines = sector.SpaceObjects
.OfType<IVehicle>()
.Concat(sector.SpaceObjects.OfType<Fleet>().SelectMany(f =>
f.LeafVehicles.OfType<IVehicle>()))
.Where(m => m.DetonatesWhenEnemiesEnterSector && m.IsHostileTo(sobj.Owner))
.Shuffle()
.ToList();

// for log messages
var totalDamage = 0;
Expand Down Expand Up @@ -231,7 +238,7 @@ public static void DealWithMines(this ISpaceObject sobj)
if (minesDetonated.Any() || minesSwept.Any() || minesAttacking.Any())
owner.Log.Add(sobj.CreateLogMessage(sobj + " encountered a mine field at " + sector + " and took " + totalDamage + " points of damage, sweeping " + minesSwept.Sum(kvp => kvp.Value) + " mines.", LogMessageType.Generic));
foreach (var emp in minesSwept.Keys.Union(minesDetonated.Keys).Union(minesAttacking.Keys))
emp.Log.Add(sobj.CreateLogMessage(sobj + " encountered our mine field at " + sector + ". " + minesDetonated[emp] + " of our mines detonated, " + minesAttacking[emp] + " others fired weapons, and " + minesSwept[emp] + " were swept. " + sector.SpaceObjects.OfType<Mine>().Where(m => m.Owner == emp).Count() + " mines remain in the sector.", LogMessageType.Generic));
emp.Log.Add(sobj.CreateLogMessage(sobj + " encountered our mine field at " + sector + ". " + minesDetonated[emp] + " of our mines detonated, " + minesAttacking[emp] + " others fired weapons, and " + minesSwept[emp] + " were swept. " + sector.SpaceObjects.OfType<IVehicle>().Where(m => m.DetonatesWhenEnemiesEnterSector && m.Owner == emp).Count() + " mines remain in the sector.", LogMessageType.Generic));
}
}

Expand Down
3 changes: 3 additions & 0 deletions FrEee.Core.Domain/FrEee.Core.Domain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
<Compile Remove="Processes\Combat\Simple\Battle.cs" />
<Compile Remove="Gameplay\Commands\EditStrategyCommand.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Vehicles\IHullFactory.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FrEee.Core.Utility\FrEee.Core.Utility.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using FrEee.Objects.Civilization;
using FrEee.Objects.Vehicles;
using FrEee.Vehicles;

namespace FrEee.Gameplay.Commands.Designs;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using FrEee.Objects.Civilization;
using FrEee.Objects.Civilization.Orders;
using FrEee.Objects.GameState;
using FrEee.Objects.Vehicles;
using FrEee.Vehicles;

namespace FrEee.Gameplay.Commands.Designs;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using FrEee.Objects.Civilization;
using FrEee.Objects.Vehicles;
using FrEee.Vehicles;

namespace FrEee.Gameplay.Commands.Designs;

Expand Down
12 changes: 6 additions & 6 deletions FrEee.Core.Domain/Modding/Abilities/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
using FrEee.Objects.Civilization.Diplomacy.Clauses;
using FrEee.Objects.Space;
using FrEee.Objects.Technology;
using FrEee.Objects.Vehicles;
using FrEee.Modding;
using System;
using System.Collections.Generic;
using System.Linq;
using FrEee.Objects.GameState;
using FrEee.Modding.Abilities;
using FrEee.Extensions;
using FrEee.Vehicles;

namespace FrEee.Modding.Abilities;

Expand Down Expand Up @@ -49,7 +49,7 @@ public static ILookup<Ability, Ability> AbilityTree(this IAbilityObject obj, Fun
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static IDictionary<Ability, IAbilityObject> ActivatableAbilities(this Vehicle v)
public static IDictionary<Ability, IAbilityObject> ActivatableAbilities(this IVehicle v)
{
var dict = new Dictionary<Ability, IAbilityObject>();
foreach (var a in v.Hull.Abilities)
Expand Down Expand Up @@ -96,10 +96,10 @@ public static IDictionary<Ability, IAbilityObject> ActivatableAbilities(this Pla
/// <returns></returns>
public static IDictionary<Ability, IAbilityObject> ActivatableAbilities(this IAbilityObject o)
{
if (o is Vehicle)
return ((Vehicle)o).ActivatableAbilities();
if (o is Planet)
return ((Planet)o).ActivatableAbilities();
if (o is IVehicle v)
return v.ActivatableAbilities();
if (o is Planet p)
return p.ActivatableAbilities();

var dict = new Dictionary<Ability, IAbilityObject>();
foreach (var a in o.Abilities())
Expand Down
2 changes: 1 addition & 1 deletion FrEee.Core.Domain/Modding/Loaders/ComponentLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using FrEee.Objects.Vehicles;
using FrEee.Processes.Combat;
using FrEee.Vehicles.Types;

namespace FrEee.Modding.Loaders;

Expand Down
52 changes: 13 additions & 39 deletions FrEee.Core.Domain/Modding/Loaders/HullLoader.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using FrEee.Objects.Technology;
using FrEee.Objects.Vehicles;
using FrEee.Extensions;
using FrEee.Objects.Technology;
using FrEee.Utility;
using FrEee.Vehicles;
using FrEee.Vehicles.Types;
using System;
using System.Collections.Generic;
using System.Linq;

Expand All @@ -22,46 +25,17 @@ public override IEnumerable<IModObject> Load(Mod mod)
{
foreach (var rec in DataFile.Records)
{
IHull<IVehicle> hull;
IHull hull;
var hullname = rec.Get<string>("Name", null);
var hulltype = rec.Get<string>("Vehicle Type", null);
switch (hulltype)
try
{
case "Ship":
hull = new Hull<Ship>();
break;

case "Base":
hull = new Hull<Base>();
break;

case "Fighter":
hull = new Hull<Fighter>();
break;

case "Satellite":
hull = new Hull<Satellite>();
break;

case "Troop":
hull = new Hull<Troop>();
break;

case "Drone":
hull = new Hull<Drone>();
break;

case "Mine":
hull = new Hull<Mine>();
break;

case "Weapon Platform":
hull = new Hull<WeaponPlatform>();
break;

default:
Mod.Errors.Add(new DataParsingException("Invalid vehicle type \"" + hulltype + "\" specified for " + hullname + " hull.", Mod.CurrentFileName, rec));
continue;
hull = DIRoot.Hulls.Build(Parser.ParseEnum<VehicleTypes>(hulltype));
}
catch (InvalidOperationException ex)
{
Mod.Errors.Add(new DataParsingException("Invalid vehicle type \"" + hulltype + "\" specified for " + hullname + " hull.", Mod.CurrentFileName, rec));
continue;
}
hull.TemplateParameters = rec.Parameters;
hull.ModID = rec.Get<string>("ID", hull);
Expand Down
2 changes: 1 addition & 1 deletion FrEee.Core.Domain/Modding/Loaders/MountLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using FrEee.Extensions;
using System.Collections.Generic;
using System.Linq;
using FrEee.Objects.Vehicles;
using FrEee.Processes.Combat;
using FrEee.Vehicles.Types;

namespace FrEee.Modding.Loaders;

Expand Down
1 change: 1 addition & 0 deletions FrEee.Core.Domain/Modding/Mod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using FrEee.Processes.AI;
using FrEee.Modding.Abilities;
using FrEee.Modding.Scripts;
using FrEee.Vehicles;

namespace FrEee.Modding;

Expand Down
2 changes: 1 addition & 1 deletion FrEee.Core.Domain/Modding/Templates/ComponentTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
using System.IO;
using System.Linq;
using FrEee.Objects.GameState;
using FrEee.Objects.Vehicles;
using FrEee.Processes.Combat;
using FrEee.Modding.Abilities;
using FrEee.Vehicles.Types;

namespace FrEee.Modding.Templates;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using FrEee.Objects.Vehicles;
using FrEee.Processes.Combat;
using FrEee.Vehicles.Types;

namespace FrEee.Objects.Civilization.CargoStorage;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
using FrEee.Extensions;
using System;
using System.Collections.Generic;
using FrEee.Objects.Vehicles;
using FrEee.Objects.GameState;
using FrEee.Vehicles;
using FrEee.Vehicles.Types;

namespace FrEee.Objects.Civilization.CargoStorage;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using FrEee.Objects.GameState;
using FrEee.Objects.Space;
using FrEee.Objects.Vehicles;
using FrEee.Vehicles.Types;
using System.Collections.Generic;

namespace FrEee.Objects.Civilization.CargoStorage;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using FrEee.Objects.Space;
using FrEee.Objects.Technology;
using FrEee.Objects.Vehicles;
using FrEee.Modding;
using FrEee.Utility;
using FrEee.Serialization;
Expand All @@ -14,6 +13,8 @@
using FrEee.Objects.GameState;
using FrEee.Modding.Templates;
using FrEee.Modding.Abilities;
using FrEee.Vehicles;
using FrEee.Vehicles.Types;

namespace FrEee.Objects.Civilization.Construction;

Expand Down Expand Up @@ -403,7 +404,7 @@ public bool ExecuteOrders()
Orders.Add(copy);
}
builtThisTurn.Add(order.Item);
if (order.Item is Ship || order.Item is Base)
if (order.Item is IMajorSpaceVehicle)
{
// trigger ship built happiness changes
Owner.TriggerHappinessChange(hm => hm.AnyShipConstructed);
Expand Down
3 changes: 2 additions & 1 deletion FrEee.Core.Domain/Objects/Civilization/Diplomacy/Package.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
using System.Collections.Generic;
using System.Linq;
using Tech = FrEee.Objects.Technology.Technology;
using FrEee.Objects.Vehicles;
using FrEee.Objects.GameState;
using FrEee.Modding;
using FrEee.Vehicles;
using FrEee.Vehicles.Types;

namespace FrEee.Objects.Civilization.Diplomacy;

Expand Down
11 changes: 6 additions & 5 deletions FrEee.Core.Domain/Objects/Civilization/Empire.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using FrEee.Objects.Civilization.Diplomacy.Clauses;
using FrEee.Objects.LogMessages;
using FrEee.Objects.Space;
using FrEee.Objects.Vehicles;
using FrEee.Modding;
using FrEee.Utility;
using FrEee.Serialization;
Expand All @@ -24,6 +23,8 @@
using FrEee.Gameplay.Commands;
using FrEee.Gameplay.Commands.Orders;
using FrEee.Gameplay.Commands.Projects;
using FrEee.Vehicles;
using FrEee.Vehicles.Types;

namespace FrEee.Objects.Civilization;

Expand Down Expand Up @@ -62,7 +63,7 @@ public Empire()
/// <summary>
/// The current empire being controlled by the player.
/// </summary>
public static Empire Current
public static Empire? Current
{
get
{
Expand Down Expand Up @@ -383,8 +384,8 @@ public ResourceQuantity Maintenance
{
// shouldn't change except at turn processing...
// TODO - facility/unit maintenance?
if (maintenance == null || Empire.Current == null)
maintenance = OwnedSpaceObjects.OfType<SpaceVehicle>().Sum(v => v.MaintenanceCost);
if (maintenance is null || Current is null)
maintenance = OwnedSpaceObjects.OfType<ISpaceVehicle>().Sum(v => ((IVehicle)v).MaintenanceCost);
return maintenance;
}
}
Expand Down Expand Up @@ -902,7 +903,7 @@ public void Dispose()
Game.Current.UnassignID(this);
if (Game.Current.Empires.Contains(this))
Game.Current.Empires[Game.Current.Empires.IndexOf(this)] = null;
foreach (var x in OwnedSpaceObjects.OfType<SpaceVehicle>().ToArray())
foreach (var x in OwnedSpaceObjects.OfType<ISpaceVehicle>().ToArray())
x.Dispose();
foreach (var x in ColonizedPlanets.ToArray())
x.Colony.Dispose();
Expand Down
19 changes: 19 additions & 0 deletions FrEee.Core.Domain/Objects/Civilization/IHasMaintenanceCost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FrEee.Utility;

namespace FrEee.Objects.Civilization;

/// <summary>
/// Something that has a per-turn maintenance cost.
/// </summary>
public interface IHasMaintenanceCost
{
/// <summary>
/// Cost to maintain this object per turn.
/// </summary>
ResourceQuantity MaintenanceCost { get; }
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using FrEee.Objects.LogMessages;
using FrEee.Objects.Space;
using FrEee.Objects.Vehicles;
using FrEee.Modding;
using FrEee.Serialization;
using FrEee.Extensions;
using System.Collections.Generic;
using System.Linq;
using FrEee.Objects.Technology;
using FrEee.Objects.GameState;
using FrEee.Processes.Combat;
using FrEee.Modding.Abilities;
using FrEee.Vehicles;
using FrEee.Vehicles.Types;

namespace FrEee.Objects.Civilization.Orders;

Expand Down Expand Up @@ -112,15 +112,14 @@ public void Execute(IOrderable ord)
{
executor.SupplyRemaining += Ability.Value1.Value.ToInt();
// TODO - normalize supplies on other stuff, not just space vehicles
if (executor is SpaceVehicle)
(executor as SpaceVehicle).NormalizeSupplies();
if (executor is ISpaceVehicle sv)
sv.NormalizeSupplies();
Owner.RecordLog(executor, executor + " has activated its emergency resupply pod and now has " + executor.SupplyRemaining.ToUnitString(true) + " supplies.", LogMessageType.Generic);
}
else if (Ability.Rule.Matches("Emergency Energy"))
{
if (executor is Vehicle)
if (executor is ISpaceVehicle v)
{
var v = executor as Vehicle;
v.EmergencySpeed += Ability.Value1.Value.ToInt();
Owner.RecordLog(executor, executor + " has activated its emergency propulsion and has had its speed boosted to " + v.StrategicSpeed + " temporarily.", LogMessageType.Generic);
}
Expand Down
Loading

0 comments on commit e01b3f5

Please sign in to comment.