aboutsummaryrefslogtreecommitdiff
path: root/src/Java/gtPlusPlus/australia/gen/map/MapGenExtendedVillage.java
blob: 8fc69402422680048dc4757cb2f2a2b36bddc5c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package gtPlusPlus.australia.gen.map;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;

import gtPlusPlus.api.objects.Logger;
import gtPlusPlus.australia.GTplusplus_Australia;

import java.util.Map.Entry;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
import net.minecraft.world.biome.BiomeGenBase;
import net.minecraft.world.gen.structure.MapGenStructure;
import net.minecraft.world.gen.structure.StructureBoundingBox;
import net.minecraft.world.gen.structure.StructureComponent;
import net.minecraft.world.gen.structure.StructureStart;
import net.minecraft.world.gen.structure.StructureVillagePieces;

public class MapGenExtendedVillage extends MapGenStructure
{
    /** A list of all the biomes villages can spawn in. */
    public static List<BiomeGenBase> villageSpawnBiomes = Arrays.asList(new BiomeGenBase[] {GTplusplus_Australia.Australian_Desert_Biome_3, GTplusplus_Australia.Australian_Plains_Biome, GTplusplus_Australia.Australian_Forest_Biome, GTplusplus_Australia.Australian_Outback_Biome});
    /** World terrain type, 0 for normal, 1 for flat map */
    private int terrainType;
    private int field_82665_g;
    private int field_82666_h;

    public MapGenExtendedVillage(){
        this.field_82665_g = 8;
        this.field_82666_h = 4;
    }

    public MapGenExtendedVillage(Map<?, ?> p_i2093_1_){
        this();
        Iterator<?> iterator = p_i2093_1_.entrySet().iterator();

        Logger.INFO("Created Extended Village Object.");
        
        while (iterator.hasNext())
        {
            Entry<?, ?> entry = (Entry<?, ?>)iterator.next();

            if (((String)entry.getKey()).equals("size"))
            {
                this.terrainType = MathHelper.parseIntWithDefaultAndMax((String)entry.getValue(), this.terrainType, 0);
            }
            else if (((String)entry.getKey()).equals("distance"))
            {
                this.field_82665_g = MathHelper.parseIntWithDefaultAndMax((String)entry.getValue(), this.field_82665_g, this.field_82666_h + 1);
            }
        }
    }

    public String func_143025_a()
    {
        return "ExtendedVillage";
    }

    protected boolean canSpawnStructureAtCoords(int p_75047_1_, int p_75047_2_)
    {
    	
    	
        int k = p_75047_1_;
        int l = p_75047_2_;

        if (p_75047_1_ < 0)
        {
            p_75047_1_ -= this.field_82665_g - 1;
        }

        if (p_75047_2_ < 0)
        {
            p_75047_2_ -= this.field_82665_g - 1;
        }

        int i1 = p_75047_1_ / this.field_82665_g;
        int j1 = p_75047_2_ / this.field_82665_g;
        Random random = this.worldObj.setRandomSeed(i1, j1, 10387312);
        i1 *= this.field_82665_g;
        j1 *= this.field_82665_g;
        i1 += random.nextInt(this.field_82665_g - this.field_82666_h);
        j1 += random.nextInt(this.field_82665_g - this.field_82666_h);

        if (k == i1 && l == j1)
        {
            boolean flag = this.worldObj.getWorldChunkManager().areBiomesViable(k * 16 + 8, l * 16 + 8, 0, villageSpawnBiomes);

            if (flag)
            {
            	Logger.INFO("Found viable biome(s) for custom village");
                return true;
            }
        }

        return false;
    }

    protected StructureStart getStructureStart(int p_75049_1_, int p_75049_2_)
    {
        return new MapGenExtendedVillage.Start(this.worldObj, this.rand, p_75049_1_, p_75049_2_, this.terrainType);
    }

    public static class Start extends StructureStart
        {
            /** well ... thats what it does */
            private boolean hasMoreThanTwoComponents;

            public Start() {
            }

            public Start(World p_i2092_1_, Random p_i2092_2_, int p_i2092_3_, int p_i2092_4_, int p_i2092_5_)
            {
                super(p_i2092_3_, p_i2092_4_);
                Logger.INFO("Trying to Start Village Builder.");
                List<?> list = StructureVillagePieces.getStructureVillageWeightedPieceList(p_i2092_2_, p_i2092_5_);
                StructureVillagePieces.Start start = new StructureVillagePieces.Start(p_i2092_1_.getWorldChunkManager(), 0, p_i2092_2_, (p_i2092_3_ << 4) + 2, (p_i2092_4_ << 4) + 2, list, p_i2092_5_);
                this.components.add(start);
                start.buildComponent(start, this.components, p_i2092_2_);
                List<?> list1 = start.field_74930_j;
                List<?> list2 = start.field_74932_i;
                int l;
                Logger.INFO("List1: "+list1.size()+" | List2: "+list2.size());

                while (!list1.isEmpty() || !list2.isEmpty())
                {
                    Logger.INFO("Iterating non empty list.");
                    StructureComponent structurecomponent;

                    if (list1.isEmpty())
                    {
                        l = p_i2092_2_.nextInt(list2.size());
                        structurecomponent = (StructureComponent)list2.remove(l);
                        structurecomponent.buildComponent(start, this.components, p_i2092_2_);
                    }
                    else
                    {
                        l = p_i2092_2_.nextInt(list1.size());
                        structurecomponent = (StructureComponent)list1.remove(l);
                        structurecomponent.buildComponent(start, this.components, p_i2092_2_);
                    }
                }
                Logger.INFO("Finished iterating lists, updating bounding box for structure.");

                this.updateBoundingBox();
                l = 0;
                Iterator iterator = this.components.iterator();

                while (iterator.hasNext())
                {
                    Logger.INFO("Iterating Components.");
                    StructureComponent structurecomponent1 = (StructureComponent)iterator.next();

                    if (!(structurecomponent1 instanceof StructureVillagePieces.Road))
                    {
                        ++l;
                    }
                }

            	Logger.INFO("hasMoreThanTwoComponents? "+(l > 2));
                this.hasMoreThanTwoComponents = l > 2;
            }

