Skip to content

Commit

Permalink
Implement entity lookat for target entities
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Apr 3, 2024
1 parent 97f2d16 commit 702052e
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,27 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.soulfiremc.generator.generators;
package com.soulfiremc.generator;

import com.google.gson.JsonElement;
import com.google.gson.internal.Streams;
import com.google.gson.stream.JsonWriter;
import com.soulfiremc.generator.generators.AttributesDataGenerator;
import com.soulfiremc.generator.generators.AttributesJavaGenerator;
import com.soulfiremc.generator.generators.BlockCollisionShapesDataGenerator;
import com.soulfiremc.generator.generators.BlocksDataGenerator;
import com.soulfiremc.generator.generators.BlocksJavaGenerator;
import com.soulfiremc.generator.generators.EffectsDataGenerator;
import com.soulfiremc.generator.generators.EnchantmentsDataGenerator;
import com.soulfiremc.generator.generators.EntitiesDataGenerator;
import com.soulfiremc.generator.generators.EntitiesJavaGenerator;
import com.soulfiremc.generator.generators.IDataGenerator;
import com.soulfiremc.generator.generators.ItemsDataGenerator;
import com.soulfiremc.generator.generators.ItemsJavaGenerator;
import com.soulfiremc.generator.generators.LanguageDataGenerator;
import com.soulfiremc.generator.generators.MapColorJavaGenerator;
import com.soulfiremc.generator.generators.TagsDataGenerator;
import com.soulfiremc.generator.generators.WorldExporterGenerator;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
*/
package com.soulfiremc.generator.mixin;

