From 913904f13ec4594eb64d3bba138ad43d12fe9f70 Mon Sep 17 00:00:00 2001 From: Jens Steenmetz Date: Tue, 25 Jun 2024 21:19:38 +0200 Subject: [PATCH 1/2] test: fix connected components test --- .../ConnectedComponentsTestScene.unity | 4 +- .../AplibTests/ConnectedComponentsTests.cs | 153 +++++++++--------- 2 files changed, 77 insertions(+), 80 deletions(-) diff --git a/aplib.net-demo/Assets/Scenes/TestScenes/ConnectedComponentsTestScene.unity b/aplib.net-demo/Assets/Scenes/TestScenes/ConnectedComponentsTestScene.unity index 7820b6b7..6b0970ab 100644 --- a/aplib.net-demo/Assets/Scenes/TestScenes/ConnectedComponentsTestScene.unity +++ b/aplib.net-demo/Assets/Scenes/TestScenes/ConnectedComponentsTestScene.unity @@ -20,7 +20,7 @@ RenderSettings: m_FogDensity: 0.01 m_LinearFogStart: 0 m_LinearFogEnd: 300 - m_AmbientSkyColor: {r: 0.21176471, g: 0.23394229, b: 0.25882354, a: 1} + m_AmbientSkyColor: {r: 1, g: 1, b: 1, a: 1} m_AmbientEquatorColor: {r: 0.114, g: 0.125, b: 0.133, a: 1} m_AmbientGroundColor: {r: 0.047, g: 0.043, b: 0.035, a: 1} m_AmbientIntensity: 1 @@ -838,6 +838,7 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: maxAmmoCount: 0 + _currentAmmoCount: 0 --- !u!114 &1220979210 MonoBehaviour: m_ObjectHideFlags: 0 @@ -851,7 +852,6 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: emptyInventoryImage: {fileID: 2800000, guid: 8d003bcfe1c8c044893bf31bdf72fba7, type: 3} - inventoryObject: {fileID: 1220979207} _inventorySize: 4 --- !u!114 &1220979211 MonoBehaviour: diff --git a/aplib.net-demo/Assets/Testing/AplibTests/ConnectedComponentsTests.cs b/aplib.net-demo/Assets/Testing/AplibTests/ConnectedComponentsTests.cs index fada142a..8d728121 100644 --- a/aplib.net-demo/Assets/Testing/AplibTests/ConnectedComponentsTests.cs +++ b/aplib.net-demo/Assets/Testing/AplibTests/ConnectedComponentsTests.cs @@ -1,35 +1,84 @@ using Aplib.Core; -using Aplib.Core.Agents; using Aplib.Core.Belief.Beliefs; using Aplib.Core.Belief.BeliefSets; -using Aplib.Core.Desire.DesireSets; -using Aplib.Core.Desire.Goals; -using Aplib.Core.Desire.GoalStructures; -using Aplib.Core.Intent.Actions; -using Aplib.Core.Intent.Tactics; using Aplib.Integrations.Unity; -using Aplib.Integrations.Unity.Actions; using LevelGeneration; using NUnit.Framework; using System.Collections; +using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.TestTools; +using static Aplib.Core.Combinators; +using Action = Aplib.Core.Intent.Actions.Action; +using BdiAgent = Aplib.Core.Agents.BdiAgent; +using DesireSet = Aplib.Core.Desire.DesireSets.DesireSet; +using Goal = Aplib.Core.Desire.Goals.Goal; +using PrimitiveTactic = Aplib.Core.Intent.Tactics.PrimitiveTactic; +using Tactic = Aplib.Core.Intent.Tactics.Tactic; +using TransformPathfinderAction = Aplib.Integrations.Unity.Actions.TransformPathfinderAction; namespace Testing.AplibTests { - public class ConnectedComponentsBeliefSet : BeliefSet + public class ConnectedComponentsBeliefSet : IBeliefSet { - public Belief PlayerRigidbody = new( - GameObject.Find("Player"), - x => x.GetComponent()); + public Rigidbody PlayerRigidbody { get; } = GameObject.Find("Player").GetComponent(); + + public Belief, Vector3> GetCurrentTarget; + + public Queue TargetPositionsInConnectedComponents { get; } = new(GetTargetPositionsInConnectedComponents()); + + public Queue TeleporterPositions { get; } = new(GetTeleporterLandingPoints()); + + public ConnectedComponentsBeliefSet() + { + GetCurrentTarget = new( + reference: TargetPositionsInConnectedComponents, + getObservationFromReference: positions => positions.Peek(), + shouldUpdate: () => + { + if (TargetPositionsInConnectedComponents.Count == 0) return false; + + if (Vector3.Distance(PlayerRigidbody.position, TargetPositionsInConnectedComponents.Peek()) > 0.4f) + return false; + + Debug.Log($"Reached target at {TargetPositionsInConnectedComponents.Peek()}"); + TargetPositionsInConnectedComponents.Dequeue(); + Debug.Log($"Remaining targets: {TargetPositionsInConnectedComponents.Count}"); + + return TargetPositionsInConnectedComponents.Count > 0; + } + ); + } + + public void UpdateBeliefs() + { + GetCurrentTarget.UpdateBelief(); + } + + private static IEnumerable GetTargetPositionsInConnectedComponents() + { + GameObject gridGameObject = GameObject.Find("LevelGeneration"); + LevelGenerationPipeline levelGenerationPipeline = gridGameObject.GetComponent(); + SpawningExtensions spawningExtensions = gridGameObject.GetComponent(); + + Vector3 centreOfCellHeightOffset = Vector3.up * 1.5f; + + return levelGenerationPipeline.Grid + .DetermineConnectedComponents() + .Select(cells => spawningExtensions.CenterOfCell(cells.First()) + centreOfCellHeightOffset); + } + + private static IEnumerable GetTeleporterLandingPoints() + => GameObject.Find("Teleporters") + .GetComponentsInChildren() + .Select(x => x.LandingPoint); } public class ConnectedComponentsTests { private const string _sceneName = "ConnectedComponentsTestScene"; - private readonly Vector3 _centreOfCellHeightOffset = Vector3.up * 1.7f; [SetUp] public void SetUp() @@ -47,85 +96,33 @@ public void SetUp() [UnityTest] public IEnumerator CanVisitEveryConnectedComponent() { - // Arrange - ConnectedComponentsBeliefSet rootBeliefSet = new(); - GameObject gridGameObject = GameObject.Find("LevelGeneration"); - LevelGenerationPipeline levelGenerationPipeline = gridGameObject.GetComponent(); - SpawningExtensions spawningExtensions = gridGameObject.GetComponent(); - - // Arrange ==> Level information - Vector3[] cellsToVisit = levelGenerationPipeline.Grid.DetermineConnectedComponents() - .Select(cells => spawningExtensions.CenterOfCell(cells.First()) + _centreOfCellHeightOffset).ToArray(); - Vector3[] teleporterPositions = GameObject.Find("Teleporters") - .GetComponentsInChildren() - .Select(x => x.LandingPoint).ToArray(); - - int currentCellToVisitIndex = 0; + // Arrange + ConnectedComponentsBeliefSet connectedComponentBeliefSet = new(); - Vector3 currentCellPosition() - { - return cellsToVisit[currentCellToVisitIndex]; - } + // Remove doors and keys from the level. + GameObject doorsAndKeys = GameObject.Find("Doors and keys"); + Object.Destroy(doorsAndKeys); - // Arrange ==> GoalStructure: Visit cell of the current connected component - TransformPathfinderAction approachCurrentCellAction = new( + Tactic moveToNextComponent = new TransformPathfinderAction( beliefSet => beliefSet.PlayerRigidbody, - currentCellPosition(), - 1.4f); - - Action waitForTeleportAction = new(_ => { Debug.Log("Waiting for teleport..."); }); - - PrimitiveTactic approachCurrentCellTactic = new(approachCurrentCellAction); - PrimitiveTactic waitForTeleportTactic = new(waitForTeleportAction, - beliefSet => teleporterPositions.Any(teleporterPosition => - (teleporterPosition - ((Rigidbody)beliefSet.PlayerRigidbody).position).magnitude < 0.4f)); - FirstOfTactic waitForTeleportOrApproachCurrentCellTactic = new( - waitForTeleportTactic, - approachCurrentCellTactic); + beliefSet => beliefSet.GetCurrentTarget, + 1.5f); - Goal approachCurrentCellGoal = new(waitForTeleportOrApproachCurrentCellTactic, - beliefSet => (currentCellPosition() - ((Rigidbody)beliefSet.PlayerRigidbody).position).magnitude < - 1.5f); - - PrimitiveGoalStructure approachCurrentCellGoalStructure = - new(approachCurrentCellGoal); - - RepeatGoalStructure visitCurrentCellGoalStructure = - new(approachCurrentCellGoalStructure); - - // Arrange ==> GoalStructure: Target the next cell until every cell has been targeted - Action targetNextCellAction = new(_ => - { - Debug.Log($"Reached cell at {currentCellPosition()}"); - currentCellToVisitIndex++; - }); - - PrimitiveTactic targetNextCellTactic = new(targetNextCellAction); - Goal visitedEveryCellGoal = new(targetNextCellTactic, - _ => currentCellToVisitIndex >= cellsToVisit.Length); - - PrimitiveGoalStructure visitedEveryCellGoalStructure = - new(visitedEveryCellGoal); - - // Arrange ==> GoalStructure: Visit every connected component - SequentialGoalStructure visitCurrentCellAndVisitEveryCellGoalStructure = - new(visitCurrentCellGoalStructure, visitedEveryCellGoalStructure); - - RepeatGoalStructure visitEveryConnectedComponentGoalStructure = - new(visitCurrentCellAndVisitEveryCellGoalStructure); - - // Arrange ==> DesireSet - DesireSet desireSet = new(visitEveryConnectedComponentGoalStructure); + DesireSet visitAllComponents = new Goal(moveToNextComponent, HasVisitedEveryConnectedComponent); // Act - BdiAgent agent = new(rootBeliefSet, desireSet); + BdiAgent agent = new(connectedComponentBeliefSet, visitAllComponents); AplibRunner testRunner = new(agent); yield return testRunner.Test(); // Assert Assert.AreEqual(CompletionStatus.Success, agent.Status); + + // Local functions + static bool HasVisitedEveryConnectedComponent(ConnectedComponentsBeliefSet beliefSet) + => beliefSet.TargetPositionsInConnectedComponents.Count == 0; } } } From 98357204ef9bc899f9c8b2bbd9d57f7fd71553ce Mon Sep 17 00:00:00 2001 From: Jens Steenmetz Date: Tue, 25 Jun 2024 21:56:30 +0200 Subject: [PATCH 2/2] refactor: define local variable --- .../Assets/Testing/AplibTests/ConnectedComponentsTests.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/aplib.net-demo/Assets/Testing/AplibTests/ConnectedComponentsTests.cs b/aplib.net-demo/Assets/Testing/AplibTests/ConnectedComponentsTests.cs index 8d728121..db29a191 100644 --- a/aplib.net-demo/Assets/Testing/AplibTests/ConnectedComponentsTests.cs +++ b/aplib.net-demo/Assets/Testing/AplibTests/ConnectedComponentsTests.cs @@ -40,10 +40,12 @@ public ConnectedComponentsBeliefSet() { if (TargetPositionsInConnectedComponents.Count == 0) return false; - if (Vector3.Distance(PlayerRigidbody.position, TargetPositionsInConnectedComponents.Peek()) > 0.4f) + Vector3 currentTarget = TargetPositionsInConnectedComponents.Peek(); + + if (Vector3.Distance(PlayerRigidbody.position, currentTarget) > 0.4f) return false; - Debug.Log($"Reached target at {TargetPositionsInConnectedComponents.Peek()}"); + Debug.Log($"Reached target at {currentTarget}"); TargetPositionsInConnectedComponents.Dequeue(); Debug.Log($"Remaining targets: {TargetPositionsInConnectedComponents.Count}");