Thief beacons (try 2) (#29997)

content
This commit is contained in:
Ed
2024-08-08 16:17:50 +03:00
committed by GitHub
parent 6d6c836095
commit 03745efc7f
14 changed files with 264 additions and 24 deletions

View File

@@ -21,16 +21,15 @@ public sealed class StealConditionSystem : EntitySystem
[Dependency] private readonly MetaDataSystem _metaData = default!;
[Dependency] private readonly MobStateSystem _mobState = default!;
[Dependency] private readonly SharedObjectivesSystem _objectives = default!;
[Dependency] private readonly EntityLookupSystem _lookup = default!;
private EntityQuery<ContainerManagerComponent> _containerQuery;
private EntityQuery<MetaDataComponent> _metaQuery;
public override void Initialize()
{
base.Initialize();
_containerQuery = GetEntityQuery<ContainerManagerComponent>();
_metaQuery = GetEntityQuery<MetaDataComponent>();
SubscribeLocalEvent<StealConditionComponent, ObjectiveAssignedEvent>(OnAssigned);
SubscribeLocalEvent<StealConditionComponent, ObjectiveAfterAssignEvent>(OnAfterAssign);
@@ -96,25 +95,33 @@ public sealed class StealConditionSystem : EntitySystem
if (!_containerQuery.TryGetComponent(mind.OwnedEntity, out var currentManager))
return 0;
var stack = new Stack<ContainerManagerComponent>();
var containerStack = new Stack<ContainerManagerComponent>();
var count = 0;
//check stealAreas
if (condition.CheckStealAreas)
{
var areasQuery = AllEntityQuery<StealAreaComponent>();
while (areasQuery.MoveNext(out var uid, out var area))
{
if (!area.Owners.Contains(mind.Owner))
continue;
var nearestEnt = _lookup.GetEntitiesInRange(uid, area.Range);
foreach (var ent in nearestEnt)
{
CheckEntity(ent, condition, ref containerStack, ref count);
}
}
}
//check pulling object
if (TryComp<PullerComponent>(mind.OwnedEntity, out var pull)) //TO DO: to make the code prettier? don't like the repetition
{
var pulledEntity = pull.Pulling;
if (pulledEntity != null)
{
// check if this is the item
count += CheckStealTarget(pulledEntity.Value, condition);
//we don't check the inventories of sentient entity
if (!HasComp<MindContainerComponent>(pulledEntity))
{
// if it is a container check its contents
if (_containerQuery.TryGetComponent(pulledEntity, out var containerManager))
stack.Push(containerManager);
}
CheckEntity(pulledEntity.Value, condition, ref containerStack, ref count);
}
}
@@ -131,16 +138,30 @@ public sealed class StealConditionSystem : EntitySystem
// if it is a container check its contents
if (_containerQuery.TryGetComponent(entity, out var containerManager))
stack.Push(containerManager);
containerStack.Push(containerManager);
}
}
} while (stack.TryPop(out currentManager));
} while (containerStack.TryPop(out currentManager));
var result = count / (float) condition.CollectionSize;
result = Math.Clamp(result, 0, 1);
return result;
}
private void CheckEntity(EntityUid entity, StealConditionComponent condition, ref Stack<ContainerManagerComponent> containerStack, ref int counter)
{
// check if this is the item
counter += CheckStealTarget(entity, condition);
//we don't check the inventories of sentient entity
if (!TryComp<MindContainerComponent>(entity, out var pullMind))
{
// if it is a container check its contents
if (_containerQuery.TryGetComponent(entity, out var containerManager))
containerStack.Push(containerManager);
}
}
private int CheckStealTarget(EntityUid entity, StealConditionComponent condition)
{
// check if this is the target