Weather System - mganzarcik/fabulae GitHub Wiki

Weather depends on date and location – a cold location in summer will probably have lower temperatures than a warm location in summer.

Weather is computed but is not rendered while indoors.

`[toggle gif]` Rain

Weather Profiles

The engine uses a concept of Weather Profiles and Weather Profile Modifiers. A Weather Profile defines the minimum and maximum temperatures, minimum and maximum duration, chances of different types of precipitation (small, medium and high precipitation, which for rain would imply something like a drizzle, rain and a storm) and the specific particle effects to be used for these precipitation types.

They also define sounds that should be played alongside the particle effects. The profile works with two kinds of sounds:

  • Continuous: one random track from the continuous tracks is looped for the whole duration of the particle effect. As an example, for a rain particle effect, this would be the sound of rain drops falling to the ground.
  • Random: these tracks are played at random intervals during the effect’s duration. The weather picks a random track from these at a configuration-defined interval and then generates a random number between 0 and 100. If the number is lower than the track’s chance to play, the track will be played once. For the rain example above, these sounds would be the different thunder sounds that can occur.

Weather Profiles are assigned to months in the Calendar. Each month should have a Weather Profile defined if any weather effects are to occur during its duration.

Weather Profile Modifiers define temperature and precipitation chance modifiers that should be applied per game map to the Weather Profile of the current month. A map can have more than one modifier defined.

Example Weather Profile:

Example Weather Profile Modifier:

`[toggle gif]` Snow

Weather Management

All the weather related logic is managed by a Weather Manager. Weather Manager is invoked regularly, but the time between invocations is a little randomized to make the weather appear more chaotic. The configuration defines the minimum and maximum number of seconds between two calls of the Weather Manager. Each time it is called, it will randomly pick a number from this interval to determine when it will be called again.

Every time the Weather Manager gets called, it updates the weather effect currently in progress (if any), ending it if it ran out of its duration, checks the current game date, gathers the currently active Weather Profiles and Weather Profile Modifiers and then decides if any action needs to be taken to change the current weather based on the logic below.

Detailed steps description

Determine current temperature

The temperature is determined in the following way:

  1. Check if it is day or night. If it is nighttime, apply the nighttime temperature modifier (defined globally in the Configuration) to the temperature minimums and maximums.
  2. Take the current temperature. If no temperature has been defined yet, calculate a random new one between the current profile’s minimum and maxim, set it and end.
  3. Determine if the current temperature is within the minimum and maximum of the current weather profile.
    1. If it is, randomly determine whether to modify the temperature or keep it as it is. If change is required, generated a random number between the minimum and maximum. If the number falls below the current temperature, decrease the temperature by one, otherwise increase it by one. If the new temperature is less or more than the profile defined minimum and maximum, cap it before setting it. Overall, this logic will make the temperature near the average temperature over long periods of time.
    2. If it is not, determine whether we need to lower it or increase it in order to get within the limits and then do so.

Should precipitation occur?

In order to determine whether any precipitation should occur during the current hour, the system does a simple check.

  1. A number between 0 (inclusive) and 99 (inclusive) is generated.
  2. If the number is between 0 (inclusive) and small precipitation chance (exclusive), small precipitation occurs.
  3. If the number is between the small precipitation chance (inclusive) and small precipitation chance + medium precipitation chance (exclusive), medium precipitation occurs.
  4. If the number is between small precipitation chance + medium precipitation chance (inclusive) and small precipitation chance + medium precipitation chance + high precipitation chance (exclusive), high precipitation occurs
  5. Otherwise, no precipitation occurs

The type of the precipitation is determined by the current temperature – a threshold is defined in the configuration. Any temperature below the threshold results in snow and any above in rain.

Example:

If the chances are defined as follows:

  • Small precipitation: 25%
  • Medium precipitation: 10%
  • High precipitation: 5%

If the random number belongs to [0, 24], we will have a small precipitation. If it belongs to [25, 34], will have medium precipitation. If it belongs to [35, 39], we will have high precipitation. In all other cases, there will be no precipitation.

This means that when defining the chances in the Weather Profile, the sum of the individual chances should not go over 100%.

⚠️ **GitHub.com Fallback** ⚠️