Skip to content

Commit

Permalink
Added ability to find all floors within a matrix that have been visit…
Browse files Browse the repository at this point in the history
…ed. Resolved a complexity issue.
  • Loading branch information
ben_singer committed Nov 29, 2024
1 parent 2e82ad0 commit 77b2827
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 26 deletions.
27 changes: 27 additions & 0 deletions NetAF.Tests/Assets/Locations/Matrix_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using NetAF.Assets;
using NetAF.Serialization.Assets;

namespace NetAF.Tests.Assets.Locations
{
Expand Down Expand Up @@ -150,5 +151,31 @@ public void Given2Point1UnitApart_WhenDistanceBetweenPoints_Then1()

Assert.AreEqual(1, (int)result);
}

[TestMethod]
public void GivenVisitedRoomOn1And1VisitedRoomOn3_When_ThenReturnContaining1And3()
{
List<RoomPosition> roomPositions =
[
new(new(string.Empty, string.Empty), new Point3D(0, 0, 0)),
new(new(string.Empty, string.Empty), new Point3D(0, 1, 0)),
new(new(string.Empty, string.Empty), new Point3D(0, 1, 1)),
new(new(string.Empty, string.Empty), new Point3D(0, 1, 2)),
new(new(string.Empty, string.Empty), new Point3D(0, 1, 3))
];

var serialization = RoomSerialization.FromRoom(roomPositions[2].Room);
serialization.HasBeenVisited = true;
roomPositions[2].Room.RestoreFrom(serialization);
roomPositions[4].Room.RestoreFrom(serialization);

var matrix = new Matrix([.. roomPositions]);

var result = matrix.FindAllZWithVisitedRooms();

Assert.AreEqual(2, result.Length);
Assert.AreEqual(1, result[0]);
Assert.AreEqual(3, result[1]);
}
}
}
73 changes: 48 additions & 25 deletions NetAF/Assets/ExaminableObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,8 @@ public class ExaminableObject : IExaminable
if (request.Examinable.Description != null)
description.Append(request.Examinable.Description.GetDescription());

if (request.Examinable.Commands?.Any() ?? false)
{
if (description.Length > 0)
description.Append(" ");

description.Append($"{Environment.NewLine}{Environment.NewLine}{request.Examinable.Identifier.Name} provides the following commands: ");

for (int i = 0; i < request.Examinable.Commands.Length; i++)
{
CustomCommand customCommand = request.Examinable.Commands[i];
description.Append($"{Environment.NewLine}\"{customCommand.Help.Command}\" - {customCommand.Help.Description.RemoveSentenceEnd()}, ");
}

if (description.ToString().EndsWith(", "))
{
description.Remove(description.Length - 2, 2);
description.EnsureFinishedSentence();
}
}

if (description.Length == 0)
description.Append(request.Examinable.Identifier.Name);

if (description.Length == 0)
description.Append(request.Examinable.GetType().Name);
AddCommandsToDescription(request, ref description);
EnsureAtleastABasicDescription(request, ref description);

if (request.Examinable.Attributes.Count > 0)
description.Append($"\n\n{StringUtilities.ConstructAttributesAsString(request.Examinable.Attributes.GetAsDictionary())}");
Expand All @@ -69,6 +46,52 @@ public class ExaminableObject : IExaminable

#endregion

#region StaticMethods

/// <summary>
/// Ensure that at least a basic description has been added.
/// </summary>
/// <param name="request">The examination request.</param>
/// <param name="description">The current description.</param>
private static void EnsureAtleastABasicDescription(ExaminationRequest request, ref StringBuilder description)
{
if (description.Length == 0)
description.Append(request.Examinable.Identifier.Name);

if (description.Length == 0)
description.Append(request.Examinable.GetType().Name);
}

/// <summary>
/// Add any commands to the description.
/// </summary>
/// <param name="request">The examination request.</param>
/// <param name="description">The current description.</param>
private static void AddCommandsToDescription(ExaminationRequest request, ref StringBuilder description)
{
if (request.Examinable.Commands == null || request.Examinable.Commands.Length == 0)
return;

if (description.Length > 0)
description.Append(' ');

description.Append($"{Environment.NewLine}{Environment.NewLine}{request.Examinable.Identifier.Name} provides the following commands: ");

for (int i = 0; i < request.Examinable.Commands.Length; i++)
{
CustomCommand customCommand = request.Examinable.Commands[i];
description.Append($"{Environment.NewLine}\"{customCommand.Help.Command}\" - {customCommand.Help.Description.RemoveSentenceEnd()}, ");
}

if (description.ToString().EndsWith(", "))
{
description.Remove(description.Length - 2, 2);
description.EnsureFinishedSentence();
}
}

#endregion

#region Implementation of IExaminable

/// <summary>
Expand Down
24 changes: 24 additions & 0 deletions NetAF/Assets/Locations/Matrix.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace NetAF.Assets.Locations
Expand Down Expand Up @@ -88,6 +89,29 @@ public double DistanceBetweenRooms(Room a, Room b)
return DistanceBetweenPoints(aPos.Value, bPos.Value);
}

/// <summary>
/// Find all Z that have visited rooms.
/// </summary>
/// <returns>An array containing all Z with visited rooms.</returns>
public int[] FindAllZWithVisitedRooms()
{
List<int> floors = [];

for (var floor = 0; floor < Depth; floor++)
{
foreach (var room in FindAllRoomsOnZ(floor))
{
if (room.HasBeenVisited)
{
floors.Add(floor);
continue;
}
}
}

return [.. floors];
}

#endregion

#region StaticMethods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ public void BuildRegionMap(Region region, Point2D startPosition, Point3D focusPo
var rooms = matrix.ToRooms().Where(r => r != null).ToArray();
var unvisitedRoomPositions = rooms.Select(region.GetPositionOfRoom).Where(r => !r.Room.HasBeenVisited).ToList();
var visitedRoomPositions = rooms.Select(region.GetPositionOfRoom).Where(r => r.Room.HasBeenVisited).ToList();
var multiLevel = matrix.Depth > 1;
var multiLevel = matrix.Depth > 1 && (region.IsVisibleWithoutDiscovery || matrix.FindAllZWithVisitedRooms().Length > 1);
var indicatorLength = 3 + matrix.Depth.ToString().Length;
var maxAvailableWidth = maxSize.Width;
var x = startPosition.X;
Expand Down

0 comments on commit 77b2827

Please sign in to comment.