-
Notifications
You must be signed in to change notification settings - Fork 497
/
BlockOilFlowing.java
282 lines (252 loc) · 6.98 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
/**
* 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 cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.Block;
import net.minecraft.block.BlockFluid;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.util.Icon;
import net.minecraft.world.World;
import net.minecraftforge.liquids.ILiquid;
import buildcraft.BuildCraftCore;
import buildcraft.BuildCraftEnergy;
import buildcraft.core.DefaultProps;
public class BlockOilFlowing extends BlockFluid implements ILiquid {
int numAdjacentSources = 0;
boolean isOptimalFlowDirection[] = new boolean[4];
int flowCost[] = new int[4];
@SideOnly(Side.CLIENT)
private Icon[] field_94425_a;
public BlockOilFlowing(int i, Material material) {
super(i, material);
setHardness(100F);
setLightOpacity(3);
}
@Override
public int getRenderType() {
return BuildCraftCore.oilModel;
}
private void updateFlow(World world, int i, int j, int k) {
int l = world.getBlockMetadata(i, j, k);
world.setBlockAndMetadataWithNotify(i, j, k, blockID + 1, l, 1);
world.markBlockRangeForRenderUpdate(i, j, k, i, j, k);
world.markBlockForUpdate(i, j, k);
}
@Override
public void updateTick(World world, int i, int j, int k, Random random) {
int l = getFlowDecay(world, i, j, k);
byte byte0 = 1;
boolean flag = true;
if (l > 0) {
int i1 = -100;
numAdjacentSources = 0;
i1 = getSmallestFlowDecay(world, i - 1, j, k, i1);
i1 = getSmallestFlowDecay(world, i + 1, j, k, i1);
i1 = getSmallestFlowDecay(world, i, j, k - 1, i1);
i1 = getSmallestFlowDecay(world, i, j, k + 1, i1);
int j1 = i1 + byte0;
if (j1 >= 8 || i1 < 0) {
j1 = -1;
}
if (getFlowDecay(world, i, j + 1, k) >= 0) {
int l1 = getFlowDecay(world, i, j + 1, k);
if (l1 >= 8) {
j1 = l1;
} else {
j1 = l1 + 8;
}
}
if (j1 != l) {
l = j1;
if (l < 0) {
world.func_94575_c(i, j, k, 0);
} else {
world.setBlockMetadataWithNotify(i, j, k, l,1);
world.scheduleBlockUpdate(i, j, k, blockID, tickRate(world));
world.notifyBlocksOfNeighborChange(i, j, k, blockID);
}
} else if (flag) {
updateFlow(world, i, j, k);
}
} else {
updateFlow(world, i, j, k);
}
if (liquidCanDisplaceBlock(world, i, j - 1, k)) {
if (l >= 8) {
world.setBlockAndMetadataWithNotify(i, j - 1, k, blockID, l,1);
} else {
world.setBlockAndMetadataWithNotify(i, j - 1, k, blockID, l + 8,1);
}
} else if (l >= 0 && (l == 0 || blockBlocksFlow(world, i, j - 1, k))) {
boolean aflag[] = getOptimalFlowDirections(world, i, j, k);
int k1 = l + byte0;
if (l >= 8) {
k1 = 1;
}
if (k1 >= 8)
return;
if (aflag[0]) {
flowIntoBlock(world, i - 1, j, k, k1);
}
if (aflag[1]) {
flowIntoBlock(world, i + 1, j, k, k1);
}
if (aflag[2]) {
flowIntoBlock(world, i, j, k - 1, k1);
}
if (aflag[3]) {
flowIntoBlock(world, i, j, k + 1, k1);
}
}
}
private void flowIntoBlock(World world, int i, int j, int k, int l) {
if (liquidCanDisplaceBlock(world, i, j, k)) {
int i1 = world.getBlockId(i, j, k);
if (i1 > 0) {
Block.blocksList[i1].dropBlockAsItem(world, i, j, k, world.getBlockMetadata(i, j, k), 0);
}
world.setBlockAndMetadataWithNotify(i, j, k, blockID, l,1);
}
}
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 world, int i, int j, int k) {
int l = world.getBlockId(i, j, k);
if (l == Block.doorWood.blockID || l == Block.doorSteel.blockID || l == Block.signPost.blockID || l == Block.ladder.blockID || l == Block.reed.blockID)
return true;
if (l == 0)
return false;
Material material = Block.blocksList[l].blockMaterial;
return material.isSolid();
}
protected int getSmallestFlowDecay(World world, int i, int j, int k, int l) {
int i1 = getFlowDecay(world, i, j, k);
if (i1 < 0)
return l;
if (i1 >= 8) {
i1 = 0;
}
return l >= 0 && i1 >= l ? l : 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 void onBlockAdded(World world, int i, int j, int k) {
super.onBlockAdded(world, i, j, k);
if (world.getBlockId(i, j, k) == blockID) {
world.scheduleBlockUpdate(i, j, k, blockID, tickRate(world));
}
}
@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.field_94425_a = new Icon[] {iconRegister.registerIcon("buildcraft:oil"), iconRegister.registerIcon("buildcraft:oil_flow")};
}
@Override
@SideOnly(Side.CLIENT)
public Icon getBlockTextureFromSideAndMetadata(int par1, int par2) {
return par1 != 0 && par1 != 1 ? this.field_94425_a[1] : this.field_94425_a[0];
}
}