            /**
             * currently only defined for Villages, returns true if Village has more than 2 non-road components
             */
            public boolean isSizeableStructure()
            {
                //return this.hasMoreThanTwoComponents;
            	return true;
            }

            public void func_143022_a(NBTTagCompound p_143022_1_)
            {
                super.func_143022_a(p_143022_1_);
                p_143022_1_.setBoolean("Valid", this.hasMoreThanTwoComponents);
            }

            public void func_143017_b(NBTTagCompound p_143017_1_)
            {
                super.func_143017_b(p_143017_1_);
                this.hasMoreThanTwoComponents = p_143017_1_.getBoolean("Valid");
            }
        }
    
    /**
     * Generates structures in specified chunk next to existing structures. Does *not* generate StructureStarts.
     */
    @Override
    public boolean generateStructuresInChunk(World p_75051_1_, Random p_75051_2_, int p_75051_3_, int p_75051_4_){
    	//Logger.INFO("Try generate Structs in chunk.");
        this.callPrivateFunction1(p_75051_1_);
        int k = (p_75051_3_ << 4) + 8;
        int l = (p_75051_4_ << 4) + 8;
        boolean flag = false;
        Iterator iterator = this.structureMap.values().iterator();
        
        //Logger.INFO("Iteration Size: "+this.structureMap.values().size());
        while (iterator.hasNext())
        {
        	//Logger.INFO("Iterating.");
            StructureStart structurestart = (StructureStart)iterator.next();

            if (structurestart.isSizeableStructure() && (structurestart.getBoundingBox().intersectsWith(k, l, k + 15, l + 15) || structurestart.getBoundingBox().intersectsWith(k, l, k - 15, l - 15)))
            {
            	Logger.INFO("Iterating. 2");
                structurestart.generateStructure(p_75051_1_, p_75051_2_, new StructureBoundingBox(k, l, k + 15, l + 15));
                flag = true;
                this.callPrivateFunction2(structurestart.func_143019_e(), structurestart.func_143018_f(), structurestart);
            }
           /* else {
            	Logger.INFO("Iterating. 3");
            	Logger.INFO("structurestart.isSizeableStructure()? "+structurestart.isSizeableStructure());
            	Logger.INFO("structurestart.getBoundingBox().intersectsWith(k, l, k + 15, l + 15)? "+(structurestart.getBoundingBox().intersectsWith(k, l, k + 15, l + 15) || structurestart.getBoundingBox().intersectsWith(k, l, k - 15, l - 15)));    
            	Logger.INFO("K: "+k+" | L: "+l);
            	Logger.INFO("structure bounding box info: x-:"+structurestart.getBoundingBox().minX+"   y-:"+structurestart.getBoundingBox().minY+"   x+:"+structurestart.getBoundingBox().maxX+"   y+:"+structurestart.getBoundingBox().maxY);
            }*/
        }

        return flag;
    }
    
    Method mMethod1;
    Method mMethod2;
    private boolean callPrivateFunction1(World aWorld) {
    	if (mMethod1 == null) {
    		try {
				mMethod1 = MapGenStructure.class.getDeclaredMethod("func_143027_a", World.class);
			} catch (NoSuchMethodException | SecurityException e) {
				return false;
			}
    	}
    	if (mMethod1 != null) {
    		try {
    			//Logger.INFO("Invoking func_143027_a");
				mMethod1.invoke(this, aWorld);
				return true;
			} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {}
    	}
    	return false;
    }
    
    private boolean callPrivateFunction2(int aInt1, int aInt2, StructureStart aStruct) {
    	if (mMethod2 == null) {
    		try {
    			mMethod2 = MapGenStructure.class.getDeclaredMethod("func_143026_a", int.class, int.class, StructureStart.class);
			} catch (NoSuchMethodException | SecurityException e) {
				return false;
			}
    	}
    	if (mMethod2 != null) {
    		try {
    			Logger.INFO("Invoking func_143026_a");
    			mMethod2.invoke(this, aInt1, aInt2, aStruct);
				return true;
			} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {}
    	}
    	return false;
    }
}