fix(common): let stat setters take zero and check totals (#55)

This commit is contained in:
Fiereu authored and GitHub committed 2026-07-25 16:43:10 +02:00
1 parent 391e2efeb7
commit e26c8f91f4
4 files changed
+92 -122

No files matched your search

@@ -17,41 +17,42 @@ open class PokemonStats(private val individualCap: Int, private val totalCap: In
val total val total
get() = hp + atk + def + spAtk + spDef + spd get() = hp + atk + def + spAtk + spDef + spd
private fun cr(value: Int): Byte { private fun checkedSet(stat: PokemonStat, value: Int) {
if (value <= 0) error("Value can't be <= 0") if (value < 0) error("Value can't be negative")
if (value > individualCap) error("Value exceeds individual capacity") if (value > individualCap) error("Value exceeds individual capacity")
if (total > totalCap) error("Total value exceeds capacity") if (total - (get(stat)!!.toInt() and 0xFF) + value > totalCap)
return value.toByte() error("Total value exceeds capacity")
set(stat, value.toByte())
} }
var hp var hp
get() = get(PokemonStat.HP)!!.toInt() get() = get(PokemonStat.HP)!!.toInt() and 0xFF
set(value) = set(PokemonStat.HP, cr(value)) set(value) = checkedSet(PokemonStat.HP, value)
var atk var atk
get() = get(PokemonStat.ATTACK)!!.toInt() get() = get(PokemonStat.ATTACK)!!.toInt() and 0xFF
set(value) = set(PokemonStat.ATTACK, cr(value)) set(value) = checkedSet(PokemonStat.ATTACK, value)
var def var def
get() = get(PokemonStat.DEFENSE)!!.toInt() get() = get(PokemonStat.DEFENSE)!!.toInt() and 0xFF
set(value) = set(PokemonStat.DEFENSE, cr(value)) set(value) = checkedSet(PokemonStat.DEFENSE, value)
var spAtk var spAtk
get() = get(PokemonStat.SP_ATTACK)!!.toInt() get() = get(PokemonStat.SP_ATTACK)!!.toInt() and 0xFF
set(value) = set(PokemonStat.SP_ATTACK, cr(value)) set(value) = checkedSet(PokemonStat.SP_ATTACK, value)
var spDef var spDef
get() = get(PokemonStat.SP_DEFENSE)!!.toInt() get() = get(PokemonStat.SP_DEFENSE)!!.toInt() and 0xFF
set(value) = set(PokemonStat.SP_DEFENSE, cr(value)) set(value) = checkedSet(PokemonStat.SP_DEFENSE, value)
var spd var spd
get() = get(PokemonStat.SPEED)!!.toInt() get() = get(PokemonStat.SPEED)!!.toInt() and 0xFF
set(value) = set(PokemonStat.SPEED, cr(value)) set(value) = checkedSet(PokemonStat.SPEED, value)
} }
class EVs : PokemonStats(252, 510) class EVs : PokemonStats(252, 510)
class IVs : PokemonStats(31, 155) class IVs : PokemonStats(31, 186)
fun IVs.compress(): Int = fun IVs.compress(): Int =
(((hp and 31) shl 0) or (((hp and 31) shl 0) or
@@ -70,69 +71,3 @@ fun decompressIVs(value: Int): IVs =
spDef = (value shr 20) and 31 spDef = (value shr 20) and 31
spd = (value shr 25) and 31 spd = (value shr 25) and 31
} }
enum class PokemonBattleStat {
HP,
ATTACK,
DEFENSE,
SP_ATTACK,
SP_DEFENSE,
SPEED,
ACCURACY,
EVASION
}
// Not sure if this is correct just copying from EVs here
class PokemonBattleStats :
EnumMap<PokemonBattleStat, Byte>(PokemonBattleStat.entries.associateWith { 0.toByte() }) {
private val individualCap: Int = 252
private val totalCap: Int = 510
val total
get() = hp + atk + def + spAtk + spDef + spd + acc + evs
private fun cr(value: Int): Byte {
if (value <= 0) error("Value can't be <= 0")
if (value > individualCap) error("Value exceeds individual capacity")
if (total > totalCap) error("Total value exceeds capacity")
return value.toByte()
}
var hp
get() = get(PokemonBattleStat.HP)!!.toInt()
set(value) = set(PokemonBattleStat.HP, cr(value))
var atk
get() = get(PokemonBattleStat.ATTACK)!!.toInt()
set(value) = set(PokemonBattleStat.ATTACK, cr(value))
var def
get() = get(PokemonBattleStat.DEFENSE)!!.toInt()
set(value) = set(PokemonBattleStat.DEFENSE, cr(value))
var spAtk
get() = get(PokemonBattleStat.SP_ATTACK)!!.toInt()
set(value) = set(PokemonBattleStat.SP_ATTACK, cr(value))
var spDef
get() = get(PokemonBattleStat.SP_DEFENSE)!!.toInt()
set(value) = set(PokemonBattleStat.SP_DEFENSE, cr(value))
var spd
get() = get(PokemonBattleStat.SPEED)!!.toInt()
set(value) = set(PokemonBattleStat.SPEED, cr(value))
var acc
get() = get(PokemonBattleStat.ACCURACY)!!.toInt()
set(value) = set(PokemonBattleStat.SPEED, value.toByte())
var evs
get() = get(PokemonBattleStat.EVASION)!!.toInt()
set(value) = set(PokemonBattleStat.EVASION, value.toByte())
init {
// just guessing that "252 / 2" => 100%
acc = 252 / 2
evs = 252 / 2
}
}
@@ -0,0 +1,49 @@
package de.fiereu.openmmo.common.enums
import io.kotest.assertions.throwables.shouldThrowAny
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
class PokemonStatsTest :
FunSpec({
test("zero is a valid stat value") {
val evs = EVs()
evs.atk = 10
evs.atk = 0
evs.atk shouldBe 0
}
test("the individual cap still rejects") {
shouldThrowAny { EVs().apply { hp = 253 } }
shouldThrowAny { IVs().apply { hp = 32 } }
}
test("the total cap counts the replaced value, not the old one") {
val evs = EVs()
evs.hp = 252
evs.atk = 252
shouldThrowAny { evs.def = 7 }
evs.def = 6
evs.total shouldBe 510
evs.hp = 100
evs.def = 158
evs.total shouldBe 510
}
test("a full set of perfect IVs fits the total cap") {
val ivs = IVs()
ivs.hp = 31
ivs.atk = 31
ivs.def = 31
ivs.spAtk = 31
ivs.spDef = 31
ivs.spd = 31
ivs.total shouldBe 186
}
test("an all-zero IV word decompresses and compresses back") {
val ivs = decompressIVs(0)
ivs.total shouldBe 0
ivs.compress() shouldBe 0
}
})
@@ -39,30 +39,20 @@ private fun packRarity(p: Pokemon): Int =
(if (p.isFatefulEncounter) PokemonRarityFlag.FATEFUL_ENCOUNTER.mask else 0) or (if (p.isFatefulEncounter) PokemonRarityFlag.FATEFUL_ENCOUNTER.mask else 0) or
(if (p.isRaidEncounter) PokemonRarityFlag.RAID_ENCOUNTER.mask else 0) (if (p.isRaidEncounter) PokemonRarityFlag.RAID_ENCOUNTER.mask else 0)
// EVs are six raw bytes in wire order hp, atk, def, spd, spAtk, spDef. Bypasses the checked stat // EVs are six raw bytes in wire order hp, atk, def, spd, spAtk, spDef.
// setters, which reject a zero value.
private fun evsFromWire(hp: Int, atk: Int, def: Int, spd: Int, spAtk: Int, spDef: Int): EVs = private fun evsFromWire(hp: Int, atk: Int, def: Int, spd: Int, spAtk: Int, spDef: Int): EVs =
EVs().apply { EVs().apply {
put(PokemonStat.HP, hp.toByte()) this.hp = hp
put(PokemonStat.ATTACK, atk.toByte()) this.atk = atk
put(PokemonStat.DEFENSE, def.toByte()) this.def = def
put(PokemonStat.SPEED, spd.toByte()) this.spd = spd
put(PokemonStat.SP_ATTACK, spAtk.toByte()) this.spAtk = spAtk
put(PokemonStat.SP_DEFENSE, spDef.toByte()) this.spDef = spDef
} }
// IVs are a packed 30-bit word (six 5-bit stats). The top two bits carry unidentified flags and are // IVs are a packed 30-bit word (six 5-bit stats). The top two bits carry unidentified flags and are
// dropped on write, so they stay zero for constructed monsters. Bypasses the checked IV setters, // dropped on write, so they stay zero for constructed monsters.
// which reject a zero roll. private fun ivsFromBits(bits: Int): IVs = decompressIVs(bits)
private fun ivsFromBits(bits: Int): IVs =
IVs().apply {
put(PokemonStat.HP, ((bits shr 0) and 31).toByte())
put(PokemonStat.ATTACK, ((bits shr 5) and 31).toByte())
put(PokemonStat.DEFENSE, ((bits shr 10) and 31).toByte())
put(PokemonStat.SP_ATTACK, ((bits shr 15) and 31).toByte())
put(PokemonStat.SP_DEFENSE, ((bits shr 20) and 31).toByte())
put(PokemonStat.SPEED, ((bits shr 25) and 31).toByte())
}
/** /**
* The 148-byte party/PC monster record. The named fields are decoded from real captures. The * The 148-byte party/PC monster record. The named fields are decoded from real captures. The
@@ -6,7 +6,6 @@ import de.fiereu.openmmo.common.PokemonMove
import de.fiereu.openmmo.common.enums.EVs import de.fiereu.openmmo.common.enums.EVs
import de.fiereu.openmmo.common.enums.IVs import de.fiereu.openmmo.common.enums.IVs
import de.fiereu.openmmo.common.enums.PokemonContainer import de.fiereu.openmmo.common.enums.PokemonContainer
import de.fiereu.openmmo.common.enums.PokemonStats
import de.fiereu.openmmo.db.game.tables.records.CharactersRecord import de.fiereu.openmmo.db.game.tables.records.CharactersRecord
import de.fiereu.openmmo.db.game.tables.records.PokemonRecord import de.fiereu.openmmo.db.game.tables.records.PokemonRecord
import de.fiereu.openmmo.db.game.tables.references.CHARACTERS import de.fiereu.openmmo.db.game.tables.references.CHARACTERS
@@ -236,26 +235,23 @@ constructor(
caughtAt = caughtAt, caughtAt = caughtAt,
) )
// The stat setters reject zero, so only set stats that are above it.
private fun PokemonStats.setPositive(
hp: Short?,
atk: Short?,
def: Short?,
spAtk: Short?,
spDef: Short?,
spd: Short?,
) {
hp?.toInt()?.let { if (it > 0) this.hp = it }
atk?.toInt()?.let { if (it > 0) this.atk = it }
def?.toInt()?.let { if (it > 0) this.def = it }
spAtk?.toInt()?.let { if (it > 0) this.spAtk = it }
spDef?.toInt()?.let { if (it > 0) this.spDef = it }
spd?.toInt()?.let { if (it > 0) this.spd = it }
}
private fun PokemonRecord.hydrateEvs(): EVs = private fun PokemonRecord.hydrateEvs(): EVs =
EVs().also { it.setPositive(evHp, evAtk, evDef, evSpAtk, evSpDef, evSpd) } EVs().also {
it.hp = (evHp ?: 0).toInt()
it.atk = (evAtk ?: 0).toInt()
it.def = (evDef ?: 0).toInt()
it.spAtk = (evSpAtk ?: 0).toInt()
it.spDef = (evSpDef ?: 0).toInt()
it.spd = (evSpd ?: 0).toInt()
}
private fun PokemonRecord.hydrateIvs(): IVs = private fun PokemonRecord.hydrateIvs(): IVs =
IVs().also { it.setPositive(ivHp, ivAtk, ivDef, ivSpAtk, ivSpDef, ivSpd) } IVs().also {
it.hp = (ivHp ?: 0).toInt()
it.atk = (ivAtk ?: 0).toInt()
it.def = (ivDef ?: 0).toInt()
it.spAtk = (ivSpAtk ?: 0).toInt()
it.spDef = (ivSpDef ?: 0).toInt()
it.spd = (ivSpd ?: 0).toInt()
}
} }