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

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

1.1       rubenllo    1: /*
                      2:  * rooms.c - Draw the nine rooms on the screen
                      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 nine rooms on the screen
                     17:  *
                     18:  */
                     19:
                     20: #include <stdlib.h>
                     21: #include "curses.h"
                     22: #include "rogue.h"
                     23:
                     24: void horiz(int cnt);
                     25: void vert(int cnt);
                     26:
                     27: void
                     28: do_rooms(void)
                     29: {
                     30:     register int i;
                     31:     register struct room *rp;
                     32:     register struct linked_list *item;
                     33:     register struct thing *tp;
                     34:     int left_out;
                     35:     int num_monsters;
                     36:     int which_monster;
                     37:     int j;
                     38:     coord top;
                     39:     coord bsze;
                     40:     coord mp;
                     41:     coord *np;
                     42:
                     43:     /*
                     44:      * bsze is the maximum room size
                     45:      */
                     46:     bsze.x = cols/3;
                     47:     bsze.y = (lines-2)/3;
                     48:     /*
                     49:      * Clear things for a new level
                     50:      */
                     51:     for (rp = rooms; rp < &rooms[MAXROOMS]; rp++) {
                     52:        rp->r_flags = 0;
                     53:        rp->r_fires = NULL;
                     54:     }
                     55:     /*
                     56:      * Put the gone rooms, if any, on the level
                     57:      */
                     58:     left_out = rnd(4);
                     59:     for (i = 0; i < left_out; i++)
                     60:        rooms[rnd_room()].r_flags |= ISGONE;
                     61:     /*
                     62:      * dig and populate all the rooms on the level
                     63:      */
                     64:     for (i = 0, rp = rooms; i < MAXROOMS; rp++, i++)
                     65:     {
                     66:        bool has_gold=FALSE;
                     67:
                     68:        /*
                     69:         * Find upper left corner of box that this room goes in
                     70:         */
                     71:        top.x = (i%3)*bsze.x;
                     72:        top.y = i/3*bsze.y + 1;
                     73:        if (rp->r_flags & ISGONE)
                     74:        {
                     75:            /*
                     76:             * Place a gone room.  Make certain that there is a blank line
                     77:             * for passage drawing.
                     78:             */
                     79:            do
                     80:            {
                     81:                rp->r_pos.x = top.x + rnd(bsze.x-2) + 1;
                     82:                rp->r_pos.y = top.y + rnd(bsze.y-2) + 1;
                     83:                rp->r_max.x = -cols;
                     84:                rp->r_max.x = -lines;
                     85:            } until(rp->r_pos.y > 0 && rp->r_pos.y < lines-2);
                     86:            continue;
                     87:        }
                     88:        if (rnd(10) < level-1)
                     89:            rp->r_flags |= ISDARK;
                     90:        /*
                     91:         * Find a place and size for a random room
                     92:         */
                     93:        do
                     94:        {
                     95:            rp->r_max.x = rnd(bsze.x - 4) + 4;
                     96:            rp->r_max.y = rnd(bsze.y - 4) + 4;
                     97:            rp->r_pos.x = top.x + rnd(bsze.x - rp->r_max.x);
                     98:            rp->r_pos.y = top.y + rnd(bsze.y - rp->r_max.y);
                     99:        } until (rp->r_pos.y != 0);
                    100:
                    101:        /* Draw the room */
                    102:        draw_room(rp);
                    103:
                    104:        /*
                    105:         * Put the gold in
                    106:         */
                    107:        if (rnd(100) < 50 && level >= cur_max)
                    108:        {
                    109:            register struct linked_list *item;
                    110:            register struct object *cur;
                    111:            coord tp;
                    112:
                    113:            has_gold = TRUE;    /* This room has gold in it */
                    114:
                    115:            item = spec_item(GOLD, 0, 0, 0);
                    116:            cur = OBJPTR(item);
                    117:
                    118:            /* Put the gold into the level list of items */
                    119:            attach(lvl_obj, item);
                    120:
                    121:            /* Put it somewhere */
                    122:            rnd_pos(rp, &tp);
                    123:            mvaddch(tp.y, tp.x, GOLD);
                    124:            cur->o_pos = tp;
                    125:            if (roomin(&tp) != rp) {
                    126:                endwin();
                    127:                abort();
                    128:            }
                    129:        }
                    130:
                    131:        /*
                    132:         * Put the monster in
                    133:         */
                    134:        if (rnd(100) < (has_gold ? 80 : 25) + vlevel/2)
                    135:        {
                    136:            do
                    137:            {
                    138:                rnd_pos(rp, &mp);
                    139:            } until(mvwinch(stdscr, mp.y, mp.x) == FLOOR);
                    140:            which_monster = randmonster(FALSE, FALSE);
                    141:            num_monsters = 1;
                    142:            /*
                    143:             * see if we should make a whole bunch
                    144:             */
                    145:            for (j=0; j<MAXFLAGS; j++) {
                    146:                if (monsters[which_monster].m_flags[j] == AREMANY)
                    147:                        num_monsters = roll(3,3);
                    148:            }
                    149:            for (j=0; j<num_monsters; j++) {
                    150:                    if ((np = fallpos(&mp, FALSE, 2)) != NULL &&
                    151:                         mvwinch(stdscr, np->y, np->x) == FLOOR) {
                    152:                            item = new_item(sizeof *tp);
                    153:                            tp = THINGPTR(item);
                    154:                            new_monster(item, which_monster, np, FALSE);
                    155:                            /*
                    156:                             * See if we want to give it a treasure to
                    157:                             * carry around.
                    158:                             */
                    159:                            carry_obj(tp, monsters[tp->t_index].m_carry);
                    160:                            tp->t_no_move = movement(tp);
                    161:
                    162:                            /*
                    163:                             * If it has a fire, mark it
                    164:                             */
                    165:                            if (on(*tp, HASFIRE)) {
                    166:                                register struct linked_list *fire_item;
                    167:
                    168:                                fire_item = creat_item();
                    169:                                ldata(fire_item) = (char *) tp;
                    170:                                attach(rp->r_fires, fire_item);
                    171:                                rp->r_flags |= HASFIRE;
                    172:                            }
                    173:                    }
                    174:            }
                    175:        }
                    176:     }
                    177: }
                    178: 
                    179: /*
                    180:  * Given a room pointer and a pointer to a door, supposedly in that room,
                    181:  * return the coordinates of the entrance to the doorway.
                    182:  */
                    183:
                    184: coord *
                    185: doorway(struct room *rp, coord *door)
                    186: {
                    187:     register int misses = 0;
                    188:     static coord answer;
                    189:
                    190:     /* Do we have decent parameters? */
                    191:     if (rp == NULL || door == NULL) return(NULL);
                    192:
                    193:     /* Initialize the answer to be the door, then calculate the offset */
                    194:     answer = *door;
                    195:
                    196:     /* Calculate the x-offset */
                    197:     if (door->x == rp->r_pos.x) answer.x++;
                    198:     else if (door->x == rp->r_pos.x + rp->r_max.x - 1) answer.x--;
                    199:     else misses++;
                    200:
                    201:     /* Calculate the y-offset */
                    202:     if (door->y == rp->r_pos.y) answer.y++;
                    203:     else if (door->y == rp->r_pos.y + rp->r_max.y - 1) answer.y--;
                    204:     else misses++;
                    205:
                    206:     if (misses <= 1) return(&answer);
                    207:     else return(NULL);
                    208: }
                    209: 
                    210: /*
                    211:  * Draw a box around a room
                    212:  */
                    213:
                    214: void
                    215: draw_room(struct room *rp)
                    216: {
                    217:     register int j, k;
                    218:
                    219:     move(rp->r_pos.y, rp->r_pos.x+1);
                    220:     vert(rp->r_max.y-2);                               /* Draw left side */
                    221:     move(rp->r_pos.y+rp->r_max.y-1, rp->r_pos.x);
                    222:     horiz(rp->r_max.x);                                        /* Draw bottom */
                    223:     move(rp->r_pos.y, rp->r_pos.x);
                    224:     horiz(rp->r_max.x);                                        /* Draw top */
                    225:     vert(rp->r_max.y-2);                               /* Draw right side */
                    226:     /*
                    227:      * Put the floor down
                    228:      */
                    229:     for (j = 1; j < rp->r_max.y-1; j++)
                    230:     {
                    231:        move(rp->r_pos.y + j, rp->r_pos.x+1);
                    232:        for (k = 1; k < rp->r_max.x-1; k++)
                    233:            addch(FLOOR);
                    234:     }
                    235: }
                    236: 
                    237: /*
                    238:  * horiz:
                    239:  *     draw a horizontal line
                    240:  */
                    241:
                    242: void
                    243: horiz(int cnt)
                    244: {
                    245:     while (cnt--)
                    246:        addch('-');
                    247: }
                    248: 
                    249: /*
                    250:  * rnd_pos:
                    251:  *     pick a random spot in a room
                    252:  */
                    253:
                    254: void
                    255: rnd_pos(struct room *rp, coord *cp)
                    256: {
                    257:     cp->x = rp->r_pos.x + rnd(rp->r_max.x-2) + 1;
                    258:     cp->y = rp->r_pos.y + rnd(rp->r_max.y-2) + 1;
                    259: }
                    260:
                    261:
                    262: 
                    263: /*
                    264:  * roomin:
                    265:  *     Find what room some coordinates are in. NULL means they aren't
                    266:  *     in any room.
                    267:  */
                    268:
                    269: struct room *
                    270: roomin(coord *cp)
                    271: {
                    272:     register struct room *rp;
                    273:
                    274:     for (rp = rooms; rp < &rooms[MAXROOMS]; rp++)
                    275:        if (inroom(rp, cp))
                    276:            return rp;
                    277:     return NULL;
                    278: }
                    279: 
                    280: /*
                    281:  * vert:
                    282:  *     draw a vertical line
                    283:  */
                    284:
                    285: void
                    286: vert(int cnt)
                    287: {
                    288:     register int x, y;
                    289:
                    290:     getyx(stdscr, y, x);
                    291:     x--;
                    292:     while (cnt--) {
                    293:        move(++y, x);
                    294:        addch('|');
                    295:     }
                    296: }

CVSweb