Change Encyclo button name and macros icon
[ryzomcore.git] / nel / src / 3d / landscape_user.cpp
blob76d73583fa77dd39fe68ac0c32b94f23573dbaa9
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/landscape_user.h"
20 #include "nel/misc/file.h"
21 #include "nel/misc/path.h"
22 #include "nel/misc/hierarchical_timer.h"
23 #include "nel/misc/progress_callback.h"
25 using namespace NLMISC;
27 #ifdef DEBUG_NEW
28 #define new DEBUG_NEW
29 #endif
31 namespace NL3D
34 H_AUTO_DECL( NL3D_UI_Landscape )
35 H_AUTO_DECL( NL3D_Render_Landscape_updateLightingAll )
36 H_AUTO_DECL( NL3D_Load_Landscape )
38 #define NL3D_HAUTO_UI_LANDSCAPE H_AUTO_USE( NL3D_UI_Landscape )
39 #define NL3D_HAUTO_LANDSCAPE_UPDATE_LIGHTING_ALL H_AUTO_USE( NL3D_Render_Landscape_updateLightingAll )
40 #define NL3D_HAUTO_LOAD_LANDSCAPE H_AUTO_USE( NL3D_Load_Landscape )
42 // ***************************************************************************
43 CLandscapeUser::~CLandscapeUser()
46 // must ensure all loading is ended
47 removeAllZones();
49 // then delete
50 _Scene->deleteModel(_Landscape);
51 _Landscape= NULL;
55 // ****************************************************************************
56 void CLandscapeUser::setZonePath(const std::string &zonePath)
58 NL3D_HAUTO_UI_LANDSCAPE;
59 _ZoneManager.setZonePath(zonePath);
62 // ****************************************************************************
63 void CLandscapeUser::invalidateAllTiles()
65 _Landscape->Landscape.invalidateAllTiles();
68 // ****************************************************************************
69 void CLandscapeUser::loadBankFiles(const std::string &tileBankFile, const std::string &farBankFile)
71 NL3D_HAUTO_LOAD_LANDSCAPE;
73 // Release all OLD tiles
74 _Landscape->Landscape.releaseAllTiles();
76 // Clear the bank
77 _Landscape->Landscape.TileBank.clear ();
79 // First, load the banks.
80 //=======================
81 CIFile bankFile(CPath::lookup(tileBankFile));
82 _Landscape->Landscape.TileBank.serial(bankFile);
83 // All textures path are relative!
84 _Landscape->Landscape.TileBank.makeAllPathRelative();
85 // Use DDS!!!
86 _Landscape->Landscape.TileBank.makeAllExtensionDDS();
87 // No absolute path
88 _Landscape->Landscape.TileBank.setAbsPath ("");
90 CIFile farbankFile(CPath::lookup(farBankFile));
91 _Landscape->Landscape.TileFarBank.serial(farbankFile);
92 bankFile.close();
93 farbankFile.close();
96 // ****************************************************************************
98 void CLandscapeUser::flushTiles (NLMISC::IProgressCallback &progress)
100 // After loading the TileBank, and before initTileBanks(), must load the vegetables descritpor
101 _Landscape->Landscape.TileBank.loadTileVegetableDescs();
103 // init the TileBanks descriptors
104 if ( ! _Landscape->Landscape.initTileBanks() )
106 nlwarning( "You need to recompute bank.farbank for the far textures" );
109 // Count tiles
110 uint tileCount = 0;
111 sint ts;
112 for (ts=0; ts<_Landscape->Landscape.TileBank.getTileSetCount (); ts++)
114 CTileSet *tileSet=_Landscape->Landscape.TileBank.getTileSet (ts);
115 tileCount += tileSet->getNumTile128();
116 tileCount += tileSet->getNumTile256();
117 tileCount += CTileSet::count;
120 // Second, temporary, flushTiles.
121 //===============================
122 uint tile = 0;
123 for (ts=0; ts<_Landscape->Landscape.TileBank.getTileSetCount (); ts++)
125 CTileSet *tileSet=_Landscape->Landscape.TileBank.getTileSet (ts);
126 sint tl;
127 for (tl=0; tl<tileSet->getNumTile128(); tl++)
129 // Progress bar
130 progress.progress ((float)tile/(float)tileCount);
131 tile++;
133 _Landscape->Landscape.flushTiles (_Scene->getDriver(), (uint16)tileSet->getTile128(tl), 1);
135 for (tl=0; tl<tileSet->getNumTile256(); tl++)
137 // Progress bar
138 progress.progress ((float)tile/(float)tileCount);
139 tile++;
141 _Landscape->Landscape.flushTiles (_Scene->getDriver(), (uint16)tileSet->getTile256(tl), 1);
143 for (tl=0; tl<CTileSet::count; tl++)
145 // Progress bar
146 progress.progress ((float)tile/(float)tileCount);
147 tile++;
149 _Landscape->Landscape.flushTiles (_Scene->getDriver(), (uint16)tileSet->getTransition(tl)->getTile (), 1);
154 // ****************************************************************************
156 void CLandscapeUser::loadAllZonesAround(const CVector &pos, float radius, std::vector<std::string> &zonesAdded)
158 NL3D_HAUTO_LOAD_LANDSCAPE;
160 zonesAdded.clear();
162 _ZoneManager.checkZonesAround ((uint)pos.x, (uint)(-pos.y), (uint)radius);
163 while (_ZoneManager.isLoading())
165 CZoneManager::SZoneManagerWork Work;
166 if (_ZoneManager.isWorkComplete(Work))
168 nlassert(!Work.ZoneRemoved);
169 if (Work.ZoneAdded)
171 if (Work.Zone == (CZone*)-1)
173 nlwarning ("Can't load zone %s", Work.NameZoneAdded.c_str ());
175 else
177 _Landscape->Landscape.addZone (*Work.Zone);
179 delete Work.Zone;
180 std::string zoneadd = Work.NameZoneAdded;
181 zoneadd = zoneadd.substr(0, zoneadd.find('.'));
182 zonesAdded.push_back(zoneadd);
186 else
188 nlSleep (1);
190 _ZoneManager.checkZonesAround ((uint)pos.x, (uint)(-pos.y), (uint)radius);
192 // Yoyo: must check the binds of the zones.
193 _Landscape->Landscape.checkBinds();
196 // ****************************************************************************
197 void CLandscapeUser::refreshAllZonesAround(const CVector &pos, float radius, std::vector<std::string> &zonesAdded, std::vector<std::string> &zonesRemoved,
198 NLMISC::IProgressCallback &progress, const std::vector<uint16> *validZoneIds)
200 NL3D_HAUTO_LOAD_LANDSCAPE;
202 zonesAdded.clear();
203 zonesRemoved.clear();
204 std::string za, zr;
206 _ZoneManager.checkZonesAround ((uint)pos.x, (uint)(-pos.y), (uint)radius, validZoneIds);
207 refreshZonesAround (pos, radius, za, zr);
209 // Zone to load
210 uint zoneToLoad = _ZoneManager.getNumZoneLeftToLoad ();
211 while (_ZoneManager.isLoading() || _ZoneManager.isRemoving())
213 // Progress bar
214 if (zoneToLoad != 0)
215 progress.progress ((float)(zoneToLoad-_ZoneManager.getNumZoneLeftToLoad ())/(float)zoneToLoad);
217 refreshZonesAround (pos, radius, za, zr);
219 // some zone added or removed??
220 if(!za.empty())
221 zonesAdded.push_back(za);
222 if(!zr.empty())
223 zonesRemoved.push_back(zr);
225 _ZoneManager.checkZonesAround ((uint)pos.x, (uint)(-pos.y), (uint)radius);
227 if (_ZoneManager.isLoading())
228 nlSleep (0);
232 // ***************************************************************************
233 void CLandscapeUser::getAllZoneLoaded(std::vector<std::string> &zoneLoaded) const
235 // Build the list of zoneId.
236 std::vector<uint16> zoneIds;
237 _Landscape->Landscape.getZoneList(zoneIds);
239 // transcript
240 zoneLoaded.clear();
241 zoneLoaded.resize(zoneIds.size());
242 for(uint i=0;i<zoneLoaded.size();i++)
244 CLandscape::buildZoneName(zoneIds[i], zoneLoaded[i]);
248 // ****************************************************************************
249 void CLandscapeUser::loadAllZonesAround(const CVector &pos, float radius)
251 std::vector<std::string> dummy;
252 loadAllZonesAround(pos, radius, dummy);
256 // ****************************************************************************
257 void CLandscapeUser::refreshZonesAround(const CVector &pos, float radius)
259 NL3D_HAUTO_LOAD_LANDSCAPE;
261 std::string dummy1, dummy2;
262 refreshZonesAround(pos, radius, dummy1, dummy2);
264 // ****************************************************************************
265 void CLandscapeUser::refreshZonesAround(const CVector &pos, float radius, std::string &zoneAdded, std::string &zoneRemoved, const std::vector<uint16> *validZoneIds)
267 NL3D_HAUTO_LOAD_LANDSCAPE;
269 zoneRemoved.clear();
270 zoneAdded.clear();
271 CZoneManager::SZoneManagerWork Work;
272 // Check if new zone must be added to landscape
273 if (_ZoneManager.isWorkComplete(Work))
275 if (Work.ZoneAdded)
277 if (Work.Zone == (CZone*)-1)
279 //nlwarning ("Can't load zone %s", Work.NameZoneAdded.c_str ());
281 else
283 _Landscape->Landscape.addZone(*Work.Zone);
285 // Yoyo: must check the binds of the new inserted zone.
288 _Landscape->Landscape.checkBinds(Work.Zone->getZoneId());
290 catch (const EBadBind &e)
292 nlwarning ("Bind error : %s", e.what());
293 nlstopex(("Zone Data Bind Error. Please send a report. You may continue but it should crash!"));
296 delete Work.Zone;
297 zoneAdded = Work.NameZoneAdded;
298 zoneAdded = zoneAdded.substr(0, zoneAdded.find('.'));
302 // Check if a zone must be removed from landscape
303 if (Work.ZoneRemoved)
305 _Landscape->Landscape.removeZone (Work.IdZoneToRemove);
306 zoneRemoved = Work.NameZoneRemoved;
307 zoneRemoved = zoneRemoved.substr(0, zoneRemoved.find('.'));
311 _ZoneManager.checkZonesAround((uint)pos.x, (uint)(-pos.y), (uint)radius, validZoneIds);
314 // ****************************************************************************
315 void CLandscapeUser::removeAllZones()
317 NL3D_HAUTO_LOAD_LANDSCAPE;
319 // Ensure Async Loading is ended
320 CZoneManager::SZoneManagerWork Work;
321 while (_ZoneManager.isLoading())
323 if (_ZoneManager.isWorkComplete(Work))
325 // Zone to add?
326 if (Work.ZoneAdded)
328 if (Work.Zone == (CZone*)-1)
330 nlwarning ("Can't load zone %s", Work.NameZoneAdded.c_str ());
332 else
334 // just discard it! cause will be all cleared below
335 delete Work.Zone;
339 // Zone to remove?
340 if (Work.ZoneRemoved)
342 _Landscape->Landscape.removeZone (Work.IdZoneToRemove);
345 else
347 nlSleep(1);
351 // Then do a full clear
352 _Landscape->Landscape.clear();
353 _ZoneManager.clear();
356 // ****************************************************************************
357 void CLandscapeUser::setupStaticLight (const CRGBA &diffuse, const CRGBA &ambiant, float multiply)
359 NL3D_HAUTO_UI_LANDSCAPE;
360 _Landscape->Landscape.setupStaticLight(diffuse, ambiant, multiply);
365 // ****************************************************************************
366 void CLandscapeUser::setThreshold (float thre)
368 NL3D_HAUTO_UI_LANDSCAPE;
369 _Landscape->Landscape.setThreshold(thre);
371 // ****************************************************************************
372 float CLandscapeUser::getThreshold () const
374 NL3D_HAUTO_UI_LANDSCAPE;
375 return _Landscape->Landscape.getThreshold();
377 // ****************************************************************************
378 void CLandscapeUser::setTileNear (float tileNear)
380 NL3D_HAUTO_UI_LANDSCAPE;
381 _Landscape->Landscape.setTileNear(tileNear);
383 // ****************************************************************************
384 float CLandscapeUser::getTileNear () const
386 NL3D_HAUTO_UI_LANDSCAPE;
387 return _Landscape->Landscape.getTileNear();
389 // ****************************************************************************
390 void CLandscapeUser::setTileMaxSubdivision (uint tileDiv)
392 NL3D_HAUTO_UI_LANDSCAPE;
393 _Landscape->Landscape.setTileMaxSubdivision(tileDiv);
395 // ****************************************************************************
396 uint CLandscapeUser::getTileMaxSubdivision ()
398 NL3D_HAUTO_UI_LANDSCAPE;
399 return _Landscape->Landscape.getTileMaxSubdivision();
403 // ****************************************************************************
404 std::string CLandscapeUser::getZoneName(const CVector &pos)
406 NL3D_HAUTO_UI_LANDSCAPE;
407 return _ZoneManager.getZoneName((uint)pos.x, (uint)(-pos.y), 0, 0).first;
411 // ****************************************************************************
412 CVector CLandscapeUser::getHeightFieldDeltaZ(float x, float y) const
414 NL3D_HAUTO_UI_LANDSCAPE;
415 return _Landscape->Landscape.getHeightFieldDeltaZ(x,y);
418 // ****************************************************************************
419 void CLandscapeUser::setHeightField(const CHeightMap &hf)
421 NL3D_HAUTO_UI_LANDSCAPE;
422 _Landscape->Landscape.setHeightField(hf);
426 // ****************************************************************************
427 void CLandscapeUser::enableVegetable(bool enable)
429 NL3D_HAUTO_UI_LANDSCAPE;
430 _Landscape->Landscape.enableVegetable(enable);
433 // ****************************************************************************
434 void CLandscapeUser::loadVegetableTexture(const std::string &textureFileName)
436 NL3D_HAUTO_LOAD_LANDSCAPE;
437 _Landscape->Landscape.loadVegetableTexture(textureFileName);
440 // ****************************************************************************
441 void CLandscapeUser::setupVegetableLighting(const CRGBA &ambient, const CRGBA &diffuse, const CVector &directionalLight)
443 NL3D_HAUTO_UI_LANDSCAPE;
444 _Landscape->Landscape.setupVegetableLighting(ambient, diffuse, directionalLight);
447 // ****************************************************************************
448 void CLandscapeUser::setVegetableWind(const CVector &windDir, float windFreq, float windPower, float windBendMin)
450 NL3D_HAUTO_UI_LANDSCAPE;
451 _Landscape->Landscape.setVegetableWind(windDir, windFreq, windPower, windBendMin);
454 // ****************************************************************************
455 void CLandscapeUser::setVegetableUpdateLightingFrequency(float freq)
457 NL3D_HAUTO_UI_LANDSCAPE;
458 _Landscape->Landscape.setVegetableUpdateLightingFrequency(freq);
462 // ****************************************************************************
463 void CLandscapeUser::setUpdateLightingFrequency(float freq)
465 NL3D_HAUTO_UI_LANDSCAPE;
466 _Landscape->Landscape.setUpdateLightingFrequency(freq);
470 // ****************************************************************************
471 void CLandscapeUser::enableAdditive (bool enable)
473 NL3D_HAUTO_UI_LANDSCAPE;
474 _Landscape->enableAdditive(enable);
476 // ****************************************************************************
477 bool CLandscapeUser::isAdditiveEnabled () const
479 NL3D_HAUTO_UI_LANDSCAPE;
480 return _Landscape->isAdditive ();
483 // ****************************************************************************
484 void CLandscapeUser::setPointLightDiffuseMaterial(CRGBA diffuse)
486 NL3D_HAUTO_UI_LANDSCAPE;
487 _Landscape->Landscape.setPointLightDiffuseMaterial(diffuse);
489 // ****************************************************************************
490 CRGBA CLandscapeUser::getPointLightDiffuseMaterial () const
492 NL3D_HAUTO_UI_LANDSCAPE;
493 return _Landscape->Landscape.getPointLightDiffuseMaterial();
496 // ****************************************************************************
497 void CLandscapeUser::setDLMGlobalVegetableColor(CRGBA gvc)
499 NL3D_HAUTO_UI_LANDSCAPE;
500 _Landscape->Landscape.setDLMGlobalVegetableColor(gvc);
502 // ****************************************************************************
503 CRGBA CLandscapeUser::getDLMGlobalVegetableColor() const
505 NL3D_HAUTO_UI_LANDSCAPE;
506 return _Landscape->Landscape.getDLMGlobalVegetableColor();
508 // ****************************************************************************
509 void CLandscapeUser::updateLightingAll()
511 NL3D_HAUTO_LANDSCAPE_UPDATE_LIGHTING_ALL;
512 _Landscape->Landscape.updateLightingAll();
514 // ****************************************************************************
515 void CLandscapeUser::postfixTileFilename (const char *postfix)
517 NL3D_HAUTO_LANDSCAPE_UPDATE_LIGHTING_ALL;
518 _Landscape->Landscape.TileBank.postfixTileFilename (postfix);
520 // ****************************************************************************
521 void CLandscapeUser::postfixTileVegetableDesc (const char *postfix)
523 NL3D_HAUTO_LANDSCAPE_UPDATE_LIGHTING_ALL;
524 _Landscape->Landscape.TileBank.postfixTileVegetableDesc (postfix);
527 // ***************************************************************************
528 void CLandscapeUser::enableReceiveShadowMap(bool state)
530 _Landscape->enableReceiveShadowMap(state);
533 // ***************************************************************************
534 bool CLandscapeUser::canReceiveShadowMap() const
536 return _Landscape->canReceiveShadowMap();
539 // ***************************************************************************
540 void CLandscapeUser::setRefineCenterAuto(bool mode)
542 _Landscape->setRefineCenterAuto(mode);
545 // ***************************************************************************
546 void CLandscapeUser::setRefineCenterUser(const CVector &refineCenter)
548 _Landscape->setRefineCenterUser(refineCenter);
551 // ***************************************************************************
552 bool CLandscapeUser::getRefineCenterAuto() const
554 return _Landscape->getRefineCenterAuto();
557 // ***************************************************************************
558 const CVector &CLandscapeUser::getRefineCenterUser() const
560 return _Landscape->getRefineCenterUser();
564 // ***************************************************************************
565 void CLandscapeUser::addTileCallback(ULandscapeTileCallback *cb)
567 NL3D_HAUTO_UI_LANDSCAPE;
568 _Landscape->Landscape.addTileCallback(cb);
571 // ***************************************************************************
572 void CLandscapeUser::removeTileCallback(ULandscapeTileCallback *cb)
574 NL3D_HAUTO_UI_LANDSCAPE;
575 _Landscape->Landscape.removeTileCallback(cb);
578 // ***************************************************************************
579 bool CLandscapeUser::isTileCallback(ULandscapeTileCallback *cb)
581 NL3D_HAUTO_UI_LANDSCAPE;
582 return _Landscape->Landscape.isTileCallback(cb);
585 // ***************************************************************************
586 void CLandscapeUser::setZFunc(UMaterial::ZFunc val)
588 NL3D_HAUTO_UI_LANDSCAPE;
589 _Landscape->Landscape.setZFunc((CMaterial::ZFunc)val);
592 // ***************************************************************************
593 const CZone* CLandscapeUser::getZone (sint zoneId) const
595 NL3D_HAUTO_UI_LANDSCAPE;
596 return _Landscape->Landscape.getZone(zoneId);
599 // ***************************************************************************
600 void CLandscapeUser::setVegetableDensity(float density)
602 NL3D_HAUTO_UI_LANDSCAPE;
603 _Landscape->Landscape.setVegetableDensity(density);
606 // ***************************************************************************
607 float CLandscapeUser::getVegetableDensity() const
609 NL3D_HAUTO_UI_LANDSCAPE;
610 return _Landscape->Landscape.getVegetableDensity();
613 // ***************************************************************************
614 float CLandscapeUser::getRayCollision(const NLMISC::CVector &start, const NLMISC::CVector &end)
616 NL3D_HAUTO_UI_LANDSCAPE;
617 return _Landscape->Landscape.getRayCollision(start, end);
620 } // NL3D