HungerComponent.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using Content.Shared.Alert;
  2. using Content.Shared.Damage;
  3. using Content.Shared.Nutrition.EntitySystems;
  4. using Robust.Shared.GameStates;
  5. using Robust.Shared.Prototypes;
  6. using Robust.Shared.Serialization;
  7. using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom;
  8. using Robust.Shared.Serialization.TypeSerializers.Implementations.Generic;
  9. namespace Content.Shared.Nutrition.Components;
  10. [RegisterComponent, NetworkedComponent, Access(typeof(HungerSystem))]
  11. [AutoGenerateComponentState(fieldDeltas: true), AutoGenerateComponentPause]
  12. public sealed partial class HungerComponent : Component
  13. {
  14. /// <summary>
  15. /// The hunger value as authoritatively set by the server as of <see cref="LastAuthoritativeHungerChangeTime"/>.
  16. /// This value should be updated relatively infrequently. To get the current hunger, which changes with each update,
  17. /// use <see cref="HungerSystem.GetHunger"/>.
  18. /// </summary>
  19. [DataField, ViewVariables(VVAccess.ReadOnly)]
  20. [AutoNetworkedField]
  21. public float LastAuthoritativeHungerValue;
  22. /// <summary>
  23. /// The time at which <see cref="LastAuthoritativeHungerValue"/> was last updated.
  24. /// </summary>
  25. [DataField(customTypeSerializer: typeof(TimeOffsetSerializer)), ViewVariables(VVAccess.ReadWrite)]
  26. [AutoNetworkedField]
  27. public TimeSpan LastAuthoritativeHungerChangeTime;
  28. /// <summary>
  29. /// The base amount at which <see cref="LastAuthoritativeHungerValue"/> decays.
  30. /// </summary>
  31. /// <remarks>Any time this is modified, <see cref="HungerSystem.SetAuthoritativeHungerValue"/> should be called.</remarks>
  32. [DataField("baseDecayRate"), ViewVariables(VVAccess.ReadWrite)]
  33. public float BaseDecayRate = 0.05f;
  34. /// <summary>
  35. /// The actual amount at which <see cref="LastAuthoritativeHungerValue"/> decays.
  36. /// Affected by <seealso cref="CurrentThreshold"/>
  37. /// </summary>
  38. /// <remarks>Any time this is modified, <see cref="HungerSystem.SetAuthoritativeHungerValue"/> should be called.</remarks>
  39. [DataField("actualDecayRate"), ViewVariables(VVAccess.ReadWrite)]
  40. [AutoNetworkedField]
  41. public float ActualDecayRate;
  42. /// <summary>
  43. /// The last threshold this entity was at.
  44. /// Stored in order to prevent recalculating
  45. /// </summary>
  46. [DataField("lastThreshold"), ViewVariables(VVAccess.ReadWrite)]
  47. [AutoNetworkedField]
  48. public HungerThreshold LastThreshold;
  49. /// <summary>
  50. /// The current hunger threshold the entity is at
  51. /// </summary>
  52. /// <remarks>Any time this is modified, <see cref="HungerSystem.SetAuthoritativeHungerValue"/> should be called.</remarks>
  53. [DataField("currentThreshold"), ViewVariables(VVAccess.ReadWrite)]
  54. [AutoNetworkedField]
  55. public HungerThreshold CurrentThreshold;
  56. /// <summary>
  57. /// A dictionary relating HungerThreshold to the amount of <see cref="HungerSystem.GetHunger">current hunger</see> needed for each one
  58. /// </summary>
  59. [DataField("thresholds", customTypeSerializer: typeof(DictionarySerializer<HungerThreshold, float>))]
  60. [AutoNetworkedField]
  61. public Dictionary<HungerThreshold, float> Thresholds = new()
  62. {
  63. { HungerThreshold.Overfed, 200.0f },
  64. { HungerThreshold.Okay, 150.0f },
  65. { HungerThreshold.Peckish, 100.0f },
  66. { HungerThreshold.Starving, 50.0f },
  67. { HungerThreshold.Dead, 0.0f }
  68. };
  69. /// <summary>
  70. /// A dictionary relating hunger thresholds to corresponding alerts.
  71. /// </summary>
  72. [DataField("hungerThresholdAlerts")]
  73. [AutoNetworkedField]
  74. public Dictionary<HungerThreshold, ProtoId<AlertPrototype>> HungerThresholdAlerts = new()
  75. {
  76. { HungerThreshold.Peckish, "Peckish" },
  77. { HungerThreshold.Starving, "Starving" },
  78. { HungerThreshold.Dead, "Starving" }
  79. };
  80. [DataField]
  81. public ProtoId<AlertCategoryPrototype> HungerAlertCategory = "Hunger";
  82. /// <summary>
  83. /// A dictionary relating HungerThreshold to how much they modify <see cref="BaseDecayRate"/>.
  84. /// </summary>
  85. [DataField("hungerThresholdDecayModifiers", customTypeSerializer: typeof(DictionarySerializer<HungerThreshold, float>))]
  86. [AutoNetworkedField]
  87. public Dictionary<HungerThreshold, float> HungerThresholdDecayModifiers = new()
  88. {
  89. { HungerThreshold.Overfed, 1.2f },
  90. { HungerThreshold.Okay, 1f },
  91. { HungerThreshold.Peckish, 0.8f },
  92. { HungerThreshold.Starving, 0.6f },
  93. { HungerThreshold.Dead, 0.6f }
  94. };
  95. /// <summary>
  96. /// The amount of slowdown applied when an entity is starving
  97. /// </summary>
  98. [DataField("starvingSlowdownModifier"), ViewVariables(VVAccess.ReadWrite)]
  99. [AutoNetworkedField]
  100. public float StarvingSlowdownModifier = 0.75f;
  101. /// <summary>
  102. /// Damage dealt when your current threshold is at HungerThreshold.Dead
  103. /// </summary>
  104. [DataField("starvationDamage")]
  105. public DamageSpecifier? StarvationDamage;
  106. /// <summary>
  107. /// The time when the hunger threshold will update next.
  108. /// </summary>
  109. [DataField("nextUpdateTime", customTypeSerializer: typeof(TimeOffsetSerializer)), ViewVariables(VVAccess.ReadWrite)]
  110. [AutoNetworkedField]
  111. [AutoPausedField]
  112. public TimeSpan NextThresholdUpdateTime;
  113. /// <summary>
  114. /// The time between each hunger threshold update.
  115. /// </summary>
  116. [ViewVariables(VVAccess.ReadWrite)]
  117. [AutoNetworkedField]
  118. public TimeSpan ThresholdUpdateRate = TimeSpan.FromSeconds(1);
  119. }
  120. [Serializable, NetSerializable]
  121. public enum HungerThreshold : byte
  122. {
  123. Overfed = 1 << 3,
  124. Okay = 1 << 2,
  125. Peckish = 1 << 1,
  126. Starving = 1 << 0,
  127. Dead = 0,
  128. }