From df951e0abd900dee170ea6dd9b19edb3235b2a2c Mon Sep 17 00:00:00 2001 From: Giovanni Harting <539@idlegandalf.com> Date: Tue, 28 Jun 2016 14:39:54 +0200 Subject: test of new AITask --- .../sosnitzka/ztic_addon/traits/TraitHaunted.java | 16 +--- .../ztic_addon/util/EntityAIPermanentPanic.java | 98 ++++++++++++++++++++ .../com/sosnitzka/ztic_addon/util/EntityPanic.java | 102 --------------------- 3 files changed, 100 insertions(+), 116 deletions(-) create mode 100644 src/main/java/com/sosnitzka/ztic_addon/util/EntityAIPermanentPanic.java delete mode 100644 src/main/java/com/sosnitzka/ztic_addon/util/EntityPanic.java (limited to 'src/main/java') diff --git a/src/main/java/com/sosnitzka/ztic_addon/traits/TraitHaunted.java b/src/main/java/com/sosnitzka/ztic_addon/traits/TraitHaunted.java index 3f33c90..5f931e1 100644 --- a/src/main/java/com/sosnitzka/ztic_addon/traits/TraitHaunted.java +++ b/src/main/java/com/sosnitzka/ztic_addon/traits/TraitHaunted.java @@ -1,6 +1,6 @@ package com.sosnitzka.ztic_addon.traits; -import com.sosnitzka.ztic_addon.util.EntityPanic; +import com.sosnitzka.ztic_addon.util.EntityAIPermanentPanic; import net.minecraft.entity.EntityCreature; import net.minecraft.entity.EntityLiving; import net.minecraft.entity.EntityLivingBase; @@ -9,9 +9,6 @@ import net.minecraft.util.text.TextFormatting; import net.minecraftforge.common.MinecraftForge; import slimeknights.tconstruct.library.traits.AbstractTrait; -/** - * Created by Robert on 03.06.2016. - */ public class TraitHaunted extends AbstractTrait { public TraitHaunted() { @@ -23,16 +20,7 @@ public class TraitHaunted extends AbstractTrait { public void onHit(ItemStack tool, EntityLivingBase player, EntityLivingBase target, float damage, boolean isCritical) { if (target instanceof EntityLiving) { //((EntityLiving) target).tasks.addTask(0, new EntityAIAvoidEntity((EntityCreature) target, EntityPlayer.class,16f, 2.0d, 2.4D)); - ((EntityLiving) target).tasks.addTask(0, new EntityPanic((EntityCreature) target, 2.0D)); + ((EntityLiving) target).tasks.addTask(0, new EntityAIPermanentPanic((EntityCreature) target, 2.0D)); } } - - - - - /*@SubscribeEvent - public void onDamage(LivingAttackEvent e){ - System.out.println(e.getEntity() + " " + e.getSource()); - }*/ - } diff --git a/src/main/java/com/sosnitzka/ztic_addon/util/EntityAIPermanentPanic.java b/src/main/java/com/sosnitzka/ztic_addon/util/EntityAIPermanentPanic.java new file mode 100644 index 0000000..a5d95f5 --- /dev/null +++ b/src/main/java/com/sosnitzka/ztic_addon/util/EntityAIPermanentPanic.java @@ -0,0 +1,98 @@ +package com.sosnitzka.ztic_addon.util; + +import net.minecraft.block.Block; +import net.minecraft.block.state.IBlockState; +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityCreature; +import net.minecraft.entity.ai.EntityAIBase; +import net.minecraft.entity.ai.RandomPositionGenerator; +import net.minecraft.init.Blocks; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Vec3d; +import net.minecraft.world.World; + +public class EntityAIPermanentPanic extends EntityAIBase { + private EntityCreature theEntityCreature; + private double speed; + private double randPosX; + private double randPosY; + private double randPosZ; + + public EntityAIPermanentPanic(EntityCreature creature, double speedIn) { + this.theEntityCreature = creature; + this.speed = speedIn; + this.setMutexBits(1); + } + + /** + * Returns whether the EntityAIBase should begin execution. + */ + public boolean shouldExecute() { + Vec3d vec3d = RandomPositionGenerator.findRandomTarget(this.theEntityCreature, 5, 4); + + if (vec3d == null) { + return false; + } else { + this.randPosX = vec3d.xCoord; + this.randPosY = vec3d.yCoord; + this.randPosZ = vec3d.zCoord; + + if (this.theEntityCreature.isBurning()) { + BlockPos blockpos = this.getRandPos(this.theEntityCreature.worldObj, this.theEntityCreature, 5, 4); + + if (blockpos != null) { + this.randPosX = (double) blockpos.getX(); + this.randPosY = (double) blockpos.getY(); + this.randPosZ = (double) blockpos.getZ(); + } + } + + return true; + } + } + + /** + * Execute a one shot task or start executing a continuous task + */ + public void startExecuting() { + this.theEntityCreature.getNavigator().tryMoveToXYZ(this.randPosX, this.randPosY, this.randPosZ, this.speed); + } + + /** + * Returns whether an in-progress EntityAIBase should continue executing + */ + public boolean continueExecuting() { + return !this.theEntityCreature.getNavigator().noPath(); + } + + private BlockPos getRandPos(World worldIn, Entity entityIn, int horizontalRange, int verticalRange) { + BlockPos blockpos = new BlockPos(entityIn); + BlockPos.MutableBlockPos mutableBlockPos = new BlockPos.MutableBlockPos(); + int i = blockpos.getX(); + int j = blockpos.getY(); + int k = blockpos.getZ(); + float f = (float) (horizontalRange * horizontalRange * verticalRange * 2); + BlockPos blockpos1 = null; + + for (int l = i - horizontalRange; l <= i + horizontalRange; ++l) { + for (int i1 = j - verticalRange; i1 <= j + verticalRange; ++i1) { + for (int j1 = k - horizontalRange; j1 <= k + horizontalRange; ++j1) { + mutableBlockPos.setPos(l, i1, j1); + IBlockState iblockstate = worldIn.getBlockState(mutableBlockPos); + Block block = iblockstate.getBlock(); + + if (block == Blocks.WATER || block == Blocks.FLOWING_WATER) { + float f1 = (float) ((l - i) * (l - i) + (i1 - j) * (i1 - j) + (j1 - k) * (j1 - k)); + + if (f1 < f) { + f = f1; + blockpos1 = new BlockPos(mutableBlockPos); + } + } + } + } + } + + return blockpos1; + } +} \ No newline at end of file diff --git a/src/main/java/com/sosnitzka/ztic_addon/util/EntityPanic.java b/src/main/java/com/sosnitzka/ztic_addon/util/EntityPanic.java deleted file mode 100644 index 8820070..0000000 --- a/src/main/java/com/sosnitzka/ztic_addon/util/EntityPanic.java +++ /dev/null @@ -1,102 +0,0 @@ -package com.sosnitzka.ztic_addon.util; - -import net.minecraft.block.Block; -import net.minecraft.block.state.IBlockState; -import net.minecraft.entity.Entity; -import net.minecraft.entity.EntityCreature; -import net.minecraft.entity.ai.EntityAIBase; -import net.minecraft.entity.ai.RandomPositionGenerator; -import net.minecraft.init.Blocks; -import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.Vec3d; -import net.minecraft.world.World; - -public class EntityPanic extends EntityAIBase { - private EntityCreature theEntityCreature; - protected double speed; - private double randPosX; - private double randPosY; - private double randPosZ; - - public EntityPanic(EntityCreature creature, double speedIn) { - this.theEntityCreature = creature; - this.speed = speedIn; - this.setMutexBits(1); - } - - /** - * Returns whether the EntityAIBase should begin execution. - */ - public boolean shouldExecute() { - if (this.theEntityCreature.getAITarget() == null && !this.theEntityCreature.isBurning()) { - return false; - } else { - Vec3d vec3d = RandomPositionGenerator.findRandomTarget(this.theEntityCreature, 15, 4); - - if (vec3d == null) { - return false; - } else { - this.randPosX = vec3d.xCoord; - this.randPosY = vec3d.yCoord; - this.randPosZ = vec3d.zCoord; - - if (this.theEntityCreature.isBurning()) { - BlockPos blockpos = this.getRandPos(this.theEntityCreature.worldObj, this.theEntityCreature, 15, 4); - - if (blockpos != null) { - this.randPosX = (double) blockpos.getX(); - this.randPosY = (double) blockpos.getY(); - this.randPosZ = (double) blockpos.getZ(); - } - } - - return true; - } - } - } - - /** - * Execute a one shot task or start executing a continuous task - */ - public void startExecuting() { - this.theEntityCreature.getNavigator().tryMoveToXYZ(this.randPosX, this.randPosY, this.randPosZ, this.speed); - } - - /** - * Returns whether an in-progress EntityAIBase should continue executing - */ - public boolean continueExecuting() { - return !this.theEntityCreature.getNavigator().noPath(); - } - - private BlockPos getRandPos(World worldIn, Entity entityIn, int horizontalRange, int verticalRange) { - BlockPos blockpos = new BlockPos(entityIn); - BlockPos.MutableBlockPos blockpos$mutableblockpos = new BlockPos.MutableBlockPos(); - int i = blockpos.getX(); - int j = blockpos.getY(); - int k = blockpos.getZ(); - float f = (float) (horizontalRange * horizontalRange * verticalRange * 2); - BlockPos blockpos1 = null; - - for (int l = i - horizontalRange; l <= i + horizontalRange; ++l) { - for (int i1 = j - verticalRange; i1 <= j + verticalRange; ++i1) { - for (int j1 = k - horizontalRange; j1 <= k + horizontalRange; ++j1) { - blockpos$mutableblockpos.setPos(l, i1, j1); - IBlockState iblockstate = worldIn.getBlockState(blockpos$mutableblockpos); - Block block = iblockstate.getBlock(); - - if (block == Blocks.WATER || block == Blocks.FLOWING_WATER) { - float f1 = (float) ((l - i) * (l - i) + (i1 - j) * (i1 - j) + (j1 - k) * (j1 - k)); - - if (f1 < f) { - f = f1; - blockpos1 = new BlockPos(blockpos$mutableblockpos); - } - } - } - } - } - - return blockpos1; - } -} \ No newline at end of file -- cgit v1.2.3