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 COutpostSquadFamily
;
36 class COutpostManager
;
37 class COutpostSquadManager
;
38 template <class FamilyT
>
40 template <class FamilyT
>
43 class COutpostSpawnZone
;
45 extern NLMISC::CVariable
<bool> LogOutpostDebug
;
47 //////////////////////////////////////////////////////////////////////////////
48 // Helpers functions //
49 //////////////////////////////////////////////////////////////////////////////
51 namespace OUTPOSTHELPERS
{
53 extern uint32
getEntityOutpost(CAIEntityPhysical
const* entity
);
54 extern bool isAttackingFaction(uint32 factionIndex
, CAIEntityPhysical
const* player
);
58 #define RYAI_DEBUG_OUTPOSTS 1
60 #ifdef RYAI_DEBUG_OUTPOSTS
62 #define OUTPOST_DBG nldebug
63 #define OUTPOST_INF nlinfo
64 #define OUTPOST_WRN nlwarning
65 #define OUTPOST_ERR nlerror
69 extern NLMISC::CLog OutpostDbgLog
, OutpostInfLog
, OutpostWrnLog
, OutpostErrLog
;
70 #define OUTPOST_DBG OutpostDbgLog.setPosition( __LINE__, __FILE__, __FUNCTION__ ), OutpostDbgLog.displayNL
71 #define OUTPOST_INF OutpostInfLog.setPosition( __LINE__, __FILE__, __FUNCTION__ ), OutpostInfLog.displayNL
72 #define OUTPOST_WRN OutpostWrnLog.setPosition( __LINE__, __FILE__, __FUNCTION__ ), OutpostWrnLog.displayNL
73 #define OUTPOST_ERR OutpostErrLog.setPosition( __LINE__, __FILE__, __FUNCTION__ ), OutpostErrLog.displayNL
77 //////////////////////////////////////////////////////////////////////////////
79 //////////////////////////////////////////////////////////////////////////////
82 : public NLMISC::CDbgRefCount
<COutpost
>
83 , public CAliasChild
<CContinent
>
84 , public NLMISC::CRefCount
85 , public CAliasTreeRoot
87 , public IManagerParent
91 typedef std::map
< TAIAlias
, NLMISC::CSmartPtr
<CGroupDesc
<COutpostSquadFamily
> > > CSquadLinks
;
93 /// Return the outpost corresponding to the alias, or NULL if not found (with a nlwarning)
94 static COutpost
* getOutpostByAlias(TAIAlias outpostAlias
);
96 COutpost(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
<COutpostSpawnZone
>& spawnZones() { return _SpawnZones
; }
120 CAliasCont
<COutpostSpawnZone
> const& spawnZones() const { return _SpawnZones
; }
121 CAliasCont
<COutpostManager
>& managers() { return _Managers
; }
122 CAliasCont
<COutpostManager
> const& managers() const { return _Managers
; }
125 /// @name Outpost zone
127 NLMISC::CSmartPtr
<CAIPlaceOutpost
> const& getShape() const { return _Shape
; }
128 void setShape(NLMISC::CSmartPtr
<CAIPlaceOutpost
> 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 outpost 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 outpost, or InvalidGuildId
142 TAllianceId
getOwnerAlliance() const { return _OwnerAllianceId
; }
144 /// Assign the outpost 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 outpost
148 void setAttackerAlliance( TAllianceId attackerAllianceId
);
150 /// Set the EGS state of the outpost
151 void setState( OUTPOSTENUMS::TOutpostState state
);
153 /// Manages service event (typically service ups and downs)
154 void serviceEvent(CServiceEvent
const& info
);
156 std::string
const& getOutpostName() const { return _OutpostName
; }
158 /// Called regularly at low frequency by continent update
161 void addZone(COutpostSpawnZone
* zone
);
162 void removeZone(COutpostSpawnZone
* zone
);
163 COutpostSpawnZone
* getZone(NLMISC::TStringId zoneName
);
165 void createSquad(CGroupDesc
<COutpostSquadFamily
> const* groupDesc
, COutpostSquadManager
* manager
, std::string
const& initialStateName
, COutpostSpawnZone
const* spawnZone
, uint32 spawnOrder
, uint32 respawTimeGC
, OUTPOSTENUMS::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
, OUTPOSTENUMS::TPVPSide side
);
167 void createSquad(uint32 dynGroupAlias
, uint32 zoneAlias
, uint32 spawnOrder
, uint32 respawTimeGC
, OUTPOSTENUMS::TPVPSide side
);
168 void spawnSquad(uint32 groupId
);
169 void despawnSquad(uint32 groupId
);
170 void deleteSquad(uint32 groupId
);
171 void despawnAllSquads();
172 void sendOutpostSquadStatus(CGroupNpc
* group
);
173 void squadLeaderDied(CGroupNpc
* group
);
174 void squadDied(CGroupNpc
* group
);
176 void triggerSpecialEvent(OUTPOSTENUMS::TSpecialOutpostEvent eventId
);
178 void setBuildingBotSheet(uint32 buildingAlias
, NLMISC::CSheetId sheetId
, bool autoSpawnDespawn
, const std::string
& customName
);
183 /// returns the state of the outpost
184 std::string
getStateName() const;
186 /// return true if the outpost 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 outpost
190 void addSquadLink( TAIAlias alias
, CGroupDesc
<COutpostSquadFamily
> *squad
)
192 _SquadLinks
.insert( std::make_pair( alias
, squad
) );
195 /// get a squad that can be created/spawned in the outpost
196 CGroupDesc
<COutpostSquadFamily
> *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 sendOutpostMessage(std::string
const& msgName
, T
& paramStruct
);
214 /// @name AI service hierarchy
216 CAliasCont
<COutpostSpawnZone
> _SpawnZones
;
217 CAliasCont
<COutpostManager
> _Managers
;
220 /// Links to the squad group descs
221 CSquadLinks _SquadLinks
;
223 /// The guild or the tribe that control the outpost. Can be InvalidAllianceId if tribe alliance id not known yet.
224 TAllianceId _OwnerAllianceId
;
226 /// The guild that wants to control the outpost (if any). Can be InvalidAllianceId (peace time).
227 TAllianceId _AttackerAllianceId
;
229 /// current state of the outpost
230 OUTPOSTENUMS::TOutpostState _State
;
232 /// The tribe that controls this outpost when no guild owns it
235 /// Name of the outpost
236 std::string _OutpostName
;
238 // Zone map to retrieve a spawn zone based on its name
239 typedef std::map
<NLMISC::TStringId
, NLMISC::CDbgPtr
<COutpostSpawnZone
> > TZoneList
;
242 /// The polygon surounding the outpost
244 NLMISC::CSmartPtr
<CAIPlaceOutpost
> _Shape
;
247 //////////////////////////////////////////////////////////////////////////////
248 // COutpostSquadFamily //
249 //////////////////////////////////////////////////////////////////////////////
251 class COutpostSquadFamily
252 : public NLMISC::CDbgRefCount
<COutpostSquadFamily
>
253 , public NLMISC::CRefCount
254 , public CAliasChild
<CAIInstance
>
257 COutpostSquadFamily(CAIInstance
* owner
, uint32 alias
, std::string
const& name
)
258 : CAliasChild
<CAIInstance
>(owner
, alias
, name
)
261 virtual ~COutpostSquadFamily()
266 CAliasCont
<CGroupDesc
<COutpostSquadFamily
> >& groupDescs() { return _GroupDescs
; }
267 CAliasCont
<CGroupDesc
<COutpostSquadFamily
> > 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
<COutpostSquadFamily
> > _GroupDescs
;
281 //////////////////////////////////////////////////////////////////////////////
282 // COutpostSpawnZone //
283 //////////////////////////////////////////////////////////////////////////////
285 /// Zone for outpost activity
286 class COutpostSpawnZone
287 : public NLMISC::CDbgRefCount
<COutpostSpawnZone
>
291 COutpostSpawnZone(COutpost
* owner
, CAIAliasDescriptionNode
* adn
);
292 ~COutpostSpawnZone();
294 virtual std::string
getFullName() const;
295 virtual std::string
getIndexString() const;
298 //////////////////////////////////////////////////////////////////////////////
299 // COutpostManager //
300 // This is a container of bot groups, //
301 // This is *not* a singleton manager as it is in the EGS. //
302 //////////////////////////////////////////////////////////////////////////////
304 class COutpostManager
308 COutpostManager(COutpost
* parent
, uint32 alias
, std::string
const& name
, std::string
const& filename
= std::string());
309 virtual ~COutpostManager();
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 outpost)
318 void setEnemies( TAllianceId attackerAllianceId
);
320 CAIEvent EventOutpostPeaceStateEnd
;
321 CAIEvent EventOutpostPeaceStateBegin
;
322 CAIEvent EventOutpostTribeOwnershipBegin
;
323 CAIEvent EventOutpostTribeOwnershipEnd
;
324 CAIEvent EventOutpostGuildOwnershipBegin
;
325 CAIEvent EventOutpostGuildOwnershipEnd
;
326 CAIEvent EventOutpostOwnerChanged
;
327 CAIEvent EventOutpostAttackerChanged
;
328 CAIEvent EventOutpostStateChanged
;
330 virtual void registerEvents();
331 virtual void unregisterEvents();
333 void setAutoSpawn(bool autoSpawn
) { _AutoSpawn
= autoSpawn
; }
334 void autoSpawnBegin();
341 //////////////////////////////////////////////////////////////////////////////
342 // COutpostManager //
343 //////////////////////////////////////////////////////////////////////////////
344 /// This class derives from COutpostManager and adds a state machine for
346 class COutpostSquadManager
347 : public COutpostManager
350 COutpostSquadManager(COutpost
* parent
, uint32 alias
, std::string
const& name
, std::string
const& filename
= std::string());