0
0
mirror of https://github.com/sp-tarkov/modules.git synced 2025-02-13 08:50:43 -05:00
modules/project/SPT.Debugging/Patches/PMCBotSpawnLocationPatch.cs

99 lines
3.9 KiB
C#
Raw Normal View History

using System.Linq;
using System.Reflection;
2024-05-21 19:10:17 +01:00
using SPT.Reflection.Patching;
using EFT;
using EFT.UI;
using HarmonyLib;
using System.Collections.Generic;
using EFT.Game.Spawning;
using System;
2024-05-21 19:10:17 +01:00
namespace SPT.Debugging.Patches
{
// TODO: Instantiation of this is fairly slow, need to find best way to cache it
public class SptSpawnHelper
{
2024-08-29 11:41:21 +01:00
private readonly List<ISpawnPoint> _playerSpawnPoints;
private readonly Random _rnd = new Random();
//private readonly GStruct381 _spawnSettings = new GStruct381();
public SptSpawnHelper()
{
IEnumerable<ISpawnPoint> locationSpawnPoints = SpawnPointManagerClass.CreateFromScene();
var playerSpawns = locationSpawnPoints.Where(x => x.Categories.HasFlag(ESpawnCategoryMask.Player)).ToList();
2024-08-29 11:41:21 +01:00
this._playerSpawnPoints = locationSpawnPoints.Where(x => x.Categories.HasFlag(ESpawnCategoryMask.Player)).ToList();
}
public void PrintSpawnPoints()
{
2024-08-29 11:41:21 +01:00
foreach (var spawnPoint in _playerSpawnPoints)
{
2024-05-21 19:10:17 +01:00
ConsoleScreen.Log("[SPT PMC Bot spawn] Spawn point " + spawnPoint.Id + " location is " + spawnPoint.Position.ToString());
}
}
public ISpawnPoint SelectSpawnPoint()
{
// TODO: Select spawn points more intelligently
2024-08-29 11:41:21 +01:00
return this._playerSpawnPoints[_rnd.Next(this._playerSpawnPoints.Count)];
}
public List<ISpawnPoint> SelectSpawnPoints(int count)
{
// TODO: Fine-grained spawn selection
2024-08-29 11:41:21 +01:00
if (count > this._playerSpawnPoints.Count())
{
ConsoleScreen.Log($"[SPT PMC Bot spawn] Wanted: ${count} but only {this._playerSpawnPoints.Count()} spawn points found, returning all");
2024-08-29 11:41:21 +01:00
return this._playerSpawnPoints;
}
2024-08-29 11:41:21 +01:00
return this._playerSpawnPoints.OrderBy(x => _rnd.Next()).Take(count).ToList();
}
}
public class PMCBotSpawnLocationPatch : ModulePatch
{
protected override MethodBase GetTargetMethod()
{
2024-09-03 12:27:08 +01:00
return AccessTools.Method(typeof(BotSpawner), nameof(BotSpawner.TryToSpawnInZoneInner));
}
[PatchPrefix]
2024-10-31 17:19:37 +00:00
public static bool PatchPrefix(BotSpawner __instance, BotCreationDataClass data)
{
var firstBotRole = data.Profiles[0].Info.Settings.Role;
if (firstBotRole is not (WildSpawnType.pmcBEAR or WildSpawnType.pmcUSEC))
{
2024-05-21 19:10:17 +01:00
ConsoleScreen.Log("[SPT PMC Bot spawn] Spawning a set of Scavs. Skipping...");
return true; // Do original method
}
var helper = new SptSpawnHelper();
var newSpawns = helper.SelectSpawnPoints(data.Count);
for (int i = 0; i < data.Count; i++)
{
2024-05-21 19:10:17 +01:00
ConsoleScreen.Log($"[SPT PMC Bot spawn] Trying to spawn bot {i}");
var currentSpawnData = data.Separate(1);
// Unset group settings
// TODO: Allow for PMC bot groups?
currentSpawnData.SpawnParams.ShallBeGroup = null;
var spawnPointDetails = newSpawns[i];
var currentZone = __instance.GetClosestZone(spawnPointDetails.Position, out float _);
// CorePointId of player spawns seems to always be 0. Bots will not activate properly if this ID is used
// TODO: Verify if CorePointId of 1 is acceptable in all cases
2024-05-21 19:10:17 +01:00
ConsoleScreen.Log($"[SPT PMC Bot spawn] spawn point chosen: {spawnPointDetails.Name} Core point id was: {spawnPointDetails.CorePointId}");
currentSpawnData.AddPosition(spawnPointDetails.Position, spawnPointDetails.CorePointId);
__instance.SpawnBotsInZoneOnPositions(newSpawns.GetRange(i, 1), currentZone, currentSpawnData);
}
2024-10-03 10:28:48 +01:00
return false; // Skip original
}
}
}