-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add ParentPhase class and update phase parents to use it #23
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
base: version/1.21.11
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,13 +52,19 @@ data class PhaseConfig( | |
| } | ||
| } | ||
|
|
||
| @ConfigSerializable | ||
| data class ParentPhase( | ||
| val id: String, | ||
| val weightOverride: Int? = null | ||
| ) | ||
|
|
||
| @ConfigSerializable | ||
| data class Phase( | ||
| val id: String, | ||
| val displayName: String = id.replaceFirstChar { it.uppercaseChar() }.replace('_', ' '), | ||
| val startsAt: Int, | ||
| val weight: Int, | ||
| val parents: List<String>, | ||
| val parents: List<ParentPhase>, | ||
|
||
| val blocks: List<BlockEntry>, | ||
| val entities: List<EntityEntry> | ||
| ) { | ||
|
|
@@ -92,29 +98,32 @@ data class PhaseConfig( | |
| var remainingBudget = parentBudget | ||
|
|
||
| var levelFactor = 1.0 | ||
| var levelFactorSum = 0.0 | ||
| val levelFactors = ArrayList<Double>(parents.size) | ||
| for (i in parents.indices) { | ||
| levelFactors += levelFactor | ||
| levelFactorSum += levelFactor | ||
| val levelFactors = parents.map { | ||
| val f = levelFactor | ||
| levelFactor *= PARENT_DECAY | ||
| f | ||
| } | ||
|
|
||
| for ((idx, parentId) in parents.withIndex()) { | ||
| val effectiveParentWeights = parents.mapIndexed { idx, parentPhase -> | ||
| val override = parentPhase.weightOverride?.toDouble() | ||
| (levelFactors[idx]) * (override ?: 1.0) | ||
| } | ||
|
|
||
| val totalParentWeight = effectiveParentWeights.sum() | ||
|
|
||
| for ((idx, parentPhase) in parents.withIndex()) { | ||
| if (remainingBudget <= 1e-9) break | ||
| val parent = config.findById(parentId) ?: continue | ||
| val parent = config.findById(parentPhase.id) ?: continue | ||
|
Comment on lines
+107
to
+116
|
||
| val parentBlocks = parent.blocks | ||
| if (parentBlocks.isEmpty()) continue | ||
|
|
||
| val parentRawTotal = parentBlocks.sumOf { it.weight.toDouble() } | ||
| if (parentRawTotal <= 0.0) continue | ||
|
|
||
| val shareForThisParent = if (levelFactorSum > 0.0) | ||
| parentBudget * (levelFactors[idx] / levelFactorSum) | ||
| else 0.0 | ||
| val shareForThisParent = | ||
| if (totalParentWeight > 0.0) | ||
| parentBudget * (effectiveParentWeights[idx] / totalParentWeight) | ||
| else 0.0 | ||
|
|
||
| val assigned = shareForThisParent.coerceAtMost(remainingBudget) | ||
| val scale = assigned / parentRawTotal | ||
| val scale = assigned / parentBlocks.sumOf { it.weight.toDouble() } | ||
|
|
||
| choices.ensureCapacity(choices.size + parentBlocks.size) | ||
| for (entry in parentBlocks) { | ||
|
|
@@ -144,34 +153,38 @@ data class PhaseConfig( | |
|
|
||
| val extraSteps = (this.weight - 1).coerceAtLeast(0) | ||
| val parentShare = (extraSteps * PARENT_SHARE_PER_WEIGHT).coerceIn(0.0, PARENT_SHARE_MAX) | ||
|
|
||
| val parentBudget = (if (ownTotal > 0.0) ownTotal else 1.0) * parentShare | ||
| var remainingBudget = parentBudget | ||
|
|
||
| var levelFactor = 1.0 | ||
| var levelFactorSum = 0.0 | ||
| val levelFactors = ArrayList<Double>(parents.size) | ||
| for (i in parents.indices) { | ||
| levelFactors += levelFactor | ||
| levelFactorSum += levelFactor | ||
| val levelFactors = parents.map { | ||
| val f = levelFactor | ||
| levelFactor *= PARENT_DECAY | ||
| f | ||
| } | ||
|
|
||
| val effectiveParentWeights = parents.mapIndexed { idx, parentPhase -> | ||
| val override = parentPhase.weightOverride?.toDouble() | ||
| (levelFactors[idx]) * (override ?: 1.0) | ||
| } | ||
|
|
||
| for ((idx, parentId) in parents.withIndex()) { | ||
| val totalParentWeight = effectiveParentWeights.sum() | ||
|
|
||
| for ((idx, parentPhase) in parents.withIndex()) { | ||
| if (remainingBudget <= 1e-9) break | ||
| val parent = config.findById(parentId) ?: continue | ||
| val parent = config.findById(parentPhase.id) ?: continue | ||
|
Comment on lines
+166
to
+175
|
||
| val parentEntities = parent.entities | ||
| if (parentEntities.isEmpty()) continue | ||
|
|
||
| val parentRawTotal = parentEntities.sumOf { it.weight } | ||
| if (parentRawTotal <= 0.0) continue | ||
|
|
||
| val shareForThisParent = if (levelFactorSum > 0.0) | ||
| parentBudget * (levelFactors[idx] / levelFactorSum) | ||
| else 0.0 | ||
| val shareForThisParent = | ||
| if (totalParentWeight > 0.0) | ||
| parentBudget * (effectiveParentWeights[idx] / totalParentWeight) | ||
| else 0.0 | ||
|
|
||
| val assigned = shareForThisParent.coerceAtMost(remainingBudget) | ||
| val scale = assigned / parentRawTotal | ||
| val parentTotal = parentEntities.sumOf { it.weight } | ||
| if (parentTotal <= 0.0) continue | ||
| val scale = assigned / parentTotal | ||
|
|
||
| for (e in parentEntities) { | ||
| val w = e.weight * scale | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing validation for weightOverride: The ParentPhase class accepts an optional weightOverride parameter but doesn't validate that it's non-negative. A negative or zero weightOverride could lead to unexpected behavior in the weight calculations. Consider adding an init block to ParentPhase that validates
weightOverride?.let { require(it > 0) { "weightOverride must be greater than 0 if specified" } }.