/*
** finishalleys.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__
void finishalleys()
#else
int finishalleys()
#endif
{

  int i, nhbrid;
  int livewalk, targetcell;

#if PROGRESS
  if (livect > 0) fprintf(stderr,"Alleys ");
#endif

  while(livect > 0)
  {
    targetcell = RANDOM()%livect;
    livewalk = live;
    for (i = 0; i < (targetcell - 1); i++)
      if (livewalk == NOPOINTER)
      {
        fprintf(stderr,"live list too short in finishalleys\n");
        showdebugmaze();
        freespace();
        exit(1);
      }
      else
      {
        livewalk = statlist[livewalk].next;
      }
/*
** Since this is a live cell, it is supposed to have a street cell
** neighbor; find one and adopt its street number.  If directions
** are implemented, then this has to be expanded to pick a random
** neighbor street fairly and adopt its number and direction.
** [Done in makestreet(), enabled by the "-1" last parameter.]
*/
    for (nhbrid = 0; nhbrid < 4; nhbrid++)
      if (nhbrexists(livewalk,nhbrid))
        if (statlist[nhbris(livewalk,nhbrid)].status == STREET)
        {
          makestreet(livewalk,
                     statlist[nhbris(livewalk,nhbrid)].streetnum,-1);
          break;
        }
  }
/*  fprintf(stderr,"\n"); */
  return;
}
