unit blkpick;
 
interface
 
uses
  Windows, Messages, Classes, SysUtils, Graphics, Controls, StdCtrls, Forms,
  Dialogs, DBCtrls, DB, DBGrids, DBTables, Grids, ExtCtrls, ApoDSet, Menus, blkedit, blkdet, lunardll,
  Mask,math,about;
type
  TForm2 = class(TForm)
    DBGrid1: TDBGrid;
    DataSource1: TDataSource;
    Panel2: TPanel;
    Table1: TApolloTable;
    Panel1: TPanel;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    Lof1: TMenuItem;
    Save1: TMenuItem;
    Exit1: TMenuItem;
    Blocks1: TMenuItem;
    EditBlocks1: TMenuItem;
    OpenDialog1: TOpenDialog;
    Panel3: TPanel;
    DBEdit1: TDBEdit;
    Label1: TLabel;
    Button5: TButton;
    Panel4: TPanel;
    Label2: TLabel;
    DBEdit2: TDBEdit;
    Label3: TLabel;
    Button6: TButton;
    DataSource2: TDataSource;
    ApolloTable1: TApolloTable;
    ApolloTable1BLOCKNO: TFloatField;
    ApolloTable1TITLE: TStringField;
    ApolloTable1USAGE: TMemoField;
    ApolloTable1ASMNAME: TStringField;
    ApolloTable1BELOW: TFloatField;
    ApolloTable1ABOVE: TFloatField;
    ApolloTable1SIDES: TFloatField;
    ApolloTable1SPRUD: TFloatField;
    ApolloTable1SPRLR: TFloatField;
    ApolloTable1CAPE: TFloatField;
    ApolloTable1FIREBALL: TFloatField;
    ApolloTable1RELOC: TMemoField;
    ApolloTable1AUTHOR: TStringField;
    ApolloTable1EMAIL: TStringField;
    ApolloTable1DESCR: TStringField;
    Table1BLOCKNO: TFloatField;
    Table1M16NO: TFloatField;
    ApolloTable1WHOLEPAGE: TBooleanField;
    Help1: TMenuItem;
    About1: TMenuItem;
    DBLookupListBox1: TDBLookupListBox;
    procedure FormCreate(Sender: TObject);
    procedure EditBlocks1Click(Sender: TObject);
    procedure Exit1Click(Sender: TObject);
    procedure Lof1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Save1Click(Sender: TObject);
    procedure About1Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;
 
var
  Form2: TForm2;
 
implementation
 
{$R *.DFM}
var fn:string;
procedure TForm2.FormCreate(Sender: TObject);
begin
  Table1.Open;
  ApolloTable1.Open;
end;
 
procedure TForm2.EditBlocks1Click(Sender: TObject);
begin
  ApolloTable1.Close;
  form1.Show;
end;
 
procedure TForm2.Exit1Click(Sender: TObject);
begin
   Form2.Close;
end;

procedure TForm2.Lof1Click(Sender: TObject);
begin
   OpenDialog1.Execute;
   fn := OpenDialog1.FileName;
end;
 
procedure TForm2.Button3Click(Sender: TObject);
begin
   Table1.Delete;
   Table1.Close;
   Table1.Exclusive := true;
   Table1.Open;
   Table1.Pack;
   Table1.Close;
   Table1.Exclusive := false;
   Table1.Open;
end;
 
procedure TForm2.Button5Click(Sender: TObject);
begin
   Table1.Refresh;
   Panel3.Visible := false;
end;
 
procedure TForm2.Button2Click(Sender: TObject);
begin
   Panel3.Visible := true;
end;
 
procedure TForm2.Button6Click(Sender: TObject);
begin
   Table1.Commit;
   Table1.Refresh;
   Panel4.Visible := false;
end;
 
procedure TForm2.Button1Click(Sender: TObject);
begin
  Table1.Append;
  Panel4.Visible := true;
end;
 
function intstr(i:integer; w :integer): string;
var
  s:string;
begin
  str(i:w,s);
  intstr:=s;
end;
procedure TForm2.Button4Click(Sender: TObject);
begin
   Form3.ApolloTable1.Open;
   Form3.ApolloTable1.SetOrder(1);
   Form3.ApolloTable1.Seek(IntStr(Table1BLOCKNO.AsInteger,6));
   Form3.ShowModal;
