add a bunch of math operators and make AddMotion actually work

This commit is contained in:
gamma-delta 2021-12-28 19:30:51 -06:00
parent 41f68813d8
commit a60006c10b
15 changed files with 271 additions and 64 deletions

View file

@ -1,6 +1,7 @@
package at.petrak.hex.common.casting
import at.petrak.hex.common.casting.operators.*
import at.petrak.hex.common.casting.operators.math.*
import at.petrak.hex.common.casting.operators.spells.OpAddMotion
import at.petrak.hex.common.casting.operators.spells.OpExplode
import at.petrak.hex.common.casting.operators.spells.OpPrint
@ -53,6 +54,14 @@ interface SpellOperator {
// Two opposing triangles to swap
"aawdd" to OpSwap,
// == Math ==
"waaw" to OpAdd,
"wddw" to OpSub,
"waqaw" to OpMulDot,
"wdedw" to OpDivCross,
"wqaqw" to OpAbsLen,
"wedew" to OpPowProj,
// == Spells ==
// hook for debug

View file

@ -7,7 +7,6 @@ import at.petrak.hex.common.casting.SpellOperator.Companion.getChecked
import at.petrak.hex.common.casting.SpellWidget
import net.minecraft.world.entity.projectile.ProjectileUtil
import net.minecraft.world.phys.AABB
import net.minecraft.world.phys.HitResult
import net.minecraft.world.phys.Vec3
object OpEntityRaycast : SimpleOperator {
@ -26,11 +25,7 @@ object OpEntityRaycast : SimpleOperator {
SpellOperator.MAX_DISTANCE
)
return SpellOperator.spellListOf(
if (entityHitResult != null && entityHitResult.type == HitResult.Type.ENTITY) {
entityHitResult.entity
} else {
SpellWidget.NULL
}
entityHitResult?.entity ?: SpellWidget.NULL
)
}
}

View file

@ -0,0 +1,15 @@
package at.petrak.hex.common.casting.operators.math
import at.petrak.hex.common.casting.CastException
import at.petrak.hex.common.casting.SpellDatum
import com.mojang.datafixers.util.Either
import net.minecraft.world.phys.Vec3
object MathOpUtils {
fun GetNumOrVec(datum: SpellDatum<*>): Either<Double, Vec3> =
when (datum.payload) {
is Double -> Either.left(datum.payload)
is Vec3 -> Either.right(datum.payload)
else -> throw CastException(CastException.Reason.OP_WRONG_TYPE, Either::class.java, datum.payload)
}
}

View file

@ -0,0 +1,20 @@
package at.petrak.hex.common.casting.operators.math
import at.petrak.hex.common.casting.CastingContext
import at.petrak.hex.common.casting.SpellDatum
import at.petrak.hex.common.casting.SpellOperator.Companion.spellListOf
import at.petrak.hex.common.casting.operators.SimpleOperator
import kotlin.math.absoluteValue
object OpAbsLen : SimpleOperator {
override val argc: Int
get() = 1
override fun execute(args: List<SpellDatum<*>>, ctx: CastingContext): List<SpellDatum<*>> {
val x = MathOpUtils.GetNumOrVec(args[0])
return spellListOf(
x.map({ num -> num.absoluteValue }, { vec -> vec.length() })
)
}
}

View file

@ -0,0 +1,28 @@
package at.petrak.hex.common.casting.operators.math
import at.petrak.hex.common.casting.CastingContext
import at.petrak.hex.common.casting.SpellDatum
import at.petrak.hex.common.casting.SpellOperator.Companion.spellListOf
import at.petrak.hex.common.casting.operators.SimpleOperator
object OpAdd : SimpleOperator {
override val argc: Int
get() = 2
override fun execute(args: List<SpellDatum<*>>, ctx: CastingContext): List<SpellDatum<*>> {
val lhs = MathOpUtils.GetNumOrVec(args[0])
val rhs = MathOpUtils.GetNumOrVec(args[1])
return spellListOf(
lhs.map({ lnum ->
rhs.map(
{ rnum -> lnum + rnum }, { rvec -> rvec.add(lnum, lnum, lnum) }
)
}, { lvec ->
rhs.map(
{ rnum -> lvec.add(rnum, rnum, rnum) }, { rvec -> lvec.add(rvec) }
)
})
)
}
}

View file

