Debian Codegraph @

gcc/testsuite/gm2/projects/pim/run/pass/random/BoxMap.mod

raw
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 BoxMap ;
20
21 (*
22 Title : MakeMap
23 Author : Gaius Mulley
24 Date : 18/7/88
25 LastEdit : 18/7/88
26 System : LOGITECH MODULA-2/86
27 Description: Generates a simple random box map for Dungeon
28 *)
29
30 FROM MapOptions IMPORT isVerbose ;
31 FROM StdIO IMPORT Write, Read ;
32 FROM StrIO IMPORT WriteString, WriteLn ;
33 FROM NumberIO IMPORT WriteCard, ReadCard ;
34 FROM Assertion IMPORT Assert ;
35
36 FROM Geometry IMPORT IsSubLine, IsSubRange, IsIntersectingRange,
37 IntersectionLength, IsPointOnLine, Abs, Min, Max ;
38
39 FROM MakeBoxes IMPORT InitBoxes, KillBoxes,
40 AddBoxes, GetAndDeleteRandomBox ;
41
42 FROM StoreCoords IMPORT InitCoords, KillCoords,
43 GetAndDeleteRandomCoord, AddCoord, CoordsExist ;
44
45 FROM Chance IMPORT InitRandom, KillRandom,
46 GetAndDeleteRandom, AddRandom,
47 GetRand ;
48
49 FROM Options IMPORT userX, userY,
50 userMinRoomLength, userMaxRoomLength,
51 userMinCorridorLength, userMaxCorridorLength,
52 userTotalCorridorLength ;
53
54
55 CONST
56 MaxCard = MAX(CARDINAL) ;
57 MaxStack = 5000 ;
58
59 TYPE
60 Square = RECORD
61 Contents : (Empty, Secret, Door, Wall, Treasure) ;
62 RoomOfSquare: CARDINAL ;
63 END ;
64
65 Map = ARRAY [1..MaxX], [1..MaxY] OF Square ;
66
67 StackEntity = RECORD
68 PerimeterIndex : CARDINAL ; (* Untried Coords *)
69 BoxIndex : CARDINAL ; (* Untried boxes *)
70 OrientationIndex: CARDINAL ; (* Untried orient's *)
71 END ;
72
73 VAR
74 CurrentMap : Map ;
75 Stack : ARRAY [1..MaxStack] OF StackEntity ;
76 StackPtr : CARDINAL ;
77
78
79 (*
80 MinDistanceBetweenRooms - returns the minimum distance between two rooms.
81 *)
82
83 PROCEDURE MinDistanceBetweenRooms () : CARDINAL ;
84 BEGIN
85 RETURN userMinRoomLength-1
86 END MinDistanceBetweenRooms ;
87
88
89 (*
90 InitializeMap - Initializes CurrentMap.
91 CurrentMap has its boarder set to a Wall and middle
92 is set to Empty.
93 *)
94
95 PROCEDURE InitializeMap ;
96 VAR
97 i, j: CARDINAL ;
98 BEGIN
99 FOR i := 1 TO userX DO
100 FOR j := 1 TO userY DO
101 WITH CurrentMap[i, j] DO
102 Contents := Empty ;
103 RoomOfSquare := 0
104 END
105 END
106 END
107 END InitializeMap ;
108
109
110 (*
111 Init - Initialize the module and start the generation of a map.
112 *)
113
114 PROCEDURE Init ;
115 BEGIN
116 NoOfBoxes := 0 ;
117 (* Initialize box 0 the edge of the map *)
118 WITH Boxes[0] DO
119 x1 := 1 ;
120 x2 := userX ;
121 y1 := 1 ;
122 y2 := userY
123 END ;
124 StackPtr := 0
125 END Init ;
126
127
128 (*
129 CreateBoxMap - builds a map with central corridors and ajoining rooms.
130 *)
131
132 PROCEDURE CreateBoxMap ;
133 BEGIN
134 Init ;
135 CorridorMap ;
136 RoomMap
137 END CreateBoxMap ;
138
139
140 (*
141 CorridorMap - makes a map based arround central corridors.
142 *)
143
144 PROCEDURE CorridorMap ;
145 BEGIN
146 CreateCorridors ;
147 NoOfCorridorBoxes := NoOfBoxes
148 END CorridorMap ;
149
150
151 (*
152 CleanUpStack - cleans up the temporary stack where alternative rooms were
153 stored but are no longer needed.
154 *)
155
156 PROCEDURE CleanUpStack ;
157 BEGIN
158 WHILE StackPtr>0 DO
159 WITH Stack[StackPtr] DO
160 KillBoxes(BoxIndex) ;
161 KillCoords(PerimeterIndex) ;
162 KillRandom(OrientationIndex)
163 END ;
164 DEC(StackPtr)
165 END
166 END CleanUpStack ;
167
168
169 (*
170 RoomMap - creates the rooms on the map which fill in space left by the
171 corridors.
172 *)
173
174 PROCEDURE RoomMap ;
175 BEGIN
176 IF isVerbose ()
177 THEN
178 WriteString('Starting Room building') ; WriteLn
179 END ;
180 CreateRooms
181 END RoomMap ;
182
183
184 (*
185 CreateCorridors - creates a length of corridor on the map.
186 *)
187
188 PROCEDURE CreateCorridors ;
189 VAR
190 Length,
191 LengthLeft: CARDINAL ;
192 BEGIN
193 LengthLeft := userTotalCorridorLength ;
194 InitBoxCorridor ; (* Place new Box on the stack *)
195 REPEAT
196 IF MakeCorridor()
197 THEN
198 WITH Boxes[NoOfBoxes] DO
199 Length := Max(Abs(x1, x2), Abs(y1, y2))
200 END ;
201 IF LengthLeft>Length
202 THEN
203 DEC(LengthLeft, Length) ;
204 InitBoxCorridor (* Place new corridors on the stack *)
205 ELSE
206 LengthLeft := 0 (* All done *)
207 END
208 ELSE
209 IF StackPtr>0
210 THEN
211 (* Retract last corridor and try another *)
212 WriteString('backtracking, cannot place corridors onto an empty map') ; WriteLn ;
213 WriteString('halting - quicker than backtracking') ; WriteLn ;
214 HALT ;
215 WITH Boxes[NoOfBoxes] DO
216 INC(LengthLeft, Max(Abs(x1, x2), Abs(y1, y2)))
217 END ;
218 KillBox ;
219 UnMakeBox
220 ELSE
221 WriteString('run out of ideas! userMaxCorridorLength is too large!') ;
222 WriteLn ;
223 LengthLeft := 0 (* Fail safe exit *)
224 END
225 END
226 UNTIL LengthLeft=0
227 END CreateCorridors ;
228
229
230 (*
231 CreateRooms - places rooms inbetween the corridors on the map.
232 *)
233
234 PROCEDURE CreateRooms ;
235 VAR
236 Finished: BOOLEAN ;
237 BEGIN
238 InitBoxRoom ;
239 Finished := FALSE ;
240 REPEAT
241 IF MakeRoom()
242 THEN
243 InitBoxRoom ;
244 Finished := NOT CoordsExist(Stack[StackPtr].PerimeterIndex)
245 ELSE
246 Finished := TRUE ;
247 (*
248 IF StackPtr>0
249 THEN
250 (* Retract last room and try another *)
251 WriteString('Backtracking room') ; WriteLn ;
252 KillBox ;
253 UnMakeBox
254 ELSE
255 WriteString('Run out of ideas! Trying to create rooms!') ;
256 WriteLn ;
257 END
258 *)
259 END
260 UNTIL Finished ;
261 END CreateRooms ;
262
263
264 (*
265 MakeCorridor - returns true if a corridor was legally placed
266 onto the map.
267 *)
268
269 PROCEDURE MakeCorridor () : BOOLEAN ;
270 VAR
271 Success : BOOLEAN ;
272 x, y : CARDINAL ;
273 BEGIN
274 WITH Stack[StackPtr] DO
275 (*
276 Perimeter has been previously pushed.
277 We now try to place a piece of corridor
278 on a selected perimeter coordinate.
279 *)
280 Success := FALSE ;
281 REPEAT
282 GetAndDeleteRandomCoord(PerimeterIndex, x, y) ;
283 x := Min(x, userX) ;
284 y := Min(y, userY) ;
285 IF x#0 (* x=0 means no more coordinates to fetch *)
286 THEN
287 Success := PutCorridorOntoMap(x, y)
288 END
289 UNTIL Success OR (x=0) (* x=0 and y=0 means no coordinates left *)
290 (* when x=0 y is also 0. *)
291 END ;
292 RETURN( Success )
293 END MakeCorridor ;
294
295
296 (*
297 MakeRoom - returns true if a room was legally placed
298 onto the map.
299 *)
300
301 PROCEDURE MakeRoom () : BOOLEAN ;
302 VAR
303 Success : BOOLEAN ;
304 x, y : CARDINAL ;
305 BEGIN
306 WITH Stack[StackPtr] DO
307 (*
308 Perimeter has been previously pushed.
309 We now try to place a piece of corridor
310 on a selected perimeter coordinate.
311 *)
312 Success := FALSE ;
313 REPEAT
314 GetAndDeleteRandomCoord(PerimeterIndex, x, y) ;
315 IF x#0 (* x=0 means no more coordinates to fetch *)
316 THEN
317 Success := PutRoomOntoMap(x, y)
318 END
319 UNTIL Success OR (x=0) (* x=0 and y=0 means no coordinates left *)
320 (* when x=0 y is also 0. *)
321 END ;
322 RETURN( Success )
323 END MakeRoom ;
324
325
326 (*
327 UnMakeBox - deletes the last box placed in the Box list.
328 *)
329
330 PROCEDURE UnMakeBox ;
331 BEGIN
332 (*
333 IF NoOfCorridorBoxes>0
334 THEN
335 FindSpaceNextToRoom
336 END ;
337 *)
338 DEC(NoOfBoxes)
339 END UnMakeBox ;
340
341
342 (*
343 KillSurroundingBoxes - finds a pocket of space on the map and deletes
344 all neighbouring boxes.
345 *)
346
347 (*
348 PROCEDURE KillSurroundingBoxes ;
349 VAR
350 x, y,
351 i, j,
352 Swap, b: CARDINAL ;
353 BEGIN
354 GetFreeSpace(x, y) ;
355 i := x ;
356 j := y ;
357 Swap := NoOfBoxes ;
358 REPEAT
359 b := 1 ;
360 WHILE b<=Swap DO
361 IF IsPointOnBox(b, i, j)
362 THEN
363 SwapBox(b, Swap) ;
364 DEC(Swap)
365 END ;
366 INC(b)
367 END ;
368 WalkClockWise(i, j)
369 UNTIL (x=i) AND (y=j) ;
370 RenewBoxes(Swap, Swap)
371 END KillSurroundingBoxes ;
372 *)
373
374
375 (*
376 SwapBox - swaps two boxes, i and j, arround on the stack.
377 *)
378
379 PROCEDURE SwapBox (i, j: CARDINAL) ;
380 VAR
381 s: StackEntity ;
382 b: Box ;
383 BEGIN
384 b := Boxes[i] ;
385 Boxes[i] := Boxes[j] ;
386 Boxes[j] := b ;
387 s := Stack[i] ;
388 Stack[i] := Stack[j] ;
389 Stack[j] := s
390 END SwapBox ;
391
392
393 (*
394 FindSpaceNextToRoom - finds a pocket of space on the map and places
395 a room near this onto the top of the box stack.
396 *)
397
398 PROCEDURE FindSpaceNextToRoom ;
399 VAR
400 t: Box ;
401 x, y, b, d,
402 Nearest,
403 Distance : CARDINAL ;
404 BEGIN
405 GetSpaceCoord(x, y) ;
406 Nearest := 1 ;
407 Distance := DistanceAppartPoint(1, x, y) ;
408 b := NoOfBoxes-1 ;
409 WHILE b>1 DO
410 d := DistanceAppartPoint(b, x, y) ;
411 IF d<Distance
412 THEN
413 Distance := d ;
414 Nearest := b
415 END ;
416 DEC(b)
417 END ;
418 SwapBox(Nearest, NoOfBoxes)
419 END FindSpaceNextToRoom ;
420
421
422 (*
423 GetSpaceCoord - Sets x and y to a coordinate which has no room on it.
424 *)
425
426 PROCEDURE GetSpaceCoord (VAR x, y: CARDINAL) ;
427 VAR
428 Space: BOOLEAN ;
429 BEGIN
430 Space := FALSE ;
431 x := 1 ;
432 WHILE (NOT Space) AND (x<=userX) DO
433 y := 1 ;
434 WHILE (NOT Space) AND (y<=userY) DO
435 IF NOT IsSpace(x, y)
436 THEN
437 INC(y)
438 ELSE
439 Space := TRUE
440 END
441 END ;
442 IF NOT Space
443 THEN
444 INC(x)
445 END
446 END
447 END GetSpaceCoord ;
448
449
450 (*
451 Reschedule - reorders boxes on the stack, all boxes that touch the
452 top box are placed n-1 n-2 etc on the stack,
453 efficient recursive backtracking!
454 *)
455
456 PROCEDURE Reschedule (Lowest: CARDINAL) ;
457 VAR
458 b,
459 Swap: CARDINAL ;
460 t : Box ;
461 BEGIN
462 Swap := NoOfBoxes-1 ;
463 b := Lowest+1 ;
464 WITH Boxes[NoOfBoxes] DO
465 WHILE Swap>b DO
466 IF IsTouchingBox(b, x1, y1, x2, y2)
467 THEN
468 SwapBox(b, Swap) ;
469 INC(b)
470 END ;
471 DEC(Swap)
472 END
473 END
474 END Reschedule ;
475
476
477 (*
478 InitBoxCorridor - initializes a new corridor on the Stack,
479 the perimeter of the map is also pushed.
480 *)
481
482 PROCEDURE InitBoxCorridor ;
483 BEGIN
484 INC(StackPtr) ;
485 WITH Stack[StackPtr] DO
486 PerimeterIndex := InitCoords() ;
487 PushPerimeterOfBoxes(PerimeterIndex, FALSE) ;
488 OrientationIndex := 0 ;
489 BoxIndex := 0
490 END
491 END InitBoxCorridor ;
492
493
494 (*
495 InitBoxRoom - initializes a new corridor on the Stack,
496 the perimeter of the map is also pushed.
497 *)
498
499 PROCEDURE InitBoxRoom ;
500 BEGIN
501 (*
502 This is a really nasty kludge - because of memory space limitations
503 the StoreCoords module is pushed for space when creating large size
504 maps.
505 The kludge to get arround this is to kill all perimeter coordinates of the
506 previous box. This can be done since we never invoke backtracking
507 when creating boxrooms - but we may when we come up with a suitable
508 reliable algorithm, however, until then we can take advantage of
509 no backtracking and delete all perimeter coords of the last box.
510 *)
511 IF StackPtr>1
512 THEN
513 (* Ok delete perimeter coord *)
514 KillCoords(Stack[StackPtr].PerimeterIndex) ;
515 KillBoxes(Stack[StackPtr].BoxIndex)
516 END ;
517 (* All done - kludge over *)
518 INC(StackPtr) ;
519 WITH Stack[StackPtr] DO
520 PerimeterIndex := InitCoords() ;
521 PushPerimeterOfBoxes(PerimeterIndex, TRUE) ;
522 OrientationIndex := 0 ;
523 BoxIndex := 0
524 END
525 END InitBoxRoom ;
526
527
528 (*
529 KillBox - pops the last Box from the stack.
530 *)
531
532 PROCEDURE KillBox ;
533 BEGIN
534 WITH Stack[StackPtr] DO
535 KillCoords(PerimeterIndex)
536 END ;
537 DEC(StackPtr)
538 END KillBox ;
539
540
541 (*
542 PutCorridorOntoMap - returns true if it has placed a corridor
543 onto a map.
544 Otherwise no corridor has been placed onto
545 this map.
546 *)
547
548 PROCEDURE PutCorridorOntoMap (x, y: CARDINAL) : BOOLEAN ;
549 VAR
550 LenX,
551 LenY : CARDINAL ;
552 Success : BOOLEAN ;
553 BEGIN
554 CheckInitBoxCorridorIndex ;
555 WITH Stack[StackPtr] DO
556 Success := FALSE ;
557 REPEAT
558 IF GetBox(LenX, LenY)
559 THEN
560 Success := PlaceCorridorBox(x, y, LenX-1, LenY-1)
561 END
562 UNTIL Success OR (LenX=0) ;
563 END ;
564 CheckKillBoxIndex(LenX=0) ;
565 RETURN( Success )
566 END PutCorridorOntoMap ;
567
568
569 (*
570 PutRoomOntoMap - returns true if it has placed a room
571 onto a map.
572 Otherwise no room has been placed onto
573 this map.
574 *)
575
576 PROCEDURE PutRoomOntoMap (x, y: CARDINAL) : BOOLEAN ;
577 VAR
578 LenX,
579 LenY : CARDINAL ;
580 Success : BOOLEAN ;
581 BEGIN
582 CheckInitBoxRoomIndex ;
583 WITH Stack[StackPtr] DO
584 Success := FALSE ;
585 REPEAT
586 IF GetBox(LenX, LenY)
587 THEN
588 Success := PlaceRoomBox(x, y, LenX-1, LenY-1)
589 END
590 UNTIL Success OR (LenX=0) ;
591 END ;
592 CheckKillBoxIndex(LenX=0) ;
593 RETURN( Success )
594 END PutRoomOntoMap ;
595
596
597 (*
598 GetBox - returns true if a box can be returned.
599 It chooses one box from the box index,
600 from the stack.
601 The lengths of the Box are returned
602 in LengthX and LengthY.
603 *)
604
605 PROCEDURE GetBox (VAR LengthX, LengthY: CARDINAL) : BOOLEAN ;
606 BEGIN
607 WITH Stack[StackPtr] DO
608 GetAndDeleteRandomBox(BoxIndex, LengthX, LengthY)
609 END ;
610 RETURN(LengthX#0) (* LengthX#0 means found legal size box *)
611 END GetBox ;
612
613
614 (*
615 CheckInitBoxCorridorIndex - checks to see whether the current
616 stacked box needs
617 a list of legal corridor sizes stacked.
618 *)
619
620 PROCEDURE CheckInitBoxCorridorIndex ;
621 BEGIN
622 WITH Stack[StackPtr] DO
623 IF BoxIndex=0
624 THEN
625 (* Without stacked box list of legal sized corridors *)
626 BoxIndex := InitBoxes() ;
627 AddBoxes(BoxIndex, userMinCorridorLength, CorridorWidth,
628 userMaxCorridorLength, CorridorWidth) ;
629 AddBoxes(BoxIndex, CorridorWidth, userMinCorridorLength,
630 CorridorWidth, userMaxCorridorLength)
631 END
632 END
633 END CheckInitBoxCorridorIndex ;
634
635
636 (*
637 CheckInitBoxRoomIndex - checks to see whether the current stack box
638 needs a list of legal corridor sizes stacked.
639 *)
640
641 PROCEDURE CheckInitBoxRoomIndex ;
642 BEGIN
643 WITH Stack[StackPtr] DO
644 IF BoxIndex=0
645 THEN
646 (* Without stacked box list of legal sized rooms *)
647 BoxIndex := InitBoxes() ;
648 AddBoxes(BoxIndex,
649 userMinRoomLength, userMinRoomLength,
650 userMaxRoomLength, userMaxRoomLength)
651 END
652 END
653 END CheckInitBoxRoomIndex ;
654
655
656 (*
657 CheckKillBoxIndex - if NeedToKill is set then the list of boxes
658 on the stack is killed.
659 Ideally this procedure should be a macro.
660 *)
661
662 PROCEDURE CheckKillBoxIndex (NeedToKill: BOOLEAN) ;
663 BEGIN
664 IF NeedToKill
665 THEN
666 WITH Stack[StackPtr] DO
667 KillBoxes(BoxIndex) ;
668 BoxIndex := 0
669 END
670 END
671 END CheckKillBoxIndex ;
672
673
674 (*
675 PlaceCorridorBox - returns true if a box can make a corridor at
676 position x, y.
677 All 4 orientations are tried.
678
679
680 2 1
681 4 3
682
683 Ie 1: (x, y) (x+LenX, y+LenY)
684 2: (x, y) (x-LenX, y+LenY)
685 3: (x, y) (x+LenX, y-LenY)
686 4: (x, y) (x-LenX, y-LenY)
687 *)
688
689 PROCEDURE PlaceCorridorBox (x, y: CARDINAL; LenX, LenY: CARDINAL) : BOOLEAN ;
690 VAR
691 Success: BOOLEAN ;
692 i : CARDINAL ;
693 BEGIN
694 CheckInitOrientationIndex ;
695 WITH Stack[StackPtr] DO
696 Success := FALSE ;
697 REPEAT
698 i := GetAndDeleteRandom(OrientationIndex) ;
699 CASE i OF
700
701 1: Success := AttemptToPlaceCorridor(x, y, x+LenX, y+LenY) |
702
703 2: IF x>LenX
704 THEN
705 Success := AttemptToPlaceCorridor(x-LenX, y, x, y+LenY)
706 END |
707
708 3: IF y>LenY
709 THEN
710 Success := AttemptToPlaceCorridor(x, y-LenY, x+LenX, y)
711 END |
712
713 4: IF (x>LenX) AND (y>LenY)
714 THEN
715 Success := AttemptToPlaceCorridor(x-LenX, y-LenY, x, y)
716 END
717
718 ELSE
719 END
720 UNTIL Success OR (i=0) ;
721 END ;
722 CheckKillOrientationIndex(i=0) ;
723 RETURN( Success )
724 END PlaceCorridorBox ;
725
726
727 (*
728 PlaceRoomBox - returns true if a box can make a corridor at
729 position x, y.
730 All 4 orientations are tried.
731
732
733 2 1
734 4 3
735
736 Ie 1: (x, y) (x+LenX, y+LenY)
737 2: (x, y) (x-LenX, y+LenY)
738 3: (x, y) (x+LenX, y-LenY)
739 4: (x, y) (x-LenX, y-LenY)
740 *)
741
742 PROCEDURE PlaceRoomBox (x, y: CARDINAL; LenX, LenY: CARDINAL) : BOOLEAN ;
743 VAR
744 Success: BOOLEAN ;
745 i : CARDINAL ;
746 BEGIN
747 CheckInitOrientationIndex ;
748 WITH Stack[StackPtr] DO
749 Success := FALSE ;
750 REPEAT
751 i := GetAndDeleteRandom(OrientationIndex) ;
752 CASE i OF
753
754 1: Success := AttemptToPlaceRoom(x, y, x+LenX, y+LenY) |
755
756 2: IF x>LenX
757 THEN
758 Success := AttemptToPlaceRoom(x-LenX, y, x, y+LenY)
759 END |
760
761 3: IF y>LenY
762 THEN
763 Success := AttemptToPlaceRoom(x, y-LenY, x+LenX, y)
764 END |
765
766 4: IF (x>LenX) AND (y>LenY)
767 THEN
768 Success := AttemptToPlaceRoom(x-LenX, y-LenY, x, y)
769 END
770
771 ELSE
772 END
773 UNTIL Success OR (i=0) ;
774 END ;
775 CheckKillOrientationIndex(i=0) ;
776 RETURN( Success )
777 END PlaceRoomBox ;
778
779
780 (*
781 CheckInitOrientationIndex - checks to see whether the current stacked
782 entity needs a new orientation index to also
783 be stacked.
784 *)
785
786 PROCEDURE CheckInitOrientationIndex ;
787 BEGIN
788 WITH Stack[StackPtr] DO
789 IF OrientationIndex=0
790 THEN
791 OrientationIndex := InitRandom() ;
792 AddRandom(OrientationIndex, 4)
793 END
794 END
795 END CheckInitOrientationIndex ;
796
797
798 (*
799 CheckKillOrientationIndex - checks to see whether the current stacked
800 entities orientation index needs to be
801 deleted.
802 This procedure ideally should be a macro..
803 *)
804
805 PROCEDURE CheckKillOrientationIndex (NeedToKill: BOOLEAN) ;
806 BEGIN
807 IF NeedToKill
808 THEN
809 WITH Stack[StackPtr] DO
810 KillRandom(OrientationIndex) ;
811 OrientationIndex := 0
812 END
813 END
814 END CheckKillOrientationIndex ;
815
816
817 (*
818 PushPerimeterOfBoxes - pushes all the current perimeter of the box map onto
819 the perimeter stack.
820 *)
821
822 PROCEDURE PushPerimeterOfBoxes (CoordIndex: CARDINAL; NoOpt: BOOLEAN) ;
823 VAR
824 i: CARDINAL ;
825 BEGIN
826 IF NoOfBoxes=0
827 THEN
828 (* Perimeter is center square in map *)
829 AddCoord(CoordIndex, userX DIV 2, userY DIV 2)
830 ELSE
831 i := 1 ;
832 WHILE i<=NoOfBoxes DO
833 PushPerimeterOfWalls(CoordIndex, i, NoOpt) ;
834 INC(i)
835 END
836 END
837 END PushPerimeterOfBoxes ;
838
839
840 (*
841 PushPerimeterOfWalls - pushes all coordinates of a box wall which are
842 external to the group of boxes.
843 Ie any wall which does is not shared by an
844 adjacent box MUST be external.
845 NoOpt determines whether optimization should be
846 applied to the restricting of perimeter coords.
847 Optimiztion tests for the minimum size of a room
848 to any wall, if this fails the coord is not added
849 to the perimeter list.
850 However this should not be used when pushing the
851 room perimeter since optimization is too restrictive.
852 (Corridor restrictions etc).
853 *)
854
855 PROCEDURE PushPerimeterOfWalls (CoordIndex: CARDINAL; b: CARDINAL;
856 NoOpt: BOOLEAN) ;
857 VAR
858 i, j: CARDINAL ;
859 BEGIN
860 WITH Boxes[b] DO
861 FOR i := x1 TO x2 DO
862 IF IsExternalHorizWallPerimeter(b, i, y1) AND
863 (NoOpt OR IsEnoughSpacePointToBox(i, y1))
864 THEN
865 AddCoord(CoordIndex, i, y1)
866 END ;
867 IF IsExternalHorizWallPerimeter(b, i, y2) AND
868 (NoOpt OR IsEnoughSpacePointToBox(i, y2))
869 THEN
870 AddCoord(CoordIndex, i, y2)
871 END
872 END ;
873 FOR j := y1 TO y2 DO
874 IF IsExternalVertWallPerimeter(b, x1, j) AND
875 (NoOpt OR IsEnoughSpacePointToBox(x1, j))
876 THEN
877 AddCoord(CoordIndex, x1, j)
878 END ;
879 IF IsExternalVertWallPerimeter(b, x2, j) AND
880 (NoOpt OR IsEnoughSpacePointToBox(x2, j))
881 THEN
882 AddCoord(CoordIndex, x2, j)
883 END
884 END
885 END
886 END PushPerimeterOfWalls ;
887
888
889 (*
890 IsExternalHorizWallPerimeter - returns true if coordinates,
891 x and y are not on any Horiz
892 wall of any box except b.
893 This routine allows point z, y to be
894 on a Vertical wall, but NOT on another
895 Horizontal wall.
896 *)
897
898 PROCEDURE IsExternalHorizWallPerimeter (b: CARDINAL;
899 x, y: CARDINAL) : BOOLEAN ;
900 VAR
901 i : CARDINAL ;
902 Found: BOOLEAN ;
903 BEGIN
904 Found := FALSE ;
905 IF NOT IsCornerPerimeter(b, x, y)
906 THEN
907 i := 0 ;
908 WHILE (i<=NoOfBoxes) AND (NOT Found) DO
909 IF i#b
910 THEN
911 WITH Boxes[i] DO
912 IF IsPointOnLine(x, y, x1, y1, x2, y1)
913 THEN
914 Found := TRUE
915 ELSIF IsPointOnLine(x, y, x1, y2, x2, y2)
916 THEN
917 Found := TRUE
918 END
919 END
920 END ;
921 INC(i)
922 END
923 END ;
924 RETURN( NOT Found )
925 END IsExternalHorizWallPerimeter ;
926
927
928 (*
929 IsExternalVertWallPerimeter - returns true if coordinates,
930 x and y are not on any Vertical
931 wall of any box except b.
932 This routine allows point z, y to be
933 on a Horizontal wall, but NOT on another
934 Vertical wall.
935 *)
936
937 PROCEDURE IsExternalVertWallPerimeter (b: CARDINAL;
938 x, y: CARDINAL) : BOOLEAN ;
939 VAR
940 i : CARDINAL ;
941 Found: BOOLEAN ;
942 BEGIN
943 Found := FALSE ;
944 IF NOT IsCornerPerimeter(b, x, y)
945 THEN
946 i := 0 ;
947 WHILE (i<=NoOfBoxes) AND (NOT Found) DO
948 IF i#b
949 THEN
950 WITH Boxes[i] DO
951 IF IsPointOnLine(x, y, x1, y1, x1, y2)
952 THEN
953 Found := TRUE
954 ELSIF IsPointOnLine(x, y, x2, y1, x2, y2)
955 THEN
956 Found := TRUE
957 END
958 END
959 END ;
960 INC(i)
961 END
962 END ;
963 RETURN( NOT Found )
964 END IsExternalVertWallPerimeter ;
965
966
967 (*
968 AttemptToPlaceCorridor - attempts to place a corridor x1, y1 x2, y2
969 onto the map.
970 If it succeeds it returns true
971 otherwise false
972 *)
973
974 PROCEDURE AttemptToPlaceCorridor (x1, y1, x2, y2: CARDINAL) : BOOLEAN ;
975 VAR
976 Success: BOOLEAN ;
977 BEGIN
978 IF IsCorridorSatisfied(x1, y1, x2, y2)
979 THEN
980 AddBox(x1, y1, x2, y2) ;
981 Success := TRUE
982 ELSE
983 Success := FALSE
984 END ;
985 RETURN( Success )
986 END AttemptToPlaceCorridor ;
987
988
989 (*
990 AttemptToPlaceRoom - attempts to place a room x1, y1 x2, y2
991 onto the map.
992 If it succeeds it returns true
993 otherwise false
994 *)
995
996 PROCEDURE AttemptToPlaceRoom (x1, y1, x2, y2: CARDINAL) : BOOLEAN ;
997 VAR
998 Success: BOOLEAN ;
999 BEGIN
1000 IF IsRoomSatisfied(x1, y1, x2, y2)
1001 THEN
1002 AddBox(x1, y1, x2, y2) ;
1003 Success := TRUE
1004 ELSE
1005 Success := FALSE
1006 END ;
1007 RETURN( Success )
1008 END AttemptToPlaceRoom ;
1009
1010
1011 (*
1012 IsCorridorSatisfied - returns true if a Corridor x1, y1 x2, y2
1013 may be placed onto the map without
1014 contraveining the various rules.
1015 *)
1016
1017 PROCEDURE IsCorridorSatisfied (x1, y1, x2, y2: CARDINAL) : BOOLEAN ;
1018 VAR
1019 Success: BOOLEAN ;
1020 BEGIN
1021 (* Put(x1, y1, x2, y2) ; *)
1022 IF (x2>userX) OR (y2>userY)
1023 THEN
1024 (* WriteString('Failed SIZE') ; WriteLn *)
1025 Success := FALSE
1026 ELSIF NOT DistanceAppartEdge(x1, y1, x2, y2)
1027 THEN
1028 Success := FALSE
1029 ELSIF IsOverLappingBox(x1, y1, x2, y2)
1030 THEN
1031 (* WriteString('Failed OVERLAP') ; *)
1032 Success := FALSE
1033 ELSIF NOT IsCorridorJoin(x1, y1, x2, y2)
1034 THEN
1035 (* WriteString('Failed CORRIDOR JOIN') ; *)
1036 Success := FALSE
1037 ELSIF NOT IsEnoughSpaceBetweenCorridors(x1, y1, x2, y2)
1038 THEN
1039 (* WriteString('Failed SPACE') ; *)
1040 Success := FALSE
1041 ELSE
1042 Success := TRUE
1043 END ;
1044 RETURN( Success )
1045 END IsCorridorSatisfied ;
1046
1047
1048 (*
1049 IsRoomSatisfied - returns true if a box x1, y1 x2, y2
1050 may be placed onto the map without
1051 contraveining the various rules.
1052 *)
1053
1054 PROCEDURE IsRoomSatisfied (x1, y1, x2, y2: CARDINAL) : BOOLEAN ;
1055 VAR
1056 Success: BOOLEAN ;
1057 BEGIN
1058 (* Put(x1, y1, x2, y2) ; *)
1059 IF (x2>userX) OR (y2>userY)
1060 THEN
1061 (* WriteString('Failed SIZE') ; WriteLn ; *)
1062 Success := FALSE
1063 ELSIF IsOverLappingBox(x1, y1, x2, y2)
1064 THEN
1065 (* WriteString('Failed OVERLAP') ; *)
1066 Success := FALSE
1067 ELSIF NOT DistanceAppartEdge(x1, y1, x2, y2)
1068 THEN
1069 Success := FALSE
1070 ELSIF NOT IsBoxRoomLegal(x1, y1, x2, y2)
1071 THEN
1072 (* WriteString('Failed Legal') ; *)
1073 Success := FALSE
1074 ELSIF NOT IsRoomJoin(x1, y1, x2, y2)
1075 THEN
1076 (* WriteString('Failed ROOM JOIN') ; *)
1077 Success := FALSE
1078 ELSIF NOT IsEnoughSpaceBetweenRooms(x1, y1, x2, y2)
1079 THEN
1080 (* WriteString('Failed SPACE') ; *)
1081 Success := FALSE
1082 ELSE
1083 Success := TRUE
1084 (* ; WriteString('SUCCESS') ; *)
1085 END ;
1086 RETURN( Success )
1087 END IsRoomSatisfied ;
1088
1089
1090 (*
1091 IsEnoughSpacePointToBox - returns true if there is enough space
1092 between a point, x, y and all the boxes.
1093 This routine is called before perimeter
1094 coordinates are pushed, therefore coordinates
1095 pushed are not doomed to failure due to lack
1096 of space.
1097 This routine consists of a reduced
1098 IsEnoughSpaceBetweenBoxes procedure.
1099 *)
1100
1101 PROCEDURE IsEnoughSpacePointToBox (x, y: CARDINAL) : BOOLEAN ;
1102 VAR
1103 ok : BOOLEAN ;
1104 i : CARDINAL ;
1105 Distance: CARDINAL ;
1106 BEGIN
1107 ok := TRUE ;
1108 i := 0 ; (* 0 = Perimeter of map *)
1109 WHILE ok AND (i<=NoOfBoxes) DO
1110 Distance := DistanceAppartPoint(i, x, y) ;
1111 IF Distance#0
1112 THEN
1113 ok := (Distance>=MinDistanceBetweenRooms())
1114 END ;
1115 INC(i)
1116 END ;
1117 RETURN( ok )
1118 END IsEnoughSpacePointToBox ;
1119
1120
1121 (*
1122 IsEnoughSpaceBetweenCorridors - returns true if there is enough
1123 space between box x1, y1 x2, y2
1124 and the other boxes.
1125 Also tests for right angle connection.
1126 *)
1127
1128 PROCEDURE IsEnoughSpaceBetweenCorridors (x1, y1, x2, y2: CARDINAL) : BOOLEAN ;
1129 VAR
1130 ok : BOOLEAN ;
1131 i : CARDINAL ;
1132 Distance: CARDINAL ;
1133 BEGIN
1134 ok := TRUE ;
1135 i := 1 ;
1136 WHILE ok AND (i<=NoOfBoxes) DO
1137 IF IsTouchingBox(i, x1, y1, x2, y2)
1138 THEN
1139 (* Check for a box that is not at right angles to new box. *)
1140 (* We are only allowed to touch a box at right angles. *)
1141 IF NOT IsDifferentOrientationBox(i, x1, y1, x2, y2)
1142 THEN
1143 (* touching a box which is not at right angles *)
1144 ok := FALSE
1145 END
1146 ELSIF FreeSpace(i, x1, y1, x2, y2)
1147 THEN
1148 Distance := DistanceAppartBox(i, x1, y1, x2, y2) ;
1149 (*
1150 Distance := Min(
1151 DistanceAppartBox(i, x1, y1, x2, y2),
1152 DistanceAppartDiagonal(i, x1, y1, x2, y2)
1153 ) ;
1154 *)
1155 IF Distance=0
1156 THEN
1157 ELSE
1158 ok := (Distance>=MinDistanceBetweenCorridors)
1159 END
1160 END ;
1161 INC(i)
1162 END ;
1163 RETURN( ok )
1164 END IsEnoughSpaceBetweenCorridors ;
1165
1166
1167 (*
1168 IsBoxRoomLegal - returns true if a box x1, y1, x2, y2 does not
1169 have a wall which is next to but not sharing
1170 another wall.
1171 *)
1172
1173 PROCEDURE IsBoxRoomLegal (x1, y1, x2, y2: CARDINAL) : BOOLEAN ;
1174 VAR
1175 ok : BOOLEAN ;
1176 i : CARDINAL ;
1177 Distance: CARDINAL ;
1178 CoveredN,
1179 CoveredS,
1180 CoveredE,
1181 CoveredW: BOOLEAN ;
1182 BEGIN
1183 CoveredN := IsFullyCovered(x1, y2, x2, y2) ;
1184 CoveredS := IsFullyCovered(x1, y1, x2, y1) ;
1185 CoveredE := IsFullyCovered(x2, y1, x2, y2) ;
1186 CoveredW := IsFullyCovered(x1, y1, x1, y2) ;
1187 ok := TRUE ;
1188 i := 1 ;
1189 WHILE ok AND (i<=NoOfBoxes) DO
1190 IF NOT IsTouchingBox(i, x1, y1, x2, y2)
1191 THEN
1192 IF (x1>1) AND (NOT CoveredW)
1193 THEN
1194 ok := NOT IsTouchingBox(i, x1-1, y1, x2, y2)
1195 END ;
1196 IF ok AND (y1>1) AND (NOT CoveredS)
1197 THEN
1198 ok := NOT IsTouchingBox(i, x1, y1-1, x2, y2)
1199 END ;
1200 IF ok AND (x2<userX) AND (NOT CoveredE)
1201 THEN
1202 ok := NOT IsTouchingBox(i, x1, y1, x2+1, y2)
1203 END ;
1204 IF ok AND (y2<userY) AND (NOT CoveredN)
1205 THEN
1206 ok := NOT IsTouchingBox(i, x1, y1, x2, y2+1)
1207 END
1208 END ;
1209 INC(i)
1210 END ;
1211 RETURN( ok )
1212 END IsBoxRoomLegal ;
1213
1214
1215 (*
1216 IsFullyCovered - returns true if every point on the line
1217 x1, y1, x2, y2 is covered. The line must
1218 either be horizontal or vertical.
1219 *)
1220
1221 PROCEDURE IsFullyCovered (x1, y1, x2, y2: CARDINAL) : BOOLEAN ;
1222 VAR
1223 i : CARDINAL ;
1224 Covered: BOOLEAN ;
1225 BEGIN
1226 Covered := TRUE ;
1227 IF x1=x2
1228 THEN
1229 (* Vertical *)
1230 i := y1 ;
1231 WHILE Covered AND (i<=y2) DO
1232 Covered := IsSpace(x1, i) ;
1233 INC(i)
1234 END
1235 ELSIF y1=y2
1236 THEN
1237 (* Horizontal *)
1238 i := x1 ;
1239 WHILE Covered AND (i<=x2) DO
1240 Covered := IsSpace(i, y1) ;
1241 INC(i)
1242 END
1243 END ;
1244 RETURN( Covered )
1245 END IsFullyCovered ;
1246
1247
1248 (*
1249 IsEnoughSpaceBetweenRooms - returns true if there is enough
1250 space between box x1, y1 x2, y2
1251 and the other boxes.
1252 *)
1253
1254 PROCEDURE IsEnoughSpaceBetweenRooms (x1, y1, x2, y2: CARDINAL) : BOOLEAN ;
1255 VAR
1256 ok : BOOLEAN ;
1257 i : CARDINAL ;
1258 Distance: CARDINAL ;
1259 BEGIN
1260 ok := TRUE ;
1261 i := 1 ;
1262 WHILE ok AND (i<=NoOfBoxes) DO
1263 IF NOT IsTouchingBox(i, x1, y1, x2, y2)
1264 THEN
1265 (* Dont test the walls of the box for contraveining the space rule *)
1266 (*
1267 IF (x1+1<x2-1) AND (y1+1<y2-1) AND FreeSpace(i, x1+1, y1+1, x2-1, y2-1)
1268 THEN
1269 Distance := DistanceAppartDiagonal(i, x1+1, y1+1, x2-1, y2-1) ;
1270 *)
1271 IF FreeSpace(i, x1, y1, x2, y2)
1272 THEN
1273 (*
1274 Distance := DistanceAppartDiagonal(i, x1, y1, x2, y2) ;
1275 *)
1276 Distance := Max( DistanceAppartDiagonal(i, x1, y1, x2, y2),
1277 DistanceAppartBox(i, x1, y1, x2, y2) ) ;
1278 (* WriteString('Dist') ; WriteCard(Distance, 6) ; WriteLn ; *)
1279 IF Distance=0
1280 THEN
1281 (* touching a box *)
1282 ELSE
1283 Assert(NOT IsTouchingBox(i, x1, y1, x2, y2)) ;
1284 ok := (Distance>=MinDistanceBetweenRooms())
1285 END
1286 END
1287 END ;
1288 INC(i)
1289 END ;
1290 RETURN( ok )
1291 END IsEnoughSpaceBetweenRooms ;
1292
1293
1294 (*
1295 FreeSpace - returns true if there exists free space between box
1296 X1, Y1, X2, Y2 and box b.
1297 Should not be called if box b touches X1, Y1, X2, Y2.
1298 *)
1299
1300 PROCEDURE FreeSpace (b: CARDINAL; X1, Y1, X2, Y2: CARDINAL) : BOOLEAN ;
1301 VAR
1302 Free : BOOLEAN ;
1303 xs, xe,
1304 ys, ye,
1305 i, j : CARDINAL ;
1306 BEGIN
1307 WITH Boxes[b] DO
1308 IF Abs(X1, x2)<Abs(X2, x1)
1309 THEN
1310 xs := Min(X1, x2) ;
1311 xe := Max(X1, x2)
1312 ELSE
1313 xs := Min(X2, x1) ;
1314 xe := Max(X2, x1)
1315 END ;
1316 IF Abs(Y1, y2)<Abs(Y2, y1)
1317 THEN
1318 ys := Min(Y1, y2) ;
1319 ye := Max(Y1, y2)
1320 ELSE
1321 ys := Min(Y2, y1) ;
1322 ye := Max(Y2, y1)
1323 END ;
1324 Free := FALSE ;
1325 i := xs ;
1326 WHILE (NOT Free) AND (i<=xe) DO
1327 j := ys ;
1328 WHILE (NOT Free) AND (j<=ye) DO
1329 Free := IsSpace(i, j) ;
1330 INC(j)
1331 END ;
1332 INC(i)
1333 END
1334 END ;
1335 (* IF Free THEN WriteString('FREE') END ; *)
1336 RETURN( Free )
1337 END FreeSpace ;
1338
1339
1340 (*
1341 IsSpace - returns true if point x, y is not in any box.
1342 A wall is counted as in the box.
1343 *)
1344
1345 PROCEDURE IsSpace (x, y: CARDINAL) : BOOLEAN ;
1346 VAR
1347 b : CARDINAL ;
1348 InBox: BOOLEAN ;
1349 BEGIN
1350 InBox := FALSE ;
1351 b := 1 ; (* Not zero of course !! *)
1352 WHILE (NOT InBox) AND (b<=NoOfBoxes) DO
1353 WITH Boxes[b] DO
1354 InBox := IsSubRange(x1, x2, x) AND IsSubRange(y1, y2, y)
1355 END ;
1356 INC(b)
1357 END ;
1358 RETURN( NOT InBox )
1359 END IsSpace ;
1360
1361
1362 (*
1363 DistanceAppartEdge - returns true if the box, x1, y1, x2, y2, is a
1364 required distance away from the edge of the
1365 map.
1366 Cannot use room zero for this test as we are inside
1367 it and may touch one wall but be too near another!
1368 *)
1369
1370 PROCEDURE DistanceAppartEdge (x1, y1, x2, y2: CARDINAL) : BOOLEAN ;
1371 VAR
1372 ok : BOOLEAN ;
1373 Distance: CARDINAL ;
1374 BEGIN
1375 ok := TRUE ;
1376 Distance := Abs(x1, 1) ;
1377 IF (Distance>0) AND ok
1378 THEN
1379 ok := (Distance>=MinDistanceBetweenRooms())
1380 END ;
1381 Distance := Abs(x2, userX) ;
1382 IF (Distance>0) AND ok
1383 THEN
1384 ok := (Distance>=MinDistanceBetweenRooms())
1385 END ;
1386 Distance := Abs(y1, 1) ;
1387 IF (Distance>0) AND ok
1388 THEN
1389 ok := (Distance>=MinDistanceBetweenRooms())
1390 END ;
1391 Distance := Abs(y2, userY) ;
1392 IF (Distance>0) AND ok
1393 THEN
1394 ok := (Distance>=MinDistanceBetweenRooms())
1395 END ;
1396 RETURN( ok )
1397 END DistanceAppartEdge ;
1398
1399
1400 (*
1401 DistanceAppartPoint - returns the distance appart between box, b,
1402 and point X, Y.
1403 *)
1404
1405 PROCEDURE DistanceAppartPoint (b: CARDINAL; X, Y: CARDINAL) : CARDINAL ;
1406 VAR
1407 Xmin,
1408 Ymin: CARDINAL ;
1409 BEGIN
1410 WITH Boxes[b] DO
1411 IF IsSubRange(x1, x2, X)
1412 THEN
1413 Ymin := Min( Abs(y1, Y), Abs(y2, Y) )
1414 ELSE
1415 Ymin := MaxCard
1416 END ;
1417 IF IsSubRange(y1, y2, Y)
1418 THEN
1419 Xmin := Min( Abs(x1, X), Abs(x2, X) )
1420 ELSE
1421 Xmin := MaxCard
1422 END
1423 END ;
1424 RETURN( Min(Xmin, Ymin) )
1425 END DistanceAppartPoint ;
1426
1427
1428 (*
1429 DistanceAppartBox - returns the distance appart between box, b,
1430 and box X1, Y1, X2, Y2
1431 *)
1432
1433 PROCEDURE DistanceAppartBox (b: CARDINAL; X1, Y1, X2, Y2: CARDINAL) : CARDINAL ;
1434 VAR
1435 Xmin,
1436 Ymin: CARDINAL ;
1437 BEGIN
1438 WITH Boxes[b] DO
1439 IF IsIntersectingRange(x1, x2, X1, X2)
1440 THEN
1441 Ymin := Min(
1442 Min( Abs(y1, Y1), Abs(y2, Y2) ),
1443 Min( Abs(y1, Y2), Abs(Y1, y2) )
1444 )
1445 ELSE
1446 Ymin := MaxCard
1447 END ;
1448 IF IsIntersectingRange(y1, y2, Y1, Y2)
1449 THEN
1450 Xmin := Min(
1451 Min( Abs(x1, X1), Abs(x2, X2) ),
1452 Min( Abs(x1, X2), Abs(X1, x2) )
1453 )
1454 ELSE
1455 Xmin := MaxCard
1456 END
1457 END ;
1458 RETURN( Min(Xmin, Ymin) )
1459 END DistanceAppartBox ;
1460
1461
1462 (*
1463 DistanceAppartDiagonal - returns the diagonal
1464 distance appart between X1, Y1, X2, Y2
1465 and box b.
1466 *)
1467
1468 PROCEDURE DistanceAppartDiagonal (b: CARDINAL;
1469 X1, Y1, X2, Y2: CARDINAL) : CARDINAL ;
1470 BEGIN
1471 WITH Boxes[b] DO
1472 RETURN(
1473 Min(
1474 Min( Abs(x1, X2), Abs(x2, X1) ),
1475 Min( Abs(y1, Y2), Abs(y2, Y1) )
1476 )
1477 )
1478 END
1479 END DistanceAppartDiagonal ;
1480
1481
1482 (*
1483 IsCorridorJoin - returns true if a box corridor x1, y1 x2, y2
1484 joins another corridor at right angles without
1485 cutting off the potential corridor door.
1486
1487 A corridor is thought of as
1488
1489 ##########################
1490 | |
1491 | |
1492 ##########################
1493
1494 and may only be placed together in a way such that
1495 they meet -| or - etc
1496 |
1497
1498 False is returned if this box corridor does not
1499 correctly form a T junction with another.
1500 *)
1501
1502 PROCEDURE IsCorridorJoin (x1, y1, x2, y2: CARDINAL) : BOOLEAN ;
1503 VAR
1504 ok,
1505 DoorFound: BOOLEAN ;
1506 b : CARDINAL ;
1507 BEGIN
1508 ok := TRUE ;
1509 IF NoOfBoxes=0
1510 THEN
1511 DoorFound := TRUE
1512 ELSE
1513 DoorFound := FALSE ;
1514 b := 1 ;
1515 WHILE ok AND (b<=NoOfBoxes) DO
1516 (* WriteString('Box') ; WriteCard(b, 2) ; *)
1517 IF IsTouchingBox(b, x1, y1, x2, y2)
1518 THEN
1519 (* WriteString('TouchingBox') ; *)
1520 IF IsDifferentOrientationBox(b, x1, y1, x2, y2)
1521 THEN
1522 (* WriteString('Different Orientation') ; *)
1523 IF NOT DoorFound
1524 THEN
1525 DoorFound := IsCorridorWallJoinBox(b, x1, y1, x2, y2)
1526 END
1527 (* ; IF ok THEN WriteString('WallJoin') END ; *)
1528 ELSE
1529 ok := FALSE (* Dont allow parallel corridors to touch *)
1530 END
1531 END ;
1532 INC(b)
1533 END
1534 END ;
1535 RETURN( ok AND DoorFound )
1536 END IsCorridorJoin ;
1537
1538
1539 (*
1540 IsRoomJoin - returns true if a box room x1, y1 x2, y2
1541 joins another room with enough space for a door.
1542 *)
1543
1544 PROCEDURE IsRoomJoin (x1, y1, x2, y2: CARDINAL) : BOOLEAN ;
1545 VAR
1546 DoorFound: BOOLEAN ;
1547 b : CARDINAL ;
1548 BEGIN
1549 IF NoOfBoxes=0
1550 THEN
1551 DoorFound := TRUE
1552 ELSE
1553 DoorFound := FALSE ;
1554 b := 1 ;
1555 WHILE (NOT DoorFound) AND (b<=NoOfBoxes) DO
1556 (* WriteString('Box') ; WriteCard(b, 2) ; *)
1557 IF IsTouchingBox(b, x1, y1, x2, y2)
1558 THEN
1559 IF NOT DoorFound
1560 THEN
1561 DoorFound := IsRoomWallJoinBox(b, x1, y1, x2, y2)
1562 END
1563 END ;
1564 INC(b)
1565 END
1566 END ;
1567 RETURN( DoorFound )
1568 END IsRoomJoin ;
1569
1570
1571 (*
1572 IsCorridorWallJoinBox - returns true if box, b, and box X1, Y1 X2, Y2
1573 form a correct join ie covering the potential
1574 door.
1575 *)
1576
1577 PROCEDURE IsCorridorWallJoinBox (b: CARDINAL;
1578 X1, Y1, X2, Y2: CARDINAL) : BOOLEAN ;
1579 VAR
1580 Success: BOOLEAN ;
1581 BEGIN
1582 WITH Boxes[b] DO
1583 Success := (((X1=x1) OR (X1=x2) OR (X2=x1) OR (X2=x2))
1584 AND IsSubLine(Y1, Y2, y1, y2)) OR
1585 (((Y1=y1) OR (Y1=y2) OR (Y2=y1) OR (Y2=y2))
1586 AND IsSubLine(X1, X2, x1, x2)) ;
1587 RETURN( Success )
1588 END
1589 END IsCorridorWallJoinBox ;
1590
1591
1592 (*
1593 IsRoomWallJoinBox - returns true if box, b, and box X1, Y1 X2, Y2
1594 form a correct join ie covering the potential
1595 door.
1596 *)
1597
1598 PROCEDURE IsRoomWallJoinBox (b: CARDINAL;
1599 X1, Y1, X2, Y2: CARDINAL) : BOOLEAN ;
1600 VAR
1601 DoorWidth: CARDINAL ;
1602 BEGIN
1603 DoorWidth := 0 ;
1604 WITH Boxes[b] DO
1605 IF (X1=x2) OR (x1=X2)
1606 THEN
1607 DoorWidth := IntersectionLength(Y1, Y2, y1, y2) ;
1608 IF (IsSubRange(Y1, Y2, y1) OR IsSubRange(y1, y2, Y1)) AND (DoorWidth>1)
1609 THEN
1610 DEC(DoorWidth)
1611 END ;
1612 IF (IsSubRange(Y1, Y2, y2) OR IsSubRange(y1, y2, Y2)) AND (DoorWidth>1)
1613 THEN
1614 DEC(DoorWidth)
1615 END
1616 ELSIF (Y1=y2) OR (y1=Y2)
1617 THEN
1618 DoorWidth := IntersectionLength(X1, X2, x1, x2) ;
1619 IF (IsSubRange(X1, X2, x1) OR IsSubRange(x1, x2, X1)) AND (DoorWidth>1)
1620 THEN
1621 DEC(DoorWidth)
1622 END ;
1623 IF (IsSubRange(X1, X2, x2) OR IsSubRange(x1, x2, X2)) AND (DoorWidth>1)
1624 THEN
1625 DEC(DoorWidth)
1626 END
1627 END ;
1628 RETURN( DoorWidth>=MinDoorLength )
1629 END
1630 END IsRoomWallJoinBox ;
1631
1632
1633 (*
1634 IsDifferentOrientationBox - returns true if box b has a different
1635 orientation to box X1, Y1 X2, Y2.
1636 *)
1637
1638 PROCEDURE IsDifferentOrientationBox (b: CARDINAL;
1639 X1, Y1, X2, Y2: CARDINAL) : BOOLEAN ;
1640 BEGIN
1641 WITH Boxes[b] DO
1642 IF Abs(X1, X2) = Abs(Y1, Y2)
1643 THEN
1644 RETURN( TRUE ) (* Square X1, Y1, X2, Y2 *)
1645 ELSIF Abs(X1, X2) > Abs(Y1, Y2)
1646 THEN
1647 RETURN( Abs(x1, x2) <= Abs(y1, y2) )
1648 ELSE
1649 RETURN( Abs(x1, x2) >= Abs(y1, y2) )
1650 END
1651 END
1652 END IsDifferentOrientationBox ;
1653
1654
1655 (*
1656 IsTouchingBox - returns true if a box X1, Y1 X2, Y2 touches box b
1657 or if it intersects with this box.
1658 *)
1659
1660 PROCEDURE IsTouchingBox (b: CARDINAL; X1, Y1, X2, Y2: CARDINAL) : BOOLEAN ;
1661 BEGIN
1662 WITH Boxes[b] DO
1663 RETURN( NOT ((X2<x1) OR (X1>x2) OR (Y2<y1) OR (Y1>y2)) )
1664 END
1665 END IsTouchingBox ;
1666
1667
1668 (*
1669 IsCornerPerimeter - returns true if box, b, has a corner x, y which
1670 is a perimeter.
1671 *)
1672
1673 PROCEDURE IsCornerPerimeter (b: CARDINAL; x, y: CARDINAL) : BOOLEAN ;
1674 VAR
1675 Perimeter: BOOLEAN ;
1676 i, j : CARDINAL ;
1677 BEGIN
1678 IF IsCorner(b, x, y)
1679 THEN
1680 Perimeter := FALSE ;
1681 i := x-1 ;
1682 j := y-1 ;
1683 WHILE (NOT Perimeter) AND (i<=x+1) DO
1684 j := y-1 ;
1685 WHILE (NOT Perimeter) AND (j<=y+1) DO
1686 IF IsSubRange(1, userX, i) AND IsSubRange(1, userY, j)
1687 THEN
1688 Perimeter := IsSpace(i, j)
1689 END ;
1690 INC(j, 2)
1691 END ;
1692 INC(i, 2)
1693 END ;
1694 RETURN( Perimeter )
1695 ELSE
1696 RETURN( FALSE )
1697 END
1698 END IsCornerPerimeter ;
1699
1700
1701 (*
1702 IsCorner - returns true if box, b, has a corner x, y.
1703 *)
1704
1705 PROCEDURE IsCorner (b: CARDINAL; x, y: CARDINAL) : BOOLEAN ;
1706 BEGIN
1707 WITH Boxes[b] DO
1708 RETURN( ((x1=x) OR (x2=x)) AND ((y1=y) OR (y2=y)) )
1709 END
1710 END IsCorner ;
1711
1712
1713 (*
1714 IsOverLappingBox - returns true if box X1, Y1 X2, Y2 overlaps
1715 with another box NOT including edges touching.
1716 *)
1717
1718 PROCEDURE IsOverLappingBox (X1, Y1, X2, Y2: CARDINAL) : BOOLEAN ;
1719 VAR
1720 b : CARDINAL ;
1721 Found: BOOLEAN ;
1722 BEGIN
1723 Found := FALSE ;
1724 b := 1 ;
1725 WHILE (NOT Found) AND (b<=NoOfBoxes) DO
1726 WITH Boxes[b] DO
1727 Found := IsIntersection(x1, y1, x2, y2, X1, Y1, X2, Y2)
1728 END ;
1729 INC(b)
1730 END ;
1731 RETURN( Found )
1732 END IsOverLappingBox ;
1733
1734
1735 (*
1736 IsIntersection - returns true if two boxes x1, y1 x2, y2 intersects
1737 with X1, Y1 X2, Y2. Wall touching is allowed.
1738 *)
1739
1740 PROCEDURE IsIntersection (x1, y1, x2, y2,
1741 X1, Y1, X2, Y2: CARDINAL) : BOOLEAN ;
1742 BEGIN
1743 RETURN( NOT ( (x2<=X1) OR (x1>=X2) OR (y2<=Y1) OR (y1>=Y2) ) )
1744 END IsIntersection ;
1745
1746
1747 (*
1748 AddBox - adds a box to the list of boxes and
1749 adds a box to the Map.
1750 *)
1751
1752 PROCEDURE AddBox (X1, Y1, X2, Y2: CARDINAL) ;
1753 BEGIN
1754 IF NoOfBoxes=MaxBoxes
1755 THEN
1756 WriteString('Too many boxes in Module MakeMap') ; WriteLn ;
1757 HALT
1758 ELSE
1759 INC(NoOfBoxes) ;
1760 WITH Boxes[NoOfBoxes] DO
1761 x1 := X1 ;
1762 y1 := Y1 ;
1763 x2 := X2 ;
1764 y2 := Y2
1765 END
1766 END
1767 END AddBox ;
1768
1769
1770 (*
1771 GetCh - waits for a character to be pressed.
1772 *)
1773
1774 PROCEDURE GetCh ;
1775 VAR
1776 ch: CHAR ;
1777 BEGIN
1778 Read(ch)
1779 END GetCh ;
1780
1781
1782 BEGIN
1783 Init
1784 END BoxMap.