Added support for different tree generation types and a callback.

This commit is contained in:
sk89q 2011-01-30 13:53:57 -08:00
parent 1bc9c54233
commit 639d3e3f94
7 changed files with 826 additions and 9 deletions

View file

@ -0,0 +1,373 @@
package net.minecraft.server;
import java.util.Random;
//CraftBukkit start
import org.bukkit.BlockChangeDelegate;
//CraftBukkit end
public class WorldGenBigTree extends WorldGenerator {
static final byte[] a = new byte[] { (byte) 2, (byte) 0, (byte) 0, (byte) 1, (byte) 2, (byte) 1};
Random b = new Random();
// CraftBukkit start
BlockChangeDelegate c;
// CraftBukkit end
int[] d = new int[] { 0, 0, 0};
int e = 0;
int f;
double g = 0.618D;
double h = 1.0D;
double i = 0.381D;
double j = 1.0D;
double k = 1.0D;
int l = 1;
int m = 12;
int n = 4;
int[][] o;
public WorldGenBigTree() {}
void a() {
this.f = (int) ((double) this.e * this.g);
if (this.f >= this.e) {
this.f = this.e - 1;
}
int i = (int) (1.382D + Math.pow(this.k * (double) this.e / 13.0D, 2.0D));
if (i < 1) {
i = 1;
}
int[][] aint = new int[i * this.e][4];
int j = this.d[1] + this.e - this.n;
int k = 1;
int l = this.d[1] + this.f;
int i1 = j - this.d[1];
aint[0][0] = this.d[0];
aint[0][1] = j;
aint[0][2] = this.d[2];
aint[0][3] = l;
--j;
while (i1 >= 0) {
int j1 = 0;
float f = this.a(i1);
if (f < 0.0F) {
--j;
--i1;
} else {
for (double d0 = 0.5D; j1 < i; ++j1) {
double d1 = this.j * (double) f * ((double) this.b.nextFloat() + 0.328D);
double d2 = (double) this.b.nextFloat() * 2.0D * 3.14159D;
int k1 = (int) (d1 * Math.sin(d2) + (double) this.d[0] + d0);
int l1 = (int) (d1 * Math.cos(d2) + (double) this.d[2] + d0);
int[] aint1 = new int[] { k1, j, l1};
int[] aint2 = new int[] { k1, j + this.n, l1};
if (this.a(aint1, aint2) == -1) {
int[] aint3 = new int[] { this.d[0], this.d[1], this.d[2]};
double d3 = Math.sqrt(Math.pow((double) Math.abs(this.d[0] - aint1[0]), 2.0D) + Math.pow((double) Math.abs(this.d[2] - aint1[2]), 2.0D));
double d4 = d3 * this.i;
if ((double) aint1[1] - d4 > (double) l) {
aint3[1] = l;
} else {
aint3[1] = (int) ((double) aint1[1] - d4);
}
if (this.a(aint3, aint1) == -1) {
aint[k][0] = k1;
aint[k][1] = j;
aint[k][2] = l1;
aint[k][3] = aint3[1];
++k;
}
}
}
--j;
--i1;
}
}
this.o = new int[k][4];
System.arraycopy(aint, 0, this.o, 0, k);
}
void a(int i, int j, int k, float f, byte b0, int l) {
int i1 = (int) ((double) f + 0.618D);
byte b1 = a[b0];
byte b2 = a[b0 + 3];
int[] aint = new int[] { i, j, k};
int[] aint1 = new int[] { 0, 0, 0};
int j1 = -i1;
int k1 = -i1;
for (aint1[b0] = aint[b0]; j1 <= i1; ++j1) {
aint1[b1] = aint[b1] + j1;
k1 = -i1;
while (k1 <= i1) {
double d0 = Math.sqrt(Math.pow((double) Math.abs(j1) + 0.5D, 2.0D) + Math.pow((double) Math.abs(k1) + 0.5D, 2.0D));
if (d0 > (double) f) {
++k1;
} else {
aint1[b2] = aint[b2] + k1;
int l1 = this.c.getTypeId(aint1[0], aint1[1], aint1[2]);
if (l1 != 0 && l1 != 18) {
++k1;
} else {
this.c.setTypeId(aint1[0], aint1[1], aint1[2], l);
++k1;
}
}
}
}
}
float a(int i) {
if ((double) i < (double) ((float) this.e) * 0.3D) {
return -1.618F;
} else {
float f = (float) this.e / 2.0F;
float f1 = (float) this.e / 2.0F - (float) i;
float f2;
if (f1 == 0.0F) {
f2 = f;
} else if (Math.abs(f1) >= f) {
f2 = 0.0F;
} else {
f2 = (float) Math.sqrt(Math.pow((double) Math.abs(f), 2.0D) - Math.pow((double) Math.abs(f1), 2.0D));
}
f2 *= 0.5F;
return f2;
}
}
float b(int i) {
return i >= 0 && i < this.n ? (i != 0 && i != this.n - 1 ? 3.0F : 2.0F) : -1.0F;
}
void a(int i, int j, int k) {
int l = j;
for (int i1 = j + this.n; l < i1; ++l) {
float f = this.b(l - j);
this.a(i, l, k, f, (byte) 1, 18);
}
}
void a(int[] aint, int[] aint1, int i) {
int[] aint2 = new int[] { 0, 0, 0};
byte b0 = 0;
byte b1;
for (b1 = 0; b0 < 3; ++b0) {
aint2[b0] = aint1[b0] - aint[b0];
if (Math.abs(aint2[b0]) > Math.abs(aint2[b1])) {
b1 = b0;
}
}
if (aint2[b1] != 0) {
byte b2 = a[b1];
byte b3 = a[b1 + 3];
byte b4;
if (aint2[b1] > 0) {
b4 = 1;
} else {
b4 = -1;
}
double d0 = (double) aint2[b2] / (double) aint2[b1];
double d1 = (double) aint2[b3] / (double) aint2[b1];
int[] aint3 = new int[] { 0, 0, 0};
int j = 0;
for (int k = aint2[b1] + b4; j != k; j += b4) {
aint3[b1] = MathHelper.b((double) (aint[b1] + j) + 0.5D);
aint3[b2] = MathHelper.b((double) aint[b2] + (double) j * d0 + 0.5D);
aint3[b3] = MathHelper.b((double) aint[b3] + (double) j * d1 + 0.5D);
this.c.setTypeId(aint3[0], aint3[1], aint3[2], i);
}
}
}
void b() {
int i = 0;
for (int j = this.o.length; i < j; ++i) {
int k = this.o[i][0];
int l = this.o[i][1];
int i1 = this.o[i][2];
this.a(k, l, i1);
}
}
boolean c(int i) {
return (double) i >= (double) this.e * 0.2D;
}
void c() {
int i = this.d[0];
int j = this.d[1];
int k = this.d[1] + this.f;
int l = this.d[2];
int[] aint = new int[] { i, j, l};
int[] aint1 = new int[] { i, k, l};
this.a(aint, aint1, 17);
if (this.l == 2) {
++aint[0];
++aint1[0];
this.a(aint, aint1, 17);
++aint[2];
++aint1[2];
this.a(aint, aint1, 17);
aint[0] += -1;
aint1[0] += -1;
this.a(aint, aint1, 17);
}
}
void d() {
int i = 0;
int j = this.o.length;
for (int[] aint = new int[] { this.d[0], this.d[1], this.d[2]}; i < j; ++i) {
int[] aint1 = this.o[i];
int[] aint2 = new int[] { aint1[0], aint1[1], aint1[2]};
aint[1] = aint1[3];
int k = aint[1] - this.d[1];
if (this.c(k)) {
this.a(aint, aint2, 17);
}
}
}
int a(int[] aint, int[] aint1) {
int[] aint2 = new int[] { 0, 0, 0};
byte b0 = 0;
byte b1;
for (b1 = 0; b0 < 3; ++b0) {
aint2[b0] = aint1[b0] - aint[b0];
if (Math.abs(aint2[b0]) > Math.abs(aint2[b1])) {
b1 = b0;
}
}
if (aint2[b1] == 0) {
return -1;
} else {
byte b2 = a[b1];
byte b3 = a[b1 + 3];
byte b4;
if (aint2[b1] > 0) {
b4 = 1;
} else {
b4 = -1;
}
double d0 = (double) aint2[b2] / (double) aint2[b1];
double d1 = (double) aint2[b3] / (double) aint2[b1];
int[] aint3 = new int[] { 0, 0, 0};
int i = 0;
int j;
for (j = aint2[b1] + b4; i != j; i += b4) {
aint3[b1] = aint[b1] + i;
aint3[b2] = (int) ((double) aint[b2] + (double) i * d0);
aint3[b3] = (int) ((double) aint[b3] + (double) i * d1);
int k = this.c.getTypeId(aint3[0], aint3[1], aint3[2]);
if (k != 0 && k != 18) {
break;
}
}
return i == j ? -1 : Math.abs(i);
}
}
boolean e() {
int[] aint = new int[] { this.d[0], this.d[1], this.d[2]};
int[] aint1 = new int[] { this.d[0], this.d[1] + this.e - 1, this.d[2]};
int i = this.c.getTypeId(this.d[0], this.d[1] - 1, this.d[2]);
if (i != 2 && i != 3) {
return false;
} else {
int j = this.a(aint, aint1);
if (j == -1) {
return true;
} else if (j < 6) {
return false;
} else {
this.e = j;
return true;
}
}
}
public void a(double d0, double d1, double d2) {
this.m = (int) (d0 * 12.0D);
if (d0 > 0.5D) {
this.n = 5;
}
this.j = d1;
this.k = d2;
}
public boolean a(World world, Random random, int i, int j, int k) {
// CraftBukkit start
// sk: The idea is to have (our) WorldServer implement
// BlockChangeDelegate and then we can implicitly cast World to
// WorldServer (a safe cast, AFAIK) and no code will be broken. This
// then allows plugins to catch manually-invoked generation events
return generate((BlockChangeDelegate)world, random, i, j, k);
}
public boolean generate(BlockChangeDelegate world, Random random, int i, int j, int k) {
// CraftBukkit end
this.c = world;
long l = random.nextLong();
this.b.setSeed(l);
this.d[0] = i;
this.d[1] = j;
this.d[2] = k;
if (this.e == 0) {
this.e = 5 + this.b.nextInt(this.m);
}
if (!this.e()) {
return false;
} else {
this.a();
this.b();
this.c();
this.d();
return true;
}
}
}

