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
#include "Headers/DivideCrowd.h"

#include <core.h>
#include "DetourCommon.h"

namespace Divide {
namespace AI {
namespace Navigation {
 
    DivideDtCrowd::DivideDtCrowd(NavigationMesh *navMesh) : _crowd(nullptr),
                                                            _recast(navMesh),
                                                            _targetRef(0),
                                                            _activeAgents(0)
    {
        assert(_recast);
        _crowd = dtAllocCrowd();
        if(!_crowd){
            ERROR_FN(Locale::get("ERROR_DETOUR_CROWD_INSTANCE"));
            assert(_crowd != nullptr);
        }
        _targetPos[0] = _targetPos[1] = _targetPos[2] = 0.0f;
        // Set default agent parameters
        _anticipateTurns = true;
        _optimizeVis = true;
        _optimizeTopo = true;
        _obstacleAvoidance = true;
        _separation = false;
        _obstacleAvoidanceType = 3.0f;
        _separationWeight = 2.0f;

        memset(_trails, 0, sizeof(_trails));

        _vod = dtAllocObstacleAvoidanceDebugData();
        assert(_vod != nullptr);
        _vod->init(2048);

        memset(&_agentDebug, 0, sizeof(_agentDebug));
        _agentDebug.idx = -1;
        _agentDebug.vod = _vod;
        
        dtNavMesh * nav = navMesh->getNavigationMesh();
        assert(nav);

        dtCrowd* crowd = _crowd;
        
        if (nav && crowd && crowd->getAgentCount() == 0) {
            crowd->init(MAX_AGENTS, navMesh->getConfigParams().getAgentRadius(), nav);
            // Make polygons with 'disabled' flag invalid.
            crowd->getEditableFilter(0)->setExcludeFlags(SAMPLE_POLYFLAGS_DISABLED);
            // Create different avoidance settings presets. The crowd object can store multiple,
            // identified by an index number.
            // Setup local avoidance params to different qualities.
            dtObstacleAvoidanceParams params;
            // Use mostly default settings, copy from dtCrowd.
            memcpy(&params, crowd->getObstacleAvoidanceParams(0), sizeof(dtObstacleAvoidanceParams));
            // Low (11)
            params.velBias = 0.5f;
            params.adaptiveDivs = 5;
            params.adaptiveRings = 2;
            params.adaptiveDepth = 1;
            crowd->setObstacleAvoidanceParams(0, &params);
            // Medium (22)
            params.velBias = 0.5f;
            params.adaptiveDivs = 5;
            params.adaptiveRings = 2;
            params.adaptiveDepth = 2;
            crowd->setObstacleAvoidanceParams(1, &params);
            // Good (45)
            params.velBias = 0.5f;
            params.adaptiveDivs = 7;
            params.adaptiveRings = 2;
            params.adaptiveDepth = 3;
            crowd->setObstacleAvoidanceParams(2, &params);
            // High (66)
            params.velBias = 0.5f;
            params.adaptiveDivs = 7;
            params.adaptiveRings = 3;
            params.adaptiveDepth = 3;
            crowd->setObstacleAvoidanceParams(3, &params);
        }
    }

    DivideDtCrowd::~DivideDtCrowd()
    {
        dtFreeCrowd(_crowd);
        dtFreeObstacleAvoidanceDebugData(_vod);
    }

    void DivideDtCrowd::update(const U64 deltaTime) {
        dtNavMesh* nav = _recast->getNavigationMesh();
 
        if (!nav || !_crowd) return;
        // TimeVal startTime = getPerfTime();
        _crowd->update(getUsToSec(deltaTime), &_agentDebug);
        // TimeVal endTime = getPerfTime();
        // Update agent trails
        for(I32 i = 0; i < _crowd->getAgentCount(); ++i) {
            const dtCrowdAgent* ag = _crowd->getAgent(i);
            
            if (!ag->active)  continue;

            AgentTrail* trail = &_trails[i];
            // Update agent movement trail.
            trail->htrail = (trail->htrail + 1) % AGENT_MAX_TRAIL;
            dtVcopy(&trail->trail[trail->htrail*3], ag->npos);
        }
        _agentDebug.vod->normalizeSamples();
        //m_crowdSampleCount.addSample((float)_crowd->getVelocitySampleCount());
        //m_crowdTotalTime.addSample(getPerfDeltaTimeUsec(startTime, endTime) / 1000.0f);
    }
    
