| 1 |
(* Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 |
| 2 |
Free Software Foundation, Inc. *) |
| 3 |
(* This file is part of Chisel. |
| 4 |
|
| 5 |
Chisel is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License as published by the Free |
| 7 |
Software Foundation; either version 3, or (at your option) any later |
| 8 |
version. |
| 9 |
|
| 10 |
Chisel is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 |
for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public License along |
| 16 |
with gm2; see the file COPYING. If not, write to the Free Software |
| 17 |
Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *) |
| 18 |
|
| 19 |
IMPLEMENTATION MODULE RoomMap ; |
| 20 |
|
| 21 |
|
| 22 |
FROM MapOptions IMPORT isVerbose, isDebugging, isStatistics ; |
| 23 |
FROM NumberIO IMPORT WriteCard ; |
| 24 |
FROM StrIO IMPORT WriteLn, WriteString ; |
| 25 |
FROM Assertion IMPORT Assert ; |
| 26 |
|
| 27 |
FROM Geometry IMPORT IsSubLine, IsSubRange, Swap, IntersectionLength, |
| 28 |
Abs, Max, Min, IsPointOnLine, IsIntersectingRange ; |
| 29 |
|
| 30 |
FROM Chance IMPORT GetRand, InitRandom, KillRandom, GetAndDeleteRandom, |
| 31 |
AddRandom ; |
| 32 |
|
| 33 |
FROM StoreCoords IMPORT InitCoords, KillCoords, GetAndDeleteRandomCoord, |
| 34 |
AddCoord ; |
| 35 |
|
| 36 |
FROM BoxMap IMPORT Boxes, NoOfBoxes, NoOfCorridorBoxes, CreateBoxMap, |
| 37 |
MinDoorLength, MaxDoorLength ; |
| 38 |
|
| 39 |
CONST |
| 40 |
MaxLineStack = 20 ; |
| 41 |
CorridorDoorLength = 2 ; |
| 42 |
|
| 43 |
VAR |
| 44 |
NoOfCorridors: CARDINAL ; |
| 45 |
NoOfLines : CARDINAL ; |
| 46 |
Lines : ARRAY [1..MaxLineStack] OF Line ; |
| 47 |
|
| 48 |
|
| 49 |
(* |
| 50 |
CreateRoomMap - copy boxes into rooms and amalgamate boxes into rooms. |
| 51 |
*) |
| 52 |
|
| 53 |
PROCEDURE CreateRoomMap ; |
| 54 |
BEGIN |
| 55 |
RETURN ; (* Remove this line if you really want to generate a map. *) |
| 56 |
(* WriteString('Creating Boxes') ; WriteLn ; *) |
| 57 |
CreateBoxMap ; |
| 58 |
(* WriteString('Creating Rooms') ; WriteLn ; *) |
| 59 |
InitRooms ; |
| 60 |
CreateCorridors ; |
| 61 |
CreateRooms ; |
| 62 |
END CreateRoomMap ; |
| 63 |
|
| 64 |
|
| 65 |
(* |
| 66 |
InitRooms - copies the box array from Module BoxMap into the Room array. |
| 67 |
*) |
| 68 |
|
| 69 |
PROCEDURE InitRooms ; |
| 70 |
VAR |
| 71 |
i: CARDINAL ; |
| 72 |
BEGIN |
| 73 |
i := 1 ; |
| 74 |
WHILE i<=NoOfBoxes DO |
| 75 |
WITH Rooms[i] DO |
| 76 |
RoomNo := i ; |
| 77 |
NoOfDoors := 0 ; |
| 78 |
NoOfWalls := 4 ; |
| 79 |
(* Treasures := {} ; *) |
| 80 |
Walls[1].X1 := Boxes[i].x1 ; (* Lower y=c *) |
| 81 |
Walls[1].Y1 := Boxes[i].y1 ; |
| 82 |
Walls[1].X2 := Boxes[i].x2 ; |
| 83 |
Walls[1].Y2 := Boxes[i].y1 ; |
| 84 |
|
| 85 |
Walls[2].X1 := Boxes[i].x2 ; (* Right x=c *) |
| 86 |
Walls[2].Y1 := Boxes[i].y1 ; |
| 87 |
Walls[2].X2 := Boxes[i].x2 ; |
| 88 |
Walls[2].Y2 := Boxes[i].y2 ; |
| 89 |
|
| 90 |
Walls[3].X1 := Boxes[i].x1 ; (* Top y=c *) |
| 91 |
Walls[3].Y1 := Boxes[i].y2 ; |
| 92 |
Walls[3].X2 := Boxes[i].x2 ; |
| 93 |
Walls[3].Y2 := Boxes[i].y2 ; |
| 94 |
|
| 95 |
Walls[4].X1 := Boxes[i].x1 ; (* Left x=c *) |
| 96 |
Walls[4].Y1 := Boxes[i].y1 ; |
| 97 |
Walls[4].X2 := Boxes[i].x1 ; |
| 98 |
Walls[4].Y2 := Boxes[i].y2 |
| 99 |
END ; |
| 100 |
INC(i) |
| 101 |
END ; |
| 102 |
GenNoOfRooms := NoOfBoxes ; |
| 103 |
NoOfCorridors := NoOfCorridorBoxes ; |
| 104 |
(* Now set all other rooms to void *) |
| 105 |
i := GenNoOfRooms+1 ; |
| 106 |
WHILE i<=MaxNoOfRooms DO |
| 107 |
Rooms[i].RoomNo := 0 ; |
| 108 |
INC(i) |
| 109 |
END ; |
| 110 |
IF isStatistics () |
| 111 |
THEN |
| 112 |
WriteString('Corridors') ; WriteCard(NoOfCorridors, 4) ; WriteLn ; |
| 113 |
WriteString('Rooms ') ; WriteCard(GenNoOfRooms, 4) ; WriteLn |
| 114 |
END |
| 115 |
END InitRooms ; |
| 116 |
|
| 117 |
|
| 118 |
(* |
| 119 |
CreateCorridors - creates corridors from the corridor boxes. |
| 120 |
*) |
| 121 |
|
| 122 |
PROCEDURE CreateCorridors ; |
| 123 |
BEGIN |
| 124 |
AmalgamateCorridors ; |
| 125 |
CreateCorridorDoors |
| 126 |
END CreateCorridors ; |
| 127 |
|
| 128 |
|
| 129 |
(* |
| 130 |
CreateRooms - creates rooms from the room boxes. |
| 131 |
*) |
| 132 |
|
| 133 |
PROCEDURE CreateRooms ; |
| 134 |
BEGIN |
| 135 |
AmalgamateRooms ; |
| 136 |
IF isVerbose () |
| 137 |
THEN |
| 138 |
WriteString('Creating RoomDoors') ; WriteLn |
| 139 |
END ; |
| 140 |
CreateMinRoomDoors ; |
| 141 |
CreateRoomDoors |
| 142 |
END CreateRooms ; |
| 143 |
|
| 144 |
|
| 145 |
(* |
| 146 |
CreateCorridorDoors - places corridors doors along the corridors. |
| 147 |
*) |
| 148 |
|
| 149 |
PROCEDURE CreateCorridorDoors ; |
| 150 |
VAR |
| 151 |
i, j: CARDINAL ; |
| 152 |
BEGIN |
| 153 |
i := 1 ; |
| 154 |
WHILE i<=NoOfCorridors DO |
| 155 |
j := 1 ; |
| 156 |
WHILE j<=NoOfCorridors DO |
| 157 |
IF (i#j) AND RoomExists(i) AND RoomExists(j) |
| 158 |
THEN |
| 159 |
MakeCorridorDoors(i, j) |
| 160 |
END ; |
| 161 |
INC(j) |
| 162 |
END ; |
| 163 |
INC(i) |
| 164 |
END |
| 165 |
END CreateCorridorDoors ; |
| 166 |
|
| 167 |
|
| 168 |
(* |
| 169 |
CreateMinRoomDoors - create minimum doors arround map. Thus allowing |
| 170 |
an entrance into every room. |
| 171 |
*) |
| 172 |
|
| 173 |
PROCEDURE CreateMinRoomDoors ; |
| 174 |
VAR |
| 175 |
Done: BOOLEAN ; |
| 176 |
i : CARDINAL ; |
| 177 |
BEGIN |
| 178 |
REPEAT |
| 179 |
Done := FALSE ; |
| 180 |
i := 1 ; |
| 181 |
WHILE (NOT Done) AND (i<=GenNoOfRooms) DO |
| 182 |
IF RoomExists(i) |
| 183 |
THEN |
| 184 |
IF Rooms[i].NoOfDoors=0 |
| 185 |
THEN |
| 186 |
IF MakeDoorInRoomToSafty(i) |
| 187 |
THEN |
| 188 |
END ; |
| 189 |
Done := (Rooms[i].NoOfDoors#0) |
| 190 |
END |
| 191 |
END ; |
| 192 |
INC(i) |
| 193 |
END |
| 194 |
UNTIL NOT Done ; |
| 195 |
(* Now remove all rooms that have no doors *) |
| 196 |
IF isStatistics () |
| 197 |
THEN |
| 198 |
WriteString('Number of rooms') ; WriteCard(GenNoOfRooms, 4) ; WriteLn ; |
| 199 |
END ; |
| 200 |
i := 1 ; |
| 201 |
WHILE i<=GenNoOfRooms DO |
| 202 |
IF RoomExists(i) AND (Rooms[i].NoOfDoors=0) |
| 203 |
THEN |
| 204 |
RemoveRoom(i) ; |
| 205 |
IF isStatistics () |
| 206 |
THEN |
| 207 |
WriteString('Removing room') ; WriteCard(i, 4) ; WriteLn |
| 208 |
END |
| 209 |
END ; |
| 210 |
INC(i) |
| 211 |
END |
| 212 |
END CreateMinRoomDoors ; |
| 213 |
|
| 214 |
|
| 215 |
(* |
| 216 |
CreateRoomDoors - places room doors in rooms. |
| 217 |
*) |
| 218 |
|
| 219 |
PROCEDURE CreateRoomDoors ; |
| 220 |
VAR |
| 221 |
i, n: CARDINAL ; |
| 222 |
BEGIN |
| 223 |
i := NoOfCorridors+1 ; |
| 224 |
WHILE i<=GenNoOfRooms DO |
| 225 |
IF RoomExists(i) |
| 226 |
THEN |
| 227 |
n := GetRand(MaxDoorsPerRoom-1 DIV 2)+1 ; |
| 228 |
WHILE (Rooms[i].NoOfDoors<MaxDoorsPerRoom) AND (n>0) DO |
| 229 |
IF MakeDoorInRoom(i) |
| 230 |
THEN |
| 231 |
END ; |
| 232 |
DEC(n) |
| 233 |
END |
| 234 |
END ; |
| 235 |
INC(i) |
| 236 |
END |
| 237 |
END CreateRoomDoors ; |
| 238 |
|
| 239 |
|
| 240 |
(* |
| 241 |
MakeCorridorDoors - checks for corridor doors thoughout the corridor rooms. |
| 242 |
*) |
| 243 |
|
| 244 |
PROCEDURE MakeCorridorDoors (r1, r2: CARDINAL) ; |
| 245 |
VAR |
| 246 |
i, j: CARDINAL ; |
| 247 |
BEGIN |
| 248 |
i := 1 ; |
| 249 |
WHILE i<=Rooms[r1].NoOfWalls DO |
| 250 |
j := 1 ; |
| 251 |
WHILE j<=Rooms[r2].NoOfWalls DO |
| 252 |
IF IsIntersection( Rooms[r1].Walls[i].X1, Rooms[r1].Walls[i].Y1, |
| 253 |
Rooms[r1].Walls[i].X2, Rooms[r1].Walls[i].Y2, |
| 254 |
Rooms[r2].Walls[j].X1, Rooms[r2].Walls[j].Y1, |
| 255 |
Rooms[r2].Walls[j].X2, Rooms[r2].Walls[j].Y2 ) |
| 256 |
THEN |
| 257 |
CheckForCorridorDoor(r1, i, r2, j) |
| 258 |
END ; |
| 259 |
INC(j) |
| 260 |
END ; |
| 261 |
INC(i) |
| 262 |
END |
| 263 |
END MakeCorridorDoors ; |
| 264 |
|
| 265 |
|
| 266 |
(* |
| 267 |
MakeDoorInRoomToSafty - true is returned if a door is made in room r |
| 268 |
to a room which already has a door. |
| 269 |
Thus room, r, is reachable by the whole map. |
| 270 |
*) |
| 271 |
|
| 272 |
PROCEDURE MakeDoorInRoomToSafty (r: CARDINAL) : BOOLEAN ; |
| 273 |
VAR |
| 274 |
RoomList, |
| 275 |
Neighbour: CARDINAL ; |
| 276 |
Done : BOOLEAN ; |
| 277 |
BEGIN |
| 278 |
Done := FALSE ; |
| 279 |
RoomList := InitRandom() ; |
| 280 |
AddRandom(RoomList, GenNoOfRooms) ; (* 1..GenNoOfRooms *) |
| 281 |
Neighbour := GetAndDeleteRandom(RoomList) ; |
| 282 |
WHILE (Neighbour#0) AND (NOT Done) DO |
| 283 |
IF RoomExists(Neighbour) AND (r#Neighbour) AND |
| 284 |
IsTouching(r, Neighbour) AND (Rooms[Neighbour].NoOfDoors#0) |
| 285 |
THEN |
| 286 |
Done := MakeDoorBetweenRooms(r, Neighbour) |
| 287 |
END ; |
| 288 |
Neighbour := GetAndDeleteRandom(RoomList) |
| 289 |
END ; |
| 290 |
KillRandom(RoomList) ; |
| 291 |
RETURN( Done ) |
| 292 |
END MakeDoorInRoomToSafty ; |
| 293 |
|
| 294 |
|
| 295 |
(* |
| 296 |
MakeDoorInRoom - true is returned if a door is made in room r. |
| 297 |
*) |
| 298 |
|
| 299 |
PROCEDURE MakeDoorInRoom (r: CARDINAL) : BOOLEAN ; |
| 300 |
VAR |
| 301 |
RoomList, |
| 302 |
Neighbour: CARDINAL ; |
| 303 |
Done : BOOLEAN ; |
| 304 |
BEGIN |
| 305 |
Done := FALSE ; |
| 306 |
RoomList := InitRandom() ; |
| 307 |
AddRandom(RoomList, GenNoOfRooms) ; (* 1..GenNoOfRooms *) |
| 308 |
Neighbour := GetAndDeleteRandom(RoomList) ; |
| 309 |
WHILE (Neighbour#0) AND (NOT Done) DO |
| 310 |
IF RoomExists(Neighbour) AND (r#Neighbour) AND IsTouching(r, Neighbour) |
| 311 |
THEN |
| 312 |
Done := MakeDoorBetweenRooms(r, Neighbour) |
| 313 |
END ; |
| 314 |
Neighbour := GetAndDeleteRandom(RoomList) |
| 315 |
END ; |
| 316 |
KillRandom(RoomList) ; |
| 317 |
RETURN( Done ) |
| 318 |
END MakeDoorInRoom ; |
| 319 |
|
| 320 |
|
| 321 |
(* |
| 322 |
MakeDoorBetweenRooms - returns true if it makes a door between |
| 323 |
rooms r1 and r2. |
| 324 |
*) |
| 325 |
|
| 326 |
PROCEDURE MakeDoorBetweenRooms (r1, r2: CARDINAL) : BOOLEAN ; |
| 327 |
VAR |
| 328 |
CoordList: CARDINAL ; |
| 329 |
Success : BOOLEAN ; |
| 330 |
BEGIN |
| 331 |
Success := FALSE ; |
| 332 |
CoordList := InitCoords() ; |
| 333 |
PushPossibleDoorCoords(CoordList, r1, r2) ; |
| 334 |
Success := ChooseDoor(CoordList, r1, r2) ; |
| 335 |
KillCoords(CoordList) ; |
| 336 |
RETURN( Success ) |
| 337 |
END MakeDoorBetweenRooms ; |
| 338 |
|
| 339 |
|
| 340 |
(* |
| 341 |
PushPossibleDoorCoords - pushes the possible door coordinates on the |
| 342 |
coordinate stack CoordList. |
| 343 |
The door links rooms r1 and r2 together. |
| 344 |
*) |
| 345 |
|
| 346 |
PROCEDURE PushPossibleDoorCoords (CoordList: CARDINAL; r1, r2: CARDINAL) ; |
| 347 |
VAR |
| 348 |
i, j : CARDINAL ; |
| 349 |
BEGIN |
| 350 |
i := 1 ; |
| 351 |
WHILE i<=Rooms[r1].NoOfWalls DO |
| 352 |
j := 1 ; |
| 353 |
WHILE j<=Rooms[r2].NoOfWalls DO |
| 354 |
IF IsIntersection( Rooms[r1].Walls[i].X1, Rooms[r1].Walls[i].Y1, |
| 355 |
Rooms[r1].Walls[i].X2, Rooms[r1].Walls[i].Y2, |
| 356 |
Rooms[r2].Walls[j].X1, Rooms[r2].Walls[j].Y1, |
| 357 |
Rooms[r2].Walls[j].X2, Rooms[r2].Walls[j].Y2 ) |
| 358 |
THEN |
| 359 |
IF PossibleDoorLength(r1, i, r2, j)>=MinDoorLength |
| 360 |
THEN |
| 361 |
PushPossibleDoorCoordsOnWall(CoordList, r1, i, r2, j) |
| 362 |
END |
| 363 |
END ; |
| 364 |
INC(j) |
| 365 |
END ; |
| 366 |
INC(i) |
| 367 |
END |
| 368 |
END PushPossibleDoorCoords ; |
| 369 |
|
| 370 |
|
| 371 |
(* |
| 372 |
PushPossibleDoorCoordsOnWall - pushes the coordinates which can take a door |
| 373 |
between rooms r1 and r2 on walls w1 and w2 |
| 374 |
onto the coordinate stack CoordList. |
| 375 |
*) |
| 376 |
|
| 377 |
PROCEDURE PushPossibleDoorCoordsOnWall (CoordList: CARDINAL ; |
| 378 |
r1, w1, r2, w2: CARDINAL) ; |
| 379 |
VAR |
| 380 |
s, e, |
| 381 |
x1, y1, x2, y2, |
| 382 |
x3, y3, x4, y4: CARDINAL ; |
| 383 |
BEGIN |
| 384 |
IF isVerbose () |
| 385 |
THEN |
| 386 |
WriteString('Pushing walls') ; WriteCard(w1, 4) ; WriteCard(w2, 4) ; WriteLn |
| 387 |
END ; |
| 388 |
WITH Rooms[r1].Walls[w1] DO |
| 389 |
x1 := X1 ; |
| 390 |
y1 := Y1 ; |
| 391 |
x2 := X2 ; |
| 392 |
y2 := Y2 |
| 393 |
END ; |
| 394 |
WITH Rooms[r2].Walls[w2] DO |
| 395 |
x3 := X1 ; |
| 396 |
y3 := Y1 ; |
| 397 |
x4 := X2 ; |
| 398 |
y4 := Y2 |
| 399 |
END ; |
| 400 |
IF IsVertical(x1, y1, x2, y2) AND IsVertical(x3, y3, x4, y4) |
| 401 |
THEN |
| 402 |
Assert(x1=x3) ; Assert(x2=x4) ; |
| 403 |
IF IsSubRange(y1, y2, y3) |
| 404 |
THEN |
| 405 |
s := y3 |
| 406 |
ELSE |
| 407 |
Assert(IsSubRange(y3, y4, y1)) ; |
| 408 |
s := y1 |
| 409 |
END ; |
| 410 |
e := Min(y2, y4) ; |
| 411 |
INC(s) ; |
| 412 |
DEC(e) ; |
| 413 |
WHILE s<=e DO |
| 414 |
IF IsDoorAllowed(r1, r2, x1, s, x1, e) AND |
| 415 |
IsDoorAllowed(r2, r1, x1, s, x1, e) |
| 416 |
THEN |
| 417 |
AddCoord(CoordList, x1, s) ; |
| 418 |
IF isDebugging () |
| 419 |
THEN |
| 420 |
WriteString('Point') ; WriteCard(x1, 4) ; WriteCard(s, 4) ; WriteLn |
| 421 |
END |
| 422 |
END ; |
| 423 |
INC(s) |
| 424 |
END |
| 425 |
ELSE |
| 426 |
Assert(IsHorizontal(x1, y1, x2, y2) AND IsHorizontal(x3, y3, x4, y4)) ; |
| 427 |
Assert(y1=y3) ; Assert(y2=y4) ; |
| 428 |
IF IsSubRange(x1, x2, x3) |
| 429 |
THEN |
| 430 |
s := x3 |
| 431 |
ELSE |
| 432 |
Assert(IsSubRange(x3, x4, x1)) ; |
| 433 |
s := x1 |
| 434 |
END ; |
| 435 |
e := Min(x2, x4) ; |
| 436 |
INC(s) ; |
| 437 |
DEC(e) ; |
| 438 |
WHILE s<=e DO |
| 439 |
IF IsDoorAllowed(r1, r2, s, y1, e, y1) AND |
| 440 |
IsDoorAllowed(r2, r1, s, y1, e, y1) |
| 441 |
THEN |
| 442 |
AddCoord(CoordList, s, y1) ; |
| 443 |
IF isDebugging () |
| 444 |
THEN |
| 445 |
WriteString('Point') ; WriteCard(s, 4) ; WriteCard(y1, 4) ; WriteLn |
| 446 |
END |
| 447 |
END ; |
| 448 |
INC(s) |
| 449 |
END |
| 450 |
END |
| 451 |
END PushPossibleDoorCoordsOnWall ; |
| 452 |
|
| 453 |
|
| 454 |
(* |
| 455 |
ChooseDoor - chooses a door from the CoordList which connects rooms |
| 456 |
r1 and r2. |
| 457 |
*) |
| 458 |
|
| 459 |
PROCEDURE ChooseDoor (CoordList: CARDINAL; r1, r2: CARDINAL) : BOOLEAN ; |
| 460 |
VAR |
| 461 |
x, y : CARDINAL ; |
| 462 |
w1, w2: CARDINAL ; |
| 463 |
ok : BOOLEAN ; |
| 464 |
BEGIN |
| 465 |
GetAndDeleteRandomCoord(CoordList, x, y) ; |
| 466 |
ok := (x#0) AND (y#0) ; |
| 467 |
IF ok |
| 468 |
THEN |
| 469 |
w1 := FindWall(r1, x, y) ; |
| 470 |
w2 := FindWall(r2, x, y) ; |
| 471 |
MakeRoomDoor(r1, w1, r2, w2, x, y) |
| 472 |
END ; |
| 473 |
RETURN( ok ) |
| 474 |
END ChooseDoor ; |
| 475 |
|
| 476 |
|
| 477 |
(* |
| 478 |
FindWall - returns the wall number of a room r1 which has the point x, y |
| 479 |
on it. A corner point will return a wall of zero. |
| 480 |
*) |
| 481 |
|
| 482 |
PROCEDURE FindWall (r: CARDINAL; x, y: CARDINAL) : CARDINAL ; |
| 483 |
VAR |
| 484 |
Found: BOOLEAN ; |
| 485 |
i : CARDINAL ; |
| 486 |
BEGIN |
| 487 |
i := 1 ; |
| 488 |
Found := FALSE ; |
| 489 |
WITH Rooms[r] DO |
| 490 |
WHILE (i<=NoOfWalls) AND (NOT Found) DO |
| 491 |
WITH Walls[i] DO |
| 492 |
IF ((x=X1) AND (y=Y1)) OR ((x=X2) AND (y=Y2)) |
| 493 |
THEN |
| 494 |
(* Corner has been found *) |
| 495 |
Found := TRUE ; |
| 496 |
i := 0 |
| 497 |
ELSIF IsPointOnLine(x, y, X1, Y1, X2, Y2) |
| 498 |
THEN |
| 499 |
Found := TRUE |
| 500 |
ELSE |
| 501 |
INC(i) |
| 502 |
END |
| 503 |
END |
| 504 |
END |
| 505 |
END ; |
| 506 |
IF Found |
| 507 |
THEN |
| 508 |
RETURN( i ) |
| 509 |
ELSE |
| 510 |
RETURN( 0 ) |
| 511 |
END |
| 512 |
END FindWall ; |
| 513 |
|
| 514 |
|
| 515 |
(* |
| 516 |
MakeRoomDoor - makes a door between rooms r1 and r2 on walls w1 and w2 |
| 517 |
at position x, y. |
| 518 |
*) |
| 519 |
|
| 520 |
PROCEDURE MakeRoomDoor (r1: CARDINAL; w1: CARDINAL; r2: CARDINAL; w2: CARDINAL; |
| 521 |
x, y: CARDINAL) ; |
| 522 |
VAR |
| 523 |
l, h, List, |
| 524 |
x1, y1, x2, y2, |
| 525 |
x3, y3, x4, y4: CARDINAL ; |
| 526 |
Done : BOOLEAN ; |
| 527 |
BEGIN |
| 528 |
IF isVerbose () |
| 529 |
THEN |
| 530 |
WriteString('Making walls') ; WriteCard(w1, 4) ; WriteCard(w2, 4) ; WriteLn |
| 531 |
END ; |
| 532 |
WITH Rooms[r1].Walls[w1] DO |
| 533 |
x1 := X1 ; |
| 534 |
y1 := Y1 ; |
| 535 |
x2 := X2 ; |
| 536 |
y2 := Y2 |
| 537 |
END ; |
| 538 |
WITH Rooms[r2].Walls[w2] DO |
| 539 |
x3 := X1 ; |
| 540 |
y3 := Y1 ; |
| 541 |
x4 := X2 ; |
| 542 |
y4 := Y2 |
| 543 |
END ; |
| 544 |
IF IsVertical(x1, y1, x2, y2) AND IsVertical(x3, y3, x4, y4) |
| 545 |
THEN |
| 546 |
Assert(x1=x3) ; Assert(x2=x4) ; |
| 547 |
Done := FALSE ; |
| 548 |
List := InitRandom() ; |
| 549 |
AddRandom(List, MaxDoorLength) ; |
| 550 |
REPEAT |
| 551 |
l := GetAndDeleteRandom(List) ; |
| 552 |
IF l>=MinDoorLength |
| 553 |
THEN |
| 554 |
h := Min(y+l-1, Min(y2, y4)-1) ; |
| 555 |
Done := IsDoorAllowed(r1, r2, x, y, x, h) AND |
| 556 |
IsDoorAllowed(r2, r1, x, y, x, h) |
| 557 |
END |
| 558 |
UNTIL (l=0) OR Done ; |
| 559 |
KillRandom(List) ; |
| 560 |
Assert(l#0) ; |
| 561 |
FixRoomDoor(r2, r1, x, y, x, h) |
| 562 |
ELSIF IsHorizontal(x1, y1, x2, y2) AND IsHorizontal(x3, y3, x4, y4) |
| 563 |
THEN |
| 564 |
Assert(y1=y3) ; Assert(y2=y4) ; |
| 565 |
Done := FALSE ; |
| 566 |
List := InitRandom() ; |
| 567 |
AddRandom(List, MaxDoorLength) ; |
| 568 |
REPEAT |
| 569 |
l := GetAndDeleteRandom(List) ; |
| 570 |
IF l>=MinDoorLength |
| 571 |
THEN |
| 572 |
h := Min(x+l-1, Min(x2, x4)-1) ; |
| 573 |
Done := IsDoorAllowed(r1, r2, x, y, h, y) AND |
| 574 |
IsDoorAllowed(r2, r1, x, y, h, y) |
| 575 |
END |
| 576 |
UNTIL (l=0) OR Done ; |
| 577 |
KillRandom(List) ; |
| 578 |
Assert(l#0) ; |
| 579 |
FixRoomDoor(r2, r1, x, y, h, y) |
| 580 |
ELSE |
| 581 |
HALT |
| 582 |
END |
| 583 |
END MakeRoomDoor ; |
| 584 |
|
| 585 |
|
| 586 |
(* |
| 587 |
IsDoorAllowed - checks whether a door can be built in r1 leading to r2 |
| 588 |
the coordinates of the door are x1, y1, x2, y2. |
| 589 |
*) |
| 590 |
|
| 591 |
PROCEDURE IsDoorAllowed (r1, r2: CARDINAL; x1, y1, x2, y2: CARDINAL) : BOOLEAN ; |
| 592 |
VAR |
| 593 |
ok : BOOLEAN ; |
| 594 |
i, w: CARDINAL ; |
| 595 |
BEGIN |
| 596 |
ok := (NOT DoorsClash(r1, x1, y1, x2, y2)) AND |
| 597 |
(Rooms[r1].NoOfDoors<MaxDoorsPerRoom) AND |
| 598 |
(Max(Abs(x1, x2)+1, Abs(y1, y2)+1)>=MinDoorLength) ; |
| 599 |
IF ok |
| 600 |
THEN |
| 601 |
w := FindWall(r1, x1, y1) ; |
| 602 |
IF IsVertical(x1, y1, x2, y2) |
| 603 |
THEN |
| 604 |
i := y1 ; |
| 605 |
WHILE (i<=y2) AND ok DO |
| 606 |
ok := (w=FindWall(r1, x1, i)) ; |
| 607 |
INC(i) |
| 608 |
END |
| 609 |
ELSIF IsHorizontal(x1, y1, x2, y2) |
| 610 |
THEN |
| 611 |
i := x1 ; |
| 612 |
WHILE (i<=x2) AND ok DO |
| 613 |
ok := (w=FindWall(r1, i, y1)) ; |
| 614 |
INC(i) |
| 615 |
END |
| 616 |
END |
| 617 |
END ; |
| 618 |
RETURN( ok ) |
| 619 |
END IsDoorAllowed ; |
| 620 |
|
| 621 |
|
| 622 |
(* |
| 623 |
FixRoomDoor - places a door between rooms r1 and r2 with coordinates |
| 624 |
x1, y1, x2, y2. |
| 625 |
*) |
| 626 |
|
| 627 |
PROCEDURE FixRoomDoor (r1, r2: CARDINAL; x1, y1, x2, y2: CARDINAL) ; |
| 628 |
BEGIN |
| 629 |
IF IsConnectionSecret(r1, r2) |
| 630 |
THEN |
| 631 |
AddDoor(r1, r2, x1, y1, x2, y2, Secret) ; |
| 632 |
AddDoor(r2, r1, x1, y1, x2, y2, Secret) |
| 633 |
ELSIF (NOT Adjacent(r1, r2)) AND (GetRand(100)>49) |
| 634 |
THEN |
| 635 |
AddDoor(r1, r2, x1, y1, x2, y2, Secret) ; |
| 636 |
AddDoor(r2, r1, x1, y1, x2, y2, Secret) |
| 637 |
ELSE |
| 638 |
AddDoor(r1, r2, x1, y1, x2, y2, Closed) ; |
| 639 |
AddDoor(r2, r1, x1, y1, x2, y2, Closed) |
| 640 |
END |
| 641 |
END FixRoomDoor ; |
| 642 |
|
| 643 |
|
| 644 |
(* |
| 645 |
IsConnectionSecret - returns true if the rooms, r1 and r2, are |
| 646 |
connected via a secret door. |
| 647 |
*) |
| 648 |
|
| 649 |
PROCEDURE IsConnectionSecret (r1, r2: CARDINAL) : BOOLEAN ; |
| 650 |
VAR |
| 651 |
i: CARDINAL ; |
| 652 |
BEGIN |
| 653 |
i := 1 ; |
| 654 |
WITH Rooms[r1] DO |
| 655 |
WHILE i<=NoOfDoors DO |
| 656 |
WITH Doors[i] DO |
| 657 |
IF (LeadsTo=r1) AND (StateOfDoor=Secret) |
| 658 |
THEN |
| 659 |
RETURN( TRUE ) |
| 660 |
ELSE |
| 661 |
INC(i) |
| 662 |
END |
| 663 |
END |
| 664 |
END |
| 665 |
END ; |
| 666 |
RETURN( FALSE ) |
| 667 |
END IsConnectionSecret ; |
| 668 |
|
| 669 |
|
| 670 |
(* |
| 671 |
CheckForCorridorDoor - checks whether a door can be built on walls |
| 672 |
w1 and w2 of rooms r1 and r2. |
| 673 |
*) |
| 674 |
|
| 675 |
PROCEDURE CheckForCorridorDoor (r1: CARDINAL; w1: CARDINAL; |
| 676 |
r2: CARDINAL; w2: CARDINAL) ; |
| 677 |
VAR |
| 678 |
l: CARDINAL ; |
| 679 |
BEGIN |
| 680 |
l := PossibleDoorLength(r1, w1, r2, w2) ; |
| 681 |
(* WriteString('Intersection length') ; WriteCard(l, 4) ; WriteLn ; *) |
| 682 |
IF l>=CorridorDoorLength |
| 683 |
THEN |
| 684 |
BuildCorridorDoor(r1, w1, r2, w2) |
| 685 |
END |
| 686 |
END CheckForCorridorDoor ; |
| 687 |
|
| 688 |
|
| 689 |
(* |
| 690 |
BuildCorridorDoor - will build a door on walls w1 and w2. |
| 691 |
BuildCorridorDoor works out the coordinates |
| 692 |
for the corridor doors. |
| 693 |
*) |
| 694 |
|
| 695 |
PROCEDURE BuildCorridorDoor (r1: CARDINAL; w1: CARDINAL; |
| 696 |
r2: CARDINAL; w2: CARDINAL) ; |
| 697 |
VAR |
| 698 |
x1, y1, x2, y2, |
| 699 |
x3, y3, x4, y4: CARDINAL ; |
| 700 |
BEGIN |
| 701 |
WITH Rooms[r1].Walls[w1] DO |
| 702 |
x1 := X1 ; |
| 703 |
y1 := Y1 ; |
| 704 |
x2 := X2 ; |
| 705 |
y2 := Y2 |
| 706 |
END ; |
| 707 |
WITH Rooms[r2].Walls[w2] DO |
| 708 |
x3 := X1 ; |
| 709 |
y3 := Y1 ; |
| 710 |
x4 := X2 ; |
| 711 |
y4 := Y2 |
| 712 |
END ; |
| 713 |
IF IsVertical(x1, y1, x2, y2) AND IsVertical(x3, y3, x4, y4) |
| 714 |
THEN |
| 715 |
Assert(x1=x3) ; Assert(x2=x4) ; |
| 716 |
IF Abs(y1, y2)<Abs(y3, y4) |
| 717 |
THEN |
| 718 |
AttemptBuildCorridorDoor(r1, r2, x1, y1+1, x2, y2-1) |
| 719 |
ELSE |
| 720 |
AttemptBuildCorridorDoor(r1, r2, x1, y3+1, x2, y4-1) |
| 721 |
END |
| 722 |
ELSIF IsHorizontal(x1, y1, x2, y2) AND IsHorizontal(x3, y3, x4, y4) |
| 723 |
THEN |
| 724 |
Assert(y1=y3) ; Assert(y2=y4) ; |
| 725 |
IF Abs(x1, x2)<Abs(x3, x4) |
| 726 |
THEN |
| 727 |
AttemptBuildCorridorDoor(r1, r2, x1+1, y1, x2-1, y2) |
| 728 |
ELSE |
| 729 |
AttemptBuildCorridorDoor(r1, r2, x3+1, y1, x4-1, y4) |
| 730 |
END |
| 731 |
END |
| 732 |
END BuildCorridorDoor ; |
| 733 |
|
| 734 |
|
| 735 |
(* |
| 736 |
AttemptBuildCorridorDoor - attempts to make a corridor door |
| 737 |
between rooms r1 and r2 with |
| 738 |
coordinates x1, y1, x2, y2. |
| 739 |
*) |
| 740 |
|
| 741 |
PROCEDURE AttemptBuildCorridorDoor (r1, r2: CARDINAL ; |
| 742 |
x1, y1, x2, y2: CARDINAL) ; |
| 743 |
BEGIN |
| 744 |
IF IsDoorAllowed(r1, r2, x1, y1, x2, y2) AND |
| 745 |
IsDoorAllowed(r2, r1, x1, y1, x2, y2) |
| 746 |
THEN |
| 747 |
IF IsConnectionSecret(r1, r2) |
| 748 |
THEN |
| 749 |
AddDoor(r1, r2, x1, y1, x2, y2, Secret) ; |
| 750 |
AddDoor(r2, r1, x1, y1, x2, y2, Secret) |
| 751 |
ELSIF (NOT Adjacent(r1, r2)) AND (GetRand(100)>65) |
| 752 |
THEN |
| 753 |
AddDoor(r1, r2, x1, y1, x2, y2, Secret) ; |
| 754 |
AddDoor(r2, r1, x1, y1, x2, y2, Secret) |
| 755 |
ELSE |
| 756 |
AddDoor(r1, r2, x1, y1, x2, y2, Open) ; |
| 757 |
AddDoor(r2, r1, x1, y1, x2, y2, Open) |
| 758 |
END |
| 759 |
ELSE |
| 760 |
IF isVerbose () |
| 761 |
THEN |
| 762 |
WriteString('Not allowing corridor door!') ; WriteLn |
| 763 |
END |
| 764 |
END |
| 765 |
END AttemptBuildCorridorDoor ; |
| 766 |
|
| 767 |
|
| 768 |
(* |
| 769 |
DoorsClash - returns true if there does exist a door which clashes with |
| 770 |
x1, y1, x2, y2 in room r1. |
| 771 |
*) |
| 772 |
|
| 773 |
PROCEDURE DoorsClash (r: CARDINAL; x1, y1, x2, y2: CARDINAL) : BOOLEAN ; |
| 774 |
VAR |
| 775 |
Clash: BOOLEAN ; |
| 776 |
i : CARDINAL ; |
| 777 |
BEGIN |
| 778 |
IF IsVertical(x1, y1, x2, y2) |
| 779 |
THEN |
| 780 |
INC(y2) ; |
| 781 |
IF y1>1 |
| 782 |
THEN |
| 783 |
DEC(y1) |
| 784 |
END |
| 785 |
ELSE |
| 786 |
INC(x2) ; |
| 787 |
IF x1>1 |
| 788 |
THEN |
| 789 |
DEC(x1) |
| 790 |
END |
| 791 |
END ; |
| 792 |
Clash := FALSE ; |
| 793 |
WITH Rooms[r] DO |
| 794 |
i := 1 ; |
| 795 |
WHILE (i<=NoOfDoors) AND (NOT Clash) DO |
| 796 |
WITH Doors[i].Position DO |
| 797 |
IF IsIntersection(x1, y1, x2, y2, X1, Y1, X2, Y2) |
| 798 |
THEN |
| 799 |
Clash := TRUE |
| 800 |
END |
| 801 |
END ; |
| 802 |
INC(i) |
| 803 |
END |
| 804 |
END ; |
| 805 |
RETURN( Clash ) |
| 806 |
END DoorsClash ; |
| 807 |
|
| 808 |
|
| 809 |
(* |
| 810 |
AddDoor - adds a door in room r1 that leads to a door in r2. |
| 811 |
The coordinates of the door are x1, y1, x2, y2 and the |
| 812 |
door type is in Status. |
| 813 |
*) |
| 814 |
|
| 815 |
PROCEDURE AddDoor (r1, r2: CARDINAL; |
| 816 |
x1, y1, x2, y2: CARDINAL; Status: DoorStatus) ; |
| 817 |
BEGIN |
| 818 |
; Assert(IsTouching(r1, r2)) |
| 819 |
; Assert(IsDoorAllowed(r1, r2, x1, y1, x2, y2)) ; |
| 820 |
; Assert(r1#r2) ; |
| 821 |
; Assert(RoomExists(r1) AND RoomExists(r2)) ; |
| 822 |
WITH Rooms[r1] DO |
| 823 |
Assert(NoOfDoors<MaxDoorsPerRoom) ; |
| 824 |
INC(NoOfDoors) ; |
| 825 |
WITH Doors[NoOfDoors] DO |
| 826 |
Position.X1 := x1 ; |
| 827 |
Position.Y1 := y1 ; |
| 828 |
Position.X2 := x2 ; |
| 829 |
Position.Y2 := y2 ; |
| 830 |
StateOfDoor := Status ; |
| 831 |
LeadsTo := r2 |
| 832 |
END |
| 833 |
END |
| 834 |
END AddDoor ; |
| 835 |
|
| 836 |
|
| 837 |
(* |
| 838 |
PossibleDoorLength - returns the possible door length between rooms |
| 839 |
r1 and r2 on wall w1 and w2. |
| 840 |
*) |
| 841 |
|
| 842 |
PROCEDURE PossibleDoorLength (r1: CARDINAL; w1: CARDINAL; |
| 843 |
r2: CARDINAL; w2: CARDINAL) : CARDINAL ; |
| 844 |
VAR |
| 845 |
l, |
| 846 |
x1, x2, x3, x4, |
| 847 |
y1, y2, y3, y4: CARDINAL ; |
| 848 |
BEGIN |
| 849 |
WITH Rooms[r1].Walls[w1] DO |
| 850 |
x1 := X1 ; |
| 851 |
y1 := Y1 ; |
| 852 |
x2 := X2 ; |
| 853 |
y2 := Y2 |
| 854 |
END ; |
| 855 |
WITH Rooms[r2].Walls[w2] DO |
| 856 |
x3 := X1 ; |
| 857 |
y3 := Y1 ; |
| 858 |
x4 := X2 ; |
| 859 |
y4 := Y2 |
| 860 |
END ; |
| 861 |
IF IsVertical(x1, y1, x2, y2) AND IsVertical(x3, y3, x4, y4) |
| 862 |
THEN |
| 863 |
l := IntersectionLength(y1, y2, y3, y4)+1 ; |
| 864 |
IF (IsSubRange(y1, y2, y3) OR IsSubRange(y3, y4, y1)) AND (l>0) |
| 865 |
THEN |
| 866 |
DEC(l) |
| 867 |
END ; |
| 868 |
IF (IsSubRange(y1, y2, y4) OR IsSubRange(y3, y4, y2)) AND (l>0) |
| 869 |
THEN |
| 870 |
DEC(l) |
| 871 |
END |
| 872 |
ELSIF IsHorizontal(x1, y1, x2, y2) AND IsHorizontal(x3, y3, x4, y4) |
| 873 |
THEN |
| 874 |
l := IntersectionLength(x1, x2, x3, x4)+1 ; |
| 875 |
IF (IsSubRange(x1, x2, x3) OR IsSubRange(x3, x4, x1)) AND (l>0) |
| 876 |
THEN |
| 877 |
DEC(l) |
| 878 |
END ; |
| 879 |
IF (IsSubRange(x1, x2, x4) OR IsSubRange(x3, x4, x2)) AND (l>0) |
| 880 |
THEN |
| 881 |
DEC(l) |
| 882 |
END |
| 883 |
ELSE |
| 884 |
l := 0 |
| 885 |
END ; |
| 886 |
RETURN( l ) |
| 887 |
END PossibleDoorLength ; |
| 888 |
|
| 889 |
|
| 890 |
(* |
| 891 |
AmalgamateCorridors - joins corridors together. |
| 892 |
*) |
| 893 |
|
| 894 |
PROCEDURE AmalgamateCorridors ; |
| 895 |
VAR |
| 896 |
i, j: CARDINAL ; |
| 897 |
Done: BOOLEAN ; |
| 898 |
BEGIN |
| 899 |
REPEAT |
| 900 |
Done := FALSE ; |
| 901 |
i := 1 ; |
| 902 |
WHILE (NOT Done) AND (i<=NoOfCorridors) DO |
| 903 |
j := 1 ; |
| 904 |
WHILE (NOT Done) AND (j<=NoOfCorridors) DO |
| 905 |
IF (i#j) AND RoomExists(i) AND RoomExists(j) |
| 906 |
THEN |
| 907 |
IF Amalgamate(i, j) |
| 908 |
THEN |
| 909 |
Done := TRUE |
| 910 |
END |
| 911 |
END ; |
| 912 |
INC(j) |
| 913 |
END ; |
| 914 |
INC(i) |
| 915 |
END |
| 916 |
UNTIL NOT Done |
| 917 |
END AmalgamateCorridors ; |
| 918 |
|
| 919 |
|
| 920 |
(* |
| 921 |
AmalgamateRooms - joins rooms together. |
| 922 |
*) |
| 923 |
|
| 924 |
PROCEDURE AmalgamateRooms ; |
| 925 |
VAR |
| 926 |
i, j: CARDINAL ; |
| 927 |
Done: BOOLEAN ; |
| 928 |
BEGIN |
| 929 |
REPEAT |
| 930 |
Done := FALSE ; |
| 931 |
i := NoOfCorridors+1 ; |
| 932 |
WHILE (NOT Done) AND (i<=GenNoOfRooms) DO |
| 933 |
j := NoOfCorridors+1 ; |
| 934 |
WHILE (NOT Done) AND (j<=GenNoOfRooms) DO |
| 935 |
IF (i#j) AND RoomExists(i) AND RoomExists(j) |
| 936 |
THEN |
| 937 |
IF Amalgamate(i, j) |
| 938 |
THEN |
| 939 |
Done := TRUE |
| 940 |
END |
| 941 |
END ; |
| 942 |
INC(j) |
| 943 |
END ; |
| 944 |
INC(i) |
| 945 |
END |
| 946 |
UNTIL NOT Done |
| 947 |
END AmalgamateRooms ; |
| 948 |
|
| 949 |
|
| 950 |
(* |
| 951 |
Amalgamate - returns true if it can join two rooms r1 and r2 together. |
| 952 |
*) |
| 953 |
|
| 954 |
PROCEDURE Amalgamate (r1, r2: CARDINAL) : BOOLEAN ; |
| 955 |
BEGIN |
| 956 |
IF IsTouching(r1, r2) |
| 957 |
THEN |
| 958 |
NoOfLines := 0 ; |
| 959 |
CopyWallsToLines(r1) ; |
| 960 |
CopyWallsToLines(r2) ; |
| 961 |
RemoveRoom(r1) ; |
| 962 |
RemoveRoom(r2) ; |
| 963 |
IF CompactLines() AND IsLineRoomSatisfied() |
| 964 |
THEN |
| 965 |
CopyLinesToWalls(r1) ; |
| 966 |
InsertRoom(r1) ; |
| 967 |
RETURN( TRUE ) |
| 968 |
ELSE |
| 969 |
InsertRoom(r1) ; |
| 970 |
InsertRoom(r2) ; |
| 971 |
RETURN( FALSE ) |
| 972 |
END |
| 973 |
ELSE |
| 974 |
RETURN( FALSE ) |
| 975 |
END |
| 976 |
END Amalgamate ; |
| 977 |
|
| 978 |
|
| 979 |
(* |
| 980 |
CompactLines - returns true if the lines in the line buffer are |
| 981 |
reduced by forming a bigger room. |
| 982 |
*) |
| 983 |
|
| 984 |
PROCEDURE CompactLines () : BOOLEAN ; |
| 985 |
VAR |
| 986 |
Done, |
| 987 |
Compacted: BOOLEAN ; |
| 988 |
i, j : CARDINAL ; |
| 989 |
BEGIN |
| 990 |
Compacted := FALSE ; |
| 991 |
REPEAT |
| 992 |
Done := FALSE ; |
| 993 |
i := 1 ; |
| 994 |
WHILE (i<=NoOfLines) AND (NOT Done) DO |
| 995 |
j := 1 ; |
| 996 |
WHILE (j<=NoOfLines) AND (NOT Done) DO |
| 997 |
IF (i#j) AND (NOT IsNulLine(i)) AND (NOT IsNulLine(j)) |
| 998 |
THEN |
| 999 |
IF IsIntersectionLine(i, j) |
| 1000 |
THEN |
| 1001 |
DeleteIntersectionLine(i, j) ; |
| 1002 |
LinkUpLines ; |
| 1003 |
Done := TRUE ; |
| 1004 |
Compacted := TRUE |
| 1005 |
END |
| 1006 |
END ; |
| 1007 |
INC(j) |
| 1008 |
END ; |
| 1009 |
INC(i) |
| 1010 |
END |
| 1011 |
UNTIL NOT Done ; |
| 1012 |
RETURN( Compacted ) |
| 1013 |
END CompactLines ; |
| 1014 |
|
| 1015 |
|
| 1016 |
(* |
| 1017 |
LinkUpLines - attempts to join lines that naturally run in to each other. |
| 1018 |
*) |
| 1019 |
|
| 1020 |
PROCEDURE LinkUpLines ; |
| 1021 |
VAR |
| 1022 |
Joined: BOOLEAN ; |
| 1023 |
i, j : CARDINAL ; |
| 1024 |
BEGIN |
| 1025 |
Joined := FALSE ; |
| 1026 |
i := 1 ; |
| 1027 |
WHILE (i<=NoOfLines) AND (NOT Joined) DO |
| 1028 |
j := 1 ; |
| 1029 |
WHILE (j<=NoOfLines) AND (NOT Joined) DO |
| 1030 |
Joined := JoinedLines(i, j) ; |
| 1031 |
INC(j) |
| 1032 |
END ; |
| 1033 |
INC(i) |
| 1034 |
END |
| 1035 |
END LinkUpLines ; |
| 1036 |
|
| 1037 |
|
| 1038 |
(* |
| 1039 |
JoinedLines - returns true if it can join lines i and j. |
| 1040 |
*) |
| 1041 |
|
| 1042 |
PROCEDURE JoinedLines (i, j: CARDINAL) : BOOLEAN ; |
| 1043 |
VAR |
| 1044 |
Joined : BOOLEAN ; |
| 1045 |
x1, x2, x3, x4, |
| 1046 |
y1, y2, y3, y4: CARDINAL ; |
| 1047 |
BEGIN |
| 1048 |
WITH Lines[i] DO |
| 1049 |
x1 := X1 ; |
| 1050 |
y1 := Y1 ; |
| 1051 |
x2 := X2 ; |
| 1052 |
y2 := Y2 |
| 1053 |
END ; |
| 1054 |
WITH Lines[j] DO |
| 1055 |
x3 := X1 ; |
| 1056 |
y3 := Y1 ; |
| 1057 |
x4 := X2 ; |
| 1058 |
y4 := Y2 |
| 1059 |
END ; |
| 1060 |
Joined := FALSE ; |
| 1061 |
(* X1 <= X2 - always *) |
| 1062 |
WITH Lines[i] DO |
| 1063 |
IF IsVertical(x1, y1, x2, y2) AND IsVertical(x3, y3, x4, y4) AND (x1=x3) |
| 1064 |
THEN |
| 1065 |
IF y4=y1 |
| 1066 |
THEN |
| 1067 |
AddLine(x3, y3, x2, y2) ; |
| 1068 |
DeleteLine(i) ; |
| 1069 |
DeleteLine(j) ; |
| 1070 |
Joined := TRUE |
| 1071 |
ELSIF y2=y3 |
| 1072 |
THEN |
| 1073 |
AddLine(x1, y1, x4, y4) ; |
| 1074 |
DeleteLine(i) ; |
| 1075 |
DeleteLine(j) ; |
| 1076 |
Joined := TRUE |
| 1077 |
END |
| 1078 |
ELSIF IsHorizontal(x1, y1, x2, y2) AND IsHorizontal(x3, y3, x4, y4) AND |
| 1079 |
(y1=y3) |
| 1080 |
THEN |
| 1081 |
IF x4=x1 |
| 1082 |
THEN |
| 1083 |
AddLine(x3, y3, x2, y2) ; |
| 1084 |
DeleteLine(i) ; |
| 1085 |
DeleteLine(j) ; |
| 1086 |
Joined := TRUE |
| 1087 |
ELSIF x2=x3 |
| 1088 |
THEN |
| 1089 |
AddLine(x1, y1, x4, y4) ; |
| 1090 |
DeleteLine(i) ; |
| 1091 |
DeleteLine(j) ; |
| 1092 |
Joined := TRUE |
| 1093 |
END |
| 1094 |
END |
| 1095 |
END ; |
| 1096 |
RETURN( Joined ) |
| 1097 |
END JoinedLines ; |
| 1098 |
|
| 1099 |
|
| 1100 |
(* |
| 1101 |
IsIntersectionLine - returns true if lines i and j intersect. |
| 1102 |
*) |
| 1103 |
|
| 1104 |
PROCEDURE IsIntersectionLine (i, j: CARDINAL) : BOOLEAN ; |
| 1105 |
BEGIN |
| 1106 |
RETURN( |
| 1107 |
IsIntersection( |
| 1108 |
Lines[i].X1, Lines[i].Y1, Lines[i].X2, Lines[i].Y2, |
| 1109 |
Lines[j].X1, Lines[j].Y1, Lines[j].X2, Lines[j].Y2) |
| 1110 |
) |
| 1111 |
END IsIntersectionLine ; |
| 1112 |
|
| 1113 |
|
| 1114 |
(* |
| 1115 |
DeleteIntersectionLine - joins two lines together, i and j, and |
| 1116 |
removes the intersection. |
| 1117 |
*) |
| 1118 |
|
| 1119 |
PROCEDURE DeleteIntersectionLine (i, j: CARDINAL) ; |
| 1120 |
VAR |
| 1121 |
x1, x2, x3, x4, |
| 1122 |
y1, y2, y3, y4: CARDINAL ; |
| 1123 |
BEGIN |
| 1124 |
WITH Lines[i] DO |
| 1125 |
x1 := X1 ; |
| 1126 |
y1 := Y1 ; |
| 1127 |
x2 := X2 ; |
| 1128 |
y2 := Y2 |
| 1129 |
END ; |
| 1130 |
WITH Lines[j] DO |
| 1131 |
x3 := X1 ; |
| 1132 |
y3 := Y1 ; |
| 1133 |
x4 := X2 ; |
| 1134 |
y4 := Y2 |
| 1135 |
END ; |
| 1136 |
IF IsVertical(x1, y1, x2, y2) AND IsVertical(x3, y3, x4, y4) |
| 1137 |
THEN |
| 1138 |
DeleteLine(i) ; |
| 1139 |
DeleteLine(j) ; |
| 1140 |
SubtractIntersection(y1, y2, y3, y4) ; |
| 1141 |
AddLine(x1, y1, x2, y2) ; |
| 1142 |
AddLine(x3, y3, x4, y4) |
| 1143 |
ELSIF IsHorizontal(x1, y1, x2, y2) AND IsHorizontal(x3, y3, x4, y4) |
| 1144 |
THEN |
| 1145 |
DeleteLine(i) ; |
| 1146 |
DeleteLine(j) ; |
| 1147 |
SubtractIntersection(x1, x2, x3, x4) ; |
| 1148 |
AddLine(x1, y1, x2, y2) ; |
| 1149 |
AddLine(x3, y3, x4, y4) |
| 1150 |
END |
| 1151 |
END DeleteIntersectionLine ; |
| 1152 |
|
| 1153 |
|
| 1154 |
(* |
| 1155 |
SubtractIntersection - deletes the intersecting entities of the range |
| 1156 |
i1..i2 j1..j2. |
| 1157 |
*) |
| 1158 |
|
| 1159 |
PROCEDURE SubtractIntersection (VAR i1, i2, j1, j2: CARDINAL) ; |
| 1160 |
VAR |
| 1161 |
k1, k2, |
| 1162 |
l1, l2: CARDINAL ; |
| 1163 |
BEGIN |
| 1164 |
Assert(i1<=i2) ; |
| 1165 |
Assert(j1<=j2) ; |
| 1166 |
IF IsSubRange(i1, i2, j1) |
| 1167 |
THEN |
| 1168 |
k1 := i1 ; |
| 1169 |
k2 := j1 |
| 1170 |
ELSIF IsSubRange(j1, j2, i1) |
| 1171 |
THEN |
| 1172 |
k1 := j1 ; |
| 1173 |
k2 := i1 |
| 1174 |
END ; |
| 1175 |
IF IsSubRange(i1, i2, j2) |
| 1176 |
THEN |
| 1177 |
l1 := j2 ; |
| 1178 |
l2 := i2 |
| 1179 |
ELSIF IsSubRange(j1, j2, i2) |
| 1180 |
THEN |
| 1181 |
l1 := i2 ; |
| 1182 |
l2 := j2 |
| 1183 |
END ; |
| 1184 |
i1 := k1 ; |
| 1185 |
i2 := k2 ; |
| 1186 |
j1 := l1 ; |
| 1187 |
j2 := l2 |
| 1188 |
END SubtractIntersection ; |
| 1189 |
|
| 1190 |
|
| 1191 |
(* |
| 1192 |
AddLine - adds a line to the lines buffer. |
| 1193 |
*) |
| 1194 |
|
| 1195 |
PROCEDURE AddLine (x1, y1, x2, y2: CARDINAL) ; |
| 1196 |
VAR |
| 1197 |
i: CARDINAL ; |
| 1198 |
BEGIN |
| 1199 |
IF (x1>x2) OR (y1>y2) |
| 1200 |
THEN |
| 1201 |
Swap(x1, x2) ; |
| 1202 |
Swap(y1, y2) |
| 1203 |
ELSIF (x1#x2) OR (y1#y2) |
| 1204 |
THEN |
| 1205 |
(* Do not store points *) |
| 1206 |
IF NOT InsertLine(x1, y1, x2, y2) |
| 1207 |
THEN |
| 1208 |
INC(NoOfLines) ; |
| 1209 |
WITH Lines[NoOfLines] DO |
| 1210 |
X1 := x1 ; |
| 1211 |
Y1 := y1 ; |
| 1212 |
X2 := x2 ; |
| 1213 |
Y2 := y2 |
| 1214 |
END |
| 1215 |
END |
| 1216 |
(* ; DisplayLines *) |
| 1217 |
END |
| 1218 |
END AddLine ; |
| 1219 |
|
| 1220 |
|
| 1221 |
(* |
| 1222 |
InsertLine - attempts to insert a line in a free slot, |
| 1223 |
true is returned if successfull. |
| 1224 |
*) |
| 1225 |
|
| 1226 |
PROCEDURE InsertLine (x1, y1, x2, y2: CARDINAL) : BOOLEAN ; |
| 1227 |
VAR |
| 1228 |
Success: BOOLEAN ; |
| 1229 |
i : CARDINAL ; |
| 1230 |
BEGIN |
| 1231 |
i := 1 ; |
| 1232 |
Success := FALSE ; |
| 1233 |
WHILE (NOT Success) AND (i<=NoOfLines) DO |
| 1234 |
IF IsNulLine(i) |
| 1235 |
THEN |
| 1236 |
WITH Lines[i] DO |
| 1237 |
X1 := x1 ; |
| 1238 |
Y1 := y1 ; |
| 1239 |
X2 := x2 ; |
| 1240 |
Y2 := y2 |
| 1241 |
END ; |
| 1242 |
Success := TRUE |
| 1243 |
ELSE |
| 1244 |
INC(i) |
| 1245 |
END |
| 1246 |
END ; |
| 1247 |
RETURN( Success ) |
| 1248 |
END InsertLine ; |
| 1249 |
|
| 1250 |
|
| 1251 |
(* |
| 1252 |
DeleteLine - deletes a line from the lines buffer. |
| 1253 |
*) |
| 1254 |
|
| 1255 |
PROCEDURE DeleteLine (l: CARDINAL) ; |
| 1256 |
BEGIN |
| 1257 |
WITH Lines[l] DO |
| 1258 |
X1 := 0 ; |
| 1259 |
Y1 := 0 ; |
| 1260 |
X2 := 0 ; |
| 1261 |
Y2 := 0 |
| 1262 |
END |
| 1263 |
(* ; DisplayLines *) |
| 1264 |
END DeleteLine ; |
| 1265 |
|
| 1266 |
|
| 1267 |
PROCEDURE DisplayLines ; |
| 1268 |
VAR |
| 1269 |
i: CARDINAL ; |
| 1270 |
BEGIN |
| 1271 |
WriteString('Lines') ; WriteLn ; |
| 1272 |
i := 1 ; |
| 1273 |
WHILE i<=NoOfLines DO |
| 1274 |
WITH Lines[i] DO |
| 1275 |
WriteCard(X1, 4) ; WriteCard(Y1, 4) ; |
| 1276 |
WriteCard(X2, 4) ; WriteCard(Y2, 4) ; WriteLn |
| 1277 |
END ; |
| 1278 |
INC(i) |
| 1279 |
END |
| 1280 |
END DisplayLines ; |
| 1281 |
|
| 1282 |
|
| 1283 |
(* |
| 1284 |
IsNulLine - returns true if line l is a nul line. |
| 1285 |
*) |
| 1286 |
|
| 1287 |
PROCEDURE IsNulLine (l: CARDINAL) : BOOLEAN ; |
| 1288 |
BEGIN |
| 1289 |
WITH Lines[l] DO |
| 1290 |
RETURN( (X1=0) AND (Y1=0) AND (X2=0) AND (Y2=0) ) |
| 1291 |
END |
| 1292 |
END IsNulLine ; |
| 1293 |
|
| 1294 |
|
| 1295 |
(* |
| 1296 |
RemoveRoom - removes a room, r, from the room list. |
| 1297 |
*) |
| 1298 |
|
| 1299 |
PROCEDURE RemoveRoom (r: CARDINAL) ; |
| 1300 |
BEGIN |
| 1301 |
Rooms[r].RoomNo := 0 (* No longer exists *) |
| 1302 |
END RemoveRoom ; |
| 1303 |
|
| 1304 |
|
| 1305 |
(* |
| 1306 |
InsertRoom - inserts a room, r, back into the room list. |
| 1307 |
*) |
| 1308 |
|
| 1309 |
PROCEDURE InsertRoom (r: CARDINAL) ; |
| 1310 |
BEGIN |
| 1311 |
Rooms[r].RoomNo := r |
| 1312 |
END InsertRoom ; |
| 1313 |
|
| 1314 |
|
| 1315 |
(* |
| 1316 |
RoomExist - returns true if a room, r, exists. |
| 1317 |
*) |
| 1318 |
|
| 1319 |
PROCEDURE RoomExists (r: CARDINAL) : BOOLEAN ; |
| 1320 |
BEGIN |
| 1321 |
RETURN( Rooms[r].RoomNo#0 ) |
| 1322 |
END RoomExists ; |
| 1323 |
|
| 1324 |
|
| 1325 |
(* |
| 1326 |
IsLineRoomSatisfied - returns true if the line room meets the requirements |
| 1327 |
of a room. |
| 1328 |
*) |
| 1329 |
|
| 1330 |
PROCEDURE IsLineRoomSatisfied () : BOOLEAN ; |
| 1331 |
VAR |
| 1332 |
Count, i: CARDINAL ; |
| 1333 |
BEGIN |
| 1334 |
Count := 0 ; |
| 1335 |
i := 1 ; |
| 1336 |
WHILE i<=NoOfLines DO |
| 1337 |
IF NOT IsNulLine(i) |
| 1338 |
THEN |
| 1339 |
INC(Count) |
| 1340 |
END ; |
| 1341 |
INC(i) |
| 1342 |
END ; |
| 1343 |
RETURN( Count<=MaxWallsPerRoom ) |
| 1344 |
(* Must also check for a door into another room *) |
| 1345 |
END IsLineRoomSatisfied ; |
| 1346 |
|
| 1347 |
|
| 1348 |
(* |
| 1349 |
CopyWallsToLines - copies walls from room r into the lines buffer. |
| 1350 |
*) |
| 1351 |
|
| 1352 |
PROCEDURE CopyWallsToLines (r: CARDINAL) ; |
| 1353 |
VAR |
| 1354 |
i: CARDINAL ; |
| 1355 |
BEGIN |
| 1356 |
i := 1 ; |
| 1357 |
WITH Rooms[r] DO |
| 1358 |
WHILE i<=NoOfWalls DO |
| 1359 |
INC(NoOfLines) ; |
| 1360 |
WITH Lines[NoOfLines] DO |
| 1361 |
X1 := Walls[i].X1 ; |
| 1362 |
Y1 := Walls[i].Y1 ; |
| 1363 |
X2 := Walls[i].X2 ; |
| 1364 |
Y2 := Walls[i].Y2 |
| 1365 |
END ; |
| 1366 |
INC(i) |
| 1367 |
END |
| 1368 |
END |
| 1369 |
END CopyWallsToLines ; |
| 1370 |
|
| 1371 |
|
| 1372 |
(* |
| 1373 |
CopyLinesToWalls - copies the lines buffer into the walls of room r. |
| 1374 |
*) |
| 1375 |
|
| 1376 |
PROCEDURE CopyLinesToWalls (r: CARDINAL) ; |
| 1377 |
BEGIN |
| 1378 |
WITH Rooms[r] DO |
| 1379 |
NoOfWalls := 0 ; |
| 1380 |
WHILE NoOfLines>0 DO |
| 1381 |
IF NOT IsNulLine(NoOfLines) |
| 1382 |
THEN |
| 1383 |
INC(NoOfWalls) ; |
| 1384 |
WITH Lines[NoOfLines] DO |
| 1385 |
Walls[NoOfWalls].X1 := X1 ; |
| 1386 |
Walls[NoOfWalls].Y1 := Y1 ; |
| 1387 |
Walls[NoOfWalls].X2 := X2 ; |
| 1388 |
Walls[NoOfWalls].Y2 := Y2 |
| 1389 |
END |
| 1390 |
END ; |
| 1391 |
DEC(NoOfLines) |
| 1392 |
END |
| 1393 |
END |
| 1394 |
END CopyLinesToWalls ; |
| 1395 |
|
| 1396 |
|
| 1397 |
(* |
| 1398 |
IsTouching - returns true if room r1 and r2 touch each other. |
| 1399 |
*) |
| 1400 |
|
| 1401 |
PROCEDURE IsTouching (r1, r2: CARDINAL) : BOOLEAN ; |
| 1402 |
VAR |
| 1403 |
i, j: CARDINAL ; |
| 1404 |
ok : BOOLEAN ; |
| 1405 |
BEGIN |
| 1406 |
ok := FALSE ; |
| 1407 |
i := 1 ; |
| 1408 |
WHILE (NOT ok) AND (i<=Rooms[r1].NoOfWalls) DO |
| 1409 |
j := 1 ; |
| 1410 |
WHILE (NOT ok) AND (j<=Rooms[r2].NoOfWalls) DO |
| 1411 |
ok := IsIntersection(Rooms[r1].Walls[i].X1, Rooms[r1].Walls[i].Y1, |
| 1412 |
Rooms[r1].Walls[i].X2, Rooms[r1].Walls[i].Y2, |
| 1413 |
Rooms[r2].Walls[j].X1, Rooms[r2].Walls[j].Y1, |
| 1414 |
Rooms[r2].Walls[j].X2, Rooms[r2].Walls[j].Y2) ; |
| 1415 |
INC(j) |
| 1416 |
END ; |
| 1417 |
INC(i) |
| 1418 |
END ; |
| 1419 |
RETURN( ok ) |
| 1420 |
END IsTouching ; |
| 1421 |
|
| 1422 |
|
| 1423 |
(* |
| 1424 |
IsIntersection - returns true if the line x1, y1, x2, y2 touches |
| 1425 |
line X1, Y1, X2, Y2. |
| 1426 |
This routine does not consider perpendicular |
| 1427 |
intersections. |
| 1428 |
*) |
| 1429 |
|
| 1430 |
PROCEDURE IsIntersection (x1, y1, x2, y2, X1, Y1, X2, Y2: CARDINAL) : BOOLEAN ; |
| 1431 |
BEGIN |
| 1432 |
Assert(x1#0) ; |
| 1433 |
Assert(X1#0) ; |
| 1434 |
IF IsVertical(x1, y1, x2, y2) AND IsVertical(X1, Y1, X2, Y2) AND |
| 1435 |
(x1=X1) |
| 1436 |
THEN |
| 1437 |
RETURN( IsIntersectingRange(y1, y2, Y1, Y2) ) |
| 1438 |
ELSIF IsHorizontal(x1, y1, x2, y2) AND IsHorizontal(X1, Y1, X2, Y2) AND |
| 1439 |
(y1=Y1) |
| 1440 |
THEN |
| 1441 |
RETURN( IsIntersectingRange(x1, x2, X1, X2) ) |
| 1442 |
ELSE |
| 1443 |
RETURN( FALSE ) |
| 1444 |
END |
| 1445 |
END IsIntersection ; |
| 1446 |
|
| 1447 |
|
| 1448 |
(* |
| 1449 |
IsVertical - returns true if line x1, y1, x2, y2 is vertical. |
| 1450 |
*) |
| 1451 |
|
| 1452 |
PROCEDURE IsVertical (x1, y1, x2, y2: CARDINAL) : BOOLEAN ; |
| 1453 |
BEGIN |
| 1454 |
RETURN( (x1=x2) AND (y1#y2) ) |
| 1455 |
END IsVertical ; |
| 1456 |
|
| 1457 |
|
| 1458 |
(* |
| 1459 |
IsHorizontal - returns true if line x1, y1, x2, y2 is horizontal. |
| 1460 |
*) |
| 1461 |
|
| 1462 |
PROCEDURE IsHorizontal (x1, y1, x2, y2: CARDINAL) : BOOLEAN ; |
| 1463 |
BEGIN |
| 1464 |
RETURN( (y1=y2) AND (x1#x2) ) |
| 1465 |
END IsHorizontal ; |
| 1466 |
|
| 1467 |
|
| 1468 |
(* |
| 1469 |
Adjacent - tests whether two rooms r1 & r2 are adjacent. |
| 1470 |
*) |
| 1471 |
|
| 1472 |
PROCEDURE Adjacent (r1, r2: CARDINAL) : BOOLEAN ; |
| 1473 |
VAR |
| 1474 |
i: CARDINAL ; |
| 1475 |
BEGIN |
| 1476 |
WITH Rooms[r1] DO |
| 1477 |
i := NoOfDoors ; |
| 1478 |
WHILE i>0 DO |
| 1479 |
IF Doors[i].LeadsTo=r2 |
| 1480 |
THEN |
| 1481 |
RETURN( TRUE ) |
| 1482 |
ELSE |
| 1483 |
DEC(i) |
| 1484 |
END |
| 1485 |
END |
| 1486 |
END ; |
| 1487 |
RETURN( FALSE ) |
| 1488 |
END Adjacent ; |
| 1489 |
|
| 1490 |
|
| 1491 |
PROCEDURE Stop ; |
| 1492 |
BEGIN |
| 1493 |
HALT |
| 1494 |
END Stop ; |
| 1495 |
|
| 1496 |
|
| 1497 |
END RoomMap. |