View file

@ -0,0 +1,100 @@
package net.minecraft.server;
import java.util.Random;
//CraftBukkit start
import org.bukkit.BlockChangeDelegate;
//CraftBukkit end
public class WorldGenForest extends WorldGenerator {
public WorldGenForest() {}
public boolean a(World world, Random random, int i, int j, int k) {
// CraftBukkit start
// sk: The idea is to have (our) WorldServer implement
// BlockChangeDelegate and then we can implicitly cast World to
// WorldServer (a safe cast, AFAIK) and no code will be broken. This
// then allows plugins to catch manually-invoked generation events
return generate((BlockChangeDelegate)world, random, i, j, k);
}
public boolean generate(BlockChangeDelegate world, Random random, int i, int j, int k) {
// CraftBukkit end
int l = random.nextInt(3) + 5;
boolean flag = true;
if (j >= 1 && j + l + 1 <= 128) {
int i1;
int j1;
int k1;
int l1;
for (i1 = j; i1 <= j + 1 + l; ++i1) {
byte b0 = 1;
if (i1 == j) {
b0 = 0;
}
if (i1 >= j + 1 + l - 2) {
b0 = 2;
}
for (j1 = i - b0; j1 <= i + b0 && flag; ++j1) {
for (k1 = k - b0; k1 <= k + b0 && flag; ++k1) {
if (i1 >= 0 && i1 < 128) {
l1 = world.getTypeId(j1, i1, k1);
if (l1 != 0 && l1 != Block.LEAVES.id) {
flag = false;
}
} else {
flag = false;
}
}
}
}
if (!flag) {
return false;
} else {
i1 = world.getTypeId(i, j - 1, k);
if ((i1 == Block.GRASS.id || i1 == Block.DIRT.id) && j < 128 - l - 1) {
world.setTypeId(i, j - 1, k, Block.DIRT.id);
int i2;
for (i2 = j - 3 + l; i2 <= j + l; ++i2) {
j1 = i2 - (j + l);
k1 = 1 - j1 / 2;
for (l1 = i - k1; l1 <= i + k1; ++l1) {
int j2 = l1 - i;
for (int k2 = k - k1; k2 <= k + k1; ++k2) {
int l2 = k2 - k;
if ((Math.abs(j2) != k1 || Math.abs(l2) != k1 || random.nextInt(2) != 0 && j1 != 0) && !Block.o[world.getTypeId(l1, i2, k2)]) {
world.setTypeIdAndData(l1, i2, k2, Block.LEAVES.id, 2);
}
}
}
}
for (i2 = 0; i2 < l; ++i2) {
j1 = world.getTypeId(i, j + i2, k);
if (j1 == 0 || j1 == Block.LEAVES.id) {
world.setTypeIdAndData(i, j + i2, k, Block.LOG.id, 2);
}
}
return true;
} else {
return false;
}
}
} else {
return false;
}
}
}

