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