add a triangulateAuraAround method to the API because I was using that twice without making it nice

This commit is contained in:
Ellpeck 2018-12-05 11:21:08 +01:00
parent 6266d54aba
commit b35aed4bde
6 changed files with 48 additions and 16 deletions

View file

@ -13,6 +13,7 @@ import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import net.minecraftforge.items.IItemHandler;
import org.apache.commons.lang3.mutable.MutableFloat;
import org.apache.commons.lang3.mutable.MutableInt;
import org.apache.commons.lang3.mutable.MutableObject;
import org.lwjgl.util.vector.Vector3f;
@ -96,6 +97,16 @@ public class InternalHooks implements NaturesAuraAPI.IInternalHooks {
return result.intValue();
}
@Override
public int triangulateAuraInArea(World world, BlockPos pos, int radius) {
MutableFloat result = new MutableFloat(IAuraChunk.DEFAULT_AURA);
IAuraChunk.getSpotsInArea(world, pos, radius, (blockPos, spot) -> {
float percentage = 1F - (float) pos.getDistance(blockPos.getX(), blockPos.getY(), blockPos.getZ()) / radius;
result.add(spot * percentage);
});
return result.intValue();
}
@Override
public BlockPos getLowestAuraDrainSpot(World world, BlockPos pos, int radius, BlockPos defaultSpot) {
MutableInt lowestAmount = new MutableInt(Integer.MAX_VALUE);

View file

@ -232,6 +232,11 @@ public final class NaturesAuraAPI {
*/
int getAuraInArea(World world, BlockPos pos, int radius);
/**
* @see IAuraChunk#triangulateAuraInArea(World, BlockPos, int)
*/
int triangulateAuraInArea(World world, BlockPos pos, int radius);
/**
* @see IAuraChunk#getLowestSpot(World, BlockPos, int, BlockPos)
*/

View file

@ -65,7 +65,8 @@ public interface IAuraChunk extends ICapabilityProvider, INBTSerializable<NBTTag
/**
* Convenience method that adds up all of the aura from each drain spot from
* {@link #getSpotsInArea(World, BlockPos, int, BiConsumer)} and
* conveniently returns it.
* conveniently returns it. For a better visual display with a more gradual
* increase, use {@link #triangulateAuraInArea(World, BlockPos, int)}.
*
* @param world The world
* @param pos The center position
@ -77,6 +78,25 @@ public interface IAuraChunk extends ICapabilityProvider, INBTSerializable<NBTTag
return NaturesAuraAPI.instance().getAuraInArea(world, pos, radius);
}
/**
* Convenience method that adds up all of the aura from each drain spot from
* {@link #getSpotsInArea(World, BlockPos, int, BiConsumer)}, but multiplies
* their amount by the percentual distance to the supplied position. This
* will cause for a lot more gradual of an increase and decrease of Aura
* when moving closer to actual spots. This should be used for visual
* purposes as it is more performance intensive than {@link
* #getAuraInArea(World, BlockPos, int)}.
*
* @param world The world
* @param pos The center position
* @param radius The radius around the center to search for spots in
* @return The amount of Aura presetn in that area, based on the drain spots
* that are found and their distance to the center
*/
static int triangulateAuraInArea(World world, BlockPos pos, int radius) {
return NaturesAuraAPI.instance().triangulateAuraInArea(world, pos, radius);
}
/**
* This method returns the position of the lowest drain spot (meaning the
* one that has the least Aura stored) in the given area. This should be

View file

@ -1,6 +1,7 @@
package de.ellpeck.naturesaura.api.internal;
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
import de.ellpeck.naturesaura.api.multiblock.IMultiblock;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.ResourceLocation;
@ -37,7 +38,12 @@ public class StubHooks implements NaturesAuraAPI.IInternalHooks {
@Override
public int getAuraInArea(World world, BlockPos pos, int radius) {
return 0;
return IAuraChunk.DEFAULT_AURA;
}
@Override
public int triangulateAuraInArea(World world, BlockPos pos, int radius) {
return IAuraChunk.DEFAULT_AURA;
}
@Override

View file

@ -3,7 +3,6 @@ package de.ellpeck.naturesaura.blocks.tiles;
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.MathHelper;
import org.apache.commons.lang3.mutable.MutableFloat;
public class TileEntityAuraDetector extends TileEntityImpl implements ITickable {
@ -12,12 +11,8 @@ public class TileEntityAuraDetector extends TileEntityImpl implements ITickable
@Override
public void update() {
if (!this.world.isRemote && this.world.getTotalWorldTime() % 20 == 0) {
MutableFloat totalAmount = new MutableFloat(IAuraChunk.DEFAULT_AURA);
IAuraChunk.getSpotsInArea(this.world, this.pos, 25, (pos, spot) -> {
float percentage = 1F - (float) this.pos.getDistance(pos.getX(), pos.getY(), pos.getZ()) / 25F;
totalAmount.add(spot * percentage);
});
int power = MathHelper.clamp(MathHelper.ceil(totalAmount.intValue() / (IAuraChunk.DEFAULT_AURA * 2F) * 15F), 0, 15);
int totalAmount = IAuraChunk.triangulateAuraInArea(this.world, this.pos, 25);
int power = MathHelper.clamp(MathHelper.ceil(totalAmount / (IAuraChunk.DEFAULT_AURA * 2F) * 15F), 0, 15);
if (this.redstonePower != power) {
this.redstonePower = power;
this.world.updateComparatorOutputLevel(this.pos, this.getBlockType());

View file

@ -34,7 +34,6 @@ import net.minecraftforge.fml.common.gameevent.TickEvent.Phase;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.items.IItemHandler;
import org.apache.commons.lang3.mutable.MutableFloat;
import org.apache.commons.lang3.mutable.MutableInt;
import org.lwjgl.opengl.GL11;
@ -240,12 +239,8 @@ public class ClientEvents {
if (!mc.gameSettings.showDebugInfo) {
GlStateManager.color(83 / 255F, 160 / 255F, 8 / 255F);
MutableFloat totalAmount = new MutableFloat(IAuraChunk.DEFAULT_AURA);
IAuraChunk.getSpotsInArea(mc.world, mc.player.getPosition(), 35, (pos, spot) -> {
float percentage = 1F - (float) mc.player.getDistance(pos.getX() + 0.5F, pos.getY() + 0.5F, pos.getZ() + 0.5F) / 35F;
totalAmount.add(spot * percentage);
});
float totalPercentage = totalAmount.intValue() / (IAuraChunk.DEFAULT_AURA * 2F);
int totalAmount = IAuraChunk.triangulateAuraInArea(mc.world, mc.player.getPosition(), 35);
float totalPercentage = totalAmount / (IAuraChunk.DEFAULT_AURA * 2F);
int tHeight = MathHelper.ceil(MathHelper.clamp(totalPercentage, 0F, 1F) * 50);
if (tHeight < 50)