add weather system
This commit is contained in:
@@ -2169,18 +2169,15 @@ namespace ReallifeGamemode.Server.Commands
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Weather weatherBefore = NAPI.World.GetWeather();
|
Weather weatherBefore = World.WeatherSync.Weather;
|
||||||
NAPI.World.SetWeather(weather);
|
World.WeatherSync.SetWeather(weather);
|
||||||
Weather weatherAfter = NAPI.World.GetWeather();
|
Weather weatherAfter = World.WeatherSync.Weather;
|
||||||
|
|
||||||
|
ChatService.SendMessage(player, "~w~Wetter geändert: " + weatherAfter);
|
||||||
|
|
||||||
if (!weatherBefore.Equals(weatherAfter))
|
if (!weatherBefore.Equals(weatherAfter))
|
||||||
{
|
{
|
||||||
ChatService.SendMessage(player, "~w~Wetter geändert: " + NAPI.World.GetWeather());
|
NAPI.Notification.SendNotificationToAll("Das Wetter wurde von ~g~" + player.Name + " ~s~auf ~g~" + weatherAfter + "~s~ geändert.", true);
|
||||||
NAPI.Notification.SendNotificationToAll("Das Wetter wurde von ~g~" + player.Name + " ~s~auf ~g~" + NAPI.World.GetWeather() + "~s~ geändert.", true);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ChatService.SendMessage(player, "~w~Das Wetter konnte nicht geändert werden");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
104
ReallifeGamemode.Server/World/WeatherSync.cs
Normal file
104
ReallifeGamemode.Server/World/WeatherSync.cs
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
using System.Timers;
|
||||||
|
using GTANetworkAPI;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @overview Life of German Reallife - WeatherSync
|
||||||
|
* @author Alex_qp
|
||||||
|
* @copyright (c) 2008 - 2021 Life of German
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace ReallifeGamemode.Server.World
|
||||||
|
{
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Serverwide WeatherSystem (+ sync)
|
||||||
|
/// </summary>
|
||||||
|
static class WeatherSync
|
||||||
|
{
|
||||||
|
// SETTINGS
|
||||||
|
private const uint _syncInterval = 5 * 60 * 1000; // 5 min
|
||||||
|
private static readonly int[] _countRange = new int[] { 3, 5 }; // min, max both inclusive. How often should weather be synced before actual changing?
|
||||||
|
|
||||||
|
// INTERNAL
|
||||||
|
private static int _count = 0;
|
||||||
|
private static int _targetCount = GetNewTargetCount();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get/Set the current weather.
|
||||||
|
/// <remarks>Only use in main thread!</remarks>
|
||||||
|
/// </summary>
|
||||||
|
public static Weather Weather
|
||||||
|
{
|
||||||
|
get { return NAPI.World.GetWeather(); }
|
||||||
|
set { NAPI.World.SetWeather(value); }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Set the current weather by string.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>Only use in main thread!</remarks>
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="weather"></param>
|
||||||
|
public static void SetWeather(string weather)
|
||||||
|
{
|
||||||
|
NAPI.World.SetWeather(weather);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Timer weatherTimer;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Loads/Starts the WeatherSync.
|
||||||
|
/// </summary>
|
||||||
|
public static void Load()
|
||||||
|
{
|
||||||
|
weatherTimer = new Timer(_syncInterval);
|
||||||
|
weatherTimer.Start();
|
||||||
|
weatherTimer.Elapsed += OnWeatherTimer;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int GetNewTargetCount()
|
||||||
|
{
|
||||||
|
return new Random().Next(_countRange[0], _countRange[1] + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Weather GetRandomWeather(Weather[] weathers)
|
||||||
|
{
|
||||||
|
return weathers[new Random().Next(weathers.Length)];
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void OnWeatherTimer(object sender, ElapsedEventArgs e)
|
||||||
|
{
|
||||||
|
NAPI.Task.Run(() =>
|
||||||
|
{
|
||||||
|
if (_count < _targetCount)
|
||||||
|
{
|
||||||
|
// sync weather for all players
|
||||||
|
_count++;
|
||||||
|
Weather = Weather;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// change weather and reset counters
|
||||||
|
_count = 0;
|
||||||
|
_targetCount = GetNewTargetCount();
|
||||||
|
Weather = Weather switch
|
||||||
|
{
|
||||||
|
Weather.EXTRASUNNY => GetRandomWeather(new Weather[] { Weather.EXTRASUNNY, Weather.CLEAR }),
|
||||||
|
Weather.CLEAR => GetRandomWeather(new Weather[] { Weather.EXTRASUNNY, Weather.CLEAR, Weather.CLOUDS, Weather.SMOG }),
|
||||||
|
Weather.CLOUDS => GetRandomWeather(new Weather[] { Weather.CLEAR, Weather.CLOUDS, Weather.SMOG, Weather.FOGGY, Weather.OVERCAST }),
|
||||||
|
Weather.SMOG => GetRandomWeather(new Weather[] { Weather.CLOUDS, Weather.SMOG, Weather.FOGGY, Weather.OVERCAST }),
|
||||||
|
Weather.FOGGY => GetRandomWeather(new Weather[] { Weather.CLOUDS, Weather.SMOG, Weather.FOGGY, Weather.OVERCAST, Weather.RAIN }),
|
||||||
|
Weather.OVERCAST => GetRandomWeather(new Weather[] { Weather.CLOUDS, Weather.SMOG, Weather.RAIN, Weather.CLEARING }), // may add overcast
|
||||||
|
Weather.RAIN => GetRandomWeather(new Weather[] { Weather.OVERCAST, Weather.RAIN, Weather.CLEARING, Weather.THUNDER }),
|
||||||
|
Weather.THUNDER => GetRandomWeather(new Weather[] { Weather.RAIN, Weather.THUNDER, Weather.CLEARING }),
|
||||||
|
Weather.CLEARING => GetRandomWeather(new Weather[] { Weather.CLEAR, Weather.CLOUDS, Weather.CLEARING }),
|
||||||
|
_ => GetRandomWeather(new Weather[] { Weather.EXTRASUNNY, Weather.CLEAR }),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user