don't divide by zero

This commit is contained in:
yrsegal@gmail.com 2022-04-09 19:51:05 -04:00
parent eef29eee49
commit 67d7e20469
4 changed files with 54 additions and 5 deletions

View file

@ -0,0 +1,28 @@
package at.petrak.hexcasting.common.casting.mishaps
import at.petrak.hexcasting.api.spell.SpellDatum
import at.petrak.hexcasting.common.casting.CastingContext
import at.petrak.hexcasting.common.casting.colors.FrozenColorizer
import at.petrak.hexcasting.common.lib.HexDamageSources
import net.minecraft.network.chat.Component
import net.minecraft.network.chat.TextComponent
import net.minecraft.world.item.DyeColor
import net.minecraft.world.phys.Vec3
class MishapDivideByZero(val numerator: Component, val wasVec: Boolean) : Mishap() {
constructor(number: Double) : this(TextComponent("%d".format(number)), false)
constructor(vec: Vec3) : this(SpellDatum.make(vec).display(), true)
override fun accentColor(ctx: CastingContext, errorCtx: Context): FrozenColorizer =
dyeColor(DyeColor.RED)
override fun execute(ctx: CastingContext, errorCtx: Context, stack: MutableList<SpellDatum<*>>) {
ctx.caster.hurt(HexDamageSources.OVERCAST, ctx.caster.health / 2)
}
override fun errorMessage(ctx: CastingContext, errorCtx: Context): Component {
if (wasVec)
return error("divide_by_zero_vec", numerator)
return error("divide_by_zero", numerator)
}
}

View file

@ -4,6 +4,7 @@ import at.petrak.hexcasting.api.spell.ConstManaOperator
import at.petrak.hexcasting.api.spell.Operator.Companion.spellListOf
import at.petrak.hexcasting.api.spell.SpellDatum
import at.petrak.hexcasting.common.casting.CastingContext
import at.petrak.hexcasting.common.casting.mishaps.MishapDivideByZero
import net.minecraft.world.phys.Vec3
object OpDivCross : ConstManaOperator {
@ -17,14 +18,31 @@ object OpDivCross : ConstManaOperator {
return spellListOf(
lhs.map({ lnum ->
rhs.map(
{ rnum -> lnum / rnum }, { rvec -> Vec3(lnum / rvec.x, lnum / rvec.y, lnum / rvec.z) }
{ rnum ->
if (rnum == 0.0)
throw MishapDivideByZero(lnum)
lnum / rnum
},
{ rvec ->
if (rvec.x == 0.0 && rvec.y == 0.0 && rvec.z == 0.0)
throw MishapDivideByZero(lnum)
Vec3(lnum / rvec.x, lnum / rvec.y, lnum / rvec.z)
}
)
}, { lvec ->
rhs.map(
{ rnum -> lvec.scale(1.0 / rnum) },
{ rvec -> lvec.cross(rvec) }
{ rnum ->
if (rnum == 0.0)
throw MishapDivideByZero(lvec)
lvec.scale(1.0 / rnum)
},
{ rvec ->
if (rvec.x == 0.0 && rvec.y == 0.0 && rvec.z == 0.0)
throw MishapDivideByZero(lvec)
lvec.cross(rvec)
}
)
})
)
}
}
}

View file

@ -3,7 +3,8 @@ package at.petrak.hexcasting.common.lib;
import net.minecraft.world.damagesource.DamageSource;
public class HexDamageSources {
public static final DamageSource OVERCAST = new DamageSource("hexcasting.overcast").bypassArmor()
public static final DamageSource OVERCAST = new DamageSource("hexcasting.overcast")
.bypassArmor()
.bypassMagic()
.setMagic();
}

View file

@ -318,6 +318,8 @@
"hexcasting.mishap.already_brainswept": "The villager has already been used",
"hexcasting.mishap.no_spell_circle": "%s requires a spell circle",
"hexcasting.mishap.others_name": "Tried to invade %s's privacy",
"hexcasting.mishap.divide_by_zero": "Attempted to divide %s by zero",
"hexcasting.mishap.divide_by_zero_vec": "Attempted to divide %s by a zero vector",
"hexcasting.mishap.no_akashic_record": "No akashic record at %s",
"hexcasting.landing": "I seem to have discovered a new method of magical arts, in which one draws patterns strange and wild onto a hexagonal grid. It fascinates me. I've decided to start a journal of my thoughts and findings.$(br2)$(l:https://discord.gg/4xxHGYteWk)Discord Server Link/$",