This commit is contained in:
SamSWAT 2023-06-29 19:30:53 +03:00
parent c6576c0e11
commit 3015237661
7 changed files with 74 additions and 41 deletions

View File

@ -35,35 +35,35 @@ namespace SamSWAT.HeliCrash
{ {
case "bigmap": case "bigmap":
{ {
return PickRandom(Plugin.HeliCrashLocations.Customs); return Plugin.HeliCrashLocations.Customs.Shuffle().SelectRandom();
} }
case "Interchange": case "Interchange":
{ {
return PickRandom(Plugin.HeliCrashLocations.Interchange); return Plugin.HeliCrashLocations.Interchange.Shuffle().SelectRandom();;
} }
case "RezervBase": case "RezervBase":
{ {
return PickRandom(Plugin.HeliCrashLocations.Rezerv); return Plugin.HeliCrashLocations.Rezerv.Shuffle().SelectRandom();;
} }
case "Shoreline": case "Shoreline":
{ {
return PickRandom(Plugin.HeliCrashLocations.Shoreline); return Plugin.HeliCrashLocations.Shoreline.Shuffle().SelectRandom();;
} }
case "Woods": case "Woods":
{ {
return PickRandom(Plugin.HeliCrashLocations.Woods); return Plugin.HeliCrashLocations.Woods.Shuffle().SelectRandom();;
} }
case "Lighthouse": case "Lighthouse":
{ {
return PickRandom(Plugin.HeliCrashLocations.Lighthouse); return Plugin.HeliCrashLocations.Lighthouse.Shuffle().SelectRandom();;
} }
case "TarkovStreets": case "TarkovStreets":
{ {
return PickRandom(Plugin.HeliCrashLocations.StreetsOfTarkov); return Plugin.HeliCrashLocations.StreetsOfTarkov.Shuffle().SelectRandom();;
} }
case "develop": case "develop":
{ {
return PickRandom(Plugin.HeliCrashLocations.Develop); return Plugin.HeliCrashLocations.Develop.Shuffle().SelectRandom();;
} }
default: return new Location(); default: return new Location();
} }
@ -71,7 +71,7 @@ namespace SamSWAT.HeliCrash
private async Task<GameObject> LoadChoppaAsync() private async Task<GameObject> LoadChoppaAsync()
{ {
string path = Plugin.Directory + "Assets/Content/Vehicles/sikorsky_uh60_blackhawk.bundle"; var path = $"{Plugin.Directory}/Assets/Content/Vehicles/sikorsky_uh60_blackhawk.bundle";
var bundleLoadRequest = AssetBundle.LoadFromFileAsync(path); var bundleLoadRequest = AssetBundle.LoadFromFileAsync(path);
@ -82,6 +82,7 @@ namespace SamSWAT.HeliCrash
if (_heliBundle == null) if (_heliBundle == null)
{ {
Plugin.LogSource.LogFatal("Can't load UH-60 Blackhawk bundle");
Debug.LogError("[SamSWAT.HeliCrash]: Can't load UH-60 Blackhawk bundle"); Debug.LogError("[SamSWAT.HeliCrash]: Can't load UH-60 Blackhawk bundle");
return null; return null;
} }
@ -91,20 +92,14 @@ namespace SamSWAT.HeliCrash
while (!assetLoadRequest.isDone) while (!assetLoadRequest.isDone)
await Task.Yield(); await Task.Yield();
var requestedGO = assetLoadRequest.allAssets[0] as GameObject; var requestedGo = assetLoadRequest.allAssets[0] as GameObject;
if (requestedGO == null) if (requestedGo != null) return requestedGo;
{
Debug.LogError("[SamSWAT.HeliCrash]: failed to load asset"); Plugin.LogSource.LogFatal("Failed to load heli asset");
return null; Debug.LogError("[SamSWAT.HeliCrash]: failed to load heli asset");
} return null;
return requestedGO;
}
private T PickRandom<T>(List<T> list)
{
return list[Random.Range(0, list.Count)];
} }
} }
} }

View File

