Subversion Repository Public Repository

Divide-Framework

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include "Headers/AIEntity.h"

#include "AI/PathFinding/Headers/DivideCrowd.h"
#include "AI/PathFinding/NavMeshes/Headers/NavMesh.h"
#include "AI/ActionInterface/Headers/AITeam.h"
#include "AI/ActionInterface/Headers/AISceneImpl.h"

#include "Core/Math/Headers/Transform.h"
#include "Graphs/Headers/SceneGraphNode.h"
#include "PathFinding/Waypoints/Headers/WaypointGraph.h"  ///< For waypoint movement
#include "PathFinding/NavMeshes/Headers/NavMesh.h" ///< For NavMesh movement
#include "Dynamics/Entities/Units/Headers/NPC.h"
#include "Managers/Headers/AIManager.h"

namespace Divide {
using namespace AI;

static const D32 DESTINATION_RADIUS = 3 * 3;

AIEntity::AIEntity(const vec3<F32>& currentPosition, const stringImpl& name)  : GUIDWrapper(),
                                                                                _name(name),
                                                                                _AISceneImpl(nullptr),
                                                                                _unitRef(nullptr),
                                                                                _teamPtr(nullptr),
                                                                                _detourCrowd(nullptr),
                                                                                _agent(nullptr),
                                                                                _agentID(-1),
                                                                                _distanceToTarget(-1.f),
                                                                                _previousDistanceToTarget(-1.f),
                                                                                _moveWaitTimer(0ULL),
                                                                                _stopped(false)
{
    _currentPosition.set(currentPosition);
    _agentRadiusCategory = AGENT_RADIUS_SMALL;
}

AIEntity::~AIEntity()
{
    if (_detourCrowd) {
        _detourCrowd->removeAgent(getAgentID());
    }

    _agentID = -1;
    _agent = nullptr;

    MemoryManager::DELETE( _AISceneImpl );
    MemoryManager::DELETE_HASHMAP(_sensorList);
}

void AIEntity::load(const vec3<F32>& position) {
    setPosition(position);

    if (!isAgentLoaded() && _detourCrowd) {
        _agentID = _detourCrowd->addAgent(position, 
                                          _unitRef ? _unitRef->getMovementSpeed() : 
                                                     (_detourCrowd->getAgentHeight() / 2) * 3.5f,
                                          10.0f);
        _agent = _detourCrowd->getAgent(_agentID);
        _destination = position;
    }
}

void AIEntity::unload() {
    if (!isAgentLoaded()) {
        return;
    }
    if (_detourCrowd) {
        _detourCrowd->removeAgent(getAgentID());
    }
    _agentID = -1;
    _agent = nullptr;
}

void AIEntity::sendMessage(AIEntity* receiver, AIMsg msg, const cdiggins::any& msg_content) {
    assert(receiver != nullptr);
    if (getGUID() != receiver->getGUID()) {
        receiver->receiveMessage(this, msg, msg_content);
    }
}

void AIEntity::receiveMessage(AIEntity* sender, AIMsg msg, const cdiggins::any& msg_content) {
    this->processMessage(sender, msg, msg_content);
}

void AIEntity::processMessage(AIEntity* sender, AIMsg msg, const cdiggins::any& msg_content) {
    assert(_AISceneImpl);

    ReadLock r_lock(_updateMutex);
    _AISceneImpl->processMessage(sender, msg, msg_content);
}

Sensor* AIEntity::getSensor(SensorType type) {
    ReadLock r_lock(_updateMutex);
    if (_sensorList.find(type) != _sensorList.end()) {
        return _sensorList[type];
    }
    return nullptr;
}

bool AIEntity::addSensor(SensorType type) {
    WriteLock w_lock(_updateMutex);
    Sensor* sensor = nullptr;
    switch (type) {
        case AUDIO_SENSOR: {
            sensor = MemoryManager_NEW AudioSensor(this);
        } break;
        case VISUAL_SENSOR: {
            sensor = MemoryManager_NEW VisualSensor(this);
        } break;
    };

    if (sensor) {
        SensorMap::iterator it = _sensorList.find(type);
        if (it != _sensorList.end()) {
            MemoryManager::SAFE_UPDATE( it->second, sensor );
        } else {
            hashAlg::emplace(_sensorList, type, sensor);
        }
        return true;
    }

    return false;
}

bool AIEntity::addAISceneImpl(AISceneImpl* AISceneImpl) {
    assert(AISceneImpl);

    WriteLock w_lock(_updateMutex);
    MemoryManager::SAFE_UPDATE( _AISceneImpl, AISceneImpl );
    _AISceneImpl->addEntityRef(this);
    return true;
}

void AIEntity::processInput(const U64 deltaTime) {
    ReadLock r_lock(_managerQueryMutex);
    if (_AISceneImpl) {
        _AISceneImpl->processInput(deltaTime);
    }
}

void AIEntity::processData(const U64 deltaTime){
    ReadLock r_lock(_managerQueryMutex);
    if (_AISceneImpl) {
        _AISceneImpl->processData(deltaTime);
    }
}

void AIEntity::update(const U64 deltaTime){
    ReadLock r_lock(_managerQueryMutex);
    if (_AISceneImpl) {
        _AISceneImpl->update(deltaTime, _unitRef);
    }
    if (_unitRef) {
        _unitRef->update(deltaTime);
        if (!_detourCrowd) {
            resetCrowd();
        }
    }
    updatePosition(deltaTime);
}

void AIEntity::init() {
    DIVIDE_ASSERT(_AISceneImpl != nullptr, 
                  "AIEntity error: Can't init entity without a proper AISceneImpl");
    _AISceneImpl->init();
}
 
I32 AIEntity::getTeamID() const { 
    if (_teamPtr != nullptr) {
        return _teamPtr->getTeamID();
    } 
    return -1; 
}

void AIEntity::setTeamPtr(AITeam* const teamPtr) {
    if (_teamPtr != nullptr) {
        _teamPtr->removeTeamMember(this);
    }
    _teamPtr = teamPtr;
}

void AIEntity::addUnitRef(NPC* const npc) {
    _unitRef = npc;
    if (_unitRef) {
        load(_unitRef->getPosition());
    }
}

D32 AIEntity::getAgentHeight() const {
    return _detourCrowd ? _detourCrowd->getAgentHeight() : 0.0;
}

D32 AIEntity::getAgentRadius() const {
    return _detourCrowd ? _detourCrowd->getAgentRadius() : 0.0;
}

void AIEntity::resetCrowd() {
    if (_detourCrowd) {
        unload();
    }

    _detourCrowd = _teamPtr->getCrowd(getAgentRadiusCategory());

    if (_detourCrowd) {
        _destination = _detourCrowd->getLastDestination();
        load(_unitRef != nullptr ? _unitRef->getPosition() : vec3<F32>());
    }
}

bool AIEntity::setPosition(const vec3<F32> position) {
    if (!isAgentLoaded()) {
        if (_unitRef) {
            _unitRef->setPosition(position);
        }
        return false;
    }
    if (!_detourCrowd || !_detourCrowd->isValidNavMesh()) {
        return false;
    }

    vec3<F32> result;
    // Find position on NavMesh
    if (!_detourCrowd->getNavMesh().getClosestPosition(position, vec3<F32>(5), DESTINATION_RADIUS, result)) {
        //return false;
    }
    // Remove agent from crowd and re-add at position
    _detourCrowd->removeAgent(_agentID);
    _agentID = _detourCrowd->addAgent(result, (_detourCrowd->getAgentHeight()/2)*3.5f, 10.0f);
    _agent = _detourCrowd->getAgent(_agentID);

    if (_unitRef) {
        _unitRef->setPosition(position);
    }
    return true;
}

void AIEntity::updatePosition(const U64 deltaTime){
    if(isAgentLoaded() && getAgent()->active){
        _previousDistanceToTarget = _distanceToTarget;
        _distanceToTarget = _currentPosition.distanceSquared(_destination);

        // if we are walking but did not change distance
        if(std::abs(_previousDistanceToTarget - _distanceToTarget) < DESTINATION_RADIUS){
            _moveWaitTimer += deltaTime;
            if(getUsToSec(_moveWaitTimer) > 5){
                _moveWaitTimer = 0;
                //updateDestination(_detourCrowd->getNavMesh().getRandomPosition());
                //return;
            }
        }else{
            _moveWaitTimer = 0;
        }
        _currentPosition.setV(getAgent()->npos);
        _currentVelocity.setV(getAgent()->nvel);
    }
    
    if(_unitRef){          
        _unitRef->setPosition(_currentPosition);
        _unitRef->setVelocity(_currentVelocity);
    }
}

bool AIEntity::updateDestination(const vec3<F32>& destination, bool updatePreviousPath) {
    if (!isAgentLoaded()) {
        return false;
    }
    vec3<F32> result;
    // Find position on navmesh
    if(!_detourCrowd->getNavMesh().getRandomPositionInCircle(destination, 
                                                             DESTINATION_RADIUS, 
                                                             vec3<F32>(5), 
                                                             result, 
                                                             10)) {
        if (!_detourCrowd->getNavMesh().getClosestPosition(destination, 
                                                           vec3<F32>(5), 
                                                           DESTINATION_RADIUS, 
                                                           result)) {
            return false;
        }
    }
    _detourCrowd->setMoveTarget(_agentID, result, updatePreviousPath);
    _destination = result;
    _stopped = false;

    return true;
}

const vec3<F32>& AIEntity::getPosition() const {
    return _currentPosition;
}

const vec3<F32>& AIEntity::getDestination() const {
    if (isAgentLoaded())
        return _destination;

    return getPosition();     // TODO this is not ideal
}

bool AIEntity::destinationReached(){
    if(!isAgentLoaded())
        return false;

    if(_currentPosition.distanceSquared(getDestination()) <= DESTINATION_RADIUS)
        return true;

    return _detourCrowd->destinationReached(getAgent(), DESTINATION_RADIUS);
}

void AIEntity::setDestination(const vec3<F32>& destination) {
    if (!isAgentLoaded())
        return;

    _destination = destination;
    _stopped = false;
}

void AIEntity::moveForward() {
    vec3<F32> lookDirection = _unitRef != nullptr ? _unitRef->getLookingDirection() : WORLD_Z_NEG_AXIS;
    lookDirection.normalize();

    setVelocity(lookDirection * getMaxSpeed());
}

void AIEntity::moveBackwards(){
    vec3<F32> lookDirection = _unitRef != nullptr ? _unitRef->getLookingDirection() : WORLD_Z_NEG_AXIS;
    lookDirection.normalize();

    setVelocity(lookDirection * getMaxSpeed() * -1.0f);
}

void AIEntity::setVelocity(const vec3<F32>& velocity){
    _stopped = false;
    _destination.reset();

    if (isAgentLoaded()) {
        _detourCrowd->requestVelocity(getAgentID(), velocity);
    }
}

void AIEntity::stop() {
    if (!isAgentLoaded()) {
        _stopped = true;
        return;
    }

    if (_detourCrowd->stopAgent(getAgentID())) {
        _destination.reset();
        _stopped = true;
    }
}

vec3<F32> AIEntity::getVelocity() const {
    return (isAgentLoaded() ? vec3<F32>(getAgent()->nvel) : vec3<F32>());
}

D32 AIEntity::getMaxSpeed(){
    return (isAgentLoaded() ? getAgent()->params.maxSpeed : 0.0);
}

D32 AIEntity::getMaxAcceleration(){
    return (isAgentLoaded()  ? getAgent()->params.maxAcceleration : 0.0);
}

}; //namespace Divide

