Skip to content

Commit

Permalink
Readd Meteor Filler (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
koolkrafter5 authored Nov 22, 2024
1 parent 222ad16 commit ba0a0e3
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class CachedMeteorRecipe extends CachedRecipe {

private final List<PositionedStack> input = new ArrayList<>();
private final List<PositionedStack> outputs = new ArrayList<>();
private final List<PositionedStack> filler = new ArrayList<>();
private final int cost;
private final int radius;
private Point focus;
Expand All @@ -40,17 +41,21 @@ public CachedMeteorRecipe(MeteorParadigm meteor, ItemStack focusStack) {
int row = 0;
int col = 0;

float totalMeteorWeight = meteor.getTotalMeteorWeight();
float totalComponentWeight = meteor.getTotalListWeight(meteor.componentList);
int fillerChance = meteor.fillerChance;
List<MeteorParadigmComponent> sortedComponents = new ArrayList<>(meteor.componentList);
sortedComponents.sort(Comparator.comparingInt(c -> -c.getChance()));
sortedComponents.sort(Comparator.comparingInt(c -> -c.getWeight()));

float fillerRatio = (float) (fillerChance / 100.0);
float componentRatio = 1 - fillerRatio;

for (MeteorParadigmComponent component : sortedComponents) {
ItemStack stack = component.getValidBlockParadigm();
int xPos = 3 + 18 * col;
int yPos = 37 + 18 * row;

List<String> tooltips = new ArrayList<>();
float chance = component.getChance() / totalMeteorWeight;
float chance = component.getWeight() / totalComponentWeight * componentRatio;
tooltips.add(I18n.format("nei.recipe.meteor.chance", getFormattedChance(chance)));
tooltips.add(I18n.format("nei.recipe.meteor.amount", getEstimatedAmount(chance, meteor.radius)));
this.outputs.add(new TooltipStack(stack, xPos, yPos, tooltips));
Expand All @@ -61,10 +66,45 @@ public CachedMeteorRecipe(MeteorParadigm meteor, ItemStack focusStack) {
row++;
}

if (focusStack != null && matchItem(focusStack, stack)) {
if (matchItem(focusStack, stack)) {
this.focus = new Point(xPos - 1, yPos - 1);
}
}

if (fillerChance > 0) {
if (col != 0) {
col = 0;
row++;
}

List<MeteorParadigmComponent> sortedFiller = new ArrayList<>(meteor.fillerList);
sortedFiller.sort(Comparator.comparingInt(c -> -c.getWeight()));
float totalFillerWeight = meteor.getTotalListWeight(meteor.fillerList);

for (MeteorParadigmComponent filler : sortedFiller) {
ItemStack stack = filler.getValidBlockParadigm();
int xPos = 3 + 18 * col;
int yPos = 37 + 18 * row;

List<String> tooltips = new ArrayList<>();
float chance = filler.getWeight() / totalFillerWeight * fillerRatio;
tooltips.add(I18n.format("nei.recipe.meteor.chance", getFormattedChance(chance)));
tooltips.add(I18n.format("nei.recipe.meteor.amount", getEstimatedAmount(chance, meteor.radius)));
tooltips.add(I18n.format("nei.recipe.meteor.filler"));
this.outputs.add(new TooltipStack(stack, xPos, yPos, tooltips));

col++;
if (col > 8) {
col = 0;
row++;
}

if (matchItem(focusStack, stack)) {
this.focus = new Point(xPos - 1, yPos - 1);
}
}
}

this.radius = meteor.radius;
this.cost = meteor.cost;
}
Expand Down Expand Up @@ -115,6 +155,9 @@ public void loadCraftingRecipes(ItemStack result) {
if (meteor.componentList.stream().anyMatch(m -> matchItem(result, m.getValidBlockParadigm()))) {
arecipes.add(new CachedMeteorRecipe(meteor, result));
}
if (meteor.fillerList.stream().anyMatch(m -> matchItem(result, m.getValidBlockParadigm()))) {
arecipes.add(new CachedMeteorRecipe(meteor, result));
}
}
}

Expand Down Expand Up @@ -205,6 +248,6 @@ private String getFormattedChance(float chance) {
}

private int getEstimatedAmount(float chance, int radius) {
return (int) Math.ceil(4f / 3 * Math.PI * Math.pow(radius, 3) * chance);
return (int) Math.ceil(4f / 3 * Math.PI * Math.pow(radius + 0.5, 3) * chance);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;

import cpw.mods.fml.common.registry.GameRegistry;

public class Meteor {

public String[] ores;
public int radius;
public int cost;
public String focusModId;
public String focusName;
public int focusMeta;
private String[] ores;
private int radius;
private int cost;
private String focusModId;
private String focusName;
private int focusMeta;
private String[] filler;
private int fillerChance;

public static void loadConfig() {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
Expand All @@ -35,9 +38,11 @@ public static void loadConfig() {
findItemStack(m.focusModId, m.focusName, m.focusMeta),
m.ores,
m.radius,
m.cost);
m.cost,
m.filler,
m.fillerChance);
}
} catch (FileNotFoundException e) {
} catch (FileNotFoundException | JsonSyntaxException e) {
e.printStackTrace();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,37 @@
public class MeteorParadigm {

public List<MeteorParadigmComponent> componentList = new ArrayList<>();
public List<MeteorParadigmComponent> fillerList = new ArrayList<>();
public ItemStack focusStack;
public int radius;
public int cost;
public int fillerChance; // Out of 100

public static Random rand = new Random();

public MeteorParadigm(ItemStack focusStack, int radius, int cost) {
new MeteorParadigm(focusStack, radius, cost, 0);
}

public MeteorParadigm(ItemStack focusStack, int radius, int cost, int fillerChance) {
this.focusStack = focusStack;
this.radius = radius;
this.cost = cost;
this.fillerChance = fillerChance;
}

// modId:itemName:meta:weight
private static final Pattern itemNamePattern = Pattern.compile("(.*):(.*):(\\d+):(\\d+)");
// OREDICT:oreDictName:weight
private static final Pattern oredictPattern = Pattern.compile("OREDICT:(.*):(\\d+)");

public void parseStringArray(String[] oreArray) {
for (int i = 0; i < oreArray.length; ++i) {
String oreName = oreArray[i];
public static List<MeteorParadigmComponent> parseStringArray(String[] blockArray) {
List<MeteorParadigmComponent> addList = new ArrayList<>();
for (int i = 0; i < blockArray.length; ++i) {
String blockName = blockArray[i];
boolean success = false;

Matcher matcher = itemNamePattern.matcher(oreName);
Matcher matcher = itemNamePattern.matcher(blockName);
if (matcher.matches()) {
String modID = matcher.group(1);
String itemName = matcher.group(2);
Expand All @@ -53,50 +61,51 @@ public void parseStringArray(String[] oreArray) {
ItemStack stack = GameRegistry.findItemStack(modID, itemName, 1);
if (stack != null && stack.getItem() instanceof ItemBlock) {
stack.setItemDamage(meta);
componentList.add(new MeteorParadigmComponent(stack, weight));
addList.add(new MeteorParadigmComponent(stack, weight));
success = true;
}

} else if ((matcher = oredictPattern.matcher(oreName)).matches()) {
} else if ((matcher = oredictPattern.matcher(blockName)).matches()) {
String oreDict = matcher.group(1);
int weight = Integer.parseInt(matcher.group(2));

List<ItemStack> list = OreDictionary.getOres(oreDict);
for (ItemStack stack : list) {
if (stack != null && stack.getItem() instanceof ItemBlock) {
componentList.add(new MeteorParadigmComponent(stack, weight));
addList.add(new MeteorParadigmComponent(stack, weight));
success = true;
break;
}
}

} else {
// Legacy config
String oreDict = oreName;
int weight = Integer.parseInt(oreArray[++i]);
String oreDict = blockName;
int weight = Integer.parseInt(blockArray[++i]);

List<ItemStack> list = OreDictionary.getOres(oreDict);
for (ItemStack stack : list) {
if (stack != null && stack.getItem() instanceof ItemBlock) {
componentList.add(new MeteorParadigmComponent(stack, weight));
addList.add(new MeteorParadigmComponent(stack, weight));
success = true;
break;
}
}
}

if (!success) {
AlchemicalWizardry.logger.warn("Unable to add Meteor Paradigm \"" + oreName + "\"");
AlchemicalWizardry.logger.warn("Unable to add Meteor Paradigm \"" + blockName + "\"");
}
}
return addList;
}

public int getTotalMeteorWeight() {
int totalMeteorWeight = 0;
for (MeteorParadigmComponent mpc : componentList) {
totalMeteorWeight += mpc.getChance();
public int getTotalListWeight(List<MeteorParadigmComponent> blockList) {
int totalWeight = 0;
for (MeteorParadigmComponent mpc : blockList) {
totalWeight += mpc.getWeight();
}
return totalMeteorWeight;
return totalWeight;
}

public void createMeteorImpact(World world, int x, int y, int z, boolean[] flags) {
Expand All @@ -115,22 +124,41 @@ public void createMeteorImpact(World world, int x, int y, int z, boolean[] flags
}

int newRadius = radius;

int fillerChance = this.fillerChance;
if (hasOrbisTerrae) {
newRadius += 2;
fillerChance *= 1.12;
} else if (hasTerrae) {
newRadius += 1;
fillerChance *= 1.06;
}
if (fillerChance > 100) {
fillerChance = 100;
}

world.createExplosion(null, x, y, z, newRadius * 4, AlchemicalWizardry.doMeteorsDestroyBlocks);

float iceChance = hasCrystallos ? 1 : 0;
float soulChance = hasIncendium ? 1 : 0;
float obsidChance = hasTennebrae ? 1 : 0;
List<MeteorParadigmComponent> fillerList;

float totalChance = iceChance + soulChance + obsidChance;
if (hasCrystallos || hasIncendium || hasTennebrae) {
fillerList = new ArrayList<>();
if (hasCrystallos) {
fillerList.add(new MeteorParadigmComponent(new ItemStack(Blocks.ice), 180)); // 180 = 2^2 * 3^2 * 5
}
if (hasIncendium) {
fillerList.add(new MeteorParadigmComponent(new ItemStack(Blocks.netherrack), 60));
fillerList.add(new MeteorParadigmComponent(new ItemStack(Blocks.soul_sand), 60));
fillerList.add(new MeteorParadigmComponent(new ItemStack(Blocks.glowstone), 60));
}
if (hasTennebrae) {
fillerList.add(new MeteorParadigmComponent(new ItemStack(Blocks.obsidian), 180));
}
} else {
fillerList = this.fillerList;
}

int totalMeteorWeight = getTotalMeteorWeight();
int totalComponentWeight = getTotalListWeight(componentList);
int totalFillerWeight = getTotalListWeight(fillerList);

for (int i = -newRadius; i <= newRadius; i++) {
for (int j = -newRadius; j <= newRadius; j++) {
Expand All @@ -143,75 +171,30 @@ public void createMeteorImpact(World world, int x, int y, int z, boolean[] flags
continue;
}

int randNum = world.rand.nextInt(totalMeteorWeight);

boolean hasPlacedBlock = false;

for (MeteorParadigmComponent mpc : componentList) {
randNum -= mpc.getChance();

if (randNum < 0) {
ItemStack blockStack = mpc.getValidBlockParadigm();
if (blockStack != null && blockStack.getItem() instanceof ItemBlock) {
((ItemBlock) blockStack.getItem()).placeBlockAt(
blockStack,
null,
world,
x + i,
y + j,
z + k,
0,
0,
0,
0,
blockStack.getItemDamage());
if (AlchemicalWizardry.isGregTechLoaded)
setGTOresNaturalIfNeeded(world, x + i, y + j, z + k);
world.markBlockForUpdate(x + i, y + j, z + k);
hasPlacedBlock = true;
break;
}
// world.setBlock(x + i, y + j, z + k,
// Block.getBlockById(Item.getIdFromItem(blockStack.getItem())), blockStack.getItemDamage(),
// 3);
// hasPlacedBlock = true;
// break;
}
if (fillerChance <= 0 || world.rand.nextInt(100) >= fillerChance) {
setMeteorBlock(x + i, y + j, z + k, world, componentList, totalComponentWeight);
} else {
setMeteorBlock(x + i, y + j, z + k, world, fillerList, totalFillerWeight);
}
}
}
}
}

if (!hasPlacedBlock) {
float randChance = rand.nextFloat() * totalChance;

if (randChance < iceChance) {
world.setBlock(x + i, y + j, z + k, Blocks.ice, 0, 3);
} else {
randChance -= iceChance;

if (randChance < soulChance) {
switch (rand.nextInt(3)) {
case 0:
world.setBlock(x + i, y + j, z + k, Blocks.soul_sand, 0, 3);
break;
case 1:
world.setBlock(x + i, y + j, z + k, Blocks.glowstone, 0, 3);
break;
case 2:
world.setBlock(x + i, y + j, z + k, Blocks.netherrack, 0, 3);
break;
}
} else {
randChance -= soulChance;

if (randChance < obsidChance) {
world.setBlock(x + i, y + j, z + k, Blocks.obsidian, 0, 3);
} else {
randChance -= obsidChance;

world.setBlock(x + i, y + j, z + k, Blocks.stone, 0, 3);
}
}
}
}
private void setMeteorBlock(int x, int y, int z, World world, List<MeteorParadigmComponent> blockList,
int totalListWeight) {
int randNum = world.rand.nextInt(totalListWeight);
for (MeteorParadigmComponent mpc : blockList) {
randNum -= mpc.getWeight();

if (randNum < 0) {
ItemStack blockStack = mpc.getValidBlockParadigm();
if (blockStack != null && blockStack.getItem() instanceof ItemBlock) {
((ItemBlock) blockStack.getItem())
.placeBlockAt(blockStack, null, world, x, y, z, 0, 0, 0, 0, blockStack.getItemDamage());
if (AlchemicalWizardry.isGregTechLoaded) setGTOresNaturalIfNeeded(world, x, y, z);
world.markBlockForUpdate(x, y, z);
break;
}
}
}
Expand Down
Loading

0 comments on commit ba0a0e3

Please sign in to comment.