@ -0,0 +1,30 @@
package at.petrak.hex.common.casting.operators.math
import at.petrak.hex.common.casting.CastingContext
import at.petrak.hex.common.casting.SpellDatum
import at.petrak.hex.common.casting.SpellOperator.Companion.spellListOf
import at.petrak.hex.common.casting.operators.SimpleOperator
import net.minecraft.world.phys.Vec3
object OpDivCross : SimpleOperator {
override val argc: Int
get() = 2
override fun execute(args: List<SpellDatum<*>>, ctx: CastingContext): List<SpellDatum<*>> {
val lhs = MathOpUtils.GetNumOrVec(args[0])
val rhs = MathOpUtils.GetNumOrVec(args[1])
return spellListOf(
lhs.map({ lnum ->
rhs.map(
{ rnum -> lnum / rnum }, { rvec -> Vec3(lnum / rvec.x, lnum / rvec.y, lnum / rvec.z) }
)
}, { lvec ->
rhs.map(
{ rnum -> lvec.scale(1.0 / rnum) },
{ rvec -> lvec.cross(rvec) }
)
})
)
}
}

View file

@ -0,0 +1,28 @@
package at.petrak.hex.common.casting.operators.math
import at.petrak.hex.common.casting.CastingContext
import at.petrak.hex.common.casting.SpellDatum
import at.petrak.hex.common.casting.SpellOperator.Companion.spellListOf
import at.petrak.hex.common.casting.operators.SimpleOperator
object OpMulDot : SimpleOperator {
override val argc: Int
get() = 2
override fun execute(args: List<SpellDatum<*>>, ctx: CastingContext): List<SpellDatum<*>> {
val lhs = MathOpUtils.GetNumOrVec(args[0])
val rhs = MathOpUtils.GetNumOrVec(args[1])
return spellListOf(
lhs.map({ lnum ->
rhs.map(
{ rnum -> lnum * rnum }, { rvec -> rvec.scale(lnum) }
)
}, { lvec ->
rhs.map(
{ rnum -> lvec.multiply(rnum, rnum, rnum) }, { rvec -> lvec.dot(rvec) }
)
})
)
}
}

View file

@ -0,0 +1,31 @@
package at.petrak.hex.common.casting.operators.math
import at.petrak.hex.common.casting.CastingContext
import at.petrak.hex.common.casting.SpellDatum
import at.petrak.hex.common.casting.SpellOperator.Companion.spellListOf
import at.petrak.hex.common.casting.operators.SimpleOperator
import net.minecraft.world.phys.Vec3
import kotlin.math.pow
object OpPowProj : SimpleOperator {
override val argc: Int
get() = 2
override fun execute(args: List<SpellDatum<*>>, ctx: CastingContext): List<SpellDatum<*>> {
val lhs = MathOpUtils.GetNumOrVec(args[0])
val rhs = MathOpUtils.GetNumOrVec(args[1])
return spellListOf(
lhs.map({ lnum ->
rhs.map(
{ rnum -> lnum.pow(rnum) }, { rvec -> Vec3(lnum.pow(rvec.x), lnum.pow(rvec.y), lnum.pow(rvec.z)) }
)
}, { lvec ->
rhs.map(
{ rnum -> Vec3(lvec.x.pow(rnum), lvec.y.pow(rnum), lvec.z.pow(rnum)) },
{ rvec -> rvec.scale(lvec.dot(rvec) / rvec.dot(rvec)) }
)
})
)
}
}

View file

@ -0,0 +1,29 @@
package at.petrak.hex.common.casting.operators.math
import at.petrak.hex.common.casting.CastingContext
import at.petrak.hex.common.casting.SpellDatum
import at.petrak.hex.common.casting.SpellOperator.Companion.spellListOf
import at.petrak.hex.common.casting.operators.SimpleOperator
import net.minecraft.world.phys.Vec3
object OpSub : SimpleOperator {
override val argc: Int
get() = 2
override fun execute(args: List<SpellDatum<*>>, ctx: CastingContext): List<SpellDatum<*>> {
val lhs = MathOpUtils.GetNumOrVec(args[0])
val rhs = MathOpUtils.GetNumOrVec(args[1])
return spellListOf(
lhs.map({ lnum ->
rhs.map(
{ rnum -> lnum - rnum }, { rvec -> Vec3(lnum - rvec.x, lnum - rvec.y, lnum - rvec.z) }
)
}, { lvec ->
rhs.map(
{ rnum -> lvec.subtract(rnum, rnum, rnum) }, { rvec -> lvec.subtract(rvec) }
)
})
)
}
}