View file

@ -0,0 +1,104 @@
package net.minecraft.server;
import java.util.Random;
//CraftBukkit start
import org.bukkit.BlockChangeDelegate;
//CraftBukkit end
public class WorldGenTaiga1 extends WorldGenerator {
public WorldGenTaiga1() {}
public boolean a(World world, Random random, int i, int j, int k) {
// CraftBukkit start
// sk: The idea is to have (our) WorldServer implement
// BlockChangeDelegate and then we can implicitly cast World to
// WorldServer (a safe cast, AFAIK) and no code will be broken. This
// then allows plugins to catch manually-invoked generation events
return generate((BlockChangeDelegate)world, random, i, j, k);
}
public boolean generate(BlockChangeDelegate world, Random random, int i, int j, int k) {
// CraftBukkit end
int l = random.nextInt(5) + 7;
int i1 = l - random.nextInt(2) - 3;
int j1 = l - i1;
int k1 = 1 + random.nextInt(j1 + 1);
boolean flag = true;
if (j >= 1 && j + l + 1 <= 128) {
int l1;
int i2;
int j2;
int k2;
int l2;
for (l1 = j; l1 <= j + 1 + l && flag; ++l1) {
boolean flag1 = true;
if (l1 - j < i1) {
l2 = 0;
} else {
l2 = k1;
}
for (i2 = i - l2; i2 <= i + l2 && flag; ++i2) {
for (j2 = k - l2; j2 <= k + l2 && flag; ++j2) {
if (l1 >= 0 && l1 < 128) {
k2 = world.getTypeId(i2, l1, j2);
if (k2 != 0 && k2 != Block.LEAVES.id) {
flag = false;
}
} else {
flag = false;
}
}
}
}
if (!flag) {
return false;
} else {
l1 = world.getTypeId(i, j - 1, k);
if ((l1 == Block.GRASS.id || l1 == Block.DIRT.id) && j < 128 - l - 1) {
world.setTypeId(i, j - 1, k, Block.DIRT.id);
l2 = 0;
for (i2 = j + l; i2 >= j + i1; --i2) {
for (j2 = i - l2; j2 <= i + l2; ++j2) {
k2 = j2 - i;
for (int i3 = k - l2; i3 <= k + l2; ++i3) {
int j3 = i3 - k;
if ((Math.abs(k2) != l2 || Math.abs(j3) != l2 || l2 <= 0) && !Block.o[world.getTypeId(j2, i2, i3)]) {
world.setTypeIdAndData(j2, i2, i3, Block.LEAVES.id, 1);
}
}
}
if (l2 >= 1 && i2 == j + i1 + 1) {
--l2;
} else if (l2 < k1) {
++l2;
}
}
for (i2 = 0; i2 < l - 1; ++i2) {
j2 = world.getTypeId(i, j + i2, k);
if (j2 == 0 || j2 == Block.LEAVES.id) {
world.setTypeIdAndData(i, j + i2, k, Block.LOG.id, 1);
}
}
return true;
} else {
return false;
}
}
} else {
return false;
}
}
}

