Diagonal Cost

A not uncommon concern in roguelike (or grid-based) games that seek more realism—putting aside how realistic motion is for a virtual character on a virtual grid—is whether to cost a diagonal move at 99 versus orthogonal moves at 70, more or less. Why these numbers? They are a ratio,

    $ cfu 'printf("%.16f\n", 99/70.0)'
    1.4142857142857144
    $ cfu 'printf("%.16f\n", sqrt(2))'
    1.4142135623730951

and small integers suit energy based movement systems and lack various problems (portability comes to mind) of floating point. You may want to multiply the numbers by 2 or more so that characters can be hasted or slowed without running into fractional values: a hasted diagonal might cost 99, normal diagonal 198, slowed diagonal 297. (Can an actor be double hasted or slowed? What happens to game balance if something can or cannot move a lot?) There are other ratios with bigger numbers that better approximate the square root of two, but how accurate do you need to be?

    (defun deltas (target)
      (loop with n = 1 and delta = 0 and best = 10
            for d from 1 to 640 do
            (loop while (< (/ n d) target) do (incf n))
            (when (< (abs (- (/ (- n 1) d) target))
                     (abs (- (/ n d) target)))
              (decf n))
            (setf delta (abs (- (/ n d) target)))
            (when (< delta best)
              (format t "~&~a/~a~c~a~&" n d #\Tab delta)
              (setf best delta))))

    (deltas (sqrt 2))

This is not the most efficient of algorithms, which is why I stopped it at 640; improvements get increasingly rare as the numbers embiggen. Probably you can find a longer pre-computed chart for these numbers elsewhere on the Internet, or maybe a better algorithm.

    SBCL> (DELTAS (SQRT 2))
    1/1     0.41421354
    3/2     0.08578646
    4/3     0.080880165
    7/5     0.014213562
    17/12   0.0024530888
    24/17   0.0024487972
    41/29   4.2045116e-4
    99/70   7.212162e-5
    239/169 1.2397766e-5
    577/408 2.1457672e-6
    NIL

Consider a chess where diagonal moves cost more; every so often one of the players would get two moves in a row. This probably would be a significant advantage, especially if players timed an attack to take advantage of a free move, though players of roughly equal skill would need to play around with this to see how significant the occasional free (or lost) move is, and how much that would impact other aspects of the game, such as how much do Bishops suck? And how to cost the Knight that teleports 2 by 1 squares? And does a long diagonal move cost the same as one that moves only one cell? Computers have the advantage that they can do the calculations for many actors at the same time. However, just because the computer can does not mean it should; gameplay may be improved by simplifying movement costs so the players have fewer things to worry about. Especially where a doubled or lost move causes their downfall.

Monty Python - The Bishop

Maybe games that do implement more expensive diagonals need to make attacks less lethal so that the occasional double or lost move is less of a problem? On the realism front one might also consider what diagonals are allowed; some roguelikes ban diagonal moves if a wall intrudes (Brogue), while others have skills for flanking movements when a character is "surrounded" by enough monsters, who may in turn get free attacks due to their positions (Sil), etc.

   ....
   .@##
   .#.#

Source