1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
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/>.
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
;
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
] =
47 CRyzomTime::good
, CRyzomTime::good
,
48 CRyzomTime::bad
, CRyzomTime::bad
,
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
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.
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() );
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
);
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
;
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
);
144 return CRyzomTime::unknownWeather
;
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
];