Optimize AI and EntityLiving ticking

Reduce usage of getCubes as it is an expensive call.
Remove iterator usage and object creation from PathfinderGoalSelector
methods as these are called very often.
Update EntityLiving goal selectors less often as this is still quite an
expensive task.
Dieser Commit ist enthalten in:
Travis Watkins 2012-04-21 13:47:29 -05:00 committet von EvilSeph
Ursprung 343f39b2b3
Commit e8459c1ff6
2 geänderte Dateien mit 49 neuen und 32 gelöschten Zeilen

Datei anzeigen

@ -1090,22 +1090,28 @@ public abstract class EntityLiving extends Entity {
--this.aN; --this.aN;
this.setPosition(d0, d1, d2); this.setPosition(d0, d1, d2);
this.c(this.yaw, this.pitch); this.c(this.yaw, this.pitch);
List list = this.world.getCubes(this, this.boundingBox.shrink(0.03125D, 0.0D, 0.03125D)); // CraftBukkit start - getCubes is expensive, use an approximation
if (this.world.getTypeId(MathHelper.floor(d0), MathHelper.floor(d1), MathHelper.floor(d2)) != 0) {
if (list.size() > 0) { d1 += 1.0D;
double d4 = 0.0D;
for (int i = 0; i < list.size(); ++i) {
AxisAlignedBB axisalignedbb = (AxisAlignedBB) list.get(i);
if (axisalignedbb.e > d4) {
d4 = axisalignedbb.e;
}
}
d1 += d4 - this.boundingBox.b;
this.setPosition(d0, d1, d2); this.setPosition(d0, d1, d2);
} }
//List list = this.world.getCubes(this, this.boundingBox.shrink(0.03125D, 0.0D, 0.03125D));
//if (list.size() > 0) {
// double d4 = 0.0D;
// for (int i = 0; i < list.size(); ++i) {
// AxisAlignedBB axisalignedbb = (AxisAlignedBB) list.get(i);
// if (axisalignedbb.e > d4) {
// d4 = axisalignedbb.e;
// }
// }
// d1 += d4 - this.boundingBox.b;
// this.setPosition(d0, d1, d2);
//}
// CraftBukkit end
} }
// MethodProfiler.a("ai"); // CraftBukkit - not in production code // MethodProfiler.a("ai"); // CraftBukkit - not in production code
@ -1116,13 +1122,13 @@ public abstract class EntityLiving extends Entity {
this.aY = 0.0F; this.aY = 0.0F;
} else if (this.aF()) { } else if (this.aF()) {
if (this.c_()) { if (this.c_()) {
MethodProfiler.a("newAi"); // MethodProfiler.a("newAi"); // CraftBukkit - not in production code
this.z_(); this.z_();
MethodProfiler.a(); // MethodProfiler.a(); // CraftBukkit - not in production code
} else { } else {
MethodProfiler.a("oldAi"); // MethodProfiler.a("oldAi"); // CraftBukkit - not in production code
this.d_(); this.d_();
MethodProfiler.a(); // MethodProfiler.a(); // CraftBukkit - not in production code
this.X = this.yaw; this.X = this.yaw;
} }
} }
@ -1234,10 +1240,14 @@ public abstract class EntityLiving extends Entity {
this.m.a(); this.m.a();
//MethodProfiler.a(); // CraftBukkit - not in production code //MethodProfiler.a(); // CraftBukkit - not in production code
//MethodProfiler.a("targetSelector"); // CraftBukkit - not in production code //MethodProfiler.a("targetSelector"); // CraftBukkit - not in production code
this.targetSelector.a(); // CraftBukkit start - run goal selectors every other tick
if ((this.aV & 1) == 0) {
this.targetSelector.a();
//MethodProfiler.a(); // CraftBukkit - not in production code //MethodProfiler.a(); // CraftBukkit - not in production code
//MethodProfiler.a("goalSelector"); // CraftBukkit - not in production code //MethodProfiler.a("goalSelector"); // CraftBukkit - not in production code
this.goalSelector.a(); this.goalSelector.a();
}
// CraftBukkit end
//MethodProfiler.a(); // CraftBukkit - not in production code //MethodProfiler.a(); // CraftBukkit - not in production code
//MethodProfiler.a("navigation"); // CraftBukkit - not in production code //MethodProfiler.a("navigation"); // CraftBukkit - not in production code
this.navigation.d(); this.navigation.d();

Datei anzeigen

@ -7,6 +7,7 @@ public class PathfinderGoalSelector {
private ArrayList a = new ArrayList(); private ArrayList a = new ArrayList();
private ArrayList b = new ArrayList(); private ArrayList b = new ArrayList();
private ArrayList arraylist = new ArrayList(); // CraftBukkit - create list for object reuse
public PathfinderGoalSelector() {} public PathfinderGoalSelector() {}
@ -15,11 +16,12 @@ public class PathfinderGoalSelector {
} }
public void a() { public void a() {
ArrayList arraylist = new ArrayList(); //ArrayList arraylist = new ArrayList(); // CraftBukkit
Iterator iterator = this.a.iterator(); arraylist.clear(); // CraftBukkit - prepare reused list
while (iterator.hasNext()) { // CraftBukkit start - don't use iterators for private fields
PathfinderGoalSelectorItem pathfindergoalselectoritem = (PathfinderGoalSelectorItem) iterator.next(); for (int i = 0; i < this.a.size(); i++) {
PathfinderGoalSelectorItem pathfindergoalselectoritem = (PathfinderGoalSelectorItem) this.a.get(i);
boolean flag = this.b.contains(pathfindergoalselectoritem); boolean flag = this.b.contains(pathfindergoalselectoritem);
if (flag) { if (flag) {
@ -36,6 +38,7 @@ public class PathfinderGoalSelector {
this.b.add(pathfindergoalselectoritem); this.b.add(pathfindergoalselectoritem);
} }
} }
// CraftBukkit end
boolean flag1 = false; boolean flag1 = false;
@ -46,8 +49,10 @@ public class PathfinderGoalSelector {
Iterator iterator1; Iterator iterator1;
PathfinderGoalSelectorItem pathfindergoalselectoritem1; PathfinderGoalSelectorItem pathfindergoalselectoritem1;
for (iterator1 = arraylist.iterator(); iterator1.hasNext(); pathfindergoalselectoritem1.a.c()) { //CraftBukkit start - don't use iterators for private fields
pathfindergoalselectoritem1 = (PathfinderGoalSelectorItem) iterator1.next(); for (int i = 0; i < arraylist.size(); i++) {
pathfindergoalselectoritem1 = (PathfinderGoalSelectorItem) arraylist.get(i);
pathfindergoalselectoritem1.a.c();
if (flag1) { if (flag1) {
System.out.println(pathfindergoalselectoritem1.a.toString() + ", "); System.out.println(pathfindergoalselectoritem1.a.toString() + ", ");
} }
@ -57,19 +62,20 @@ public class PathfinderGoalSelector {
System.out.println("Running: "); System.out.println("Running: ");
} }
for (iterator1 = this.b.iterator(); iterator1.hasNext(); pathfindergoalselectoritem1.a.e()) { for (int i = 0; i < this.b.size(); i++) {
pathfindergoalselectoritem1 = (PathfinderGoalSelectorItem) iterator1.next(); pathfindergoalselectoritem1 = (PathfinderGoalSelectorItem) this.b.get(i);
pathfindergoalselectoritem1.a.e();
if (flag1) { if (flag1) {
System.out.println(pathfindergoalselectoritem1.a.toString()); System.out.println(pathfindergoalselectoritem1.a.toString());
} }
} }
// CraftBukkit end
} }
private boolean a(PathfinderGoalSelectorItem pathfindergoalselectoritem) { private boolean a(PathfinderGoalSelectorItem pathfindergoalselectoritem) {
Iterator iterator = this.a.iterator(); // CraftBukkit start - don't use iterators for private fields
for (int i = 0; i < this.a.size(); i++) {
while (iterator.hasNext()) { PathfinderGoalSelectorItem pathfindergoalselectoritem1 = (PathfinderGoalSelectorItem) this.a.get(i);
PathfinderGoalSelectorItem pathfindergoalselectoritem1 = (PathfinderGoalSelectorItem) iterator.next();
if (pathfindergoalselectoritem1 != pathfindergoalselectoritem) { if (pathfindergoalselectoritem1 != pathfindergoalselectoritem) {
if (pathfindergoalselectoritem.b >= pathfindergoalselectoritem1.b) { if (pathfindergoalselectoritem.b >= pathfindergoalselectoritem1.b) {
@ -81,6 +87,7 @@ public class PathfinderGoalSelector {
} }
} }
} }
// CraftBukkit end
return true; return true;
} }