3
0
Mirror von https://github.com/PaperMC/Paper.git synchronisiert 2024-12-20 13:30:05 +01:00
Paper/src/main/java/net/minecraft/server/Village.java

301 Zeilen
8.7 KiB
Java

2012-03-09 03:54:29 +01:00
package net.minecraft.server;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Village {
private final World world;
private final List doors = new ArrayList();
private final ChunkCoordinates c = new ChunkCoordinates(0, 0, 0);
private final ChunkCoordinates center = new ChunkCoordinates(0, 0, 0);
private int size = 0;
private int f = 0;
private int time = 0;
private int population = 0;
private List i = new ArrayList();
private int j = 0;
public Village(World world) {
this.world = world;
}
public void tick(int i) {
this.time = i;
this.k();
this.j();
if (i % 20 == 0) {
this.i();
}
if (i % 30 == 0) {
this.countPopulation();
}
int j = this.population / 16;
if (this.j < j && this.doors.size() > 20 && this.world.random.nextInt(7000) == 0) {
Vec3D vec3d = this.a(MathHelper.d((float) this.center.x), MathHelper.d((float) this.center.y), MathHelper.d((float) this.center.z), 2, 4, 2);
if (vec3d != null) {
EntityIronGolem entityirongolem = new EntityIronGolem(this.world);
entityirongolem.setPosition(vec3d.a, vec3d.b, vec3d.c);
this.world.addEntity(entityirongolem, org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason.VILLAGE_DEFENSE); // CraftBukkit
2012-03-09 03:54:29 +01:00
++this.j;
}
}
}
private Vec3D a(int i, int j, int k, int l, int i1, int j1) {
for (int k1 = 0; k1 < 10; ++k1) {
int l1 = i + this.world.random.nextInt(16) - 8;
int i2 = j + this.world.random.nextInt(6) - 3;
int j2 = k + this.world.random.nextInt(16) - 8;
if (this.a(l1, i2, j2) && this.b(l1, i2, j2, l, i1, j1)) {
2012-07-29 09:33:13 +02:00
return Vec3D.a().create((double) l1, (double) i2, (double) j2);
2012-03-09 03:54:29 +01:00
}
}
return null;
}
private boolean b(int i, int j, int k, int l, int i1, int j1) {
2012-07-29 09:33:13 +02:00
if (!this.world.t(i, j - 1, k)) {
2012-03-09 03:54:29 +01:00
return false;
} else {
int k1 = i - l / 2;
int l1 = k - j1 / 2;
for (int i2 = k1; i2 < k1 + l; ++i2) {
for (int j2 = j; j2 < j + i1; ++j2) {
for (int k2 = l1; k2 < l1 + j1; ++k2) {
2012-07-29 09:33:13 +02:00
if (this.world.s(i2, j2, k2)) {
2012-03-09 03:54:29 +01:00
return false;
}
}
}
}
return true;
}
}
private void countPopulation() {
2012-07-29 09:33:13 +02:00
List list = this.world.a(EntityIronGolem.class, AxisAlignedBB.a().a((double) (this.center.x - this.size), (double) (this.center.y - 4), (double) (this.center.z - this.size), (double) (this.center.x + this.size), (double) (this.center.y + 4), (double) (this.center.z + this.size)));
2012-03-09 03:54:29 +01:00
this.j = list.size();
}
private void i() {
2012-07-29 09:33:13 +02:00
List list = this.world.a(EntityVillager.class, AxisAlignedBB.a().a((double) (this.center.x - this.size), (double) (this.center.y - 4), (double) (this.center.z - this.size), (double) (this.center.x + this.size), (double) (this.center.y + 4), (double) (this.center.z + this.size)));
2012-03-09 03:54:29 +01:00
this.population = list.size();
}
public ChunkCoordinates getCenter() {
return this.center;
}
public int getSize() {
return this.size;
}
public int getDoorCount() {
return this.doors.size();
}
public int d() {
return this.time - this.f;
}
public int getPopulationCount() {
return this.population;
}
public boolean a(int i, int j, int k) {
2012-07-29 09:33:13 +02:00
return this.center.e(i, j, k) < (float) (this.size * this.size);
2012-03-09 03:54:29 +01:00
}
public List getDoors() {
return this.doors;
}
public VillageDoor b(int i, int j, int k) {
VillageDoor villagedoor = null;
int l = Integer.MAX_VALUE;
Iterator iterator = this.doors.iterator();
while (iterator.hasNext()) {
VillageDoor villagedoor1 = (VillageDoor) iterator.next();
2012-07-29 09:33:13 +02:00
int i1 = villagedoor1.b(i, j, k);
2012-03-09 03:54:29 +01:00
if (i1 < l) {
villagedoor = villagedoor1;
l = i1;
}
}
return villagedoor;
}
public VillageDoor c(int i, int j, int k) {
VillageDoor villagedoor = null;
int l = Integer.MAX_VALUE;
Iterator iterator = this.doors.iterator();
while (iterator.hasNext()) {
VillageDoor villagedoor1 = (VillageDoor) iterator.next();
2012-07-29 09:33:13 +02:00
int i1 = villagedoor1.b(i, j, k);
2012-03-09 03:54:29 +01:00
if (i1 > 256) {
i1 *= 1000;
} else {
i1 = villagedoor1.f();
}
if (i1 < l) {
villagedoor = villagedoor1;
l = i1;
}
}
return villagedoor;
}
2012-07-29 09:33:13 +02:00
public VillageDoor e(int i, int j, int k) {
if (this.center.e(i, j, k) > (float) (this.size * this.size)) {
2012-03-09 03:54:29 +01:00
return null;
} else {
Iterator iterator = this.doors.iterator();
VillageDoor villagedoor;
do {
if (!iterator.hasNext()) {
return null;
}
villagedoor = (VillageDoor) iterator.next();
} while (villagedoor.locX != i || villagedoor.locZ != k || Math.abs(villagedoor.locY - j) > 1);
return villagedoor;
}
}
public void addDoor(VillageDoor villagedoor) {
this.doors.add(villagedoor);
this.c.x += villagedoor.locX;
this.c.y += villagedoor.locY;
this.c.z += villagedoor.locZ;
this.l();
this.f = villagedoor.addedTime;
}
public boolean isAbandoned() {
return this.doors.isEmpty();
}
public void a(EntityLiving entityliving) {
Iterator iterator = this.i.iterator();
2012-07-29 09:33:13 +02:00
VillageAggressor villageaggressor;
2012-03-09 03:54:29 +01:00
do {
if (!iterator.hasNext()) {
2012-07-29 09:33:13 +02:00
this.i.add(new VillageAggressor(this, entityliving, this.time));
2012-03-09 03:54:29 +01:00
return;
}
2012-07-29 09:33:13 +02:00
villageaggressor = (VillageAggressor) iterator.next();
} while (villageaggressor.a != entityliving);
2012-03-09 03:54:29 +01:00
2012-07-29 09:33:13 +02:00
villageaggressor.b = this.time;
2012-03-09 03:54:29 +01:00
}
public EntityLiving b(EntityLiving entityliving) {
double d0 = Double.MAX_VALUE;
2012-07-29 09:33:13 +02:00
VillageAggressor villageaggressor = null;
Iterator iterator = this.i.iterator();
2012-03-09 03:54:29 +01:00
2012-07-29 09:33:13 +02:00
while (iterator.hasNext()) {
VillageAggressor villageaggressor1 = (VillageAggressor) iterator.next();
double d1 = villageaggressor1.a.e(entityliving);
2012-03-09 03:54:29 +01:00
if (d1 <= d0) {
2012-07-29 09:33:13 +02:00
villageaggressor = villageaggressor1;
2012-03-09 03:54:29 +01:00
d0 = d1;
}
}
2012-07-29 09:33:13 +02:00
return villageaggressor != null ? villageaggressor.a : null;
2012-03-09 03:54:29 +01:00
}
private void j() {
Iterator iterator = this.i.iterator();
while (iterator.hasNext()) {
2012-07-29 09:33:13 +02:00
VillageAggressor villageaggressor = (VillageAggressor) iterator.next();
2012-03-09 03:54:29 +01:00
2012-07-29 09:33:13 +02:00
if (!villageaggressor.a.isAlive() || Math.abs(this.time - villageaggressor.b) > 300) {
2012-03-09 03:54:29 +01:00
iterator.remove();
}
}
}
private void k() {
boolean flag = false;
boolean flag1 = this.world.random.nextInt(50) == 0;
Iterator iterator = this.doors.iterator();
while (iterator.hasNext()) {
VillageDoor villagedoor = (VillageDoor) iterator.next();
if (flag1) {
villagedoor.d();
}
if (!this.isDoor(villagedoor.locX, villagedoor.locY, villagedoor.locZ) || Math.abs(this.time - villagedoor.addedTime) > 1200) {
this.c.x -= villagedoor.locX;
this.c.y -= villagedoor.locY;
this.c.z -= villagedoor.locZ;
flag = true;
villagedoor.g = true;
iterator.remove();
}
}
if (flag) {
this.l();
}
}
private boolean isDoor(int i, int j, int k) {
int l = this.world.getTypeId(i, j, k);
return l <= 0 ? false : l == Block.WOODEN_DOOR.id;
}
private void l() {
int i = this.doors.size();
if (i == 0) {
2012-07-29 09:33:13 +02:00
this.center.b(0, 0, 0);
2012-03-09 03:54:29 +01:00
this.size = 0;
} else {
2012-07-29 09:33:13 +02:00
this.center.b(this.c.x / i, this.c.y / i, this.c.z / i);
2012-03-09 03:54:29 +01:00
int j = 0;
VillageDoor villagedoor;
2012-07-29 09:33:13 +02:00
for (Iterator iterator = this.doors.iterator(); iterator.hasNext(); j = Math.max(villagedoor.b(this.center.x, this.center.y, this.center.z), j)) {
2012-03-09 03:54:29 +01:00
villagedoor = (VillageDoor) iterator.next();
}
2012-03-30 23:33:51 +02:00
this.size = Math.max(32, (int) Math.sqrt((double) j) + 1);
2012-03-09 03:54:29 +01:00
}
}
}