-
Notifications
You must be signed in to change notification settings - Fork 5
/
MeteorGenerator.java
470 lines (412 loc) · 15.2 KB
/
MeteorGenerator.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*******************************************************************************
* @author Reika Kalseki
*
* Copyright 2017
*
* All rights reserved.
* Distribution of the software in any form is only allowed with
* explicit, prior permission from the owner.
******************************************************************************/
package Reika.MeteorCraft;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Random;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
import Reika.DragonAPI.ModList;
import Reika.DragonAPI.Instantiable.ItemDrop;
import Reika.DragonAPI.Instantiable.Data.WeightedRandom;
import Reika.DragonAPI.Instantiable.Data.WeightedRandom.InvertedWeightedRandom;
import Reika.DragonAPI.Instantiable.Data.BlockStruct.BlockArray;
import Reika.DragonAPI.Instantiable.Data.Immutable.BlockKey;
import Reika.DragonAPI.Instantiable.Data.Immutable.Coordinate;
import Reika.DragonAPI.Libraries.Java.ReikaJavaLibrary;
import Reika.DragonAPI.Libraries.Java.ReikaRandomHelper;
import Reika.DragonAPI.Libraries.MathSci.ReikaMathLibrary;
import Reika.DragonAPI.Libraries.Registry.ReikaItemHelper;
import Reika.DragonAPI.Libraries.Registry.ReikaOreHelper;
import Reika.DragonAPI.Libraries.World.ReikaWorldHelper;
import Reika.DragonAPI.ModInteract.ItemHandlers.AppEngHandler;
import Reika.DragonAPI.ModInteract.ItemHandlers.MagicCropHandler;
import Reika.DragonAPI.ModRegistry.ModOreList;
import Reika.MeteorCraft.CustomOreLoader.CustomOreEntry;
import Reika.MeteorCraft.Entity.EntityMeteor;
import Reika.MeteorCraft.Registry.MeteorOptions;
import cpw.mods.fml.common.registry.GameRegistry;
public class MeteorGenerator {
public static final MeteorGenerator instance = new MeteorGenerator();
private final HashMap<MeteorType, InvertedWeightedRandom<BlockKey>> viableOres = new HashMap();
private final HashMap<ModOreList, HashMap<MeteorType, ArrayList<Integer>>> metas = new HashMap();
private final HashMap<ModOreList, HashMap<MeteorType, ArrayList<Block>>> ids = new HashMap();
private static final Random rand = new Random();
private final Material[] mats;
private static final HashSet<Material> softMeteorMats;
static {
softMeteorMats = new HashSet(ReikaJavaLibrary.makeListFrom(Material.circuits, Material.glass, Material.snow, Material.ice, Material.cactus, Material.craftedSnow, Material.fire, Material.leaves, Material.plants, Material.portal, Material.gourd, Material.redstoneLight, Material.sponge, Material.lava, Material.water, Material.vine, Material.web));
}
private MeteorGenerator() {
this.addMetadata(ModOreList.PITCHBLENDE, MeteorType.STONE, 1);
this.addMetadata(ModOreList.PITCHBLENDE, MeteorType.END, 5);
this.addMetadata(ModOreList.FORCE, MeteorType.STONE, 0);
this.addMetadata(ModOreList.FORCE, MeteorType.NETHERRACK, 1);
this.addID(ModOreList.ESSENCE, MeteorType.STONE, MagicCropHandler.getInstance().oreID);
this.addID(ModOreList.ESSENCE, MeteorType.NETHERRACK, MagicCropHandler.getInstance().netherOreID);
this.addID(ModOreList.ESSENCE, MeteorType.END, MagicCropHandler.getInstance().endOreID);
this.addMetadata(ModOreList.MIMICHITE, MeteorType.STONE, 0);
this.addMetadata(ModOreList.MIMICHITE, MeteorType.NETHERRACK, 1);
this.addMetadata(ModOreList.MIMICHITE, MeteorType.END, 2);
for (int k = 0; k < MeteorType.list.length; k++) {
MeteorType type = MeteorType.list[k];
Block id = type.blockID;
int meta = type.blockMeta;
for (int i = 0; i < ReikaOreHelper.oreList.length; i++) {
ReikaOreHelper ore = ReikaOreHelper.oreList[i];
if (this.canGenerateOre(ore)) {
if (this.isValidOreForType(type, ore)) {
this.addOre(type, BlockKey.fromItem(ore.getOreBlock()), MeteorCraft.config.getOreWeight(ore));
}
}
}
for (int i = 0; i < ModOreList.oreList.length; i++) {
ModOreList ore = ModOreList.oreList[i];
if (this.canGenerateOre(ore) && this.canGenOreIn(type.blockID, ore)) {
Collection<ItemStack> li = ore.getAllOreBlocks();
for (ItemStack is : li) {
if (is.getItem() != null && Block.getBlockFromItem(is.getItem()) != null) {
BlockKey block = BlockKey.fromItem(is);
if (MeteorCraft.config.isItemStackGenerationPermitted(block)) {
//ReikaJavaLibrary.pConsole(type.name()+" INIT:"+ore.name());
if (this.isValidOreIDForType(type, ore, block.blockID)) {
//ReikaJavaLibrary.pConsole(type.name()+" ID:"+ore.name());
if (this.isValidOreMetaForType(type, ore, block.metadata)) {
//ReikaJavaLibrary.pConsole(type.name()+" META:"+ore.name());
String mod = ReikaItemHelper.getRegistrantMod(is);
if (mod == null || mod.equalsIgnoreCase("gregtech") || mod.equalsIgnoreCase("gt"))
continue;
MeteorCraft.logger.log("Registering "+mod+":"+block+" ("+ore.displayName+" ore) to meteor type "+type.name());
this.addOre(type, block, MeteorCraft.config.getOreWeight(ore));
}
}
}
}
}
}
}
}
ArrayList<Material> mats = new ArrayList();
mats.add(Material.circuits);
mats.add(Material.glass);
mats.add(Material.snow);
mats.add(Material.ice);
mats.add(Material.cactus);
mats.add(Material.craftedSnow);
mats.add(Material.fire);
mats.add(Material.leaves);
mats.add(Material.plants);
mats.add(Material.portal);
mats.add(Material.gourd);
mats.add(Material.redstoneLight);
mats.add(Material.sponge);
mats.add(Material.lava);
mats.add(Material.water);
mats.add(Material.vine);
mats.add(Material.web);
this.mats = new Material[mats.size()];
for (int i = 0; i < mats.size(); i++) {
this.mats[i] = mats.get(i);
}
List<CustomOreEntry> li = CustomOreLoader.instance.getEntries();
for (int i = 0; i < li.size(); i++) {
CustomOreEntry en = li.get(i);
for (Entry<BlockKey, Double> e : en.getItems().entrySet()) {
for (MeteorType type : en.getMeteorTypes()) {
this.addOre(type, e.getKey(), e.getValue());
}
}
}
}
private void addMetadata(ModOreList ore, MeteorType type, int dmg) {
HashMap<MeteorType, ArrayList<Integer>> map = metas.get(ore);
if (map == null) {
map = new HashMap();
metas.put(ore, map);
}
ArrayList<Integer> li = map.get(type);
if (li == null) {
li = new ArrayList();
map.put(type, li);
}
if (!li.contains(dmg))
li.add(dmg);
}
private void addID(ModOreList ore, MeteorType type, Block id) {
HashMap<MeteorType, ArrayList<Block>> map = ids.get(ore);
if (map == null) {
map = new HashMap();
ids.put(ore, map);
}
ArrayList<Block> li = map.get(type);
if (li == null) {
li = new ArrayList();
map.put(type, li);
}
if (!li.contains(id))
li.add(id);
}
public boolean hasListedMetadatasForOre(ModOreList ore) {
return metas.get(ore) != null;
}
public boolean hasListedIDsForOre(ModOreList ore) {
return ids.get(ore) != null;
}
public boolean isValidOreForType(MeteorType type, ReikaOreHelper ore) {
return type.blockID == ore.getOreGenBlock();
}
public boolean isValidOreMetaForType(MeteorType type, ModOreList ore, int meta) {
HashMap<MeteorType, ArrayList<Integer>> map = metas.get(ore);
if (map != null) {
ArrayList<Integer> li = map.get(type);
if (li != null) {
return li.contains(meta);
}
else
return true;
}
else
return true;
}
public boolean isValidOreIDForType(MeteorType type, ModOreList ore, Block id) {
HashMap<MeteorType, ArrayList<Block>> map = ids.get(ore);
if (map != null) {
ArrayList<Block> li = map.get(type);
if (li != null) {
return li.contains(id);
}
else
return true;
}
else
return true;
}
private void addOre(MeteorType type, BlockKey is, double weight) {
InvertedWeightedRandom<BlockKey> dat = this.getOres(type);
if (dat == null) {
dat = new InvertedWeightedRandom();
}
dat.addEntry(weight, is);
viableOres.put(type, dat);
}
private InvertedWeightedRandom<BlockKey> getOres(MeteorType type) {
return viableOres.get(type);
}
public boolean hasOresForType(MeteorType type) {
return this.getOres(type) != null && !this.getOres(type).isEmpty();
}
private BlockKey getRandomOre(MeteorType type) {
return this.getOres(type).getRandomEntry();
}
public boolean canGenerateOre(ModOreList ore) {
return MeteorCraft.config.shouldGenerateOre(ore) && ore.existsInGame();
}
public boolean canGenerateOre(ReikaOreHelper ore) {
return MeteorCraft.config.shouldGenerateOre(ore);
}
public BlockKey getBlock(MeteorType type, boolean ore) {
if (ore && ReikaRandomHelper.doWithChance(MeteorOptions.ORE.getValue()) && this.hasOresForType(type)) {
return this.getRandomOre(type);
}
else {
return type.getBlock();
}
}
public void generate(World world, int x, int y, int z, EntityMeteor e) {
this.generate(world, x, y, z, e.getType(), e.genOres());
}
public void generate(World world, int x, int y, int z) {
this.generate(world, x, y, z, this.getTypeFor(world), true);
}
private MeteorType getTypeFor(World world) {
if (world.provider.dimensionId == -1 || world.provider.isHellWorld)
return MeteorType.NETHERRACK;
if (world.provider.dimensionId == 1 || world.provider.hasNoSky)
return MeteorType.END;
return MeteorType.STONE;
}
private void generate(World world, int x, int y, int z, MeteorType type, boolean ore) {
BlockArray blocks = this.getMeteorBlockArray(world, x, y, z);
for (int i = 0; i < blocks.getSize(); i++) {
Coordinate c = blocks.getNthBlock(i);
int fx = c.xCoord;
int fy = c.yCoord;
int fz = c.zCoord;
BlockKey is = this.getBlock(type, ore);
if (fy > 0 && world.getBlock(fx, fy, fz) != Blocks.bedrock)
is.place(world, fx, fy, fz);
}
}
public BlockArray getMeteorBlockArray(World world, int x, int y, int z) {
BlockArray blocks = new BlockArray();
int radius = 2+rand.nextInt(3);
for (int i = -radius; i <= radius; i++) {
for (int j = -radius; j <= radius; j++) {
for (int k = -radius; k <= radius; k++) {
double dd = ReikaMathLibrary.py3d(i, j, k);
int dx = x+i;
int dy = y+j;
int dz = z+k;
//dy = world.getTopSolidOrLiquidBlock(dx, dz);
if (dd <= radius || (j < 0 && ReikaMathLibrary.py3d(i, 0, k) <= radius)) {
//ItemStack is = this.getBlock(e);
//world.setBlock(dx, dy, dz, is.getItem(), is.getItemDamage(), 3);
blocks.addBlockCoordinate(dx, dy, dz);
}
}
}
}
blocks.sink(world, mats);
return blocks;
}
public static boolean canStopMeteor(World world, int x, int y, int z) {
Block b = world.getBlock(x, y, z);
if (b == Blocks.air)
return false;
if (ReikaWorldHelper.softBlocks(world, x, y, z))
return false;
if (ModList.NATURA.isLoaded() && b == GameRegistry.findBlock(ModList.NATURA.modLabel, "Cloud"))
return false;
Material mat = ReikaWorldHelper.getMaterial(world, x, y, z);
if (softMeteorMats.contains(mat))
return false;
return true;
}
public static boolean canGenOreIn(Block b, ReikaOreHelper ore) {
return b == ore.getOreGenBlock();
}
//Very basic rules:
public static boolean canGenOreIn(Block blockID, ModOreList ore) {
if (blockID == AppEngHandler.getInstance().skystone)
return false;
if (ore.isNetherOres())
return blockID == Blocks.netherrack;
if (ore == ModOreList.ARDITE || ore == ModOreList.COBALT)
return blockID == Blocks.netherrack;
if (ore == ModOreList.FIRESTONE)
return blockID == Blocks.netherrack;
if (ore == ModOreList.MAGMANITE)
return blockID == Blocks.netherrack;
if (ore == ModOreList.AMMONIUM)
return blockID == Blocks.netherrack;
if (ore == ModOreList.SODALITE)
return blockID == Blocks.end_stone;
if (ore == ModOreList.ESSENCE)
return blockID == Blocks.netherrack || blockID == Blocks.stone;
if (blockID == Blocks.end_stone)
return ore == ModOreList.PITCHBLENDE;
if (ore == ModOreList.MIMICHITE || ore == ModOreList.DRACONIUM)
return true;
if (ore == ModOreList.THORIUM)
return blockID == Blocks.netherrack;
if (ore == ModOreList.ENDIUM)
return blockID == Blocks.end_stone;
return blockID == Blocks.stone;
}
public static boolean canStopMeteorRayTrace(World world, int x, int y, int z, EntityMeteor e, int dist) {
int r = 4;
for (int i = 0; i < dist; i++) {
for (int a = -r; a <= r; a++) {
for (int b = -r; b <= r; b++) {
for (int c = -r; c <= r; c++) {
int dx = a+MathHelper.floor_double(x+e.motionX*i);
int dy = b+MathHelper.floor_double(y+e.motionY*i);
int dz = c+MathHelper.floor_double(z+e.motionZ*i);
if (dy < world.provider.getActualHeight()) {
if (canStopMeteor(world, dx, dy, dz))
return true;
}
}
}
}
}
return false;
}
public static enum MeteorType {
STONE(Blocks.stone, 100),
NETHERRACK(Blocks.netherrack, 20),
END(Blocks.end_stone, 10),
SKYSTONE(AppEngHandler.getInstance().skystone, 15);
public final Block blockID;
public final int blockMeta;
private final int chance;
private Collection<ItemDrop> drops = new ArrayList();
private static WeightedRandom<MeteorType> rand = new WeightedRandom();
public static final MeteorType[] list = values();
private MeteorType(Block b, int c) {
blockID = b;
blockMeta = 0;
chance = c;
}
public BlockKey getBlock() {
return new BlockKey(blockID, blockMeta);
}
public static MeteorType getWeightedType() {
return rand.getRandomEntry();
}
private void addDrop(ItemStack is, int min, int max) {
if (is == null || is.getItem() == null) {
MeteorCraft.logger.logError("Cannot add drop to meteor type "+this+"! Null item!");
return;
}
if (this.isValid())
drops.add(new ItemDrop(is, min, max));
}
private void addDrop(Item is, int min, int max) {
this.addDrop(new ItemStack(is), min, max);
}
static {
for (int i = 0; i < list.length; i++) {
MeteorType m = list[i];
if (m.isValid())
rand.addEntry(m, m.chance);
}
if (MeteorOptions.TYPEHISTORY.getState()) {
rand.setHistorical();
}
MeteorType.STONE.addDrop(Items.glowstone_dust, 16, 32);
MeteorType.STONE.addDrop(Items.gunpowder, 9, 18);
//MeteorType.NETHERRACK.addDrop(Items.glowstone_dust, 16, 32);
//MeteorType.NETHERRACK.addDrop(Items.gunpowder, 9, 18);
MeteorType.NETHERRACK.addDrop(Items.blaze_powder, 16, 32);
MeteorType.NETHERRACK.addDrop(Items.nether_wart, 16, 32);
//MeteorType.END.addDrop(Items.glowstone_dust, 16, 32);
//MeteorType.END.addDrop(Items.gunpowder, 9, 18);
MeteorType.END.addDrop(Items.ender_pearl, 4, 12);
MeteorType.END.addDrop(Items.nether_star, 0, 1);
if (ModList.APPENG.isLoaded()) {
//MeteorType.SKYSTONE.addDrop(Items.glowstone_dust, 16, 32);
//MeteorType.SKYSTONE.addDrop(Items.gunpowder, 9, 18);
MeteorType.SKYSTONE.addDrop(AppEngHandler.getInstance().getCertusQuartzDust(), 16, 32);
Collection<ItemStack> c = AppEngHandler.getInstance().getPossibleMeteorChestLoot();
for (ItemStack is : c) {
MeteorType.SKYSTONE.addDrop(is, 0, 1);
}
}
}
public Collection<ItemDrop> getDroppedItems() {
return Collections.unmodifiableCollection(drops);
}
public boolean isValid() {
return blockID != null;
}
}
}