Annotation of researchv10no/games/pacman/monster.c, revision 1.1.1.1

1.1       root        1: #include <stdio.h>
                      2: #include       "pacdefs.h"
                      3: 
                      4: extern char
                      5:        *vs_cm;
                      6: 
                      7: extern char
                      8:        brd[BRDY][BRDX],
                      9:        display[BRDY][BRDX];
                     10: 
                     11: extern int
                     12:        putch();
                     13: 
                     14: extern char
                     15:        *tgoto();
                     16: 
                     17: extern int
                     18:        delay,
                     19:        game,
                     20:        killflg,
                     21:        potion,
                     22:        rounds;
                     23: 
                     24: extern unsigned
                     25:        pscore;
                     26: 
                     27: extern struct pac
                     28:        *pacptr;
                     29: 
                     30: int    rscore[MAXMONSTER];
                     31: 
                     32: struct pac
                     33:        monst[MAXMONSTER];
                     34: 
                     35: startmonst()
                     36: {
                     37:        register struct pac *mptr;
                     38:        register int monstnum;
                     39: 
                     40:        if (potion == TRUE)
                     41:        {
                     42:                /* don't start if potion active */
                     43:                return;
                     44:        };
                     45: 
                     46:        for (mptr = &monst[0], monstnum = 0; monstnum < MAXMONSTER; mptr++, monstnum++)
                     47:        {
                     48:                if (mptr->stat == START)
                     49:                {
                     50:                        rscore[monstnum] = 1;
                     51: 
                     52:                        /* clear home */
                     53:                        PLOT(mptr->ypos, mptr->xpos, VACANT);
                     54: 
                     55:                        /* initialize moving monster */
                     56:                        mptr->ypos = MBEGINY;
                     57:                        mptr->xpos = MBEGINX;
                     58:                        mptr->speed = SLOW;
                     59:                        mptr->danger = TRUE;
                     60:                        mptr->stat = RUN;
                     61:                        PLOT(MBEGINY, MBEGINX, MONSTER);
                     62: 
                     63:                        /* DRIGHT or DLEFT? */
                     64:                        mptr->dirn = nrand(2) + DLEFT;
                     65:                        break;
                     66:                };
                     67:        };
                     68: }
                     69: 
                     70: monster(mnum)
                     71:        int mnum;
                     72: {
                     73:        register int newx,newy;
                     74:        register int tmpx, tmpy;
                     75:        struct pac *mptr;
                     76:        int gmod2;
                     77: 
                     78:        mptr = &monst[mnum];
                     79: 
                     80:        /* remember monster's current position */
                     81:        tmpx = mptr->xpos;
                     82:        tmpy = mptr->ypos;
                     83: 
                     84:        /* if we can, let's move a monster */
                     85:        if (mptr->stat == RUN)
                     86:        {
                     87:                gmod2 = game % 2;
                     88:                /* if a monster was displayed ... */
                     89:                if ((gmod2 == 1) ||
                     90:                        ((gmod2 == 0) &&
                     91:                        (( (rounds - 1) % rscore[mnum]) == 0)))
                     92:                {
                     93:                        /* replace display character */
                     94:                        PLOT(tmpy, tmpx, display[tmpy][tmpx]);
                     95:                };
                     96: 
                     97:                /* get a new direction */
                     98:                mptr->dirn = which(mptr, tmpx, tmpy);
                     99:                switch (mptr->dirn)
                    100:                {
                    101:                case DUP:
                    102:                        newy = tmpy + UPINT;
                    103:                        newx = tmpx;
                    104:                        break;
                    105: 
                    106:                case DDOWN:
                    107:                        newy = tmpy + DOWNINT;
                    108:                        newx = tmpx;
                    109:                        break;
                    110: 
                    111:                case DLEFT:
                    112:                        newx = tmpx + LEFTINT;
                    113:                        newy = tmpy;
                    114:                        if (newx <= 0)
                    115:                                newx = XWRAP;   /* wrap around */
                    116:                        break;
                    117: 
                    118:                case DRIGHT:
                    119:                        newx = tmpx + RIGHTINT;
                    120:                        newy = tmpy;
                    121:                        if (newx >= XWRAP)
                    122:                                newx = 0;       /* wrap around */
                    123:                        break;
                    124:                }
                    125: 
                    126:                /* use brd to determine if this was a valid direction */
                    127:                switch (brd[newy][newx])
                    128:                {
                    129:                case GOLD:
                    130:                case VACANT:
                    131:                case POTION:
                    132:                case TREASURE:
                    133:                case CHOICE:
                    134:                        /* set new position */
                    135:                        mptr->xpos = newx;
                    136:                        mptr->ypos = newy;
                    137: 
                    138:                        /* run into a pacman? */
                    139:                        if ((newy == pacptr->ypos) &&
                    140:                                (newx == pacptr->xpos))
                    141:                        {
                    142:                                killflg = dokill(mnum);
                    143:                        };
                    144:                        rscore[mnum] = pscore / 100 + 1;
                    145:                        if ((gmod2 == 1) || (killflg == TURKEY) ||
                    146:                                ( (gmod2 == 0) &&
                    147:                                ((rounds % rscore[mnum]) == 0)))
                    148:                        {
                    149: 
                    150:                                if (mptr->danger == TRUE)
                    151:                                {
                    152:                                        PLOT(newy, newx, MONSTER);
                    153:                                }
                    154:                                else if (killflg != GOTONE)
                    155:                                {
                    156:                                        PLOT(newy, newx, RUNNER);
                    157:                                };
                    158:                        };
                    159:                        break;
                    160: 
                    161:                default:
                    162:                        errgen("bad direction");
                    163:                        break;
                    164:                };
                    165:        }
                    166: }
                    167: 
                    168: which(mptr, x, y)      /* which directions are available ? */
                    169:        struct pac *mptr;
                    170:        int x, y;
                    171: {
                    172:        register int movecnt;
                    173:        register int submovecnt;
                    174:        register int next;
                    175:        int moves[4];
                    176:        int submoves[4];
                    177:        int nydirn, nxdirn;
                    178:        int goodmoves;
                    179:        int offx, offy;
                    180:        int tmpdirn;
                    181:        char *brdptr;
                    182: 
                    183:        /*
                    184:         * As a general rule: determine the set of all
                    185:         * possible moves, but select only those moves
                    186:         * that don't require a monster to backtrack.
                    187:         */
                    188:        movecnt = 0;
                    189:        brdptr = &(brd[y][x]);
                    190:        if (((tmpdirn = mptr->dirn) != DDOWN) &&
                    191:                ((next = *(brdptr + (BRDX * UPINT))) != WALL) &&
                    192:                (next != GATE))
                    193:        {
                    194:                moves[movecnt++] = DUP;
                    195:        };
                    196:        if ((tmpdirn != DUP) &&
                    197:                ((next = *(brdptr + (BRDX * DOWNINT))) != WALL) &&
                    198:                (next != GATE))
                    199:        {
                    200:                moves[movecnt++] = DDOWN;
                    201:        };
                    202:        if ((tmpdirn != DRIGHT) &&
                    203:                ((next = *(brdptr + LEFTINT)) != WALL) &&
                    204:                (next != GATE))
                    205:        {
                    206:                moves[movecnt++] = DLEFT;
                    207:        };
                    208:        if ((tmpdirn != DLEFT) &&
                    209:                ((next = *(brdptr + RIGHTINT)) != WALL) &&
                    210:                (next != GATE))
                    211:        {
                    212:                moves[movecnt++] = DRIGHT;
                    213:        };
                    214: 
                    215:        /*
                    216:         * If the player requested intelligent monsters and
                    217:         * the player is scoring high ...
                    218:         */
                    219:        if (((game == 3) || (game == 4)) && (nrand(1000) < pscore))
                    220:        {
                    221:                /* make monsters intelligent */
                    222:                if (pacptr->danger == TRUE)
                    223:                {
                    224:                        /*
                    225:                         * Holy Cow!! The pacman is dangerous,
                    226:                         * permit monsters to reverse direction
                    227:                         */
                    228:                        switch (tmpdirn)
                    229:                        {
                    230:                        case DUP:
                    231:                                if ((*(brdptr + (BRDX * DOWNINT)) != WALL) &&
                    232:                                        (*(brdptr + (BRDX * DOWNINT)) != GATE))
                    233:                                {
                    234:                                        moves[movecnt++] = DDOWN;
                    235:                                };
                    236:                                break;
                    237: 
                    238:                        case DDOWN:
                    239:                                if ((*(brdptr + (BRDX * UPINT)) != WALL) &&
                    240:                                        (*(brdptr + (BRDX * UPINT)) != GATE))
                    241:                                {
                    242:                                        moves[movecnt++] = DUP;
                    243:                                };
                    244:                                break;
                    245: 
                    246:                        case DRIGHT:
                    247:                                if ((*(brdptr + LEFTINT) != WALL) &&
                    248:                                        (*(brdptr + LEFTINT) != GATE))
                    249:                                {
                    250:                                        moves[movecnt++] = DLEFT;
                    251:                                };
                    252:                                break;
                    253: 
                    254:                        case DLEFT:
                    255:                                if ((*(brdptr + RIGHTINT) != WALL) &&
                    256:                                        (*(brdptr + RIGHTINT) != GATE))
                    257:                                {
                    258:                                        moves[movecnt++] = DRIGHT;
                    259:                                };
                    260:                                break;
                    261:                        };
                    262:                };
                    263: 
                    264:                /* determine the offset from the pacman */
                    265:                offx = x - pacptr->xpos;
                    266:                offy = y - pacptr->ypos;
                    267:                if (offx > 0)
                    268:                {
                    269:                        /*need to go left */
                    270:                        nxdirn = DLEFT;
                    271:                }
                    272:                else
                    273:                {
                    274:                        if (offx < 0)
                    275:                        {
                    276:                                nxdirn = DRIGHT;
                    277:                        }
                    278:                        else
                    279:                        {
                    280:                                /*need to stay here */
                    281:                                nxdirn = DNULL;
                    282:                        };
                    283:                };
                    284:                if (offy > 0)
                    285:                {
                    286:                        /*need to go up */
                    287:                        nydirn = DUP;
                    288:                }
                    289:                else
                    290:                {
                    291:                        if (offy < 0)
                    292:                        {
                    293:                                /* need to go down */
                    294:                                nydirn = DDOWN;
                    295:                        }
                    296:                        else
                    297:                        {
                    298:                                /* need to stay here */
                    299:                                nydirn = DNULL;
                    300:                        };
                    301:                };
                    302:                goodmoves = 0;
                    303:                for (submovecnt = 0; submovecnt < movecnt; submovecnt++)
                    304:                {
                    305:                        if (pacptr->danger == FALSE)
                    306:                        {
                    307:                                if ((moves[submovecnt] == nydirn) ||
                    308:                                        (moves[submovecnt] == nxdirn))
                    309:                                {
                    310:                                        submoves[goodmoves++] = moves[submovecnt];
                    311:                                };
                    312:                        }
                    313:                        else
                    314:                        {
                    315:                                if ((moves[submovecnt] != nydirn) &&
                    316:                                        (moves[submovecnt] != nxdirn))
                    317:                                {
                    318:                                        submoves[goodmoves++] = moves[submovecnt];
                    319:                                };
                    320:                        };
                    321:                };
                    322:                if (goodmoves > 0)
                    323:                {
                    324:                        return(submoves[nrand(goodmoves)]);
                    325:                };
                    326:        };
                    327:        return(moves[nrand(movecnt)]);
                    328: }

unix.superglobalmegacorp.com

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