This commit is contained in:
bryanthaboi
2026-08-13 13:07:22 -04:00
3 changed files with 147 additions and 9 deletions
+23 -8
View File
@@ -37,8 +37,10 @@ local Prize = require("src.battle.gen2.Prize")
-- * Gen 1's `user` / `target` / `battler` are battler wrappers around a mon
-- ({ mon = , name = , isPlayer = }); Gen 2's engine works on the party mon
-- table directly, so that is what these payloads carry.
-- * Gen 1's `rng` is love.math.random (1..n); Gen 2's injected `random` is
-- the cart's BattleRandom convention (0..n-1). Both keys are present.
-- * Gen 1's `rng` is love.math.random (rng(n) → 1..n, rng(lo,hi) → lo..hi).
-- Gen 2's cart BattleRandom is random(n) → 0..n-1. Both live on the
-- battle: `random` / `roller()` are BattleRandom for damage, accuracy,
-- Magnitude, etc.; `rng` is the Gen 1 / love-style view of the same stream.
local Runtime = require("src.mods.Runtime")
-- The two battle lines that carry the cart's own `line` break: a marker-bearing
-- literal has to stay reachable from a translation mod (#186, #245), which is
@@ -88,6 +90,19 @@ local function rand(random, n)
return math.random(n) - 1
end
-- Gen 1 / love.math view of BattleRandom: rng(n) → 1..n, rng(lo,hi) → lo..hi.
local function loveStyleRng(random)
return function(lo, hi)
if hi == nil then
local n = lo or 1
if n < 1 then n = 1 end
return (rand(random, n) or 0) + 1
end
if hi < lo then lo, hi = hi, lo end
return lo + (rand(random, hi - lo + 1) or 0)
end
end
-- data/trainers/leaders.asm. The two lists are ONE array in the ROM: only
-- KantoGymLeaders carries the -1 terminator, and GymLeaders falls through into
-- it, so IsGymLeader matches all twenty-two classes while IsKantoGymLeader
@@ -212,12 +227,15 @@ Battle.KANTO_BADGE_ORDER = {
-- this wild battle is BATTLETYPE_ROAMING; the caller built `wild`
-- through Roamers.beginBattle and reads Battle.roaming back to
-- bank the beast's HP afterwards
-- random(n) 0..n-1, injected so a test is deterministic
-- random(n) 0..n-1, injected so a test is deterministic (BattleRandom)
-- rng(lo,hi) / rng(n) Gen 1 / love.math contract; defaults over `random`
function Battle.new(opts)
opts = opts or {}
local self = setmetatable({}, Battle)
self.data = opts.data or {}
self.random = opts.random or function(n) return rand(nil, n) end
-- Same stream as `random`, Gen 1 / love.math calling convention.
self.rng = opts.rng or loveStyleRng(self.random)
self.party = opts.party or {}
self.trainer = opts.trainer
self.save = opts.save
@@ -403,11 +421,8 @@ function Battle:endBattle(outcome)
Runtime.emit("battle.ended", { battle = self, result = outcome })
end
-- A never-nil 0..n-1 roller for the hook contexts. `random` is optional on the
-- constructor (a headless test injects one, the game leaves it to love.math),
-- and a mod reaching for ctx.rng must not have to know that. Note the
-- convention: this is the cart's BattleRandom byte (0..n-1), NOT Gen 1's
-- love.math.random (1..n).
-- Never-nil BattleRandom (0..n-1) for call sites that want the cart byte.
-- `battle.rng` is the Gen 1 / love.math view of the same stream.
function Battle:roller()
if not self.rollerFn then
self.rollerFn = function(n) return rand(self.random, n) end
+11 -1
View File
@@ -265,8 +265,18 @@ Effects.MAGNITUDE_POWER = {
-- is what damagecalc reads as the move's power -- data/moves/moves.asm stores
-- MAGNITUDE at power 1 precisely because this overwrites it. Returns the
-- power and the magnitude number the text prints.
--
-- `random` is BattleRandom (0..n-1). If none is supplied, roll via love.math
-- / math.random — never hard-code 0 (that always yields Magnitude 4).
function Effects.magnitudePower(random)
local roll = random and random(256) or 0
local roll
if type(random) == "function" then
roll = random(256) or 0
elseif love and love.math and love.math.random then
roll = love.math.random(256) - 1
else
roll = math.random(256) - 1
end
for _, row in ipairs(Effects.MAGNITUDE_POWER) do
if row[1] >= roll then return row[2], row[3] end
end