User File #17185532309086037

Upload All User Files

#17185532309086037 - GBA Kingdom Hearts: Chain of Memories -- Shop RNG script v1

KH_Shop_v1.lua
777 downloads
Uploaded 9/4/2014 10:29 PM by FatRatKnight (see all 245)
The goal is to display exactly what you're getting from the shop, in four colorful blocks. With 16 different sorts of packs to choose from, all combinations of (attack, magic, item, mix) and (leaf, brown, black, Moogle) are shown. It should correctly identify everything I know and hint at things I don't know.
The script displays the limits of what I know, but I hope it isn't hard to make adjustments to fill in the gaps.
--[[(GBA) Kingdom Hearts: Chain of Memories
Shopping RNG display script
v1

There are four colored blocks. The block itself indicates which pack, and each
row within the block indicates which quality of the pack (leaf, brown, black,
Moogle). The top row of the block will tell you what the number of each of the
five cards you're getting, with a + to indicate premium.

The numbers on the left side of the screen are the raw 300-sided dice rolls.
This is in case you needed the numbers behind what my script is predicting.
Keep in mind that for each card, one number is used to identify it, one more is
used to determine the value, and in the case of attack or magic cards, a third
is spent to determine if it is premium.

The white number in the lower-right corner is merely counting how many RNG
rolls the game has made since the script began or the last time the game reset
its RNG. Might be a good idea to know how much thinking the game is doing.

The information of shops isn't complete in this script, but I can't find errors
in card values or premium chances. I just need the identifiers at this point,
and I should have in place everything needed to fill those into the script.
]]--

local R4s= memory.readdwordsigned

--*****************************************************************************
local function Roll(rTbl)  -- [1], [2], [3], [4]
--*****************************************************************************
    return bit.band(0xFFFFFFFF,bit.bxor(
        bit.lshift(rTbl[2], 2),bit.lshift(rTbl[4], 1),
        bit.rshift(rTbl[1],30),bit.rshift(rTbl[3],31)
    ))
end

local function RollDie(r,v)  return bit.band(r,0x00007FFF)%v  end
--Give it an RNG value and sides of the die.

--#############################################################################

local T= {0,0,0,0}
local Count= 0
--*****************************************************************************
local function CountRNG()
--*****************************************************************************
-- Updates the script's stored RNG. Will also count RNG rolls since.
-- If the script can't find a match in 1000 rolls, it resets the counter.

    for i= 0, 1000 do
        local Match= true
        for j= 1, 4 do
            Match= Match and (T[j] == R4s(0x02034030+4*(j-1)))
        end
        if Match then  Count= Count+i; return end

-- No match this time. Roll our stored value, and check for a match next time.
        table.insert(T,1,Roll(T))
        T[5]= nil
    end

--Give up. Reset counter and our stored RNG value.
    for i= 1, 4 do
        T[i]= R4s(0x02034030+4*(i-1))
        Count= 0
    end
end


--*****************************************************************************
local function NextRNGs(v)
--*****************************************************************************
-- Reads the game's current RNG, and predicts stuff from there.
-- Parameter indicates number of RNGs to list

    local t= {R4s(0x02034030),R4s(0x02034034),R4s(0x02034038),R4s(0x0203403C)}
    local rtbl= {} -- I'll hold my RNG values here.

    for i= 0, v-1 do
        table.insert(t,1,Roll(t)); t[5]= nil
        rtbl[i]= t[1]
    end

    return rtbl
end

local StripeColors= {[0]=0xFFFFFFFF,0xC0C0C0FF}
--*****************************************************************************
local function Display_RNGs(r)
--*****************************************************************************
-- Its only purpose is to display a list of numbers.
-- It's probably a good idea to help calibrate the script or something.
    for i= 0, #r do
        local X= math.floor(i/15)
        local Y= (i%15)*7 + X
        local clr= StripeColors[X%2]
        X= X*13

        gui.text(X,Y,string.format("%3d",RollDie(r[i],300)),clr)
    end
end

