Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make out of media a mishap and kill SideEffect earlyExit #776

Merged
merged 3 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import at.petrak.hexcasting.api.casting.eval.vm.CastingImage
import at.petrak.hexcasting.api.casting.eval.vm.SpellContinuation
import at.petrak.hexcasting.api.casting.iota.Iota
import at.petrak.hexcasting.api.casting.mishaps.MishapNotEnoughArgs
import at.petrak.hexcasting.api.casting.mishaps.MishapNotEnoughMedia
import at.petrak.hexcasting.common.lib.hex.HexEvalSounds

/**
Expand All @@ -27,6 +28,8 @@ interface ConstMediaAction : Action {
override fun operate(env: CastingEnvironment, image: CastingImage, continuation: SpellContinuation): OperationResult {
val stack = image.stack.toMutableList()

if (env.extractMedia(this.mediaCost, true) > 0)
vgskye marked this conversation as resolved.
Show resolved Hide resolved
throw MishapNotEnoughMedia(this.mediaCost)
if (this.argc > stack.size)
throw MishapNotEnoughArgs(this.argc, stack.size)
val args = stack.takeLast(this.argc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import at.petrak.hexcasting.api.casting.eval.vm.CastingImage
import at.petrak.hexcasting.api.casting.eval.vm.SpellContinuation
import at.petrak.hexcasting.api.casting.iota.Iota
import at.petrak.hexcasting.api.casting.mishaps.MishapNotEnoughArgs
import at.petrak.hexcasting.api.casting.mishaps.MishapNotEnoughMedia
import at.petrak.hexcasting.common.lib.hex.HexEvalSounds
import net.minecraft.nbt.CompoundTag

Expand Down Expand Up @@ -44,6 +45,8 @@ interface SpellAction : Action {

val sideEffects = mutableListOf<OperatorSideEffect>()

if (env.extractMedia(result.cost, true) > 0)
throw MishapNotEnoughMedia(result.cost)
if (result.cost > 0)
sideEffects.add(OperatorSideEffect.ConsumeMedia(result.cost))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,12 @@ public boolean isEnlightened() {
* If there was enough media found, it will return less or equal to zero; if there wasn't, it will be
* positive.
*/
public long extractMedia(long cost) {
public long extractMedia(long cost, boolean simulate) {
for (var extractMediaComponent : preMediaExtract)
vgskye marked this conversation as resolved.
Show resolved Hide resolved
cost = extractMediaComponent.onExtractMedia(cost);
cost = extractMediaEnvironment(cost);
cost = extractMediaComponent.onExtractMedia(cost, simulate);
cost = extractMediaEnvironment(cost, simulate);
for (var extractMediaComponent : postMediaExtract)
cost = extractMediaComponent.onExtractMedia(cost);
cost = extractMediaComponent.onExtractMedia(cost, simulate);
return cost;
}

Expand All @@ -253,7 +253,7 @@ public long extractMedia(long cost) {
* If there was enough media found, it will return less or equal to zero; if there wasn't, it will be
* positive.
*/
protected abstract long extractMediaEnvironment(long cost);
protected abstract long extractMediaEnvironment(long cost, boolean simulate);

/**
* Get if the vec is close enough, to the player or sentinel ...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ interface ExtractMedia extends CastingEnvironmentComponent {
* remaining cost after deducting whatever cost source this component
* is responsible for (should be &gt;= 0)
*/
long onExtractMedia(long cost);
long onExtractMedia(long cost, boolean simulate);

/**
* ExtractMedia component that extracts media BEFORE the call to {@link CastingEnvironment#extractMediaEnvironment(long)}
* ExtractMedia component that extracts media BEFORE the call to {@link CastingEnvironment#extractMediaEnvironment(long, boolean)}
*/
interface Pre extends ExtractMedia {}

/**
* ExtractMedia component that extracts media AFTER the call to {@link CastingEnvironment#extractMediaEnvironment(long)}
* ExtractMedia component that extracts media AFTER the call to {@link CastingEnvironment#extractMediaEnvironment(long, boolean)}
* if the input is &lt;= 0 you should also probably return 0 (since media cost was already paid off)
*/
interface Post extends ExtractMedia {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public Vec3 mishapSprayPos() {
}

@Override
public long extractMediaEnvironment(long cost) {
public long extractMediaEnvironment(long cost, boolean simulate) {
var entity = this.getImpetus();
if (entity == null)
return cost;
Expand All @@ -122,7 +122,9 @@ public long extractMediaEnvironment(long cost) {

long mediaToTake = Math.min(cost, mediaAvailable);
cost -= mediaToTake;
entity.setMedia(mediaAvailable - mediaToTake);
if (!simulate) {
entity.setMedia(mediaAvailable - mediaToTake);
}

return cost;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void postExecution(CastResult result) {
}

@Override
public long extractMediaEnvironment(long costLeft) {
public long extractMediaEnvironment(long costLeft, boolean simulate) {
if (this.caster.isCreative())
return 0;

Expand All @@ -52,11 +52,11 @@ public long extractMediaEnvironment(long costLeft) {
// The contracts on the AD and on this function are different.
// ADs return the amount extracted, this wants the amount left
if (casterMediaHolder != null) {
long extracted = casterMediaHolder.withdrawMedia((int) costLeft, false);
long extracted = casterMediaHolder.withdrawMedia((int) costLeft, simulate);
costLeft -= extracted;
}
if (canCastFromInv && costLeft > 0) {
costLeft = this.extractMediaFromInventory(costLeft, this.canOvercast());
costLeft = this.extractMediaFromInventory(costLeft, this.canOvercast(), simulate);
}

return costLeft;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ public boolean hasEditPermissionsAtEnvironment(BlockPos pos) {
/**
* Search the player's inventory for media ADs and use them.
*/
protected long extractMediaFromInventory(long costLeft, boolean allowOvercast) {
protected long extractMediaFromInventory(long costLeft, boolean allowOvercast, boolean simulate) {
List<ADMediaHolder> sources = MediaHelper.scanPlayerForMediaStuff(this.caster);

var startCost = costLeft;

for (var source : sources) {
var found = MediaHelper.extractMedia(source, costLeft, false, false);
var found = MediaHelper.extractMedia(source, costLeft, false, simulate);
costLeft -= found;
if (costLeft <= 0) {
break;
Expand All @@ -122,24 +122,31 @@ protected long extractMediaFromInventory(long costLeft, boolean allowOvercast) {
if (costLeft > 0 && allowOvercast) {
double mediaToHealth = HexConfig.common().mediaToHealthRate();
double healthToRemove = Math.max(costLeft / mediaToHealth, 0.5);
var mediaAbleToCastFromHP = this.caster.getHealth() * mediaToHealth;
if (simulate) {
long simulatedRemovedMedia = Mth.ceil(Math.min(this.caster.getHealth(), healthToRemove) * mediaToHealth);
costLeft -= simulatedRemovedMedia;
} else {
var mediaAbleToCastFromHP = this.caster.getHealth() * mediaToHealth;

Mishap.trulyHurt(this.caster, this.caster.damageSources().source(HexDamageTypes.OVERCAST), (float) healthToRemove);
Mishap.trulyHurt(this.caster, this.caster.damageSources().source(HexDamageTypes.OVERCAST), (float) healthToRemove);

var actuallyTaken = Mth.ceil(mediaAbleToCastFromHP - (this.caster.getHealth() * mediaToHealth));
var actuallyTaken = Mth.ceil(mediaAbleToCastFromHP - (this.caster.getHealth() * mediaToHealth));

HexAdvancementTriggers.OVERCAST_TRIGGER.trigger(this.caster, actuallyTaken);
this.caster.awardStat(HexStatistics.MEDIA_OVERCAST, actuallyTaken);
HexAdvancementTriggers.OVERCAST_TRIGGER.trigger(this.caster, actuallyTaken);
this.caster.awardStat(HexStatistics.MEDIA_OVERCAST, actuallyTaken);

costLeft -= actuallyTaken;
costLeft -= actuallyTaken;
}
}

this.caster.awardStat(HexStatistics.MEDIA_USED, (int) (startCost - costLeft));
HexAdvancementTriggers.SPEND_MEDIA_TRIGGER.trigger(
this.caster,
startCost - costLeft,
costLeft < 0 ? -costLeft : 0
);
if (!simulate) {
this.caster.awardStat(HexStatistics.MEDIA_USED, (int) (startCost - costLeft));
HexAdvancementTriggers.SPEND_MEDIA_TRIGGER.trigger(
this.caster,
startCost - costLeft,
costLeft < 0 ? -costLeft : 0
);
}

return costLeft;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,12 @@ public void postCast(CastingImage image) {
}

@Override
public long extractMediaEnvironment(long cost) {
public long extractMediaEnvironment(long cost, boolean simulate) {
if (this.caster.isCreative())
return 0;

var canOvercast = this.canOvercast();
var remaining = this.extractMediaFromInventory(cost, canOvercast);
if (remaining > 0 && !canOvercast) {
this.caster.sendSystemMessage(Component.translatable("hexcasting.message.cant_overcast"));
}
return remaining;
return this.extractMediaFromInventory(cost, canOvercast, simulate);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@ import net.minecraft.world.item.ItemStack
*/
sealed class OperatorSideEffect {
/** Return whether to cancel all further [OperatorSideEffect] */
abstract fun performEffect(harness: CastingVM): Boolean
abstract fun performEffect(harness: CastingVM)
SamsTheNerd marked this conversation as resolved.
Show resolved Hide resolved

data class RequiredEnlightenment(val awardStat: Boolean) : OperatorSideEffect() {
override fun performEffect(harness: CastingVM): Boolean {
override fun performEffect(harness: CastingVM) {
harness.env.castingEntity?.sendSystemMessage("hexcasting.message.cant_great_spell".asTranslatedComponent)


return true
}
}

Expand All @@ -36,33 +33,28 @@ sealed class OperatorSideEffect {
val awardStat: Boolean = true
) :
OperatorSideEffect() {
override fun performEffect(harness: CastingVM): Boolean {
override fun performEffect(harness: CastingVM) {
this.spell.cast(harness.env, harness.image)?.let { harness.image = it }
if (awardStat)
(harness.env.castingEntity as? ServerPlayer)?.awardStat(HexStatistics.SPELLS_CAST)

return false
}
}

data class ConsumeMedia(val amount: Long) : OperatorSideEffect() {
override fun performEffect(harness: CastingVM): Boolean {
val leftoverMedia = harness.env.extractMedia(this.amount)
return leftoverMedia > 0
override fun performEffect(harness: CastingVM) {
harness.env.extractMedia(this.amount, false)
}
}

data class Particles(val spray: ParticleSpray) : OperatorSideEffect() {
override fun performEffect(harness: CastingVM): Boolean {
override fun performEffect(harness: CastingVM) {
harness.env.produceParticles(this.spray, harness.env.pigment)
// this.spray.sprayParticles(harness.env.world, harness.env.colorizer)

return false
}
}

data class DoMishap(val mishap: Mishap, val errorCtx: Mishap.Context) : OperatorSideEffect() {
override fun performEffect(harness: CastingVM): Boolean {
override fun performEffect(harness: CastingVM) {
val spray = mishap.particleSpray(harness.env)
val color = mishap.accentColor(harness.env, errorCtx)
spray.sprayParticles(harness.env.world, color)
Expand All @@ -75,8 +67,6 @@ sealed class OperatorSideEffect {
)

harness.image = harness.image.copy(stack = mishap.executeReturnStack(harness.env, errorCtx, harness.image.stack.toMutableList()))

return true
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ class CastingVM(var image: CastingImage, val env: CastingEnvironment) {
continuation = image2.continuation
lastResolutionType = image2.resolutionType
try {
performSideEffects(info, image2.sideEffects)
performSideEffects(image2.sideEffects)
} catch (e: Exception) {
e.printStackTrace()
performSideEffects(info, listOf(OperatorSideEffect.DoMishap(MishapInternalException(e), Mishap.Context(null, null))))
performSideEffects(listOf(OperatorSideEffect.DoMishap(MishapInternalException(e), Mishap.Context(null, null))))
}
info.earlyExit = info.earlyExit || !lastResolutionType.success
}
Expand Down Expand Up @@ -152,13 +152,9 @@ class CastingVM(var image: CastingImage, val env: CastingEnvironment) {
/**
* Execute the side effects of a pattern, updating our aggregated info.
*/
fun performSideEffects(info: TempControllerInfo, sideEffects: List<OperatorSideEffect>) {
fun performSideEffects(sideEffects: List<OperatorSideEffect>) {
for (haskellProgrammersShakingandCryingRN in sideEffects) {
val mustStop = haskellProgrammersShakingandCryingRN.performEffect(this)
if (mustStop) {
info.earlyExit = true
break
}
haskellProgrammersShakingandCryingRN.performEffect(this)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package at.petrak.hexcasting.api.casting.mishaps

import at.petrak.hexcasting.api.casting.eval.CastingEnvironment
import at.petrak.hexcasting.api.casting.eval.ResolvedPatternType
import at.petrak.hexcasting.api.casting.iota.Iota
import at.petrak.hexcasting.api.pigment.FrozenPigment
import at.petrak.hexcasting.api.utils.asTranslatedComponent
import net.minecraft.world.item.DyeColor

class MishapNotEnoughMedia(private val cost: Long) : Mishap() {
override fun accentColor(ctx: CastingEnvironment, errorCtx: Context): FrozenPigment =
dyeColor(DyeColor.RED)

override fun resolutionType(ctx: CastingEnvironment) = ResolvedPatternType.ERRORED

override fun execute(env: CastingEnvironment, errorCtx: Context, stack: MutableList<Iota>) {
env.extractMedia(cost, false)
}

override fun errorMessage(ctx: CastingEnvironment, errorCtx: Context) = "hexcasting.message.cant_overcast".asTranslatedComponent
vgskye marked this conversation as resolved.
Show resolved Hide resolved
}
Loading