-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
118 changed files
with
2,240 additions
and
1,709 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
24 changes: 0 additions & 24 deletions
24
generator/src/main/java/com/faforever/neroxis/generator/WeightedConstrainedOptions.java
This file was deleted.
Oops, something went wrong.
14 changes: 13 additions & 1 deletion
14
generator/src/main/java/com/faforever/neroxis/generator/WeightedOption.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,3 +1,15 @@ | ||
package com.faforever.neroxis.generator; | ||
|
||
public record WeightedOption<T>(T option, float weight) {} | ||
import java.util.Objects; | ||
|
||
public record WeightedOption<T>( | ||
T option, | ||
float weight | ||
) { | ||
public WeightedOption { | ||
Objects.requireNonNull(option, "Option cannot be null"); | ||
if (weight <= 0) { | ||
throw new IllegalArgumentException("Weight must be greater than 0"); | ||
} | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
generator/src/main/java/com/faforever/neroxis/generator/WeightedOptionsWithFallback.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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.faforever.neroxis.generator; | ||
|
||
import com.faforever.neroxis.generator.util.HasParameterConstraints; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Random; | ||
import java.util.function.Predicate; | ||
|
||
public sealed interface WeightedOptionsWithFallback<T> { | ||
|
||
@SafeVarargs | ||
static <T extends HasParameterConstraints> WeightedOptionsWithFallback<T> of(T fallback, WeightedOption<T>... options) { | ||
if (options.length == 0) { | ||
return new WeightedOptionsWithFallback.Single<>(fallback); | ||
} | ||
|
||
return new WeightedOptionsWithFallback.Multi<>(fallback, List.of(options)); | ||
} | ||
|
||
default T select(Random random) { | ||
return select(random, option -> true); | ||
} | ||
|
||
default T select(Random random, Predicate<? super T> filter) { | ||
return switch (this) { | ||
case Single(T option) -> option; | ||
case Multi(T fallbackOption, List<WeightedOption<T>> options) -> { | ||
List<WeightedOption<T>> matchingOptions = options.stream() | ||
.filter(weightedOption -> filter.test( | ||
weightedOption.option())) | ||
.toList(); | ||
if (matchingOptions.isEmpty()) { | ||
yield fallbackOption; | ||
} | ||
|
||
double[] weights = matchingOptions.stream().mapToDouble(WeightedOption::weight).toArray(); | ||
double[] cumulativeWeights = new double[weights.length]; | ||
double sum = 0; | ||
for (int i = 0; i < weights.length; i++) { | ||
sum += weights[i]; | ||
cumulativeWeights[i] = sum; | ||
} | ||
double value = random.nextDouble(sum); | ||
int index = Arrays.binarySearch(cumulativeWeights, value); | ||
if (index < 0) { | ||
index = (index + 1) * -1; | ||
} | ||
yield matchingOptions.get(index).option(); | ||
} | ||
}; | ||
} | ||
|
||
record Multi<T>( | ||
T fallbackOption, | ||
List<WeightedOption<T>> options | ||
) implements WeightedOptionsWithFallback<T> { | ||
|
||
public Multi { | ||
Objects.requireNonNull(fallbackOption, "Fallback cannot be null"); | ||
if (options.size() < 2) { | ||
throw new IllegalArgumentException("At least two options must be provided"); | ||
} | ||
options = List.copyOf(options); | ||
} | ||
|
||
} | ||
|
||
record Single<T>(T option) implements WeightedOptionsWithFallback<T> { | ||
public Single { | ||
Objects.requireNonNull(option, "Option cannot be null"); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.