-------------------------------------------------------------------------------
local Atk_ID  ={
-------------------------------------------------------------------------------
  {--Leaf
[0]="TW","TW","TW","TW","TW","TW","TW","TW","TW","TW",-- 0~ 9 TW Three Wishes
    "TW","TW","TW","TW","TW","TW","TW","TW","TW","TW",--10~19 Ph Pumpkinhead
    "Ph","Ph","Ph","Ph","Ph","Ph","Ph","Ph","Ph","Ph",--20~29 WS Wishing Star
    "Ph","Ph","Ph","Ph","Ph","Ph","Ph","Ph","Ph","Ph",--30~39 LL Lady Luck
    "WS","WS","WS","WS","WS","WS","WS","WS","WS","WS",--40~49 Ol Olympia
    "WS","WS","WS","WS","WS","WS","WS","WS","WS","WS",--50~59 
    "LL","LL","LL","LL","LL","LL","LL","LL","LL","LL",--60~69 Cc Crabclaw
    "LL","LL","LL","LL","LL","LL","LL","LL","LL","LL",--70~79
    "Ol","Ol","Ol","Ol","Ol","Ol","Ol","Ol","Ol","Ol",--80~89
    "Ol","Ol","Ol","Ol","Ol","Ol","Ol","Ol","Ol","Ol" --90~99
  },
  {--Brown
[0]="TW","TW","TW","TW","TW","TW","TW","TW","TW","TW",-- 0~ 9 DR Divine Rose
    "TW","TW","TW","TW","Ph","Ph","Ph","Ph","Ph","Ph",--10~19
    "Ph","Ph","Ph","Ph","Ph","Ph","Ph","Ph","WS","WS",--20~29
    "WS","WS","WS","WS","WS","WS","WS","WS","WS","WS",--30~39
    "WS","WS","LL","LL","LL","LL","LL","LL","LL","LL",--40~49
    "LL","LL","LL","LL","LL","LL","Ol","Ol","Ol","Ol",--50~59
    "Ol","Ol","Ol","Ol","Ol","Ol","Ol","Ol","Ol","Ol",--60~69
    "?1","?1","?1","?1","?1","Cc","Cc","Cc","Cc","Cc",--70~79
    "?2","?2","?2","?2","?2","?3","?3","?3","?3","?3",--80~89
    "?4","?4","?4","?4","?4","DR","DR","DR","DR","DR" --90~99
  },
  {--Black
[0]="TW","TW","TW","TW","Ph","Ph","Ph","Ph","WS","WS",-- 0~ 9 FH Fairy Harp
    "WS","WS","LL","LL","LL","LL","Ol","Ol","Ol","Ol",--10~19 Sb Spellbinder
    "?1","?1","?1","?1","?1","?1","?1","?1","?1","?1",--20~29 MC Metal Chocobo
    "Cc","Cc","Cc","Cc","Cc","Cc","Cc","Cc","Cc","Cc",--30~39 Lh Lionheart
    "?2","?2","?2","?2","?2","?2","?2","?2","?2","?2",--40~49 Ok Oathkeeper
    "?3","?3","?3","?3","?3","?3","?3","?3","?3","?3",--50~59 Ob Oblivion
    "?4","?4","?4","?4","?4","?4","?4","?4","?4","?4",--60~69 DD Diamond Dust
    "DR","DR","DR","DR","DR","DR","DR","DR","DR","DR",--70~79 1A One-Winged Angel
    "?5","?5","?5","?5","?5","?6","?6","?6","?6","?6",--80~89 UW Ultima Weapon
    "?7","?7","?7","?7","?7","?8","?8","?8","?8","?8",--90~99
  },
  {--Moogle
[0]="TW","TW","TW","TW","Ph","Ph","Ph","Ph","WS","WS",-- 0~ 9
    "WS","WS","LL","LL","LL","LL","Ol","Ol","Ol","Ol",--10~19
    "?1","?1","?1","?1","?1","?1","Cc","Cc","Cc","Cc",--20~29
    "Cc","Cc","?2","?2","?2","?2","?2","?2","?3","?3",--30~39
    "?3","?3","?3","?3","?4","?4","?4","?4","?4","?4",--40~49
    "DR","DR","DR","DR","DR","DR","?5","?5","?5","?5",--50~59
    "?5","?5","?5","?5","?5","?5","?6","?6","?6","?6",--60~69
    "?6","?6","?6","?6","?6","?6","?7","?7","?7","?7",--70~79
    "?7","?7","?7","?7","?7","?7","?8","?8","?8","?8",--80~89
    "?8","?8","?8","?8","?8","?8","?9","?9","?9","?9" --90~99
  }
}

