Resolve "Toggle Free Look with Hotkey"
[ryzomcore.git] / ryzom / server / src / ai_service / ai_generic_fight.h
blob9ff94de78db8bdfe2e131af5cc2ff30f7cc77db2
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.
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/>.
19 #ifndef RYAI_GENERIC_FIGHT_H
20 #define RYAI_GENERIC_FIGHT_H
22 #include "profile.h"
23 #include "sheets.h"
24 #include "path_behaviors.h"
27 // Forward declarations
28 class CAIEntityPhysical;
29 class CModEntityPhysical;
30 class CBotPlayer;
32 // a big bad global var !
33 extern CAIEntityPhysical *TempSpeaker;
34 extern CBotPlayer *TempPlayer;
35 extern float getDistBetWeen(CAIEntityPhysical& creat1, CAIEntityPhysical& creat2);
37 template <class TVectorSrc, class TVectorDst>
38 float buildDecalage(TVectorSrc const& src, TVectorDst const& dst, CAIVector* decalage)
40 if (decalage!=NULL)
42 CAIVector& vect = *decalage;
43 vect = dst;
44 vect -= src;
45 float normXY = (float)vect.norm();
46 float normZ = (dst.h()-src.h())/1000.f;
47 // :KLUDGE: This is a dirty dirty hack
48 // As z precision is 2m, entities can be near contact but considered 2m away if they are on differents PACS planes.
49 // So we bring back all z dists 2m closer to 0.
50 if (normZ > 2.f) normZ -= 2.f;
51 else if (normZ < -2.f) normZ += 2.f;
52 else normZ = 0.f;
53 // End of hack
54 float normXYZ = (float)sqrt(normXY*normXY + normZ*normZ);
55 return normXYZ;
57 else
59 CAIVector vect;
60 return buildDecalage(src, dst, &vect);
64 //////////////////////////////////////////////////////////////////////////////
65 // CBotProfileFightHeal //
66 //////////////////////////////////////////////////////////////////////////////
67 class CBotProfileFightHeal
68 : public CAIBaseProfile
70 public:
71 CBotProfileFightHeal() : CAIBaseProfile() { }
72 virtual void eventTargetKilled() = 0;
73 virtual void noMoreTarget() = 0;
75 public:
76 /// @name Fight ranges/distances parameters
77 //@{
78 static float fightDists[AISHEETS::FIGHTCFG_MAX];
79 static float fightDefaultMinRange;
80 static float fightDefaultMaxRange;
81 static float fightMeleeMinRange;
82 static float fightMeleeMaxRange;
83 static float fightRangeMinRange;
84 static float fightRangeMaxRange;
85 static float fightMixedMinRange;
86 static float fightMixedMaxRange;
87 static float giveUpDistance;
88 static bool fleeUnreachableTargets;
89 //@}
92 //////////////////////////////////////////////////////////////////////////////
93 // CBotProfileFight //
94 //////////////////////////////////////////////////////////////////////////////
96 /// Generic fight profile class
97 class CBotProfileFight
98 : public CBotProfileFightHeal
99 , public NLMISC::IDbgPtrData
101 public:
102 /// @name Constructors, destructor
103 //@{
104 CBotProfileFight(CProfileOwner* owner, CAIEntityPhysical* ennemy);
105 virtual ~CBotProfileFight();
106 //@}
108 /// @name Event handlers
109 //@{
110 virtual void eventBeginFight() = 0;
111 virtual void eventTargetKilled() = 0;
112 //@}
114 /// @name IAIProfile implementation
115 //@{
116 virtual void beginProfile();
117 virtual void resumeProfile();
118 virtual void endProfile();
119 virtual void updateProfile(uint ticksSinceLastUpdate);
120 virtual std::string getOneLineInfoString() const;
121 //@}
123 virtual void noMoreTarget() = 0;
125 public:
126 /// @name Accessors
127 //@{
128 bool isHitting() const { return _Bot->isHitting(); }
129 bool atAttackDist() const { return _AtAttackDist; }
130 virtual AITYPES::TProfiles getAIProfileType() const { return AITYPES::BOT_FIGHT; }
131 //@}
133 protected:
134 NLMISC::CDbgPtr<CSpawnBot> _Bot;
135 NLMISC::CRefPtr<CAIEntityPhysical> _Ennemy;
137 bool _Engaged;
139 CPathPosition _PathPos;
140 CPathCont _PathCont;
142 bool _RangeCalculated;
143 bool _UseFightConfig;
144 float _RangeMin;
145 float _RangeMax;
147 bool _AtAttackDist;
149 bool _SearchAlternativePath;
152 //////////////////////////////////////////////////////////////////////////////
153 // CBotProfileHeal //
154 //////////////////////////////////////////////////////////////////////////////
156 /// Generic fight profile class
157 class CBotProfileHeal
158 : public CBotProfileFightHeal
160 public:
161 /// @name Constructors, destructor
162 //@{
163 CBotProfileHeal(TDataSetRow const& row, CProfileOwner* owner);
164 virtual ~CBotProfileHeal();
165 //@}
167 /// @name Event handlers
168 //@{
169 // virtual void eventBeginFight() = 0;
170 virtual void eventTargetKilled() { nlwarning("eventTargetKilled called on a heal profile"); }
171 //@}
173 /// @name IAIProfile implementation
174 //@{
175 virtual void beginProfile();
176 virtual void resumeProfile();
177 virtual void endProfile();
178 virtual void updateProfile(uint ticksSinceLastUpdate);
179 virtual std::string getOneLineInfoString() const;
180 //@}
182 virtual void noMoreTarget() = 0;
184 public:
185 /// @name Accessors
186 //@{
187 bool isHitting() const { return (_Bot->getActionFlags()&RYZOMACTIONFLAGS::Attacks)!=0; }
188 bool atAttackDist() const { return _AtAttackDist; }
189 virtual AITYPES::TProfiles getAIProfileType() const { return AITYPES::BOT_HEAL; }
190 //@}
192 protected:
193 NLMISC::CDbgPtr<CSpawnBot> _Bot;
195 bool _Engaged;
197 CPathPosition _PathPos;
198 CPathCont _PathCont;
199 TDataSetRow _Row;
201 bool _RangeCalculated;
202 bool _UseFightConfig;
203 float _RangeMin;
204 float _RangeMax;
206 bool _AtAttackDist;
208 bool _SearchAlternativePath;
211 //////////////////////////////////////////////////////////////////////////////
212 // CBotProfileFlee //
213 //////////////////////////////////////////////////////////////////////////////
215 class CBotProfileFlee
216 : public CAIBaseProfile
218 public:
219 /// @name Constructor and destructor
220 //@{
221 CBotProfileFlee(CProfileOwner *owner);
222 virtual ~CBotProfileFlee();
223 //@}
225 /// @name IAIProfile implementation
226 //@{
227 virtual void beginProfile();
228 virtual void updateProfile(uint ticksSinceLastUpdate);
229 virtual void endProfile() { }
230 virtual AITYPES::TProfiles getAIProfileType () const { return AITYPES::BOT_FLEE; }
231 virtual std::string getOneLineInfoString() const;
232 //@}
234 private:
235 RYAI_MAP_CRUNCH::CDirection _LastDir;
236 RYAI_MAP_CRUNCH::CMapPosition _LastStartPos;
238 RYAI_MAP_CRUNCH::TAStarFlag _DenyFlags;
240 CPathPosition _PathPos;
241 CPathCont _fightFleePathContainer;
243 public:
244 /// @name Flee ranges/distances parameters
245 //@{
246 static float giveUpDistanceUnreachable;
247 //@}
249 protected:
250 NLMISC::CDbgPtr<CSpawnBot> _Bot;
253 //////////////////////////////////////////////////////////////////////////////
254 // CBotProfileReturnAfterFight //
255 //////////////////////////////////////////////////////////////////////////////
257 class CBotProfileFollowPos;
259 class CBotProfileReturnAfterFight
260 : public CAIBaseProfile
262 public:
263 /// @name Constructor and destructor
264 //@{
265 CBotProfileReturnAfterFight(CProfileOwner *owner);
266 virtual ~CBotProfileReturnAfterFight();
267 //@}
269 /// @name IAIProfile implementation
270 //@{
271 virtual void beginProfile();
272 virtual void updateProfile(uint ticksSinceLastUpdate);
273 virtual void endProfile();
274 virtual AITYPES::TProfiles getAIProfileType () const { return AITYPES::BOT_RETURN_AFTER_FIGHT; }
275 virtual std::string getOneLineInfoString() const;
276 virtual void stateChangeProfile();
277 //@}
279 private:
281 RYAI_MAP_CRUNCH::CDirection _LastDir;
282 RYAI_MAP_CRUNCH::CMapPosition _LastStartPos;
284 RYAI_MAP_CRUNCH::TAStarFlag _DenyFlags;
286 CPathPosition _PathPos;
287 CPathCont _fightFleePathContainer;
289 // CPathCont _PathCont;
290 // NLMISC::CSmartPtr<CBotProfileFollowPos> _MoveProfile;
292 public:
293 /// @name Flee ranges/distances parameters
294 //@{
295 // static float giveUpDistanceUnreachable;
296 //@}
298 protected:
299 NLMISC::CDbgPtr<CSpawnBot> _Bot;
302 //////////////////////////////////////////////////////////////////////////////
303 // CFightOrganizer //
304 //////////////////////////////////////////////////////////////////////////////
306 class CFightOrganizer
308 public:
309 CFightOrganizer();
311 template <class TContainerIterator>
312 void reorganize(TContainerIterator const& first, TContainerIterator const& last)
314 H_AUTO(CFightOrganizer_reorganize);
315 _HaveEnnemy = false;
317 for (TContainerIterator it=first, itEnd=last;it!=itEnd;++it)
319 CBot* bot = (*it);
320 if (!reorganizeIteration(bot))
321 break;
325 /// @name Virtual interface
326 //@{
327 virtual void setFight(CSpawnBot* bot, CAIEntityPhysical* ennemy) = 0;
328 virtual void setHeal(CSpawnBot* bot, CAIEntityPhysical* target) = 0;
329 virtual void setNoFight(CSpawnBot* bot) = 0;
330 virtual void setFlee(CSpawnBot* bot, CAIVector& fleeVect) = 0;
331 virtual void setReturnAfterFight(CSpawnBot* bot) = 0;
332 //@}
334 private:
335 bool healIteration(CBot* bot, CBot* otherBot);
336 bool reorganizeIteration(CBot* bot);
338 protected:
339 bool _HaveEnnemy;
342 /****************************************************************************/
343 /* Profile factories */
344 /****************************************************************************/
346 //////////////////////////////////////////////////////////////////////////////
347 // CBotProfileFightFactory //
348 //////////////////////////////////////////////////////////////////////////////
350 class CBotProfileFightFactory
351 : public IAIProfileFactory
353 public:
354 NLMISC::CSmartPtr<IAIProfile> createAIProfile(CProfileOwner* owner)
356 nlassert(false);
357 return NULL;
360 RYAI_DECLARE_PROFILE_FACTORY(CBotProfileFightFactory);
362 //////////////////////////////////////////////////////////////////////////////
363 // CBotProfileFleeFactory //
364 //////////////////////////////////////////////////////////////////////////////
366 class CBotProfileFleeFactory
367 : public IAIProfileFactory
369 public:
370 NLMISC::CSmartPtr<IAIProfile> createAIProfile(CProfileOwner* owner)
372 nlassert(false);
373 return NULL;
376 RYAI_DECLARE_PROFILE_FACTORY(CBotProfileFleeFactory);
378 #endif