Merge branch '138-toggle-free-look-with-hotkey' into main/gingo-test
[ryzomcore.git] / ryzom / client / src / npc_icon.h
blobeb0e9bf94b71ce2a25588b419097166047c6c72e
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2013 Laszlo KIS-ADAM (dfighter) <dfighter1985@gmail.com>
6 //
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #ifndef CL_NPC_ICON_H
22 #define CL_NPC_ICON_H
24 #include "nel/misc/types_nl.h"
25 #include "game_share/entity_types.h"
26 #include "game_share/msg_client_server.h"
27 #include "nel/misc/cdb.h"
28 #include "entity_cl.h"
29 #include "interface_v3/view_radar.h"
30 #include <string>
31 #include <map>
32 #include <set>
34 namespace NLMISC
36 class CBitMemStream;
39 namespace NPC_ICON
41 // This enum maps TNPCMissionGiverState and adds some new values
42 enum TNPCIconId
44 IconNone = AwaitingFirstData,
45 IconNotAMissionGiver = NotAMissionGiver,
46 IconListHasOutOfReachMissions = ListHasOutOfReachMissions,
47 IconListHasAlreadyTakenMissions = ListHasAlreadyTakenMissions,
48 IconListHasAvailableMission = ListHasAvailableMission,
49 IconAutoHasUnavailableMissions = AutoHasUnavailableMissions,
50 IconAutoHasAvailableMission = AutoHasAvailableMission,
51 IconStepMission,
53 NbNPCIcons
57 using NLMISC::ICDBNode;
59 /**
60 * Description of a mission giver NPC.
61 * Fed from the server on request by CNPCIconCache.
63 class CNPCMissionGiverDesc
65 public:
67 /// Constructor
68 CNPCMissionGiverDesc() : _MissionGiverState(NPC_ICON::AwaitingFirstData), _LastUpdateTimestamp(0), _IsDescTransient(true), _HasChanged(false)
71 // Current assignment operator: bitwise copy
73 /// Return the current state
74 NPC_ICON::TNPCMissionGiverState getState() const { return _MissionGiverState; }
76 /// Return the time of last update (only valid if getState() != NPC_ICON::AwaitingFirstData)
77 NLMISC::TGameCycle getLastUpdateTimestamp() const { return _LastUpdateTimestamp; }
79 /// Return true if we are awaiting a description update
80 bool isDescTransient() const { return _IsDescTransient; }
82 /// Set as transient when about to be updated
83 void setDescTransient() { _IsDescTransient = true; }
85 /// Return true if state changed since the previous update
86 bool hasChanged() const { return _HasChanged; }
88 /// Set if state changed or not
89 void setChanged(bool changed) { _HasChanged = changed; }
91 /// Called on receival of update for this NPC. Return true if something has changed.
92 bool updateMissionAvailabilityForThisChar(NPC_ICON::TNPCMissionGiverState state);
94 /// Get debug dump
95 std::string getDump() const;
97 private:
98 NPC_ICON::TNPCMissionGiverState _MissionGiverState;
99 NLMISC::TGameCycle _LastUpdateTimestamp;
100 bool _IsDescTransient;
101 bool _HasChanged;
105 typedef uint32 TNPCIconCacheKey;
109 * NPC icon management system.
110 * Tells which icons to display above NPCs, using data from either the server database
111 * (for mission steps) or requesting and caching data from the server (for mission givers).
113 class CNPCIconCache
115 public:
118 * Description of an icon (main texture + optional overlay).
119 * The spotId tells which small icon, and in which render layer, to display in the radar.
121 class CNPCIconDesc
123 public:
124 void init(const std::string& main, const std::string& over, CViewRadar::TRadarSpotId spotId=CViewRadar::Std) { _TextureMain=main; _TextureOver=over; _SpotId=spotId; }
125 const std::string& getTextureMain() const { return _TextureMain; }
126 const std::string& getTextureOver() const { return _TextureOver; }
127 CViewRadar::TRadarSpotId getSpotId() const { return _SpotId; }
129 private:
130 std::string _TextureMain;
131 std::string _TextureOver;
132 CViewRadar::TRadarSpotId _SpotId;
135 /// Accessor for singleton
136 static CNPCIconCache& getInstance()
138 if (!_Instance)
139 _Instance = new CNPCIconCache;
140 return *_Instance;
143 /// Release singleton
144 static void release();
146 /// Return key by entity (or 0 if entity if icon cache is N/A for this entity)
147 inline static TNPCIconCacheKey entityToKey(const CEntityCL *entity)
149 return entity->npcAlias();
153 * Return the filename of the icon to display for the specified NPC, using the following precedence:
154 * 1. Icon for the case when the character has a mission step to do with the NPC (Step icon)
155 * 2. Icon for the case when the character may or may not take a mission with the NPC (Auto or List icon)
156 * 3. No mission related with this NPC (empty strings returned)
158 const CNPCIconCache::CNPCIconDesc& getNPCIcon(const CEntityCL *entity, bool bypassEnabled = false);
160 /// Init the system in either enabled or disabled state
161 void init(bool enabled) { _Enabled = enabled; }
163 /// Add DB observers
164 void addObservers();
166 /// Remove DB observers
167 void removeObservers();
169 /// Request an update about proposed missions of all qualifying npcs in vision, from the server
170 void onEventForMissionAvailabilityForThisChar();
172 /// Update routine, to be called from main loop
173 void update();
175 /// Enable or disable the system
176 void setEnabled(bool b);
178 /// Return the main enabled state
179 bool enabled() const { return _Enabled; }
181 /// Get debug dump
182 std::string getDump() const;
184 protected:
186 class CMissionStartStopObserver : public ICDBNode::IPropertyObserver
188 public:
189 virtual void update(ICDBNode* node);
191 MissionStartStopObserver;
193 class CMissionNpcAliasObserver : public ICDBNode::IPropertyObserver
195 public:
196 virtual void update(ICDBNode* node);
198 MissionNpcAliasObserver;
200 class CMissionPrerequisitEventObserver : public ICDBNode::IPropertyObserver
202 public:
203 virtual void update(ICDBNode* node);
205 MissionPrerequisitEventObserver;
207 /// Request an update about proposed missions of the specified npc, from the server.
208 void queryMissionGiverData(TNPCIconCacheKey npcIconCacheKey);
209 friend struct commands_queryMissionGiverDataClass;
211 /** Request an update about proposed missions on all qualifying visible NPCs, from the server.
212 * If olderThan==0, all qualifying (i.e. that have missions) NPCs are included (and a request
213 * is sent even for transient descriptions), whereas with a positive delay, only NPCs with
214 * information old than Now-olderThan are included and transient descriptions are ignored.
216 void queryAllVisibleMissionGiverData(NLMISC::TGameCycle olderThan);
218 /** Update visible NPCs related to missions in progress, regarding
219 * both missions actors (Step icon) and mission givers' proposed missions (Auto/List icon):
220 * - when a mission is taken (started, new TITLE in database)
221 * - when a mission is completed or failed (FINISHED changed in database)
223 void onEventForMissionInProgress();
224 friend class CMissionStartStopObserver;
226 /// Update visible NPCs related to missions in progress, regarding missions actors (Step icon)
227 void onNpcAliasChangedInMissionGoals();
228 friend class CMissionNpcAliasObserver;
230 /// Called on receival of update for this NPC. Return true if something has changed.
231 bool onReceiveMissionAvailabilityForThisChar(TNPCIconCacheKey npcIconCacheKey, NPC_ICON::TNPCMissionGiverState state);
232 friend void impulseSetNpcIconDesc(NLMISC::CBitMemStream &impulse);
234 /// Called on receival of new timer delay
235 void setMissionGiverTimer(NLMISC::TGameCycle delay);
236 friend void impulseSetNpcIconTimer(NLMISC::CBitMemStream &impulse);
238 /// Return true if the specified NPC is involved in current mission goals
239 bool isNPCaCurrentGoal(TNPCIconCacheKey npcIconCacheKey) const;
241 /// Store keys of current goals when current goals are changed
242 void storeKeysOfCurrentGoals();
244 /// Called when data have been modified
245 void refreshIconsOfScene(bool force = false);
248 private:
250 /// Private constructor (singleton)
251 CNPCIconCache();
253 typedef std::map<TNPCIconCacheKey, CNPCMissionGiverDesc> CMissionGiverMap;
255 /// Map of mission giver descriptions
256 CMissionGiverMap _MissionGivers;
258 typedef std::vector<TNPCIconCacheKey> CSmallKeyList;
260 /// List of NPCs involved in current goals (typically, contains 0 to 3 elements)
261 CSmallKeyList _KeysOfCurrentGoals;
263 /// List of NPCs for which the description is to be requested at the current update
264 CSmallKeyList _DescriptionsToRequest;
266 /// Used by update() to do tick-update tasks
267 NLMISC::TGameCycle _LastRequestTimestamp;
269 /// Used by update() to do low frequency tasks
270 NLMISC::TGameCycle _LastTimerUpdateTimestamp;
272 /// Descriptions of icons for each case
273 CNPCIconDesc _Icons[NPC_ICON::NbNPCIcons];
275 /// Main switch to disable the system
276 bool _Enabled;
278 // See in .cpp
279 static NLMISC::TGameCycle _CacheRefreshTimerDelay;
280 static NLMISC::TGameCycle _CatchallTimerPeriod;
282 /// Instance of singleton
283 static CNPCIconCache *_Instance;
286 #endif // CL_NPC_ICON_H