Skip to content
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
102 changes: 47 additions & 55 deletions build-logic/src/main/kotlin/rylween/gradle/RylweenBuildTasks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -128,44 +128,48 @@ abstract class RunInstalledLauncherWithJavaTask
"configure exactly one launcher smoke mode: screen or script"
}
val launcherArgs =
mutableListOf(
executableFile.get().asFile.path,
"--bundle",
bundleFile.get().asFile.path,
"--log",
logFile.get().asFile.path,
)
if (hasScreen) {
launcherArgs += listOf("--screen", screen.get())
}
if (userId.isPresent) {
launcherArgs += listOf("--user", userId.get())
}
if (stateSlug.isPresent) {
launcherArgs += listOf("--state", stateSlug.get())
}
if (hasScript) {
launcherArgs += listOf("--script", script.get())
}
if (title.orNull == true) {
launcherArgs += "--title"
}
if (seed.isPresent) {
launcherArgs += listOf("--seed", seed.get().toString())
}
if (debugLog.orNull == true) {
launcherArgs += "--debug-log"
}
if (debugSecrets.orNull == true) {
launcherArgs += "--debug-secrets"
}
if (loadFile.isPresent) {
launcherArgs += listOf("--load", loadFile.get().asFile.path)
}
if (saveFile.isPresent) {
if (resetSaveFile.orNull == true) saveFile.get().asFile.delete()
launcherArgs += listOf("--save", saveFile.get().asFile.path)
}
buildList {
addAll(
listOf(
executableFile.get().asFile.path,
"--bundle",
bundleFile.get().asFile.path,
"--log",
logFile.get().asFile.path,
),
)
if (hasScreen) {
addAll(listOf("--screen", screen.get()))
}
if (userId.isPresent) {
addAll(listOf("--user", userId.get()))
}
if (stateSlug.isPresent) {
addAll(listOf("--state", stateSlug.get()))
}
if (hasScript) {
addAll(listOf("--script", script.get()))
}
if (title.orNull == true) {
add("--title")
}
if (seed.isPresent) {
addAll(listOf("--seed", seed.get().toString()))
}
if (debugLog.orNull == true) {
add("--debug-log")
}
if (debugSecrets.orNull == true) {
add("--debug-secrets")
}
if (loadFile.isPresent) {
addAll(listOf("--load", loadFile.get().asFile.path))
}
if (saveFile.isPresent) {
if (resetSaveFile.orNull == true) saveFile.get().asFile.delete()
addAll(listOf("--save", saveFile.get().asFile.path))
}
}
val javaHome = javaLauncher.get().metadata.installationPath.asFile.absolutePath
val output = ByteArrayOutputStream()
val result =
Expand Down Expand Up @@ -1458,15 +1462,9 @@ abstract class GenerateGameCatalogHandlesTask : DefaultTask() {
)
}

private fun List<String>.distinctInOrder(): List<String> {
val seen = linkedSetOf<String>()
return filter { seen.add(it) }
}
private fun List<String>.distinctInOrder(): List<String> = distinct()

private fun List<SourceHandle>.distinctNamesInOrder(): List<String> {
val seen = linkedSetOf<String>()
return mapNotNull { handle -> handle.name.takeIf { seen.add(it) } }
}
private fun List<SourceHandle>.distinctNamesInOrder(): List<String> = map(SourceHandle::name).distinct()

private fun MatchResult.groupOffset(index: Int): Int = groups[index]?.range?.first ?: range.first