-------------------------------------------------------------------------------
local Mag_ID= {
-------------------------------------------------------------------------------
  {--Leaf
[0]="Fi","Fi","Fi","Fi","Fi","Fi","Fi","Fi","Fi","Fi",-- 0~ 9 Fi Fire
    "Fi","Fi","Fi","Fi","Fi","Bl","Bl","Bl","Bl","Bl",--10~19 Bl Blizzard
    "Bl","Bl","Bl","Bl","Bl","Bl","Bl","Bl","Bl","Bl",--20~29 Th Thunder
    "Th","Th","Th","Th","Th","Th","Th","Th","Th","Th",--30~39 Cu Cure
    "Th","Th","Th","Th","Th","Cu","Cu","Cu","Cu","Cu",--40-49
    "Cu","Cu","Cu","Cu","Cu","Si","Si","Si","Si","Si",--50~59 Ae Aero
    "Si","Si","Si","Si","Si","Si","Si","Si","Si","Si",--60~69
    "Si","Si","Si","Si","Si","Ge","Ge","Ge","Ge","Ge",--70~79 Si Simba
    "Ge","Ge","Ge","Ge","Ge","Du","Du","Du","Du","Du",--80~89 Ge Genie
    "Du","Du","Du","Du","Du","Cl","Cl","Cl","Cl","Cl" --90~99
  },
  {--Brown
[0]="Fi","Fi","Fi","Fi","Fi","Fi","Fi","Fi","Fi","Fi",-- 0~ 9 Du Dumbo
    "Bl","Bl","Bl","Bl","Bl","Bl","Bl","Bl","Bl","Bl",--10~19
    "Th","Th","Th","Th","Th","Th","Th","Th","Th","Th",--20~29
    "Cu","Cu","Cu","Cu","Cu","?1","?1","?1","?1","?1",--30~39 Cl Cloud
    "Ae","Ae","Ae","Ae","Ae","?2","?2","?2","?2","?2",--40~49
    "Si","Si","Si","Si","Si","Ge","Ge","Ge","Ge","Ge",--50~59
    "Ge","Ge","Ge","Ge","Ge","Ge","Ge","Ge","Ge","Ge",--60~69
    "Ge","Ge","Ge","Ge","Ge","Du","Du","Du","Du","Du",--70~79
    "Du","Du","Du","Du","Du","Du","Du","Du","Du","Du",--80~89
    "Du","Du","Du","Du","Du","Cl","Cl","Cl","Cl","Cl" --90~99
  },
  {--Black
[0]="Fi","Fi","Fi","Fi","Fi","Bl","Bl","Bl","Bl","Bl",-- 0~ 9 Gr Gravity
    "Th","Th","Th","Th","Th","?1","?1","?1","?1","?1",--10~19 St Stop
    "?1","?1","?1","?1","?1","?2","?2","?2","?2","?2",--20~29 Ba Bambi
    "?2","?2","?2","?2","?2","Ae","Ae","Ae","Ae","Ae",--30~39 Mu Mushu
    "Ae","Ae","Ae","Ae","Ae","Si","Si","Si","Si","Si",--40~49 TB Tinker Bell
    "Ge","Ge","Ge","Ge","Ge","Ge","Ge","Ge","?3","?3",--50~59
    "?3","?3","?3","?3","?3","?3","Du","Du","Du","Du",--60~69
    "Du","Du","Du","Du","?4","?4","?4","?4","?4","?4",--70~79
    "?4","?4","?5","?5","?5","?5","?5","?5","?5","?5",--80~89
    "Cl","Cl","Cl","Cl","Cl","Cl","Cl","Cl","Cl","Cl" --90~99
  },
  {--Moogle
[0]="Fi","Fi","Fi","Fi","Fi","Bl","Bl","Bl","Bl","Bl",-- 0~ 9
    "Th","Th","Th","Th","Th","?0","?0","?0","?0","?0",--10~19
    "?1","?1","?1","?1","?1","?2","?2","?2","?2","?2",--20~29
    "Si","Si","Si","Si","Si","Ge","Ge","Ge","Ge","Ge",--30~39
    "Ge","Ge","Ge","Ge","Ge","?3","?3","?3","?3","?3",--40~49
    "?3","?3","?3","?3","?3","Du","Du","Du","Du","Du",--50~59
    "Du","Du","Du","Du","Du","?4","?4","?4","?4","?4",--60~69
    "?4","?4","?4","?4","?4","?5","?5","?5","?5","?5",--70~79
    "?5","?5","?5","?5","?5","Cl","Cl","Cl","Cl","Cl",--80~89
    "Cl","Cl","Cl","Cl","Cl","Cl","Cl","Cl","Cl","Cl" --90~99
  }
}

