PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/HybridPop.java

234 lines
9.4 KiB
Java
Raw Normal View History

2014-12-28 13:02:30 +01:00
package com.intellectualcrafters.plot.generator;
2015-07-05 17:44:10 +02:00
import java.util.HashMap;
import java.util.HashSet;
2015-07-03 11:30:26 +02:00
import org.bukkit.World;
import org.bukkit.block.Biome;
2015-07-05 17:44:10 +02:00
import com.intellectualcrafters.plot.PS;
import com.intellectualcrafters.plot.object.PlotLoc;
import com.intellectualcrafters.plot.object.PlotPopulator;
import com.intellectualcrafters.plot.object.PlotWorld;
import com.intellectualcrafters.plot.object.PseudoRandom;
import com.intellectualcrafters.plot.object.RegionWrapper;
import com.intellectualcrafters.plot.object.schematic.PlotItem;
import com.intellectualcrafters.plot.util.BlockManager;
2014-12-28 13:02:30 +01:00
/**
* @author Citymonstret
*/
2015-03-27 08:36:28 +01:00
public class HybridPop extends PlotPopulator {
2014-12-28 13:02:30 +01:00
/*
* Sorry, this isn't well documented at the moment.
* We advise you to take a look at a world generation tutorial for
* information about how a BlockPopulator works.
*/
final short plotsize;
final short pathsize;
final byte wall;
final byte wallfilling;
final byte roadblock;
final int size;
final int roadheight;
final int wallheight;
final int plotheight;
final byte[] plotfloors;
final byte[] filling;
final short pathWidthLower;
final short pathWidthUpper;
2015-07-03 11:30:26 +02:00
private final HybridPlotWorld plotworld;
2014-12-28 13:02:30 +01:00
Biome biome;
private long state;
private boolean doFilling = false;
private boolean doFloor = false;
private boolean doState = false;
2015-02-23 02:32:27 +01:00
2014-12-28 13:02:30 +01:00
public HybridPop(final PlotWorld pw) {
this.plotworld = (HybridPlotWorld) pw;
// save configuration
this.plotsize = (short) this.plotworld.PLOT_WIDTH;
this.pathsize = (short) this.plotworld.ROAD_WIDTH;
this.roadblock = this.plotworld.ROAD_BLOCK.data;
this.wallfilling = this.plotworld.WALL_FILLING.data;
this.size = this.pathsize + this.plotsize;
this.wall = this.plotworld.WALL_BLOCK.data;
int count1 = 0;
int count2 = 0;
this.plotfloors = new byte[this.plotworld.TOP_BLOCK.length];
for (int i = 0; i < this.plotworld.TOP_BLOCK.length; i++) {
count1++;
this.plotfloors[i] = this.plotworld.TOP_BLOCK[i].data;
if (this.plotworld.TOP_BLOCK[i].data != 0) {
this.doFloor = true;
}
}
this.filling = new byte[this.plotworld.MAIN_BLOCK.length];
for (int i = 0; i < this.plotworld.MAIN_BLOCK.length; i++) {
count2++;
this.filling[i] = this.plotworld.MAIN_BLOCK[i].data;
if (this.plotworld.MAIN_BLOCK[i].data != 0) {
this.doFilling = true;
}
}
2015-02-20 07:34:19 +01:00
if (((count1 > 0) && this.doFloor) || ((count2 > 0) && this.doFilling)) {
this.doState = true;
2014-12-28 13:02:30 +01:00
}
this.wallheight = this.plotworld.WALL_HEIGHT;
this.roadheight = this.plotworld.ROAD_HEIGHT;
this.plotheight = this.plotworld.PLOT_HEIGHT;
2015-04-04 18:20:26 +02:00
if (this.pathsize == 0) {
this.pathWidthLower = (short) -1;
this.pathWidthUpper = (short) (this.plotsize + 1);
}
else {
if ((this.pathsize % 2) == 0) {
this.pathWidthLower = (short) (Math.floor(this.pathsize / 2) - 1);
} else {
this.pathWidthLower = (short) (Math.floor(this.pathsize / 2));
}
this.pathWidthUpper = (short) (this.pathWidthLower + this.plotsize + 1);
2014-12-28 13:02:30 +01:00
}
}
2015-02-23 02:32:27 +01:00
2014-12-28 13:02:30 +01:00
public final long nextLong() {
final long a = this.state;
this.state = xorShift64(a);
return a;
}
2015-02-23 02:32:27 +01:00
2014-12-28 13:02:30 +01:00
public final long xorShift64(long a) {
a ^= (a << 21);
a ^= (a >>> 35);
a ^= (a << 4);
return a;
}
2015-02-23 02:32:27 +01:00
2014-12-28 13:02:30 +01:00
public final int random(final int n) {
final long result = ((nextLong() >>> 32) * n) >> 32;
return (int) result;
}
2015-02-23 02:32:27 +01:00
2014-12-28 13:02:30 +01:00
@Override
2015-03-27 08:36:28 +01:00
public void populate(World world, RegionWrapper requiredRegion, PseudoRandom random, int cx, int cz) {
PS.get().getPlotManager(world.getName());
2015-07-16 12:32:04 +02:00
int sx = (short) ((this.X - this.plotworld.ROAD_OFFSET_X) % this.size);
int sz = (short) ((this.Z - this.plotworld.ROAD_OFFSET_Z) % this.size);
if (sx < 0) {
sx += this.size;
}
if (sz < 0) {
sz += this.size;
}
2015-03-27 08:36:28 +01:00
if (requiredRegion != null) {
2014-12-28 13:02:30 +01:00
for (short x = 0; x < 16; x++) {
for (short z = 0; z < 16; z++) {
2015-04-19 02:44:11 +02:00
if (contains(requiredRegion, x, z)) {
2015-02-20 07:34:19 +01:00
if (this.doFilling) {
2014-12-28 13:02:30 +01:00
for (short y = 1; y < this.plotheight; y++) {
2015-03-27 08:36:28 +01:00
setBlock(x, y, z, this.filling);
2014-12-28 13:02:30 +01:00
}
}
2015-02-20 07:34:19 +01:00
if (this.doFloor) {
2015-03-27 08:36:28 +01:00
setBlock(x, (short) this.plotheight, z, this.plotfloors);
2014-12-28 13:02:30 +01:00
}
if (this.plotworld.PLOT_SCHEMATIC) {
final int absX = ((sx + x) % this.size);
final int absZ = ((sz + z) % this.size);
final PlotLoc loc = new PlotLoc(absX, absZ);
final HashMap<Short, Byte> blocks = this.plotworld.G_SCH_DATA.get(loc);
if (blocks != null) {
for (final short y : blocks.keySet()) {
2015-05-02 08:18:34 +02:00
setBlockAbs(x, (short) (this.plotheight + y), z, blocks.get(y));
}
}
2015-02-27 11:28:16 +01:00
if (this.plotworld.G_SCH_STATE != null) {
HashSet<PlotItem> states = this.plotworld.G_SCH_STATE.get(loc);
if (states != null) {
for (PlotItem items : states) {
BlockManager.manager.addItems(this.plotworld.worldname, items);
}
}
}
}
2014-12-28 13:02:30 +01:00
}
}
}
return;
}
2015-04-01 14:36:46 +02:00
2014-12-28 13:02:30 +01:00
for (short x = 0; x < 16; x++) {
for (short z = 0; z < 16; z++) {
2015-04-01 14:36:46 +02:00
final int absX = ((sx + x) % this.size);
final int absZ = ((sz + z) % this.size);
2015-02-20 07:34:19 +01:00
final boolean gx = absX > this.pathWidthLower;
final boolean gz = absZ > this.pathWidthLower;
final boolean lx = absX < this.pathWidthUpper;
final boolean lz = absZ < this.pathWidthUpper;
2014-12-28 13:02:30 +01:00
// inside plot
if (gx && gz && lx && lz) {
2015-04-01 14:36:46 +02:00
for (short y = 1; y < this.plotheight; y++) {
setBlock(x, y, z, this.filling);
2014-12-28 13:02:30 +01:00
}
2015-04-01 14:36:46 +02:00
setBlock(x, (short) this.plotheight, z, this.plotfloors);
if (this.plotworld.PLOT_SCHEMATIC) {
final PlotLoc loc = new PlotLoc(absX, absZ);
final HashMap<Short, Byte> blocks = this.plotworld.G_SCH_DATA.get(loc);
if (blocks != null) {
for (final short y : blocks.keySet()) {
2015-05-02 08:18:34 +02:00
setBlockAbs(x, (short) (this.plotheight + y), z, blocks.get(y));
}
}
2015-02-27 11:28:16 +01:00
if (this.plotworld.G_SCH_STATE != null) {
HashSet<PlotItem> states = this.plotworld.G_SCH_STATE.get(loc);
if (states != null) {
for (PlotItem items : states) {
items.x = this.X + x;
items.z = this.Z + z;
BlockManager.manager.addItems(this.plotworld.worldname, items);
}
}
}
}
2015-04-04 18:20:26 +02:00
} else if (pathsize != 0) {
2014-12-28 13:02:30 +01:00
// wall
2015-02-20 07:34:19 +01:00
if (((absX >= this.pathWidthLower) && (absX <= this.pathWidthUpper) && (absZ >= this.pathWidthLower) && (absZ <= this.pathWidthUpper))) {
2015-04-01 14:36:46 +02:00
for (short y = 1; y <= this.wallheight; y++) {
setBlock(x, y, z, this.wallfilling);
2014-12-28 13:02:30 +01:00
}
2015-04-01 14:36:46 +02:00
if (!this.plotworld.ROAD_SCHEMATIC_ENABLED) {
setBlock(x, this.wallheight + 1, z, this.wall);
2014-12-28 13:02:30 +01:00
}
}
// road
else {
2015-04-01 14:36:46 +02:00
for (short y = 1; y <= this.roadheight; y++) {
setBlock(x, y, z, this.roadblock);
2014-12-28 13:02:30 +01:00
}
}
if (this.plotworld.ROAD_SCHEMATIC_ENABLED) {
final PlotLoc loc = new PlotLoc(absX, absZ);
2015-02-20 07:34:19 +01:00
final HashMap<Short, Byte> blocks = this.plotworld.G_SCH_DATA.get(loc);
2014-12-28 13:02:30 +01:00
if (blocks != null) {
2015-02-20 07:34:19 +01:00
for (final short y : blocks.keySet()) {
2015-05-02 08:18:34 +02:00
setBlockAbs(x, (short) (this.roadheight + y), z, blocks.get(y));
2014-12-28 13:02:30 +01:00
}
}
}
}
}
}
}
2015-02-23 02:32:27 +01:00
2015-03-27 08:36:28 +01:00
private void setBlock(final short x, final short y, final short z, final byte[] blkids) {
2014-12-28 13:02:30 +01:00
if (blkids.length == 1) {
2015-03-27 08:36:28 +01:00
setBlock(x, y, z, blkids[0]);
2015-02-20 07:34:19 +01:00
} else {
2014-12-28 13:02:30 +01:00
final int i = random(blkids.length);
2015-03-27 08:36:28 +01:00
setBlock(x, y, z, blkids[i]);
2014-12-28 13:02:30 +01:00
}
}
}