Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Nuclei/Events/ServerEvents.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Nuclei.Features;

namespace Nuclei.Events;

Expand All @@ -15,6 +16,7 @@ public static class ServerEvents
internal static void OnServerStarted()
{
ServerStarted?.Invoke();
TimeService.Initialize();
}

/// <summary>
Expand All @@ -26,4 +28,4 @@ internal static void OnServerStopped()
{
ServerStopped?.Invoke();
}
}
}
2 changes: 1 addition & 1 deletion Nuclei/Features/MissionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static class MissionService
/// <summary>
/// The current mission time.
/// </summary>
public static float CurrentMissionTime => Globals.MissionManagerInstance.missionTime;
public static float CurrentMissionTime => Globals.MissionManagerInstance.MissionTime;

/// <summary>
/// Gets all Mission Keys as an IEnumerable.
Expand Down
12 changes: 12 additions & 0 deletions Nuclei/Features/NucleiConfig.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json.Nodes;
using BepInEx.Configuration;
using Nuclei.Enums;
using Nuclei.Helpers;
Expand Down Expand Up @@ -62,6 +65,9 @@ public static class NucleiConfig

internal static ConfigEntry<bool>? RefreshServerNamePeriodically;
internal const bool DefaultRefreshServerNamePeriodically = true;

internal static ConfigEntry<bool>? RandomizeWeather;
internal const bool DefaultRandomizeWeather = false;

internal static ConfigEntry<short>? TargetFrameRate;
internal const short DefaultTargetFrameRate = 120;
Expand Down Expand Up @@ -164,6 +170,12 @@ internal static void InitSettings(ConfigFile config)

CommandPrefix = config.Bind(GeneralSection, "CommandPrefix", DefaultCommandPrefix, "What to use as the command prefix (the character at the start of a command).");
Nuclei.Logger?.LogDebug($"CommandPrefix: {CommandPrefix.Value}");

RandomizeWeather = config.Bind(GeneralSection, "RandomizeWeather", DefaultRandomizeWeather,
"Randomize weather by modifying the .json mission file directly. This requires the missions to be in " +
"the mission folder you assigned in DedicatedServerConfig.json, meaning all missions' MissionGroup must be User, not BuiltIn");

Nuclei.Logger?.LogDebug($"CommandPrefix: {CommandPrefix.Value}");

Nuclei.Logger?.LogDebug("Loaded settings!");
}
Expand Down
11 changes: 10 additions & 1 deletion Nuclei/Features/TimeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ private void FixedUpdate()
_lastTime = currentTime;

TimeEvents.OnEverySecond();


// MoTD
var motdFreq = NucleiConfig.MotDFrequency!.Value;
if (currentTime % motdFreq == 0)
{
ChatService.SendMotD();
}

if (currentTime % 3600 == 0)
{
TimeEvents.OnEveryHour();
Expand Down Expand Up @@ -85,4 +94,4 @@ private void FixedUpdate()
TimeEvents.OnEvery30Seconds();
}
}
}
}
38 changes: 38 additions & 0 deletions Nuclei/Features/WeatherRandomizerService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.IO;
using System.Linq;
using System.Text.Json;

namespace Nuclei.Features;

public class WeatherRandomizerService
{
internal static string MissionDir = null!;
public static void RandomizeWeather(string missionName)
{
var currentMissionDir = Directory.GetDirectories(MissionDir).First(x => x.Contains(missionName));
Nuclei.Logger?.LogInfo($"currentMissionDir: {currentMissionDir} ");
var currentMissionFile = $"{currentMissionDir}/{missionName}.json";
Nuclei.Logger?.LogInfo($"currentMissionFile: {currentMissionFile} ");

string json = File.ReadAllText(currentMissionFile);
var parsedJson = System.Text.Json.Nodes.JsonNode.Parse(json)!;

var writerOptions = new JsonSerializerOptions()
{
WriteIndented = true
};

Random rnd = new Random();
parsedJson["environment"]!["timeOfDay"] = rnd.Next(3,13);
parsedJson["environment"]!["timeFactor"] = 2.0;
parsedJson["environment"]!["weatherIntensity"] = rnd.NextDouble() * 0.9;
parsedJson["environment"]!["cloudAltitude"] = 500 + rnd.NextDouble() * 1000;
parsedJson["environment"]!["cloudAltitude"] = 500 + rnd.NextDouble() * 1000;
parsedJson["environment"]!["windSpeed"] = rnd.NextDouble() * 4;
parsedJson["environment"]!["windTurbulence"] = rnd.NextDouble() * 1;
parsedJson["environment"]!["windHeading"] = rnd.Next(0, 360);
parsedJson["environment"]!["windRandomHeading"] = rnd.Next(0, 91);
File.WriteAllText(currentMissionFile, parsedJson.ToJsonString(writerOptions));
}
}
1 change: 1 addition & 0 deletions Nuclei/Nuclei.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<PackageReference Include="BepInEx.Analyzers" Version="1.*" PrivateAssets="all"/>
<PackageReference Include="BepInEx.Core" Version="5.*"/>
<PackageReference Include="BepInEx.PluginInfoProps" Version="1.*"/>
<PackageReference Include="System.Text.Json" Version="10.0.1" />
</ItemGroup>

<!-- Nuclear Option-specific Assembly References -->
Expand Down
48 changes: 48 additions & 0 deletions Nuclei/Patches/MissionSaveLoadPatches.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using HarmonyLib;
using NuclearOption.SavedMission;
using Nuclei.Features;

[HarmonyPriority(Priority.First)]
[HarmonyWrapSafe]
[HarmonyPatch(typeof(MissionSaveLoad))]
public class MissionSaveLoadPatches
{
[HarmonyPostfix]
[HarmonyPatch(nameof(MissionSaveLoad.TryLoad))]
private static void Postfix(
MissionKey item,
ref Mission mission,
ref string error,
ref bool __result)
{
if (!__result || mission == null) return;

RandomizeWeather(ref mission);
ModifyDifficulty(ref mission);
}

private static void ModifyDifficulty(ref Mission mission)
{
// This is set to scale for larger player counts better
foreach (var f in mission.factions)
{
f.addAIPerEnemyPlayer = 0.80f;
f.AIAircraftLimit = 8;
}
}

private static void RandomizeWeather(ref Mission mission)
{
if (!NucleiConfig.RandomizeWeather!.Value) return;

var rnd = new Random();
mission.environment.timeOfDay = rnd.Next(3, 18);
mission.environment.timeFactor = 8f;
mission.environment.weatherIntensity = (float)(rnd.NextDouble() * 0.9);
mission.environment.cloudAltitude = (float)(500 + rnd.NextDouble() * 1000);
mission.environment.windSpeed = (float)(rnd.NextDouble() * 4);
mission.environment.windTurbulence = (float)rnd.NextDouble();
mission.environment.windHeading = rnd.Next(0, 360);
}
}