[alt.sources] townmaze part 03/04

xanthian@zorch.SF-Bay.ORG (Kent Paul Dolan) (04/18/91)

Archive-name: townmaze/part03

#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of archive 3 (of 4)."
# Contents:  makestreet.c
# Wrapped by xanthian@zorch on Wed Apr 17 20:34:41 1991
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'makestreet.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'makestreet.c'\"
else
echo shar: Extracting \"'makestreet.c'\" \(8837 characters\)
sed "s/^X//" >'makestreet.c' <<'END_OF_FILE'
X/*
X** makestreet.c  Copyright 1991 Kent Paul Dolan,
X**               Mountain View, CA, USA 94039-0755
X**
X** Written to satisfy an inquiry on USENet's rec.games.programmer newsgroup.
X** May be freely used or modified in any non-commercial work.  Copyrighted
X** only to prevent patenting by someone else.
X*/
X
X#include <stdio.h>
X#include "townmaze.h"
X#include "townproto.h"
X
X#ifdef __STDC__
Xint makestreet(int chosencell,int streetid,int streetpoints)
X#else
Xint makestreet(chosencell,streetid,streetpoints)
X  int chosencell,streetid, streetpoints;
X#endif
X{
X
X  int choseni, chosenj;
X  int nhbrid, nextnhbrid, thirdnhbrid;
X  int streetwalk, highstreetnum, lowstreetnum;
X  int canstraighten;
X
X/*
X** Check that a street isn't going in next to an unused cell; if it is,
X** mark it dead, instead if it's live; in any case, refuse to do it.
X*/
X
X  for (nhbrid = 0; nhbrid < 4; nhbrid++)
X  {
X    if (nhbrexists(chosencell,nhbrid))
X      if (statlist[nhbris(chosencell,nhbrid)].status == UNUSED)
X      {
X        if (statlist[chosencell].status == LIVE)
X          movefromto(&live,&livect,&dead,&deadct,DEAD,chosencell);
X        return(1==0);
X      }
X  }
X   
X/*
X** Check for and abide by straightness control
X*/
X
X   if ((mazestraightness > 0) && (streetpoints == -1))
X     if ((RANDOM()%1000) < mazestraightness)
X     {
X       canstraighten = (1==0);
X       for (nhbrid = 0; nhbrid < 4; nhbrid++)
X         if (nhbrexists(chosencell,nhbrid))
X           if (statlist[nhbris(chosencell,nhbrid)].streetdir ==
X               ((nhbrid+2)%4))
X             canstraighten = (1==1);
X       if (!canstraighten) return(1==0);
X     }
X/*
X** Update the chosen cell in the status list
X*/
X  switch (statlist[chosencell].status)
X  {
X  case UNUSED:
X  case STREET:
X    fprintf(stderr,"error picking street; bad cell status %d\n",
X      statlist[chosencell].status);
X    freespace();
X    exit(1);
X    break;
X
X  case LIVE:
X/*  fprintf(stderr,"moving cell %d from live to street\n",chosencell); */
X    movefromto(&live,&livect,&street,&streetct,STREET,chosencell);
X    break;
X
X  case DEAD:
X/*  fprintf(stderr,"moving cell %d from dead to street\n",chosencell); */
X    movefromto(&dead,&deadct,&street,&streetct,STREET,chosencell);
X    break;
X
X  case ISOLATED:
X/*  fprintf(stderr,"moving cell %d from isolated to street\n",chosencell);*/
X    movefromto(&isolated,&isolatedct,&street,&streetct,STREET,chosencell);
X    break;
X
X  default:
X    fprintf(stderr,"unexpected chosencell status in makegates %d\n",
X      statlist[chosencell].status);
X  }
X
X/*
X** Give the street the required streetid, either new or continuing an
X** existing street.
X*/
X
X  statlist[chosencell].streetnum = streetid;
X
X/*
X** Update the chosen cell on the map.
X*/
X
X  choseni = chosencell / (mazewidth/2);
X  chosenj = chosencell % (mazewidth/2);
X
X  choseni = 2 * choseni + 1;
X  chosenj = 2 * chosenj + 1;
X
X  cmaze[choseni - 1][chosenj] = HDOOR;
X  cmaze[choseni][chosenj + 1] = VDOOR;
X  cmaze[choseni + 1][chosenj] = HDOOR;
X  cmaze[choseni][chosenj - 1] = VDOOR;
X
X/*
X** Update the neighbors of the new street cell;
X*/
X
X  for (nhbrid = 0; nhbrid < 4; nhbrid++)
X  {
X    if (nhbrexists(chosencell,nhbrid))
X    switch (statlist[nhbris(chosencell,nhbrid)].status)
X    {
X    case UNUSED:
X      fprintf(stderr,
X              "logic error; tried to put a street beside an unused cell\n");
X      showdebugmaze();
X      freespace();
X      exit(1);
X      break;
X    case STREET:
X      switch (nhbrid) /* change the door to a passage, adopt a continuing */
X      {               /* direction from a neighbor street */
X      case 0:
X        cmaze[choseni - 1][chosenj] = BLANK;
X        if (statlist[nhbris(chosencell,nhbrid)].streetdir == 2)
X          statlist[chosencell].streetdir = 2;
X        break;
X      case 1:
X        cmaze[choseni][chosenj + 1] = BLANK;
X        if (statlist[nhbris(chosencell,nhbrid)].streetdir == 3)
X          statlist[chosencell].streetdir = 3;
X        break;
X      case 2:
X        cmaze[choseni + 1][chosenj] = BLANK;
X        if (statlist[nhbris(chosencell,nhbrid)].streetdir == 0)
X          statlist[chosencell].streetdir = 0;
X        break;
X      case 3:
X        cmaze[choseni][chosenj - 1] = BLANK;
X        if (statlist[nhbris(chosencell,nhbrid)].streetdir == 1)
X          statlist[chosencell].streetdir = 1;
X        break;
X      default:
X        fprintf(stderr,"bad nhbrid in nhbr update street switch case\n");
X        /* The above should be impossible; nhbrexists and nhbris already */
X        /* both checked for this problem */
X        showdebugmaze();
X        freespace();
X        exit(1);
X      }
X
X      if (statlist[chosencell].streetnum !=
X          statlist[nhbris(chosencell,nhbrid)].streetnum)
X      {
X
X/*
X** Two different streets have met; merge their streetids and decrement the
X** streetcount.
X*/
X
X        lowstreetnum =
X          ( ( statlist[chosencell].streetnum >
X              statlist[nhbris(chosencell,nhbrid)].streetnum )
X            ? statlist[nhbris(chosencell,nhbrid)].streetnum 
X            : statlist[chosencell].streetnum
X          );
X        highstreetnum =
X          ( ( statlist[chosencell].streetnum <
X              statlist[nhbris(chosencell,nhbrid)].streetnum )
X            ? statlist[nhbris(chosencell,nhbrid)].streetnum 
X            : statlist[chosencell].streetnum
X          );
X
X        streetwalk = street;
X        while (streetwalk != NOPOINTER)
X        {
X          if (statlist[streetwalk].streetnum == highstreetnum)
X            statlist[streetwalk].streetnum = lowstreetnum;
X          streetwalk = statlist[streetwalk].next;
X        }
X        streetnumct--;
X      }
X      break;
X    case DEAD: /* do nothing; that's why it's called dead */
X      break;
X    case LIVE:
X      movefromto(&live,&livect,&dead,&deadct,DEAD,nhbris(chosencell,nhbrid));
X      for (nextnhbrid = 0; nextnhbrid < 4; nextnhbrid++)
X        if (nhbrexists(nhbris(chosencell,nhbrid),nextnhbrid))
X          if (statlist[nhbris(nhbris(chosencell,nhbrid),nextnhbrid)].status
X              == ISOLATED)
X          {
X            movefromto(&dead,&deadct,&live,&livect,LIVE,
X                       nhbris(chosencell,nhbrid));
X            break;
X          }
X      break;
X    case ISOLATED:
X      movefromto(&isolated,&isolatedct,&dead,&deadct,DEAD,
X                 nhbris(chosencell,nhbrid));
X      statlist[nhbris(chosencell,nhbrid)].status = DEAD;
X/*
X** Only let an isolated cell become live if it is interior; stops
X** streets from running along the city wall, creating lots of gates.
X*/
X      if (interiorcell(nhbris(chosencell,nhbrid)))
X        for (nextnhbrid = 0; nextnhbrid < 4; nextnhbrid++)
X          if (nhbrexists(nhbris(chosencell,nhbrid),nextnhbrid))
X            if (statlist[nhbris(nhbris(chosencell,nhbrid),nextnhbrid)].status
X                == ISOLATED)
X            {
X              movefromto(&dead,&deadct,&live,&livect,LIVE,
X                         nhbris(chosencell,nhbrid));
X              break;
X            }
X/*
X** When an isolated cell stops being so, neighbors may stop being live
X** that were depending on it for that status; we're not done!
X*/
X      for (nextnhbrid = 0; nextnhbrid < 4; nextnhbrid++)
X        if (nhbrexists(nhbris(chosencell,nhbrid),nextnhbrid))
X          if (statlist[nhbris(nhbris(chosencell,nhbrid),nextnhbrid)].status
X              == LIVE)
X          {
X            movefromto(&live,&livect,&dead,&deadct,DEAD,
X                       nhbris(nhbris(chosencell,nhbrid),nextnhbrid));
X            for (thirdnhbrid =0; thirdnhbrid <4; thirdnhbrid++)
X            {
X              if (nhbrexists(nhbris(nhbris(chosencell,
X                                    nhbrid),
X                             nextnhbrid),
X                  thirdnhbrid))
X                if (statlist[nhbris(nhbris(nhbris(chosencell,
X                                           nhbrid),
X                                    nextnhbrid),
X                             thirdnhbrid)].status
X                    == ISOLATED)
X                {
X                  movefromto(&dead,&deadct,&live,&livect,LIVE,
X                             nhbris(nhbris(chosencell,nhbrid),nextnhbrid));
X                  break;
X                }
X            }
X          }
X      break;
X    default:
X      fprintf(stderr,"bad statlist[%d].status entry %d\n",
X              nhbris(chosencell,nhbrid),
X              statlist[nhbris(chosencell,nhbrid)].status);
X      showdebugmaze();
X      freespace();
X      exit(1);
X    }
X  }
X  if (statlist[chosencell].streetdir == -1)
X     if (streetpoints != -1)
X       statlist[chosencell].streetdir = streetpoints;
X     else
X     {
X       for (nhbrid = 0; nhbrid < 4; nhbrid++)
X       {
X         if (nhbrexists(chosencell,nhbrid))
X           if (statlist[nhbris(chosencell,nhbrid)].status == STREET)
X           {
X             statlist[chosencell].streetdir = ((nhbrid +2)%4);
X             break;
X           }
X       }
X     }
X/*  showdebugmaze(); */
X  return(1==1);
X}
END_OF_FILE
if test 8837 -ne `wc -c <'makestreet.c'`; then
    echo shar: \"'makestreet.c'\" unpacked with wrong size!
fi
# end of 'makestreet.c'
fi
echo shar: End of archive 3 \(of 4\).
cp /dev/null ark3isdone
MISSING=""
for I in 1 2 3 4 ; do
    if test ! -f ark${I}isdone ; then
	MISSING="${MISSING} ${I}"
    fi
done
if test "${MISSING}" = "" ; then
    echo You have unpacked all 4 archives.
    rm -f ark[1-9]isdone
else
    echo You still need to unpack the following archives:
    echo "        " ${MISSING}
fi
##  End of shell archive.
exit 0