Guest User

ClearNight by ClearShot

a guest
Feb 2nd, 2023
580
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.21 KB | None | 0 0
  1. using Facepunch;
  2. using Network;
  3. using Newtonsoft.Json;
  4. using Oxide.Core;
  5. using Oxide.Core.Plugins;
  6. using ProtoBuf;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using UnityEngine;
  12.  
  13. namespace Oxide.Plugins
  14. {
  15. [Info("Clear Night", "Clearshot", "2.3.5")]
  16. [Description("Always bright nights")]
  17. class ClearNight : CovalencePlugin
  18. {
  19. private PluginConfig _config;
  20. private EnvSync _envSync;
  21. private List<DateTime> _fullMoonDates = new List<DateTime> {
  22. new DateTime(2024, 1, 25),
  23. new DateTime(2024, 2, 24),
  24. new DateTime(2024, 3, 25),
  25. new DateTime(2024, 4, 23),
  26. new DateTime(2024, 5, 23),
  27. new DateTime(2024, 6, 21),
  28. new DateTime(2024, 7, 21),
  29. new DateTime(2024, 8, 19),
  30. new DateTime(2024, 9, 17),
  31. new DateTime(2024, 10, 17),
  32. new DateTime(2024, 11, 15),
  33. new DateTime(2024, 12, 15)
  34. };
  35. private Dictionary<string, string> _weatherSync = new Dictionary<string, string>();
  36. private DateTime _date;
  37. private Climate _climate;
  38. private int _current = 0;
  39. private bool _playSound = false;
  40.  
  41. private bool IsDay = false;
  42. private bool IsNight = false;
  43.  
  44. [PluginReference("NightVision")]
  45. Plugin NightVisionRef;
  46. VersionNumber NightVisionMinVersion = new VersionNumber(1, 4, 0);
  47.  
  48. void OnServerInitialized()
  49. {
  50. _envSync = BaseNetworkable.serverEntities.OfType<EnvSync>().FirstOrDefault();
  51. _climate = SingletonComponent<global::Climate>.Instance;
  52. _date = _fullMoonDates[_current];
  53.  
  54. TOD_Sky.Instance.Components.Time.OnDay += OnDay;
  55. TOD_Sky.Instance.Components.Time.OnSunset += OnSunset;
  56.  
  57. if (_envSync == null)
  58. {
  59. NextTick(() => {
  60. LogError("Unable to find EnvSync! Are you using a custom map?");
  61. Interface.Oxide.UnloadPlugin(Name);
  62. });
  63. return;
  64. }
  65.  
  66. if (NightVisionRef != null && NightVisionRef.Version < NightVisionMinVersion)
  67. {
  68. NextTick(() => {
  69. LogError($"NightVision version: v{NightVisionRef.Version}");
  70. LogError($"Please update NightVision to v{NightVisionMinVersion} or higher!");
  71. Interface.Oxide.UnloadPlugin(Name);
  72. });
  73. return;
  74. }
  75.  
  76. timer.Every(_config.syncInterval, () => {
  77. if (!_envSync.limitNetworking)
  78. {
  79. _envSync.limitNetworking = true;
  80. }
  81.  
  82. if (NightVisionRef != null)
  83. {
  84. NightVisionRef?.CallHook("BlockEnvUpdates", true);
  85. }
  86.  
  87. UpdateCelestials();
  88. SyncWeather();
  89.  
  90. List<Connection> subscribers = _envSync.net.group.subscribers;
  91. if (subscribers != null && subscribers.Count > 0)
  92. {
  93. for (int i = 0; i < subscribers.Count; i++)
  94. {
  95. Connection connection = subscribers[i];
  96. global::BasePlayer basePlayer = connection.player as global::BasePlayer;
  97.  
  98. if (!(basePlayer == null))
  99. {
  100. if (NightVisionRef != null && (bool)NightVisionRef?.CallHook("IsPlayerTimeLocked", basePlayer)) continue;
  101.  
  102. UpdatePlayerDateTime(ref connection, _config.freezeMoon && IsNight ? _date : _date.AddHours(TOD_Sky.Instance.Cycle.Hour));
  103.  
  104. if (_playSound)
  105. {
  106. var effect = new Effect(_config.sound, basePlayer, 0, Vector3.zero, Vector3.forward);
  107. EffectNetwork.Send(effect, connection);
  108. }
  109. }
  110. }
  111. }
  112.  
  113. _playSound = false;
  114. });
  115. }
  116.  
  117. void Unload()
  118. {
  119. TOD_Sky.Instance.Components.Time.OnDay -= OnDay;
  120. TOD_Sky.Instance.Components.Time.OnSunset -= OnSunset;
  121.  
  122. if (_envSync != null)
  123. {
  124. _envSync.limitNetworking = false;
  125. }
  126.  
  127. if (NightVisionRef != null)
  128. {
  129. NightVisionRef?.CallHook("BlockEnvUpdates", false);
  130. }
  131.  
  132. ServerMgr.SendReplicatedVars("weather.");
  133. }
  134.  
  135. void OnDay()
  136. {
  137. if (_config.randomizeDates)
  138. {
  139. _current = UnityEngine.Random.Range(0, _fullMoonDates.Count - 1);
  140. _date = _fullMoonDates[_current];
  141. }
  142. else
  143. {
  144. _current = _current >= _fullMoonDates.Count ? 0 : _current;
  145. _date = _fullMoonDates[_current];
  146. _current++;
  147. }
  148. }
  149.  
  150. void OnSunset()
  151. {
  152. _playSound = _config.playSoundAtSunset;
  153. }
  154.  
  155. private void UpdatePlayerDateTime(ref Connection connection, DateTime date)
  156. {
  157. NetWrite netWrite = Network.Net.sv.StartWrite();
  158. connection.validate.entityUpdates = connection.validate.entityUpdates + 1;
  159. BaseNetworkable.SaveInfo saveInfo = new global::BaseNetworkable.SaveInfo
  160. {
  161. forConnection = connection,
  162. forDisk = false
  163. };
  164. netWrite.PacketID(Message.Type.Entities);
  165. netWrite.UInt32(connection.validate.entityUpdates);
  166. using (saveInfo.msg = Pool.Get<Entity>())
  167. {
  168. _envSync.Save(saveInfo);
  169. saveInfo.msg.environment.dateTime = date.ToBinary();
  170. saveInfo.msg.environment.fog = 0;
  171. saveInfo.msg.environment.rain = 0;
  172. saveInfo.msg.environment.clouds = 0;
  173. saveInfo.msg.environment.wind = 0;
  174. if (saveInfo.msg.baseEntity == null)
  175. {
  176. LogError(this + ": ToStream - no BaseEntity!?");
  177. }
  178. if (saveInfo.msg.baseNetworkable == null)
  179. {
  180. LogError(this + ": ToStream - no baseNetworkable!?");
  181. }
  182. saveInfo.msg.ToProto(netWrite);
  183. _envSync.PostSave(saveInfo);
  184. netWrite.Send(new SendInfo(connection));
  185. }
  186. }
  187.  
  188. private void UpdatePlayerWeather(ref Dictionary<string, string> weatherVars)
  189. {
  190. NetWrite netWrite = Network.Net.sv.StartWrite();
  191. List<Network.Connection> list = Facepunch.Pool.GetList<Network.Connection>();
  192. foreach (Network.Connection connection in Network.Net.sv.connections)
  193. {
  194. if (connection.connected)
  195. {
  196. list.Add(connection);
  197. }
  198. }
  199. List<KeyValuePair<string, string>> list2 = Facepunch.Pool.GetList<KeyValuePair<string, string>>();
  200. list2.AddRange(weatherVars.ToList());
  201. netWrite.PacketID(Message.Type.ConsoleReplicatedVars);
  202. netWrite.Int32(list2.Count);
  203. foreach (var item in list2)
  204. {
  205. netWrite.String(item.Key);
  206. netWrite.String(item.Value);
  207. }
  208. netWrite.Send(new SendInfo(list));
  209. Facepunch.Pool.FreeList<KeyValuePair<string, string>>(ref list2);
  210. Facepunch.Pool.FreeList<Network.Connection>(ref list);
  211. }
  212.  
  213. private void UpdateCelestials()
  214. {
  215. float f = 0.0174532924f * TOD_Sky.Instance.World.Latitude;
  216. float num = Mathf.Sin(f);
  217. float num2 = Mathf.Cos(f);
  218. float longitude = TOD_Sky.Instance.World.Longitude;
  219. float num3 = 1.57079637f;
  220. int year = _date.Year;
  221. int month = _date.Month;
  222. int day = _date.Day;
  223. float num4 = TOD_Sky.Instance.Cycle.Hour - TOD_Sky.Instance.World.UTC;
  224. float num5 = (float)(367 * year - 7 * (year + (month + 9) / 12) / 4 + 275 * month / 9 + day - 730530) + num4 / 24f;
  225. float num7 = 23.4393f - 3.563E-07f * num5;
  226. float f2 = 0.0174532924f * num7;
  227. float num8 = Mathf.Sin(f2);
  228. float num9 = Mathf.Cos(f2);
  229. float num37 = 282.9404f + 4.70935E-05f * num5;
  230. float num38 = 0.016709f - 1.151E-09f * num5;
  231. float num39 = 356.047f + 0.985600233f * num5;
  232. float num40 = 0.0174532924f * num39;
  233. float num41 = Mathf.Sin(num40);
  234. float num42 = Mathf.Cos(num40);
  235. float f6 = num40 + num38 * num41 * (1f + num38 * num42);
  236. float num43 = Mathf.Sin(f6);
  237. float num44 = Mathf.Cos(f6) - num38;
  238. float num45 = Mathf.Sqrt(1f - num38 * num38) * num43;
  239. float num46 = 57.29578f * Mathf.Atan2(num45, num44);
  240. float num47 = Mathf.Sqrt(num44 * num44 + num45 * num45);
  241. float num48 = num46 + num37;
  242. float f7 = 0.0174532924f * num48;
  243. float num49 = Mathf.Sin(f7);
  244. float num50 = Mathf.Cos(f7);
  245. float num51 = num47 * num50;
  246. float num52 = num47 * num49;
  247. float num53 = num51;
  248. float num54 = num52 * num9;
  249. float y2 = num52 * num8;
  250. float num55 = Mathf.Atan2(num54, num53);
  251. float f8 = Mathf.Atan2(y2, Mathf.Sqrt(num53 * num53 + num54 * num54));
  252. float num56 = Mathf.Sin(f8);
  253. float num57 = Mathf.Cos(f8);
  254. float num58 = num46 + num37 + 180f + 15f * num4;
  255. float num59 = 0.0174532924f * (num58 + longitude);
  256. float f9 = num59 - num55;
  257. float num60 = Mathf.Sin(f9);
  258. float num61 = Mathf.Cos(f9) * num57;
  259. float num62 = num60 * num57;
  260. float num63 = num56;
  261. float num64 = num61 * num - num63 * num2;
  262. float num65 = num62;
  263. float y3 = num61 * num2 + num63 * num;
  264. float num67 = Mathf.Atan2(y3, Mathf.Sqrt(num64 * num64 + num65 * num65));
  265. float num68 = num3 - num67;
  266. float SunZenith = 57.29578f * num68;
  267. float LerpValue = Mathf.InverseLerp(105f, 90f, SunZenith);
  268. if (LerpValue > 0.1f)
  269. {
  270. IsDay = true;
  271. IsNight = false;
  272. }
  273. else
  274. {
  275. IsDay = false;
  276. IsNight = true;
  277. }
  278. }
  279.  
  280. private void SyncWeather()
  281. {
  282. if (_config.syncWeather)
  283. {
  284. _weatherSync["weather.atmosphere_brightness"] = _climate.WeatherOverrides.Atmosphere.Brightness != -1f ? _climate.WeatherOverrides.Atmosphere.Brightness.ToString() : _climate.WeatherState.Atmosphere.Brightness.ToString();
  285. _weatherSync["weather.atmosphere_contrast"] = _climate.WeatherOverrides.Atmosphere.Contrast != -1f ? _climate.WeatherOverrides.Atmosphere.Contrast.ToString() : _climate.WeatherState.Atmosphere.Contrast.ToString();
  286. _weatherSync["weather.atmosphere_directionality"] = _climate.WeatherOverrides.Atmosphere.Directionality != -1f ? _climate.WeatherOverrides.Atmosphere.Directionality.ToString() : _climate.WeatherState.Atmosphere.Directionality.ToString();
  287. _weatherSync["weather.atmosphere_mie"] = _climate.WeatherOverrides.Atmosphere.MieMultiplier != -1f ? _climate.WeatherOverrides.Atmosphere.MieMultiplier.ToString() : _climate.WeatherState.Atmosphere.MieMultiplier.ToString();
  288. _weatherSync["weather.atmosphere_rayleigh"] = _climate.WeatherOverrides.Atmosphere.RayleighMultiplier != -1f ? _climate.WeatherOverrides.Atmosphere.RayleighMultiplier.ToString() : _climate.WeatherState.Atmosphere.RayleighMultiplier.ToString();
  289. _weatherSync["weather.clear_chance"] = _climate.Weather.ClearChance.ToString();
  290. _weatherSync["weather.cloud_attenuation"] = _climate.WeatherOverrides.Clouds.Attenuation != -1f ? _climate.WeatherOverrides.Clouds.Attenuation.ToString() : _climate.WeatherState.Clouds.Attenuation.ToString();
  291. _weatherSync["weather.cloud_brightness"] = _climate.WeatherOverrides.Clouds.Brightness != -1f ? _climate.WeatherOverrides.Clouds.Brightness.ToString() : _climate.WeatherState.Clouds.Brightness.ToString();
  292. _weatherSync["weather.cloud_coloring"] = _climate.WeatherOverrides.Clouds.Coloring != -1f ? _climate.WeatherOverrides.Clouds.Coloring.ToString() : _climate.WeatherState.Clouds.Coloring.ToString();
  293. _weatherSync["weather.cloud_coverage"] = _climate.WeatherOverrides.Clouds.Coverage != -1f ? _climate.WeatherOverrides.Clouds.Coverage.ToString() : _climate.WeatherState.Clouds.Coverage.ToString();
  294. _weatherSync["weather.cloud_opacity"] = _climate.WeatherOverrides.Clouds.Opacity != -1f ? _climate.WeatherOverrides.Clouds.Opacity.ToString() : _climate.WeatherState.Clouds.Opacity.ToString();
  295. _weatherSync["weather.cloud_saturation"] = _climate.WeatherOverrides.Clouds.Saturation != -1f ? _climate.WeatherOverrides.Clouds.Saturation.ToString() : _climate.WeatherState.Clouds.Saturation.ToString();
  296. _weatherSync["weather.cloud_scattering"] = _climate.WeatherOverrides.Clouds.Scattering != -1f ? _climate.WeatherOverrides.Clouds.Scattering.ToString() : _climate.WeatherState.Clouds.Scattering.ToString();
  297. _weatherSync["weather.cloud_sharpness"] = _climate.WeatherOverrides.Clouds.Sharpness != -1f ? _climate.WeatherOverrides.Clouds.Sharpness.ToString() : _climate.WeatherState.Clouds.Sharpness.ToString();
  298. _weatherSync["weather.cloud_size"] = _climate.WeatherOverrides.Clouds.Size != -1f ? _climate.WeatherOverrides.Clouds.Size.ToString() : _climate.WeatherState.Clouds.Size.ToString();
  299. _weatherSync["weather.dust_chance"] = _climate.Weather.DustChance.ToString();
  300. _weatherSync["weather.fog"] = _climate.WeatherOverrides.Atmosphere.Fogginess != -1f ? _climate.WeatherOverrides.Atmosphere.Fogginess.ToString() : _climate.WeatherState.Atmosphere.Fogginess.ToString();
  301. _weatherSync["weather.fog_chance"] = _climate.Weather.FogChance.ToString();
  302. _weatherSync["weather.overcast_chance"] = _climate.Weather.OvercastChance.ToString();
  303. _weatherSync["weather.rain"] = _climate.WeatherOverrides.Rain != -1f ? _climate.WeatherOverrides.Rain.ToString() : _climate.WeatherState.Rain.ToString();
  304. _weatherSync["weather.rain_chance"] = _climate.Weather.RainChance.ToString();
  305. _weatherSync["weather.rainbow"] = _climate.WeatherOverrides.Rainbow != -1f ? _climate.WeatherOverrides.Rainbow.ToString() : _climate.WeatherState.Rainbow.ToString();
  306. _weatherSync["weather.storm_chance"] = _climate.Weather.StormChance.ToString();
  307. _weatherSync["weather.thunder"] = _climate.WeatherOverrides.Thunder != -1f ? _climate.WeatherOverrides.Thunder.ToString() : _climate.WeatherState.Thunder.ToString();
  308. _weatherSync["weather.wind"] = _climate.WeatherOverrides.Wind != -1f ? _climate.WeatherOverrides.Wind.ToString() : _climate.WeatherState.Wind.ToString();
  309.  
  310. if (IsNight)
  311. {
  312. foreach (var pair in _config.weatherAtNight)
  313. {
  314. _weatherSync[pair.Key] = pair.Value;
  315. }
  316. }
  317.  
  318. UpdatePlayerWeather(ref _weatherSync);
  319. }
  320. else if (IsNight && _config.weatherAtNight.Count > 0)
  321. {
  322. UpdatePlayerWeather(ref _config.weatherAtNight);
  323. }
  324. }
  325.  
  326. [Command("clearnight.debug")]
  327. private void DebugCommand(Core.Libraries.Covalence.IPlayer player, string command, string[] args)
  328. {
  329. player.Message("clearnight.debug");
  330. if (!player.IsAdmin && !player.IsServer) return;
  331.  
  332. StringBuilder _sb = new StringBuilder();
  333. _sb.AppendLine("\n*** DEBUG START ***\n");
  334. _sb.AppendLine($"ClearNight version: {Version}");
  335. _sb.AppendLine($"ClearNight date: {(_config.freezeMoon && IsNight ? _date : _date.AddHours(TOD_Sky.Instance.Cycle.Hour))}");
  336. _sb.AppendLine($"ClearNight IsNight: {IsNight}");
  337. _sb.AppendLine($"ClearNight IsDay: {IsDay}");
  338.  
  339. _sb.AppendLine($"\n[Server date and time]");
  340. _sb.AppendLine($"Year: {TOD_Sky.Instance.Cycle.Year}");
  341. _sb.AppendLine($"Month: {TOD_Sky.Instance.Cycle.Month}");
  342. _sb.AppendLine($"Day: {TOD_Sky.Instance.Cycle.Day}");
  343. _sb.AppendLine($"Hour: {TOD_Sky.Instance.Cycle.Hour}");
  344. _sb.AppendLine($"IsNight: {TOD_Sky.Instance.IsNight}");
  345. _sb.AppendLine($"IsDay: {TOD_Sky.Instance.IsDay}");
  346.  
  347. _sb.AppendLine($"\n[Config]");
  348. _sb.AppendLine(JsonConvert.SerializeObject(_config, Formatting.Indented, Config.Settings));
  349.  
  350. _sb.AppendLine($"\nNightVision installed: {NightVisionRef != null}");
  351. if (NightVisionRef != null)
  352. {
  353. _sb.AppendLine($"NightVision version: {NightVisionRef.Version}");
  354. }
  355.  
  356. _sb.AppendLine("\n*** DEBUG END ***");
  357. Puts(_sb.ToString());
  358. LogToFile("debug", _sb.ToString(), this);
  359. }
  360.  
  361. #region Config
  362.  
  363. protected override void LoadDefaultConfig()
  364. {
  365. Puts("Generating new default config file...");
  366. Config.WriteObject(GetDefaultConfig(), true);
  367. }
  368.  
  369. private PluginConfig GetDefaultConfig()
  370. {
  371. PluginConfig defaultConfig = new PluginConfig();
  372.  
  373. defaultConfig.fullMoonDates = _fullMoonDates.Select(d => d.ToString("MM/dd/yyyy")).ToArray();
  374. defaultConfig.weatherAtNight = new Dictionary<string, string> {
  375. { "weather.atmosphere_brightness", "1" },
  376. { "weather.atmosphere_contrast", "1.5" },
  377. { "weather.cloud_coverage", "0" },
  378. { "weather.cloud_size", "0" },
  379. { "weather.fog", "0" },
  380. { "weather.fog_chance", "0" }
  381. };
  382.  
  383. return defaultConfig;
  384. }
  385.  
  386. protected override void LoadConfig()
  387. {
  388. base.LoadConfig();
  389. _config = Config.ReadObject<PluginConfig>();
  390.  
  391. bool invalidDates = true;
  392. if (_config.fullMoonDates.Length > 0)
  393. {
  394. List<DateTime> tempDates = new List<DateTime>();
  395. foreach (string date in _config.fullMoonDates)
  396. {
  397. DateTime dt;
  398. if (DateTime.TryParse(date, out dt))
  399. {
  400. tempDates.Add(dt);
  401. }
  402. else
  403. {
  404. Puts($"invalid date: {date}");
  405. }
  406. }
  407.  
  408. if (tempDates.Count > 0)
  409. {
  410. invalidDates = false;
  411. _fullMoonDates = tempDates;
  412. Puts($"registered {_fullMoonDates.Count} {(_fullMoonDates.Count == 1 ? "date" : "dates")} from config");
  413. }
  414. }
  415.  
  416. if (invalidDates)
  417. {
  418. Puts("no valid dates registered, using default dates");
  419. }
  420.  
  421. Config.WriteObject(_config, true);
  422. }
  423.  
  424. private class PluginConfig
  425. {
  426. public string[] fullMoonDates;
  427. public Dictionary<string, string> weatherAtNight;
  428. public bool syncWeather = true;
  429. public bool randomizeDates = false;
  430. public bool freezeMoon = false;
  431. public bool playSoundAtSunset = false;
  432. public string sound = "assets/bundled/prefabs/fx/player/howl.prefab";
  433. public float syncInterval = 5f;
  434. }
  435.  
  436. #endregion
  437. }
  438. }
  439.  
Advertisement
Add Comment
Please, Sign In to add comment