Skip to content

Commit

Permalink
Merge pull request #212 from Darkmajia/puddle-viscosity-again
Browse files Browse the repository at this point in the history
robust puddle viscosity (again)
  • Loading branch information
formlessnameless authored Sep 5, 2024
2 parents 9f741f6 + bb5cdd2 commit 16370bf
Showing 1 changed file with 27 additions and 32 deletions.
59 changes: 27 additions & 32 deletions Content.Server/Fluids/EntitySystems/PuddleSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ private void OnSolutionUpdate(Entity<PuddleComponent> entity, ref SolutionContai

_deletionQueue.Remove(entity);
UpdateSlip(entity, entity.Comp, args.Solution);
UpdateSlow(entity, args.Solution);
UpdateSlow(entity, entity.Comp, args.Solution);
UpdateEvaporation(entity, args.Solution);
UpdateAppearance(entity, entity.Comp);
}
Expand Down Expand Up @@ -421,41 +421,36 @@ private void UpdateSlip(EntityUid entityUid, PuddleComponent component, Solution
}
}

private void UpdateSlow(EntityUid uid, Solution solution)
private void UpdateSlow(EntityUid uid, PuddleComponent component, Solution solution)
{
/// declare a maximum viscosity variable
var maxViscosity = 0f;
/// declare a minimum viscosity variable
var minViscosity = 0f;
/// declare a total viscosity variable
var totalViscosity = 0f;

/// for each reagent in the puddle, grab the reagent's information in the reagentProto variable.
var totalViscosity = 0f;
var totalQuantity = 0f;
var fullPuddleAmount = FixedPoint2.New(component.OverflowVolume.Float() * LowThreshold);

foreach (var (reagent, _) in solution.Contents)
{
var reagentQuant = solution.GetReagentQuantity(reagent).Float();
var reagentProto = _prototypeManager.Index<ReagentPrototype>(reagent.Prototype);
/// set maximum viscosity to whichever is higher (zero on the left, the reagent's viscosity on the right)
maxViscosity = Math.Max(maxViscosity, reagentProto.Viscosity);
/// do the opposite to minimum viscosity.
minViscosity = Math.Min(minViscosity, reagentProto.Viscosity);
}

/// total viscosity is the average of all viscosities in the solution
totalViscosity = maxViscosity + minViscosity;

/// if totalviscosity is not zero, ensure that the uid has the speedmodifiercontactcomponent, and then modify its speed by the total viscosity
if (totalViscosity != 0)
{
var comp = EnsureComp<SpeedModifierContactsComponent>(uid);
var speed = 1 - totalViscosity;
_speedModContacts.ChangeModifiers(uid, speed, comp);
}

/// if totalviscosity *is* zero, remove the speedmodifiercontactcomponent, because it's not doing anything
else
{
RemComp<SpeedModifierContactsComponent>(uid);
}

totalViscosity += reagentQuant * reagentProto.Viscosity;
totalQuantity += reagentQuant;
}

totalViscosity /= totalQuantity;

if (totalQuantity < fullPuddleAmount)
totalViscosity /= 2;

if (totalViscosity != 0)
{
var comp = EnsureComp<SpeedModifierContactsComponent>(uid);
var speed = 1 - totalViscosity;
_speedModContacts.ChangeModifiers(uid, speed, comp);
}
else
{
RemComp<SpeedModifierContactsComponent>(uid);
}
}

private void OnAnchorChanged(Entity<PuddleComponent> entity, ref AnchorStateChangedEvent args)
Expand Down

0 comments on commit 16370bf

Please sign in to comment.