Skip to content

Commit

Permalink
Debugging cat movement
Browse files Browse the repository at this point in the history
  • Loading branch information
paulyhedral committed Jan 18, 2024
1 parent 57e32e7 commit 092bdbc
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,8 @@ public ClientConfig(ForgeConfigSpec.Builder builder) {
SKITTISH_ANIMALS = builder.comment("Sets the likelihood (in percent) that a skittish cat will avoid other animals.").translation("catherder.config.client.skittish_animals").defineInRange("skittish_animals", 40, 1, 100);
SKITTISH_OTHERS = builder.comment("Sets the likelihood (in percent) that a skittish cat will avoid any other creature.").translation("catherder.config.client.skittish_others").defineInRange("skittish_others", 75, 1, 100);
SKITTISH_TWITCHINESS = builder.comment("Sets how sensitive the check for fleeing from an entity is.").translation("catherder.config.client.skittish_twitchiness").defineInRange("skittish_twitchiness", 750, 1, 1000);

builder.pop();
}

{
builder.push("Domestic");

MAX_ITEM_DISTANCE = builder.comment("Sets the maximum distance domestic items may be away from each other to be considered part of the cat's wandering area.").translation("catherder.config.client.domestic_item_max_distance").defineInRange("domestic_item_max_distance", 40, 20, 100);
MAX_WANDER_DISTANCE = builder.comment("Sets the maximum distance a cat in wander mode will wander from its owner.").translation("catherder.config.client.wander_max_distance").defineInRange("wander_max_distance", 200, 20, 1000);
MAX_WANDER_DISTANCE = builder.comment("Sets the maximum distance a cat in wander mode will wander from its owner.").translation("catherder.config.client.wander_max_distance").defineInRange("wander_max_distance", 600, 200, 1000);

builder.pop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public class CatEntity extends AbstractCatEntity {

private static final EntityDataAccessor<Byte> SIZE = SynchedEntityData.defineId(CatEntity.class, EntityDataSerializers.BYTE);
private static final EntityDataAccessor<ItemStack> TOY_VARIANT = SynchedEntityData.defineId(CatEntity.class, EntityDataSerializers.ITEM_STACK);
private static final EntityDataAccessor<Boolean> IS_LYING = SynchedEntityData.defineId(CatEntity.class, EntityDataSerializers.BOOLEAN);
private static final EntityDataAccessor<Boolean> IS_LYING = SynchedEntityData.defineId(CatEntity.class, EntityDataSerializers.BOOLEAN);
private static final EntityDataAccessor<Boolean> RELAX_STATE_ONE = SynchedEntityData.defineId(CatEntity.class, EntityDataSerializers.BOOLEAN);

// Use Cache.make to ensure static fields are not initialised too early (before Serializers have been registered)
Expand Down Expand Up @@ -229,12 +229,12 @@ protected void registerGoals() {

this.goalSelector.addGoal(6, new FetchGoal(this, 1.3D, 32.0F));
this.goalSelector.addGoal(6, new CatDomesticWanderGoal(this, 1.0D, ConfigHandler.CLIENT.MAX_ITEM_DISTANCE.get()));
this.goalSelector.addGoal(6, new CatWanderGoal(this, 1.0D, ConfigHandler.CLIENT.MAX_WANDER_DISTANCE.get()));
// this.goalSelector.addGoal(6, new CatWanderGoal(this, 1.0D, ConfigHandler.CLIENT.MAX_WANDER_DISTANCE.get()));

this.goalSelector.addGoal(7, new CatLieOnBedGoal<>(this, 1.1F, 16));
this.goalSelector.addGoal(7, new CatSitOnBlockGoal<>(this, 0.8F));
this.goalSelector.addGoal(7, new CatFollowOwnerGoal(this, 1.0D, 20.0F, 4.0F));

this.goalSelector.addGoal(9, new CatFollowOwnerGoal(this, 1.0D, 10.0F, 2.0F));
this.goalSelector.addGoal(9, new CatLieOnBedGoal<>(this, 1.1F, 16));
this.goalSelector.addGoal(9, new CatSitOnBlockGoal<>(this, 0.8F));

this.goalSelector.addGoal(10, new UseLitterboxGoal<>(this, 10));

Expand Down Expand Up @@ -850,7 +850,7 @@ else if(target instanceof Player && owner instanceof Player && !((Player) owner)
// return false;
// }

return !(target instanceof TamableAnimal) || !((TamableAnimal) target).isTame();
return !(target instanceof TamableAnimal) || !((TamableAnimal) target).isTame();
}

// TODO
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.sweetrpg.catherder.api.feature.Mode;
import com.sweetrpg.catherder.api.inferface.IThrowableItem;
import com.sweetrpg.catherder.common.config.ConfigHandler;
import com.sweetrpg.catherder.common.entity.CatEntity;
import com.sweetrpg.catherder.common.util.EntityUtil;
import net.minecraft.world.entity.LivingEntity;
Expand Down Expand Up @@ -42,9 +43,9 @@ public boolean canUse() {
if(owner == null) {
return false;
}
// else if (this.cat.getMode() == Mode.PATROL) {
// return false;
// }
else if (this.cat.isMode(Mode.DOMESTIC)) {
return false;
}
else if(owner.isSpectator()) {
return false;
}
Expand All @@ -67,6 +68,9 @@ public boolean canContinueToUse() {
else if(this.cat.isInSittingPose()) {
return false;
}
else if (this.cat.isMode(Mode.DOMESTIC)) {
return false;
}

return this.cat.distanceToSqr(this.owner) > (this.stopDist * this.stopDist);
}
Expand Down Expand Up @@ -102,7 +106,12 @@ public void tick() {
if(--this.timeToRecalcPath <= 0) {
this.timeToRecalcPath = 10;
if(!this.cat.isLeashed() && !this.cat.isPassenger()) { // Is not leashed and is not a passenger
if(this.cat.distanceToSqr(this.owner) >= 400.0D) { // Further than ? blocks away teleport (12 units == one block?)
var wanderMaxDistance = 400.0D;
if(this.cat.getMode() == Mode.WANDERING) {
wanderMaxDistance = ConfigHandler.CLIENT.MAX_WANDER_DISTANCE.get();
}

if(this.cat.distanceToSqr(this.owner) >= wanderMaxDistance) { // Further than ? blocks away teleport (12 units == one block?)
EntityUtil.tryToTeleportNearEntity(this.cat, this.navigator, this.owner, 4);
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import com.sweetrpg.catherder.CatHerder;
import com.sweetrpg.catherder.api.feature.Mode;
import com.sweetrpg.catherder.api.inferface.IThrowableItem;
import com.sweetrpg.catherder.common.config.ConfigHandler;
import com.sweetrpg.catherder.common.entity.CatEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.util.RandomSource;
import net.minecraft.world.entity.ai.goal.Goal;
import net.minecraft.world.entity.ai.navigation.PathNavigation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.pathfinder.BlockPathTypes;
import net.minecraft.world.phys.Vec3;

import javax.annotation.Nullable;
Expand All @@ -16,21 +20,21 @@
public class CatWanderGoal extends Goal {

protected final CatEntity cat;

private final PathNavigation navigator;
protected final double speed;
protected final float maximumDistance;
protected int executionChance;
private float oldWaterCost;

private final double NUM_BLOCKS_AWAY = 15;
private final double BLOCK_SIZE = 12;
private final double MAX_DISTANCE = NUM_BLOCKS_AWAY * BLOCK_SIZE;

public CatWanderGoal(CatEntity catIn, double speedIn, float maxiumDistance) {
public CatWanderGoal(CatEntity catIn, double speedIn) {
this.cat = catIn;
this.speed = speedIn;
this.maximumDistance = maxiumDistance;
this.navigator = catIn.getNavigation();
this.executionChance = 60;
this.setFlags(EnumSet.of(Goal.Flag.MOVE));
this.setFlags(EnumSet.of(Goal.Flag.MOVE, Goal.Flag.LOOK));
}

@Override
Expand All @@ -55,7 +59,32 @@ public boolean canUse() {
BlockPos catPos = this.cat.blockPosition();

// if the owner is more than 40-ish blocks away
return (ownerPos.distSqr(catPos) < (this.maximumDistance * this.maximumDistance));
var maximumDistance = ConfigHandler.CLIENT.MAX_WANDER_DISTANCE.get();
return (ownerPos.distSqr(catPos) < (maximumDistance * maximumDistance));
}

@Override
public boolean canContinueToUse() {
if(this.navigator.isDone()) {
return false;
}
else if(this.cat.isInSittingPose()) {
return false;
}

return true;
}

@Override
public void start() {
this.oldWaterCost = this.cat.getPathfindingMalus(BlockPathTypes.WATER);
this.cat.setPathfindingMalus(BlockPathTypes.WATER, 0.0F);
}

@Override
public void stop() {
this.navigator.stop();
this.cat.setPathfindingMalus(BlockPathTypes.WATER, this.oldWaterCost);
}

@Override
Expand All @@ -74,7 +103,7 @@ else if(this.cat.getRandom().nextInt(this.executionChance) != 0) {
Vec3 pos = this.getPosition();
// if(pos != null) {
this.cat.setOnGround(true);
if(this.cat.getNavigation().moveTo(pos.x, pos.y, pos.z, this.speed)) {
if(this.navigator.moveTo(pos.x, pos.y, pos.z, this.speed)) {
CatHerder.LOGGER.debug("Cat {} is moving to {}", this.cat, pos);
}
else {
Expand All @@ -85,7 +114,6 @@ else if(this.cat.getRandom().nextInt(this.executionChance) != 0) {

// @Nullable
protected Vec3 getPosition() {
PathNavigation pathNavigate = this.cat.getNavigation();
RandomSource random = this.cat.getRandom();

int xzRange = 5;
Expand All @@ -101,7 +129,7 @@ protected Vec3 getPosition() {

BlockPos testPos = bestPos.offset(x, y, z);

if(pathNavigate.isStableDestination(testPos)) {
if(this.navigator.isStableDestination(testPos)) {
float weight = this.cat.getWalkTargetValue(testPos);

if(weight > bestWeight) {
Expand Down

0 comments on commit 092bdbc

Please sign in to comment.