Skip to content

Commit 9a4db67

Browse files
s5bugSirius902
andcommitted
Use Vector in many places in the VM
Co-authored-by: Sirius902 <10891979+Sirius902@users.noreply.github.com>
1 parent a0cb154 commit 9a4db67

File tree

176 files changed

+545
-405
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

176 files changed

+545
-405
lines changed

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

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import kotlin.math.abs
2323
import kotlin.math.roundToInt
2424
import kotlin.math.roundToLong
2525

26-
fun List<Iota>.getDouble(idx: Int, argc: Int = 0): Double {
26+
fun Vector<Iota>.getDouble(idx: Int, argc: Int = 0): Double {
2727
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
2828
if (x is DoubleIota) {
2929
return x.double
@@ -33,7 +33,7 @@ fun List<Iota>.getDouble(idx: Int, argc: Int = 0): Double {
3333
}
3434
}
3535

36-
fun List<Iota>.getEntity(idx: Int, argc: Int = 0): Entity {
36+
fun Vector<Iota>.getEntity(idx: Int, argc: Int = 0): Entity {
3737
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
3838
if (x is EntityIota) {
3939
return x.entity
@@ -42,7 +42,7 @@ fun List<Iota>.getEntity(idx: Int, argc: Int = 0): Entity {
4242
}
4343
}
4444

45-
fun List<Iota>.getList(idx: Int, argc: Int = 0): Vector<Iota> {
45+
fun Vector<Iota>.getList(idx: Int, argc: Int = 0): Vector<Iota> {
4646
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
4747
if (x is ListIota) {
4848
return x.list
@@ -51,7 +51,7 @@ fun List<Iota>.getList(idx: Int, argc: Int = 0): Vector<Iota> {
5151
}
5252
}
5353

54-
fun List<Iota>.getPattern(idx: Int, argc: Int = 0): HexPattern {
54+
fun Vector<Iota>.getPattern(idx: Int, argc: Int = 0): HexPattern {
5555
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
5656
if (x is PatternIota) {
5757
return x.pattern
@@ -60,7 +60,7 @@ fun List<Iota>.getPattern(idx: Int, argc: Int = 0): HexPattern {
6060
}
6161
}
6262

63-
fun List<Iota>.getVec3(idx: Int, argc: Int = 0): Vec3 {
63+
fun Vector<Iota>.getVec3(idx: Int, argc: Int = 0): Vec3 {
6464
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
6565
if (x is Vec3Iota) {
6666
return x.vec3
@@ -69,7 +69,7 @@ fun List<Iota>.getVec3(idx: Int, argc: Int = 0): Vec3 {
6969
}
7070
}
7171

72-
fun List<Iota>.getBool(idx: Int, argc: Int = 0): Boolean {
72+
fun Vector<Iota>.getBool(idx: Int, argc: Int = 0): Boolean {
7373
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
7474
if (x is BooleanIota) {
7575
return x.bool
@@ -80,7 +80,7 @@ fun List<Iota>.getBool(idx: Int, argc: Int = 0): Boolean {
8080

8181
// Helpers
8282

83-
fun List<Iota>.getItemEntity(idx: Int, argc: Int = 0): ItemEntity {
83+
fun Vector<Iota>.getItemEntity(idx: Int, argc: Int = 0): ItemEntity {
8484
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
8585
if (x is EntityIota) {
8686
val e = x.entity
@@ -90,7 +90,7 @@ fun List<Iota>.getItemEntity(idx: Int, argc: Int = 0): ItemEntity {
9090
throw MishapInvalidIota.ofType(x, if (argc == 0) idx else argc - (idx + 1), "entity.item")
9191
}
9292

93-
fun List<Iota>.getPlayer(idx: Int, argc: Int = 0): ServerPlayer {
93+
fun Vector<Iota>.getPlayer(idx: Int, argc: Int = 0): ServerPlayer {
9494
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
9595
if (x is EntityIota) {
9696
val e = x.entity
@@ -100,7 +100,7 @@ fun List<Iota>.getPlayer(idx: Int, argc: Int = 0): ServerPlayer {
100100
throw MishapInvalidIota.ofType(x, if (argc == 0) idx else argc - (idx + 1), "entity.player")
101101
}
102102

103-
fun List<Iota>.getMob(idx: Int, argc: Int = 0): Mob {
103+
fun Vector<Iota>.getMob(idx: Int, argc: Int = 0): Mob {
104104
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
105105
if (x is EntityIota) {
106106
val e = x.entity
@@ -110,7 +110,7 @@ fun List<Iota>.getMob(idx: Int, argc: Int = 0): Mob {
110110
throw MishapInvalidIota.ofType(x, if (argc == 0) idx else argc - (idx + 1), "entity.mob")
111111
}
112112

113-
fun List<Iota>.getLivingEntityButNotArmorStand(idx: Int, argc: Int = 0): LivingEntity {
113+
fun Vector<Iota>.getLivingEntityButNotArmorStand(idx: Int, argc: Int = 0): LivingEntity {
114114
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
115115
if (x is EntityIota) {
116116
val e = x.entity
@@ -120,7 +120,7 @@ fun List<Iota>.getLivingEntityButNotArmorStand(idx: Int, argc: Int = 0): LivingE
120120
throw MishapInvalidIota.ofType(x, if (argc == 0) idx else argc - (idx + 1), "entity.living")
121121
}
122122

123-
fun List<Iota>.getPositiveDouble(idx: Int, argc: Int = 0): Double {
123+
fun Vector<Iota>.getPositiveDouble(idx: Int, argc: Int = 0): Double {
124124
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
125125
if (x is DoubleIota) {
126126
val double = x.double
@@ -131,7 +131,7 @@ fun List<Iota>.getPositiveDouble(idx: Int, argc: Int = 0): Double {
131131
throw MishapInvalidIota.of(x, if (argc == 0) idx else argc - (idx + 1), "double.positive")
132132
}
133133

134-
fun List<Iota>.getPositiveDoubleUnder(idx: Int, max: Double, argc: Int = 0): Double {
134+
fun Vector<Iota>.getPositiveDoubleUnder(idx: Int, max: Double, argc: Int = 0): Double {
135135
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
136136
if (x is DoubleIota) {
137137
val double = x.double
@@ -142,7 +142,7 @@ fun List<Iota>.getPositiveDoubleUnder(idx: Int, max: Double, argc: Int = 0): Dou
142142
throw MishapInvalidIota.of(x, if (argc == 0) idx else argc - (idx + 1), "double.positive.less", max)
143143
}
144144

145-
fun List<Iota>.getPositiveDoubleUnderInclusive(idx: Int, max: Double, argc: Int = 0): Double {
145+
fun Vector<Iota>.getPositiveDoubleUnderInclusive(idx: Int, max: Double, argc: Int = 0): Double {
146146
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
147147
if (x is DoubleIota) {
148148
val double = x.double
@@ -153,7 +153,7 @@ fun List<Iota>.getPositiveDoubleUnderInclusive(idx: Int, max: Double, argc: Int
153153
throw MishapInvalidIota.of(x, if (argc == 0) idx else argc - (idx + 1), "double.positive.less.equal", max)
154154
}
155155

156-
fun List<Iota>.getDoubleBetween(idx: Int, min: Double, max: Double, argc: Int = 0): Double {
156+
fun Vector<Iota>.getDoubleBetween(idx: Int, min: Double, max: Double, argc: Int = 0): Double {
157157
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
158158
if (x is DoubleIota) {
159159
val double = x.double
@@ -164,7 +164,7 @@ fun List<Iota>.getDoubleBetween(idx: Int, min: Double, max: Double, argc: Int =
164164
throw MishapInvalidIota.of(x, if (argc == 0) idx else argc - (idx + 1), "double.between", min, max)
165165
}
166166

167-
fun List<Iota>.getInt(idx: Int, argc: Int = 0): Int {
167+
fun Vector<Iota>.getInt(idx: Int, argc: Int = 0): Int {
168168
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
169169
if (x is DoubleIota) {
170170
val double = x.double
@@ -176,7 +176,7 @@ fun List<Iota>.getInt(idx: Int, argc: Int = 0): Int {
176176
throw MishapInvalidIota.of(x, if (argc == 0) idx else argc - (idx + 1), "int")
177177
}
178178

179-
fun List<Iota>.getLong(idx: Int, argc: Int = 0): Long {
179+
fun Vector<Iota>.getLong(idx: Int, argc: Int = 0): Long {
180180
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
181181
if (x is DoubleIota) {
182182
val double = x.double
@@ -188,7 +188,7 @@ fun List<Iota>.getLong(idx: Int, argc: Int = 0): Long {
188188
throw MishapInvalidIota.of(x, if (argc == 0) idx else argc - (idx + 1), "int") // shh we're lying
189189
}
190190

191-
fun List<Iota>.getPositiveInt(idx: Int, argc: Int = 0): Int {
191+
fun Vector<Iota>.getPositiveInt(idx: Int, argc: Int = 0): Int {
192192
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
193193
if (x is DoubleIota) {
194194
val double = x.double
@@ -200,7 +200,7 @@ fun List<Iota>.getPositiveInt(idx: Int, argc: Int = 0): Int {
200200
throw MishapInvalidIota.of(x, if (argc == 0) idx else argc - (idx + 1), "int.positive")
201201
}
202202

203-
fun List<Iota>.getPositiveLong(idx: Int, argc: Int = 0): Long {
203+
fun Vector<Iota>.getPositiveLong(idx: Int, argc: Int = 0): Long {
204204
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
205205
if (x is DoubleIota) {
206206
val double = x.double
@@ -212,7 +212,7 @@ fun List<Iota>.getPositiveLong(idx: Int, argc: Int = 0): Long {
212212
throw MishapInvalidIota.of(x, if (argc == 0) idx else argc - (idx + 1), "int.positive")
213213
}
214214

215-
fun List<Iota>.getPositiveIntUnder(idx: Int, max: Int, argc: Int = 0): Int {
215+
fun Vector<Iota>.getPositiveIntUnder(idx: Int, max: Int, argc: Int = 0): Int {
216216
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
217217
if (x is DoubleIota) {
218218
val double = x.double
@@ -224,7 +224,7 @@ fun List<Iota>.getPositiveIntUnder(idx: Int, max: Int, argc: Int = 0): Int {
224224
throw MishapInvalidIota.of(x, if (argc == 0) idx else argc - (idx + 1), "int.positive.less", max)
225225
}
226226

227-
fun List<Iota>.getPositiveIntUnderInclusive(idx: Int, max: Int, argc: Int = 0): Int {
227+
fun Vector<Iota>.getPositiveIntUnderInclusive(idx: Int, max: Int, argc: Int = 0): Int {
228228
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
229229
if (x is DoubleIota) {
230230
val double = x.double
@@ -236,7 +236,7 @@ fun List<Iota>.getPositiveIntUnderInclusive(idx: Int, max: Int, argc: Int = 0):
236236
throw MishapInvalidIota.of(x, if (argc == 0) idx else argc - (idx + 1), "int.positive.less.equal", max)
237237
}
238238

239-
fun List<Iota>.getIntBetween(idx: Int, min: Int, max: Int, argc: Int = 0): Int {
239+
fun Vector<Iota>.getIntBetween(idx: Int, min: Int, max: Int, argc: Int = 0): Int {
240240
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
241241
if (x is DoubleIota) {
242242
val double = x.double
@@ -248,7 +248,7 @@ fun List<Iota>.getIntBetween(idx: Int, min: Int, max: Int, argc: Int = 0): Int {
248248
throw MishapInvalidIota.of(x, if (argc == 0) idx else argc - (idx + 1), "int.between", min, max)
249249
}
250250

251-
fun List<Iota>.getBlockPos(idx: Int, argc: Int = 0): BlockPos {
251+
fun Vector<Iota>.getBlockPos(idx: Int, argc: Int = 0): BlockPos {
252252
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
253253
if (x is Vec3Iota) {
254254
return BlockPos.containing(x.vec3)
@@ -257,7 +257,7 @@ fun List<Iota>.getBlockPos(idx: Int, argc: Int = 0): BlockPos {
257257
throw MishapInvalidIota.ofType(x, if (argc == 0) idx else argc - (idx + 1), "vector")
258258
}
259259

260-
fun List<Iota>.getNumOrVec(idx: Int, argc: Int = 0): Either<Double, Vec3> {
260+
fun Vector<Iota>.getNumOrVec(idx: Int, argc: Int = 0): Either<Double, Vec3> {
261261
val datum = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
262262
return when (datum) {
263263
is DoubleIota -> Either.left(datum.double)
@@ -270,7 +270,7 @@ fun List<Iota>.getNumOrVec(idx: Int, argc: Int = 0): Either<Double, Vec3> {
270270
}
271271
}
272272

273-
fun List<Iota>.getLongOrList(idx: Int, argc: Int = 0): Either<Long, Vector<Iota>> {
273+
fun Vector<Iota>.getLongOrList(idx: Int, argc: Int = 0): Either<Long, Vector<Iota>> {
274274
val datum = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
275275
if (datum is DoubleIota) {
276276
val double = datum.double
@@ -309,15 +309,15 @@ fun aplKinnie(operatee: Either<Double, Vec3>, fn: DoubleUnaryOperator): Iota =
309309
{ vec -> Vec3Iota(Vec3(fn.applyAsDouble(vec.x), fn.applyAsDouble(vec.y), fn.applyAsDouble(vec.z))) }
310310
)
311311

312-
inline val Boolean.asActionResult get() = listOf(BooleanIota(this))
313-
inline val Double.asActionResult get() = listOf(DoubleIota(this))
314-
inline val Number.asActionResult get() = listOf(DoubleIota(this.toDouble()))
312+
inline val Boolean.asActionResult: Vector<Iota> get() = Vector.from(listOf(BooleanIota(this)))
313+
inline val Double.asActionResult: Vector<Iota> get() = Vector.from(listOf(DoubleIota(this)))
314+
inline val Number.asActionResult: Vector<Iota> get() = Vector.from(listOf(DoubleIota(this.toDouble())))
315315

316-
inline val List<Iota>.asActionResult get() = listOf(ListIota(Vector.from(this)))
316+
inline val Vector<Iota>.asActionResult: Vector<Iota> get() = Vector.from(listOf(ListIota(this)))
317317

318-
inline val BlockPos.asActionResult get() = listOf(Vec3Iota(Vec3.atCenterOf(this)))
319-
inline val Vector3f.asActionResult get() = listOf(Vec3Iota(Vec3(this)))
320-
inline val Vec3.asActionResult get() = listOf(Vec3Iota(this))
318+
inline val BlockPos.asActionResult: Vector<Iota> get() = Vector.from(listOf(Vec3Iota(Vec3.atCenterOf(this))))
319+
inline val Vector3f.asActionResult: Vector<Iota> get() = Vector.from(listOf(Vec3Iota(Vec3(this))))
320+
inline val Vec3.asActionResult: Vector<Iota> get() = Vector.from(listOf(Vec3Iota(this)))
321321

322-
inline val Entity?.asActionResult get() = listOf(if (this == null) NullIota() else EntityIota(this))
323-
inline val HexPattern.asActionResult get() = listOf(PatternIota(this))
322+
inline val Entity?.asActionResult: Vector<Iota> get() = Vector.from(listOf(if (this == null) NullIota() else EntityIota(this)))
323+
inline val HexPattern.asActionResult: Vector<Iota> get() = Vector.from(listOf(PatternIota(this)))

Common/src/main/java/at/petrak/hexcasting/api/casting/arithmetic/operator/OperatorBasic.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,22 @@ 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.Mishap
10+
import at.petrak.hexcasting.api.utils.Vector
1011
import at.petrak.hexcasting.common.lib.hex.HexEvalSounds
1112
import java.util.function.Consumer
1213

1314
abstract class OperatorBasic(arity: Int, accepts: IotaMultiPredicate) : Operator(arity, accepts) {
1415

1516
@Throws(Mishap::class)
1617
override fun operate(env: CastingEnvironment, image: CastingImage, continuation: SpellContinuation): OperationResult {
17-
val stack = image.stack.toMutableList()
18-
val args = stack.takeLast(arity)
19-
repeat(arity) { stack.removeLast() }
18+
val stack = Vector.VectorBuilder<Iota>()
19+
stack.addAll(image.stack.dropRight(arity))
20+
val args = image.stack.takeRight(arity)
2021

2122
val ret = apply(args, env)
22-
ret.forEach(Consumer { e: Iota -> stack.add(e) })
23+
ret.forEach(Consumer { e: Iota -> stack.addOne(e) })
2324

24-
val image2 = image.copy(stack = stack, opsConsumed = image.opsConsumed + 1)
25+
val image2 = image.copy(stack = stack.result(), opsConsumed = image.opsConsumed + 1)
2526
return OperationResult(image2, listOf(), continuation, HexEvalSounds.NORMAL_EXECUTE)
2627
}
2728

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import at.petrak.hexcasting.api.casting.eval.OperationResult
55
import at.petrak.hexcasting.api.casting.eval.vm.CastingImage
66
import at.petrak.hexcasting.api.casting.eval.vm.SpellContinuation
77
import at.petrak.hexcasting.api.casting.iota.Iota
8+
import at.petrak.hexcasting.api.utils.Vector
89
import net.minecraft.world.phys.Vec3
910
import java.text.DecimalFormat
1011

@@ -54,8 +55,8 @@ interface Action {
5455
override val argc: Int
5556
get() = 0
5657

57-
override fun execute(args: List<Iota>, env: CastingEnvironment): List<Iota> =
58-
listOf(x)
58+
override fun execute(args: Vector<Iota>, env: CastingEnvironment): Vector<Iota> =
59+
Vector.from(listOf(x))
5960
}
6061

6162
public val DOUBLE_FORMATTER = DecimalFormat("####.####")

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ 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
1010
import at.petrak.hexcasting.api.casting.mishaps.MishapNotEnoughMedia
11+
import at.petrak.hexcasting.api.utils.Vector
1112
import at.petrak.hexcasting.common.lib.hex.HexEvalSounds
1213

1314
/**
@@ -18,30 +19,30 @@ interface ConstMediaAction : Action {
1819
val mediaCost: Long
1920
get() = 0
2021

21-
fun execute(args: List<Iota>, env: CastingEnvironment): List<Iota>
22+
fun execute(args: Vector<Iota>, env: CastingEnvironment): Vector<Iota>
2223

23-
fun executeWithOpCount(args: List<Iota>, env: CastingEnvironment): CostMediaActionResult {
24+
fun executeWithOpCount(args: Vector<Iota>, env: CastingEnvironment): CostMediaActionResult {
2425
val stack = this.execute(args, env)
2526
return CostMediaActionResult(stack)
2627
}
2728

2829
override fun operate(env: CastingEnvironment, image: CastingImage, continuation: SpellContinuation): OperationResult {
29-
val stack = image.stack.toMutableList()
30+
val stack = Vector.VectorBuilder<Iota>()
3031

3132
if (env.extractMedia(this.mediaCost, true) > 0)
3233
throw MishapNotEnoughMedia(this.mediaCost)
33-
if (this.argc > stack.size)
34-
throw MishapNotEnoughArgs(this.argc, stack.size)
35-
val args = stack.takeLast(this.argc)
36-
repeat(this.argc) { stack.removeLast() }
34+
if (this.argc > image.stack.size)
35+
throw MishapNotEnoughArgs(this.argc, image.stack.size)
36+
val args = image.stack.takeRight(this.argc)
37+
stack.addAll(image.stack.dropRight(this.argc))
3738
val result = this.executeWithOpCount(args, env)
3839
stack.addAll(result.resultStack)
3940

4041
val sideEffects = mutableListOf<OperatorSideEffect>(OperatorSideEffect.ConsumeMedia(this.mediaCost))
4142

42-
val image2 = image.copy(stack = stack, opsConsumed = image.opsConsumed + result.opCount)
43+
val image2 = image.copy(stack = stack.result(), opsConsumed = image.opsConsumed + result.opCount)
4344
return OperationResult(image2, sideEffects, continuation, HexEvalSounds.NORMAL_EXECUTE)
4445
}
4546

46-
data class CostMediaActionResult(val resultStack: List<Iota>, val opCount: Long = 1)
47+
data class CostMediaActionResult(val resultStack: Vector<Iota>, val opCount: Long = 1)
4748
}

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ 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
1212
import at.petrak.hexcasting.api.casting.mishaps.MishapNotEnoughMedia
13+
import at.petrak.hexcasting.api.utils.Vector
1314
import at.petrak.hexcasting.common.lib.hex.HexEvalSounds
1415
import net.minecraft.nbt.CompoundTag
1516

@@ -21,23 +22,23 @@ interface SpellAction : Action {
2122
fun awardsCastingStat(ctx: CastingEnvironment): Boolean = true
2223

2324
fun execute(
24-
args: List<Iota>,
25+
args: Vector<Iota>,
2526
env: CastingEnvironment
2627
): Result
2728

2829
fun executeWithUserdata(
29-
args: List<Iota>, env: CastingEnvironment, userData: CompoundTag
30+
args: Vector<Iota>, env: CastingEnvironment, userData: CompoundTag
3031
): Result {
3132
return this.execute(args, env)
3233
}
3334

3435
override fun operate(env: CastingEnvironment, image: CastingImage, continuation: SpellContinuation): OperationResult {
35-
val stack = image.stack.toMutableList()
36+
val stack = Vector.VectorBuilder<Iota>()
3637

37-
if (this.argc > stack.size)
38-
throw MishapNotEnoughArgs(this.argc, stack.size)
39-
val args = stack.takeLast(this.argc)
40-
for (_i in 0 until this.argc) stack.removeLast()
38+
if (this.argc > image.stack.size)
39+
throw MishapNotEnoughArgs(this.argc, image.stack.size)
40+
val args = image.stack.takeRight(this.argc)
41+
stack.addAll(image.stack.dropRight(this.argc))
4142

4243
// execute!
4344
val userDataMut = image.userData.copy()
@@ -61,7 +62,7 @@ interface SpellAction : Action {
6162
for (spray in result.particles)
6263
sideEffects.add(OperatorSideEffect.Particles(spray))
6364

64-
val image2 = image.copy(stack = stack, opsConsumed = image.opsConsumed + result.opCount, userData = userDataMut)
65+
val image2 = image.copy(stack = stack.result(), opsConsumed = image.opsConsumed + result.opCount, userData = userDataMut)
6566

6667
val sound = if (this.hasCastingSound(env)) HexEvalSounds.SPELL else HexEvalSounds.MUTE
6768
return OperationResult(image2, sideEffects, continuation, sound)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ sealed class OperatorSideEffect {
6666
)
6767
)
6868

69-
harness.image = harness.image.copy(stack = mishap.executeReturnStack(harness.env, errorCtx, harness.image.stack.toMutableList()))
69+
harness.image = harness.image.copy(stack = mishap.executeReturnStack(harness.env, errorCtx, harness.image.stack))
7070
}
7171
}
7272
}

0 commit comments

Comments
 (0)