User blog:RottenBlock/The Crystal Ring Works Just Fine

Redpanther recently sent me a message, asking if I might fix the Crystal Ring bug in Kichikuou Rance. The ring is supposed to improve a commander's magic by 2 points, but was reported as broken on the wiki's Items page back in 2015. I turned the game upside down, and here's the short answer: the Crystal Ring is not broken. Or at least it's not broken in the English version.

This wasn't easy to check. Ironically, it probably took longer to prove that it wasn't broken than it would have taken to fix it if it had been broken! The only way to work out if the game is using an item correctly is to follow the entire combat process, and that took some doing.

The Long Story
First off, let's be clear that the person who reported this said that the item wasn't working in combat. We're not just talking about some kind of menu display issue, and I bring that up for a reason. Kichikuou's menus always show characters with their base stats, not stats adjusted by treasure. That might confuse people into thinking treasures "weren't working," so I'm glad the person who reported it mentioned combat problems, even if they turned out to be mistaken. With that out of the way, let's go on to combat.

When you attack in Kichikuou, you'll see two numbers appear that fly from your side of the screen to the enemy troops. One comes from your troops and represents their attack (this becomes the top number when they settle into place) and the other represents your commander's attack (the bottom number). The Crystal Ring increases your Commander's magic by 2, so has no influence on your troops. We only care about the lower number.

Here's how commander damage works in Kichikuou. First, the game finds your commander's relevant stat and adds the bonus from the item. For example, at start of game, Asuka's magic is 6, and if we hacked the game to give her a free Crystal Ring, it would give her +2 for a total of 8. The game handles this math fine, I've verified this and all later steps by peppering the code with ZD commands (fucking intolerable to put up with, by the way). Certain factors can give magic a +3 bonus at this stage (buffs?), but they don't apply to Asuka in our example so I haven't investigated them.

Your commander's attack is then compared to the defender's relevant defence stat, in this case magic defence. Let's say Asuka attacks my favourite testing/punching bag, Dodge, generic commander of the southern Rebel Army. I like to test against Dodge because his Magic Defence is 0, which makes things much simpler.

Assuming your attack is higher than the enemy defence, we store a variable that we'll call your "damage potential" or DP. Your DP is equal to 8 times your attack minus your enemy's defence ((Attack - Defence) * 8). The game reduces results over 100 to 100. If your attack is lower than your enemy's defence, DP is set to ((Attack - Defence) + 8), minimum 1. Asuka vs Dodge gives 48 DP without the ring, and 64 DP with the ring. Debug commands prove everything is still solid.

DP is then compared to two other variables, one positive and one negative, and then clasped back in between 1 and 100 just in case. These variables always 0 in my test cases, making them irrelevant to our purposes here. The point is: nothing has happened to erase the bonus from the ring.

The game now handles multiple attacks. Asuka's multiple attack stat is quite high for a start-of-game character, at 20. The game generates a random number from 1 to your multiple attack stat, then adds half your multiple attack stat to the total again (RND(1, 20) + 10). Let's call this the number of successful hits. The Chuhai and White Destruction Beam abilities circumvent this calculation with their own, but we're just looking at a regular magic attack here so don't worry about that. For argument's sake, let's say Asuka landed exactly 20 attacks.

The number of hits you land is the number that appears on screen at the start of an attack. It's the one that starts on your commander's portrait and flies over to the enemy troops. This is before it changes to the final tally. To work out the final tally, we need to see how many of those attacks actually did damage (killed an enemy soldier).

The game uses different damage calculation formulas depending on how many attacks you land. If you landed 1-80 attacks, as Asuka did in our example, the game tests each attack individually. It does this by rolling a random number from 1-100. If the number is lower than your DP+1, the attack causes a single point of damage (kills a single soldier). So without the ring, Asuka has a 48+1=49% chance of doing damage with each of her 20 attacks, while with the ring she has a 64+1=65% chance of doing damage. Over 20 attacks, she'll cause an average of 9.6 damage without the ring, and 12.8 damage with the ring.

If Asuka had somehow landed 81 or more attacks, the game goes a different route to spare the trouble of calculating 81+ attacks in a row and wasting the player's time. Ironically, this procedure is more complicated to explain, but the computer can run through it in seconds.

