diff --git a/Source/Framework/IO/ByteBuffer.cs b/Source/Framework/IO/ByteBuffer.cs index 413582a3e..2a2055b44 100644 --- a/Source/Framework/IO/ByteBuffer.cs +++ b/Source/Framework/IO/ByteBuffer.cs @@ -149,9 +149,7 @@ namespace Framework.IO public uint ReadPackedTime() { - uint packedDate = ReadUInt32(); - var time = new DateTime((int)((packedDate >> 24) & 0x1F) + 2000, (int)((packedDate >> 20) & 0xF) + 1, (int)((packedDate >> 14) & 0x3F) + 1, (int)(packedDate >> 6) & 0x1F, (int)(packedDate & 0x3F), 0); - return (uint)Time.DateTimeToUnixTime(time); + return (uint)Time.GetUnixTimeFromPackedTime(ReadUInt32()); } public Vector3 ReadVector3() @@ -360,14 +358,12 @@ namespace Framework.IO public void WritePackedTime(long time) { - var now = Time.UnixTimeToDateTime(time); - WriteUInt32(Convert.ToUInt32((now.Year - 2000) << 24 | (now.Month - 1) << 20 | (now.Day - 1) << 14 | (int)now.DayOfWeek << 11 | now.Hour << 6 | now.Minute)); + WriteUInt32(Time.GetPackedTimeFromUnixTime(time)); } public void WritePackedTime() { - DateTime now = DateTime.Now; - WriteUInt32(Convert.ToUInt32((now.Year - 2000) << 24 | (now.Month - 1) << 20 | (now.Day - 1) << 14 | (int)now.DayOfWeek << 11 | now.Hour << 6 | now.Minute)); + WriteUInt32(Time.GetPackedTimeFromDateTime(DateTime.Now)); } #endregion diff --git a/Source/Framework/Util/Time.cs b/Source/Framework/Util/Time.cs index 86c91d06f..c15044014 100644 --- a/Source/Framework/Util/Time.cs +++ b/Source/Framework/Util/Time.cs @@ -95,6 +95,7 @@ public static class Time { return DateTimeOffset.FromUnixTimeSeconds(unixTime).LocalDateTime; } + public static long DateTimeToUnixTime(DateTime dateTime) { return ((DateTimeOffset)dateTime).ToUnixTimeSeconds(); @@ -186,6 +187,23 @@ public static class Time return $"Days: {days} Hours: {hours} Minutes: {minute}"; } + public static long GetUnixTimeFromPackedTime(uint packedDate) + { + var time = new DateTime((int)((packedDate >> 24) & 0x1F) + 2000, (int)((packedDate >> 20) & 0xF) + 1, (int)((packedDate >> 14) & 0x3F) + 1, (int)(packedDate >> 6) & 0x1F, (int)(packedDate & 0x3F), 0); + return (uint)DateTimeToUnixTime(time); + } + + public static uint GetPackedTimeFromUnixTime(long unixTime) + { + var now = UnixTimeToDateTime(unixTime); + return Convert.ToUInt32((now.Year - 2000) << 24 | (now.Month - 1) << 20 | (now.Day - 1) << 14 | (int)now.DayOfWeek << 11 | now.Hour << 6 | now.Minute); + } + + public static uint GetPackedTimeFromDateTime(DateTime now) + { + return Convert.ToUInt32((now.Year - 2000) << 24 | (now.Month - 1) << 20 | (now.Day - 1) << 14 | (int)now.DayOfWeek << 11 | now.Hour << 6 | now.Minute); + } + public static void Profile(string description, int iterations, Action func) { //Run at highest priority to minimize fluctuations caused by other processes/threads diff --git a/Source/Game/Conditions/ConditionManager.cs b/Source/Game/Conditions/ConditionManager.cs index 28b2c573d..7ccc96f74 100644 --- a/Source/Game/Conditions/ConditionManager.cs +++ b/Source/Game/Conditions/ConditionManager.cs @@ -2043,7 +2043,14 @@ namespace Game return false; } - // TODO: time condition + if (condition.Time[0] != 0) + { + var from = Time.GetUnixTimeFromPackedTime(condition.Time[0]); + var to = Time.GetUnixTimeFromPackedTime(condition.Time[1]); + + if (GameTime.GetGameTime() < from || GameTime.GetGameTime() > to) + return false; + } if (condition.WorldStateExpressionID != 0) { @@ -2055,7 +2062,9 @@ namespace Game return false; } - // TODO: weather condition + if (condition.WeatherID != 0) + if (player.GetMap().GetZoneWeather(player.GetZoneId()) != (WeatherState)condition.WeatherID) + return false; if (condition.Achievement[0] != 0) { diff --git a/Source/Game/Maps/Map.cs b/Source/Game/Maps/Map.cs index 07455f9f4..f9c63cb82 100644 --- a/Source/Game/Maps/Map.cs +++ b/Source/Game/Maps/Map.cs @@ -3467,6 +3467,21 @@ namespace Game.Maps return info.DefaultWeather; } + public WeatherState GetZoneWeather(uint zoneId) + { + ZoneDynamicInfo zoneDynamicInfo = _zoneDynamicInfo.LookupByKey(zoneId); + if (zoneDynamicInfo != null) + { + if (zoneDynamicInfo.WeatherId != 0) + return zoneDynamicInfo.WeatherId; + + if (zoneDynamicInfo.DefaultWeather != null) + return zoneDynamicInfo.DefaultWeather.GetWeatherState(); + } + + return WeatherState.Fine; + } + public void SetZoneWeather(uint zoneId, WeatherState weatherId, float weatherGrade) { if (!_zoneDynamicInfo.ContainsKey(zoneId)) diff --git a/Source/Game/Weather/WeatherManager.cs b/Source/Game/Weather/WeatherManager.cs index 802d8fbae..c5659b7f9 100644 --- a/Source/Game/Weather/WeatherManager.cs +++ b/Source/Game/Weather/WeatherManager.cs @@ -339,7 +339,7 @@ namespace Game UpdateWeather(); } - WeatherState GetWeatherState() + public WeatherState GetWeatherState() { if (m_grade < 0.27f) return WeatherState.Fine;