home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff255.lzh / SunMaze / maze.h < prev    next >
C/C++ Source or Header  |  1989-10-19  |  3KB  |  91 lines

  1. #define StackX (short) *(stack+(stackptr<<1))
  2. #define StackY (short) *(stack+(stackptr<<1)+1)
  3.  
  4. /* MAZETEST (xpos,ypos,mazewidth,pointer to buffer) */
  5. #ifdef BITMAP
  6. #define MAZETEST(a,b,c,d) (*((d)+((short)(b)*(short)((c)>>3))+((a)>>3)) & 0x80 >> ((a) & 7))
  7. #else
  8. #define MAZETEST(a,b,c,d) (*(d)+(a)+((b)*(c))))
  9. #endif
  10.  
  11. /* MAZESET (xpos,ypos,mazewidth,pointer to buffer */
  12. #ifdef BITMAP
  13. #define MAZESET(a,b,c,d) *((d)+((short)(b)*(short)((c)>>3))+((a)>>3))|= 0x80 >> ((a) & 7)
  14. #else
  15. #define MAZESET(a,b,c,d) *((d)+(a)+((b)*(c)))=1
  16. #endif
  17.  
  18. long seed;                     /* FastRand() stuff */
  19. short testpoint[4][2]=
  20.  {{-1,0},{1,0},{0,1},{0,-1}};  /* = left,right,down,up */
  21. short stackptr,direction,newpos(),maze();
  22. long *vposr = (long*)0xdff004; /* Beam position */
  23.  
  24. short maze(width,height,area)
  25. register short width;
  26. register char  *area;
  27. short height;
  28. {
  29.  register short x,y;
  30.  register short *stack;
  31.  short i,j;
  32.  
  33.  seed=*vposr;           /* new Seed for FastRand() */
  34.  x=2;                   /* starting position */
  35.  y=(FastRand(seed) % (height-4) + 2) & 0xfffe;
  36.  
  37.   /* we need a 'stack' to store those positions, from where we can */
  38.   /* continue if we get into a dead end */
  39.  if(!(stack=(short*) AllocMem(width*height,NULL))) return(NULL);
  40.  
  41.  stackptr=1;
  42.  StackX=x;StackY=y;
  43.  
  44.  j= width & 1 ? width-1:width-2;
  45.  for(i=0;i<height;i++)          /* surround the 'maze' with 'passages' */
  46.  {
  47.    MAZESET(0,i,width,area);
  48.    MAZESET(j,i,width,area);
  49.  }
  50.  j= height & 1 ? height-1:height-2;
  51.  for(i=0;i<width;i++)
  52.  {
  53.    MAZESET(i,0,width,area);
  54.    MAZESET(i,j,width,area);
  55.  }
  56.  
  57.  MAZESET(x,y,width,area);         /* set the first position to 'passage' */
  58.  
  59.  while(stackptr)                  /* do we have a untested position left ? */
  60.  {
  61.   direction=seed=FastRand(seed);  /* pick up a random direction */
  62.  
  63.   for (i=0;i<4;i++,direction++)   /* test all 4 directions */
  64.   {
  65.    direction &= 3;                /* = %4 */
  66.    /* test if the position we are moving to is already a 'passage' */
  67.    if(! MAZETEST(x+2*testpoint[direction][0],2*testpoint[direction][1]+y,width,area))
  68.    {
  69.     /* if not move to the new position */
  70.     MAZESET(x+testpoint[direction][0],testpoint[direction][1]+y,width,area);
  71.     x+=2*testpoint[direction][0];
  72.     y+=2*testpoint[direction][1];
  73.     MAZESET(x,y,width,area);
  74.     StackX=x;                     /* stack current position because */
  75.     StackY=y;                     /* there may be more than one direction */
  76.     stackptr++;                   /* we can move to */
  77.     goto endtest;                 /* GOTO ?!? */
  78.    }
  79.   }
  80.   stackptr--;                   /* dead-end, delete this point from stack */
  81.   x=StackX;                     /* test last position again */
  82.   y=StackY;
  83.   endtest:
  84.       ; /* Inserted ; to make Aztec happy -- PDS(3) -- 31-jul-88 */
  85.  }
  86.  FreeMem(stack,width*height);
  87.  return(1);
  88. }
  89.  
  90.  
  91.