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
|
/*
* spotless:off
* KubaTech - Gregtech Addon
* Copyright (C) 2022 - 2024 kuba6000
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <https://www.gnu.org/licenses/>.
* spotless:on
*/
package kubatech.test;
import static gregtech.api.util.GT_RecipeBuilder.HOURS;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.File;
import java.util.Map;
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.MinecraftException;
import net.minecraft.world.World;
import net.minecraft.world.WorldProvider;
import net.minecraft.world.WorldProviderSurface;
import net.minecraft.world.WorldServer;
import net.minecraft.world.WorldSettings;
import net.minecraft.world.WorldType;
import net.minecraft.world.biome.BiomeGenBase;
import net.minecraft.world.chunk.storage.IChunkLoader;
import net.minecraft.world.storage.IPlayerFileData;
import net.minecraft.world.storage.ISaveHandler;
import net.minecraft.world.storage.WorldInfo;
import net.minecraftforge.common.DimensionManager;
import org.junit.jupiter.api.Test;
import gregtech.api.GregTech_API;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.common.blocks.GT_Item_Machines;
import ic2.api.crops.CropCard;
import ic2.api.crops.Crops;
import ic2.core.Ic2Items;
import ic2.core.crop.TileEntityCrop;
import ic2.core.item.ItemCropSeed;
import kubatech.api.eig.EIGDropTable;
import kubatech.tileentity.gregtech.multiblock.GT_MetaTileEntity_ExtremeIndustrialGreenhouse;
import kubatech.tileentity.gregtech.multiblock.eigbuckets.EIGIC2Bucket;
public class EIGTests {
private static final int EIG_CONTROLLER_METADATA = 12_792;
private static final int EIG_SIMULATION_TIME = 24 * HOURS;
private static final int NUMBER_OF_TESTS_TO_DO = 1000;
static World myWorld;
public EIGTests() {
if (!DimensionManager.isDimensionRegistered(256)) {
DimensionManager.registerProviderType(256, WorldProviderSurface.class, false);
DimensionManager.registerDimension(256, 256);
}
if (myWorld == null) {
myWorld = new WorldServer(MinecraftServer.getServer(), new ISaveHandler() {
@Override
public WorldInfo loadWorldInfo() {
return null;
}
@Override
public void checkSessionLock() throws MinecraftException {
}
@Override
public IChunkLoader getChunkLoader(WorldProvider p_75763_1_) {
return null;
}
@Override
public void saveWorldInfoWithPlayer(WorldInfo p_75755_1_, NBTTagCompound p_75755_2_) {
}
@Override
public void saveWorldInfo(WorldInfo p_75761_1_) {
}
@Override
public IPlayerFileData getSaveHandler() {
return null;
}
@Override
public void flush() {
}
@Override
public File getWorldDirectory() {
return null;
}
@Override
public File getMapFileFromName(String p_75758_1_) {
return null;
}
@Override
public String getWorldDirectoryName() {
return "dummy";
}
},
"DummyTestWorld",
256,
new WorldSettings(256, WorldSettings.GameType.SURVIVAL, false, false, WorldType.DEFAULT),
MinecraftServer.getServer().theProfiler) {
@Override
public File getChunkSaveLocation() {
return new File("ignoreme");
}
@Override
public int getBlockLightValue(int p_72957_1_, int p_72957_2_, int p_72957_3_) {
return 4;
}
@Override
public BiomeGenBase getBiomeGenForCoords(int x, int z) {
// give the environment a fighting chance of being bearable for crops
return BiomeGenBase.jungle;
}
};
}
}
EIGDropTable getRealDrops(TileEntityCrop cropTile, CropCard cc, int growth, int gain, int resistance) {
cropTile.setCrop(cc);
cropTile.setGrowth((byte) growth);
cropTile.setGain((byte) gain);
cropTile.setResistance((byte) resistance);
EIGDropTable expected = new EIGDropTable();
byte startingHumidity = cropTile.humidity;
byte startingNutrients = cropTile.nutrients;
byte startingAirQuality = cropTile.airQuality;
int startingNutrientStorage = cropTile.nutrientStorage;
int startingWaterStorage = cropTile.waterStorage;
// reset the crop to it's stage after harvest
cropTile.setSize((byte) cc.maxSize());
cropTile.setSize(cc.getSizeAfterHarvest(cropTile));
for (int timeElapsed = 0; timeElapsed < EIG_SIMULATION_TIME; timeElapsed += TileEntityCrop.tickRate) {
// force reset the stats to max because the eig shouldn't make them change.
// some crops check water storage in the can grow and we are ticking which consumes water.
cropTile.humidity = startingHumidity;
cropTile.nutrients = startingNutrients;
cropTile.airQuality = startingAirQuality;
cropTile.nutrientStorage = startingNutrientStorage;
cropTile.waterStorage = startingWaterStorage;
// if fully grown harvest the crop
if (cropTile.getSize() >= cc.maxSize()) {
ItemStack[] stacks = cropTile.harvest_automated(false);
for (ItemStack stack : stacks) {
expected.addDrop(stack, stack.stackSize);
}
}
cropTile.tick();
}
// ensure it leaves the same way it came in
cropTile.humidity = startingHumidity;
cropTile.nutrients = startingNutrients;
cropTile.airQuality = startingAirQuality;
cropTile.nutrientStorage = startingNutrientStorage;
cropTile.waterStorage = startingWaterStorage;
return expected;
}
EIGDropTable getEIGDrops(GT_MetaTileEntity_ExtremeIndustrialGreenhouse EIG, ItemStack stack) {
EIGDropTable generated = new EIGDropTable();
EIGIC2Bucket bucket = new EIGIC2Bucket(stack, stack.stackSize, null, false);
bucket.revalidate(EIG);
bucket.addProgress(EIG_SIMULATION_TIME, generated);
return generated;
}
@Test
void EIGDrops() {
myWorld.setBlock(10, 80, 0, Blocks.farmland, 0, 0);
myWorld.setBlock(10, 81, 0, Block.getBlockFromItem(Ic2Items.crop.getItem()), 0, 0);
// using stickreed since it has a random stage after harvest.
// it's also more preferable to test using faster growing crops since they can be harvested more often.
CropCard cc = Crops.instance.getCropCard("IC2", "stickreed");
TileEntityCrop cropTile = (TileEntityCrop) myWorld.getTileEntity(10, 81, 0);
ItemStack ccStack = ItemCropSeed.generateItemStackFromValues(cc, (byte) 10, (byte) 10, (byte) 10, (byte) 1);
GT_Item_Machines itemMachines = (GT_Item_Machines) Item.getItemFromBlock(GregTech_API.sBlockMachines);
itemMachines.placeBlockAt(
new ItemStack(itemMachines, 1, EIG_CONTROLLER_METADATA),
null,
myWorld,
0,
81,
0,
2,
0,
0,
0,
EIG_CONTROLLER_METADATA);
IGregTechTileEntity te = (IGregTechTileEntity) myWorld.getTileEntity(0, 81, 0);
GT_MetaTileEntity_ExtremeIndustrialGreenhouse EIG = (GT_MetaTileEntity_ExtremeIndustrialGreenhouse) te
.getMetaTileEntity();
// update stats of crop TE to those provided by the EIG
cropTile.humidity = EIGIC2Bucket.getHumidity(EIG, false);
cropTile.nutrients = EIGIC2Bucket.getNutrients(EIG);
cropTile.airQuality = EIGIC2Bucket.getAirQuality(EIG);
int[] abc = new int[] { 0, -2, 3 };
int[] xyz = new int[] { 0, 0, 0 };
EIG.getExtendedFacing()
.getWorldOffset(abc, xyz);
xyz[0] += te.getXCoord();
xyz[1] += te.getYCoord();
xyz[2] += te.getZCoord();
myWorld.setBlock(xyz[0], xyz[1] - 2, xyz[2], GregTech_API.sBlockCasings4, 1, 0);
myWorld.setBlock(xyz[0], xyz[1] - 1, xyz[2], Blocks.farmland, 0, 0);
ItemStack stackToTest = null;
double realAvg = 0, eigAvg = 0;
for (int i = 0; i < NUMBER_OF_TESTS_TO_DO; i++) {
EIGDropTable expected = getRealDrops(cropTile, cc, 10, 10, 10);
EIGDropTable generated = getEIGDrops(EIG, ccStack);
// we are only comparing one item from drops
if (stackToTest == null) {
stackToTest = expected.entrySet()
.stream()
.max(Map.Entry.comparingByValue())
.get()
.getKey();
}
realAvg += expected.getItemAmount(stackToTest);
// EIG with ic2 crops doesn't actually have variance, it uses very precise approximations that create
// accurate growth rate and drop quality approximations.
eigAvg += generated.getItemAmount(stackToTest);
}
realAvg /= NUMBER_OF_TESTS_TO_DO;
eigAvg /= NUMBER_OF_TESTS_TO_DO;
double accuracy = Math.min(realAvg / eigAvg, eigAvg / realAvg);
String debugInfo = String.format("realAvg: %.5f | eigAvg : %.5f | accuracy = %.5f", realAvg, eigAvg, accuracy);
System.out.println(debugInfo);
// We aim for about 99% accuracy over here.
assertTrue(accuracy >= 0.99d);
}
}
|