Files
zzz_REBIRTH__Utils/Harmony/Harmony_SkyManager.cs
2025-06-04 16:44:53 +09:30

170 lines
8.2 KiB
C#

using PI.NGSS;
namespace Harmony.SkyManagerPatches
{
internal class SkyManagerPatches
{
[HarmonyPatch(typeof(SkyManager))]
[HarmonyPatch("UpdateSunMoonAngles")]
public class UpdateSunMoonAnglesPatch
{
public static bool Prefix(SkyManager __instance)
{
if (!(bool)(UnityEngine.Object)SkyManager.sunLight || !(bool)(UnityEngine.Object)SkyManager.moonLight)
{
return false;
}
int num1 = GameStats.GetInt(EnumGameStats.DayLightLength);
SkyManager.duskTime = 22f;
if (num1 > 22)
{
SkyManager.duskTime = Mathf.Clamp((float)num1, 0.0f, 23.999f);
}
SkyManager.dawnTime = Mathf.Clamp(SkyManager.duskTime - (float)num1, 0.0f, 23.999f);
if ((double)Time.time - (double)SkyManager.worldRotationTime >= 0.20000000298023224 || SkyManager.bUpdateSunMoonNow)
{
SkyManager.worldRotationTime = Time.time;
#region rebirth
if (RebirthUtilities.ScenarioSkip() && !GameManager.Instance.World.IsEditor() && !GameUtils.IsPlaytesting() && RebirthUtilities.IsHiveDayActive())
{
SkyManager.worldRotationTime = 0f;
}
#endregion
SkyManager.bUpdateSunMoonNow = false;
float num2 = SkyManager.TimeOfDay();
#region rebirth
if (RebirthUtilities.ScenarioSkip() && !GameManager.Instance.World.IsEditor() && !GameUtils.IsPlaytesting() && RebirthUtilities.IsHiveDayActive())
{
num2 = 0;
}
#endregion
if ((double)num2 >= (double)SkyManager.dawnTime && (double)num2 < (double)SkyManager.duskTime)
{
SkyManager.worldRotationTarget = (float)(((double)num2 - (double)SkyManager.dawnTime) / ((double)SkyManager.duskTime - (double)SkyManager.dawnTime));
}
else
{
float num3 = 24f - SkyManager.duskTime;
float num4 = num3 + SkyManager.dawnTime;
SkyManager.worldRotationTarget = (double)num2 >= (double)SkyManager.dawnTime ? (num2 - SkyManager.duskTime) / num4 : (num3 + num2) / num4;
++SkyManager.worldRotationTarget;
}
SkyManager.worldRotationTarget *= 0.5f;
SkyManager.worldRotationTarget = Mathf.Clamp01(SkyManager.worldRotationTarget);
}
float num5 = SkyManager.worldRotationTarget - SkyManager.worldRotation;
float worldRotationTarget = SkyManager.worldRotationTarget;
if ((double)num5 < -0.5)
{
++worldRotationTarget;
}
else if ((double)num5 > 0.5)
{
--worldRotationTarget;
}
SkyManager.worldRotation = Mathf.Lerp(SkyManager.worldRotation, worldRotationTarget, 0.05f);
if ((double)SkyManager.worldRotation < 0.0)
{
++SkyManager.worldRotation;
}
else if ((double)SkyManager.worldRotation >= 1.0)
{
--SkyManager.worldRotation;
}
SkyManager.dayPercent = SkyManager.CalcDayPercent();
double angle1 = (double)SkyManager.worldRotation * 360.0;
SkyManager.sunDirV = Quaternion.AngleAxis((float)angle1, __instance.sunAxis) * __instance.sunStartV;
SkyManager.moonLightRot = Quaternion.LookRotation(Quaternion.AngleAxis((float)angle1, __instance.sunAxis) * __instance.moonStartV);
float angle2 = SkyManager.worldRotation * 360f;
if ((double)SkyManager.sunIntensity >= 1.0 / 1000.0)
{
if ((double)angle2 < 14.0)
{
angle2 = 14f;
}
if ((double)angle2 > 166.0)
{
angle2 = 166f;
}
Vector3 eulerAngles = Quaternion.LookRotation(Quaternion.AngleAxis(angle2, __instance.sunAxis) * __instance.sunStartV).eulerAngles;
SkyManager.sunLightT.localEulerAngles = eulerAngles;
SkyManager.sunLight.shadowStrength = 1f;
SkyManager.sunLight.shadows = (double)SkyManager.sunIntensity > 0.0 ? LightShadows.Soft : LightShadows.None;
SkyManager.moonLight.enabled = false;
}
else if ((double)SkyManager.moonLightColor.grayscale > 0.0)
{
if ((double)angle2 < 166.0)
{
angle2 = 166f;
}
if ((double)angle2 > 346.0)
{
angle2 = 346f;
}
Vector3 eulerAngles = Quaternion.LookRotation(Quaternion.AngleAxis(angle2, __instance.sunAxis) * __instance.moonStartV).eulerAngles;
SkyManager.moonLightT.localEulerAngles = eulerAngles;
float num6 = SkyManager.fogLightScale * SkyManager.moonBright * Utils.FastLerp(0.2f, 1f, GamePrefs.GetFloat(EnumGamePrefs.OptionsGfxBrightness) * 2f);
SkyManager.moonLight.intensity = num6;
SkyManager.moonLight.color = SkyManager.moonLightColor;
SkyManager.moonLight.shadowStrength = 1f;
SkyManager.moonLight.shadows = (double)num6 > 0.0 ? LightShadows.Soft : LightShadows.None;
SkyManager.moonLight.enabled = true;
}
else
{
SkyManager.moonLight.enabled = false;
}
SkyManager.sunMoonDirV = SkyManager.sunDirV;
if ((double)SkyManager.sunIntensity < 1.0 / 1000.0)
{
SkyManager.sunMoonDirV = SkyManager.moonLightRot * Vector3.forward;
}
if (!GameManager.IsDedicatedServer && (bool)(UnityEngine.Object)SkyManager.mainCamera)
{
Vector3 position = SkyManager.mainCamera.transform.position;
if ((bool)(UnityEngine.Object)SkyManager.moonSpriteT)
{
SkyManager.moonSpriteT.position = SkyManager.moonLightRot * Vector3.forward * -45000f;
SkyManager.moonSpriteT.rotation = Quaternion.LookRotation(SkyManager.moonSpriteT.position, Vector3.up);
SkyManager.moonSpriteT.position += position;
float num7 = 6857.143f;
if (SkyManager.IsBloodMoonVisible())
{
num7 *= 1.3f;
}
SkyManager.moonSpriteT.localScale = new Vector3(num7, num7, num7);
}
__instance.UpdateSunShaftSettings();
}
SkyManager.atmosphereSphere.Rotate(__instance.starAxis, SkyManager.worldRotation * 0.004f);
if (!__instance.bUpdateShaders || !(bool)(UnityEngine.Object)SkyManager.cloudsSphereMtrl)
{
return false;
}
SkyManager.cloudsSphereMtrl.SetVector("_SunDir", (Vector4)SkyManager.sunDirV);
SkyManager.cloudsSphereMtrl.SetVector("_SunMoonDir", (Vector4)SkyManager.sunMoonDirV);
return false;
}
}
[HarmonyPatch(typeof(SkyManager))]
[HarmonyPatch("IsBloodMoonVisible")]
public class IsBloodMoonVisiblePatch
{
public static void Postfix(ref bool __result)
{
if (WeatherManager.forceRain > 0.5f && (WeatherManager.forceClouds * 100f) >= 70f)
{
//Log.Out("SkyManagerPatches-IsBloodMoonVisible 1");
__result = true;
}
}
}
}
}