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/>.
17 #ifndef RYAI_GRP_FAUNA_H
18 #define RYAI_GRP_FAUNA_H
20 #include "server_share/r2_variables.h"
21 #include "ai_vision.h"
23 #include "ai_place_xyr.h"
25 #include "ai_bot_fauna.h"
27 #include "state_instance.h"
29 #include "ai_generic_fight.h"
32 class CSpawnGroupFauna
;
34 #define GROUP_VISION_UPDATE_RADIUS (80)
36 //////////////////////////////////////////////////////////////////////////////
37 // CSpawnGroupFauna //
38 //////////////////////////////////////////////////////////////////////////////
40 /// This class represents a fauna group - it may be specialised at a later date
41 class CSpawnGroupFauna
43 , public CFightOrganizer
46 // public enums ----------------------------------------------------
58 CSpawnGroupFauna(CPersistent
<CSpawnGroup
>& owner
, RYAI_MAP_CRUNCH::TAStarFlag denyFlag
);
64 /// @name CFightOrganizer implementation
66 virtual void setFight(CSpawnBot
* bot
, CAIEntityPhysical
* ennemy
);
67 virtual void setHeal(CSpawnBot
* bot
, CAIEntityPhysical
* target
);
68 virtual void setNoFight(CSpawnBot
* bot
);
69 virtual void setFlee(CSpawnBot
* bot
, CAIVector
& fleeVect
);
70 virtual void setReturnAfterFight(CSpawnBot
* bot
);
73 virtual void spawnBots();
74 virtual void spawnBots(const std::string
&name
);
75 virtual void despawnBots(bool immediately
);
77 // overrides the init to avoid automatic bot spawn ..
78 void spawnBotOfGroup() { }
80 virtual void despawnGrp(); // critical code (despawn 'this' object).
82 uint32
getLastUpdate() const { return _LastUpdate
; }
84 // Update Rate feature.
85 int getUpdatePriority() const { return IsRingShard
? (_UpdatePriority
&7): _UpdatePriority
; }
86 // overloading to recalc the priority update rate.
87 void recalcUpdatePriorityDeltaAndGroupPos();
89 CAIPos
const& magnetPos() const;
90 float magnetRadiusNear() const;
91 float magnetRadiusFar() const;
93 void resetTimer() { _Timer
.set(0); }
95 CGrpFauna
& getPersistent();
97 // std::string buildDebugString(uint idx) const;
99 void setPlace(int placeIndex
);
101 void incCurrentCycle();
103 void setCurrentCycle(uint32 cycle
);
105 uint32
getCurrentCycleTime();
107 CAIPlace
* targetPlace() const { return _TargetPlace
; }
109 CPathCont
& getPathCont() { return _PathCont
; }
111 CBotFauna
* leader() const { return _Leader
; }
113 void setDespawnImmediately(bool immediately
) { _DespawnImmediately
= immediately
; }
115 bool despawnImmediately() { return _DespawnImmediately
; }
117 void setMustDespawnBots(bool toDespawnBots
= true) { _MustDespawnBots
=toDespawnBots
; }
119 bool mustDespawnBots() { return _MustDespawnBots
; }
122 uint32
getDt() const { return _DeltaTime
; }
124 CBotFauna
* findLeader();
126 // Behavior update method.
127 void generalUpdate(TState state
= StateUndefined
);
129 // Update method for different states.
130 void updateSpawning();
131 void updateActivity(AITYPES::TProfiles activity
);
133 void checkTimers(); // called at each update to see if the last timer delay has expired (if true calls updateCycles)
138 NLMISC::CDbgPtr
<CAIPlace
> _TargetPlace
; // a dynamic pointer to one of the above places
140 bool _ArrivedInZone
; // indicates that we have not reach the new zone
142 NLMISC::CDbgPtr
<CBotFauna
> _Leader
; // the individual in the group who is treated as the leader
145 uint32 _CurrentCycle
;
148 uint32 _LastUpdate
; // gamecycle at which update() last called
149 uint32 _DeltaTime
; // Dt, simply
151 bool _MustDespawnBots
; // if its a day group and its night for instance.
152 bool _DespawnImmediately
; // when MustdespawnBots, indicate if we must done it immedialtely or at best when no player can see it
155 //////////////////////////////////////////////////////////////////////////////
157 //////////////////////////////////////////////////////////////////////////////
161 , public CPersistentStateInstance
164 , public CPopulationOwner
169 INVALID_PLACE
= CFaunaGenericPlace::INVALID_PLACE
188 CSpawnGroupFauna::TState _Activity
;
198 friend class CGrpFauna
;
199 uint16
getPopIndex(int ind
)
201 return _PopList
[ind
];
203 std::vector
<uint16
> _PopList
;
207 CGrpFauna(CMgrFauna
* mgr
, CAIAliasDescriptionNode
* aliasTree
, RYAI_MAP_CRUNCH::TAStarFlag denyFlags
);
209 virtual ~CGrpFauna();
211 RYZOMID::TTypeId
getRyzomType() { return RYZOMID::creature
; }
213 CDynGrpBase
* getGrpDynBase() { return this; }
215 //////////////////////////////////////////////////////////////////////////
216 // PersistentStateInstance
218 CAliasTreeOwner
* aliasTreeOwner() { return this; }
220 CAIS::CCounter
& getSpawnCounter();
222 bool timeAllowSpawn(uint32 popVersion
= 12345) const;
224 void stateChange(CAIState
const* oldState
, CAIState
const* newState
);
226 CGroup
* getGroup() { return this; }
228 //////////////////////////////////////////////////////////////////////////
230 virtual void lastBotDespawned();
231 virtual void firstBotSpawned();
234 CDebugHistory
* getDebugHistory() { return this; }
236 CPersistentStateInstance
* getPersistentStateInstance() { return this; }
238 virtual void setEvent(uint eventId
);
239 virtual void serviceEvent (const CServiceEvent
&info
);
241 virtual std::string
getFullName() const { return CGroup::getFullName(); }
242 virtual std::string
getIndexString() const { return CGroup::getIndexString(); }
244 IAliasCont
* getAliasCont(AITYPES::TAIType type
);
245 CAliasTreeOwner
* createChild(IAliasCont
* cont
, CAIAliasDescriptionNode
* aliasTree
);
247 NLMISC::CSmartPtr
<CSpawnGroup
> createSpawnGroup();
249 CSpawnGroupFauna
* getSpawnObj() const { return NLMISC::type_cast
<CSpawnGroupFauna
*>(CGroup::getSpawnObj()); }
253 // This struct is used to manage dead creatures and their corresponding respawned creature.
255 // inheritted virtual interface ------------------------------------
256 virtual bool spawn();
257 bool spawnPop(uint popVersion
);
258 virtual void despawnGrp();
260 // Methods for setting up static data ------------------------------
261 void setType(AITYPES::TFaunaType type
);
262 void setCyles(std::string
const& cycles
);
264 // Methods for manageing population descriptions
265 void setPopulation(CPopulation
*pop
);
267 // Methods for managing places
268 void setPlace(CAIPlace
*& variable
, CAIPlace
* place
);
269 void setupWanderPlace();
271 // Miscelaneous accessors ------------------------------------------
272 CMgrFauna
& mgr() const;
274 // reference timer value
275 static uint32
refTimer(TTime time
);
276 uint32
timer(TTime time
) const { return _Timer
[time
]; }
277 void setTimer(TTime timer
, uint32 time
) { _Timer
[timer
] = time
*10; } ///< we set time in seconds.
279 AITYPES::TFaunaType
getType() const { return _Type
; }
281 void setSpawnType(AITYPES::TSpawnType sp_type
) { _SpawnType
= sp_type
; }
283 void displayPlaces(CStringWriter
& stringWriter
) const;
285 CAliasCont
<CAIPlace
>& places() { return _Places
; }
286 CAliasCont
<CPopulation
>& populations() { return _Populations
; }
288 AITYPES::CPropertySet
& faction() { return _Faction
; }
290 static const CCycleDef cycles
[];
291 static const uint32 nbCycle
;
293 void setAutoSpawn(bool autoSpawn
) { CGroup::setAutoSpawn(autoSpawn
); if (!isAutoSpawn()) _CurrentCycle
=std::numeric_limits
<uint32
>::max(); }
295 /// @name CChild implementation
297 virtual std::string
getOneLineInfoString() const;
298 virtual std::vector
<std::string
> getMultiLineInfoString() const;
301 /** Find next valid place with the wanted flags.
302 * If several places are valid, then one is picked randomly
303 * \return INVALID_PLACE if no such place exist
305 sint
getNextPlace(const CFaunaGenericPlace
*startPlace
, CAIPlaceXYRFauna::TFlag wantedFlag
) const;
307 // check that all places designated by the arcs of a given place are reachable
308 bool checkArcs(const CAIPlace
&startPlace
) const;
311 // Dynamic data modified during normal activity --------------------
312 uint32 _CurPopulation
; // which alternative bot population is active
316 // Static data initilised at init time -----------------------------
318 // basic data (name, id, pointer to manager, etc)
319 AITYPES::TFaunaType _Type
; // FaunaTypeHerbivore, FaunaTypePredator, ... etc
321 // Definition of a vector of vectors of classes for populations ----
323 CAliasCont
<CPopulation
> _Populations
;
324 CAliasCont
<CAIPlace
> _Places
;
326 AITYPES::TSpawnType _SpawnType
;
328 CAITimer _priorityRecalcTimer
; // timer used to determine time of next recalc of _updateMask
330 uint32 _Timer
[LAST_TIME
];
335 double _MotivationGroupProtection
; // A value between 0 an 1 giving the importance of the group protection.
336 double _MotivationHarvestSap
; // Motivation for the sap harvest.
338 std::vector
<Cycle
> _Cycles
; // Cycles List.
339 uint32 _CurrentCycle
; // current Cycle.
340 sint32 _CurrentCycleIndex
; // the pop index in the cycle
342 AITYPES::CPropertySet _Faction
;