View file

@ -0,0 +1,117 @@
package net.minecraft.server;
import java.util.Random;
//CraftBukkit start
import org.bukkit.BlockChangeDelegate;
//CraftBukkit end
public class WorldGenTaiga2 extends WorldGenerator {
public WorldGenTaiga2() {}
public boolean a(World world, Random random, int i, int j, int k) {
// CraftBukkit start
// sk: The idea is to have (our) WorldServer implement
// BlockChangeDelegate and then we can implicitly cast World to
// WorldServer (a safe cast, AFAIK) and no code will be broken. This
// then allows plugins to catch manually-invoked generation events
return generate((BlockChangeDelegate)world, random, i, j, k);
}
public boolean generate(BlockChangeDelegate world, Random random, int i, int j, int k) {
// CraftBukkit end
int l = random.nextInt(4) + 6;
int i1 = 1 + random.nextInt(2);
int j1 = l - i1;
int k1 = 2 + random.nextInt(2);
boolean flag = true;
if (j >= 1 && j + l + 1 <= 128) {
int l1;
int i2;
int j2;
int k2;
for (l1 = j; l1 <= j + 1 + l && flag; ++l1) {
boolean flag1 = true;
if (l1 - j < i1) {
k2 = 0;
} else {
k2 = k1;
}
for (i2 = i - k2; i2 <= i + k2 && flag; ++i2) {
for (int l2 = k - k2; l2 <= k + k2 && flag; ++l2) {
if (l1 >= 0 && l1 < 128) {
j2 = world.getTypeId(i2, l1, l2);
if (j2 != 0 && j2 != Block.LEAVES.id) {
flag = false;
}
} else {
flag = false;
}
}
}
}
if (!flag) {
return false;
} else {
l1 = world.getTypeId(i, j - 1, k);
if ((l1 == Block.GRASS.id || l1 == Block.DIRT.id) && j < 128 - l - 1) {
world.setTypeId(i, j - 1, k, Block.DIRT.id);
k2 = random.nextInt(2);
i2 = 1;
byte b0 = 0;
int i3;
int j3;
for (j2 = 0; j2 <= j1; ++j2) {
j3 = j + l - j2;
for (i3 = i - k2; i3 <= i + k2; ++i3) {
int k3 = i3 - i;
for (int l3 = k - k2; l3 <= k + k2; ++l3) {
int i4 = l3 - k;
if ((Math.abs(k3) != k2 || Math.abs(i4) != k2 || k2 <= 0) && !Block.o[world.getTypeId(i3, j3, l3)]) {
world.setTypeIdAndData(i3, j3, l3, Block.LEAVES.id, 1);
}
}
}
if (k2 >= i2) {
k2 = b0;
b0 = 1;
++i2;
if (i2 > k1) {
i2 = k1;
}
} else {
++k2;
}
}
j2 = random.nextInt(3);
for (j3 = 0; j3 < l - j2; ++j3) {
i3 = world.getTypeId(i, j + j3, k);
if (i3 == 0 || i3 == Block.LEAVES.id) {
world.setTypeIdAndData(i, j + j3, k, Block.LOG.id, 1);
}
}
return true;
} else {
return false;
}
}
} else {
return false;
}
}
}

