-
Notifications
You must be signed in to change notification settings - Fork 497
/
OilPopulate.java
222 lines (181 loc) · 7.34 KB
/
OilPopulate.java
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
/**
* Copyright (c) SpaceToad, 2011 http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public License
* 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.energy;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import net.minecraft.block.Block;
import net.minecraft.block.BlockFlower;
import net.minecraft.world.World;
import net.minecraft.world.biome.BiomeGenBase;
import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.event.ForgeSubscribe;
import net.minecraftforge.event.terraingen.PopulateChunkEvent;
import net.minecraftforge.event.terraingen.TerrainGen;
import buildcraft.BuildCraftCore;
import buildcraft.BuildCraftEnergy;
public class OilPopulate {
public static final Set<Integer> surfaceDepositBiomes = new HashSet<Integer>();
public static final Set<Integer> excludedBiomes = new HashSet<Integer>();
static {
surfaceDepositBiomes.add(BiomeGenBase.desert.biomeID);
surfaceDepositBiomes.add(BiomeGenBase.taiga.biomeID);
excludedBiomes.add(BiomeGenBase.sky.biomeID);
excludedBiomes.add(BiomeGenBase.hell.biomeID);
}
@ForgeSubscribe
public void populate(PopulateChunkEvent.Post event) {
boolean doGen = TerrainGen.populate(event.chunkProvider, event.world, event.rand, event.chunkX, event.chunkX, event.hasVillageGenerated, PopulateChunkEvent.Populate.EventType.CUSTOM);
if (!doGen) {
return;
}
// shift to world coordinates
int worldX = event.chunkX << 4;
int worldZ = event.chunkZ << 4;
doPopulate(event.world, event.rand, worldX, worldZ);
}
public static void doPopulate(World world, Random rand, int x, int z) {
BiomeGenBase biome = world.getBiomeGenForCoords(x + 16, z + 16);
// Do not generate oil in the End or Nether
if (excludedBiomes.contains(biome.biomeID)) {
return;
}
// Generate a surface oil lake
if (surfaceDepositBiomes.contains(biome.biomeID) && rand.nextDouble() <= 0.02) { // 2%
int lakeX = rand.nextInt(10) + 2 + x;
int lakeZ = rand.nextInt(10) + 2 + z;
int lakeY = world.getTopSolidOrLiquidBlock(lakeX, lakeZ) - 1;
int blockId = world.getBlockId(lakeX, lakeY, lakeZ);
if (blockId == biome.topBlock) {
generateSurfaceDeposit(world, rand, lakeX, lakeY, lakeZ, 3);
}
}
double bonus = surfaceDepositBiomes.contains(biome.biomeID) ? 1.5 : 1;
boolean mediumDeposit = rand.nextDouble() <= 0.0015 * bonus; // 0.15%
boolean largeDeposit = rand.nextDouble() <= 0.0005 * bonus; // 0.05%
if (BuildCraftCore.debugMode && x == 0 && z == 0) {
largeDeposit = true;
}
if (mediumDeposit || largeDeposit) {
// Generate a large cave deposit
int baseX = x, baseZ = z;
int wellY = 20 + rand.nextInt(10);
int baseY;
if (largeDeposit && BuildCraftEnergy.spawnOilSprings && (BuildCraftCore.debugMode || rand.nextDouble() <= 0.25)) {
baseY = 0;
} else {
baseY = wellY;
}
int radius = 0;
if (largeDeposit) {
radius = 8 + rand.nextInt(9);
} else if (mediumDeposit) {
radius = 4 + rand.nextInt(4);
}
int radiusSq = radius * radius;
for (int poolX = -radius; poolX <= radius; poolX++) {
for (int poolY = -radius; poolY <= radius; poolY++) {
for (int poolZ = -radius; poolZ <= radius; poolZ++) {
int distance = poolX * poolX + poolY * poolY + poolZ * poolZ;
if (distance <= radiusSq) {
world.setBlock(poolX + baseX, poolY + wellY, poolZ + baseZ, BuildCraftEnergy.oilStill.blockID);
}
}
}
}
boolean started = false;
for (int y = world.getActualHeight(); y >= baseY; --y) {
if (started) {
int blockId = world.getBlockId(baseX, y, baseZ);
if (blockId == Block.bedrock.blockID) {
if (BuildCraftEnergy.spawnOilSprings) {
world.setBlock(baseX, y, baseZ, BuildCraftCore.springBlock.blockID, 1, 2);
}
break;
}
world.setBlock(baseX, y, baseZ, BuildCraftEnergy.oilStill.blockID);
} else {
int blockId = world.getBlockId(baseX, y, baseZ);
Block block = Block.blocksList[blockId];
if (blockId != 0 && !block.isLeaves(world, baseX, y, baseZ) && !block.isWood(world, baseX, y, baseZ)) {
started = true;
if (largeDeposit) {
generateSurfaceDeposit(world, rand, baseX, y, baseZ, 20 + rand.nextInt(20));
} else if (mediumDeposit) {
generateSurfaceDeposit(world, rand, baseX, y, baseZ, 5 + rand.nextInt(5));
}
int wellHeight = 4;
if (largeDeposit) {
wellHeight = 16;
}
int ymax = Math.min(y + wellHeight, world.getActualHeight() - 1);
for (int h = y + 1; h <= ymax; ++h) {
world.setBlock(baseX, h, baseZ, BuildCraftEnergy.oilStill.blockID);
}
}
}
}
}
}
public static void generateSurfaceDeposit(World world, Random rand, int x, int y, int z, int radius) {
int depth = rand.nextDouble() < 0.5 ? 1 : 2;
setOilWithProba(world, rand, 1, x, y, z, true, depth);
for (int w = 1; w <= radius; ++w) {
float proba = (float) (radius - w + 4) / (float) (radius + 4);
for (int d = -w; d <= w; ++d) {
setOilWithProba(world, rand, proba, x + d, y, z + w, false, depth);
setOilWithProba(world, rand, proba, x + d, y, z - w, false, depth);
setOilWithProba(world, rand, proba, x + w, y, z + d, false, depth);
setOilWithProba(world, rand, proba, x - w, y, z + d, false, depth);
}
}
for (int dx = x - radius; dx <= x + radius; ++dx) {
for (int dz = z - radius; dz <= z + radius; ++dz) {
if (world.getBlockId(dx, y - 1, dz) != BuildCraftEnergy.oilStill.blockID) {
if (isOil(world, dx + 1, y - 1, dz) && isOil(world, dx - 1, y - 1, dz) && isOil(world, dx, y - 1, dz + 1)
&& isOil(world, dx, y - 1, dz - 1)) {
setOilWithProba(world, rand, 1.0F, dx, y, dz, true, depth);
}
}
}
}
}
private static boolean isOilOrWater(World world, int x, int y, int z) {
int blockId = world.getBlockId(x, y, z);
return blockId == Block.waterMoving.blockID || blockId == Block.waterStill.blockID || blockId == BuildCraftEnergy.oilStill.blockID || blockId == BuildCraftEnergy.oilMoving.blockID;
}
private static boolean isOil(World world, int x, int y, int z) {
int blockId = world.getBlockId(x, y, z);
return (blockId == BuildCraftEnergy.oilStill.blockID || blockId == BuildCraftEnergy.oilMoving.blockID);
}
public static void setOilWithProba(World world, Random rand, float proba, int x, int y, int z, boolean force, int depth) {
if ((rand.nextFloat() <= proba && world.getBlockId(x, y - 2, z) != 0) || force) {
boolean adjacentOil = false;
for (int d = -1; d <= 1; ++d) {
if (isOil(world, x + d, y - 1, z) || isOil(world, x - d, y - 1, z) || isOil(world, x, y - 1, z + d) || isOil(world, x, y - 1, z - d)) {
adjacentOil = true;
}
}
if (adjacentOil || force) {
if (world.isAirBlock(x, y + 1, z) || !world.isBlockOpaqueCube(x, y + 1, z) || Block.blocksList[world.getBlockId(x, y + 1, z)] instanceof BlockFlower) {
world.setBlockToAir(x, y + 1, z);
if (isOilOrWater(world, x, y, z)) {
world.setBlock(x, y, z, BuildCraftEnergy.oilStill.blockID);
} else {
world.setBlockToAir(x, y, z);
}
for (int d = depth; d > 0; d--) {
if (isOilOrWater(world, x, y - d - 1, z) || world.isBlockSolidOnSide(x, y - d - 1, z, ForgeDirection.UP)) {
world.setBlock(x, y - d, z, BuildCraftEnergy.oilStill.blockID);
}
}
}
}
}
}
}