Skip to content

Commit

Permalink
hopefully working crab hunting
Browse files Browse the repository at this point in the history
  • Loading branch information
AViewFromTheTop committed Oct 26, 2023
1 parent 0d9e705 commit d5a5a7f
Show file tree
Hide file tree
Showing 15 changed files with 155 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"replace": false,
"values": [
"minecraft:squid",
"minecraft:glow_squid",
"minecraft:cod",
"minecraft:salmon",
"minecraft:tropical_fish",
"minecraft:tadpole"
]
}
1 change: 0 additions & 1 deletion src/main/java/net/frozenblock/wilderwild/WilderWild.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.datafix.schemas.NamespacedSchema;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.quiltmc.qsl.frozenblock.misc.datafixerupper.api.QuiltDataFixerBuilder;
import org.quiltmc.qsl.frozenblock.misc.datafixerupper.api.QuiltDataFixes;
import org.quiltmc.qsl.frozenblock.misc.datafixerupper.api.SimpleFixes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import net.frozenblock.wilderwild.registry.RegisterEntities;
import net.frozenblock.wilderwild.tag.WilderEntityTags;
import net.minecraft.core.HolderLookup;
import net.minecraft.world.entity.EntityType;
import org.jetbrains.annotations.NotNull;

final class WWEntityTagProvider extends FabricTagProvider.EntityTypeTagProvider {
Expand All @@ -36,5 +37,13 @@ public WWEntityTagProvider(@NotNull FabricDataOutput output, @NotNull Completabl
protected void addTags(@NotNull HolderLookup.Provider arg) {
this.getOrCreateTagBuilder(WilderEntityTags.STAYS_IN_MESOGLEA)
.add(RegisterEntities.JELLYFISH);

this.getOrCreateTagBuilder(WilderEntityTags.CRAB_HUNT_TARGETS)
.add(EntityType.SQUID)
.add(EntityType.GLOW_SQUID)
.add(EntityType.COD)
.add(EntityType.SALMON)
.add(EntityType.TROPICAL_FISH)
.add(EntityType.TADPOLE);
}
}
52 changes: 8 additions & 44 deletions src/main/java/net/frozenblock/wilderwild/entity/Crab.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.mojang.serialization.Dynamic;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiConsumer;
Expand All @@ -33,7 +32,6 @@
import net.frozenblock.wilderwild.registry.RegisterEntities;
import net.frozenblock.wilderwild.registry.RegisterItems;
import net.frozenblock.wilderwild.registry.RegisterMemoryModuleTypes;
import net.frozenblock.wilderwild.registry.RegisterSensorTypes;
import net.frozenblock.wilderwild.registry.RegisterSounds;
import net.frozenblock.wilderwild.tag.WilderBiomeTags;
import net.frozenblock.wilderwild.tag.WilderBlockTags;
Expand Down Expand Up @@ -83,8 +81,6 @@
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
import net.minecraft.world.entity.ai.navigation.PathNavigation;
import net.minecraft.world.entity.ai.navigation.WallClimberNavigation;
import net.minecraft.world.entity.ai.sensing.Sensor;
import net.minecraft.world.entity.ai.sensing.SensorType;
import net.minecraft.world.entity.animal.Animal;
import net.minecraft.world.entity.animal.Bucketable;
import net.minecraft.world.entity.player.Player;
Expand Down Expand Up @@ -115,45 +111,6 @@ public class Crab extends Animal implements VibrationSystem, Bucketable {
public static final int EMERGE_LENGTH_IN_TICKS = 29;
public static final double UNDERGROUND_PLAYER_RANGE = 4;
private static final Map<ServerLevelAccessor, Integer> CRABS_PER_LEVEL = new HashMap<>();
protected static final List<SensorType<? extends Sensor<? super Crab>>> SENSORS = List.of(
SensorType.NEAREST_LIVING_ENTITIES,
SensorType.NEAREST_PLAYERS,
SensorType.NEAREST_ADULT,
SensorType.HURT_BY,
RegisterSensorTypes.CRAB_TEMPTATIONS,
RegisterSensorTypes.CRAB_SPECIFIC_SENSOR,
RegisterSensorTypes.CRAB_NEARBY_PLAYER_SENSOR,
RegisterSensorTypes.CRAB_CAN_DIG_SENSOR
);
protected static final List<? extends MemoryModuleType<?>> MEMORY_MODULES = List.of(
MemoryModuleType.NEAREST_LIVING_ENTITIES,
MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES,
MemoryModuleType.NEAREST_PLAYERS,
MemoryModuleType.NEAREST_VISIBLE_PLAYER,
MemoryModuleType.NEAREST_VISIBLE_ATTACKABLE_PLAYER,
RegisterMemoryModuleTypes.IS_PLAYER_NEARBY,
MemoryModuleType.LOOK_TARGET,
MemoryModuleType.WALK_TARGET,
MemoryModuleType.CANT_REACH_WALK_TARGET_SINCE,
MemoryModuleType.PATH,
MemoryModuleType.ATTACK_TARGET,
MemoryModuleType.NEAREST_ATTACKABLE,
MemoryModuleType.ATTACK_COOLING_DOWN,
MemoryModuleType.HURT_BY,
MemoryModuleType.HURT_BY_ENTITY,
MemoryModuleType.IS_PANICKING,
MemoryModuleType.IS_EMERGING,
MemoryModuleType.DIG_COOLDOWN,
RegisterMemoryModuleTypes.CAN_DIG,
MemoryModuleType.TEMPTING_PLAYER,
MemoryModuleType.IS_TEMPTED,
MemoryModuleType.TEMPTATION_COOLDOWN_TICKS,
MemoryModuleType.BREED_TARGET,
RegisterMemoryModuleTypes.IS_UNDERGROUND,
RegisterMemoryModuleTypes.NEARBY_CRABS,
RegisterMemoryModuleTypes.HEAL_COOLDOWN_TICKS,
RegisterMemoryModuleTypes.FIRST_BRAIN_TICK
);
private static final int DIG_TICKS_UNTIL_PARTICLES = 17;
private static final int DIG_TICKS_UNTIL_STOP_PARTICLES = 82;
private static final int EMERGE_TICKS_UNTIL_PARTICLES = 1;
Expand Down Expand Up @@ -207,7 +164,7 @@ public static AttributeSupplier.Builder addAttributes() {
@Override
@NotNull
protected Brain.Provider<Crab> brainProvider() {
return Brain.provider(MEMORY_MODULES, SENSORS);
return Brain.provider(CrabAi.MEMORY_MODULES, CrabAi.SENSORS);
}

@Override
Expand Down Expand Up @@ -629,6 +586,10 @@ public void saveToBucketTag(@NotNull ItemStack stack) {
Bucketable.saveDefaultDataToBucketTag(this, stack);
CompoundTag compoundTag = stack.getOrCreateTag();
compoundTag.putInt("Age", this.getAge());
Brain<Crab> brain = this.getBrain();
if (brain.hasMemoryValue(MemoryModuleType.HAS_HUNTING_COOLDOWN)) {
compoundTag.putLong("HuntingCooldown", brain.getTimeUntilExpiry(MemoryModuleType.HAS_HUNTING_COOLDOWN));
}
}

@Override
Expand All @@ -637,6 +598,9 @@ public void loadFromBucketTag(@NotNull CompoundTag tag) {
if (tag.contains("Age")) {
this.setAge(tag.getInt("Age"));
}
if (tag.contains("HuntingCooldown")) {
this.getBrain().setMemoryWithExpiry(MemoryModuleType.HAS_HUNTING_COOLDOWN, true, tag.getLong("HuntingCooldown"));
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import net.frozenblock.wilderwild.entity.Crab;
import net.frozenblock.wilderwild.registry.RegisterEntities;
import net.frozenblock.wilderwild.registry.RegisterMemoryModuleTypes;
import net.frozenblock.wilderwild.registry.RegisterSensorTypes;
import net.frozenblock.wilderwild.tag.WilderItemTags;
import net.minecraft.util.Unit;
import net.minecraft.util.valueproviders.UniformInt;
Expand Down Expand Up @@ -56,6 +57,7 @@
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
import net.minecraft.world.entity.ai.memory.MemoryStatus;
import net.minecraft.world.entity.ai.sensing.Sensor;
import net.minecraft.world.entity.ai.sensing.SensorType;
import net.minecraft.world.entity.schedule.Activity;
import net.minecraft.world.item.crafting.Ingredient;
import org.jetbrains.annotations.NotNull;
Expand All @@ -69,16 +71,58 @@ public final class CrabAi {
private static final int DIGGING_DURATION = Crab.DIG_LENGTH_IN_TICKS;
private static final int EMERGE_DURATION = Crab.EMERGE_LENGTH_IN_TICKS;
public static final double UNDERGROUND_PLAYER_RANGE = Crab.UNDERGROUND_PLAYER_RANGE;
public static final List<SensorType<? extends Sensor<? super Crab>>> SENSORS = List.of(
SensorType.NEAREST_LIVING_ENTITIES,
SensorType.NEAREST_PLAYERS,
SensorType.NEAREST_ADULT,
SensorType.HURT_BY,
RegisterSensorTypes.CRAB_ATTACKABLES,
RegisterSensorTypes.CRAB_TEMPTATIONS,
RegisterSensorTypes.CRAB_SPECIFIC_SENSOR,
RegisterSensorTypes.CRAB_NEARBY_PLAYER_SENSOR,
RegisterSensorTypes.CRAB_CAN_DIG_SENSOR
);
public static final List<? extends MemoryModuleType<?>> MEMORY_MODULES = List.of(
MemoryModuleType.NEAREST_LIVING_ENTITIES,
MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES,
MemoryModuleType.NEAREST_PLAYERS,
MemoryModuleType.NEAREST_VISIBLE_PLAYER,
MemoryModuleType.NEAREST_VISIBLE_ATTACKABLE_PLAYER,
RegisterMemoryModuleTypes.IS_PLAYER_NEARBY,
MemoryModuleType.LOOK_TARGET,
MemoryModuleType.WALK_TARGET,
MemoryModuleType.CANT_REACH_WALK_TARGET_SINCE,
MemoryModuleType.PATH,
MemoryModuleType.ATTACK_TARGET,
MemoryModuleType.NEAREST_ATTACKABLE,
MemoryModuleType.HAS_HUNTING_COOLDOWN,
MemoryModuleType.ATTACK_COOLING_DOWN,
MemoryModuleType.HURT_BY,
MemoryModuleType.HURT_BY_ENTITY,
MemoryModuleType.IS_PANICKING,
MemoryModuleType.IS_EMERGING,
MemoryModuleType.DIG_COOLDOWN,
RegisterMemoryModuleTypes.CAN_DIG,
MemoryModuleType.TEMPTING_PLAYER,
MemoryModuleType.IS_TEMPTED,
MemoryModuleType.TEMPTATION_COOLDOWN_TICKS,
MemoryModuleType.BREED_TARGET,
RegisterMemoryModuleTypes.IS_UNDERGROUND,
RegisterMemoryModuleTypes.NEARBY_CRABS,
RegisterMemoryModuleTypes.HEAL_COOLDOWN_TICKS,
RegisterMemoryModuleTypes.FIRST_BRAIN_TICK
);

private static final BehaviorControl<Crab> DIG_COOLDOWN_SETTER = BehaviorBuilder.create(instance -> instance.group(instance.registered(MemoryModuleType.DIG_COOLDOWN)).apply(instance, memoryAccessor -> (world, crab, l) -> {
if (instance.tryGet(memoryAccessor).isPresent()) {
memoryAccessor.setWithExpiry(Unit.INSTANCE, getRandomDigCooldown(crab));
}
return true;
}));
private static final BehaviorControl<Crab> EMERGE_COOLDOWN_SETTER = BehaviorBuilder.create(instance -> instance.group(instance.registered(MemoryModuleType.DIG_COOLDOWN)).apply(instance, memoryAccessor -> (world, crab, l) -> {

private static final BehaviorControl<Crab> HUNTING_COOLDOWN_SETTER = BehaviorBuilder.create(instance -> instance.group(instance.registered(MemoryModuleType.HAS_HUNTING_COOLDOWN)).apply(instance, memoryAccessor -> (world, crab, l) -> {
if (instance.tryGet(memoryAccessor).isPresent()) {
memoryAccessor.setWithExpiry(Unit.INSTANCE, getRandomEmergeCooldown(crab));
memoryAccessor.setWithExpiry(true, 2400);
}
return true;
}));
Expand Down Expand Up @@ -195,6 +239,7 @@ private static void addFightActivity(@NotNull Crab crab, @NotNull Brain<Crab> br
10,
ImmutableList.of(
DIG_COOLDOWN_SETTER,
HUNTING_COOLDOWN_SETTER,
StopAttackingIfTargetInvalid.create(
livingEntity -> !crab.canTargetEntity(livingEntity), CrabAi::onTargetInvalid, true
),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2023 FrozenBlock
* This file is part of Wilder Wild.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, see <https://www.gnu.org/licenses/>.
*/

package net.frozenblock.wilderwild.entity.ai.crab;

import net.frozenblock.wilderwild.tag.WilderEntityTags;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
import net.minecraft.world.entity.ai.sensing.NearestVisibleLivingEntitySensor;
import net.minecraft.world.entity.ai.sensing.Sensor;
import org.jetbrains.annotations.NotNull;

public class CrabAttackablesSensor extends NearestVisibleLivingEntitySensor {

public CrabAttackablesSensor() {

}

protected boolean isMatchingEntity(@NotNull LivingEntity attacker, @NotNull LivingEntity target) {
return this.isClose(attacker, target) && this.isHuntTarget(attacker, target) && Sensor.isEntityAttackable(attacker, target);
}

private boolean isHuntTarget(@NotNull LivingEntity attacker, LivingEntity target) {
return !attacker.getBrain().hasMemoryValue(MemoryModuleType.HAS_HUNTING_COOLDOWN) && target.getType().is(WilderEntityTags.CRAB_HUNT_TARGETS);
}

private boolean isClose(LivingEntity attacker, @NotNull LivingEntity target) {
return Math.sqrt(target.distanceToSqr(attacker)) <= 8D;
}

@NotNull
protected MemoryModuleType<LivingEntity> getMemory() {
return MemoryModuleType.NEAREST_ATTACKABLE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@
import org.jetbrains.annotations.NotNull;

public class CrabCanDigSensor extends Sensor<Crab> {

@Override
@NotNull
public Set<MemoryModuleType<?>> requires() {
return ImmutableSet.of(RegisterMemoryModuleTypes.CAN_DIG);
}

@Override
protected void doTick(ServerLevel level, @NotNull Crab crab) {
protected void doTick(@NotNull ServerLevel level, @NotNull Crab crab) {
Brain<?> brain = crab.getBrain();
if (crab.canHideOnGround()) {
brain.setMemory(RegisterMemoryModuleTypes.CAN_DIG, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.jetbrains.annotations.NotNull;

public class CrabDig<E extends Crab> extends Behavior<E> {

public CrabDig(int duration) {
super(
Map.of(
Expand All @@ -43,20 +44,20 @@ public CrabDig(int duration) {
}

@Override
protected boolean canStillUse(ServerLevel level, E entity, long gameTime) {
protected boolean canStillUse(@NotNull ServerLevel level, @NotNull E entity, long gameTime) {
return true;
}

@Override
protected void start(ServerLevel level, @NotNull E crab, long gameTime) {
protected void start(@NotNull ServerLevel level, @NotNull E crab, long gameTime) {
crab.endNavigation();
crab.setPose(Pose.DIGGING);
crab.playSound(RegisterSounds.ENTITY_CRAB_DIG, 0.5F, 1.0F);
crab.resetDiggingTicks();
}

@Override
protected void stop(ServerLevel level, @NotNull E crab, long gameTime) {
protected void stop(@NotNull ServerLevel level, @NotNull E crab, long gameTime) {
if (crab.hasPose(Pose.DIGGING)) {
crab.getBrain().setMemory(RegisterMemoryModuleTypes.IS_UNDERGROUND, true);
crab.getBrain().setMemoryWithExpiry(MemoryModuleType.DIG_COOLDOWN, Unit.INSTANCE, CrabAi.getRandomEmergeCooldown(crab));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,25 @@
import org.jetbrains.annotations.NotNull;

public class CrabEmerge<E extends Crab> extends Behavior<E> {

public CrabEmerge(int duration) {
super(ImmutableMap.of(MemoryModuleType.IS_EMERGING, MemoryStatus.VALUE_PRESENT), duration);
}

@Override
protected boolean canStillUse(ServerLevel level, E entity, long gameTime) {
protected boolean canStillUse(@NotNull ServerLevel level, @NotNull E entity, long gameTime) {
return true;
}

@Override
protected void start(ServerLevel level, @NotNull E crab, long gameTime) {
protected void start(@NotNull ServerLevel level, @NotNull E crab, long gameTime) {
crab.setPose(Pose.EMERGING);
crab.playSound(RegisterSounds.ENTITY_CRAB_EMERGE, 0.5F, 1.0F);
crab.resetDiggingTicks();
}

@Override
protected void stop(ServerLevel level, @NotNull E crab, long gameTime) {
protected void stop(@NotNull ServerLevel level, @NotNull E crab, long gameTime) {
if (crab.hasPose(Pose.EMERGING)) {
crab.setPose(Pose.STANDING);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@
import net.minecraft.world.entity.ai.behavior.BehaviorControl;
import net.minecraft.world.entity.ai.behavior.declarative.BehaviorBuilder;
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

public class CrabHeal {
public static BehaviorControl<Crab> create() {

@Contract(" -> new")
public static @NotNull BehaviorControl<Crab> create() {
return BehaviorBuilder.create(instance -> instance.group(
instance.present(RegisterMemoryModuleTypes.IS_UNDERGROUND),
instance.present(MemoryModuleType.DIG_COOLDOWN),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@
import org.jetbrains.annotations.NotNull;

public class CrabNearbyPlayerSensor extends Sensor<Crab> {

@Override
@NotNull
public Set<MemoryModuleType<?>> requires() {
return ImmutableSet.of(MemoryModuleType.NEAREST_PLAYERS, RegisterMemoryModuleTypes.IS_PLAYER_NEARBY);
}

@Override
protected void doTick(ServerLevel level, @NotNull Crab crab) {
protected void doTick(@NotNull ServerLevel level, @NotNull Crab crab) {
Brain<?> brain = crab.getBrain();
if (brain.hasMemoryValue(MemoryModuleType.NEAREST_PLAYERS) && !brain.getMemory(MemoryModuleType.NEAREST_PLAYERS).get().isEmpty()) {
brain.setMemory(RegisterMemoryModuleTypes.IS_PLAYER_NEARBY, true);
Expand Down
Loading

0 comments on commit d5a5a7f

Please sign in to comment.