-
-
Notifications
You must be signed in to change notification settings - Fork 512
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
Add radioactive chunk miche and make AI to use and omit radioactive chunks #5867
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,6 +71,9 @@ public sealed class MicrobeAISystem : AEntitySetSystem<float>, ISpeciesMemberLoc | |
private readonly List<(Entity Entity, Vector3 Position, float EngulfSize, CompoundBag Compounds)> | ||
chunkDataCache = new(); | ||
|
||
private readonly List<(Entity Entity, Vector3 Position, CompoundBag Compounds)> | ||
terrainChunkDataCache = new(); | ||
|
||
private readonly Dictionary<Species, bool> speciesUsingVaryingCompounds = new(); | ||
private readonly HashSet<BioProcess> varyingCompoundsTemporary = new(); | ||
|
||
|
@@ -103,9 +106,8 @@ public MicrobeAISystem(IReadonlyCompoundClouds cloudSystem, IDaylightInfo lightI | |
microbesSet = world.GetEntities().With<WorldPosition>().With<SpeciesMember>() | ||
.With<Health>().With<Engulfer>().With<Engulfable>().Without<AttachedToEntity>().AsSet(); | ||
|
||
// Engulfables, which are basically all chunks when they aren't cells, and aren't attached so that they | ||
// also aren't eaten already | ||
chunksSet = world.GetEntities().With<Engulfable>().With<WorldPosition>().With<CompoundStorage>() | ||
// Chunks that aren't cells or attached so that they also aren't eaten already | ||
chunksSet = world.GetEntities().With<WorldPosition>().With<CompoundStorage>() | ||
.Without<SpeciesMember>().Without<AttachedToEntity>().AsSet(); | ||
} | ||
|
||
|
@@ -330,6 +332,17 @@ private void ChooseActions(in Entity entity, ref MicrobeAI ai, ref CompoundAbsor | |
control.SetMucocystState(ref organelles, ref compoundStorage, entity, false); | ||
} | ||
|
||
var radiationAmount = compounds.GetCompoundAmount(Compound.Radiation); | ||
var radiationFraction = radiationAmount / compounds.GetCapacityForCompound(Compound.Radiation); | ||
|
||
if (radiationFraction > Constants.RADIATION_DAMAGE_THRESHOLD * 0.7f) | ||
{ | ||
if (RunFromNearestRadioactiveChunk(ref position, ref ai, ref control)) | ||
{ | ||
return; | ||
} | ||
} | ||
|
||
// If this microbe is out of ATP, pick an amount of time to rest | ||
if (compounds.GetCompoundAmount(Compound.ATP) < 1.0f) | ||
{ | ||
|
@@ -497,6 +510,16 @@ private void ChooseActions(in Entity entity, ref MicrobeAI ai, ref CompoundAbsor | |
// There is no reason to be engulfing at this stage | ||
control.SetStateColonyAware(entity, MicrobeState.Normal); | ||
|
||
// If the microbe has radiation protection it means it has melanosomes and can stay near tha radioactive chunks | ||
// to produce ATP | ||
if (organelles.RadiationProtection > 0) | ||
{ | ||
if (GoNearRadioactiveChunk(ref position, ref ai, ref control, speciesFocus)) | ||
{ | ||
return; | ||
} | ||
} | ||
|
||
// Otherwise just wander around and look for compounds | ||
if (!isSessile) | ||
{ | ||
|
@@ -510,6 +533,82 @@ private void ChooseActions(in Entity entity, ref MicrobeAI ai, ref CompoundAbsor | |
} | ||
} | ||
|
||
private (Entity Entity, Vector3 Position, CompoundBag Compounds)? GetNearestRadioactiveChunk( | ||
ref WorldPosition position, float maxDistance) | ||
{ | ||
(Entity Entity, Vector3 Position, CompoundBag Compounds)? chosenChunk = null; | ||
float bestFoundChunkDistance = float.MaxValue; | ||
|
||
BuildChunksCache(); | ||
|
||
foreach (var chunk in terrainChunkDataCache) | ||
{ | ||
if (!chunk.Compounds.Compounds.Keys.Contains(Compound.Radiation)) | ||
{ | ||
continue; | ||
} | ||
|
||
var distance = (chunk.Position - position.Position).LengthSquared(); | ||
|
||
if (distance > bestFoundChunkDistance) | ||
continue; | ||
|
||
if (distance > maxDistance) | ||
continue; | ||
|
||
chosenChunk = chunk; | ||
} | ||
|
||
return chosenChunk; | ||
} | ||
|
||
private bool RunFromNearestRadioactiveChunk(ref WorldPosition position, ref MicrobeAI ai, | ||
ref MicrobeControl control) | ||
{ | ||
var chosenChunk = GetNearestRadioactiveChunk(ref position, 500.0f); | ||
|
||
if (chosenChunk == null) | ||
{ | ||
return false; | ||
} | ||
|
||
var oppositeDirection = position.Position + (position.Position - chosenChunk.Value.Position); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be normalized and then multiplied by a certain distance? I think that this math may end up picking a point really near if the distance between the chunk and the current position gets too low. |
||
|
||
ai.TargetPosition = oppositeDirection; | ||
control.LookAtPoint = ai.TargetPosition; | ||
|
||
control.SetMoveSpeedTowardsPoint(ref position, ai.TargetPosition, Constants.AI_BASE_MOVEMENT); | ||
control.Sprinting = true; | ||
|
||
return true; | ||
} | ||
|
||
private bool GoNearRadioactiveChunk(ref WorldPosition position, ref MicrobeAI ai, | ||
ref MicrobeControl control, float speciesFocus) | ||
{ | ||
var maxDistance = 30000.0f * speciesFocus / Constants.MAX_SPECIES_FOCUS + 3000.0f; | ||
var chosenChunk = GetNearestRadioactiveChunk(ref position, maxDistance); | ||
|
||
if (chosenChunk == null) | ||
{ | ||
return false; | ||
} | ||
|
||
// If the microbe is close to the chunk it doesn't need to go any closer | ||
if (position.Position.DistanceSquaredTo(chosenChunk.Value.Position) < 700.0f) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe a slight bit of randomness in the distance could be used here to make cells not always stop at an exact distance away from a chunk? (the microbe AI should already have access to a random instance that is fetched for each entity) |
||
{ | ||
control.SetMoveSpeed(0.0f); | ||
return true; | ||
} | ||
|
||
ai.TargetPosition = chosenChunk.Value.Position; | ||
control.LookAtPoint = ai.TargetPosition; | ||
|
||
control.SetMoveSpeedTowardsPoint(ref position, ai.TargetPosition, Constants.AI_BASE_MOVEMENT); | ||
|
||
return true; | ||
} | ||
|
||
private (Entity Entity, Vector3 Position, float EngulfSize, CompoundBag Compounds)? GetNearestChunkItem( | ||
in Entity entity, ref Engulfer engulfer, ref MicrobeControl control, ref WorldPosition position, | ||
CompoundBag ourCompounds, float speciesFocus, float speciesOpportunism, Random random, bool ironEater, | ||
|
@@ -1320,34 +1419,45 @@ private void BuildChunksCache() | |
// To allow multithreaded AI access safely | ||
lock (chunkDataCache) | ||
{ | ||
if (chunkCacheBuilt) | ||
return; | ||
|
||
foreach (ref readonly var chunk in chunksSet.GetEntities()) | ||
lock (terrainChunkDataCache) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this second level of locking is unnecessary and could be replaced with a comment like:
|
||
{ | ||
if (chunk.Has<TimedLife>()) | ||
if (chunkCacheBuilt) | ||
return; | ||
|
||
foreach (ref readonly var chunk in chunksSet.GetEntities()) | ||
{ | ||
// Ignore already despawning chunks | ||
ref var timed = ref chunk.Get<TimedLife>(); | ||
if (chunk.Has<TimedLife>()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a new code? Is this really that useful to add here? This means that relatively the chunk cache will take quite a bit longer to build. And my hunch is that this doesn't help much as it takes just a few seconds for chunks to dissolve, so microbes don't see that many disappearing chunks even without this check. |
||
{ | ||
// Ignore already despawning chunks | ||
ref var timed = ref chunk.Get<TimedLife>(); | ||
|
||
if (timed.TimeToLiveRemaining <= 0) | ||
continue; | ||
} | ||
if (timed.TimeToLiveRemaining <= 0) | ||
continue; | ||
} | ||
|
||
// Ignore chunks that wouldn't yield any useful compounds when absorbing | ||
ref var compounds = ref chunk.Get<CompoundStorage>(); | ||
// Ignore chunks that wouldn't yield any useful compounds when absorbing | ||
ref var compounds = ref chunk.Get<CompoundStorage>(); | ||
|
||
if (!compounds.Compounds.HasAnyCompounds()) | ||
continue; | ||
if (!compounds.Compounds.HasAnyCompounds()) | ||
continue; | ||
|
||
// TODO: determine if it is a good idea to resolve this data here immediately | ||
ref var position = ref chunk.Get<WorldPosition>(); | ||
ref var engulfable = ref chunk.Get<Engulfable>(); | ||
// TODO: determine if it is a good idea to resolve this data here immediately | ||
ref var position = ref chunk.Get<WorldPosition>(); | ||
|
||
chunkDataCache.Add((chunk, position.Position, engulfable.AdjustedEngulfSize, compounds.Compounds)); | ||
} | ||
if (chunk.Has<Engulfable>()) | ||
{ | ||
ref var engulfable = ref chunk.Get<Engulfable>(); | ||
chunkDataCache.Add((chunk, position.Position, engulfable.AdjustedEngulfSize, | ||
compounds.Compounds)); | ||
} | ||
else | ||
{ | ||
terrainChunkDataCache.Add((chunk, position.Position, compounds.Compounds)); | ||
} | ||
} | ||
|
||
chunkCacheBuilt = true; | ||
chunkCacheBuilt = true; | ||
} | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo in the comment