spawnBots accept now name and vpx, addedspawnGroup_ffsssffff_c
[ryzomcore.git] / ryzom / server / src / ai_service / ai_grp.h
blob440967206c8b9c932b2166a6317ee7c27121ef06
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_GRP_H
20 #define RYAI_GRP_H
22 #include "persistent_spawnable.h"
23 #include "ai_mgr.h"
24 #include "ai_bot.h"
25 #include "keyword_owner.h"
26 #include "debug_history.h"
27 #include "game_share/misc_const.h"
28 #include "profile_in_state.h"
29 #include "ai_aggro.h"
30 #include "dyn_grp.h"
31 #include "service_dependencies.h"
33 // forward decl
34 class CDebugHistory;
35 class CGroup;
36 class CBot;
37 class CSpawnBot;
38 class CPersistentStateInstance;
40 extern bool GrpHistoryRecordLog;
42 //////////////////////////////////////////////////////////////////////////////
43 // CSpawnGroup //
44 //////////////////////////////////////////////////////////////////////////////
46 /// This is a common parent for all grp classes (bot group classes)
47 /// Group contains a container of Bots.
48 class CSpawnGroup
49 : public NLMISC::CDbgRefCount<CSpawnGroup>
50 , public CSpawnable<CPersistent<CSpawnGroup> >
51 , public CProfileOwner
52 , public CProfileParameters
54 public:
55 CSpawnGroup(CPersistent<CSpawnGroup>& owner);
57 virtual ~CSpawnGroup();
59 virtual void spawnBotOfGroup();
61 void aggroLost(TDataSetRow const& aggroBot) const { }
63 void aggroGain(TDataSetRow const& aggroBot) const { }
65 virtual void spawnBots() = 0;
66 virtual void spawnBots(const std::string &name) = 0;
67 virtual void spawnBots(const std::string &name, const std::string &vpx) = 0;
68 virtual void despawnBots(bool immediately) = 0;
70 virtual void update() = 0;
72 // Update Rate feature.
73 virtual int getUpdatePriority() const { return 0; }
75 virtual void recalcUpdatePriorityDelta() { }
77 CGroup& getPersistent() const;
79 CAliasCont<CBot> const& bots() const;
80 CAliasCont<CBot>& bots();
82 CBot* findLeader();
84 CProfilePtr& movingProfile() { return _MovingProfile; }
85 CProfilePtr& activityProfile() { return _ActivityProfile; }
86 CProfilePtr const& activityProfile() const { return _ActivityProfile; }
87 CProfilePtr& fightProfile() { return _FightProfile; }
89 /// only for use by State Machine (or problems will occurs -> callStateChanged if same )
90 void setMoveProfileFromStateMachine(IAIProfileFactory* staticProfile);
91 /// only for use by State Machine (or problems will occurs -> callStateChanged if same )
92 void setActivityProfileFromStateMachine(IAIProfileFactory* staticProfile);
95 bool calcCenterPos(CAIVector& grp_pos, bool allowDeadBot = false);
97 // Respawn bot list
98 class CBotToSpawn
100 private:
101 friend class CSpawnGroup;
102 CBotToSpawn(uint32 botIndex, uint32 despawnTime, uint32 respawnTime)
103 : _botIndex(botIndex)
105 _despawnTimer.set(despawnTime);
106 _respawnTimer.set(respawnTime);
108 bool waitingDespawnTimeOver() const
110 return _despawnTimer.test();
112 bool waitingRespawnTimeOver() const
114 return _respawnTimer.test();
116 uint32 getBotIndex() const
118 return _botIndex;
120 CAITimer& respawnTimer()
122 return _respawnTimer;
124 uint32 _botIndex;
125 CAITimer _despawnTimer;
126 CAITimer _respawnTimer;
129 void incSpawnedBot(CBot& spawnBot);
131 void decSpawnedBot();
133 void addBotToDespawnAndRespawnTime(CBot* faunaBot, uint32 despawnTime, uint32 respawnTime);
135 void checkDespawn();
136 void checkRespawn();
138 uint32 nbSpawnedBot() const { return _NbSpawnedBot; }
140 uint32 nbBotToRespawn() const { return (uint32)_BotsToRespawn.size(); }
142 uint32 nbBotToDespawn() const { return (uint32)_BotsToDespawn.size(); }
144 bool isGroupAlive(uint32 const nbMoreKilledBot = 0) const;
146 CAIVector const& getCenterPos() const { return _CenterPos; }
148 void setCenterPos(CAIVector const& pos) { _CenterPos = pos; }
150 std::vector<std::string> getMultiLineInfoString() const;
152 virtual NLMISC::CSmartPtr<CAIPlace const> buildFirstHitPlace(TDataSetRow const& aggroBot) const;
154 void addAggroFor(TDataSetRow const& bot, float aggro, bool forceReturnAggro, NLMISC::CSmartPtr<CAIPlace const> place = NLMISC::CSmartPtr<CAIPlace const>(NULL));
155 void setAggroMinimumFor(TDataSetRow const& bot, float aggro, bool forceReturnAggro, NLMISC::CSmartPtr<CAIPlace const> place = NLMISC::CSmartPtr<CAIPlace const>(NULL));
156 bool haveAggro() const;
157 bool haveAggroOrReturnPlace() const;
160 const std::string getActionName() const { return _ActionName; }
161 void setActionName(const std::string &name) { _ActionName = name; }
163 const std::string getUrl() const { return _Url; }
164 void setUrl(const std::string &url) { _Url = url; }
166 protected:
167 CProfilePtr _PunctualHoldMovingProfile;
168 CProfilePtr _PunctualHoldActivityProfile;
170 // The group center pos (not always updated)
171 CAIVector _CenterPos;
173 private:
174 uint32 _NbSpawnedBot;
176 std::vector<CBotToSpawn> _BotsToRespawn;
177 std::vector<CBotToSpawn> _BotsToDespawn;
179 CProfilePtr _MovingProfile;
180 CProfilePtr _ActivityProfile;
181 CProfilePtr _FightProfile;
183 std::string _ActionName;
184 std::string _Url;
187 //////////////////////////////////////////////////////////////////////////////
188 // CGroup //
189 //////////////////////////////////////////////////////////////////////////////
191 class CGroup
192 : public NLMISC::CDbgRefCount<CGroup>
193 , public CPersistent<CSpawnGroup>
194 , public CAliasChild<CManager>
195 , public CAIEntity
196 , public CDebugHistory
197 , public NLMISC::CRefCount
198 , public CProfileParameters
199 , public CServiceEvent::CHandler
201 public:
202 friend class CSpawnGroup;
204 CGroup(CManager* owner, RYAI_MAP_CRUNCH::TAStarFlag denyFlag, CAIAliasDescriptionNode* aliasTree = NULL);
205 CGroup(CManager* owner, RYAI_MAP_CRUNCH::TAStarFlag denyFlag, uint32 alias, std::string const& name);
206 virtual ~CGroup();
208 void serviceEvent(CServiceEvent const& info);
210 CBot* getLeader();
211 CBot* getSquadLeader(bool checkAliveStatus = true);
213 void despawnBots();
215 virtual CDynGrpBase* getGrpDynBase() = 0;
217 CAliasTreeOwner* aliasTreeOwner() { return this; }
219 std::string botName;
220 std::string botVpx;
221 bool _AutoDestroy;
222 void autoDestroy(bool ad) { _AutoDestroy = ad; }
224 /// @name CChild implementation
225 //@{
226 virtual std::string getIndexString() const;
227 virtual std::string getOneLineInfoString() const;
228 virtual std::vector<std::string> getMultiLineInfoString() const;
229 virtual std::string getFullName() const;
230 //@}
232 virtual void lastBotDespawned();
233 virtual void firstBotSpawned();
235 virtual CPersistentStateInstance* getPersistentStateInstance() = 0;
237 RYAI_MAP_CRUNCH::TAStarFlag getAStarFlag() const { return _DenyFlags; }
239 virtual CAIS::CCounter& getSpawnCounter() = 0;
240 virtual RYZOMID::TTypeId getRyzomType() = 0;
241 virtual void setEvent(uint eventId) = 0;
243 virtual bool spawn();
245 virtual NLMISC::CSmartPtr<CSpawnGroup> createSpawnGroup() = 0;
247 virtual void despawnGrp();
249 void despawnBots(bool immediately);
251 CAliasCont<CBot> const& bots() const { return _Bots; }
252 CAliasCont<CBot>& bots() { return _Bots; }
254 void display(CStringWriter& stringWriter);
256 CBot* getBot(uint32 index);
258 // debugging stuff
259 CDebugHistory* getDebugHistory() { return this; }
261 CBot* getNextValidBotChild(CBot* child = NULL) { return _Bots.getNextValidChild(child); }
263 CManager& getManager() { return *getOwner(); }
265 void setEscortTeamId(uint16 teamId) { _EscortTeamId = teamId; }
266 uint16 getEscortTeamId() const { return _EscortTeamId; }
268 void setEscortRange(float range) { _EscortRange = range; }
269 float getEscortRange() const { return _EscortRange; }
271 virtual void setAutoSpawn(bool autoSpawn) { _AutoSpawn = autoSpawn; }
272 bool isAutoSpawn() const { return _AutoSpawn; }
274 CAIInstance* getAIInstance() const { return getOwner()->getAIInstance(); }
276 void setEventParams(const std::vector<std::string> &a) { _EventParams = a; }
277 std::string getEventParamString(uint32 i) { if (i >= _EventParams.size()) return ""; return _EventParams[i]; }
278 float getEventParamFloat(uint32 i) { if (i >= _EventParams.size()) return 0.0f; return (float)atof(_EventParams[i].c_str()); }
280 uint32 _AggroRange;
281 uint32 _UpdateNbTicks;
283 protected:
284 CAliasCont<CBot> _Bots;
285 /// Team Id of the escort (if any).
286 uint16 _EscortTeamId;
287 /// The range of the escort, ie the maximal distance of any escorter player that alow the group to be escorted
288 uint8 _EscortRange;
289 /// The bots automaticaly spawn when the group is spawned.
290 bool _AutoSpawn;
292 RYAI_MAP_CRUNCH::TAStarFlag _DenyFlags;
294 std::vector<std::string> _EventParams;
297 /****************************************************************************/
298 /* Inlined methods */
299 /****************************************************************************/
301 //////////////////////////////////////////////////////////////////////////////
302 // CSpawnGroup //
303 //////////////////////////////////////////////////////////////////////////////
305 inline
306 CSpawnGroup::CSpawnGroup(CPersistent<CSpawnGroup>& owner)
307 : CSpawnable<CPersistent<CSpawnGroup> >(owner)
308 , CProfileOwner()
309 , _NbSpawnedBot(0)
313 inline
314 void CSpawnGroup::setMoveProfileFromStateMachine(IAIProfileFactory* staticProfile)
316 _MovingProfile.setAIProfile(this, staticProfile, true);
319 inline
320 void CSpawnGroup::setActivityProfileFromStateMachine(IAIProfileFactory* staticProfile)
322 _ActivityProfile.setAIProfile(this, staticProfile, true);
325 inline
326 bool CSpawnGroup::isGroupAlive(uint32 const nbMoreKilledBot) const
328 return ((sint32)_NbSpawnedBot-(sint32)_BotsToDespawn.size()-(sint32)nbMoreKilledBot)>0;
331 inline
332 CGroup& CSpawnGroup::getPersistent() const
334 return static_cast<CGroup&>(CSpawnable<CPersistent<CSpawnGroup> >::getPersistent());
337 inline
338 CAliasCont<CBot> const& CSpawnGroup::bots() const
340 return getPersistent()._Bots;
343 inline
344 CAliasCont<CBot>& CSpawnGroup::bots()
346 return getPersistent()._Bots;
349 //////////////////////////////////////////////////////////////////////////////
350 // CGroup //
351 //////////////////////////////////////////////////////////////////////////////
353 inline
354 bool CGroup::spawn()
356 if (isSpawned())
357 return true;
359 if (!getSpawnCounter().remainToMax())
360 return false;
362 setSpawn(createSpawnGroup());
363 return true;
366 inline
367 void CGroup::despawnGrp()
369 if (!isSpawned())
370 return;
371 setSpawn(NULL);
372 if (_AutoDestroy)
373 getOwner()->groups().removeChildByIndex(getChildIndex());
376 inline
377 void CGroup::despawnBots(bool immediately)
379 if (!isSpawned())
380 return;
381 getSpawnObj()->despawnBots(immediately);
384 inline
385 CBot* CGroup::getBot(uint32 index)
387 if (index>=_Bots.size())
388 return NULL;
389 return _Bots[index];
392 #endif