From 8de2de63ad22875e9fb7a55e61d98791d637916d Mon Sep 17 00:00:00 2001 From: Sergey Lapin Date: Tue, 3 Feb 2026 01:46:13 +0300 Subject: [PATCH] Updated planner code --- src/gamedata/CharacterAIModule.cpp | 76 ++++++++++++++---------------- src/gamedata/CharacterAIModule.h | 4 +- 2 files changed, 37 insertions(+), 43 deletions(-) diff --git a/src/gamedata/CharacterAIModule.cpp b/src/gamedata/CharacterAIModule.cpp index 3da68bb..ecf4845 100644 --- a/src/gamedata/CharacterAIModule.cpp +++ b/src/gamedata/CharacterAIModule.cpp @@ -407,6 +407,28 @@ void CharacterAIModule::buildPlans(flecs::entity town, const TownNPCs &npcs, { OgreAssert(town.is_valid(), "Bad town entity"); std::lock_guard lock(*ai.mutex); + auto planner = ai.planner; + auto buildPlan = [planner](Blackboard &blackboard, + const TownAI::goal_t &goal, + TownAI::Plan &plan) -> bool { + if (goal.is_reached(blackboard)) + return false; + plan.goal = &goal; + std::vector *> path; + int actionCount = blackboard.getActionsCount(); + auto actionsData = blackboard.getActionsData(); + path.resize(actionCount * actionCount); + int path_length = planner->plan(blackboard, goal, actionsData, + actionCount, path.data(), + path.size()); + if (path_length > 0) { + plan.goal = &goal; + plan.plan.insert(plan.plan.end(), path.begin(), + path.begin() + path_length); + return true; + } + return false; + }; for (auto it = npcs.npcs.begin(); it != npcs.npcs.end(); it++) { if (ai.blackboards.find(it->first) == ai.blackboards.end()) continue; @@ -419,10 +441,8 @@ void CharacterAIModule::buildPlans(flecs::entity town, const TownNPCs &npcs, int index = it->first; ai.plans[index] = {}; for (const auto &goal : ai.goals) { - if (goal.is_reached(bb)) - continue; struct TownAI::Plan plan; - plan.goal = &goal; + bool created = buildPlan(bb, goal, plan); #if 0 std::cout << "blackboard: " << bb.stats.dump(4) @@ -431,15 +451,6 @@ void CharacterAIModule::buildPlans(flecs::entity town, const TownNPCs &npcs, << goal.goal.stats.dump(4) << std::endl; #endif - std::vector *> path; - int actionCount = - ai.blackboards.at(it->first).getActionsCount(); - auto actionsData = - ai.blackboards.at(it->first).getActionsData(); - path.resize(actionCount * actionCount); - int path_length = ai.planner->plan( - bb, goal, actionsData, actionCount, path.data(), - path.size()); #if 0 std::cout << "Actions: " << std::endl; for (auto &action : actions) { @@ -459,38 +470,21 @@ void CharacterAIModule::buildPlans(flecs::entity town, const TownNPCs &npcs, } #endif #if 1 - std::cout << bb.index << " "; - std::cout << "Goal: " << goal.get_name(); - std::cout << std::endl; - std::cout << "Path: "; - int count = 0; - if (path_length < 0) { - std::cout << "Bad plan " << path_length + if (created) { + std::cout << bb.index << " "; + std::cout << "Goal: " << goal.get_name(); + std::cout << std::endl; + std::cout << "Path: "; + for (auto &action : plan.plan) { + OgreAssert(action, "No action"); + std::cout << action->get_name() + " "; + } + std::cout << " size: " << plan.plan.size() << std::endl; - } - for (auto &action : path) { - if (count >= path_length) - break; - OgreAssert(action, "No action"); - std::cout << action->get_name(); - if (count < path_length - 1) - std::cout << ", "; - count++; - } - std::cout << std::endl; - std::cout << path_length << std::endl; - // OgreAssert(path_length == 0, - // "planning"); -#endif - if (path_length > 0) { - plan.goal = &goal; - plan.plan.insert(plan.plan.end(), path.begin(), - path.begin() + path_length); ai.plans[it->first].push_back(plan); - break; - } - if (path_length > 0) OgreAssert(false, "plan"); + } +#endif } } } diff --git a/src/gamedata/CharacterAIModule.h b/src/gamedata/CharacterAIModule.h index c92a13b..262f170 100644 --- a/src/gamedata/CharacterAIModule.h +++ b/src/gamedata/CharacterAIModule.h @@ -78,9 +78,9 @@ struct TownAI { goap::BasePlanner > > planner; std::unordered_map blackboards; + typedef goap::BasePlanner >::BaseGoal goal_t; struct Plan { - const goap::BasePlanner >::BaseGoal *goal; + const goal_t *goal; std::vector *> plan; }; std::unordered_map > plans;