2021-06-27 05:40:03 +02:00
|
|
|
using System;
|
2020-06-18 22:52:44 +10:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Robust.Shared.GameObjects;
|
|
|
|
|
using Robust.Shared.IoC;
|
|
|
|
|
using Robust.Shared.Map;
|
|
|
|
|
|
|
|
|
|
namespace Content.Server.AI.Utils
|
|
|
|
|
{
|
|
|
|
|
public static class Visibility
|
|
|
|
|
{
|
|
|
|
|
// Should this be in robust or something? Fark it
|
2021-12-05 18:09:01 +01:00
|
|
|
public static IEnumerable<EntityUid> GetNearestEntities(EntityCoordinates grid, Type component, float range)
|
2020-06-18 22:52:44 +10:00
|
|
|
{
|
2021-12-08 17:04:21 +01:00
|
|
|
var entMan = IoCManager.Resolve<IEntityManager>();
|
2020-06-18 22:52:44 +10:00
|
|
|
var inRange = GetEntitiesInRange(grid, component, range).ToList();
|
2021-12-08 17:04:21 +01:00
|
|
|
var sortedInRange = inRange.OrderBy(o => (entMan.GetComponent<TransformComponent>(o).Coordinates.Position - grid.Position).Length);
|
2020-06-18 22:52:44 +10:00
|
|
|
|
|
|
|
|
return sortedInRange;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-05 18:09:01 +01:00
|
|
|
public static IEnumerable<EntityUid> GetEntitiesInRange(EntityCoordinates grid, Type component, float range)
|
2020-06-18 22:52:44 +10:00
|
|
|
{
|
|
|
|
|
var entityManager = IoCManager.Resolve<IEntityManager>();
|
2021-09-28 13:35:29 +02:00
|
|
|
foreach (var entity in entityManager.GetAllComponents(component).Select(c => c.Owner))
|
2020-06-18 22:52:44 +10:00
|
|
|
{
|
2021-12-08 17:04:21 +01:00
|
|
|
var transform = entityManager.GetComponent<TransformComponent>(entity);
|
|
|
|
|
|
|
|
|
|
if (transform.Coordinates.GetGridId(entityManager) != grid.GetGridId(entityManager))
|
2020-06-18 22:52:44 +10:00
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-08 17:04:21 +01:00
|
|
|
if ((transform.Coordinates.Position - grid.Position).Length <= range)
|
2020-06-18 22:52:44 +10:00
|
|
|
{
|
|
|
|
|
yield return entity;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|