{$ifndef unix}
{$A+,B-,D+,E+,F-,G+,I+,L+,N+,O+,P+,Q-,R-,S+,T-,V-,X+,Y+}
{$else}
{$B-,I+,S-,T-,X+}
{$endif}
unit menus2;

interface

procedure basemenu(rasse,id,x,y,z,Location:longint);

implementation

uses
 tstring,
 types,
 utils,
 kb,
 evgraph,
{$ifndef unix}
 crt,
{$else}
 xcrt,
{$endif}
 grafik,
 data,
 maus2kb,
 schedule,
 listen,
 menus1,
 menus3,
 menus4,
 menus5,
 menus6;

const
	data_beams:^t_beams=@beams;
	data_torps:^t_torps=@torps;
	data_mines:^t_torps=@mines;

procedure basemenu(rasse,id,x,y,z,Location:longint);
var
 sd,
 md:t_bdata;

 IsShip,
 IsOwnShip,
 IsOpponent,
 tIsShip,
 tIsOpponent,
 CanFight:boolean;
 Missionen:word;
 Showthing,
 tshowthing:word;

 PlanetNr,
 tPlanetNr,

 tBaseNr,

 shipnr,
 tshipnr,

 oshipnr,
 toshipnr:longint;

 planet,
 tplanet:tp_planet;

 base,
 tbase:tp_starbase;

 ship,
 oship,
 tship,
 toship:tp_schiff;

 redraw:byte;
 dx,
 dy,
 dz,
 dt,
 targetx,
 targety,
 targetz:sint;
 s:string;
 c:char;
 cs:byte;

 procedure missionselect;
 var
  i:byte;
  d:t_bdata;
 begin
  initb(d,8,15*18,21,11,15,8);
  window(6,15*18-2,9+21*8,16*18+1+10*16);
  writeg(d,#12'Option:');
  for i:=0 to 9 do writeg(d,#13#10#0'C'#$8c+char(i+48)+':'#0'C'#$8e+bmissionen[i]);
  repeat
   i:=byte(kbreadkey);
   if i=0 then kbreadkey
   else if byte(i-48)<=9 then begin
    base^.mission1:=i-48;
    exit;
   end else if i=27 then exit;
  until false;
 end;

 procedure buildship;
 var
  o1,o2,o3:t_bdata;
  h,
  h1,
  h2,
  h3:array[0..159] of byte;
  nh,
  nh1,
  nh2,
  nh3,
  nx,

  maxe,
  maxb,
  maxt:byte;

  ub:integer;
  cr,
  mo,
  du,
  tr:^longint;

  i,j:integer;
  g:word;

  x,
  p,p3,
  rd:byte;
  y1,y2:integer;

  function gewicht:word;
  begin
   with base^.build
   do with hulls[hullid]
   do gewicht:=masse+egewicht(engid)*enganz+
	baygewicht*bays+mlegergewicht*mines+
	bgewicht*beams+tgewicht(torprtyp)*torprs
	+(maxcrew+99) div 100;
  end;


 begin
  if planetnr<>-1 then with planet^ do begin
   cr:=@creds;
   mo:=@minedmolyb;
   du:=@mineddura;
   tr:=@minedtrit;
  end else with base^ do begin
   cr:=@creds;
   mo:=@molyb;
   du:=@dura;
   tr:=@trit;
  end;
  maxe:=0;
  maxb:=0;
  maxt:=0;
  for i:=9 downto 1 do if engines[i].techlvl<=base^.teche and $f then begin
   maxe:=i;
   break;
  end;
  for i:=9 downto 1 do if beams[i].techlvl<=base^.techb and $f then begin
   maxb:=i;
   break;
  end;
  for i:=9 downto 1 do if torprohre[i].techlvl<=base^.techtl and $f then begin
   maxt:=i;
   break;
  end;
  with base^ do with build do begin
   clearscreen(0);
   x:=0;
   if hullid=255 then y1:=-1 else with hulls[hullid] do begin
    inc(cr^,costcred);
    inc(mo^,costmolyb);
    inc(du^,costdura);
    inc(tr^,costtrit);
    inc(stml,mines);
    inc(stba,bays);
    inc(storage[0,engid],enganz);
    inc(storage[1,beamtyp],beams);
    inc(storage[2,torprtyp],torprs);
   end;
   y2:=0;
   initb(o1,8,8,20,29,15,0);
   initb(o2,173,8,26,29,15,0);
   initb(o3,386,8,30,29,15,0);
   window(6,6,627,473);
   vline(169,7,472,$f,0);
   vline(382,7,472,$f,0);

   repeat
    nh:=0;
    nh1:=0;
    nh2:=0;
    nh3:=0;
    for i:=0 to 159
    do with hulls[i] do if (rassenflags and (longint(1) shl rasse)<>0) then begin
     if (costcred<=cr^)and(costmolyb<=mo^)and(costdura<=du^)and(costtrit<=tr^)
     and(techlvl<=techH and $f)
     then begin
      h[nh]:=i;
      inc(nh);
     end else begin
      h1[nh1]:=i;
      inc(nh1);
     end;
    end else begin
     if (canbuildtyp(base,i)) then begin
      if (costcred<=cr^)and(costmolyb<=mo^)and(costdura<=du^)and(costtrit<=tr^)
      and(techlvl+1<techH and $f)
      then begin
       h2[nh2]:=i;
       inc(nh2);
      end else begin
       h3[nh3]:=i;
       inc(nh3);
      end;
     end;
    end;
    nx:=nh+nh1+nh2+nh3;

    y1:=-1;
    if hullid<>255 then begin
     for i:=0 to nh do if h[i]=hullid then begin
      y1:=i;
      break;
     end;
     if y1=-1 then for i:=0 to nh1-1 do if h1[i]=hullid then begin
      y1:=i+nh;
      break;
     end;
     if y1=-1 then for i:=0 to nh2-1 do if h2[i]=hullid then begin
      y1:=i+nh+nh1;
      break;
     end;
     if y1=-1 then for i:=0 to nh3-1 do if h3[i]=hullid then begin
      y1:=i+nh+nh1+nh2;
      break;
     end;
    end;

    repeat
     p:=(y1+1) div 27;
     writeg(o1,#0'C'#$f#12'Schiffstyp:');
     for i:=-1 to 25 do if i+p*27<nx then begin
      if (i+p*27>=nh)and(i+p*27<nh+nh1)or(i+p*27>=nh+nh1+nh2)
      then begin
       if i+p*27=y1 then begin
	if x=0 then writeg(o1,#13#10#0'C'#$27)
	else writeg(o1,#13#10#0'C'#$07)
       end else writeg(o1,#13#10#0'C'#$7);
      end else begin
       if i+p*27=y1 then begin
	if x=0 then writeg(o1,#13#10#0'C'#$2e)
	else writeg(o1,#13#10#0'C'#$0e)
       end else writeg(o1,#13#10#0'C'#$e);
      end;
      if i+p=-1 then writeg(o1,'kein Schiff')
      else if i+p*27<nh         then writeg(o1,hulls[h [i+p*27]].name)
      else if i+p*27<nh+nh1     then writeg(o1,hulls[h1[i+p*27-nh]].name)
      else if i+p*27<nh+nh1+nh2 then writeg(o1,hulls[h2[i+p*27-nh-nh1]].name)
      else 			    writeg(o1,hulls[h3[i+p*27-nh-nh1-nh2]].name);
     end else writeg(o1,#13#10);
     writeg(o1,#13#10#0'C'#$a'P'+char(p+49)+'/'+char((nx+27) div 27+48));
     repeat
      if y1<0 then writeg(o2,#0'C'#$f#12)
      else begin
       if y1<nh		    then hullid:=h [y1]
       else if y1<nh+nh1     then hullid:=h1[y1-nh]
       else if y1<nh+nh1+nh2 then hullid:=h2[y1-nh-nh1]
       else			 hullid:=h3[y1-nh-nh1-nh2];
       with hulls[hullid] do begin
	if (engid<1)or(engid>maxe) then engid:=1;
	if (beamtyp<1)or(beamtyp>maxb) then beamtyp:=1;
	if (torprtyp<1)or(torprtyp>maxt) then torprtyp:=1;

	if (x<>2)and((x<>1)or(y2<>0))and(storage[0,engid]<enganz)then for i:=9 downto 1
	do if storage[0,i]>=enganz then begin
	 engid:=i;
	 break;
	end; { Antriebe einstellen }

	if maxb=0 then beams:=0
	else begin
	 if beams>25 then beams:=25;
	 if beams>storage[1,beamtyp] then beams:=storage[1,beamtyp];
	end;
	if torprs>25 then torprs:=25;
	if torprs>storage[2,torprtyp] then torprs:=storage[2,torprtyp];
	if bays>25 then bays:=25;
	if bays>stba then bays:=stba;
	if mines>25 then mines:=25;
	if mines>stml then mines:=stml;

	if genflags and 1<>0 then begin
	 if mines>maxmines then mines:=maxmines;
	 repeat
	  ub:=(beams-maxbeams)*2+(torprs-maxtorps)*3+(bays-maxbays)*4;
	  if ub>0 then begin
	   case y2 of
	    2:if torprs>0 then dec(torprs)
	      else if bays>0 then dec(bays)
	      else if beams>0 then dec(beams);
	    3:if bays>0 then dec(bays)
	      else if torprs>0 then dec(torprs)
	      else if beams>0 then dec(beams);
	   else
	    if torprs>0 then dec(torprs)
	    else if bays>0 then dec(bays)
	    else dec(beams);
	   end;
	  end;
	 until ub<=0
	end else if genflags and 4=0 then begin
	 if torprs>maxtorps then torprs:=maxtorps;
	 if beams>maxbeams then beams:=maxbeams;
	 if bays>maxbays then bays:=maxbays;
	 if mines>maxmines then mines:=maxmines;
	end else if (maxtorps=0)and(maxbeams=0)and(maxbays=0)and(maxmines=0)
	then begin
	 torprs:=0;
	 mines:=0;
	 bays:=0;
	 beams:=0;
	end;
	writeg(o2,#0'C'#$f#12'Schiffsdaten:'#13#10);
	g:=gewicht;
	showp(312,25+16,bild);
	writeg(o2,name+#10#13#10#0'C'#$e'Technologie :'#0'C'#$a);
	if (techlvl>techh and $f)
	or(y1>=nh+nh1+nh2)and(techlvl+1>=techh and $f)then writeg(o2,#0'C'#$d);
	if (y1<nh+nh1) then writeg(o2,w2s(techlvl,4,false,false))
	else writeg(o2,w2s(techlvl+2,4,false,false));
	writeg(o2,
	 #13#10#0'C'#$e'H�llenst„rke:'#0'C'#$a+w2s(huelle,4,false,false)+
	 #13#10#0'C'#$e'Schildst„rke:'#0'C'#$a+w2s(schild,4,false,false)+
	 #13#10#0'C'#$e'min. Crew   :'#0'C'#$a+w2s(mincrew,4,false,false)+
	 #13#10#0'C'#$e'max. Crew   :'#0'C'#$a+w2s(maxcrew,4,false,false)+
	 #13#10#0'C'#$e'max. Neutr. :'#0'C'#$a+w2s(maxfuel,4,false,false)+

      #10#13#10#0'C'#$e'Credits     :'#0'C'#$a);
	if costcred>cr^ then writeg(o2,#0'C'#$d);
	writeg(o2,w2s(costcred,4,false,false)+
	 #0'C'#$e'/'#0'C'#$a+l2s(cr^,8,false,false,false)+
	 #13#10#0'C'#$e'Molybdenium :'#0'C'#$a);
	if costmolyb>mo^ then writeg(o2,#0'C'#$d);
	writeg(o2,w2s(costmolyb,4,false,false)+
	 #0'C'#$e'/'#0'C'#$a+l2s(mo^,8,false,false,false)+
	 #13#10#0'C'#$e'Duranium    :'#0'C'#$a);
	if costdura>du^ then writeg(o2,#0'C'#$d);
	writeg(o2,w2s(costdura,4,false,false)+
	 #0'C'#$e'/'#0'C'#$a+l2s(du^,8,false,false,false)+
	 #13#10#0'C'#$e'Tritanium   :'#0'C'#$a);
	if costtrit>tr^ then writeg(o2,#0'C'#$d);
	writeg(o2,w2s(costtrit,4,false,false)+
	 #0'C'#$e'/'#0'C'#$a+l2s(tr^,8,false,false,false));

	writeg(o2,
      #10#13#10#0'C'#$e'Masse norm.:'#0'C'#$a+w2s(masse,5,false,false)+
	 #13#10#0'C'#$e'Max. Masse :'#0'C'#$a+w2s(maxmasse,5,false,false)+
	 #13#10#0'C'#$e'Masse jetzt:'#0'C'#$a+w2s(g,5,false,false)+
	 #13#10#0'C'#$e'Rest       :'#0'C'#$a);
	if integer(maxmasse)-g<0 then writeg(o2,#0'C'#$d);
	writeg(o2,l2s(maxmasse-g,5,true,false,false));

	if y2=0 then begin
	 if x=1 then writeg(o2,#13#10#10#0'C'#$2f)
	 else writeg(o2,#13#10#10#0'C'#$f)
	end else writeg(o2,#13#10#10#0'C'#$f);
	writeg(o2,nstring(engname(engid),20)+#0'C'#$f':'#0'C'#$a);
	if storage[0,engid]<enganz then writeg(o2,#0'C'#$d);
	writeg(o2,w2s(min(enganz,storage[0,engid]),2,false,false)+#0'C'#$e'/'#0'C'#$a+
	 w2s(enganz,2,false,false));

	if y2=1 then begin
	 if x=1 then writeg(o2,#13#10#0'C'#$2f)
	 else writeg(o2,#13#10#0'C'#$f)
	end else writeg(o2,#13#10#0'C'#$f);
	writeg(o2,nstring(beamname(beamtyp),20)+#0'C'#$f':'#0'C'#$a+
	 w2s(beams,2,false,false)+#0'C'#$e'/'#0'C'#$a+
	 w2s(maxbeams,2,false,false));

	if y2=2 then begin
	 if x=1 then writeg(o2,#13#10#0'C'#$2f)
	 else writeg(o2,#13#10#0'C'#$f)
	end else writeg(o2,#13#10#0'C'#$f);
	writeg(o2,nstring(torprname(torprtyp),20)+#0'C'#$f':'#0'C'#$a+
	 w2s(torprs,2,false,false)+#0'C'#$e'/'#0'C'#$a+
	 w2s(maxtorps,2,false,false));

	if y2=3 then begin
	 if x=1 then writeg(o2,#13#10#0'C'#$2f)
	 else writeg(o2,#13#10#0'C'#$f)
	end else writeg(o2,#13#10#0'C'#$f);
	writeg(o2,'J„ger-Startrampen   '#0'C'#$f':'#0'C'#$a+
	 w2s(bays,2,false,false)+#0'C'#$e'/'#0'C'#$a+
	 w2s(maxbays,2,false,false));

	if y2=4 then begin
	 if x=1 then writeg(o2,#13#10#0'C'#$2f)
	 else writeg(o2,#13#10#0'C'#$f)
	end else writeg(o2,#13#10#0'C'#$f);
	writeg(o2,'Minenwerfer         '#0'C'#$f':'#0'C'#$a+
	 w2s(mines,2,false,false)+#0'C'#$e'/'#0'C'#$a+
	 w2s(maxmines,2,false,false));

	if y2=5 then begin
	 if x=1 then writeg(o2,#13#10#10#0'C'#$2f)
	 else writeg(o2,#13#10#10#0'C'#$f)
	end else writeg(o2,#13#10#10#0'C'#$f);
	writeg(o2,'Missionen           '#0'C'#$f': -->');

	if y2=6 then begin
	 if x=1 then writeg(o2,#13#10#0'C'#$2f)
	 else writeg(o2,#13#10#0'C'#$f)
	end else writeg(o2,#13#10#0'C'#$f);
	writeg(o2,'Missionen           '#0'C'#$f': -->'#13#10);
	if ((y1<nh)or(techlvl+1<techh and $f))
	and(costcred<=cr^)and(costmolyb<=mo^)
	and(costdura<=du^)and(costtrit<=tr^)
	and(storage[0,engid]>=enganz)
	and(g<=maxmasse)
	then writeg(o2,#0'C'#$c'F10'#0'C'#$f':'#0'C'#$e' Schiff bauen')
	else writeg(o2,#0'C'#$7'F10: Schiff bauen');

       end;
      end;

      repeat
       writeg(o3,#0'C'#$f#12);
       if y1<>-1 then case y2 of
	0:begin
	 writeg(o3,'Antriebe:');
	 for i:=1 to 9 do begin
	  if i=engid then begin
	   if x>1 then writeg(o3,#0'C'#$2e)
	   else writeg(o3,#0'C'#$e)
	  end else if i<=maxe
	  then writeg(o3,#0'C'#$e)
	  else writeg(o3,#0'C'#$7);
	  writeg(o3,#13#10+nstring(engname(i),20)+
	  #0'C'#$f':'#0'C'#$a+w2s(storage[0,i],3,false,false)+#0'C'#$b);
	  if (x>1)and(engid=i) then begin
	   if storage[0,i]>=100 then writeg(o3,#0'C'#$8);
	   writeg(o3,' (+)');
	  end;
	 end;
	 with engines[engid] do begin
	  writeg(o3,#0'C'#$f#13#10#10#10+name+':'+
	  #13#10#0'C'#$e'Credits    :'#0'C'#$a);
	  if costcred>cr^ then writeg(o3,#0'C'#$d);
	  writeg(o3,w2s(costcred,4,false,false)+
	  #13#10#0'C'#$e'Molybdenium:'#0'C'#$a);
	  if costmolyb>mo^ then writeg(o3,#0'C'#$d);
	  writeg(o3,w2s(costmolyb,4,false,false)+
	  #13#10#0'C'#$e'Duranium   :'#0'C'#$a);
	  if costdura>du^ then writeg(o3,#0'C'#$d);
	  writeg(o3,w2s(costdura,4,false,false)+
	  #13#10#0'C'#$e'Tritanium  :'#0'C'#$a);
	  if costtrit>tr^ then writeg(o3,#0'C'#$d);
	  writeg(o3,w2s(costtrit,4,false,false)+
	  #13#10#0'C'#$e'Technologie:'#0'C'#$a+
	  w2s(techlvl,4,false,false)+
	  #13#10#0'C'#$e'Masse      :'#0'C'#$a+
	  w2s(egewicht(engid),4,false,false)+
	  #13#10#10#0'C'#$f'Verbrauchskurve:');
	  displayfuel(400,348,engid);
	  showp(532,224,159+engid);
	 end;
	end;
	1:begin
	 writeg(o3,'Waffen:');
	 for i:=1 to 9 do begin
	  if i=beamtyp then begin
	   if x>1 then writeg(o3,#0'C'#$2e)
	   else writeg(o3,#0'C'#$e)
	  end else if i<=maxb
	  then writeg(o3,#0'C'#$e)
	  else writeg(o3,#0'C'#$7);
	  writeg(o3,#13#10+nstring(beamname(i),20)+
	  #0'C'#$f':'#0'C'#$a+w2s(storage[1,i],3,false,false)+#0'C'#$b);
	  if (x>1)and(beamtyp=i) then begin
	   if storage[1,i]>=100 then writeg(o3,#0'C'#$8);
	   writeg(o3,' (+)');
	  end;
	 end;
	 with {$ifndef unix}data.beams{$else}data_beams^{$endif}[beamtyp] do begin
	  writeg(o3,#0'C'#$f#13#10#10#10+name+':'+
	  #13#10#0'C'#$e'Credits    :'#0'C'#$a);
	  if costcred>cr^ then writeg(o3,#0'C'#$d);
	  writeg(o3,w2s(costcred,4,false,false)+
	  #13#10#0'C'#$e'Molybdenium:'#0'C'#$a);
	  if costmolyb>mo^ then writeg(o3,#0'C'#$d);
	  writeg(o3,w2s(costmolyb,4,false,false)+
	  #13#10#0'C'#$e'Duranium   :'#0'C'#$a);
	  if costdura>du^ then writeg(o3,#0'C'#$d);
	  writeg(o3,w2s(costdura,4,false,false)+
	  #13#10#0'C'#$e'Tritanium  :'#0'C'#$a);
	  if costtrit>tr^ then writeg(o3,#0'C'#$d);
	  writeg(o3,w2s(costtrit,4,false,false)+
	  #13#10#0'C'#$e'Technologie:'#0'C'#$a+
	  w2s(techlvl,4,false,false)+
	  #13#10#0'C'#$e'Masse      :'#0'C'#$a+
	  w2s(egewicht(beamtyp),4,false,false)+
	  #13#10#10#0'C'#$f'Schadenskurve:'+
	  #13#10#10#10#10#10#0'C'#$c'gegen H�lle'+
	  #13#10#0'C'#$9'gegen Crew');
	  displaybeam(400,348,beamtyp);
	  showp(532,224,159+beamtyp);
	 end;
	end;
	2:begin
	 writeg(o3,'Torpedos:');
	 for i:=1 to 9 do begin
	  if i=torprtyp then begin
	   if x>1 then writeg(o3,#0'C'#$2e)
	   else writeg(o3,#0'C'#$e)
	  end else if i<=maxt
	  then writeg(o3,#0'C'#$e)
	  else writeg(o3,#0'C'#$7);

	  writeg(o3,#13#10+nstring(torprname(i),20)+
	  #0'C'#$f':'#0'C'#$a+w2s(storage[2,i],3,false,false)+#0'C'#$b);
	  if (x>1)and(torprtyp=i) then begin
	   if storage[2,i]>=100 then writeg(o3,#0'C'#$8);
	   writeg(o3,' (+)');
	  end;
	 end;
	 with torprohre[torprtyp] do begin
	  writeg(o3,#0'C'#$f#13#10#10#10+name+':'+
	  #13#10#0'C'#$e'Credits    :'#0'C'#$a);
	  if costcred>cr^ then writeg(o3,#0'C'#$d);
	  writeg(o3,w2s(costcred,4,false,false)+
	  #13#10#0'C'#$e'Molybdenium:'#0'C'#$a);
	  if costmolyb>mo^ then writeg(o3,#0'C'#$d);
	  writeg(o3,w2s(costmolyb,4,false,false)+
	  #13#10#0'C'#$e'Duranium   :'#0'C'#$a);
	  if costdura>du^ then writeg(o3,#0'C'#$d);
	  writeg(o3,w2s(costdura,4,false,false)+
	  #13#10#0'C'#$e'Tritanium  :'#0'C'#$a);
	  if costtrit>tr^ then writeg(o3,#0'C'#$d);
	  writeg(o3,w2s(costtrit,4,false,false)+
	  #13#10#0'C'#$e'Masse      :'#0'C'#$a+
	  w2s(egewicht(torprtyp),4,false,false)+
	  #13#10#0'C'#$e'Technologie:'#0'C'#$a+
	  w2s(techlvl,4,false,false)+
	  #13#10#10#0'C'#$f'Nachladezeit:'#0'C'#$a+
	  w2s(recharge,4,false,false)+#0'C'#$e' Runden');
	  showp(532,224,159+torprtyp);
	 end;
	end;
	3:begin
	 writeg(o3,'J„ger-Startrampen');
	 showp(400,25+16,199);
	 writeg(o3,#13#10#10#10#10#10#10#10#0'C'#$e'Credits:  '#0'C'#$a'10'+
	 #13#10#10#0'C'#$e'Masse :'#0'C'#$a' 10'+
	 #13#10#10#0'C'#$e'Vorrat:'#0'C'#$a+w2s(stba,3,false,false));
	 if x>1 then begin
	  writeg(o3,#13#10#10#0'C'#$2f);
	  if stba>=100 then writeg(o3,#0'C'#8);
	  writeg(o3,'(+) '#0'C'#$2e'zum Bauen')
	 end else writeg(o3,#13#10#10#0'C'#$0f'Hier bauen...');
	end;
	4:begin
	 writeg(o3,'Minenwerfer');
	 showp(400,25+16,189);
	 writeg(o3,#13#10#10#10#10#10#10#10#0'C'#$e'Credits:  '#0'C'#$a'5'+
	 #13#10#10#0'C'#$e'Masse :'#0'C'#$a' 10'+
	 #13#10#10#0'C'#$e'Vorrat:'#0'C'#$a+w2s(stml,3,false,false));
	 if x>1 then begin
	  writeg(o3,#13#10#10#0'C'#$2f);
	  if stml>=100 then writeg(o3,#0'C'#8);
	  writeg(o3,'(+) '#0'C'#$2e'zum Bauen')
	 end else writeg(o3,#13#10#10#0'C'#$0f'Hier bauen...');
	end;
	5:with hulls[hullid] do begin
	 writeg(o3,'Missionen:');
	 for i:=0 to 27 do begin
	  if (missionflags and gmissionflags[rasse])and (longint(1) shl i)<>0
	  then writeg(o3,#13#10#0'C'#$a)
	  else writeg(o3,#13#10#0'C'#$c);
	  writeg(o3,missions[i]);
	 end;
	end;
	6:with hulls[hullid] do begin
	 writeg(o3,'Missionen:');
	 for i:=8 to 31 do begin
	  if (missionflags and gmissionflags[rasse]) and (longint(1) shl i)<>0
	  then writeg(o3,#13#10#0'C'#$a)
	  else writeg(o3,#13#10#0'C'#$c);
	  writeg(o3,missions[i]);
	 end;
	 writeg(o3,#13#10#0'C'#$a);
	 for i:=32 to 35 do writeg(o3,missions[i]+#13#10);
	end;
       end;
       repeat
	rd:=0;
	kbreset;
	c:=kbreadkey;
	if c=#0 then cs:=byte(kbreadkey);
	case c of
	 '+':case x of
	  1:case y2 of
	   0:if maxe<>1 then begin
	    inc(engid);
	    rd:=2;
	   end;
	   1:if beams<storage[1,beamtyp] then begin
	    inc(beams);
	    rd:=2;
	   end;
	   2:if torprs<storage[2,torprtyp] then begin
	    inc(torprs);
	    rd:=2;
	   end;
	   3:if bays<stba then begin
	    inc(bays);
	    rd:=2;
	   end;
	   4:if mines<stml then begin
	    inc(mines);
	    rd:=2;
	   end;
	  end;
	  2:with engines[engid] do if (storage[0,engid]<100)
	  and(costcred<=cr^)and(costmolyb<=mo^)
	  and(costdura<=du^)and(costtrit<=tr^)
	  then begin
	   dec(cr^,costcred);
	   dec(mo^,costmolyb);
	   dec(du^,costdura);
	   dec(tr^,costtrit);
	   inc(storage[0,engid]);
	   rd:=4;
	  end;
	  3:with {$ifndef unix}data.beams{$else}data_beams^{$endif}[beamtyp]
	  do if (storage[1,beamtyp]<100)
	  and(costcred<=cr^)and(costmolyb<=mo^)
	  and(costdura<=du^)and(costtrit<=tr^)
	  then begin
	   dec(cr^,costcred);
	   dec(mo^,costmolyb);
	   dec(du^,costdura);
	   dec(tr^,costtrit);
	   inc(storage[1,beamtyp]);
	   rd:=4;
	  end;
	  4:with torprohre[torprtyp] do if (storage[2,torprtyp]<100)
	  and(costcred<=cr^)and(costmolyb<=mo^)
	  and(costdura<=du^)and(costtrit<=tr^)
	  then begin
	   dec(cr^,costcred);
	   dec(mo^,costmolyb);
	   dec(du^,costdura);
	   dec(tr^,costtrit);
	   inc(storage[2,torprtyp]);
	   rd:=4;
	  end;
	  5:if (stba<100)and(10<=cr^)then begin
	   dec(cr^,10);
	   inc(stba);
	   rd:=4;
	  end;
	  6:if (stml<100)and(5<=cr^)then begin
	   dec(cr^,5);
	   inc(stml);
	   rd:=4;
	  end;
	 end;
	 '-':if x=1 then case y2 of
	  0:begin
	   if engid>1 then dec(engid) else engid:=maxe;
	   rd:=2;
	  end;
	  1:if beams>0 then begin
	   dec(beams);
	   rd:=2;
	  end;
	  2:if torprs>0 then begin
	   dec(torprs);
	   rd:=2;
	  end;
	  3:if bays>0 then begin
	   dec(bays);
	   rd:=2;
	  end;
	  4:if mines>0 then begin
	   dec(mines);
	   rd:=2;
	  end;
	 end;
	 #0:case cs of
	  $44:if y1=-1 then begin
	   hullid:=255;
	   rd:=5;
	  end else begin
	   with hulls[hullid] do if((y1<nh)or(techlvl+1<techh and $f))
	   and(costcred<=cr^)and(costmolyb<=mo^)
	   and(costdura<=du^)and(costtrit<=tr^)
	   and(storage[0,engid]>=enganz)
	   and(g<=maxmasse)
	   then begin
	    dec(cr^,costcred);
	    dec(mo^,costmolyb);
	    dec(du^,costdura);
	    dec(tr^,costtrit);
	    dec(stml,mines);
	    dec(stba,bays);
	    dec(storage[0,engid],enganz);
	    dec(storage[1,beamtyp],beams);
	    dec(storage[2,torprtyp],torprs);
	    rd:=5;
	   end;
	  end;
	  $48:case x of
	   0:begin
	    if y1>=0 then dec(y1) else y1:=nx-1;
	    rd:=3;
	   end;
	   1:begin
	    if y2>0 then dec(y2);
	    if (y2=1)and(maxb=0) then dec(y2);
	    rd:=2;
	   end;
	   2:begin
	    if engid>1 then dec(engid);
	    rd:=2;
	   end;
	   3:begin
	    if beamtyp>1 then dec(beamtyp);
	    rd:=2;
	   end;
	   4:begin
	    if torprtyp>1 then dec(torprtyp);
	    rd:=2;
	   end;
	  end;
	  $50:case x of
	   0:begin
	    if y1<nx-1 then inc(y1) else y1:=-1;
	    rd:=3;
	   end;
	   1:begin
	    if y2<6 then inc(y2);
	    if (y2=1)and(maxb=0) then inc(y2);
	    rd:=2;
	   end;
	   2:begin
	    if engid<maxe then inc(engid);
	    rd:=2;
	   end;
	   3:begin
	    if beamtyp<maxb then inc(beamtyp);
	    rd:=2;
	   end;
	   4:begin
	    if torprtyp<maxt then inc(torprtyp);
	    rd:=2;
	   end;
	  end;
	  $4d:if x=0 then begin
	   if y1<>-1 then begin
	    x:=1;
	    rd:=3;
	   end;
	  end else if(x=1)and(y2<5)then begin
	   x:=y2+2;
	   rd:=2;
	  end;
	  $4b:if x=1 then begin
	   x:=0;
	   rd:=3;
	  end else if x>1 then begin
	   x:=1;
	   rd:=2;
	  end;
	 end;
	 #27:begin
	  hullid:=255;
	  rd:=5;
	 end;
	end;
       until rd>0;
      until rd>1;
     until rd>2;
    until rd>3;
   until rd>4;
  end;
  redraw:=2;
 end;

 procedure TechLvl(var tech:byte;t:string30);
 var
  i,j:byte;
  d:t_bdata;
  s:set of byte;
  co:array[0..12] of longint;
  crb:^longint;
  cr:longint;
  c:char;
 begin
  if (tech and $f)<12 then begin
   if planetnr<>-1 then crb:=@planet^.creds else crb:=@base^.creds;

   initb(d,16,7*16+8,32,13,15,8);
   window(14,7*16+6,17+32*8,9+7*16+13*16);
   writeg(d,#12'Technologiestufe '+t);
   s:=[];
   cr:=0;
   for j:=tech shr 4+1 to tech and $f do inc(cr,
    (j-tech shr 4)*longint(j)*100);
   for i:=1 to 12 do begin
    if i>=tech and $f then begin
     co[i]:=0;
     for j:=tech shr 4+1 to i
     do inc(co[i],(j-tech shr 4)*longint(j)*100);
     dec(co[i],cr);
     writeg(d,#13#10#0'C'#$8c'   ');
     if co[i]>crb^ then writeg(d,#0'C'#$87) else s:=s+[i];
     writeg(d,az10[i]+#0'C'#$8e+w2s(i,5,false,false)+
	':'#0'C'#$8a);
     if co[i]>crb^ then writeg(d,#0'C'#$87);
     writeg(d,w2s(co[i],7,false,false));
    end else begin
     writeg(d,#13#10#0'C'#$87'   '+az10[i]+w2s(i,5,false,false)+
	':      0');
    end;
   end;
   redraw:=3;
   repeat
    c:=upcase(kbreadkey);
    case c of
     #27:exit;
     '0'..'9','A','B':begin
      j:=pos(c,az10);
      if (j>=tech shr 4)and(co[j]<=crb^) then begin
       dec(crb^,co[j]);
       tech:=tech and $f0 or j;
       exit;
      end;
     end;
     #0:kbreadkey;
    end;
   until false;
  end;
 end;

 procedure buildtorp;
 var
  o3:t_bdata;

  ub:integer;
  cr,
  mo,
  du,
  tr:^longint;

  i,j:integer;

  x,
  y,
  rd:byte;
  b:array[1..9] of boolean;

 begin
  if planetnr<>-1 then with planet^ do begin
   cr:=@creds;
   mo:=@minedmolyb;
   du:=@mineddura;
   tr:=@minedtrit;
  end else with base^ do begin
   cr:=@creds;
   mo:=@molyb;
   du:=@dura;
   tr:=@trit;
  end;
  y:=1;
  initb(o3,8,8,38,29,15,0);
  window(6,6,9+38*8,473);
  writeg(o3,#12);
  repeat
   for i:=1 to 9
   do with {$ifndef unix}data.torps{$else}data_torps^{$endif}[i]
   do with base^
   do b[i]:=(techlvl<=techt and $f)and(costcred<=cr^)and(costmolyb<=mo^)
	and(costdura<=du^)and(costtrit<=tr^);
   writeg(o3,#0'C'#$f#0'@'#0#0'Torpedos:');
   for i:=1 to 9 do begin
    if i=y then begin
     if b[i] then writeg(o3,#0'C'#$2e)
     else writeg(o3,#0'C'#$27)
    end else if b[i]
    then writeg(o3,#0'C'#$e)
    else writeg(o3,#0'C'#$7);
    with base^ do writeg(o3,#13#10+nstring(torpname(i),20)+
    #0'C'#$f':'#0'C'#$a+w2s(torps[i],3,false,false));
    if (x>1)and(y=i) then writeg(o3,' '#0'C'#$b'(+)')
    else writeg(o3,' '#0'C'#$b'   ');
   end;
   with {$ifndef unix}data.torps{$else}data_torps^{$endif}[y] do begin
    writeg(o3,#0'C'#$f#13#10#10#10+nstring(name+':',21)+
    #13#10#0'C'#$e'Credits    :'#0'C'#$a);
    if costcred>cr^ then writeg(o3,#0'C'#$d);
    writeg(o3,w2s(costcred,4,false,false)+
    #0'C'#$e'/'#0'C'#$0a+l2s(cr^,8,false,false,false)+
    #13#10#0'C'#$e'Molybdenium:'#0'C'#$a);
    if costmolyb>mo^ then writeg(o3,#0'C'#$d);
    writeg(o3,w2s(costmolyb,4,false,false)+
    #0'C'#$e'/'#0'C'#$0a+l2s(mo^,8,false,false,false)+
    #13#10#0'C'#$e'Duranium   :'#0'C'#$a);
    if costdura>du^ then writeg(o3,#0'C'#$d);
    writeg(o3,w2s(costdura,4,false,false)+
    #0'C'#$e'/'#0'C'#$0a+l2s(du^,8,false,false,false)+
    #13#10#0'C'#$e'Tritanium  :'#0'C'#$a);
    if costtrit>tr^ then writeg(o3,#0'C'#$d);
    writeg(o3,w2s(costtrit,4,false,false)+
    #0'C'#$e'/'#0'C'#$0a+l2s(tr^,8,false,false,false)+
    #13#10#0'C'#$e'Technologie:'#0'C'#$a);
    if techlvl>base^.techt and $f then writeg(o3,#0'C'#$d);
    writeg(o3,w2s(techlvl,4,false,false)+
    #13#10#0'C'#$e'Masse      :'#0'C'#$a'   5'+
    #13#10#10#0'C'#$f'Schadenskurve:'+
    #13#10#10#10#10#10#0'C'#$c'gegen H�lle'+
    #13#10#0'C'#$9'gegen Crew'+
    #13#10#0'C'#$8'Trefferwahrscheinlichkeit');
    displaytorp(400-386,348,y);
    showp(218,200,199+y);
   end;
   case kbreadkey of
    '+':begin
     if b[y] then with torps[y] do begin
      dec(cr^,costcred);
      dec(mo^,costmolyb);
      dec(du^,costdura);
      dec(tr^,costtrit);
      inc(base^.torps[y]);
     end;
    end;
    #27:begin
     redraw:=2;
     exit;
    end;
    #0:case kbreadkey of
     #$48:if y=1 then y:=9 else dec(y);
     #$50:if y=9 then y:=1 else inc(y);
    end;
   end;
  until false;
 end;

 procedure buildmine;
 var
  o3:t_bdata;

  ub:integer;
  cr,
  mo,
  du,
  tr:^longint;

  i,j:integer;

  x,
  y,
  rd:byte;
  b:array[1..9] of boolean;

 begin
  if planetnr<>-1 then with planet^ do begin
   cr:=@creds;
   mo:=@minedmolyb;
   du:=@mineddura;
   tr:=@minedtrit;
  end else with base^ do begin
   cr:=@creds;
   mo:=@molyb;
   du:=@dura;
   tr:=@trit;
  end;
  y:=1;
  initb(o3,8,8,38,29,15,0);
  window(6,6,9+38*8,473);
  writeg(o3,#12);
  repeat
   for i:=1 to 9
   do with {$ifndef unix}data.mines{$else}data_mines^{$endif}[i]
   do with base^
   do b[i]:=(techlvl<=techm and $f)and(costcred<=cr^)and(costmolyb<=mo^)
	and(costdura<=du^)and(costtrit<=tr^);
   writeg(o3,#0'C'#$f#0'@'#0#0'Torpedos:');
   for i:=1 to 9 do begin
    if i=y then begin
     if b[i] then writeg(o3,#0'C'#$2e)
     else writeg(o3,#0'C'#$27)
    end else if b[i]
    then writeg(o3,#0'C'#$e)
    else writeg(o3,#0'C'#$7);
    with base^ do writeg(o3,#13#10+nstring(minename(i),20)+
    #0'C'#$f':'#0'C'#$a+w2s(minen[i],3,false,false));
    if (x>1)and(y=i) then writeg(o3,' '#0'C'#$b'(+)')
    else writeg(o3,' '#0'C'#$b'   ');
   end;
   with {$ifndef unix}data.mines{$else}data_mines^{$endif}[y] do begin
    writeg(o3,#0'C'#$f#13#10#10#10+nstring(name+':',21)+
    #13#10#0'C'#$e'Credits    :'#0'C'#$a);
    if costcred>cr^ then writeg(o3,#0'C'#$d);
    writeg(o3,w2s(costcred,4,false,false)+
    #0'C'#$e'/'#0'C'#$0a+l2s(cr^,8,false,false,false)+
    #13#10#0'C'#$e'Molybdenium:'#0'C'#$a);
    if costmolyb>mo^ then writeg(o3,#0'C'#$d);
    writeg(o3,w2s(costmolyb,4,false,false)+
    #0'C'#$e'/'#0'C'#$0a+l2s(mo^,8,false,false,false)+
    #13#10#0'C'#$e'Duranium   :'#0'C'#$a);
    if costdura>du^ then writeg(o3,#0'C'#$d);
    writeg(o3,w2s(costdura,4,false,false)+
    #0'C'#$e'/'#0'C'#$0a+l2s(du^,8,false,false,false)+
    #13#10#0'C'#$e'Tritanium  :'#0'C'#$a);
    if costtrit>tr^ then writeg(o3,#0'C'#$d);
    writeg(o3,w2s(costtrit,4,false,false)+
    #0'C'#$e'/'#0'C'#$0a+l2s(tr^,8,false,false,false)+
    #13#10#0'C'#$e'Technologie:'#0'C'#$a);
    if techlvl>base^.techm and $f then writeg(o3,#0'C'#$d);
    writeg(o3,w2s(techlvl,4,false,false)+
    #13#10#0'C'#$e'Masse      :'#0'C'#$a'   5'+
    #13#10#10#0'C'#$f'Schadenskurve:'+
    #13#10#10#10#10#10#0'C'#$c'gegen H�lle'+
    #13#10#0'C'#$9'gegen Crew'+
    #13#10#0'C'#$8'Trefferwahrscheinlichkeit');
    displaytorp(400-386,348,y);
    showp(218,200,199+y);
   end;
   case kbreadkey of
    '+':begin
     if b[y] then with mines[y] do begin
      dec(cr^,costcred);
      dec(mo^,costmolyb);
      dec(du^,costdura);
      dec(tr^,costtrit);
      inc(base^.minen[y]);
     end;
    end;
    #27:begin
     redraw:=2;
     exit;
    end;
    #0:case kbreadkey of
     #$48:if y=1 then y:=9 else dec(y);
     #$50:if y=9 then y:=1 else inc(y);
    end;
   end;
  until false;
 end;

 procedure buildfighter;
 var
  o3:t_bdata;
  cr,
  mo,
  du,
  tr:^longint;

 begin
  if planetnr<>-1 then with planet^ do begin
   cr:=@creds;
   mo:=@minedmolyb;
   du:=@mineddura;
   tr:=@minedtrit;
  end else with base^ do begin
   cr:=@creds;
   mo:=@molyb;
   du:=@dura;
   tr:=@trit;
  end;
  initb(o3,8,8,40,9,15,0);
  window(6,6,9+40*8,9+9*16);
  redraw:=2;
  repeat
   writeg(o3,#12);
   writeg(o3,#0'C'#$f'J„ger:'+
   #13#10#0'C'#$e'Credits    :'#0'C'#$a);
   if 50>cr^ then writeg(o3,#0'C'#$d);
   writeg(o3,'  50'#0'C'#$e'/'#0'C'#$0a+l2s(cr^,8,false,false,false)+
   #13#10#0'C'#$e'Molybdenium:'#0'C'#$a);
   if 2>mo^ then writeg(o3,#0'C'#$d);
   writeg(o3,'   2'#0'C'#$e'/'#0'C'#$0a+l2s(mo^,8,false,false,false)+
   #13#10#0'C'#$e'Duranium   :'#0'C'#$a);
   if 0>du^ then writeg(o3,#0'C'#$d);
   writeg(o3,'   0'#0'C'#$e'/'#0'C'#$0a+l2s(du^,8,false,false,false)+
   #13#10#0'C'#$e'Tritanium  :'#0'C'#$a);
   if 1>tr^ then writeg(o3,#0'C'#$d);
   writeg(o3,'   1'#0'C'#$e'/'#0'C'#$0a+l2s(tr^,8,false,false,false)+
   #13#10#0'C'#$e'Masse      :'#0'C'#$a'   1');
   writeg(o3,#13#10#0'C'#$e'Anzahl     :'#0'C'#$a);
   if base^.fighter>=150 then writeg(o3,#0'C'#$0d);
   writeg(o3,w2s(base^.fighter,4,false,false));
   if base^.fighter<150 then writeg(o3,#13#10#10#0'C'#$0c' +'#0'C'#$0e' zum bauen...');
   showp(234,50,210+rasse);
   case kbreadkey of
    '+':begin
     if (cr^>=50)and(mo^>=2)and(tr^>=1)and(base^.fighter<150) then begin
      dec(cr^,50);
      dec(mo^,2);
      dec(tr^,1);
      inc(base^.fighter);
     end;
    end;
    #27:begin
     redraw:=2;
     exit;
    end;
    #0:case kbreadkey of
     #$48:if y=1 then y:=9 else dec(y);
     #$50:if y=9 then y:=1 else inc(y);
    end;
   end;
  until false;
 end;

 procedure builddefs;
 var
  o3:t_bdata;
  cr,
  mo,
  du,
  tr:^longint;

 begin
  if planetnr<>-1 then with planet^ do begin
   cr:=@creds;
   mo:=@minedmolyb;
   du:=@mineddura;
   tr:=@minedtrit;
  end else with base^ do begin
   cr:=@creds;
   mo:=@molyb;
   du:=@dura;
   tr:=@trit;
  end;
  initb(o3,8,8,40,8,15,0);
  window(6,6,9+40*8,9+8*16);
  redraw:=2;
  repeat
   writeg(o3,#12);
   writeg(o3,#0'C'#$f'Abwehrposten:'+
   #13#10#0'C'#$e'Credits    :'#0'C'#$a);
   if 50>cr^ then writeg(o3,#0'C'#$d);
   writeg(o3,'  10'#0'C'#$e'/'#0'C'#$0a+l2s(cr^,8,false,false,false)+
   #13#10#0'C'#$e'Molybdenium:'#0'C'#$a);
   if 2>mo^ then writeg(o3,#0'C'#$d);
   writeg(o3,'   0'#0'C'#$e'/'#0'C'#$0a+l2s(mo^,8,false,false,false)+
   #13#10#0'C'#$e'Duranium   :'#0'C'#$a);
   if 0>du^ then writeg(o3,#0'C'#$d);
   writeg(o3,'   1'#0'C'#$e'/'#0'C'#$0a+l2s(du^,8,false,false,false)+
   #13#10#0'C'#$e'Tritanium  :'#0'C'#$a);
   if 1>tr^ then writeg(o3,#0'C'#$d);
   writeg(o3,'   0'#0'C'#$e'/'#0'C'#$0a+l2s(tr^,8,false,false,false));
   writeg(o3,#13#10#0'C'#$e'Anzahl     :'#0'C'#$a);
   if base^.defs>=200 then writeg(o3,#0'C'#$0d);
   writeg(o3,w2s(base^.defs,4,false,false));
   if base^.defs<200 then writeg(o3,#13#10#10#0'C'#$0c' +'#0'C'#$0e' zum bauen...');
   case kbreadkey of
    '+':begin
     if (cr^>=10)and(du^>=1)and(base^.defs<200) then begin
      dec(cr^,10);
      dec(du^,1);
      inc(base^.defs);
     end;
    end;
    #27:begin
     redraw:=2;
     exit;
    end;
    #0:case kbreadkey of
     #$48:if y=1 then y:=9 else dec(y);
     #$50:if y=9 then y:=1 else inc(y);
    end;
   end;
  until false;
 end;

 procedure tastatur;
 begin
  with base^ do repeat
   if redraw<>3 then begin
    DisplayStars(rasse,x,y,z,targetx,targety,targetz);
    begin
     tPlanetNr:=PlanID(targetx,targety,targetz);
     tplanet:=getplan(tplanetnr);
     tBaseNr:=BaseID(targetx,targety,targetz);
     tbase:=getbase(tBaseNr);
     if (tshowthing<>3)
     or(tship=nil)
     or(tship^.posx<>targetx)
     or(tship^.posy<>targety)
     or(tship^.posz<>targetz)
     then begin
      tshipnr:=firstship(rasse,targetx,targety,targetz,5);
      if tshipnr=id_nr then tshipnr:=nextship(tshipnr,rasse,targetx,targety,targetz,5);
      if tshipnr=id_nr then tshipnr:=-1;
      tship:=getship(tshipnr);
     end;
     if (tshowthing<>4)
     or(toship=nil)
     or(toship^.posx<>targetx)
     or(toship^.posy<>targety)
     or(toship^.posz<>targetz)
     then begin
      toshipnr:=firstship(rasse,targetx,targety,targetz,7);
      if toshipnr=id_nr then tshipnr:=nextship(toshipnr,rasse,targetx,targety,targetz,7);
      if toshipnr=id_nr then toshipnr:=-1;
      toship:=getship(toshipnr);
     end;
     tisopponent:=(toshipnr<>-1)or(tbasenr<>-1)and(tbase^.owner<>rasse);
     tIsShip:=tshipnr<>-1;
    end;
    writeg(md,#12#0'C'#$f'Auf: '#0'C'#$e+l2s(posx,5,false,false,false)+'X'+
		  l2s(posy,5,false,false,false)+'Y'+l2s(posz,5,false,false,false)+'Z'#0'C'#$7#13#10);
    if PlanetNr<>-1 then begin
     if showthing=0 then showthing:=1;
     if showthing=1 then writeg(md,#0'C'#$e);
     writeg(md,'<P>lanet '#0'C'#$7);
    end;
    if isship then begin
     if showthing=0 then showthing:=3;
     if showthing=3 then writeg(md,#0'C'#$e);
     writeg(md,'<S>chiff '#0'C'#$7);
    end;
    if Oshipnr<>-1 then begin
     if showthing=0 then showthing:=4;
     if showthing=4 then writeg(md,#0'C'#$c);
     writeg(md,'<G>egner '#0'C'#$7);
    end;
    case showthing of
     1:begin
      writeg(md,#13#10#0'C'#$c'X:'#0'C'#$e+planet^.name+#13#10);
      with ship^
      do if planet^.racetyp=rasse then begin
       writeg(md,rassen[planet^.racetyp]+#13#10);
      end else writeg(md,#10);
     end;
     3:writeg(md,#13#10#0'C'#$c'X:'#0'C'#$e+ship^.schiffname+#13#10#10);
     4:writeg(md,#13#10#0'C'#$c'X:'#0'C'#$e+oship^.schiffname+#13#10+
		       rassen[oship^.owner]+#13#10);
    else
     writeg(md,#13#10#10#10);
    end;
    writeg(md,#0'C'#$c'N'#0'C'#$f'ach:'#0'C'#$e+l2s(targetx,5,true,false,false)+'X'+
		  l2s(targety,5,true,false,false)+'Y'+l2s(targetz,5,true,false,false)+'Z');
    if mlock then writeg(md,'  '#0'C'#$40'MLOCK');
    writeg(md,#0'C'#$7#13#10);
    if (tPlanetNr<>-1) then begin
     if tshowthing=0 then begin
      tshowthing:=1;
      sound(3000);kbdelay(250000);nosound;mreset;kbreset;
     end;
     if tshowthing=1 then writeg(md,#0'C'#$e);
     writeg(md,'<P>lanet '#0'C'#$7);
    end else if tshowthing=1 then tshowthing:=0;
    if (tBaseNr<>-1)and(tbasenr<>id) then begin
     if tshowthing=0 then begin
      tshowthing:=2;
      sound(3000);kbdelay(250000);nosound;mreset;kbreset;
     end;
     if tshowthing=2 then writeg(md,#0'C'#$e);
     writeg(md,'<B>asis '#0'C'#$7);
    end else if tshowthing=2 then tshowthing:=0;
    if tisship then begin
     if tshowthing=0 then begin
      tshowthing:=3;
      sound(2000);kbdelay(250000);nosound;mreset;kbreset;
     end;
     if tshowthing=3 then begin
      writeg(md,#0'C'#$e);
     end;
     writeg(md,'<S>chiffe '#0'C'#$7);
    end else if tshowthing=3 then tshowthing:=0;
    if toShipNr<>-1 then begin
     if tshowthing=0 then begin
      tshowthing:=4;
      sound(2000);kbdelay(250000);nosound;mreset;kbreset;
     end;
     if tshowthing=4 then writeg(md,#0'C'#$c);
     writeg(md,'<G>egner '#0'C'#$7);
    end else if tshowthing=4 then tshowthing:=0;
    dx:=dist(posx,posy,posz,targetx,targety,targetz);
    case tshowthing of
     1:begin
      writeg(md,#13#10#0'C'#$c'Y:'#0'C'#$e+tplanet^.name+#13#10);
      with tship^
      do if (tbasenr<>-1) then begin
       writeg(md,rassen[tbase^.owner]+#13#10);
      end else if tplanet^.racetyp=rasse then begin
       writeg(md,rassen[tplanet^.racetyp]+#13#10);
      end else writeg(md,#10);
      if dx<=75 then showph(336,0,tplanet^.bild)
      else filledrectangle(336,0,336+31,31,0);
     end;
     2:begin writeg(md,#13#10#0'C'#$c'Y:'#0'C'#$e+tbase^.name+#13#10+
		       rassen[tbase^.owner]+#13#10);
      if dx<=75 then showph(336,0,169)
      else filledrectangle(336,0,336+31,31,0);
     end;
     3:begin writeg(md,#13#10#0'C'#$c'Y:'#0'C'#$e+tship^.schiffname+#13#10#10);
      if dx<=75 then showph(336,0,hulls[tship^.typnr].bild)
      else filledrectangle(336,0,336+31,31,0);
     end;
     4:begin writeg(md,#13#10#0'C'#$c'Y:'#0'C'#$e+toship^.schiffname+#13#10+
		       rassen[toship^.owner]+#13#10);
      if dx<=75 then showph(336,0,hulls[toship^.typnr].bild)
      else filledrectangle(336,0,336+31,31,0);
     end;
    else
     filledrectangle(336,0,336+31,31,0);
     writeg(md,#13#10#10#10);
    end;
    writeg(md,#0'C'#$e'Distanz:'#0'C'#$a+
	   w2s(dx,6,false,false)+#13#10#10);

    if (dx>75)or(tshowthing=0) then writeg(md,#0'C'#$7'Visueller Kontakt...')
    else writeg(md,#0'C'#$c'V'#0'C'#$f'isueller Kontakt...');
   end;
   showph(592,436,rasse+242);
   showp(224,250,169);
   repeat
    dx:=dist(posx,posy,posz,targetx,targety,targetz);
    writeg(md,#13#10);
    redraw:=0;
    c:=upcase(kbreadkey);
    if c=#0 then cs:=byte(kbreadkey) else cs:=kbscancode;
    if keyhandle(c,cs,rasse,posx,posy,posz,targetx,targety,targetz,redraw)
    then begin
     if redraw=3 then redraw:=2
     else if redraw=2 then redraw:=1;
    end else case c of
     #0:case cs of

      $30:{Ab}begin
       if tBaseNr<>-1 then tshowthing:=2;
       redraw:=1;
      end;

      $22:{Ag}begin
       if isopponent then begin
	showthing:=4;
	oshipnr:=shiplist(rasse,x,y,z,7,id_nr,'Gegnerische Schiffe bei Schiff',0);
	if oshipnr=-1 then showthing:=0 else oship:=getship(oshipnr);
	redraw:=2;
       end;
       if tisopponent then begin
	tshowthing:=4;
	toshipnr:=shiplist(rasse,targetx,targety,targetz,7,id_nr,'Gegnerische Schiffe bei Zielpunkt',0);
	if toshipnr=-1 then tshowthing:=0 else toship:=getship(toshipnr);
	redraw:=2;
       end;
      end;

      $19:{Ap}begin
       if PlanetNr<>-1 then showthing:=1;
       if tPlanetNr<>-1 then tshowthing:=1;
       redraw:=1;
      end;

      $1f:{As}begin
       if isship then showthing:=3;
       if tisship then tshowthing:=3;
       redraw:=2;
      end;

      $3b:buildtorp;
      $3c:buildmine;
      $3d:buildfighter;
      $3e:builddefs;

      $41:{F5}if tplanetnr<>-1 then with planet^
      do if supplies>=100 then begin
       dec(supplies,100);
       inc(creds,100);
       redraw:=3;
      end;

      $40:{F6}if tplanetnr<>-1 then with planet^
      do if supplies>=10 then begin
       dec(supplies,10);
       inc(creds,10);
       redraw:=3;
      end;

      $3f:{F7}if tplanetnr<>-1 then with planet^
      do if supplies>=1 then begin
       dec(supplies,1);
       inc(creds,1);
       redraw:=3;
      end;

      255:{Mausklick}begin
       dx:=planlist(rasse,x,y,z,4);
       if dx=-2 then begin
	window(44*8-2,20*16-2,65*8+1,21*16+1);
	writex(45,21,$4f,'kein Planet in Sektor');
	while not kbkeypressed do begin
	 filledrectangle2(44*8,20*16,65*8-1,21*16-1,1,2);
	 kbdelay(250000);
	 if not kbkeypressed
	 then filledrectangle2(44*8,20*16,65*8-1,21*16-1,$fe,1);
	 kbdelay(250000);
	end;
	kbreset;
       end else if dx<>-1 then begin
	tplanet:=getplan(dx);
	with tplanet^ do begin
	 targetx:=posx;
	 targety:=posy;
	 targetz:=posz;
	 tshowthing:=1;
	 tplanetnr:=id_nr;
	end;
       end;
       redraw:=2;
      end;
     end;
     'B':begin
      NeuerName(name,20);
      redraw:=3;
     end;
     'O':begin
      MissionSelect;
      redraw:=2;
     end;
     'N':begin
      dx:=planlist(rasse,x,y,z,4);
      if dx=-2 then begin
       window(44*8-2,20*16-2,65*8+1,21*16+1);
       writex(45,21,$4f,'kein Planet in Sektor');
       while not kbkeypressed do begin
	filledrectangle2(44*8,20*16,65*8-1,21*16-1,1,2);
	kbdelay(250000);
	if not kbkeypressed
	then filledrectangle2(44*8,20*16,65*8-1,21*16-1,$fe,1);
	kbdelay(250000);
       end;
       kbreset;
      end else if dx<>-1 then begin
       tplanet:=getplan(dx);
       with tplanet^ do begin
	targetx:=posx;
	targety:=posy;
	targetz:=posz;
	tshowthing:=1;
	tplanetnr:=id_nr;
       end;
      end;
      redraw:=2;
     end;
     'V':if dist(posx,posy,posz,targetx,targety,targetz)<=75 then begin
      case tshowthing of
       1:if tplanet^.racetyp=rasse then visualcontact(rasse,2,tplanetnr)
	 else if (tbasenr<>-1)or(tshipnr<>-1) then visualcontact(rasse,5,tplanetnr)
	 else visualcontact(rasse,4,tplanetnr);
       2:if tbase^.owner=rasse then visualcontact(rasse,1,tbasenr)
	 else visualcontact(rasse,6,tbasenr);
       3:visualcontact(rasse,0,tshipnr);
       4:visualcontact(rasse,3,toshipnr);
      end;
      redraw:=2;
     end;
     #02:{Cb}begin
      if tBaseNr<>-1 then tshowthing:=2;
      redraw:=1;
     end;
     #07:{Cg}begin
      if isopponent then showthing:=4;{**}
      if tisopponent then tshowthing:=4;
      redraw:=1;
     end;
     #16:{Cp}begin
      if PlanetNr<>-1 then showthing:=1;
      if tPlanetNr<>-1 then tshowthing:=1;
      redraw:=1;
     end;
     #19:{Cs}begin
      if isship then showthing:=3;
      if tisship then tshowthing:=3;
      redraw:=1;
     end;
     '+':begin
      runmenu(basemenu,rasse,nextbase(id,rasse,0,0,0,1),0,0,0,0);
      kbreset;
      redraw:=4;
     end;
     '-':begin
      runmenu(basemenu,rasse,lastbase(id,rasse,0,0,0,1),0,0,0,0);
      kbreset;
      redraw:=4;
     end;
     'X':case showthing of
      1:if planet^.racetyp=rasse then begin
       runmenu(planmenu,rasse,planetnr,0,0,0,0);
       redraw:=4;
      end else begin
       visualcontact(rasse,2,planetnr);
       redraw:=2;
      end;
      3:begin
       runmenu(shipmenu,rasse,shipnr,0,0,0,0);
       redraw:=4;
      end;
      4:;
     end;
     'Y':case tshowthing of
      1:if tplanet^.racetyp=rasse then begin
       runmenu(planmenu,rasse,tplanetnr,0,0,0,0);
       redraw:=4;
      end else begin
       if tplanet^.racetyp=rasse then visualcontact(rasse,2,tplanetnr)
       else if (tbasenr<>-1)or(tshipnr<>-1) then visualcontact(rasse,5,tplanetnr)
       else if dist(posx,posy,posz,targetx,targety,targetz)<=75 then visualcontact(rasse,4,tplanetnr);
       redraw:=2;
      end;
      2:if tbase^.owner=rasse then begin
       runmenu(basemenu,rasse,tbasenr,0,0,0,0);
       redraw:=4;
      end else begin
       if dist(posx,posy,posz,targetx,targety,targetz)<=75 then visualcontact(rasse,6,tbasenr);
       redraw:=2;
      end;
      3:begin
       runmenu(shipmenu,rasse,tshipnr,0,0,0,0);
       redraw:=4;
      end;
      4:begin
       if dist(posx,posy,posz,targetx,targety,targetz)<=75 then visualcontact(rasse,3,toshipnr);
       redraw:=2;
      end;
     end;
     'A':techlvl(techE,'Antrieb');
     'H':techlvl(techH,'H�lle');
     'M':techlvl(techM,'Minen');
     'R':techlvl(techTl,'Torpedorohre');
     'T':techlvl(techT,'Torpedos');
     'W':techlvl(techB,'Waffen');
     'I':begin
      repairid:=shiplist(rasse,posx,posy,posz,5,-1,'zu reparierendes Schiff:',0);
      redraw:=2;
     end;
     'S':if (techh>0)and(teche>0) then buildship;
    end;
   until redraw>0;
  until redraw>1;
 end;

begin
 showthing:=0;
 tshowthing:=0;
 initb(sd,8,8,40,29,15,0);
 initb(md,340,264,36,13,15,0);
 toship:=nil;
 tship:=nil;
 if id=-1 then id:=baseid(x,y,z);
 if id=-1 then begin
  oldmenu;
  exit;
 end;
 base:=getbase(id);
 if base=nil then begin
  oldmenu;
  exit;
 end;
 with base^ do begin
  x:=posx;
  y:=posy;
  z:=posz;
  planetNr:=planID(posx,posy,posz);
  if planetNr<>-1 then planet:=getplan(planetnr) else planet:=nil;
  CanFight:=(defs<>0)and(fighter<>0);
  Missionen:=$3f or byte(canfight) shl 6;
  shipnr:=firstship(rasse,x,y,z,5);
  ship:=getship(shipnr);
  isship:=shipnr<>-1;

  oshipnr:=firstship(rasse,posx,posy,posz,7);
  oship:=getship(oshipnr);

  targetx:=posx;
  targety:=posy;
  targetz:=posz;
 end;
 redraw:=0;
 with base^ do repeat
  if redraw<>3 then begin
   ClearScreen(0);
   Window(6,6,329,473);
   Window(338,262,629,473);
  end;

  writeg(sd,#0'C'#$c#12'B'#0'C'#$f'asis:'#0'C'#$e+name);
  writeg(sd,#13#10#0'C'#$f'Status:');
  if damage=0 then writeg(sd,#0'C'#$a)
  else if damage<501 then writeg(sd,#0'C'#$e)
  else writeg(sd,#0'C'#$c);
  writeg(sd,w2s(damage div 10,3,false,false)+'.'+strg(damage mod 10)+'% besch„digt');

  writeg(sd,#13#10#10#0'C'#$f'Technologiestufen:'+
	    #13#10#0'C'#$e' Schiffs'#0'C'#$c'h'#0'C'#$e'�lle:'#0'C'#$a+
	    w2s(techH and $f,2,false,false)+
	    '     '#0'C'#$c'A'#0'C'#$e'ntrieb     :'#0'C'#$a+
	    w2s(techE and $f,2,false,false)+
	    #13#10#0'C'#$c' W'#0'C'#$e'affen      :'#0'C'#$a+
	    w2s(techB and $f,2,false,false)+
	    #0'C'#$c'     T'#0'C'#$e'orpedos    :'#0'C'#$a+
	    w2s(techT and $f,2,false,false)+
	    #0'C'#$c' F1'+
	    #13#10#0'C'#$e' Torpedo'#0'C'#$c'r'#0'C'#$e'ohre:'#0'C'#$a+
	    w2s(techTL and $f,2,false,false)+
	    #0'C'#$c'     M'#0'C'#$e'inen       :'#0'C'#$a+
	    w2s(techM and $f,2,false,false)+
	    #0'C'#$c' F2');

  if planetnr<>-1 then with planet^ do begin
   writeg(sd,#13#10#10#0'C'#$f'Rohstoffe:          gef”rdert:  KT/Mine:');

   writeg(sd,#13#10#0'C'#$e' Neutronium :'#0'C'#$a+w2s(minedneutr,6,false,false)+
	     ' = '+w2s(minedneutr*100 div(minedneutr+neutr),3,false,false)+
	     '%'#0'C'#$e+w2s(vertneutr,8,false,false)+
	     '/100'#13#10#0'C'#$e' Molybdenium:'#0'C'#$a+w2s(minedmolyb,6,false,false)+
	     ' = '+w2s(minedmolyb*100 div(minedmolyb+molyb),3,false,false)+
	     '%'#0'C'#$e+w2s(vertmolyb,8,false,false)+
	     '/100'#13#10#0'C'#$e' Duranium   :'#0'C'#$a+w2s(mineddura,6,false,false)+
	     ' = '+w2s(mineddura*100 div(mineddura+dura),3,false,false)+
	     '%'#0'C'#$e+w2s(vertdura,8,false,false)+
	     '/100'#13#10#0'C'#$e' Tritanium  :'#0'C'#$a+w2s(minedtrit,6,false,false)+
	     ' = '+w2s(minedtrit*100 div(minedtrit+trit),3,false,false)+
	     '%'+#0'C'#$e+w2s(verttrit,8,false,false)+'/100');
   writeg(sd,#13#10#0'C'#$e' Waren      :'#0'C'#$a+w2s(supplies,6,false,false)+
	     #0'C'#$7'  Verkauf: '#0'C'#$c'F5 F6 F7'+
	     #13#10#0'C'#$e' Credits    :'#0'C'#$a+w2s(creds,6,false,false));
   writeg(sd,#13#10#0'C'#$e' Besatzung  :'#0'C'#$a);
   if crew<5000 then writeg(sd,#0'C'#$d);
   writeg(sd,l2s(crew,6,false,false,false));

  end else begin
   writeg(sd,#13#10#10#0'C'#$f'Rohstoffe:');

   writeg(sd,#13#10#0'C'#$e' Neutronium :'#0'C'#$a+w2s(neutr,6,false,false)+
	     #13#10#0'C'#$e' Molybdenium:'#0'C'#$a+w2s(molyb,6,false,false)+
	     #13#10#0'C'#$e' Duranium   :'#0'C'#$a+w2s(dura,6,false,false)+
	     #13#10#0'C'#$e' Tritanium  :'#0'C'#$a+w2s(trit,6,false,false)+
	     #13#10#0'C'#$e' Waren      :'#0'C'#$a+w2s(supplies,6,false,false)+
	     #0'C'#$7'  Verkauf: '#0'C'#$c'F5 F6 F7'+
	     #13#10#0'C'#$e' Credits    :'#0'C'#$a+w2s(creds,6,false,false));
   writeg(sd,#13#10#0'C'#$e' Besatzung  :'#0'C'#$a);
   if crew<5000 then writeg(sd,#0'C'#$d);
   writeg(sd,l2s(crew,6,false,false,false));
  end;
  writeg(sd,#13#10#0'C'#$e' Raumminen  :'#0'C'#$a+l2s(torps[1]+torps[2]+torps[3]+
  torps[4]+torps[5]+torps[6]+torps[7]+torps[8]+torps[9],6,false,false,false)+
      #0'C'#$e#13#10);
  writeg(sd,' Minen      :'#0'C'#$a+l2s(Minen[1]+Minen[2]+Minen[3]+Minen[4]+
	   Minen[5]+Minen[6]+Minen[7]+Minen[8]+Minen[9],6,false,false,false));
  writeg(sd,#13#10#0'C'#$e' J„ger      :'#0'C'#$a+w2s(fighter,6,false,false)+#0'C'#$c' F3');
  writeg(sd,#13#10#0'C'#$e' Abwehr     :'#0'C'#$a+w2s(defs,6,false,false)+#0'C'#$c' F4');
  writeg(sd,#0'C'#$c#13#10#10'I'#0'C'#$f'nstandsetzung von Schiff:'+
	   #13#10#0'C'#$e' ');
  if (crew<5000)or(damage>500) then writeg(sd,#0'C'#$7);
  writeg(sd,shipname(repairid));
  writeg(sd,#13#10#10#0'C'#$c'S'#0'C'#$f'chiffsbau:'#13#10#0'C'#$e' ');
  if (crew<5000)or(damage>500) then writeg(sd,#0'C'#$7);
  with build do
  if hullid<160 then writeg(sd,hulls[hullid].name)
  else writeg(sd,'kein Schiff');
  writeg(sd,#13#10#10#0'C'#$c'O'#0'C'#$f'ption:'#13#10#0'C'#$e' ');
  if (crew<5000)or(damage>500) then writeg(sd,#0'C'#$7);
  writeg(sd,bmissionen[mission1]);

  tastatur;
 until redraw=4;
end;

end.