SanAndreasUnity/Assets/Scripts/Behaviours/NPCPedSpawner.cs

307 lines
11 KiB
C#
Raw Normal View History

2021-09-06 22:45:25 +00:00
using SanAndreasUnity.Importing.Paths;
using SanAndreasUnity.Importing.Items.Definitions;
using SanAndreasUnity.Net;
using SanAndreasUnity.Utilities;
using System.Collections.Generic;
using System.Linq;
2021-09-19 18:09:11 +00:00
using SanAndreasUnity.Behaviours.Peds.AI;
2021-09-08 20:46:58 +00:00
using SanAndreasUnity.Behaviours.World;
using SanAndreasUnity.Behaviours.WorldSystem;
using UnityEngine;
using Random = UnityEngine.Random;
2021-09-19 18:09:11 +00:00
using WorldSystemArea = SanAndreasUnity.Behaviours.WorldSystem.WorldSystem<SanAndreasUnity.Behaviours.Peds.AI.PedAI>.Area;
namespace SanAndreasUnity.Behaviours
{
2021-09-07 00:13:54 +00:00
public class NPCPedSpawner : StartupSingleton<NPCPedSpawner>
{
2021-09-08 20:46:58 +00:00
public int totalMaxNumSpawnedPeds = 40;
public int maxNumSpawnedPedsPerPlayer = 10;
private WorldSystem<PedAI> _worldSystem;
[SerializeField] private int _areaSize = 200;
[SerializeField] private int _revealRadius = 100;
public float timeToKeepRevealingAfterRemoved = 3f;
2021-09-08 22:05:30 +00:00
public float minSpawnDistanceFromFocusPoint = 40;
2021-09-09 02:31:18 +00:00
public float maxSpawnDistanceFromFocusPoint = 100;
2021-09-08 22:05:30 +00:00
2021-09-08 20:46:58 +00:00
private readonly List<WorldSystemArea> _visibleAreas = new List<WorldSystemArea>(32);
private readonly List<(PedAI pedAI, WorldSystemArea area)> _spawnedPeds = new List<(PedAI, WorldSystemArea)>();
private readonly List<PedAI> _pedsToDestroy = new List<PedAI>();
2021-09-08 20:46:58 +00:00
private float _timeUntilNewSpawn = 0f;
public float timeIntervalToAttemptSpawn = 0.33f;
private float _timeUntilNewDestroy = 0f;
public float timeIntervalToAttemptDestroy = 0.65f;
2021-09-08 20:46:58 +00:00
public FocusPointManager<PedAI> FocusPointManager { get; private set; }
2021-09-06 21:33:12 +00:00
protected override void OnSingletonAwake()
{
2021-09-08 20:46:58 +00:00
_timeUntilNewSpawn = this.timeIntervalToAttemptSpawn;
_timeUntilNewDestroy = this.timeIntervalToAttemptDestroy;
2021-09-06 21:33:12 +00:00
Ped.onStart += PedOnStart;
}
private void PedOnStart(Ped ped)
{
if (!NetStatus.IsServer)
return;
2021-09-08 20:46:58 +00:00
if (this.FocusPointManager != null && ped.PlayerOwner != null)
{
this.FocusPointManager.RegisterFocusPoint(
ped.transform,
new FocusPointParameters(false, 0f, this.timeToKeepRevealingAfterRemoved));
}
}
2021-09-08 20:46:58 +00:00
void OnLoaderFinished()
{
2021-09-08 20:46:58 +00:00
if (!NetStatus.IsServer)
return;
int worldSize = Cell.Instance != null ? Cell.Instance.WorldSize : Cell.DefaultWorldSize;
int numAreasPerAxis = Mathf.CeilToInt(worldSize / (float)_areaSize);
_worldSystem = new WorldSystem<PedAI>(
new WorldSystemParams { worldSize = (uint) worldSize, numAreasPerAxis = (ushort) numAreasPerAxis },
new WorldSystemParams { worldSize = 50000, numAreasPerAxis = 1 },
OnAreaChangedVisibility);
this.FocusPointManager = new FocusPointManager<PedAI>(_worldSystem, _revealRadius);
}
private void OnAreaChangedVisibility(WorldSystem<PedAI>.Area area, bool isVisible)
{
if (isVisible)
_visibleAreas.Add(area);
else
_visibleAreas.Remove(area);
}
void OnPedChangedArea(PedAI pedAI, AreaIndex oldAreaIndex, AreaIndex newAreaIndex)
{
var newArea = _worldSystem.GetAreaAt(newAreaIndex);
if (null == newArea || !newArea.WasVisibleInLastUpdate) // new area not visible
_pedsToDestroy.AddIfNotPresent(pedAI);
}
2021-09-08 20:46:58 +00:00
void UpdateAreas()
{
if (null == _worldSystem)
return;
this.FocusPointManager.Update();
_worldSystem.Update();
this.UpdateDestroying();
this.UpdateSpawning();
}
2021-09-08 20:46:58 +00:00
void UpdateDestroying()
{
_timeUntilNewDestroy -= Time.deltaTime;
if (_timeUntilNewDestroy > 0)
return;
_timeUntilNewDestroy = this.timeIntervalToAttemptDestroy;
// destroy only 1 ped per attempt
2021-09-08 20:46:58 +00:00
// check if there are registered peds to destroy
_pedsToDestroy.RemoveDeadObjects();
if (_pedsToDestroy.Count > 0)
{
Destroy(_pedsToDestroy[0].MyPed.gameObject);
_pedsToDestroy.RemoveAt(0);
return;
2021-09-08 20:46:58 +00:00
}
// check if any of spawned peds is in invisible area - can happen if area became invisible
2021-09-08 20:46:58 +00:00
_spawnedPeds.RemoveAll(_ => null == _.pedAI);
int index = _spawnedPeds.FindIndex(_ =>
{
var area = _worldSystem.GetAreaAt(_.pedAI.MyPed.transform.position);
return null == area || !area.WasVisibleInLastUpdate;
});
if (index >= 0)
{
Destroy(_spawnedPeds[index].pedAI.MyPed.gameObject);
_spawnedPeds.RemoveAt(index);
}
}
void UpdateSpawning()
{
2021-09-08 20:46:58 +00:00
// check if we should spawn new peds
_timeUntilNewSpawn -= Time.deltaTime;
if (_timeUntilNewSpawn <= 0)
{
_timeUntilNewSpawn = this.timeIntervalToAttemptSpawn;
_spawnedPeds.RemoveAll(_ => null == _.pedAI);
if (!this.IsOverLimit())
{
2021-09-08 20:46:58 +00:00
// we should spawn new ped
// get area where to spawn
var area = this.GetAreaWhereToSpawnPed();
if (area != null)
this.StartCoroutine(this.SpawnPedCoroutine(area));
}
}
}
2021-09-08 20:46:58 +00:00
private WorldSystemArea GetAreaWhereToSpawnPed()
{
2021-09-08 20:46:58 +00:00
// we need to choose random visible area, because otherwise it will always try
// to spawn in the same area (assuming focus points don't change position)
if (_visibleAreas.Count == 0)
return null;
// maybe prefer areas which have less spawned peds inside ?
// but how to find those areas efficiently ?
2021-09-08 20:46:58 +00:00
return _visibleAreas.RandomElement();
/*for (int i = 0; i < _visibleAreas.Count; i++)
2021-09-06 21:46:57 +00:00
{
2021-09-08 20:46:58 +00:00
var area = _visibleAreas[i];
}*/
}
public bool IsOverLimit()
{
int numPlayers = Player.AllPlayersList.Count;
2021-09-08 20:46:58 +00:00
return _spawnedPeds.Count >= this.totalMaxNumSpawnedPeds || _spawnedPeds.Count >= numPlayers * this.maxNumSpawnedPedsPerPlayer;
}
private void Update()
{
if (NetStatus.IsServer)
{
this.UpdateAreas();
}
}
private System.Collections.IEnumerator SpawnPedCoroutine(WorldSystemArea worldSystemArea)
{
Vector3 worldSystemAreaCenter = _worldSystem.GetAreaCenter(worldSystemArea);
Vector3 targetZone = worldSystemAreaCenter;
float areaRadius = _areaSize * Mathf.Sqrt(2) / 2f; // radius of outer circle
2021-09-08 22:05:30 +00:00
bool hasFocusPointsThatSeeArea = worldSystemArea.FocusPointsThatSeeMe != null && worldSystemArea.FocusPointsThatSeeMe.Count > 0;
2021-09-08 20:46:58 +00:00
2021-09-12 17:53:27 +00:00
List<NodeFile> areasToSearch = NodeReader.GetAreasInRadius(targetZone, areaRadius)
.ToList();
2021-09-08 20:46:58 +00:00
2021-09-12 17:53:27 +00:00
if (areasToSearch.Count == 0)
2021-09-08 20:46:58 +00:00
yield break;
// choose random node among all nodes that satisfy conditions
float randomValue = Random.Range(0f, 15f);
2021-09-12 17:53:27 +00:00
var pathNode = areasToSearch
2021-09-10 23:12:39 +00:00
.SelectMany(_ => _.PedNodes
.Where(pn => pn.ShouldPedBeSpawnedHere
2021-09-08 20:46:58 +00:00
&& pn.Flags.SpawnProbability != 0
2021-09-08 22:05:30 +00:00
&& Vector3.Distance(pn.Position, targetZone) < areaRadius
2021-09-09 02:31:18 +00:00
&& (!hasFocusPointsThatSeeArea || worldSystemArea.FocusPointsThatSeeMe.All(f => Vector3.Distance(pn.Position, f.Position).BetweenExclusive(this.minSpawnDistanceFromFocusPoint, this.maxSpawnDistanceFromFocusPoint)))))
2021-09-08 20:46:58 +00:00
.RandomElementOrDefault();
if (EqualityComparer<PathNode>.Default.Equals(pathNode, default))
yield break;
2021-09-08 20:46:58 +00:00
var newPed = this.SpawnPed(worldSystemArea, pathNode);
// TODO: initialize PedDefinition right after ped is created, so we don't have to wait 1 frame until it is available
yield return null;
2021-09-06 21:46:57 +00:00
yield return null;
2021-09-06 21:33:12 +00:00
2021-09-08 20:46:58 +00:00
this.AddWeaponToPed(newPed.MyPed);
}
2021-09-06 21:33:12 +00:00
2021-09-08 20:46:58 +00:00
private PedAI SpawnPed(WorldSystemArea worldSystemArea, PathNode pathNode)
{
Vector3 spawnPos = new Vector3(pathNode.Position.x, pathNode.Position.y, pathNode.Position.z);
2021-09-06 23:33:19 +00:00
2021-09-08 20:46:58 +00:00
Ped newPed = Ped.SpawnPed(Ped.RandomPedId, spawnPos + new Vector3(0, 1, 0), Quaternion.identity, true);
2021-09-06 21:46:57 +00:00
2021-09-08 20:46:58 +00:00
var ai = newPed.gameObject.GetOrAddComponent<PedAI>();
_spawnedPeds.Add((ai, worldSystemArea));
2021-09-11 17:15:50 +00:00
ai.StartWalkingAround(pathNode);
2021-09-08 20:46:58 +00:00
var areaChangeDetector = newPed.gameObject.AddComponent<AreaChangeDetector>();
areaChangeDetector.Init(_worldSystem);
areaChangeDetector.onAreaChanged += (oldIndex, newIndex) => OnPedChangedArea(ai, oldIndex, newIndex);
2021-09-08 20:46:58 +00:00
return ai;
}
public void AddWeaponToPed(Ped ped)
2021-09-08 20:46:58 +00:00
{
if (null == ped.PedDef)
return;
Weapon weapon = null;
var defaultType = ped.PedDef.DefaultType;
2021-09-12 23:04:54 +00:00
if (defaultType.IsCop())
2021-09-08 20:46:58 +00:00
weapon = ped.WeaponHolder.AddWeapon(WeaponId.Pistol);
2021-09-12 23:04:54 +00:00
else if (defaultType.IsCriminal())
weapon = ped.WeaponHolder.AddWeapon(new int[]{WeaponId.Pistol, WeaponId.DesertEagle}.RandomElement());
2021-09-08 20:46:58 +00:00
else if (defaultType.IsGangMember())
2021-09-12 23:04:54 +00:00
weapon = ped.WeaponHolder.AddWeapon(new int[]{WeaponId.MicroUzi, WeaponId.Tec9}.RandomElement());
2021-09-08 20:46:58 +00:00
if (weapon != null)
{
ped.WeaponHolder.SwitchWeapon(weapon.SlotIndex);
weapon.AddRandomAmmoAmount();
}
}
private void OnDrawGizmosSelected()
{
if (null == Ped.Instance)
return;
float radius = 200f;
Vector3 center = Ped.Instance.transform.position;
var pathNodes = NodeReader.GetAreasInRadius(center, radius)
.SelectMany(_ => _.PedNodes)
.Where(pn => pn.ShouldPedBeSpawnedHere
&& pn.Flags.SpawnProbability != 0
&& Vector3.Distance(pn.Position, center) < radius);
Gizmos.color = Color.yellow;
foreach (PathNode pathNode in pathNodes)
{
Gizmos.DrawWireSphere(pathNode.Position, pathNode.PathWidth / 2f);
foreach (PathNode linkedNode in NodeReader.GetAllLinkedNodes(pathNode))
Gizmos.DrawLine(pathNode.Position, linkedNode.Position);
}
}
}
}