Add infos into target window
[ryzomcore.git] / ryzom / server / src / entities_game_service / weather_everywhere.cpp
blobdabe9b453ca5dc1f962a6b01276f94e39bf1eaed
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
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/>.
19 #include "stdpch.h"
20 #include "weather_everywhere.h"
21 #include "nel/misc/sheet_id.h"
22 #include "nel/georges/load_form.h"
23 #include "nel/georges/u_form_elm.h"
24 #include "zone_manager.h"
25 #include "egs_sheets/egs_sheets.h"
26 #include "game_share/time_weather_season/weather_manager.h"
27 #include "game_share/time_weather_season/weather_setup_sheet_base.h"
28 #include "game_share/time_weather_season/weather_function_params_sheet_base.h"
29 #include "game_share/time_weather_season/weather_predict.h"
30 #include "game_share/season.h"
32 using namespace NLMISC;
33 using namespace NLGEORGES;
34 using namespace std;
37 /// Singleton instance
38 CWeatherEverywhere WeatherEverywhere;
40 /// Number of possible weather setups
41 const uint NB_WEATHER_SETUPS = 6;
43 /// Mapping from weather setups to EWeather value
44 const CRyzomTime::EWeather WeatherSetupsToValue [NB_WEATHER_SETUPS] =
46 CRyzomTime::best,
47 CRyzomTime::good, CRyzomTime::good,
48 CRyzomTime::bad, CRyzomTime::bad,
49 CRyzomTime::worst
54 * Initialization
56 void CWeatherEverywhere::init()
58 // Init weather manager (with weather setup sheets)
59 std::vector<CSheetId> sheetVec;
60 CSheetId::buildIdVector( sheetVec, "weather_setup" ); // list weather_setup sheets
61 std::vector<const CWeatherSetupSheetBase *> weatherSheets;
62 std::vector<std::string> weatherSheetNames;
63 for ( uint k=0; k!=sheetVec.size(); ++k )
65 const CWeatherSetupSheetBase *sheet = CSheets::getWeatherSetupSheet( sheetVec[k] ); // get static sheet
66 if ( sheet )
68 weatherSheetNames.push_back( sheetVec[k].toString() );
69 weatherSheets.push_back( sheet );
72 CWeatherManager weatherManager;
73 weatherManager.init( weatherSheets, weatherSheetNames );
75 // Load the continents (george forms). The sorted list is found in CStaticWorld.
76 sheetVec.clear();
77 const CStaticWorld *staticWorld = CSheets::getWorldForm( CSheetId("ryzom.world") );
78 nlassert( staticWorld );
79 nlassertex( staticWorld->Continents.size() == CONTINENT::NB_CONTINENTS, ("ryzom.world should contain exactly the same number of continents as the enum size CONTINENT::TContinent (=%u), not %u", (uint)(CONTINENT::NB_CONTINENTS), staticWorld->Continents.size()) );
80 UFormLoader *formLoader = UFormLoader::createLoader();
81 _WeatherFunctionsBySeasonByContinent.resize( staticWorld->Continents.size() );
82 for ( uint iContinent=0; iContinent!=CONTINENT::NB_CONTINENTS; ++iContinent )
84 _WeatherFunctionsBySeasonByContinent[iContinent] = new CWeatherFunction [EGSPD::CSeason::Invalid];
85 CSmartPtr<UForm> continentForm = formLoader->loadForm( staticWorld->Continents[iContinent].toString().c_str() ); // load continent form
86 if ( continentForm == NULL )
88 nlwarning( "Continent sheet '%s' not found", staticWorld->Continents[iContinent].toString().c_str() );
90 else
92 for ( uint iSeason=0; iSeason!=EGSPD::CSeason::Invalid; ++iSeason )
94 string seasonStr = EGSPD::CSeason::toString( (EGSPD::CSeason::TSeason) iSeason);
95 const UFormElm *seasonWFNode = NULL;
96 if ( continentForm->getRootNode().getNodeByName( &seasonWFNode, (seasonStr + string("WeatherFunction")).c_str() ) && seasonWFNode )
98 CWeatherFunctionSheet wfs;
99 wfs.build( *seasonWFNode );
100 _WeatherFunctionsBySeasonByContinent[iContinent][iSeason].buildFromSheet( wfs, weatherManager );
102 else
104 // Not a warning because indoor continent does not have any weather function
105 nlinfo( "%s has no node %s", staticWorld->Continents[iContinent].toString().c_str(), (seasonStr + string("WeatherFunction")).c_str() );
110 UFormLoader::releaseLoader(formLoader);
112 // Use always first weather function param sheet
113 if ( CSheets::getWeatherFunctionParamsSheets().begin() != CSheets::getWeatherFunctionParamsSheets().end() )
115 _WeatherFunctionParamsSheet = &(*CSheets::getWeatherFunctionParamsSheets().begin()).second;
121 * Destructor
123 CWeatherEverywhere::~CWeatherEverywhere()
125 for ( uint i=0; i!=_WeatherFunctionsBySeasonByContinent.size(); ++i )
127 delete [] _WeatherFunctionsBySeasonByContinent[i];
133 * Return the weather at the specified position & time.
134 * In case of failure (such as a position outside a continent), return unknownWeather.
136 CRyzomTime::EWeather CWeatherEverywhere::getWeather( const NLMISC::CVector& pos, const CRyzomTime& ryzomTime ) const
138 if ( ! _WeatherFunctionParamsSheet )
139 return CRyzomTime::unknownWeather;
141 // Get continent of which the position belongs
142 CContinent *continent = CZoneManager::getInstance().getContinent( pos );
143 if ( ! continent )
144 return CRyzomTime::unknownWeather;
146 // Predict weather
147 float weatherFloatValue = CPredictWeather::predictWeather(
148 ryzomTime.getRyzomDay(),
149 ryzomTime.getRyzomTime(),
150 *_WeatherFunctionParamsSheet,
151 _WeatherFunctionsBySeasonByContinent[continent->getId()] );
153 // Map from weather setup to EWeather value
154 uint weatherSetupIndex = min( (uint)(weatherFloatValue*((float)NB_WEATHER_SETUPS)), NB_WEATHER_SETUPS-1 );
155 //nldebug( "Weather setup of position is %u", weatherSetupIndex );
156 return WeatherSetupsToValue[weatherSetupIndex];