import com.soulfiremc.generator.DataGenerators;
import com.soulfiremc.generator.Main;
import com.soulfiremc.generator.generators.DataGenerators;
import java.nio.file.Path;
import net.minecraft.DetectedVersion;
import net.minecraft.server.MinecraftServer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ public void onLoad() {
var bestVisiblePoint = control.getEntityVisiblePoint(target);
if (bestVisiblePoint != null) {
distance =
bestVisiblePoint.distance(bot.sessionDataManager().clientEntity().getEyePosition());
bestVisiblePoint.distance(bot.sessionDataManager().clientEntity().eyePosition());
} else {
distance =
target
.getEyePosition()
.distance(bot.sessionDataManager().clientEntity().getEyePosition());
.eyePosition()
.distance(bot.sessionDataManager().clientEntity().eyePosition());
}

if (distance > lookRange) {
Expand All @@ -97,7 +97,7 @@ public void onLoad() {
} else {
bot.sessionDataManager()
.clientEntity()
.lookAt(RotationOrigin.EYES, RotationOrigin.EYES, target);
.lookAt(RotationOrigin.EYES, target.originPosition(RotationOrigin.EYES));
}

TickHookContext.INSTANCE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void placeBlock(Hand hand, Vector3i againstBlock, Direction againstFace)
return;
}

var eyePosition = clientEntity.getEyePosition();
var eyePosition = clientEntity.eyePosition();

var againstPlacePosition = getMiddleBlockFace(againstBlock, againstFace);

Expand All @@ -127,7 +127,7 @@ public void placeBlock(Hand hand, Vector3i againstBlock, Direction againstFace)
rayCastToBlock(
levelState.getBlockStateAt(againstBlock),
eyePosition,
clientEntity.getRotationVector(),
clientEntity.rotationVector(),
againstBlock);
if (rayCast.isEmpty()) {
return;
Expand Down Expand Up @@ -166,8 +166,8 @@ public void sendEndBreakBlock(Vector3i blockPos) {

public Direction getBlockFaceLookedAt(Vector3i blockPos) {
var clientEntity = dataManager.clientEntity();
var eyePosition = clientEntity.getEyePosition();
var headRotation = clientEntity.getRotationVector();
var eyePosition = clientEntity.eyePosition();
var headRotation = clientEntity.rotationVector();
var blockPosDouble = blockPos.toDouble();
var blockBoundingBox = new AABB(blockPosDouble, blockPosDouble.add(1, 1, 1));
var intersection =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public Vector3d getEntityVisiblePoint(Entity entity) {
}
}

var eye = dataManager.clientEntity().getEyePosition();
var eye = dataManager.clientEntity().eyePosition();

// sort by distance to the bot
points.sort(Comparator.comparingDouble(eye::distance));
Expand Down Expand Up @@ -287,7 +287,7 @@ public boolean canSee(Vector3d vec) { // intensive method, don't use it too ofte
return false;
}

var eye = dataManager.clientEntity().getEyePosition();
var eye = dataManager.clientEntity().eyePosition();
var distance = eye.distance(vec);
if (distance >= 256) {
return false;
Expand All @@ -312,7 +312,7 @@ public float getHitItemCooldownTicks() {

return (float)
(1.0
/ dataManager.clientEntity().getAttributeValue(AttributeType.GENERIC_ATTACK_SPEED)
/ dataManager.clientEntity().attributeValue(AttributeType.GENERIC_ATTACK_SPEED)
* 20.0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,15 @@ public void onPosition(ClientboundPlayerPositionPacket packet) {

@EventHandler
public void onLookAt(ClientboundPlayerLookAtPacket packet) {
clientEntity.lookAt(
packet.getOrigin(), Vector3d.from(packet.getX(), packet.getY(), packet.getZ()));
var targetPosition = Vector3d.from(packet.getX(), packet.getY(), packet.getZ());
if (packet.getTargetEntityOrigin() != null) {
var entity = entityTrackerState.getEntity(packet.getTargetEntityId());
if (entity != null) {
targetPosition = entity.originPosition(packet.getTargetEntityOrigin());
}
}

// TODO: Implement entity look at
clientEntity.lookAt(packet.getOrigin(), targetPosition);
}

@EventHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ public float getSpeed() {
playerSpeedAttribute.modifiers().remove(physics.sprintingUUID);
}

return (float) clientEntity.getAttributeValue(AttributeType.GENERIC_MOVEMENT_SPEED);
return (float) clientEntity.attributeValue(AttributeType.GENERIC_MOVEMENT_SPEED);
}

public void moveEntity(LevelState world, double dx, double dy, double dz) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public void handleEntityEvent(EntityEvent event) {
}

@Override
public double getEyeHeight() {
public double eyeHeight() {
if (this.controlState.sneaking()) {
return sessionDataManager
.connection()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,11 @@ public void handleEntityEvent(EntityEvent event) {
log.debug("Unhandled entity event for entity {}: {}", entityId, event.name());
}

public void lookAt(RotationOrigin origin, RotationOrigin entityOrigin, Entity entity) {
lookAt(origin, entityOrigin == RotationOrigin.EYES ? entity.getEyePosition() : entity.pos());
public Vector3d originPosition(RotationOrigin origin) {
return switch (origin) {
case EYES -> eyePosition();
case FEET -> pos();
};
}

/**
Expand All @@ -98,11 +101,11 @@ public void lookAt(RotationOrigin origin, RotationOrigin entityOrigin, Entity en
* @param position The block or location to look at.
*/
public void lookAt(RotationOrigin origin, Vector3d position) {
var eyes = origin == RotationOrigin.EYES;
var originPosition = originPosition(origin);

var dx = position.getX() - x;
var dy = position.getY() - (eyes ? y + getEyeHeight() : y);
var dz = position.getZ() - z;
var dx = position.getX() - originPosition.getX();
var dy = position.getY() - originPosition.getY();
var dz = position.getZ() - originPosition.getZ();

var sqr = Math.sqrt(dx * dx + dz * dz);

Expand All @@ -112,15 +115,15 @@ public void lookAt(RotationOrigin origin, Vector3d position) {
MathHelper.wrapDegrees((float) (Math.atan2(dz, dx) * 180.0F / (float) Math.PI) - 90.0F);
}

public double getEyeHeight() {
public double eyeHeight() {
return 1.62F;
}

public Vector3d getEyePosition() {
return Vector3d.from(x, y + getEyeHeight(), z);
public Vector3d eyePosition() {
return Vector3d.from(x, y + eyeHeight(), z);
}

public Vector3d getRotationVector() {
public Vector3d rotationVector() {
var yawRadians = (float) Math.toRadians(yaw);
var pitchRadians = (float) Math.toRadians(pitch);
var x = -Math.sin(yawRadians) * Math.cos(pitchRadians);
Expand Down Expand Up @@ -159,7 +162,7 @@ public AABB boundingBox(double x, double y, double z) {
return new AABB(x - w, y, z - w, x + w, y + h, z + w);
}

public double getAttributeValue(AttributeType type) {
public double attributeValue(AttributeType type) {
return attributeState.getOrCreateAttribute(type).calculateValue();
}
}

0 comments on commit 702052e

Please sign in to comment.