[rec.games.moria] A bug in Moria

djgrabin@phoenix.Princeton.EDU (David Joseph Grabiner) (01/12/89)

There is a bug in the monster movement routine in Unix Moria 4.87, and
in any other version which used the same original source.  
     When a monster is moving toward you, it finds the direction which
is most directly toward you, and then chooses alternate directions if
the direct move is blocked.  Usually, the alternate directions are
chosen correctly; for example, if a monster is above you and to your
right, and its first attempt is to move left, then if left is blocked,
it will move left and down.  However, if the monster's first move is
down and that is blocked, the program confuses left and right.  That can
lead to a problem in this situation:

     #.A
     #.#B
     #@#

If the monster is standing at A, which may be the door to a room, it
will first try to move down.  Since there is a wall in the way, it now
tries the next direction on the list, which is 3, not 1 as it should be.
The monster will thus move to B, into the room, instead of advancing to
attack.  From B, it will move properly: it will try to move left first,
then down and left, and then up and left, and the up and left move takes
it back to A.  Thus the monster can see a clear path to you but cannot
attack.  
    The same bug can also occur when the position is left-right
reflected.  
    The offending portion of the program, starting at line 224 of
creature.c, is given below, along with the fixed version.

Bugged version:

    case 10: case 14:
      mm[0] = 2;
      if (x < 0) 
	{
	  mm[1] = 1;
	  mm[2] = 3;
	  mm[3] = 4;
	  mm[4] = 6;
	}
      else
	{
	  mm[1] = 3;
	  mm[2] = 1;
	  mm[3] = 6;
	  mm[4] = 4;
	}
      break;

Corrected version:

    case 10: case 14:
      mm[0] = 2;
      if (x < 0)
	{
	  mm[1] = 3; /* These four lines have been changed */
	  mm[2] = 1; /* by switching 1 with 3, and 4 with 6 */
	  mm[3] = 6; 
	  mm[4] = 4;
	}
      else
	{
	  mm[1] = 1; /* The same changes have been made to */
	  mm[2] = 3; /* these four lines */
	  mm[3] = 4;
	  mm[4] = 6;
	}
      break;

David Grabiner
EMAIL: djgrabin@phoenix.princeton.edu or djgrabin@pucc.bitnet
US mail: 219 Dod Hall, Princeton University, Princeton, NJ 08544-8014.

Murphy's Law of mathematics: The crucial inequality always goes
                             in the wrong direction.