Skip to content

Commit

Permalink
Automatically overwrite entity ID for new spawn events
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahStolk committed Oct 15, 2023
1 parent 77f90e1 commit cb89051
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 20 deletions.
38 changes: 30 additions & 8 deletions src/DevilDaggersInfo.Core.Replay/ReplayEventsData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@

namespace DevilDaggersInfo.Core.Replay;

/// <summary>
/// Represents all the events in a replay.
/// <remarks>
/// IMPORTANT: This class generally lets you corrupt its state for the sake of performance and ease of use.
/// <list type="bullet">
/// <item><description>When changing the internal type of a spawn event, be sure to also update the list of entity types using <see cref="ChangeEntityType(int, EntityType)"/>.</description></item>
/// <item><description>When adding or inserting a spawn event, the entity ID is re-calculated and overwritten. TODO: We should probably rewrite the event classes to be mutable structs and exclude EntityId and EntityType from them, then add a new wrapper class containing EntityId, EntityType, and TEventStruct as properties instead.</description></item>
/// </list>
/// </remarks>
/// </summary>
public class ReplayEventsData
{
private readonly List<IEvent> _events = new();
Expand Down Expand Up @@ -31,6 +41,9 @@ public void Clear()

public void AddEvent(IEvent e)
{
if (e is IEntitySpawnEvent spawnEvent)
spawnEvent.EntityId = _entityTypes.Count;

_events.Add(e);
_eventOffsetsPerTick[^1]++;

Expand Down Expand Up @@ -92,22 +105,31 @@ public void InsertEvent(int index, IEvent e)
if (index < 0 || index > _events.Count)
throw new ArgumentOutOfRangeException(nameof(index));

_events.Insert(index, e);
if (e is IEntitySpawnEvent spawnEvent)
{
// TODO: EntityId should be automatically calculated and not taken from the event.
_entityTypes.Insert(spawnEvent.EntityId, spawnEvent.EntityType);

// Increment all entity IDs that are higher than the added entity ID.
int entityId = 1; // Skip 0 as it is always reserved.
for (int i = 0; i < _events.Count; i++)
{
if (i == index)
continue;

if (_events[i] is IEntitySpawnEvent otherSpawnEvent && otherSpawnEvent.EntityId >= spawnEvent.EntityId)
otherSpawnEvent.EntityId++;
{
spawnEvent.EntityId = entityId;
_events.Insert(index, e);
_entityTypes.Insert(entityId, spawnEvent.EntityType);
}
else if (_events[i] is IEntitySpawnEvent otherSpawnEvent)
{
if (i >= index)
otherSpawnEvent.EntityId++;
else
entityId++;
}
}
}
else
{
_events.Insert(index, e);
}

int? containingTick = null;
for (int i = 0; i < _eventOffsetsPerTick.Count; i++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,18 @@ public void AddGemEvent()
Assert.AreEqual(_eventCount + 1, _replay.EventsData.EventOffsetsPerTick[^1]);
}

[TestMethod]
public void AddSpawnEvent()
[DataTestMethod]
[DataRow(0)]
[DataRow(1)]
[DataRow(2)]
[DataRow(3)]
[DataRow(4)]
[DataRow(5)]
[DataRow(6)]
public void AddSpawnEvent(int ignoredEntityId)
{
// TODO: The entity ID should be calculated automatically.
const int entityId = 6;
_replay.EventsData.AddEvent(new ThornSpawnEvent(entityId, -1, default, 0));
// The entity ID should be changed to 6 regardless of the value of ignoredEntityId.
_replay.EventsData.AddEvent(new ThornSpawnEvent(ignoredEntityId, -1, default, 0));

// There should be one new event and one new entity.
Assert.AreEqual(_eventCount + 1, _replay.EventsData.Events.Count);
Expand All @@ -112,7 +118,7 @@ public void AddSpawnEvent()
Assert.AreEqual(_tickCount, _replay.EventsData.EventOffsetsPerTick.Count);

// The new entity should be a Thorn.
Assert.AreEqual(EntityType.Thorn, _replay.EventsData.EntityTypes[entityId]);
Assert.AreEqual(EntityType.Thorn, _replay.EventsData.EntityTypes[6]);

// Original data should be unchanged.
ValidateOriginalEntityTypes();
Expand Down Expand Up @@ -381,10 +387,16 @@ public void InsertGemEvent()
}
}

[TestMethod]
public void InsertSpawnEventAtStart()
[DataTestMethod]
[DataRow(0)]
[DataRow(1)]
[DataRow(2)]
[DataRow(3)]
[DataRow(4)]
public void InsertSpawnEventAtStart(int ignoredEntityId)
{
_replay.EventsData.InsertEvent(0, new ThornSpawnEvent(1, -1, default, 0));
// The entity ID should be changed to 1 regardless of the value of ignoredEntityId.
_replay.EventsData.InsertEvent(0, new ThornSpawnEvent(ignoredEntityId, -1, default, 0));

// There should be one new event and one new entity.
Assert.AreEqual(_eventCount + 1, _replay.EventsData.Events.Count);
Expand Down Expand Up @@ -426,10 +438,16 @@ public void InsertSpawnEventAtStart()
}
}

[TestMethod]
public void InsertSpawnEvent()
[DataTestMethod]
[DataRow(0)]
[DataRow(1)]
[DataRow(2)]
[DataRow(3)]
[DataRow(4)]
public void InsertSpawnEvent(int ignoredEntityId)
{
_replay.EventsData.InsertEvent(10, new ThornSpawnEvent(3, -1, default, 0));
// The entity ID should be changed to 3 regardless of the value of ignoredEntityId.
_replay.EventsData.InsertEvent(10, new ThornSpawnEvent(ignoredEntityId, -1, default, 0));

// There should be one new event and one new entity.
Assert.AreEqual(_eventCount + 1, _replay.EventsData.Events.Count);
Expand Down

0 comments on commit cb89051

Please sign in to comment.