Skip to content

Commit

Permalink
fix no ai in water bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Treetrain1 committed Oct 23, 2023
1 parent e8c505f commit c0f3072
Showing 1 changed file with 41 additions and 20 deletions.
61 changes: 41 additions & 20 deletions src/main/java/net/frozenblock/wilderwild/entity/ai/crab/CrabAi.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@
import com.google.common.collect.ImmutableSet;
import com.mojang.datafixers.util.Pair;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import net.frozenblock.wilderwild.entity.Crab;
import net.frozenblock.wilderwild.registry.RegisterEntities;
import net.frozenblock.wilderwild.registry.RegisterMemoryModuleTypes;
import net.frozenblock.wilderwild.tag.WilderItemTags;
import net.minecraft.util.Unit;
import net.minecraft.util.valueproviders.UniformInt;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.Brain;
import net.minecraft.world.entity.ai.attributes.Attributes;
Expand All @@ -41,12 +45,14 @@
import net.minecraft.world.entity.ai.behavior.DoNothing;
import net.minecraft.world.entity.ai.behavior.EraseMemoryIf;
import net.minecraft.world.entity.ai.behavior.FollowTemptation;
import net.minecraft.world.entity.ai.behavior.GateBehavior;
import net.minecraft.world.entity.ai.behavior.LookAtTargetSink;
import net.minecraft.world.entity.ai.behavior.MeleeAttack;
import net.minecraft.world.entity.ai.behavior.MoveToTargetSink;
import net.minecraft.world.entity.ai.behavior.RandomStroll;
import net.minecraft.world.entity.ai.behavior.RunOne;
import net.minecraft.world.entity.ai.behavior.SetEntityLookTarget;
import net.minecraft.world.entity.ai.behavior.SetEntityLookTargetSometimes;
import net.minecraft.world.entity.ai.behavior.SetWalkTargetFromAttackTargetIfTargetOutOfReach;
import net.minecraft.world.entity.ai.behavior.StartAttacking;
import net.minecraft.world.entity.ai.behavior.StopAttackingIfTargetInvalid;
Expand All @@ -61,6 +67,9 @@
import org.jetbrains.annotations.Nullable;

public final class CrabAi {
private static final float SPEED_MODIFIER = 1F;
private static final float FOLLOWING_ADULT_SPEED_MODIFIER = 1.2F;
private static final float CHASING_SPEED_MODIFIER = 1.3F;
private static final UniformInt ADULT_FOLLOW_RANGE = UniformInt.of(5, 16);
private static final int DIGGING_DURATION = Crab.DIG_LENGTH_IN_TICKS;
private static final int EMERGE_DURATION = Crab.EMERGE_LENGTH_IN_TICKS;
Expand All @@ -80,18 +89,18 @@ public final class CrabAi {
}));

public static void updateActivity(@NotNull Crab crab) {
crab.getBrain().setActiveActivityToFirstValid(ImmutableList.of(Activity.EMERGE, Activity.DIG, Activity.HIDE, Activity.FIGHT, Activity.IDLE));
crab.getBrain().setActiveActivityToFirstValid(List.of(Activity.EMERGE, Activity.DIG, Activity.HIDE, Activity.FIGHT, Activity.IDLE));
}

