2023-01-13 15:25:15 +01:00
|
|
|
package net.anvilcraft.classiccasting.research;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2023-03-22 17:05:58 +01:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Set;
|
2023-01-13 15:25:15 +01:00
|
|
|
|
|
|
|
import net.anvilcraft.classiccasting.CCItems;
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.init.Items;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
import thaumcraft.api.aspects.Aspect;
|
|
|
|
import thaumcraft.api.aspects.AspectList;
|
|
|
|
import thaumcraft.api.research.ResearchCategories;
|
|
|
|
import thaumcraft.api.research.ResearchCategoryList;
|
|
|
|
import thaumcraft.api.research.ResearchItem;
|
|
|
|
import thaumcraft.common.config.ConfigItems;
|
|
|
|
import thaumcraft.common.lib.research.ResearchManager;
|
|
|
|
|
|
|
|
public class CCResearchManager {
|
2023-03-22 17:05:58 +01:00
|
|
|
|
|
|
|
public static Set<String> discoverable = new HashSet<>();
|
|
|
|
|
2023-01-13 15:25:15 +01:00
|
|
|
public static ItemStack createNote(ItemStack stack, String key) {
|
|
|
|
ResearchItem research = ResearchCategories.getResearch(key);
|
2023-01-13 16:28:16 +01:00
|
|
|
if (research == null)
|
|
|
|
return null;
|
2023-01-13 15:25:15 +01:00
|
|
|
if (stack.stackTagCompound == null) {
|
|
|
|
stack.setTagCompound(new NBTTagCompound());
|
|
|
|
}
|
|
|
|
if (!stack.stackTagCompound.hasKey("tcr")) {
|
|
|
|
stack.stackTagCompound.setTag("tcr", new NBTTagCompound());
|
|
|
|
}
|
|
|
|
NBTTagCompound var3 = (NBTTagCompound) stack.stackTagCompound.getTag("tcr");
|
|
|
|
var3.setString("project", key);
|
|
|
|
research.tags.writeToNBT(var3, "tags");
|
|
|
|
NBTTagCompound progress = new NBTTagCompound();
|
2023-01-13 16:28:16 +01:00
|
|
|
for (Aspect a : research.tags.getAspects()) {
|
2023-01-13 15:25:15 +01:00
|
|
|
progress.setInteger(a.getTag(), 0);
|
|
|
|
}
|
|
|
|
var3.setTag("progress", progress);
|
|
|
|
new AspectList().writeToNBT(var3, "failed");
|
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ResearchNoteData getData(ItemStack stack) {
|
|
|
|
ResearchNoteData var1 = new ResearchNoteData();
|
|
|
|
if (stack.stackTagCompound == null) {
|
|
|
|
return var1;
|
|
|
|
}
|
|
|
|
if (!stack.stackTagCompound.hasKey("tcr")) {
|
|
|
|
return var1;
|
|
|
|
}
|
|
|
|
NBTTagCompound var3 = stack.stackTagCompound.getCompoundTag("tcr");
|
2023-01-13 16:28:16 +01:00
|
|
|
if (!var3.hasKey("project") || !var3.hasKey("tags") || !var3.hasKey("progress")
|
|
|
|
|| !var3.hasKey("failed")) {
|
2023-01-13 15:25:15 +01:00
|
|
|
return var1;
|
|
|
|
}
|
2023-01-13 16:28:16 +01:00
|
|
|
|
2023-01-13 15:25:15 +01:00
|
|
|
var1.key = var3.getString("project");
|
|
|
|
var1.tags.readFromNBT(var3, "tags");
|
|
|
|
NBTTagCompound progress = var3.getCompoundTag("progress");
|
2023-01-13 16:28:16 +01:00
|
|
|
for (Aspect a : var1.tags.getAspects()) {
|
2023-01-13 15:25:15 +01:00
|
|
|
var1.progress.put(a, progress.getInteger(a.getTag()));
|
|
|
|
}
|
|
|
|
var1.failedTags.readFromNBT(var3, "failed");
|
|
|
|
// WTF?
|
|
|
|
/*
|
|
|
|
boolean busted = false;
|
|
|
|
for (Aspect tt : var1.tags.getAspects()) {
|
|
|
|
if (tt != null) continue;
|
|
|
|
busted = true;
|
|
|
|
break;
|
|
|
|
}
|
2023-01-13 16:28:16 +01:00
|
|
|
if (!busted && var1.tags.length != ResearchList.getResearchTags(var1.key).length)
|
|
|
|
{ busted = true;
|
2023-01-13 15:25:15 +01:00
|
|
|
}
|
|
|
|
if (busted) {
|
|
|
|
byte[] tt = var1.tags;
|
|
|
|
int[] tp = var1.progress;
|
|
|
|
var1.tags = ResearchList.getResearchTags(var1.key);
|
|
|
|
var1.progress = new int[ResearchList.getResearchTags(var1.key).length];
|
|
|
|
var1.failedTags = new byte[64];
|
|
|
|
for (int a = 0; a < var1.tags.length; ++a) {
|
|
|
|
for (int b = 0; b < tt.length; ++b) {
|
|
|
|
if (var1.tags[a] != tt[b]) continue;
|
|
|
|
var1.progress[a] = tp[b];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}*/
|
|
|
|
return var1;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void updateData(ItemStack stack, ResearchNoteData data) {
|
|
|
|
if (stack.stackTagCompound == null) {
|
|
|
|
stack.setTagCompound(new NBTTagCompound());
|
|
|
|
}
|
|
|
|
if (!stack.stackTagCompound.hasKey("tcr")) {
|
|
|
|
stack.stackTagCompound.setTag("tcr", new NBTTagCompound());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NBTTagCompound var3 = (NBTTagCompound) stack.stackTagCompound.getTag("tcr");
|
|
|
|
|
|
|
|
var3.setString("project", data.key);
|
|
|
|
data.tags.writeToNBT(var3, "tags");
|
|
|
|
NBTTagCompound progress = new NBTTagCompound();
|
|
|
|
for (Aspect a : data.tags.getAspects()) {
|
|
|
|
if (data.progress.containsKey(a)) {
|
|
|
|
progress.setInteger(a.getTag(), data.progress.get(a));
|
|
|
|
} else {
|
|
|
|
progress.setInteger(a.getTag(), 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var3.setTag("progress", progress);
|
|
|
|
data.failedTags.writeToNBT(var3, "failed");
|
|
|
|
}
|
|
|
|
|
2023-01-13 16:28:16 +01:00
|
|
|
public static void
|
|
|
|
createResearchNoteForTable(ClassicResearchTableExtension table, String key) {
|
|
|
|
if (table.getStackInSlot(5) == null && table.getStackInSlot(6) != null
|
|
|
|
&& table.getStackInSlot(6).isItemEqual(new ItemStack(Items.paper))) {
|
2023-01-13 15:25:15 +01:00
|
|
|
table.decrStackSize(6, 1);
|
|
|
|
ResearchItem item = ResearchCategories.getResearch(key);
|
|
|
|
//int md = ResearchList.getResearchPrimaryTag(key);
|
|
|
|
int md = 0;
|
2023-01-13 16:28:16 +01:00
|
|
|
table.setInventorySlotContents(
|
|
|
|
5,
|
|
|
|
CCResearchManager.createNote(
|
|
|
|
new ItemStack(CCItems.researchNotes, 1, md), key
|
|
|
|
)
|
|
|
|
);
|
2023-01-13 15:25:15 +01:00
|
|
|
table.markDirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-13 16:28:16 +01:00
|
|
|
public static String
|
|
|
|
findMatchingResearch(EntityPlayer player, Aspect[] tags, short[] tagAmounts) {
|
2023-01-13 15:25:15 +01:00
|
|
|
int bestMatchNum = 0;
|
|
|
|
String bestMatch = null;
|
|
|
|
for (ResearchCategoryList cat : ResearchCategories.researchCategories.values()) {
|
|
|
|
for (ResearchItem research : cat.research.values()) {
|
2023-01-13 16:28:16 +01:00
|
|
|
if (research.isStub() || research.isLost()
|
|
|
|
|| ResearchManager.isResearchComplete(
|
|
|
|
player.getDisplayName(), research.key
|
|
|
|
)
|
|
|
|
|| !ResearchManager.doesPlayerHaveRequisites(
|
|
|
|
player.getDisplayName(), research.key
|
2023-03-22 17:05:58 +01:00
|
|
|
)
|
|
|
|
|| !isDiscoverable(research.key))
|
2023-01-13 16:28:16 +01:00
|
|
|
continue;
|
2023-01-13 15:25:15 +01:00
|
|
|
int match = 0;
|
|
|
|
for (int q = 0; q < 5; ++q) {
|
2023-01-13 16:28:16 +01:00
|
|
|
if (tags[q] == null || tagAmounts[q] <= 0
|
|
|
|
|| research.tags.getAmount(tags[q]) <= 0)
|
|
|
|
continue;
|
2023-01-13 15:25:15 +01:00
|
|
|
++match;
|
|
|
|
}
|
2023-01-13 16:28:16 +01:00
|
|
|
if (match <= 0 || match <= bestMatchNum)
|
|
|
|
continue;
|
2023-01-13 15:25:15 +01:00
|
|
|
bestMatchNum = match;
|
|
|
|
bestMatch = research.key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bestMatch;
|
|
|
|
}
|
|
|
|
|
2023-01-13 16:28:16 +01:00
|
|
|
public static boolean progressTableResearch(
|
|
|
|
World world,
|
|
|
|
EntityPlayer researcher,
|
|
|
|
ClassicResearchTableExtension table,
|
|
|
|
ItemStack note,
|
|
|
|
int baseChance,
|
|
|
|
int baseLoss,
|
|
|
|
Aspect[] inTags,
|
|
|
|
short[] inTagAmounts
|
|
|
|
) {
|
2023-01-13 15:25:15 +01:00
|
|
|
AspectList tags;
|
|
|
|
String key;
|
|
|
|
boolean progressed = false;
|
|
|
|
if (baseLoss <= 0) {
|
|
|
|
baseLoss = 1;
|
|
|
|
}
|
2023-01-13 16:28:16 +01:00
|
|
|
if (note.stackTagCompound == null
|
|
|
|
&& (key = CCResearchManager.findLostResearch(researcher)) != null) {
|
2023-01-13 15:25:15 +01:00
|
|
|
note = CCResearchManager.createNote(note, key);
|
|
|
|
}
|
|
|
|
ResearchNoteData data = CCResearchManager.getData(note);
|
|
|
|
ResearchItem research = ResearchCategories.getResearch(data.key);
|
2023-01-13 16:28:16 +01:00
|
|
|
if (research == null)
|
|
|
|
return false;
|
2023-01-13 15:25:15 +01:00
|
|
|
if ((tags = research.tags.copy()) != null) {
|
|
|
|
boolean found;
|
|
|
|
for (Aspect tag : inTags) {
|
2023-01-13 16:28:16 +01:00
|
|
|
if (tag == null)
|
|
|
|
continue;
|
2023-01-13 15:25:15 +01:00
|
|
|
found = false;
|
|
|
|
for (Aspect entry : tags.getAspects()) {
|
2023-01-13 16:28:16 +01:00
|
|
|
if (entry != tag)
|
|
|
|
continue;
|
2023-01-13 15:25:15 +01:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
2023-01-13 16:28:16 +01:00
|
|
|
if (found || data.failedTags.getAmount(tag) >= 100)
|
|
|
|
continue;
|
|
|
|
data.failedTags.add(
|
|
|
|
tag, Math.round((float) (2 + world.rand.nextInt(baseLoss)) / 10.0f)
|
|
|
|
);
|
2023-01-13 15:25:15 +01:00
|
|
|
}
|
|
|
|
for (Aspect tag : tags.getAspects()) {
|
|
|
|
found = false;
|
|
|
|
int entry;
|
|
|
|
for (entry = 0; entry < inTags.length; ++entry) {
|
2023-01-13 16:28:16 +01:00
|
|
|
if (inTags[entry] != tag)
|
|
|
|
continue;
|
2023-01-13 15:25:15 +01:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
2023-01-13 16:28:16 +01:00
|
|
|
if (!found)
|
|
|
|
continue;
|
|
|
|
float chance = (float) baseChance / 100.0f;
|
2023-01-13 15:25:15 +01:00
|
|
|
int tries = CCResearchManager.researchIterations(inTagAmounts[entry]);
|
|
|
|
if (tries == 0) {
|
|
|
|
tries = 1;
|
2023-01-13 16:28:16 +01:00
|
|
|
chance = (float) baseChance / 2.0f;
|
2023-01-13 15:25:15 +01:00
|
|
|
}
|
|
|
|
for (int a = 0; a < tries; ++a) {
|
2023-01-13 16:28:16 +01:00
|
|
|
if (!(world.rand.nextFloat() <= chance))
|
2023-01-13 15:25:15 +01:00
|
|
|
continue;
|
2023-01-13 16:28:16 +01:00
|
|
|
if (data.tags.getAmount(tag) <= 0
|
|
|
|
|| data.progress.get(tag) >= research.tags.getAmount(tag))
|
|
|
|
break;
|
|
|
|
data.progress.put(tag, data.progress.get(tag) + 1);
|
|
|
|
chance *= 0.9f;
|
|
|
|
progressed = true;
|
|
|
|
continue;
|
2023-01-13 15:25:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
CCResearchManager.updateData(note, data);
|
|
|
|
} else {
|
2023-01-13 16:28:16 +01:00
|
|
|
table.contents[5]
|
|
|
|
= new ItemStack(ConfigItems.itemResource, 7 + world.rand.nextInt(3), 9);
|
2023-01-13 15:25:15 +01:00
|
|
|
}
|
|
|
|
return progressed;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static int researchIterations(int amount) {
|
|
|
|
return 1 + amount / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String findLostResearch(EntityPlayer player) {
|
|
|
|
String bestMatch = null;
|
|
|
|
ArrayList<String> choices = new ArrayList<String>();
|
2023-01-13 16:28:16 +01:00
|
|
|
for (ResearchCategoryList cat : ResearchCategories.researchCategories.values()) {
|
2023-01-13 15:25:15 +01:00
|
|
|
for (ResearchItem research : cat.research.values()) {
|
2023-01-13 16:28:16 +01:00
|
|
|
if (research.isStub() || !research.isLost()
|
|
|
|
|| ResearchManager.isResearchComplete(
|
|
|
|
player.getDisplayName(), research.key
|
|
|
|
)
|
|
|
|
|| !ResearchManager.doesPlayerHaveRequisites(
|
|
|
|
player.getDisplayName(), research.key
|
|
|
|
))
|
|
|
|
continue;
|
2023-01-13 15:25:15 +01:00
|
|
|
choices.add(research.key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (choices.size() > 0) {
|
2023-01-13 16:28:16 +01:00
|
|
|
bestMatch
|
|
|
|
= (String) choices.get(player.worldObj.rand.nextInt(choices.size()));
|
2023-01-13 15:25:15 +01:00
|
|
|
}
|
|
|
|
return bestMatch;
|
|
|
|
}
|
2023-03-22 17:05:58 +01:00
|
|
|
|
|
|
|
public static boolean isDiscoverable(String key) {
|
|
|
|
if (discoverable.contains(key)) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
ResearchItem item = ResearchCategories.getResearch(key);
|
|
|
|
return item != null && (item.category == "ALCHEMY" || item.category == "ARTIFICE" || item.category == "GOLEMANCY");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void addDiscoverableResearch(ResearchItem research) {
|
|
|
|
discoverable.add(research.key);
|
|
|
|
}
|
2023-01-13 15:25:15 +01:00
|
|
|
}
|