Skip to content

Commit

Permalink
do the performance improvements for circles and packaged item casts a…
Browse files Browse the repository at this point in the history
…s well.
  • Loading branch information
Talia-12 committed Feb 6, 2024
1 parent 7ab0cc2 commit b29ab76
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import at.petrak.hexcasting.api.casting.eval.CastingEnvironment;
import at.petrak.hexcasting.api.casting.eval.MishapEnvironment;
import at.petrak.hexcasting.api.casting.eval.sideeffects.OperatorSideEffect;
import at.petrak.hexcasting.api.casting.eval.vm.CastingImage;
import at.petrak.hexcasting.api.casting.mishaps.Mishap;
import at.petrak.hexcasting.api.casting.mishaps.MishapDisallowedSpell;
import at.petrak.hexcasting.api.mod.HexConfig;
Expand All @@ -33,6 +34,10 @@
public class CircleCastEnv extends CastingEnvironment {
protected final CircleExecutionState execState;

private int soundsPlayed = 0;
private @Nullable Mishap lastMishap = null;
private @Nullable Mishap.Context lastMishapContext = null;

public CircleCastEnv(ServerLevel world, CircleExecutionState execState) {
super(world);
this.execState = execState;
Expand Down Expand Up @@ -77,9 +82,10 @@ public void postExecution(CastResult result) {

// we always want to play this sound one at a time
var sound = result.getSound().sound();
if (sound != null) {
if (soundsPlayed < 100 && sound != null) { // TODO: Make configurable
var soundPos = this.execState.currentPos;
this.world.playSound(null, soundPos, sound, SoundSource.PLAYERS, 1f, 1f);
soundsPlayed++;
}

// TODO: this is gonna bite us in the bum someday
Expand All @@ -90,15 +96,30 @@ public void postExecution(CastResult result) {
if (imp != null) {
for (var sideEffect : result.getSideEffects()) {
if (sideEffect instanceof OperatorSideEffect.DoMishap doMishap) {
var msg = doMishap.getMishap().errorMessageWithName(this, doMishap.getErrorCtx());
if (msg != null) {
imp.postMishap(msg);
}
lastMishap = doMishap.getMishap();
lastMishapContext = doMishap.getErrorCtx();
}
}
}
}

@Override
public void postCast(CastingImage image) {
super.postCast(image);

var imp = this.getImpetus();
if (lastMishap != null && lastMishapContext != null && imp != null) {
var msg = lastMishap.errorMessageWithName(this, lastMishapContext);
if (msg != null) {
imp.postMishap(msg);
}
}

soundsPlayed = 0;
lastMishap = null;
lastMishapContext = null;
}

@Override
public Vec3 mishapSprayPos() {
return Vec3.atCenterOf(this.execState.currentPos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@

import at.petrak.hexcasting.api.casting.eval.CastResult;
import at.petrak.hexcasting.api.casting.eval.sideeffects.EvalSound;
import at.petrak.hexcasting.api.casting.eval.vm.CastingImage;
import at.petrak.hexcasting.api.casting.iota.PatternIota;
import at.petrak.hexcasting.api.casting.math.HexPattern;
import at.petrak.hexcasting.api.pigment.FrozenPigment;
import at.petrak.hexcasting.common.lib.hex.HexEvalSounds;
import at.petrak.hexcasting.common.msgs.MsgNewSpiralPatternsS2C;
import at.petrak.hexcasting.xplat.IXplatAbstractions;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;

import java.util.List;
import java.util.HashSet;
import java.util.Set;

public class PackagedItemCastEnv extends PlayerBasedCastEnv {

protected EvalSound sound = HexEvalSounds.NOTHING;

private final Set<HexPattern> castPatterns = new HashSet<>();

public PackagedItemCastEnv(ServerPlayer caster, InteractionHand castingHand) {
super(caster, castingHand);
}
Expand All @@ -25,17 +30,26 @@ public void postExecution(CastResult result) {
super.postExecution(result);

if (result.component1() instanceof PatternIota patternIota) {
var packet = new MsgNewSpiralPatternsS2C(
this.caster.getUUID(), List.of(patternIota.getPattern()), 140
);
IXplatAbstractions.INSTANCE.sendPacketToPlayer(this.caster, packet);
IXplatAbstractions.INSTANCE.sendPacketTracking(this.caster, packet);
castPatterns.add(patternIota.getPattern());
}

// TODO: how do we know when to actually play this sound?
this.sound = this.sound.greaterOf(result.getSound());
}

@Override
public void postCast(CastingImage image) {
super.postCast(image);

var packet = new MsgNewSpiralPatternsS2C(
this.caster.getUUID(), castPatterns.stream().toList(), 140
);
IXplatAbstractions.INSTANCE.sendPacketToPlayer(this.caster, packet);
IXplatAbstractions.INSTANCE.sendPacketTracking(this.caster, packet);

castPatterns.clear();
}

@Override
public long extractMediaEnvironment(long costLeft) {
if (this.caster.isCreative())
Expand Down

0 comments on commit b29ab76

Please sign in to comment.