View file

@ -0,0 +1,100 @@
package net.minecraft.server;
import java.util.Random;
// CraftBukkit start
import org.bukkit.BlockChangeDelegate;
// CraftBukkit end
public class WorldGenTrees extends WorldGenerator {
public WorldGenTrees() {}
public boolean a(World world, Random random, int i, int j, int k) {
// CraftBukkit start
// sk: The idea is to have (our) WorldServer implement
// BlockChangeDelegate and then we can implicitly cast World to
// WorldServer (a safe cast, AFAIK) and no code will be broken. This
// then allows plugins to catch manually-invoked generation events
return generate((BlockChangeDelegate)world, random, i, j, k);
}
public boolean generate(BlockChangeDelegate world, Random random, int i, int j, int k) {
// CraftBukkit end
int l = random.nextInt(3) + 4;
boolean flag = true;
if (j >= 1 && j + l + 1 <= 128) {
int i1;
int j1;
int k1;
int l1;
for (i1 = j; i1 <= j + 1 + l; ++i1) {
byte b0 = 1;
if (i1 == j) {
b0 = 0;
}
if (i1 >= j + 1 + l - 2) {
b0 = 2;
}
for (j1 = i - b0; j1 <= i + b0 && flag; ++j1) {
for (k1 = k - b0; k1 <= k + b0 && flag; ++k1) {
if (i1 >= 0 && i1 < 128) {
l1 = world.getTypeId(j1, i1, k1);
if (l1 != 0 && l1 != Block.LEAVES.id) {
flag = false;
}
} else {
flag = false;
}
}
}
}
if (!flag) {
return false;
} else {
i1 = world.getTypeId(i, j - 1, k);
if ((i1 == Block.GRASS.id || i1 == Block.DIRT.id) && j < 128 - l - 1) {
world.setTypeId(i, j - 1, k, Block.DIRT.id);
int i2;
for (i2 = j - 3 + l; i2 <= j + l; ++i2) {
j1 = i2 - (j + l);
k1 = 1 - j1 / 2;
for (l1 = i - k1; l1 <= i + k1; ++l1) {
int j2 = l1 - i;
for (int k2 = k - k1; k2 <= k + k1; ++k2) {
int l2 = k2 - k;
if ((Math.abs(j2) != k1 || Math.abs(l2) != k1 || random.nextInt(2) != 0 && j1 != 0) && !Block.o[world.getTypeId(l1, i2, k2)]) {
world.setTypeId(l1, i2, k2, Block.LEAVES.id);
}
}
}
}
for (i2 = 0; i2 < l; ++i2) {
j1 = world.getTypeId(i, j + i2, k);
if (j1 == 0 || j1 == Block.LEAVES.id) {
world.setTypeId(i, j + i2, k, Block.LOG.id);
}
}
return true;
} else {
return false;
}
}
} else {
return false;
}
}
}