end;
const
main: array[1..96] of byte = ($DA,$5A,$8B,$4B,$AB,$C2,$30,
$AF,$06,$BD,$7E,$85,$05,$AF,$04,$BD,$7E,$29,$FF,$00,$AA,$A9,
$00,$00,$A8,$AF,$00,$BD,$7E,$85,$00,$B1,$05,$F0,$08,$B1,$00,
$C5,$03,$F0,$19,$80,$0C,$C8,$E2,$20,$B1,$00,$88,$C5,$04,$C2,
$20,$F0,$0B,$C8,$C8,$CA,$D0,$E3,$E2,$30,$AB,$7A,$FA,$60,$C2,
$20,$EA,$EA,$EA,$AF,$02,$BD,$7E,$85,$00,$B1,$00,$85,$00,$E2,
$30,$AB,$7A,$FA,$A9,$00,$8F,$06,$BD,$7E,$6C,$00,$00,$EA);
function HexToInt(s:string):longint;
var len,i:integer;
    x:char;
    d,c:longint;
    z,y:extended;
begin
  s := UpperCase(s);
  len := Length(s);
  c := 0;
  for i := 1 to len do
  begin
    x := s[len-(i-1)];
    d := 0;
    case x of
    '0':
    d := 0;
    '1':
    d := 1;
    '2':
    d := 2;
    '3':
    d := 3;
    '4':
    d := 4;
    '5':
    d := 5;
    '6':
    d := 6;
    '7':
    d := 7;
    '8':
    d := 8;
    '9':
    d := 9;
    'A':
    d := 10;
    'B':
    d := 11;
    'C':
    d := 12;
    'D':
    d := 13;
    'E':
    d := 14;
    'F':
    d := 15;
    end;
    z := IntPower(10,i-1);
    y := d;
    d := Round(y * z);
    z := IntPower(1.6,i-1);
    y := d;
    d := Round(y * z);
    c := c + d;
  end;
  result := c;
