-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Weight table cleanup; fog diagnostics; fix neoforge GUI layer registr…
…ation
- Loading branch information
OreCruncher
committed
Jan 2, 2025
1 parent
a7f86a1
commit 17ea498
Showing
15 changed files
with
113 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 11 additions & 36 deletions
47
common/src/main/java/org/orecruncher/dsurround/lib/WeightTable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ncher/dsurround/mixins/core/MixinGui.java → ...g/orecruncher/fabric/mixins/MixinGui.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.