Using threads more
This commit is contained in:
@@ -9,7 +9,6 @@
|
||||
#include "CharacterAIModule.h"
|
||||
namespace ECS
|
||||
{
|
||||
#if 1
|
||||
class ActionNodeActions {
|
||||
struct WalkToAction : public goap::BaseAction<Blackboard> {
|
||||
int node;
|
||||
@@ -24,6 +23,21 @@ class ActionNodeActions {
|
||||
, node(node)
|
||||
{
|
||||
}
|
||||
bool can_run(const Blackboard &state,
|
||||
bool debug = false) override
|
||||
{
|
||||
return (state.distance_to(prereq) == 0);
|
||||
}
|
||||
void _plan_effects(Blackboard &state) override
|
||||
{
|
||||
const ActionNodeList &alist =
|
||||
ECS::get<ActionNodeList>();
|
||||
state.apply(effects);
|
||||
// node position can change in case of character
|
||||
const Ogre::Vector3 &nodePosition =
|
||||
alist.dynamicNodes[node].position;
|
||||
state.setPosition(nodePosition);
|
||||
}
|
||||
int get_cost(const Blackboard &bb) const override
|
||||
{
|
||||
int ret = m_cost;
|
||||
@@ -35,21 +49,16 @@ class ActionNodeActions {
|
||||
{
|
||||
const ActionNodeList &alist =
|
||||
ECS::get<ActionNodeList>();
|
||||
const TownNPCs &npcs = bb.town.get<TownNPCs>();
|
||||
const TownAI &ai = bb.town.get<TownAI>();
|
||||
// const TownNPCs &npcs = bb.town.get<TownNPCs>();
|
||||
// const TownAI &ai = bb.town.get<TownAI>();
|
||||
const Ogre::Vector3 &nodePosition =
|
||||
alist.nodes[node].position;
|
||||
flecs::entity e = npcs.npcs.at(bb.index).e;
|
||||
bool validActive = e.is_valid() &&
|
||||
e.has<CharacterBase>();
|
||||
alist.dynamicNodes[node].position;
|
||||
// flecs::entity e = npcs.npcs.at(bb.index).e;
|
||||
// bool validActive = e.is_valid() &&
|
||||
// e.has<CharacterBase>();
|
||||
const Ogre::Vector3 &npcPosition =
|
||||
validActive ?
|
||||
npcs.npcs.at(bb.index)
|
||||
.e.get<CharacterBase>()
|
||||
.mBodyNode
|
||||
->_getDerivedPosition() :
|
||||
npcs.npcs.at(bb.index).position;
|
||||
float dist = nodePosition.squaredDistance(
|
||||
bb.getPosition();
|
||||
float dist = npcPosition.squaredDistance(
|
||||
nodePosition);
|
||||
ret += (int)Ogre::Math::Ceil(dist);
|
||||
}
|
||||
@@ -59,6 +68,7 @@ out:
|
||||
};
|
||||
struct RunActionNode : public goap::BaseAction<Blackboard> {
|
||||
int node;
|
||||
float radius;
|
||||
Ogre::String action;
|
||||
RunActionNode(int node, const Ogre::String &action,
|
||||
const Blackboard &prereq,
|
||||
@@ -72,6 +82,23 @@ out:
|
||||
, node(node)
|
||||
, action(action)
|
||||
{
|
||||
const ActionNodeList &alist =
|
||||
ECS::get<ActionNodeList>();
|
||||
radius = alist.dynamicNodes[node].radius;
|
||||
}
|
||||
bool can_run(const Blackboard &state,
|
||||
bool debug = false) override
|
||||
{
|
||||
bool pre = (state.distance_to(prereq) == 0);
|
||||
if (!pre)
|
||||
return pre;
|
||||
const ActionNodeList &alist =
|
||||
ECS::get<ActionNodeList>();
|
||||
const Ogre::Vector3 &nodePosition =
|
||||
alist.dynamicNodes[node].position;
|
||||
const Ogre::Vector3 &npcPosition = state.getPosition();
|
||||
return (npcPosition.squaredDistance(nodePosition) <
|
||||
radius * radius);
|
||||
}
|
||||
};
|
||||
std::vector<goap::BaseAction<Blackboard> *> m_actions;
|
||||
@@ -155,7 +182,6 @@ public:
|
||||
return m_actions;
|
||||
}
|
||||
};
|
||||
#endif
|
||||
CharacterAIModule::CharacterAIModule(flecs::world &ecs)
|
||||
{
|
||||
static std::mutex ecs_mutex;
|
||||
@@ -299,11 +325,34 @@ CharacterAIModule::CharacterAIModule(flecs::world &ecs)
|
||||
std::lock_guard<std::mutex> lock(ecs_mutex);
|
||||
ECS::get_mut<ActionNodeList>().updateDynamicNodes();
|
||||
});
|
||||
ecs.system<TownNPCs>("UpdateNPCPositions")
|
||||
.kind(flecs::OnUpdate)
|
||||
.each([](flecs::entity e, TownNPCs &npcs) {
|
||||
for (auto it = npcs.npcs.begin(); it != npcs.npcs.end();
|
||||
it++) {
|
||||
auto &npc = npcs.npcs.at(it->first);
|
||||
if (npc.e.is_valid() &&
|
||||
npc.e.has<CharacterBase>())
|
||||
npc.position =
|
||||
npc.e.get<CharacterBase>()
|
||||
.mBodyNode
|
||||
->_getDerivedPosition();
|
||||
}
|
||||
});
|
||||
ecs.system<ActionNodeList, TownAI, TownNPCs>("UpdateBlackboards")
|
||||
.kind(flecs::OnUpdate)
|
||||
.interval(0.1f)
|
||||
.each([this](flecs::entity town, ActionNodeList &alist,
|
||||
TownAI &ai, const TownNPCs &npcs) {
|
||||
for (auto it = npcs.npcs.begin(); it != npcs.npcs.end();
|
||||
it++) {
|
||||
const auto &npc = npcs.npcs.at(it->first);
|
||||
if (ai.blackboards.find(it->first) ==
|
||||
ai.blackboards.end())
|
||||
continue;
|
||||
ai.blackboards.at(it->first).setPosition(
|
||||
npc.position);
|
||||
}
|
||||
Ogre::Root::getSingleton().getWorkQueue()->addTask(
|
||||
[this, town, &alist, npcs, &ai]() {
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user