## Attack/defense formula for game

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems.

Moderator: Moderators

gauauu
Posts: 702
Joined: Sat Jan 09, 2016 9:21 pm
Location: Central Illinois, USA
Contact:

### Re: Attack/defense formula for game

This FAQ explains in-depth how Dragon Warrior 1 worked.

There are two formulas for attack damage for enemies. The standard range is
from:

(EnemyStrength - HeroDefense / 2) / 4,

to:

(EnemyStrength - HeroDefense / 2) / 2

nocash
Posts: 1228
Joined: Fri Feb 24, 2012 12:09 pm
Contact:

### Re: Attack/defense formula for game

DRW wrote:For example, this calculation:
damage = attack / (100 / (100 + defense));
where all variables are global zeropage variables of type unsigned char, requires 11 scanlines worth of CPU time.
Unsigned char means that your attack & defense values are in range 0..255?
Or is it only smaller range like 0..15? Then you could easily pre-calculate all possible results and store them in a table.

Ehm, how or why do you calculate "100 / (100 + defense)"?
The result would be always zero?
Unless defense were zero, or the unsigned chars were negative?
Or unless your unsigned chars were converted to floating point values? Floating point won't be very fast for sure.
Or did you use fixed point fractions not shown in the formula?

In general, "damage=max(0,attack-defense)" doesn't sound so bad. It's simple, and more or less logical.
If it works depends on what you are doing...
For example, a paper hat might help against rain drops, but not so much against bulldozers.
In that case the formula would be fine. But it might fail if you had other situations in mind.

PS. Just wondering "giving him more initial energy" means that the opponents are all males?
I've recently found a similar gendered sentence in old source code that I had written a few decades ago : )

tepples
Posts: 22050
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

### Re: Attack/defense formula for game

nocash wrote:PS. Just wondering "giving him more initial energy" means that the opponents are all males?
I've recently found a similar gendered sentence in old source code that I had written a few decades ago : )
Use of "he" to mean "he or she" is older usage that may have been more common in previous decades.

DRW
Posts: 1982
Joined: Sat Sep 07, 2013 2:59 pm