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 arraylist = new ArrayList();
016        arraylist.add(new StructureVillagePieceWeight(ComponentVillageHouse4_Garden.class, 4, MathHelper.getRandomIntegerInRange(par0Random, 2 + par1, 4 + par1 * 2)));
017        arraylist.add(new StructureVillagePieceWeight(ComponentVillageChurch.class, 20, MathHelper.getRandomIntegerInRange(par0Random, 0 + par1, 1 + par1)));
018        arraylist.add(new StructureVillagePieceWeight(ComponentVillageHouse1.class, 20, MathHelper.getRandomIntegerInRange(par0Random, 0 + par1, 2 + par1)));
019        arraylist.add(new StructureVillagePieceWeight(ComponentVillageWoodHut.class, 3, MathHelper.getRandomIntegerInRange(par0Random, 2 + par1, 5 + par1 * 3)));
020        arraylist.add(new StructureVillagePieceWeight(ComponentVillageHall.class, 15, MathHelper.getRandomIntegerInRange(par0Random, 0 + par1, 2 + par1)));
021        arraylist.add(new StructureVillagePieceWeight(ComponentVillageField.class, 3, MathHelper.getRandomIntegerInRange(par0Random, 1 + par1, 4 + par1)));
022        arraylist.add(new StructureVillagePieceWeight(ComponentVillageField2.class, 3, MathHelper.getRandomIntegerInRange(par0Random, 2 + par1, 4 + par1 * 2)));
023        arraylist.add(new StructureVillagePieceWeight(ComponentVillageHouse2.class, 15, MathHelper.getRandomIntegerInRange(par0Random, 0, 1 + par1)));
024        arraylist.add(new StructureVillagePieceWeight(ComponentVillageHouse3.class, 8, MathHelper.getRandomIntegerInRange(par0Random, 0 + par1, 3 + par1 * 2)));
025        VillagerRegistry.addExtraVillageComponents(arraylist, par0Random, par1);
026
027        Iterator iterator = arraylist.iterator();
028
029        while (iterator.hasNext())
030        {
031            if (((StructureVillagePieceWeight)iterator.next()).villagePiecesLimit == 0)
032            {
033                iterator.remove();
034            }
035        }
036
037        return arraylist;
038    }
039
040    private static int func_75079_a(List par0List)
041    {
042        boolean flag = false;
043        int i = 0;
044        StructureVillagePieceWeight structurevillagepieceweight;
045
046        for (Iterator iterator = par0List.iterator(); iterator.hasNext(); i += structurevillagepieceweight.villagePieceWeight)
047        {
048            structurevillagepieceweight = (StructureVillagePieceWeight)iterator.next();
049
050            if (structurevillagepieceweight.villagePiecesLimit > 0 && structurevillagepieceweight.villagePiecesSpawned < structurevillagepieceweight.villagePiecesLimit)
051            {
052                flag = true;
053            }
054        }
055
056        return flag ? i : -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 oclass = par1StructureVillagePieceWeight.villagePieceClass;
062        Object object = null;
063
064        if (oclass == ComponentVillageHouse4_Garden.class)
065        {
066            object = ComponentVillageHouse4_Garden.func_74912_a(par0ComponentVillageStartPiece, par2List, par3Random, par4, par5, par6, par7, par8);
067        }
068        else if (oclass == ComponentVillageChurch.class)
069        {
070            object = ComponentVillageChurch.func_74919_a(par0ComponentVillageStartPiece, par2List, par3Random, par4, par5, par6, par7, par8);
071        }
072        else if (oclass == ComponentVillageHouse1.class)
073        {
074            object = ComponentVillageHouse1.func_74898_a(par0ComponentVillageStartPiece, par2List, par3Random, par4, par5, par6, par7, par8);
075        }
076        else if (oclass == ComponentVillageWoodHut.class)
077        {
078            object = ComponentVillageWoodHut.func_74908_a(par0ComponentVillageStartPiece, par2List, par3Random, par4, par5, par6, par7, par8);
079        }
080        else if (oclass == ComponentVillageHall.class)
081        {
082            object = ComponentVillageHall.func_74906_a(par0ComponentVillageStartPiece, par2List, par3Random, par4, par5, par6, par7, par8);
083        }
084        else if (oclass == ComponentVillageField.class)
085        {
086            object = ComponentVillageField.func_74900_a(par0ComponentVillageStartPiece, par2List, par3Random, par4, par5, par6, par7, par8);
087        }
088        else if (oclass == ComponentVillageField2.class)
089        {
090            object = ComponentVillageField2.func_74902_a(par0ComponentVillageStartPiece, par2List, par3Random, par4, par5, par6, par7, par8);
091        }
092        else if (oclass == ComponentVillageHouse2.class)
093        {
094            object = ComponentVillageHouse2.func_74915_a(par0ComponentVillageStartPiece, par2List, par3Random, par4, par5, par6, par7, par8);
095        }
096        else if (oclass == ComponentVillageHouse3.class)
097        {
098            object = ComponentVillageHouse3.func_74921_a(par0ComponentVillageStartPiece, par2List, par3Random, par4, par5, par6, par7, par8);
099        }
100        else
101        {
102            object = VillagerRegistry.getVillageComponent(par1StructureVillagePieceWeight, par0ComponentVillageStartPiece , par2List, par3Random, par4, par5, par6, par7, par8);
103        }
104
105        return (ComponentVillage)object;
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 j1 = func_75079_a(par0ComponentVillageStartPiece.structureVillageWeightedPieceList);
114
115        if (j1 <= 0)
116        {
117            return null;
118        }
119        else
120        {
121            int k1 = 0;
122
123            while (k1 < 5)
124            {
125                ++k1;
126                int l1 = par2Random.nextInt(j1);
127                Iterator iterator = par0ComponentVillageStartPiece.structureVillageWeightedPieceList.iterator();
128
129                while (iterator.hasNext())
130                {
131                    StructureVillagePieceWeight structurevillagepieceweight = (StructureVillagePieceWeight)iterator.next();
132                    l1 -= structurevillagepieceweight.villagePieceWeight;
133
134                    if (l1 < 0)
135                    {
136                        if (!structurevillagepieceweight.canSpawnMoreVillagePiecesOfType(par7) || structurevillagepieceweight == par0ComponentVillageStartPiece.structVillagePieceWeight && par0ComponentVillageStartPiece.structureVillageWeightedPieceList.size() > 1)
137                        {
138                            break;
139                        }
140
141                        ComponentVillage componentvillage = func_75083_a(par0ComponentVillageStartPiece, structurevillagepieceweight, par1List, par2Random, par3, par4, par5, par6, par7);
142
143                        if (componentvillage != null)
144                        {
145                            ++structurevillagepieceweight.villagePiecesSpawned;
146                            par0ComponentVillageStartPiece.structVillagePieceWeight = structurevillagepieceweight;
147
148                            if (!structurevillagepieceweight.canSpawnMoreVillagePieces())
149                            {
150                                par0ComponentVillageStartPiece.structureVillageWeightedPieceList.remove(structurevillagepieceweight);
151                            }
152
153                            return componentvillage;
154                        }
155                    }
156                }
157            }
158
159            StructureBoundingBox structureboundingbox = ComponentVillageTorch.func_74904_a(par0ComponentVillageStartPiece, par1List, par2Random, par3, par4, par5, par6);
160
161            if (structureboundingbox != null)
162            {
163                return new ComponentVillageTorch(par0ComponentVillageStartPiece, par7, par2Random, structureboundingbox, 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 componentvillage = getNextVillageComponent(par0ComponentVillageStartPiece, par1List, par2Random, par3, par4, par5, par6, par7 + 1);
184
185            if (componentvillage != null)
186            {
187                int j1 = (componentvillage.boundingBox.minX + componentvillage.boundingBox.maxX) / 2;
188                int k1 = (componentvillage.boundingBox.minZ + componentvillage.boundingBox.maxZ) / 2;
189                int l1 = componentvillage.boundingBox.maxX - componentvillage.boundingBox.minX;
190                int i2 = componentvillage.boundingBox.maxZ - componentvillage.boundingBox.minZ;
191                int j2 = l1 > i2 ? l1 : i2;
192
193                if (par0ComponentVillageStartPiece.getWorldChunkManager().areBiomesViable(j1, k1, j2 / 2 + 4, MapGenVillage.villageSpawnBiomes))
194                {
195                    par1List.add(componentvillage);
196                    par0ComponentVillageStartPiece.field_74932_i.add(componentvillage);
197                    return componentvillage;
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 structureboundingbox = ComponentVillagePathGen.func_74933_a(par0ComponentVillageStartPiece, par1List, par2Random, par3, par4, par5, par6);
218
219            if (structureboundingbox != null && structureboundingbox.minY > 10)
220            {
221                ComponentVillagePathGen componentvillagepathgen = new ComponentVillagePathGen(par0ComponentVillageStartPiece, par7, par2Random, structureboundingbox, par6);
222                int j1 = (componentvillagepathgen.boundingBox.minX + componentvillagepathgen.boundingBox.maxX) / 2;
223                int k1 = (componentvillagepathgen.boundingBox.minZ + componentvillagepathgen.boundingBox.maxZ) / 2;
224                int l1 = componentvillagepathgen.boundingBox.maxX - componentvillagepathgen.boundingBox.minX;
225                int i2 = componentvillagepathgen.boundingBox.maxZ - componentvillagepathgen.boundingBox.minZ;
226                int j2 = l1 > i2 ? l1 : i2;
227
228                if (par0ComponentVillageStartPiece.getWorldChunkManager().areBiomesViable(j1, k1, j2 / 2 + 4, MapGenVillage.villageSpawnBiomes))
229                {
230                    par1List.add(componentvillagepathgen);
231                    par0ComponentVillageStartPiece.field_74930_j.add(componentvillagepathgen);
232                    return componentvillagepathgen;
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}