Disease performance improvements (#7118)

This commit is contained in:
metalgearsloth
2022-03-14 13:30:34 +11:00
committed by GitHub
parent bb9ad4259c
commit 38b34ebf08
2 changed files with 33 additions and 36 deletions

View File

@@ -185,9 +185,7 @@ namespace Content.Server.Disease
/// </summary> /// </summary>
private void OnInteractDiseasedHand(EntityUid uid, DiseasedComponent component, InteractHandEvent args) private void OnInteractDiseasedHand(EntityUid uid, DiseasedComponent component, InteractHandEvent args)
{ {
if (!_interactionSystem.InRangeUnobstructed(args.User, args.Target)) InteractWithDiseased(args.Target, args.User);
return;
InteractWithDiseased (args.Target, args.User);
} }
/// <summary> /// <summary>
@@ -266,13 +264,14 @@ namespace Content.Server.Disease
/// Tries to infect anyone that /// Tries to infect anyone that
/// interacts with a diseased person or body /// interacts with a diseased person or body
/// </summary> /// </summary>
private void InteractWithDiseased(EntityUid diseased, EntityUid target) private void InteractWithDiseased(EntityUid diseased, EntityUid target, DiseaseCarrierComponent? diseasedCarrier = null)
{ {
if (!TryComp<DiseaseCarrierComponent>(target, out var carrier)) if (!Resolve(diseased, ref diseasedCarrier, false) ||
diseasedCarrier.Diseases.Count == 0 ||
!TryComp<DiseaseCarrierComponent>(target, out var carrier))
return; return;
var disease = _random.Pick(Comp<DiseaseCarrierComponent>(diseased).Diseases); var disease = _random.Pick(diseasedCarrier.Diseases);
if (disease != null)
TryInfect(carrier, disease, 0.4f); TryInfect(carrier, disease, 0.4f);
} }
@@ -283,16 +282,14 @@ namespace Content.Server.Disease
/// to not be guaranteed you are looking /// to not be guaranteed you are looking
/// for TryInfect. /// for TryInfect.
/// </summary> /// </summary>
public void TryAddDisease(DiseaseCarrierComponent? target, DiseasePrototype? addedDisease, string? diseaseName = null, EntityUid host = default!) public void TryAddDisease(DiseaseCarrierComponent? target, DiseasePrototype? addedDisease, string? diseaseName = null, EntityUid? host = null)
{ {
if (diseaseName != null && _prototypeManager.TryIndex(diseaseName, out DiseasePrototype? diseaseProto)) if (diseaseName != null && _prototypeManager.TryIndex(diseaseName, out DiseasePrototype? diseaseProto))
addedDisease = diseaseProto; addedDisease = diseaseProto;
if (host != default!) if (!TryComp(host, out target))
target = Comp<DiseaseCarrierComponent>(host); return;
if (target != null)
{
foreach (var disease in target.AllDiseases) foreach (var disease in target.AllDiseases)
{ {
if (disease.ID == addedDisease?.ID) //ID because of the way protoypes work if (disease.ID == addedDisease?.ID) //ID because of the way protoypes work
@@ -302,7 +299,6 @@ namespace Content.Server.Disease
target.Diseases.Add(freshDisease); target.Diseases.Add(freshDisease);
AddQueue.Enqueue(target.Owner); AddQueue.Enqueue(target.Owner);
} }
}
/// <summary> /// <summary>
/// Pits the infection chance against the /// Pits the infection chance against the
@@ -312,7 +308,7 @@ namespace Content.Server.Disease
/// </summary> /// </summary>
public void TryInfect(DiseaseCarrierComponent carrier, DiseasePrototype? disease, float chance = 0.7f) public void TryInfect(DiseaseCarrierComponent carrier, DiseasePrototype? disease, float chance = 0.7f)
{ {
if(disease == null || !disease.Infectious) if(disease is not { Infectious: true })
return; return;
var infectionChance = chance - carrier.DiseaseResist; var infectionChance = chance - carrier.DiseaseResist;
if (infectionChance <= 0) if (infectionChance <= 0)
@@ -326,13 +322,14 @@ namespace Content.Server.Disease
/// and then tries to infect anyone in range /// and then tries to infect anyone in range
/// if the snougher is not wearing a mask. /// if the snougher is not wearing a mask.
/// </summary> /// </summary>
public void SneezeCough(EntityUid uid, DiseasePrototype? disease, string snoughMessage, bool airTransmit = true, float infectionChance = 0.3f) public void SneezeCough(EntityUid uid, DiseasePrototype? disease, string snoughMessage, bool airTransmit = true, TransformComponent? xform = null)
{ {
var xform = Comp<TransformComponent>(uid); if (!Resolve(uid, ref xform)) return;
if (snoughMessage != string.Empty)
if (!string.IsNullOrEmpty(snoughMessage))
_popupSystem.PopupEntity(Loc.GetString(snoughMessage, ("person", uid)), uid, Filter.Pvs(uid)); _popupSystem.PopupEntity(Loc.GetString(snoughMessage, ("person", uid)), uid, Filter.Pvs(uid));
if (disease == null || !disease.Infectious || airTransmit == false) if (disease is not { Infectious: true } || !airTransmit)
return; return;
if (_inventorySystem.TryGetSlotEntity(uid, "mask", out var maskUid) && if (_inventorySystem.TryGetSlotEntity(uid, "mask", out var maskUid) &&
@@ -340,12 +337,13 @@ namespace Content.Server.Disease
blocker.Enabled) blocker.Enabled)
return; return;
var carrierQuery = GetEntityQuery<DiseaseCarrierComponent>();
foreach (var entity in _lookup.GetEntitiesInRange(xform.MapID, xform.WorldPosition, 2f)) foreach (var entity in _lookup.GetEntitiesInRange(xform.MapID, xform.WorldPosition, 2f))
{ {
if (!_interactionSystem.InRangeUnobstructed(uid, entity)) if (!carrierQuery.TryGetComponent(entity, out var carrier) ||
continue; !_interactionSystem.InRangeUnobstructed(uid, entity)) continue;
if (TryComp<DiseaseCarrierComponent>(entity, out var carrier))
TryInfect(carrier, disease, 0.3f); TryInfect(carrier, disease, 0.3f);
} }
} }
@@ -354,7 +352,7 @@ namespace Content.Server.Disease
/// Adds a disease to the carrier's /// Adds a disease to the carrier's
/// past diseases to give them immunity /// past diseases to give them immunity
/// IF they don't already have the disease. /// IF they don't already have the disease.
/// <summary> /// </summary>
public void Vaccinate(DiseaseCarrierComponent carrier, DiseasePrototype disease) public void Vaccinate(DiseaseCarrierComponent carrier, DiseasePrototype disease)
{ {
foreach (var currentDisease in carrier.Diseases) foreach (var currentDisease in carrier.Diseases)

View File

@@ -3,12 +3,11 @@ using JetBrains.Annotations;
namespace Content.Server.Disease namespace Content.Server.Disease
{ {
[UsedImplicitly]
/// <summary> /// <summary>
/// Makes the diseased sneeze or cough /// Makes the diseased sneeze or cough
/// or neither. /// or neither.
/// </summary> /// </summary>
[UsedImplicitly]
public sealed class DiseaseSnough : DiseaseEffect public sealed class DiseaseSnough : DiseaseEffect
{ {
/// <summary> /// <summary>