Change Encyclo button name and macros icon
[ryzomcore.git] / nel / src / 3d / zone_manager.cpp
blob1ba32806d9c0c3d00af2824e3fc4ffd72dab0403
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
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/>.
17 #include "std3d.h"
19 #include "nel/3d/zone_manager.h"
20 #include "nel/misc/path.h"
21 #include "nel/misc/file.h"
23 using namespace std;
24 using namespace NLMISC;
26 #ifdef DEBUG_NEW
27 #define new DEBUG_NEW
28 #endif
30 namespace NL3D
33 // ------------------------------------------------------------------------------------------------
34 // CZoneManager
35 // ------------------------------------------------------------------------------------------------
37 // ------------------------------------------------------------------------------------------------
38 CZoneManager::CZoneManager()
40 _RemovingZone= false;
41 _ZoneTileColorMono = true;
42 _ZoneTileColorFactor = 1.0f;
43 _LastArea= 0;
44 _LastX= _LastY= std::numeric_limits<uint32>::max();
47 // ------------------------------------------------------------------------------------------------
48 CZoneManager::~CZoneManager()
52 // ------------------------------------------------------------------------------------------------
53 uint CZoneManager::getNumZoneLeftToLoad ()
55 // Make a set of the loaded zone
56 set<uint16> zoneLoaded;
57 uint32 i;
58 for (i = 0; i < _LoadedZones.size(); ++i)
60 zoneLoaded.insert (_LoadedZones[i]);
63 // Check for each zone in the list if they are loaded or not
64 uint zoneCount = 0;
65 for (i = 0; i < _ZoneList.size(); ++i)
67 if (zoneLoaded.find (_ZoneList[i]) == zoneLoaded.end ())
68 zoneCount++;
70 return zoneCount;
73 // ------------------------------------------------------------------------------------------------
74 void CZoneManager::checkZonesAround (uint x, uint y, uint area, const std::vector<uint16> *validZoneIds)
76 if (_RemovingZone) return;
78 // Obtain the new set of zones around
79 if ( (x != _LastX) || (y != _LastY) || (area != _LastArea) )
80 getListZoneId (x, y, area, _ZoneList, validZoneIds);
81 _LastX = x;
82 _LastY = y;
83 _LastArea = area;
85 // **** Look if we have zone loaded that is not needed anymore
86 uint32 i, j;
87 for (i = 0; i < _LoadedZones.size(); ++i)
89 // If the loadedzone i do not appear in the zone list so we have to remove it
90 bool bFound = false;
91 uint16 nLoadedZone = _LoadedZones[i];
92 for (j = 0; j < _ZoneList.size(); ++j)
94 if (_ZoneList[j] == nLoadedZone)
96 bFound = true;
97 break;
101 if (!bFound)
103 // Remove the zone nLoadedZone
104 _IdZoneToRemove = nLoadedZone;
105 _RemovingZone = true;
106 return;
110 // **** Look if we have zone not already loaded
111 for (i = 0; i < _ZoneList.size(); ++i)
113 // If the zone requested appear in the zone loaded, we don't have to load it
114 bool bFound = false;
115 uint16 nZone = _ZoneList[i];
116 for (j = 0; j < _LoadedZones.size(); ++j)
118 if (_LoadedZones[j] == nZone)
120 bFound = true;
121 break;
125 // if the zone is not already loaded
126 if (!bFound)
128 // Already loading ?
129 std::list<CLoadingZone>::iterator ite = _LoadingZones.begin ();
130 while (ite != _LoadingZones.end())
132 if (ite->ZoneToAddId == nZone)
133 break;
135 // Next loading zone
136 ite++;
139 // Not loading ?
140 if (ite == _LoadingZones.end())
142 // Add a new zone to load
143 _LoadingZones.push_back(CLoadingZone ());
144 CLoadingZone &newZone = _LoadingZones.back();
145 newZone.ZoneToAddName = getZoneNameFromId(nZone);
146 newZone.ZoneToAddId = nZone;
147 newZone.Zone = NULL;
149 // We have to load this zone. add a load task
150 CAsyncFileManager &rAFM = CAsyncFileManager::getInstance();
152 // Make a position
153 uint x, y;
154 getZonePos (newZone.ZoneToAddId, x, y);
155 CVector v = CVector ((float)x, -(float)y, 0);
156 rAFM.addTask (new CZoneLoadingTask(newZone.ZoneToAddName, &newZone.Zone, v, _ZoneTileColorMono, _ZoneTileColorFactor));
162 // ------------------------------------------------------------------------------------------------
163 bool CZoneManager::isWorkComplete (CZoneManager::SZoneManagerWork &rWork)
165 // Check if there is someting to add
166 std::list<CLoadingZone>::iterator ite = _LoadingZones.begin ();
167 while (ite != _LoadingZones.end())
169 // Loaded ?
170 if (ite->Zone)
172 rWork.ZoneAdded = true;
173 rWork.NameZoneAdded = ite->ZoneToAddName;
174 rWork.ZoneRemoved = false;
175 rWork.IdZoneToRemove = 0;
176 rWork.NameZoneRemoved.clear();
177 rWork.Zone = const_cast<CZone*>(ite->Zone);
178 _LoadedZones.push_back (ite->ZoneToAddId);
180 // Remove from loading zone
181 _LoadingZones.erase(ite);
182 return true;
185 // Next zone
186 ite++;
189 if (_RemovingZone)
191 _RemovingZone = false;
192 rWork.ZoneAdded = false;
193 rWork.NameZoneAdded.clear();
194 rWork.ZoneRemoved = true;
195 rWork.IdZoneToRemove = _IdZoneToRemove;
196 rWork.NameZoneRemoved = getZoneNameFromId(_IdZoneToRemove);
197 uint32 i, j;
198 for (i = 0 ; i < _LoadedZones.size(); ++i)
199 if (_LoadedZones[i] == _IdZoneToRemove)
200 break;
201 if (i < _LoadedZones.size())
203 for (j = i; j < _LoadedZones.size()-1; ++j)
204 _LoadedZones[j] = _LoadedZones[j+1];
205 _LoadedZones.resize(_LoadedZones.size()-1);
207 rWork.Zone = NULL;
208 return true;
211 return false;
214 // ------------------------------------------------------------------------------------------------
215 void CZoneManager::clear()
217 nlassert(_LoadingZones.empty());
218 _LoadedZones.clear();
219 _RemovingZone = false;
223 // ------------------------------------------------------------------------------------------------
224 // CZoneLoadingTask
225 // ------------------------------------------------------------------------------------------------
227 // ------------------------------------------------------------------------------------------------
228 CZoneLoadingTask::CZoneLoadingTask(const std::string &sZoneName, TVolatileZonePtr *ppZone, CVector &pos, bool monochrome, float factor)
230 *ppZone = NULL;
231 _Zone = ppZone;
232 _ZoneName = sZoneName;
233 Position = pos;
234 _Monochrome = monochrome;
235 _TileColorFactor = max(0.0f, factor);
238 // ------------------------------------------------------------------------------------------------
239 void CZoneLoadingTask::run(void)
241 // Lookup the zone
242 string zonePathLookup = CPath::lookup (_ZoneName, false, false, true);
243 if (zonePathLookup.empty())
244 zonePathLookup = _ZoneName;
246 CZone *ZoneTmp = new CZone;
247 CIFile file;
248 file.setAsyncLoading(true);
249 file.setCacheFileOnOpen(true);
250 if(file.open(zonePathLookup))
252 ZoneTmp->serial(file);
253 file.close();
254 ZoneTmp->setTileColor(_Monochrome, _TileColorFactor);
255 *_Zone = ZoneTmp;
257 else
259 //nldebug("CZoneLoadingTask::run(): File not found: %s", zonePathLookup.c_str ());
260 delete ZoneTmp;
261 *_Zone = (CZone*)-1; // Return error
264 delete this;
268 // ***************************************************************************
269 void CZoneLoadingTask::getName (std::string &result) const
271 result = "LoadZone(" + _ZoneName + ")";
274 // ***************************************************************************
275 CZoneLoadingTask::~CZoneLoadingTask()
280 } // NL3D