1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
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.
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/>.
23 #include "nel/misc/variable.h"
24 #include "child_container.h"
25 #include "alias_tree_root.h"
26 #include "game_share/zc_shard_common.h"
27 #include "service_dependencies.h"
28 #include "state_instance.h"
29 #include "manager_parent.h"
30 #include "event_reaction_container.h"
31 #include "ai_place_patat.h"
35 class CSpireSquadFamily;
37 class CSpireSquadManager;
38 template <class FamilyT>
40 template <class FamilyT>
43 class CSpireSpawnZone;
45 extern NLMISC::CVariable<bool> LogSpireDebug;
47 //////////////////////////////////////////////////////////////////////////////
48 // Helpers functions //
49 //////////////////////////////////////////////////////////////////////////////
51 namespace SPIREHELPERS {
53 extern uint32 getEntitySpire(CAIEntityPhysical const* entity);
54 extern bool isAttackingFaction(uint32 factionIndex, CAIEntityPhysical const* player);
58 #define RYAI_DEBUG_SPIRES 1
60 #ifdef RYAI_DEBUG_SPIRES
62 #define SPIRE_DBG nldebug
63 #define SPIRE_INF nlinfo
64 #define SPIRE_WRN nlwarning
65 #define SPIRE_ERR nlerror
69 extern NLMISC::CLog SpireDbgLog, SpireInfLog, SpireWrnLog, SpireErrLog;
70 #define SPIRE_DBG SpireDbgLog.setPosition( __LINE__, __FILE__, __FUNCTION__ ), SpireDbgLog.displayNL
71 #define SPIRE_INF SpireInfLog.setPosition( __LINE__, __FILE__, __FUNCTION__ ), SpireInfLog.displayNL
72 #define SPIRE_WRN SpireWrnLog.setPosition( __LINE__, __FILE__, __FUNCTION__ ), SpireWrnLog.displayNL
73 #define SPIRE_ERR SpireErrLog.setPosition( __LINE__, __FILE__, __FUNCTION__ ), SpireErrLog.displayNL
77 //////////////////////////////////////////////////////////////////////////////
79 //////////////////////////////////////////////////////////////////////////////
82 : public NLMISC::CDbgRefCount<CSpire>
83 , public CAliasChild<CContinent>
84 , public NLMISC::CRefCount
85 , public CAliasTreeRoot
87 , public IManagerParent
91 typedef std::map< TAIAlias, NLMISC::CSmartPtr<CGroupDesc<CSpireSquadFamily> > > CSquadLinks;
93 /// Return the spire corresponding to the alias, or NULL if not found (with a nlwarning)
94 static CSpire* getSpireByAlias(TAIAlias spireAlias);
96 CSpire(CContinent* owner, uint32 alias, std::string const& name, std::string const& filename);
99 /// @name CChild implementation
101 virtual std::string getIndexString() const;
102 virtual std::string getOneLineInfoString() const;
103 // virtual std::vector<std::string> getMultiLineInfoString() const;
104 virtual std::string getFullName() const;
105 // virtual std::string getName() const;
108 /// @name IManagerParent implementation
110 virtual CAIInstance* getAIInstance() const;
111 virtual CCellZone* getCellZone() { return NULL; }
112 // virtual std::string getIndexString() const = 0;
113 virtual std::string getManagerIndexString(CManager const* manager) const;
114 virtual void groupDead(CGroup* grp) { }
117 /// @name Children containers accessors
119 CAliasCont<CSpireSpawnZone>& spawnZones() { return _SpawnZones; }
120 CAliasCont<CSpireSpawnZone> const& spawnZones() const { return _SpawnZones; }
121 CAliasCont<CSpireManager>& managers() { return _Managers; }
122 CAliasCont<CSpireManager> const& managers() const { return _Managers; }
127 NLMISC::CSmartPtr<CAIPlaceSpire> const& getShape() const { return _Shape; }
128 void setShape(NLMISC::CSmartPtr<CAIPlaceSpire> const& shape) { _Shape = shape; }
131 /// Returns the group containing building bots
132 CGroup* getBuildingGroup();
133 /// Returns the building bot with the specified alias
134 CBot* getBuildingBotByAlias(TAIAlias alias);
136 /// Set the tribe that can own the spire when no guild owns it
137 void setTribe(std::string const& tribeName);
139 std::string const& getTribe() const { return _Tribe; }
141 /// Return the guild defending the spire, or InvalidGuildId
142 TAllianceId getOwnerAlliance() const { return _OwnerAllianceId; }
144 /// Assign the spire to a guild (defender) or to a tribe with InvalidAllianceId.
145 void setOwnerAlliance( TAllianceId ownerAllianceId );
147 /// Set the attacker guilds (opponent and its allies) of the spire
148 void setAttackerAlliance( TAllianceId attackerAllianceId );
150 /// Set the EGS state of the spire
151 void setState( SPIREENUMS::TSpireState state );
153 /// Manages service event (typically service ups and downs)
154 void serviceEvent(CServiceEvent const& info);
156 std::string const& getSpireName() const { return _SpireName; }
158 /// Called regularly at low frequency by continent update
161 void addZone(CSpireSpawnZone* zone);
162 void removeZone(CSpireSpawnZone* zone);
163 CSpireSpawnZone* getZone(NLMISC::TStringId zoneName);
165 void createSquad(CGroupDesc<CSpireSquadFamily> const* groupDesc, CSpireSquadManager* manager, std::string const& initialStateName, CSpireSpawnZone const* spawnZone, uint32 spawnOrder, uint32 respawTimeGC, SPIREENUMS::TPVPSide side);
166 void createSquad(std::string const& dynGroupName, std::string const& stateMachineName, std::string const& initialStateName, std::string const& zoneName, uint32 spawnOrder, uint32 respawTimeGC, SPIREENUMS::TPVPSide side);
167 void createSquad(uint32 dynGroupAlias, uint32 zoneAlias, uint32 spawnOrder, uint32 respawTimeGC, SPIREENUMS::TPVPSide side);
168 void spawnSquad(uint32 groupId);
169 void despawnSquad(uint32 groupId);
170 void deleteSquad(uint32 groupId);
171 void despawnAllSquads();
172 void sendSpireSquadStatus(CGroupNpc* group);
173 void squadLeaderDied(CGroupNpc* group);
174 void squadDied(CGroupNpc* group);
176 void triggerSpecialEvent(SPIREENUMS::TSpecialSpireEvent eventId);
178 void setBuildingBotSheet(uint32 buildingAlias, NLMISC::CSheetId sheetId, bool autoSpawnDespawn, const std::string & customName);
183 /// returns the state of the spire
184 std::string getStateName() const;
186 /// return true if the spire is owned by a guild, not by a tribe
187 bool isBelongingToAGuild() const { return (_OwnerAllianceId!=InvalidAllianceId); }
189 // add a link to a squad that can be created/spawned in the spire
190 void addSquadLink( TAIAlias alias, CGroupDesc<CSpireSquadFamily> *squad )
192 _SquadLinks.insert( std::make_pair( alias, squad ) );
195 /// get a squad that can be created/spawned in the spire
196 CGroupDesc<CSpireSquadFamily> *getSquad( TAIAlias alias )
198 CSquadLinks::iterator it=_SquadLinks.find( alias );
199 if ( it != _SquadLinks.end() )
205 const CSquadLinks& squadLinks() const { return _SquadLinks; }
208 IAliasCont *getAliasCont(AITYPES::TAIType type);
209 CAliasTreeOwner *createChild(IAliasCont *cont, CAIAliasDescriptionNode *aliasTree);
211 void sendSpireMessage(std::string const& msgName, T& paramStruct);
214 /// @name AI service hierarchy
216 CAliasCont<CSpireSpawnZone> _SpawnZones;
217 CAliasCont<CSpireManager> _Managers;
220 /// Links to the squad group descs
221 CSquadLinks _SquadLinks;
223 /// The guild or the tribe that control the spire. Can be InvalidAllianceId if tribe alliance id not known yet.
224 TAllianceId _OwnerAllianceId;
226 /// The guild that wants to control the spire (if any). Can be InvalidAllianceId (peace time).
227 TAllianceId _AttackerAllianceId;
229 /// current state of the spire
230 SPIREENUMS::TSpireState _State;
232 /// The tribe that controls this spire when no guild owns it
235 /// Name of the spire
236 std::string _SpireName;
238 // Zone map to retrieve a spawn zone based on its name
239 typedef std::map<NLMISC::TStringId, NLMISC::CDbgPtr<CSpireSpawnZone> > TZoneList;
242 /// The polygon surounding the spire
244 NLMISC::CSmartPtr<CAIPlaceSpire> _Shape;
247 //////////////////////////////////////////////////////////////////////////////
248 // CSpireSquadFamily //
249 //////////////////////////////////////////////////////////////////////////////
251 class CSpireSquadFamily
252 : public NLMISC::CDbgRefCount<CSpireSquadFamily>
253 , public NLMISC::CRefCount
254 , public CAliasChild<CAIInstance>
257 CSpireSquadFamily(CAIInstance* owner, uint32 alias, std::string const& name)
258 : CAliasChild<CAIInstance>(owner, alias, name)
261 virtual ~CSpireSquadFamily()
266 CAliasCont<CGroupDesc<CSpireSquadFamily> >& groupDescs() { return _GroupDescs; }
267 CAliasCont<CGroupDesc<CSpireSquadFamily> > const& groupDescs() const { return _GroupDescs; }
269 virtual std::string getFullName() const;
270 virtual std::string getIndexString() const;
273 IAliasCont *getAliasCont(AITYPES::TAIType type);
274 CAliasTreeOwner *createChild(IAliasCont *cont, CAIAliasDescriptionNode *aliasTree);
277 /// These are only references to the group descs that are created when reading the squad template primitive
278 CAliasCont< CGroupDesc<CSpireSquadFamily> > _GroupDescs;
281 //////////////////////////////////////////////////////////////////////////////
282 // CSpireSpawnZone //
283 //////////////////////////////////////////////////////////////////////////////
285 /// Zone for spire activity
286 class CSpireSpawnZone
287 : public NLMISC::CDbgRefCount<CSpireSpawnZone>
291 CSpireSpawnZone(CSpire* owner, CAIAliasDescriptionNode* adn);
294 virtual std::string getFullName() const;
295 virtual std::string getIndexString() const;
298 //////////////////////////////////////////////////////////////////////////////
300 // This is a container of bot groups, //
301 // This is *not* a singleton manager as it is in the EGS. //
302 //////////////////////////////////////////////////////////////////////////////
308 CSpireManager(CSpire* parent, uint32 alias, std::string const& name, std::string const& filename = std::string());
309 virtual ~CSpireManager();
313 IAliasCont* getAliasCont(AITYPES::TAIType type);
314 CAliasTreeOwner* createChild(IAliasCont* cont, CAIAliasDescriptionNode* aliasTree);
315 virtual std::string getOneLineInfoString() const;
317 /// Change the list of enemies of the squad (attackers of the spire)
318 void setEnemies( TAllianceId attackerAllianceId );
320 CAIEvent EventSpirePeaceStateEnd;
321 CAIEvent EventSpirePeaceStateBegin;
322 CAIEvent EventSpireTribeOwnershipBegin;
323 CAIEvent EventSpireTribeOwnershipEnd;
324 CAIEvent EventSpireGuildOwnershipBegin;
325 CAIEvent EventSpireGuildOwnershipEnd;
326 CAIEvent EventSpireOwnerChanged;
327 CAIEvent EventSpireAttackerChanged;
328 CAIEvent EventSpireStateChanged;
330 virtual void registerEvents();
331 virtual void unregisterEvents();
333 void setAutoSpawn(bool autoSpawn) { _AutoSpawn = autoSpawn; }
334 void autoSpawnBegin();
341 //////////////////////////////////////////////////////////////////////////////
343 //////////////////////////////////////////////////////////////////////////////
344 /// This class derives from CSpireManager and adds a state machine for
346 class CSpireSquadManager
347 : public CSpireManager
350 CSpireSquadManager(CSpire* parent, uint32 alias, std::string const& name, std::string const& filename = std::string());