Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Patryk26g committed Feb 3, 2025
1 parent a7d2772 commit 4ce00a9
Showing 1 changed file with 31 additions and 37 deletions.
68 changes: 31 additions & 37 deletions src/microbe_stage/systems/MicrobeAISystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -510,11 +510,11 @@ 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
// If the microbe has radiation protection it means it has melanosomes and can stay near the radioactive chunks
// to produce ATP
if (organelles.RadiationProtection > 0)
{
if (GoNearRadioactiveChunk(ref position, ref ai, ref control, speciesFocus))
if (GoNearRadioactiveChunk(ref position, ref ai, ref control, speciesFocus, random))
{
return;
}
Expand Down Expand Up @@ -573,6 +573,7 @@ private bool RunFromNearestRadioactiveChunk(ref WorldPosition position, ref Micr
}

var oppositeDirection = position.Position + (position.Position - chosenChunk.Value.Position);
oppositeDirection = oppositeDirection.Normalized() * 500.0f;

ai.TargetPosition = oppositeDirection;
control.LookAtPoint = ai.TargetPosition;
Expand All @@ -584,7 +585,7 @@ private bool RunFromNearestRadioactiveChunk(ref WorldPosition position, ref Micr
}

private bool GoNearRadioactiveChunk(ref WorldPosition position, ref MicrobeAI ai,
ref MicrobeControl control, float speciesFocus)
ref MicrobeControl control, float speciesFocus, Random random)
{
var maxDistance = 30000.0f * speciesFocus / Constants.MAX_SPECIES_FOCUS + 3000.0f;
var chosenChunk = GetNearestRadioactiveChunk(ref position, maxDistance);
Expand All @@ -594,8 +595,11 @@ private bool GoNearRadioactiveChunk(ref WorldPosition position, ref MicrobeAI ai
return false;
}

// Range from 0.8 to 1.2
var randomMultiplier = (float)random.NextDouble() * 0.4f + 0.8f;

// 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)
if (position.Position.DistanceSquaredTo(chosenChunk.Value.Position) < 700.0f * randomMultiplier)
{
control.SetMoveSpeed(0.0f);
return true;
Expand Down Expand Up @@ -1417,47 +1421,37 @@ private void CleanSpeciesUsingVaryingCompound()
private void BuildChunksCache()
{
// To allow multithreaded AI access safely
// As the chunk lock is always held when building all of the chunk caches,
// the other individual cache objects don't need separate locks to protect them
lock (chunkDataCache)
{
lock (terrainChunkDataCache)
{
if (chunkCacheBuilt)
return;

foreach (ref readonly var chunk in chunksSet.GetEntities())
{
if (chunk.Has<TimedLife>())
{
// Ignore already despawning chunks
ref var timed = ref chunk.Get<TimedLife>();

if (timed.TimeToLiveRemaining <= 0)
continue;
}
if (chunkCacheBuilt)
return;

// Ignore chunks that wouldn't yield any useful compounds when absorbing
ref var compounds = ref chunk.Get<CompoundStorage>();
foreach (ref readonly var chunk in chunksSet.GetEntities())
{
// 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>();
// TODO: determine if it is a good idea to resolve this data here immediately
ref var position = ref chunk.Get<WorldPosition>();

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));
}
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;
}
}

Expand Down

2 comments on commit 4ce00a9

@hhyyrylainen
Copy link
Member

@hhyyrylainen hhyyrylainen commented on 4ce00a9 Feb 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is random.NextFloat() available here? (to replace (float)random.NextDouble())

@Patryk26g
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, only with XoShiRo128plus, but this one is Random

Please sign in to comment.