Skip to content

Commit

Permalink
Add tests for various combat scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
QuinnBast committed Sep 3, 2023
1 parent 3d84898 commit 4a0030d
Show file tree
Hide file tree
Showing 15 changed files with 421 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ private void CheckCombat(TimeMachine timeMachine, OnTickEventArgs onTick)
CombatEvent = combat,
});

// Also include any defensive combat events
entityToFight.GetComponent<PositionManager>().OnRegisterCombatEffects?.Invoke(this, new OnRegisterCombatEventArgs()
{
Direction = onTick.Direction,
CurrentState = timeMachine.GetState(),
CombatEvent = combat,
});

timeMachine.AddEvent(combat);
localCombatEvents.Add(combat);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,10 @@ public bool TransferSpecialistsTo(SpecialistManager specialistManager, TimeMachi
/// <param name="specialistIds">List of specialist Ids to transfer</param>
public bool TransferSpecialistsById(SpecialistManager destinationSpecialistManager, List<string> specialistIds, TimeMachine timeMachine)
{
var specialistsMatchingId = _specialists.Where(it => specialistIds.Contains(it.GetId())).ToList();
var specialistsMatchingId = _specialists.Where(it => specialistIds.Contains(it.GetSpecialistId().ToString())).ToList();
if (destinationSpecialistManager.CanAddSpecialists(specialistsMatchingId.Count))
{
_specialists = _specialists.Where(it => !specialistIds.Contains(it.GetId())).ToList();
_specialists = _specialists.Where(it => !specialistIds.Contains(it.GetSpecialistId().ToString())).ToList();
destinationSpecialistManager._specialists.AddRange(specialistsMatchingId);

specialistsMatchingId.ForEach(spec =>
Expand Down
19 changes: 19 additions & 0 deletions Core/SubterfugeCore/Core/Entities/EntityData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Collections.Generic;
using Subterfuge.Remake.Api.Network;
using Subterfuge.Remake.Core.Players;

namespace Subterfuge.Remake.Core.Entities
{
public class EntityData
{
public int DrillerCount { get; set; } = 0;
public int ShieldCount { get; set; } = 0;
public Player Owner { get; set; } = null;
public List<SpecialistTypeId> Specialists
{
get;
set;
} = null;

}
}
8 changes: 4 additions & 4 deletions Core/SubterfugeCore/Core/Entities/Specialists/Heroes/Queen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Subterfuge.Remake.Core.Entities.Specialists.Heroes
{
public class Queen: Specialist
{
private int shieldDelta = 25;
public static int SHIELD_PROVIDED = 25;

public Queen(Player owner) : base(owner, true)
{
Expand All @@ -18,7 +18,7 @@ public override void ArriveAtLocation(IEntity entity, TimeMachine timeMachine)
if (!this._isCaptured)
{
entity.GetComponent<SpecialistManager>().AllowHireFromLocation();
entity.GetComponent<ShieldManager>().AlterShieldCapacity(shieldDelta);
entity.GetComponent<ShieldManager>().AlterShieldCapacity(SHIELD_PROVIDED);
}
}

Expand All @@ -28,7 +28,7 @@ public override void LeaveLocation(IEntity entity, TimeMachine timeMachine)
if (!this._isCaptured)
{
entity.GetComponent<SpecialistManager>().DisallowHireFromLocation();
entity.GetComponent<ShieldManager>().AlterShieldCapacity(shieldDelta * -1);
entity.GetComponent<ShieldManager>().AlterShieldCapacity(SHIELD_PROVIDED * -1);
}
}

Expand All @@ -47,7 +47,7 @@ public override SpecialistTypeId GetSpecialistId()

public override string GetDescription()
{
return $"Adds ${shieldDelta} shields to the Queen's Location. If the Queen dies, the owner is eliminated.";
return $"Adds ${SHIELD_PROVIDED} shields to the Queen's Location. If the Queen dies, the owner is eliminated.";
}
}
}
16 changes: 0 additions & 16 deletions Core/SubterfugeCore/Core/Entities/Specialists/Specialist.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ namespace Subterfuge.Remake.Core.Entities.Specialists
/// </summary>
public abstract class Specialist
{
/// <summary>
/// The name of the specialist
/// </summary>
/// TODO: Generate the id from a known seed.
private readonly string _specialistId = Guid.NewGuid().ToString();

/// <summary>
/// The player who owns the specialist
/// </summary>
Expand Down Expand Up @@ -50,16 +44,6 @@ bool isHero
IsHero = isHero;
}

/// <summary>
/// Returns the specialist id.
/// </summary>
/// <returns>The specialist's id</returns>
public string GetId()
{
// TODO: Do something else here.
return GetSpecialistId().ToString();
}

/// <summary>
/// Gets the owner of the specialist.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ Player owner
return new Smuggler(owner);
case SpecialistTypeId.Veteran:
return new Veteran(owner);
case SpecialistTypeId.Martyr:
return new Martyr(owner);
case SpecialistTypeId.Theif:
return new Theif(owner);
default:
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ private void OnRegisterCombatEffects(object positionManager, OnRegisterCombatEve

registerCombatEventArgs.CombatEvent.AddEffectToCombat(new AlterShieldEffect(
registerCombatEventArgs.CombatEvent,
friendlyEntity,
GetShieldDelta(enemyEntity)
enemyEntity,
GetShieldDelta(enemyEntity) * -1
));

if (GetLevel() >= 2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ public Martyr(Player owner) : base(owner, false)

public override void ArriveAtLocation(IEntity entity, TimeMachine timeMachine)
{
if (_isCaptured)
if (!_isCaptured)
{
entity.GetComponent<PositionManager>().OnRegisterCombatEffects += ExplodeOnCombatEffects;
}
}

public override void LeaveLocation(IEntity entity, TimeMachine timeMachine)
{
if (_isCaptured)
if (!_isCaptured)
{
entity.GetComponent<PositionManager>().OnRegisterCombatEffects -= ExplodeOnCombatEffects;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ private void StealEnemyDrillers(object? combat, OnRegisterCombatEventArgs combat
combatEventArgs.CombatEvent.AddEffectToCombat(new AlterDrillerEffect(
combatEventArgs.CombatEvent,
friendlyEntity,
(int)(enemyCarrier.GetDrillerCount() * _stealPerLevel[_level]),
(int)(enemyCarrier.GetDrillerCount() * _stealPerLevel[_level] * -1)
(int)(enemyCarrier.GetDrillerCount() * _stealPerLevel[_level - 1]),
(int)(enemyCarrier.GetDrillerCount() * _stealPerLevel[_level - 1] * -1)
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ private void AlterDrilleresOnCombat(object sender, OnRegisterCombatEventArgs eve
{
var friendlyEntity = eventArgs.CombatEvent.GetEntityOwnedBy(_owner);
var friendlyDrillers = friendlyEntity.GetComponent<DrillerCarrier>().GetDrillerCount();
var extraDrillers = (int)(ExtraDrillersPerLevel[_level] * friendlyDrillers);
var extraDrillers = (int)(ExtraDrillersPerLevel[_level - 1] * friendlyDrillers);

eventArgs.CombatEvent.AddEffectToCombat(new AlterDrillerEffect(
eventArgs.CombatEvent,
friendlyEntity,
0,
-1 * (drillersPerLevel[_level] + extraDrillers)
-1 * (drillersPerLevel[_level - 1] + extraDrillers)
));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public override bool BackwardAction(TimeMachine timeMachine)
{
_outpost.GetComponent<DrillerCarrier>().AlterDrillers(_drillersTransferred * -1);
_outpost.GetComponent<SpecialistManager>()
.TransferSpecialistsById(_sub.GetComponent<SpecialistManager>(), _specialistsTransferred.Select(it => it.GetId()).ToList(), timeMachine);
.TransferSpecialistsById(_sub.GetComponent<SpecialistManager>(), _specialistsTransferred.Select(it => it.GetSpecialistId().ToString()).ToList(), timeMachine);
timeMachine.GetState().AddSub(_sub);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public override bool BackwardAction(TimeMachine timeMachine)
{
// Put specialists back on the sub.
_outpost.GetComponent<SpecialistManager>()
.TransferSpecialistsById(_sub.GetComponent<SpecialistManager>(), specialistsTransferred.Select(it => it.GetId()).ToList(), timeMachine);
.TransferSpecialistsById(_sub.GetComponent<SpecialistManager>(), specialistsTransferred.Select(it => it.GetSpecialistId().ToString()).ToList(), timeMachine);

// Put drillers back on the sub.
_sub.GetComponent<DrillerCarrier>().AlterDrillers(drillersTransferred);
Expand All @@ -94,7 +94,7 @@ public override bool BackwardAction(TimeMachine timeMachine)
{
// Put specialists back on the sub.
_outpost.GetComponent<SpecialistManager>()
.TransferSpecialistsById(_sub.GetComponent<SpecialistManager>(), specialistsTransferred.Select(it => it.GetId()).ToList(), timeMachine);
.TransferSpecialistsById(_sub.GetComponent<SpecialistManager>(), specialistsTransferred.Select(it => it.GetSpecialistId().ToString()).ToList(), timeMachine);
}
}

Expand All @@ -112,8 +112,8 @@ public override bool WasEventSuccessful()
var outpostDrillerCount = _outpost.GetComponent<DrillerCarrier>().GetDrillerCount();
if (subDrillerCount <= 0 && outpostDrillerCount <= 0)
{
// Attacker wins in the case of a tie
return _sub;
// Defender wins in the case of a tie
return _outpost;
}

if (subDrillerCount <= 0)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using Subterfuge.Remake.Core.Entities;
using Subterfuge.Remake.Core.Entities.Components;
using Subterfuge.Remake.Core.Entities.Positions;
using Subterfuge.Remake.Core.GameEvents.Base;
using Subterfuge.Remake.Core.Timing;

Expand All @@ -13,8 +13,8 @@ public class EntityExplodeEffect : PositionalGameEvent
private IEntity _entityExploding;
private GameState _state;

private List<IEntity> entitiesDestroyed;
private Dictionary<IEntity, EntityData> EntitiesBeforeExplosion;

public EntityExplodeEffect(
GameTick occursAt,
IEntity entityExploding,
Expand All @@ -27,19 +27,26 @@ GameState currentState

public override bool ForwardAction(TimeMachine timeMachine)
{
entitiesDestroyed = _state.EntitesInRange(Constants.BaseSubVisionRadius,
_entityExploding.GetComponent<PositionManager>().CurrentLocation);
EntitiesBeforeExplosion = _state.EntitesInRange(Constants.BaseSubVisionRadius,
_entityExploding.GetComponent<PositionManager>().CurrentLocation)
.ToDictionary(entity => entity, entity => new EntityData()
{
DrillerCount = entity.GetComponent<DrillerCarrier>().GetDrillerCount(),
ShieldCount = entity.GetComponent<ShieldManager>().GetShields(),
Specialists = entity.GetComponent<SpecialistManager>().GetSpecialists().ConvertAll(spec => spec.GetSpecialistId())
});

// Remove all of the entities from the game state.
foreach(IEntity e in entitiesDestroyed)
foreach(KeyValuePair<IEntity, EntityData> e in EntitiesBeforeExplosion)
{
if (e is Sub)
if (e.Key is Sub)
{
timeMachine.GetState().RemoveSub(e as Sub);
timeMachine.GetState().RemoveSub(e.Key as Sub);
}
else
{
e.GetComponent<DrillerCarrier>().Destroy();
e.Key.GetComponent<DrillerCarrier>().SetDrillerCount(0);
e.Key.GetComponent<DrillerCarrier>().Destroy();
}
}

Expand All @@ -48,16 +55,16 @@ public override bool ForwardAction(TimeMachine timeMachine)

public override bool BackwardAction(TimeMachine timeMachine)
{
foreach(IEntity e in entitiesDestroyed)
foreach(KeyValuePair<IEntity, EntityData> e in EntitiesBeforeExplosion)
{
if (e is Sub)
if (e.Key is Sub)
{
timeMachine.GetState().AddSub(e as Sub);
timeMachine.GetState().AddSub(e.Key as Sub);
}
else
{
// TODO: This is not perfect... Need to ensure that we can "redo" a destroy event somehow.
e.GetComponent<DrillerCarrier>().UndoDestroy(10);
e.Key.GetComponent<DrillerCarrier>().UndoDestroy(e.Value.DrillerCount);
e.Key.GetComponent<ShieldManager>().SetShields(e.Value.ShieldCount);
}
}

Expand Down
Loading

0 comments on commit 4a0030d

Please sign in to comment.