home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3203 / makeunused.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-18  |  8.5 KB  |  199 lines

  1. /*
  2. ** makeunused.c  Copyright 1991 Kent Paul Dolan,
  3. **               Mountain View, CA, USA 94039-0755
  4. **
  5. ** Written to satisfy an inquiry on USENet's rec.games.programmer newsgroup.
  6. ** May be freely used or modified in any non-commercial work.  Copyrighted
  7. ** only to prevent patenting by someone else.
  8. */
  9.  
  10. #include <stdio.h>
  11. #include "townmaze.h"
  12. #include "townproto.h"
  13.  
  14. #ifdef __STDC__
  15. void makeunused()
  16. #else
  17. int makeunused()
  18. #endif
  19. {
  20.  
  21.   int totalcells;
  22.   int chosencell;
  23.   int tries;
  24.   int i;
  25.   int mazei, mazej;
  26. /*
  27. ** Pepper unused cells around the city interior; keep them apart for
  28. ** algorithmic robustness and connectivity reasons.
  29. */
  30.  
  31. #if PROGRESS
  32.   fprintf(stderr,"Unused ");
  33. #endif
  34.  
  35.   totalcells = ((mazeheight-1)/2 * (mazewidth-1)/2);
  36.  
  37.   for (i = 0; i < mazeunused; i++)
  38.   {
  39.  
  40. /*  fprintf(stderr,"Unused %d\n",i); */
  41.  
  42. /*
  43. ** Set up to prevent infinite loop from unforseen geometry problems.
  44. */
  45.  
  46.    tries = 0;
  47.  
  48. /*
  49. ** Keep looking until a candidate cell is found for this ith unused cell.
  50. */
  51.     do
  52.     {
  53.       /* not perfectly fair, but good enough for moderate sized mazes. */
  54.  
  55.       chosencell = RANDOM()%totalcells;
  56.  
  57. /*    fprintf(stderr,"  chosencell %d\n",chosencell); */
  58.  
  59.       tries++;
  60.  
  61.     } while
  62.       (   (tries <= MAXTRIES)
  63.        && (   /* To avoid locking in an isolated room, check all 49 cells */
  64.               /* centered on this cell, the hard way: no loop, no direct  */
  65.               /* index to the cells. Goal is to be able to run a street  */
  66.               /* on all four sides of the 3x3 cell array centered at this */
  67.               /* cell. */
  68.  
  69.               /* First verify cell and immediate neighbors two out all */
  70.               /* have all their neighbors; if not, we're too close to a */
  71.               /* border. */
  72.  
  73.               (interiorcell(chosencell) != (1==1))
  74.  
  75.            || (interiorcell(nhbris(chosencell,0)) != (1==1))
  76.            || (interiorcell(nhbris(nhbris(chosencell,0),0)) != (1==1))
  77.            || (interiorcell(nhbris(chosencell,1)) != (1==1))
  78.            || (interiorcell(nhbris(nhbris(chosencell,1),1)) != (1==1))
  79.            || (interiorcell(nhbris(chosencell,2)) != (1==1))
  80.            || (interiorcell(nhbris(nhbris(chosencell,2),2)) != (1==1))
  81.            || (interiorcell(nhbris(chosencell,3)) != (1==1))
  82.            || (interiorcell(nhbris(nhbris(chosencell,3),3)) != (1==1))
  83.  
  84.               /* Now check all 49 cells for ISOLATED status -- yeech! */
  85.  
  86.               /* check the chosen cell */
  87.            || (statlist[chosencell].status != ISOLATED)
  88.  
  89.               /* check to the north/up */
  90.            || (statlist[nhbris(chosencell,0)].status != ISOLATED)
  91.            || (statlist[nhbris(nhbris(chosencell,0),0)].status != ISOLATED)
  92.            || (statlist[nhbris(nhbris(nhbris(chosencell,
  93.                                0),0),1)].status != ISOLATED)
  94.            || (statlist[nhbris(nhbris(nhbris(chosencell,
  95.                                0),0),3)].status != ISOLATED)
  96.            || (statlist[nhbris(nhbris(nhbris(nhbris(chosencell,
  97.                                0),0),3),3)].status != ISOLATED)
  98.            || (statlist[nhbris(nhbris(nhbris(chosencell,
  99.                                0),0),0)].status != ISOLATED)
  100.            || (statlist[nhbris(nhbris(nhbris(nhbris(chosencell,
  101.                                0),0),0),1)].status != ISOLATED)
  102.            || (statlist[nhbris(nhbris(nhbris(nhbris(nhbris(chosencell,
  103.                                0),0),0),1),1)].status != ISOLATED)
  104.            || (statlist[nhbris(nhbris(nhbris(nhbris(chosencell,
  105.                                0),0),0),3)].status != ISOLATED)
  106.            || (statlist[nhbris(nhbris(nhbris(nhbris(nhbris(chosencell,
  107.                                0),0),0),3),3)].status != ISOLATED)
  108.            || (statlist[nhbris(nhbris(nhbris(nhbris(nhbris(nhbris(chosencell,
  109.                                0),0),0),3),3),3)].status != ISOLATED)
  110.            || (statlist[nhbris(nhbris(chosencell,0),3)].status != ISOLATED)
  111.  
  112.               /* check to the east/right */
  113.            || (statlist[nhbris(chosencell,1)].status != ISOLATED)
  114.            || (statlist[nhbris(nhbris(chosencell,1),0)].status != ISOLATED)
  115.            || (statlist[nhbris(nhbris(chosencell,1),1)].status != ISOLATED)
  116.            || (statlist[nhbris(nhbris(nhbris(chosencell,
  117.                                1),1),2)].status != ISOLATED)
  118.            || (statlist[nhbris(nhbris(nhbris(chosencell,
  119.                                1),1),0)].status != ISOLATED)
  120.            || (statlist[nhbris(nhbris(nhbris(nhbris(chosencell,
  121.                                1),1),0),0)].status != ISOLATED)
  122.            || (statlist[nhbris(nhbris(nhbris(chosencell,
  123.                                1),1),1)].status != ISOLATED)
  124.            || (statlist[nhbris(nhbris(nhbris(nhbris(chosencell,
  125.                                1),1),1),2)].status != ISOLATED)
  126.            || (statlist[nhbris(nhbris(nhbris(nhbris(nhbris(chosencell,
  127.                                1),1),1),2),2)].status != ISOLATED)
  128.            || (statlist[nhbris(nhbris(nhbris(nhbris(chosencell,
  129.                                1),1),1),0)].status != ISOLATED)
  130.            || (statlist[nhbris(nhbris(nhbris(nhbris(nhbris(chosencell,
  131.                                1),1),1),0),0)].status != ISOLATED)
  132.            || (statlist[nhbris(nhbris(nhbris(nhbris(nhbris(nhbris(chosencell,
  133.                                1),1),1),0),0),0)].status != ISOLATED)
  134.  
  135.               /* check to the south/down */
  136.            || (statlist[nhbris(chosencell,2)].status != ISOLATED)
  137.            || (statlist[nhbris(nhbris(chosencell,2),1)].status != ISOLATED)
  138.            || (statlist[nhbris(nhbris(chosencell,2),2)].status != ISOLATED)
  139.            || (statlist[nhbris(nhbris(nhbris(chosencell,
  140.                                2),2),3)].status != ISOLATED)
  141.            || (statlist[nhbris(nhbris(nhbris(chosencell,
  142.                                2),2),1)].status != ISOLATED)
  143.            || (statlist[nhbris(nhbris(nhbris(nhbris(chosencell,
  144.                                2),2),1),1)].status != ISOLATED)
  145.            || (statlist[nhbris(nhbris(nhbris(chosencell,
  146.                                2),2),2)].status != ISOLATED)
  147.            || (statlist[nhbris(nhbris(nhbris(nhbris(chosencell,
  148.                                2),2),2),3)].status != ISOLATED)
  149.            || (statlist[nhbris(nhbris(nhbris(nhbris(nhbris(chosencell,
  150.                                2),2),2),3),3)].status != ISOLATED)
  151.            || (statlist[nhbris(nhbris(nhbris(nhbris(chosencell,
  152.                                2),2),2),1)].status != ISOLATED)
  153.            || (statlist[nhbris(nhbris(nhbris(nhbris(nhbris(chosencell,
  154.                                2),2),2),1),1)].status != ISOLATED)
  155.            || (statlist[nhbris(nhbris(nhbris(nhbris(nhbris(nhbris(chosencell,
  156.                                2),2),2),1),1),1)].status != ISOLATED)
  157.  
  158.               /* check to the west/left */
  159.            || (statlist[nhbris(chosencell,3)].status != ISOLATED)
  160.            || (statlist[nhbris(nhbris(chosencell,3),2)].status != ISOLATED)
  161.            || (statlist[nhbris(nhbris(chosencell,3),3)].status != ISOLATED)
  162.            || (statlist[nhbris(nhbris(nhbris(chosencell,
  163.                                3),3),0)].status != ISOLATED)
  164.            || (statlist[nhbris(nhbris(nhbris(chosencell,
  165.                                3),3),2)].status != ISOLATED)
  166.            || (statlist[nhbris(nhbris(nhbris(nhbris(chosencell,
  167.                                3),3),2),2)].status != ISOLATED)
  168.            || (statlist[nhbris(nhbris(nhbris(chosencell,
  169.                                3),3),3)].status != ISOLATED)
  170.            || (statlist[nhbris(nhbris(nhbris(nhbris(chosencell,
  171.                                3),3),3),0)].status != ISOLATED)
  172.            || (statlist[nhbris(nhbris(nhbris(nhbris(nhbris(chosencell,
  173.                                3),3),3),0),0)].status != ISOLATED)
  174.            || (statlist[nhbris(nhbris(nhbris(nhbris(chosencell,
  175.                                3),3),3),2)].status != ISOLATED)
  176.            || (statlist[nhbris(nhbris(nhbris(nhbris(nhbris(chosencell,
  177.                                3),3),3),2),2)].status != ISOLATED)
  178.            || (statlist[nhbris(nhbris(nhbris(nhbris(nhbris(nhbris(chosencell,
  179.                                3),3),3),2),2),2)].status != ISOLATED)
  180.           )
  181.       );
  182.  
  183.     if (tries <= MAXTRIES)
  184.     {
  185.       movefromto(&isolated,&isolatedct,&unused,&unusedct,UNUSED,chosencell);
  186.       mazei = (chosencell/((mazewidth-1)/2));
  187.       mazej = (chosencell%((mazewidth-1)/2));
  188.       mazei = (2*mazei) + 1;
  189.       mazej = (2*mazej) + 1;
  190.       cmaze[mazei][mazej] = WALL;
  191. /*
  192. ** Tried moving neighbors of unused cells to the DEAD list here; big mistake;
  193. ** ended up with isolated dead cells; let the later routines do it.
  194. */
  195.     }
  196.   }
  197.   return;
  198. }
  199.