home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 577a.lha / TownMaze_v1.1 / src.lzh / makeunused.c < prev    next >
C/C++ Source or Header  |  1991-08-04  |  9KB  |  322 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. /*
  62. ** Some C compilers couldn't cope with the big ugly while condition that used
  63. ** to be here, so it has become an internal routine called here.  Thanks to
  64. ** dwade@jarthur.Claremont.edu and rmk@rmkhome.uucp (Rick Kelly) for reporting
  65. ** the problem.
  66. */
  67.  
  68.     } while ((tries <= MAXTRIES) && wimpy_cc(chosencell));
  69.  
  70.     if (tries <= MAXTRIES)
  71.     {
  72.       movefromto(&isolated,&isolatedct,&unused,&unusedct,UNUSED,chosencell);
  73.       mazei = (chosencell/((mazewidth-1)/2));
  74.       mazej = (chosencell%((mazewidth-1)/2));
  75.       mazei = (2*mazei) + 1;
  76.       mazej = (2*mazej) + 1;
  77.       cmaze[mazei][mazej] = WALL;
  78. /*
  79. ** Tried moving neighbors of unused cells to the DEAD list here; big mistake;
  80. ** ended up with isolated dead cells; let the later routines do it.
  81. */
  82.     }
  83.   }
  84.   return;
  85. }
  86.  
  87. /*
  88. ** Several compilers across the net died trying to digest what is now the
  89. ** contents of this routine as a single statement.  Sigh.
  90. */
  91.  
  92. #ifdef __STDC__
  93. int wimpy_cc(int chosencell)
  94. #else
  95. int wimpy_cc(chosencell)
  96.   int chosencell;
  97. #endif
  98.  
  99. {
  100.  
  101. /*
  102.  
  103. To avoid locking in an isolated room, check all 49 cells centered on
  104. this cell, the hard way: no loop, no direct index to the cells. Goal
  105. is to be able to run a street on all four sides of the 3x3 cell array
  106. centered at this cell. 
  107.  
  108. First verify cell and immediate neighbors two out all have all their
  109. neighbors; if not, we're too close to a border. 
  110.  
  111.   if (interiorcell(chosencell) != (1==1)) return((1==1));
  112.  
  113.   if (interiorcell(nhbris(chosencell,0)) != (1==1)) return((1==1));
  114.  
  115.   if (interiorcell(nhbris(nhbris(chosencell,0),0)) != (1==1)) return((1==1));
  116.  
  117.   if (interiorcell(nhbris(chosencell,1)) != (1==1)) return((1==1));
  118.  
  119.   if (interiorcell(nhbris(nhbris(chosencell,1),1)) != (1==1)) return((1==1));
  120.  
  121.   if (interiorcell(nhbris(chosencell,2)) != (1==1)) return((1==1));
  122.  
  123.   if (interiorcell(nhbris(nhbris(chosencell,2),2)) != (1==1)) return((1==1));
  124.  
  125.   if (interiorcell(nhbris(chosencell,3)) != (1==1)) return((1==1));
  126.  
  127.   if (interiorcell(nhbris(nhbris(chosencell,3),3)) != (1==1)) return((1==1));
  128.  
  129. /*
  130. ** Now check all 49 cells for ISOLATED status -- yeech!
  131. **
  132. ** Check the chosen cell.
  133. */
  134.  
  135.   if (statlist[chosencell].status != ISOLATED)  return((1==1));
  136. /*
  137. ** Check to the north/up.
  138. */
  139.  
  140.   if (statlist[nhbris(chosencell,0)].status != ISOLATED) return((1==1));
  141.  
  142.   if (statlist[nhbris(nhbris(chosencell,0),0)].status != ISOLATED)
  143.     return((1==1));
  144.  
  145.   if (statlist[nhbris(nhbris(nhbris(chosencell,0),0),1)].status != ISOLATED)
  146.     return((1==1));
  147.  
  148.   if (statlist[nhbris(nhbris(nhbris(chosencell,0),0),3)].status != ISOLATED)
  149.     return((1==1));
  150.  
  151.   if (statlist[nhbris(nhbris(nhbris(nhbris(chosencell,0),0),3),3)].status
  152.       != ISOLATED)
  153.      return((1==1));
  154.  
  155.   if (statlist[nhbris(nhbris(nhbris(chosencell,0),0),0)].status != ISOLATED)
  156.      return((1==1));
  157.  
  158.   if (statlist[nhbris(nhbris(nhbris(nhbris(chosencell,0),0),0),1)].status
  159.       != ISOLATED)
  160.     return((1==1));
  161.  
  162.   if (statlist[nhbris(nhbris(nhbris(nhbris(nhbris(chosencell,0),0),0),1),1)
  163.               ].status != ISOLATED)
  164.     return((1==1));
  165.  
  166.   if (statlist[nhbris(nhbris(nhbris(nhbris(chosencell,0),0),0),3)].status
  167.       != ISOLATED)
  168.         return((1==1));
  169.  
  170.   if (statlist[nhbris(nhbris(nhbris(nhbris(nhbris(chosencell,0),0),0),3),3)
  171.               ].status != ISOLATED)
  172.     return((1==1));
  173.  
  174.   if (statlist[nhbris(nhbris(nhbris(nhbris(nhbris(nhbris(chosencell,0),0),
  175.                0),3),3),3)].status != ISOLATED)
  176.     return((1==1));
  177.  
  178.   if (statlist[nhbris(nhbris(chosencell,0),3)].status != ISOLATED)
  179.     return((1==1));
  180.  
  181. /*
  182. ** Check to the east/right.
  183. */
  184.  
  185.   if (statlist[nhbris(chosencell,1)].status != ISOLATED) return((1==1));
  186.  
  187.   if (statlist[nhbris(nhbris(chosencell,1),0)].status != ISOLATED)
  188.     return((1==1));
  189.  
  190.   if (statlist[nhbris(nhbris(chosencell,1),1)].status != ISOLATED)
  191.     return((1==1));
  192.  
  193.   if (statlist[nhbris(nhbris(nhbris(chosencell,1),1),2)].status != ISOLATED)
  194.     return((1==1));
  195.  
  196.   if (statlist[nhbris(nhbris(nhbris(chosencell,1),1),0)].status != ISOLATED)
  197.     return((1==1));
  198.  
  199.   if (statlist[nhbris(nhbris(nhbris(nhbris(chosencell,1),1),0),0)].status
  200.       != ISOLATED)
  201.     return((1==1));
  202.  
  203.   if (statlist[nhbris(nhbris(nhbris(chosencell,1),1),1)].status != ISOLATED)
  204.     return((1==1));
  205.  
  206.   if (statlist[nhbris(nhbris(nhbris(nhbris(chosencell,1),1),1),2)].status
  207.       != ISOLATED)
  208.     return((1==1));
  209.  
  210.   if (statlist[nhbris(nhbris(nhbris(nhbris(nhbris(chosencell,1),1),1),2),2)
  211.               ].status != ISOLATED)
  212.     return((1==1));
  213.  
  214.   if (statlist[nhbris(nhbris(nhbris(nhbris(chosencell,1),1),1),0)].status
  215.       != ISOLATED)
  216.     return((1==1));
  217.  
  218.   if (statlist[nhbris(nhbris(nhbris(nhbris(nhbris(chosencell,1),1),1),0),0)
  219.               ].status != ISOLATED)
  220.     return((1==1));
  221.  
  222.   if (statlist[nhbris(nhbris(nhbris(nhbris(nhbris(nhbris(chosencell,1),1),
  223.                       1),0),0),0)].status != ISOLATED)
  224.     return((1==1));
  225.  
  226. /*
  227. ** check to the south/down
  228. */
  229.  
  230.   if (statlist[nhbris(chosencell,2)].status != ISOLATED) return((1==1));
  231.  
  232.   if (statlist[nhbris(nhbris(chosencell,2),1)].status != ISOLATED)
  233.     return((1==1));
  234.  
  235.   if (statlist[nhbris(nhbris(chosencell,2),2)].status != ISOLATED)
  236.     return((1==1));
  237.  
  238.   if (statlist[nhbris(nhbris(nhbris(chosencell,2),2),3)].status != ISOLATED)
  239.     return((1==1));
  240.  
  241.   if (statlist[nhbris(nhbris(nhbris(chosencell,2),2),1)].status != ISOLATED)
  242.     return((1==1));
  243.  
  244.   if (statlist[nhbris(nhbris(nhbris(nhbris(chosencell,2),2),1),1)].status
  245.       != ISOLATED)
  246.     return((1==1));
  247.  
  248.   if (statlist[nhbris(nhbris(nhbris(chosencell,2),2),2)].status != ISOLATED)
  249.     return((1==1));
  250.  
  251.   if (statlist[nhbris(nhbris(nhbris(nhbris(chosencell,2),2),2),3)].status
  252.       != ISOLATED)
  253.     return((1==1));
  254.  
  255.   if (statlist[nhbris(nhbris(nhbris(nhbris(nhbris(chosencell,2),2),2),3),3)
  256.               ].status != ISOLATED)
  257.     return((1==1));
  258.  
  259.   if (statlist[nhbris(nhbris(nhbris(nhbris(chosencell,2),2),2),1)].status
  260.       != ISOLATED)
  261.     return((1==1));
  262.  
  263.   if (statlist[nhbris(nhbris(nhbris(nhbris(nhbris(chosencell,2),2),2),1),1)
  264.               ].status != ISOLATED)
  265.     return((1==1));
  266.  
  267.   if (statlist[nhbris(nhbris(nhbris(nhbris(nhbris(nhbris(chosencell,2),2),
  268.                2),1),1),1)].status != ISOLATED)
  269.     return((1==1));
  270.  
  271. /*
  272. ** check to the west/left
  273. */
  274.  
  275.   if (statlist[nhbris(chosencell,3)].status != ISOLATED) return((1==1));
  276.  
  277.   if (statlist[nhbris(nhbris(chosencell,3),2)].status != ISOLATED)
  278.     return((1==1));
  279.  
  280.   if (statlist[nhbris(nhbris(chosencell,3),3)].status != ISOLATED)
  281.     return((1==1));
  282.  
  283.   if (statlist[nhbris(nhbris(nhbris(chosencell,3),3),0)].status != ISOLATED)
  284.     return((1==1));
  285.  
  286.   if (statlist[nhbris(nhbris(nhbris(chosencell,3),3),2)].status != ISOLATED)
  287.     return((1==1));
  288.  
  289.   if (statlist[nhbris(nhbris(nhbris(nhbris(chosencell,3),3),2),2)].status
  290.       != ISOLATED)
  291.     return((1==1));
  292.  
  293.   if (statlist[nhbris(nhbris(nhbris(chosencell,3),3),3)].status != ISOLATED)
  294.     return((1==1));
  295.  
  296.   if (statlist[nhbris(nhbris(nhbris(nhbris(chosencell,3),3),3),0)].status
  297.       != ISOLATED)
  298.     return((1==1));
  299.  
  300.   if (statlist[nhbris(nhbris(nhbris(nhbris(nhbris(chosencell,3),3),3),0),0)
  301.               ].status != ISOLATED)
  302.     return((1==1));
  303.  
  304.   if (statlist[nhbris(nhbris(nhbris(nhbris(chosencell,3),3),3),2)].status
  305.       != ISOLATED)
  306.     return((1==1));
  307.  
  308.   if (statlist[nhbris(nhbris(nhbris(nhbris(nhbris(chosencell,3),3),3),2),2)
  309.               ].status != ISOLATED)
  310.     return((1==1));
  311.  
  312.   if (statlist[nhbris(nhbris(nhbris(nhbris(nhbris(nhbris(chosencell,3),3),
  313.                3),2),2),2)].status != ISOLATED)
  314.     return((1==1));
  315.  
  316. /*
  317. ** Found no unisolated cell!  Celebrate the good news.
  318. */
  319.     return((1==0));
  320.  
  321. }
  322.