@NotNull
public static Brain<?> makeBrain(@NotNull Crab crab, @NotNull Brain<Crab> brain) {
public static Brain<Crab> makeBrain(@NotNull Crab crab, @NotNull Brain<Crab> brain) {
addCoreActivity(brain);
initEmergeActivity(brain);
initDiggingActivity(brain);
initHideActivity(brain);
addIdleActivity(brain);
addFightActivity(crab, brain);
brain.setCoreActivities(ImmutableSet.of(Activity.CORE));
brain.setCoreActivities(Set.of(Activity.CORE));
brain.setDefaultActivity(Activity.IDLE);
brain.useDefaultActivity();
return brain;
Expand Down Expand Up @@ -127,7 +136,7 @@ private static void initDiggingActivity(@NotNull Brain<Crab> brain) {
Pair.of(0, new ForceUnmount()),
Pair.of(1, new CrabDig<>(DIGGING_DURATION))
),
ImmutableSet.of(
Set.of(
Pair.of(RegisterMemoryModuleTypes.FIRST_BRAIN_TICK, MemoryStatus.VALUE_PRESENT),
Pair.of(MemoryModuleType.DIG_COOLDOWN, MemoryStatus.VALUE_ABSENT),
Pair.of(RegisterMemoryModuleTypes.IS_UNDERGROUND, MemoryStatus.VALUE_ABSENT),
Expand All @@ -146,7 +155,7 @@ private static void initHideActivity(@NotNull Brain<Crab> brain) {
Pair.of(0, CrabTryToEmerge.create()),
Pair.of(1, CrabHeal.create())
),
ImmutableSet.of(
Set.of(
Pair.of(MemoryModuleType.DIG_COOLDOWN, MemoryStatus.VALUE_PRESENT),
Pair.of(RegisterMemoryModuleTypes.IS_UNDERGROUND, MemoryStatus.VALUE_PRESENT)
)
Expand All @@ -156,21 +165,33 @@ private static void initHideActivity(@NotNull Brain<Crab> brain) {
private static void addIdleActivity(@NotNull Brain<Crab> brain) {
brain.addActivity(
Activity.IDLE,
10,
ImmutableList.of(
new AnimalMakeLove(RegisterEntities.CRAB, 0.8F),
new RunOne<>(
List.of(
Pair.of(new FollowTemptation(CrabAi::getSpeedModifier), 1),
Pair.of(BabyFollowAdult.create(ADULT_FOLLOW_RANGE, CrabAi::getSpeedModifierFollowingAdult), 1)
Pair.of(0, SetEntityLookTargetSometimes.create(EntityType.PLAYER, 6.0F, UniformInt.of(30, 60))),
Pair.of(1, new AnimalMakeLove(RegisterEntities.CRAB, 0.8F)),
Pair.of(
2,
new RunOne<>(
List.of(
Pair.of(new FollowTemptation(CrabAi::getSpeedModifier), 1),
Pair.of(BabyFollowAdult.create(ADULT_FOLLOW_RANGE, CrabAi::getSpeedModifierFollowingAdult), 1)
)
)
),
StartAttacking.create(CrabAi::findNearestValidAttackTarget),
new RunOne<>(
ImmutableMap.of(MemoryModuleType.WALK_TARGET, MemoryStatus.VALUE_ABSENT, RegisterMemoryModuleTypes.FIRST_BRAIN_TICK, MemoryStatus.VALUE_PRESENT),
List.of(
Pair.of(RandomStroll.stroll(1F), 1),
Pair.of(new DoNothing(30, 100), 2)
Pair.of(3, StartAttacking.create(CrabAi::findNearestValidAttackTarget)),
Pair.of(
4,
new GateBehavior<>(
Map.of(MemoryModuleType.WALK_TARGET, MemoryStatus.VALUE_ABSENT, RegisterMemoryModuleTypes.FIRST_BRAIN_TICK, MemoryStatus.VALUE_PRESENT),
ImmutableSet.of(),
GateBehavior.OrderPolicy.ORDERED,
GateBehavior.RunningPolicy.TRY_ALL,
List.of(
Pair.of(RandomStroll.swim(1F), 2),
Pair.of(RandomStroll.stroll(1F), 2),
Pair.of(new DoNothing(30, 100), 3),
Pair.of(BehaviorBuilder.triggerIf(Entity::isInWaterOrBubble), 5),
Pair.of(BehaviorBuilder.triggerIf(Entity::onGround), 5)
)
)
)
)
Expand Down Expand Up @@ -200,15 +221,15 @@ private static boolean isTarget(@NotNull Crab crab, @NotNull LivingEntity living
}

private static float getSpeedModifierChasing(@Nullable LivingEntity livingEntity) {
return 1.3F;
return CHASING_SPEED_MODIFIER;
}

private static float getSpeedModifierFollowingAdult(LivingEntity entity) {
return 1.2F;
return FOLLOWING_ADULT_SPEED_MODIFIER;
}

private static float getSpeedModifier(LivingEntity entity) {
return 1F;
return SPEED_MODIFIER;
}

private static void onTargetInvalid(@NotNull Crab crab, @NotNull LivingEntity target) {
Expand Down

0 comments on commit c0f3072

Please sign in to comment.