1 (* Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010
2 Free Software Foundation, Inc. *)
3 (* This file is part of Chisel.
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
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
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. *)
19 IMPLEMENTATION MODULE StoreCoords ;
22 FROM MapOptions IMPORT isVerbose ;
23 FROM StrIO IMPORT WriteString, WriteLn ;
24 FROM NumberIO IMPORT WriteCard ;
25 FROM Chance IMPORT GetRand ;
39 Start, (* Start of the Coord list *)
40 End : CARDINAL ; (* End of the Coord list *)
44 CoordIndex : ARRAY [0..MaxIndex] OF Index ;
45 Coords : ARRAY [1..MaxCoord] OF Coord ;
46 NoOfCoords : CARDINAL ; (* Number of coordinates in array Coords *)
47 NoOfIndices: CARDINAL ; (* Number of indices in CoordIndex *)
51 InitCoords - Initializes a potential list of coordinates.
52 An index to this potential coordinate list is returned.
55 PROCEDURE InitCoords () : CARDINAL ;
57 IF NoOfIndices=MaxIndex
59 WriteString('too many coordinate list indices in Module StoreCoords') ;
61 WriteString('increase MaxIndex') ;
66 WITH CoordIndex[NoOfIndices] DO
67 Start := NoOfCoords+1 ;
70 AddCoord(NoOfIndices, 0, 0) ; (* Dummy coordinate that we keep *)
71 RETURN(NoOfIndices) (* for the life of this list. *)
77 KillCoords - Kills a complete coordinate list.
80 PROCEDURE KillCoords (CoordListIndex: CARDINAL) ;
84 (* Destroy index to Coord list *)
85 WITH CoordIndex[CoordListIndex] DO
88 WriteString('No of coords') ; WriteCard(End-Start+1, 4) ; WriteLn
94 If killed last Coord list see if we can garbage collect
95 previously killed middle indices.
97 IF NoOfIndices=CoordListIndex
101 UNTIL (NoOfIndices=0) OR (CoordIndex[NoOfIndices].Start#0)
103 NoOfCoords := CoordIndex[NoOfIndices].End
105 WriteString('all Coordinate lists have been killed - Module StoreCoords') ;
114 AddCoord - places a coordinate into the specified list.
117 PROCEDURE AddCoord (CoordListIndex: CARDINAL; x, y: CARDINAL) ;
119 IF NoOfCoords=MaxCoord
121 WriteString('too many coordinates in a list in Module StoreCoords') ;
123 WriteString('increase MaxCoord') ;
126 ELSIF UniqueCoord(CoordListIndex, x, y)
129 WITH Coords[NoOfCoords] DO
133 WITH CoordIndex[CoordListIndex] DO
141 UniqueCoord - returns true if x and y are unique in the coord list.
144 PROCEDURE UniqueCoord (CoordListIndex: CARDINAL;
145 x, y: CARDINAL) : BOOLEAN ;
150 WITH CoordIndex[CoordListIndex] DO
153 WHILE (NOT Found) AND (i<=End) DO
155 Found := (X=x) AND (Y=y)
165 GetAndDeleteRandomCoord - Returns a random coordinate from the coordinate
166 list and then it is deleted from the list.
169 PROCEDURE GetAndDeleteRandomCoord (CoordListIndex: CARDINAL;
170 VAR x, y: CARDINAL) ;
174 WITH CoordIndex[CoordListIndex] DO
175 i := Start+GetRand(End-Start+1) ; (* +1 for GetRand *)
186 UNTIL (j=i) OR (Coords[j].X#0) ;
190 X := 0 ; (* Now delete this box *)
194 END GetAndDeleteRandomCoord ;
198 CoordsExist - returns true if a coordinate exists
199 within the CoordListIndex.
202 PROCEDURE CoordsExist (CoordListIndex: CARDINAL) : BOOLEAN ;
208 WITH CoordIndex[CoordListIndex] DO
211 (* Was at least one coordinate *)
213 WHILE (NOT ok) AND (i<=End) DO
214 ok := (Coords[i].X#0) ; (* #0 means coordinate still exists *)
227 WITH CoordIndex[NoOfIndices] DO