From 49c41c1ef455133d72c8d901b656de51ae12a61c Mon Sep 17 00:00:00 2001 From: Timo Ley Date: Sat, 14 Jan 2023 17:06:03 +0100 Subject: [PATCH] feat: implement old aspect calculation --- build.gradle | 2 +- .../auracore/crafting/AspectCalculation.java | 194 ++++++++++++++++++ .../crafting/AuracoreCraftingManager.java | 8 +- .../MixinThaumcraftCraftingManager.java | 85 ++++++++ src/main/resources/auracore.mixins.json | 3 +- 5 files changed, 289 insertions(+), 3 deletions(-) create mode 100644 src/main/java/dev/tilera/auracore/crafting/AspectCalculation.java create mode 100644 src/main/java/dev/tilera/auracore/mixins/MixinThaumcraftCraftingManager.java diff --git a/build.gradle b/build.gradle index 18ee546..a9be87f 100644 --- a/build.gradle +++ b/build.gradle @@ -24,7 +24,7 @@ apply from: './gradle/scripts/mixins.gradle' sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 -version = "1.6.1" +version = "1.7.0" group= "dev.tilera" archivesBaseName = "auracore" diff --git a/src/main/java/dev/tilera/auracore/crafting/AspectCalculation.java b/src/main/java/dev/tilera/auracore/crafting/AspectCalculation.java new file mode 100644 index 0000000..6ad413f --- /dev/null +++ b/src/main/java/dev/tilera/auracore/crafting/AspectCalculation.java @@ -0,0 +1,194 @@ +package dev.tilera.auracore.crafting; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; + +import dev.tilera.auracore.api.AuracoreRecipes; +import dev.tilera.auracore.api.crafting.CrucibleRecipe; +import dev.tilera.auracore.api.crafting.IInfusionRecipe; +import dev.tilera.auracore.api.crafting.ShapedInfusionCraftingRecipe; +import dev.tilera.auracore.api.crafting.ShapelessInfusionCraftingRecipe; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import thaumcraft.api.ThaumcraftApi; +import thaumcraft.api.aspects.Aspect; +import thaumcraft.api.aspects.AspectList; +import thaumcraft.api.crafting.IArcaneRecipe; +import thaumcraft.api.crafting.ShapedArcaneRecipe; +import thaumcraft.api.crafting.ShapelessArcaneRecipe; +import thaumcraft.common.lib.crafting.ThaumcraftCraftingManager; + +public class AspectCalculation { + + public static AspectList generateTagsFromInfusionRecipes(Item item, int meta, ArrayList history) { + AspectList ret = null; + int value = 0; + List recipeList = AuracoreRecipes.getInfusionRecipes(); + for (int q = 0; q < recipeList.size(); ++q) { + int idS; + IInfusionRecipe recipe = recipeList.get(q); + int idR = recipe.getRecipeOutput().getItemDamage() < 0 ? 0 : recipe.getRecipeOutput().getItemDamage(); + int n = idS = meta < 0 ? 0 : meta; + if (recipe.getRecipeOutput().getItem() != item || idR != idS) continue; + HashMap, ItemStack> ingredients = new HashMap, ItemStack>(); + AspectList ph = new AspectList(); + int cval = 0; + try { + if (recipe instanceof ShapedInfusionCraftingRecipe) { + int width = ((ShapedInfusionCraftingRecipe)recipe).recipeWidth; + int height = ((ShapedInfusionCraftingRecipe)recipe).recipeHeight; + ItemStack[] items = ((ShapedInfusionCraftingRecipe)recipe).recipeItems; + for (int i = 0; i < width && i < 3; ++i) { + for (int j = 0; j < height && j < 3; ++j) { + if (items[i + j * width] == null) continue; + items[i + j * width].stackSize = 1; + if (ingredients.containsKey(Arrays.asList(Item.getIdFromItem(items[i + j * width].getItem()), items[i + j * width].getItemDamage()))) { + ItemStack is = (ItemStack)ingredients.get(Arrays.asList(items[i + j * width].getItem(), items[i + j * width].getItemDamage())); + ++is.stackSize; + ingredients.put(Arrays.asList(Item.getIdFromItem(items[i + j * width].getItem()), items[i + j * width].getItemDamage()), is); + continue; + } + ingredients.put(Arrays.asList(Item.getIdFromItem(items[i + j * width].getItem()), items[i + j * width].getItemDamage()), items[i + j * width]); + } + } + } else { + List items = ((ShapelessInfusionCraftingRecipe)recipe).recipeItems; + for (int i = 0; i < items.size() && i < 9; ++i) { + if (items.get(i) == null) continue; + ((ItemStack)items.get((int)i)).stackSize = 1; + if (ingredients.containsKey(Arrays.asList(((ItemStack)items.get((int)i)).getItem(), ((ItemStack)items.get(i)).getItemDamage()))) { + ItemStack is = (ItemStack)ingredients.get(Arrays.asList(Item.getIdFromItem(((ItemStack)items.get((int)i)).getItem()), ((ItemStack)items.get(i)).getItemDamage())); + ++is.stackSize; + ingredients.put(Arrays.asList(Item.getIdFromItem(((ItemStack)items.get((int)i)).getItem()), ((ItemStack)items.get(i)).getItemDamage()), is); + continue; + } + ingredients.put(Arrays.asList(Item.getIdFromItem(((ItemStack)items.get((int)i)).getItem()), ((ItemStack)items.get(i)).getItemDamage()), items.get(i)); + } + } + Collection ings = ingredients.values(); + for (ItemStack is : ings) { + AspectList obj = ThaumcraftCraftingManager.generateTags(is.getItem(), is.getItemDamage(), history); + AspectList objC = null; + if (is.getItem().getContainerItem() != null) { + objC = ThaumcraftCraftingManager.generateTags(is.getItem().getContainerItem(), -1, history); + } + if (obj == null) continue; + for (Aspect as : obj.getAspects()) { + float amnt; + if (objC != null && objC.getAmount(as) > 0 || !((amnt = (float)(obj.getAmount(as) * is.stackSize) / (float)recipe.getRecipeOutput().stackSize) > 0.5f)) continue; + float tmod = 0.8f; + ph.add(as, Math.max(Math.round(amnt * tmod), 1)); + cval += Math.max(Math.round(amnt * tmod), 1); + } + } + ph.add(Aspect.MAGIC, Math.round((float)recipe.getCost() / 10.0f / (float)recipe.getRecipeOutput().stackSize)); + for (Aspect tag : recipe.getAspects().getAspects()) { + ph.add(tag, Math.round((float)recipe.getAspects().getAmount(tag) / (float)recipe.getRecipeOutput().stackSize)); + } + if (cval < value) continue; + ret = ph; + value = cval; + continue; + } + catch (Exception e) { + e.printStackTrace(); + } + } + return ret; + } + + public static AspectList generateTagsFromCrucibleRecipes(Item item, int meta, ArrayList history) { + CrucibleRecipe cr = AuracoreRecipes.getCrucibleRecipe(new ItemStack(item, 1, meta)); + if (cr != null) { + AspectList ot = new AspectList(); + int ss = cr.recipeOutput.stackSize; + ot.add(Aspect.MAGIC, Math.round((float)cr.cost / 10.0f)); + for (Aspect tt : cr.aspects.getAspects()) { + int amt = cr.aspects.getAmount(tt) / ss; + ot.add(tt, amt); + } + return ot; + } + return null; + } + + public static AspectList generateTagsFromArcaneRecipes(Item item, int meta, ArrayList history) { + AspectList ret = null; + int value = 0; + List recipeList = ThaumcraftApi.getCraftingRecipes(); + for (int q = 0; q < recipeList.size(); ++q) { + int idS; + if (!(recipeList.get(q) instanceof IArcaneRecipe)) continue; + IArcaneRecipe recipe = (IArcaneRecipe)recipeList.get(q); + if (!(recipe instanceof ShapedArcaneRecipe || recipe instanceof ShapelessArcaneRecipe)) continue; + int idR = recipe.getRecipeOutput().getItemDamage() < 0 ? 0 : recipe.getRecipeOutput().getItemDamage(); + int n = idS = meta < 0 ? 0 : meta; + if (recipe.getRecipeOutput().getItem() != item || idR != idS) continue; + HashMap, ItemStack> ingredients = new HashMap, ItemStack>(); + AspectList ph = new AspectList(); + int cval = 0; + try { + if (recipe instanceof ShapedArcaneRecipe) { + int width = ((ShapedArcaneRecipe)recipe).width; + int height = ((ShapedArcaneRecipe)recipe).width; + ItemStack[] items = (ItemStack[]) ((ShapedArcaneRecipe)recipe).input; + for (int i = 0; i < width && i < 3; ++i) { + for (int j = 0; j < height && j < 3; ++j) { + if (items[i + j * width] == null) continue; + items[i + j * width].stackSize = 1; + if (ingredients.containsKey(Arrays.asList(Item.getIdFromItem(items[i + j * width].getItem()), items[i + j * width].getItemDamage()))) { + ItemStack is = (ItemStack)ingredients.get(Arrays.asList(Item.getIdFromItem(items[i + j * width].getItem()), items[i + j * width].getItemDamage())); + ++is.stackSize; + ingredients.put(Arrays.asList(Item.getIdFromItem(items[i + j * width].getItem()), items[i + j * width].getItemDamage()), is); + continue; + } + ingredients.put(Arrays.asList(Item.getIdFromItem(items[i + j * width].getItem()), items[i + j * width].getItemDamage()), items[i + j * width]); + } + } + } else { + List items = ((ShapelessArcaneRecipe)recipe).getInput(); + for (int i = 0; i < items.size() && i < 9; ++i) { + if (items.get(i) == null) continue; + ((ItemStack)items.get((int)i)).stackSize = 1; + if (ingredients.containsKey(Arrays.asList(Item.getIdFromItem(((ItemStack)items.get((int)i)).getItem()), ((ItemStack)items.get(i)).getItemDamage()))) { + ItemStack is = (ItemStack)ingredients.get(Arrays.asList(Item.getIdFromItem(((ItemStack)items.get((int)i)).getItem()), ((ItemStack)items.get(i)).getItemDamage())); + ++is.stackSize; + ingredients.put(Arrays.asList(Item.getIdFromItem(((ItemStack)items.get((int)i)).getItem()), ((ItemStack)items.get(i)).getItemDamage()), is); + continue; + } + ingredients.put(Arrays.asList(Item.getIdFromItem(((ItemStack)items.get((int)i)).getItem()), ((ItemStack)items.get(i)).getItemDamage()), items.get(i)); + } + } + Collection ings = ingredients.values(); + for (ItemStack is : ings) { + AspectList obj = ThaumcraftCraftingManager.generateTags(is.getItem(), is.getItemDamage(), history); + AspectList objC = null; + if (is.getItem().getContainerItem() != null) { + objC = ThaumcraftCraftingManager.generateTags(is.getItem().getContainerItem(), -1, history); + } + if (obj == null) continue; + for (Aspect as : obj.getAspects()) { + float amnt; + if (objC != null && objC.getAmount(as) > 0 || !((amnt = (float)(obj.getAmount(as) * is.stackSize) / (float)recipe.getRecipeOutput().stackSize) > 0.5f)) continue; + float tmod = 0.8f; + ph.add(as, Math.max(Math.round(amnt * tmod), 1)); + cval += Math.max(Math.round(amnt * tmod), 1); + } + } + ph.add(Aspect.MAGIC, Math.round((float)AuracoreCraftingManager.getArcaneRecipeVisCost(recipe, null) / 10.0f / (float)recipe.getRecipeOutput().stackSize)); + if (cval < value) continue; + ret = ph; + value = cval; + continue; + } + catch (Exception e) { + e.printStackTrace(); + } + } + return ret; + } + +} diff --git a/src/main/java/dev/tilera/auracore/crafting/AuracoreCraftingManager.java b/src/main/java/dev/tilera/auracore/crafting/AuracoreCraftingManager.java index f1318dd..afa2b12 100644 --- a/src/main/java/dev/tilera/auracore/crafting/AuracoreCraftingManager.java +++ b/src/main/java/dev/tilera/auracore/crafting/AuracoreCraftingManager.java @@ -41,7 +41,13 @@ public class AuracoreCraftingManager { public static int getArcaneRecipeVisCost(IArcaneRecipe recipe, TileMagicWorkbench awb) { if (recipe == null) return 0; int sum = 0; - AspectList aspects = recipe.getAspects(awb); + AspectList aspects = recipe.getAspects(); + if (awb != null) { + aspects = recipe.getAspects(awb); + } + if (aspects == null) { + aspects = new AspectList(); + } for (Aspect aspect : aspects.getAspects()) { if (aspect != null) sum += aspects.getAmount(aspect); } diff --git a/src/main/java/dev/tilera/auracore/mixins/MixinThaumcraftCraftingManager.java b/src/main/java/dev/tilera/auracore/mixins/MixinThaumcraftCraftingManager.java new file mode 100644 index 0000000..ea8f4b8 --- /dev/null +++ b/src/main/java/dev/tilera/auracore/mixins/MixinThaumcraftCraftingManager.java @@ -0,0 +1,85 @@ +package dev.tilera.auracore.mixins; + +import java.util.ArrayList; +import java.util.List; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Overwrite; +import org.spongepowered.asm.mixin.gen.Invoker; + +import dev.tilera.auracore.api.Aspects; +import dev.tilera.auracore.crafting.AspectCalculation; +import net.minecraft.item.Item; +import thaumcraft.api.aspects.Aspect; +import thaumcraft.api.aspects.AspectList; +import thaumcraft.common.lib.crafting.ThaumcraftCraftingManager; + +@Mixin(ThaumcraftCraftingManager.class) +public abstract class MixinThaumcraftCraftingManager { + + @Invoker("generateTagsFromCrucibleRecipes") + public static AspectList invokeGenerateTagsFromCrucibleRecipes(Item item, int meta, ArrayList history) { + throw new RuntimeException(); + } + + @Invoker("generateTagsFromArcaneRecipes") + public static AspectList invokeGenerateTagsFromArcaneRecipes(Item item, int meta, ArrayList history) { + throw new RuntimeException(); + } + + @Invoker("generateTagsFromInfusionRecipes") + public static AspectList invokeGenerateTagsFromInfusionRecipes(Item item, int meta, ArrayList history) { + throw new RuntimeException(); + } + + @Invoker("generateTagsFromCraftingRecipes") + public static AspectList invokeGenerateTagsFromCraftingRecipes(Item item, int meta, ArrayList history) { + throw new RuntimeException(); + } + + /** + * @author tilera + * @reason recipe tag generation + */ + @Overwrite(remap = false) + private static AspectList generateTagsFromRecipes(Item item, int meta, ArrayList history) { + AspectList ret = null; + ret = AspectCalculation.generateTagsFromCrucibleRecipes(item, meta, history); + if (ret != null) { + return ret; + } + ret = invokeGenerateTagsFromCrucibleRecipes(item, meta, history); + if (ret != null) { + return ret; + } + ret = AspectCalculation.generateTagsFromArcaneRecipes(item, meta, history); + if (ret != null) { + return ret; + } + ret = invokeGenerateTagsFromArcaneRecipes(item, meta, history); + if (ret != null) { + return ret; + } + ret = AspectCalculation.generateTagsFromInfusionRecipes(item, meta, history); + if (ret != null) { + return ret; + } + ret = invokeGenerateTagsFromInfusionRecipes(item, meta, history); + if (ret != null) { + return ret; + } + ret = invokeGenerateTagsFromCraftingRecipes(item, meta, history); + + if (ret != null) { + int vis = ret.getAmount(Aspects.VIS); + if (vis > 0) { + ret.remove(Aspects.VIS); + int magic = Math.round((float)vis / 10.0f); + ret.add(Aspect.MAGIC, magic); + } + } + + return ret; + } + +} diff --git a/src/main/resources/auracore.mixins.json b/src/main/resources/auracore.mixins.json index 0e6fa79..0f765ef 100644 --- a/src/main/resources/auracore.mixins.json +++ b/src/main/resources/auracore.mixins.json @@ -20,7 +20,8 @@ "MixinEventHandlerEntity", "MixinThaumcraftWorldGenerator", "MixinPlayerKnowledge", - "MixinConfigAspects" + "MixinConfigAspects", + "MixinThaumcraftCraftingManager" ], "client": [ "MixinBlockCustomOreRenderer",