[BACK]Return to passages.c CVS log [TXT][DIR] Up to [contributed] / early-roguelike / arogue7

Annotation of early-roguelike/arogue7/passages.c, Revision 1.1.1.1

1.1       rubenllo    1: /*
                      2:  * passages.c  -  Draw the connecting passages
                      3:  *
                      4:  * Advanced Rogue
                      5:  * Copyright (C) 1984, 1985, 1986 Michael Morgan, Ken Dalka and AT&T
                      6:  * All rights reserved.
                      7:  *
                      8:  * Based on "Rogue: Exploring the Dungeons of Doom"
                      9:  * Copyright (C) 1980, 1981 Michael Toy, Ken Arnold and Glenn Wichman
                     10:  * All rights reserved.
                     11:  *
                     12:  * See the file LICENSE.TXT for full copyright and licensing information.
                     13:  */
                     14:
                     15: /*
                     16:  * Draw the connecting passages
                     17:  *
                     18:  * @(#)passages.c      3.4 (Berkeley) 6/15/81
                     19:  */
                     20:
                     21: #include <stdlib.h>
                     22: #include "curses.h"
                     23: #include "rogue.h"
                     24:
                     25: void conn(int r1, int r2);
                     26: void door(struct room *rm, coord *cp);
                     27:
                     28: /*
                     29:  * do_passages:
                     30:  *     Draw all the passages on a level.
                     31:  */
                     32:
                     33: void
                     34: do_passages(void)
                     35: {
                     36:     register struct rdes *r1, *r2 = NULL;
                     37:     register int i, j;
                     38:     register int roomcount;
                     39:     static struct rdes
                     40:     {
                     41:        bool    conn[MAXROOMS];         /* possible to connect to room i? */
                     42:        bool    isconn[MAXROOMS];       /* connection been made to room i? */
                     43:        bool    ingraph;                /* this room in graph already? */
                     44:     } rdes[MAXROOMS] = {
                     45:        { { 0, 1, 0, 1, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0 },
                     46:        { { 1, 0, 1, 0, 1, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0 },
                     47:        { { 0, 1, 0, 0, 0, 1, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0 },
                     48:        { { 1, 0, 0, 0, 1, 0, 1, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0 },
                     49:        { { 0, 1, 0, 1, 0, 1, 0, 1, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0 },
                     50:        { { 0, 0, 1, 0, 1, 0, 0, 0, 1 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0 },
                     51:        { { 0, 0, 0, 1, 0, 0, 0, 1, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0 },
                     52:        { { 0, 0, 0, 0, 1, 0, 1, 0, 1 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0 },
                     53:        { { 0, 0, 0, 0, 0, 1, 0, 1, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0 },
                     54:     };
                     55:
                     56:     /*
                     57:      * reinitialize room graph description
                     58:      */
                     59:     for (r1 = rdes; r1 <= &rdes[MAXROOMS-1]; r1++)
                     60:     {
                     61:        for (j = 0; j < MAXROOMS; j++)
                     62:            r1->isconn[j] = FALSE;
                     63:        r1->ingraph = FALSE;
                     64:     }
                     65:
                     66:     /*
                     67:      * starting with one room, connect it to a random adjacent room and
                     68:      * then pick a new room to start with.
                     69:      */
                     70:     roomcount = 1;
                     71:     r1 = &rdes[rnd(MAXROOMS)];
                     72:     r1->ingraph = TRUE;
                     73:     do
                     74:     {
                     75:        /*
                     76:         * find a room to connect with
                     77:         */
                     78:        j = 0;
                     79:        for (i = 0; i < MAXROOMS; i++)
                     80:            if (r1->conn[i] && !rdes[i].ingraph && rnd(++j) == 0)
                     81:                r2 = &rdes[i];
                     82:        /*
                     83:         * if no adjacent rooms are outside the graph, pick a new room
                     84:         * to look from
                     85:         */
                     86:        if (j == 0)
                     87:        {
                     88:            do
                     89:                r1 = &rdes[rnd(MAXROOMS)];
                     90:            until (r1->ingraph);
                     91:        }
                     92:        /*
                     93:         * otherwise, connect new room to the graph, and draw a tunnel
                     94:         * to it
                     95:         */
                     96:        else
                     97:        {
                     98:            r2->ingraph = TRUE;
                     99:            i = (int)(r1 - rdes);
                    100:            j = (int)(r2 - rdes);
                    101:            conn(i, j);
                    102:            r1->isconn[j] = TRUE;
                    103:            r2->isconn[i] = TRUE;
                    104:            roomcount++;
                    105:        }
                    106:     } while (roomcount < MAXROOMS);
                    107:
                    108:     /*
                    109:      * attempt to add passages to the graph a random number of times so
                    110:      * that there isn't just one unique passage through it.
                    111:      */
                    112:     for (roomcount = rnd(5); roomcount > 0; roomcount--)
                    113:     {
                    114:        r1 = &rdes[rnd(MAXROOMS)];      /* a random room to look from */
                    115:        /*
                    116:         * find an adjacent room not already connected
                    117:         */
                    118:        j = 0;
                    119:        for (i = 0; i < MAXROOMS; i++)
                    120:            if (r1->conn[i] && !r1->isconn[i] && rnd(++j) == 0)
                    121:                r2 = &rdes[i];
                    122:        /*
                    123:         * if there is one, connect it and look for the next added
                    124:         * passage
                    125:         */
                    126:        if (j != 0)
                    127:        {
                    128:            i = (int)(r1 - rdes);
                    129:            j = (int)(r2 - rdes);
                    130:            conn(i, j);
                    131:            r1->isconn[j] = TRUE;
                    132:            r2->isconn[i] = TRUE;
                    133:        }
                    134:     }
                    135: }
                    136:
                    137: /*
                    138:  * conn:
                    139:  *     Draw a corridor from a room in a certain direction.
                    140:  */
                    141:
                    142: void
                    143: conn(int r1, int r2)
                    144: {
                    145:     register struct room *rpf, *rpt = NULL;
                    146:     register char rmt;
                    147:     register int distance, max_diag, offset, i;
                    148:     register int rm;
                    149:     int turns[3], turn_dist[3];
                    150:     register char direc;
                    151:     coord delta, curr, turn_delta, spos, epos;
                    152:
                    153:     if (r1 < r2)
                    154:     {
                    155:        rm = r1;
                    156:        if (r1 + 1 == r2)
                    157:            direc = 'r';
                    158:        else
                    159:            direc = 'd';
                    160:     }
                    161:     else
                    162:     {
                    163:        rm = r2;
                    164:        if (r2 + 1 == r1)
                    165:            direc = 'r';
                    166:        else
                    167:            direc = 'd';
                    168:     }
                    169:     rpf = &rooms[rm];
                    170:     /*
                    171:      * Set up the movement variables, in two cases:
                    172:      * first drawing one down.
                    173:      */
                    174:     if (direc == 'd')
                    175:     {
                    176:        rmt = rm + 3;                           /* room # of dest */
                    177:        rpt = &rooms[rmt];                      /* room pointer of dest */
                    178:        delta.x = 0;                            /* direction of move */
                    179:        delta.y = 1;
                    180:        spos.x = rpf->r_pos.x;                  /* start of move */
                    181:        spos.y = rpf->r_pos.y;
                    182:        epos.x = rpt->r_pos.x;                  /* end of move */
                    183:        epos.y = rpt->r_pos.y;
                    184:        if (!(rpf->r_flags & ISGONE))           /* if not gone pick door pos */
                    185:        {
                    186:            spos.x += rnd(rpf->r_max.x-2)+1;
                    187:            spos.y += rpf->r_max.y-1;
                    188:        }
                    189:        if (!(rpt->r_flags & ISGONE))
                    190:            epos.x += rnd(rpt->r_max.x-2)+1;
                    191:        distance = abs(spos.y - epos.y) - 1;    /* distance to move */
                    192:        turn_delta.y = 0;                       /* direction to turn */
                    193:        turn_delta.x = (spos.x < epos.x ? 1 : -1);
                    194:        offset = abs(spos.x - epos.x);  /* how far to turn */
                    195:     }
                    196:     else if (direc == 'r')                     /* setup for moving right */
                    197:     {
                    198:        rmt = rm + 1;
                    199:        rpt = &rooms[rmt];
                    200:        delta.x = 1;
                    201:        delta.y = 0;
                    202:        spos.x = rpf->r_pos.x;
                    203:        spos.y = rpf->r_pos.y;
                    204:        epos.x = rpt->r_pos.x;
                    205:        epos.y = rpt->r_pos.y;
                    206:        if (!(rpf->r_flags & ISGONE))
                    207:        {
                    208:            spos.x += rpf->r_max.x-1;
                    209:            spos.y += rnd(rpf->r_max.y-2)+1;
                    210:        }
                    211:        if (!(rpt->r_flags & ISGONE))
                    212:            epos.y += rnd(rpt->r_max.y-2)+1;
                    213:        distance = abs(spos.x - epos.x) - 1;
                    214:        turn_delta.y = (spos.y < epos.y ? 1 : -1);
                    215:        turn_delta.x = 0;
                    216:        offset = abs(spos.y - epos.y);
                    217:     }
                    218:     else
                    219:        debug("error in connection tables");
                    220:
                    221:     /*
                    222:      * Draw in the doors on either side of the passage or just put #'s
                    223:      * if the rooms are gone.
                    224:      */
                    225:     if (!(rpf->r_flags & ISGONE)) door(rpf, &spos);
                    226:     else
                    227:     {
                    228:        cmov(spos);
                    229:        addch('#');
                    230:     }
                    231:     if (!(rpt->r_flags & ISGONE)) door(rpt, &epos);
                    232:     else
                    233:     {
                    234:        cmov(epos);
                    235:        addch('#');
                    236:     }
                    237:
                    238:     /* How far can we move diagonally? */
                    239:     max_diag = min(distance, offset);
                    240:
                    241:     /*
                    242:      * Decide how many turns we will have.
                    243:      */
                    244:     for (i=0; i<3; i++) turn_dist[i] = 0;      /* Init distances */
                    245:     if (max_diag > 0) {
                    246:        int nturns;
                    247:
                    248:        for (i=0, nturns=0; i<3; i++) {
                    249:            if (rnd(3 - i + nturns) == 0) {
                    250:                nturns++;
                    251:                turns[i] = 0;
                    252:            }
                    253:            else turns[i] = -1;
                    254:        }
                    255:     }
                    256:     else {
                    257:        /* Just use a straight line (middle turn) */
                    258:        turns[0] = turns[2] = -1;
                    259:        turns[1] = 0;
                    260:     }
                    261:
                    262:     /*
                    263:      * Now decide how long each turn will be (for those selected above).
                    264:      */
                    265:     while (max_diag > 0) {
                    266:        for (i=0; i<3; i++) {
                    267:            if (turns[i] >= 0 && max_diag > 0 && rnd(2) == 0) {
                    268:                turn_dist[i]++;
                    269:                max_diag--;
                    270:            }
                    271:        }
                    272:     }
                    273:
                    274:     /*
                    275:      * If we have extra offset space, add it to the straight turn.
                    276:      */
                    277:     if (offset > distance) turn_dist[1] += offset - distance;
                    278:
                    279:     /*
                    280:      * Decide where we want to make our turns.
                    281:      * First calculate the offsets, then use those offsets to calculate
                    282:      * the exact position relative to "distance."
                    283:      */
                    284:     turns[0] = rnd(distance - turn_dist[0] - turn_dist[2]);
                    285:     turns[2] = rnd(distance - turn_dist[0] - turn_dist[2] - turns[0]);
                    286:     turns[1] = rnd(distance - turn_dist[0] - turn_dist[2] -
                    287:                   turns[0] - turns[2]);
                    288:
                    289:     turns[0] = distance - turns[0];
                    290:     turns[1] = turns[0] - turn_dist[0] - turns[1];
                    291:     turns[2] = turns[1] - turns[2];
                    292:
                    293:     /*
                    294:      * Get ready to move...
                    295:      */
                    296:     curr.x = spos.x;
                    297:     curr.y = spos.y;
                    298:     while (distance > 0) {
                    299:        /*
                    300:         * Move to next row/column
                    301:         */
                    302:        curr.x += delta.x;
                    303:        curr.y += delta.y;
                    304:
                    305:        /*
                    306:         * Check if we are at a turn place; if so make a turn
                    307:         */
                    308:        for (i=0; i<3; i++) {
                    309:            if (distance == turns[i] && turn_dist[i] > 0) {
                    310:                /*
                    311:                 * If this is the start of a straight path,
                    312:                 * we might put in a right-angle turn (33% chance).
                    313:                 */
                    314:                if (i == 1 && rnd(3) == 0) {
                    315:                    cmov(curr);
                    316:                    addch(PASSAGE);
                    317:                }
                    318:
                    319:                /* Now dig the turn */
                    320:                while (turn_dist[i]--) {
                    321:                    curr.x += turn_delta.x;
                    322:                    curr.y += turn_delta.y;
                    323:                    cmov(curr);
                    324:                    addch(PASSAGE);
                    325:                    if (i != 1) {       /* A diagonal */
                    326:                        if (--distance > 0) {
                    327:                            curr.x += delta.x;
                    328:                            curr.y += delta.y;
                    329:                        }
                    330:                    }
                    331:                }
                    332:            }
                    333:        }
                    334:
                    335:        if (distance > 0) {
                    336:            /*
                    337:             * Dig the passage.
                    338:             */
                    339:            cmov(curr);
                    340:            addch(PASSAGE);
                    341:            distance--;
                    342:        }
                    343:     }
                    344:     curr.x += delta.x;
                    345:     curr.y += delta.y;
                    346:     if (!ce(curr, epos))
                    347:        msg("Warning, connectivity problem (%d, %d) to (%d, %d).",
                    348:            curr.y, curr.x, epos.y, epos.x);
                    349: }
                    350:
                    351: /*
                    352:  * Add a door or possibly a secret door
                    353:  * also enters the door in the exits array of the room.
                    354:  */
                    355:
                    356: void
                    357: door(struct room *rm, coord *cp)
                    358: {
                    359:     struct linked_list *newroom;
                    360:     coord *exit;
                    361:
                    362:     cmov(*cp);
                    363:     addch((rnd(10) < level - 1 && rnd(100) < 20) ? SECRETDOOR : DOOR);
                    364:
                    365:     /* Insert the new room into the linked list of rooms */
                    366:     newroom = new_item(sizeof(coord));
                    367:     exit = DOORPTR(newroom);
                    368:     *exit = *cp;
                    369:     attach(rm->r_exit, newroom);
                    370: }

CVSweb