Skip to content

Commit

Permalink
Get away from using comma-delimited strings for acoustic names
Browse files Browse the repository at this point in the history
  • Loading branch information
Sollace committed Mar 9, 2024
1 parent bbc805a commit 2ec37b4
Show file tree
Hide file tree
Showing 16 changed files with 161 additions and 159 deletions.
10 changes: 5 additions & 5 deletions src/main/java/eu/ha3/presencefootsteps/PFDebugHud.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import eu.ha3.presencefootsteps.api.DerivedBlock;
import eu.ha3.presencefootsteps.sound.SoundEngine;
import eu.ha3.presencefootsteps.sound.generator.Locomotion;
import eu.ha3.presencefootsteps.world.Emitter;
import eu.ha3.presencefootsteps.world.PrimitiveLookup;
import eu.ha3.presencefootsteps.world.SoundsKey;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.block.BlockState;
import net.minecraft.client.MinecraftClient;
Expand Down Expand Up @@ -109,7 +109,7 @@ private static void insertAt(List<String> values, List<String> destination, Stri
values.clear();
}

private void renderSoundList(String title, Map<String, String> sounds, List<String> list) {
private void renderSoundList(String title, Map<String, SoundsKey> sounds, List<String> list) {
if (sounds.isEmpty()) {
return;
}
Expand All @@ -124,7 +124,7 @@ private void renderSoundList(String title, Map<String, String> sounds, List<Stri
if (!entry.getKey().isEmpty()) {
combinedList.append(entry.getKey()).append(":");
}
combinedList.append(entry.getValue());
combinedList.append(entry.getValue().raw());
}
combinedList.append(" ]");
list.add(combinedList.toString());
Expand All @@ -134,10 +134,10 @@ private void renderSoundList(String title, Map<String, String> sounds, List<Stri
}

if (sounds.isEmpty()) {
list.add(Emitter.UNASSIGNED);
list.add(SoundsKey.UNASSIGNED.raw());
} else {
sounds.forEach((key, value) -> {
list.add((key.isEmpty() ? "default" : key) + ": " + value);
list.add((key.isEmpty() ? "default" : key) + ": " + value.raw());
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import eu.ha3.presencefootsteps.sound.Options;
import eu.ha3.presencefootsteps.sound.State;
import eu.ha3.presencefootsteps.world.SoundsKey;
import net.minecraft.entity.LivingEntity;

public interface AcousticLibrary {
Expand All @@ -10,5 +11,5 @@ public interface AcousticLibrary {
*/
void addAcoustic(String name, Acoustic acoustic);

void playAcoustic(LivingEntity location, String acousticName, State event, Options options);
void playAcoustic(LivingEntity location, SoundsKey acousticName, State event, Options options);
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package eu.ha3.presencefootsteps.sound.acoustics;

import com.google.common.base.Strings;
import eu.ha3.presencefootsteps.PresenceFootsteps;
import eu.ha3.presencefootsteps.sound.Options;
import eu.ha3.presencefootsteps.sound.State;
import eu.ha3.presencefootsteps.sound.player.SoundPlayer;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import eu.ha3.presencefootsteps.world.Emitter;
import eu.ha3.presencefootsteps.world.SoundsKey;
import net.minecraft.entity.LivingEntity;

import java.util.Map;
import java.util.stream.Stream;

public class AcousticsPlayer implements AcousticLibrary {
private final Map<String, Acoustic> acoustics = new Object2ObjectOpenHashMap<>();
Expand All @@ -23,25 +21,20 @@ public AcousticsPlayer(SoundPlayer player) {

@Override
public void addAcoustic(String name, Acoustic acoustic) {
acoustics.put(name, acoustic);
if (acoustics.put(name, acoustic) != null) {
PresenceFootsteps.logger.info("Duplicate acoustic: " + name);
}
}

@Override
public void playAcoustic(LivingEntity location, String acousticName, State event, Options inputOptions) {
if (!Emitter.isEmitter(acousticName)) {
return;
}

if (acousticName.contains(",")) {
Stream.of(acousticName.split(","))
.map(Strings::nullToEmpty)
.filter(s -> !s.isEmpty())
.distinct()
.forEach(fragment -> playAcoustic(location, fragment, event, inputOptions));
} else if (!acoustics.containsKey(acousticName)) {
PresenceFootsteps.logger.warn("Tried to play a missing acoustic: " + acousticName);
} else {
acoustics.get(acousticName).playSound(player, location, event, inputOptions);
public void playAcoustic(LivingEntity location, SoundsKey sounds, State event, Options inputOptions) {
for (String acousticName : sounds.names()) {
Acoustic acoustic = acoustics.get(acousticName);
if (acoustic == null) {
PresenceFootsteps.logger.warn("Tried to play a missing acoustic: " + acousticName);
} else {
acoustic.playSound(player, location, event, inputOptions);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import eu.ha3.presencefootsteps.sound.SoundEngine;
import eu.ha3.presencefootsteps.world.Association;
import eu.ha3.presencefootsteps.world.AssociationPool;
import eu.ha3.presencefootsteps.world.Emitter;
import eu.ha3.presencefootsteps.world.Solver;
import eu.ha3.presencefootsteps.world.SoundsKey;
import eu.ha3.presencefootsteps.world.Substrates;

class TerrestrialStepSoundGenerator implements StepSoundGenerator {
Expand Down Expand Up @@ -215,7 +215,7 @@ public final void produceStep(@Nullable State event, double verticalOffsetAsMinu
Options options = Options.singular("gliding_volume", volume);
State state = entity.isSubmergedInWater() ? State.SWIM : event;

engine.getIsolator().acoustics().playAcoustic(entity, "_SWIM", state, options);
engine.getIsolator().acoustics().playAcoustic(entity, SoundsKey.SWIM, state, options);

playStep(associations.findAssociation(entity.getBlockPos().down(), Solver.MESSY_FOLIAGE_STRATEGY), event);
} else {
Expand Down Expand Up @@ -327,8 +327,8 @@ protected void playStep(Association association, State eventType) {

if (engine.getConfig().getEnabledFootwear()) {
if (entity.getEquippedStack(EquipmentSlot.FEET).getItem() instanceof ArmorItem bootItem) {
String bootSound = engine.getIsolator().primitives().getAssociation(bootItem.getEquipSound(), Substrates.DEFAULT);
if (Emitter.isEmitter(bootSound)) {
SoundsKey bootSound = engine.getIsolator().primitives().getAssociation(bootItem.getEquipSound(), Substrates.DEFAULT);
if (bootSound.isEmitter()) {
engine.getIsolator().stepPlayer().playStep(association, eventType, Options.singular("volume_percentage", 0.5F));
engine.getIsolator().acoustics().playAcoustic(entity, bootSound, eventType, Options.EMPTY);

Expand All @@ -343,7 +343,7 @@ protected void playStep(Association association, State eventType) {
protected void playSinglefoot(double verticalOffsetAsMinus, State eventType, boolean foot) {
Association assos = associations.findAssociation(verticalOffsetAsMinus, isRightFoot);

if (assos.isSilent()) {
if (!assos.isResult()) {
assos = associations.findAssociation(verticalOffsetAsMinus + 1, isRightFoot);
}

Expand All @@ -355,7 +355,7 @@ protected void playMultifoot(double verticalOffsetAsMinus, State eventType) {
Association leftFoot = associations.findAssociation(verticalOffsetAsMinus, false);
Association rightFoot = associations.findAssociation(verticalOffsetAsMinus, true);

if (leftFoot.dataEquals(rightFoot)) {
if (leftFoot.isResult() && leftFoot.dataEquals(rightFoot)) {
// If the two feet solve to the same sound, except NO_ASSOCIATION, only play the sound once
if (isRightFoot) {
leftFoot = Association.NOT_EMITTER;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

import eu.ha3.presencefootsteps.sound.State;
import eu.ha3.presencefootsteps.util.MathUtil;
import eu.ha3.presencefootsteps.world.SoundsKey;
import eu.ha3.presencefootsteps.config.Variator;
import eu.ha3.presencefootsteps.sound.Options;
import eu.ha3.presencefootsteps.sound.SoundEngine;
import net.minecraft.entity.LivingEntity;
import net.minecraft.util.math.Vec3d;

class WingedStepSoundGenerator extends TerrestrialStepSoundGenerator {

private static final SoundsKey SWIFT = SoundsKey.of("_SWIFT");
private static final SoundsKey WING = SoundsKey.of("_WING");
protected boolean isFalling = false;

protected FlightState state = FlightState.IDLE;
Expand Down Expand Up @@ -93,9 +95,9 @@ protected void simulateJumpingLanding() {
if (!isAirborne) {
float volume = speedingJumpStateChange ? 2
: MathUtil.scalex(lastFallDistance, variator.HUGEFALL_LANDING_DISTANCE_MIN, variator.HUGEFALL_LANDING_DISTANCE_MAX);
engine.getIsolator().acoustics().playAcoustic(entity, "_SWIFT", State.LAND, Options.singular("gliding_volume", volume));
engine.getIsolator().acoustics().playAcoustic(entity, SWIFT, State.LAND, Options.singular("gliding_volume", volume));
} else {
engine.getIsolator().acoustics().playAcoustic(entity, "_SWIFT", State.JUMP, Options.EMPTY);
engine.getIsolator().acoustics().playAcoustic(entity, SWIFT, State.JUMP, Options.EMPTY);
}
}

Expand Down Expand Up @@ -127,7 +129,7 @@ protected void simulateFlying() {
variator.WING_IMMOBILE_FADE_START + variator.WING_IMMOBILE_FADE_DURATION);
}

engine.getIsolator().acoustics().playAcoustic(entity, "_WING", State.WALK, Options.singular("gliding_volume", volume));
engine.getIsolator().acoustics().playAcoustic(entity, WING, State.WALK, Options.singular("gliding_volume", volume));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,36 @@ public Random getRNG() {
@SuppressWarnings("deprecation")
@Override
public void playStep(Association association, State event, Options options) {
if (association.isSilent()) {
return;
}

AcousticLibrary library = engine.getIsolator().acoustics();

if (association.hasCoreSound()) {
library.playAcoustic(association.source(), association.acousticNames(), event, options);
if (association.dry().isResult()) {
library.playAcoustic(association.source(), association.dry(), event, options);
} else if (!association.state().isLiquid()) {
BlockSoundGroup soundType = association.soundGroup();
BlockSoundGroup soundType = association.state().getSoundGroup();
BlockState above = association.source().getWorld().getBlockState(association.pos().up());

if (above.isOf(Blocks.SNOW)) {
soundType = above.getSoundGroup();
}

immediatePlayer.playSound(association.source(), soundType.getStepSound().getId().toString(), soundType.getVolume() * 0.15F, soundType.getPitch(), options);
immediatePlayer.playSound(association.source(),
soundType.getStepSound().getId().toString(),
soundType.getVolume() * 0.15F,
soundType.getPitch(),
options
);
}

if (association.hasWetSound() && Options.WET_VOLUME_OPTIONS.get("volume_percentage") > 0.1F) {
library.playAcoustic(association.source(), association.wetAcousticNames(), event, options.and(Options.WET_VOLUME_OPTIONS));
if (association.wet().isEmitter() && Options.WET_VOLUME_OPTIONS.get("volume_percentage") > 0.1F) {
library.playAcoustic(association.source(), association.wet(), event, options.and(Options.WET_VOLUME_OPTIONS));
}

if (association.hasFoliageSound() && Options.FOLIAGE_VOLUME_OPTIONS.get("volume_percentage") > 0.1F) {
library.playAcoustic(association.source(), association.foliageAcousticNames(), event, options.and(Options.FOLIAGE_VOLUME_OPTIONS));
if (association.foliage().isEmitter() && Options.FOLIAGE_VOLUME_OPTIONS.get("volume_percentage") > 0.1F) {
library.playAcoustic(association.source(), association.foliage(), event, options.and(Options.FOLIAGE_VOLUME_OPTIONS));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@
import net.minecraft.util.Identifier;

abstract class AbstractSubstrateLookup<T> implements Lookup<T> {
private final Map<String, Map<Identifier, String>> substrates = new Object2ObjectLinkedOpenHashMap<>();
private final Map<String, Map<Identifier, SoundsKey>> substrates = new Object2ObjectLinkedOpenHashMap<>();

protected abstract Identifier getId(T key);

@Override
public String getAssociation(T key, String substrate) {
public SoundsKey getAssociation(T key, String substrate) {
final Identifier id = getId(key);
return getSubstrateMap(id, substrate).getOrDefault(id, Emitter.UNASSIGNED);
return getSubstrateMap(id, substrate).getOrDefault(id, SoundsKey.UNASSIGNED);
}

@Nullable
protected Map<Identifier, String> getSubstrateMap(Identifier id, String substrate) {
Map<Identifier, String> primitives = substrates.get(substrate);
protected Map<Identifier, SoundsKey> getSubstrateMap(Identifier id, String substrate) {
Map<Identifier, SoundsKey> primitives = substrates.get(substrate);
if (primitives != null) {
return primitives;
}
Expand Down Expand Up @@ -50,7 +50,7 @@ public void add(String key, String value) {

substrates
.computeIfAbsent(substrate, s -> new Object2ObjectLinkedOpenHashMap<>())
.put(new Identifier(primitive), value);
.put(new Identifier(primitive), SoundsKey.of(value));
}

@Override
Expand Down
47 changes: 12 additions & 35 deletions src/main/java/eu/ha3/presencefootsteps/world/Association.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,58 +7,35 @@
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.entity.LivingEntity;
import net.minecraft.sound.BlockSoundGroup;
import net.minecraft.util.math.BlockPos;

public record Association (
BlockState state,
BlockPos pos,
@Nullable LivingEntity source,

String acousticNames,
String wetAcousticNames,
String foliageAcousticNames
SoundsKey dry,
SoundsKey wet,
SoundsKey foliage
) {
public static final Association NOT_EMITTER = new Association(Blocks.AIR.getDefaultState(), BlockPos.ORIGIN, null, Emitter.NOT_EMITTER, Emitter.NOT_EMITTER, Emitter.NOT_EMITTER);
public static final Association NOT_EMITTER = new Association(Blocks.AIR.getDefaultState(), BlockPos.ORIGIN, null, SoundsKey.NON_EMITTER, SoundsKey.NON_EMITTER, SoundsKey.NON_EMITTER);

public static Association of(BlockState state, BlockPos pos, LivingEntity source, String dry, String wet, String foliage) {
if (Emitter.isResult(dry) || Emitter.isResult(wet) || Emitter.isResult(foliage)) {
return new Association(state, pos.toImmutable(), source, dry, wet, foliage);
public static Association of(BlockState state, BlockPos pos, LivingEntity source, SoundsKey dry, SoundsKey wet, SoundsKey foliage) {
if (dry.isSilent() && wet.isSilent() && foliage.isSilent()) {
return NOT_EMITTER;
}
return new Association(state, pos.toImmutable(), source, Emitter.UNASSIGNED, Emitter.UNASSIGNED, Emitter.UNASSIGNED);
return new Association(state, pos.toImmutable(), source, dry, wet, foliage);
}

public boolean isNull() {
return !Emitter.isResult(acousticNames)
&& !Emitter.isResult(wetAcousticNames)
&& !Emitter.isResult(foliageAcousticNames);
}

public boolean hasCoreSound() {
return Emitter.isResult(acousticNames);
}

public boolean hasWetSound() {
return Emitter.isEmitter(wetAcousticNames);
}

public boolean hasFoliageSound() {
return Emitter.isEmitter(foliageAcousticNames);
public boolean isResult() {
return dry.isResult() || wet.isResult() || foliage.isResult();
}

public boolean isSilent() {
return this == NOT_EMITTER || (
Emitter.isNotEmitter(acousticNames)
&& Emitter.isNotEmitter(wetAcousticNames)
&& Emitter.isNotEmitter(foliageAcousticNames)
);
}

public BlockSoundGroup soundGroup() {
return state.getSoundGroup();
return this == NOT_EMITTER;
}

public boolean dataEquals(Association other) {
return Objects.equals(acousticNames, other.acousticNames);
return Objects.equals(dry, other.dry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public final class AssociationPool {
private final Solver solver;

private boolean wasGolem;
private String association;
private SoundsKey association;

public AssociationPool(LivingEntity entity, SoundEngine engine) {
this.entity = entity;
Expand Down Expand Up @@ -64,11 +64,11 @@ public Association findAssociation(BlockPos pos, String strategy) {
* @param substrate The substrate corresponding to the stage of lookup being performed. One of the values in {@link Substrates}
* @return The matching acoustic names or {@link Emitter#UNASSIGNED} if no match could be determined.
*/
public String get(BlockPos pos, BlockState state, String substrate) {
public SoundsKey get(BlockPos pos, BlockState state, String substrate) {
for (Entity golem : entity.getWorld().getOtherEntities(entity, new Box(pos).expand(0.5, 0, 0.5), e -> {
return !e.isCollidable() || e.getBoundingBox().maxY < entity.getY() + 0.2F;
})) {
if (Emitter.isEmitter(association = engine.getIsolator().golems().getAssociation(golem.getType(), substrate))) {
if ((association = engine.getIsolator().golems().getAssociation(golem.getType(), substrate)).isEmitter()) {
return association;
}
}
Expand All @@ -86,15 +86,15 @@ public String get(BlockPos pos, BlockState state, String substrate) {
return association;
}

return Emitter.UNASSIGNED;
return SoundsKey.UNASSIGNED;
}

private boolean getForState(BlockState state, String substrate) {
return Emitter.isResult(association = engine.getIsolator().blocks().getAssociation(state, substrate));
return (association = engine.getIsolator().blocks().getAssociation(state, substrate)).isResult();
}

private boolean getForPrimitive(BlockState state) {
BlockSoundGroup sounds = state.getSoundGroup();
return Emitter.isResult(association = engine.getIsolator().primitives().getAssociation(sounds, PrimitiveLookup.getSubstrate(sounds)));
return (association = engine.getIsolator().primitives().getAssociation(sounds, PrimitiveLookup.getSubstrate(sounds))).isResult();
}
}
Loading

0 comments on commit 2ec37b4

Please sign in to comment.