mirror of
https://github.com/DramaticShape/DramaticShapeVoxelMod.git
synced 2026-08-12 13:00:50 +02:00
a3fb18a589
Gen 1 has no shininess of its own, but it has the four DVs Gen 2 reads to decide it -- and the engine already ships that reading (Stats.isShiny, its own comment calling it "the RBY virtual shiny", allowlisted for mods precisely so an indicator mod can call it). Nothing new is stored on a Pokemon and nothing migrates: every save already contains the answer, and this starts drawing it. Random DVs land on the pattern 1 in 8192, which is the classic rate and the default the odds dial ships at. Deriving rather than storing is what makes it survive a save, a box, a trade and an evolution with no second copy of the truth to drift. mon.shiny is a cache written from the DVs, never read as the source. The roll goes in Pokemon.new -- every wild, gift, starter and traded mon is built there, and it is before the battle bakes its sprite, which battle.started is already too late for. It draws from the mod's own random stream so installing this does not shift the sequence damage rolls and encounter slots come out of. Trainers stay ordinary by themselves: the engine pins their DVs, as the real games do. The models are genuinely recoloured, as part of the extraction. Each species is decoded once, packed as usual, then recoloured and packed again as NNNs.dsm. The colours are Stadium's own HSL slide (hue in degrees, saturation and lightness on a -8..+8 scale at 12.5% a step); five species carry an explicit colour table instead, because Stadium gives them a real alternate texture that no single slide reproduces -- Jigglypuff's body must stay pink while its irises rotate to green. Extraction is the right moment because StadiumFx's generated frames are still marked there and the packer drops the marker: it is the last point a flame is distinguishable from a hide. A shiny Charizard has a shiny hide and an ordinary fire. The normal packs are written BEFORE the recolour, so they come out byte-identical and stadium_extract_test still diffs all 151 against the Python oracle unchanged -- no format change, no DSM4, no second implementation to keep in step. REV goes to 3 so an existing cache rebuilds. Flat art is tinted instead, because the engine bakes a species palette into a cache with no notion of which individual is drawn. The tint comes from that species' own slide rather than a generic gold. A multiply can only darken, so species whose shiny is lighter read quieter there than on the model; the status page's star is the mode-proof mark. Tests: 58 assertions in tests/shiny_test.lua, including the colour transform against 640 real colour pairs lifted from the verified texture set, the DV model, the read side, and the end-to-end through the engine's own constructor. stadium_extract_test gains --mod (worktrees have neither the ROM nor the packs, both gitignored) and now also checks that every shiny pack is the same length as its twin and actually differs. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
305 lines
11 KiB
Lua
305 lines
11 KiB
Lua
-- The shiny system, headless.
|
|
--
|
|
-- luajit mods/DramaticShapeVoxelMod/tests/shiny_test.lua [--mod=DIR]
|
|
--
|
|
-- Run from the PROJECT ROOT (it requires src.pokemon.Stats, the engine's own
|
|
-- shiny predicate -- the point being that we agree with the engine rather
|
|
-- than carry a second copy of the rule).
|
|
--
|
|
-- Two halves:
|
|
--
|
|
-- the DV model that a decided mon really does satisfy the Gen 2
|
|
-- pattern, that a miss really does not, that the odds
|
|
-- are the number they claim, and that the derived HP DV
|
|
-- is kept legal.
|
|
-- the recolour against a fixture of real (normal, shiny) colour
|
|
-- pairs lifted from the verified texture set, so the
|
|
-- Lua transform is checked against the Python that
|
|
-- produced the shipped colours rather than against
|
|
-- itself.
|
|
|
|
package.path = "./?.lua;./?/init.lua;" .. package.path
|
|
|
|
local args = {}
|
|
for _, a in ipairs({ ... }) do
|
|
local k, v = a:match("^%-%-([%w_]+)=(.*)$")
|
|
if k then args[k] = v else args[a:gsub("^%-%-", "")] = true end
|
|
end
|
|
|
|
local MOD = args.mod or "mods/DramaticShapeVoxelMod"
|
|
|
|
-- ------- the mod namespace, enough of it
|
|
|
|
local loaded = {}
|
|
local V = {}
|
|
function V.require(name)
|
|
if loaded[name] == nil then
|
|
loaded[name] = assert(loadfile(MOD .. "/lib/" .. name .. ".lua"))(V)
|
|
end
|
|
return loaded[name]
|
|
end
|
|
function V.data(name)
|
|
return assert(loadfile(MOD .. "/data/" .. name .. ".lua"))(V)
|
|
end
|
|
V.mod = { log = { warn = function() end, info = function() end } }
|
|
|
|
local Shiny = V.require("Shiny")
|
|
local ShinyPalette = V.require("ShinyPalette")
|
|
local Stats = require("src.pokemon.Stats")
|
|
|
|
-- ------- a tiny harness
|
|
|
|
local pass, fail = 0, 0
|
|
local function ok(cond, what)
|
|
if cond then
|
|
pass = pass + 1
|
|
else
|
|
fail = fail + 1
|
|
io.write("FAIL: " .. what .. "\n")
|
|
end
|
|
end
|
|
local function eq(got, want, what)
|
|
ok(got == want, ("%s (got %s, want %s)")
|
|
:format(what, tostring(got), tostring(want)))
|
|
end
|
|
|
|
-- always-hit and always-miss stand-ins for the odds roll
|
|
local function hit() return 1 end
|
|
local function miss() return 2 end
|
|
|
|
local function mon(dvs, level, species)
|
|
return { species = species, level = level, dvs = dvs,
|
|
statExp = { hp = 0, attack = 0, defense = 0, speed = 0, special = 0 } }
|
|
end
|
|
|
|
-- ------- the DV model
|
|
|
|
do
|
|
local m = mon({ attack = 0, defense = 0, speed = 0, special = 0, hp = 0 })
|
|
Shiny.decide(m, hit)
|
|
ok(Shiny.isShiny(m), "a hit produces a mon the ENGINE calls shiny")
|
|
ok(Stats.isShiny(m.dvs), "and the engine's own predicate agrees")
|
|
eq(m.dvs.defense, 10, "defense pinned to 10")
|
|
eq(m.dvs.speed, 10, "speed pinned to 10")
|
|
eq(m.dvs.special, 10, "special pinned to 10")
|
|
eq(m.shiny, true, "the cached flag is set")
|
|
end
|
|
|
|
do
|
|
-- nearest legal Attack, so a shiny encounter is not also a stat reroll
|
|
local cases = { [0] = 2, [4] = 3, [5] = 6, [9] = 10, [13] = 14, [15] = 15 }
|
|
for from, want in pairs(cases) do
|
|
local m = mon({ attack = from, defense = 0, speed = 0, special = 0, hp = 0 })
|
|
Shiny.decide(m, hit)
|
|
eq(m.dvs.attack, want, ("attack %d moves to the nearest legal %d")
|
|
:format(from, want))
|
|
end
|
|
end
|
|
|
|
do
|
|
-- the HP DV is derived from the low bits of the other four; a write that
|
|
-- forgets to resync it produces a mon no real game could make
|
|
for atk = 0, 15 do
|
|
local m = mon({ attack = atk, defense = 3, speed = 7, special = 1, hp = 0 })
|
|
Shiny.decide(m, hit)
|
|
local want = (m.dvs.attack % 2) * 8 + (m.dvs.defense % 2) * 4
|
|
+ (m.dvs.speed % 2) * 2 + (m.dvs.special % 2)
|
|
eq(m.dvs.hp, want, "HP DV stays derived from the other four")
|
|
end
|
|
end
|
|
|
|
do
|
|
-- a MISS must not leave an accidentally-shiny mon shiny, or the rate is
|
|
-- the requested one and 1/8192 in parallel
|
|
local m = mon({ attack = 2, defense = 10, speed = 10, special = 10, hp = 8 })
|
|
ok(Stats.isShiny(m.dvs), "fixture starts out shiny by luck")
|
|
Shiny.decide(m, miss)
|
|
ok(not Shiny.isShiny(m), "a miss clears an accidentally-shiny mon")
|
|
eq(m.shiny, nil, "and clears the cached flag rather than storing false")
|
|
end
|
|
|
|
do
|
|
-- the odds are the number they claim. 1/1 must be every time; a large
|
|
-- denominator must essentially never fire on a fixed stub.
|
|
local saved = Shiny.ODDS_DENOM
|
|
Shiny.setOdds(1)
|
|
local m = mon({ attack = 0, defense = 0, speed = 0, special = 0, hp = 0 })
|
|
Shiny.decide(m, function(_, hi) return math.random(1, hi) end)
|
|
ok(Shiny.isShiny(m), "odds of 1 make every mon shiny")
|
|
|
|
Shiny.setOdds(0)
|
|
eq(Shiny.ODDS_DENOM, 1, "a denominator below 1 is refused")
|
|
Shiny.setOdds(saved)
|
|
eq(Shiny.ODDS_DENOM, saved, "and a sane one is accepted")
|
|
end
|
|
|
|
do
|
|
-- stats must follow the DVs, and a full-health mon must stay full: a wild
|
|
-- mon that appears at less than full HP is visible in the first frame
|
|
local Data = require("src.core.Data")
|
|
local haveData = type(Data) == "table" and type(Data.pokemon) == "table"
|
|
and Data.pokemon.PIKACHU ~= nil
|
|
if haveData then
|
|
local m = mon({ attack = 0, defense = 0, speed = 0, special = 0, hp = 0 },
|
|
10, "PIKACHU")
|
|
m.stats = Stats.calc(Data.pokemon.PIKACHU, 10, m.dvs, m.statExp)
|
|
m.hp = m.stats.hp
|
|
local before = m.stats.hp
|
|
Shiny.decide(m, hit)
|
|
ok(m.stats.hp >= before, "max HP tracks the raised DVs")
|
|
eq(m.hp, m.stats.hp, "a full-health mon stays full after a restat")
|
|
else
|
|
io.write("note: no ROM-backed data; skipped the restat check\n")
|
|
end
|
|
end
|
|
|
|
-- ------- the recolour
|
|
|
|
do
|
|
eq(type(ShinyPalette.forDex(6)), "table", "colours load for Charizard")
|
|
local ch = ShinyPalette.forDex(6)
|
|
eq(ch.slide.h, -136, "Charizard's hue slide is the Stadium value")
|
|
eq(ch.slide.s, -6, "and its saturation step")
|
|
ok(ShinyPalette.forDex(130).lut ~= nil,
|
|
"Gyarados carries an explicit table, not a slide")
|
|
ok(ShinyPalette.forDex(6).lut == nil, "and Charizard does not")
|
|
end
|
|
|
|
do
|
|
-- alpha is never touched, and a texture with nothing to do comes back
|
|
-- unchanged rather than rebuilt
|
|
local fn = ShinyPalette.transform(ShinyPalette.forDex(6))
|
|
local px = string.char(222, 131, 123, 77) .. string.char(0, 0, 0, 0)
|
|
local out = ShinyPalette.recolorTexels(px, fn)
|
|
eq(#out, #px, "the texel string keeps its length")
|
|
eq(out:byte(4), 77, "alpha survives the transform")
|
|
eq(out:byte(8), 0, "and so does a fully transparent texel's alpha")
|
|
end
|
|
|
|
do
|
|
local fixture = loadfile(MOD .. "/tests/shiny_palette_fixture.lua")
|
|
if not fixture then
|
|
io.write("note: no colour fixture; skipped the cross-check\n")
|
|
else
|
|
local rows = fixture()
|
|
local worst, bad = 0, 0
|
|
for _, r in ipairs(rows) do
|
|
local fn = ShinyPalette.transform(ShinyPalette.forDex(r[1]))
|
|
local gr, gg, gb = r[2], r[3], r[4]
|
|
if fn then gr, gg, gb = fn(r[2], r[3], r[4]) end
|
|
local d = math.max(math.abs(gr - r[5]), math.abs(gg - r[6]),
|
|
math.abs(gb - r[7]))
|
|
if d > worst then worst = d end
|
|
if d > 2 then bad = bad + 1 end
|
|
end
|
|
eq(bad, 0, ("%d colour pairs reproduce the shipped textures"):format(#rows))
|
|
-- a unit or two of drift is the two languages' float rounding; more than
|
|
-- that means the colour model itself has diverged
|
|
ok(worst <= 2, ("worst channel drift is %d, within rounding"):format(worst))
|
|
end
|
|
end
|
|
|
|
-- ------- the read side
|
|
--
|
|
-- The extraction writes NNNs.dsm beside NNN.dsm; this is the other half of
|
|
-- that contract. Driven off a directory of real packs when one is given
|
|
-- (--packs=DIR, e.g. the extract test's --out), because the interesting
|
|
-- cases are a shiny pack that EXISTS and one that does not, and both have
|
|
-- to be real files for the fallback to be worth testing.
|
|
if args.packs then
|
|
local Vp = { mod = { log = { warn = function() end, info = function() end } } }
|
|
local loadedP = {}
|
|
function Vp.require(name)
|
|
if loadedP[name] == nil then
|
|
loadedP[name] = assert(loadfile(MOD .. "/lib/" .. name .. ".lua"))(Vp)
|
|
end
|
|
return loadedP[name]
|
|
end
|
|
-- stand in for the mod's own reader, pointed at the pack directory
|
|
function Vp.mod:read(rel)
|
|
local name = rel:match("([^/]+)$")
|
|
local fp = io.open(args.packs .. "/" .. name, "rb")
|
|
if not fp then return nil end
|
|
local b = fp:read("*a")
|
|
fp:close()
|
|
return b
|
|
end
|
|
local StadiumPack = Vp.require("StadiumPack")
|
|
StadiumPack.DIR = "."
|
|
|
|
local normal = StadiumPack.load(6, false)
|
|
local shiny = StadiumPack.load(6, true)
|
|
ok(normal ~= nil, "the normal Charizard pack loads")
|
|
ok(shiny ~= nil, "and so does the shiny one")
|
|
if normal and shiny then
|
|
eq(shiny.shiny, true, "the shiny model is flagged as such")
|
|
eq(normal.shiny, nil, "and the normal one is not")
|
|
ok(normal ~= shiny, "they are SEPARATE models, not one shared table")
|
|
eq(#normal.prims, #shiny.prims, "same geometry")
|
|
eq(normal.texCount, shiny.texCount, "same texture count")
|
|
-- the point of the whole exercise: different pixels
|
|
local differs = false
|
|
for i = 1, math.min(#normal.textures, #shiny.textures) do
|
|
if normal.textures[i].rgba ~= shiny.textures[i].rgba then
|
|
differs = true
|
|
break
|
|
end
|
|
end
|
|
ok(differs, "and different texels")
|
|
end
|
|
|
|
-- a species with no shiny pack must fall back rather than vanish: that is
|
|
-- what an install from before rev 3 looks like
|
|
local missing = StadiumPack.load(999, true)
|
|
eq(missing, nil, "an out-of-range species is still nil")
|
|
end
|
|
|
|
-- ------- end to end, through the engine's own constructor
|
|
--
|
|
-- The wrap is the whole feature: if Pokemon.new does not carry the verdict,
|
|
-- nothing downstream has anything to draw. Exercised against the real
|
|
-- constructor and the fixture dataset rather than a stub, because what is
|
|
-- being tested is precisely that we hooked the thing the game calls.
|
|
|
|
do
|
|
local okKit, T = pcall(require, "tests.modkit")
|
|
local Data = okKit and T.fixtures and T.fixtures.load()
|
|
local okPk, Pokemon = pcall(require, "src.pokemon.Pokemon")
|
|
local species = Data and Data.pokemon
|
|
and (Data.pokemon.PIKACHU and "PIKACHU"
|
|
or next(Data.pokemon))
|
|
if not (okKit and okPk and species) then
|
|
io.write("note: no fixture dataset; skipped the end-to-end check\n")
|
|
else
|
|
local ShinyBattle = V.require("ShinyBattle")
|
|
ShinyBattle.install()
|
|
ShinyBattle.install() -- twice: the wrap must not stack
|
|
|
|
local saved = Shiny.ODDS_DENOM
|
|
|
|
Shiny.setOdds(1)
|
|
local m = Pokemon.new(Data, species, 7)
|
|
ok(Shiny.isShiny(m), "a mon built at odds 1 comes out shiny")
|
|
eq(m.shiny, true, "and carries the flag")
|
|
eq(m.hp, m.stats.hp, "and is at full health despite the restat")
|
|
ok(Stats.isShiny(m.dvs), "and the ENGINE agrees it is shiny")
|
|
|
|
-- and at long odds it essentially never is: 400 draws at 1/8192 would
|
|
-- fire about 5% of the time, so a single failure here is signal
|
|
Shiny.setOdds(8192)
|
|
local shinies = 0
|
|
for _ = 1, 400 do
|
|
if Shiny.isShiny(Pokemon.new(Data, species, 7)) then
|
|
shinies = shinies + 1
|
|
end
|
|
end
|
|
ok(shinies <= 2, ("400 mons at 1/8192 produced %d shinies")
|
|
:format(shinies))
|
|
|
|
Shiny.setOdds(saved)
|
|
end
|
|
end
|
|
|
|
io.write(("\n%d passed, %d failed\n"):format(pass, fail))
|
|
os.exit(fail == 0 and 0 or 1)
|