fix(common): let stat setters take zero and check totals (#55)
This commit is contained in:
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
|
||||
get() = hp + atk + def + spAtk + spDef + spd
|
||||
|
||||
private fun cr(value: Int): Byte {
|
||||
if (value <= 0) error("Value can't be <= 0")
|
||||
private fun checkedSet(stat: PokemonStat, value: Int) {
|
||||
if (value < 0) error("Value can't be negative")
|
||||
if (value > individualCap) error("Value exceeds individual capacity")
|
||||
if (total > totalCap) error("Total value exceeds capacity")
|
||||
return value.toByte()
|
||||
if (total - (get(stat)!!.toInt() and 0xFF) + value > totalCap)
|
||||
error("Total value exceeds capacity")
|
||||
set(stat, value.toByte())
|
||||
}
|
||||
|
||||
var hp
|
||||
get() = get(PokemonStat.HP)!!.toInt()
|
||||
set(value) = set(PokemonStat.HP, cr(value))
|
||||
get() = get(PokemonStat.HP)!!.toInt() and 0xFF
|
||||
set(value) = checkedSet(PokemonStat.HP, value)
|
||||
|
||||
var atk
|
||||
get() = get(PokemonStat.ATTACK)!!.toInt()
|
||||
set(value) = set(PokemonStat.ATTACK, cr(value))
|
||||
get() = get(PokemonStat.ATTACK)!!.toInt() and 0xFF
|
||||
set(value) = checkedSet(PokemonStat.ATTACK, value)
|
||||
|
||||
var def
|
||||
get() = get(PokemonStat.DEFENSE)!!.toInt()
|
||||
set(value) = set(PokemonStat.DEFENSE, cr(value))
|
||||
get() = get(PokemonStat.DEFENSE)!!.toInt() and 0xFF
|
||||
set(value) = checkedSet(PokemonStat.DEFENSE, value)
|
||||
|
||||
var spAtk
|
||||
get() = get(PokemonStat.SP_ATTACK)!!.toInt()
|
||||
set(value) = set(PokemonStat.SP_ATTACK, cr(value))
|
||||
get() = get(PokemonStat.SP_ATTACK)!!.toInt() and 0xFF
|
||||
set(value) = checkedSet(PokemonStat.SP_ATTACK, value)
|
||||
|
||||
var spDef
|
||||
get() = get(PokemonStat.SP_DEFENSE)!!.toInt()
|
||||
set(value) = set(PokemonStat.SP_DEFENSE, cr(value))
|
||||
get() = get(PokemonStat.SP_DEFENSE)!!.toInt() and 0xFF
|
||||
set(value) = checkedSet(PokemonStat.SP_DEFENSE, value)
|
||||
|
||||
var spd
|
||||
get() = get(PokemonStat.SPEED)!!.toInt()
|
||||
set(value) = set(PokemonStat.SPEED, cr(value))
|
||||
get() = get(PokemonStat.SPEED)!!.toInt() and 0xFF
|
||||
set(value) = checkedSet(PokemonStat.SPEED, value)
|
||||
}
|
||||
|
||||
class EVs : PokemonStats(252, 510)
|
||||
|
||||
class IVs : PokemonStats(31, 155)
|
||||
class IVs : PokemonStats(31, 186)
|
||||
|
||||
fun IVs.compress(): Int =
|
||||
(((hp and 31) shl 0) or
|
||||
@@ -70,69 +71,3 @@ fun decompressIVs(value: Int): IVs =
|
||||
spDef = (value shr 20) 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.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
|
||||
// setters, which reject a zero value.
|
||||
// EVs are six raw bytes in wire order hp, atk, def, spd, spAtk, spDef.
|
||||
private fun evsFromWire(hp: Int, atk: Int, def: Int, spd: Int, spAtk: Int, spDef: Int): EVs =
|
||||
EVs().apply {
|
||||
put(PokemonStat.HP, hp.toByte())
|
||||
put(PokemonStat.ATTACK, atk.toByte())
|
||||
put(PokemonStat.DEFENSE, def.toByte())
|
||||
put(PokemonStat.SPEED, spd.toByte())
|
||||
put(PokemonStat.SP_ATTACK, spAtk.toByte())
|
||||
put(PokemonStat.SP_DEFENSE, spDef.toByte())
|
||||
this.hp = hp
|
||||
this.atk = atk
|
||||
this.def = def
|
||||
this.spd = spd
|
||||
this.spAtk = spAtk
|
||||
this.spDef = spDef
|
||||
}
|
||||
|
||||
// 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,
|
||||
// which reject a zero roll.
|
||||
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())
|
||||
}
|
||||
// dropped on write, so they stay zero for constructed monsters.
|
||||
private fun ivsFromBits(bits: Int): IVs = decompressIVs(bits)
|
||||
|
||||
/**
|
||||
* The 148-byte party/PC monster record. The named fields are decoded from real captures. The
|
||||
|
||||
+16
-20
@@ -6,7 +6,6 @@ import de.fiereu.openmmo.common.PokemonMove
|
||||
import de.fiereu.openmmo.common.enums.EVs
|
||||
import de.fiereu.openmmo.common.enums.IVs
|
||||
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.PokemonRecord
|
||||
import de.fiereu.openmmo.db.game.tables.references.CHARACTERS
|
||||
@@ -236,26 +235,23 @@ constructor(
|
||||
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 =
|
||||
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 =
|
||||
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()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user