end;
procedure TForm2.Save1Click(Sender: TObject);
var i,j:integer;
f2:file;
t:system.text;
a:array[1..16] of array of word;
d:array[1..7] of array of boolean;
b:array of string;
c:array of string;
count:array[1..7] of integer;
count2:array[1..7] of boolean;
count3:byte;
data:array of byte;
str:TFileStream;
curofs:longint;
offset,mfofs,fsize:longint;
size,size2:word;
dataptr:longint;
ptr1:longint;
ptr3,ptr4:word;
bank,p1,p2,bb:byte;
b1,b2,b3,b4:byte;
asmsize:word;
found:boolean;
f:integer;
asw:boolean;
aswa:byte;
ofs:longint;
lmasm:boolean;
s,s2:string;
begin
   Table1.GoTop;
   asmsize := 0;
   for i := 1 to 16 do
   SetLength(a[i],Table1.Count);
   for i := 1 to 7 do
   SetLength(d[i],Table1.Count);
   SetLength(b,Table1.Count);
   SetLength(c,Table1.Count);
   for i := 1 to 7 do
   count[i] := 0;
   size := 0;
   dataptr := 0;
   s := fn;
   f := pos('.',s);
   delete(s,f,1000);
   s := s + '.dsc';
   system.assign(t,s);
   rewrite(t);
   for i := 0 to Table1.Count-1 do
   begin
     ApolloTable1.Seek(IntStr(Table1BLOCKNO.AsInteger,6));
     found := false;
     f := 0;
     for j := 0 to i-1 do
     begin
       if b[j] = ApolloTable1ASMNAME.AsString then
       begin
         found := true;
         f := j;
       end;
     end;
     if found = false then
     begin
       system.assign(f2,ApolloTable1ASMNAME.AsString);
       b[i] := ApolloTable1ASMNAME.AsString;
       reset(f2,1);
       a[15,i] := FileSize(f2);
       a[16,i] := asmsize;
       asmsize := asmsize + a[15,i];
       size := size + a[15,i];
       system.Close(f2);
     end
     else
     begin
       b[i] := ApolloTable1ASMNAME.AsString;
       a[16,i] := a[16,f];
       a[15,i] := 0;
     end;
     write(t,inttohex(Table1M16NO.AsInteger,3));
     write(t,chr(9));
     write(t,ApolloTable1WHOLEPAGE.AsString);
     write(t,chr(9));
     write(t,ApolloTable1TITLE.AsString);
     write(t,'\n');
     writeln(t,ApolloTable1DESCR.AsString);
     c[i] := ApolloTable1RELOC.AsString;
     j := ApolloTable1BELOW.AsInteger;
     if j <> -1 then
     begin
       a[1,count[1]] := Table1M16NO.AsInteger;
       a[2,count[1]] := j + a[16,i];
       d[1,count[1]] := ApolloTable1WHOLEPAGE.AsBoolean;
       inc(Count[1]);
     end;
     j := ApolloTable1ABOVE.AsInteger;
     if j <> -1 then
     begin
       a[3,count[2]] := Table1M16NO.AsInteger;
       a[4,count[2]] := j + a[16,i];
       d[2,count[2]] := ApolloTable1WHOLEPAGE.AsBoolean;
       inc(Count[2]);
     end;
     j := ApolloTable1SIDES.AsInteger;
     if j <> -1 then
     begin
       a[5,count[3]] := Table1M16NO.AsInteger;
       a[6,count[3]] := j + a[16,i];
       d[3,count[3]] := ApolloTable1WHOLEPAGE.AsBoolean;
       inc(Count[3]);
     end;
     j := ApolloTable1SPRUD.AsInteger;
     if j <> -1 then
     begin
       a[7,count[4]] := Table1M16NO.AsInteger;
       a[8,count[4]] := j + a[16,i];
       d[4,count[4]] := ApolloTable1WHOLEPAGE.AsBoolean;
       inc(Count[4]);
     end;
     j := ApolloTable1SPRLR.AsInteger;
     if j <> -1 then
     begin
       a[9,count[5]] := Table1M16NO.AsInteger;
       a[10,count[5]] := j + a[16,i];
       d[5,count[5]] := ApolloTable1WHOLEPAGE.AsBoolean;
       inc(Count[5]);
     end;
     j := ApolloTable1CAPE.AsInteger;
     if j <> -1 then
     begin
       a[11,count[6]] := Table1M16NO.AsInteger;
       a[12,count[6]] := j + a[16,i];
       d[6,count[6]] := ApolloTable1WHOLEPAGE.AsBoolean;
       inc(Count[6]);
     end;
     j := ApolloTable1FIREBALL.AsInteger;
     if j <> -1 then
     begin
       a[13,count[7]] := Table1M16NO.AsInteger;
       a[14,count[7]] := j + a[16,i];
       d[7,count[7]] := ApolloTable1WHOLEPAGE.AsBoolean;
       inc(Count[7]);
     end;
     table1.Skip(1);
   end;
   system.Close(t);
   count3 := 0;
   for i := 1 to 7 do
   begin
     dec(count[i]);
     if count[i] <> -1 then
     begin
       count2[i] := true;
       inc(count3);
     end
     else
     begin
       count2[i] := false;
     end;
   end;
   size2 := 0;
   for i := 1 to 7 do
   begin
     if count2[i] then
     begin
       size2 := size2 + ((count[i]+1) * 6);
     end;
   end;
   size := size + (count3 * 35) + size2;
   mfofs := size;
   size := size + 97;
   setlength(data,size);
   LunarOpenFile(PChar(fn),LC_READWRITE);
   LunarReadFile(b1,1,$81C0,LC_SEEK);
   if b1 = ord('A') then
   begin
     asw := true;
     aswa := 1;
     LunarReadFile(p1,1,$1ABDF0,LC_SEEK);
     if p1 <> $FF then
     begin
       LunarReadFile(b1,1,$1ABDF1,LC_SEEK);
       LunarReadFile(b2,1,$1ABDF2,LC_SEEK);
       LunarReadFile(b3,1,$1ABDF3,LC_SEEK);
       LunarReadFile(b4,1,$1ABDF4,LC_SEEK);
       offset := b4 shl $18 + b3 shl $10 + b2 shl $8 + b1;
       LunarReadFile(b1,1,$1ABDF5,LC_SEEK);
       LunarReadFile(b2,1,$1ABDF6,LC_SEEK);
       fsize := b2 shl $8 + b1;
       LunarEraseRatArea(offset,fsize+1,0);
     end;
   end
   else
   begin
     asw := false;
     aswa := 0;
     LunarReadFile(p1,1,$2BDF0,LC_SEEK);
     if p1 <> $FF then
     begin
       LunarReadFile(b1,1,$2BDF1,LC_SEEK);
       LunarReadFile(b2,1,$2BDF2,LC_SEEK);
       LunarReadFile(b3,1,$2BDF3,LC_SEEK);
       LunarReadFile(b4,1,$2BDF4,LC_SEEK);
       offset := b4 shl $18 + b3 shl $10 + b2 shl $8 + b1;
       LunarReadFile(b1,1,$2BDF5,LC_SEEK);
       LunarReadFile(b2,1,$2BDF6,LC_SEEK);
       fsize := b2 shl $8 + b1;
       LunarEraseRatArea(offset,fsize+1,0);
     end;
   end;
   if asw = false then
   begin
     ofs := $80200;
     LunarReadFile(b1,1,$7F2B4,LC_SEEK);
     LunarReadFile(b2,1,$7F2B6,LC_SEEK);
   end
   else
   begin
     ofs := $300200;
     LunarReadFile(b1,1,$2FF2B4,LC_SEEK);
     LunarReadFile(b2,1,$2FF2B6,LC_SEEK);
   end;
   if (b1 <> ord('1')) or (b2 < ord('3')) then
   begin
     MessageBox(form1.Handle,'Lunar Magic ASM hacks not installed, please save at least one level in LM','Error',MB_OK or MB_ICONERROR);
   end
   else
   begin
     fsize := LunarGetFileSize();
     offset := LunarWriteRatArea(data[0],size+8,0,ofs,fsize,RATF_LOROM or RATF_NOWRITERAT or RATF_NOWRITEDATA);
     if offset = 0 then
     begin
       LunarExpandRom(8);
       fsize := LunarGetFileSize();
       offset := LunarWriteRatArea(data[0],size+8,0,ofs,fsize,RATF_LOROM or RATF_NOWRITERAT or RATF_NOWRITEDATA);
     end;
     if offset = 0 then
     begin
       LunarExpandRom(16);
       fsize := LunarGetFileSize();
       offset := LunarWriteRatArea(data[0],size+8,0,ofs,fsize,RATF_LOROM or RATF_NOWRITERAT or RATF_NOWRITEDATA);
     end;
     if offset = 0 then
     begin
       LunarExpandRom(24);
       fsize := LunarGetFileSize();
       offset := LunarWriteRatArea(data[0],size+8,0,ofs,fsize,RATF_LOROM or RATF_NOWRITERAT or RATF_NOWRITEDATA);
     end;
     if offset = 0 then
     begin
       LunarExpandRom(32);
       fsize := LunarGetFileSize();
       offset := LunarWriteRatArea(data[0],size+8,0,ofs,fsize,RATF_LOROM or RATF_NOWRITERAT or RATF_NOWRITEDATA);
     end;
     offset := offset + 8;
     ptr1 := LunarPCToSNES(offset,LC_LOROM,LC_HEADER);
     bank := (ptr1 and $FF0000) shr $10;
     ptr3 := ptr1 and $FFFF;
     ptr4 := ptr3 + mfofs;
     ptr3 := ptr3 + (count3 * 35);
     for i := 1 to 7 do
     begin
       if count2[i] then
       begin
         data[dataptr] := $C2;
         inc(dataptr);
         data[dataptr] := $20;
         inc(dataptr);
         data[dataptr] := $A9;
         inc(dataptr);
         p1 := ptr3 and $FF;
         p2 := ptr3 and $FF00 shr $8;
         data[dataptr] := p1;
         inc(dataptr);
         data[dataptr] := p2;
         inc(dataptr);
         ptr3 := ptr3 + ((count[i]+1) * 2);
         data[dataptr] := $8F;
         inc(dataptr);
         data[dataptr] := $00;
         inc(dataptr);
         data[dataptr] := $BD;
         inc(dataptr);
         data[dataptr] := $7E;
         inc(dataptr);
         data[dataptr] := $A9;
         inc(dataptr);
         p1 := ptr3 and $FF;
         p2 := ptr3 and $FF00 shr $8;
         data[dataptr] := p1;
         inc(dataptr);
         data[dataptr] := p2;
         inc(dataptr);
         ptr3 := ptr3 + ((count[i]+1) * 2);
         data[dataptr] := $8F;
         inc(dataptr);
         data[dataptr] := $02;
         inc(dataptr);
         data[dataptr] := $BD;
         inc(dataptr);
         data[dataptr] := $7E;
         inc(dataptr);
         data[dataptr] := $A9;
         inc(dataptr);
         p1 := count[i]+1;
         p2 := aswa;
         data[dataptr] := p1;
         inc(dataptr);
         data[dataptr] := p2;
         inc(dataptr);
         data[dataptr] := $8F;
         inc(dataptr);
         data[dataptr] := $04;
         inc(dataptr);
         data[dataptr] := $BD;
         inc(dataptr);
         data[dataptr] := $7E;
         inc(dataptr);
         data[dataptr] := $A9;
         inc(dataptr);
         p1 := ptr3 and $FF;
         p2 := ptr3 and $FF00 shr $8;
         data[dataptr] := p1;
         inc(dataptr);
         data[dataptr] := p2;
         inc(dataptr);
         ptr3 := ptr3 + ((count[i]+1) * 2);
         data[dataptr] := $8F;
         inc(dataptr);
         data[dataptr] := $06;
         inc(dataptr);
         data[dataptr] := $BD;
         inc(dataptr);
         data[dataptr] := $7E;
         inc(dataptr);
         data[dataptr] := $20;
         inc(dataptr);
         p1 := ptr4 and $FF;
         p2 := ptr4 and $FF00 shr $8;
         data[dataptr] := p1;
         inc(dataptr);
         data[dataptr] := p2;
         inc(dataptr);
         data[dataptr] := $EA;
         inc(dataptr);
         data[dataptr] := $6B;
         inc(dataptr);
       end;
     end;
     ptr4 := ptr3;
     if count2[1] then
     begin
       for i := 0 to count[1] do
       begin
         data[dataptr] := a[1,i];
         inc(dataptr);
         data[dataptr] := a[1,i] and $FF00 shr $8;
         inc(dataptr);
       end;
       for i := 0 to count[1] do
       begin
         a[2,i] := ptr3 + a[2,i];
         data[dataptr] := a[2,i];
         inc(dataptr);
         data[dataptr] := a[2,i] and $FF00 shr $8;
         inc(dataptr);
       end;
       for i := 0 to count[1] do
       begin
         if d[1,i] then
         data[dataptr] := 0
         else
         data[dataptr] := 1;
         inc(dataptr);
         data[dataptr] := 0;
         inc(dataptr);
       end;
     end;
     if count2[2] then
     begin
       for i := 0 to count[2] do
       begin
         data[dataptr] := a[3,i];
         inc(dataptr);
         data[dataptr] := a[3,i] and $FF00 shr $8;
         inc(dataptr);
       end;
       for i := 0 to count[2] do
       begin
         a[4,i] := ptr3 + a[4,i];
         data[dataptr] := a[4,i];
         inc(dataptr);
         data[dataptr] := a[4,i] and $FF00 shr $8;
         inc(dataptr);
       end;
       for i := 0 to count[2] do
       begin
         if d[2,i] then
         data[dataptr] := 0
         else
         data[dataptr] := 1;
         inc(dataptr);
         data[dataptr] := 0;
         inc(dataptr);
       end;
     end;
     if count2[3] then
     begin
       for i := 0 to count[3] do
       begin
         data[dataptr] := a[5,i];
         inc(dataptr);
         data[dataptr] := a[5,i] and $FF00 shr $8;
         inc(dataptr);
       end;
       for i := 0 to count[3] do
       begin
         a[6,i] := ptr3 + a[6,i];
         data[dataptr] := a[6,i];
         inc(dataptr);
         data[dataptr] := a[6,i] and $FF00 shr $8;
         inc(dataptr);
       end;
       for i := 0 to count[3] do
       begin
         if d[3,i] then
         data[dataptr] := 0
         else
         data[dataptr] := 1;
         inc(dataptr);
         data[dataptr] := 0;
         inc(dataptr);
       end;
     end;
     if count2[4] then
     begin
       for i := 0 to count[4] do
       begin
         data[dataptr] := a[7,i];
         inc(dataptr);
         data[dataptr] := a[7,i] and $FF00 shr $8;
         inc(dataptr);
       end;
       for i := 0 to count[4] do
       begin
         a[8,i] := ptr3 + a[8,i];
         data[dataptr] := a[8,i];
         inc(dataptr);
         data[dataptr] := a[8,i] and $FF00 shr $8;
         inc(dataptr);
       end;
       for i := 0 to count[4] do
       begin
         if d[4,i] then
         data[dataptr] := 0
         else
         data[dataptr] := 1;
         inc(dataptr);
         data[dataptr] := 0;
         inc(dataptr);
       end;
     end;
     if count2[5] then
     begin
       for i := 0 to count[5] do
       begin
         data[dataptr] := a[9,i];
         inc(dataptr);
         data[dataptr] := a[9,i] and $FF00 shr $8;
         inc(dataptr);
       end;
       for i := 0 to count[5] do
       begin
         a[10,i] := ptr3 + a[10,i];
         data[dataptr] := a[10,i];
         inc(dataptr);
         data[dataptr] := a[10,i] and $FF00 shr $8;
         inc(dataptr);
       end;
       for i := 0 to count[5] do
       begin
         if d[5,i] then
         data[dataptr] := 0
         else
         data[dataptr] := 1;
         inc(dataptr);
         data[dataptr] := 0;
         inc(dataptr);
       end;
     end;
     if count2[6] then
     begin
       for i := 0 to count[6] do
       begin
         data[dataptr] := a[11,i];
         inc(dataptr);
         data[dataptr] := a[11,i] and $FF00 shr $8;
         inc(dataptr);
       end;
       for i := 0 to count[6] do
       begin
         a[12,i] := ptr3 + a[12,i];
         data[dataptr] := a[12,i];
         inc(dataptr);
         data[dataptr] := a[12,i] and $FF00 shr $8;
         inc(dataptr);
       end;
       for i := 0 to count[6] do
       begin
         if d[6,i] then
         data[dataptr] := 0
         else
         data[dataptr] := 1;
         inc(dataptr);
         data[dataptr] := 0;
         inc(dataptr);
       end;
     end;
     if count2[7] then
     begin
       for i := 0 to count[7] do
       begin
         data[dataptr] := a[13,i];
         inc(dataptr);
         data[dataptr] := a[13,i] and $FF00 shr $8;
         inc(dataptr);
       end;
       for i := 0 to count[7] do
       begin
         a[14,i] := ptr3 + a[14,i];
         data[dataptr] := a[14,i];
         inc(dataptr);
         data[dataptr] := a[14,i] and $FF00 shr $8;
         inc(dataptr);
       end;
       for i := 0 to count[7] do
       begin
         if d[7,i] then
         data[dataptr] := 0
         else
         data[dataptr] := 1;
         inc(dataptr);
         data[dataptr] := 0;
         inc(dataptr);
       end;
     end;
     for i := 0 to Table1.Count-1 do
     begin
       system.assign(f2,b[i]);
       reset(f2,1);
       if a[15,i] <> 0 then
       begin
         for j := 1 to a[15,i] do
         begin
           blockread(f2,p1,1);
           data[dataptr] := p1;
           inc(dataptr);
         end;
         if c[i] <> '-1' then
         begin
           f := dataptr - a[15,i];
           while length(c[i]) > 0 do
           begin
             j := pos(',',c[i]);
             s := c[i];
             s2 := c[i];
             delete(s,j,1000);
             delete(s2,1,j);
             c[i] := s2;
             j := hextoint(s);
             p1 := data[f+j];
             p2 := data[f+j+1];
             ptr3 := p2 shl $8 + p1;
             ptr3 := ptr3 + a[16,i] + ptr4;
             p1 := ptr3 and $FF;
             p2 := ptr3 and $FF00 shr $8;
             data[f+j] := p1;
             data[f+j+1] := p2;
           end;
         end;
       end;
       system.Close(f2);
     end;
     for i := 1 to 96 do
     begin
       data[dataptr] := main[i];
       inc(dataptr);
     end;
     ptr1 := LunarPCToSNES(offset,LC_LOROM,LC_HEADER);
     ptr3 := ptr1;
     ptr1 := offset;
     LunarWriteRatArea(data[0],size,offset-8,ofs,LunarGetFileSize(),RATF_LOROM);
     LunarCloseFile;
     str := TFileStream.Create(fn,fmOpenReadWrite);
     p1 := 0;
     if asw = true then
     begin
       str.Seek($1ABDF0,sobeginning);
       str.Write(p1,1);
       str.Seek($1ABDF1,sobeginning);
       str.Write(ptr1,4);
       str.Seek($1ABDF5,sobeginning);
       str.Write(size,2);
       ptr1 := $1B0000
     end
     else
     begin
       str.Seek($2BDF0,sobeginning);
       str.Write(p1,1);
       str.Seek($2BDF1,sobeginning);
       str.Write(ptr1,4);
       str.Seek($2BDF5,sobeginning);
       str.Write(size,2);
       ptr1 := $30000
     end;
     if count2[1] then
     begin
         bb := $22;
         p2 := ptr3 and $FF;
         p1 := ptr3 and $FF00 shr $8;
         str.Seek($7890+ptr1,sobeginning);
         str.Write(bb,1);
         str.Write(p2,1);
         str.Write(p1,1);
         str.Write(bank,1);
         ptr3 := ptr3 + 35;
     end
     else
     begin
         bb := $EA;
         str.Seek($7890+ptr1,sobeginning);
         str.Write(bb,1);
         str.Write(bb,1);
         str.Write(bb,1);
         str.Write(bb,1);
     end;
     if count2[2] then
     begin
         bb := $22;
         p2 := ptr3 and $FF;
         p1 := ptr3 and $FF00 shr $8;
         str.Seek($78A0+ptr1,sobeginning);
         str.Write(bb,1);
         str.Write(p2,1);
         str.Write(p1,1);
         str.Write(bank,1);
         ptr3 := ptr3 + 35;
     end
     else
     begin
         bb := $EA;
         str.Seek($78A0+ptr1,sobeginning);
         str.Write(bb,1);
         str.Write(bb,1);
         str.Write(bb,1);
         str.Write(bb,1);
     end;
     if count2[3] then
     begin
         bb := $22;
         p2 := ptr3 and $FF;
         p1 := ptr3 and $FF00 shr $8;
         str.Seek($78B0+ptr1,sobeginning);
         str.Write(bb,1);
         str.Write(p2,1);
         str.Write(p1,1);
         str.Write(bank,1);
         ptr3 := ptr3 + 35;
     end
     else
     begin
         bb := $EA;
         str.Seek($78B0+ptr1,sobeginning);
         str.Write(bb,1);
         str.Write(bb,1);
         str.Write(bb,1);
         str.Write(bb,1);
     end;
     if count2[4] then
     begin
         bb := $22;
         p2 := ptr3 and $FF;
         p1 := ptr3 and $FF00 shr $8;
         str.Seek($7920+ptr1,sobeginning);
         str.Write(bb,1);
         str.Write(p2,1);
         str.Write(p1,1);
         str.Write(bank,1);
         ptr3 := ptr3 + 35;
     end
     else
     begin
         bb := $EA;
         str.Seek($7920+ptr1,sobeginning);
         str.Write(bb,1);
         str.Write(bb,1);
         str.Write(bb,1);
         str.Write(bb,1);
     end;
     if count2[5] then
     begin
         bb := $22;
         p2 := ptr3 and $FF;
         p1 := ptr3 and $FF00 shr $8;
         str.Seek($7930+ptr1,sobeginning);
         str.Write(bb,1);
         str.Write(p2,1);
         str.Write(p1,1);
         str.Write(bank,1);
         ptr3 := ptr3 + 35;
     end
     else
     begin
         bb := $EA;
         str.Seek($7930+ptr1,sobeginning);
         str.Write(bb,1);
         str.Write(bb,1);
         str.Write(bb,1);
         str.Write(bb,1);
     end;
     if count2[6] then
     begin
         bb := $22;
         p2 := ptr3 and $FF;
         p1 := ptr3 and $FF00 shr $8;
         str.Seek($7980+ptr1,sobeginning);
         str.Write(bb,1);
         str.Write(p2,1);
         str.Write(p1,1);
         str.Write(bank,1);
         ptr3 := ptr3 + 35;
     end
     else
     begin
         bb := $EA;
         str.Seek($7980+ptr1,sobeginning);
         str.Write(bb,1);
         str.Write(bb,1);
         str.Write(bb,1);
         str.Write(bb,1);
     end;
     if count2[7] then
     begin
         bb := $22;
         p2 := ptr3 and $FF;
         p1 := ptr3 and $FF00 shr $8;
         str.Seek($79C0+ptr1,sobeginning);
         str.Write(bb,1);
         str.Write(p2,1);
         str.Write(p1,1);
         str.Write(bank,1);
     end
     else
     begin
         bb := $EA;
         str.Seek($79C0+ptr1,sobeginning);
         str.Write(bb,1);
         str.Write(bb,1);
         str.Write(bb,1);
         str.Write(bb,1);
     end;
     str.Free;
   end;
end;
 
procedure TForm2.About1Click(Sender: TObject);
begin
form4.show;
end;
 
end.
 