    001package net.minecraft.world.gen.structure;
    002
    003import java.util.ArrayList;
    004import java.util.Iterator;
    005import java.util.List;
    006import java.util.Random;
    007
    008import cpw.mods.fml.common.registry.VillagerRegistry;
    009import net.minecraft.util.MathHelper;
    010
    011public class StructureVillagePieces
    012{
    013    public static ArrayList getStructureVillageWeightedPieceList(Random par0Random, int par1)
    014    {
    015        ArrayList var2 = new ArrayList();
    016        var2.add(new StructureVillagePieceWeight(ComponentVillageHouse4_Garden.class, 4, MathHelper.getRandomIntegerInRange(par0Random, 2 + par1, 4 + par1 * 2)));
    017        var2.add(new StructureVillagePieceWeight(ComponentVillageChurch.class, 20, MathHelper.getRandomIntegerInRange(par0Random, 0 + par1, 1 + par1)));
    018        var2.add(new StructureVillagePieceWeight(ComponentVillageHouse1.class, 20, MathHelper.getRandomIntegerInRange(par0Random, 0 + par1, 2 + par1)));
    019        var2.add(new StructureVillagePieceWeight(ComponentVillageWoodHut.class, 3, MathHelper.getRandomIntegerInRange(par0Random, 2 + par1, 5 + par1 * 3)));
    020        var2.add(new StructureVillagePieceWeight(ComponentVillageHall.class, 15, MathHelper.getRandomIntegerInRange(par0Random, 0 + par1, 2 + par1)));
    021        var2.add(new StructureVillagePieceWeight(ComponentVillageField.class, 3, MathHelper.getRandomIntegerInRange(par0Random, 1 + par1, 4 + par1)));
    022        var2.add(new StructureVillagePieceWeight(ComponentVillageField2.class, 3, MathHelper.getRandomIntegerInRange(par0Random, 2 + par1, 4 + par1 * 2)));
    023        var2.add(new StructureVillagePieceWeight(ComponentVillageHouse2.class, 15, MathHelper.getRandomIntegerInRange(par0Random, 0, 1 + par1)));
    024        var2.add(new StructureVillagePieceWeight(ComponentVillageHouse3.class, 8, MathHelper.getRandomIntegerInRange(par0Random, 0 + par1, 3 + par1 * 2)));
    025        VillagerRegistry.addExtraVillageComponents(var2, par0Random, par1);
    026
    027        Iterator var3 = var2.iterator();
    028
    029        while (var3.hasNext())
    030        {
    031            if (((StructureVillagePieceWeight)var3.next()).villagePiecesLimit == 0)
    032            {
    033                var3.remove();
    034            }
    035        }
    036
    037        return var2;
    038    }
    039
    040    private static int func_75079_a(List par0List)
    041    {
    042        boolean var1 = false;
    043        int var2 = 0;
    044        StructureVillagePieceWeight var4;
    045
    046        for (Iterator var3 = par0List.iterator(); var3.hasNext(); var2 += var4.villagePieceWeight)
    047        {
    048            var4 = (StructureVillagePieceWeight)var3.next();
    049
    050            if (var4.villagePiecesLimit > 0 && var4.villagePiecesSpawned < var4.villagePiecesLimit)
    051            {
    052                var1 = true;
    053            }
    054        }
    055
    056        return var1 ? var2 : -1;
    057    }
    058
    059    private static ComponentVillage func_75083_a(ComponentVillageStartPiece par0ComponentVillageStartPiece, StructureVillagePieceWeight par1StructureVillagePieceWeight, List par2List, Random par3Random, int par4, int par5, int par6, int par7, int par8)
    060    {
    061        Class var9 = par1StructureVillagePieceWeight.villagePieceClass;
    062        Object var10 = null;
    063
    064        if (var9 == ComponentVillageHouse4_Garden.class)
    065        {
    066            var10 = ComponentVillageHouse4_Garden.func_74912_a(par0ComponentVillageStartPiece, par2List, par3Random, par4, par5, par6, par7, par8);
    067        }
    068        else if (var9 == ComponentVillageChurch.class)
    069        {
    070            var10 = ComponentVillageChurch.func_74919_a(par0ComponentVillageStartPiece, par2List, par3Random, par4, par5, par6, par7, par8);
    071        }
    072        else if (var9 == ComponentVillageHouse1.class)
    073        {
    074            var10 = ComponentVillageHouse1.func_74898_a(par0ComponentVillageStartPiece, par2List, par3Random, par4, par5, par6, par7, par8);
    075        }
    076        else if (var9 == ComponentVillageWoodHut.class)
    077        {
    078            var10 = ComponentVillageWoodHut.func_74908_a(par0ComponentVillageStartPiece, par2List, par3Random, par4, par5, par6, par7, par8);
    079        }
    080        else if (var9 == ComponentVillageHall.class)
    081        {
    082            var10 = ComponentVillageHall.func_74906_a(par0ComponentVillageStartPiece, par2List, par3Random, par4, par5, par6, par7, par8);
    083        }
    084        else if (var9 == ComponentVillageField.class)
    085        {
    086            var10 = ComponentVillageField.func_74900_a(par0ComponentVillageStartPiece, par2List, par3Random, par4, par5, par6, par7, par8);
    087        }
    088        else if (var9 == ComponentVillageField2.class)
    089        {
    090            var10 = ComponentVillageField2.func_74902_a(par0ComponentVillageStartPiece, par2List, par3Random, par4, par5, par6, par7, par8);
    091        }
    092        else if (var9 == ComponentVillageHouse2.class)
    093        {
    094            var10 = ComponentVillageHouse2.func_74915_a(par0ComponentVillageStartPiece, par2List, par3Random, par4, par5, par6, par7, par8);
    095        }
    096        else if (var9 == ComponentVillageHouse3.class)
    097        {
    098            var10 = ComponentVillageHouse3.func_74921_a(par0ComponentVillageStartPiece, par2List, par3Random, par4, par5, par6, par7, par8);
    099        }
    100        else
    101        {
    102            var10 = VillagerRegistry.getVillageComponent(par1StructureVillagePieceWeight, par0ComponentVillageStartPiece , par2List, par3Random, par4, par5, par6, par7, par8);
    103        }
    104
    105        return (ComponentVillage)var10;
    106    }
    107
    108    /**
    109     * attempts to find a next Village Component to be spawned
    110     */
    111    private static ComponentVillage getNextVillageComponent(ComponentVillageStartPiece par0ComponentVillageStartPiece, List par1List, Random par2Random, int par3, int par4, int par5, int par6, int par7)
    112    {
    113        int var8 = func_75079_a(par0ComponentVillageStartPiece.structureVillageWeightedPieceList);
    114
    115        if (var8 <= 0)
    116        {
    117            return null;
    118        }
    119        else
    120        {
    121            int var9 = 0;
    122
    123            while (var9 < 5)
    124            {
    125                ++var9;
    126                int var10 = par2Random.nextInt(var8);
    127                Iterator var11 = par0ComponentVillageStartPiece.structureVillageWeightedPieceList.iterator();
    128
    129                while (var11.hasNext())
    130                {
    131                    StructureVillagePieceWeight var12 = (StructureVillagePieceWeight)var11.next();
    132                    var10 -= var12.villagePieceWeight;
    133
    134                    if (var10 < 0)
    135                    {
    136                        if (!var12.canSpawnMoreVillagePiecesOfType(par7) || var12 == par0ComponentVillageStartPiece.structVillagePieceWeight && par0ComponentVillageStartPiece.structureVillageWeightedPieceList.size() > 1)
    137                        {
    138                            break;
    139                        }
    140
    141                        ComponentVillage var13 = func_75083_a(par0ComponentVillageStartPiece, var12, par1List, par2Random, par3, par4, par5, par6, par7);
    142
    143                        if (var13 != null)
    144                        {
    145                            ++var12.villagePiecesSpawned;
    146                            par0ComponentVillageStartPiece.structVillagePieceWeight = var12;
    147
    148                            if (!var12.canSpawnMoreVillagePieces())
    149                            {
    150                                par0ComponentVillageStartPiece.structureVillageWeightedPieceList.remove(var12);
    151                            }
    152
    153                            return var13;
    154                        }
    155                    }
    156                }
    157            }
    158
    159            StructureBoundingBox var14 = ComponentVillageTorch.func_74904_a(par0ComponentVillageStartPiece, par1List, par2Random, par3, par4, par5, par6);
    160
    161            if (var14 != null)
    162            {
    163                return new ComponentVillageTorch(par0ComponentVillageStartPiece, par7, par2Random, var14, par6);
    164            }
    165            else
    166            {
    167                return null;
    168            }
    169        }
    170    }
    171
    172    /**
    173     * attempts to find a next Structure Component to be spawned, private Village function
    174     */
    175    private static StructureComponent getNextVillageStructureComponent(ComponentVillageStartPiece par0ComponentVillageStartPiece, List par1List, Random par2Random, int par3, int par4, int par5, int par6, int par7)
    176    {
    177        if (par7 > 50)
    178        {
    179            return null;
    180        }
    181        else if (Math.abs(par3 - par0ComponentVillageStartPiece.getBoundingBox().minX) <= 112 && Math.abs(par5 - par0ComponentVillageStartPiece.getBoundingBox().minZ) <= 112)
    182        {
    183            ComponentVillage var8 = getNextVillageComponent(par0ComponentVillageStartPiece, par1List, par2Random, par3, par4, par5, par6, par7 + 1);
    184
    185            if (var8 != null)
    186            {
    187                int var9 = (var8.boundingBox.minX + var8.boundingBox.maxX) / 2;
    188                int var10 = (var8.boundingBox.minZ + var8.boundingBox.maxZ) / 2;
    189                int var11 = var8.boundingBox.maxX - var8.boundingBox.minX;
    190                int var12 = var8.boundingBox.maxZ - var8.boundingBox.minZ;
    191                int var13 = var11 > var12 ? var11 : var12;
    192
    193                if (par0ComponentVillageStartPiece.getWorldChunkManager().areBiomesViable(var9, var10, var13 / 2 + 4, MapGenVillage.villageSpawnBiomes))
    194                {
    195                    par1List.add(var8);
    196                    par0ComponentVillageStartPiece.field_74932_i.add(var8);
    197                    return var8;
    198                }
    199            }
    200
    201            return null;
    202        }
    203        else
    204        {
    205            return null;
    206        }
    207    }
    208
    209    private static StructureComponent getNextComponentVillagePath(ComponentVillageStartPiece par0ComponentVillageStartPiece, List par1List, Random par2Random, int par3, int par4, int par5, int par6, int par7)
    210    {
    211        if (par7 > 3 + par0ComponentVillageStartPiece.terrainType)
    212        {
    213            return null;
    214        }
    215        else if (Math.abs(par3 - par0ComponentVillageStartPiece.getBoundingBox().minX) <= 112 && Math.abs(par5 - par0ComponentVillageStartPiece.getBoundingBox().minZ) <= 112)
    216        {
    217            StructureBoundingBox var8 = ComponentVillagePathGen.func_74933_a(par0ComponentVillageStartPiece, par1List, par2Random, par3, par4, par5, par6);
    218
    219            if (var8 != null && var8.minY > 10)
    220            {
    221                ComponentVillagePathGen var9 = new ComponentVillagePathGen(par0ComponentVillageStartPiece, par7, par2Random, var8, par6);
    222                int var10 = (var9.boundingBox.minX + var9.boundingBox.maxX) / 2;
    223                int var11 = (var9.boundingBox.minZ + var9.boundingBox.maxZ) / 2;
    224                int var12 = var9.boundingBox.maxX - var9.boundingBox.minX;
    225                int var13 = var9.boundingBox.maxZ - var9.boundingBox.minZ;
    226                int var14 = var12 > var13 ? var12 : var13;
    227
    228                if (par0ComponentVillageStartPiece.getWorldChunkManager().areBiomesViable(var10, var11, var14 / 2 + 4, MapGenVillage.villageSpawnBiomes))
    229                {
    230                    par1List.add(var9);
    231                    par0ComponentVillageStartPiece.field_74930_j.add(var9);
    232                    return var9;
    233                }
    234            }
    235
    236            return null;
    237        }
    238        else
    239        {
    240            return null;
    241        }
    242    }
    243
    244    /**
    245     * attempts to find a next Structure Component to be spawned
    246     */
    247    static StructureComponent getNextStructureComponent(ComponentVillageStartPiece par0ComponentVillageStartPiece, List par1List, Random par2Random, int par3, int par4, int par5, int par6, int par7)
    248    {
    249        return getNextVillageStructureComponent(par0ComponentVillageStartPiece, par1List, par2Random, par3, par4, par5, par6, par7);
    250    }
    251
    252    static StructureComponent getNextStructureComponentVillagePath(ComponentVillageStartPiece par0ComponentVillageStartPiece, List par1List, Random par2Random, int par3, int par4, int par5, int par6, int par7)
    253    {
    254        return getNextComponentVillagePath(par0ComponentVillageStartPiece, par1List, par2Random, par3, par4, par5, par6, par7);
    255    }
    256}