@ -13,26 +13,21 @@ namespace SamSWAT.HeliCrash
{ {
protected override MethodBase GetTargetMethod() protected override MethodBase GetTargetMethod()
{ {
return PatchConstants.LocalGameType.BaseType.GetMethod("method_10", BindingFlags.NonPublic | BindingFlags.Instance); //return PatchConstants.LocalGameType.BaseType.GetMethod("method_10", BindingFlags.NonPublic | BindingFlags.Instance);
return typeof(GameWorld).GetMethod(nameof(GameWorld.OnGameStarted));
} }
[PatchPostfix] [PatchPostfix]
public static void PatchPostfix() public static void PatchPostfix()
{ {
var gameWorld = Singleton<GameWorld>.Instance; var gameWorld = Singleton<GameWorld>.Instance;
bool crashAvailable = LocationScene.GetAll<AirdropPoint>().Any(); var crashAvailable = LocationScene.GetAll<AirdropPoint>().Any();
string location = gameWorld.RegisteredPlayers[0].Location; var location = gameWorld.RegisteredPlayers[0].Location;
if (gameWorld != null && crashAvailable && WillHeliCrash()) if (gameWorld == null || !crashAvailable || !BlessRNG.RngBool(Plugin.HeliCrashChance.Value)) return;
{
var heliCrash = gameWorld.gameObject.AddComponent<HeliCrash>(); var heliCrash = gameWorld.gameObject.AddComponent<HeliCrash>();
heliCrash.Init(location); heliCrash.Init(location);
}
}
private static bool WillHeliCrash()
{
return Random.Range(0, 100) <= Plugin.HeliCrashChance.Value;
} }
} }
} }

View File

@ -2,21 +2,25 @@
using BepInEx.Configuration; using BepInEx.Configuration;
using Newtonsoft.Json; using Newtonsoft.Json;
using System.IO; using System.IO;
using System.Reflection;
using BepInEx.Logging;
namespace SamSWAT.HeliCrash namespace SamSWAT.HeliCrash
{ {
[BepInPlugin("com.SamSWAT.HeliCrash", "SamSWAT.HeliCrash", "2.0.0")] [BepInPlugin("com.SamSWAT.HeliCrash", "SamSWAT.HeliCrash", "2.1.0")]
public class Plugin : BaseUnityPlugin public class Plugin : BaseUnityPlugin
{ {
public static HeliCrashLocations HeliCrashLocations; internal static HeliCrashLocations HeliCrashLocations;
internal static string Directory; internal static string Directory;
internal static ConfigEntry<int> HeliCrashChance; internal static ConfigEntry<int> HeliCrashChance;
internal static ManualLogSource LogSource;
private void Awake() private void Awake()
{ {
Directory = Path.Combine(BepInEx.Paths.PluginPath, "SamSWAT.HeliCrash/").Replace("\\", "/"); LogSource = Logger;
Directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
new HeliCrashPatch().Enable(); new HeliCrashPatch().Enable();
var json = File.ReadAllText(Directory + "HeliCrashLocations.json"); var json = File.ReadAllText($"{Directory}/HeliCrashLocations.json");
HeliCrashLocations = JsonConvert.DeserializeObject<HeliCrashLocations>(json); HeliCrashLocations = JsonConvert.DeserializeObject<HeliCrashLocations>(json);
HeliCrashChance = Config.Bind( HeliCrashChance = Config.Bind(

View File

@ -63,11 +63,12 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="HeliCrash.cs" /> <Compile Include="HeliCrash.cs" />
<Compile Include="HeliCrashLocations.cs" />
<Compile Include="HeliCrashPatch.cs" /> <Compile Include="HeliCrashPatch.cs" />
<Compile Include="Plugin.cs" /> <Compile Include="Plugin.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Utils.cs" /> <Compile Include="Utils\BlessRNG.cs" />
<Compile Include="Utils\HeliCrashLocations.cs" />
<Compile Include="Utils\Utils.cs" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project> </Project>

View File

@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace SamSWAT.HeliCrash
{
internal static class BlessRNG
{
private static readonly Random Rng = new Random((int) DateTime.Now.Ticks);
private static float Random(float a, float b)
{
var num = (float) Rng.NextDouble();
return a + (b - a) * num;
}
public static T SelectRandom<T>(this IReadOnlyList<T> list)
{
if (list.Count == 0)
{
return default;
}
var index = Rng.Next(0, list.Count);
return list[index];
}
public static bool RngBool(float chanceInPercent = 50f)
{
return Random(0f, 100f) < chanceInPercent;
}
internal static List<T> Shuffle<T>(this List<T> l)
{
return l.OrderBy(x => Random(0.0f, 5f)).ToList();
}
}
}