Better checkHit formula

All development conversation and discussion takes place here

Moderator: Moderator

Post Reply
Message
Author
Susramanian
Spiderkin
Posts: 454
Joined: Sat May 15, 2010 3:09 am

Better checkHit formula

#1 Post 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.

darkgod
Master of Eyal
Posts: 10750
Joined: Wed Jul 24, 2002 9:26 pm
Location: Angolwen
Contact:

Re: Better checkHit formula

#2 Post 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
[tome] joylove: You can't just release an expansion like one would release a Kraken XD
--
[tome] phantomfrettchen: your ability not to tease anyone is simply stunning ;)

tiger_eye
Perspiring Physicist
Posts: 889
Joined: Thu Feb 17, 2011 5:20 am

Re: Better checkHit formula

#3 Post 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 4921 times
See? Very, very similar, but with smooth tails.

Caio!

Grey
Loremaster
Posts: 3517
Joined: Thu Sep 23, 2010 10:18 pm
Location: London, England
Contact:

Re: Better checkHit formula

#4 Post by Grey »

Definitely nicer - would be cool to have this instead.
http://www.gamesofgrey.com - My own T-Engine games!
Roguelike Radio - A podcast about roguelikes

Susramanian
Spiderkin
Posts: 454
Joined: Sat May 15, 2010 3:09 am

Re: Better checkHit formula

#5 Post 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.

tiger_eye
Perspiring Physicist
Posts: 889
Joined: Thu Feb 17, 2011 5:20 am

Re: Better checkHit formula

#6 Post 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

Final Master
Sher'Tul
Posts: 1022
Joined: Fri May 21, 2010 8:16 pm
Location: Inside the minds of all
Contact:

Re: Better checkHit formula

#7 Post 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]?
Final Master's Character Guides
Final Master's Guide to the Arena
Edge: Final Master... official Tome 4 (thread) necromancer.
Zonk: I'd rather be sick than on fire! :D

Susramanian
Spiderkin
Posts: 454
Joined: Sat May 15, 2010 3:09 am

Re: Better checkHit formula

#8 Post 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).

tiger_eye
Perspiring Physicist
Posts: 889
Joined: Thu Feb 17, 2011 5:20 am

Re: Better checkHit formula

#9 Post 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 4879 times
And here's your function:
susramanian_3d-map.png
susramanian_3d-map.png (42.71 KiB) Viewed 4879 times
and here's the average of the two:
average_3d-map.png
average_3d-map.png (49.47 KiB) Viewed 4879 times

Susramanian
Spiderkin
Posts: 454
Joined: Sat May 15, 2010 3:09 am

Re: Better checkHit formula

#10 Post 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 :)

tiger_eye
Perspiring Physicist
Posts: 889
Joined: Thu Feb 17, 2011 5:20 am

Re: Better checkHit formula

#11 Post 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 4873 times
compare50.png
compare50.png (21.67 KiB) Viewed 4873 times
compare100.png
compare100.png (21.65 KiB) Viewed 4873 times

Susramanian
Spiderkin
Posts: 454
Joined: Sat May 15, 2010 3:09 am

Re: Better checkHit formula

#12 Post 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.

Post Reply