Use a weighted pool to decide which entities to spawn

This commit is contained in:
Sollace 2021-03-06 15:27:09 +02:00
parent 9c19184910
commit f3b589b307

View file

@ -3,9 +3,9 @@ package com.minelittlepony.unicopia.ability.magic.spell;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.google.common.collect.Lists;
import com.minelittlepony.unicopia.ability.magic.Caster; import com.minelittlepony.unicopia.ability.magic.Caster;
import com.minelittlepony.unicopia.entity.EntityReference; import com.minelittlepony.unicopia.entity.EntityReference;
import com.minelittlepony.unicopia.util.Weighted;
import com.minelittlepony.unicopia.util.WorldEvent; import com.minelittlepony.unicopia.util.WorldEvent;
import com.minelittlepony.unicopia.util.shape.Shape; import com.minelittlepony.unicopia.util.shape.Shape;
import com.minelittlepony.unicopia.util.shape.Sphere; import com.minelittlepony.unicopia.util.shape.Sphere;
@ -17,17 +17,17 @@ import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag; import net.minecraft.nbt.ListTag;
import net.minecraft.particle.ParticleTypes; import net.minecraft.particle.ParticleTypes;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.Vec3d;
import net.minecraft.world.Difficulty; import net.minecraft.world.Difficulty;
public class NecromancySpell extends AbstractPlacedSpell { public class NecromancySpell extends AbstractPlacedSpell {
private final List<EntityType<? extends LivingEntity>> spawns = Lists.newArrayList( private final Weighted<EntityType<? extends LivingEntity>> spawnPool = new Weighted<EntityType<? extends LivingEntity>>()
EntityType.ZOMBIE, .put(7, EntityType.ZOMBIE)
EntityType.HUSK, .put(4, EntityType.HUSK)
EntityType.ZOMBIFIED_PIGLIN .put(2, EntityType.ZOMBIFIED_PIGLIN)
); .put(1, EntityType.ZOMBIE_VILLAGER)
.put(1, EntityType.ZOMBIE_HORSE);
private final List<EntityReference<LivingEntity>> summonedEntities = new ArrayList<>(); private final List<EntityReference<LivingEntity>> summonedEntities = new ArrayList<>();
@ -54,6 +54,8 @@ public class NecromancySpell extends AbstractPlacedSpell {
return true; return true;
} }
summonedEntities.removeIf(ref -> !ref.isPresent(source.getWorld()));
float additional = source.getWorld().getLocalDifficulty(source.getOrigin()).getLocalDifficulty(); float additional = source.getWorld().getLocalDifficulty(source.getOrigin()).getLocalDifficulty();
Shape affectRegion = new Sphere(false, radius); Shape affectRegion = new Sphere(false, radius);
@ -72,12 +74,12 @@ public class NecromancySpell extends AbstractPlacedSpell {
BlockPos loc = new BlockPos(pos); BlockPos loc = new BlockPos(pos);
if (source.getWorld().isAir(loc.up()) && !source.getWorld().isAir(loc)) { if (source.getWorld().isAir(loc.up()) && !source.getWorld().isAir(loc)) {
spawnMonster(source, pos); spawnPool.get().ifPresent(type -> {
spawnMonster(source, pos, type);
});
} }
} }
summonedEntities.removeIf(ref -> !ref.isPresent(source.getWorld()));
return true; return true;
} }
@ -97,9 +99,8 @@ public class NecromancySpell extends AbstractPlacedSpell {
}); });
} }
protected void spawnMonster(Caster<?> source, Vec3d pos) { protected void spawnMonster(Caster<?> source, Vec3d pos, EntityType<? extends LivingEntity> type) {
int index = (int)MathHelper.nextDouble(source.getWorld().random, 0, spawns.size()); LivingEntity zombie = type.create(source.getWorld());
LivingEntity zombie = spawns.get(index).create(source.getWorld());
source.subtractEnergyCost(3); source.subtractEnergyCost(3);
@ -113,6 +114,7 @@ public class NecromancySpell extends AbstractPlacedSpell {
EntityReference<LivingEntity> ref = new EntityReference<>(); EntityReference<LivingEntity> ref = new EntityReference<>();
ref.set(zombie); ref.set(zombie);
summonedEntities.add(ref); summonedEntities.add(ref);
setDirty();
} }
@Override @Override
@ -128,8 +130,8 @@ public class NecromancySpell extends AbstractPlacedSpell {
@Override @Override
public void fromNBT(CompoundTag compound) { public void fromNBT(CompoundTag compound) {
super.fromNBT(compound); super.fromNBT(compound);
summonedEntities.clear();
if (compound.contains("summonedEntities")) { if (compound.contains("summonedEntities")) {
summonedEntities.clear();
compound.getList("summonedEntities", 10).forEach(tag -> { compound.getList("summonedEntities", 10).forEach(tag -> {
EntityReference<LivingEntity> ref = new EntityReference<>(); EntityReference<LivingEntity> ref = new EntityReference<>();
ref.fromNBT((CompoundTag)tag); ref.fromNBT((CompoundTag)tag);