Skip to content

Commit

Permalink
Apply a cap to the number of entities producing sounds at any given t…
Browse files Browse the repository at this point in the history
…ime. Closes #218
  • Loading branch information
Sollace committed Sep 27, 2023
1 parent 22db125 commit 2c820d4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/main/java/eu/ha3/presencefootsteps/PFConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class PFConfig extends JsonFile {
private int otherPlayerVolume = 100;
private int runningVolumeIncrease = 0;
private int wetSoundsVolume = 50;
private int maxSteppingEntities = 50;

private String stance = "UNKNOWN";

Expand Down Expand Up @@ -75,6 +76,10 @@ public boolean getEnabledMP() {
return multiplayer && getEnabled();
}

public int getMaxSteppingEntities() {
return Math.max(1, maxSteppingEntities);
}

public boolean getEnabled() {
return getGlobalVolume() > 0;
}
Expand Down
25 changes: 21 additions & 4 deletions src/main/java/eu/ha3/presencefootsteps/sound/SoundEngine.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package eu.ha3.presencefootsteps.sound;

import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.stream.Stream;
Expand Down Expand Up @@ -86,8 +91,8 @@ public boolean isRunning(MinecraftClient client) {
return config.getEnabled() && (client.isInSingleplayer() || config.getEnabledMP());
}

private Stream<? extends Entity> getTargets(Entity cameraEntity) {
return cameraEntity.getWorld().getOtherEntities(null, cameraEntity.getBoundingBox().expand(16), e -> {
private Stream<? extends Entity> getTargets(final Entity cameraEntity) {
final List<? extends Entity> entities = cameraEntity.getWorld().getOtherEntities(null, cameraEntity.getBoundingBox().expand(16), e -> {
return e instanceof LivingEntity
&& !(e instanceof WaterCreatureEntity)
&& !(e instanceof FlyingEntity)
Expand All @@ -99,9 +104,21 @@ private Stream<? extends Entity> getTargets(Entity cameraEntity) {
&& !e.hasVehicle()
&& !((LivingEntity)e).isSleeping()
&& (!(e instanceof PlayerEntity) || !e.isSpectator())
&& e.distanceTo(cameraEntity) <= 16
&& e.squaredDistanceTo(cameraEntity) <= 256
&& config.getEntitySelector().test(e);
}).stream();
});

final Comparator<Entity> nearest = Comparator.comparingDouble(e -> e.squaredDistanceTo(cameraEntity));

if (entities.size() < config.getMaxSteppingEntities()) {
return entities.stream();
}
Set<Integer> alreadyVisited = new HashSet<>();
return entities.stream()
.sorted(nearest)
// Always play sounds for players and the entities closest to the camera
// If multiple entities share the same block, only play sounds for one of each distinct type
.filter(e -> e == cameraEntity || e instanceof PlayerEntity || (alreadyVisited.size() < config.getMaxSteppingEntities() && alreadyVisited.add(Objects.hash(e.getType(), e.getBlockPos()))));
}

public void onFrame(MinecraftClient client, Entity cameraEntity) {
Expand Down

0 comments on commit 2c820d4

Please sign in to comment.