Annotation of 43BSDReno/games/hunt/NEW/makemaze.c, revision 1.1

1.1     ! root        1: /*
        !             2:  *  Hunt
        !             3:  *  Copyright (c) 1985 Conrad C. Huang, Gregory S. Couch, Kenneth C.R.C. Arnold
        !             4:  *  San Francisco, California
        !             5:  *
        !             6:  *  Copyright (c) 1985 Regents of the University of California.
        !             7:  *  All rights reserved.  The Berkeley software License Agreement
        !             8:  *  specifies the terms and conditions for redistribution.
        !             9:  */
        !            10: 
        !            11: # include      "hunt.h"
        !            12: 
        !            13: # define       ISCLEAR(y,x)    (Maze[y][x] == SPACE)
        !            14: # define       ODD(n)          ((n) & 01)
        !            15: 
        !            16: makemaze()
        !            17: {
        !            18:        register char   *sp;
        !            19:        register int    y, x;
        !            20: 
        !            21:        /*
        !            22:         * fill maze with walls
        !            23:         */
        !            24:        sp = &Maze[0][0];
        !            25:        while (sp < &Maze[HEIGHT - 1][WIDTH])
        !            26:                *sp++ = DOOR;
        !            27: 
        !            28:        x = rand_num(WIDTH / 2) * 2 + 1;
        !            29:        y = rand_num(HEIGHT / 2) * 2 + 1;
        !            30:        dig_maze(x, y);
        !            31:        remap();
        !            32: }
        !            33: 
        !            34: # define       NPERM   24
        !            35: # define       NDIR    4
        !            36: 
        !            37: int    dirs[NPERM][NDIR] = {
        !            38:                {0,1,2,3},      {3,0,1,2},      {0,2,3,1},      {0,3,2,1},
        !            39:                {1,0,2,3},      {2,3,0,1},      {0,2,1,3},      {2,3,1,0},
        !            40:                {1,0,3,2},      {1,2,0,3},      {3,1,2,0},      {2,0,3,1},
        !            41:                {1,3,0,2},      {0,3,1,2},      {1,3,2,0},      {2,0,1,3},
        !            42:                {0,1,3,2},      {3,1,0,2},      {2,1,0,3},      {1,2,3,0},
        !            43:                {2,1,3,0},      {3,0,2,1},      {3,2,0,1},      {3,2,1,0}
        !            44:        };
        !            45: 
        !            46: int    incr[NDIR][2] = {
        !            47:                {0, 1}, {1, 0}, {0, -1}, {-1, 0}
        !            48:        };
        !            49: 
        !            50: dig(y, x)
        !            51: int    y, x;
        !            52: {
        !            53:        register int    *dp;
        !            54:        register int    *ip;
        !            55:        register int    ny, nx;
        !            56:        register int    *endp;
        !            57: 
        !            58:        Maze[y][x] = SPACE;                     /* Clear this spot */
        !            59:        dp = dirs[rand_num(NPERM)];
        !            60:        endp = &dp[NDIR];
        !            61:        while (dp < endp) {
        !            62:                ip = &incr[*dp++][0];
        !            63:                ny = y + *ip++;
        !            64:                nx = x + *ip;
        !            65:                if (candig(ny, nx))
        !            66:                        dig(ny, nx);
        !            67:        }
        !            68: }
        !            69: 
        !            70: /*
        !            71:  * candig:
        !            72:  *     Is it legal to clear this spot?
        !            73:  */
        !            74: candig(y, x)
        !            75: register int   y, x;
        !            76: {
        !            77:        register int    i;
        !            78: 
        !            79:        if (ODD(x) && ODD(y))
        !            80:                return FALSE;           /* can't touch ODD spots */
        !            81: 
        !            82:        if (y < UBOUND || y >= DBOUND)
        !            83:                return FALSE;           /* Beyond vertical bounds, NO */
        !            84:        if (x < LBOUND || x >= RBOUND)
        !            85:                return FALSE;           /* Beyond horizontal bounds, NO */
        !            86: 
        !            87:        if (ISCLEAR(y, x))
        !            88:                return FALSE;           /* Already clear, NO */
        !            89: 
        !            90:        i = ISCLEAR(y, x + 1);
        !            91:        i += ISCLEAR(y, x - 1);
        !            92:        if (i > 1)
        !            93:                return FALSE;           /* Introduces cycle, NO */
        !            94:        i += ISCLEAR(y + 1, x);
        !            95:        if (i > 1)
        !            96:                return FALSE;           /* Introduces cycle, NO */
        !            97:        i += ISCLEAR(y - 1, x);
        !            98:        if (i > 1)
        !            99:                return FALSE;           /* Introduces cycle, NO */
        !           100: 
        !           101:        return TRUE;                    /* OK */
        !           102: }
        !           103: 
        !           104: dig_maze(x, y)
        !           105: int    x, y;
        !           106: {
        !           107:        register int    tx, ty;
        !           108:        register int    i, j;
        !           109:        int             order[4];
        !           110: #define        MNORTH  0x1
        !           111: #define        MSOUTH  0x2
        !           112: #define        MEAST   0x4
        !           113: #define        MWEST   0x8
        !           114: 
        !           115:        Maze[y][x] = SPACE;
        !           116:        order[0] = MNORTH;
        !           117:        for (i = 1; i < 4; i++) {
        !           118:                j = rand_num(i + 1);
        !           119:                order[i] = order[j];
        !           120:                order[j] = 0x1 << i;
        !           121:        }
        !           122:        for (i = 0; i < 4; i++) {
        !           123:                switch (order[i]) {
        !           124:                  case MNORTH:
        !           125:                        tx = x;
        !           126:                        ty = y - 2;
        !           127:                        break;
        !           128:                  case MSOUTH:
        !           129:                        tx = x;
        !           130:                        ty = y + 2;
        !           131:                        break;
        !           132:                  case MEAST:
        !           133:                        tx = x + 2;
        !           134:                        ty = y;
        !           135:                        break;
        !           136:                  case MWEST:
        !           137:                        tx = x - 2;
        !           138:                        ty = y;
        !           139:                        break;
        !           140:                }
        !           141:                if (tx < 0 || ty < 0 || tx >= WIDTH || ty >= HEIGHT)
        !           142:                        continue;
        !           143:                if (Maze[ty][tx] == SPACE)
        !           144:                        continue;
        !           145:                Maze[(y + ty) / 2][(x + tx) / 2] = SPACE;
        !           146:                dig_maze(tx, ty);
        !           147:        }
        !           148: }
        !           149: 
        !           150: remap()
        !           151: {
        !           152:        register int    y, x;
        !           153:        register char   *sp;
        !           154:        register int    stat;
        !           155: 
        !           156:        for (y = 0; y < HEIGHT; y++)
        !           157:                for (x = 0; x < WIDTH; x++) {
        !           158:                        sp = &Maze[y][x];
        !           159:                        if (*sp == SPACE)
        !           160:                                continue;
        !           161:                        stat = 0;
        !           162:                        if (y - 1 >= 0 && Maze[y - 1][x] != SPACE)
        !           163:                                stat |= NORTH;
        !           164:                        if (y + 1 < HEIGHT && Maze[y + 1][x] != SPACE)
        !           165:                                stat |= SOUTH;
        !           166:                        if (x + 1 < WIDTH && Maze[y][x + 1] != SPACE)
        !           167:                                stat |= EAST;
        !           168:                        if (x - 1 >= 0 && Maze[y][x - 1] != SPACE)
        !           169:                                stat |= WEST;
        !           170:                        switch (stat) {
        !           171:                          case WEST | EAST:
        !           172:                          case EAST:
        !           173:                          case WEST:
        !           174:                                *sp = WALL1;
        !           175:                                break;
        !           176:                          case NORTH | SOUTH:
        !           177:                          case NORTH:
        !           178:                          case SOUTH:
        !           179:                                *sp = WALL2;
        !           180:                                break;
        !           181:                          case 0:
        !           182: # ifdef RANDOM
        !           183:                                *sp = DOOR;
        !           184: # endif RANDOM
        !           185: # ifdef REFLECT
        !           186:                                *sp = rand_num(2) ? WALL4 : WALL5;
        !           187: # endif REFLECT
        !           188:                                break;
        !           189:                          default:
        !           190:                                *sp = WALL3;
        !           191:                                break;
        !           192:                        }
        !           193:                }
        !           194:        bcopy((char *) Maze, (char *) Orig_maze, sizeof Maze);
        !           195: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.