-------------------------------------------------------------------------------
local Itm_ID= {
-------------------------------------------------------------------------------
  {--Leaf
[0]="*P","*P","*P","*P","*P","*P","*P","*P","*P","*P",-- 0~ 9 *P Potion
    "*P","*P","*P","*P","*P","*P","*P","*P","*P","*P",--10~19 *E Ether
    "*P","*P","*P","*P","*P","*P","*P","*P","*P","*P",--20~29 HP Hi-Potion
    "*P","*P","*P","*P","*P","*P","*P","*P","*P","*P",--30~39
    "*P","*P","*P","*P","*P","*P","*P","*P","*P","*P",--40~49
    "*E","*E","*E","*E","*E","*E","*E","*E","*E","*E",--50~59
    "*E","*E","*E","*E","*E","*E","*E","*E","*E","*E",--60~69
    "*E","*E","*E","*E","*E","*E","*E","*E","*E","*E",--70~79
    "*E","*E","*E","*E","*E","*E","*E","*E","*E","*E",--80~89
    "*E","*E","*E","*E","*E","*E","*E","*E","*E","*E" --90~99
  },
  {--Brown
[0]="*P","*P","*P","*P","*P","*P","*P","*P","*P","*P",-- 0~ 9 MP Mega-Potion
    "*P","*P","*P","*P","*P","*P","*P","*P","*P","*P",--10~19 ME Mega-Ether
    "*P","*P","*P","*P","*P","*P","*P","*P","*P","*P",--20~29 *x Elixir
    "*P","*P","*P","*P","*P","*P","*P","*P","*P","*P",--30~39 Mx Megalixir
    "*E","*E","*E","*E","*E","*E","*E","*E","*E","*E",--40~49
    "*E","*E","*E","*E","*E","*E","*E","*E","*E","*E",--50~59
    "*E","*E","*E","*E","*E","*E","*E","*E","*E","*E",--60~69
    "*E","*E","*E","*E","*E","*E","*E","*E","*E","*E",--70~79
    "HP","HP","HP","HP","HP","HP","HP","HP","HP","HP",--80~89
    "?1","?1","?1","?1","?1","?1","?1","?1","?1","?1" --90~99
  },
  {--Black
[0]="*E","*E","*E","*E","*E","*E","*E","*E","*E","*E",-- 0~ 9
    "*E","*E","*E","*E","*E","*E","*E","*E","*E","*E",--10~19
    "HP","HP","HP","HP","HP","HP","HP","HP","HP","HP",--20~29
    "HP","HP","HP","HP","HP","HP","HP","HP","HP","HP",--30~39
    "??","??","??","??","??","??","??","??","??","??",--40~49
    "??","??","??","??","??","??","??","??","??","??",--50~59
    "??","??","??","??","??","??","??","??","??","??",--60~69
    "??","??","??","??","??","??","??","??","??","??",--70~79
    "??","??","??","??","??","??","??","??","??","??",--80~89
    "??","??","??","??","??","??","??","??","??","??" --90~99
  },
  {--Moogle
[0]="*E","*E","*E","*E","*E","*E","*E","*E","*E","*E",-- 0~ 9
    "*E","*E","*E","*E","*E","HP","HP","HP","HP","HP",--10~19
    "HP","HP","HP","HP","HP","HP","HP","HP","HP","HP",--20~29
    "HP","HP","HP","HP","HP","??","??","??","??","??",--30~39
    "??","??","??","??","??","??","??","??","??","??",--40~49
    "??","??","??","??","??","??","??","??","??","??",--50~59
    "??","??","??","??","??","??","??","??","??","??",--60~69
    "??","??","??","??","??","??","??","??","??","??",--70~79
    "??","??","??","??","??","??","??","??","??","??",--80~89
    "??","??","??","??","??","??","??","??","??","??" --90~99
  }
}

local Mix_ID= {[0]=Atk_ID,Mag_ID,Itm_ID}

