-
Notifications
You must be signed in to change notification settings - Fork 244
[ADD] Аллергии #2571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Rorkh
wants to merge
16
commits into
AdventureTimeSS14:master
Choose a base branch
from
Rorkh:allergies-feature
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[ADD] Аллергии #2571
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
809d3dd
[ADD] Система аллергий
Rorkh fde50c2
[TWEAK] Трейт и конфигурация в компоненте
Rorkh 5c720d4
[TWEAK] YAML fix
Rorkh 09b25fc
[TWEAK] Swab popups
Rorkh 8d95223
[FIX] Clanker driven development
Rorkh 45fb046
Merge branch 'master' into allergies-feature
Rorkh bcb05c5
[ADD] Allergic stack and reactions
Rorkh 2d8fc6e
[FIX] Fixes and etc etc...
Rorkh c3a9801
[TWEAK] Снятие симптомов аллергии, баланс
Rorkh ef57367
Merge branch 'master' into allergies-feature
Rorkh a04c639
[FIX] Allergic reactions sort
Rorkh 0d9654a
[TWEAK] Документация, clanker-driven
Rorkh e8a204f
Merge branch 'allergies-feature' of https://github.com/Rorkh/space_st…
Rorkh b6a6edc
Update medicine.yml
Rorkh 0abf96b
Merge branch 'master' into allergies-feature
Schrodinger71 99856fa
Merge branch 'master' into allergies-feature
Darkiich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| using Content.Shared.Chemistry.Reagent; | ||
| using Robust.Shared.Prototypes; | ||
|
|
||
| namespace Content.Server.ADT.Body; | ||
|
|
||
| /// <summary> | ||
| /// When allergy triggered by allergens in bloodstream. | ||
| /// </summary> | ||
| [ByRefEvent] | ||
| public struct AllergyTriggeredEvent; | ||
|
|
||
| /// <summary> | ||
| /// When allergy stack has faded to zero. | ||
| /// </summary> | ||
| [ByRefEvent] | ||
| public struct AllergyFadedEvent; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| using Content.Shared.ADT.Body.Allergies; | ||
| using Content.Shared.ADT.Body.Allergies.Prototypes; | ||
| using Content.Shared.EntityEffects; | ||
| using Robust.Shared.Timing; | ||
| using System.Linq; | ||
|
|
||
| namespace Content.Server.ADT.Body; | ||
|
|
||
| public sealed partial class AllergicSystem : EntitySystem | ||
| { | ||
| private List<AllergicReactionPrototype> _reactions = new(); | ||
|
|
||
| [Dependency] private IGameTiming _timing = default!; | ||
| [Dependency] private SharedEntityEffectsSystem _effectSystem = default!; | ||
|
|
||
| public void InitializeShock() | ||
| { | ||
| SubscribeLocalEvent<AllergicComponent, AllergyFadedEvent>(Unshock); | ||
| _reactions = _proto.EnumeratePrototypes<AllergicReactionPrototype>() | ||
| .OrderBy(r => r.StackThreshold) | ||
| .ToList(); | ||
| } | ||
|
|
||
| private void Unshock(EntityUid uid, AllergicComponent allergic, ref AllergyFadedEvent ev) | ||
| { | ||
| allergic.NextShockEvent = TimeSpan.Zero; | ||
| } | ||
|
|
||
| private void AssignNextShockTiming(AllergicComponent allergic) | ||
| { | ||
| allergic.NextShockEvent = _timing.CurTime + TimeSpan.FromSeconds(_random.NextFloat(allergic.MinimumTimeTilNextShockEvent, allergic.MaximumTimeTilNextShockEvent)); | ||
| } | ||
|
|
||
| private void UpdateShock(EntityUid uid, AllergicComponent allergic) | ||
| { | ||
| if (allergic.AllergyStack <= 0) | ||
| return; | ||
|
|
||
| if (allergic.NextShockEvent == TimeSpan.Zero) | ||
| { | ||
| AssignNextShockTiming(allergic); | ||
| return; | ||
| } | ||
|
|
||
| if (allergic.NextShockEvent > _timing.CurTime) | ||
| return; | ||
|
|
||
| AllergicReactionPrototype? picked = null; | ||
|
|
||
| foreach (var reaction in _reactions) | ||
| { | ||
| if (allergic.AllergyStack >= reaction.StackThreshold) | ||
| picked = reaction; | ||
| } | ||
|
|
||
| if (picked == null) | ||
| return; | ||
|
|
||
| foreach (var effect in picked.Effects) | ||
| { | ||
| _effectSystem.ApplyEffect(uid, effect); | ||
| } | ||
|
|
||
| AssignNextShockTiming(allergic); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| using Content.Shared.ADT.Body.Allergies; | ||
|
|
||
| namespace Content.Server.ADT.Body; | ||
|
|
||
| public sealed partial class AllergicSystem : EntitySystem | ||
| { | ||
| #region Events | ||
|
|
||
| private void IncrementStackOnTrigger(EntityUid uid, AllergicComponent allergic, ref AllergyTriggeredEvent ev) | ||
| { | ||
| AdjustAllergyStack(uid, allergic.StackGrow, allergic); | ||
| } | ||
|
|
||
| #endregion | ||
|
|
||
| #region Flow | ||
|
|
||
| private void UpdateStack(EntityUid uid, AllergicComponent allergic) | ||
| { | ||
| if (allergic.AllergyStack <= 0) | ||
| return; | ||
|
|
||
| if (allergic.NextStackFade > _timing.CurTime) | ||
| return; | ||
|
|
||
| float newStack = allergic.AllergyStack + allergic.StackFade; | ||
| SetAllergyStack(uid, newStack, allergic); | ||
|
|
||
| if (newStack <= 0) | ||
| { | ||
| var faded = new AllergyFadedEvent(); | ||
| RaiseLocalEvent(uid, ref faded); | ||
| } | ||
|
|
||
| allergic.NextStackFade = allergic.NextStackFade + allergic.StackFadeRate; | ||
| } | ||
|
|
||
| #endregion | ||
|
|
||
| #region API | ||
|
|
||
| /// <summary> | ||
| /// Метод для изменения стака аллергии на относительное значение. | ||
| /// </summary> | ||
| /// <param name="uid"></param> | ||
| /// <param name="relativeStack"></param> | ||
| /// <param name="allergic"></param> | ||
| public void AdjustAllergyStack(EntityUid uid, float relativeStack, AllergicComponent? allergic = null) | ||
| { | ||
| if (!Resolve(uid, ref allergic)) | ||
| return; | ||
|
|
||
| SetAllergyStack(uid, allergic.AllergyStack + relativeStack, allergic); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Метод для изменения стака аллергии. | ||
| /// </summary> | ||
| /// <param name="uid"></param> | ||
| /// <param name="stack"></param> | ||
| /// <param name="allergic"></param> | ||
| public void SetAllergyStack(EntityUid uid, float stack, AllergicComponent? allergic = null) | ||
| { | ||
| if (!Resolve(uid, ref allergic)) | ||
| return; | ||
|
|
||
| allergic.AllergyStack = MathF.Min(MathF.Max(0, stack), allergic.MaximumStack); | ||
| } | ||
|
|
||
| #endregion | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| using Content.Server.Body.Systems; | ||
| using Content.Shared.ADT.Body.Allergies; | ||
| using Content.Shared.Chemistry.Reagent; | ||
| using Content.Shared.Damage; | ||
| using Content.Shared.Damage.Prototypes; | ||
| using Content.Shared.EntityEffects.Effects; | ||
| using Robust.Shared.Prototypes; | ||
| using Robust.Shared.Random; | ||
| using System.Linq; | ||
|
|
||
| namespace Content.Server.ADT.Body; | ||
|
|
||
| public sealed partial class AllergicSystem : EntitySystem | ||
| { | ||
| [Dependency] private readonly IRobustRandom _random = default!; | ||
| [Dependency] private readonly IPrototypeManager _proto = default!; | ||
|
|
||
| private ProtoId<DamageTypePrototype> _allergyDamageType = "Poison"; | ||
| private DamageTypePrototype? _allergyDamageTypeProto; | ||
|
|
||
| public override void Initialize() | ||
| { | ||
| base.Initialize(); | ||
|
|
||
| SubscribeLocalEvent<AllergicComponent, ComponentInit>(OnInit); | ||
| SubscribeLocalEvent<AllergicComponent, GetReagentEffectsEvent>(OnGetReagentEffects); | ||
|
|
||
| SubscribeLocalEvent<AllergicComponent, AllergyTriggeredEvent>(OnAllergyTriggered); | ||
|
|
||
| InitializeShock(); | ||
|
|
||
| _allergyDamageTypeProto = _proto.Index<DamageTypePrototype>(_allergyDamageType); | ||
| } | ||
|
|
||
| private void OnInit(EntityUid uid, AllergicComponent component, ComponentInit args) | ||
| { | ||
| component.Triggers = GetRandomAllergies(component.Min, component.Max); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Метод для обработки метаболизма реагента. | ||
| /// При наличии аллергена, добавляет эффект изменения здоровья и вызывает AllergyTriggeredEvent. | ||
| /// </summary> | ||
| /// <param name="uid"></param> | ||
| /// <param name="component"></param> | ||
| /// <param name="ev"></param> | ||
| private void OnGetReagentEffects(EntityUid uid, AllergicComponent component, ref GetReagentEffectsEvent ev) | ||
| { | ||
| if (!component.Triggers.Contains(ev.Reagent.Prototype)) | ||
| return; | ||
|
|
||
| var damageSpecifier = new DamageSpecifier(_allergyDamageTypeProto!, 0.25); | ||
| var damageEffect = new HealthChange | ||
| { | ||
| Damage = damageSpecifier | ||
| }; | ||
|
|
||
| AllergyTriggeredEvent triggered = new(); | ||
| RaiseLocalEvent(uid, ref triggered); | ||
|
|
||
| ev.Effects = ev.Effects.Append(damageEffect).ToArray(); | ||
| } | ||
|
|
||
| private void OnAllergyTriggered(EntityUid uid, AllergicComponent allergic, ref AllergyTriggeredEvent ev) | ||
| { | ||
| IncrementStackOnTrigger(uid, allergic, ref ev); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Метод для получения рандомных аллергенов. | ||
| /// </summary> | ||
| /// <param name="min">Максимальное кол-во</param> | ||
| /// <param name="max">Минимальное кол-во</param> | ||
| /// <returns></returns> | ||
| private List<ProtoId<ReagentPrototype>> GetRandomAllergies(int min, int max) | ||
| { | ||
| int reagentsCount = _proto.Count<ReagentPrototype>(); | ||
|
|
||
| if (reagentsCount == 0) | ||
| return new(); | ||
|
|
||
| int safeMin = Math.Clamp(min, 0, reagentsCount); | ||
| int safeMax = Math.Clamp(max, safeMin, reagentsCount); | ||
| int allergiesCount = _random.Next(safeMin, safeMax + 1); | ||
|
|
||
| var picked = new HashSet<int>(); | ||
| while (picked.Count < allergiesCount) | ||
| { | ||
| picked.Add(_random.Next(reagentsCount)); | ||
| } | ||
|
|
||
| int index = 0; | ||
| List<ProtoId<ReagentPrototype>> allergies = new(); | ||
|
|
||
| foreach (var proto in _proto.EnumeratePrototypes<ReagentPrototype>()) | ||
| { | ||
| if (picked.Contains(index)) | ||
| allergies.Add(proto.ID); | ||
| index++; | ||
| } | ||
|
|
||
| return allergies; | ||
| } | ||
|
|
||
| public override void Update(float frameTime) | ||
| { | ||
| base.Update(frameTime); | ||
|
|
||
| var query = EntityQueryEnumerator<AllergicComponent>(); | ||
|
|
||
| while (query.MoveNext(out var uid, out var allergic)) | ||
| { | ||
| UpdateStack(uid, allergic); | ||
| UpdateShock(uid, allergic); | ||
| } | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
Content.Server/ADT/EntityEffects/AdjustAllergicStackEffectSystem.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| using Content.Server.ADT.Body; | ||
| using Content.Shared.ADT.Body.Allergies; | ||
| using Content.Shared.EntityEffects; | ||
| using Content.Shared.EntityEffects.Effects; | ||
|
|
||
| namespace Content.Server.ADT.EntityEffects; | ||
|
|
||
| /// <summary> | ||
| /// Эффект для изменения значения стака аллергии. | ||
| /// </summary> | ||
| public sealed partial class AdjustAllergicStackEntityEffectSystem : EntityEffectSystem<AllergicComponent, AdjustAllergicStack> | ||
| { | ||
| [Dependency] private AllergicSystem _allergic = default!; | ||
|
|
||
| protected override void Effect(Entity<AllergicComponent> entity, ref EntityEffectEvent<AdjustAllergicStack> args) | ||
| { | ||
| _allergic.AdjustAllergyStack(entity, args.Effect.Amount * args.Scale); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.