Skip to content

Commit

Permalink
Fixing reverting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
desuwatch committed Jan 18, 2025
1 parent 5fe6144 commit ee7e119
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 10 deletions.
45 changes: 37 additions & 8 deletions Content.Shared/Nutrition/EntitySystems/HungerSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ private void OnRejuvenate(EntityUid uid, HungerComponent component, RejuvenateEv
SetHunger(uid, component.Thresholds[HungerThreshold.Okay], component);
}

/// <summary>
/// Gets the current hunger value of the given <see cref="HungerComponent"/>.
/// </summary>
public float GetHunger(HungerComponent component)
{
var dt = _timing.CurTime - component.LastAuthoritativeHungerChangeTime;
var value = component.LastAuthoritativeHungerValue - (float)dt.TotalSeconds * component.ActualDecayRate;
return ClampHungerWithinThresholds(component, value);
}

/// <summary>
/// Adds to the current hunger of an entity by the specified value
/// </summary>
Expand All @@ -82,7 +92,7 @@ public void ModifyHunger(EntityUid uid, float amount, HungerComponent? component
{
if (!Resolve(uid, ref component))
return;
SetHunger(uid, component.CurrentHunger + amount, component);
SetHunger(uid, GetHunger(component) + amount, component);
}

/// <summary>
Expand All @@ -95,11 +105,23 @@ public void SetHunger(EntityUid uid, float amount, HungerComponent? component =
{
if (!Resolve(uid, ref component))
return;
component.CurrentHunger = Math.Clamp(amount,
component.Thresholds[HungerThreshold.Dead],
component.Thresholds[HungerThreshold.Overfed]);

SetAuthoritativeHungerValue((uid, component), amount);
UpdateCurrentThreshold(uid, component);
Dirty(uid, component);
}

/// <summary>
/// Sets <see cref="HungerComponent.LastAuthoritativeHungerValue"/> and
/// <see cref="HungerComponent.LastAuthoritativeHungerChangeTime"/>, and dirties this entity. This "resets" the
/// starting point for <see cref="GetHunger"/>'s calculation.
/// </summary>
/// <param name="entity">The entity whose hunger will be set.</param>
/// <param name="value">The value to set the entity's hunger to.</param>
private void SetAuthoritativeHungerValue(Entity<HungerComponent> entity, float value)
{
entity.Comp.LastAuthoritativeHungerChangeTime = _timing.CurTime;
entity.Comp.LastAuthoritativeHungerValue = ClampHungerWithinThresholds(entity.Comp, value);
DirtyField(entity.Owner, entity.Comp, nameof(HungerComponent.LastAuthoritativeHungerChangeTime));
}

private void UpdateCurrentThreshold(EntityUid uid, HungerComponent? component = null)
Expand Down Expand Up @@ -167,7 +189,7 @@ component.StarvationDamage is { } damage &&
/// <returns></returns>
public HungerThreshold GetHungerThreshold(HungerComponent component, float? food = null)
{
food ??= component.CurrentHunger;
food ??= GetHunger(component);
var result = HungerThreshold.Dead;
var value = component.Thresholds[HungerThreshold.Overfed];
foreach (var threshold in component.Thresholds)
Expand Down Expand Up @@ -229,16 +251,23 @@ public bool TryGetStatusIconPrototype(HungerComponent component, [NotNullWhen(tr
return prototype != null;
}

private static float ClampHungerWithinThresholds(HungerComponent component, float hungerValue)
{
return Math.Clamp(hungerValue,
component.Thresholds[HungerThreshold.Dead],
component.Thresholds[HungerThreshold.Overfed]);
}

public override void Update(float frameTime)
{
base.Update(frameTime);

var query = EntityQueryEnumerator<HungerComponent>();
while (query.MoveNext(out var uid, out var hunger))
{
if (_timing.CurTime < hunger.NextUpdateTime)
if (_timing.CurTime < hunger.NextThresholdUpdateTime)
continue;
hunger.NextUpdateTime = _timing.CurTime + hunger.UpdateRate;
hunger.NextThresholdUpdateTime = _timing.CurTime + hunger.NextThresholdUpdateTime;

ModifyHunger(uid, -hunger.ActualDecayRate, hunger);
DoContinuousHungerEffects(uid, hunger);
Expand Down
30 changes: 30 additions & 0 deletions Content.Shared/_CorvaxNext/NextVars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,39 @@ namespace Content.Shared._CorvaxNext.NextVars;
// ReSharper disable once InconsistentNaming
public sealed class NextVars
{
/**
* Auto cryo sleep
*/

public static readonly CVarDef<bool> AutoCryoSleepEnabled =
CVarDef.Create("auto_cryo_sleep.enabled", true, CVar.SERVER);

public static readonly CVarDef<int> AutoCryoSleepTime =
CVarDef.Create("auto_cryo_sleep.time", 1200, CVar.SERVER);

public static readonly CVarDef<int> AutoCryoSleepUpdateTime =
CVarDef.Create("auto_cryo_sleep.update_time", 120, CVar.SERVER);

/// <summary>
/// Offer item.
/// </summary>
public static readonly CVarDef<bool> OfferModeIndicatorsPointShow =
CVarDef.Create("hud.offer_mode_indicators_point_show", true, CVar.ARCHIVE | CVar.CLIENTONLY);

/*
* _CorvaxNext Bind Standing and Laying System
*/

public static readonly CVarDef<bool> AutoGetUp =
CVarDef.Create("laying.auto_get_up", true, CVar.CLIENT | CVar.ARCHIVE | CVar.REPLICATED);

/// <summary>
/// When true, entities that fall to the ground will be able to crawl under tables and
/// plastic flaps, allowing them to take cover from gunshots.
/// </summary>
public static readonly CVarDef<bool> CrawlUnderTables =
CVarDef.Create("laying.crawlundertables", false, CVar.REPLICATED);

// public static readonly CVarDef<bool> OfferModeIndicatorsPointShow =
// CVarDef.Create("hud.offer_mode_indicators_point_show", true, CVar.ARCHIVE | CVar.CLIENTONLY);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using Content.Shared._CorvaxNext.NextVars;
using Content.Shared._CorvaxNext.Targeting;
using Content.Shared.Body.Components;
using Content.Shared.Buckle;
using Content.Shared.Buckle.Components;
Expand Down Expand Up @@ -322,7 +321,7 @@ standingState.CurrentState is not StandingState.Lying ||
obj.Value,
uid,
PopupType.MediumCaution);
_damageable.TryChangeDamage(uid, new DamageSpecifier() { DamageDict = { { "Blunt", 5 } } }, ignoreResistances: true, canEvade: true, targetPart: TargetBodyPart.Head);
_damageable.TryChangeDamage(uid, new DamageSpecifier() { DamageDict = { { "Blunt", 5 } } }, ignoreResistances: true);
_stun.TryStun(uid, TimeSpan.FromSeconds(2), true);
_audioSystem.PlayPredicted(_bonkSound, uid, obj.Value);
return false;
Expand Down

0 comments on commit ee7e119

Please sign in to comment.