Jump to content
ruadath

Shadow Dragon RNG lua Script! (version 1 now available!)

Recommended Posts

Hey guys,

I revisited this game recently in an attempt to crack the RNG formula and found it somewhat easier than expected. In particular, I already managed to crack the PRNG, so that I can calculate all of the random numbers in advance.

My only issue is... interpretation... while my script readily obtains the 32 bit hexadecimal numbers produced by the game's RNG, I haven't been able to figure out how the game converts these values to a random number between 0 and 99 (it's not as simple as division like it was in the GBA games). I can do stuff in game to get the associated random number for any string, but I'm looking for a mathematical formula so I can get a lua script ready and post it to the website. If anyone has some experience with figuring out this kind of stuff, I'd appreciate some help.

For anyone interested in helping, I can grab data relatively quickly by modifying Lena's in game HP growth rate and giving her a first level up to figure out what the value is. Hopefully we can wrap this up fairly quickly and get this up and running!

Edited by ruadath

Share this post


Link to post
Share on other sites

There's some data here. Match up whatever your script spits out with the random numbers generated by the times shown. See if you can find a pattern.

Share this post


Link to post
Share on other sites

It's not an issue of not being to generate the data (and in particular, I'm not trying to figure out the clock seeds yet). Right now the issue is that I have a bunch of data in a spreadsheet that looks like

HEX RN

ABB6 BA1A 0

923D 5A37 2

But I'm unable to figure out a general pattern from these examples. I tried doing this bit by bit (figuring out values for 0000 0001, then 0000 0002, then 0000 0004, etc.), but this approach doesn't seem to be working because the function isn't linear (or at least, this isn't apparent). Once I figure this out, I'll be able to get a lua script ready easily, since I already figured out how the game moves form 1 hex string to the next.

Edited by ruadath

Share this post


Link to post
Share on other sites

Sorry, I've been a bit busy with family stuff yesterday and today, but I'll get a spreadsheet up soon. I actually realized there was an error (an off by one error in mapping 32 bit strings and 0-99 numbers) in my data collection process, so hopefully when I get around to regenerating stuff later today/tomorrow it will be easier to figure out the pattern.

Edited by ruadath

Share this post


Link to post
Share on other sites

OK, I got the whole thing now. It was actually as simple as taking the value mod 2^30 and then mod 100. So I've got all that I need to write a lua script now, and the only thing left before we can say the RNG is completely cracked is figuring out how the DS clock seed works.

Share this post


Link to post
Share on other sites

Lua script is now available

function nextrng(r1)
        local seed = {0x18EE6547,0x8C7732AF, 0x463B9957, 0xA31DCCA7}
        return AND(XOR(r1, SHIFT(r1, -3), SHIFT(r1, -4), SHIFT(r1, 3), SHIFT(r1, 4), seed[r1%4+1]),0xFFFFFFFF)
end
 
function rngsim(n)
        local rngbase=0x02196E08
        local result = { memory.readword(rngbase)+memory.readword(rngbase+2)*65536 }
        for i = 2, n do
                result[i] = nextrng(result[i-1])
        end
        return result
end
 
while true do
        local nsim = 47
        rngs = rngsim(503)
        for i = 1, nsim do
                gui.text(236, 8*(i-1)-190, string.format("%3d", (rngs[i]%2147483648)%100))
        end
        gui.text(160,-190,"Previous RN:")
        emu.frameadvance()
 
    end

EDIT: Clock function is monic and linear in seconds, which means any initial seed is possible. This means for TAS/LTC planning purposes, it suffices to hack the initial RNG state to whatever you want since it is always possible (and very easy) to calculate a clock time that will generate the desired seed.

Edited by ruadath

Share this post


Link to post
Share on other sites

In other words, it's possible to do a TAS run? If so, that would be interesting, to say the least! Mapping all the numbers needed would be a hell of a task, though.

Share this post


Link to post
Share on other sites

Vykan already completed a TAS on normal mode in just over 12 minutes without being able to view the RNG at all

With this script, RNG manipulation becomes possible, and this should hopefully allow for a significant time shave, either by skipping the prologue by doing H1 instead of normal mode, or by optimizing NM with crits/riskier strats.

Edited by ruadath

Share this post


Link to post
Share on other sites

So this is the project you were talking about XD Why not do it in H5? Plan out all the dodges and crits, you will hit like 8mins if you rush.

Worst part about that video is skipping the entire enemy phase, really made it no fun to watch.

Share this post


Link to post
Share on other sites

There is something wrong with the script. The numbers it gives are right most of the time but not always. I'll figure this out and provide an update as soon as I do.

EDIT: OK I figured it out, and update the script accordingly.

Edited by ruadath

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...