1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
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/>.
19 #include "nel/3d/zone_manager.h"
20 #include "nel/misc/path.h"
21 #include "nel/misc/file.h"
24 using namespace NLMISC
;
33 // ------------------------------------------------------------------------------------------------
35 // ------------------------------------------------------------------------------------------------
37 // ------------------------------------------------------------------------------------------------
38 CZoneManager::CZoneManager()
41 _ZoneTileColorMono
= true;
42 _ZoneTileColorFactor
= 1.0f
;
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
;
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
65 for (i
= 0; i
< _ZoneList
.size(); ++i
)
67 if (zoneLoaded
.find (_ZoneList
[i
]) == zoneLoaded
.end ())
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
);
85 // **** Look if we have zone loaded that is not needed anymore
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
91 uint16 nLoadedZone
= _LoadedZones
[i
];
92 for (j
= 0; j
< _ZoneList
.size(); ++j
)
94 if (_ZoneList
[j
] == nLoadedZone
)
103 // Remove the zone nLoadedZone
104 _IdZoneToRemove
= nLoadedZone
;
105 _RemovingZone
= true;
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
115 uint16 nZone
= _ZoneList
[i
];
116 for (j
= 0; j
< _LoadedZones
.size(); ++j
)
118 if (_LoadedZones
[j
] == nZone
)
125 // if the zone is not already loaded
129 std::list
<CLoadingZone
>::iterator ite
= _LoadingZones
.begin ();
130 while (ite
!= _LoadingZones
.end())
132 if (ite
->ZoneToAddId
== nZone
)
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
;
149 // We have to load this zone. add a load task
150 CAsyncFileManager
&rAFM
= CAsyncFileManager::getInstance();
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())
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
);
191 _RemovingZone
= false;
192 rWork
.ZoneAdded
= false;
193 rWork
.NameZoneAdded
.clear();
194 rWork
.ZoneRemoved
= true;
195 rWork
.IdZoneToRemove
= _IdZoneToRemove
;
196 rWork
.NameZoneRemoved
= getZoneNameFromId(_IdZoneToRemove
);
198 for (i
= 0 ; i
< _LoadedZones
.size(); ++i
)
199 if (_LoadedZones
[i
] == _IdZoneToRemove
)
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);
214 // ------------------------------------------------------------------------------------------------
215 void CZoneManager::clear()
217 nlassert(_LoadingZones
.empty());
218 _LoadedZones
.clear();
219 _RemovingZone
= false;
223 // ------------------------------------------------------------------------------------------------
225 // ------------------------------------------------------------------------------------------------
227 // ------------------------------------------------------------------------------------------------
228 CZoneLoadingTask::CZoneLoadingTask(const std::string
&sZoneName
, TVolatileZonePtr
*ppZone
, CVector
&pos
, bool monochrome
, float factor
)
232 _ZoneName
= sZoneName
;
234 _Monochrome
= monochrome
;
235 _TileColorFactor
= max(0.0f
, factor
);
238 // ------------------------------------------------------------------------------------------------
239 void CZoneLoadingTask::run(void)
242 string zonePathLookup
= CPath::lookup (_ZoneName
, false, false, true);
243 if (zonePathLookup
.empty())
244 zonePathLookup
= _ZoneName
;
246 CZone
*ZoneTmp
= new CZone
;
248 file
.setAsyncLoading(true);
249 file
.setCacheFileOnOpen(true);
250 if(file
.open(zonePathLookup
))
252 ZoneTmp
->serial(file
);
254 ZoneTmp
->setTileColor(_Monochrome
, _TileColorFactor
);
259 //nldebug("CZoneLoadingTask::run(): File not found: %s", zonePathLookup.c_str ());
261 *_Zone
= (CZone
*)-1; // Return error
268 // ***************************************************************************
269 void CZoneLoadingTask::getName (std::string
&result
) const
271 result
= "LoadZone(" + _ZoneName
+ ")";
274 // ***************************************************************************
275 CZoneLoadingTask::~CZoneLoadingTask()