Commits for Divide-Framework/trunk/Source Code/AI/AIEntity.cpp

Diff revisions: vs.
Revision Author Commited Message
331 Diff Diff IonutCava picture IonutCava Sat 06 Dec, 2014 20:53:45 +0000

[Ionut]
- Limited line length to 132 characters to improve readability and diff-comparisons
- Refactored memory allocation/deallocation functions
- Fixed a few compatibility issues with HashMap.h
- Fixed a bug in GPU Skinning shaders (cast a float to int)

326 Diff Diff IonutCava picture IonutCava Tue 30 Sep, 2014 21:11:32 +0000

[Ionut]
- Fixed more memory leaks
- Simplified Task interface and timing System
- Improved compatibility between Boost, STL and EASTL with random combinations of vectors, strings and maps
- Simplified Light class by removing the “slot” member
- Random optimizations

325 Diff Diff IonutCava picture IonutCava Wed 17 Sep, 2014 20:06:13 +0000

[Ionut]
- Reworked transform system:
— Parent transform chain system moved from Transform class to the PhysicsComponent
— PhysicsComponent now returns all transform values needed both global and local (still needs heavy optimization and caching)

- Reworked SceneGraph node management:
— Proper ref counting of SceneNodes and proper removing of resource (ResourceCache is now empty on destruction, as it should be)
— Removed parent transform tracking as that’s the PhysicsComponent’s responsibility
— Only nodes loaded via the ResourceCache are allowed to be added to the SceneGraph (added proper loaders for Impostors, Sky, etc)

