Skip to content

Commit

Permalink
Entity dragging almost ish
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePlasticPotato committed Dec 14, 2024
1 parent fad8c88 commit aaa7a38
Show file tree
Hide file tree
Showing 16 changed files with 380 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,17 @@ public ClientShipWorldCore getShipObjectWorld() {
@Shadow
public abstract ClientPacketListener getConnection();

@Inject(
method = "tick",
at = @At("HEAD")
)
public void preTick(final CallbackInfo ci) {
// Tick the ship world and then drag entities
if (!pause && shipObjectWorld != null && level != null && getConnection() != null) {
//EntityDragger.INSTANCE.dragEntitiesWithShips(level.entitiesForRendering(), true);
}
}

@Inject(
method = "tick",
at = @At("TAIL")
Expand All @@ -112,7 +123,8 @@ public void postTick(final CallbackInfo ci) {
if (!pause && shipObjectWorld != null && level != null && getConnection() != null) {
shipObjectWorld.tickNetworking(getConnection().getConnection().getRemoteAddress());
shipObjectWorld.postTick();
EntityDragger.INSTANCE.dragEntitiesWithShips(level.entitiesForRendering());
//EntityDragger.INSTANCE.dragEntitiesWithShips(level.entitiesForRendering());
EntityDragger.INSTANCE.dragEntitiesWithShips(level.entitiesForRendering(), false);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private void preRender(final float tickDelta, final long startTime, final boolea
((IEntityDraggingInformationProvider) entity).getDraggingInformation();
final Long lastShipStoodOn = entityDraggingInformation.getLastShipStoodOn();
// Then try getting [entityShouldBeHere] from [entityDraggingInformation]
if (lastShipStoodOn != null && entityDraggingInformation.isEntityBeingDraggedByAShip()) {
if (lastShipStoodOn != null && entityDraggingInformation.isEntityBeingDraggedByAShip()) { //for testing
final ClientShip shipObject =
VSGameUtilsKt.getShipObjectWorld(clientWorld).getLoadedShips().getById(lastShipStoodOn);
if (shipObject != null) {
Expand Down Expand Up @@ -200,6 +200,10 @@ private void preRender(final float tickDelta, final long startTime, final boolea
entity.xo = (entityShouldBeHere.x() - (entity.getX() * tickDelta)) / (1.0 - tickDelta);
entity.yo = (entityShouldBeHere.y() - (entity.getY() * tickDelta)) / (1.0 - tickDelta);
entity.zo = (entityShouldBeHere.z() - (entity.getZ() * tickDelta)) / (1.0 - tickDelta);
//why the fuck do we do this

//what if i just...
//dont
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.valkyrienskies.mod.mixin.feature.ai.goal_redirector;

import net.minecraft.world.entity.ai.goal.RandomStrollGoal;
import org.spongepowered.asm.mixin.Mixin;

@Mixin(RandomStrollGoal.class)
public class MixinRandomStrollGoal {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.valkyrienskies.mod.mixin.feature.ai.goal_redirector;

import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;
import org.spongepowered.asm.mixin.Mixin;

@Mixin(WaterAvoidingRandomStrollGoal.class)
public class MixinWaterAvoidingRandomStrollGoal {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import java.util.Random;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.core.BlockPos;
import net.minecraft.core.particles.BlockParticleOption;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityDimensions;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.MoverType;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.RenderShape;
Expand All @@ -27,6 +30,7 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
import org.valkyrienskies.core.api.ships.ClientShip;
import org.valkyrienskies.core.api.ships.Ship;
import org.valkyrienskies.mod.common.VSGameUtilsKt;
import org.valkyrienskies.mod.common.util.EntityDraggingInformation;
Expand All @@ -38,6 +42,27 @@ public abstract class MixinEntity implements IEntityDraggingInformationProvider

// region collision

@Shadow
public abstract void setPos(Vec3 arg);

@Shadow
public abstract boolean is(Entity arg);

@Shadow
public abstract boolean isControlledByLocalInstance();

@Shadow
public abstract EntityType<?> getType();

@Shadow
protected boolean firstTick;

@Shadow
public abstract Iterable<Entity> getIndirectPassengers();

@Shadow
public abstract BlockPos getOnPos();

/**
* Cancel movement of entities that are colliding with unloaded ships
*/
Expand Down Expand Up @@ -74,6 +99,14 @@ public Vec3 collideWithShips(final Entity entity, Vec3 movement, final Operation
entityDraggingInformation.setLastShipStoodOn(null);
entityDraggingInformation.setAddedMovementLastTick(new Vector3d());
entityDraggingInformation.setAddedYawRotLastTick(0.0);

for (Entity entityRiding : entity.getIndirectPassengers()) {
final EntityDraggingInformation passengerDraggingInformation =
((IEntityDraggingInformationProvider) entityRiding).getDraggingInformation();
passengerDraggingInformation.setLastShipStoodOn(null);
passengerDraggingInformation.setAddedMovementLastTick(new Vector3d());
passengerDraggingInformation.setAddedYawRotLastTick(0.0);
}
}

return collisionResultWithWorld;
Expand Down Expand Up @@ -214,6 +247,36 @@ private void preSpawnSprintParticle(final CallbackInfo ci) {
}
}
}

@Inject(
method = "baseTick",
at = @At("TAIL")
)
private void postBaseTick(final CallbackInfo ci) {
final EntityDraggingInformation entityDraggingInformation = getDraggingInformation();

if (level != null && level.isClientSide && !firstTick && getType() != EntityType.PLAYER && !isControlledByLocalInstance()) {
final Ship ship = VSGameUtilsKt.getShipObjectManagingPos(level, getOnPos());
if (ship != null) {
entityDraggingInformation.setLastShipStoodOn(ship.getId());
getIndirectPassengers().forEach(entity -> {
final EntityDraggingInformation passengerDraggingInformation =
((IEntityDraggingInformationProvider) entity).getDraggingInformation();
passengerDraggingInformation.setLastShipStoodOn(ship.getId());
});
} else {
if (!level.getBlockState(getOnPos()).isAir()) {
entityDraggingInformation.setLastShipStoodOn(null);
getIndirectPassengers().forEach(entity -> {
final EntityDraggingInformation passengerDraggingInformation =
((IEntityDraggingInformationProvider) entity).getDraggingInformation();
passengerDraggingInformation.setLastShipStoodOn(null);
});
}
}
}
}

// endregion

// region shadow functions and fields
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.valkyrienskies.mod.mixin.feature.entity_collision;

import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.Level;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.valkyrienskies.core.api.ships.ClientShip;
import org.valkyrienskies.mod.common.VSGameUtilsKt;
import org.valkyrienskies.mod.common.util.EntityDraggingInformation;
import org.valkyrienskies.mod.common.util.EntityLerper;
import org.valkyrienskies.mod.common.util.IEntityDraggingInformationProvider;
import org.valkyrienskies.mod.util.LoggingKt;

@Mixin(LivingEntity.class)
public abstract class MixinLivingEntity extends Entity {

public MixinLivingEntity(EntityType<?> entityType, Level level) {
super(entityType, level);
}

@Inject(
method = "aiStep",
at = @At(value = "HEAD")
)
private void preAiStep(CallbackInfo ci) {
// fake lerp movement gaming
if (this.level != null && this.level.isClientSide() && !firstTick) {
if (((Object) this) instanceof LocalPlayer) return;
EntityDraggingInformation dragInfo = ((IEntityDraggingInformationProvider) this).getDraggingInformation();
if (dragInfo != null && dragInfo.getLastShipStoodOn() != null) {
final ClientShip ship = VSGameUtilsKt.getShipObjectWorld((ClientLevel) level).getAllShips().getById(dragInfo.getLastShipStoodOn());
if (ship != null) {
//EntityLerper.INSTANCE.lerpStep(dragInfo, ship, (LivingEntity) (Object) this);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package org.valkyrienskies.mod.mixin.feature.entity_collision;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import java.util.function.Consumer;
import net.minecraft.network.protocol.game.ClientboundMoveEntityPacket;
import net.minecraft.network.protocol.game.ClientboundSetEntityMotionPacket;
import net.minecraft.network.protocol.game.ClientboundTeleportEntityPacket;
import net.minecraft.server.level.ServerEntity;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
import org.joml.Vector3d;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.valkyrienskies.core.api.ships.ServerShip;
import org.valkyrienskies.mod.common.VSGameUtilsKt;
import org.valkyrienskies.mod.common.ValkyrienSkiesMod;
import org.valkyrienskies.mod.common.networking.PacketEntityShipMotion;
import org.valkyrienskies.mod.common.util.EntityDraggingInformation;
import org.valkyrienskies.mod.common.util.IEntityDraggingInformationProvider;

@Mixin(ServerEntity.class)
public class MixinServerEntity {

@Shadow
@Final
private Entity entity;

@Shadow
@Final
private ServerLevel level;

@WrapOperation(
method = "sendChanges",
at = @At(
value = "INVOKE",
target = "Ljava/util/function/Consumer;accept(Ljava/lang/Object;)V")
)
private void wrapBroadcastAccept(Consumer instance, Object t, Operation<Void> original) {
if (t instanceof ClientboundSetEntityMotionPacket || t instanceof ClientboundTeleportEntityPacket || t instanceof ClientboundMoveEntityPacket) {
if (entity instanceof IEntityDraggingInformationProvider draggedEntity) {
EntityDraggingInformation dragInfo = draggedEntity.getDraggingInformation();

if (dragInfo != null && dragInfo.isEntityBeingDraggedByAShip() && dragInfo.getLastShipStoodOn() != null) {
ServerShip ship = VSGameUtilsKt.getShipObjectWorld(level).getAllShips().getById(dragInfo.getLastShipStoodOn());
if (ship != null) {
Vector3d position = ship.getWorldToShip().transformPosition(new Vector3d(entity.getX(), entity.getY(), entity.getZ()));
Vector3d motion = ship.getTransform().transformDirectionNoScalingFromWorldToShip(new Vector3d(entity.getDeltaMovement().x(), entity.getDeltaMovement().y(), entity.getDeltaMovement().z()), new Vector3d());
Vector3d entityLookYawOnly = new Vector3d(Math.sin(-Math.toRadians(entity.getYRot())), 0.0, Math.cos(-Math.toRadians(entity.getYRot())));
double yaw = ship.getTransform().transformDirectionNoScalingFromWorldToShip(entityLookYawOnly, new Vector3d()).y;
double pitch = entity.getXRot();
PacketEntityShipMotion vsPacket = new PacketEntityShipMotion(entity.getId(), ship.getId(),
position.x, position.y, position.z,
motion.x, motion.y, motion.z,
yaw, pitch);

ValkyrienSkiesMod.getVsCore().getSimplePacketNetworking().sendToAllClients(vsPacket);
return;
}


}
}
}
original.call(instance, t);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.valkyrienskies.core.api.ships.LoadedServerShip;
import org.valkyrienskies.mod.common.VSGameUtilsKt;
import org.valkyrienskies.mod.common.util.SplitHandler;
import org.valkyrienskies.mod.common.util.SplittingDisablerAttachment;

@Mixin(Contraption.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ private void preTick(final CallbackInfo ci) {
// endregion

vsPipeline.preTickGame();
for (final ServerLevel level : getAllLevels()) {
//EntityDragger.INSTANCE.dragEntitiesWithShips(level.getAllEntities(), true);
}
}

/**
Expand Down Expand Up @@ -232,7 +235,7 @@ private void postTick(final CallbackInfo ci) {
vsPipeline.postTickGame();
// Only drag entities after we have updated the ship positions
for (final ServerLevel level : getAllLevels()) {
EntityDragger.INSTANCE.dragEntitiesWithShips(level.getAllEntities());
EntityDragger.INSTANCE.dragEntitiesWithShips(level.getAllEntities(), false);
if (LoadedMods.getWeather2())
Weather2Compat.INSTANCE.tick(level);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.valkyrienskies.mod.common.networking

import org.valkyrienskies.core.impl.networking.simple.SimplePacket

/**
* This packet is used to update an entity's relative position while being dragged by a ship, in place of
* [net.minecraft.network.protocol.game.ClientboundTeleportEntityPacket], [net.minecraft.network.protocol.game.ClientboundMoveEntityPacket], and [net.minecraft.network.protocol.game.ClientboundSetEntityMotionPacket].
*/
data class PacketEntityShipMotion(
val entityID: Int,
val shipID: Long,
val x: Double,
val y: Double,
val z: Double,
val xVel: Double,
val yVel: Double,
val zVel: Double,
val yRot: Double,
val xRot: Double,
): SimplePacket
Loading

0 comments on commit aaa7a38

Please sign in to comment.