implement swindler's gambits

This commit is contained in:
gamma-delta 2022-04-08 17:54:19 -05:00
parent 3645a4eade
commit a3c1abf65e
6 changed files with 95 additions and 3 deletions

View file

@ -1,5 +1,6 @@
package at.petrak.hexcasting
import at.petrak.hexcasting.api.PatternRegistry
import at.petrak.hexcasting.client.*
import at.petrak.hexcasting.common.blocks.HexBlocks
import at.petrak.hexcasting.common.casting.RegisterPatterns
@ -58,6 +59,7 @@ object HexMod {
// gotta do it at *some* point
modBus.register(RegisterPatterns::class.java)
modBus.register(HexDataGenerators::class.java)
modBus.register(PatternRegistry)
HexItems.ITEMS.register(modBus)
HexBlocks.BLOCKS.register(modBus)

View file

@ -1,5 +1,6 @@
package at.petrak.hexcasting.api
import at.petrak.hexcasting.HexMod
import at.petrak.hexcasting.api.spell.Operator
import at.petrak.hexcasting.common.casting.mishaps.MishapInvalidPattern
import at.petrak.hexcasting.hexmath.EulerPathFinder
@ -9,6 +10,8 @@ import net.minecraft.nbt.CompoundTag
import net.minecraft.resources.ResourceLocation
import net.minecraft.server.level.ServerLevel
import net.minecraft.world.level.saveddata.SavedData
import net.minecraftforge.eventbus.api.SubscribeEvent
import net.minecraftforge.fml.event.lifecycle.FMLLoadCompleteEvent
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ConcurrentLinkedDeque
import java.util.concurrent.ConcurrentMap
@ -64,6 +67,11 @@ object PatternRegistry {
this.specialHandlers.add(handler)
}
@JvmStatic
fun addSpecialHandler(id: ResourceLocation, handler: SpecialHandler) {
this.addSpecialHandler(SpecialHandlerEntry(id, handler))
}
/**
* Internal use only.
*/
@ -198,4 +206,13 @@ object PatternRegistry {
const val TAG_SAVED_DATA = "hex.per-world-patterns"
private const val TAG_OP_ID = "op_id"
private const val TAG_START_DIR = "start_dir"
@SubscribeEvent
fun printPatternCount(evt: FMLLoadCompleteEvent) {
HexMod.getLogger().info(
"Loaded ${this.regularPatternLookup.size} regular patterns, " +
"${this.perWorldPatternLookup.size} per-world patterns, and " +
"${this.specialHandlers.size} special handlers."
)
}
}

View file

@ -23,8 +23,10 @@ import at.petrak.hexcasting.common.casting.operators.spells.sentinel.OpDestroySe
import at.petrak.hexcasting.common.casting.operators.spells.sentinel.OpGetSentinelPos;
import at.petrak.hexcasting.common.casting.operators.spells.sentinel.OpGetSentinelWayfind;
import at.petrak.hexcasting.common.items.HexItems;
import at.petrak.hexcasting.hexmath.HexAngle;
import at.petrak.hexcasting.hexmath.HexDir;
import at.petrak.hexcasting.hexmath.HexPattern;
import it.unimi.dsi.fastutil.booleans.BooleanArrayList;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.eventbus.api.SubscribeEvent;
@ -69,7 +71,7 @@ public class RegisterPatterns {
// == Modify Stack ==
PatternRegistry.mapPattern(HexPattern.FromAnglesSig("a", HexDir.EAST), prefix("undo"), OpUndo.INSTANCE);
// PatternRegistry.mapPattern(HexPattern.FromAnglesSig("a", HexDir.EAST), prefix("undo"), OpUndo.INSTANCE);
PatternRegistry.mapPattern(HexPattern.FromAnglesSig("d", HexDir.EAST), prefix("const/null"), Widget.NULL);
PatternRegistry.mapPattern(HexPattern.FromAnglesSig("aadaa", HexDir.EAST), prefix("duplicate"),
OpDuplicate.INSTANCE);
@ -363,7 +365,7 @@ public class RegisterPatterns {
}
// Add zilde->number
PatternRegistry.addSpecialHandler(new PatternRegistry.SpecialHandlerEntry(prefix("number"), pat -> {
PatternRegistry.addSpecialHandler(prefix("number"), pat -> {
var sig = pat.anglesSignature();
if (sig.startsWith("aqaa") || sig.startsWith("dedd")) {
var negate = sig.startsWith("dedd");
@ -388,6 +390,44 @@ public class RegisterPatterns {
} else {
return null;
}
}));
});
// and swizzles
PatternRegistry.addSpecialHandler(prefix("swizzle"), pat -> {
var directions = pat.directions();
HexDir flatDir;
if (pat.angles().isEmpty()) {
return null;
} else if (pat.angles().get(0) == HexAngle.LEFT_BACK) {
flatDir = directions.get(0).rotatedBy(HexAngle.LEFT);
} else {
flatDir = pat.startDir();
}
var mask = new BooleanArrayList();
for (int i = 0; i < directions.size(); i++) {
// Angle with respect to the *start direction*
var angle = directions.get(i).angleFrom(flatDir);
if (angle == HexAngle.FORWARD) {
mask.add(true);
continue;
}
if (i >= directions.size() - 1) {
// then we're out of angles!
return null;
}
var angle2 = directions.get(i + 1).angleFrom(flatDir);
if (angle == HexAngle.RIGHT && angle2 == HexAngle.LEFT) {
mask.add(false);
i++;
continue;
}
return null;
}
return new OpSwizzle(mask);
});
}
}

View file

@ -0,0 +1,20 @@
package at.petrak.hexcasting.common.casting.operators
import at.petrak.hexcasting.api.spell.ConstManaOperator
import at.petrak.hexcasting.api.spell.SpellDatum
import at.petrak.hexcasting.common.casting.CastingContext
import it.unimi.dsi.fastutil.booleans.BooleanList
class OpSwizzle(val mask: BooleanList) : ConstManaOperator {
override val argc: Int
get() = mask.size
override fun execute(args: List<SpellDatum<*>>, ctx: CastingContext): List<SpellDatum<*>> {
val out = ArrayList<SpellDatum<*>>(this.mask.size)
for ((i, include) in this.mask.withIndex()) {
if (include)
out.add(args[i])
}
return out
}
}

View file

@ -57,6 +57,18 @@ data class HexPattern(val startDir: HexDir, val angles: MutableList<HexAngle> =
return out
}
fun directions(): List<HexDir> {
val out = ArrayList<HexDir>(this.angles.size + 1)
out.add(this.startDir)
var compass: HexDir = this.startDir
for (a in this.angles) {
compass *= a
out.add(compass)
}
return out
}
fun finalDir(): HexDir =
this.angles.fold(this.startDir) { acc, angle -> acc * angle }

View file

@ -252,6 +252,7 @@
"hexcasting.spell.hexcasting:const/vec/nz": "Vector Reflection -Z",
"hexcasting.spell.hexcasting:const/vec/0": "Vector Reflection Zero",
"hexcasting.spell.hexcasting:number": "Numerical Reflection",
"hexcasting.spell.hexcasting:swizzle": "Swindler's Gambit",
"hexcasting.spell.unknown": "Special Handler",
"hexcasting.mishap.invalid_pattern": "That pattern isn't associated with any action",