- Optimized some of the math classes (mat4, mat3)

322 Diff Diff IonutCava picture IonutCava Sat 06 Sep, 2014 20:33:47 +0000

[Ionut]
- Refactored most of Boost related code to be C++11 based
— Boost is only used for ThreadPool, Mutex handling, XML parsing and networking (ASIO)
— Function binds, threads, regex, NonCopyable base, Hashing algorithms and more are now using C++11 libraries
- Replaced all FOR_EACH calls with standard, range, “for” calls

321 Diff Diff IonutCava picture IonutCava Wed 03 Sep, 2014 22:05:15 +0000

[Ionut]
- Added support (and enabled by default) for the EASTL library (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2271.html / https://github.com/paulhodge/EASTL)
— Strings, vectors and hash_maps can be combined from EASTL, STL and Boost (not all combinations work. e.g. EASTL strings with STL/Boost containers due to the lack of a proper hash function)

320 Diff Diff IonutCava picture IonutCava Sun 31 Aug, 2014 21:45:33 +0000

[Ionut]
- More AI work (entity captures flag and returns it to base)

319 Diff Diff IonutCava picture IonutCava Sat 30 Aug, 2014 21:37:57 +0000

[Ionut]
- Performance tweaks based on profiling data

318 Diff Diff IonutCava picture IonutCava Sat 30 Aug, 2014 17:35:53 +0000

[Ionut]
- Wrapped the entire code in a “Divide” namespace
- VertexBuffers now call “shrink_to_fit” on all internal data storage
- Improved some vector performance by preferring “emplace_back” instead of “push_back” + proepr usage of reserve / resize
- Wrapped OIS specific types and classes in Divide::Input namespace
- Added the messageBox.layout file (forgot it in the previous few commits)

317 Diff Diff IonutCava picture IonutCava Tue 26 Aug, 2014 22:08:06 +0000

[Ionut]
- More AI Work

316 IonutCava picture IonutCava Mon 25 Aug, 2014 22:04:03 +0000

[Ionut]
- Cleaned up AI classes
- Added basic Order <-> Goal system