Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
AIProcessor.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018 DIVIDE-Studio
3 Copyright (c) 2009 Ionut Cava
4
5 This file is part of DIVIDE Framework.
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software
9 and associated documentation files (the "Software"), to deal in the Software
10 without restriction,
11 including without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so,
15 subject to the following conditions:
16
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED,
22 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23 PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY,
26 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
27 IN CONNECTION WITH THE SOFTWARE
28 OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 */
31
32#pragma once
33#ifndef DVD_AI_PROCESSOR_H_
34#define DVD_AI_PROCESSOR_H_
35
36#include "GOAPInterface.h"
37#include "AI/Headers/AIEntity.h"
38
39namespace Divide {
40class Texture;
41namespace AI {
42
43class AIManager;
44enum class AIMsg : U8;
47 public:
48 AIProcessor(AIManager& parentManager);
49 virtual ~AIProcessor();
50 virtual void addEntityRef(AIEntity* entity);
51
52 void worldState(const GOAPWorldState& state) { _worldState = state; }
53 GOAPWorldState& worldState() noexcept { return _worldState; }
54 const GOAPWorldState& worldState() const noexcept { return _worldState; }
55
57 virtual void registerAction(const GOAPAction& action);
58 virtual void registerActionSet(const GOAPActionSet& actionSet);
60 void registerGoal(const GOAPGoal& goal);
61 void registerGoalList(const GOAPGoalList& goalList);
62
63 virtual GOAPGoal* findGoal(const string& goalName) {
64 const GOAPGoalList::iterator it =
65 eastl::find_if(begin(_goals),
66 end(_goals),
67 [&goalName](const GOAPGoal& goal) noexcept -> bool
68 {
69 return goal.name() == goalName;
70 });
71
72 if (it != end(_goals)) {
73 return &*it;
74 }
75
76 return nullptr;
77 }
78
79 const GOAPGoalList& goalList() const noexcept { return _goals; }
80
81 protected:
82 friend class AIEntity;
83 GOAPGoalList& goalList() noexcept { return _goals; }
84
86 _activeGoals.clear();
87 for (GOAPGoal& goal : goalList()) {
88 goal.relevancy(0.0f);
89 activateGoal(goal.name());
90 }
91 }
92
94 bool activateGoal(const string& name) {
95 GOAPGoal* goal = findGoal(name);
96 if (goal != nullptr) {
97 _activeGoals.push_back(goal);
98 }
99 return goal != nullptr;
100 }
101
104 {
105 if (_activeGoals.empty())
106 {
107 return nullptr;
108 }
109
110 eastl::sort(begin(_activeGoals), end(_activeGoals),
111 [](GOAPGoal const* a, GOAPGoal const* b) noexcept {
112 return a->relevancy() < b->relevancy();
113 });
114
115 _activeGoal = _activeGoals.back();
116 _currentStep = U32_MAX;
117 assert(_activeGoal != nullptr);
118
119 return _activeGoal;
120 }
121
123 if (goal == nullptr) {
124 return false;
125 }
126 if (_activeGoals.empty()) {
127 return false;
128 }
129
130 auto* const it = eastl::find_if(
131 begin(_activeGoals), end(_activeGoals),
132 [goal](GOAPGoal const* actGoal) noexcept {
133 return actGoal->name() == goal->name();
134 });
135
136 if (it == end(_activeGoals)) {
137 return false;
138 }
139
140 _activeGoals.erase(it);
141 return true;
142 }
143
144 bool replanGoal() {
145 if (_activeGoal != nullptr) {
146 if (_activeGoal->plan(_worldState, _actionSet)) {
147 popActiveGoal(_activeGoal);
148 advanceGoal();
149 return true;
150 }
151
152 _planLog = "Plan Log: \n";
153 _planLog.append("\t OpenList: \n");
154 _planLog.append(_activeGoal->getOpenList());
155 _planLog.append("\t ClosedList: \n");
156 _planLog.append(_activeGoal->getClosedList());
157 invalidateCurrentPlan();
158 }
159
160 return false;
161 }
162
164 {
165 if (_activeGoal == nullptr)
166 {
167 return false;
168 }
169
170 const GOAPPlan& plan = _activeGoal->getCurrentPlan();
171 if (plan.empty())
172 {
173 return false;
174 }
175
176 assert(_currentStep < U32_MAX - 1u);
177
178 _currentStep++;
179 const GOAPAction* crtAction = getActiveAction();
180 if (!crtAction || !performAction(*crtAction))
181 {
182 invalidateCurrentPlan();
183 return false;
184 }
185 return true;
186 }
187
188 string printPlan() const
189 {
190 if (_activeGoal == nullptr)
191 {
192 return "no active goal!";
193 }
194
195 string returnString;
196 const GOAPPlan& plan = _activeGoal->getCurrentPlan();
197 for (const GOAPAction* action : plan)
198 {
199 Util::StringFormat( returnString, "{} - {}\n", returnString, printActionStats(*action) );
200 }
201
202 return returnString;
203 }
204
206 {
207 _activeGoal = nullptr;
208 _currentStep = U32_MAX;
209 }
210
211 inline const GOAPAction* getActiveAction() const
212 {
213 assert(_activeGoal != nullptr);
214 const GOAPPlan& plan = _activeGoal->getCurrentPlan();
215 if ( _currentStep == U32_MAX || _currentStep >= plan.size())
216 {
217 return nullptr;
218 }
219
220 return plan[_currentStep];
221 }
222
223 GOAPGoal* getActiveGoal() const noexcept { return _activeGoal; }
224
225 const string& getPlanLog() const noexcept { return _planLog; }
226
227 virtual const char* printActionStats(const GOAPAction& planStep) const;
229 virtual bool performAction(const GOAPAction& planStep) = 0;
230 virtual bool processData(U64 deltaTimeUS) = 0;
231 virtual bool processInput(U64 deltaTimeUS) = 0;
232 virtual bool update(U64 deltaTimeUS, NPC* unitRef = nullptr) = 0;
233 virtual void processMessage(AIEntity& sender, AIMsg msg, const std::any& msg_content) = 0;
234 void init() {
235 if (!_init) {
236 initInternal();
237 _init = true;
238 }
239 }
240
241 virtual void initInternal() = 0;
242
243 virtual string toString(bool state = false) const = 0;
244
245 protected:
248
249 private:
254
257 std::atomic_bool _init;
258
259 string _planLog;
260};
261
262} // namespace AI
263} // namespace Divide
264
265#endif //DVD_AI_PROCESSOR_H_
#define NOINITVTABLE
Based on OgreCrowd.
Definition: AIEntity.h:60
Provides a scene-level AI implementation.
Definition: AIProcessor.h:46
virtual bool processInput(U64 deltaTimeUS)=0
const GOAPAction * getActiveAction() const
Definition: AIProcessor.h:211
GOAPWorldState & worldState() noexcept
Definition: AIProcessor.h:53
virtual void initInternal()=0
const GOAPWorldState & worldState() const noexcept
Definition: AIProcessor.h:54
GOAPGoal * getActiveGoal() const noexcept
Definition: AIProcessor.h:223
virtual bool performActionStep(GOAPAction::operationsIterator step)=0
bool activateGoal(const string &name)
Although we want the goal to be activated, it might not be the most relevant in the current scene sta...
Definition: AIProcessor.h:94
virtual bool update(U64 deltaTimeUS, NPC *unitRef=nullptr)=0
virtual bool processData(U64 deltaTimeUS)=0
virtual string toString(bool state=false) const =0
string printPlan() const
Definition: AIProcessor.h:188
virtual void processMessage(AIEntity &sender, AIMsg msg, const std::any &msg_content)=0
const GOAPGoalList & goalList() const noexcept
Definition: AIProcessor.h:79
const string & getPlanLog() const noexcept
Definition: AIProcessor.h:225
virtual GOAPGoal * findGoal(const string &goalName)
Definition: AIProcessor.h:63
AIManager & _parentManager
Definition: AIProcessor.h:247
std::atomic_bool _init
Definition: AIProcessor.h:257
bool popActiveGoal(GOAPGoal *goal)
Definition: AIProcessor.h:122
virtual bool performAction(const GOAPAction &planStep)=0
vector< GOAPGoal * > _activeGoals
Definition: AIProcessor.h:256
GOAPWorldState _worldState
Definition: AIProcessor.h:253
GOAPGoal * findRelevantGoal()
Get the most relevant goal and set it as active.
Definition: AIProcessor.h:103
GOAPGoalList & goalList() noexcept
Definition: AIProcessor.h:83
virtual void invalidateCurrentPlan()
Definition: AIProcessor.h:205
GOAPActionSet _actionSet
Definition: AIProcessor.h:252
void worldState(const GOAPWorldState &state)
Definition: AIProcessor.h:52
const Divide::string & name() const noexcept
Definition: GOAPInterface.h:65
F32 relevancy() const noexcept
Definition: GOAPInterface.h:62
NPC base class. Every character in the game is an NPC by default except the Player.
Definition: NPC.h:46
hashMap< I32, bool >::const_iterator operationsIterator
Definition: Action.h:18
vector< const GOAPAction * > GOAPActionSet
Definition: GOAPInterface.h:49
vector< GOAPGoal > GOAPGoalList
Definition: GOAPInterface.h:90
vector< const GOAPAction * > GOAPPlan
Definition: GOAPInterface.h:50
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
uint8_t U8
eastl::vector< Type > vector
Definition: Vector.h:42
constexpr U32 U32_MAX
uint32_t U32
uint64_t U64