REM This program tries to join fragmented lines from the CD ROM files
REM it goes through the whole file multiple times.  Starting with each
REM line segment, trying to find matches...

Numpts = 7900
NumLines = 2800

REM DOESNT YET WRITE OUT POINTS IN COMPACT FORMAT...

DIM LNS(NumLines) AS STRING * 16
DIM Lptr%(NumLines)
DIM OLNS(NumLines) AS STRING * 16
DIM x%(Numpts), y%(Numpts)
DIM a%(Numpts), b%(Numpts)

'   nmp, nmpo is total number of map points
'   LNi, LNo  is total number of lines
'    is pointer to OUTPUT array

FO$ = "MAPTEMP.map"
PRINT "Output will be send to "; FO$
PRINT
INPUT "Enter name of source file"; FI$
IF INSTR(FI$, ".") = 0 THEN FI$ = FI$ + ".map"
CLS

Loadfile: OPEN FI$ FOR INPUT AS #1
          OPEN FO$ FOR OUTPUT AS #2
          LNi = 0
          FOR i = 1 TO 8
              LINE INPUT #1, a$
              PRINT #2, a$
          NEXT i: i = 0
          DO WHILE NOT EOF(1)
             i = i + 1: INPUT #1, x%(i), y%(i)
             IF x%(i) = 0 AND NOT EOF(1) THEN ' Get line color & store with x=0
                IF y%(i) = -1 THEN EXIT DO ' All labels listed at end of file
                INPUT #1, y%(i)
                LNi = LNi + 1: Lptr%(LNi) = i 'Save Pointer to line
                LINE INPUT #1, LNS(LNi)       'and line name
             END IF
          LOOP: nmp = i  'nmp points to 0,-1 at end, but x% and y% are 0,0
          CLOSE #1: REM first X% value is map color.  2nd val is 1st pt

Begin: OutLn = 0: nmpo = 1
       j = 0
       DO UNTIL j >= LNi - 1' Starting with first line, to all lines
         j = j + 1
         REM PRINT "j="; j; " Doing "; LNS(j); "...";
         OutLn = OutLn + 1: OLNS(OutLn) = LNS(j)
         x = Lptr%(j): GOSUB CpyOutLine 'finishes with x-> to next line
         Endx% = x%(x - 1): Endy% = y%(x - 1)
         Clr = y%(Lptr%(j))
         FOR i = j + 1 TO LNi
           REM PRINT "    looping for i="; i;
           IF Clr = y%(Lptr%(i)) THEN
              REM PRINT "color match"
              IF Endx% = x%(Lptr%(i) + 1) AND Endy% = y%(Lptr%(i) + 1) THEN
                 REM Last point of j = first point of i
                 PRINT "j="; j; "<=>";
                 x = Lptr%(i) + 2: GOSUB CpyOutLine
                 Endx% = x%(x - 1): Endy% = y%(x - 1)
                 GOSUB DelLine
              END IF
           ELSE REM PRINT "no "
           END IF
         NEXT i
       LOOP 'j
       CLOSE #1
       GOSUB SaveAB
       PRINT "De-Fragged map is now saved in "; FO$
       END

CpyOutLine: REM PRINT "            Outputting points";
Cagn:  REM PRINT nmpo;
       a%(nmpo) = x%(x)
       b%(nmpo) = y%(x)
       nmpo = nmpo + 1: x = x + 1
       IF x%(x) <> 0 THEN GOTO Cagn
       REM PRINT
       RETURN
              
DelLine: Ptr = Lptr%(i) ' Points to beginning of this line
         PRINT Ptr
         Count = Lptr%(i + 1) - Lptr%(i)
         IF Lptr%(i + 1) <> 0 THEN
            FOR z = Lptr%(i + 1) TO nmp
                x%(Ptr) = x%(z): y%(Ptr) = y%(z): Ptr = Ptr + 1
            NEXT z: nmp = nmp - Count
            LNi = LNi - 1
            FOR L = i TO LNi
                LNS(L) = LNS(L + 1)       'Bubble down the names
                Lptr%(L) = Lptr%(L + 1) - Count'Bubble down the pointers to the lines
            NEXT L
         END IF
         RETURN

SaveAB: j = 0
        FOR i = 1 TO nmpo - 1'nmpo actually points to NEXT line to be used
           IF a%(i) = 0 THEN
              j = j + 1
              PRINT #2, "0,0"
              PRINT #2, b%(i); ","; OLNS(j)
           ELSE PRINT #2, a%(i); b%(i)
           END IF
        NEXT i
        PRINT #2, "0,-1"
        CLOSE #2
        RETURN

