data 0,1 , 200,1 , 440,1 , 680,2 , 880,2 , 1040,1 , 1200,1
data 1360,0 , 1520,0 , 1720,0 , 1880,3 , 1960,3 , 2040,4
dim rowdata%(12 , 1) , co%(12) , palcols%(79) , bitpatt$(3)
pi = 3.141592653589
bf = 1
rf = 1
gf = 1
threshold% = 10
cls
for rowcount% = 0 to 12
	read rowdata%(rowcount% , 0)
	read rowdata%(rowcount% , 1)
next rowcount%

f$ = "heights2.dat"
if dir$(f$) = "" then
	f$ = "d:" + f$
	if dir$(f$) > "" then exit if
	sound 500 , 2
	color 12 + 16
	print "Height database missing"
	end
end if
readfonts

open "b" , 1 , f$

'==========================================================

cls
locate 12 , 30
if command$ > "" then centrelo$ = command$ else input "Location    ",centrelo$
locate 3 , 1
sqsize% = 100
boxint% = 1
locate 6 , 1
maxh = 1500
colscale = 1
call LocationIn(centrelo$ , ecentre , ncentre)
ebase = ecentre
nbase = ncentre
call ENtoNGR(ebase , nbase , baselo$)
ecentre = 10 * (ecentre \ 10)
ncentre = 10 * (ncentre \ 10)			'nearest 10km square below left
eref% = ecentre - sqsize% \ 2
nref% = ncentre + (sqsize% - 20) \ 2			'top left of screen

do until nref% - sqsize% > -10
	nref% = nref% + 10
	ncentre = ncentre + 10
loop

xhome% = 2 * (ebase - eref%) + 10
yhome% = 2 * (nref% - nbase) + 20
homeaddr% = xhome% + yhome% * 320

n100% = ((ncentre - sqsize \ 2) \ 100)
estart% = rowdata%(n100% , 1) * 100

do until eref% > estart%
	eref% = eref% + 10
	ecentre = ecentre + 10
loop

estop% = max ((rowdata%(n100% + 1 , 0) - rowdata%(n100% ,0)) / .4 , estart% + sqsize%)

do until eref% <= (estop% - sqsize% + 100)
	eref% = eref% - 10
	ecentre = ecentre - 10
loop
'screen 12
call ENtoNGR(ecentre , ncentre , centrelo$)
def seg = &hA000
reg 1 , &h13
call interrupt &h10
call SetUpPalette

reg 1 , 2                  ' hide cursor
call interrupt &h33
for y% = 0 to 127
	col% = 255 - y% * 2
	for x% = 270 to 278
		adr& = x% + 320 * (y% + 3) + 36
		poke adr& , col%
	next x%
	if y% mod 16 = 0 or y% = 127 then
		hd = col% ^ 1.32
		call TextXY256(using$("####m" , hd) , 270 , y% + 8 , 128 , 1)
	end if
next y%
for ycount% = 0 to sqsize% * 1.2step 10
	for xcount% = 0 to sqsize% step 10
		e% = eref% + xcount%
		n% = nref% - ycount%        'count DOWN screen
		e10% = (e% \ 10) mod 10
		n10% = (n% \ 10) mod 10
		e100% = e% \ 100
		n100% = n% \ 100
		estart% = rowdata%(n100% , 1)
		filebreak& = rowdata%(n100% , 0) * 1000
		km100sq% = e100% - estart%
		km10sq% = n10% + e10% * 10
		offset10& = km100sq% * 40000  +  km10sq% * 400
		pointer& = offset10& + filebreak&
		if pointer& < 0 then pointer& = 0
		seek 1 , pointer&
		get$ 1 , 400 , ch400$
		for g% = 0 to 399
			ch1$ = mid$(ch400$ , g% + 1 , 1)
			col% = asc(ch1$)
			if h% > 0 and col% = 1 then col% = 2
			e1% = g% mod 20
			n1% = g% \ 20
			x% = (e1% + xcount% * 2) * boxint%
			y% = (n1% + ycount% * 2) * boxint%
			adr& = x% + 10 + 320 * y%
			poke adr& , col%
		next g%
		if inkey$ = chr$(27) then finish
	next xcount%
next ycount%

