| 1 | package net.minecraft.world.gen.structure;
|
| 2 |
|
| 3 | import com.google.common.collect.Maps;
|
| 4 | import java.util.Iterator;
|
| 5 | import java.util.List;
|
| 6 | import java.util.Map;
|
| 7 | import java.util.Random;
|
| 8 | import java.util.concurrent.Callable;
|
| 9 | import net.minecraft.crash.CrashReport;
|
| 10 | import net.minecraft.crash.CrashReportCategory;
|
| 11 | import net.minecraft.nbt.NBTBase;
|
| 12 | import net.minecraft.nbt.NBTTagCompound;
|
| 13 | import net.minecraft.util.BlockPos;
|
| 14 | import net.minecraft.util.ReportedException;
|
| 15 | import net.minecraft.world.ChunkCoordIntPair;
|
| 16 | import net.minecraft.world.World;
|
| 17 | import net.minecraft.world.chunk.ChunkPrimer;
|
| 18 | import net.minecraft.world.gen.MapGenBase;
|
| 19 |
|
| 20 | public abstract class MapGenStructure extends MapGenBase
|
| 21 | {
|
| 22 | private MapGenStructureData structureData;
|
| 23 | protected Map<Long, StructureStart> structureMap = Maps.<Long, StructureStart>newHashMap();
|
| 24 |
|
| 25 | public abstract String getStructureName();
|
| 26 |
|
| 27 | /**
|
| 28 | * Recursively called by generate()
|
| 29 | */
|
| 30 | protected final void recursiveGenerate(World worldIn, final int chunkX, final int chunkZ, int p_180701_4_, int p_180701_5_, ChunkPrimer chunkPrimerIn)
|
| 31 | {
|
| 32 | this.initializeStructureData(worldIn);
|
| 33 |
|
| 34 | if (!this.structureMap.containsKey(Long.valueOf(ChunkCoordIntPair.chunkXZ2Int(chunkX, chunkZ))))
|
| 35 | {
|
| 36 | this.rand.nextInt();
|
| 37 |
|
| 38 | try
|
| 39 | {
|
| 40 | if (this.canSpawnStructureAtCoords(chunkX, chunkZ))
|
| 41 | {
|
| 42 | StructureStart structurestart = this.getStructureStart(chunkX, chunkZ);
|
| 43 | this.structureMap.put(Long.valueOf(ChunkCoordIntPair.chunkXZ2Int(chunkX, chunkZ)), structurestart);
|
| 44 | this.setStructureStart(chunkX, chunkZ, structurestart);
|
| 45 | }
|
| 46 | }
|
| 47 | catch (Throwable throwable)
|
| 48 | {
|
| 49 | CrashReport crashreport = CrashReport.makeCrashReport(throwable, "Exception preparing structure feature");
|
| 50 | CrashReportCategory crashreportcategory = crashreport.makeCategory("Feature being prepared");
|
| 51 | crashreportcategory.addCrashSectionCallable("Is feature chunk", new Callable<String>()
|
| 52 | {
|
| 53 | public String call() throws Exception
|
| 54 | {
|
| 55 | return MapGenStructure.this.canSpawnStructureAtCoords(chunkX, chunkZ) ? "True" : "False";
|
| 56 | }
|
| 57 | });
|
| 58 | crashreportcategory.addCrashSection("Chunk location", String.format("%d,%d", new Object[] {Integer.valueOf(chunkX), Integer.valueOf(chunkZ)}));
|
| 59 | crashreportcategory.addCrashSectionCallable("Chunk pos hash", new Callable<String>()
|
| 60 | {
|
| 61 | public String call() throws Exception
|
| 62 | {
|
| 63 | return String.valueOf(ChunkCoordIntPair.chunkXZ2Int(chunkX, chunkZ));
|
| 64 | }
|
| 65 | });
|
| 66 | crashreportcategory.addCrashSectionCallable("Structure type", new Callable<String>()
|
| 67 | {
|
| 68 | public String call() throws Exception
|
| 69 | {
|
| 70 | return MapGenStructure.this.getClass().getCanonicalName();
|
| 71 | }
|
| 72 | });
|
| 73 | throw new ReportedException(crashreport);
|
| 74 | }
|
| 75 | }
|
| 76 | }
|
| 77 |
|
| 78 | public boolean generateStructure(World worldIn, Random randomIn, ChunkCoordIntPair chunkCoord)
|
| 79 | {
|
| 80 | this.initializeStructureData(worldIn);
|
| 81 | int i = (chunkCoord.chunkXPos << 4) + 8;
|
| 82 | int j = (chunkCoord.chunkZPos << 4) + 8;
|
| 83 | boolean flag = false;
|
| 84 |
|
| 85 | for (StructureStart structurestart : this.structureMap.values())
|
| 86 | {
|
| 87 | if (structurestart.isSizeableStructure() && structurestart.func_175788_a(chunkCoord) && structurestart.getBoundingBox().intersectsWith(i, j, i + 15, j + 15))
|
| 88 | {
|
| 89 | structurestart.generateStructure(worldIn, randomIn, new StructureBoundingBox(i, j, i + 15, j + 15));
|
| 90 | structurestart.func_175787_b(chunkCoord);
|
| 91 | flag = true;
|
| 92 | this.setStructureStart(structurestart.getChunkPosX(), structurestart.getChunkPosZ(), structurestart);
|
| 93 | }
|
| 94 | }
|
| 95 |
|
| 96 | return flag;
|
| 97 | }
|
| 98 |
|
| 99 | public boolean func_175795_b(BlockPos pos)
|
| 100 | {
|
| 101 | this.initializeStructureData(this.worldObj);
|
| 102 | return this.func_175797_c(pos) != null;
|
| 103 | }
|
| 104 |
|
| 105 | protected StructureStart func_175797_c(BlockPos pos)
|
| 106 | {
|
| 107 | label24:
|
| 108 |
|
| 109 | for (StructureStart structurestart : this.structureMap.values())
|
| 110 | {
|
| 111 | if (structurestart.isSizeableStructure() && structurestart.getBoundingBox().isVecInside(pos))
|
| 112 | {
|
| 113 | Iterator<StructureComponent> iterator = structurestart.getComponents().iterator();
|
| 114 |
|
| 115 | while (true)
|
| 116 | {
|
| 117 | if (!iterator.hasNext())
|
| 118 | {
|
| 119 | continue label24;
|
| 120 | }
|
| 121 |
|
| 122 | StructureComponent structurecomponent = (StructureComponent)iterator.next();
|
| 123 |
|
| 124 | if (structurecomponent.getBoundingBox().isVecInside(pos))
|
| 125 | {
|
| 126 | break;
|
| 127 | }
|
| 128 | }
|
| 129 |
|
| 130 | return structurestart;
|
| 131 | }
|
| 132 | }
|
| 133 |
|
| 134 | return null;
|
| 135 | }
|
| 136 |
|
| 137 | public boolean isPositionInStructure(World worldIn, BlockPos pos)
|
| 138 | {
|
| 139 | this.initializeStructureData(worldIn);
|
| 140 |
|
| 141 | for (StructureStart structurestart : this.structureMap.values())
|
| 142 | {
|
| 143 | if (structurestart.isSizeableStructure() && structurestart.getBoundingBox().isVecInside(pos))
|
| 144 | {
|
| 145 | return true;
|
| 146 | }
|
| 147 | }
|
| 148 |
|
| 149 | return false;
|
| 150 | }
|
| 151 |
|
| 152 | public BlockPos getClosestStrongholdPos(World worldIn, BlockPos pos)
|
| 153 | {
|
| 154 | this.worldObj = worldIn;
|
| 155 | this.initializeStructureData(worldIn);
|
| 156 | this.rand.setSeed(worldIn.getSeed());
|
| 157 | long i = this.rand.nextLong();
|
| 158 | long j = this.rand.nextLong();
|
| 159 | long k = (long)(pos.getX() >> 4) * i;
|
| 160 | long l = (long)(pos.getZ() >> 4) * j;
|
| 161 | this.rand.setSeed(k ^ l ^ worldIn.getSeed());
|
| 162 | this.recursiveGenerate(worldIn, pos.getX() >> 4, pos.getZ() >> 4, 0, 0, (ChunkPrimer)null);
|
| 163 | double d0 = Double.MAX_VALUE;
|
| 164 | BlockPos blockpos = null;
|
| 165 |
|
| 166 | for (StructureStart structurestart : this.structureMap.values())
|
| 167 | {
|
| 168 | if (structurestart.isSizeableStructure())
|
| 169 | {
|
| 170 | StructureComponent structurecomponent = (StructureComponent)structurestart.getComponents().get(0);
|
| 171 | BlockPos blockpos1 = structurecomponent.getBoundingBoxCenter();
|
| 172 | double d1 = blockpos1.distanceSq(pos);
|
| 173 |
|
| 174 | if (d1 < d0)
|
| 175 | {
|
| 176 | d0 = d1;
|
| 177 | blockpos = blockpos1;
|
| 178 | }
|
| 179 | }
|
| 180 | }
|
| 181 |
|
| 182 | if (blockpos != null)
|
| 183 | {
|
| 184 | return blockpos;
|
| 185 | }
|
| 186 | else
|
| 187 | {
|
| 188 | List<BlockPos> list = this.getCoordList();
|
| 189 |
|
| 190 | if (list != null)
|
| 191 | {
|
| 192 | BlockPos blockpos2 = null;
|
| 193 |
|
| 194 | for (BlockPos blockpos3 : list)
|
| 195 | {
|
| 196 | double d2 = blockpos3.distanceSq(pos);
|
| 197 |
|
| 198 | if (d2 < d0)
|
| 199 | {
|
| 200 | d0 = d2;
|
| 201 | blockpos2 = blockpos3;
|
| 202 | }
|
| 203 | }
|
| 204 |
|
| 205 | return blockpos2;
|
| 206 | }
|
| 207 | else
|
| 208 | {
|
| 209 | return null;
|
| 210 | }
|
| 211 | }
|
| 212 | }
|
| 213 |
|
| 214 | protected List<BlockPos> getCoordList()
|
| 215 | {
|
| 216 | return null;
|
| 217 | }
|
| 218 |
|
| 219 | private void initializeStructureData(World worldIn)
|
| 220 | {
|
| 221 | if (this.structureData == null)
|
| 222 | {
|
| 223 | this.structureData = (MapGenStructureData)worldIn.loadItemData(MapGenStructureData.class, this.getStructureName());
|
| 224 |
|
| 225 | if (this.structureData == null)
|
| 226 | {
|
| 227 | this.structureData = new MapGenStructureData(this.getStructureName());
|
| 228 | worldIn.setItemData(this.getStructureName(), this.structureData);
|
| 229 | }
|
| 230 | else
|
| 231 | {
|
| 232 | NBTTagCompound nbttagcompound = this.structureData.getTagCompound();
|
| 233 |
|
| 234 | for (String s : nbttagcompound.getKeySet())
|
| 235 | {
|
| 236 | NBTBase nbtbase = nbttagcompound.getTag(s);
|
| 237 |
|
| 238 | if (nbtbase.getId() == 10)
|
| 239 | {
|
| 240 | NBTTagCompound nbttagcompound1 = (NBTTagCompound)nbtbase;
|
| 241 |
|
| 242 | if (nbttagcompound1.hasKey("ChunkX") && nbttagcompound1.hasKey("ChunkZ"))
|
| 243 | {
|
| 244 | int i = nbttagcompound1.getInteger("ChunkX");
|
| 245 | int j = nbttagcompound1.getInteger("ChunkZ");
|
| 246 | StructureStart structurestart = MapGenStructureIO.getStructureStart(nbttagcompound1, worldIn);
|
| 247 |
|
| 248 | if (structurestart != null)
|
| 249 | {
|
| 250 | this.structureMap.put(Long.valueOf(ChunkCoordIntPair.chunkXZ2Int(i, j)), structurestart);
|
| 251 | }
|
| 252 | }
|
| 253 | }
|
| 254 | }
|
| 255 | }
|
| 256 | }
|
| 257 | }
|
| 258 |
|
| 259 | private void setStructureStart(int chunkX, int chunkZ, StructureStart start)
|
| 260 | {
|
| 261 | this.structureData.writeInstance(start.writeStructureComponentsToNBT(chunkX, chunkZ), chunkX, chunkZ);
|
| 262 | this.structureData.markDirty();
|
| 263 | }
|
| 264 |
|
| 265 | protected abstract boolean canSpawnStructureAtCoords(int chunkX, int chunkZ);
|
| 266 |
|
| 267 | protected abstract StructureStart getStructureStart(int chunkX, int chunkZ);
|
| 268 | }
|
| 269 |
|