1
0

HelpProgressConditionSystem.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using Content.Server.Objectives.Components;
  2. using Content.Shared.Mind;
  3. using Content.Shared.Objectives.Components;
  4. using Content.Shared.Objectives.Systems;
  5. namespace Content.Server.Objectives.Systems;
  6. /// <summary>
  7. /// Handles help progress condition logic.
  8. /// </summary>
  9. public sealed class HelpProgressConditionSystem : EntitySystem
  10. {
  11. [Dependency] private readonly SharedObjectivesSystem _objectives = default!;
  12. [Dependency] private readonly TargetObjectiveSystem _target = default!;
  13. public override void Initialize()
  14. {
  15. base.Initialize();
  16. SubscribeLocalEvent<HelpProgressConditionComponent, ObjectiveGetProgressEvent>(OnGetProgress);
  17. }
  18. private void OnGetProgress(EntityUid uid, HelpProgressConditionComponent comp, ref ObjectiveGetProgressEvent args)
  19. {
  20. if (!_target.GetTarget(uid, out var target))
  21. return;
  22. args.Progress = GetProgress(target.Value);
  23. }
  24. private float GetProgress(EntityUid target)
  25. {
  26. var total = 0f; // how much progress they have
  27. var max = 0f; // how much progress is needed for 100%
  28. if (TryComp<MindComponent>(target, out var mind))
  29. {
  30. foreach (var objective in mind.Objectives)
  31. {
  32. // this has the potential to loop forever, anything setting target has to check that there is no HelpProgressCondition.
  33. var info = _objectives.GetInfo(objective, target, mind);
  34. if (info == null)
  35. continue;
  36. max++; // things can only be up to 100% complete yeah
  37. total += info.Value.Progress;
  38. }
  39. }
  40. // no objectives that can be helped with...
  41. if (max == 0f)
  42. return 1f;
  43. // require 50% completion for this one to be complete
  44. var completion = total / max;
  45. return completion >= 0.5f ? 1f : completion / 0.5f;
  46. }
  47. }