luxkun / ReGoap

Generic C# GOAP (Goal Oriented Action Planning) library with Unity3d examples
Apache License 2.0
1.03k stars 147 forks source link

A Planning bug #24

Closed TMPxyz closed 6 years ago

TMPxyz commented 6 years ago

The planner seems to have a bug here.

Here's a unit-test to reproduce it.

So the expected plan is like MineOre -> BuyFood -> MineOre; However the planner can only create plan like MineOre -> BuyFood;

        public void TestPlan2(IGoapPlanner<string, object> planner)
        {
            var gameObject = new GameObject();

            ReGoapTestsHelper.GetCustomAction(gameObject, "Mine Ore",
                new Dictionary<string, object> { },
                new Dictionary<string, object> { { "hasMoney", true } }, 10);
            ReGoapTestsHelper.GetCustomAction(gameObject, "Buy Food",
                new Dictionary<string, object> { { "hasMoney", true } },
                new Dictionary<string, object> { { "hasFood", true }, { "hasMoney", false } }, 2);

            var theGoal = ReGoapTestsHelper.GetCustomGoal(gameObject, "PrepareFoodAndMoney",
                new Dictionary<string, object> { { "hasMoney", true }, { "hasFood", true } });

            var memory = gameObject.AddComponent<ReGoapTestMemory>();
            memory.Init();

            var agent = gameObject.AddComponent<ReGoapTestAgent>();
            agent.Init();

            var plan = planner.Plan(agent, null, null, null);

            Assert.That(plan, Is.EqualTo(theGoal));
            // validate plan actions
            ReGoapTestsHelper.ApplyAndValidatePlan(plan, memory);
        }
TestPlan2 (0.067s)
---
Expected: True
  But was:  False
---
at ReGoap.Unity.Editor.Test.ReGoapTestsHelper.ApplyAndValidatePlan (IReGoapGoal`2 plan, ReGoap.Unity.Test.ReGoapTestMemory memory) [0x000d7] in E:\works\programs\GameProjects\TestGround\TestReGOAP\Assets\ReGoap\Unity\Editor\Test\ReGoapTestsHelper.cs:55
at ReGoap.Unity.Editor.Test.ReGoapTests.TestPlan2 (IGoapPlanner`2 planner) [0x000ef] in E:\works\programs\GameProjects\TestGround\TestReGOAP\Assets\ReGoap\Unity\Editor\Test\ReGoapTests.cs:72
at ReGoap.Unity.Editor.Test.ReGoapTests.TestPlan2 () [0x00008] in E:\works\programs\GameProjects\TestGround\TestReGOAP\Assets\ReGoap\Unity\Editor\Test\ReGoapTests.cs:45
---
[ReGoalPlanner] Starting planning calculation for agent: GoapAgent('')
[Astar] Success iterations: 4
[ReGoapPlanner] Calculated plan for goal 'GoapGoal('PrepareFoodAndMoney')', plan length: 2
[ReGoapPlanner] 0) GoapAction('Mine Ore')
[ReGoapPlanner] 1) GoapAction('Buy Food')
luxkun commented 6 years ago

Thanks for the report TMPxyz, very much appreciated.

I found two big issues both in the goal changer (inside the node Init) and in the conflict checker in the Expand function.

The last two commits should fix these and I have also included your unittests (if that's not okay let me know and I'll promptly remove them).