Compare commits
1 Commits
c854605a96
...
8de2de63ad
| Author | SHA1 | Date | |
|---|---|---|---|
| 8de2de63ad |
@@ -407,6 +407,28 @@ void CharacterAIModule::buildPlans(flecs::entity town, const TownNPCs &npcs,
|
||||
{
|
||||
OgreAssert(town.is_valid(), "Bad town entity");
|
||||
std::lock_guard<std::mutex> 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<goap::BaseAction<Blackboard> *> 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<goap::BaseAction<Blackboard> *> 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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,9 +78,9 @@ struct TownAI {
|
||||
goap::BasePlanner<Blackboard, goap::BaseAction<Blackboard> > >
|
||||
planner;
|
||||
std::unordered_map<int, Blackboard> blackboards;
|
||||
typedef goap::BasePlanner<Blackboard, goap::BaseAction<Blackboard> >::BaseGoal goal_t;
|
||||
struct Plan {
|
||||
const goap::BasePlanner<Blackboard,
|
||||
goap::BaseAction<Blackboard> >::BaseGoal *goal;
|
||||
const goal_t *goal;
|
||||
std::vector<goap::BaseAction<Blackboard> *> plan;
|
||||
};
|
||||
std::unordered_map<int, std::vector<struct Plan> > plans;
|
||||
|
||||
Reference in New Issue
Block a user