From 8eb1cbe1eeed9c8a85d359c94fcc0f7775789f9f Mon Sep 17 00:00:00 2001 From: Patryk26g Date: Tue, 3 Dec 2024 21:51:57 +0100 Subject: [PATCH 1/6] Engulf microbe even if out of ATP --- src/microbe_stage/systems/MicrobeAISystem.cs | 61 +++++++++++++++----- 1 file changed, 45 insertions(+), 16 deletions(-) diff --git a/src/microbe_stage/systems/MicrobeAISystem.cs b/src/microbe_stage/systems/MicrobeAISystem.cs index 7e41751938..0976ee3b75 100644 --- a/src/microbe_stage/systems/MicrobeAISystem.cs +++ b/src/microbe_stage/systems/MicrobeAISystem.cs @@ -329,6 +329,13 @@ private void ChooseActions(in Entity entity, ref MicrobeAI ai, ref CompoundAbsor if (compounds.GetCompoundAmount(Compound.ATP) < compounds.GetCapacityForCompound(Compound.ATP) * ai.ATPThreshold) { + // even if we are out of ATP and there is microbe nearby, engulf them + bool isMicrobeHunting = CheckForHuntingConditions(ref ai, ref position, ref organelles, ref ourSpecies, + ref engulfer, ref cellProperties, ref control, entity, compounds, speciesFocus, speciesAggression, + speciesActivity, strain, speciesOpportunism, random, true); + if (isMicrobeHunting) + return; + bool outOfSomething = false; foreach (var compound in compounds.Compounds) { @@ -454,6 +461,35 @@ private void ChooseActions(in Entity entity, ref MicrobeAI ai, ref CompoundAbsor } } + // check if species can hunt any prey and if so - engage in chase + bool isHunting = CheckForHuntingConditions(ref ai, ref position, ref organelles, ref ourSpecies, ref engulfer, + ref cellProperties, ref control, entity, compounds, speciesFocus, speciesAggression, + speciesActivity, strain, speciesOpportunism, random, false); + if (isHunting) + return; + + // There is no reason to be engulfing at this stage + control.SetStateColonyAware(entity, MicrobeState.Normal); + + // Otherwise just wander around and look for compounds + if (!isSessile) + { + SeekCompounds(in entity, ref ai, ref position, ref control, ref organelles, ref absorber, compounds, + speciesActivity, speciesFocus, random); + } + else + { + // This organism is sessile, and will not act until the environment changes + control.SetMoveSpeed(0.0f); + } + } + + private bool CheckForHuntingConditions(ref MicrobeAI ai, ref WorldPosition position, + ref OrganelleContainer organelles, ref SpeciesMember ourSpecies, + ref Engulfer engulfer, ref CellProperties cellProperties, ref MicrobeControl control, in Entity entity, + CompoundBag compounds, float speciesFocus, float speciesAggression, float speciesActivity, + float speciesOpportunism, float strain, Random random, bool outOfAtp) + { // If there are no chunks, look for living prey to hunt var possiblePrey = GetNearestPreyItem(ref ai, ref position, ref organelles, ref ourSpecies, ref engulfer, compounds, speciesFocus, speciesAggression, speciesOpportunism, random); @@ -469,32 +505,25 @@ private void ChooseActions(in Entity entity, ref MicrobeAI ai, ref CompoundAbsor { GD.PrintErr("Microbe AI tried to engage prey with no position: " + e); ai.FocusedPrey = default; - return; + return false; } bool engulfPrey = cellProperties.CanEngulfObject(ref ourSpecies, ref engulfer, possiblePrey) == EngulfCheckResult.Ok && position.Position.DistanceSquaredTo(prey) < 10.0f * engulfer.EngulfingSize; + // if out of ATP and the prey is out of reach to engulf, do nothing + if (!(outOfAtp && engulfPrey)) + { + return false; + } + EngagePrey(ref ai, ref control, ref organelles, ref position, compounds, entity, prey, engulfPrey, speciesAggression, speciesFocus, speciesActivity, strain, random); - return; + return true; } - // There is no reason to be engulfing at this stage - control.SetStateColonyAware(entity, MicrobeState.Normal); - - // Otherwise just wander around and look for compounds - if (!isSessile) - { - SeekCompounds(in entity, ref ai, ref position, ref control, ref organelles, ref absorber, compounds, - speciesActivity, speciesFocus, random); - } - else - { - // This organism is sessile, and will not act until the environment changes - control.SetMoveSpeed(0.0f); - } + return false; } private (Entity Entity, Vector3 Position, float EngulfSize, CompoundBag Compounds)? GetNearestChunkItem( From 6053530a4894a216cdef119cb009f4f344381a6a Mon Sep 17 00:00:00 2001 From: Patryk26g Date: Tue, 3 Dec 2024 22:00:44 +0100 Subject: [PATCH 2/6] Fix --- src/microbe_stage/systems/MicrobeAISystem.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/microbe_stage/systems/MicrobeAISystem.cs b/src/microbe_stage/systems/MicrobeAISystem.cs index 0976ee3b75..05b77ab982 100644 --- a/src/microbe_stage/systems/MicrobeAISystem.cs +++ b/src/microbe_stage/systems/MicrobeAISystem.cs @@ -464,7 +464,7 @@ private void ChooseActions(in Entity entity, ref MicrobeAI ai, ref CompoundAbsor // check if species can hunt any prey and if so - engage in chase bool isHunting = CheckForHuntingConditions(ref ai, ref position, ref organelles, ref ourSpecies, ref engulfer, ref cellProperties, ref control, entity, compounds, speciesFocus, speciesAggression, - speciesActivity, strain, speciesOpportunism, random, false); + speciesActivity, speciesOpportunism, strain, random, false); if (isHunting) return; @@ -513,7 +513,7 @@ private bool CheckForHuntingConditions(ref MicrobeAI ai, ref WorldPosition posit 10.0f * engulfer.EngulfingSize; // if out of ATP and the prey is out of reach to engulf, do nothing - if (!(outOfAtp && engulfPrey)) + if (outOfAtp && !engulfPrey) { return false; } From 49e16ae10d47c8f173cbf7d1c2ac0de6e5c6b1e4 Mon Sep 17 00:00:00 2001 From: Patryk26g Date: Tue, 3 Dec 2024 22:02:32 +0100 Subject: [PATCH 3/6] Fix 2 --- src/microbe_stage/systems/MicrobeAISystem.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/microbe_stage/systems/MicrobeAISystem.cs b/src/microbe_stage/systems/MicrobeAISystem.cs index 05b77ab982..53774e0a87 100644 --- a/src/microbe_stage/systems/MicrobeAISystem.cs +++ b/src/microbe_stage/systems/MicrobeAISystem.cs @@ -332,7 +332,7 @@ private void ChooseActions(in Entity entity, ref MicrobeAI ai, ref CompoundAbsor // even if we are out of ATP and there is microbe nearby, engulf them bool isMicrobeHunting = CheckForHuntingConditions(ref ai, ref position, ref organelles, ref ourSpecies, ref engulfer, ref cellProperties, ref control, entity, compounds, speciesFocus, speciesAggression, - speciesActivity, strain, speciesOpportunism, random, true); + speciesActivity, speciesOpportunism, strain, random, true); if (isMicrobeHunting) return; From 77707098ba466794d2b3d21b0f35e4d3a43150ea Mon Sep 17 00:00:00 2001 From: Patryk26g Date: Thu, 5 Dec 2024 20:51:10 +0100 Subject: [PATCH 4/6] Fix --- src/microbe_stage/systems/MicrobeAISystem.cs | 26 +++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/microbe_stage/systems/MicrobeAISystem.cs b/src/microbe_stage/systems/MicrobeAISystem.cs index 53774e0a87..efeeffcb80 100644 --- a/src/microbe_stage/systems/MicrobeAISystem.cs +++ b/src/microbe_stage/systems/MicrobeAISystem.cs @@ -280,6 +280,8 @@ private void ChooseActions(in Entity entity, ref MicrobeAI ai, ref CompoundAbsor ref var control = ref entity.Get(); + ref var cellHealth = ref entity.Get(); + var compounds = entity.Get().Compounds; // Adjusted behaviour values (calculated here as these are needed by various methods) @@ -317,8 +319,10 @@ private void ChooseActions(in Entity entity, ref MicrobeAI ai, ref CompoundAbsor return; } + float atpLevel = compounds.GetCompoundAmount(Compound.ATP); + // If this microbe is out of ATP, pick an amount of time to rest - if (compounds.GetCompoundAmount(Compound.ATP) < 1.0f) + if (atpLevel < 1.0f) { // Keep the maximum at 95% full, as there is flickering when near full ai.ATPThreshold = 0.95f * speciesFocus / Constants.MAX_SPECIES_FOCUS; @@ -326,15 +330,19 @@ private void ChooseActions(in Entity entity, ref MicrobeAI ai, ref CompoundAbsor if (ai.ATPThreshold > MathUtils.EPSILON) { - if (compounds.GetCompoundAmount(Compound.ATP) < - compounds.GetCapacityForCompound(Compound.ATP) * ai.ATPThreshold) + if (atpLevel < atpLevel * ai.ATPThreshold) { - // even if we are out of ATP and there is microbe nearby, engulf them - bool isMicrobeHunting = CheckForHuntingConditions(ref ai, ref position, ref organelles, ref ourSpecies, - ref engulfer, ref cellProperties, ref control, entity, compounds, speciesFocus, speciesAggression, - speciesActivity, speciesOpportunism, strain, random, true); - if (isMicrobeHunting) - return; + if (cellHealth.CurrentHealth > 2 * Constants.ENGULF_NO_ATP_DAMAGE) + { + // even if we are out of ATP and there is microbe nearby, engulf them. + // make sure engulfing doesn't kill the cell + bool isMicrobeHunting = CheckForHuntingConditions(ref ai, ref position, ref organelles, + ref ourSpecies, ref engulfer, ref cellProperties, ref control, entity, compounds, + speciesFocus, speciesAggression, speciesActivity, speciesOpportunism, strain, + random, true); + if (isMicrobeHunting) + return; + } bool outOfSomething = false; foreach (var compound in compounds.Compounds) From bb64fe5602228f2f5348e981536e6854b56ba5e0 Mon Sep 17 00:00:00 2001 From: Patryk26g Date: Sat, 7 Dec 2024 10:13:41 +0100 Subject: [PATCH 5/6] Fix --- src/microbe_stage/systems/MicrobeAISystem.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/microbe_stage/systems/MicrobeAISystem.cs b/src/microbe_stage/systems/MicrobeAISystem.cs index efeeffcb80..4b651f1579 100644 --- a/src/microbe_stage/systems/MicrobeAISystem.cs +++ b/src/microbe_stage/systems/MicrobeAISystem.cs @@ -330,7 +330,7 @@ private void ChooseActions(in Entity entity, ref MicrobeAI ai, ref CompoundAbsor if (ai.ATPThreshold > MathUtils.EPSILON) { - if (atpLevel < atpLevel * ai.ATPThreshold) + if (atpLevel < compounds.GetCapacityForCompound(Compound.ATP) * ai.ATPThreshold) { if (cellHealth.CurrentHealth > 2 * Constants.ENGULF_NO_ATP_DAMAGE) { From e5d76b345d4954de7317e852a1c74616d18fc10f Mon Sep 17 00:00:00 2001 From: Patryk26g Date: Thu, 12 Dec 2024 19:28:55 +0100 Subject: [PATCH 6/6] Fix --- src/microbe_stage/systems/MicrobeAISystem.cs | 46 ++++++++++++-------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/src/microbe_stage/systems/MicrobeAISystem.cs b/src/microbe_stage/systems/MicrobeAISystem.cs index 9ea20c95d6..46d2487ede 100644 --- a/src/microbe_stage/systems/MicrobeAISystem.cs +++ b/src/microbe_stage/systems/MicrobeAISystem.cs @@ -334,8 +334,6 @@ private void ChooseActions(in Entity entity, ref MicrobeAI ai, ref CompoundAbsor float atpLevel = compounds.GetCompoundAmount(Compound.ATP); - float atpLevel = compounds.GetCompoundAmount(Compound.ATP); - // If this microbe is out of ATP, pick an amount of time to rest if (atpLevel < 1.0f) { @@ -349,13 +347,12 @@ private void ChooseActions(in Entity entity, ref MicrobeAI ai, ref CompoundAbsor { if (cellHealth.CurrentHealth > 2 * Constants.ENGULF_NO_ATP_DAMAGE) { - // even if we are out of ATP and there is microbe nearby, engulf them. + // Even if we are out of ATP and there is microbe nearby, engulf them. // make sure engulfing doesn't kill the cell - bool isMicrobeHunting = CheckForHuntingConditions(ref ai, ref position, ref organelles, - ref ourSpecies, ref engulfer, ref cellProperties, ref control, entity, compounds, - speciesFocus, speciesAggression, speciesActivity, speciesOpportunism, strain, - random, true); - if (isMicrobeHunting) + if (CheckForHuntingConditions(ref ai, ref position, ref organelles, ref ourSpecies, ref engulfer, + ref cellProperties, ref control, ref cellHealth, ref compoundStorage, entity, speciesFocus, + speciesAggression, speciesActivity, speciesOpportunism, strain, random, true, + atpLevel)) return; } @@ -486,8 +483,8 @@ private void ChooseActions(in Entity entity, ref MicrobeAI ai, ref CompoundAbsor // check if species can hunt any prey and if so - engage in chase bool isHunting = CheckForHuntingConditions(ref ai, ref position, ref organelles, ref ourSpecies, ref engulfer, - ref cellProperties, ref control, entity, compounds, speciesFocus, speciesAggression, - speciesActivity, speciesOpportunism, strain, random, false); + ref cellProperties, ref control, ref health, ref compoundStorage, entity, speciesFocus, speciesAggression, + speciesActivity, speciesOpportunism, strain, random, false, atpLevel); if (isHunting) return; @@ -509,10 +506,12 @@ private void ChooseActions(in Entity entity, ref MicrobeAI ai, ref CompoundAbsor private bool CheckForHuntingConditions(ref MicrobeAI ai, ref WorldPosition position, ref OrganelleContainer organelles, ref SpeciesMember ourSpecies, - ref Engulfer engulfer, ref CellProperties cellProperties, ref MicrobeControl control, in Entity entity, - CompoundBag compounds, float speciesFocus, float speciesAggression, float speciesActivity, - float speciesOpportunism, float strain, Random random, bool outOfAtp) + ref Engulfer engulfer, ref CellProperties cellProperties, ref MicrobeControl control, ref Health health, + ref CompoundStorage compoundStorage, in Entity entity, float speciesFocus, float speciesAggression, + float speciesActivity, float speciesOpportunism, float strain, Random random, bool outOfAtp, float atpLevel) { + var compounds = compoundStorage.Compounds; + // If there are no chunks, look for living prey to hunt var possiblePrey = GetNearestPreyItem(ref ai, ref position, ref organelles, ref ourSpecies, ref engulfer, compounds, speciesFocus, speciesAggression, speciesOpportunism, random); @@ -541,8 +540,8 @@ private bool CheckForHuntingConditions(ref MicrobeAI ai, ref WorldPosition posit return false; } - EngagePrey(ref ai, ref control, ref organelles, ref position, compounds, entity, prey, engulfPrey, - speciesAggression, speciesFocus, speciesActivity, strain, random); + EngagePrey(ref ai, ref control, ref organelles, ref position, ref compoundStorage, ref health, entity, + prey, engulfPrey, speciesAggression, speciesFocus, speciesActivity, strain, random, atpLevel); return true; } @@ -897,10 +896,21 @@ private void FleeFromPredators(ref WorldPosition position, ref MicrobeAI ai, ref } private void EngagePrey(ref MicrobeAI ai, ref MicrobeControl control, ref OrganelleContainer organelles, - ref WorldPosition position, CompoundBag ourCompounds, in Entity entity, Vector3 target, bool engulf, - float speciesAggression, float speciesFocus, float speciesActivity, float strain, Random random) + ref WorldPosition position, ref CompoundStorage compoundStorage, ref Health health, in Entity entity, + Vector3 target, bool engulf, float speciesAggression, float speciesFocus, float speciesActivity, + float strain, Random random, float atpLevel) { - control.SetStateColonyAware(entity, engulf ? MicrobeState.Engulf : MicrobeState.Normal); + var ourCompounds = compoundStorage.Compounds; + + if (atpLevel <= Constants.ENGULF_NO_ATP_TRIGGER_THRESHOLD) + { + control.EnterEngulfModeForcedState(ref health, ref compoundStorage, entity, Compound.ATP); + } + else + { + control.SetStateColonyAware(entity, engulf ? MicrobeState.Engulf : MicrobeState.Normal); + } + ai.TargetPosition = target; control.LookAtPoint = ai.TargetPosition; if (CanShootToxin(ourCompounds, speciesFocus))