Skip to content

Commit

Permalink
entity pushing movement sim
Browse files Browse the repository at this point in the history
  • Loading branch information
rfresh2 committed Jan 23, 2025
1 parent ec56229 commit 4f51ec1
Show file tree
Hide file tree
Showing 7 changed files with 272 additions and 130 deletions.
12 changes: 12 additions & 0 deletions src/main/java/com/zenith/feature/world/World.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.zenith.feature.world;

import com.zenith.cache.data.chunk.Chunk;
import com.zenith.cache.data.entity.EntityLiving;
import com.zenith.mc.block.*;
import com.zenith.mc.dimension.DimensionData;
import com.zenith.mc.dimension.DimensionRegistry;
Expand Down Expand Up @@ -321,5 +322,16 @@ public static FluidState getFluidState(final int x, final int y, final int z) {
return getFluidState(getBlockState(x, y, z).id());
}

public static boolean onClimbable(EntityLiving entity) {
Block inBlock = getBlock(MathHelper.floorI(entity.getX()), MathHelper.floorI(entity.getY()), MathHelper.floorI(entity.getZ()));
if (inBlock.blockTags().contains(BlockTags.CLIMBABLE)) {
return true;
}
// todo: trapdoors
// else if (inBlock.name().endsWith("_trapdoor") && trapdoorUsableAsLadder(inBlock, blockPos)) {
//
// }
return false;
}

}
11 changes: 11 additions & 0 deletions src/main/java/com/zenith/mc/block/LocalizedCollisionBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ public LocalizedCollisionBox stretch(double x, double y, double z) {
);
}

public LocalizedCollisionBox inflate(double x, double y, double z) {
return new LocalizedCollisionBox(
minX() - x, maxX() + x,
minY() - y, maxY() + y,
minZ() - z, maxZ() + z,
this.x,
this.y,
this.z
);
}

public LocalizedCollisionBox move(final double x, final double y, final double z) {
return new LocalizedCollisionBox(
this.minX() + x, this.maxX() + x,
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/zenith/mc/entity/EntityData.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public record EntityData(
float height,
boolean attackable,
boolean pickable,
boolean livingEntity,
EntityType mcplType
) implements RegistryData { }
14 changes: 14 additions & 0 deletions src/main/java/com/zenith/mc/entity/EntityDataManager.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.zenith.mc.entity;

import com.zenith.cache.data.entity.EntityLiving;
import com.zenith.mc.block.LocalizedCollisionBox;
import com.zenith.util.Maps;
import it.unimi.dsi.fastutil.objects.Reference2ObjectMap;
import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap;
Expand Down Expand Up @@ -28,4 +30,16 @@ public EntityData getEntityData(final EntityType type) {
if (data == entityTypeToData.defaultReturnValue()) return null;
return data;
}

public LocalizedCollisionBox getCollisionBox(final EntityLiving entity) {
var data = getEntityData(entity.getEntityType());
if (data == null) return null;
double w = data.width() / 2;
return new LocalizedCollisionBox(
entity.getX() - w, entity.getX() + w,
entity.getY(), entity.getY() + data.height(),
entity.getZ() - w, entity.getZ() + w,
entity.getX(), entity.getY(), entity.getZ()
);
}
}
260 changes: 130 additions & 130 deletions src/main/java/com/zenith/mc/entity/EntityRegistry.java

Large diffs are not rendered by default.

94 changes: 94 additions & 0 deletions src/main/java/com/zenith/module/impl/PlayerSimulation.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package com.zenith.module.impl;

import com.zenith.cache.data.entity.EntityLiving;
import com.zenith.cache.data.entity.EntityPlayer;
import com.zenith.event.module.ClientBotTick;
import com.zenith.feature.world.*;
import com.zenith.feature.world.raycast.RaycastHelper;
import com.zenith.mc.block.*;
import com.zenith.mc.dimension.DimensionRegistry;
import com.zenith.mc.entity.EntityData;
import com.zenith.module.Module;
import com.zenith.util.Timer;
import com.zenith.util.math.MathHelper;
import com.zenith.util.math.MutableVec3d;
import lombok.Getter;
import org.geysermc.mcprotocollib.protocol.data.game.PlayerListEntry;
import org.geysermc.mcprotocollib.protocol.data.game.entity.Effect;
import org.geysermc.mcprotocollib.protocol.data.game.entity.attribute.Attribute;
import org.geysermc.mcprotocollib.protocol.data.game.entity.attribute.AttributeModifier;
Expand All @@ -25,6 +29,7 @@
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.level.ServerboundPlayerInputPacket;
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.player.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

Expand Down Expand Up @@ -309,6 +314,7 @@ && isOnGround()
this.lastSprinting = this.isSprinting;
}
this.movementInput.reset();
tickEntityPushing();
}

