Files
dsrc/sku.0/sys.server/compiled/game/script/planet/planet_weather.script
T

149 lines
3.9 KiB
Plaintext

// Weather Control script
// by Cinco Barnes 11-30-2002
// modified by Steve Jakab
// message param names
const string PARAM_WEATHER_DESIRED = "desired";
const string PARAM_WEATHER_CURRENT = "current";
// weather types
const int WEATHER_GOOD = 0;
const int WEATHER_MILD = 1;
const int WEATHER_SEVERE = 2;
const int WEATHER_EXTREME = 3;
// Percent chance for given weather to occur. Make sure it adds up to 100! We don't verify this!
const int[] WEATHER_CHANCE = {
70, 15, 10, 5
//5, 10, 15, 70
};
// How long we want a given weather type to last. If the type is used as a transistion, it will last half as long
const int[][] WEATHER_DURATION_LIMITS = {
{
1200, 2400
},
{
240, 480
},
{
120, 300
},
{
120, 300
}
};
trigger OnAttach()
{
if ( isInWorld(self) )
{
debugServerConsoleMsg(null, "starting weather from OnAttach()");
startWeather(self);
}
return SCRIPT_CONTINUE;
}
trigger OnInitialize()
{
debugServerConsoleMsg(null, "starting weather from OnInitialize()");
startWeather(self);
return SCRIPT_CONTINUE;
}
trigger OnDetach()
{
// Reset to clear weather regardless of state
setWeatherData (WEATHER_GOOD, 0.0f, 0.0f);
return SCRIPT_CONTINUE;
}
void startWeather(obj_id self)
{
// start off with good weather
int desiredWeather = WEATHER_GOOD;
int currentWeather = WEATHER_GOOD;
float windVectorX = rand(1.0f, 2.0f);
float windVectorZ = rand(1.0f, 2.0f);
setWeatherData (currentWeather, windVectorX, windVectorZ);
int weatherDuration = rand (WEATHER_DURATION_LIMITS[currentWeather][0], WEATHER_DURATION_LIMITS[currentWeather][1]);
debugServerConsoleMsg(null, "planet_weather script start setting weather to " + currentWeather + "(" + desiredWeather +
") for " + weatherDuration + " seconds");
dictionary params = new dictionary();
params.put(PARAM_WEATHER_DESIRED, desiredWeather);
params.put(PARAM_WEATHER_CURRENT, currentWeather);
messageTo(self, "updateWeather", params, weatherDuration, false);
}
messageHandler updateWeather()
{
// get the next weather
int desiredWeather = params.getInt(PARAM_WEATHER_DESIRED);
int currentWeather = params.getInt(PARAM_WEATHER_CURRENT);
debugServerConsoleMsg(null, "planet_weather script got weather " + currentWeather + "(" + desiredWeather + ")");
if ( currentWeather != desiredWeather )
{
// move the current weather closer to the desired weather
if ( currentWeather > desiredWeather )
--currentWeather;
else
++currentWeather;
}
else if (desiredWeather != 0)
{
// we want to transition back to clear weather
desiredWeather = 0;
--currentWeather;
}
else
{
// decide on a new weather type
int r = rand(1, 100);
for ( int i = 0; i <= WEATHER_EXTREME; ++i )
{
if ( r <= WEATHER_CHANCE[i] )
{
desiredWeather = i;
break;
}
r -= WEATHER_CHANCE[i];
}
if ( desiredWeather != 0 )
++currentWeather;
}
// sanity check
if ( currentWeather < 0 )
currentWeather = 0;
else if ( currentWeather > WEATHER_EXTREME )
currentWeather = WEATHER_EXTREME;
if ( desiredWeather < 0 )
desiredWeather = 0;
else if ( desiredWeather > WEATHER_EXTREME )
desiredWeather = WEATHER_EXTREME;
// decide how long the current weather will last
int weatherDuration = rand (WEATHER_DURATION_LIMITS[currentWeather][0], WEATHER_DURATION_LIMITS[currentWeather][1]);
// transition weather lasts half as long
if ( desiredWeather != currentWeather )
weatherDuration /= 2;
float windVectorX = rand(1.0f, 2.0f);
float windVectorZ = rand(1.0f, 2.0f);
setWeatherData (currentWeather, windVectorX, windVectorZ);
debugServerConsoleMsg(null, "planet_weather script setting weather to " + currentWeather + "(" + desiredWeather +
") for " + weatherDuration + " seconds");
dictionary newParams = new dictionary();
newParams.put(PARAM_WEATHER_DESIRED, desiredWeather);
newParams.put(PARAM_WEATHER_CURRENT, currentWeather);
messageTo(self, "updateWeather", newParams, weatherDuration, false);
return SCRIPT_CONTINUE;
}