local tbl_card_val= {
[0]=0,0,0,0,0,1,1,1,1,1, -- 0~ 9
    1,1,1,1,1,1,1,1,1,1, --10~19
    2,2,2,2,2,2,2,2,2,2, --20~29
    2,2,2,2,2,3,3,3,3,3, --30~39
    3,3,3,3,3,3,3,3,3,3, --40~49
    3,4,4,4,4,4,4,4,4,4, --50~59
    4,4,4,4,4,5,5,5,5,5, --60~69
    5,5,5,5,5,6,6,6,6,6, --70~79
    6,6,6,6,6,7,7,7,7,7, --80~89
    7,8,8,8,8,8,9,9,9,9  --90~99
}

local Mix_clr= {[0]=0xFF6000FF,0xFFFF00FF,0x0080FFFF,0x00FFFFFF,0x00FF00FF}
--*****************************************************************************
local function PredictCards(r)  --Have 15 RNG values, start at index zero
--*****************************************************************************
--There are three aspects to generating a card:
--  * Identifier, what card it is
--  * Value, a number from 0 to 9
--  * Premium, 10% chance to shine most beautifully
--The aim is to correctly predict all five cards, for all four pack types, and
--all four pack qualities. 80 cards to display.

--Messy, I just want it to work.


--Attack
    local XX,YY= 140, 16  -- Position
    for Card= 0, 4 do
        local x= XX + Card*9
        local id= RollDie(r[Card*3+0],100)
        local val=RollDie(r[Card*3+1],100)
        local sp= RollDie(r[Card*3+2],100)

        local clr= 0xFF6000FF  -- Determine premium
        if sp < 10 then clr= 0xFFFF00FF; gui.text(x,YY,"+",clr) end

        gui.text(x+4,YY,tbl_card_val[val],clr)

        for q= 1, 4 do -- Quality of pack: Leaf, brown, black, Moogle
            gui.text(x,YY+6*q, Atk_ID[q][id], clr)
        end
    end

--Magic
    XX,YY= 194, 16  -- Position
    for Card= 0, 4 do
        local x= XX + Card*9
        local id= RollDie(r[Card*3+0],100)
        local val=RollDie(r[Card*3+1],100)
        local sp= RollDie(r[Card*3+2],100)

        local clr= 0x0080FFFF  -- Determine premium
        if sp < 10 then clr= 0x00FFFFFF; gui.text(x,YY,"+",clr) end

        gui.text(x+4,YY,tbl_card_val[val],clr)

        for q= 1, 4 do -- Quality of pack: Leaf, brown, black, Moogle
            gui.text(x,YY+6*q, Mag_ID[q][id], clr)
        end
    end

--Item
    XX,YY= 140,120  -- Position
    for Card= 0, 4 do
        local x= XX + Card*9
        local id= RollDie(r[Card*2+0],100)
        local val=RollDie(r[Card*2+1],100)

        local clr= 0x00FF00FF -- Items have no premium

        gui.text(x+4,YY,tbl_card_val[val],clr)

        for q= 1, 4 do -- Quality of pack: Leaf, brown, black, Moogle
            gui.text(x,YY+6*q, Itm_ID[q][id], clr)
        end
    end

--Mix
    XX,YY= 194,120  -- Position
    local ri= 0
    for Card= 0, 4 do
        local x= XX + Card*9
        local id= RollDie(r[ri],300); ri= ri+1
        local val=RollDie(r[ri],100); ri= ri+1

        local Type= math.floor(id/100); id= id%100
        local MixTbl= Mix_ID[Type]

        local Special= 0
        if Type ~= 2 then
            local sp= RollDie(r[ri],100); ri= ri+1
            if sp < 10 then Special= 1 end
        end
        local clr= Mix_clr[Type*2 + Special]
        if Special == 1 then gui.text(x,YY,"+",clr) end

        gui.text(x+4,YY,tbl_card_val[val],clr)

        for q= 1, 4 do -- Quality of pack: Leaf, brown, black, Moogle
            gui.text(x,YY+6*q, MixTbl[q][id], clr)
        end
    end
end

--*****************************************************************************
local function Fn()
--*****************************************************************************
    CountRNG(); gui.text(206,152,string.format("%8d",Count))
    local Tbl= NextRNGs(15)
    PredictCards(Tbl)
    Display_RNGs(Tbl)
end
gui.register(Fn)