Page 1 of 1

Better checkHit formula

Posted: Mon Jan 10, 2011 6:17 am
by Susramanian
The current formula only takes into consideration the difference between attack and defense. Assuming attack is greater than defense, here's how the code checks for a hit:

Code: Select all

local d = atk - def
hit = math.log10(1 + 5 * d / 50) * 100 + 50
It produces a 50% chance to hit if there's no difference, and a capped chance if the difference is around 20. With inflated numbers in the endgame, this means that changes to attack and defense (and saves, since they use the same sort of formula, if I recall correctly) usually have no effect. For example, boosting your spell save from 10 to 15 would do nothing in the endgame because it's getting checked against numbers that are much more than twenty higher than it. Only values that are already competitive are worth boosting, which is obviously not desirable.

Consider a very different formula, still assuming attack is greater than defense:

Code: Select all

local d = atk - def
hit = math.min(d / def, 0.5) * 100 + 50
This also produces a 50% chance to hit if there's no difference, but only gives a capped chance to hit if the attack is at least 50% more than defense. So against a defense of 60, you would need an attack of 90 to cap your hit chance. This breaks down at low values; against a defense of 2, an attack of 3 caps your hit chance. This is also not desirable.

Why not calculate hit chance using the average of these two values?

Code: Select all

local d = atk - def
hit_1 = math.log10(1 + 5 * d / 50) * 100 + 50
hit_2 = math.min(d / def, .5) * 100 + 50
hit = (hit_1 + hit_2) / 2
This should give sensible hit chances at both extremes, and makes increases to attributes valuable no matter how crappy they are. Somebody with fancy graphing software should draw us a pretty picture to back up my mental math.

Re: Better checkHit formula

Posted: Mon Jan 10, 2011 11:38 am
by darkgod
Here are the results:

Hit% is the current formula.
Hit% 2 is yours.
Final is the average of the two.

First, a variable atk ranging from 1 to 100 against a defense of 50
Image

Now, a variable def ranging from 1 to 100 against an atk of 50
Image

Re: Better checkHit formula

Posted: Fri Mar 04, 2011 7:16 pm
by tiger_eye
I don't mean to reinvent the wheel--and I know "checkHit()" is used for just about everything--but I think a standard logistic distribution make a lot of sense here. It's what nearly all chess rating algorithms use, and it's usage is further justified by many other similar "real world" examples. It's also extremely similar to what ToME already uses.

With "d = atk - def", the current method is

Code: Select all

hit = 0.5 + sign(d) * log10(1 + 5*abs(d)/50.0)
and the logistic distribuion is

Code: Select all

hit = 1 / (1 + exp(-(atk-def)/7) )
Let's compare:
checkHit_logsitic.png
checkHit_logsitic.png (20.45 KiB) Viewed 4922 times
See? Very, very similar, but with smooth tails.

Caio!

Re: Better checkHit formula

Posted: Fri Mar 04, 2011 7:23 pm
by Grey
Definitely nicer - would be cool to have this instead.

Re: Better checkHit formula

Posted: Fri Mar 04, 2011 7:48 pm
by Susramanian
It is definitely nicer than the current checkHit, but I'd still like to see something that also took into consideration the ratio of attack to defense, not just the difference. To use an obviously extreme example, consider an attack of 1020 against a defense of 1000. Attack is only 2% more than defense, and yet results in (nearly) guaranteed hits. I think a method that takes the ratio into consideration as well as the difference is important for a game where the values scale so drastically from the early game to the endgame.

Re: Better checkHit formula

Posted: Fri Mar 04, 2011 8:15 pm
by tiger_eye
Susramanian, interesting idea. While I don't disagree with it in principle, I know that "checkHit()" is used for many, many things, so changing it drastically may have many unintended consequences.

Also, are there any 2D/3D plots of your ideas that you would like to see? I have a working environment up and running, and I can easily make pretty plots like I did here:

http://forums.te4.org/viewtopic.php?f=41&t=25055

Re: Better checkHit formula

Posted: Fri Mar 04, 2011 8:25 pm
by Final Master
I'm ... not sure if this is really necessary, but, there isn't a different to[hit] check for the various attack types are there, aka melee, ranged, magical, natural? Is ... other than it being a pain to make them up and balance them, there any other reasons why there isn't [if there isn't]?

Re: Better checkHit formula

Posted: Fri Mar 04, 2011 9:06 pm
by Susramanian
tiger_eye, it might be nice to see graphs showing what happens at a given value of, say, defense. How about a graph assuming a defense of 10 with attack as the independent variable that shows hit chance using your logistic distribution. Then do the same for a defense of 50 and one for 100.

Then do another set of three graphs assuming the same defense values, only use an average like what I originally proposed. Use your distribution instead of Darkgod's when averaging:

Code: Select all

local d = atk - def
hit_1 = 1 / (1 + exp(-(atk-def)/7) )
hit_2 = math.min(d / def, .5) * 100 + 50
hit = (hit_1 + hit_2) / 2
Or you could fix attack and vary defense instead, and then we could look at these from the perspective of a caster wondering whether boosting their defense by 15 in the Far East will do them any good. (The current answer: almost certainly not).

Re: Better checkHit formula

Posted: Fri Mar 04, 2011 9:36 pm
by tiger_eye
Susramanian wrote:tiger_eye, it might be nice to see graphs showing what happens at a given value of, say, defense. How about a graph assuming a defense of 10 with attack as the independent variable that shows hit chance using your logistic distribution. Then do the same for a defense of 50 and one for 100.
It's only a function of difference, so it's the same plot. Here it is (contours every 20%):
logistic_3d-map.png
logistic_3d-map.png (30.21 KiB) Viewed 4880 times
And here's your function:
susramanian_3d-map.png
susramanian_3d-map.png (42.71 KiB) Viewed 4880 times
and here's the average of the two:
average_3d-map.png
average_3d-map.png (49.47 KiB) Viewed 4880 times

Re: Better checkHit formula

Posted: Fri Mar 04, 2011 9:53 pm
by Susramanian
tiger_eye wrote:It's only a function of difference, so it's the same plot.
I know (that's my whole problem with it!) The idea was to have several side-by-side pairs-- a difference-only plot next to a difference-and-ratio plot for several fixed values-- to see how they compare in the early, mid-, and endgame. The logistic curves would of course be the same in each pair, but the others would vary. Any chance of seeing that? Honestly, I'm finding it hard to read the 3D graphs. Call me old fashioned :)

Re: Better checkHit formula

Posted: Fri Mar 04, 2011 10:17 pm
by tiger_eye
Picky, picky! :wink:

The scale of the x-axis (atk) is the same for each plot, [def-50, def+50], so it's easier to compare between plots.
compare10.png
compare10.png (20.43 KiB) Viewed 4874 times
compare50.png
compare50.png (21.67 KiB) Viewed 4874 times
compare100.png
compare100.png (21.65 KiB) Viewed 4874 times

Re: Better checkHit formula

Posted: Fri Mar 04, 2011 10:30 pm
by Susramanian
I think that the curve resulting from the average is quite nice. It pulls in the range of competitive attack values at very low levels, doesn't do much in the middle levels, and widens the range of competitive values at high levels. Admittedly, performing calculations with two very different formulas and then averaging the results to get pleasant numbers seems embarrassingly clumsy, but it was the simplest way I could think of off the top of my head to achieve this. Whatever formula we end up using, I think we should aim for results like these.