procedure SortDistances( var D : distancearray; largest : sector ); var smallest : dist; where : sector; s, t : sector; begin for s := 1 to largest - 1 do begin smallest := d[s]; where := s; for t := s + 1 to largest do if smallest.d > d[t].d then begin smallest := d[t]; where := t; end; {for t} d[where] := d[s]; d[s] := smallest; end; {for s} end; {Sort Distances} function SetupDistances : boolean; { return false if they aborted } var s, n : integer; begin s := GetSector; if s <> 0 then begin TwoWayDistances( s, distances, false, true ); for n := 1 to maxSector do distances[ n ].s := n; SetUpDistances := true; end {if} else SetUpDistances := false; end; {SetUpDistances} function DisplayPort( WhichPort : PortPtr ) : string; begin if WhichPort = 0 then DisplayPort := 'Port data not found!' else with space.ports.data[ WhichPort ] do DisplayPort := ' F:' + str(amts[ fuel ], 4) + ' O:' + str( amts[ organics], 4) + ' E:' + str( amts[ equipment ], 4 ); end; {DisplayPort} function DisplayNote( s : sector ) : string; { print the note associated with sector s } var n : 0..MaxNote; begin n := NoteNumber( s ); if n = 0 then Displaynote := ' Missing note! ' else Displaynote := ' ' + space.notes.data[n].info; end; {write note} procedure DisplaySector( s : sector; dist : integer; ToFile : boolean; var TheFile : text); var line : string; begin with space.sectors[s] do begin line := 'Sctr:' + str( s, 3); if dist <> Error then line := line + ' Dist:' + str( dist, 2 ); if number = UnExplored then line := line + ' Unexplored sector'; if portType <> NotAPort then begin line := line + ' port type ' + status( portType ); if portType <> Class0 then line := line + DisplayPort( portnumber( s ) ); end; {if port} if (etc and SpaceLane) <> Nothing then line := line + ' space lane'; if (etc and StarDock) <> Nothing then line := line + ' Star Dock'; if (etc and HasFighters) <> Nothing then line := line + ' has fighters'; if (etc and NoteExists) <> Nothing then line := line + DisplayNote( s ); if number = 1 then if AppearanceCount(s) = 1 then line := line + ' dead end (' + str(HowFar( s ),1) + ')'; writeln( line); if ToFile then writeln( TheFile, line ); end; {with} end; {DisplaySector} type WhichStuff = (any, PortOnly, NoteOnly, UnExpOnly ); procedure EliminateUnwanted( var distances : distanceArray; removeMe : WhichStuff; var HowManyLeft : integer ); { Remove the sectors you don't want in the list by pushing them to the back} var whichSector : sector; keep : boolean; begin HowManyLeft := 0; for whichSector := 1 to MaxSector do begin case removeMe of any : keep := distances[ whichSector ].d <> maxint; PortOnly : keep := space.sectors[whichSector].porttype <> NotAPort; NoteOnly : keep := space.sectors[whichSector].etc and NoteExists <> Nothing; UnExpOnly: keep := (space.sectors[whichSector].number = UnExplored) and (distances[ whichSector ].d <> maxint); end; {case} if keep then begin HowManyLeft := HowManyLeft + 1; distances[ howManyLeft ] := distances[ whichSector ]; end; {if} end; {for} write('Total of ', HowManyLeft ); case removeMe of any : writeln(' sectors from base sector'); PortOnly : writeln(' known ports'); NoteOnly : writeln(' sectors with notes'); UnExpOnly: writeln(' unexplored sectors with known position'); end; {case} end; {EliminateUnwanted} procedure FindSmallest( var distances : DistanceArray; lowest, highest : sector ); { Examine the distance array from lowest to highest, and move the sector at smallest distance into the "lowest" slot. } var temp : dist; s, smallest : sector; begin Smallest := lowest; for s := lowest to highest do if distances[ s ].d < distances[ smallest ].d then smallest := s; temp := distances[ smallest ]; distances[ smallest ] := distances[ lowest ]; distances[ lowest ] := temp; end; {FindSmallest} procedure nearestStuff( Filter : WhichStuff ); var Number, s, n : integer; log : boolean; f : text; begin if SetUpDistances then begin EliminateUnwanted( distances, filter, Number ); log := prompt( 'Log to disk? '); if log then begin assign( f, GetNewFileName('File name for report? ', 'report.html') ); rewrite( f ); end; {if} if Number <> 0 then begin for n := 1 to Number do begin FindSmallest( distances, n, Number ); DisplaySector( distances[ n ].s, distances[n].d, log, f ); if n mod 20 = 0 then if not prompt( 'more? ') then begin if log then close( f ); exit; end; end; {for} end; {if} if log then close( f ); end; {if} end; {nearport} procedure SortPorts( var NumPorts : integer ); var s, n : integer; begin NumPorts := 0; if SetUpDistances then begin EliminateUnwanted( distances, PortOnly, NumPorts ); writeln('Sorting...'); SortDistances( distances, NumPorts ) end; {if} end; {SortPorts}