Added a modifiable and toggleable flying height limit to NPCs

This commit is contained in:
luisc committed 2022-01-13 05:28:20 -06:00
1 parent 04def38b63
commit 555ceb741f
5 files changed
+81 -6

No files matched your search

+14
View File
@@ -37,6 +37,8 @@ public class DataAI {
public int movementType = 0;
public double flySpeed = 1.0D;
public double flyGravity = 0.0D;
public boolean hasFlyLimit = true;
public int flyHeightLimit = 2;
public EnumAnimation animationType = EnumAnimation.NONE;
public EnumStandingType standingType = EnumStandingType.RotateBody;
@@ -110,6 +112,16 @@ public class DataAI {
else
flyGravity = compound.getDouble("FlyGravity");
if(!compound.hasKey("HasFlyLimit"))
hasFlyLimit = false;
else
hasFlyLimit = compound.getBoolean("HasFlyLimit");
if(!compound.hasKey("FlyHeightLimit"))
flyHeightLimit = 5;
else
flyHeightLimit = compound.getInteger("FlyHeightLimit");
startPos = compound.getIntArray("StartPosNew");
if (startPos == null || startPos.length != 3){
startPos = new int[] {
@@ -159,6 +171,8 @@ public class DataAI {
compound.setInteger("MovingType", movementType);
compound.setDouble("FlySpeed",flySpeed);
compound.setDouble("FlyGravity",flyGravity);
compound.setBoolean("HasFlyLimit",hasFlyLimit);
compound.setInteger("FlyHeightLimit", flyHeightLimit);
npc.setAvoidWater(avoidsWater);
@@ -2,6 +2,7 @@ package noppes.npcs.ai.pathfinder;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.EntityMoveHelper;
import net.minecraft.init.Blocks;
import net.minecraft.util.MathHelper;
import noppes.npcs.entity.EntityNPCInterface;
@@ -50,7 +51,18 @@ public class FlyingMoveHelper extends EntityMoveHelper{
if(verticalSpeed > Math.abs(this.entity.posY - this.posY))
verticalSpeed = Math.abs(this.entity.posY - this.posY);
this.entity.motionY -= Math.signum(this.entity.posY - this.posY) * verticalSpeed;
int blockY = (int) this.posY;
double heightOffGround = 0;
if(this.entity.ai.hasFlyLimit) {
for (blockY = (int) this.posY; blockY > 0; blockY--) {
heightOffGround = this.posY - blockY;
if (this.entity.worldObj.getBlock((int) this.posX, blockY, (int) this.posZ) != Blocks.air || heightOffGround > this.entity.ai.flyHeightLimit){
break;
}
}
}
if(heightOffGround < this.entity.ai.flyHeightLimit || !this.entity.ai.hasFlyLimit)
this.entity.motionY -= Math.signum(this.entity.posY - this.posY) * verticalSpeed;
}
double d5 = MathHelper.sqrt_double(d4);
@@ -42,6 +42,16 @@ public class SubGuiNpcMovement extends SubGuiInterface implements ITextfieldList
addTextField(new GuiNpcTextField(19,this, guiLeft + 80, y + 155, 40, 20, ai.flyGravity + ""));
getTextField(19).doublesOnly = true;
getTextField(19).setMinMaxDefaultDouble(0.0D,1.0D,0.0D);
this.addLabel(new GuiNpcLabel(20, "Limit Height", guiLeft + 150, y + 116));
addButton(new GuiNpcButton(20, guiLeft + 210, y + 110, 40, 20, new String[]{"gui.no","gui.yes"}, ai.hasFlyLimit?1:0));
if(ai.hasFlyLimit) {
this.addLabel(new GuiNpcLabel(21, "Height", guiLeft + 150, y + 140));
addTextField(new GuiNpcTextField(21, this, guiLeft + 185, y + 135, 40, 20, ai.flyHeightLimit + ""));
getTextField(21).integersOnly = true;
getTextField(21).setMinMaxDefaultDouble(0.0D, Double.MAX_VALUE, 1.0D);
}
}
if(ai.movingType == EnumMovingType.Wandering){
@@ -159,10 +169,12 @@ public class SubGuiNpcMovement extends SubGuiInterface implements ITextfieldList
else if (button.id == 15) {
ai.movementType = button.getValue();
initGui();
} else if (button.id == 20) {
ai.hasFlyLimit = button.getValue() == 1;
initGui();
} else if (button.id == 66) {
close();
}
else if(button.id == 66){
close();
}
}
@Override
@@ -191,6 +203,9 @@ public class SubGuiNpcMovement extends SubGuiInterface implements ITextfieldList
else if(textfield.id == 19){
ai.flyGravity = textfield.getDouble();
}
else if(textfield.id == 21){
ai.flyHeightLimit = textfield.getInteger();
}
}
}
@@ -3,6 +3,7 @@ package noppes.npcs.entity;
import net.minecraft.entity.Entity;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.EntityAISwimming;
import net.minecraft.init.Blocks;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
@@ -36,10 +37,26 @@ public abstract class EntityNPCFlying extends EntityNPCInterface {
return;
}
int blockY = (int) this.posY;
double heightOffGround = 0;
if(this.ai.hasFlyLimit) {
for (blockY = (int) this.posY; blockY > 0; blockY--) {
heightOffGround = this.posY - blockY;
if (this.worldObj.getBlock((int) this.posX, blockY, (int) this.posZ) != Blocks.air || heightOffGround > this.ai.flyHeightLimit){
break;
}
}
}
if (!this.onGround && !this.velocityChanged && this.motionY < 0 && this.hurtTime == 0)
{
this.motionY *= this.ai.flyGravity;
this.velocityChanged = true;
if(heightOffGround >= this.ai.flyHeightLimit && this.ai.hasFlyLimit){
this.motionY *= 0.8D;
this.velocityChanged = true;
} else {
this.motionY *= this.ai.flyGravity;
this.velocityChanged = true;
}
}
}
@@ -1040,6 +1040,23 @@ public class ScriptNpc<T extends EntityNPCInterface> extends ScriptLiving<T> imp
return npc.ai.flyGravity;
}
public void setFlyHeightLimit(int flyHeightLimit){
if(flyHeightLimit < 0)
flyHeightLimit = 0;
this.npc.ai.flyHeightLimit = flyHeightLimit;
}
public int getFlyHeightLimit(int flyHeightLimit){
return this.npc.ai.flyHeightLimit;
}
public void limitFlyHeight(boolean limit){
this.npc.ai.hasFlyLimit = limit;
}
public boolean isFlyHeightLimited(boolean limit){
return this.npc.ai.hasFlyLimit;
}
public void setSpeed(int speed) {
npc.ai.setWalkingSpeed(speed);
}