00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "std3d.h"
00027
00028 #include <iostream>
00029
00030 #include "3d/zone_manager.h"
00031 #include "nel/misc/path.h"
00032 #include "nel/misc/file.h"
00033
00034 using namespace std;
00035 using namespace NLMISC;
00036
00037
00038 namespace NL3D
00039 {
00040
00041
00042
00043
00044
00045
00046 CZoneManager::CZoneManager()
00047 {
00048 _Zone = NULL;
00049 _AddingZone= false;
00050 _RemovingZone= false;
00051 _WorkInProgress = false;
00052 }
00053
00054
00055 CZoneManager::~CZoneManager()
00056 {
00057 }
00058
00059
00060 void CZoneManager::checkZonesAround (uint x, uint y, uint area)
00061 {
00062 _WorkInProgress = true;
00063 if ((_AddingZone) || (_RemovingZone)) return;
00064
00065
00066 if ((x != _LastX) || (y != _LastY))
00067 getListZoneId (x, y, area, _ZoneList);
00068 _LastX = x;
00069 _LastY = y;
00070
00071
00072 uint32 i, j;
00073 for (i = 0; i < _LoadedZones.size(); ++i)
00074 {
00075
00076 bool bFound = false;
00077 uint16 nLoadedZone = _LoadedZones[i];
00078 for (j = 0; j < _ZoneList.size(); ++j)
00079 {
00080 if (_ZoneList[j] == nLoadedZone)
00081 {
00082 bFound = true;
00083 break;
00084 }
00085 }
00086
00087 if (!bFound)
00088 {
00089
00090 _IdZoneToRemove = nLoadedZone;
00091 _RemovingZone = true;
00092 return;
00093 }
00094 }
00095
00096
00097 for (i = 0; i < _ZoneList.size(); ++i)
00098 {
00099
00100 bool bFound = false;
00101 uint16 nZone = _ZoneList[i];
00102 for (j = 0; j < _LoadedZones.size(); ++j)
00103 {
00104 if (_LoadedZones[j] == nZone)
00105 {
00106 bFound = true;
00107 break;
00108 }
00109 }
00110
00111 if (!bFound)
00112 {
00113
00114 _AddingZone = true;
00115 CAsyncFileManager &rAFM = CAsyncFileManager::getInstance();
00116 _ZoneToAddName = getZoneNameFromId(nZone);
00117 _ZoneToAddId = nZone;
00118 rAFM.addTask (new CZoneLoadingTask(_ZoneToAddName, &_Zone) );
00119 return;
00120 }
00121 }
00122 _WorkInProgress = false;
00123 }
00124
00125
00126 bool CZoneManager::isWorkComplete (CZoneManager::SZoneManagerWork &rWork)
00127 {
00128
00129 if (_AddingZone)
00130 {
00131 if (_Zone != NULL)
00132 {
00133 _AddingZone= false;
00134 rWork.ZoneAdded = true;
00135 rWork.NameZoneAdded = _ZoneToAddName;
00136 rWork.ZoneRemoved = false;
00137 rWork.IdZoneToRemove = 0;
00138 rWork.NameZoneRemoved = "";
00139 rWork.Zone = const_cast<CZone*>(_Zone);
00140 _LoadedZones.push_back (_ZoneToAddId);
00141 return true;
00142 }
00143 else
00144 {
00145 return false;
00146 }
00147
00148 }
00149 if (_RemovingZone)
00150 {
00151 _RemovingZone = false;
00152 rWork.ZoneAdded = false;
00153 rWork.NameZoneAdded = "";
00154 rWork.ZoneRemoved = true;
00155 rWork.IdZoneToRemove = _IdZoneToRemove;
00156 rWork.NameZoneRemoved = getZoneNameFromId(_IdZoneToRemove);
00157 uint32 i, j;
00158 for (i = 0 ; i < _LoadedZones.size(); ++i)
00159 if (_LoadedZones[i] == _IdZoneToRemove)
00160 break;
00161 if (i < _LoadedZones.size())
00162 {
00163 for (j = i; j < _LoadedZones.size()-1; ++j)
00164 _LoadedZones[j] = _LoadedZones[j+1];
00165 _LoadedZones.resize(_LoadedZones.size()-1);
00166 }
00167 rWork.Zone = NULL;
00168 return true;
00169 }
00170 return false;
00171 }
00172
00173
00174
00175
00176
00177
00178 CZoneLoadingTask::CZoneLoadingTask(const std::string &sZoneName, TVolatileZonePtr *ppZone)
00179 {
00180 *ppZone = NULL;
00181 _Zone = ppZone;
00182 _ZoneName = sZoneName;
00183 }
00184
00185
00186 void CZoneLoadingTask::run(void)
00187 {
00188
00189 string zonePathLookup = CPath::lookup (_ZoneName, false, false, true);
00190 if (zonePathLookup == "")
00191 zonePathLookup = _ZoneName;
00192
00193 CZone *ZoneTmp = new CZone;
00194 CIFile file;
00195 if(file.open(zonePathLookup))
00196 {
00197 ZoneTmp->serial(file);
00198 file.close();
00199 *_Zone = ZoneTmp;
00200 }
00201 else
00202 {
00203 nldebug("CZoneLoadingTask::run(): File not found: %s", zonePathLookup.c_str ());
00204 delete ZoneTmp;
00205 *_Zone = (CZone*)-1;
00206 }
00207 delete this;
00208 }
00209
00210 }
00211