finish:
call TextXY256("Base "+ baselo$ , 240 , 200 , 128 , 1)
call TextXY256("Disp Colours" , 240 , 160 , 128 , 1)
call TextXY256("+/- P R G B N" , 240 , 170 , 128 , 1)
call TextXY256("F1 .BMP FIle" , 240 , 185 , 128 , 1)

basecol% = peek(homeaddr%)
on timer(.15) gosub flash
timer on

reg 1 , 0
call interrupt &h33
reg 1 , 1
'call interrupt &h33
do
	reg 1 , 3
	call interrupt &h33
	mx% = reg(3)
	my% = reg(4)
	ik$ = ucase$(inkey$)
	select case ik$
	case "P"
		palset% = (palset% + 1) mod 6
		call SetUpPalette
	case "N" , "X"
		rf = 1
		gf = 1
		bf = 1
		call SetUpPalette
	case "R"
		rf = rf * 1.05
		call SetUpPalette
	case chr$(18)
		rf = rf / 1.05
      call SetUpPalette
	case "G"
		gf = gf * 1.05
      call SetUpPalette
	case chr$(7)
		gf = gf / 1.05
      call SetUpPalette
	case "B"
		bf = bf * 1.05
      call SetUpPalette
	case chr$(2)
		bf = bf / 1.05
		call SetUpPalette
	case "+"
		bf = bf * 1.05
		rf = rf * 1.05
		gf = gf * 1.05
      call SetUpPalette
	case "-"
		bf = bf / 1.05
		rf = rf / 1.05
		gf = gf / 1.05
		call SetUpPalette
	case "T"
		threshold% = threshold% + 1
      call SetUpPalette
	case chr$(20)
		threshold% = threshold% - 1
		call SetUpPalette
	case chr$(0 ,59)  'f1
		call MakeBMP
	end select
loop until ik$ = chr$(27)
screen 0 , 0 , 0
end
'============================================================
flash:
	fc% = (fc% + 1) mod 3
	if fc% = 0 then fcol% = basecol%
	if fc% = 1 then fcol% = 255
	if fc% = 2 then fcol% = 0
	poke homeaddr% , fcol%
