Show bonus/malus timer text if available
[ryzomcore.git] / nel / src / 3d / landscapeig_manager.cpp
blob78154dedaf17834ec34750b5e89fcec0ff81dc04
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/landscapeig_manager.h"
20 #include "nel/3d/scene_user.h"
21 #include "nel/3d/instance_group_user.h"
22 #include "nel/3d/shape.h"
23 #include "nel/misc/common.h"
24 #include "nel/misc/debug.h"
25 #include "nel/misc/path.h"
26 #include "nel/misc/file.h"
27 #include "nel/misc/hierarchical_timer.h"
30 using namespace NLMISC;
31 using namespace std;
33 H_AUTO_DECL ( NL3D_Load_Zone_IG )
34 H_AUTO_DECL ( NL3D_Unload_Zone_IG )
36 #define NL3D_HAUTO_LAND_MNGR_LOAD_ZONEIG H_AUTO_USE( NL3D_Load_Zone_IG )
37 #define NL3D_HAUTO_LAND_MNGR_UNLOAD_ZONEIG H_AUTO_USE( NL3D_Unload_Zone_IG )
39 #ifdef DEBUG_NEW
40 #define new DEBUG_NEW
41 #endif
43 namespace NL3D
47 // ***************************************************************************
48 CLandscapeIGManager::CInstanceGroupElement::CInstanceGroupElement(UInstanceGroup *ig, const char *fileName)
50 Ig = ig;
51 AddedToScene = false;
52 if (fileName != NULL)
53 FileName = fileName;
56 // ***************************************************************************
57 void CLandscapeIGManager::CInstanceGroupElement::release()
59 delete Ig;
60 Ig= NULL;
64 // ***************************************************************************
65 CLandscapeIGManager::CLandscapeIGManager()
67 _Scene=NULL;
69 // ***************************************************************************
70 CLandscapeIGManager::~CLandscapeIGManager()
72 // reset should have been called.
73 if(_Scene != NULL)
74 nlwarning ("CLandscapeIGManager not reseted");
76 // ***************************************************************************
77 void CLandscapeIGManager::initIG(UScene *scene, const std::string &igDesc, UDriver *driver, uint selectedTexture,
78 NLMISC::IProgressCallback * /* callBack */)
80 nlassert(scene);
81 _Scene= scene;
83 // Load the file.
84 if(igDesc.empty())
85 return;
87 string igFile = CPath::lookup(igDesc);
89 CIFile file;
91 // Shape to add should be empty !
92 nlassert(_ShapeAdded.empty ());
94 // if loading ok.
95 //if(file.is_open())
96 if (file.open (igFile))
98 char tmpBuff[260];
99 char delimiterBox[] = "\t";
100 // While the end of the file is not reached.
101 while(!file.eof())
103 // Get a line
104 file.getline(tmpBuff, 260);
105 char *token = strtok(tmpBuff, delimiterBox);
106 // create the instance group.
107 if(token != NULL)
109 if( _ZoneInstanceGroupMap.find(token)!=_ZoneInstanceGroupMap.end() )
110 throw Exception("CLandscapeIGManager::initIG() found 2 igs with same name in %s", igFile.c_str());
111 else
113 // create the instanceGroup.
114 UInstanceGroup *ig = UInstanceGroup::createInstanceGroup(token);
115 if (ig)
117 // add it to the map.
118 string tokId= toUpperAscii(string(token));
119 _ZoneInstanceGroupMap[tokId]= CInstanceGroupElement(ig, token);
121 // Add a reference on the shapes
122 CInstanceGroup &_ig = static_cast<CInstanceGroupUser*>(ig)->getInternalIG();
123 CScene &_scene = static_cast<CSceneUser*>(scene)->getScene();
124 uint i;
125 for (i=0; i<_ig.getNumInstance(); i++)
127 // Get the instance name
128 string shapeName;
129 _ig.getShapeName(i, shapeName);
130 if (!shapeName.empty ())
132 if (toLowerAscii(CFile::getExtension(shapeName)) != "pacs_prim")
134 // Insert a new shape ?
135 if (_ShapeAdded.find(shapeName) == _ShapeAdded.end())
137 // Shape present ?
138 CShapeBank *shapeBank = _scene.getShapeBank();
139 IShape *shape = NULL;
140 if (shapeBank->getPresentState (shapeName) == CShapeBank::NotPresent)
141 shapeBank->load (shapeName);
142 if (shapeBank->getPresentState (shapeName) == CShapeBank::Present)
143 shape = shapeBank->addRef(shapeName);
145 // Shape loaded ?
146 if (shape)
148 // Insert the shape
149 CSmartPtr<IShape> *smartPtr = new CSmartPtr<IShape>;
150 *smartPtr = shape;
151 _ShapeAdded.insert (TShapeMap::value_type (shapeName, smartPtr));
153 // Flush the shape
154 IDriver *_driver = static_cast<CDriverUser*>(driver)->getDriver();
155 shape->flushTextures(*_driver, selectedTexture);
162 else
164 nlwarning ("CLandscapeIGManager::initIG() Can't load instance group '%s' in '%s'", token, igFile.c_str());
169 file.close();
171 else
173 nlwarning ("Couldn't load '%s'", igFile.c_str());
176 // ***************************************************************************
177 UInstanceGroup *CLandscapeIGManager::loadZoneIG(const std::string &name)
179 NL3D_HAUTO_LAND_MNGR_LOAD_ZONEIG
181 if(name.empty())
182 return NULL;
184 // try to find this InstanceGroup.
185 ItZoneInstanceGroupMap it;
186 it= _ZoneInstanceGroupMap.find( translateName(name) );
188 // if found.
189 if( it!= _ZoneInstanceGroupMap.end() )
191 // if not already added to the scene.
192 if( !it->second.AddedToScene )
194 // add to the scene.
195 if (it->second.Ig != NULL)
197 it->second.Ig->addToScene(*_Scene);
198 it->second.AddedToScene= true;
201 return it->second.Ig;
203 else
205 return NULL;
208 // ***************************************************************************
209 void CLandscapeIGManager::loadArrayZoneIG(const std::vector<std::string> &names, std::vector<UInstanceGroup *> *dest /*= NULL*/)
211 if (dest)
213 dest->clear();
214 dest->reserve(names.size());
216 for(uint i=0; i<names.size(); i++)
218 UInstanceGroup *ig = loadZoneIG(names[i]);
219 if (dest && ig)
221 dest->push_back(ig);
226 // ***************************************************************************
227 void CLandscapeIGManager::unloadArrayZoneIG(const std::vector<std::string> &names)
229 for(uint i=0; i<names.size(); i++)
231 unloadZoneIG(names[i]);
235 // ***************************************************************************
236 void CLandscapeIGManager::unloadZoneIG(const std::string &name)
238 NL3D_HAUTO_LAND_MNGR_UNLOAD_ZONEIG
239 if(name.empty())
240 return;
242 // try to find this InstanceGroup.
243 ItZoneInstanceGroupMap it;
244 it= _ZoneInstanceGroupMap.find( translateName(name) );
246 // if found.
247 if( it!= _ZoneInstanceGroupMap.end() )
249 // if really added to the scene.
250 if( it->second.AddedToScene )
252 // remove from the scene.
253 it->second.Ig->removeFromScene(*_Scene);
254 it->second.AddedToScene= false;
259 // ***************************************************************************
260 bool CLandscapeIGManager::isIGAddedToScene(const std::string &name) const
262 if(name.empty())
263 return false;
265 // try to find this InstanceGroup.
266 ConstItZoneInstanceGroupMap it;
267 it= _ZoneInstanceGroupMap.find( translateName(name) );
269 // if found.
270 if( it!= _ZoneInstanceGroupMap.end() )
271 return it->second.AddedToScene;
272 else
273 return false;
276 // ***************************************************************************
277 UInstanceGroup *CLandscapeIGManager::getIG(const std::string &name) const
279 if(name.empty())
280 return NULL;
282 // try to find this InstanceGroup.
283 ConstItZoneInstanceGroupMap it;
284 it= _ZoneInstanceGroupMap.find( translateName(name) );
286 // if found.
287 if( it!= _ZoneInstanceGroupMap.end() )
288 return it->second.Ig;
289 else
290 return NULL;
294 // ***************************************************************************
295 std::string CLandscapeIGManager::translateName(const std::string &name) const
297 std::string ret;
298 ret= toUpperAscii(name + ".ig");
299 return ret;
303 // ***************************************************************************
304 void CLandscapeIGManager::reset()
306 while( _ZoneInstanceGroupMap.begin() != _ZoneInstanceGroupMap.end() )
308 string name= _ZoneInstanceGroupMap.begin()->first;
309 // first remove from scene
310 unloadZoneIG( name.substr(0, name.find('.')) );
312 // then delete this entry.
313 _ZoneInstanceGroupMap.begin()->second.release();
314 _ZoneInstanceGroupMap.erase(_ZoneInstanceGroupMap.begin());
317 // For all shape reference
318 TShapeMap::iterator ite = _ShapeAdded.begin ();
319 while (ite != _ShapeAdded.end())
321 // Unreference shape
322 CScene &_scene = static_cast<CSceneUser*>(_Scene)->getScene();
323 CSmartPtr<IShape> *smartPtr = (CSmartPtr<IShape> *)(ite->second);
324 IShape *shapeToRelease = *smartPtr;
325 *smartPtr = NULL;
326 _scene.getShapeBank()->release(shapeToRelease);
327 delete smartPtr;
329 // Next
330 ite++;
332 _ShapeAdded.clear ();
334 _Scene=NULL;
338 // ***************************************************************************
339 void CLandscapeIGManager::reloadAllIgs()
341 vector<std::string> bkupIgFileNameList;
342 vector<bool> bkupIgAddedToScene;
344 // First, erase all igs.
345 while( _ZoneInstanceGroupMap.begin() != _ZoneInstanceGroupMap.end() )
347 string name= _ZoneInstanceGroupMap.begin()->first;
349 // bkup the state of this ig.
350 bkupIgFileNameList.push_back(_ZoneInstanceGroupMap.begin()->second.FileName);
351 bkupIgAddedToScene.push_back(_ZoneInstanceGroupMap.begin()->second.AddedToScene);
353 // first remove from scene
354 unloadZoneIG( name.substr(0, name.find('.')) );
356 // then delete this entry.
357 _ZoneInstanceGroupMap.begin()->second.release();
358 _ZoneInstanceGroupMap.erase(_ZoneInstanceGroupMap.begin());
361 // Then reload all Igs.
362 for(uint i=0; i<bkupIgFileNameList.size(); i++)
364 const char *token= bkupIgFileNameList[i].c_str();
365 UInstanceGroup *ig = UInstanceGroup::createInstanceGroup(token);
366 // add it to the map.
367 string tokId= toUpperAscii(token);
368 _ZoneInstanceGroupMap[tokId]= CInstanceGroupElement(ig, token);
370 // If was addedToScene before, re-add to scene now.
371 if(bkupIgAddedToScene[i])
373 loadZoneIG( tokId.substr(0, tokId.find('.')) );
379 // ***************************************************************************
380 void CLandscapeIGManager::getAllIG(std::vector<UInstanceGroup *> &dest) const
382 dest.clear();
383 dest.reserve(_ZoneInstanceGroupMap.size());
384 // add the instances
385 for(TZoneInstanceGroupMap::const_iterator it = _ZoneInstanceGroupMap.begin(); it != _ZoneInstanceGroupMap.end(); ++it)
387 dest.push_back(it->second.Ig);
391 // ***************************************************************************
392 void CLandscapeIGManager::getAllIGWithNames(std::vector<std::pair<UInstanceGroup *, std::string> > &dest) const
394 dest.clear();
395 dest.reserve(_ZoneInstanceGroupMap.size());
396 // add the instances
397 for(TZoneInstanceGroupMap::const_iterator it = _ZoneInstanceGroupMap.begin(); it != _ZoneInstanceGroupMap.end(); ++it)
399 dest.push_back(std::make_pair(it->second.Ig, it->second.FileName));
404 } // NL3D