Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

Diff Revisions 877 vs 878 for /trunk/Source Code/AI/AIManager.cpp

Diff revisions: vs.
  @@ -12,8 +12,10 @@
12 12
13 13 using namespace AI;
14 14
15 - AIManager::AIManager(Scene& parentScene)
15 + AIManager::AIManager(Scene& parentScene, TaskPool& pool)
16 16 : SceneComponent(parentScene),
17 + _parentPool(pool),
18 + _activeTask(nullptr),
17 19 _navMeshDebugDraw(false),
18 20 _pauseUpdate(true),
19 21 _updating(false),
  @@ -30,6 +32,10 @@
30 32 destroy();
31 33 }
32 34
35 + void AIManager::initialize() {
36 +
37 + }
38 +
33 39 /// Clear up any remaining AIEntities
34 40 void AIManager::destroy() {
35 41 {
  @@ -48,55 +54,41 @@
48 54 }
49 55 }
50 56
51 - void AIManager::update() {
57 + void AIManager::update(const U64 deltaTime) {
52 58 static const U64 updateFreq = Time::MillisecondsToMicroseconds(Config::AI_THREAD_UPDATE_FREQUENCY);
53 - _previousTime = Time::ElapsedMicroseconds(true);
54 - _running = true;
55 59
56 - while(true) {
57 - if (_shouldStop) {
58 - break;
59 - }
60 -
61 - _currentTime = Time::ElapsedMicroseconds(true);
62 - if (_currentTime >= _previousTime + updateFreq) {
63 -
64 - /// use internal delta time calculations
65 - _deltaTime = _currentTime - _previousTime;
66 - {
67 - /// Lock the entities during update() adding or deleting entities is
68 - /// suspended until this returns
69 - ReadLock r_lock(_updateMutex);
70 - if (!_aiTeams.empty() && !_pauseUpdate) {
71 - _updating = true;
72 - if (_sceneCallback) {
73 - _sceneCallback();
74 - }
60 + _currentTime += deltaTime;
61 + if (_currentTime >= _previousTime + updateFreq && !shouldStop()) {
62 + _running = true;
63 + /// use internal delta time calculations
64 + _deltaTime = _currentTime - _previousTime;
65 + {
66 + /// Lock the entities during update() adding or deleting entities is
67 + /// suspended until this returns
68 + ReadLock r_lock(_updateMutex);
69 + if (!_aiTeams.empty() && !_pauseUpdate) {
70 + _updating = true;
71 + if (_sceneCallback) {
72 + _sceneCallback();
73 + }
75 74
76 - if (processInput(_deltaTime)) { // sensors
77 - if (processData(_deltaTime)) { // think
78 - updateEntities(_deltaTime); // react
79 - }
75 + if (processInput(_deltaTime)) { // sensors
76 + if (processData(_deltaTime)) { // think
77 + updateEntities(_deltaTime); // react
80 78 }
81 -
82 - _updating = false;
83 79 }
84 - }
85 - _previousTime = Time::ElapsedMicroseconds(true);
86 80
87 - if (Config::AI_THREAD_UPDATE_FREQUENCY > Config::MIN_SLEEP_THRESHOLD_MS) {
88 - //ToDo: this needs adjustment to account for AI execution time
89 - std::this_thread::sleep_for(std::chrono::microseconds(updateFreq));
81 + _updating = false;
90 82 }
91 83 }
84 + _previousTime = _currentTime;
85 + _running = false;
92 86 }
93 -
94 - _running = false;
95 87 }
96 88
97 89 bool AIManager::processInput(const U64 deltaTime) { // sensors
98 90 for (AITeamMap::value_type& team : _aiTeams) {
99 - if (!team.second->processInput(deltaTime)) {
91 + if (!team.second->processInput(_parentPool, deltaTime)) {
100 92 return false;
101 93 }
102 94 }
  @@ -105,7 +97,7 @@
105 97
106 98 bool AIManager::processData(const U64 deltaTime) { // think
107 99 for (AITeamMap::value_type& team : _aiTeams) {
108 - if (!team.second->processData(deltaTime)) {
100 + if (!team.second->processData(_parentPool, deltaTime)) {
109 101 return false;
110 102 }
111 103 }
  @@ -114,7 +106,7 @@
114 106
115 107 bool AIManager::updateEntities(const U64 deltaTime) { // react
116 108 for (AITeamMap::value_type& team : _aiTeams) {
117 - if (!team.second->update(deltaTime)) {
109 + if (!team.second->update(_parentPool, deltaTime)) {
118 110 return false;
119 111 }
120 112 }
  @@ -223,4 +215,11 @@
223 215 }
224 216 }
225 217
218 + bool AIManager::shouldStop() const {
219 + if (_activeTask != nullptr) {
220 + _activeTask->stopTask();
221 + _activeTask->wait();
222 + }
223 + return _shouldStop;
224 + }
226 225 }; // namespace Divide