Skip to content
Closed
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
2 changes: 1 addition & 1 deletion samples/FleetBattler/BattleDraw.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal static class BattleDraw {
internal const uint EffectStride = 32;
internal const uint ConstantCount = 20;
internal const int RouteSegments = 48, RouteCapacity = RouteSegments + 1;
internal const int SiteEffectCapacity = 2 * CelestialSystem.BodyCount;
internal const int SiteEffectCapacity = (FleetSimulation.FacilityCount + 2) * CelestialSystem.BodyCount;
private const int OrbitSegments = 72;
internal const int SolarTrackCapacity = (CelestialSystem.BodyCount - CelestialSystem.FrontierBodyCount) * OrbitSegments;

Expand Down
11 changes: 8 additions & 3 deletions samples/FleetBattler/CommandSelection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ internal void Click( FleetSimulation fleet, int ship, bool squad, SelectionMode

internal void Squadron( FleetSimulation fleet, int squad, SelectionMode mode ) {
if ( mode == SelectionMode.Replace ) Clear( );
for ( int i = 0; i < fleet.Count; ++i )
if ( fleet.Alive( i ) && fleet.SideOf( i ) == 0 && fleet.SquadronOf( i ) == squad ) Include( fleet, i, mode );
if ( (uint)squad < (uint)fleet.SquadCount ) {
int id = fleet.FleetId( squad );
for ( int i = 0; i < fleet.Count; ++i )
if ( fleet.Alive( i ) && fleet.SideOf( i ) == 0 && fleet.FleetId( fleet.SquadronOf( i ) ) == id ) Include( fleet, i, mode );
}
Prune( fleet );
}

Expand All @@ -86,7 +89,9 @@ internal static int Hit( FleetSimulation fleet, Vector2 pixel, Matrix4x4 viewPro
float best = MathF.Pow( MathF.Max( 11, dimensions.Y * 0.013f ), 2 );
int result = -1;
for ( int i = 0; i < fleet.Count; ++i ) {
if ( !fleet.Alive( i ) || fleet.SideOf( i ) != faction ) continue;
if ( !fleet.Alive( i ) ) continue;
int side = fleet.SideOf( i );
if ( faction == -2 ? !fleet.IsHostile( (int)FactionId.Sol, side ) : faction >= 0 && side != faction ) continue;
float distance = Vector2.DistanceSquared( pixel, Project( fleet.VisualPosition( i, interpolation ), viewProjection, dimensions ) );
if ( distance >= best ) continue;
best = distance;
Expand Down
24 changes: 24 additions & 0 deletions samples/FleetBattler/Factions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Numerics;

namespace FleetBattler;

internal enum FactionId { Sol, Raiders, Merchants, Rival, Count }
internal enum Disposition { Allied, Neutral, Hostile }

internal static class Factions {
internal static string Name( FactionId id ) => id switch {
FactionId.Sol => "SOL AUTHORITY", FactionId.Raiders => "AMBER RAIDERS",
FactionId.Merchants => "FREE MERCHANTS", _ => "RIVAL COMMAND"
};

internal static Disposition Attitude( FactionId from, FactionId toward ) => from == toward ? Disposition.Allied
: from == FactionId.Raiders || toward == FactionId.Raiders ||
from is FactionId.Sol && toward is FactionId.Rival || from is FactionId.Rival && toward is FactionId.Sol
? Disposition.Hostile : Disposition.Neutral;

internal static Vector4 ShipTint( FactionId id ) => id switch {
FactionId.Sol => new( 0.66f, 1.55f, 2.25f, 1.04f ),
FactionId.Merchants => new( 0.82f, 1.42f, 0.97f, 1 ),
_ => new( 2.9f, 0.89f, 0.37f, 1.11f )
};
}
24 changes: 14 additions & 10 deletions samples/FleetBattler/FleetCombat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ internal sealed partial class FleetSimulation {
private readonly ushort[] _railIncoming;
private readonly int[] _lastHit;
private readonly int[] _nextShot;
private readonly int[] _factionCount = new int[2];
private readonly int[] _factionCount = new int[(int)FactionId.Count];
private readonly CombatEvent[] _events = new CombatEvent[EffectCapacity];
private int _eventCursor, _eventCount;

private readonly record struct CombatEvent( Vector2 From, Vector2 To, int Tick, int Faction, bool Burst, WeaponKind Weapon );

internal int ActiveCount => CyanCount + AmberCount;
internal int ActiveCount => CyanCount + AmberCount + MerchantCount;
internal int CyanCount => _factionCount[0];
internal int AmberCount => _factionCount[1];
internal int AmberCount => _factionCount[(int)FactionId.Raiders] + _factionCount[(int)FactionId.Rival];
internal int MerchantCount => _factionCount[(int)FactionId.Merchants];
internal FactionId FactionOf( int ship ) => (FactionId)SideOf( ship );
internal bool IsHostile( int from, int toward ) => Factions.Attitude( (FactionId)from, (FactionId)toward ) == Disposition.Hostile;
internal int ShotsFired { get; private set; }
internal int ShipsLost { get; private set; }
internal int LastEngagementTick { get; private set; } = -10_000;
Expand All @@ -39,20 +42,20 @@ internal sealed partial class FleetSimulation {

internal void Attack( FleetSelection selection, int target ) {
if ( Frontier && Role( selection.Squad ) != HullRole.Corvette ) return;
if ( !Alive( target ) || _squads[selection.Squad].Faction == SideOf( target ) ) return;
if ( !Alive( target ) || !IsHostile( _squads[selection.Squad].Faction, SideOf( target ) ) ) return;
SetOrder( selection, new Order {
Enemy = target + 1, Target = _positions[target], Detached = selection.IsShip
} );
}

private Vector2 Chase( ref Order order, Vector2 from, int faction, out Vector2 facing ) {
int target = order.Enemy - 1;
if ( !Alive( target ) ) {
if ( !Alive( target ) || !IsHostile( faction, SideOf( target ) ) ) {
int squad = _squadOfShip[target];
var unit = _squads[squad];
target = -1;
for ( int i = unit.First; i < unit.First + unit.Count; ++i ) {
if ( !Alive( i ) ) continue;
if ( !Alive( i ) || !IsHostile( faction, SideOf( i ) ) ) continue;
target = i;
break;
}
Expand Down Expand Up @@ -117,7 +120,7 @@ private void ResolveVolley( ) {
var weapon = Frontier ? design.Weapon : WeaponKind.Pulse;
float range = Frontier ? design.Range + (side == 0 && WeaponUpgrade ? 3 : 0) : side == 0 && WeaponUpgrade ? 19 : 16;
int preferred = (_shipOrders[ship].Detached ? _shipOrders[ship].Enemy : _squads[_squadOfShip[ship]].Order.Enemy) - 1;
int target = Alive( preferred ) && Vector2.DistanceSquared( _positions[preferred], _positions[ship] ) <= range * range
int target = Alive( preferred ) && IsHostile( side, SideOf( preferred ) ) && Vector2.DistanceSquared( _positions[preferred], _positions[ship] ) <= range * range
? preferred : ClosestHostile( ship, side, range );
if ( target < 0 ) continue;
int damage = Frontier ? design.Damage + (side == 0 && WeaponUpgrade ? 4 : 0) : side == 0 ? WeaponUpgrade ? 17 : 13 : 11;
Expand Down Expand Up @@ -163,8 +166,9 @@ internal void ApplyHullDamage( int ship, int damage ) {

private void ClearRetiredOrders( int squad, int first, int count ) {
RefreshSurveys( );
for ( int body = 1; body < _constructionWorkers.Length; ++body )
if ( _constructionWorkers[body]?.Squad == squad ) AdvanceMine( body );
for ( int body = 1; body < _bodies.Length; ++body )
foreach ( var facility in Facilities )
if ( SiteWorker( body, facility )?.Squad == squad ) CancelSite( body, facility );
for ( int i = 0; i < SquadCount; ++i ) ClearTarget( ref _squads[i].Order, _squads[i].Center );
for ( int ship = 0; ship < Count; ++ship ) ClearTarget( ref _shipOrders[ship], _positions[ship] );

Expand All @@ -185,7 +189,7 @@ private int ClosestHostile( int ship, int side, float range ) {
for ( int x = Math.Max( 1, cell.X - reach ); x <= Math.Min( _gridSide - 2, cell.X + reach ); ++x ) {
int other = _grid[y * _gridSide + x];
for ( int visited = 0; other >= 0 && visited < 8; ++visited, other = _next[other] ) {
if ( SideOf( other ) == side ) continue;
if ( !IsHostile( side, SideOf( other ) ) ) continue;
float d = Vector2.DistanceSquared( origin, _positions[other] );
if ( d >= best ) continue;
best = d;
Expand Down
61 changes: 61 additions & 0 deletions samples/FleetBattler/FleetGroups.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
namespace FleetBattler;

internal sealed partial class FleetSimulation {
private int _nextFleetId;
internal int FleetId( int squad ) => _squads[squad].FleetId;
internal bool IsFleetLeader( int squad ) {
if ( !Frontier ) return true;
int id = FleetId( squad );
for ( int i = 0; i < squad; ++i )
if ( ShipCount( i ) > 0 && FleetId( i ) == id ) return false;
return true;
}

internal int FleetStrength( int squad ) {
int id = FleetId( squad ), count = 0;
for ( int i = 0; i < SquadCount; ++i )
if ( FleetId( i ) == id ) count += ShipCount( i );
return count;
}

internal bool CanMergeFleets( ReadOnlySpan<bool> selected ) {
int first = 0;
for ( int i = 0; i < SquadCount; ++i ) {
if ( !SelectedCombatSquad( i, selected ) ) continue;
int id = FleetId( i );
if ( first != 0 && first != id ) return true;
first = id;
}
return false;
}

internal bool MergeFleets( ReadOnlySpan<bool> selected ) {
if ( !Frontier || !CanMergeFleets( selected ) ) return false;
Span<int> groups = stackalloc int[SquadCount];
int count = 0;
for ( int i = 0; i < SquadCount; ++i )
if ( SelectedCombatSquad( i, selected ) ) groups[count++] = FleetId( i );
int target = groups[0];
for ( int i = 0; i < SquadCount; ++i )
if ( ShipCount( i ) > 0 && groups[..count].Contains( FleetId( i ) ) ) _squads[i].FleetId = target;
return true;
}

internal bool CanSplitFleets( ReadOnlySpan<bool> selected ) {
for ( int i = 0; i < SquadCount; ++i )
if ( SelectedCombatSquad( i, selected ) && FleetStrength( i ) > ShipCount( i ) ) return true;
return false;
}

internal bool SplitFleets( ReadOnlySpan<bool> selected ) {
if ( !Frontier || !CanSplitFleets( selected ) ) return false;
for ( int i = 0; i < SquadCount; ++i )
if ( SelectedCombatSquad( i, selected ) && FleetStrength( i ) > ShipCount( i ) )
_squads[i].FleetId = ++_nextFleetId;
return true;
}

private bool SelectedCombatSquad( int squad, ReadOnlySpan<bool> selected ) =>
selected.Length >= Count && IsPlayerSquad( squad ) && ShipCount( squad ) > 0 &&
Role( squad ) == HullRole.Corvette && AnySelectedInSquad( squad, selected );
}
Loading
Loading