2021-09-06 22:45:25 +00:00
|
|
|
|
using SanAndreasUnity.Importing.Paths;
|
2021-08-07 23:05:13 +00:00
|
|
|
|
using SanAndreasUnity.Importing.Items.Definitions;
|
|
|
|
|
using SanAndreasUnity.Net;
|
|
|
|
|
using SanAndreasUnity.Utilities;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2021-09-08 20:46:58 +00:00
|
|
|
|
using SanAndreasUnity.Behaviours.World;
|
|
|
|
|
using SanAndreasUnity.Behaviours.WorldSystem;
|
2021-08-07 23:05:13 +00:00
|
|
|
|
using UnityEngine;
|
2021-09-08 20:46:58 +00:00
|
|
|
|
using WorldSystemArea = SanAndreasUnity.Behaviours.WorldSystem.WorldSystem<SanAndreasUnity.Behaviours.PedAI>.Area;
|
2021-08-07 23:05:13 +00:00
|
|
|
|
|
|
|
|
|
namespace SanAndreasUnity.Behaviours
|
|
|
|
|
{
|
2021-09-07 00:13:54 +00:00
|
|
|
|
public class NPCPedSpawner : StartupSingleton<NPCPedSpawner>
|
2021-08-07 23:05:13 +00:00
|
|
|
|
{
|
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-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)>();
|
2021-09-09 01:34:43 +00:00
|
|
|
|
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;
|
|
|
|
|
|
2021-09-09 01:34:43 +00:00
|
|
|
|
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-08-07 23:05:13 +00:00
|
|
|
|
|
|
|
|
|
|
2021-09-06 21:33:12 +00:00
|
|
|
|
protected override void OnSingletonAwake()
|
2021-08-07 23:05:13 +00:00
|
|
|
|
{
|
2021-09-08 20:46:58 +00:00
|
|
|
|
_timeUntilNewSpawn = this.timeIntervalToAttemptSpawn;
|
2021-09-09 01:34:43 +00:00
|
|
|
|
_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-08-07 23:05:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-08 20:46:58 +00:00
|
|
|
|
void OnLoaderFinished()
|
2021-08-07 23:05:13 +00:00
|
|
|
|
{
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-09 01:34:43 +00:00
|
|
|
|
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();
|
|
|
|
|
|
2021-09-09 01:34:43 +00:00
|
|
|
|
this.UpdateDestroying();
|
|
|
|
|
|
|
|
|
|
this.UpdateSpawning();
|
|
|
|
|
}
|
2021-09-08 20:46:58 +00:00
|
|
|
|
|
2021-09-09 01:34:43 +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
|
|
|
|
|
2021-09-09 01:34:43 +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
|
|
|
|
}
|
|
|
|
|
|
2021-09-09 01:34:43 +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
|
|
|
|
|
2021-09-09 01:34:43 +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-08-07 23:05:13 +00:00
|
|
|
|
{
|
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-08-07 23:05:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-08 20:46:58 +00:00
|
|
|
|
private WorldSystemArea GetAreaWhereToSpawnPed()
|
2021-08-07 23:05:13 +00:00
|
|
|
|
{
|
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-08-07 23:05:13 +00:00
|
|
|
|
|
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-08-07 23:05:13 +00:00
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
int currentArea = NodeFile.GetAreaFromPosition(targetZone);
|
|
|
|
|
List<int> areaIdsToSearch = NodeFile.GetAreaNeighborhood(currentArea);
|
|
|
|
|
areaIdsToSearch.Add(currentArea);
|
|
|
|
|
areaIdsToSearch.RemoveAll(_ => _ < 0);
|
|
|
|
|
areaIdsToSearch = areaIdsToSearch.Distinct().ToList(); // just in case above functions don't work properly
|
|
|
|
|
|
|
|
|
|
if (areaIdsToSearch.Count == 0)
|
|
|
|
|
yield break;
|
|
|
|
|
|
|
|
|
|
// choose random node among all nodes that satisfy conditions
|
|
|
|
|
|
|
|
|
|
float randomValue = Random.Range(0f, 15f);
|
|
|
|
|
|
|
|
|
|
var pathNode = areaIdsToSearch
|
|
|
|
|
.Select(NodeReader.GetAreaById)
|
|
|
|
|
.SelectMany(_ => _.PathNodes
|
|
|
|
|
.Where(pn => pn.NodeType > 2 // ?
|
|
|
|
|
&& pn.Flags.SpawnProbability != 0
|
2021-09-08 22:05:30 +00:00
|
|
|
|
&& Vector3.Distance(pn.Position, targetZone) < areaRadius
|
|
|
|
|
&& (!hasFocusPointsThatSeeArea || worldSystemArea.FocusPointsThatSeeMe.All(f => Vector3.Distance(pn.Position, f.Position) > this.minSpawnDistanceFromFocusPoint))))
|
2021-09-08 20:46:58 +00:00
|
|
|
|
.RandomElementOrDefault();
|
|
|
|
|
|
|
|
|
|
if (EqualityComparer<PathNode>.Default.Equals(pathNode, default))
|
|
|
|
|
yield break;
|
2021-08-07 23:05:13 +00:00
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
|
|
ai.CurrentNode = pathNode;
|
|
|
|
|
ai.TargetNode = pathNode;
|
|
|
|
|
|
2021-09-09 01:34:43 +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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AddWeaponToPed(Ped ped)
|
|
|
|
|
{
|
|
|
|
|
if (null == ped.PedDef)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
Weapon weapon = null;
|
|
|
|
|
|
|
|
|
|
var defaultType = ped.PedDef.DefaultType;
|
|
|
|
|
|
|
|
|
|
if (defaultType == PedestrianType.Cop
|
|
|
|
|
|| defaultType == PedestrianType.Criminal)
|
|
|
|
|
weapon = ped.WeaponHolder.AddWeapon(WeaponId.Pistol);
|
|
|
|
|
else if (defaultType.IsGangMember())
|
|
|
|
|
weapon = ped.WeaponHolder.AddWeapon(WeaponId.MicroUzi);
|
|
|
|
|
|
|
|
|
|
if (weapon != null)
|
|
|
|
|
{
|
|
|
|
|
ped.WeaponHolder.SwitchWeapon(weapon.SlotIndex);
|
|
|
|
|
weapon.AddRandomAmmoAmount();
|
2021-08-07 23:05:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|