|
| 1 | +package dev.overgrown.thaumaturge.block.vessel; |
| 2 | + |
| 3 | +import dev.overgrown.thaumaturge.util.AspectMap; |
| 4 | +import net.minecraft.util.Identifier; |
| 5 | +import net.minecraft.util.math.random.Random; |
| 6 | + |
| 7 | +import java.nio.ByteBuffer; |
| 8 | +import java.security.MessageDigest; |
| 9 | +import java.security.NoSuchAlgorithmException; |
| 10 | +import java.util.*; |
| 11 | + |
| 12 | +public class AspectReactionHolder { |
| 13 | + |
| 14 | + private static final int MIN_TEMP = -2; |
| 15 | + private static final int MAX_TEMP = 4; |
| 16 | + |
| 17 | + private static final MessageDigest digest; |
| 18 | + |
| 19 | + static { |
| 20 | + try { |
| 21 | + digest = MessageDigest.getInstance("SHA-256"); |
| 22 | + } catch (NoSuchAlgorithmException e) { |
| 23 | + throw new RuntimeException("Thaumaturge: Could not Access Required SHA-256 Digest"); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + private Map<Integer,Map<Identifier, List<AspectReaction>>> reactionMap; |
| 28 | + |
| 29 | + public AspectReactionHolder(long seed) { |
| 30 | + List<AspectReactionPrimitive> primitiveList = AspectReactionPrimitiveManager.getPrimitiveList(); |
| 31 | + reactionMap = new HashMap<>(); |
| 32 | + |
| 33 | + for (AspectReactionPrimitive primitive: primitiveList) { |
| 34 | + |
| 35 | + // Get a Random Object which Seeds depends upon world seed and primary aspects in a reaction |
| 36 | + byte[] inputA = primitive.inputA().toString().getBytes(); |
| 37 | + byte[] inputB = primitive.inputB().toString().getBytes(); |
| 38 | + byte[] result = primitive.result().toString().getBytes(); |
| 39 | + ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES + inputA.length + inputB.length + result.length); |
| 40 | + buffer.putLong(seed); |
| 41 | + buffer.put(inputA); |
| 42 | + buffer.put(inputB); |
| 43 | + buffer.put(result); |
| 44 | + digest.reset(); |
| 45 | + byte[] hashResult = digest.digest(buffer.array()); |
| 46 | + long randomSeed = ((long) hashResult[0]) << 56 | |
| 47 | + ((long) hashResult[1]) << 48 | |
| 48 | + ((long) hashResult[2]) << 40 | |
| 49 | + ((long) hashResult[3]) << 32 | |
| 50 | + ((long) hashResult[4]) << 24 | |
| 51 | + ((long) hashResult[5]) << 16 | |
| 52 | + ((long) hashResult[6]) << 8 | |
| 53 | + ((long) hashResult[7]); |
| 54 | + Random random = Random.create(randomSeed); |
| 55 | + |
| 56 | + int reactionTemp; |
| 57 | + boolean forwardReactionTrendsTowardsColder = primitive.frTowardsColder(); |
| 58 | + |
| 59 | + if (primitive.FrBegin() == primitive.FrEnd()) { |
| 60 | + reactionTemp = primitive.FrEnd(); |
| 61 | + } else { |
| 62 | + reactionTemp = random.nextBetweenExclusive(primitive.FrBegin(), primitive.FrEnd()); |
| 63 | + } |
| 64 | + |
| 65 | + if (primitive.reactionDirectionReversible()) { |
| 66 | + forwardReactionTrendsTowardsColder = random.nextBoolean(); |
| 67 | + } |
| 68 | + |
| 69 | + Identifier catalyst = null; |
| 70 | + |
| 71 | + if (!primitive.catalysts().isEmpty()) { |
| 72 | + if (random.nextFloat() <= primitive.catalystChance()) { |
| 73 | + catalyst = primitive.catalysts().get(random.nextInt(primitive.catalysts().size())); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + AspectReaction forwardImplementation = new AspectReaction( |
| 78 | + Set.of(primitive.inputA(), primitive.inputB()), Set.of(primitive.result())); |
| 79 | + AspectReaction backwardImplementation = new AspectReaction( |
| 80 | + Set.of(primitive.result()), Set.of(primitive.inputA(), primitive.inputB())); |
| 81 | + |
| 82 | + // Iterate over all temps form low to the mid |
| 83 | + for (int i = MIN_TEMP; i <= reactionTemp; i++) { |
| 84 | + // Access or create a Map of Reactions that can occur at a temperature |
| 85 | + Map<Identifier, List<AspectReaction>> reactionMapAtTemp; |
| 86 | + if (reactionMap.containsKey(i)) { |
| 87 | + reactionMapAtTemp = reactionMap.get(i); |
| 88 | + } else { |
| 89 | + reactionMapAtTemp = new HashMap<>(); |
| 90 | + reactionMap.put(i, reactionMapAtTemp); |
| 91 | + } |
| 92 | + |
| 93 | + Identifier key = catalyst == null ? |
| 94 | + (forwardReactionTrendsTowardsColder ? primitive.inputA() : primitive.result()) : catalyst; |
| 95 | + |
| 96 | + // Access or Create a List of Reactions for a Aspect at a specific Temperature |
| 97 | + List<AspectReaction> aspectReactionsForAspectAtTemp; |
| 98 | + if (reactionMapAtTemp.containsKey(key)) { |
| 99 | + aspectReactionsForAspectAtTemp = reactionMapAtTemp.get(key); |
| 100 | + } else { |
| 101 | + aspectReactionsForAspectAtTemp = new ArrayList<>(); |
| 102 | + reactionMapAtTemp.put(key, aspectReactionsForAspectAtTemp); |
| 103 | + } |
| 104 | + aspectReactionsForAspectAtTemp.add(forwardReactionTrendsTowardsColder ? forwardImplementation : backwardImplementation); |
| 105 | + } |
| 106 | + |
| 107 | + // Iterate over all temps from high to end |
| 108 | + for (int i = reactionTemp + 1; i <= MAX_TEMP; i++) { |
| 109 | + // Access or create a Map of Reactions that can occur at a temperature |
| 110 | + Map<Identifier, List<AspectReaction>> reactionMapAtTemp; |
| 111 | + if (reactionMap.containsKey(i)) { |
| 112 | + reactionMapAtTemp = reactionMap.get(i); |
| 113 | + } else { |
| 114 | + reactionMapAtTemp = new HashMap<>(); |
| 115 | + reactionMap.put(i, reactionMapAtTemp); |
| 116 | + } |
| 117 | + |
| 118 | + Identifier key = catalyst == null ? |
| 119 | + (forwardReactionTrendsTowardsColder ? primitive.result() : primitive.inputA()) : catalyst; |
| 120 | + |
| 121 | + // Access or Create a List of Reactions for a Aspect at a specific Temperature |
| 122 | + List<AspectReaction> aspectReactionsForAspectAtTemp; |
| 123 | + if (reactionMapAtTemp.containsKey(key)) { |
| 124 | + aspectReactionsForAspectAtTemp = reactionMapAtTemp.get(key); |
| 125 | + } else { |
| 126 | + aspectReactionsForAspectAtTemp = new ArrayList<>(); |
| 127 | + reactionMapAtTemp.put(key, aspectReactionsForAspectAtTemp); |
| 128 | + } |
| 129 | + aspectReactionsForAspectAtTemp.add(forwardReactionTrendsTowardsColder ? backwardImplementation : forwardImplementation); |
| 130 | + } |
| 131 | + |
| 132 | + |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + public List<AspectReaction> getPossibleReactions(int temp, AspectMap map) { |
| 137 | + List<AspectReaction> list = new ArrayList<>(); |
| 138 | + if (reactionMap.containsKey(temp)) { |
| 139 | + var mapAtTemp = reactionMap.get(temp); |
| 140 | + // Query valid Reaction Implementation for all Aspects |
| 141 | + for (Identifier aspect : map.getAspects()) { |
| 142 | + if (map.getAspectLevel(aspect) > 0 && mapAtTemp.containsKey(aspect)) { |
| 143 | + List<AspectReaction> listPerAspectAtTemp = mapAtTemp.get(aspect); |
| 144 | + for (AspectReaction implementation : listPerAspectAtTemp) { |
| 145 | + boolean containsAllInputs = true; |
| 146 | + for (Identifier inputAspect : implementation.input) { |
| 147 | + if (map.getAspectLevel(inputAspect) <= 0) { |
| 148 | + containsAllInputs = false; |
| 149 | + break; |
| 150 | + } |
| 151 | + } |
| 152 | + if (containsAllInputs) { |
| 153 | + list.add(implementation); |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + return list; |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public String toString() { |
| 164 | + StringBuilder builder = new StringBuilder(); |
| 165 | + builder.append("{\n"); |
| 166 | + for (var temperatures : reactionMap.keySet()) { |
| 167 | + builder.append("\tTemp: ").append(temperatures).append(" {\n"); |
| 168 | + var mapAtTemperature = reactionMap.get(temperatures); |
| 169 | + for (var aspect : mapAtTemperature.keySet()) { |
| 170 | + builder.append("\t\tAspect: ").append(aspect).append(" [\n"); |
| 171 | + var reactionList = mapAtTemperature.get(aspect); |
| 172 | + for (var reaction : reactionList) { |
| 173 | + builder.append("\t\t\t{\n") |
| 174 | + .append("\t\t\t\t Input:").append(reaction.input).append("\n") |
| 175 | + .append("\t\t\t\t Output:").append(reaction.output).append("\n") |
| 176 | + .append("\t\t\t}\n"); |
| 177 | + } |
| 178 | + builder.append("\t\t]"); |
| 179 | + } |
| 180 | + builder.append("\t}\n"); |
| 181 | + } |
| 182 | + |
| 183 | + builder.append("}"); |
| 184 | + return builder.toString(); |
| 185 | + } |
| 186 | + |
| 187 | + public interface Provider { |
| 188 | + AspectReactionHolder thaumaturge$getAspectReactionHolder(); |
| 189 | + } |
| 190 | +} |
0 commit comments