-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move all idle-like AI routines to not doing pathing themselves, but setting a walk target, this drastically reduces the number of pathfinds needed, and more centralizes that. This standardizes the checks for movement as well, so sleeping/frozen/etc won't walk around Moves block and item lookups to also using a centralized AI, so that no longer needs to be looked up either Hunger AI is more modular, so easier to add new types of blocks to try to eat, also adjustments to how it selects them Fixes the toImmutable not being overriden for the internal mutable block pos used by vector 3s, this will fix Thuts-Mods/ThutCore#3 when it is merged over to there
- Loading branch information
Showing
67 changed files
with
1,952 additions
and
1,819 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,53 @@ | ||
package pokecube.core.ai.brain; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import net.minecraft.entity.AgeableEntity; | ||
import net.minecraft.entity.LivingEntity; | ||
import net.minecraft.entity.ai.brain.memory.MemoryModuleType; | ||
import net.minecraft.entity.ai.brain.memory.WalkTarget; | ||
import net.minecraft.entity.item.ItemEntity; | ||
import net.minecraft.pathfinding.Path; | ||
import net.minecraft.util.math.IPosWrapper; | ||
import net.minecraftforge.event.RegistryEvent.Register; | ||
import pokecube.core.PokecubeCore; | ||
import pokecube.core.ai.brain.sensors.NearBlocks.NearBlock; | ||
|
||
public class MemoryModules | ||
{ | ||
// Used for combat | ||
public static final MemoryModuleType<LivingEntity> ATTACKTARGET = new MemoryModuleType<>(Optional.empty()); | ||
public static final MemoryModuleType<LivingEntity> HUNTTARGET = new MemoryModuleType<>(Optional.empty()); | ||
public static final MemoryModuleType<IPosWrapper> MOVE_TARGET = new MemoryModuleType<>(Optional.empty()); | ||
public static final MemoryModuleType<LivingEntity> HUNTED_BY = new MemoryModuleType<>(Optional.empty()); | ||
|
||
// Used for pathing | ||
public static final MemoryModuleType<Path> PATH = MemoryModuleType.PATH; | ||
public static final MemoryModuleType<WalkTarget> WALK_TARGET = MemoryModuleType.WALK_TARGET; | ||
public static final MemoryModuleType<Path> PATH = MemoryModuleType.PATH; | ||
public static final MemoryModuleType<WalkTarget> WALK_TARGET = MemoryModuleType.WALK_TARGET; | ||
public static final MemoryModuleType<Long> NOT_FOUND_PATH = MemoryModuleType.CANT_REACH_WALK_TARGET_SINCE; | ||
|
||
// Misc | ||
public static final MemoryModuleType<IPosWrapper> LOOK_TARGET = MemoryModuleType.LOOK_TARGET; | ||
|
||
public static final MemoryModuleType<List<NearBlock>> VISIBLE_BLOCKS = new MemoryModuleType<>(Optional.empty()); | ||
public static final MemoryModuleType<List<ItemEntity>> VISIBLE_ITEMS = new MemoryModuleType<>(Optional.empty()); | ||
|
||
public static final MemoryModuleType<List<AgeableEntity>> POSSIBLE_MATES = new MemoryModuleType<>(Optional.empty()); | ||
public static final MemoryModuleType<AgeableEntity> MATE_TARGET = new MemoryModuleType<>(Optional.empty()); | ||
|
||
public static void register(final Register<MemoryModuleType<?>> event) | ||
{ | ||
event.getRegistry().register(MemoryModules.ATTACKTARGET.setRegistryName(PokecubeCore.MODID, "attack_target")); | ||
event.getRegistry().register(MemoryModules.HUNTTARGET.setRegistryName(PokecubeCore.MODID, "hunt_target")); | ||
event.getRegistry().register(MemoryModules.HUNTED_BY.setRegistryName(PokecubeCore.MODID, "hunted_by")); | ||
event.getRegistry().register(MemoryModules.MOVE_TARGET.setRegistryName(PokecubeCore.MODID, "move_target")); | ||
|
||
event.getRegistry().register(MemoryModules.VISIBLE_BLOCKS.setRegistryName(PokecubeCore.MODID, | ||
"visible_blocks")); | ||
event.getRegistry().register(MemoryModules.VISIBLE_ITEMS.setRegistryName(PokecubeCore.MODID, "visible_items")); | ||
|
||
event.getRegistry().register(MemoryModules.POSSIBLE_MATES.setRegistryName(PokecubeCore.MODID, "mate_options")); | ||
event.getRegistry().register(MemoryModules.MATE_TARGET.setRegistryName(PokecubeCore.MODID, "mate_choice")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
src/main/java/pokecube/core/ai/brain/sensors/NearBlocks.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package pokecube.core.ai.brain.sensors; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import com.google.common.collect.ImmutableSet; | ||
import com.google.common.collect.Lists; | ||
|
||
import net.minecraft.block.BlockState; | ||
import net.minecraft.entity.LivingEntity; | ||
import net.minecraft.entity.ai.brain.Brain; | ||
import net.minecraft.entity.ai.brain.memory.MemoryModuleType; | ||
import net.minecraft.entity.ai.brain.sensor.Sensor; | ||
import net.minecraft.util.math.BlockPos; | ||
import net.minecraft.world.server.ServerWorld; | ||
import pokecube.core.ai.brain.BrainUtils; | ||
import pokecube.core.ai.brain.MemoryModules; | ||
import thut.api.maths.Cruncher; | ||
import thut.api.maths.Vector3; | ||
|
||
public class NearBlocks extends Sensor<LivingEntity> | ||
{ | ||
public static class NearBlock | ||
{ | ||
private final BlockState state; | ||
private final BlockPos pos; | ||
|
||
public NearBlock(final BlockState state, final BlockPos pos) | ||
{ | ||
this.state = state; | ||
this.pos = pos; | ||
} | ||
|
||
public BlockPos getPos() | ||
{ | ||
return this.pos; | ||
} | ||
|
||
public BlockState getState() | ||
{ | ||
return this.state; | ||
} | ||
} | ||
|
||
private static final int[][] indexArr = new int[32 * 32 * 32][3]; | ||
|
||
static | ||
{ | ||
final Vector3 r = Vector3.getNewVector(); | ||
for (int i = 0; i < NearBlocks.indexArr.length; i++) | ||
{ | ||
Cruncher.indexToVals(i, r); | ||
if (Math.abs(r.y) <= 4) | ||
{ | ||
NearBlocks.indexArr[i][0] = r.intX(); | ||
NearBlocks.indexArr[i][1] = r.intY(); | ||
NearBlocks.indexArr[i][2] = r.intZ(); | ||
} | ||
else NearBlocks.indexArr[i] = null; | ||
} | ||
} | ||
|
||
long lastUpdate = 0; | ||
|
||
@Override | ||
protected void update(final ServerWorld worldIn, final LivingEntity entityIn) | ||
{ | ||
if (BrainUtils.hasAttackTarget(entityIn)) return; | ||
if (BrainUtils.hasMoveUseTarget(entityIn)) return; | ||
|
||
final Vector3 r = Vector3.getNewVector(), rAbs = Vector3.getNewVector(); | ||
final Vector3 origin = Vector3.getNewVector(); | ||
origin.set(entityIn); | ||
final List<NearBlock> list = Lists.newArrayList(); | ||
final int size = 8; | ||
for (int i = 0; i < size * size * size; i++) | ||
{ | ||
final int[] pos = NearBlocks.indexArr[i]; | ||
if (pos == null) continue; | ||
r.set(pos); | ||
rAbs.set(r).addTo(origin); | ||
if (rAbs.isAir(worldIn)) continue; | ||
final BlockPos bpos = new BlockPos(rAbs.getPos()); | ||
final BlockState state = worldIn.getBlockState(bpos); | ||
list.add(new NearBlock(state, bpos)); | ||
} | ||
final BlockPos o0 = entityIn.getPosition(); | ||
list.sort((o1, o2) -> (int) (o1.getPos().distanceSq(o0) - o1.getPos().distanceSq(o0))); | ||
final Brain<?> brain = entityIn.getBrain(); | ||
if (!list.isEmpty()) brain.setMemory(MemoryModules.VISIBLE_BLOCKS, list); | ||
else brain.removeMemory(MemoryModules.VISIBLE_BLOCKS); | ||
} | ||
|
||
@Override | ||
public Set<MemoryModuleType<?>> getUsedMemories() | ||
{ | ||
return ImmutableSet.of(MemoryModules.VISIBLE_BLOCKS); | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/pokecube/core/ai/brain/sensors/NearItems.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package pokecube.core.ai.brain.sensors; | ||
|
||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import com.google.common.collect.ImmutableSet; | ||
|
||
import net.minecraft.entity.LivingEntity; | ||
import net.minecraft.entity.ai.brain.Brain; | ||
import net.minecraft.entity.ai.brain.memory.MemoryModuleType; | ||
import net.minecraft.entity.ai.brain.sensor.Sensor; | ||
import net.minecraft.entity.item.ItemEntity; | ||
import net.minecraft.world.server.ServerWorld; | ||
import pokecube.core.ai.brain.MemoryModules; | ||
|
||
public class NearItems extends Sensor<LivingEntity> | ||
{ | ||
long lastUpdate = 0; | ||
|
||
@Override | ||
protected void update(final ServerWorld worldIn, final LivingEntity entityIn) | ||
{ | ||
final List<ItemEntity> list = worldIn.getEntitiesWithinAABB(ItemEntity.class, entityIn.getBoundingBox().grow( | ||
16.0D, 16.0D, 16.0D), (item) -> | ||
{ | ||
return item.isAlive() && entityIn.canEntityBeSeen(item); | ||
}); | ||
list.sort(Comparator.comparingDouble(entityIn::getDistanceSq)); | ||
final Brain<?> brain = entityIn.getBrain(); | ||
if (!list.isEmpty()) brain.setMemory(MemoryModules.VISIBLE_ITEMS, list); | ||
else brain.removeMemory(MemoryModules.VISIBLE_ITEMS); | ||
} | ||
|
||
@Override | ||
public Set<MemoryModuleType<?>> getUsedMemories() | ||
{ | ||
return ImmutableSet.of(MemoryModules.VISIBLE_ITEMS); | ||
} | ||
|
||
} |
Oops, something went wrong.