Expand Down Expand Up @@ -1497,15 +1495,9 @@ abstract class GenerateGameCatalogHandlesTask : DefaultTask() {

private fun String.sourceLocation(offset: Int): SourceLocation {
val boundedOffset = offset.coerceIn(0, length)
var line = 1
var lineStart = 0
for (index in 0 until boundedOffset) {
if (this[index] == '\n') {
line += 1
lineStart = index + 1
}
}
return SourceLocation(line, boundedOffset - lineStart + 1)
val before = take(boundedOffset)
val lineStart = before.lastIndexOf('\n') + 1
return SourceLocation(line = before.count { char -> char == '\n' } + 1, column = boundedOffset - lineStart + 1)
}

private fun validateNoDuplicateDeclarations(
Expand Down
41 changes: 28 additions & 13 deletions core/src/main/kotlin/rylween/compiled/CompiledBundle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ import rylween.dsl.impl.TownModel
import rylween.dsl.impl.Trigger
import rylween.dsl.impl.WorldMapModel
import rylween.dsl.impl.WorldPosition
import rylween.dsl.impl.mapCoordinates
import rylween.dsl.impl.runtimeContent
import rylween.runtime.CastleSessionSnapshot
import rylween.runtime.DungeonSavedStep
Expand All @@ -134,6 +135,8 @@ import rylween.runtime.RuntimeSnapshotModel
import rylween.runtime.ScriptContinuationSnapshot
import rylween.runtime.ScriptRootRef
import rylween.runtime.scriptRootCatalog
import kotlin.math.max
import kotlin.math.min

/**
* Complete compiled J1 bundle: content tables plus save/start state images.
Expand Down Expand Up @@ -881,6 +884,7 @@ enum class CompiledRuntimeTextPart {
HuntFoodGained,
UpkeepMemberName,
CampGuardName,
CastleThiefName,
PartyMemberCount,
PartyMemberMax,
WildernessDungeonName,
Expand All @@ -890,6 +894,7 @@ enum class CompiledRuntimeTextPart {
CurrentTownName,
TownTaxPercent,
TownTaxAmount,
TownTaxShortfall,
CreationMemberNumber,
CreationMemberName,
CreationWeaponSkill,
Expand All @@ -906,6 +911,7 @@ enum class CompiledRuntimeTextPart {
BattleLootName,
BattleLevelMemberName,
BattleLevelsGained,
BattleRoundNumber,
}

/** Screen/view command emitted from scripts and interpreted by runtime renderers. */
Expand Down Expand Up @@ -999,7 +1005,7 @@ sealed interface CompiledIntExpr {
}

/** Arithmetic operator for [CompiledIntExpr.Binary]. */
enum class CompiledBinaryOp { Add, Sub, Mul, Div, Mod }
enum class CompiledBinaryOp { Add, Sub, Mul, Div, Mod, Max, Min }

/** Applies a compiled integer binary operator using runtime-compatible divide-by-zero semantics. */
fun Int.applyCompiledBinary(
Expand All @@ -1012,6 +1018,8 @@ fun Int.applyCompiledBinary(
CompiledBinaryOp.Mul -> this * right
CompiledBinaryOp.Div -> this / (right.takeUnless { value -> value == 0 } ?: 1)
CompiledBinaryOp.Mod -> this % (right.takeUnless { value -> value == 0 } ?: 1)
CompiledBinaryOp.Max -> max(this, right)
CompiledBinaryOp.Min -> min(this, right)
}

/** Compiled boolean expression tree evaluated by scripts and runtime conditions. */
Expand Down Expand Up @@ -1239,18 +1247,13 @@ data class CompiledShopItemOffer(
sealed interface CompiledShopPrice {
data object ItemCost : CompiledShopPrice

data object HalfItemCost : CompiledShopPrice

data class Fixed(
val amount: Int,
) : CompiledShopPrice

data class LeaderLevelPlus(
val addend: Int,
) : CompiledShopPrice

data class ItemCostPlusLeaderLevelTimes(
val multiplier: Int,
/** A price from an authored formula, evaluated with the item's cost and the party leader's level. */
data class Formula(
val formula: CompiledHandleRef,
) : CompiledShopPrice
}

Expand Down Expand Up @@ -2105,6 +2108,8 @@ private class CompiledBundleCompiler(
"*", "mul" -> CompiledBinaryOp.Mul
"/", "div" -> CompiledBinaryOp.Div
"%", "mod" -> CompiledBinaryOp.Mod
"max" -> CompiledBinaryOp.Max
"min" -> CompiledBinaryOp.Min
else -> error("unknown int operator '$op'")
}

Expand Down Expand Up @@ -2171,12 +2176,21 @@ private class CompiledBundleCompiler(
)
}

// A mission's square is fixed content, or the placed location it waits on.
private fun missionCoordinate(id: MissionId): CompiledTextPart =
when (val trigger = game.missions.singleOrNull { mission -> mission.id == id }?.trigger) {
is Trigger.AtPosition -> CompiledTextPart.Literal(strings.id(trigger.point.mapCoordinates()))
is Trigger.AtLocation -> CompiledTextPart.LocationCoordinate(locationRef(trigger.location))
else -> error("text names the coordinates of mission ${id.slug}, which waits on no square")
}

private fun textPart(part: TextPart): CompiledTextPart =
when (part) {
is TextPart.Literal -> CompiledTextPart.Literal(strings.id(part.text))
is TextPart.IntValue -> CompiledTextPart.IntValue(intExpr(part.value))
is TextPart.StringValue -> CompiledTextPart.StringValue(stringExpr(part.value))
is TextPart.Pause -> error("{pause} markers only work in writeText and showTextAndWait")
is TextPart.LineColor -> CompiledTextPart.Literal(strings.id(part.color.escape))
is TextPart.RoleName -> CompiledTextPart.RoleName(roleRef(part.role))
is TextPart.RoleJob -> CompiledTextPart.RoleJob(roleRef(part.role))
is TextPart.RoleLocation -> CompiledTextPart.RoleLocation(roleRef(part.role))
Expand All @@ -2186,6 +2200,7 @@ private class CompiledBundleCompiler(
is TextPart.TownName -> CompiledTextPart.TownName(townRef(part.town))
is TextPart.FuzzyTownLocation -> CompiledTextPart.FuzzyTownLocation(townRef(part.town), part.radius)
is TextPart.LocationCoordinate -> CompiledTextPart.LocationCoordinate(locationRef(part.location))
is TextPart.MissionCoordinate -> missionCoordinate(part.mission)
TextPart.OfferedItemName -> CompiledTextPart.Runtime(CompiledRuntimeTextPart.OfferedItemName)
TextPart.OfferedItemPrice -> CompiledTextPart.Runtime(CompiledRuntimeTextPart.OfferedItemPrice)
TextPart.SelectedItemName -> CompiledTextPart.Runtime(CompiledRuntimeTextPart.SelectedItemName)
Expand All @@ -2198,6 +2213,7 @@ private class CompiledBundleCompiler(
TextPart.HuntFoodGained -> CompiledTextPart.Runtime(CompiledRuntimeTextPart.HuntFoodGained)
TextPart.UpkeepMemberName -> CompiledTextPart.Runtime(CompiledRuntimeTextPart.UpkeepMemberName)
TextPart.CampGuardName -> CompiledTextPart.Runtime(CompiledRuntimeTextPart.CampGuardName)
TextPart.CastleThiefName -> CompiledTextPart.Runtime(CompiledRuntimeTextPart.CastleThiefName)
TextPart.PartyMemberCount -> CompiledTextPart.Runtime(CompiledRuntimeTextPart.PartyMemberCount)
TextPart.PartyMemberMax -> CompiledTextPart.Runtime(CompiledRuntimeTextPart.PartyMemberMax)
TextPart.WildernessDungeonName -> CompiledTextPart.Runtime(CompiledRuntimeTextPart.WildernessDungeonName)
Expand All @@ -2207,6 +2223,7 @@ private class CompiledBundleCompiler(
TextPart.CurrentTownName -> CompiledTextPart.Runtime(CompiledRuntimeTextPart.CurrentTownName)
TextPart.TownTaxPercent -> CompiledTextPart.Runtime(CompiledRuntimeTextPart.TownTaxPercent)
TextPart.TownTaxAmount -> CompiledTextPart.Runtime(CompiledRuntimeTextPart.TownTaxAmount)
TextPart.TownTaxShortfall -> CompiledTextPart.Runtime(CompiledRuntimeTextPart.TownTaxShortfall)
TextPart.CreationMemberNumber -> CompiledTextPart.Runtime(CompiledRuntimeTextPart.CreationMemberNumber)
TextPart.CreationMemberName -> CompiledTextPart.Runtime(CompiledRuntimeTextPart.CreationMemberName)
TextPart.CreationWeaponSkill -> CompiledTextPart.Runtime(CompiledRuntimeTextPart.CreationWeaponSkill)
Expand All @@ -2223,6 +2240,7 @@ private class CompiledBundleCompiler(
TextPart.BattleLootName -> CompiledTextPart.Runtime(CompiledRuntimeTextPart.BattleLootName)
TextPart.BattleLevelMemberName -> CompiledTextPart.Runtime(CompiledRuntimeTextPart.BattleLevelMemberName)
TextPart.BattleLevelsGained -> CompiledTextPart.Runtime(CompiledRuntimeTextPart.BattleLevelsGained)
TextPart.BattleRoundNumber -> CompiledTextPart.Runtime(CompiledRuntimeTextPart.BattleRoundNumber)
}

private fun viewCommand(command: ViewCommand): CompiledViewCommand =
Expand Down Expand Up @@ -2345,11 +2363,8 @@ private class CompiledBundleCompiler(
private fun shopPrice(price: ShopPriceModel): CompiledShopPrice =
when (price) {
ShopPriceModel.ItemCost -> CompiledShopPrice.ItemCost
ShopPriceModel.HalfItemCost -> CompiledShopPrice.HalfItemCost
is ShopPriceModel.Fixed -> CompiledShopPrice.Fixed(price.amount)
is ShopPriceModel.LeaderLevelPlus -> CompiledShopPrice.LeaderLevelPlus(price.addend)
is ShopPriceModel.ItemCostPlusLeaderLevelTimes ->
CompiledShopPrice.ItemCostPlusLeaderLevelTimes(price.multiplier)
is ShopPriceModel.Formula -> CompiledShopPrice.Formula(formulaRef(price.formula))
}

private fun shopPurchase(purchase: ShopPurchaseModel): CompiledShopPurchase =
Expand Down
Loading
Loading