View file

@ -7,8 +7,12 @@ import at.petrak.hex.common.casting.SpellDatum
import at.petrak.hex.common.casting.SpellOperator.Companion.getChecked
import at.petrak.hex.common.casting.SpellOperator.Companion.spellListOf
import at.petrak.hex.common.casting.operators.SimpleOperator
import at.petrak.hex.common.network.HexMessages
import at.petrak.hex.common.network.MsgAddMotionAck
import net.minecraft.server.level.ServerPlayer
import net.minecraft.world.entity.Entity
import net.minecraft.world.phys.Vec3
import net.minecraftforge.network.PacketDistributor
object OpAddMotion : SimpleOperator, RenderedSpellImpl {
override val argc: Int
@ -23,6 +27,13 @@ object OpAddMotion : SimpleOperator, RenderedSpellImpl {
override fun cast(args: List<SpellDatum<*>>, ctx: CastingContext) {
val target = args.getChecked<Entity>(0)
val motion = args.getChecked<Vec3>(1)
target.deltaMovement = target.deltaMovement.add(motion)
if (target is ServerPlayer) {
// Player movement is apparently handled on the client; who knew
// There's apparently some magic flag I can set to auto-sync it but I can't find it
HexMessages.getNetwork().send(PacketDistributor.PLAYER.with { target }, MsgAddMotionAck(motion))
} else {
target.deltaMovement = target.deltaMovement.add(motion)
}
}
}

View file

@ -29,5 +29,7 @@ public class HexMessages {
MsgQuitSpellcasting::deserialize, MsgQuitSpellcasting::handle);
NETWORK.registerMessage(messageIdx++, MsgShiftScrollSyn.class, MsgShiftScrollSyn::serialize,
MsgShiftScrollSyn::deserialize, MsgShiftScrollSyn::handle);
NETWORK.registerMessage(messageIdx++, MsgAddMotionAck.class, MsgAddMotionAck::serialize,
MsgAddMotionAck::deserialize, MsgAddMotionAck::handle);
}
}

View file

@ -0,0 +1,42 @@
package at.petrak.hex.common.network;
import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.network.NetworkEvent;
import java.util.function.Supplier;
/**
* Sent server->client to synchronize OpAddMotion when the target is a player.
*/
public record MsgAddMotionAck(Vec3 addedMotion) {
public static MsgAddMotionAck deserialize(ByteBuf buffer) {
var buf = new FriendlyByteBuf(buffer);
var x = buf.readDouble();
var y = buf.readDouble();
var z = buf.readDouble();
return new MsgAddMotionAck(new Vec3(x, y, z));
}
public void serialize(ByteBuf buffer) {
var buf = new FriendlyByteBuf(buffer);
buf.writeDouble(this.addedMotion.x);
buf.writeDouble(this.addedMotion.y);
buf.writeDouble(this.addedMotion.z);
}
public void handle(Supplier<NetworkEvent.Context> ctx) {
ctx.get().enqueueWork(() -> {
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> {
var player = Minecraft.getInstance().player;
var motion = player.getDeltaMovement();
player.setDeltaMovement(motion.add(this.addedMotion));
});
});
ctx.get().setPacketHandled(true);
}
}

View file