private static final String SPRINT_ATTRIBUTE_ID = "minecraft:sprinting";
Expand Down Expand Up @@ -798,6 +804,94 @@ private void applyMovementInput(MutableVec3d movementInputVec, float slipperines
}
}

private void tickEntityPushing() {
List<EntityLiving> pushableEntities = new ArrayList<>(0);
for (var it = CACHE.getEntityCache().getEntities().values().iterator(); it.hasNext(); ) {
var entity = it.next();
if (entity == CACHE.getPlayerCache().getThePlayer()) continue;
if (!(entity instanceof EntityLiving entityLiving)) continue;
if (CACHE.getPlayerCache().distanceSqToSelf(entity) > 16.0) continue;
EntityType entityType = entityLiving.getEntityType();
if (entityType == EntityType.HORSE
|| entityType == EntityType.CAMEL
|| entityType == EntityType.DONKEY
|| entityType == EntityType.LLAMA
|| entityType == EntityType.MULE
|| entityType == EntityType.SKELETON_HORSE
|| entityType == EntityType.TRADER_LLAMA
|| entityType == EntityType.ZOMBIE_HORSE
) {
boolean hasPassenger = CACHE.getEntityCache().getEntities().values().stream()
.anyMatch(e -> e.isInVehicle() && e.getVehicleId() == entityLiving.getEntityId());
if (hasPassenger) continue;
else pushableEntities.add(entityLiving);
}
if (entityType == EntityType.MINECART
|| entityType == EntityType.CHEST_MINECART
|| entityType == EntityType.COMMAND_BLOCK_MINECART
|| entityType == EntityType.FURNACE_MINECART
|| entityType == EntityType.HOPPER_MINECART
|| entityType == EntityType.TNT_MINECART
|| entityType == EntityType.SPAWNER_MINECART
) {
boolean hasPassenger = entityType == EntityType.MINECART
&& CACHE.getEntityCache().getEntities().values().stream()
.anyMatch(e -> e.isInVehicle() && e.getVehicleId() == entityLiving.getEntityId());
if (hasPassenger) continue;
pushableEntities.add(entityLiving);
}
if (entityType == EntityType.ARMOR_STAND) continue;
if (entityType == EntityType.BAT) continue;
if (entityType == EntityType.BOAT || entityType == EntityType.CHEST_BOAT) {
boolean hasPassenger = CACHE.getEntityCache().getEntities().values().stream()
.anyMatch(e -> e.isInVehicle() && e.getVehicleId() == entityLiving.getEntityId());
if (hasPassenger) continue;
pushableEntities.add(entityLiving);
}
if (entityType == EntityType.PARROT) {
pushableEntities.add(entityLiving);
}
EntityData entityData = ENTITY_DATA.getEntityData(entityType);
if (entityData == null) continue;
if (entityData.livingEntity()) {
boolean isSpectator = false;
if (entityLiving instanceof EntityPlayer player) {
isSpectator = CACHE.getTabListCache().get(player.getUuid())
.map(PlayerListEntry::getGameMode)
.filter(gm -> gm == GameMode.SPECTATOR)
.isPresent();
}
if (entityLiving.isAlive() && !isSpectator && !World.onClimbable(entityLiving)) {
pushableEntities.add(entityLiving);
}
}
}
if (pushableEntities.isEmpty()) return;
var playerCB = getPlayerCollisionBox().inflate(0.2, -0.1, 0.2);
for (int i = 0; i < pushableEntities.size(); i++) {
var entity = pushableEntities.get(i);
var entityCB = ENTITY_DATA.getCollisionBox(entity);
if (!playerCB.intersects(entityCB)) continue;
double d = entity.getX() - getX();
double e = entity.getZ() - getZ();
double f = MathHelper.absMax(d, e);
if (f >= 0.01) {
f = Math.sqrt(f);
d /= f;
e /= f;
double g = 1.0 / f;
if (g > 1.0) {
g = 1.0;
}
d *= g;
e *= g;
d *= 0.05;
e *= 0.05;
velocity.add(-d, 0, -e);
}
}
}

private void updateVelocity(float speed, MutableVec3d movementInput) {
MutableVec3d vec3d = movementInputToVelocity(movementInput, speed, this.yaw);
this.velocity.add(vec3d);
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/zenith/util/math/MathHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,14 @@ public static long getSeed(int x, int y, int z) {
l = l * l * 42317861L + l * 11L;
return l >> 16;
}

public static double absMax(double x, double y) {
if (x < 0) {
x = -x;
}
if (y < 0) {
y = -y;
}
return Math.max(x, y);
}
}

0 comments on commit 4f51ec1

Please sign in to comment.