View file

@ -6,15 +6,16 @@ import java.util.List;
import java.util.Random;
// CraftBukkit start
import org.bukkit.BlockChangeDelegate;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.event.Event;
import org.bukkit.event.Event.Type;
import org.bukkit.event.block.BlockCanBuildEvent;
import org.bukkit.event.block.BlockPhysicsEvent;
// CraftBukkit end
public class WorldServer extends World {
public class WorldServer extends World implements BlockChangeDelegate {
// CraftBukkit end
public ChunkProviderServer A;
public boolean B = false;

View file

@ -18,7 +18,9 @@ import org.bukkit.Chunk;
import org.bukkit.entity.ItemDrop;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Vector;
import org.bukkit.BlockChangeDelegate;
import org.bukkit.Location;
import org.bukkit.TreeType;
import org.bukkit.World;
public class CraftWorld implements World {
@ -194,14 +196,34 @@ public class CraftWorld implements World {
return (Boat) boat.getBukkitEntity();
}
public boolean generateTree(Location loc) {
WorldGenTrees treeGen = new WorldGenTrees();
return treeGen.a(world, rand, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
public boolean generateTree(Location loc, TreeType type) {
return generateTree(loc, type, world);
}
public boolean generateBigTree(Location loc) {
WorldGenBigTree treeGen = new WorldGenBigTree();
return treeGen.a(world, rand, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
public boolean generateTree(Location loc, TreeType type, BlockChangeDelegate delegate) {
switch (type) {
case BIG_TREE:
return (new WorldGenBigTree())
.generate(delegate, rand,
loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
case BIRCH:
return (new WorldGenForest())
.generate(delegate, rand,
loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
case REDWOOD:
return (new WorldGenTaiga2())
.generate(delegate, rand,
loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
case TALL_REDWOOD:
return (new WorldGenTaiga1())
.generate(delegate, rand,
loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
case TREE:
default:
return (new WorldGenTrees())
.generate(delegate, rand,
loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
}
}
public TileEntity getTileEntityAt(final int x, final int y, final int z) {