    I32 DivideDtCrowd::addAgent(const vec3<F32>& position, F32 maxSpeed, F32 acceleration) {
        // Define parameters for agent in crowd
        dtCrowdAgentParams ap;
        memset(&ap, 0, sizeof(ap));
        ap.radius = getAgentRadius();
        ap.height = getAgentHeight();
        ap.maxAcceleration = acceleration;
        ap.maxSpeed = maxSpeed;
        ap.collisionQueryRange = ap.radius * 12.0f;
        ap.pathOptimizationRange = ap.radius * 30.0f;
        // Set update flags according to config
        ap.updateFlags = 0;
        if (_anticipateTurns)   ap.updateFlags |= DT_CROWD_ANTICIPATE_TURNS;
        if (_optimizeVis)       ap.updateFlags |= DT_CROWD_OPTIMIZE_VIS;
        if (_optimizeTopo)      ap.updateFlags |= DT_CROWD_OPTIMIZE_TOPO;
        if (_obstacleAvoidance) ap.updateFlags |= DT_CROWD_OBSTACLE_AVOIDANCE;
        if (_separation)        ap.updateFlags |= DT_CROWD_SEPARATION;
        ap.obstacleAvoidanceType = (U8)_obstacleAvoidanceType;
        ap.separationWeight = _separationWeight;
        const F32 *p = &position.x;
        I32 idx = _crowd->addAgent(p, &ap);
        if (idx != -1) {
            // If a move target is defined: move agent towards it
            // TODO: do we want to set newly added agent's destination to previously set target? 
            // or remove this behaviour?
            if (_targetRef)
                _crowd->requestMoveTarget(idx, _targetRef, _targetPos);
            
            // Init trail
            AgentTrail* trail = &_trails[idx];
            for (U32 i = 0; i < AGENT_MAX_TRAIL; ++i)
                dtVcopy(&trail->trail[i*3], p);
            trail->htrail = 0;
        }

        _activeAgents++;
        return idx;
    }
    
    vectorImpl<dtCrowdAgent*> DivideDtCrowd::getActiveAgents() {
        dtCrowdAgent** resultEntries = MemoryManager_NEW dtCrowdAgent*[getMaxNbAgents()];
        I32 size = _crowd->getActiveAgents(resultEntries,getMaxNbAgents());

        vectorImpl<dtCrowdAgent*> result(resultEntries, resultEntries + size);
        MemoryManager::DELETE_ARRAY( resultEntries );
        return result;
    }

    vectorImpl<I32> DivideDtCrowd::getActiveAgentIds() {
        vectorImpl<I32> result;
        result.reserve(getMaxNbAgents());
        const dtCrowdAgent* agent = nullptr;
        for(I32 i = 0; i < getMaxNbAgents(); i++) {
            agent = _crowd->getAgent(i);
            if(agent->active)
                result.push_back(i);
        }

        return result;
    }
    
    void DivideDtCrowd::removeAgent(const I32 idx) {
        _crowd->removeAgent(idx);
        _activeAgents--;
    }
    
    vec3<F32> DivideDtCrowd::calcVel(const vec3<F32>& position, const vec3<F32>& target, D32 speed) {
        F32 res[3];
        calcVel(res, &position.x, &target.x, speed);
        return vec3<F32>(res);
    }

    void DivideDtCrowd::calcVel(F32* velocity, const F32* position, const F32* target, const F32 speed) {
        dtVsub(velocity, target, position);
        velocity[1] = 0.0;
        dtVnormalize(velocity);
        dtVscale(velocity, velocity, speed);
    }
    
    void DivideDtCrowd::setMoveTarget(const vec3<F32>& position, bool adjust) {
        // Find nearest point on navmesh and set move request to that location.
        const dtNavMeshQuery& navquery = _recast->getNavQuery();
 
        const dtQueryFilter* filter = _crowd->getFilter(0);
        const F32* ext = _crowd->getQueryExtents();
        const F32 *p = &position.x;
        navquery.findNearestPoly(p, ext, filter, &_targetRef, _targetPos);
        // Adjust target using tiny local search. (instead of recalculating full path)
        if (adjust) {
            for (I32 i = 0; i < _crowd->getAgentCount(); ++i) {
                const dtCrowdAgent* ag = _crowd->getAgent(i);
                if (!ag->active) continue;
                F32 vel[3];
                calcVel(vel, ag->npos, p, ag->params.maxSpeed);
                _crowd->requestMoveVelocity(i, vel);
            }
        } else {
            // Move target using path finder (recalculate a full new path)
            for (I32 i = 0; i < _crowd->getAgentCount(); ++i) {
                const dtCrowdAgent* ag = _crowd->getAgent(i);
                if (!ag->active) continue;
                _crowd->requestMoveTarget(i, _targetRef, _targetPos);
            }
        }
    }

