mirror of
https://github.com/array-in-a-matrix/brainwine.git
synced 2025-04-02 11:11:58 -04:00
Replaced WeightedList with a WeightedMap
This commit is contained in:
parent
e683f9fa11
commit
76c11deca9
11 changed files with 108 additions and 89 deletions
|
@ -14,7 +14,7 @@ import org.apache.logging.log4j.Logger;
|
|||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
|
||||
import brainwine.gameserver.util.WeightedList;
|
||||
import brainwine.gameserver.util.WeightedMap;
|
||||
import brainwine.gameserver.zone.Biome;
|
||||
import brainwine.shared.JsonHelper;
|
||||
|
||||
|
@ -66,7 +66,7 @@ public class LootManager {
|
|||
}
|
||||
|
||||
public Loot getRandomLoot(int level, Biome biome, String... categories) {
|
||||
WeightedList<Loot> weightedLoot = new WeightedList<>();
|
||||
WeightedMap<Loot> weightedLoot = new WeightedMap<>();
|
||||
List<Loot> eligibleLoot = getEligibleLoot(level, biome, categories);
|
||||
|
||||
for(Loot loot : eligibleLoot) {
|
||||
|
|
|
@ -10,7 +10,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
|
|||
|
||||
import brainwine.gameserver.GameServer;
|
||||
import brainwine.gameserver.item.Item;
|
||||
import brainwine.gameserver.util.WeightedList;
|
||||
import brainwine.gameserver.util.WeightedMap;
|
||||
import brainwine.gameserver.zone.Block;
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
|
@ -32,7 +32,7 @@ public class Prefab {
|
|||
private boolean mirrorable;
|
||||
|
||||
@JsonProperty("replace")
|
||||
private Map<Item, WeightedList<Item>> replacements = new HashMap<>();
|
||||
private Map<Item, WeightedMap<Item>> replacements = new HashMap<>();
|
||||
|
||||
@JsonProperty("corresponding_replace")
|
||||
private Map<Item, CorrespondingReplacement> correspondingReplacements = new HashMap<>();
|
||||
|
@ -98,7 +98,7 @@ public class Prefab {
|
|||
return metadata;
|
||||
}
|
||||
|
||||
public Map<Item, WeightedList<Item>> getReplacements() {
|
||||
public Map<Item, WeightedMap<Item>> getReplacements() {
|
||||
return replacements;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,63 +0,0 @@
|
|||
package brainwine.gameserver.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
|
||||
import io.netty.util.internal.ThreadLocalRandom;
|
||||
|
||||
public class WeightedList<T> {
|
||||
|
||||
private final List<T> entries = new ArrayList<>();
|
||||
|
||||
@JsonCreator
|
||||
public WeightedList(Map<T, Integer> map) {
|
||||
map.forEach((k, v) -> {
|
||||
addEntry(k, v);
|
||||
});
|
||||
}
|
||||
|
||||
@JsonCreator
|
||||
public WeightedList(List<T> list) {
|
||||
for(T t : list) {
|
||||
addEntry(t, 1);
|
||||
}
|
||||
}
|
||||
|
||||
public WeightedList() {}
|
||||
|
||||
public WeightedList<T> addEntry(T entry, int weight) {
|
||||
for(int i = 0; i < weight; i++) {
|
||||
entries.add(entry);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public T next() {
|
||||
return next((T)null);
|
||||
}
|
||||
|
||||
public T next(T def) {
|
||||
return next(ThreadLocalRandom.current(), def);
|
||||
}
|
||||
|
||||
public T next(Random random) {
|
||||
return next(random, null);
|
||||
}
|
||||
|
||||
public T next(Random random, T def) {
|
||||
if(entries.isEmpty()) {
|
||||
return def;
|
||||
}
|
||||
|
||||
return entries.get(random.nextInt(entries.size()));
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return entries.isEmpty();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,82 @@
|
|||
package brainwine.gameserver.util;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Random;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonValue;
|
||||
|
||||
import io.netty.util.internal.ThreadLocalRandom;
|
||||
|
||||
public class WeightedMap<T> {
|
||||
|
||||
@JsonValue
|
||||
private final Map<T, Integer> map = new HashMap<>();
|
||||
private int totalWeight;
|
||||
|
||||
public WeightedMap() {}
|
||||
|
||||
@JsonCreator
|
||||
public WeightedMap(Collection<T> entries) {
|
||||
for(T entry : entries) {
|
||||
addEntry(entry);
|
||||
}
|
||||
}
|
||||
|
||||
@JsonCreator
|
||||
public WeightedMap(Map<T, Integer> map) {
|
||||
map.forEach((entry, weight) -> {
|
||||
addEntry(entry, weight);
|
||||
});
|
||||
}
|
||||
|
||||
public WeightedMap<T> addEntry(T entry) {
|
||||
return addEntry(entry, 1);
|
||||
}
|
||||
|
||||
public WeightedMap<T> addEntry(T entry, int weight) {
|
||||
if(weight > 0) {
|
||||
map.put(entry, weight);
|
||||
totalWeight += weight;
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return map.isEmpty();
|
||||
}
|
||||
|
||||
public T next() {
|
||||
return next((T)null);
|
||||
}
|
||||
|
||||
public T next(T def) {
|
||||
return next(ThreadLocalRandom.current(), def);
|
||||
}
|
||||
|
||||
public T next(Random random) {
|
||||
return next(random, null);
|
||||
}
|
||||
|
||||
public T next(Random random, T def) {
|
||||
if(!map.isEmpty()) {
|
||||
int rolled = random.nextInt(totalWeight);
|
||||
|
||||
for(Entry<T, Integer> entry : map.entrySet()) {
|
||||
int weight = entry.getValue();
|
||||
|
||||
if(rolled < weight) {
|
||||
return entry.getKey();
|
||||
}
|
||||
|
||||
rolled -= weight;
|
||||
}
|
||||
}
|
||||
|
||||
return def;
|
||||
}
|
||||
}
|
|
@ -7,14 +7,14 @@ import java.util.Queue;
|
|||
|
||||
import brainwine.gameserver.item.Layer;
|
||||
import brainwine.gameserver.util.MathUtils;
|
||||
import brainwine.gameserver.util.WeightedList;
|
||||
import brainwine.gameserver.util.WeightedMap;
|
||||
import brainwine.gameserver.zone.gen.models.BlockPosition;
|
||||
import brainwine.gameserver.zone.gen.models.Cave;
|
||||
import brainwine.gameserver.zone.gen.models.StoneVariant;
|
||||
|
||||
public class CaveGenerator implements GeneratorTask {
|
||||
|
||||
private final WeightedList<StoneVariant> stoneVariants;
|
||||
private final WeightedMap<StoneVariant> stoneVariants;
|
||||
private final List<CaveDecorator> decorators;
|
||||
|
||||
public CaveGenerator(GeneratorConfig config) {
|
||||
|
@ -182,7 +182,7 @@ public class CaveGenerator implements GeneratorTask {
|
|||
}
|
||||
|
||||
private CaveDecorator getRandomEligibleDecorator(GeneratorContext ctx, int size, double depth) {
|
||||
WeightedList<CaveDecorator> list = new WeightedList<>();
|
||||
WeightedMap<CaveDecorator> list = new WeightedMap<>();
|
||||
|
||||
for(CaveDecorator decorator : decorators) {
|
||||
if(size >= decorator.getMinSize() && size <= decorator.getMaxSize() && depth >= decorator.getMinDepth() && depth <= decorator.getMaxDepth()) {
|
||||
|
|
|
@ -12,7 +12,7 @@ import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
|||
import brainwine.gameserver.item.Item;
|
||||
import brainwine.gameserver.prefab.Prefab;
|
||||
import brainwine.gameserver.util.Vector2i;
|
||||
import brainwine.gameserver.util.WeightedList;
|
||||
import brainwine.gameserver.util.WeightedMap;
|
||||
import brainwine.gameserver.zone.gen.models.BaseResourceType;
|
||||
import brainwine.gameserver.zone.gen.models.Deposit;
|
||||
import brainwine.gameserver.zone.gen.models.OreDeposit;
|
||||
|
@ -34,10 +34,10 @@ public class GeneratorConfig {
|
|||
private Prefab[] uniqueStructures = {};
|
||||
|
||||
@JsonProperty("dungeons")
|
||||
private WeightedList<Prefab> dungeons = new WeightedList<>();
|
||||
private WeightedMap<Prefab> dungeons = new WeightedMap<>();
|
||||
|
||||
@JsonProperty("spawn_towers")
|
||||
private WeightedList<Prefab> spawnTowers = new WeightedList<>();
|
||||
private WeightedMap<Prefab> spawnTowers = new WeightedMap<>();
|
||||
|
||||
@JsonProperty("dungeon_region")
|
||||
private Vector2i dungeonRegion = new Vector2i(80, 64);
|
||||
|
@ -46,7 +46,7 @@ public class GeneratorConfig {
|
|||
private double dungeonRate = 0.25;
|
||||
|
||||
@JsonProperty("stone_variants")
|
||||
private WeightedList<StoneVariant> stoneVariants = new WeightedList<>();
|
||||
private WeightedMap<StoneVariant> stoneVariants = new WeightedMap<>();
|
||||
|
||||
@JsonProperty("cave_types")
|
||||
@JsonDeserialize(using = CaveDecoratorListDeserializer.class)
|
||||
|
@ -74,11 +74,11 @@ public class GeneratorConfig {
|
|||
return uniqueStructures;
|
||||
}
|
||||
|
||||
public WeightedList<Prefab> getDungeons() {
|
||||
public WeightedMap<Prefab> getDungeons() {
|
||||
return dungeons;
|
||||
}
|
||||
|
||||
public WeightedList<Prefab> getSpawnTowers() {
|
||||
public WeightedMap<Prefab> getSpawnTowers() {
|
||||
return spawnTowers;
|
||||
}
|
||||
|
||||
|
@ -90,7 +90,7 @@ public class GeneratorConfig {
|
|||
return dungeonRate;
|
||||
}
|
||||
|
||||
public WeightedList<StoneVariant> getStoneVariants() {
|
||||
public WeightedMap<StoneVariant> getStoneVariants() {
|
||||
return stoneVariants;
|
||||
}
|
||||
|
||||
|
|
|
@ -3,14 +3,14 @@ package brainwine.gameserver.zone.gen;
|
|||
import brainwine.gameserver.item.Layer;
|
||||
import brainwine.gameserver.prefab.Prefab;
|
||||
import brainwine.gameserver.util.Vector2i;
|
||||
import brainwine.gameserver.util.WeightedList;
|
||||
import brainwine.gameserver.util.WeightedMap;
|
||||
import brainwine.gameserver.zone.Block;
|
||||
|
||||
public class StructureGenerator implements GeneratorTask {
|
||||
|
||||
private final Prefab[] uniqueStructures;
|
||||
private final WeightedList<Prefab> dungeons;
|
||||
private final WeightedList<Prefab> spawnTowers;
|
||||
private final WeightedMap<Prefab> dungeons;
|
||||
private final WeightedMap<Prefab> spawnTowers;
|
||||
private final Vector2i dungeonRegion;
|
||||
private final double dungeonRate;
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
|
|||
import brainwine.gameserver.item.Item;
|
||||
import brainwine.gameserver.item.ItemRegistry;
|
||||
import brainwine.gameserver.item.Layer;
|
||||
import brainwine.gameserver.util.WeightedList;
|
||||
import brainwine.gameserver.util.WeightedMap;
|
||||
import brainwine.gameserver.zone.gen.CaveDecorator;
|
||||
import brainwine.gameserver.zone.gen.GeneratorContext;
|
||||
import brainwine.gameserver.zone.gen.models.BlockPosition;
|
||||
|
@ -16,7 +16,7 @@ import brainwine.gameserver.zone.gen.models.Cave;
|
|||
public class CandyCaneCaveDecorator extends CaveDecorator {
|
||||
|
||||
@JsonProperty("candy_canes")
|
||||
private final WeightedList<Item> candyCanes = new WeightedList<Item>()
|
||||
private final WeightedMap<Item> candyCanes = new WeightedMap<Item>()
|
||||
.addEntry(ItemRegistry.getItem("holiday/candy-cane-small"), 6)
|
||||
.addEntry(ItemRegistry.getItem("holiday/candy-cane-large"), 1);
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
|
|||
import brainwine.gameserver.item.Item;
|
||||
import brainwine.gameserver.item.ItemRegistry;
|
||||
import brainwine.gameserver.item.Layer;
|
||||
import brainwine.gameserver.util.WeightedList;
|
||||
import brainwine.gameserver.util.WeightedMap;
|
||||
import brainwine.gameserver.zone.gen.CaveDecorator;
|
||||
import brainwine.gameserver.zone.gen.GeneratorContext;
|
||||
import brainwine.gameserver.zone.gen.models.BlockPosition;
|
||||
|
@ -16,7 +16,7 @@ import brainwine.gameserver.zone.gen.models.Cave;
|
|||
public class CrystalCaveDecorator extends CaveDecorator {
|
||||
|
||||
@JsonProperty("crystals")
|
||||
private final WeightedList<Item> crystals = new WeightedList<Item>()
|
||||
private final WeightedMap<Item> crystals = new WeightedMap<Item>()
|
||||
.addEntry(ItemRegistry.getItem("ground/crystal-blue-1"), 16)
|
||||
.addEntry(ItemRegistry.getItem("ground/crystal-blue-3"), 16)
|
||||
.addEntry(ItemRegistry.getItem("ground/crystal-blue-2"), 4)
|
||||
|
|
|
@ -4,7 +4,7 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
|||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import brainwine.gameserver.item.Layer;
|
||||
import brainwine.gameserver.util.WeightedList;
|
||||
import brainwine.gameserver.util.WeightedMap;
|
||||
import brainwine.gameserver.zone.gen.CaveDecorator;
|
||||
import brainwine.gameserver.zone.gen.GeneratorContext;
|
||||
import brainwine.gameserver.zone.gen.models.BlockPosition;
|
||||
|
@ -15,7 +15,7 @@ import brainwine.gameserver.zone.gen.models.MushroomType;
|
|||
public class MushroomCaveDecorator extends CaveDecorator {
|
||||
|
||||
@JsonProperty("mushrooms")
|
||||
private final WeightedList<MushroomType> mushrooms = new WeightedList<>();
|
||||
private final WeightedMap<MushroomType> mushrooms = new WeightedMap<>();
|
||||
|
||||
@JsonProperty("mushroom_chance")
|
||||
private double rate = 0.2;
|
||||
|
|
|
@ -5,7 +5,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
|
|||
|
||||
import brainwine.gameserver.item.Item;
|
||||
import brainwine.gameserver.item.Layer;
|
||||
import brainwine.gameserver.util.WeightedList;
|
||||
import brainwine.gameserver.util.WeightedMap;
|
||||
import brainwine.gameserver.zone.gen.CaveDecorator;
|
||||
import brainwine.gameserver.zone.gen.GeneratorContext;
|
||||
import brainwine.gameserver.zone.gen.models.BlockPosition;
|
||||
|
@ -15,7 +15,7 @@ import brainwine.gameserver.zone.gen.models.Cave;
|
|||
public class RareCrystalCaveDecorator extends CaveDecorator {
|
||||
|
||||
@JsonProperty("crystals")
|
||||
private final WeightedList<Item> crystals = new WeightedList<>();
|
||||
private final WeightedMap<Item> crystals = new WeightedMap<>();
|
||||
|
||||
@JsonProperty("crystal_chance")
|
||||
private double rate = 0.05;
|
||||
|
|
Loading…
Add table
Reference in a new issue