[Guide] Army Battle Damage Formula

A forum to ask questions if you are stuck in the The Gate Rune Wars; or wish for more clarity on the gameplay systems.
Post Reply
User avatar
wataru14
Guide Writer
Posts: 716
Joined: Thu Aug 05, 2010 10:37 am
Location: Las Vegas

[Guide] Army Battle Damage Formula

Post by wataru14 »

Special thanks to Pyriel for checking the code. He was able to confirm most of of my work, and get me the last missing pieces to complete my research!

So here it is! After more than a year of banging my head against the wall, I present the damage formulae for the S1 military battles!!!!

Magic is [ Attack * 200 ]
No mystery there.

As for the rest, the formula is:
[ ( Adj.Attack * modifier ) + ( Rand / 537 ) - 30 ]

Rand is a random number between -32768 and +32767, which comes out to (-61 to +61).
So this means that there will be a (-91 to +31 added to the expected damage).

To calculate Adj.Attack, it's:
[ Unit Attack Power + ( # of troops / 3,000 ) ]
Which is pretty much what I figured, except it adds it as a bonus to attack, not a straight bonus to damage. Truncate the number instead of rounding.
Strategists increase the basic unit attack power before the number of troops is factored in.

Modifiers are:

NORMAL BATTLES
Charge gets x130 vs. Bow
Bow gets x120 vs. Magic
Charge gets x100 vs. Charge
Bow gets x90 vs. Bow
Bow gets x80 vs. Charge

TEO BATTLES
Armored Cavalry and Teo's Charge get x130 vs. everything (Teo Battle 1)
Armored Cavalry and Teo's Charge get x130 vs. Bow/Magic (Teo Battle 2)
Armored Cavalry and Teo's Charge get x40 vs. Charge (Teo Battle 2)
Charge/Bow/Magic do 0 damage vs. Armored Cavalry & Teo's Charge (Teo Battle 1)
Charge gets x130 vs. Armored Cavalry & Teo's Charge (Teo Battle 2)
Bow/Magic do 0 damage vs. Armored Cavalry & Teo's Charge (Teo Battle 2)

MILICH BATTLE 1
Poisonous Pollen kills a flat 80% of your soldiers

OTHERS
Dragon Knights kill 25% of the enemy's current troops. They do more overall damage the earlier you use them because the enemy has more troops.

There is an exception to the normal formula during normal battles.

When the commands are Charge vs. Bow, the enemy uses the player's [Adj.Attack] instead of their own, regardless of who is using which attack type. So using a weaker unit will result in you taking fewer casualties than using a strong one in this case. I believe Teo does the same for both unit types, but I need to confirm that.

As an example, in Pannu Yakuta Round 1:
Kwanda has 9000 soldiers, the Liberation Army has 7015.

Kwanda's Attack ( ATK 25) vs. Charge (Atk 13)

( 25 + [ 9000 / 3000 ] ) * 100) = 2800
Kwanda did 2830 damage, so the random variable was +60 (60-30=30)

( 13 + [ 7015 / 3000 ] ) * 100 = 1500
Liberation Army did 1489 damage, so the random variable was +19 (19-30=-11)

After reloading state and using Bow (Atk 13) instead of Charge:

( 13 + [ 7015 / 3000 ] ) * 130 = 1950
Kwanda did 1980 damage, so the random variable was still 30. He used the player's Adj.Attack. He would have done 3670 if he had used his own.

( 13 + [ 7015 / 3000 ] ) * 80 = 1200
Liberation Army did 1189 damage, so the random variable was still -11.

MYSTERY SOLVED!!!!!!!
Last edited by wataru14 on Wed Jul 27, 2022 11:31 am, edited 5 times in total.
User avatar
Celes
Global Admin
Posts: 1422
Joined: Sun Nov 12, 2006 6:25 pm

Re: [Guide] Army Battle Damage Formula

Post by Celes »

This is great work and should be placed in a guide!

Together with the unfinished War Battles Guide the entire (possible) course over all war battles can be calculated as well as how many characters can permanently die in order to reach an end with as few stars as possible.

For the sake of completeness you should add the formulas for Poisonous Pollen, Dragon Knights and Merchants.
Z3R01337
Posts: 62
Joined: Sat Feb 06, 2016 6:06 am

Re: [Guide] Army Battle Damage Formula

Post by Z3R01337 »

I think I've discovered some buggy behavior with enemy Bow vs our Charge. Bit of background: there's a issue with war battle RNG inconsistency between emu and PSTV that's been plaguing us for a while. With all known conditions equivalent (Starting RNG, characters recruited) we're getting a divergence in RNG behavior in certain war battles. After seeing this post again, I decided to look into it again. Emu's are setup to use PSP660 BIOS, same as PSTV.

Using Kwanda's War Battle, with Starting RNG the same (0x43), the pattern is T1 Magic vs Kwanda's Charge and T2 Strategist + Commander's Team T2 vs Bow Attack.

The weird part comes at the amount of units our army loses. On Bizhawk (and pSX, another emu), we lose 2103 units. On PSTV, we lose 2404 units. The formula's variance is 122, which is less than the difference between the 2 values here. So there's something going on with the formula. It only get's weirder though. I ran some trials on emu, pushing RNG forward X T2 (after Bow Attack has been selected, which is at end of T1 / start of T2). The amount of units our army lost ranged between 1188 and 3307!

On top of that, I mapped out possible modifier / unit power / unit count values to min/max damage. There was no table where all the unit losses I saw would fit, if it was using a different modifier value.

I'm attaching a spreadsheet with my numbers, I'll be gathering more data later and doing some reverse engineering myself later.
https://docs.google.com/spreadsheets/d/ ... sp=sharing
Z3R01337
Posts: 62
Joined: Sat Feb 06, 2016 6:06 am

Re: [Guide] Army Battle Damage Formula

Post by Z3R01337 »

Okay, think I figured out part of what's going on. Managed to track down the addresses the game writes unit losses to (0xa7488 our losses, 0xa7490 enemy losses). At the start of combat, the game calculates these and it matches up with the formula's above (from my 1 test case). What seems to happen is if 1 side kills another, combat stops and the losses are recalculated to how many units lost so far. RNG influences the length of combat & results of the little army guys, so the time it takes for the combat to add fluctuates, which leads to the numbers I wrote above.

So that's 1 mystery solved, but sadly it doesn't help me figure out the inconsistency between PSTV and emu :(
User avatar
Pyriel
Webmaster
Posts: 1227
Joined: Wed Aug 18, 2004 1:20 pm

Re: [Guide] Army Battle Damage Formula

Post by Pyriel »

That is weird. I don't have access to a PSTV, so there's not much I can do around that. I understand they're hard to come by these days.

Anyway, I figure I'll share some of the notes I kept from when I was looking through this for Wataru14.

Suikoden
800A8A64 - Liberation Army Troop count - Teo Battle 1
800A886C - Damage - Teo Battle 1
8001BE50 - Write new troop count.

war_last.bin
800A74AC - Liberation Army Troop Count - Gregminster Battle
800A74AE - Imperial Army Troop Count - Gregminster Battle
8001C894 - Write new troop count.

Edit: Out of curiosity, has anyone dumped the PSTV BIOS to validate it? It seems like a long shot that the RNG would be different there, if it's supposed to be on a known BIOS—and I don't think Sony changed that routine at all—but it's the simplest explanation.
Last edited by Pyriel on Fri Jun 17, 2022 9:56 pm, edited 1 time in total.
Z3R01337
Posts: 62
Joined: Sat Feb 06, 2016 6:06 am

Re: [Guide] Army Battle Damage Formula

Post by Z3R01337 »

Well, a PS Vita (and maybe even a PSP) would work too :D They run the same, since the Vita uses PSPemu for PS1 games. Sadly, debugging tools are pretty limited anyway. Thanks for the notes, I'll jot those down.

Also, I do have 1 correction to the formula. The range for RAND is incorrect. Formula for RAND is
RAND = (RNG >> 16) & 0x7fff

So range for RAND is (0,32767). That makes the random modifier to total damage (-30,31)
User avatar
Pyriel
Webmaster
Posts: 1227
Joined: Wed Aug 18, 2004 1:20 pm

Re: [Guide] Army Battle Damage Formula

Post by Pyriel »

That'd track with what I'm used to on PSX. I could swear the documentation for the built-in rand() said nothing about &0x7FFF when I double-checked it, but it's definitely there. I must've been looking at the wrong thing.

https://psx-spx.consoledev.net/kernelbios/
Z3R01337
Posts: 62
Joined: Sat Feb 06, 2016 6:06 am

Re: [Guide] Army Battle Damage Formula

Post by Z3R01337 »

Pyriel wrote: Fri Jun 17, 2022 9:41 pm Edit: Out of curiosity, has anyone dumped the PSTV BIOS to validate it? It seems like a long shot that the RNG would be different there, if it's supposed to be on a known BIOS—and I don't think Sony changed that routine at all—but it's the simplest explanation.
Yeah, it's out there. File name is PSP660.bin. I run Bizhawk using it, RNG difference during war battles is still there. As of right now, there are 3 known impactful differences (for speedruns) in RNG behavior between PSTV and Bizhawk/other emus (with PSP660 or even other BIOS)

1. RNG Resets: Events such as war battles, Marco's cup game, Georges game overwrite the RNG Value. The value changes depending on platform, not BIOS. for example, Kwanda War Battle on PSTV overwrites RNG with 0x43, on Bizhawk it overwrites with 0x67.

I did find the instruction that was doing it a while back, it's

Code: Select all

0x1475d8 lhu r2, 0x0000(r3 = 0x1f801110)
Based off a doc on Playstation inner workings, that address maps to the root counter for horizontal retrace. I don't know exactly what that is, I assume it's involved in drawing the game display.

2. War Battle RNG divergence: More or less what I mentioned here. Doesn't happen in all battles, and only happens in combat involving Charges. I think the biggest culprits are Kwanda and Sonya battles. Sonya is particularly rough, because her RNG reset can land in a range of 0x43-0x5f, so we need to map out lot's of different strats depending on the seed we landed on. And having to do the war battle on PSTV instead of on emu is very tedious.

3. Town loading zones: As you approach a town, RNG stops moving while the game is preloading the town. How long that takes is different between PSTV and emu, which can lead to small differences. This might be explained by fast disc speed though, and isn't all that important.

Also, that's a nice PSX doc, much better than the reference I had. Bookmarking it!
User avatar
Pyriel
Webmaster
Posts: 1227
Joined: Wed Aug 18, 2004 1:20 pm

Re: [Guide] Army Battle Damage Formula

Post by Pyriel »

Z3R01337 wrote: Fri Jun 17, 2022 10:10 pm Yeah, it's out there. File name is PSP660.bin. I run Bizhawk using it, RNG difference during war battles is still there. As of right now, there are 3 known impactful differences (for speedruns) in RNG behavior between PSTV and Bizhawk/other emus (with PSP660 or even other BIOS)

1. RNG Resets: Events such as war battles, Marco's cup game, Georges game overwrite the RNG Value. The value changes depending on platform, not BIOS. for example, Kwanda War Battle on PSTV overwrites RNG with 0x43, on Bizhawk it overwrites with 0x67.

I did find the instruction that was doing it a while back, it's

Code: Select all

0x1475d8 lhu r2, 0x0000(r3 = 0x1f801110)
Based off a doc on Playstation inner workings, that address maps to the root counter for horizontal retrace. I don't know exactly what that is, I assume it's involved in drawing the game display.
Yeah, that counter is (if memory serves) just the time since the last horizontal scanline was completed. On a CRT it'd effectively be a measure of how long it takes the beam to go from left to right across the screen. The fact that it's different on PSTV says to me that they changed something about the way it drives and/or interprets what's going on with the display, but I can't find much technical information on it that would give me any great insight into just how that works. Best I can do right now is say that the alleged framerate of the PSTV is between slightly less than double and slightly more than double the rate of a PSX doing NTSC standard work. Edit: Or maybe it's right around the same? I'm finding a lot of conflicting information.

Anyway, if it's seeding from that, and the rate of change in the counter isn't particularly proportional, that might account for it.

Edit: Yeah, it's seeding rand() from that periodically (I'm getting different results, but I think it's my emulator flaking). You basically need the game to be ticking at the same rate, and the GPU to be working at the same rate across systems for consistent results. I don't think Sony cares enough about speedrunning to make that a goal, unfortunately. There might be settings you can play with, but my guess is that there's just too much that's different.
Last edited by Pyriel on Sat Jun 18, 2022 9:24 am, edited 1 time in total.
Z3R01337
Posts: 62
Joined: Sat Feb 06, 2016 6:06 am

Re: [Guide] Army Battle Damage Formula

Post by Z3R01337 »

Thanks for looking into it. Disappointing there's no good fix for it, but we at least have some decent fixes on Bizhawk via Lua scripts. The war battle divergence is something that we don't have any good solution for currently.

To expand on what I mean by the war battle RNG divergence, I'll use Kwanda's War Battle as an example. RNG is seeded to 0x43 on PSTV, and on emu we can manually seed it to 0x43 as well easily. After the 1st action (Luc Magic), the RNG value will be identical on PSTV and Bizhawk, 0xe21ae1ec, index 261 from 0x43. After the Strategist + Charge, on PSTV the index will be somewhere around low 14000. On Bizhawk, it will be 15108 with RNG value 0x3dadd29f.

You can refer to this https://ak505188.github.io/Suikoden-RNG ... ions=20000 if I wasn't exactly clear on the index thing.
User avatar
Pyriel
Webmaster
Posts: 1227
Joined: Wed Aug 18, 2004 1:20 pm

Re: [Guide] Army Battle Damage Formula

Post by Pyriel »

Looks like it's just the number of times rand() was called and "advanced" the value. I was a little confused before. When I hear "index", I'm thinking like Mersenne Twist state tables, and that doesn't make sense here, but the list clears it up.

Regarding the reseeding, I don't think it actually happens all that often. My emulator was being flaky about tracing/breaking, and I only had a save in the castle to go on last night. It definitely doesn't get reseeded at all during the war battles as far as I can see. So the difference would have to come down to the number of times rand() gets called to draw little angels, I guess.
Z3R01337
Posts: 62
Joined: Sat Feb 06, 2016 6:06 am

Re: [Guide] Army Battle Damage Formula

Post by Z3R01337 »

Haha sorry for the confusion. Since RNG essentially sequential, us S1 speedrunners use "table" index as a way of explaining what RNG we're on.

Reseeding should only happen once as the war battle is loading, aka the map screen. Same thing with Marco & Georges, only happens once at the start of the mini game load. It's definitely the amount of times rand() gets called that's the difference, what I'd like to know & possibly adjust is the behavior difference between emu & PSTV.

I did make some progress on that front, for the Kwanda battle I managed to track down the frame where we get the divergence in behavior. At index 3416, RNG 0x1842456b PSTV and emu still match. The frame after that, PSTV index is 3444, emu is 3440. Since they have different RNG values at that point, everything after being different is expected.

The frame that they diverge, on emu there's 26 rand() calls. First 2 have ra = 0x800162e4, other 24 rand() calls have ra = 0x80015b6c. I did try to do some reversing at 0x162e4, sadly didn't quite figure out what was happening. Only thing I did catch is that there are checks to see if the battle is over via beq soldiercount, 0.

The addresses for soldiercount, aka the number of little angels on each side is 0xa7738 our army, 0xa773a Kwanda's army. I'm hoping to dig in more this weekend, just gotta get some stuff done first.

Here's an image of the frame the divergence happens: Image
User avatar
wataru14
Guide Writer
Posts: 716
Joined: Thu Aug 05, 2010 10:37 am
Location: Las Vegas

Re: [Guide] Army Battle Damage Formula

Post by wataru14 »

Celes wrote: Fri Jun 17, 2022 5:09 pm For the sake of completeness you should add the formulas for Poisonous Pollen, Dragon Knights and Merchants.
I added the Poisonous Pollen and Dragon Knights. But those were both figured out long ago. I can't include the Merchants because I don't know what they do.

Supposedly they cause 12.5% of the enemy to defect (25% on a big success), but in 26 years of playing I have never gotten them to succeed. At least not that I can remember. I have no idea what their base success rate is and if there are any modifiers that affect that. I've heard that they work more often if you have suffered a lot of casualties, but I don't know for sure.
User avatar
KingSpilly
Posts: 116
Joined: Tue Jan 04, 2022 2:53 pm

Re: [Guide] Army Battle Damage Formula

Post by KingSpilly »

In over 20 years, I've gotten the merchants to work one time. It was a while ago, so I don't remember the exact circumstances under which it happened.
"I'm a deeply flawed individual, but I bring joy to the world." -Tequila Sunset
User avatar
Celes
Global Admin
Posts: 1422
Joined: Sun Nov 12, 2006 6:25 pm

Re: [Guide] Army Battle Damage Formula

Post by Celes »

What we do know about merchants

There are three possible outcomes:
1. It's a big success! -> enemy loses 25% of its current units and these are added to your own
2. We have succeeded. -> enemy loses 12.5% of its current units and these are added to your own
3. Forgive us we have failed. -> Nothing happens

Merchants do not cost a turn.

Merchants are never attacked and therefore cannot die.

What we assume about Merchants

The probability of a good result increases if the opponent has fewer units than you. This information is in one of the Old Books and also corresponds to my observations. But we don't have proof of that. However, the probability is not zero either, as long as the opponent has not lost any units. It is even possible to succeed in Battle vs. Teo #1. This makes the Merchants the only way to decimate the opposing unit in this fight. I don't know if that applies to the first fight against Milich.

All available merchants are always used, so a larger unit should result in a higher probability of a better result. But we don't know that either.

So whats missing is
- the success probabilities of the three possible outcomes
- the influence of the number of merchants
Post Reply