Let's pretend Asuka somehow landed 100 attacks. When you land 81+ attacks, the game multiplies your DP by the number of attacks and divides by 100. Note that System 3 always rounds down. Because Asuka landed 100 attacks, that conveniently puts us at identical numbers to our DP:

48 * 100 / 100 = 48

64 * 100 / 100 = 64

Let's call this "Mass Damage Potential," since it's basically just Damage Potential again but retrofit for a different purpose. At this point, it's time to randomize. If your Mass DP is less than 11… wow, that's pathetic. In that case, the game does a simplified randomization process and then causes damage immediately: a 1-in-3 chance that do exactly as much damage as your Mass DP, a 1-in-3 chance that you do one point more, and a 1-in-3 chance of doing one point less. So if your Mass DP was 5, you might end up doing 4, 5 or 6 damage (kills 4-6 soldiers). That's basically the end of the process for anyone doing damage that low. Great job, team.

If your Mass DP is 11 or higher, as Asuka's is in our example, the game starts doing some math. It starts by reducing your Mass DP by 1/5th, bearing in mind that it always rounds down (it does this in a needlessly complicated manner, dividing by ten and then multiplying by two, when dividing by five would give you the same results considering our number is already 11 or higher). It then subtracts that number from your Mass DP to create what I'll call your Starting Damage.

In our test case, Asuka's Starting Damage is

48 / 10 = 4

4 * 2 = 8

48 - 8 = 40

without the ring and

64 / 10 = 6

6 * 2 = 12

64 - 12 = 52

with the ring.

The game then generates a random number anywhere from 1 up to 3/10s of your Mass DP, and adds it to your Starting Damage from the previous step:

48 / 10 = 4

4 * 3 = 12

Asuka without the ring will deal 40 damage plus a random number from 1-12, for a total of 41 through 52 damage (kills).

64 / 10 = 6

6 * 3 = 18

Asuka with the ring will deal 52 damage plus a random number from 1-18, for a total of 53 through 70 damage (kills).

At this point, the game checks to see if you did more damage than the enemy has troops, in which case your attack is cut to match the number of remaining troops (you can't kill soldiers that don't exist).

This is followed by a special check that applies exclusively to certain enemy commanders, who are marked as having "attack type 99." This includes seemingly all enemy mage commanders, certain Dark Lords (La Seizel, La Vaswald, and Red Eye), and enemy archers under randomized circumstances that I didn't bother to look into. Note that commanders that start as enemies and later join you as friendlies use different internal data before and after. The friendly versions don't qualify for attack type 99, in fact they don't even bother to set the variable required to get there. Long story short: if you're an enemy using attack type 99, this check erases all the damage calculation we just did so that the game can use an alternate calculation somewhere else in the code. Yes, they wasted all that time calculating the damage and then erased the results when they could have just skipped the whole process. I don't even know what to tell you. Since this check doesn't apply to friendlies, it doesn't factor in to our calculations.

We're essentially done now. The number on-screen changes from the number of attacks to the amount of damage (aka the number of kills), and that's the final damage.

If you've been paying attention, you'll notice that nothing happened to erase or disqualify the Crystal Ring's buff. The ring increases your odds of turning a hit into a point of damage, and it's working exactly as intended! The buff is applied correctly at the start of the process and nothing happens to invalidate or ignore it. It's not broken after all!

Here, we can prove it. Let's rig the game so that Asuka does exactly 100 attacks. This will use the alternate calculation, above, and give us a wider spread. Take another look at our example calculations above, and you'll see that Asuka doing 100 attacks without the ring can only deal between 41-52 damage, and Asuka with the ring can deal 53-70. There's no overlap, so that should prove the ring is boosting her damage.

By the way, first time I ran this test with the ring, I landed a legit 70. Good job, kiddo.

I think that wraps things up. The Crystal Ring isn't broken. It's just... not remarkable enough to make an obvious difference. Bet you're glad you stormed a 25-level dungeon for it now, huh?

Shamless Plug
If you liked this look into the game mechanics, consider checking out some of my Alicesoft patches for problems that really did exist, including my largest patch, Rance I Reworked!