return
'============================================================
Sub LocationIn(lo$ , e , n)
	lo$ = ucase$(lo$)
	q1$="AA00AA  "
	q2$="RR99XX99"
	q3$="HA000000"
	q4$="YZ999999"
	whichtype%=0:notloc%=0:notngr%=0:found%=0
	for chk%=1 to len(lo$)
		a$=mid$(lo$,chk%,1)
		if a$ < mid$(q1$,chk%,1) or a$ > mid$(q2$,chk%,1) then notloc%=-1
		if a$ < mid$(q3$,chk%,1) or a$ > mid$(q4$,chk%,1) then notngr%=-1
	next chk%
	if notloc% and notngr% then
		found%=0
		OSITE$=lo$
		searchloop%=0
		close 3             ' In case
		open "i",3,"sites.dat"
		do
			incr searchloop%
			line input #3,readline$
			posn1%=instr(readline$,"~")
			posn2%=instr(posn1%+7,readline$,"~")
			sitename$=left$(readline$,posn1%-1)
			if posn2%=0 then
				stloc$=mid$(readline$,posn1%+1,8)
			else
				stloc$=mid$(readline$,posn1%+1,posn2%-posn1%-1)
			end if
			stloc$=ucase$(stloc$)
			if instr(stloc$,"~")>0 then stloc$=left$(stloc$,6)  ' 6 Digit loc stored
			b%=instr(ucase$(sitename$),osite$)
			IF B%>0 THEN
				found%=-1
				exit loop
			end if
			loop until eof(3)
		close 3
		if not found%  and dir$("auxsites.dat") > ""then
			open "i",3,"auxsites.dat"
			searchloop% = 0
			do
				incr searchloop%
				line input #3,readline$
				posn1%=instr(readline$,"~")
				posn2%=instr(posn1%+7,readline$,"~")
					sitename$=left$(readline$,posn1%-1)
				if posn2%=0 then
					stloc$=mid$(readline$,posn1%+1,8)
				else
					stloc$=mid$(readline$,posn1%+1,posn2%-posn1%-1)
					posnhgt% = instr(posn2% , ucase$(readline$) , "H")
					if posnhgt% > 0 then mastheight = val(mid$(readline$ , posnhgt% + 1))_
								else mastheight = 0
				end if

				stloc$=ucase$(stloc$)
				if instr(stloc$,"~")>0 then stloc$=left$(stloc$,6)  ' 6 Digit loc stored
				b%=instr(ucase$(sitename$),osite$)
				IF B%>0 THEN
					found%=-1
					exit loop
				end if
			loop until eof(3)
		end if
		lo$ = stloc$
		print " ";lo$
		if found%=0 then	sound 300,1 : end
	end if

	c6% = asc(mid$(lo$ , 6 , 1))
	c5% = asc(mid$(lo$ , 5 , 1))
	if c5% >= 48 and c5% <= 57 then
		HN%=VAL(MID$(lo$,6,3))
		HE%=VAL(MID$(lo$,3,3))
		T1%=ASC(MID$(lo$,1,1))-65:IF T1%>8 THEN T1%=T1%-1
		T2%=ASC(MID$(lo$,2,1))-65:IF T2%>8 THEN T2%=T2%-1
		E = 500 * (T1% MOD 5) + 100 * (T2% MOD 5) - 1000 + HE% / 10
		N = 1900 - 500 * (T1% \ 5) - 100 * (T2% \ 5) + HN% / 10
	else
		if len(lo$) <= 6 then lo$ = lo$ + "55"
		longd=20*(asc(mid$(lo$,1,1))-74)+2*(asc(mid$(lo$,3,1))-48)_
				+(asc(mid$(lo$,5,1))-65)/12_
				+(asc(mid$(lo$,7,1))-48)/120

		latd= 10*(asc(mid$(lo$,2,1))-74)+asc(mid$(lo$,4,1))-48_
				+(asc(mid$(lo$,6,1))-65)/24_
				+(asc(mid$(lo$,8,1))-48)/240

		DR = 180/3.141592654
		LATR = LATD / DR
		LO = LONGD / DR
		Y = SQR(1 - .00667054# * SIN(LATR) * SIN(LATR))
		N = -100 + 111.0793 * (LATD - 49)
		N = N - 31.947 * SIN((LATD - 49) / DR) * COS(LATR + 49 / DR) + .971 * (2 + LONGD) ^ 2 * SIN(LATR) * COS(LATR) / Y
		E = 400 + 111.2651 * (2 + LONGD) * COS(LATR) / Y
		E = E - .0056 * (2 + LONGD) ^ 3 * (COS(LATR)) ^ 3 * ((TAN(LATR)) ^ 2 - 1)
	end if
end sub

sub GetHeight(ein , nin, h%)
	shared rowdata%()
	'need results in metres!!
	e = ein * 1000
	n = nin * 1000
	n100% = n \ 100000       '100 km row
	e100% = e \ 100000
	n10% = n \ 10000 mod 10
	e10% = e \ 10000 mod 10

	npoint% = n \ 500 mod 20
	epoint% = e \ 500 mod 20

	estart% = rowdata%(n100% , 1)
	filebreak& = rowdata%(n100% , 0) * 1000

	km100sq% = e100% - estart%
	km10sq% = n10% + e10% * 10

	offset10& = km100sq% * 40000  +  km10sq% * 400

	fineoffset% = epoint% + 380 - 20 * npoint%

	offset& = offset10& + fineoffset%
	pointer& = offset& + filebreak&

	seek 1 , pointer&
	get$ 1 , 1 , ch1$
	h% = asc(ch1$)
end sub

sub ENtoNGR(e,n,ngr$)
	 e1% = e \ 100
	 n1% = n \ 100
	 c1% = 83 - 5 * (n1% \ 5) + (e1% \ 5)
	 if c1%=73 then c1%=72
	 eh%=(e1% mod 5)
	 nh%=(n1% mod 5)
	 c2%=(4-nh%)*5+eh%+65
	 IF c2%>72 then incr c2%
	 e2&=(e - e1% * 100) * 10
	 n2&=(n - n1% * 100) * 10
	 ce$=mid$(STR$(e2&),2)
	 cn$=mid$(STR$(n2&),2)
	 while len(ce$)<3
		ce$="0"+ce$
	 wend
	 while len(cn$)<3
		cn$="0"+cn$
	 wend
	 NGR$=CHR$(C1%)+CHR$(C2%)+CE$+CN$
end sub

sub SetUpPalette
	shared  bf , rf , gf , palset%
	out &h3c6 , &hff
	for i% = 1 to 255
		select case palset%
		case 5
			r% = min((i% \ 4) * rf * 3 , 63)
			g% = min((i% \ 4) * gf * 3, 63)
			b% = min((i% \ 4) * bf * 3, 63)
		case 1
			if i% * rf > 60 then r% = min ((rf * i% - 60) , 63) else r% = 0
			if i% * gf > 15 then g% = min((gf * i% - 15)  , 63)
			if i% * gf> 120 then g% = max((183 - gf * i%)  , 0)
			if i% * gf> 200 then g% = min((gf * i% - 200) , 63)
			b% = min(bf * i%  , 63)
			if i% * bf> 50 then b% = max((113 - bf * i%) , 0)
			if i% * bf > 180 then b% = min((bf * i% - 180) , 63)
		case 2
			r% = 0
			if i% * rf >= 36 and i% < 144 then r% = min((rf * i% - 36) * 1.5 , 56) else r% = 0
			if i% * rf >= 144 and i% < 180 then r% = 56 - (rf * i% - 144) * 1.5
			if i% * rf >= 216 then r% = min((rf * i% - 216) * 1.5 , 56)
			g% = 0
			if i% * gf >= 108 then g% = min((gf * i% - 108) * 1.3, 50)
			b% = 0
			if i% * bf < 72 then b% = min(bf * i% * 1.6 + 10 , 63)
			if i% * bf >= 72 then b% = max((108 - bf * i%) * 1.75 , 0)
			if i% * bf >= 180 then b% = min((bf * i% - 180) * 1.75 , 63)
		case 3
			if i% * bf < 128 then b% = (128 - i% * bf) / 8 else b% = (i% * bf - 128) / 2
			r% = rf * i% / 2
			g% = gf * i% / 4
		case 4
			if i% * bf < 16 then b% = 3 * bf * (16 - i%)
			if i% * bf > 200 then b% = 3 * bf * (i% - 200)
			r% = 3 * rf * (rf * i% \ 8)
			g% = 3 * gf * (gf * i% \ 4)
		case 0
			if i% * rf > 110 then r% = min ((rf * i% - 111) , 63) else r% = 0
			if i% * gf > 40 then g% = min((gf * i% - 40)  , 63)
			if i% * gf > 195 then g% = max((258 - gf * i%)  , 0)
			b% = min((bf * i%) , 63)
			if i% * bf > 63 then b% = max((128 - bf * i%) , 0)
		end select
		if r% < 0 then r% = 0
		if g% < 0 then g% = 0
		if b% < 0 then b% = 0
		if r% > 63 then r% = 63
		if g% > 63 then g% = 63
		if b% > 63 then b% = 63
		out &h3c8 , i%
		out &h3c9 , r%
		out &h3c9 , g%
		out &h3c9 , b%
	next i%
end sub




prop:
	howfar% = (39 * xcount%) \ sqsize%
	locate 12 , 1
	print string$(howfar% + 1 , 219) + string$(79 - howfar% , 176)
return

sub distBear (he , hn , de , dn , dist , bearing)
	shared pi
	x = de - he
	y = dn - hn
	dist = sqr(x * x + y * y)
	if dist = -x then
		angle = pi / 2
	else
		angle = 2 * atn(y / (x + dist))
	end if
	bearing = 90 - angle * 180 / pi
	if bearing < 0 then bearing = bearing + 360
	if bearing > 360 then bearing = bearing - 360
end sub

sub ENtoLoc (e , n , locator$)
	dr = 180 / 3.141593
	T1=(N+5548.79)/6371.28
	T2=2*ATN(EXP((E-400)/6389.7))
	LONG=ATN(-COS(T2)/(COS(T1)*SIN(T2)))-2/DR
	LA=SIN(T1)*SIN(T2)
	LAT=ATN(LA/SQR(1-LA*LA))
	latd = lat*dr
	longd=long*dr
	locator$=chr$(65+int((longd+180)/20))+chr$(65+int((latd+90)/10))+_
	chr$(48+int((longd+180)/2 mod 10))+chr$(48+int((latd+90) mod 10))+_
	chr$(65+int((longd+180)*12 mod 24))+chr$(65+int((latd+90)*24 mod 24))+_
	chr$(48+int((longd+180)*120 mod 10))+chr$(48+int((latd+90)*240 mod 10))
end sub

sub textXY256(text$ , x% , y% , colour% , fontno%)
	DEF SEG = &hA000
	shared ysize% , bitpatt$()
	select case fontno%
	case 1 , 5 , 7                '7x5 font
		rowsperchar% = 8
		colsperchar% = 6
		arrayref% = 0
	case 2 , 8
   	rowsperchar% = 8
		colsperchar% = 8
		arrayref% = 1
	case 3 , 14
		rowsperchar% = 14
		colsperchar% = 8
		arrayref% = 2
	case 4 , 16
		rowsperchar% = 16
		colsperchar% = 8
		arrayref% = 3
	end select

	for charcount% = 1 to len(text$)
		ch% = asc(mid$(text$ , charcount% , 1))
		xbase% = x% + colsperchar% * (charcount% - 1)
		for rw% = 0 to rowsperchar% - 1
			thisch% = asc(mid$(bitpatt$(arrayref%) , 1 + rw% + ch% * rowsperchar% , 1))
			for bitno% = 0 to 7
				if bit(thisch% , 7 - bitno%) then
					adr& = xbase% + bitno% + 320 * (y% + rw% - rowsperchar%)
					poke adr& , colour%
				end if
			next bitno%
		next rw%
	next charcount%
end sub

sub ReadFonts
	shared bitpatt$()
	fino% = freefile
	open "b" , fino% , "allfonts"
	get$  fino% , 2048 , bitpatt$(0)
	get$  fino% , 2048 , bitpatt$(1)
	get$  fino% , 3584 , bitpatt$(2)
	get$  fino% , 4096 , bitpatt$(3)
	close fino%
end sub

sub MakeBMP
	basename$ = "LCL25"
	filename$ = dir$(basename$ + "???.bmp")
	n% = val(mid$(filename$ , 6, 3))
	do
		f$ = dir$
		n% = max(val(mid$(f$ , 6, 3)) , n%)
	loop until f$ = ""
	n$ = mid$(str$(min(n% + 1, 999)) , 2)
	do while len(n$) < 3
		n$ = "0" + n$
	loop
	f$ = basename$ + n$ + ".BMP"
	bwidth& = 320
	bheight& = 200
	ColoursInTable& = 256
	BitsPerPixel% = 8        '1 / 4 / 8 / 24  only
	NPlanes% = 1             '1 only
	InfoSize& = 40

	ImageSize& = bwidth& * bheight& * BitsperPixel% / 8
	dataoffset& = 62 + ColoursInTable& * 4
	filesize& = dataoffset& + ImageSize&

	fino% = freefile
	open "b" , fino% , f$

	bm$ = "BM"
	put$ fino% , bm$
	put fino% , 2 , filesize&
	put fino% ,, i1%
	put fino% ,, i2%
	put fino% ,, DataOffset&
	put fino% ,, InfoSize&
	put fino% ,, bwidth&
	put fino% ,, bheight&
	put fino% ,, NPlanes%
	put fino% ,, BitsPerPixel%
	put fino% ,, compression&
	put fino% ,, ImageSize&
	put fino% ,, XPelsPerMetre&
	put fino% ,, YPelsPerMetre&
	put fino% ,, ColoursInTable&
	put fino% ,, ImportantColours&
	seek fino% , 54
	for c% = 0 to coloursintable& - 1
		reg 1 , &h1015
		reg 2 , c%
		call interrupt &h10
		g% = reg(3) \ 256
		b% = reg(3) mod 256
		r% = reg(4) \ 256
		cd$ = chr$(4 * b%) + chr$(4 * g%) + chr$(4 * r%) + chr$(0)
		put$ fino% , cd$
	next c%
	seek fino% , dataoffset&      'data starts at bottom left of picture
	for y& = bheight& -1 to 0 step -1
		dat$ = peek$(y& * bwidth& , 320)
		put$ fino% , dat$
	next y&
	close fino%
	sound 1750 , 2
end sub