Skip to content

Commit

Permalink
large scale AI refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
DavieDoo committed May 19, 2022
1 parent 94e7ad3 commit c80a85f
Show file tree
Hide file tree
Showing 16 changed files with 437 additions and 362 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.spectrobes.spectrobesmod.common.entities.goals;

import com.spectrobes.spectrobesmod.common.entities.spectrobes.EntityAquaticSpectrobe;
import net.minecraft.entity.Entity;
import net.minecraft.entity.ai.goal.JumpGoal;
import net.minecraft.entity.passive.DolphinEntity;
import net.minecraft.fluid.FluidState;
import net.minecraft.tags.FluidTags;
import net.minecraft.util.Direction;
import net.minecraft.util.SoundEvents;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.vector.Vector3d;

public class AquaticJumpGoal extends JumpGoal {
private static final int[] STEPS_TO_CHECK = new int[]{0, 1, 4, 5, 6, 7};
private final EntityAquaticSpectrobe spectrobe;
private final int interval;
private boolean breached;

public AquaticJumpGoal(EntityAquaticSpectrobe p_i50329_1_, int p_i50329_2_) {
this.spectrobe = p_i50329_1_;
this.interval = p_i50329_2_;
}

/**
* Returns whether execution should begin. You can also read and cache any state necessary for execution in this
* method as well.
*/
public boolean canUse() {
if (this.spectrobe.getRandom().nextInt(this.interval) != 0) {
return false;
} else {
Direction direction = this.spectrobe.getMotionDirection();
int i = direction.getStepX();
int j = direction.getStepZ();
BlockPos blockpos = this.spectrobe.blockPosition();

for(int k : STEPS_TO_CHECK) {
if (!this.waterIsClear(blockpos, i, j, k) || !this.surfaceIsClear(blockpos, i, j, k)) {
return false;
}
}

return true;
}
}

private boolean waterIsClear(BlockPos pPos, int pDx, int pDz, int pScale) {
BlockPos blockpos = pPos.offset(pDx * pScale, 0, pDz * pScale);
return this.spectrobe.level.getFluidState(blockpos).is(FluidTags.WATER) && !this.spectrobe.level.getBlockState(blockpos).getMaterial().blocksMotion();
}

private boolean surfaceIsClear(BlockPos pPos, int pDx, int pDz, int pScale) {
return this.spectrobe.level.getBlockState(pPos.offset(pDx * pScale, 1, pDz * pScale)).isAir() && this.spectrobe.level.getBlockState(pPos.offset(pDx * pScale, 2, pDz * pScale)).isAir();
}

/**
* Returns whether an in-progress EntityAIBase should continue executing
*/
public boolean canContinueToUse() {
double d0 = this.spectrobe.getDeltaMovement().y;
return (!(d0 * d0 < (double)0.03F) || this.spectrobe.xRot == 0.0F || !(Math.abs(this.spectrobe.xRot) < 10.0F) || !this.spectrobe.isInWater()) && !this.spectrobe.isOnGround();
}

public boolean isInterruptable() {
return false;
}

/**
* Execute a one shot task or start executing a continuous task
*/
public void start() {
Direction direction = this.spectrobe.getMotionDirection();
this.spectrobe.setDeltaMovement(this.spectrobe.getDeltaMovement().add((double)direction.getStepX() * 0.6D, 0.7D, (double)direction.getStepZ() * 0.6D));
this.spectrobe.getNavigation().stop();
}

/**
* Reset the task's internal state. Called when this task is interrupted by another one
*/
public void stop() {
this.spectrobe.xRot = 0.0F;
}

/**
* Keep ticking a continuous task that has already been started
*/
public void tick() {
boolean flag = this.breached;
if (!flag) {
FluidState fluidstate = this.spectrobe.level.getFluidState(this.spectrobe.blockPosition());
this.breached = fluidstate.is(FluidTags.WATER);
}

if (this.breached && !flag) {
this.spectrobe.playSound(SoundEvents.DOLPHIN_JUMP, 1.0F, 1.0F);
}

Vector3d vector3d = this.spectrobe.getDeltaMovement();
if (vector3d.y * vector3d.y < (double)0.03F && this.spectrobe.xRot != 0.0F) {
this.spectrobe.xRot = MathHelper.rotlerp(this.spectrobe.xRot, 0.0F, 0.2F);
} else {
double d0 = Math.sqrt(Entity.getHorizontalDistanceSqr(vector3d));
double d1 = Math.signum(-vector3d.y) * Math.acos(d0 / vector3d.length()) * (double)(180F / (float)Math.PI);
this.spectrobe.xRot = (float)d1;
}

}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.spectrobes.spectrobesmod.common.entities.goals;

import com.spectrobes.spectrobesmod.SpectrobesInfo;
import com.spectrobes.spectrobesmod.common.entities.krawl.EntityKrawl;
import com.spectrobes.spectrobesmod.common.entities.krawl.EntityVortex;
import com.spectrobes.spectrobesmod.common.entities.spectrobes.EntitySpectrobe;
import com.spectrobes.spectrobesmod.common.spectrobes.SpectrobeProperties;
import net.minecraft.entity.MobEntity;
import net.minecraft.entity.ai.goal.TargetGoal;

import java.util.List;
import java.util.stream.Collectors;

public class AttackKrawlGoal extends TargetGoal {
EntityKrawl target;
Expand All @@ -27,9 +30,10 @@ public boolean canUse() {
if(mob instanceof EntitySpectrobe && ((EntitySpectrobe)mob).getStage() == SpectrobeProperties.Stage.CHILD)
return false;

List<EntityKrawl> nearbyMobs = mob.level.getEntitiesOfClass(EntityKrawl.class, mob.getBoundingBox().inflate(20, 20, 20));
if (!nearbyMobs.isEmpty()) {
this.target = nearbyMobs.get(0);
List<EntityKrawl> nearbyMobs = mob.level.getEntitiesOfClass(EntityKrawl.class, mob.getBoundingBox().inflate(5, 5, 5));
List<EntityKrawl> nonVortexKrawl = nearbyMobs.stream().filter(entityKrawl -> !entityKrawl.isVortex()).collect(Collectors.toList());
if (!nonVortexKrawl.isEmpty()) {
this.target = nonVortexKrawl.get(0);
return true;
}
return false;
Expand All @@ -46,11 +50,20 @@ public boolean canContinueToUse() {
}
}

@Override
public void tick() {
super.tick();
this.mob.setTarget(this.target);
((EntitySpectrobe)this.mob).setIsAttacking(true);
this.mob.getMoveControl().setWantedPosition(this.target.getX(), this.target.getY(), this.target.getZ(), 5);
this.mob.setAggressive(true);
}

@Override
public void start() {
this.mob.setTarget(this.target);
((EntitySpectrobe)this.mob).setIsAttacking(true);
this.mob.getNavigation().moveTo(this.mob.getNavigation().createPath(this.target, 5), 5);
this.mob.getMoveControl().setWantedPosition(this.target.getX(), this.target.getY(), this.target.getZ(), 5);
this.mob.setAggressive(true);
super.start();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.spectrobes.spectrobesmod.common.entities.goals;

import com.spectrobes.spectrobesmod.common.entities.spectrobes.EntitySpectrobe;
import com.spectrobes.spectrobesmod.common.spectrobes.SpectrobeProperties;
import net.minecraft.entity.CreatureEntity;
import net.minecraft.entity.ai.goal.AvoidEntityGoal;

public class AvoidKrawlGoal extends AvoidEntityGoal {
public AvoidKrawlGoal(CreatureEntity entity, Class toAvoid, float maxDist, double walkSpeedModifier, double sprintSpeedModifier) {
super(entity, toAvoid, maxDist, walkSpeedModifier, sprintSpeedModifier);
}

@Override
public boolean canUse() {
return ((EntitySpectrobe)mob).getStage() == SpectrobeProperties.Stage.CHILD && super.canUse();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,27 @@

public class FindFossilsGoal extends Goal {

private IWorldReader world;
private EntitySpectrobe entity;
private BlockPos target;

public FindFossilsGoal(EntitySpectrobe spectrobe) {
this.entity = spectrobe;
this.world = spectrobe.level;
}

@Override
public boolean canUse() {
if(entity.getStage() == SpectrobeProperties.Stage.CHILD && entity.isSearching()) {
List<BlockPos> lvt_1_1_ = getFossilBlocksInArea();
return !lvt_1_1_.isEmpty();
List<BlockPos> blocks = getFossilBlocksInArea();
return !blocks.isEmpty();
}
return false;
}

public void start() {
List<BlockPos> lvt_1_1_ = getFossilBlocksInArea();
if (!lvt_1_1_.isEmpty()) {
target = getClosestFossil(lvt_1_1_);
this.entity.getNavigation().moveTo(this.entity.getNavigation().createPath(target, 2), 2);
List<BlockPos> blocks = getFossilBlocksInArea();
if (!blocks.isEmpty()) {
target = getClosestFossil(blocks);
this.entity.getMoveControl().setWantedPosition(target.getX(), target.getY(), target.getZ(), 0.5);
}
}

Expand All @@ -50,10 +49,10 @@ private List<BlockPos> getFossilBlocksInArea() {
return fossilBlocks;
}

private BlockPos getClosestFossil(List<BlockPos> lvt_1_1_) {
BlockPos closest = lvt_1_1_.get(0).immutable();
private BlockPos getClosestFossil(List<BlockPos> blocks) {
BlockPos closest = blocks.get(0).immutable();

for (BlockPos pos : lvt_1_1_) {
for (BlockPos pos : blocks) {
if(entity.distanceToSqr(pos.getX(), pos.getY(), pos.getZ()) < entity.distanceToSqr(closest.getX(), closest.getY(), closest.getZ())) {
closest = pos.immutable();
}
Expand All @@ -65,7 +64,7 @@ private BlockPos getClosestFossil(List<BlockPos> lvt_1_1_) {
@Override
public boolean canContinueToUse() {
if(target != null) {
if(this.entity.distanceToSqr((double)target.getX(), (double)target.getY(), (double)target.getZ()) < 2) {
if(this.entity.distanceToSqr(target.getX(), target.getY(), target.getZ()) < 2) {
target = null;
entity.setState(1);
return false;
Expand All @@ -79,7 +78,7 @@ public boolean canContinueToUse() {

public void tick() {
if(target != null) {
this.entity.getNavigation().moveTo(this.entity.getNavigation().createPath(target, 1), 1);
this.entity.getMoveControl().setWantedPosition(target.getX(), target.getY(), target.getZ(), 0.5);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,26 @@

public class FindMineralOreGoal extends Goal {

private IWorldReader world;
private EntitySpectrobe entity;
private BlockPos target;

public FindMineralOreGoal(EntitySpectrobe spectrobe) {
this.entity = spectrobe;
this.world = spectrobe.level;
}

public boolean canUse() {
if(entity.getStage() == SpectrobeProperties.Stage.CHILD && entity.isSearching()) {
List<BlockPos> lvt_1_1_ = getMineralBlocksInArea();
return !lvt_1_1_.isEmpty();
List<BlockPos> blocks = getMineralBlocksInArea();
return !blocks.isEmpty();
}
return false;
}

public void start() {
List<BlockPos> lvt_1_1_ = getMineralBlocksInArea();
if (!lvt_1_1_.isEmpty()) {
target = getClosestMineral(lvt_1_1_);
this.entity.getNavigation().moveTo(this.entity.getNavigation().createPath(target, 2), 2);
List<BlockPos> blocks = getMineralBlocksInArea();
if (!blocks.isEmpty()) {
target = getClosestMineral(blocks);
this.entity.getMoveControl().setWantedPosition(target.getX(), target.getY(), target.getZ(), 1.0D);
}
}

Expand Down Expand Up @@ -79,7 +77,7 @@ public boolean canContinueToUse() {

public void tick() {
if(target != null) {
this.entity.getNavigation().moveTo(this.entity.getNavigation().createPath(target, 1), 1);
this.entity.getMoveControl().setWantedPosition(target.getX(), target.getY(), target.getZ(), 1.0D);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@

import com.spectrobes.spectrobesmod.common.entities.spectrobes.EntitySpectrobe;
import com.spectrobes.spectrobesmod.common.items.minerals.MineralItem;
import com.spectrobes.spectrobesmod.common.registry.SpectrobesBlocks;
import com.spectrobes.spectrobesmod.common.spectrobes.SpectrobeProperties;
import net.minecraft.entity.Entity;
import net.minecraft.entity.ai.goal.Goal;
import net.minecraft.entity.item.ItemEntity;
import net.minecraft.entity.passive.DolphinEntity;
import net.minecraft.inventory.EquipmentSlotType;
import net.minecraft.item.ItemStack;
import net.minecraft.tags.FluidTags;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.IWorldReader;

import java.util.List;

public class FindMineralsGoal extends Goal {
private IWorldReader world;
private final IWorldReader world;
private EntitySpectrobe entity;

public FindMineralsGoal(EntitySpectrobe spectrobe) {
Expand All @@ -24,8 +28,8 @@ public FindMineralsGoal(EntitySpectrobe spectrobe) {

public boolean canUse() {
if(entity.getStage() == SpectrobeProperties.Stage.CHILD && (entity.isSearching() || entity.getOwner() == null)) {
List<ItemEntity> lvt_1_1_ = entity.level.getEntitiesOfClass(ItemEntity.class, this.entity.getBoundingBox().inflate(8.0D, 8.0D, 8.0D), EntitySpectrobe.MINERAL_SELECTOR);
return !lvt_1_1_.isEmpty();
List<ItemEntity> minerals = entity.level.getEntitiesOfClass(ItemEntity.class, this.entity.getBoundingBox().inflate(8.0D, 8.0D, 8.0D), EntitySpectrobe.MINERAL_SELECTOR);
return !minerals.isEmpty();
}
return false;
}
Expand All @@ -37,9 +41,17 @@ public boolean canContinueToUse() {
}

public void start() {
List<ItemEntity> lvt_1_1_ = this.entity.level.getEntitiesOfClass(ItemEntity.class, this.entity.getBoundingBox().inflate(8.0D, 8.0D, 8.0D), EntitySpectrobe.MINERAL_SELECTOR);
if (!lvt_1_1_.isEmpty()) {
this.entity.getNavigation().moveTo(lvt_1_1_.get(0), 0.8000000476837158D);
BlockPos blockpos = null;

List<ItemEntity> minerals = this.entity.level.getEntitiesOfClass(ItemEntity.class, this.entity.getBoundingBox().inflate(2.0D, 2.0D, 2.0D), EntitySpectrobe.MINERAL_SELECTOR);

for (ItemEntity mineral :
minerals) {
blockpos = mineral.blockPosition();
}

if (blockpos != null) {
this.entity.getMoveControl().setWantedPosition(blockpos.getX(), blockpos.getY(), blockpos.getZ(), 1.0D);
}
}

Expand Down
Loading

0 comments on commit c80a85f

Please sign in to comment.