Skip to content

Commit

Permalink
Weight table cleanup; fog diagnostics; fix neoforge GUI layer registr…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
OreCruncher committed Jan 2, 2025
1 parent a7f86a1 commit 17ea498
Show file tree
Hide file tree
Showing 15 changed files with 113 additions and 93 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.orecruncher.dsurround.config;

import com.google.common.base.MoreObjects;
import net.minecraft.util.random.Weight;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.orecruncher.dsurround.lib.WeightTable;
import org.orecruncher.dsurround.lib.di.ContainerManager;
Expand All @@ -11,29 +13,30 @@
public class AcousticEntry implements WeightTable.IItem<ISoundFactory> {

private static final IConditionEvaluator CONDITION_EVALUATOR = ContainerManager.resolve(IConditionEvaluator.class);
private static final int DEFAULT_WEIGHT = 10;
private static final Weight DEFAULT_WEIGHT = Weight.of(10);

private final int weight;
private final Weight weight;
private final ISoundFactory acoustic;
private final Script conditions;

public AcousticEntry(final ISoundFactory acoustic, @Nullable final Script condition) {
this(acoustic, condition, DEFAULT_WEIGHT);
}

public AcousticEntry(final ISoundFactory acoustic, @Nullable final Script condition, int weight) {
public AcousticEntry(final ISoundFactory acoustic, @Nullable final Script condition, Weight weight) {
this.acoustic = acoustic;
this.weight = weight;
this.conditions = condition != null ? condition : Script.TRUE;
}

@NotNull
@Override
public int getWeight() {
public Weight getWeight() {
return this.weight;
}

@Override
public ISoundFactory getItem() {
public ISoundFactory data() {
return getAcoustic();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.minecraft.network.chat.TextColor;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.Music;
import net.minecraft.util.random.Weight;
import net.minecraft.world.level.biome.Biome;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -242,7 +243,7 @@ public void update(final BiomeConfigRule entry) {
targetCollection = this.loopSounds;
}
case MUSIC, MOOD, ADDITION -> {
final int weight = sr.weight();
final Weight weight = sr.weight();
acousticEntry = new AcousticEntry(factory, sr.conditions(), weight);

if (sr.type() == SoundEventType.ADDITION)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.random.Weight;
import org.orecruncher.dsurround.config.SoundEventType;
import org.orecruncher.dsurround.lib.IdentityUtils;
import org.orecruncher.dsurround.lib.scripting.Script;

public record AcousticConfig(
ResourceLocation factory,
Script conditions,
Integer weight,
Weight weight,
SoundEventType type) {

private static final Weight DEFAULT_WEIGHT = Weight.of(10);

public static final Codec<AcousticConfig> CODEC = RecordCodecBuilder.create((instance) ->
instance.group(
IdentityUtils.CODEC.fieldOf("factory").forGetter(AcousticConfig::factory),
Script.CODEC.optionalFieldOf("conditions", Script.TRUE).forGetter(AcousticConfig::conditions),
Codec.intRange(0, Integer.MAX_VALUE).optionalFieldOf("weight", 10).forGetter(AcousticConfig::weight),
Weight.CODEC.optionalFieldOf("weight", DEFAULT_WEIGHT).forGetter(AcousticConfig::weight),
SoundEventType.CODEC.optionalFieldOf("type", SoundEventType.LOOP).forGetter(AcousticConfig::type)
).apply(instance, AcousticConfig::new));
}
47 changes: 11 additions & 36 deletions common/src/main/java/org/orecruncher/dsurround/lib/WeightTable.java
Original file line number Diff line number Diff line change
@@ -1,69 +1,44 @@
package org.orecruncher.dsurround.lib;

import org.orecruncher.dsurround.lib.collections.ObjectArray;
import net.minecraft.util.random.WeightedEntry;
import net.minecraft.util.random.WeightedRandom;
import org.orecruncher.dsurround.lib.random.IRandomizer;
import org.orecruncher.dsurround.lib.random.Randomizer;

import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

/**
* Classic WeightTable for random weighted selection.
*/
@SuppressWarnings("unused")
public class WeightTable {

public static <T> Optional<T> makeSelection(final Stream<? extends IItem<T>> inputStream) {
return makeSelection(inputStream.toList(), Randomizer.current());
return makeSelection(inputStream, Randomizer.current());
}

public static <T> Optional<T> makeSelection(final Stream<? extends IItem<T>> inputStream, IRandomizer randomizer) {
return makeSelection(inputStream.toList(), randomizer);
}

public static <T> Optional<T> makeSelection(final Collection<? extends IItem<T>> selections) {
return makeSelection(selections, Randomizer.current());
}

public static <T> Optional<T> makeSelection(final Collection<? extends IItem<T>> selections, IRandomizer randomizer) {
public static <T> Optional<T> makeSelection(final List<? extends IItem<T>> selections, IRandomizer randomizer) {
if (selections.isEmpty())
return Optional.empty();

if (selections.size() == 1) {
T theItem;
if (selections instanceof List<? extends IItem<T>> theList)
theItem = theList.getFirst().getItem();
else if (selections instanceof ObjectArray<? extends IItem<T>> theArray)
theItem = theArray.getFirst().getItem();
else
theItem = selections.iterator().next().getItem();
return Optional.of(theItem);
}
if (selections.size() == 1)
return Optional.of(selections.getFirst().data());

int totalWeight = WeightedRandom.getTotalWeight(selections);

int totalWeight = 0;
for (var e : selections)
totalWeight += e.getWeight();
if (totalWeight == 0)
return Optional.empty();

int targetWeight = randomizer.nextInt(totalWeight);

for (var e : selections) {
targetWeight -= e.getWeight();
if (targetWeight < 0)
return Optional.of(e.getItem());
}

// Shouldn't get here
throw new RuntimeException("Bad weight table - ran off the end");
return WeightedRandom.getWeightedItem(selections, targetWeight).map(IItem::data);
}

public interface IItem<T> {

int getWeight();

T getItem();
public interface IItem<T> extends WeightedEntry {
T data();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ private void remove0(final int idx) {

@SuppressWarnings("unchecked")
@Override
public boolean removeIf(final Predicate<? super T> pred) {
public boolean removeIf(@NotNull final Predicate<? super T> filter) {
boolean result = false;
for (int i = this.insertionIdx - 1; i >= 0; i--) {
final T t = (T) this.data[i];
if (pred.test(t)) {
if (filter.test(t)) {
result = true;
this.remove0(i);
}
Expand Down Expand Up @@ -117,7 +117,7 @@ public boolean contains(final Object o) {
return result;
}

@SuppressWarnings({"unchecked", "hiding"})
@SuppressWarnings({"unchecked", "TypeParameterHidesVisibleType"})
@Override
public <T> T @NotNull [] toArray(final T[] a) {
// From ArrayList impl
Expand Down Expand Up @@ -173,7 +173,7 @@ public boolean removeAll(final Collection<?> c) {
}

@Override
public boolean retainAll(final Collection<?> c) {
public boolean retainAll(@NotNull final Collection<?> c) {
return this.removeIf(entry -> !c.contains(entry));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,18 @@ private void renderFog(FogRenderer.FogData data, float renderDistance, float par
RenderSystem.setShaderFogStart(this.lastData.start);
RenderSystem.setShaderFogEnd(this.lastData.end);
RenderSystem.setShaderFogShape(this.lastData.shape);
} else {
// Preserve for diagnostic trace even though action was not taken
this.lastData = data;
}
}

@Override
protected void gatherDiagnostics(CollectDiagnosticsEvent event) {
if (this.fogCalculator.enabled())
event.add(CollectDiagnosticsEvent.Section.Systems, "Fog: %f/%f, %s, %s".formatted(this.lastData.start, this.lastData.end, this.lastData.shape, this.lastData.mode));
else
event.add(CollectDiagnosticsEvent.Section.Systems, "Fog: DISABLED");
var text = "Fog: %f/%f, %s, %s ".formatted(this.lastData.start, this.lastData.end, this.lastData.shape, this.lastData.mode);
var disabledText = this.fogCalculator.getDisabledText();
if (disabledText.isPresent())
text += disabledText.get();
event.add(CollectDiagnosticsEvent.Section.Systems, text);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class BiomeFogRangeCalculator extends VanillaFogRangeCalculator {
private float targetScale;

public BiomeFogRangeCalculator(IBiomeLibrary biomeLibrary, Configuration.FogOptions fogOptions) {
super("BiomeFogRangeCalculator", fogOptions);
super("Biome", fogOptions);
this.biomeLibrary = biomeLibrary;
this.activeScale = this.targetScale = 0F;
this.lastBlockPos = BlockPos.ZERO;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import org.orecruncher.dsurround.lib.logging.ModLog;
import org.orecruncher.dsurround.lib.seasons.ISeasonalInformation;

import java.util.Optional;
import java.util.stream.Collectors;

public class HolisticFogRangeCalculator implements IFogRangeCalculator {

protected final IModLog logger;
Expand Down Expand Up @@ -78,4 +81,18 @@ public void tick() {
public void disconnect() {
this.calculators.forEach(IFogRangeCalculator::disconnect);
}

public Optional<String> getDisabledText() {
if (!this.enabled()) {
return Optional.of("(ALL DISABLED)");
}

var result = this.calculators.stream().filter(e -> !e.enabled()).map(IFogRangeCalculator::getName).collect(Collectors.joining(", "));

if (result.isEmpty()) {
return Optional.empty();
}

return Optional.of("(DISABLED: " + result + ")");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,45 @@

import net.minecraft.client.renderer.FogRenderer;
import net.minecraft.util.Mth;
import net.minecraft.util.random.WeightedEntry;
import net.minecraft.util.random.WeightedRandom;
import org.jetbrains.annotations.NotNull;
import org.orecruncher.dsurround.Configuration;
import org.orecruncher.dsurround.lib.GameUtils;
import org.orecruncher.dsurround.lib.MinecraftClock;
import org.orecruncher.dsurround.lib.random.Randomizer;
import org.orecruncher.dsurround.lib.seasons.ISeasonalInformation;
import org.orecruncher.dsurround.lib.WeightTable;

import java.util.List;

public class MorningFogRangeCalculator extends VanillaFogRangeCalculator {

private record FogDensityEntry(int weight, FogDensity density) implements WeightTable.IItem<FogDensity> {
@Override
public int getWeight() {
return this.weight();
}
@Override
public FogDensity getItem() {
return this.density();
}
}
private static final List<WeightedEntry.Wrapper<FogDensity>> SPRING_FOG = List.of(
WeightedEntry.wrap(FogDensity.NORMAL, 30),
WeightedEntry.wrap(FogDensity.MEDIUM, 20),
WeightedEntry.wrap(FogDensity.HEAVY, 10));

private static final List<WeightedEntry.Wrapper<FogDensity>> SUMMER_FOG = List.of(
WeightedEntry.wrap(FogDensity.LIGHT, 20),
WeightedEntry.wrap(FogDensity.NONE, 10));

private static final List<WeightedEntry.Wrapper<FogDensity>> AUTUMN_FOG = List.of(
WeightedEntry.wrap(FogDensity.NORMAL, 10),
WeightedEntry.wrap(FogDensity.MEDIUM, 20),
WeightedEntry.wrap(FogDensity.HEAVY, 10));

private static final FogDensityEntry[] SPRING_FOG = {
new FogDensityEntry(30, FogDensity.NORMAL),
new FogDensityEntry(20, FogDensity.MEDIUM),
new FogDensityEntry(10, FogDensity.HEAVY)
};

private static final FogDensityEntry[] SUMMER_FOG = {
new FogDensityEntry(20, FogDensity.LIGHT),
new FogDensityEntry(10, FogDensity.NONE)
};

private static final FogDensityEntry[] AUTUMN_FOG = {
new FogDensityEntry(10, FogDensity.NORMAL),
new FogDensityEntry(20, FogDensity.MEDIUM),
new FogDensityEntry(10, FogDensity.HEAVY)
};

private static final FogDensityEntry[] WINTER_FOG = {
new FogDensityEntry(20, FogDensity.LIGHT),
new FogDensityEntry(20, FogDensity.NORMAL),
new FogDensityEntry(10, FogDensity.MEDIUM)
};
private static final List<WeightedEntry.Wrapper<FogDensity>> WINTER_FOG = List.of(
WeightedEntry.wrap(FogDensity.LIGHT, 20),
WeightedEntry.wrap(FogDensity.NORMAL, 20),
WeightedEntry.wrap(FogDensity.MEDIUM, 10));

protected final ISeasonalInformation seasonInfo;
protected final MinecraftClock clock;
protected int fogDay = -1;
protected FogDensity type = FogDensity.NONE;

public MorningFogRangeCalculator(ISeasonalInformation seasonInfo, Configuration.FogOptions fogOptions) {
super("MorningFogRangeCalculator", fogOptions);
super("Morning", fogOptions);
this.seasonInfo = seasonInfo;
this.clock = new MinecraftClock();
}
Expand Down Expand Up @@ -112,7 +99,7 @@ private float getCelestialAngleDegrees() {

@NotNull
protected FogDensity getFogType() {
FogDensityEntry[] selections;
List<WeightedEntry.Wrapper<FogDensity>> selections;
var clientLevel = GameUtils.getWorld().orElseThrow();
if (this.seasonInfo.isSpring(clientLevel))
selections = SPRING_FOG;
Expand All @@ -126,6 +113,8 @@ else if (this.seasonInfo.isWinter(clientLevel))
// Shouldn't get here, but...
return FogDensity.NONE;

return WeightTable.makeSelection(List.of(selections)).orElseThrow();
var totalWeight = WeightedRandom.getTotalWeight(selections);
var targetWeight = Randomizer.current().nextInt(totalWeight);
return WeightedRandom.getWeightedItem(selections, targetWeight).map(WeightedEntry.Wrapper::data).orElseThrow();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class WeatherFogRangeCalculator extends VanillaFogRangeCalculator {
protected static final float END_IMPACT = 0.4F;

protected WeatherFogRangeCalculator(Configuration.FogOptions fogOptions) {
super("WeatherFogRangeCalculator", fogOptions);
super("Weather", fogOptions);
}

@Override
Expand Down
1 change: 0 additions & 1 deletion common/src/main/resources/dsurround.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"core.MixinEntity",
"core.MixinEntityArrow",
"core.MixinFogRenderer",
"core.MixinGui",
"core.MixinLivingEntity",
"core.MixinParticleManager",
"core.MixinRainSplashParticle",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.orecruncher.dsurround.mixins.core;
package org.orecruncher.fabric.mixins;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
Expand Down
Loading

0 comments on commit 17ea498

Please sign in to comment.