    001    package net.minecraft.src;
    002
    003    import java.util.HashMap;
    004    import java.util.Iterator;
    005    import java.util.List;
    006    import java.util.Map;
    007    import java.util.Random;
    008
    009    public abstract class MapGenStructure extends MapGenBase
    010    {
    011        /**
    012         * Used to store a list of all structures that have been recursively generated. Used so that during recursive
    013         * generation, the structure generator can avoid generating structures that intersect ones that have already been
    014         * placed.
    015         */
    016        protected Map structureMap = new HashMap();
    017
    018        /**
    019         * Recursively called by generate() (generate) and optionally by itself.
    020         */
    021        protected void recursiveGenerate(World par1World, int par2, int par3, int par4, int par5, byte[] par6ArrayOfByte)
    022        {
    023            if (!this.structureMap.containsKey(Long.valueOf(ChunkCoordIntPair.chunkXZ2Int(par2, par3))))
    024            {
    025                this.rand.nextInt();
    026
    027                try
    028                {
    029                    if (this.canSpawnStructureAtCoords(par2, par3))
    030                    {
    031                        StructureStart var7 = this.getStructureStart(par2, par3);
    032                        this.structureMap.put(Long.valueOf(ChunkCoordIntPair.chunkXZ2Int(par2, par3)), var7);
    033                    }
    034                }
    035                catch (Throwable var10)
    036                {
    037                    CrashReport var8 = CrashReport.func_85055_a(var10, "Exception preparing structure feature");
    038                    CrashReportCategory var9 = var8.func_85058_a("Feature being prepared");
    039                    var9.addCrashSectionCallable("Is feature chunk", new CallableIsFeatureChunk(this, par2, par3));
    040                    var9.addCrashSection("Chunk location", String.format("%d,%d", new Object[] {Integer.valueOf(par2), Integer.valueOf(par3)}));
    041                    var9.addCrashSectionCallable("Chunk pos hash", new CallableChunkPosHash(this, par2, par3));
    042                    var9.addCrashSectionCallable("Structure type", new CallableStructureType(this));
    043                    throw new ReportedException(var8);
    044                }
    045            }
    046        }
    047
    048        /**
    049         * Generates structures in specified chunk next to existing structures. Does *not* generate StructureStarts.
    050         */
    051        public boolean generateStructuresInChunk(World par1World, Random par2Random, int par3, int par4)
    052        {
    053            int var5 = (par3 << 4) + 8;
    054            int var6 = (par4 << 4) + 8;
    055            boolean var7 = false;
    056            Iterator var8 = this.structureMap.values().iterator();
    057
    058            while (var8.hasNext())
    059            {
    060                StructureStart var9 = (StructureStart)var8.next();
    061
    062                if (var9.isSizeableStructure() && var9.getBoundingBox().intersectsWith(var5, var6, var5 + 15, var6 + 15))
    063                {
    064                    var9.generateStructure(par1World, par2Random, new StructureBoundingBox(var5, var6, var5 + 15, var6 + 15));
    065                    var7 = true;
    066                }
    067            }
    068
    069            return var7;
    070        }
    071
    072        /**
    073         * Returns true if the structure generator has generated a structure located at the given position tuple.
    074         */
    075        public boolean hasStructureAt(int par1, int par2, int par3)
    076        {
    077            Iterator var4 = this.structureMap.values().iterator();
    078
    079            while (var4.hasNext())
    080            {
    081                StructureStart var5 = (StructureStart)var4.next();
    082
    083                if (var5.isSizeableStructure() && var5.getBoundingBox().intersectsWith(par1, par3, par1, par3))
    084                {
    085                    Iterator var6 = var5.getComponents().iterator();
    086
    087                    while (var6.hasNext())
    088                    {
    089                        StructureComponent var7 = (StructureComponent)var6.next();
    090
    091                        if (var7.getBoundingBox().isVecInside(par1, par2, par3))
    092                        {
    093                            return true;
    094                        }
    095                    }
    096                }
    097            }
    098
    099            return false;
    100        }
    101
    102        public ChunkPosition getNearestInstance(World par1World, int par2, int par3, int par4)
    103        {
    104            this.worldObj = par1World;
    105            this.rand.setSeed(par1World.getSeed());
    106            long var5 = this.rand.nextLong();
    107            long var7 = this.rand.nextLong();
    108            long var9 = (long)(par2 >> 4) * var5;
    109            long var11 = (long)(par4 >> 4) * var7;
    110            this.rand.setSeed(var9 ^ var11 ^ par1World.getSeed());
    111            this.recursiveGenerate(par1World, par2 >> 4, par4 >> 4, 0, 0, (byte[])null);
    112            double var13 = Double.MAX_VALUE;
    113            ChunkPosition var15 = null;
    114            Iterator var16 = this.structureMap.values().iterator();
    115            ChunkPosition var19;
    116            int var21;
    117            int var20;
    118            double var23;
    119            int var22;
    120
    121            while (var16.hasNext())
    122            {
    123                StructureStart var17 = (StructureStart)var16.next();
    124
    125                if (var17.isSizeableStructure())
    126                {
    127                    StructureComponent var18 = (StructureComponent)var17.getComponents().get(0);
    128                    var19 = var18.getCenter();
    129                    var20 = var19.x - par2;
    130                    var21 = var19.y - par3;
    131                    var22 = var19.z - par4;
    132                    var23 = (double)(var20 + var20 * var21 * var21 + var22 * var22);
    133
    134                    if (var23 < var13)
    135                    {
    136                        var13 = var23;
    137                        var15 = var19;
    138                    }
    139                }
    140            }
    141
    142            if (var15 != null)
    143            {
    144                return var15;
    145            }
    146            else
    147            {
    148                List var25 = this.getCoordList();
    149
    150                if (var25 != null)
    151                {
    152                    ChunkPosition var26 = null;
    153                    Iterator var27 = var25.iterator();
    154
    155                    while (var27.hasNext())
    156                    {
    157                        var19 = (ChunkPosition)var27.next();
    158                        var20 = var19.x - par2;
    159                        var21 = var19.y - par3;
    160                        var22 = var19.z - par4;
    161                        var23 = (double)(var20 + var20 * var21 * var21 + var22 * var22);
    162
    163                        if (var23 < var13)
    164                        {
    165                            var13 = var23;
    166                            var26 = var19;
    167                        }
    168                    }
    169
    170                    return var26;
    171                }
    172                else
    173                {
    174                    return null;
    175                }
    176            }
    177        }
    178
    179        /**
    180         * Returns a list of other locations at which the structure generation has been run, or null if not relevant to this
    181         * structure generator.
    182         */
    183        protected List getCoordList()
    184        {
    185            return null;
    186        }
    187
    188        protected abstract boolean canSpawnStructureAtCoords(int var1, int var2);
    189
    190        protected abstract StructureStart getStructureStart(int var1, int var2);
    191    }