Skip to content

ItemCannon fixes #1018

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 7 additions & 55 deletions src/main/java/openblocks/common/entity/EntityItemProjectile.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,64 +39,16 @@ public static void registerFixes(DataFixer fixer) {

@Override
public void onUpdate() {
final double x = posX;
final double y = posY;
final double z = posZ;

final double vx = motionX;
final double vy = motionY;
final double vz = motionZ;

// let vanilla run
super.onUpdate();
if (!isDead) return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was supposed to be isDead, then whole thing becomes more sensible.

Basically, purpose of this code was to let super.onUpdate handle all state changes, then overwrite movement with our own. Restoring just motion before update leads to numerical errors, which would cause mismatch with canon targeting algorithm (not that it was super precise in first place though).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, although with isDead instead it was double-moving the item, so that would have to get fixed too. It's basically a choice between the way I've got it, which is a a bit of a hack no doubt, and duplicating half the code inside EntityItem.onUpdate. If you'd prefer it the other way, that's fine by me. I wouldn't worry about the numerical errors though--these values are all doubles pretty close to 1, so back-of-the-envelope calculation says worst case is an acceleration of about 4.5e-15 blocks/s^2.

// and then overwrite position calculations

this.posX = x;
this.posY = y;
this.posZ = z;

this.motionX = vx;
this.motionY = vy;
this.motionZ = vz;

this.prevPosX = this.posX;
this.prevPosY = this.posY;
this.prevPosZ = this.posZ;
this.motionY -= 0.03999999910593033D;

move(MoverType.SELF, this.motionX, this.motionY, this.motionZ);

boolean hasMoved = (int)this.prevPosX != (int)this.posX || (int)this.prevPosY != (int)this.posY || (int)this.prevPosZ != (int)this.posZ;

if (hasMoved || this.ticksExisted % 25 == 0) {
if (this.world.getBlockState(new BlockPos(this)).getMaterial() == Material.LAVA) {
this.motionY = 0.20000000298023224D;
this.motionX = (this.rand.nextFloat() - this.rand.nextFloat()) * 0.2F;
this.motionZ = (this.rand.nextFloat() - this.rand.nextFloat()) * 0.2F;
playSound(SoundEvents.ENTITY_GENERIC_BURN, 0.4F, 2.0F + this.rand.nextFloat() * 0.4F);
}
// Remove the air drag that EntityItem.onUpdate adds to our velocity
if (!this.onGround) {
double f = 0.98F;
this.motionX = this.motionX / f;
this.motionY = this.motionY / f;
this.motionZ = this.motionZ / f;
}

// Zero Air Friction
float f = 1F;

// Keep ground friction
if (this.onGround) {
BlockPos underPos = new BlockPos(MathHelper.floor(this.posX), MathHelper.floor(getEntityBoundingBox().minY) - 1, MathHelper.floor(this.posZ));
IBlockState underState = this.world.getBlockState(underPos);
f = underState.getBlock().getSlipperiness(underState, this.world, underPos, this) * 0.98F;
}

this.motionX *= f;
// Y would there be Y resistance :D
// ^ not my pun, I'm just porting :P, ~B
// motionY *= 0.98;
this.motionZ *= f;

if (this.onGround) this.motionY *= -0.5D;

handleWaterMovement();
}

}
}
11 changes: 10 additions & 1 deletion src/main/java/openblocks/common/tileentity/TileEntityCannon.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,17 @@ private void fireStack(@Nonnull ItemStack stack) {
final ITriggerable rpc = createServerRpcProxy(ITriggerable.class);
rpc.trigger();

// spawn the item approximately at the end of the barrel
double yawr = Math.toRadians(currentYaw);
double pitchr = Math.toRadians(currentPitch);
double barrelLength = 0.5;
double barrelBaseHeight = 0.3;
double x = pos.getX() + 0.5 - Math.sin(yawr) * Math.cos(pitchr) * barrelLength;
double y = pos.getY() + barrelBaseHeight + Math.sin(pitchr) * barrelLength;
double z = pos.getZ() + 0.5 + Math.cos(yawr) * Math.cos(pitchr) * barrelLength;

// projectileOrigin is not used here, it's used for the calculations below.
EntityItem item = new EntityItemProjectile(world, pos.getX() + 0.5, pos.getY(), pos.getZ(), stack);
EntityItem item = new EntityItemProjectile(world, x, y, z, stack);
item.setDefaultPickupDelay();

// Now that we generate vectors instead of eular angles, this should be revised.
Expand Down