-
Notifications
You must be signed in to change notification settings - Fork 497
/
BlockOilFlowing.java
324 lines (278 loc) · 8 KB
/
BlockOilFlowing.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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/**
* 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.Random;
import net.minecraft.block.Block;
import net.minecraft.block.BlockFlowing;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.util.Icon;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.liquids.ILiquid;
import buildcraft.BuildCraftCore;
import buildcraft.BuildCraftEnergy;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
public class BlockOilFlowing extends BlockFlowing implements ILiquid {
int numAdjacentSources = 0;
boolean isOptimalFlowDirection[] = new boolean[4];
int flowCost[] = new int[4];
public BlockOilFlowing(int i, Material material) {
super(i, material);
setHardness(100F);
setLightOpacity(3);
}
@Override
public int getRenderType() {
return BuildCraftCore.oilModel;
}
private void updateFlow(World par1World, int par2, int par3, int par4) {
int l = par1World.getBlockMetadata(par2, par3, par4);
par1World.setBlock(par2, par3, par4, this.blockID + 1, l, 2);
}
/**
* How many world ticks before ticking
*/
@Override
public int tickRate(World par1World) {
return 20;
}
@Override
public void updateTick(World world, int x, int y, int z, Random random) {
int oldDecay = this.getFlowDecay(world, x, y, z);
byte viscosity = 2;
int flowDecay;
if (oldDecay > 0) {
this.numAdjacentSources = 0;
int minFlowDecay = this.getSmallestFlowDecay(world, x - 1, y, z, -100);
minFlowDecay = this.getSmallestFlowDecay(world, x + 1, y, z, minFlowDecay);
minFlowDecay = this.getSmallestFlowDecay(world, x, y, z - 1, minFlowDecay);
minFlowDecay = this.getSmallestFlowDecay(world, x, y, z + 1, minFlowDecay);
flowDecay = minFlowDecay + viscosity;
if (flowDecay >= 8 || minFlowDecay < 0) {
flowDecay = -1;
}
int decayAbove = getFlowDecay(world, x, y + 1, z);
if (decayAbove >= 0) {
if (decayAbove >= 8) {
flowDecay = decayAbove;
} else {
flowDecay = decayAbove + 8;
}
}
boolean update = true;
if (oldDecay < 8 && flowDecay < 8 && flowDecay > oldDecay && random.nextDouble() < 0.2) {
flowDecay = oldDecay;
update = false;
}
if (flowDecay == oldDecay) {
if (update) {
this.updateFlow(world, x, y, z);
}
} else {
oldDecay = flowDecay;
if (flowDecay < 0) {
world.setBlockToAir(x, y, z);
} else {
world.setBlockMetadataWithNotify(x, y, z, flowDecay, 2);
world.scheduleBlockUpdate(x, y, z, this.blockID, this.tickRate(world));
world.notifyBlocksOfNeighborChange(x, y, z, this.blockID);
}
}
} else {
this.updateFlow(world, x, y, z);
}
if (this.liquidCanDisplaceBlock(world, x, y - 1, z)) {
if (oldDecay >= 8) {
this.flowIntoBlock(world, x, y - 1, z, oldDecay);
} else {
this.flowIntoBlock(world, x, y - 1, z, oldDecay + 8);
}
} else if (oldDecay >= 0 && (oldDecay == 0 || this.blockBlocksFlow(world, x, y - 1, z))) {
boolean[] flowDirection = this.getOptimalFlowDirections(world, x, y, z);
flowDecay = oldDecay + viscosity;
if (oldDecay >= 8) {
flowDecay = 1;
}
if (flowDecay >= 8) {
return;
}
if (flowDirection[0]) {
this.flowIntoBlock(world, x - 1, y, z, flowDecay);
}
if (flowDirection[1]) {
this.flowIntoBlock(world, x + 1, y, z, flowDecay);
}
if (flowDirection[2]) {
this.flowIntoBlock(world, x, y, z - 1, flowDecay);
}
if (flowDirection[3]) {
this.flowIntoBlock(world, x, y, z + 1, flowDecay);
}
}
}
private void flowIntoBlock(World world, int i, int j, int k, int l) {
if (liquidCanDisplaceBlock(world, i, j, k)) {
int blockId = world.getBlockId(i, j, k);
if (blockId > 0) {
Block.blocksList[blockId].dropBlockAsItem(world, i, j, k, world.getBlockMetadata(i, j, k), 0);
}
world.setBlock(i, j, k, blockID, l, 3);
}
}
private int calculateFlowCost(World world, int i, int j, int k, int l, int i1) {
int j1 = 1000;
for (int k1 = 0; k1 < 4; k1++) {
if (k1 == 0 && i1 == 1 || k1 == 1 && i1 == 0 || k1 == 2 && i1 == 3 || k1 == 3 && i1 == 2) {
continue;
}
int l1 = i;
int i2 = j;
int j2 = k;
if (k1 == 0) {
l1--;
}
if (k1 == 1) {
l1++;
}
if (k1 == 2) {
j2--;
}
if (k1 == 3) {
j2++;
}
if (blockBlocksFlow(world, l1, i2, j2) || world.getBlockMaterial(l1, i2, j2) == blockMaterial && world.getBlockMetadata(l1, i2, j2) == 0) {
continue;
}
if (!blockBlocksFlow(world, l1, i2 - 1, j2)) {
return l;
}
if (l >= 4) {
continue;
}
int k2 = calculateFlowCost(world, l1, i2, j2, l + 1, k1);
if (k2 < j1) {
j1 = k2;
}
}
return j1;
}
private boolean[] getOptimalFlowDirections(World world, int i, int j, int k) {
for (int l = 0; l < 4; l++) {
flowCost[l] = 1000;
int j1 = i;
int i2 = j;
int j2 = k;
if (l == 0) {
j1--;
}
if (l == 1) {
j1++;
}
if (l == 2) {
j2--;
}
if (l == 3) {
j2++;
}
if (blockBlocksFlow(world, j1, i2, j2) || world.getBlockMaterial(j1, i2, j2) == blockMaterial && world.getBlockMetadata(j1, i2, j2) == 0) {
continue;
}
if (!blockBlocksFlow(world, j1, i2 - 1, j2)) {
flowCost[l] = 0;
} else {
flowCost[l] = calculateFlowCost(world, j1, i2, j2, 1, l);
}
}
int i1 = flowCost[0];
for (int k1 = 1; k1 < 4; k1++) {
if (flowCost[k1] < i1) {
i1 = flowCost[k1];
}
}
for (int l1 = 0; l1 < 4; l1++) {
isOptimalFlowDirection[l1] = flowCost[l1] == i1;
}
return isOptimalFlowDirection;
}
private boolean blockBlocksFlow(World par1World, int par2, int par3, int par4) {
int l = par1World.getBlockId(par2, par3, par4);
if (l != Block.doorWood.blockID && l != Block.doorIron.blockID && l != Block.signPost.blockID && l != Block.ladder.blockID && l != Block.reed.blockID) {
if (l == 0) {
return false;
} else {
Material material = Block.blocksList[l].blockMaterial;
return material == Material.portal ? true : material.blocksMovement();
}
} else {
return true;
}
}
@Override
protected int getSmallestFlowDecay(World par1World, int par2, int par3, int par4, int par5) {
int i1 = this.getFlowDecay(par1World, par2, par3, par4);
if (i1 < 0) {
return par5;
} else {
if (i1 == 0) {
++this.numAdjacentSources;
}
if (i1 >= 8) {
i1 = 0;
}
return par5 >= 0 && i1 >= par5 ? par5 : i1;
}
}
private boolean liquidCanDisplaceBlock(World world, int i, int j, int k) {
Material material = world.getBlockMaterial(i, j, k);
if (material == blockMaterial) {
return false;
} else {
return !blockBlocksFlow(world, i, j, k);
}
}
@Override
public int stillLiquidId() {
return BuildCraftEnergy.oilStill.blockID;
}
@Override
public boolean isMetaSensitive() {
return false;
}
@Override
public int stillLiquidMeta() {
return 0;
}
@Override
public boolean isBlockReplaceable(World world, int i, int j, int k) {
return true;
}
@Override
@SideOnly(Side.CLIENT)
public void registerIcons(IconRegister iconRegister) {
this.theIcon = new Icon[]{iconRegister.registerIcon("buildcraft:oil"), iconRegister.registerIcon("buildcraft:oil_flow")};
}
@Override
public int getFireSpreadSpeed(World world, int x, int y, int z, int metadata, ForgeDirection face) {
return BuildCraftEnergy.canOilBurn ? 300 : 0;
}
@Override
public int getFlammability(IBlockAccess world, int x, int y, int z, int metadata, ForgeDirection face) {
return 0;
}
@Override
public boolean isFlammable(IBlockAccess world, int x, int y, int z, int metadata, ForgeDirection face) {
return BuildCraftEnergy.canOilBurn;
}
@Override
public boolean isFireSource(World world, int x, int y, int z, int metadata, ForgeDirection side) {
return BuildCraftEnergy.canOilBurn;
}
}