/*
** nhbris.c  Copyright 1991 Kent Paul Dolan,
**           Mountain View, CA, USA 94039-0755
**
** Written to satisfy an inquiry on USENet's rec.games.programmer newsgroup.
** May be freely used or modified in any non-commercial work.  Copyrighted
** only to prevent patenting by someone else.
*/

#include <stdio.h>
#include "townmaze.h"
#include "townproto.h"

#ifdef __STDC__
int nhbris(int cellid,int nhbrid)
#else
int nhbris(cellid,nhbrid)
  int cellid, nhbrid;
#endif
{
  int celli, cellj;

  celli = cellid / (mazewidth/2);
  cellj = cellid % (mazewidth/2);

  switch (nhbrid)
  {
    case 0:
      return ( ( (celli - 1) * (mazewidth/2) ) + cellj ); /* north nhbr */
      break;
    case 1:
      return ( ( celli * (mazewidth/2) ) + cellj + 1); /* east nhbr */
      break;
    case 2:
      return ( ( (celli + 1) * (mazewidth/2) ) + cellj ); /* south nhbr */
      break;
    case 3:
      return ( ( celli * (mazewidth/2) ) + cellj - 1); /* west nhbr */
      break;
    default:
      fprintf(stderr,"bad neighbor id value to nhbris %d\n",nhbrid);
      showdebugmaze();
      freespace();
      exit(1);
  }
}