@ -5,7 +5,6 @@ import net.minecraft.client.Minecraft;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.network.NetworkDirection;
import net.minecraftforge.network.NetworkEvent;
import java.util.function.Supplier;
@ -28,10 +27,6 @@ public record MsgNewSpellPatternAck(boolean quitCasting) {
public void handle(Supplier<NetworkEvent.Context> ctx) {
ctx.get().enqueueWork(() ->
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> {
if (ctx.get().getDirection() != NetworkDirection.PLAY_TO_CLIENT) {
return;
}
if (quitCasting) {
Minecraft.getInstance().setScreen(null);
}

View file

@ -1,62 +1,34 @@
# This is an example mods.toml file. It contains the data relating to the loading mods.
# There are several mandatory fields (#mandatory), and many more that are optional (#optional).
# The overall format is standard TOML format, v0.5.0.
# Note that there are a couple of TOML lists in this file.
# Find more information on toml format here: https://github.com/toml-lang/toml
# The name of the mod loader type to load - for regular FML @Mod mods it should be javafml
modLoader = "javafml" #mandatory
# A version range to match for said mod loader - for regular FML @Mod it will be the forge version
loaderVersion = "[39,)" #mandatory This is typically bumped every Minecraft version by Forge. See our download page for lists of versions.
# The license for you mod. This is mandatory metadata and allows for easier comprehension of your redistributive properties.
# Review your options at https://choosealicense.com/. All rights reserved is the default copyright stance, and is thus the default here.
modLoader = "javafml"
loaderVersion = "[39,)"
license = "MIT"
# A URL to refer people to when problems occur with this mod
#issueTrackerURL="https://change.me.to.your.issue.tracker.example.invalid/" #optional
# A list of mods - how many allowed here is determined by the individual mod loader
[[mods]] #mandatory
# The modid of the mod
modId = "hex" #mandatory
# The version number of the mod - there's a few well known ${} variables useable here or just hardcode it
# ${file.jarVersion} will substitute the value of the Implementation-Version as read from the mod's JAR file metadata
# see the associated build.gradle script for how to populate this completely automatically during a build
version = "${file.jarVersion}" #mandatory
# A display name for the mod
displayName = "Hex" #mandatory
# A URL to query for updates for this mod. See the JSON update specification https://mcforge.readthedocs.io/en/latest/gettingstarted/autoupdate/
#updateJSONURL="https://change.me.example.invalid/updates.json" #optional
# A URL for the "homepage" for this mod, displayed in the mod UI
#displayURL="https://change.me.to.your.mods.homepage.example.invalid/" #optional
# A file name (in the root of the mod JAR) containing a logo for display
logoFile = "examplemod.png" #optional
# A text field displayed in the mod UI
credits = "Thanks for this example mod goes to Java" #optional
# A text field displayed in the mod UI
authors = "Love, Cheese and small house plants" #optional
# The description text for the mod (multi line!) (#mandatory)
description = '''
This is a long form description of the mod. You can write whatever you want here
Have some lorem ipsum.
issueTrackerURL = "https://github.com/gamma-delta/HexMod/issues"
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed mollis lacinia magna. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Sed sagittis luctus odio eu tempus. Interdum et malesuada fames ac ante ipsum primis in faucibus. Pellentesque volutpat ligula eget lacus auctor sagittis. In hac habitasse platea dictumst. Nunc gravida elit vitae sem vehicula efficitur. Donec mattis ipsum et arcu lobortis, eleifend sagittis sem rutrum. Cras pharetra quam eget posuere fermentum. Sed id tincidunt justo. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
[[mods]]
modId = "hex"
version = "${file.jarVersion}"
displayName = "Hex"
displayURL = "https://github.com/gamma-delta/HexMod"
logoFile = "examplemod.png"
credits = '''
- Alwinfy for visual effects help
- Kra3tor for sound effects
'''
# A dependency - use the . to indicate dependency for a specific modid. Dependencies are optional.
[[dependencies.examplemod]] #optional
# the modid of the dependency
modId = "forge" #mandatory
# Does this dependency have to exist - if not, ordering below must be specified
mandatory = true #mandatory
# The version range of the dependency
versionRange = "[39,)" #mandatory
# An ordering relationship for the dependency - BEFORE or AFTER required if the relationship is not mandatory
authors = "petrak@ (aka gamma-delta)"
description = '''
Cast complex spells by drawing patterns in the air!
'''
[[dependencies.hex]]
modId = "forge"
mandatory = true
versionRange = "[39,)"
ordering = "NONE"
# Side this dependency is applied on - BOTH, CLIENT or SERVER
side = "BOTH"
# Here's another dependency
[[dependencies.examplemod]]
[[dependencies.hex]]
modId = "minecraft"
mandatory = true
# This version range declares a minimum of the current minecraft version up to but not including the next major version
versionRange = "[1.18.1,1.19)"
ordering = "NONE"
side = "BOTH"

View file

@ -4,4 +4,4 @@
"item.hex.spellbook": "Spellbook",
"hex.spellbook.tooltip.page": "Selected Page %d/%d",
"hex.spelldata.desc.entity": "Entity %s"
}
}