Re: Random events in Telefang
Posted: Mon Mar 21, 2011 9:08 pm
I've been meaning to post this for a long time, but now I have time to do it... This post is a long explanation of how the game handles random events: how random numbers are generated, and the probabilities of various things. This will cover various aspects, such as which wild Denjuu appear, running from random battles, and more.
The first important thing to know is how random numbers are generated in the first place. The same function for random numbers is used for every random event in the game: wild Denjuu (including their species and level), whether you can run from a battle or not (except in predetermined fights, where you can never escapee), what move an opponent Denjuu will use, whether a Denjuu will be affected by its personality (for example, if it doesn't want to attack in a given turn), whether an attack will miss or not, critical hits, the amount of damage dealt by an attack, added effects like paralysis (if applicable), or whether a Denjuu will give you its phone number or not. There may be other examples of this too.
This random number generation function is at 000D4E-000D7C in the ROM, and since it will be used for everything, I will post the assembly code, taken from BGB's debugger (this is a very helpful emulator here). To use BGB's debugger, open a ROM, then right-click the window and go to other->debugger. Any comments on the right are added by me for a bit of clarity.
So basically, it takes values from addresses C3C0, C41F, and C420 from RAM. (Indeed, if you use GameShark codes to set these to constant values, you'll get the same results every time for everything.) C3C0 increments itself every frame, except for when the game is busy loading something. This makes random events dependent on timing. C41F and C420 are, basically, seeds that further influence the randomness, and they undergo a series of operations, which causes them to change after every random event. All of these variables get calculated to a final variable, at address C400 (which is a sum of the two newly generated seeds), which is used to then determine random events by comparing them with other values, depending on the event. This algorithm seems largely obfuscated, but I'm going to assume that C400 is uniform (i.e. there is an equal probability of it generating any number from 0-255), unless someone can prove me wrong.
So indeed, if you set a breakpoint at 0D4E (to do this, first open the debugger if it isn't already. Then double-click the gray space to the left of ROM0:0D4E), the emulator will stop here every time it does a random event. Then, when tracing the function, once it reaches ret, you can see what called the function, and therefore where a particular random event is. For now, let's look at a few -- I'm not going to cover every random event here yet, especially when I didn't look through them all yet.
First, let's look at the event that determines the species, level, and personality of a wild Denjuu. You may notice that on Wikifang 2.0, I noted a while ago that the median level was 50%, while there is a 25% probability each of it either subtracting or adding a level. In addition, I also noted that while there are four Denjuu per acre, the probabilities are not uniform -- there's one that has a 10% chance of appearing, one with a 20% chance, another with a 30% chance, and finally one with a 40% chance. Now let's explain this here. First we'll do the species:
The function for determining a species starts at 3C57. Now let's look at it!
So, as you can see from the comments (which I hope made sense), the probabilities are uneven. The 1st Denjuu from a list has a 40% chance of appearing, the 2nd has a 30% chance, and 3rd has a 20% chance, and the 4th has a meager 10% chance. So if you find that certain Denjuu are harder to find than others, then now you know why. Thankfully they made the Kochia at the beginning of Power appear only 10% of the time, so that's good.
And indeed, if you hack the bytes 3C6D, 3C71, and 3C75 with a hex editor (which are 19, 4C, and 99), you will find that different Denjuu are more likely to appear than others. Try it out! Make sure that each value is greater than the preceding one though.
Next, let's look at what determines the level, starting at 3C82 then going to 701B8:
Therefore, there is a 50% chance that the level is the median, and a 25% chance each that the level is offset by 1.
Next, let's look at the personality of the wild Denjuu, starting at offset 702A1:
This one is weird. From a quick glance of looking at the code, it seems that personality 0 (whatever it is) seems to have a higher probability that the other ones. Is this an oversight, or did I read this code incorrectly? Either way it's weird. And no, I don't know what the personalities are offhand. Sorry. But the other ones are clearly uniform.
Finally, after all the wild Denjuu stuff, let's look at couple other things. First, the probability of escaping, starting from 70A99.
I do not fully understand the first part, because I haven't looked into it enough. However, starting at 10AB6 (RO1C:4AB6), this function often seems to be called when trying to escape. From looking at it, it's a 200/256 (78.125%) chance of not being able to escape, and a 56/256 (21.875%) chance of being able to. I'm curious to what the code before it is though.
Last, but not least, let's look at the code for FD-related stuff. Here is the code for a Crypto with the personality that makes him choose the first move in the moveslot sometimes. I don't know if this is the same code for other personalites, like being late to battle or running early or whatnot. This starts at 14B38.
I didn't comment much here because I didn't fully understand it. But for Crypto's personality at least, it seems to have this probability of being normal: (2*FD+55)/256. For a Denjuu with 0 FD, that means you have a 55/256 (21.6%) chance of being able to select the move yourself, while for a Denjuu with 100 FD, the chance of the Denjuu acting normal is 255/256 (99.6%). Yes, this falls victim to the 99.6 rule: If the random number comes up 255, then Crypto does whatever the heck it wants. Sound familiar? (And yes, I checked this myself.)
So... that's all I have. It's a start, at least. Now we know how the game generates random numbers, and the probabilities of certain things, like wild Denjuu species/level/personality, and we kind of know the probability of escaping and FD-related things. Eventually I will look more into everything else, unless someone gets to it first!
But, I hope this is useful.
The first important thing to know is how random numbers are generated in the first place. The same function for random numbers is used for every random event in the game: wild Denjuu (including their species and level), whether you can run from a battle or not (except in predetermined fights, where you can never escapee), what move an opponent Denjuu will use, whether a Denjuu will be affected by its personality (for example, if it doesn't want to attack in a given turn), whether an attack will miss or not, critical hits, the amount of damage dealt by an attack, added effects like paralysis (if applicable), or whether a Denjuu will give you its phone number or not. There may be other examples of this too.
This random number generation function is at 000D4E-000D7C in the ROM, and since it will be used for everything, I will post the assembly code, taken from BGB's debugger (this is a very helpful emulator here). To use BGB's debugger, open a ROM, then right-click the window and go to other->debugger. Any comments on the right are added by me for a bit of clarity.
Spoiler!
So indeed, if you set a breakpoint at 0D4E (to do this, first open the debugger if it isn't already. Then double-click the gray space to the left of ROM0:0D4E), the emulator will stop here every time it does a random event. Then, when tracing the function, once it reaches ret, you can see what called the function, and therefore where a particular random event is. For now, let's look at a few -- I'm not going to cover every random event here yet, especially when I didn't look through them all yet.
First, let's look at the event that determines the species, level, and personality of a wild Denjuu. You may notice that on Wikifang 2.0, I noted a while ago that the median level was 50%, while there is a 25% probability each of it either subtracting or adding a level. In addition, I also noted that while there are four Denjuu per acre, the probabilities are not uniform -- there's one that has a 10% chance of appearing, one with a 20% chance, another with a 30% chance, and finally one with a 40% chance. Now let's explain this here. First we'll do the species:
The function for determining a species starts at 3C57. Now let's look at it!
Spoiler!
And indeed, if you hack the bytes 3C6D, 3C71, and 3C75 with a hex editor (which are 19, 4C, and 99), you will find that different Denjuu are more likely to appear than others. Try it out! Make sure that each value is greater than the preceding one though.
Next, let's look at what determines the level, starting at 3C82 then going to 701B8:
Spoiler!
Next, let's look at the personality of the wild Denjuu, starting at offset 702A1:
Spoiler!
Finally, after all the wild Denjuu stuff, let's look at couple other things. First, the probability of escaping, starting from 70A99.
Spoiler!
Last, but not least, let's look at the code for FD-related stuff. Here is the code for a Crypto with the personality that makes him choose the first move in the moveslot sometimes. I don't know if this is the same code for other personalites, like being late to battle or running early or whatnot. This starts at 14B38.
Spoiler!
So... that's all I have. It's a start, at least. Now we know how the game generates random numbers, and the probabilities of certain things, like wild Denjuu species/level/personality, and we kind of know the probability of escaping and FD-related things. Eventually I will look more into everything else, unless someone gets to it first!
But, I hope this is useful.