1 |
|
// Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
|
2 |
|
// Copyright (C) 2010 Winch Gate Property Limited
|
3 |
|
//
|
4 |
|
// This program is free software: you can redistribute it and/or modify
|
5 |
|
// it under the terms of the GNU Affero General Public License as
|
6 |
|
// published by the Free Software Foundation, either version 3 of the
|
7 |
|
// License, or (at your option) any later version.
|
8 |
|
//
|
9 |
|
// This program is distributed in the hope that it will be useful,
|
10 |
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11 |
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12 |
|
// GNU Affero General Public License for more details.
|
13 |
|
//
|
14 |
|
// You should have received a copy of the GNU Affero General Public License
|
15 |
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
#ifndef RYAI_ENTITY_PHYSICAL_INLINE_H
|
21 |
|
#define RYAI_ENTITY_PHYSICAL_INLINE_H
|
22 |
|
|
23 |
|
#include "ai.h"
|
24 |
|
#include "game_share/fame.h"
|
25 |
|
/*
|
26 |
|
|
27 |
|
The following base class is the common parent of bots, plrs and any other
|
28 |
|
physical object that has an existance in the world
|
29 |
|
|
30 |
|
*/
|
31 |
|
|
32 |
|
//////////////////////////////////////////////////////////////////////////////
|
33 |
|
// CAIEntityPhysical //
|
34 |
|
//////////////////////////////////////////////////////////////////////////////
|
35 |
|
|
36 |
|
#ifdef NL_OS_WINDOWS
|
37 |
|
#pragma warning (push)
|
38 |
|
#pragma warning (disable : 4355)
|
39 |
|
#endif
|
40 |
|
|
41 |
|
inline
|
42 |
|
CAIEntityPhysical::CAIEntityPhysical(CPersistentOfPhysical& owner, TDataSetRow const& entityIndex, NLMISC::CEntityId const& id, float radius, uint32 level, RYAI_MAP_CRUNCH::TAStarFlag const& AStarFlags)
|
43 |
|
: CAIEntity()
|
44 |
|
, _dataSetRow(entityIndex)
|
45 |
|
, _pos(entityIndex)
|
46 |
|
, CSpawnable<CPersistentOfPhysical >(owner)
|
47 |
|
, _id(id)
|
48 |
|
, _Stuned(0)
|
49 |
|
, _Rooted(0)
|
50 |
|
, _Blinded(0)
|
51 |
|
, _Feared(0)
|
52 |
|
//, _UnreachableTarget((CAIEntityPhysical*)NULL)
|
53 |
|
, _radius(radius)
|
54 |
|
, _food(_radius)
|
55 |
|
, _Level(level)
|
56 |
|
, _AStarFlags(AStarFlags)
|
57 |
|
{
|
58 |
|
_instanceNumber.init(*CMirrors::DataSet, entityIndex, DSPropertyAI_INSTANCE);
|
59 |
|
_mode.init (*CMirrors::DataSet, entityIndex, DSPropertyMODE);
|
60 |
|
_behaviour.init (*CMirrors::DataSet, entityIndex, DSPropertyBEHAVIOUR);
|
61 |
|
_targetRow.init (*CMirrors::DataSet, entityIndex, DSPropertyTARGET_ID);
|
62 |
|
_ActionFlags.init (*CMirrors::DataSet, entityIndex, DSPropertyACTION_FLAGS);
|
63 |
|
|
64 |
|
_RunSpeed.init (*CMirrors::DataSet, entityIndex, DSPropertyCURRENT_RUN_SPEED);
|
65 |
|
_WalkSpeed.init (*CMirrors::DataSet, entityIndex, DSPropertyCURRENT_WALK_SPEED);
|
66 |
|
|
67 |
|
_CurrentHitPoint.init (*CMirrors::DataSet, entityIndex, DSPropertyCURRENT_HIT_POINTS);
|
68 |
|
_MaxHitPoint.init (*CMirrors::DataSet, entityIndex, DSPropertyMAX_HIT_POINTS);
|
69 |
|
_VisionCounter.init (*CMirrors::DataSet, entityIndex, DSPropertyVISION_COUNTER);
|
70 |
|
_InOutpostAlias.init (*CMirrors::DataSet, entityIndex, DSPropertyIN_OUTPOST_ZONE_ALIAS);
|
71 |
|
_InOutpostSide.init (*CMirrors::DataSet, entityIndex, DSPropertyIN_OUTPOST_ZONE_SIDE);
|
72 |
|
|
73 |
|
#ifdef NL_DEBUG
|
74 |
|
const NLMISC::CDbgRefCount<CAIEntityPhysical> &ref=*(static_cast<NLMISC::CDbgRefCount<CAIEntityPhysical> * const>(this));
|
75 |
|
NLMISC::CDbgPtr<CAIEntityPhysical> dummyPtr;
|
76 |
|
|
77 |
|
nlassert(ref.getDbgRef(dummyPtr)==0);
|
78 |
|
nlassert(CAIS::instance()._CAIEntityByDataSetRow.find(entityIndex.getIndex())==CAIS::instance()._CAIEntityByDataSetRow.end());
|
79 |
|
ref.setCheckMax(false);
|
80 |
|
#endif
|
81 |
|
CAIS::instance()._CAIEntityByDataSetRow.insert(std::make_pair(entityIndex.getIndex(),this)); // we don't want to register players.
|
82 |
|
#ifdef NL_DEBUG
|
83 |
|
ref.setCheckMax(true);
|
84 |
|
nlassert(ref.getDbgRef(dummyPtr)==1);
|
85 |
|
nlassert(CAIS::instance()._CAIEntityByDataSetRow.find(entityIndex.getIndex())!=CAIS::instance()._CAIEntityByDataSetRow.end());
|
86 |
|
#endif
|
87 |
|
|
88 |
|
CAIEntityPhysicalLocator::getInstance()->addEntity(_dataSetRow, _id, this);
|
89 |
|
}
|
90 |
|
|
91 |
|
inline
|
92 |
|
CAIEntityPhysical::~CAIEntityPhysical()
|
93 |
|
{
|
94 |
|
CAIEntityPhysicalLocator::getInstance()->delEntity(_dataSetRow, _id, this);
|
95 |
|
|
96 |
|
#ifdef NL_DEBUG
|
97 |
|
const NLMISC::CDbgRefCount<CAIEntityPhysical> &ref=*(static_cast<NLMISC::CDbgRefCount<CAIEntityPhysical> * const>(this));
|
98 |
|
NLMISC::CDbgPtr<CAIEntityPhysical> dummyPtr;
|
99 |
|
#endif
|
100 |
|
|
101 |
|
// Remove healers
|
102 |
|
std::multiset<IAIEntityPhysicalHealer*>::iterator it, itEnd;
|
103 |
|
for (it=_Healers.begin(), itEnd=_Healers.end(); it!=itEnd; ++it)
|
104 |
|
{
|
105 |
|
(*it)->healerRemoved(this);
|
106 |
|
}
|
107 |
|
|
108 |
|
detachFromTargeting();
|
109 |
|
CAIS::instance()._CAIEntityByDataSetRow.erase(dataSetRow().getIndex());
|
110 |
|
|
111 |
|
#ifdef NL_DEBUG
|
112 |
|
nlassert(ref.getDbgRef(dummyPtr)==0);
|
113 |
|
nlassert(CAIS::instance()._CAIEntityByDataSetRow.find(dataSetRow().getIndex())==CAIS::instance()._CAIEntityByDataSetRow.end());
|
114 |
|
#endif
|
115 |
|
}
|
116 |
|
|
117 |
|
#ifdef NL_OS_WINDOWS
|
118 |
|
#pragma warning (pop)
|
119 |
|
#endif
|
120 |
|
|
121 |
|
inline
|
122 |
|
void CAIEntityPhysical::setActionFlags(RYZOMACTIONFLAGS::TActionFlag const& flag)
|
123 |
|
{
|
124 |
|
_ActionFlags = (_ActionFlags.getValue() | flag );
|
125 |
|
}
|
126 |
|
|
127 |
|
inline
|
128 |
|
void CAIEntityPhysical::removeActionFlags(RYZOMACTIONFLAGS::TActionFlag const& flag)
|
129 |
|
{
|
130 |
|
_ActionFlags = (_ActionFlags.getValue() & ~flag );
|
131 |
|
}
|
132 |
|
|
133 |
|
inline
|
134 |
|
float CAIEntityPhysical::getCollisionDist(float angTo) const
|
135 |
|
{
|
136 |
|
return 0.5;
|
137 |
|
}
|
138 |
|
|
139 |
|
inline
|
140 |
|
bool CAIEntityPhysical::canMove() const
|
141 |
|
{
|
142 |
|
return !isRooted() && (walkSpeed() != 0);
|
143 |
|
}
|
144 |
|
|
145 |
|
inline
|
146 |
|
float CAIEntityPhysical::walkSpeed() const
|
147 |
|
{
|
148 |
|
return _WalkSpeed()*0.1f*SpeedFactor*getSpeedFactor();
|
149 |
|
}
|
150 |
|
|
151 |
|
inline
|
152 |
|
float CAIEntityPhysical::runSpeed() const
|
153 |
|
{
|
154 |
|
return _RunSpeed()*0.1f*SpeedFactor*getSpeedFactor();
|
155 |
|
}
|
156 |
|
|
157 |
|
inline
|
158 |
|
void CAIEntityPhysical::setWPos(RYAI_MAP_CRUNCH::CWorldPosition const& pos)
|
159 |
|
{
|
160 |
|
nlassert(pos.isValid());
|
161 |
|
_wpos = pos;
|
162 |
|
}
|
163 |
|
|
164 |
|
//////////////////////////////////////////////////////////////////////////////
|
165 |
|
// CPersistentOfPhysical //
|
166 |
|
//////////////////////////////////////////////////////////////////////////////
|
167 |
|
|
168 |
|
inline
|
169 |
|
bool CPersistentOfPhysical::isAt16MetersPos(uint16 x, uint16 y) const
|
170 |
|
{
|
171 |
|
CAIEntityPhysical const* phys = getSpawnObj();
|
172 |
|
if (!phys)
|
173 |
|
return false;
|
174 |
|
return ( (uint16)(phys->x().asInt16Meters())==x
|
175 |
|
&& (uint16)(phys->y().asInt16Meters())==y );
|
176 |
|
}
|
177 |
|
|
178 |
|
inline
|
179 |
|
CAIEntityPhysical* CPersistentOfPhysical::getSpawnObj() const
|
180 |
|
{
|
181 |
|
return CPersistent<CAIEntityPhysical>::getSpawnObj();
|
182 |
|
}
|
183 |
|
|
184 |
|
//////////////////////////////////////////////////////////////////////////////
|
185 |
|
// CModEntityPhysical //
|
186 |
|
//////////////////////////////////////////////////////////////////////////////
|
187 |
|
|
188 |
|
inline
|
189 |
|
void CModEntityPhysical::setMode(MBEHAV::EMode m)
|
190 |
|
{
|
191 |
|
if (_mode().Mode == MBEHAV::DEATH) // to remove (can be replaced with the above assertion in debug mode).
|
192 |
|
return;
|
193 |
|
|
194 |
|
if (_mode().Mode == m)
|
195 |
|
return;
|
196 |
|
_mode = MBEHAV::TMode(m, pos().x().asInt(), pos().y().asInt());
|
197 |
|
}
|
198 |
|
|
199 |
|
inline
|
200 |
|
void CModEntityPhysical::setTarget(CAIEntityPhysical* target)
|
201 |
|
{
|
202 |
|
CTargetable<CAIEntityPhysical>::setTarget(target);
|
203 |
|
// if (CTargetable<CAIEntityPhysical>::getTarget()==target)
|
204 |
|
const CAIEntityPhysical* tgt=CTargetable<CAIEntityPhysical>::getTarget();
|
205 |
|
if (tgt==target)
|
206 |
|
_targetRow = target ? target->dataSetRow() : TDataSetRow();
|
207 |
|
}
|
208 |
|
|
209 |
|
inline
|
210 |
|
void CModEntityPhysical::setVisualTarget(CAIEntityPhysical* target)
|
211 |
|
{
|
212 |
|
CTargetable<CAIEntityPhysical>::setVisualTarget(target);
|
213 |
|
// if (CTargetable<CAIEntityPhysical>::getVisualTarget()==target)
|
214 |
|
const CAIEntityPhysical* tgt=CTargetable<CAIEntityPhysical>::getVisualTarget();
|
215 |
|
if (tgt==target)
|
216 |
|
_targetRow = target ? target->dataSetRow() : TDataSetRow();
|
217 |
|
}
|
218 |
|
|
219 |
|
inline
|
220 |
|
void CModEntityPhysical::setUnreachableTarget(CAIEntityPhysical* target)
|
221 |
|
{
|
222 |
|
CTargetable<CAIEntityPhysical>::setUnreachableTarget(target);
|
223 |
|
// if (CTargetable<CAIEntityPhysical>::getUnreachableTarget()==target)
|
224 |
|
const CAIEntityPhysical* tgt=CTargetable<CAIEntityPhysical>::getUnreachableTarget();
|
225 |
|
if (tgt==target)
|
226 |
|
_targetRow = target ? target->dataSetRow() : TDataSetRow();
|
227 |
|
}
|
228 |
|
|
229 |
|
inline
|
230 |
|
bool CModEntityPhysical::setPos(CAIPos const& pos)
|
231 |
|
{
|
232 |
|
RYAI_MAP_CRUNCH::CWorldPosition wpos;
|
233 |
|
if (!CWorldContainer::getWorldMap().setWorldPosition(AITYPES::vp_auto, wpos,pos))
|
234 |
|
return false;
|
235 |
|
setPos(pos, wpos);
|
236 |
|
return true;
|
237 |
|
}
|
238 |
|
|
239 |
|
inline
|
240 |
|
bool CModEntityPhysical::moveTo(CAIPos const& newPos, RYAI_MAP_CRUNCH::TAStarFlag const& denyFlags)
|
241 |
|
{
|
242 |
|
if (!wpos().isValid())
|
243 |
|
return false;
|
244 |
|
|
245 |
|
RYAI_MAP_CRUNCH::CWorldPosition tmpWPos(wpos());
|
246 |
|
if (CWorldContainer::getWorldMap().move(tmpWPos, RYAI_MAP_CRUNCH::CMapPosition(newPos), denyFlags))
|
247 |
|
{
|
248 |
|
#ifdef NL_DEBUG
|
249 |
|
nlassert(RYAI_MAP_CRUNCH::CMapPosition(newPos)==tmpWPos);
|
250 |
|
#endif
|
251 |
|
setPos(newPos, tmpWPos);
|
252 |
|
return true;
|
253 |
|
}
|
254 |
|
else
|
255 |
|
{
|
256 |
|
// a part of move has been done.
|
257 |
|
if (tmpWPos!=wpos())
|
258 |
|
{
|
259 |
|
CAIPos partNewPos(tmpWPos.toAIVector(),newPos.h(),newPos.theta());
|
260 |
|
setPos(partNewPos, tmpWPos);
|
261 |
|
return true;
|
262 |
|
}
|
263 |
|
|
264 |
|
}
|
265 |
|
// no move at all..
|
266 |
|
return false;
|
267 |
|
};
|
268 |
|
|
269 |
|
inline
|
270 |
|
void CModEntityPhysical::resetDecalage()
|
271 |
|
{
|
272 |
|
_Decalage.setXY(0, 0);
|
273 |
|
}
|
274 |
|
|
275 |
|
//////////////////////////////////////////////////////////////////////////////
|
276 |
|
// CWorldMapLink //
|
277 |
|
//////////////////////////////////////////////////////////////////////////////
|
278 |
|
|
279 |
|
template <class T>
|
280 |
|
void CWorldMapLink<T>::linkEntityToMatrix(const CAICoord &x,const CAICoord &y,CAIEntityMatrix<T>& matrix)
|
281 |
|
{
|
282 |
|
_matrixListLink.link(matrix[(uint8)(y.asInt16Meters())][(uint8)(x.asInt16Meters())]);
|
283 |
|
}
|
284 |
|
|
285 |
|
template <class T>
|
286 |
|
void CWorldMapLink<T>::linkEntityToMatrix(const CAIVectorMirror &pos,CAIEntityMatrix<T>& matrix)
|
287 |
|
{
|
288 |
|
linkEntityToMatrix(pos.x(),pos.y(),matrix);
|
289 |
|
}
|
290 |
|
|
291 |
|
template <class T>
|
292 |
|
void CWorldMapLink<T>::linkEntityToMatrix(const CAIVector &pos,CAIEntityMatrix<T>& matrix)
|
293 |
|
{
|
294 |
|
linkEntityToMatrix(pos.x(),pos.y(),matrix);
|
295 |
|
}
|
296 |
|
|
297 |
|
template <class T>
|
298 |
|
void CWorldMapLink<T>::linkToWorldMap(T *entityPtr, const CAIVector &pos,CAIEntityMatrix<T>& matrix)
|
299 |
|
{
|
300 |
|
_matrixListLink.setEntity (entityPtr);
|
301 |
|
linkEntityToMatrix (pos,matrix);
|
302 |
|
}
|
303 |
|
|
304 |
|
template <class T>
|
305 |
|
void CWorldMapLink<T>::linkToWorldMap(T *entityPtr, const CAIVectorMirror &pos,CAIEntityMatrix<T>& matrix)
|
306 |
|
{
|
307 |
|
_matrixListLink.setEntity (entityPtr);
|
308 |
|
linkEntityToMatrix (pos,matrix);
|
309 |
|
}
|
310 |
|
|
311 |
|
//////////////////////////////////////////////////////////////////////////////
|
312 |
|
// CTargetable //
|
313 |
|
//////////////////////////////////////////////////////////////////////////////
|
314 |
|
|
315 |
|
template <class T>
|
316 |
|
CTargetable<T>::CTargetable()
|
317 |
|
: _FightTargetersWeight(0.f)
|
318 |
|
, _FightTargetersValue(0.f)
|
319 |
|
{
|
320 |
|
for (size_t i=0; i<TARGET_TYPE_MAX; ++i)
|
321 |
|
{
|
322 |
|
_TargeterCount[i] = 0;
|
323 |
|
_FirstTargeters[i] = (T*)NULL;
|
324 |
|
}
|
325 |
|
_Target = (T*)NULL;
|
326 |
|
_TargetType = TARGET_TYPE_FIGHT;
|
327 |
|
_NextTargeter = (T*)NULL;
|
328 |
|
}
|
329 |
|
|
330 |
|
template <class T>
|
331 |
|
CTargetable<T>::~CTargetable()
|
332 |
|
{
|
333 |
|
// This should be done in Derived class dtor, but just in case of since we're instanciable
|
334 |
|
detachFromTargeting();
|
335 |
|
}
|
336 |
|
|
337 |
|
template <class T>
|
338 |
|
void CTargetable<T>::detachFromTargeters()
|
339 |
|
{
|
340 |
|
for (size_t i=0; i<TARGET_TYPE_MAX; ++i)
|
341 |
|
{
|
342 |
|
while (!_FirstTargeters[i].isNULL())
|
343 |
|
{
|
344 |
|
TPtr targeter = _FirstTargeters[i];
|
345 |
|
targeter->targetDied();
|
346 |
|
// This method is a bit more intrusive than targeter->setTarget(NULL), but it avoids possible infinite loops
|
347 |
|
_FirstTargeters[i] = targeter->_NextTargeter;
|
348 |
|
unlinkTargeter((TTargetType)i, targeter);
|
349 |
|
}
|
350 |
|
}
|
351 |
|
}
|
352 |
|
|
353 |
|
template <class T>
|
354 |
|
void CTargetable<T>::detachFromTargeting()
|
355 |
|
{
|
356 |
|
setTarget((T*)NULL);
|
357 |
|
detachFromTargeters();
|
358 |
|
}
|
359 |
|
|
360 |
|
template <class T>
|
361 |
|
void CTargetable<T>::linkTargeter(TTargetType type, TPtr const& targeter, TPtr const& nextTargeter)
|
362 |
|
{
|
363 |
|
++_TargeterCount[type];
|
364 |
|
if (type==TARGET_TYPE_FIGHT)
|
365 |
|
{
|
366 |
|
_FightTargetersWeight += targeter->fightWeight();
|
367 |
|
_FightTargetersValue += targeter->fightValue();
|
368 |
|
}
|
369 |
|
targeter->_Target = dynamic_cast<T*>(this);
|
370 |
|
targeter->_TargetType = type;
|
371 |
|
targeter->_NextTargeter = nextTargeter;
|
372 |
|
// Some verifications to avoid algorithm bugs
|
373 |
|
// nlassert(fightTargetersFreeWeight()>=-0.00001f);
|
374 |
|
nlassert(!targeter->_Target.isNULL());
|
375 |
|
}
|
376 |
|
|
377 |
|
template <class T>
|
378 |
|
void CTargetable<T>::unlinkTargeter(TTargetType type, TPtr const& targeter)
|
379 |
|
{
|
380 |
|
#ifdef NL_DEBUG
|
381 |
|
nlassert((T*)targeter->_Target == this);
|
382 |
|
#else
|
383 |
|
if ((T*)targeter->_Target != this)
|
384 |
|
nlwarning("Targeter's target is not me!");
|
385 |
|
#endif
|
386 |
|
targeter->_Target = (T*)NULL;
|
387 |
|
targeter->_NextTargeter = (T*)NULL;
|
388 |
|
--_TargeterCount[type];
|
389 |
|
if (type==TARGET_TYPE_FIGHT)
|
390 |
|
{
|
391 |
|
_FightTargetersWeight -= targeter->fightWeight();
|
392 |
|
_FightTargetersValue -= targeter->fightValue();
|
393 |
|
}
|
394 |
|
}
|
395 |
|
|
396 |
|
template <class T>
|
397 |
|
void CTargetable<T>::addTargeter(TTargetType type, TPtr const& targeter)
|
398 |
|
{
|
399 |
|
linkTargeter(type, targeter, _FirstTargeters[type]);
|
400 |
|
_FirstTargeters[type] = targeter;
|
401 |
|
}
|
402 |
|
|
403 |
|
template <class T>
|
404 |
|
void CTargetable<T>::removeTargeter(TTargetType type, TPtr const& targeter)
|
405 |
|
{
|
406 |
|
if (targeter.isNULL())
|
407 |
|
return;
|
408 |
|
if (_FirstTargeters[type] == targeter)
|
409 |
|
{
|
410 |
|
_FirstTargeters[type] = targeter->_NextTargeter;
|
411 |
|
unlinkTargeter(type, targeter);
|
412 |
|
return;
|
413 |
|
}
|
414 |
|
|
415 |
|
NLMISC::CDbgPtr<T> current = _FirstTargeters[type];
|
416 |
|
while (!current.isNULL()) {
|
417 |
|
NLMISC::CDbgPtr<T> next = current->_NextTargeter;
|
418 |
|
if (next == targeter)
|
419 |
|
{
|
420 |
|
current->_NextTargeter = targeter->_NextTargeter;
|
421 |
|
unlinkTargeter(type, targeter);
|
422 |
|
break;
|
423 |
|
}
|
424 |
|
else
|
425 |
|
{
|
426 |
|
current = next;
|
427 |
|
}
|
428 |
|
}
|
429 |
|
}
|
430 |
|
|
431 |
|
template <class T>
|
432 |
|
void CTargetable<T>::tryToAddTargeter(TTargetType type, TPtr const& targeter)
|
433 |
|
{
|
434 |
|
H_AUTO(CTargetable_addTargeter);
|
435 |
|
|
436 |
|
if (type==TARGET_TYPE_FIGHT)
|
437 |
|
{
|
438 |
|
|
439 |
|
// Prepare data to optimize ----------------------------------------------
|
440 |
|
size_t opponentCount = targeterCount();
|
441 |
|
// Allocate arrays filled with 0.f
|
442 |
|
float* weights = new float[opponentCount+1];
|
443 |
|
float* values = new float[opponentCount+1];
|
444 |
|
bool* take = new bool[opponentCount+1];
|
445 |
|
for (size_t i=0; i<opponentCount+1; ++i)
|
446 |
|
{
|
447 |
|
weights[i] = 0.f;
|
448 |
|
values[i] = 0.f;
|
449 |
|
take[i] = false;
|
450 |
|
}
|
451 |
|
{
|
452 |
|
H_AUTO(addTargeterInfoRetr);
|
453 |
|
// Fill with the former opponents
|
454 |
|
T* opponent = firstTargeter();
|
455 |
|
size_t i = 0;
|
456 |
|
while (opponent!=NULL)
|
457 |
|
{
|
458 |
|
weights[i] = opponent->fightWeight();
|
459 |
|
values[i] = opponent->fightValue();
|
460 |
|
opponent = opponent->nextTargeter();
|
461 |
|
take[i] = true;
|
462 |
|
++i;
|
463 |
|
}
|
464 |
|
// Add the new targeter
|
465 |
|
weights[opponentCount] = targeter->fightWeight();
|
466 |
|
values[opponentCount] = targeter->fightValue();
|
467 |
|
take[opponentCount] = false;
|
468 |
|
}
|
469 |
|
// Create the context
|
470 |
|
CKnapsackContext context(opponentCount+1, weights, values);
|
471 |
|
// Configure and run the solver ------------------------------------------
|
472 |
|
CKnapsackSolver solver(&context, take);
|
473 |
|
{
|
474 |
|
H_AUTO(addTargeterOptimize);
|
475 |
|
solver.optimize(fightTargetersWeightMax(), _TargeterChoiceAlgorithm);
|
476 |
|
}
|
477 |
|
// Save the data ---------------------------------------------------------
|
478 |
|
// If the new set is better than the old one
|
479 |
|
if (solver.totalValue()>fightTargetersValue())
|
480 |
|
{
|
481 |
|
// Unlink the old ones if needed
|
482 |
|
T* opponent = _FirstTargeters[type];
|
483 |
|
TPtr* previousPtr = &_FirstTargeters[type];
|
484 |
|
size_t i = 0;
|
485 |
|
while (opponent!=NULL)
|
486 |
|
{
|
487 |
|
if (!solver.take(i))
|
488 |
|
{
|
489 |
|
*previousPtr = opponent->_NextTargeter;
|
490 |
|
unlinkTargeter(type, opponent);
|
491 |
|
}
|
492 |
|
else
|
493 |
|
{
|
494 |
|
previousPtr = &opponent->_NextTargeter;
|
495 |
|
opponent = opponent->_NextTargeter;
|
496 |
|
}
|
497 |
|
++i;
|
498 |
|
}
|
499 |
|
// Link the new targeter if needed
|
500 |
|
if (solver.take(opponentCount))
|
501 |
|
{
|
502 |
|
addTargeter(type, targeter);
|
503 |
|
}
|
504 |
|
#ifdef NL_DEBUG
|
505 |
|
// Debug verifications
|
506 |
|
{
|
507 |
|
opponent = _FirstTargeters[type];
|
508 |
|
float value = 0.f;
|
509 |
|
float weight = 0.f;
|
510 |
|
while (opponent!=NULL)
|
511 |
|
{
|
512 |
|
value += opponent->fightValue();
|
513 |
|
weight += opponent->fightWeight();
|
514 |
|
opponent = opponent->_NextTargeter;
|
515 |
|
}
|
516 |
|
// Verify linking system
|
517 |
|
nlassert(fabs(fightTargetersValue() - value) < 0.0001);
|
518 |
|
nlassert(fabs(fightTargetersWeight() - weight) < 0.0001);
|
519 |
|
|
520 |
|
// Verify solver behaviour
|
521 |
|
nlassert(fabs(fightTargetersValue() - solver.totalValue()) < 0.0001);
|
522 |
|
nlassert(fabs(fightTargetersWeight() - solver.totalWeight()) < 0.0001);
|
523 |
|
}
|
524 |
|
#endif
|
525 |
|
}
|
526 |
|
delete [] take;
|
527 |
|
delete [] values;
|
528 |
|
delete [] weights;
|
529 |
|
|
530 |
|
}
|
531 |
|
else
|
532 |
|
{
|
533 |
|
// Just link the new targeter (no limit on visual targeters)
|
534 |
|
addTargeter(type, targeter);
|
535 |
|
}
|
536 |
|
}
|
537 |
|
|
538 |
|
template <class T>
|
539 |
|
void CTargetable<T>::setTarget(T* target)
|
540 |
|
{
|
541 |
|
setTarget(TARGET_TYPE_FIGHT, target);
|
542 |
|
}
|
543 |
|
|
544 |
|
template <class T>
|
545 |
|
void CTargetable<T>::setVisualTarget(T* target)
|
546 |
|
{
|
547 |
|
setTarget(TARGET_TYPE_VISUAL, target);
|
548 |
|
}
|
549 |
|
|
550 |
|
template <class T>
|
551 |
|
void CTargetable<T>::setUnreachableTarget(T* target)
|
552 |
|
{
|
553 |
|
setTarget(TARGET_TYPE_UNREACHABLE, target);
|
554 |
|
}
|
555 |
|
|
556 |
|
template <class T>
|
557 |
|
void CTargetable<T>::setTarget(TTargetType type, TPtr const& target)
|
558 |
|
{
|
559 |
|
if (target==_Target && type==_TargetType)
|
560 |
|
return;
|
561 |
|
|
562 |
|
if (!_Target.isNULL())
|
563 |
|
{
|
564 |
|
_Target->removeTargeter(_TargetType, dynamic_cast<T*>(this));
|
565 |
|
}
|
566 |
|
if (!target.isNULL())
|
567 |
|
{
|
568 |
|
target->tryToAddTargeter(type, dynamic_cast<T*>(this));
|
569 |
|
}
|
570 |
|
}
|
571 |
|
|
572 |
|
template <class T>
|
573 |
|
typename CTargetable<T>::TPtr CTargetable<T>::getTarget() const
|
574 |
|
{
|
575 |
|
return getTarget(TARGET_TYPE_FIGHT);
|
576 |
|
}
|
577 |
|
|
578 |
|
template <class T>
|
579 |
|
typename CTargetable<T>::TPtr CTargetable<T>::getVisualTarget() const
|
580 |
|
{
|
581 |
|
return getTarget(TARGET_TYPE_VISUAL);
|
582 |
|
}
|
583 |
|
|
584 |
|
template <class T>
|
585 |
|
typename CTargetable<T>::TPtr CTargetable<T>::getUnreachableTarget() const
|
586 |
|
{
|
587 |
|
return getTarget(TARGET_TYPE_UNREACHABLE);
|
588 |
|
}
|
589 |
|
|
590 |
|
template <class T>
|
591 |
|
typename CTargetable<T>::TPtr CTargetable<T>::getTarget(TTargetType type) const
|
592 |
|
{
|
593 |
|
if (_TargetType==type)
|
594 |
|
return _Target;
|
595 |
|
else
|
596 |
|
return (T*)NULL;
|
597 |
|
}
|
598 |
|
|
599 |
|
template <class T>
|
600 |
|
uint32 CTargetable<T>::totalTargeterCount() const
|
601 |
|
{
|
602 |
|
uint32 count = 0;
|
603 |
|
for (size_t i = 0; i<TARGET_TYPE_MAX; ++i)
|
604 |
|
count += _TargeterCount[i];
|
605 |
|
return count;
|
606 |
|
}
|
607 |
|
|
608 |
|
//----------------------------------------------------------------------------
|
609 |
|
|
610 |
|
#endif
|
611 |
|
|
|
1 |
// Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
|
|
2 |
// Copyright (C) 2010 Winch Gate Property Limited
|
|
3 |
//
|
|
4 |
// This program is free software: you can redistribute it and/or modify
|
|
5 |
// it under the terms of the GNU Affero General Public License as
|
|
6 |
// published by the Free Software Foundation, either version 3 of the
|
|
7 |
// License, or (at your option) any later version.
|
|
8 |
//
|
|
9 |
// This program is distributed in the hope that it will be useful,
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 |
// GNU Affero General Public License for more details.
|
|
13 |
//
|
|
14 |
// You should have received a copy of the GNU Affero General Public License
|
|
15 |
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
#ifndef RYAI_ENTITY_PHYSICAL_INLINE_H
|
|
21 |
#define RYAI_ENTITY_PHYSICAL_INLINE_H
|
|
22 |
|
|
23 |
#include "ai.h"
|
|
24 |
#include "game_share/fame.h"
|
|
25 |
/*
|
|
26 |
|
|
27 |
The following base class is the common parent of bots, plrs and any other
|
|
28 |
physical object that has an existance in the world
|
|
29 |
|
|
30 |
*/
|
|
31 |
|
|
32 |
//////////////////////////////////////////////////////////////////////////////
|
|
33 |
// CAIEntityPhysical //
|
|
34 |
//////////////////////////////////////////////////////////////////////////////
|
|
35 |
|
|
36 |
#ifdef NL_OS_WINDOWS
|
|
37 |
#pragma warning (push)
|
|
38 |
#pragma warning (disable : 4355)
|
|
39 |
#endif
|
|
40 |
|
|
41 |
inline
|
|
42 |
CAIEntityPhysical::CAIEntityPhysical(CPersistentOfPhysical& owner, TDataSetRow const& entityIndex, NLMISC::CEntityId const& id, float radius, uint32 level, RYAI_MAP_CRUNCH::TAStarFlag const& AStarFlags)
|
|
43 |
: CAIEntity()
|
|
44 |
, CSpawnable<CPersistentOfPhysical >(owner)
|
|
45 |
, _pos(entityIndex)
|
|
46 |
, _dataSetRow(entityIndex)
|
|
47 |
, _Stuned(0)
|
|
48 |
, _Rooted(0)
|
|
49 |
, _Blinded(0)
|
|
50 |
, _Feared(0)
|
|
51 |
//, _UnreachableTarget((CAIEntityPhysical*)NULL)
|
|
52 |
, _id(id)
|
|
53 |
, _radius(radius)
|
|
54 |
, _food(_radius)
|
|
55 |
, _Level(level)
|
|
56 |
, _AStarFlags(AStarFlags)
|
|
57 |
{
|
|
58 |
_instanceNumber.init(*CMirrors::DataSet, entityIndex, DSPropertyAI_INSTANCE);
|
|
59 |
_mode.init (*CMirrors::DataSet, entityIndex, DSPropertyMODE);
|
|
60 |
_behaviour.init (*CMirrors::DataSet, entityIndex, DSPropertyBEHAVIOUR);
|
|
61 |
_targetRow.init (*CMirrors::DataSet, entityIndex, DSPropertyTARGET_ID);
|
|
62 |
_ActionFlags.init (*CMirrors::DataSet, entityIndex, DSPropertyACTION_FLAGS);
|
|
63 |
|
|
64 |
_RunSpeed.init (*CMirrors::DataSet, entityIndex, DSPropertyCURRENT_RUN_SPEED);
|
|
65 |
_WalkSpeed.init (*CMirrors::DataSet, entityIndex, DSPropertyCURRENT_WALK_SPEED);
|
|
66 |
|
|
67 |
_CurrentHitPoint.init (*CMirrors::DataSet, entityIndex, DSPropertyCURRENT_HIT_POINTS);
|
|
68 |
_MaxHitPoint.init (*CMirrors::DataSet, entityIndex, DSPropertyMAX_HIT_POINTS);
|
|
69 |
_VisionCounter.init (*CMirrors::DataSet, entityIndex, DSPropertyVISION_COUNTER);
|
|
70 |
_InOutpostAlias.init (*CMirrors::DataSet, entityIndex, DSPropertyIN_OUTPOST_ZONE_ALIAS);
|
|
71 |
_InOutpostSide.init (*CMirrors::DataSet, entityIndex, DSPropertyIN_OUTPOST_ZONE_SIDE);
|
|
72 |
|
|
73 |
#ifdef NL_DEBUG
|
|
74 |
const NLMISC::CDbgRefCount<CAIEntityPhysical> &ref=*(static_cast<NLMISC::CDbgRefCount<CAIEntityPhysical> * const>(this));
|
|
75 |
NLMISC::CDbgPtr<CAIEntityPhysical> dummyPtr;
|
|
76 |
|
|
77 |
nlassert(ref.getDbgRef(dummyPtr)==0);
|
|
78 |
nlassert(CAIS::instance()._CAIEntityByDataSetRow.find(entityIndex.getIndex())==CAIS::instance()._CAIEntityByDataSetRow.end());
|
|
79 |
ref.setCheckMax(false);
|
|
80 |
#endif
|
|
81 |
CAIS::instance()._CAIEntityByDataSetRow.insert(std::make_pair(entityIndex.getIndex(),this)); // we don't want to register players.
|
|
82 |
#ifdef NL_DEBUG
|
|
83 |
ref.setCheckMax(true);
|
|
84 |
nlassert(ref.getDbgRef(dummyPtr)==1);
|
|
85 |
nlassert(CAIS::instance()._CAIEntityByDataSetRow.find(entityIndex.getIndex())!=CAIS::instance()._CAIEntityByDataSetRow.end());
|
|
86 |
#endif
|
|
87 |
|
|
88 |
CAIEntityPhysicalLocator::getInstance()->addEntity(_dataSetRow, _id, this);
|
|
89 |
}
|
|
90 |
|
|
91 |
inline
|
|
92 |
CAIEntityPhysical::~CAIEntityPhysical()
|
|
93 |
{
|
|
94 |
CAIEntityPhysicalLocator::getInstance()->delEntity(_dataSetRow, _id, this);
|
|
95 |
|
|
96 |
#ifdef NL_DEBUG
|
|
97 |
const NLMISC::CDbgRefCount<CAIEntityPhysical> &ref=*(static_cast<NLMISC::CDbgRefCount<CAIEntityPhysical> * const>(this));
|
|
98 |
NLMISC::CDbgPtr<CAIEntityPhysical> dummyPtr;
|
|
99 |
#endif
|
|
100 |
|
|
101 |
// Remove healers
|
|
102 |
std::multiset<IAIEntityPhysicalHealer*>::iterator it, itEnd;
|
|
103 |
for (it=_Healers.begin(), itEnd=_Healers.end(); it!=itEnd; ++it)
|
|
104 |
{
|
|
105 |
(*it)->healerRemoved(this);
|
|
106 |
}
|
|
107 |
|
|
108 |
detachFromTargeting();
|
|
109 |
CAIS::instance()._CAIEntityByDataSetRow.erase(dataSetRow().getIndex());
|
|
110 |
|
|
111 |
#ifdef NL_DEBUG
|
|
112 |
nlassert(ref.getDbgRef(dummyPtr)==0);
|
|
113 |
nlassert(CAIS::instance()._CAIEntityByDataSetRow.find(dataSetRow().getIndex())==CAIS::instance()._CAIEntityByDataSetRow.end());
|
|
114 |
#endif
|
|
115 |
}
|
|
116 |
|
|
117 |
#ifdef NL_OS_WINDOWS
|
|
118 |
#pragma warning (pop)
|
|
119 |
#endif
|
|
120 |
|
|
121 |
inline
|
|
122 |
void CAIEntityPhysical::setActionFlags(RYZOMACTIONFLAGS::TActionFlag const& flag)
|
|
123 |
{
|
|
124 |
_ActionFlags = (_ActionFlags.getValue() | flag );
|
|
125 |
}
|
|
126 |
|
|
127 |
inline
|
|
128 |
void CAIEntityPhysical::removeActionFlags(RYZOMACTIONFLAGS::TActionFlag const& flag)
|
|
129 |
{
|
|
130 |
_ActionFlags = (_ActionFlags.getValue() & ~flag );
|
|
131 |
}
|
|
132 |
|
|
133 |
inline
|
|
134 |
float CAIEntityPhysical::getCollisionDist(float angTo) const
|
|
135 |
{
|
|
136 |
return 0.5;
|
|
137 |
}
|
|
138 |
|
|
139 |
inline
|
|
140 |
bool CAIEntityPhysical::canMove() const
|
|
141 |
{
|
|
142 |
return !isRooted() && (walkSpeed() != 0);
|
|
143 |
}
|
|
144 |
|
|
145 |
inline
|
|
146 |
float CAIEntityPhysical::walkSpeed() const
|
|
147 |
{
|
|
148 |
return _WalkSpeed()*0.1f*SpeedFactor*getSpeedFactor();
|
|
149 |
}
|
|
150 |
|
|
151 |
inline
|
|
152 |
float CAIEntityPhysical::runSpeed() const
|
|
153 |
{
|
|
154 |
return _RunSpeed()*0.1f*SpeedFactor*getSpeedFactor();
|
|
155 |
}
|
|
156 |
|
|
157 |
inline
|
|
158 |
void CAIEntityPhysical::setWPos(RYAI_MAP_CRUNCH::CWorldPosition const& pos)
|
|
159 |
{
|
|
160 |
nlassert(pos.isValid());
|
|
161 |
_wpos = pos;
|
|
162 |
}
|
|
163 |
|
|
164 |
//////////////////////////////////////////////////////////////////////////////
|
|
165 |
// CPersistentOfPhysical //
|
|
166 |
//////////////////////////////////////////////////////////////////////////////
|
|
167 |
|
|
168 |
inline
|
|
169 |
bool CPersistentOfPhysical::isAt16MetersPos(uint16 x, uint16 y) const
|
|
170 |
{
|
|
171 |
CAIEntityPhysical const* phys = getSpawnObj();
|
|
172 |
if (!phys)
|
|
173 |
return false;
|
|
174 |
return ( (uint16)(phys->x().asInt16Meters())==x
|
|
175 |
&& (uint16)(phys->y().asInt16Meters())==y );
|
|
176 |
}
|
|
177 |
|
|
178 |
inline
|
|
179 |
CAIEntityPhysical* CPersistentOfPhysical::getSpawnObj() const
|
|
180 |
{
|
|
181 |
return CPersistent<CAIEntityPhysical>::getSpawnObj();
|
|
182 |
}
|
|
183 |
|
|
184 |
//////////////////////////////////////////////////////////////////////////////
|
|
185 |
// CModEntityPhysical //
|
|
186 |
//////////////////////////////////////////////////////////////////////////////
|
|
187 |
|
|
188 |
inline
|
|
189 |
void CModEntityPhysical::setMode(MBEHAV::EMode m)
|
|
190 |
{
|
|
191 |
if (_mode().Mode == MBEHAV::DEATH) // to remove (can be replaced with the above assertion in debug mode).
|
|
192 |
return;
|
|
193 |
|
|
194 |
if (_mode().Mode == m)
|
|
195 |
return;
|
|
196 |
_mode = MBEHAV::TMode(m, pos().x().asInt(), pos().y().asInt());
|
|
197 |
}
|
|
198 |
|
|
199 |
inline
|
|
200 |
void CModEntityPhysical::setTarget(CAIEntityPhysical* target)
|
|
201 |
{
|
|
202 |
CTargetable<CAIEntityPhysical>::setTarget(target);
|
|
203 |
// if (CTargetable<CAIEntityPhysical>::getTarget()==target)
|
|
204 |
const CAIEntityPhysical* tgt=CTargetable<CAIEntityPhysical>::getTarget();
|
|
205 |
if (tgt==target)
|
|
206 |
_targetRow = target ? target->dataSetRow() : TDataSetRow();
|
|
207 |
}
|
|
208 |
|
|
209 |
inline
|
|
210 |
void CModEntityPhysical::setVisualTarget(CAIEntityPhysical* target)
|
|
211 |
{
|
|
212 |
CTargetable<CAIEntityPhysical>::setVisualTarget(target);
|
|
213 |
// if (CTargetable<CAIEntityPhysical>::getVisualTarget()==target)
|
|
214 |
const CAIEntityPhysical* tgt=CTargetable<CAIEntityPhysical>::getVisualTarget();
|
|
215 |
if (tgt==target)
|
|
216 |
_targetRow = target ? target->dataSetRow() : TDataSetRow();
|
|
217 |
}
|
|
218 |
|
|
219 |
inline
|
|
220 |
void CModEntityPhysical::setUnreachableTarget(CAIEntityPhysical* target)
|
|
221 |
{
|
|
222 |
CTargetable<CAIEntityPhysical>::setUnreachableTarget(target);
|
|
223 |
// if (CTargetable<CAIEntityPhysical>::getUnreachableTarget()==target)
|
|
224 |
const CAIEntityPhysical* tgt=CTargetable<CAIEntityPhysical>::getUnreachableTarget();
|
|
225 |
if (tgt==target)
|
|
226 |
_targetRow = target ? target->dataSetRow() : TDataSetRow();
|
|
227 |
}
|
|
228 |
|
|
229 |
inline
|
|
230 |
bool CModEntityPhysical::setPos(CAIPos const& pos)
|
|
231 |
{
|
|
232 |
RYAI_MAP_CRUNCH::CWorldPosition wpos;
|
|
233 |
if (!CWorldContainer::getWorldMap().setWorldPosition(AITYPES::vp_auto, wpos,pos))
|
|
234 |
return false;
|
|
235 |
setPos(pos, wpos);
|
|
236 |
return true;
|
|
237 |
}
|
|
238 |
|
|
239 |
inline
|
|
240 |
bool CModEntityPhysical::moveTo(CAIPos const& newPos, RYAI_MAP_CRUNCH::TAStarFlag const& denyFlags)
|
|
241 |
{
|
|
242 |
if (!wpos().isValid())
|
|
243 |
return false;
|
|
244 |
|
|
245 |
RYAI_MAP_CRUNCH::CWorldPosition tmpWPos(wpos());
|
|
246 |
if (CWorldContainer::getWorldMap().move(tmpWPos, RYAI_MAP_CRUNCH::CMapPosition(newPos), denyFlags))
|
|
247 |
{
|
|
248 |
#ifdef NL_DEBUG
|
|
249 |
nlassert(RYAI_MAP_CRUNCH::CMapPosition(newPos)==tmpWPos);
|
|
250 |
#endif
|
|
251 |
setPos(newPos, tmpWPos);
|
|
252 |
return true;
|
|
253 |
}
|
|
254 |
else
|
|
255 |
{
|
|
256 |
// a part of move has been done.
|
|
257 |
if (tmpWPos!=wpos())
|
|
258 |
{
|
|
259 |
CAIPos partNewPos(tmpWPos.toAIVector(),newPos.h(),newPos.theta());
|
|
260 |
setPos(partNewPos, tmpWPos);
|
|
261 |
return true;
|
|
262 |
}
|
|
263 |
|
|
264 |
}
|
|
265 |
// no move at all..
|
|
266 |
return false;
|
|
267 |
};
|
|
268 |
|
|
269 |
inline
|
|
270 |
void CModEntityPhysical::resetDecalage()
|
|
271 |
{
|
|
272 |
_Decalage.setXY(0, 0);
|
|
273 |
}
|
|
274 |
|
|
275 |
//////////////////////////////////////////////////////////////////////////////
|
|
276 |
// CWorldMapLink //
|
|
277 |
//////////////////////////////////////////////////////////////////////////////
|
|
278 |
|
|
279 |
template <class T>
|
|
280 |
void CWorldMapLink<T>::linkEntityToMatrix(const CAICoord &x,const CAICoord &y,CAIEntityMatrix<T>& matrix)
|
|
281 |
{
|
|
282 |
_matrixListLink.link(matrix[(uint8)(y.asInt16Meters())][(uint8)(x.asInt16Meters())]);
|
|
283 |
}
|
|
284 |
|
|
285 |
template <class T>
|
|
286 |
void CWorldMapLink<T>::linkEntityToMatrix(const CAIVectorMirror &pos,CAIEntityMatrix<T>& matrix)
|
|
287 |
{
|
|
288 |
linkEntityToMatrix(pos.x(),pos.y(),matrix);
|
|
289 |
}
|
|
290 |
|
|
291 |
template <class T>
|
|
292 |
void CWorldMapLink<T>::linkEntityToMatrix(const CAIVector &pos,CAIEntityMatrix<T>& matrix)
|
|
293 |
{
|
|
294 |
linkEntityToMatrix(pos.x(),pos.y(),matrix);
|
|
295 |
}
|
|
296 |
|
|
297 |
template <class T>
|
|
298 |
void CWorldMapLink<T>::linkToWorldMap(T *entityPtr, const CAIVector &pos,CAIEntityMatrix<T>& matrix)
|
|
299 |
{
|
|
300 |
_matrixListLink.setEntity (entityPtr);
|
|
301 |
linkEntityToMatrix (pos,matrix);
|
|
302 |
}
|
|
303 |
|
|
304 |
template <class T>
|
|
305 |
void CWorldMapLink<T>::linkToWorldMap(T *entityPtr, const CAIVectorMirror &pos,CAIEntityMatrix<T>& matrix)
|
|
306 |
{
|
|
307 |
_matrixListLink.setEntity (entityPtr);
|
|
308 |
linkEntityToMatrix (pos,matrix);
|
|
309 |
}
|
|
310 |
|
|
311 |
//////////////////////////////////////////////////////////////////////////////
|
|
312 |
// CTargetable //
|
|
313 |
//////////////////////////////////////////////////////////////////////////////
|
|
314 |
|
|
315 |
template <class T>
|
|
316 |
CTargetable<T>::CTargetable()
|
|
317 |
: _FightTargetersWeight(0.f)
|
|
318 |
, _FightTargetersValue(0.f)
|
|
319 |
{
|
|
320 |
for (size_t i=0; i<TARGET_TYPE_MAX; ++i)
|
|
321 |
{
|
|
322 |
_TargeterCount[i] = 0;
|
|
323 |
_FirstTargeters[i] = (T*)NULL;
|
|
324 |
}
|
|
325 |
_Target = (T*)NULL;
|
|
326 |
_TargetType = TARGET_TYPE_FIGHT;
|
|
327 |
_NextTargeter = (T*)NULL;
|
|
328 |
}
|
|
329 |
|
|
330 |
template <class T>
|
|
331 |
CTargetable<T>::~CTargetable()
|
|
332 |
{
|
|
333 |
// This should be done in Derived class dtor, but just in case of since we're instanciable
|
|
334 |
detachFromTargeting();
|
|
335 |
}
|
|
336 |
|
|
337 |
template <class T>
|
|
338 |
void CTargetable<T>::detachFromTargeters()
|
|
339 |
{
|
|
340 |
for (size_t i=0; i<TARGET_TYPE_MAX; ++i)
|
|
341 |
{
|
|
342 |
while (!_FirstTargeters[i].isNULL())
|
|
343 |
{
|
|
344 |
TPtr targeter = _FirstTargeters[i];
|
|
345 |
targeter->targetDied();
|
|
346 |
// This method is a bit more intrusive than targeter->setTarget(NULL), but it avoids possible infinite loops
|
|
347 |
_FirstTargeters[i] = targeter->_NextTargeter;
|
|
348 |
unlinkTargeter((TTargetType)i, targeter);
|
|
349 |
}
|
|
350 |
}
|
|
351 |
}
|
|
352 |
|
|
353 |
template <class T>
|
|
354 |
void CTargetable<T>::detachFromTargeting()
|
|
355 |
{
|
|
356 |
setTarget((T*)NULL);
|
|
357 |
detachFromTargeters();
|
|
358 |
}
|
|
359 |
|
|
360 |
template <class T>
|
|
361 |
void CTargetable<T>::linkTargeter(TTargetType type, TPtr const& targeter, TPtr const& nextTargeter)
|
|
362 |
{
|
|
363 |
++_TargeterCount[type];
|
|
364 |
if (type==TARGET_TYPE_FIGHT)
|
|
365 |
{
|
|
366 |
_FightTargetersWeight += targeter->fightWeight();
|
|
367 |
_FightTargetersValue += targeter->fightValue();
|
|
368 |
}
|
|
369 |
targeter->_Target = dynamic_cast<T*>(this);
|
|
370 |
targeter->_TargetType = type;
|
|
371 |
targeter->_NextTargeter = nextTargeter;
|
|
372 |
// Some verifications to avoid algorithm bugs
|
|
373 |
// nlassert(fightTargetersFreeWeight()>=-0.00001f);
|
|
374 |
nlassert(!targeter->_Target.isNULL());
|
|
375 |
}
|
|
376 |
|
|
377 |
template <class T>
|
|
378 |
void CTargetable<T>::unlinkTargeter(TTargetType type, TPtr const& targeter)
|
|
379 |
{
|
|
380 |
#ifdef NL_DEBUG
|
|
381 |
nlassert((T*)targeter->_Target == this);
|
|
382 |
#else
|
|
383 |
if ((T*)targeter->_Target != this)
|
|
384 |
nlwarning("Targeter's target is not me!");
|
|
385 |
#endif
|
|
386 |
targeter->_Target = (T*)NULL;
|
|
387 |
targeter->_NextTargeter = (T*)NULL;
|
|
388 |
--_TargeterCount[type];
|
|
389 |
if (type==TARGET_TYPE_FIGHT)
|
|
390 |
{
|
|
391 |
_FightTargetersWeight -= targeter->fightWeight();
|
|
392 |
_FightTargetersValue -= targeter->fightValue();
|
|
393 |
}
|
|
394 |
}
|
|
395 |
|
|
396 |
template <class T>
|
|
397 |
void CTargetable<T>::addTargeter(TTargetType type, TPtr const& targeter)
|
|
398 |
{
|
|
399 |
linkTargeter(type, targeter, _FirstTargeters[type]);
|
|
400 |
_FirstTargeters[type] = targeter;
|
|
401 |
}
|
|
402 |
|
|
403 |
template <class T>
|
|
404 |
void CTargetable<T>::removeTargeter(TTargetType type, TPtr const& targeter)
|
|
405 |
{
|
|
406 |
if (targeter.isNULL())
|
|
407 |
return;
|
|
408 |
if (_FirstTargeters[type] == targeter)
|
|
409 |
{
|
|
410 |
_FirstTargeters[type] = targeter->_NextTargeter;
|
|
411 |
unlinkTargeter(type, targeter);
|
|
412 |
return;
|
|
413 |
}
|
|
414 |
|
|
415 |
NLMISC::CDbgPtr<T> current = _FirstTargeters[type];
|
|
416 |
while (!current.isNULL()) {
|
|
417 |
NLMISC::CDbgPtr<T> next = current->_NextTargeter;
|
|
418 |
if (next == targeter)
|
|
419 |
{
|
|
420 |
current->_NextTargeter = targeter->_NextTargeter;
|
|
421 |
unlinkTargeter(type, targeter);
|
|
422 |
break;
|
|
423 |
}
|
|
424 |
else
|
|
425 |
{
|
|
426 |
current = next;
|
|
427 |
}
|
|
428 |
}
|
|
429 |
}
|
|
430 |
|
|
431 |
template <class T>
|
|
432 |
void CTargetable<T>::tryToAddTargeter(TTargetType type, TPtr const& targeter)
|
|
433 |
{
|
|
434 |
H_AUTO(CTargetable_addTargeter);
|
|
435 |
|
|
436 |
if (type==TARGET_TYPE_FIGHT)
|
|
437 |
{
|
|
438 |
|
|
439 |
// Prepare data to optimize ----------------------------------------------
|
|
440 |
size_t opponentCount = targeterCount();
|
|
441 |
// Allocate arrays filled with 0.f
|
|
442 |
float* weights = new float[opponentCount+1];
|
|
443 |
float* values = new float[opponentCount+1];
|
|
444 |
bool* take = new bool[opponentCount+1];
|
|
445 |
for (size_t i=0; i<opponentCount+1; ++i)
|
|
446 |
{
|
|
447 |
weights[i] = 0.f;
|
|
448 |
values[i] = 0.f;
|
|
449 |
take[i] = false;
|
|
450 |
}
|
|
451 |
{
|
|
452 |
H_AUTO(addTargeterInfoRetr);
|
|
453 |
// Fill with the former opponents
|
|
454 |
T* opponent = firstTargeter();
|
|
455 |
size_t i = 0;
|
|
456 |
while (opponent!=NULL)
|
|
457 |
{
|
|
458 |
weights[i] = opponent->fightWeight();
|
|
459 |
values[i] = opponent->fightValue();
|
|
460 |
opponent = opponent->nextTargeter();
|
|
461 |
take[i] = true;
|
|
462 |
++i;
|
|
463 |
}
|
|
464 |
// Add the new targeter
|
|
465 |
weights[opponentCount] = targeter->fightWeight();
|
|
466 |
values[opponentCount] = targeter->fightValue();
|
|
467 |
take[opponentCount] = false;
|
|
468 |
}
|
|
469 |
// Create the context
|
|
470 |
CKnapsackContext context(opponentCount+1, weights, values);
|
|
471 |
// Configure and run the solver ------------------------------------------
|
|
472 |
CKnapsackSolver solver(&context, take);
|
|
473 |
{
|
|
474 |
H_AUTO(addTargeterOptimize);
|
|
475 |
solver.optimize(fightTargetersWeightMax(), _TargeterChoiceAlgorithm);
|
|
476 |
}
|
|
477 |
// Save the data ---------------------------------------------------------
|
|
478 |
// If the new set is better than the old one
|
|
479 |
if (solver.totalValue()>fightTargetersValue())
|
|
480 |
{
|
|
481 |
// Unlink the old ones if needed
|
|
482 |
T* opponent = _FirstTargeters[type];
|
|
483 |
TPtr* previousPtr = &_FirstTargeters[type];
|
|
484 |
size_t i = 0;
|
|
485 |
while (opponent!=NULL)
|
|
486 |
{
|
|
487 |
if (!solver.take(i))
|
|
488 |
{
|
|
489 |
*previousPtr = opponent->_NextTargeter;
|
|
490 |
unlinkTargeter(type, opponent);
|
|
491 |
}
|
|
492 |
else
|
|
493 |
{
|
|
494 |
previousPtr = &opponent->_NextTargeter;
|
|
495 |
opponent = opponent->_NextTargeter;
|
|
496 |
}
|
|
497 |
++i;
|
|
498 |
}
|
|
499 |
// Link the new targeter if needed
|
|
500 |
if (solver.take(opponentCount))
|
|
501 |
{
|
|
502 |
addTargeter(type, targeter);
|
|
503 |
}
|
|
504 |
#ifdef NL_DEBUG
|
|
505 |
// Debug verifications
|
|
506 |
{
|
|
507 |
opponent = _FirstTargeters[type];
|
|
508 |
float value = 0.f;
|
|
509 |
float weight = 0.f;
|
|
510 |
while (opponent!=NULL)
|
|
511 |
{
|
|
512 |
value += opponent->fightValue();
|
|
513 |
weight += opponent->fightWeight();
|
|
514 |
opponent = opponent->_NextTargeter;
|
|
515 |
}
|
|
516 |
// Verify linking system
|
|
517 |
nlassert(fabs(fightTargetersValue() - value) < 0.0001);
|
|
518 |
nlassert(fabs(fightTargetersWeight() - weight) < 0.0001);
|
|
519 |
|
|
520 |
// Verify solver behaviour
|
|
521 |
nlassert(fabs(fightTargetersValue() - solver.totalValue()) < 0.0001);
|
|
522 |
nlassert(fabs(fightTargetersWeight() - solver.totalWeight()) < 0.0001);
|
|
523 |
}
|
|
524 |
#endif
|
|
525 |
}
|
|
526 |
delete [] take;
|
|
527 |
delete [] values;
|
|
528 |
delete [] weights;
|
|
529 |
|
|
530 |
}
|
|
531 |
else
|
|
532 |
{
|
|
533 |
// Just link the new targeter (no limit on visual targeters)
|
|
534 |
addTargeter(type, targeter);
|
|
535 |
}
|
|
536 |
}
|
|
537 |
|
|
538 |
template <class T>
|
|
539 |
void CTargetable<T>::setTarget(T* target)
|
|
540 |
{
|
|
541 |
setTarget(TARGET_TYPE_FIGHT, target);
|
|
542 |
}
|
|
543 |
|
|
544 |
template <class T>
|
|
545 |
void CTargetable<T>::setVisualTarget(T* target)
|
|
546 |
{
|
|
547 |
setTarget(TARGET_TYPE_VISUAL, target);
|
|
548 |
}
|
|
549 |
|
|
550 |
template <class T>
|
|
551 |
void CTargetable<T>::setUnreachableTarget(T* target)
|
|
552 |
{
|
|
553 |
setTarget(TARGET_TYPE_UNREACHABLE, target);
|
|
554 |
}
|
|
555 |
|
|
556 |
template <class T>
|
|
557 |
void CTargetable<T>::setTarget(TTargetType type, TPtr const& target)
|
|
558 |
{
|
|
559 |
if (target==_Target && type==_TargetType)
|
|
560 |
return;
|
|
561 |
|
|
562 |
if (!_Target.isNULL())
|
|
563 |
{
|
|
564 |
_Target->removeTargeter(_TargetType, dynamic_cast<T*>(this));
|
|
565 |
}
|
|
566 |
if (!target.isNULL())
|
|
567 |
{
|
|
568 |
target->tryToAddTargeter(type, dynamic_cast<T*>(this));
|
|
569 |
}
|
|
570 |
}
|
|
571 |
|
|
572 |
template <class T>
|
|
573 |
typename CTargetable<T>::TPtr CTargetable<T>::getTarget() const
|
|
574 |
{
|
|
575 |
return getTarget(TARGET_TYPE_FIGHT);
|
|
576 |
}
|
|
577 |
|
|
578 |
template <class T>
|
|
579 |
typename CTargetable<T>::TPtr CTargetable<T>::getVisualTarget() const
|
|
580 |
{
|
|
581 |
return getTarget(TARGET_TYPE_VISUAL);
|
|
582 |
}
|
|
583 |
|
|
584 |
template <class T>
|
|
585 |
typename CTargetable<T>::TPtr CTargetable<T>::getUnreachableTarget() const
|
|
586 |
{
|
|
587 |
return getTarget(TARGET_TYPE_UNREACHABLE);
|
|
588 |
}
|
|
589 |
|
|
590 |
template <class T>
|
|
591 |
typename CTargetable<T>::TPtr CTargetable<T>::getTarget(TTargetType type) const
|
|
592 |
{
|
|
593 |
if (_TargetType==type)
|
|
594 |
return _Target;
|
|
595 |
else
|
|
596 |
return (T*)NULL;
|
|
597 |
}
|
|
598 |
|
|
599 |
template <class T>
|
|
600 |
uint32 CTargetable<T>::totalTargeterCount() const
|
|
601 |
{
|
|
602 |
uint32 count = 0;
|
|
603 |
for (size_t i = 0; i<TARGET_TYPE_MAX; ++i)
|
|
604 |
count += _TargeterCount[i];
|
|
605 |
return count;
|
|
606 |
}
|
|
607 |
|
|
608 |
//----------------------------------------------------------------------------
|
|
609 |
|
|
610 |
#endif
|
|
611 |
|