    void DivideDtCrowd::setMoveTarget(I32 agentId, const vec3<F32>& position, bool adjust) {
        // TODO extract common method
        // Find nearest point on navmesh and set move request to that location.
        const dtNavMeshQuery& navquery = _recast->getNavQuery();
        dtCrowd* crowd = _crowd;
        const dtQueryFilter* filter = crowd->getFilter(0);
        const F32* ext = crowd->getQueryExtents();
        const F32* p = &position.x;
        navquery.findNearestPoly(p, ext, filter, &_targetRef, _targetPos);
        // ----
        if (adjust) {
            const dtCrowdAgent *ag = getAgent(agentId);
            F32 vel[3];
            calcVel(vel, ag->npos, p, ag->params.maxSpeed);
            crowd->requestMoveVelocity(agentId, vel);
        } else {
            _crowd->requestMoveTarget(agentId, _targetRef, _targetPos);
        }
    }

    bool DivideDtCrowd::requestVelocity(I32 agentId, const vec3<F32>& velocity) {
        if (!getAgent(agentId)->active)
            return false;

        return _crowd->requestMoveVelocity(agentId, &velocity.x);
    }

    bool DivideDtCrowd::stopAgent(I32 agentId)  {
        F32 zeroVel[] = {0,0,0};
        return _crowd->resetMoveTarget(agentId) && _crowd->requestMoveVelocity(agentId, zeroVel);
    }

    F32 DivideDtCrowd::getDistanceToGoal(const dtCrowdAgent* agent, const F32 maxRange) {
        if (!agent || !agent->ncorners || agent->ncorners < 0)
            return maxRange;

        if (agent->cornerFlags[agent->ncorners-1] & DT_STRAIGHTPATH_END)
            return dtMin(dtVdist2D(agent->npos, &agent->cornerVerts[(agent->ncorners-1)*3]), maxRange);

        return maxRange;
    }

    bool DivideDtCrowd::destinationReached(const dtCrowdAgent* agent, const F32 maxDistanceFromTarget) {
        return getDistanceToGoal(agent, maxDistanceFromTarget) < maxDistanceFromTarget;
    }

    bool DivideDtCrowd::isValidNavMesh() const {
        return _recast->getNavQuery().getAttachedNavMesh() != nullptr;
    }
}; //namespace Navigation
}; //namespace AI
}; //namespace Divide

Commits for Divide-Framework/trunk/Source Code/AI/PathFinding/DivideCrowd.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)

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)

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)

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)

315 Diff Diff IonutCava picture IonutCava Mon 25 Aug, 2014 15:23:33 +0000

[Ionut]
- Update ReCast to the latest version available on GitHub
- Improved nav mesh target point selection
- Added basic “go to enemy flag” logic for the WarScene teams

311 Diff Diff IonutCava picture IonutCava Mon 18 Aug, 2014 21:40:52 +0000

[Ionut]
- Added a basic AESOP implementation
- Moved all AI related classes under the “AI” namespace

308 Diff Diff IonutCava picture IonutCava Sat 16 Aug, 2014 19:49:31 +0000

[Ionut]
- Moved Transform from SceneGraphNode to PhysicsComponent and modified BoundingBox update system based on transform update
- Added PhysicsAsset to PhysicsComponent for interfacing with PshysicsAPIs (e.g. PhysXActor inherits PhysicsAsset)
- PhysicsAsset keeps track of the parent PhysicsComponent to have direct access to a node’s transform
- Rewrote GUIConsole to only print items when visible, and use a single circular buffer for entries.
- Updated GUI input system to match the interface that both Kernel and SceneManager share (should probably abstract all the input methods in a class and have Kernel, SceneManager, Scene and GUI inherit from that. In next commit.)
- Updated CEGUI to probably match the OIS states(mouse, keyboard, etc) and properly inject time inputs.

249 Diff Diff k1ngp1n picture k1ngp1n Mon 31 Mar, 2014 20:42:31 +0000

[Ionut] [[BR]]
- Migrated to platform toolset v100 [[BR]]
- Updated boost to 1.55 [[BR]]
- Moved from “NULL” to “nullptr” [[BR]]
- DevIL is now linked dynamically [[BR]]

233 Diff Diff k1ngp1n picture k1ngp1n Tue 14 Jan, 2014 22:24:35 +0000

[Ionut] [[BR]]
- AMD compatibility fixes [[BR]]
- More pathfinding tweaks [[BR]]

232 k1ngp1n picture k1ngp1n Tue 14 Jan, 2014 14:58:15 +0000

[Ionut][[BR]]
- nVidia FBO_MS fix (TexParameter calls do not work on TEXTURE_2D_MULTISAMPLE) [[BR]]
- nVidia shading issue fix (uninitialized MaterialProperties values were used for lighting) [[BR]]
- more stability fixes for nav meshes and pathfinding (may still hang sometimes [[BR]]
- Some changes to WarScene: [[BR]]
— added some obstacles [[BR]]
— added auto load nav mesh in separate thread [[BR]]
— added auto draw debug nav mesh in DEBUG [[BR]]
— added basic entity position <---> entity destination debug line rendering to get a basic idea of unit’s path [[BR]]