{
   This file is part of VirtualBus.
   Copyright 2002-2004 Marcin Kompanowski, Adam Popiołek, Kakish,
   Maciej Fidor, Qbasob.

    VirtualBus is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    VirtualBus is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with VirtualBus; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
}


unit Unit2;

{$DEFINE MK_DE1B}

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Unit1, ComCtrls,glcontext, FileCtrl, ExtCtrls, wysw1,
  IdBaseComponent, IdComponent, IdUDPBase, IdUDPClient,umklogger, GLMisc,
  GLSound, GLSMBASS,bass;
type
  TForm2 = class(TForm)
    Button2: TButton;
    Button3: TButton;
    OpenDialog1: TOpenDialog;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    TabSheet3: TTabSheet;
    TabSheet4: TTabSheet;
    Memo1: TMemo;
    GroupBox2: TGroupBox;
    Memo2: TMemo;
    GroupBox1: TGroupBox;
    Memo3: TMemo;
    GroupBox3: TGroupBox;
    Memo4: TMemo;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    CheckBox3: TCheckBox;
    Label12: TLabel;
    CheckBox4: TCheckBox;
    CheckBox5: TCheckBox;
    CheckBox6: TCheckBox;
    CheckBox7: TCheckBox;
    ListBox4: TListBox;
    TabSheet5: TTabSheet;
    Label11: TLabel;
    gbKierunek2: TGroupBox;
    lbPierLinOpisu2: TLabel;
    lbDruLinOpisu2: TLabel;
    Label10: TLabel;
    lb1b: TLabel;
    lb2b: TLabel;
    lb3b: TLabel;
    lb4b: TLabel;
    edPierLinOpisu2: TEdit;
    edDruLinOpisu2: TEdit;
    RadioButton4: TRadioButton;
    RadioButton5: TRadioButton;
    RadioButton6: TRadioButton;
    ed1b: TEdit;
    ed2b: TEdit;
    ed3b: TEdit;
    ed4b: TEdit;
    RadioButton8: TRadioButton;
    lbLinia: TLabel;
    edLinia: TEdit;
    Label14: TLabel;
    CheckBox8: TCheckBox;
    StaticText1: TStaticText;
    CheckBox9: TCheckBox;
    Label13: TLabel;
    Label15: TLabel;
    Edit1: TEdit;
    Button1: TButton;
    OpenDialog2: TOpenDialog;
    TabControl1: TTabControl;
    Button4: TButton;
    CheckBox10: TCheckBox;
    CheckBox11: TCheckBox;
    CheckBox12: TCheckBox;
    CheckBox13: TCheckBox;
    TrackBar2: TTrackBar;
    Label16: TLabel;
    TabSheet6: TTabSheet;
    Edit2: TEdit;
    Button5: TButton;
    Memo5: TMemo;
    Edit3: TEdit;
    IdUDPClient1: TIdUDPClient;
    Edit4: TEdit;
    Label17: TLabel;
    Label18: TLabel;
    CheckBox14: TCheckBox;
    ComboBox1: TComboBox;
    ComboBox2: TComboBox;
    ComboBox3: TComboBox;
    GroupBox5: TGroupBox;
    ComboBox5: TComboBox;
    Memo6: TMemo;
    CheckBox15: TCheckBox;
    CheckBox16: TCheckBox;
    CheckBox17: TCheckBox;
    CheckBox18: TCheckBox;
    TrackBar1: TTrackBar;
    Label9: TLabel;
    Label19: TLabel;
    GLSMBASS1: TGLSMBASS;
    CheckBox19: TCheckBox;
    CheckBox20: TCheckBox;
    ComboBox4: TComboBox;
    ComboBox6: TComboBox;
    CheckBox21: TCheckBox;
    ComboBox7: TComboBox;
    CheckBox22: TCheckBox;
    ScrollBox1: TScrollBox;

    procedure Button3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Label13Click(Sender: TObject);
    procedure Label12Click(Sender: TObject);
    procedure Label15Click(Sender: TObject);
  //  procedure ListBox1Click(Sender: TObject);
    procedure ListBox2Click(Sender: TObject);
    procedure ListBox3Click(Sender: TObject);
    procedure napisy(plik:string);
    procedure ListBox4Click(Sender: TObject);

    procedure Button1Click(Sender: TObject);
    procedure TabControl1Change(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure doekranu;
    procedure dotablicy;
    procedure RadioButton8Click(Sender: TObject);
    procedure RadioButton4Click(Sender: TObject);
    procedure RadioButton6Click(Sender: TObject);
    procedure RadioButton5Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
    procedure ComboBox2Change(Sender: TObject);
    procedure ComboBox3Change(Sender: TObject);
    procedure ComboBox5Change(Sender: TObject);
    procedure odlacz;
    procedure TrackBar1Change(Sender: TObject);
  private
    { Private declarations }
  public
    plik:string;
    sch:string;
    pmodel:string;
    ptekst:string;
    nap:array[1..30] of string;
    znak:array[1..4] of string;
    nazwapliku:string;
    mp3dir:string;
    sztywnosc,wmax:real;
    mojnumer:integer;
    trafik:boolean;

    tablice:array[1..5] of record linia1,linia2:string;
        typ:byte;
        pkt1,pkt2,pkt3,pkt4:string;
        end;
    connected:boolean;
  end;

var
  Form2: TForm2;


implementation

  uses ShellApi;


{$R *.dfm}
 var
 log:tmklogger;
 s_bus,s_map:string;




procedure ref_omap;
var f:textfile;
s:string;
i:integer;
begin
    log.wriln('  ref_omap ST');
    AssignFile(F, 'map\'+form2.combobox1.Items[form2.combobox1.ItemIndex]);
    Reset(F);
    form2.memo2.Clear;
    for i:=1 to 10 do begin
        Readln(F, S);
    if (i>2) and (copy(s,1,2)='##') then form2.memo2.Lines.Add(s);
    end  ;
    closefile(f);
    log.wriln('  ref_omap CL');
end;

procedure ref_o_route;
var f:textfile;
s:string;
i:integer;
t:string;
begin
    log.wriln('  ref_o_route ST');
    form2.memo6.Clear;
 for i:=1 to 5 do begin
form2.tablice[i].typ:=1;
form2.tablice[i].linia1:='';
form2.tablice[i].linia2:='';
form2.tablice[i].pkt1:='';
form2.tablice[i].pkt2:='';
form2.tablice[i].pkt3:='';
form2.tablice[i].pkt4:='';
end;
  if (form2.combobox5.Items[form2.combobox5.ItemIndex]<>'-') and
  (form2.combobox5.Items[form2.combobox5.ItemIndex]<>'') then begin

    AssignFile(F, 'map\'+form2.ComboBox5.Items[form2.ComboBox5.itemindex]);
//    log.wriln('map\'+form2.ComboBox5.Items[form2.ComboBox5.itemindex]);
    Reset(F);
    for i:=1 to 10 do begin
        Readln(F, S);
    if (copy(s,1,2)='##') then form2.memo6.Lines.Add(s);
    end  ;
    closefile(f);
    form2.sch:=form2.ComboBox5.Items[form2.ComboBox5.itemindex];
    end else form2.sch:=form2.ComboBox1.Items[form2.ComboBox1.itemindex];

 AssignFile(F, 'map\'+form2.sch);
// log.wriln('map\'+form2.sch);
Reset(F);
repeat
readln(f,s);
    if copy(s,1,2)='LN' then begin
    delete(s,1,pos(' ',s));
    form2.edLinia.Text:=s;
    end;

    if copy(s,1,2)='K1' then begin
    delete(s,1,pos(' ',s));
    form2.tablice[1].linia1:=s;
    end;

    if copy(s,1,2)='K2' then begin
    delete(s,1,pos(' ',s));
    form2.tablice[1].linia2:=s;
    end;

    if copy(s,1,2)='K3' then begin
    delete(s,1,pos(' ',s));
    form2.tablice[2].linia1:=s;
    end;

    if copy(s,1,2)='K4' then begin
    delete(s,1,pos(' ',s));
    form2.tablice[2].linia2:=s;
    end;


    if copy(s,1,2)='K5' then begin
    delete(s,1,pos(' ',s));
    form2.tablice[3].linia1:=s;
    end;

    if copy(s,1,2)='K6' then begin
    delete(s,1,pos(' ',s));
    form2.tablice[3].linia2:=s;
    end;

    if copy(s,1,2)='K7' then begin
    delete(s,1,pos(' ',s));
    form2.tablice[4].linia1:=s;
    end;

    if copy(s,1,2)='K8' then begin
    delete(s,1,pos(' ',s));
    form2.tablice[4].linia2:=s;
    end;

    if copy(s,1,2)='K9' then begin
    delete(s,1,pos(' ',s));
    form2.tablice[5].linia1:=s;
    end;

    if copy(s,1,2)='K0' then begin
    delete(s,1,pos(' ',s));
    form2.tablice[5].linia2:=s;
    end;

    if copy(s,1,2)='1A' then begin
    delete(s,1,pos(' ',s));
    form2.tablice[1].pkt1:=s;
    end;

    if copy(s,1,2)='2A' then begin
    delete(s,1,pos(' ',s));
    form2.tablice[1].pkt2:=s;
    end;

    if copy(s,1,2)='3A' then begin
    delete(s,1,pos(' ',s));
    form2.tablice[1].pkt3:=s;
    end;

    if copy(s,1,2)='4A' then begin
    delete(s,1,pos(' ',s));
    form2.tablice[1].pkt4:=s;
    end;

    if copy(s,1,2)='1B' then begin
    delete(s,1,pos(' ',s));
    form2.tablice[2].pkt1:=s;
    end;

    if copy(s,1,2)='2B' then begin
    delete(s,1,pos(' ',s));
    form2.tablice[2].pkt2:=s;
    end;

    if copy(s,1,2)='3B' then begin
    delete(s,1,pos(' ',s));
    form2.tablice[2].pkt3:=s;
    end;

    if copy(s,1,2)='4B' then begin
    delete(s,1,pos(' ',s));
    form2.tablice[2].pkt4:=s;
    end;

    if copy(s,1,2)='1C' then begin
    delete(s,1,pos(' ',s));
    form2.tablice[3].pkt1:=s;
    end;

    if copy(s,1,2)='2C' then begin
    delete(s,1,pos(' ',s));
    form2.tablice[3].pkt2:=s;
    end;

    if copy(s,1,2)='3C' then begin
    delete(s,1,pos(' ',s));
    form2.tablice[3].pkt3:=s;
    end;

    if copy(s,1,2)='4C' then begin
    delete(s,1,pos(' ',s));
    form2.tablice[3].pkt4:=s;
    end;

    if copy(s,1,2)='1D' then begin
    delete(s,1,pos(' ',s));
    form2.tablice[4].pkt1:=s;
    end;

    if copy(s,1,2)='2D' then begin
    delete(s,1,pos(' ',s));
    form2.tablice[4].pkt2:=s;
    end;

    if copy(s,1,2)='3D' then begin
    delete(s,1,pos(' ',s));
    form2.tablice[4].pkt3:=s;
    end;

    if copy(s,1,2)='4D' then begin
    delete(s,1,pos(' ',s));
    form2.tablice[4].pkt4:=s;
    end;

    if copy(s,1,2)='1E' then begin
    delete(s,1,pos(' ',s));
    form2.tablice[5].pkt1:=s;
    end;

    if copy(s,1,2)='2E' then begin
    delete(s,1,pos(' ',s));
    form2.tablice[5].pkt2:=s;
    end;

    if copy(s,1,2)='3E' then begin
    delete(s,1,pos(' ',s));
    form2.tablice[5].pkt3:=s;
    end;

    if copy(s,1,2)='4E' then begin
    delete(s,1,pos(' ',s));
    form2.tablice[5].pkt4:=s;
    end;


    if copy(s,1,2)='D1' then begin
    delete(s,1,pos(' ',s));
    if strtoint(s)=1 then form2.tablice[1].typ:=4;
    if strtoint(s)=2 then form2.tablice[1].typ:=1;
    if strtoint(s)=3 then form2.tablice[1].typ:=2;
    if strtoint(s)=4 then form2.tablice[1].typ:=3;
    end;

    if copy(s,1,2)='D2' then begin
    delete(s,1,pos(' ',s));
    if strtoint(s)=1 then form2.tablice[2].typ:=4;
    if strtoint(s)=2 then form2.tablice[2].typ:=1;
    if strtoint(s)=3 then form2.tablice[2].typ:=2;
    if strtoint(s)=4 then form2.tablice[2].typ:=3;
    end;

    if copy(s,1,2)='D3' then begin
    delete(s,1,pos(' ',s));
    if strtoint(s)=1 then form2.tablice[3].typ:=4;
    if strtoint(s)=2 then form2.tablice[3].typ:=1;
    if strtoint(s)=3 then form2.tablice[3].typ:=2;
    if strtoint(s)=4 then form2.tablice[3].typ:=3;
    end;

    if copy(s,1,2)='D4' then begin
    delete(s,1,pos(' ',s));
    if strtoint(s)=1 then form2.tablice[4].typ:=4;
    if strtoint(s)=2 then form2.tablice[4].typ:=1;
    if strtoint(s)=3 then form2.tablice[4].typ:=2;
    if strtoint(s)=4 then form2.tablice[4].typ:=3;
    end;

    if copy(s,1,2)='D5' then begin
    delete(s,1,pos(' ',s));
    if strtoint(s)=1 then form2.tablice[5].typ:=4;
    if strtoint(s)=2 then form2.tablice[5].typ:=1;
    if strtoint(s)=3 then form2.tablice[5].typ:=2;
    if strtoint(s)=4 then form2.tablice[5].typ:=3;
    end;

until eof(f);

closefile(f);
form2.doekranu;

    log.wriln('  ref_o_route CL');
end;

procedure ref_o_tex;
 var f:textfile;
s:string;
i:integer;
begin
    log.wriln('  ref_o_tex ST');
    AssignFile(F, 'model\'+form2.combobox2.Items[form2.combobox2.ItemIndex]+'\'+
    form2.combobox3.Items[form2.combobox3.Itemindex]+'\desc.ts1');
    Reset(F);
    form2.memo4.Clear;

    for i:=1 to 10 do begin
        Readln(F, S);
    if (copy(s,1,2)='##') then form2.memo4.Lines.Add(s);
    end  ;
    closefile(f);
    log.wriln('  ref_o_tex CL');

end;

procedure ref_o_bus;
var i:integer;
f:textfile;
s:string;
sr: TSearchRec;
begin
    log.wriln('  ref_o_bus ST model\'+form2.combobox2.Items[form2.combobox2.ItemIndex]+'\desc.bs1');
    AssignFile(F, 'model\'+form2.combobox2.Items[form2.combobox2.ItemIndex]+'\desc.bs1');
    Reset(F);
    form2.memo3.Clear;
    for i:=1 to 10 do begin
        Readln(F, S);
    if (copy(s,1,2)='##') then form2.memo3.Lines.Add(s);
    end  ;
    closefile(f);
        {$IFDEF MK_DEB}
        zapisz('FF3:dir:'+GetCurrentDir);
        {$ENDIF}
   FindFirst('model\'+form2.combobox2.Items[form2.combobox2.ItemIndex]+'\*.*',faanyfile, sr);
   form2.combobox3.Clear;
repeat
if (sr.name[1]<>'.') and (sr.name[1]<>'_') and ((sr.Attr and faDirectory)=faDirectory) then
        begin
        form2.combobox3.Items.add(sr.Name);
        {$IFDEF MK_DEB}
        zapisz('+Ltex:'+sr.Name);
        {$ENDIF}
        end;
until FindNext(sr) <> 0;
findclose(sr);
form2.combobox3.ItemIndex:=0;

   ref_o_tex;

      FindClose(sr);
    log.wriln('  ref_o_bus CL');
end;


procedure zapiszkonfig;
var f:textfile;


error:integer;
begin
        assignfile(f,extractfiledir(application.exename)+'\config.cs1');
        rewrite(f);
        error := IOResult;
//        if error <> 0
//        then begin ShowMessageFmt('cannot white config.cs1 file,  error %d',[error]); halt; end;
        if form2.checkbox1.checked then writeln(f,'OF');
        if form2.checkbox2.checked then writeln(f,'OA');
        if form2.checkbox3.checked then writeln(f,'OR');
        if form2.checkbox4.checked then writeln(f,'OT');
        if form2.checkbox5.checked then writeln(f,'OM');
        if form2.checkbox6.checked then writeln(f,'OO');
        if form2.checkbox7.checked then writeln(f,'OP');
        if form2.checkbox8.checked then writeln(f,'ON');
        if form2.checkbox9.checked then writeln(f,'OD');
        if form2.checkbox10.Checked then writeln(f,'ES');
        if form2.checkbox11.Checked then writeln(f,'AO');
        if form2.checkbox12.Checked then writeln(f,'AC');
        if form2.checkbox13.Checked then writeln(f,'JO');
        if form2.checkbox14.Checked then writeln(f,'AT');
        if form2.CheckBox15.Checked then writeln(f,'TR');
        if form2.CheckBox16.Checked then writeln(f,'SB');
        if form2.CheckBox17.Checked then writeln(f,'HU');
        if form2.CheckBox18.Checked then writeln(f,'VC');
        if form2.CheckBox19.Checked then writeln(f,'PA');
        if form2.CheckBox20.Checked then writeln(f,'MU');
        writeln(f,'JE ',form2.listbox4.ItemIndex);
        writeln(f,'MA ',form2.combobox1.itemindex);
        writeln(f,'BU ',form2.combobox2.itemindex);
        writeln(f,'TE ',form2.combobox3.itemindex);
    //    writeln(f,'JD ',form2.trackbar1.position);
        writeln(f,'WM ',form2.trackbar2.position);
        writeln(f,'NR ',form2.trackbar1.position);
        writeln(f,'M3 ',form2.mp3dir);
        writeln(f,'HA ',form2.edit2.text);
        writeln(f,'HP ',form2.edit3.text);
        writeln(f,'NI ',form2.edit4.text);
        write(f,'SP ',form2.combobox4.itemindex,' ',form2.combobox6.itemindex,' ',form2.combobox7.itemindex,' ');
        if form2.checkbox21.Checked then write(f,'1 ') else write(f,'0 ');
        if form2.checkbox22.Checked then writeln(f,'1 ') else writeln(f,'0 ');        
//        writeln(f,format('ZS %f',[form2.sztywnosc]));
//        writeln(f,format('ZK %f',[form2.wmax]));
        writeln(f);
        closefile(f);

end;

procedure TForm2.odlacz;
var
t:tmemorystream;
s:string;
begin
 if connected then begin
        t:=tmemorystream.Create;
        t.SetSize(512);
        t.Position:=0;
        s:='Q';
        t.write(s[1],length(s));
        t.write(mojnumer,sizeof(integer));
        IdUDPClient1.SendBuffer(t.memory^,t.size);
 end;
end;

procedure TForm2.Button3Click(Sender: TObject);
begin
zapiszkonfig;
wmax:=form2.TrackBar2.position*10;
odlacz;
BASS_Free;
close;
end;

procedure tform2.napisy(plik:string);
var
pliknapisy,f: textfile;
linia,l1:string;

begin
AssignFile(pliknapisy, plik);
    Reset(pliknapisy);
    readln(pliknapisy,linia); {charset}
    repeat
readln(pliknapisy,linia);
l1:=copy(linia,1,pos(':',linia)-1);
delete(linia,1,pos(':',linia));
if (length(linia)>0) and (l1[1]<='9') and (l1[1]>='0')  then
case strtoint(l1) of
101:button2.Caption:=linia;
102:button3.Caption:=linia;
103:tabsheet1.Caption:=linia;
104:tabsheet2.Caption:=linia;
105:tabsheet3.caption:=linia;
106:tabsheet4.Caption:=linia;
107:tabsheet5.Caption:=linia;
108:tabsheet6.Caption:=linia;
201:label6.Caption:=linia;
301:groupbox2.Caption:=linia;
302:groupbox1.Caption:=linia;
303:groupbox3.Caption:=linia;
401:checkbox1.Caption:=linia;
402:checkbox2.Caption:=linia;
403:checkbox3.Caption:=linia;
404:checkbox4.Caption:=linia;
405:checkbox5.Caption:=linia;
406:checkbox6.Caption:=linia;
407:checkbox7.Caption:=linia;
408:lbLinia.Caption:=linia;
//409:gbKierunek1.Caption:=linia;
//410:lbPierLinOpisu1.Caption:=linia;
//411:lbDruLinOpisu1.Caption:=linia;
//412:RadioButton1.Caption:=linia;
//413:RadioButton2.Caption:=linia;
//414:RadioButton3.Caption:=linia;
415:gbKierunek2.Caption:=linia;
416:lbPierLinOpisu2.Caption:=linia;
417:lbDruLinOpisu2.Caption:=linia;
418:RadioButton4.Caption:=linia;
419:RadioButton5.Caption:=linia;
420:RadioButton6.Caption:=linia;
//421:Label9.Caption:=linia;
422:Label10.Caption:=linia;
//423:RadioButton7.Caption:=linia;
424:RadioButton8.Caption:=linia;
425:Label14.Caption:=linia;
501:begin
        assignfile(f,linia);
        reset(f);
        memo1.Clear;
        repeat
                readln(f,l1);
                memo1.Lines.add(l1);
        until eof(f);
        closefile(f);
        end;
601: nap[21]:=linia;
602: nap[22]:=linia;
603: nap[23]:=linia;
604: nap[24]:=linia;
605: nap[25]:=linia;
606: nap[26]:=linia;
701: nap[1]:=linia;
702: nap[2]:=linia;
703: nap[3]:=linia;
704: nap[4]:=linia;
705: nap[5]:=linia;
706: nap[6]:=linia;
707: nap[7]:=linia;
708: nap[8]:=linia;
709: nap[9]:=linia;
710: nap[10]:=linia;
711: nap[11]:=linia;
450: checkbox10.Caption:=linia;
451: checkbox11.Caption:=linia;
452: checkbox12.Caption:=linia;
453: checkbox13.Caption:=linia;
//454: groupbox4.Caption:=linia;
//455: radiobutton1.Caption:=linia;
//456: radiobutton2.Caption:=linia;
//457: label9.Caption:=linia;
458: label16.Caption:=linia;
460: checkbox8.Caption:=linia;
461: checkbox9.caption:=linia;
462: checkbox14.Caption:=linia;
463: checkbox15.Caption:=linia;
455: checkbox16.Caption:=linia;
465: checkbox17.Caption:=linia;
466: checkbox18.Caption:=linia;
467: checkbox19.Caption:=linia;
470: button5.Caption:=linia;
471: label9.Caption:=linia;
end;
until eof(pliknapisy);
closefile(pliknapisy);
end;


procedure TForm2.FormCreate(Sender: TObject);
var tsc:tscreen;
var sr1,sr2,sr3: TSearchRec;
s,t,t2:string;
error,i:integer;
f:textfile;
begin
log:=tmklogger.create(extractfiledir(application.exename)+'\log1.txt');
log.czysc;
sztywnosc:=5;
wmax:=40;
connected:=false;
mojnumer:=1;

// log.wriln('tablice');
doekranu;
 log.wriln('doekr');
Statictext1.Caption:='VirtualBus is free software; you can redistribute it and/or modify'+#13+
    'it under the terms of the GNU General Public License as published by'+#13+
    'the Free Software Foundation; either version 2 of the License, or'+#13+
    '(at your option) any later version.'+#13+#13+

    'VirtualBus is distributed in the hope that it will be useful,'+#13+
    'but WITHOUT ANY WARRANTY; without even the implied warranty of'+#13+
    'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the'+#13+
    'GNU General Public License for more details.'+#13+#13+

    'You should have received a copy of the GNU General Public License'+#13+
    'along with VirtualBus; if not, write to the Free Software'+#13+
    'Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA';


listbox4.Items.Clear;
log.wriln('fcr01');
   FindFirst('*.jez',faAnyFile	, sr1);
repeat
listbox4.Items.add(sr1.Name);
until FindNext(sr1) <> 0;
log.wriln('fcr02');
 log.wriln('jezyki');
napisy('Polski.jez');
 log.wriln('polski');
setcurrentdir(ExtractFilePath(Application.ExeName)+'\map\');
form2.Left:=round(tsc.Width/2-form2.width/2);
form2.top:=round(tsc.height/2-form2.height/2);
   FindFirst('*.ms1',faAnyFile	 , sr2);
repeat
combobox1.Items.add(sr2.Name);
until FindNext(sr2) <> 0;
log.wriln('fcr03');
 log.wriln('tabelka ms1');
setcurrentdir(extractFilePath(Application.ExeName));
combobox1.ItemIndex:=0;
 log.wriln('fcr04');
ref_omap;
log.wriln('fcr05');
//combobox5.ItemIndex:=0;
ref_o_route;
log.wriln('fcr06');
doekranu;
log.wriln('fcr07');
   FindFirst('model\*.*',faanyfile, sr3);
repeat
if (sr3.name[1]<>'.') and ((sr3.Attr and faDirectory)=faDirectory) then begin
 combobox2.Items.add(sr3.Name);
 end;
until FindNext(sr3) <> 0;
log.wriln('fcr08');
 log.wriln('katalogi');

findclose(sr3);
combobox2.ItemIndex:=0;

 log.wriln('fcr09');
setcurrentdir(extractFilePath(Application.ExeName));
 combobox1.ItemIndex:=0;

 log.wriln('fcr10');
decimalseparator:='.';
edit1.Text:=extractfiledir(application.exename)+'\radio\' ;
mp3dir:=extractfiledir(application.exename)+'\radio\' ;
  ref_o_bus;
    assignfile(f,extractfiledir(application.exename)+'\config.cs1');
    reset(f);
 log.wriln('config 1');
 log.wriln('fcr11');
    error:= IOResult;
 //       if error <> 0
  //      then begin ShowMessageFmt('cannot read config.cs1 file,  error %d',[error]); halt; end;
    while (not eof(f)) do
        begin
        readln(f,s);
        log.wriln(s);
        if pos('OF',s)=1 then checkbox1.checked:=true;
        if pos('OA',s)=1 then checkbox2.checked:=true;
        if pos('OR',s)=1 then checkbox3.checked:=true;
        if pos('OT',s)=1 then checkbox4.checked:=true;
        if pos('OM',s)=1 then checkbox5.checked:=true;
        if pos('OO',s)=1 then checkbox6.checked:=true;
        if pos('OP',s)=1 then checkbox7.checked:=true;
        if pos('ON',s)=1 then checkbox8.checked:=true;
        if pos('OD',s)=1 then checkbox9.Checked:=true;
        if pos('JE',s)=1 then begin delete(s,1,3);
                                napisy(listbox4.Items[strtoint(s)]);
                                 listbox4.Selected[strtoint(s)]:=true;
                                 end;
        if pos('MA',s)=1 then begin delete(s,1,3); form2.comboBox1.ItemIndex:=strtoint(s); end;
        if pos('BU',s)=1 then begin delete(s,1,3); form2.comboBox2.ItemIndex:=strtoint(s); end;
        if pos('TE',s)=1 then begin delete(s,1,3); form2.comboBox3.ItemIndex:=strtoint(s); end;
 //       if pos('JD',s)=1 then begin delete(s,1,3); form2.trackbar1.position:=strtoint(s); end;
        if pos('WM',s)=1 then begin delete(s,1,3); form2.trackbar2.position:=strtoint(s); end;
        if pos('NR',s)=1 then begin delete(s,1,3); form2.trackbar1.position:=strtoint(s); label19.Caption:=s+' Hz'; end;
        if pos('M3',s)=1 then begin delete(s,1,3); mp3dir:=s; form2.Edit1.Text:=mp3dir; end;

        if pos('HA',s)=1 then begin delete(s,1,3); form2.Edit2.Text:=s; end;
        if pos('HP',s)=1 then begin delete(s,1,3); form2.Edit3.Text:=s; end;
        if pos('NI',s)=1 then begin delete(s,1,3); form2.Edit4.Text:=s; end;
//        if pos('ZS',s)=1 then begin delete(s,1,3); sztywnosc:=strtofloat(s); end;
//        if pos('ZK',s)=1 then begin delete(s,1,3); wmax:=strtofloat(s); end;
        if pos('ES',s)=1 then form2.CheckBox10.Checked:=true;
        if pos('AO',s)=1 then form2.CheckBox11.Checked:=true;
        if pos('AC',s)=1 then form2.CheckBox12.Checked:=true;
        if pos('JO',s)=1 then form2.CheckBox13.Checked:=true;
        if pos('AT',s)=1 then form2.checkbox14.Checked:=true;
        if pos('TR',s)=1 then form2.checkbox15.Checked:=true;
        if pos('SB',s)=1 then form2.checkbox16.Checked:=true;        
        if pos('HU',s)=1 then form2.checkbox17.Checked:=true;
        if pos('VC',s)=1 then form2.checkbox18.Checked:=true;
        if pos('PA',s)=1 then form2.checkbox19.Checked:=true;
        if pos('MU',s)=1 then form2.checkbox20.Checked:=true;
        if pos('SP',s)=1 then begin
                delete(s,1,3);
                form2.combobox4.ItemIndex:=strtoint(copy(s,1,1));
                form2.ComboBox6.ItemIndex:=strtoint(copy(s,3,1));
                form2.ComboBox7.ItemIndex:=strtoint(copy(s,5,1));
                if s[7]='1' then form2.CheckBox21.Checked:=true else form2.CheckBox21.Checked:=false;
                if s[9]='1' then form2.CheckBox22.Checked:=true else form2.CheckBox22.Checked:=false;
        end;
        end;

    closefile(f);
 log.wriln('config 2');
  log.wriln('fcr12');


combobox5.Clear;
combobox5.Items.add('-');
t:=combobox1.Items[combobox1.ItemIndex];
 log.wriln('fcr13');
delete(t,length(t)-3,4);

setcurrentdir(ExtractFilePath(Application.ExeName)+'\map\');

 FindFirst(t+'*.rs1',faAnyFile , sr2);
// log.wriln(t+'*.rs1');
log.wriln('fcr14');
repeat
t2:=sr2.Name;

combobox5.Items.add(t2);
until FindNext(sr2) <> 0;
log.wriln('fcr15');
form2.sch:=t+'.ms1';
combobox5.ItemIndex:=0;
setcurrentdir(ExtractFilePath(Application.ExeName));
log.wriln('fcr16');
    ref_omap;
log.wriln('fcr17');
    ref_o_route;
log.wriln('fcr18');
    ref_o_bus;
log.wriln('fcr19');
    ref_o_tex;
log.wriln('fcr20');
//czyscimi



doekranu;
log.wriln('fcr21');
 log.wriln('doekr');

end;

procedure TForm2.Button2Click(Sender: TObject);


begin
wmax:=form2.TrackBar2.position*5;
trafik:=checkbox15.checked;

zapiszkonfig;

if connected then begin
        trafik:=false;
        plik:=s_map;
        s_bus:=extractfiledir(s_bus);
        pmodel:=extractfiledir(s_bus);
        ptekst:=extractfilename(s_bus);
        sch:=s_map;
end else begin
        plik:=combobox1.Items[combobox1.ItemIndex];
        pmodel:=combobox2.items[combobox2.itemindex];
        ptekst:=combobox3.items[combobox3.itemindex];
end;
log.wriln(plik);
log.wriln(pmodel);
log.wriln(ptekst);



{if form2.CheckBox2.state=cbchecked then form1.glsceneviewer1.Buffer.AntiAliasing:=aaDefault else
form1.glsceneviewer1.Buffer.AntiAliasing:=aanone;
form1.glsceneviewer1.Buffer.FogEnable:=form2.CheckBox1.Checked;}

if form2.checkbox8.state<>cbchecked then begin
form1.sk3.visible:=false;
//form1.glcone1.visible:=false;
end;

if form2.CheckBox2.state=cbchecked then form1.glfullscreenviewer1.Buffer.AntiAliasing:=aaDefault else
form1.glfullscreenviewer1.Buffer.AntiAliasing:=aanone;
form1.glfullscreenviewer1.Buffer.FogEnable:=form2.CheckBox1.Checked;
form2.hide;
//form1.show;

form1.startuj;
end;


procedure TForm2.Label13Click(Sender: TObject);
begin
ShellExecute(Handle,'open','mailto:komarc@poczta.onet.pl','','',sw_Normal);
end;

procedure TForm2.Label12Click(Sender: TObject);
begin
ShellExecute(Handle,'open','http://vbus.rulez.pl','','',sw_Normal);
end;

procedure TForm2.Label15Click(Sender: TObject);
begin
{ShellExecute(Handle,'open','http://glscene.sourceforge.net','','',sw_Normal);}
end;


procedure TForm2.ListBox2Click(Sender: TObject);
var i:integer;
f:textfile;
s:string;

begin
ref_o_bus;

    AssignFile(F, 'model\'+combobox2.Items[combobox2.ItemIndex]+'\'+
    combobox3.Items[combobox3.Itemindex]+'\desc.ts1');
    Reset(F);
    memo4.Clear;
    for i:=1 to 10 do begin
        Readln(F, S);
    if (copy(s,1,2)='##') then memo4.Lines.Add(s);
    end  ;
    closefile(f);


end;

procedure TForm2.ListBox3Click(Sender: TObject);

begin

ref_o_tex;
end;

procedure TForm2.ListBox4Click(Sender: TObject);
begin
napisy(listbox4.Items[listbox4.ItemIndex]);

end;



procedure TForm2.Button1Click(Sender: TObject);
var tm:string;
begin
tm:=getcurrentdir;
if (opendialog2.Execute) then begin
mp3dir:=ExtractFilePath(opendialog2.FileName);
edit1.Text:=mp3dir;

end;
setcurrentdir(tm);

end;

procedure tform2.dotablicy;
var nr:integer;
begin
nr:=tabcontrol1.tabindex+1;
tablice[nr].pkt1:=ed1b.Text;

tablice[nr].pkt2:=ed2b.text;
tablice[nr].pkt3:=ed3b.text;
tablice[nr].pkt4:=ed4b.text;
tablice[nr].linia1:=edpierlinopisu2.text;
tablice[nr].linia2:=eddrulinopisu2.text;
if radiobutton4.Checked then tablice[nr].typ:=1;
if radiobutton5.Checked then tablice[nr].typ:=2;
if radiobutton6.Checked then tablice[nr].typ:=3;
if radiobutton8.Checked then tablice[nr].typ:=4;
end;

procedure tform2.doekranu;
var nr:integer;
begin
gbkierunek2.Caption:=tabcontrol1.Tabs[tabcontrol1.tabindex];
nr:=tabcontrol1.tabindex+1;

ed1b.text:=tablice[nr].pkt1;
ed2b.text:=tablice[nr].pkt2;
ed3b.text:=tablice[nr].pkt3;
ed4b.text:=tablice[nr].pkt4;
edpierlinopisu2.text:=tablice[nr].linia1;
eddrulinopisu2.text:=tablice[nr].linia2;
if tablice[nr].typ=1 then radiobutton4.Checked:=true;
if tablice[nr].typ=2 then radiobutton5.Checked:=true;
if tablice[nr].typ=3 then radiobutton6.Checked:=true;
if tablice[nr].typ=4 then radiobutton8.Checked:=true;
end;

procedure TForm2.TabControl1Change(Sender: TObject);
begin
doekranu;


end;

procedure TForm2.Button4Click(Sender: TObject);
begin
dotablicy;
end;

procedure TForm2.RadioButton8Click(Sender: TObject);
begin
  lbDrulinopisu2.Visible:=false;
  eddrulinopisu2.Visible:=false;
end;

procedure TForm2.RadioButton4Click(Sender: TObject);
begin
  lbDrulinopisu2.Visible:=true;
  eddrulinopisu2.Visible:=true;

end;

procedure TForm2.RadioButton6Click(Sender: TObject);
begin
  lbDrulinopisu2.Visible:=true;
  eddrulinopisu2.Visible:=true;

end;

procedure TForm2.RadioButton5Click(Sender: TObject);
begin
  lbDrulinopisu2.Visible:=true;
  eddrulinopisu2.Visible:=true;

end;

procedure TForm2.Button5Click(Sender: TObject);
var  ReceivedString:string;
  t:tmemorystream;
  s:string;
  ver:double;
  i,err:integer;
begin
ver:=2.02;
err:=0;
if not connected then begin
t:=tmemorystream.create;
s:='L';
t.write(s[1],length(s));
s:=copy(edit4.text+'                    ',1,20);
t.write(s[1],length(s));
t.WriteBuffer(ver,sizeof(ver));

idudpclient1.ReceiveTimeout := 5000;
idudpclient1.port:=strtoint(edit3.Text);
idudpclient1.Host:=edit2.Text;
//idudpclient1.send('login '+edit4.Text);
idudpclient1.SendBuffer(t.memory^,t.size);
//  ReceivedString := idUDPClient1.ReceiveString();
{  t.CopyFrom(}
t.SetSize(512);
if(idUDPClient1.ReceiveBuffer(t.memory^,t.size)>0) then begin
  setlength(s,1);
  t.Position:=0;
  t.ReadBuffer(pointer(s)^,length(s));
    Memo5.Lines.Add('Received: ' + S);
    if s='N' then
        begin
                t.ReadBuffer(mojnumer,sizeof(mojnumer));
                connected:=true;
                memo5.Lines.add(inttostr(mojnumer));
                t.ReadBuffer(ver,sizeof(ver));
                memo5.Lines.add('server ver '+floattostr(ver));
                t.readbuffer(i,sizeof(i));
                setlength(s_map,i);
                t.readbuffer(pointer(s_map)^,i);
                t.readbuffer(i,sizeof(i));
                setlength(s_bus,i);
                t.readbuffer(pointer(s_bus)^,i);
                memo5.Lines.add('map :'+s_map);
                memo5.Lines.add('bus :'+s_bus);
                if not (fileexists(extractfiledir(application.exename)+'\map\'+s_map)) then err:=1;
                if not (fileexists(extractfiledir(application.exename)+'\model\'+s_bus)) then err:=2;
                memo5.Lines.add('err '+inttostr(err));
        end;

    if err>0 then odlacz;
    if s='F' then
        begin
                memo5.Lines.add('server full :(');
        end;
    if s='V' then
        begin
                memo5.Lines.add('wrong version :(');
        end;
    if s='I' then
        begin
                memo5.Lines.add('not running :(');
        end;
    end;
 t.Destroy;
 end;
end;

procedure TForm2.FormShow(Sender: TObject);
begin
log.wriln('form2 show');
end;

procedure TForm2.ComboBox1Change(Sender: TObject);
var  f:textfile;
s,t,t2:string;
var sr2: TSearchRec;
begin
 ref_omap;
 ref_o_route;
combobox5.Clear;
combobox5.Items.add('-');
t:=combobox1.Items[combobox1.ItemIndex];
delete(t,length(t)-3,4);

setcurrentdir(ExtractFilePath(Application.ExeName)+'\map\');

 FindFirst(t+'*.rs1',faAnyFile , sr2);
// log.wriln(t+'*.rs1');
repeat
t2:=sr2.Name;
combobox5.Items.add(t2);
until FindNext(sr2) <> 0;
 log.wriln('tabelka rs1 '+t2);
setcurrentdir(extractFilePath(Application.ExeName));
combobox5.ItemIndex:=0;
form2.sch:=t+'.ms1';
 //form2.Left:=round(t.Width/2-form2.width/2);
//form2.top:=round(t.height/2-form2.height/2);
end;


procedure TForm2.ComboBox2Change(Sender: TObject);
var i:integer;
f:textfile;
s:string;

begin
ref_o_bus;

    AssignFile(F, 'model\'+combobox2.Items[combobox2.ItemIndex]+'\'+
    combobox3.Items[combobox3.Itemindex]+'\desc.ts1');
    Reset(F);
    memo4.Clear;
    for i:=1 to 10 do begin
        Readln(F, S);
    if (copy(s,1,2)='##') then memo4.Lines.Add(s);
    end  ;
    closefile(f);


end;

procedure TForm2.ComboBox3Change(Sender: TObject);

begin

ref_o_tex;
end;

procedure TForm2.ComboBox5Change(Sender: TObject);
var  f:textfile;
s:string;
begin
ref_o_route;


end;

procedure TForm2.TrackBar1Change(Sender: TObject);
begin
label19.Caption:=inttostr(TrackBar1.position)+' Hz';
end;

end.
