Mercurial > hg > early-roguelike
comparison rogue5/passages.c @ 33:f502bf60e6e4
Import Rogue 5.4 from the Roguelike Restoration Project (r1490)
| author | elwin |
|---|---|
| date | Mon, 24 May 2010 20:10:59 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 32:2dcd75e6a736 | 33:f502bf60e6e4 |
|---|---|
| 1 /* | |
| 2 * Draw the connecting passages | |
| 3 * | |
| 4 * @(#)passages.c 4.22 (Berkeley) 02/05/99 | |
| 5 * | |
| 6 * Rogue: Exploring the Dungeons of Doom | |
| 7 * Copyright (C) 1980-1983, 1985, 1999 Michael Toy, Ken Arnold and Glenn Wichman | |
| 8 * All rights reserved. | |
| 9 * | |
| 10 * See the file LICENSE.TXT for full copyright and licensing information. | |
| 11 */ | |
| 12 | |
| 13 #include <stdlib.h> | |
| 14 #include <curses.h> | |
| 15 #include "rogue.h" | |
| 16 | |
| 17 /* | |
| 18 * do_passages: | |
| 19 * Draw all the passages on a level. | |
| 20 */ | |
| 21 | |
| 22 void | |
| 23 do_passages(void) | |
| 24 { | |
| 25 struct rdes *r1, *r2 = NULL; | |
| 26 int i, j; | |
| 27 int roomcount; | |
| 28 struct rdes | |
| 29 { | |
| 30 int conn[MAXROOMS]; /* possible to connect to room i? */ | |
| 31 int isconn[MAXROOMS]; /* connection been made to room i? */ | |
| 32 int ingraph; /* this room in graph already? */ | |
| 33 } rdes[MAXROOMS] = { | |
| 34 { { 0, 1, 0, 1, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0 }, | |
| 35 { { 1, 0, 1, 0, 1, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0 }, | |
| 36 { { 0, 1, 0, 0, 0, 1, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0 }, | |
| 37 { { 1, 0, 0, 0, 1, 0, 1, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0 }, | |
| 38 { { 0, 1, 0, 1, 0, 1, 0, 1, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0 }, | |
| 39 { { 0, 0, 1, 0, 1, 0, 0, 0, 1 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0 }, | |
| 40 { { 0, 0, 0, 1, 0, 0, 0, 1, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0 }, | |
| 41 { { 0, 0, 0, 0, 1, 0, 1, 0, 1 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0 }, | |
| 42 { { 0, 0, 0, 0, 0, 1, 0, 1, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0 }, | |
| 43 }; | |
| 44 | |
| 45 /* | |
| 46 * reinitialize room graph description | |
| 47 */ | |
| 48 for (r1 = rdes; r1 <= &rdes[MAXROOMS-1]; r1++) | |
| 49 { | |
| 50 for (j = 0; j < MAXROOMS; j++) | |
| 51 r1->isconn[j] = FALSE; | |
| 52 r1->ingraph = FALSE; | |
| 53 } | |
| 54 | |
| 55 /* | |
| 56 * starting with one room, connect it to a random adjacent room and | |
| 57 * then pick a new room to start with. | |
| 58 */ | |
| 59 roomcount = 1; | |
| 60 r1 = &rdes[rnd(MAXROOMS)]; | |
| 61 r1->ingraph = TRUE; | |
| 62 do | |
| 63 { | |
| 64 /* | |
| 65 * find a room to connect with | |
| 66 */ | |
| 67 j = 0; | |
| 68 for (i = 0; i < MAXROOMS; i++) | |
| 69 if (r1->conn[i] && !rdes[i].ingraph && rnd(++j) == 0) | |
| 70 r2 = &rdes[i]; | |
| 71 /* | |
| 72 * if no adjacent rooms are outside the graph, pick a new room | |
| 73 * to look from | |
| 74 */ | |
| 75 if (j == 0) | |
| 76 { | |
| 77 do | |
| 78 r1 = &rdes[rnd(MAXROOMS)]; | |
| 79 until (r1->ingraph); | |
| 80 } | |
| 81 /* | |
| 82 * otherwise, connect new room to the graph, and draw a tunnel | |
| 83 * to it | |
| 84 */ | |
| 85 else | |
| 86 { | |
| 87 r2->ingraph = TRUE; | |
| 88 i = (int)(r1 - rdes); | |
| 89 j = (int)(r2 - rdes); | |
| 90 conn(i, j); | |
| 91 r1->isconn[j] = TRUE; | |
| 92 r2->isconn[i] = TRUE; | |
| 93 roomcount++; | |
| 94 } | |
| 95 } while (roomcount < MAXROOMS); | |
| 96 | |
| 97 /* | |
| 98 * attempt to add passages to the graph a random number of times so | |
| 99 * that there isn't always just one unique passage through it. | |
| 100 */ | |
| 101 for (roomcount = rnd(5); roomcount > 0; roomcount--) | |
| 102 { | |
| 103 r1 = &rdes[rnd(MAXROOMS)]; /* a random room to look from */ | |
| 104 /* | |
| 105 * find an adjacent room not already connected | |
| 106 */ | |
| 107 j = 0; | |
| 108 for (i = 0; i < MAXROOMS; i++) | |
| 109 if (r1->conn[i] && !r1->isconn[i] && rnd(++j) == 0) | |
| 110 r2 = &rdes[i]; | |
| 111 /* | |
| 112 * if there is one, connect it and look for the next added | |
| 113 * passage | |
| 114 */ | |
| 115 if (j != 0) | |
| 116 { | |
| 117 i = (int)(r1 - rdes); | |
| 118 j = (int)(r2 - rdes); | |
| 119 conn(i, j); | |
| 120 r1->isconn[j] = TRUE; | |
| 121 r2->isconn[i] = TRUE; | |
| 122 } | |
| 123 } | |
| 124 passnum(); | |
| 125 } | |
| 126 | |
| 127 /* | |
| 128 * conn: | |
| 129 * Draw a corridor from a room in a certain direction. | |
| 130 */ | |
| 131 | |
| 132 void | |
| 133 conn(int r1, int r2) | |
| 134 { | |
| 135 struct room *rpf, *rpt = NULL; | |
| 136 int rmt; | |
| 137 int distance = 0, turn_spot, turn_distance = 0; | |
| 138 int rm; | |
| 139 int direc; | |
| 140 static coord del, turn_delta; | |
| 141 coord curr, spos, epos; | |
| 142 | |
| 143 if (r1 < r2) | |
| 144 { | |
| 145 rm = r1; | |
| 146 if (r1 + 1 == r2) | |
| 147 direc = 'r'; | |
| 148 else | |
| 149 direc = 'd'; | |
| 150 } | |
| 151 else | |
| 152 { | |
| 153 rm = r2; | |
| 154 if (r2 + 1 == r1) | |
| 155 direc = 'r'; | |
| 156 else | |
| 157 direc = 'd'; | |
| 158 } | |
| 159 rpf = &rooms[rm]; | |
| 160 /* | |
| 161 * Set up the movement variables, in two cases: | |
| 162 * first drawing one down. | |
| 163 */ | |
| 164 if (direc == 'd') | |
| 165 { | |
| 166 rmt = rm + 3; /* room # of dest */ | |
| 167 rpt = &rooms[rmt]; /* room pointer of dest */ | |
| 168 del.x = 0; /* direction of move */ | |
| 169 del.y = 1; | |
| 170 spos.x = rpf->r_pos.x; /* start of move */ | |
| 171 spos.y = rpf->r_pos.y; | |
| 172 epos.x = rpt->r_pos.x; /* end of move */ | |
| 173 epos.y = rpt->r_pos.y; | |
| 174 if (!(rpf->r_flags & ISGONE)) /* if not gone pick door pos */ | |
| 175 do | |
| 176 { | |
| 177 spos.x = rpf->r_pos.x + rnd(rpf->r_max.x - 2) + 1; | |
| 178 spos.y = rpf->r_pos.y + rpf->r_max.y - 1; |
