generated from QuiltMC/quilt-template-mod
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
begin rewrite of graphing logic to be more object-oriented
- Loading branch information
Showing
17 changed files
with
491 additions
and
238 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
42 changes: 0 additions & 42 deletions
42
src/main/java/net/pixaurora/janerator/config/GrapherFactory.java
This file was deleted.
Oops, something went wrong.
87 changes: 0 additions & 87 deletions
87
src/main/java/net/pixaurora/janerator/graphing/ConfiguredGraphLogic.java
This file was deleted.
Oops, something went wrong.
75 changes: 75 additions & 0 deletions
75
src/main/java/net/pixaurora/janerator/graphing/ConfiguredGrapherSettings.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 net.pixaurora.janerator.graphing; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import org.mariuszgromada.math.mxparser.License; | ||
import org.mariuszgromada.math.mxparser.mXparser; | ||
|
||
import com.mojang.serialization.Codec; | ||
import com.mojang.serialization.codecs.RecordCodecBuilder; | ||
|
||
import net.pixaurora.janerator.graphing.variable.InputVariable; | ||
import net.pixaurora.janerator.graphing.variable.Variable; | ||
|
||
public class ConfiguredGrapherSettings { | ||
public static final Codec<ConfiguredGrapherSettings> CODEC = RecordCodecBuilder.create( | ||
instance -> instance.group( | ||
Codec.STRING.listOf().fieldOf("variable_definitions").forGetter(ConfiguredGrapherSettings::getVariableDefinitions), | ||
Codec.STRING.fieldOf("return_statement").forGetter(ConfiguredGrapherSettings::getReturnStatement) | ||
) | ||
.apply(instance, ConfiguredGrapherSettings::new) | ||
); | ||
|
||
static { | ||
License.iConfirmNonCommercialUse("Rina Shaw <[email protected]>"); | ||
|
||
mXparser.disableImpliedMultiplicationMode(); // Implied multiplication breaks searching for missing user-defined arguments | ||
mXparser.disableAlmostIntRounding(); | ||
mXparser.disableCanonicalRounding(); | ||
mXparser.disableUlpRounding(); | ||
} | ||
|
||
private List<String> variableDefinitions; | ||
private String returnStatement; | ||
|
||
private List<Variable> variables; | ||
|
||
public ConfiguredGrapherSettings(List<String> variableDefinitions, String returnStatement) { | ||
this.variableDefinitions = variableDefinitions; | ||
this.returnStatement = returnStatement; | ||
|
||
this.variables = new ArrayList<>(this.variableDefinitions.size() + 1); | ||
this.variables.add(new InputVariable("x")); | ||
this.variables.add(new InputVariable("z")); | ||
|
||
Map<String, Variable> variableTable = new HashMap<>( | ||
this.variables.stream() | ||
.collect(Collectors.toMap(Variable::getName, variable -> variable)) | ||
); | ||
|
||
for (String definition : variableDefinitions) { | ||
Variable nextVariable = Variable.fromStringDefinition(variableTable, definition); | ||
|
||
this.variables.add(nextVariable); | ||
variableTable.put(nextVariable.getName(), nextVariable); | ||
} | ||
|
||
this.variables.add(Variable.fromStringDefinition(variableTable, "returnValue = " + returnStatement)); | ||
} | ||
|
||
public List<String> getVariableDefinitions() { | ||
return this.variableDefinitions; | ||
} | ||
|
||
public String getReturnStatement() { | ||
return this.returnStatement; | ||
} | ||
|
||
public List<Variable> getVariables() { | ||
return this.variables; | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
src/main/java/net/pixaurora/janerator/graphing/Grapher.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,88 @@ | ||
package net.pixaurora.janerator.graphing; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.stream.IntStream; | ||
|
||
import net.pixaurora.janerator.graphing.instruction.Instruction; | ||
import net.pixaurora.janerator.graphing.variable.IndependentVariable; | ||
import net.pixaurora.janerator.graphing.variable.InputVariable; | ||
import net.pixaurora.janerator.graphing.variable.Variable; | ||
|
||
public class Grapher { | ||
private List<Double> startingVariables; | ||
private List<Instruction> intermediaryInstructions; | ||
private int returnIndex; | ||
|
||
public Grapher(List<Double> startingVariables, List<Instruction> instructionsToApply, int returnIndex) { | ||
this.startingVariables = startingVariables; | ||
this.intermediaryInstructions = instructionsToApply; | ||
this.returnIndex = returnIndex; | ||
} | ||
|
||
public static Grapher fromConfig(ConfiguredGrapherSettings config) { | ||
AtomicInteger count = new AtomicInteger(-1); | ||
Map<String, Integer> nameToIndex = new HashMap<>( | ||
Map.of( | ||
"x", count.getAndIncrement(), | ||
"z", count.getAndIncrement() | ||
) | ||
); | ||
|
||
List<Variable> allVariables = config.getVariables(); | ||
List<Variable> variableDefinitions = config.getVariables().stream() | ||
.filter(variable -> ! (variable instanceof InputVariable)) | ||
.toList(); | ||
|
||
|
||
for (Variable variable : variableDefinitions) { | ||
nameToIndex.computeIfAbsent(variable.getName(), name -> count.getAndIncrement()); | ||
} | ||
|
||
List<Instruction> instructions = new ArrayList<>(); | ||
List<Double> startingVariables = IntStream.range(0, count.get()) | ||
.mapToDouble(value -> 0.0) | ||
.boxed() | ||
.toList(); | ||
|
||
for (Variable variable : variableDefinitions) { | ||
int setIndex = nameToIndex.get(variable.getName()); | ||
int[] accessIndexes = variable.getRequiredVariables().stream() | ||
.map(Variable::getName) | ||
.map(nameToIndex::get) | ||
.mapToInt(Integer::valueOf) | ||
.toArray(); | ||
|
||
Instruction instruction = variable.createInstruction(accessIndexes, setIndex); | ||
|
||
boolean definedOnce = allVariables.stream() | ||
.filter(other -> variable.getName() == other.getName()) | ||
.count() < 1; | ||
|
||
if ( | ||
variable instanceof IndependentVariable && definedOnce | ||
) { | ||
instruction.execute(startingVariables); | ||
} else { | ||
instructions.add(instruction); | ||
} | ||
} | ||
|
||
return new Grapher(startingVariables, instructions, nameToIndex.get("returnValue")); | ||
} | ||
|
||
public boolean isShaded(double x, double z) { | ||
List<Double> variables = new ArrayList<>(this.startingVariables); | ||
variables.set(0, x); | ||
variables.set(0, z); | ||
|
||
for (Instruction instruction : this.intermediaryInstructions) { | ||
instruction.execute(variables); | ||
} | ||
|
||
return variables.get(this.returnIndex) == 0.0; | ||
} | ||
} |
Oops, something went wrong.