reworked weight system

This commit is contained in:
CreepyCre 2021-02-02 13:15:39 +01:00
parent 5b987f70aa
commit 6607499177
11 changed files with 70 additions and 75 deletions

View file

@ -14,12 +14,10 @@ public class PocketTemplateV2 {
private static final Logger LOGGER = LogManager.getLogger(); private static final Logger LOGGER = LogManager.getLogger();
private static final boolean replacingPlaceholders = false; private static final boolean replacingPlaceholders = false;
private final Schematic schematic; private final Schematic schematic;
private final int size;
private final String id; private final String id;
public PocketTemplateV2(Schematic schematic, int size, String id) { public PocketTemplateV2(Schematic schematic, String id) {
this.schematic = schematic; this.schematic = schematic;
this.size = size;
this.id = id; this.id = id;
} }

View file

@ -52,14 +52,14 @@ public class SchematicV2Handler {
LOGGER.info("Loaded schematics in {} seconds", System.currentTimeMillis() - startTime); LOGGER.info("Loaded schematics in {} seconds", System.currentTimeMillis() - startTime);
} }
public void loadSchematic(Identifier templateID, String group, int size, String id) { public void loadSchematic(Identifier templateID, String group, String id) {
try { try {
if (templates.containsKey(templateID)) return; if (templates.containsKey(templateID)) return;
Path basePath = Paths.get(SchematicV2Handler.class.getResource(String.format("/data/dimdoors/pockets/schematic/v2/%s/", group)).toURI()); Path basePath = Paths.get(SchematicV2Handler.class.getResource(String.format("/data/dimdoors/pockets/schematic/v2/%s/", group)).toURI());
Path schemPath = basePath.resolve(id + ".schem"); Path schemPath = basePath.resolve(id + ".schem");
CompoundTag schemTag = NbtIo.readCompressed(Files.newInputStream(schemPath)); CompoundTag schemTag = NbtIo.readCompressed(Files.newInputStream(schemPath));
Schematic schematic = Schematic.fromTag(schemTag); Schematic schematic = Schematic.fromTag(schemTag);
PocketTemplateV2 template = new PocketTemplateV2(schematic, size, id); PocketTemplateV2 template = new PocketTemplateV2(schematic, id);
templates.put(templateID, template); templates.put(templateID, template);
} catch (URISyntaxException | IOException e) { } catch (URISyntaxException | IOException e) {
LOGGER.error("Could not load schematic!", e); LOGGER.error("Could not load schematic!", e);

View file

@ -14,21 +14,54 @@ import org.dimdev.dimdoors.pockets.TemplateUtils;
import org.dimdev.dimdoors.pockets.virtual.modifier.Modifier; import org.dimdev.dimdoors.pockets.virtual.modifier.Modifier;
import org.dimdev.dimdoors.util.Location; import org.dimdev.dimdoors.util.Location;
import org.dimdev.dimdoors.util.PocketGenerationParameters; import org.dimdev.dimdoors.util.PocketGenerationParameters;
import org.dimdev.dimdoors.util.math.StringEquationParser;
import org.dimdev.dimdoors.world.pocket.Pocket; import org.dimdev.dimdoors.world.pocket.Pocket;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
public abstract class VirtualGeneratorPocket extends VirtualSingularPocket { public abstract class VirtualGeneratorPocket extends VirtualSingularPocket {
private static final Logger LOGGER = LogManager.getLogger(); private static final Logger LOGGER = LogManager.getLogger();
private static final String defaultWeightEquation = "5"; // TODO: make config
private static final int fallbackWeight = 5; // TODO: make config
private final List<Modifier> modifierList = new ArrayList<>(); private final List<Modifier> modifierList = new ArrayList<>();
private String weight;
private StringEquationParser.Equation weightEquation;
public VirtualGeneratorPocket() { }
public VirtualGeneratorPocket(String weight) {
this.weight = weight;
try {
this.weightEquation = StringEquationParser.parse(weight);
} catch (StringEquationParser.EquationParseException e) {
LOGGER.error("Could not parse weight equation \"" + weight + "\", defaulting to weight \"5\"");
this.weightEquation = (stringDoubleMap -> 5d);
}
}
public VirtualGeneratorPocket fromTag(CompoundTag tag) { public VirtualGeneratorPocket fromTag(CompoundTag tag) {
if (!tag.contains("modifiers")) return this; this.weight = tag.contains("weight") ? tag.getString("weight") : "5";
ListTag modifiersTag = tag.getList("modifiers", 10); try {
for (int i = 0; i < modifiersTag.size(); i++) { this.weightEquation = StringEquationParser.parse(weight);
modifierList.add(Modifier.deserialize(modifiersTag.getCompound(i))); } catch (StringEquationParser.EquationParseException e) {
LOGGER.error("Could not parse weight equation \"" + weight + "\", defaulting to default weight equation \"" + defaultWeightEquation + "\"", e);
try {
this.weightEquation = StringEquationParser.parse(defaultWeightEquation);
} catch (StringEquationParser.EquationParseException equationParseException) {
LOGGER.error("Could not parse default weight equation \"" + defaultWeightEquation + "\", defaulting to fallback weight \"" + fallbackWeight + "\"", equationParseException);
this.weightEquation = stringDoubleMap -> 5d;
}
}
if (tag.contains("modifiers")) {
ListTag modifiersTag = tag.getList("modifiers", 10);
for (int i = 0; i < modifiersTag.size(); i++) {
modifierList.add(Modifier.deserialize(modifiersTag.getCompound(i)));
}
} }
return this; return this;
} }
@ -36,6 +69,8 @@ public abstract class VirtualGeneratorPocket extends VirtualSingularPocket {
public CompoundTag toTag(CompoundTag tag) { public CompoundTag toTag(CompoundTag tag) {
super.toTag(tag); super.toTag(tag);
if (!weight.equals("5")) tag.putString("weight", weight);
ListTag modifiersTag = new ListTag(); ListTag modifiersTag = new ListTag();
for (Modifier modifier : modifierList) { for (Modifier modifier : modifierList) {
modifiersTag.add(modifier.toTag(new CompoundTag())); modifiersTag.add(modifier.toTag(new CompoundTag()));
@ -75,4 +110,9 @@ public abstract class VirtualGeneratorPocket extends VirtualSingularPocket {
TemplateUtils.registerRifts(rifts, parameters.getLinkTo(), parameters.getLinkProperties(), pocket); TemplateUtils.registerRifts(rifts, parameters.getLinkTo(), parameters.getLinkProperties(), pocket);
pocket.virtualLocation = parameters.getSourceVirtualLocation(); //TODO: this makes very little sense pocket.virtualLocation = parameters.getSourceVirtualLocation(); //TODO: this makes very little sense
} }
@Override
public double getWeight(PocketGenerationParameters parameters){
return this.weightEquation.apply(parameters.toVariableMap(new HashMap<>()));
}
} }

View file

@ -46,7 +46,7 @@ public class VirtualPocketList extends WeightedList<VirtualPocket, PocketGenerat
} }
@Override @Override
public int getWeight(PocketGenerationParameters parameters) { public double getWeight(PocketGenerationParameters parameters) {
return peekNextRandomWeighted(parameters).getWeight(parameters); return peekNextRandomWeighted(parameters).getWeight(parameters);
} }
} }

View file

@ -52,9 +52,6 @@ public abstract class VirtualSingularPocket implements VirtualPocket {
public abstract Pocket prepareAndPlacePocket(PocketGenerationParameters parameters); public abstract Pocket prepareAndPlacePocket(PocketGenerationParameters parameters);
public abstract String toString();
// TODO: are equals() and hashCode() necessary?
public abstract VirtualSingularPocketType<? extends VirtualSingularPocket> getType(); public abstract VirtualSingularPocketType<? extends VirtualSingularPocket> getType();
public abstract String getKey(); public abstract String getKey();

View file

@ -24,13 +24,9 @@ import org.dimdev.dimdoors.block.ModBlocks;
import org.dimdev.dimdoors.block.entity.DetachedRiftBlockEntity; import org.dimdev.dimdoors.block.entity.DetachedRiftBlockEntity;
import org.dimdev.dimdoors.block.entity.ModBlockEntityTypes; import org.dimdev.dimdoors.block.entity.ModBlockEntityTypes;
import org.dimdev.dimdoors.pockets.PocketGroup; import org.dimdev.dimdoors.pockets.PocketGroup;
import org.dimdev.dimdoors.pockets.TemplateUtils;
import org.dimdev.dimdoors.pockets.virtual.VirtualGeneratorPocket; import org.dimdev.dimdoors.pockets.virtual.VirtualGeneratorPocket;
import org.dimdev.dimdoors.pockets.virtual.VirtualSingularPocket; import org.dimdev.dimdoors.pockets.virtual.VirtualSingularPocket;
import org.dimdev.dimdoors.rift.registry.LinkProperties;
import org.dimdev.dimdoors.rift.targets.PocketEntranceMarker; import org.dimdev.dimdoors.rift.targets.PocketEntranceMarker;
import org.dimdev.dimdoors.rift.targets.VirtualTarget;
import org.dimdev.dimdoors.util.Location;
import org.dimdev.dimdoors.util.PocketGenerationParameters; import org.dimdev.dimdoors.util.PocketGenerationParameters;
import org.dimdev.dimdoors.util.TeleportUtil; import org.dimdev.dimdoors.util.TeleportUtil;
import org.dimdev.dimdoors.world.level.DimensionalRegistry; import org.dimdev.dimdoors.world.level.DimensionalRegistry;
@ -38,9 +34,7 @@ import org.dimdev.dimdoors.world.pocket.Pocket;
import org.dimdev.dimdoors.world.pocket.VirtualLocation; import org.dimdev.dimdoors.world.pocket.VirtualLocation;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class ChunkGenerator extends VirtualGeneratorPocket { public class ChunkGenerator extends VirtualGeneratorPocket {
@ -51,10 +45,8 @@ public class ChunkGenerator extends VirtualGeneratorPocket {
private Vec3i size; private Vec3i size;
private Vec3i offset; private Vec3i offset;
private int virtualYOffset; private int virtualYOffset;
private int weight;
public ChunkGenerator() { public ChunkGenerator() {
} }
@Override @Override
@ -68,7 +60,6 @@ public class ChunkGenerator extends VirtualGeneratorPocket {
this.offset = new Vec3i(temp[0], temp[1], temp[2]); this.offset = new Vec3i(temp[0], temp[1], temp[2]);
this.virtualYOffset = tag.contains("virtual_y_offset") ? tag.getInt("virtual_y_offset") : 0; this.virtualYOffset = tag.contains("virtual_y_offset") ? tag.getInt("virtual_y_offset") : 0;
this.weight = tag.contains("weight") ? tag.getInt("weight") : 5;
return this; return this;
} }
@ -80,7 +71,6 @@ public class ChunkGenerator extends VirtualGeneratorPocket {
tag.putIntArray("size", new int[]{this.size.getX(), this.size.getY(), this.size.getZ()}); tag.putIntArray("size", new int[]{this.size.getX(), this.size.getY(), this.size.getZ()});
tag.putIntArray("offset", new int[]{this.offset.getX(), this.offset.getY(), this.offset.getZ()}); tag.putIntArray("offset", new int[]{this.offset.getX(), this.offset.getY(), this.offset.getZ()});
tag.putInt("virtual_y_offset", this.virtualYOffset); tag.putInt("virtual_y_offset", this.virtualYOffset);
tag.putInt("weight", this.weight);
return tag; return tag;
} }
@ -92,8 +82,6 @@ public class ChunkGenerator extends VirtualGeneratorPocket {
public Pocket prepareAndPlacePocket(PocketGenerationParameters parameters) { public Pocket prepareAndPlacePocket(PocketGenerationParameters parameters) {
ServerWorld world = parameters.getWorld(); ServerWorld world = parameters.getWorld();
VirtualLocation sourceVirtualLocation = parameters.getSourceVirtualLocation(); VirtualLocation sourceVirtualLocation = parameters.getSourceVirtualLocation();
VirtualTarget linkTo = parameters.getLinkTo();
LinkProperties linkProperties = parameters.getLinkProperties();
int ChunkSizeX = ((this.size.getX() >> 4) + (this.size.getX() % 16 == 0 ? 0 : 1)); int ChunkSizeX = ((this.size.getX() >> 4) + (this.size.getX() % 16 == 0 ? 0 : 1));
int ChunkSizeZ = ((this.size.getZ() >> 4) + (this.size.getZ() % 16 == 0 ? 0 : 1)); int ChunkSizeZ = ((this.size.getZ() >> 4) + (this.size.getZ() % 16 == 0 ? 0 : 1));
@ -199,11 +187,6 @@ public class ChunkGenerator extends VirtualGeneratorPocket {
return pocket; return pocket;
} }
@Override
public String toString() {
return null;
}
@Override @Override
public VirtualSingularPocketType<? extends VirtualSingularPocket> getType() { public VirtualSingularPocketType<? extends VirtualSingularPocket> getType() {
return VirtualSingularPocketType.CHUNK; return VirtualSingularPocketType.CHUNK;
@ -214,11 +197,6 @@ public class ChunkGenerator extends VirtualGeneratorPocket {
return KEY; return KEY;
} }
@Override
public int getWeight(PocketGenerationParameters parameters) {
return this.weight;
}
private static class ChunkRegionHack extends ChunkRegion { // Please someone tell me if there is a better way private static class ChunkRegionHack extends ChunkRegion { // Please someone tell me if there is a better way
ChunkRegionHack(ServerWorld world, List<Chunk> chunks) { ChunkRegionHack(ServerWorld world, List<Chunk> chunks) {
super(world, chunks); super(world, chunks);

View file

@ -26,47 +26,33 @@ public class SchematicGenerator extends VirtualGeneratorPocket {
).apply(instance, SchematicGenerator::new)); ).apply(instance, SchematicGenerator::new));
*/ */
private int size; private String id;
private String name;
private Identifier templateID; private Identifier templateID;
private int weight;
public SchematicGenerator() {} public SchematicGenerator() {}
public SchematicGenerator(int size, String name, int weight) { public SchematicGenerator(String id, String weight) {
this.size = size; super(weight);
this.name = name; this.id = id;
this.weight = weight;
this.templateID = new Identifier("dimdoors", name); this.templateID = new Identifier("dimdoors", id);
} }
public int getSize() { public String getId() {
return this.size; return this.id;
}
public String getName() {
return this.name;
} }
public Identifier getTemplateID() { public Identifier getTemplateID() {
return templateID; return templateID;
} }
@Override
public int getWeight(PocketGenerationParameters parameters){
return this.weight;
}
@Override @Override
public VirtualGeneratorPocket fromTag(CompoundTag tag) { public VirtualGeneratorPocket fromTag(CompoundTag tag) {
super.fromTag(tag); super.fromTag(tag);
this.name = tag.getString("id"); this.id = tag.getString("id");
this.size = tag.getInt("size");
this.weight = tag.contains("weight") ? tag.getInt("weight") : 5;
this.templateID = new Identifier("dimdoors", name); this.templateID = new Identifier("dimdoors", id);
return this; return this;
} }
@ -74,15 +60,13 @@ public class SchematicGenerator extends VirtualGeneratorPocket {
public CompoundTag toTag(CompoundTag tag) { public CompoundTag toTag(CompoundTag tag) {
super.toTag(tag); super.toTag(tag);
tag.putString("id", this.name); tag.putString("id", this.id);
tag.putInt("size", this.size);
tag.putInt("weight", this.weight);
return tag; return tag;
} }
@Override @Override
public void init(PocketGroup group) { public void init(PocketGroup group) {
SchematicV2Handler.getInstance().loadSchematic(templateID, group.getGroup(), size, name); SchematicV2Handler.getInstance().loadSchematic(templateID, group.getGroup(), id);
} }
@Override @Override
@ -101,15 +85,6 @@ public class SchematicGenerator extends VirtualGeneratorPocket {
return pocket; return pocket;
} }
@Override
public String toString() {
return "PocketEntry{" +
"size=" + this.size +
", name='" + this.name + '\'' +
", weight=" + this.weight +
'}';
}
@Override @Override
public VirtualSingularPocketType<? extends VirtualSingularPocket> getType() { public VirtualSingularPocketType<? extends VirtualSingularPocket> getType() {
return VirtualSingularPocketType.SCHEMATIC; return VirtualSingularPocketType.SCHEMATIC;

View file

@ -107,7 +107,7 @@ public class DepthDependentSelector extends VirtualSingularPocket {
} }
@Override @Override
public int getWeight(PocketGenerationParameters parameters) { public double getWeight(PocketGenerationParameters parameters) {
return getNextPocket(parameters).getWeight(parameters); return getNextPocket(parameters).getWeight(parameters);
} }

View file

@ -5,6 +5,8 @@ import org.dimdev.dimdoors.rift.registry.LinkProperties;
import org.dimdev.dimdoors.rift.targets.VirtualTarget; import org.dimdev.dimdoors.rift.targets.VirtualTarget;
import org.dimdev.dimdoors.world.pocket.VirtualLocation; import org.dimdev.dimdoors.world.pocket.VirtualLocation;
import java.util.Map;
public class PocketGenerationParameters { public class PocketGenerationParameters {
private final ServerWorld world; private final ServerWorld world;
private final String group; private final String group;
@ -39,4 +41,9 @@ public class PocketGenerationParameters {
public LinkProperties getLinkProperties() { public LinkProperties getLinkProperties() {
return linkProperties; return linkProperties;
} }
public Map<String, Double> toVariableMap(Map<String, Double> stringDoubleMap) {
stringDoubleMap.put("depth", (double) this.sourceVirtualLocation.getDepth());
return stringDoubleMap;
}
} }

View file

@ -5,5 +5,5 @@ public interface Weighted<P> {
Should always return the same number if the same parameters are provided. Should always return the same number if the same parameters are provided.
returned number should always be >= 0 returned number should always be >= 0
*/ */
int getWeight(P parameters); double getWeight(P parameters);
} }

View file

@ -20,8 +20,8 @@ public class WeightedList<T extends Weighted<P>, P> extends ArrayList<T> {
private T getNextRandomWeighted(P parameters, boolean peek) { private T getNextRandomWeighted(P parameters, boolean peek) {
if (!peeked) { if (!peeked) {
int totalWeight = stream().mapToInt(weighted -> weighted.getWeight(parameters)).sum(); double totalWeight = stream().mapToDouble(weighted -> weighted.getWeight(parameters)).sum();
int cursor = random.nextInt(totalWeight); double cursor = random.nextDouble() * totalWeight;
for (T weighted : this) { for (T weighted : this) {
cursor -= weighted.getWeight(parameters); cursor -= weighted.getWeight(parameters);
if (cursor <= 0) { if (cursor <= 0) {