2024-07-24 13:28:27 +03:00
|
|
|
using Content.Server._CP14.Objectives.Components;
|
2024-10-26 18:18:30 +03:00
|
|
|
using Content.Server.Objectives.Components;
|
2024-07-24 13:28:27 +03:00
|
|
|
using Content.Shared._CP14.Currency;
|
2024-10-26 18:18:30 +03:00
|
|
|
using Content.Shared.Interaction;
|
2024-07-24 13:28:27 +03:00
|
|
|
using Content.Shared.Mind;
|
|
|
|
|
using Content.Shared.Mind.Components;
|
|
|
|
|
using Content.Shared.Movement.Pulling.Components;
|
|
|
|
|
using Content.Shared.Objectives.Components;
|
|
|
|
|
using Content.Shared.Objectives.Systems;
|
|
|
|
|
using Robust.Shared.Containers;
|
|
|
|
|
|
|
|
|
|
namespace Content.Server._CP14.Objectives.Systems;
|
|
|
|
|
|
|
|
|
|
public sealed class CP14CurrencyCollectConditionSystem : EntitySystem
|
|
|
|
|
{
|
|
|
|
|
[Dependency] private readonly MetaDataSystem _metaData = default!;
|
|
|
|
|
[Dependency] private readonly SharedObjectivesSystem _objectives = default!;
|
2024-10-15 15:22:06 +03:00
|
|
|
[Dependency] private readonly CP14SharedCurrencySystem _currency = default!;
|
2024-10-26 18:18:30 +03:00
|
|
|
[Dependency] private readonly EntityLookupSystem _lookup = default!;
|
|
|
|
|
[Dependency] private readonly SharedInteractionSystem _interaction = default!;
|
2024-07-24 13:28:27 +03:00
|
|
|
|
|
|
|
|
private EntityQuery<ContainerManagerComponent> _containerQuery;
|
|
|
|
|
|
2024-10-26 18:18:30 +03:00
|
|
|
private HashSet<Entity<TransformComponent>> _nearestEnts = new();
|
|
|
|
|
|
2024-07-24 13:28:27 +03:00
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
|
|
|
|
base.Initialize();
|
|
|
|
|
|
|
|
|
|
_containerQuery = GetEntityQuery<ContainerManagerComponent>();
|
|
|
|
|
|
2024-10-26 18:18:30 +03:00
|
|
|
SubscribeLocalEvent<CP14CurrencyCollectConditionComponent, ObjectiveAfterAssignEvent>(OnCollectAfterAssign);
|
|
|
|
|
SubscribeLocalEvent<CP14CurrencyStoredConditionComponent, ObjectiveAfterAssignEvent>(OnStoredAfterAssign);
|
|
|
|
|
|
|
|
|
|
SubscribeLocalEvent<CP14CurrencyCollectConditionComponent, ObjectiveGetProgressEvent>(OnCollectGetProgress);
|
|
|
|
|
SubscribeLocalEvent<CP14CurrencyStoredConditionComponent, ObjectiveGetProgressEvent>(OnStoredGetProgress);
|
2024-07-24 13:28:27 +03:00
|
|
|
}
|
|
|
|
|
|
2024-10-26 18:18:30 +03:00
|
|
|
private void OnCollectAfterAssign(Entity<CP14CurrencyCollectConditionComponent> condition, ref ObjectiveAfterAssignEvent args)
|
2024-07-24 13:28:27 +03:00
|
|
|
{
|
2024-11-04 13:41:09 +03:00
|
|
|
_metaData.SetEntityName(condition.Owner, Loc.GetString(condition.Comp.ObjectiveText, ("coins", _currency.GetCurrencyPrettyString(condition.Comp.Currency))), args.Meta);
|
2024-10-26 18:18:30 +03:00
|
|
|
_metaData.SetEntityDescription(condition.Owner, Loc.GetString(condition.Comp.ObjectiveDescription, ("coins", _currency.GetCurrencyPrettyString(condition.Comp.Currency))), args.Meta);
|
|
|
|
|
_objectives.SetIcon(condition.Owner, condition.Comp.ObjectiveSprite);
|
2024-07-24 13:28:27 +03:00
|
|
|
}
|
|
|
|
|
|
2024-10-26 18:18:30 +03:00
|
|
|
private void OnStoredAfterAssign(Entity<CP14CurrencyStoredConditionComponent> condition, ref ObjectiveAfterAssignEvent args)
|
2024-07-24 13:28:27 +03:00
|
|
|
{
|
2024-11-04 13:41:09 +03:00
|
|
|
_metaData.SetEntityName(condition.Owner, Loc.GetString(condition.Comp.ObjectiveText, ("coins", _currency.GetCurrencyPrettyString(condition.Comp.Currency))), args.Meta);
|
2024-10-15 15:22:06 +03:00
|
|
|
_metaData.SetEntityDescription(condition.Owner, Loc.GetString(condition.Comp.ObjectiveDescription, ("coins", _currency.GetCurrencyPrettyString(condition.Comp.Currency))), args.Meta);
|
2024-07-24 13:28:27 +03:00
|
|
|
_objectives.SetIcon(condition.Owner, condition.Comp.ObjectiveSprite);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-26 18:18:30 +03:00
|
|
|
private void OnCollectGetProgress(Entity<CP14CurrencyCollectConditionComponent> condition, ref ObjectiveGetProgressEvent args)
|
2024-07-24 13:28:27 +03:00
|
|
|
{
|
|
|
|
|
args.Progress = GetProgress(args.Mind, condition);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-26 18:18:30 +03:00
|
|
|
private void OnStoredGetProgress(Entity<CP14CurrencyStoredConditionComponent> condition, ref ObjectiveGetProgressEvent args)
|
|
|
|
|
{
|
|
|
|
|
args.Progress = GetStoredProgress(args.Mind, condition);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-24 13:28:27 +03:00
|
|
|
private float GetProgress(MindComponent mind, CP14CurrencyCollectConditionComponent condition)
|
|
|
|
|
{
|
|
|
|
|
if (!_containerQuery.TryGetComponent(mind.OwnedEntity, out var currentManager))
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
var containerStack = new Stack<ContainerManagerComponent>();
|
|
|
|
|
var count = 0;
|
|
|
|
|
|
|
|
|
|
//check pulling object
|
2024-10-26 18:18:30 +03:00
|
|
|
if (TryComp<PullerComponent>(mind.OwnedEntity,
|
|
|
|
|
out var pull)) //TO DO: to make the code prettier? don't like the repetition
|
2024-07-24 13:28:27 +03:00
|
|
|
{
|
|
|
|
|
var pulledEntity = pull.Pulling;
|
|
|
|
|
if (pulledEntity != null)
|
|
|
|
|
{
|
2024-10-26 18:18:30 +03:00
|
|
|
CheckEntity(pulledEntity.Value, ref containerStack, ref count);
|
2024-07-24 13:28:27 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// recursively check each container for the item
|
|
|
|
|
// checks inventory, bag, implants, etc.
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
foreach (var container in currentManager.Containers.Values)
|
|
|
|
|
{
|
|
|
|
|
foreach (var entity in container.ContainedEntities)
|
|
|
|
|
{
|
|
|
|
|
// check if this is the item
|
2024-10-15 15:22:06 +03:00
|
|
|
count += _currency.GetTotalCurrency(entity);
|
2024-07-24 13:28:27 +03:00
|
|
|
|
|
|
|
|
// if it is a container check its contents
|
|
|
|
|
if (_containerQuery.TryGetComponent(entity, out var containerManager))
|
|
|
|
|
containerStack.Push(containerManager);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} while (containerStack.TryPop(out currentManager));
|
|
|
|
|
|
2024-10-26 18:18:30 +03:00
|
|
|
var result = count / (float)condition.Currency;
|
|
|
|
|
result = Math.Clamp(result, 0, 1);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private float GetStoredProgress(MindComponent mind, CP14CurrencyStoredConditionComponent condition)
|
|
|
|
|
{
|
|
|
|
|
var containerStack = new Stack<ContainerManagerComponent>();
|
|
|
|
|
var count = 0;
|
|
|
|
|
|
|
|
|
|
var areasQuery = AllEntityQuery<StealAreaComponent, TransformComponent>();
|
|
|
|
|
while (areasQuery.MoveNext(out var uid, out var area, out var xform))
|
|
|
|
|
{
|
|
|
|
|
if (!area.Owners.Contains(mind.Owner))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
_nearestEnts.Clear();
|
|
|
|
|
_lookup.GetEntitiesInRange(xform.Coordinates, area.Range, _nearestEnts);
|
|
|
|
|
foreach (var ent in _nearestEnts)
|
|
|
|
|
{
|
|
|
|
|
if (!_interaction.InRangeUnobstructed((uid, xform), (ent, ent.Comp), area.Range))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
CheckEntity(ent, ref containerStack, ref count);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result = count / (float)condition.Currency;
|
2024-07-24 13:28:27 +03:00
|
|
|
result = Math.Clamp(result, 0, 1);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-26 18:18:30 +03:00
|
|
|
private void CheckEntity(EntityUid entity, ref Stack<ContainerManagerComponent> containerStack, ref int counter)
|
2024-07-24 13:28:27 +03:00
|
|
|
{
|
|
|
|
|
// check if this is the item
|
2024-10-15 15:22:06 +03:00
|
|
|
counter += _currency.GetTotalCurrency(entity);
|
2024-07-24 13:28:27 +03:00
|
|
|
|
|
|
|
|
//we don't check the inventories of sentient entity
|
|
|
|
|
if (!TryComp<MindContainerComponent>(entity, out _))
|
|
|
|
|
{
|
|
|
|
|
// if it is a container check its contents
|
|
|
|
|
if (_containerQuery.TryGetComponent(entity, out var containerManager))
|
|
|
|
|
containerStack.Push(containerManager);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|