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
|
/*
* Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.org/>
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "DynamicTree.h"
//#include "QuadTree.h"
//#include "RegularGrid.h"
#include "BoundingIntervalHierarchyWrapper.h"
#include "Log.h"
#include "RegularGrid.h"
#include "Timer.h"
#include "GameObjectModel.h"
#include "ModelInstance.h"
#include "ModelIgnoreFlags.h"
#include <G3D/AABox.h>
#include <G3D/Ray.h>
#include <G3D/Vector3.h>
using VMAP::ModelInstance;
namespace {
int CHECK_TREE_PERIOD = 200;
} // namespace
template<> struct HashTrait< GameObjectModel>{
static size_t hashCode(GameObjectModel const& g) { return (size_t)(void*)&g; }
};
template<> struct PositionTrait< GameObjectModel> {
static void getPosition(GameObjectModel const& g, G3D::Vector3& p) { p = g.getPosition(); }
};
template<> struct BoundsTrait< GameObjectModel> {
static void getBounds(GameObjectModel const& g, G3D::AABox& out) { out = g.getBounds();}
static void getBounds2(GameObjectModel const* g, G3D::AABox& out) { out = g->getBounds();}
};
/*
static bool operator==(GameObjectModel const& mdl, GameObjectModel const& mdl2){
return &mdl == &mdl2;
}
*/
typedef RegularGrid2D<GameObjectModel, BIHWrap<GameObjectModel> > ParentTree;
struct DynTreeImpl : public ParentTree/*, public Intersectable*/
{
typedef GameObjectModel Model;
typedef ParentTree base;
DynTreeImpl() :
rebalance_timer(CHECK_TREE_PERIOD),
unbalanced_times(0)
{
}
void insert(Model const& mdl)
{
base::insert(mdl);
++unbalanced_times;
}
void remove(Model const& mdl)
{
base::remove(mdl);
++unbalanced_times;
}
void balance()
{
base::balance();
unbalanced_times = 0;
}
void update(uint32 difftime)
{
if (empty())
return;
rebalance_timer.Update(difftime);
if (rebalance_timer.Passed())
{
rebalance_timer.Reset(CHECK_TREE_PERIOD);
if (unbalanced_times > 0)
balance();
}
}
TimeTrackerSmall rebalance_timer;
int unbalanced_times;
};
DynamicMapTree::DynamicMapTree() : impl(new DynTreeImpl()) { }
DynamicMapTree::~DynamicMapTree()
{
delete impl;
}
void DynamicMapTree::insert(GameObjectModel const& mdl)
{
impl->insert(mdl);
}
void DynamicMapTree::remove(GameObjectModel const& mdl)
{
impl->remove(mdl);
}
bool DynamicMapTree::contains(GameObjectModel const& mdl) const
{
return impl->contains(mdl);
}
void DynamicMapTree::balance()
{
impl->balance();
}
void DynamicMapTree::update(uint32 t_diff)
{
impl->update(t_diff);
}
struct DynamicTreeIntersectionCallback
{
bool did_hit;
uint32 phase_mask;
DynamicTreeIntersectionCallback(uint32 phasemask) : did_hit(false), phase_mask(phasemask) { }
bool operator()(G3D::Ray const& r, GameObjectModel const& obj, float& distance)
{
did_hit = obj.intersectRay(r, distance, true, phase_mask, VMAP::ModelIgnoreFlags::Nothing);
return did_hit;
}
bool didHit() const { return did_hit;}
};
struct DynamicTreeIntersectionCallback_WithLogger
{
bool did_hit;
uint32 phase_mask;
DynamicTreeIntersectionCallback_WithLogger(uint32 phasemask) : did_hit(false), phase_mask(phasemask)
{
TC_LOG_DEBUG("maps", "Dynamic Intersection log");
}
bool operator()(G3D::Ray const& r, GameObjectModel const& obj, float& distance)
{
TC_LOG_DEBUG("maps", "testing intersection with %s", obj.name.c_str());
bool hit = obj.intersectRay(r, distance, true, phase_mask, VMAP::ModelIgnoreFlags::Nothing);
if (hit)
{
did_hit = true;
TC_LOG_DEBUG("maps", "result: intersects");
}
return hit;
}
bool didHit() const { return did_hit;}
};
bool DynamicMapTree::getIntersectionTime(const uint32 phasemask, const G3D::Ray& ray,
const G3D::Vector3& endPos, float& maxDist) const
{
float distance = maxDist;
DynamicTreeIntersectionCallback callback(phasemask);
impl->intersectRay(ray, callback, distance, endPos);
if (callback.didHit())
maxDist = distance;
return callback.didHit();
}
bool DynamicMapTree::getObjectHitPos(const uint32 phasemask, const G3D::Vector3& startPos,
const G3D::Vector3& endPos, G3D::Vector3& resultHit,
float modifyDist) const
{
bool result = false;
float maxDist = (endPos - startPos).magnitude();
// valid map coords should *never ever* produce float overflow, but this would produce NaNs too
ASSERT(maxDist < std::numeric_limits<float>::max());
// prevent NaN values which can cause BIH intersection to enter infinite loop
if (maxDist < 1e-10f)
{
resultHit = endPos;
return false;
}
G3D::Vector3 dir = (endPos - startPos)/maxDist; // direction with length of 1
G3D::Ray ray(startPos, dir);
float dist = maxDist;
if (getIntersectionTime(phasemask, ray, endPos, dist))
{
resultHit = startPos + dir * dist;
if (modifyDist < 0)
{
if ((resultHit - startPos).magnitude() > -modifyDist)
resultHit = resultHit + dir*modifyDist;
else
resultHit = startPos;
}
else
resultHit = resultHit + dir*modifyDist;
result = true;
}
else
{
resultHit = endPos;
result = false;
}
return result;
}
bool DynamicMapTree::isInLineOfSight(float x1, float y1, float z1, float x2, float y2, float z2, uint32 phasemask) const
{
G3D::Vector3 v1(x1, y1, z1), v2(x2, y2, z2);
float maxDist = (v2 - v1).magnitude();
if (!G3D::fuzzyGt(maxDist, 0) )
return true;
G3D::Ray r(v1, (v2-v1) / maxDist);
DynamicTreeIntersectionCallback callback(phasemask);
impl->intersectRay(r, callback, maxDist, v2);
return !callback.did_hit;
}
float DynamicMapTree::getHeight(float x, float y, float z, float maxSearchDist, uint32 phasemask) const
{
G3D::Vector3 v(x, y, z + 0.5f);
G3D::Ray r(v, G3D::Vector3(0, 0, -1));
DynamicTreeIntersectionCallback callback(phasemask);
impl->intersectZAllignedRay(r, callback, maxSearchDist);
if (callback.didHit())
return v.z - maxSearchDist;
else
return -G3D::finf();
}
|