Skip to content

Commit dbc1cb4

Browse files
authored
Make out of media a mishap and kill SideEffect earlyExit (#776)
2 parents 8b3e267 + b3a25aa commit dbc1cb4

File tree

11 files changed

+76
-58
lines changed

11 files changed

+76
-58
lines changed

Common/src/main/java/at/petrak/hexcasting/api/casting/castables/ConstMediaAction.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import at.petrak.hexcasting.api.casting.eval.vm.CastingImage
77
import at.petrak.hexcasting.api.casting.eval.vm.SpellContinuation
88
import at.petrak.hexcasting.api.casting.iota.Iota
99
import at.petrak.hexcasting.api.casting.mishaps.MishapNotEnoughArgs
10+
import at.petrak.hexcasting.api.casting.mishaps.MishapNotEnoughMedia
1011
import at.petrak.hexcasting.common.lib.hex.HexEvalSounds
1112

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

31+
if (env.extractMedia(this.mediaCost, true) > 0)
32+
throw MishapNotEnoughMedia(this.mediaCost)
3033
if (this.argc > stack.size)
3134
throw MishapNotEnoughArgs(this.argc, stack.size)
3235
val args = stack.takeLast(this.argc)

Common/src/main/java/at/petrak/hexcasting/api/casting/castables/SpellAction.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import at.petrak.hexcasting.api.casting.eval.vm.CastingImage
99
import at.petrak.hexcasting.api.casting.eval.vm.SpellContinuation
1010
import at.petrak.hexcasting.api.casting.iota.Iota
1111
import at.petrak.hexcasting.api.casting.mishaps.MishapNotEnoughArgs
12+
import at.petrak.hexcasting.api.casting.mishaps.MishapNotEnoughMedia
1213
import at.petrak.hexcasting.common.lib.hex.HexEvalSounds
1314
import net.minecraft.nbt.CompoundTag
1415

@@ -44,6 +45,8 @@ interface SpellAction : Action {
4445

4546
val sideEffects = mutableListOf<OperatorSideEffect>()
4647

48+
if (env.extractMedia(result.cost, true) > 0)
49+
throw MishapNotEnoughMedia(result.cost)
4750
if (result.cost > 0)
4851
sideEffects.add(OperatorSideEffect.ConsumeMedia(result.cost))
4952

Common/src/main/java/at/petrak/hexcasting/api/casting/eval/CastingEnvironment.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,12 @@ public boolean isEnlightened() {
238238
* If there was enough media found, it will return less or equal to zero; if there wasn't, it will be
239239
* positive.
240240
*/
241-
public long extractMedia(long cost) {
241+
public long extractMedia(long cost, boolean simulate) {
242242
for (var extractMediaComponent : preMediaExtract)
243-
cost = extractMediaComponent.onExtractMedia(cost);
244-
cost = extractMediaEnvironment(cost);
243+
cost = extractMediaComponent.onExtractMedia(cost, simulate);
244+
cost = extractMediaEnvironment(cost, simulate);
245245
for (var extractMediaComponent : postMediaExtract)
246-
cost = extractMediaComponent.onExtractMedia(cost);
246+
cost = extractMediaComponent.onExtractMedia(cost, simulate);
247247
return cost;
248248
}
249249

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

258258
/**
259259
* Get if the vec is close enough, to the player or sentinel ...

Common/src/main/java/at/petrak/hexcasting/api/casting/eval/CastingEnvironmentComponent.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ interface ExtractMedia extends CastingEnvironmentComponent {
2929
* remaining cost after deducting whatever cost source this component
3030
* is responsible for (should be &gt;= 0)
3131
*/
32-
long onExtractMedia(long cost);
32+
long onExtractMedia(long cost, boolean simulate);
3333

3434
/**
35-
* ExtractMedia component that extracts media BEFORE the call to {@link CastingEnvironment#extractMediaEnvironment(long)}
35+
* ExtractMedia component that extracts media BEFORE the call to {@link CastingEnvironment#extractMediaEnvironment(long, boolean)}
3636
*/
3737
interface Pre extends ExtractMedia {}
3838

3939
/**
40-
* ExtractMedia component that extracts media AFTER the call to {@link CastingEnvironment#extractMediaEnvironment(long)}
40+
* ExtractMedia component that extracts media AFTER the call to {@link CastingEnvironment#extractMediaEnvironment(long, boolean)}
4141
* if the input is &lt;= 0 you should also probably return 0 (since media cost was already paid off)
4242
*/
4343
interface Post extends ExtractMedia {}

Common/src/main/java/at/petrak/hexcasting/api/casting/eval/env/CircleCastEnv.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public Vec3 mishapSprayPos() {
111111
}
112112

113113
@Override
114-
public long extractMediaEnvironment(long cost) {
114+
public long extractMediaEnvironment(long cost, boolean simulate) {
115115
var entity = this.getImpetus();
116116
if (entity == null)
117117
return cost;
@@ -122,7 +122,9 @@ public long extractMediaEnvironment(long cost) {
122122

123123
long mediaToTake = Math.min(cost, mediaAvailable);
124124
cost -= mediaToTake;
125-
entity.setMedia(mediaAvailable - mediaToTake);
125+
if (!simulate) {
126+
entity.setMedia(mediaAvailable - mediaToTake);
127+
}
126128

127129
return cost;
128130
}

Common/src/main/java/at/petrak/hexcasting/api/casting/eval/env/PackagedItemCastEnv.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void postExecution(CastResult result) {
3737
}
3838

3939
@Override
40-
public long extractMediaEnvironment(long costLeft) {
40+
public long extractMediaEnvironment(long costLeft, boolean simulate) {
4141
if (this.caster.isCreative())
4242
return 0;
4343

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

6262
return costLeft;

Common/src/main/java/at/petrak/hexcasting/api/casting/eval/env/PlayerBasedCastEnv.java

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,13 @@ public boolean hasEditPermissionsAtEnvironment(BlockPos pos) {
106106
/**
107107
* Search the player's inventory for media ADs and use them.
108108
*/
109-
protected long extractMediaFromInventory(long costLeft, boolean allowOvercast) {
109+
protected long extractMediaFromInventory(long costLeft, boolean allowOvercast, boolean simulate) {
110110
List<ADMediaHolder> sources = MediaHelper.scanPlayerForMediaStuff(this.caster);
111111

112112
var startCost = costLeft;
113113

114114
for (var source : sources) {
115-
var found = MediaHelper.extractMedia(source, costLeft, false, false);
115+
var found = MediaHelper.extractMedia(source, costLeft, false, simulate);
116116
costLeft -= found;
117117
if (costLeft <= 0) {
118118
break;
@@ -122,24 +122,31 @@ protected long extractMediaFromInventory(long costLeft, boolean allowOvercast) {
122122
if (costLeft > 0 && allowOvercast) {
123123
double mediaToHealth = HexConfig.common().mediaToHealthRate();
124124
double healthToRemove = Math.max(costLeft / mediaToHealth, 0.5);
125-
var mediaAbleToCastFromHP = this.caster.getHealth() * mediaToHealth;
125+
if (simulate) {
126+
long simulatedRemovedMedia = Mth.ceil(Math.min(this.caster.getHealth(), healthToRemove) * mediaToHealth);
127+
costLeft -= simulatedRemovedMedia;
128+
} else {
129+
var mediaAbleToCastFromHP = this.caster.getHealth() * mediaToHealth;
126130

127-
Mishap.trulyHurt(this.caster, this.caster.damageSources().source(HexDamageTypes.OVERCAST), (float) healthToRemove);
131+
Mishap.trulyHurt(this.caster, this.caster.damageSources().source(HexDamageTypes.OVERCAST), (float) healthToRemove);
128132

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

131-
HexAdvancementTriggers.OVERCAST_TRIGGER.trigger(this.caster, actuallyTaken);
132-
this.caster.awardStat(HexStatistics.MEDIA_OVERCAST, actuallyTaken);
135+
HexAdvancementTriggers.OVERCAST_TRIGGER.trigger(this.caster, actuallyTaken);
136+
this.caster.awardStat(HexStatistics.MEDIA_OVERCAST, actuallyTaken);
133137

134-
costLeft -= actuallyTaken;
138+
costLeft -= actuallyTaken;
139+
}
135140
}
136141

137-
this.caster.awardStat(HexStatistics.MEDIA_USED, (int) (startCost - costLeft));
138-
HexAdvancementTriggers.SPEND_MEDIA_TRIGGER.trigger(
139-
this.caster,
140-
startCost - costLeft,
141-
costLeft < 0 ? -costLeft : 0
142-
);
142+
if (!simulate) {
143+
this.caster.awardStat(HexStatistics.MEDIA_USED, (int) (startCost - costLeft));
144+
HexAdvancementTriggers.SPEND_MEDIA_TRIGGER.trigger(
145+
this.caster,
146+
startCost - costLeft,
147+
costLeft < 0 ? -costLeft : 0
148+
);
149+
}
143150

144151
return costLeft;
145152
}

Common/src/main/java/at/petrak/hexcasting/api/casting/eval/env/StaffCastEnv.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,12 @@ public void postCast(CastingImage image) {
6969
}
7070

7171
@Override
72-
public long extractMediaEnvironment(long cost) {
72+
public long extractMediaEnvironment(long cost, boolean simulate) {
7373
if (this.caster.isCreative())
7474
return 0;
7575

7676
var canOvercast = this.canOvercast();
77-
var remaining = this.extractMediaFromInventory(cost, canOvercast);
78-
if (remaining > 0 && !canOvercast) {
79-
this.caster.sendSystemMessage(Component.translatable("hexcasting.message.cant_overcast"));
80-
}
81-
return remaining;
77+
return this.extractMediaFromInventory(cost, canOvercast, simulate);
8278
}
8379

8480
@Override

Common/src/main/java/at/petrak/hexcasting/api/casting/eval/sideeffects/OperatorSideEffect.kt

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,11 @@ import net.minecraft.world.item.ItemStack
1818
*/
1919
sealed class OperatorSideEffect {
2020
/** Return whether to cancel all further [OperatorSideEffect] */
21-
abstract fun performEffect(harness: CastingVM): Boolean
21+
abstract fun performEffect(harness: CastingVM)
2222

2323
data class RequiredEnlightenment(val awardStat: Boolean) : OperatorSideEffect() {
24-
override fun performEffect(harness: CastingVM): Boolean {
24+
override fun performEffect(harness: CastingVM) {
2525
harness.env.castingEntity?.sendSystemMessage("hexcasting.message.cant_great_spell".asTranslatedComponent)
26-
27-
28-
return true
2926
}
3027
}
3128

@@ -36,33 +33,28 @@ sealed class OperatorSideEffect {
3633
val awardStat: Boolean = true
3734
) :
3835
OperatorSideEffect() {
39-
override fun performEffect(harness: CastingVM): Boolean {
36+
override fun performEffect(harness: CastingVM) {
4037
this.spell.cast(harness.env, harness.image)?.let { harness.image = it }
4138
if (awardStat)
4239
(harness.env.castingEntity as? ServerPlayer)?.awardStat(HexStatistics.SPELLS_CAST)
43-
44-
return false
4540
}
4641
}
4742

4843
data class ConsumeMedia(val amount: Long) : OperatorSideEffect() {
49-
override fun performEffect(harness: CastingVM): Boolean {
50-
val leftoverMedia = harness.env.extractMedia(this.amount)
51-
return leftoverMedia > 0
44+
override fun performEffect(harness: CastingVM) {
45+
harness.env.extractMedia(this.amount, false)
5246
}
5347
}
5448

5549
data class Particles(val spray: ParticleSpray) : OperatorSideEffect() {
56-
override fun performEffect(harness: CastingVM): Boolean {
50+
override fun performEffect(harness: CastingVM) {
5751
harness.env.produceParticles(this.spray, harness.env.pigment)
5852
// this.spray.sprayParticles(harness.env.world, harness.env.colorizer)
59-
60-
return false
6153
}
6254
}
6355

6456
data class DoMishap(val mishap: Mishap, val errorCtx: Mishap.Context) : OperatorSideEffect() {
65-
override fun performEffect(harness: CastingVM): Boolean {
57+
override fun performEffect(harness: CastingVM) {
6658
val spray = mishap.particleSpray(harness.env)
6759
val color = mishap.accentColor(harness.env, errorCtx)
6860
spray.sprayParticles(harness.env.world, color)
@@ -75,8 +67,6 @@ sealed class OperatorSideEffect {
7567
)
7668

7769
harness.image = harness.image.copy(stack = mishap.executeReturnStack(harness.env, errorCtx, harness.image.stack.toMutableList()))
78-
79-
return true
8070
}
8171
}
8272
}

Common/src/main/java/at/petrak/hexcasting/api/casting/eval/vm/CastingVM.kt

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ class CastingVM(var image: CastingImage, val env: CastingEnvironment) {
7373
continuation = image2.continuation
7474
lastResolutionType = image2.resolutionType
7575
try {
76-
performSideEffects(info, image2.sideEffects)
76+
performSideEffects(image2.sideEffects)
7777
} catch (e: Exception) {
7878
e.printStackTrace()
79-
performSideEffects(info, listOf(OperatorSideEffect.DoMishap(MishapInternalException(e), Mishap.Context(null, null))))
79+
performSideEffects(listOf(OperatorSideEffect.DoMishap(MishapInternalException(e), Mishap.Context(null, null))))
8080
}
8181
info.earlyExit = info.earlyExit || !lastResolutionType.success
8282
}
@@ -152,13 +152,9 @@ class CastingVM(var image: CastingImage, val env: CastingEnvironment) {
152152
/**
153153
* Execute the side effects of a pattern, updating our aggregated info.
154154
*/
155-
fun performSideEffects(info: TempControllerInfo, sideEffects: List<OperatorSideEffect>) {
155+
fun performSideEffects(sideEffects: List<OperatorSideEffect>) {
156156
for (haskellProgrammersShakingandCryingRN in sideEffects) {
157-
val mustStop = haskellProgrammersShakingandCryingRN.performEffect(this)
158-
if (mustStop) {
159-
info.earlyExit = true
160-
break
161-
}
157+
haskellProgrammersShakingandCryingRN.performEffect(this)
162158
}
163159
}
164160

0 commit comments

Comments
 (0)