Fixed entity mass being overestimated

This commit is contained in:
Unknown 2018-05-11 01:28:56 +02:00 committed by unknown
parent b70735f5d6
commit fc02755a7f
3 changed files with 128 additions and 6 deletions

View file

@ -1144,7 +1144,7 @@ public class TileEntityTransporterCore extends TileEntityAbstractEnergy implemen
if ( transporterState != EnumTransporterState.ENERGIZING
&& transporterState != EnumTransporterState.ACQUIRING ) {
entityValues.count = countScanners;
entityValues.mass = 8000 * countScanners;
entityValues.mass = 2 * countScanners;
return entityValues;
}
@ -1212,7 +1212,7 @@ public class TileEntityTransporterCore extends TileEntityAbstractEnergy implemen
} else {
movingEntities.put(index, movingEntity);
entityValues.count++;
entityValues.mass += movingEntity.getMass();
entityValues.mass += movingEntity.getMassFactor();
}
}
@ -1272,7 +1272,7 @@ public class TileEntityTransporterCore extends TileEntityAbstractEnergy implemen
} else {
movingEntities.put(index, movingEntity);
entityValues.count++;
entityValues.mass += movingEntity.getMass();
entityValues.mass += movingEntity.getMassFactor();
}
}

View file

@ -0,0 +1,102 @@
package cr0s.warpdrive.data;
import javax.annotation.Nonnull;
import java.io.DataOutput;
/*
Simple DataOutput implementation to estimate data size in bytes.
Size is capped at Integer.MAX_VALUE in case of overflow.
*/
class DataOutputLength implements DataOutput {
protected int countBytes;
public DataOutputLength() {
super();
}
private void increaseCount(final int value) {
int countBytes_new = countBytes + value;
if (countBytes_new < 0) {
countBytes_new = Integer.MAX_VALUE;
}
countBytes = countBytes_new;
}
@Override
public void write(final int b) {
increaseCount(1);
}
@Override
public void write(@Nonnull final byte[] bytes) {
increaseCount(bytes.length);
}
@Override
public void write(@Nonnull final byte bytes[], final int offset, final int len) {
increaseCount(len);
}
@Override
public final void writeBoolean(final boolean value) {
increaseCount(1);
}
@Override
public final void writeByte(final int value) {
increaseCount(1);
}
@Override
public final void writeShort(final int value) {
increaseCount(2);
}
@Override
public final void writeChar(final int value) {
increaseCount(2);
}
@Override
public final void writeInt(final int value) {
increaseCount(4);
}
@Override
public final void writeLong(final long value) {
increaseCount(8);
}
@Override
public final void writeFloat(final float value) {
writeInt(Float.floatToIntBits(value));
}
@Override
public final void writeDouble(final double value) {
writeLong(Double.doubleToLongBits(value));
}
@Override
public final void writeBytes(final String value) {
final int length = value.length();
increaseCount(length);
}
@Override
public final void writeChars(final String value) {
final int length = value.length();
increaseCount(length * 2);
}
@Override
public final void writeUTF(final String string) {
final int length = Math.round(string.length() * 1.5F);
increaseCount(length);
}
public final int getLength() {
return countBytes;
}
}

View file

@ -1,11 +1,15 @@
package cr0s.warpdrive.data;
import cr0s.warpdrive.Commons;
import cr0s.warpdrive.WarpDrive;
import cr0s.warpdrive.config.WarpDriveConfig;
import java.io.IOException;
import java.lang.ref.WeakReference;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
@ -77,14 +81,30 @@ public class MovingEntity {
return v3OriginalPosition.distanceTo_square(entity);
}
public int getMass() {
public float getMassFactor() {
final Entity entity = getEntity();
if (entity == null) {
return 0;
return 0.0F;
}
final NBTTagCompound tagCompound = new NBTTagCompound();
entity.writeToNBT(tagCompound);
return tagCompound.toString().length();
int mass;
try {
final DataOutputLength dataOutputLength = new DataOutputLength();
CompressedStreamTools.write(tagCompound, dataOutputLength);
if (WarpDrive.isDev) {
WarpDrive.logger.info(String.format("Entity %s estimated mass is %d",
entity, dataOutputLength.getLength()));
}
mass = dataOutputLength.getLength();
} catch (final IOException exception) {
mass = (int) Math.sqrt(tagCompound.toString().length());
WarpDrive.logger.error(String.format("Unable to estimate mass for entity %s, defaulting to %d",
entity, mass));
}
// average player data size is 7.5 times smaller (gz compression)
return Commons.clamp(0.25F, 4.0F, mass / 80000.0F);
}
}