From 12333de3a141f8898ba95598c32b532200c022cb Mon Sep 17 00:00:00 2001 From: luisc Date: Mon, 6 Dec 2021 00:10:10 -0600 Subject: [PATCH] knockback() function for ScriptEntity now *only* knocks the entity back instead of hitting it. Flying NPC's get knocked back properly, and their vertical speed has been increased. Flying NPC's vertical flying speed can be changed by npc.setFlySpeed(double speed) from ScriptNpc. --- src/main/java/noppes/npcs/DataAI.java | 12 +++++- .../ai/pathfinder/PathNavigateFlying.java | 2 +- .../noppes/npcs/entity/EntityNPCFlying.java | 38 +++++++++---------- .../npcs/entity/EntityNPCInterface.java | 13 ++++--- .../npcs/scripted/ScriptEntityParticle.java | 2 +- .../npcs/scripted/entity/ScriptEntity.java | 9 ++--- .../scripted/entity/ScriptLivingBase.java | 1 + .../npcs/scripted/entity/ScriptNpc.java | 19 ++++++++-- .../npcs/scripted/interfaces/ICustomNpc.java | 6 ++- 9 files changed, 62 insertions(+), 40 deletions(-) diff --git a/src/main/java/noppes/npcs/DataAI.java b/src/main/java/noppes/npcs/DataAI.java index 5011b9ce7..e694065aa 100644 --- a/src/main/java/noppes/npcs/DataAI.java +++ b/src/main/java/noppes/npcs/DataAI.java @@ -35,6 +35,7 @@ public class DataAI { // Fly Change public int movementType = 0; + public double flySpeed = 1.0D; public EnumAnimation animationType = EnumAnimation.NONE; public EnumStandingType standingType = EnumStandingType.RotateBody; @@ -96,8 +97,13 @@ public class DataAI { setMovingPath(NBTTags.getIntegerArraySet(compound.getTagList("MovingPathNew",10))); movingPos = compound.getInteger("MovingPos"); movingPattern = compound.getInteger("MovingPatern"); + movementType = compound.getInteger("MovingType"); - + if(!compound.hasKey("FlySpeed")) + flySpeed = 1.0D; + else + flySpeed = compound.getDouble("FlySpeed"); + startPos = compound.getIntArray("StartPosNew"); if (startPos == null || startPos.length != 3){ startPos = new int[] { @@ -143,8 +149,10 @@ public class DataAI { compound.setTag("MovingPathNew", NBTTags.nbtIntegerArraySet(movingPath)); compound.setInteger("MovingPos", movingPos); compound.setInteger("MovingPatern", movingPattern); + compound.setInteger("MovingType", movementType); - + compound.setDouble("FlySpeed",flySpeed); + npc.setAvoidWater(avoidsWater); compound.setIntArray("StartPosNew", npc.getStartPos()); diff --git a/src/main/java/noppes/npcs/ai/pathfinder/PathNavigateFlying.java b/src/main/java/noppes/npcs/ai/pathfinder/PathNavigateFlying.java index 9a3ab7207..c2187dbeb 100644 --- a/src/main/java/noppes/npcs/ai/pathfinder/PathNavigateFlying.java +++ b/src/main/java/noppes/npcs/ai/pathfinder/PathNavigateFlying.java @@ -10,7 +10,7 @@ public class PathNavigateFlying extends PathNavigateSwimmer { super(p_i45873_1_, worldIn); } - protected PathFinder func_179679_a() + protected PathFinder getPathFinder() { return new NPCPathFinder(new FlyNodeProcessor()); } diff --git a/src/main/java/noppes/npcs/entity/EntityNPCFlying.java b/src/main/java/noppes/npcs/entity/EntityNPCFlying.java index 8c215191d..610ac8055 100644 --- a/src/main/java/noppes/npcs/entity/EntityNPCFlying.java +++ b/src/main/java/noppes/npcs/entity/EntityNPCFlying.java @@ -10,12 +10,8 @@ public abstract class EntityNPCFlying extends EntityNPCInterface { public EntityNPCFlying(World world) { super(world); - this.getNavigator().setCanSwim(true); - this.tasks.addTask(0, new EntityAISwimming(this)); } - // FLY CHANGE [WIP] - @Override public boolean canFly(){ return ai.movementType == 1; @@ -43,32 +39,36 @@ public abstract class EntityNPCFlying extends EntityNPCInterface { double speed = this.getEntityAttribute(SharedMonsterAttributes.movementSpeed).getAttributeValue(); double speedMult = 0.6000000238418579D/0.5D; - this.motionY *= speed*speedMult; - if(isEntityAlive()) { if (!this.worldObj.isRemote) { Entity entity = this.getAttackTarget(); if (entity != null && entity.isEntityAlive()) { - if (this.posY < entity.posY) { - if (this.motionY < 0.0D) { - this.motionY = 0.0D; - } - - this.motionY += (speed - this.motionY) * speed * speedMult; - } - double d0 = entity.posX - this.posX; - double d1 = entity.posY - this.posY; double d2 = entity.posZ - this.posZ; double d3 = d0 * d0 + d2 * d2; - if (d3 > 9.0D) { - double d5 = (double) MathHelper.sqrt_double(d3); - this.motionX += (d0 / d5 * speed - this.motionX) * speed * speedMult; - this.motionZ += (d2 / d5 * speed - this.motionZ) * speed * speedMult; + if(this.hurtTime == 0) { + if (this.posY != entity.posY) { + if (this.motionY * Math.signum(this.posY - entity.posY) > 0.0D) { + this.motionY = 0.0D; + } + + this.motionY -= Math.signum(this.posY - entity.posY) * (speed * speedMult) * this.ai.flySpeed; + } + + if (d3 > 9.0D) { + double d5 = MathHelper.sqrt_double(d3); + this.motionX += (d0 / d5 * speed - this.motionX) * speed * speedMult; + this.motionZ += (d2 / d5 * speed - this.motionZ) * speed * speedMult; + } } this.rotationYaw = (float) ((Math.atan2(-d0, -d2) + Math.PI) * -(180F / Math.PI)); + } else { + if (!this.onGround && this.motionY < 0.0D) + { + this.motionY *= 0.6D; + } } } diff --git a/src/main/java/noppes/npcs/entity/EntityNPCInterface.java b/src/main/java/noppes/npcs/entity/EntityNPCInterface.java index f3ece40fa..79c43f23a 100644 --- a/src/main/java/noppes/npcs/entity/EntityNPCInterface.java +++ b/src/main/java/noppes/npcs/entity/EntityNPCInterface.java @@ -25,6 +25,7 @@ import net.minecraft.item.Item; import net.minecraft.item.ItemBow; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.pathfinding.PathNavigate; import net.minecraft.potion.Potion; import net.minecraft.potion.PotionEffect; import net.minecraft.util.AxisAlignedBB; @@ -59,6 +60,8 @@ import noppes.npcs.ai.EntityAIMoveIndoors; import noppes.npcs.ai.EntityAIPanic; import noppes.npcs.ai.EntityAIWander; import noppes.npcs.ai.EntityAIWatchClosest; +import noppes.npcs.ai.pathfinder.PathNavigateFlying; +import noppes.npcs.ai.pathfinder.PathNavigateGround; import noppes.npcs.ai.selector.NPCAttackSelector; import noppes.npcs.ai.target.EntityAIClearTarget; import noppes.npcs.ai.target.EntityAIClosestTarget; @@ -153,8 +156,8 @@ public abstract class EntityNPCInterface extends EntityCreature implements IEnti public boolean updateAI = false; // Fly Change -// public EntityMoveHelper moveHelper; -// public PathNavigate navigator; + //public EntityMoveHelper moveHelper; + public PathNavigate navigator; public EntityNPCInterface(World world) { super(world); @@ -597,17 +600,15 @@ public abstract class EntityNPCInterface extends EntityCreature implements IEnti this.targetTasks.addTask(3, new EntityAIOwnerHurtByTarget(this)); this.targetTasks.addTask(4, new EntityAIOwnerHurtTarget(this)); - this.tasks.addTask(0, new EntityAIWaterNav(this)); - if(canFly()){ //this.moveHelper = new FlyingMoveHelper(this); - //this.navigator = new PathNavigateFlying(this, worldObj); + this.navigator = new PathNavigateFlying(this, worldObj); this.getNavigator().setCanSwim(true); this.tasks.addTask(0, new EntityAISwimming(this)); } else{ //this.moveHelper = new EntityMoveHelper(this); - //this.navigator = new PathNavigateGround(this, worldObj); + this.navigator = new PathNavigateGround(this, worldObj); this.tasks.addTask(0, new EntityAIWaterNav(this)); } diff --git a/src/main/java/noppes/npcs/scripted/ScriptEntityParticle.java b/src/main/java/noppes/npcs/scripted/ScriptEntityParticle.java index 19ab0e360..bc7e15120 100644 --- a/src/main/java/noppes/npcs/scripted/ScriptEntityParticle.java +++ b/src/main/java/noppes/npcs/scripted/ScriptEntityParticle.java @@ -113,7 +113,7 @@ public class ScriptEntityParticle { this.directory = directory; } - public void setHEXcolor(int HEXcolor) { + public void setHEXColor(int HEXcolor) { this.HEXcolor = HEXcolor; } diff --git a/src/main/java/noppes/npcs/scripted/entity/ScriptEntity.java b/src/main/java/noppes/npcs/scripted/entity/ScriptEntity.java index 541ff0778..373446653 100644 --- a/src/main/java/noppes/npcs/scripted/entity/ScriptEntity.java +++ b/src/main/java/noppes/npcs/scripted/entity/ScriptEntity.java @@ -457,20 +457,17 @@ public class ScriptEntity implements IEntity { * @param power How strong the knockback is * @param direction The direction in which he flies back (0-360). Usually based on getRotation() */ + @Override public void knockback(int power, float direction){ float v = direction * (float)Math.PI / 180.0F; + entity.velocityChanged = true; entity.addVelocity(-MathHelper.sin(v) * (float)power, 0.1D + power * 0.04f, MathHelper.cos(v) * (float)power); - entity.motionX *= 0.6D; - entity.motionZ *= 0.6D; - entity.attackEntityFrom(DamageSource.outOfWorld, 0.0001F); - entity.hurtResistantTime = 0; } public void knockback(int xpower, int ypower, int zpower, float direction){ float v = direction * (float)Math.PI / 180.0F; + entity.velocityChanged = true; entity.addVelocity(-MathHelper.sin(v) * (float)xpower, 0.1D + ypower * 0.04f, MathHelper.cos(v) * (float)zpower); - entity.attackEntityFrom(DamageSource.outOfWorld, 0.0001F); - entity.hurtResistantTime = 0; } public void setImmune(int ticks) { diff --git a/src/main/java/noppes/npcs/scripted/entity/ScriptLivingBase.java b/src/main/java/noppes/npcs/scripted/entity/ScriptLivingBase.java index ea06ad025..843a402fd 100644 --- a/src/main/java/noppes/npcs/scripted/entity/ScriptLivingBase.java +++ b/src/main/java/noppes/npcs/scripted/entity/ScriptLivingBase.java @@ -7,6 +7,7 @@ import net.minecraft.potion.Potion; import net.minecraft.potion.PotionEffect; import net.minecraft.util.DamageSource; import net.minecraft.util.EntityDamageSource; +import net.minecraft.util.MathHelper; import noppes.npcs.controllers.ScriptController; import noppes.npcs.scripted.ScriptItemStack; import noppes.npcs.scripted.constants.EntityType; diff --git a/src/main/java/noppes/npcs/scripted/entity/ScriptNpc.java b/src/main/java/noppes/npcs/scripted/entity/ScriptNpc.java index dcd8d93e3..02fcde002 100644 --- a/src/main/java/noppes/npcs/scripted/entity/ScriptNpc.java +++ b/src/main/java/noppes/npcs/scripted/entity/ScriptNpc.java @@ -923,10 +923,6 @@ public class ScriptNpc extends ScriptLiving imp return ((EntityNPCInterface)this.npc).timers; } - public boolean canFly(){ - return npc.ai.movementType == 1; - } - public void setFly(int fly){ if(fly > 0) fly = 1; @@ -935,6 +931,21 @@ public class ScriptNpc extends ScriptLiving imp npc.ai.movementType = fly; } + public boolean canFly(){ + return npc.ai.movementType == 1; + } + + public void setFlySpeed(double flySpeed){ + if(flySpeed < 0.0D) + flySpeed = 0.0D; + + npc.ai.flySpeed = flySpeed; + } + + public double getFlySpeed(double flySpeed){ + return npc.ai.flySpeed; + } + public void setSkinType(byte type) { npc.display.skinType = type; npc.updateClient = true; diff --git a/src/main/java/noppes/npcs/scripted/interfaces/ICustomNpc.java b/src/main/java/noppes/npcs/scripted/interfaces/ICustomNpc.java index f4c504e8d..fdbb03e48 100644 --- a/src/main/java/noppes/npcs/scripted/interfaces/ICustomNpc.java +++ b/src/main/java/noppes/npcs/scripted/interfaces/ICustomNpc.java @@ -676,9 +676,13 @@ public interface ICustomNpc extends IEntityLiving { public long getAge(); + public void setFly(int fly); + public boolean canFly(); - public void setFly(int fly); + public void setFlySpeed(double flySpeed); + + public double getFlySpeed(double flySpeed); public void setSkinType(byte type);