Skip to content

Commit

Permalink
refactor moar, increase the default max op count since it is now fast…
Browse files Browse the repository at this point in the history
… enough for that to be fine.
  • Loading branch information
Talia-12 committed Feb 6, 2024
1 parent 59b6f10 commit 9a87439
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class CastingVM(var image: CastingImage, val env: CastingEnvironment) {
*/
@Throws(MishapTooManyCloseParens::class)
private fun handleParentheses(iota: Iota): Pair<CastingImage, ResolvedPatternType>? {
val sig = (iota as? PatternIota)?.pattern?.anglesSignature()
val sig = (iota as? PatternIota)?.pattern?.angles

var displayDepth = this.image.parenCount

Expand All @@ -173,13 +173,13 @@ class CastingVM(var image: CastingImage, val env: CastingEnvironment) {
} else {

when (sig) {
SpecialPatterns.CONSIDERATION.anglesSignature() -> {
SpecialPatterns.CONSIDERATION.angles -> {
this.image.copy(
escapeNext = true,
) to ResolvedPatternType.EVALUATED
}

SpecialPatterns.EVANITION.anglesSignature() -> {
SpecialPatterns.EVANITION.angles -> {
val newParens = this.image.parenthesized.toMutableList()
val last = newParens.removeLastOrNull()
val newParenCount = this.image.parenCount + if (last == null || last.escaped || last.iota !is PatternIota) 0 else when (last.iota.pattern) {
Expand All @@ -190,7 +190,7 @@ class CastingVM(var image: CastingImage, val env: CastingEnvironment) {
this.image.copy(parenthesized = newParens, parenCount = newParenCount) to if (last == null) ResolvedPatternType.ERRORED else ResolvedPatternType.UNDONE
}

SpecialPatterns.INTROSPECTION.anglesSignature() -> {
SpecialPatterns.INTROSPECTION.angles -> {
// we have escaped the parens onto the stack; we just also record our count.
val newParens = this.image.parenthesized.toMutableList()
newParens.add(ParenthesizedIota(iota, false))
Expand All @@ -200,7 +200,7 @@ class CastingVM(var image: CastingImage, val env: CastingEnvironment) {
) to if (this.image.parenCount == 0) ResolvedPatternType.EVALUATED else ResolvedPatternType.ESCAPED
}

SpecialPatterns.RETROSPECTION.anglesSignature() -> {
SpecialPatterns.RETROSPECTION.angles -> {
val newParenCount = this.image.parenCount - 1
displayDepth--
if (newParenCount == 0) {
Expand Down Expand Up @@ -243,19 +243,19 @@ class CastingVM(var image: CastingImage, val env: CastingEnvironment) {
) to ResolvedPatternType.ESCAPED
} else {
when (sig) {
SpecialPatterns.CONSIDERATION.anglesSignature() -> {
SpecialPatterns.CONSIDERATION.angles -> {
this.image.copy(
escapeNext = true
) to ResolvedPatternType.EVALUATED
}

SpecialPatterns.INTROSPECTION.anglesSignature() -> {
SpecialPatterns.INTROSPECTION.angles -> {
this.image.copy(
parenCount = this.image.parenCount + 1
) to ResolvedPatternType.EVALUATED
}

SpecialPatterns.RETROSPECTION.anglesSignature() -> {
SpecialPatterns.RETROSPECTION.angles -> {
throw MishapTooManyCloseParens()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public boolean isTruthy() {
public boolean toleratesOther(Iota that) {
return typesMatch(this, that)
&& that instanceof PatternIota piota
&& this.getPattern().anglesSignature().equals(piota.getPattern().anglesSignature());
&& this.getPattern().getAngles().equals(piota.getPattern().getAngles());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import net.minecraft.world.phys.Vec2
/**
* Sequence of angles to define a pattern traced.
*/
data class HexPattern(public val startDir: HexDir, public val angles: MutableList<HexAngle> = arrayListOf()) {
data class HexPattern(val startDir: HexDir, val angles: MutableList<HexAngle> = arrayListOf()) {
/**
* @return True if it successfully appended, false if not.
*/
Expand Down Expand Up @@ -113,7 +113,7 @@ data class HexPattern(public val startDir: HexDir, public val angles: MutableLis
fun toLines(hexSize: Float, origin: Vec2): List<Vec2> =
this.positions().map { coordToPx(it, hexSize, origin) }

fun sigsEqual(that: HexPattern) = this.anglesSignature() == that.anglesSignature()
fun sigsEqual(that: HexPattern) = this.angles == that.angles

override fun toString(): String = buildString {
append("HexPattern[")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public interface ServerConfigAccess {
// fun fact, although dimension keys are a RegistryHolder, they aren't a registry, so i can't do tags
boolean canTeleportInThisDimension(ResourceKey<Level> dimension);

int DEFAULT_MAX_OP_COUNT = 1_000_000;
int DEFAULT_MAX_OP_COUNT = 2_000_000;
int DEFAULT_MAX_SPELL_CIRCLE_LENGTH = 1024;
int DEFAULT_OP_BREAK_HARVEST_LEVEL = 3;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import at.petrak.hexcasting.api.casting.PatternShapeMatch;
import at.petrak.hexcasting.api.casting.castables.SpecialHandler;
import at.petrak.hexcasting.api.casting.eval.CastingEnvironment;
import at.petrak.hexcasting.api.casting.math.HexAngle;
import at.petrak.hexcasting.api.casting.math.HexPattern;
import at.petrak.hexcasting.api.mod.HexTags;
import at.petrak.hexcasting.api.utils.HexUtils;
Expand All @@ -16,12 +17,13 @@
import org.apache.commons.lang3.NotImplementedException;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

// Now an internal-only class used to do final processing on the registered stuff
public class PatternRegistryManifest {
private static final ConcurrentMap<String, ResourceKey<ActionRegistryEntry>> NORMAL_ACTION_LOOKUP =
private static final ConcurrentMap<List<HexAngle>, ResourceKey<ActionRegistryEntry>> NORMAL_ACTION_LOOKUP =
new ConcurrentHashMap<>();

/**
Expand All @@ -42,8 +44,11 @@ public static void processRegistry(@Nullable ServerLevel overworld) {
var registry = IXplatAbstractions.INSTANCE.getActionRegistry();
for (var key : registry.registryKeySet()) {
var entry = registry.get(key);
if (entry == null)
continue;

if (!HexUtils.isOfTag(registry, key, HexTags.Actions.PER_WORLD_PATTERN)) {
NORMAL_ACTION_LOOKUP.put(entry.prototype().anglesSignature(), key);
NORMAL_ACTION_LOOKUP.put(entry.prototype().getAngles(), key);
} else {
perWorldActionCount++;
}
Expand All @@ -64,6 +69,8 @@ public static Pair<SpecialHandler, ResourceKey<SpecialHandler.Factory<?>>> match
var registry = IXplatAbstractions.INSTANCE.getSpecialHandlerRegistry();
for (var key : registry.registryKeySet()) {
var factory = registry.get(key);
if (factory == null)
continue;
var handler = factory.tryMatch(pat,environment);
if (handler != null) {
return Pair.of(handler, key);
Expand All @@ -84,15 +91,15 @@ public static PatternShapeMatch matchPattern(HexPattern pat, CastingEnvironment
boolean checkForAlternateStrokeOrders) {
// I am PURPOSELY checking normal actions before special handlers
// This way we don't get a repeat of the phial number literal incident
var sig = pat.anglesSignature();
var sig = pat.getAngles();
if (NORMAL_ACTION_LOOKUP.containsKey(sig)) {
var key = NORMAL_ACTION_LOOKUP.get(sig);
return new PatternShapeMatch.Normal(key);
}

// Look it up in the world?
var perWorldPatterns = ScrungledPatternsSave.open(environment.getWorld().getServer().overworld());
var entry = perWorldPatterns.lookup(sig);
var entry = perWorldPatterns.lookup(pat.anglesSignature());
if (entry != null) {
return new PatternShapeMatch.PerWorld(entry.key(), true);
}
Expand Down

0 comments on commit 9a87439

Please sign in to comment.