mirror of
https://github.com/space-syndicate/space-station-14.git
synced 2026-10-09 18:31:17 +03:00
* refactor: EntityTableContext methods now use strongly typed keys, which are bound to type of value you want to extract from context. * refactor: remove unused attributes * refactor: extracted EntityTableContext into separate file, fixed xml-doc
51 lines
1.6 KiB
C#
51 lines
1.6 KiB
C#
using Content.Shared.EntityTable.EntitySelectors;
|
|
using Content.Shared.GameTicking.Rules.Components;
|
|
using Robust.Shared.Prototypes;
|
|
|
|
namespace Content.Shared.EntityTable.Conditions;
|
|
|
|
/// <summary>
|
|
/// Condition that only succeeds if a table supplies a sufficient "cost" to a given
|
|
/// </summary>
|
|
public sealed partial class HasBudgetCondition : EntityTableCondition
|
|
{
|
|
public static readonly EntityTableContextKey<float> BudgetContextKey = new("Budget");
|
|
|
|
/// <summary>
|
|
/// Used for determining the cost for the budget.
|
|
/// If null, attempts to fetch the cost from the attached selector.
|
|
/// </summary>
|
|
[DataField]
|
|
public int? CostOverride;
|
|
|
|
protected override bool EvaluateImplementation(EntityTableSelector root,
|
|
IEntityManager entMan,
|
|
IPrototypeManager proto,
|
|
EntityTableContext ctx)
|
|
{
|
|
if (!ctx.TryGetData<float>(BudgetContextKey, out var budget))
|
|
return false;
|
|
|
|
int cost;
|
|
if (CostOverride != null)
|
|
{
|
|
cost = CostOverride.Value;
|
|
}
|
|
else
|
|
{
|
|
if (root is not EntSelector entSelector)
|
|
return false;
|
|
|
|
if (!proto.Index(entSelector.Id).TryComp(out DynamicRuleCostComponent? costComponent, entMan.ComponentFactory))
|
|
{
|
|
var log = Logger.GetSawmill("HasBudgetCondition");
|
|
log.Error($"Rule {entSelector.Id} does not have a DynamicRuleCostComponent.");
|
|
return false;
|
|
}
|
|
|
|
cost = costComponent.Cost;
|
|
}
|
|
|
|
return budget >= cost;
|
|
}
|
|
}
|