Changeset 143 for trunk/Start.pas


Ignore:
Timestamp:
May 8, 2018, 4:37:34 PM (6 years ago)
Author:
chronos
Message:
  • Modified: Brains array changed to object list and TBrainInfo record to TBrain class for better code maintainability.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Start.pas

    r137 r143  
    7070    procedure ReplayBtnClick(Sender: TObject);
    7171  public
    72     BrainPicture: array [0 .. maxBrain - 1] of TBitmap;
    7372    EmptyPicture: TBitmap;
    7473    procedure UpdateFormerGames;
    7574    procedure UpdateMaps;
    7675  private
    77     WorldSize, StartLandMass, MaxTurn, AutoEnemies, AutoDiff, MultiControl,
    78       MiniWidth, MiniHeight, SelectedAction, Page, ShowTab, Tab, Diff0,
    79       bixDefault, nMapLandTiles, nMapStartPositions, LoadTurn, LastTurn,
     76    WorldSize: Integer;
     77    StartLandMass: Integer;
     78    MaxTurn: Integer;
     79    AutoEnemies: Integer;
     80    AutoDiff: Integer;
     81    MultiControl: Integer;
     82    MiniWidth: Integer;
     83    MiniHeight: Integer;
     84    SelectedAction: Integer;
     85    Page: Integer;
     86    ShowTab: Integer;
     87    Tab: Integer;
     88    Diff0: Integer;
     89    bixDefault: Integer;
     90    nMapLandTiles: Integer;
     91    nMapStartPositions: Integer;
     92    LoadTurn: Integer;
     93    LastTurn: Integer;
    8094    { last turn of selected former game }
    81     SlotAvailable, bixPopup: integer; { brain concerned by brain context menu }
     95    SlotAvailable: Integer;
     96    bixPopup: Integer; { brain concerned by brain context menu }
    8297    ListIndex: array [0 .. 3] of integer;
    8398    MapFileName: string;
     
    186201  r0, r1: HRgn;
    187202  Location: TPoint;
     203  AIBrains: TBrains;
    188204begin
    189205  LoadConfig;
     
    195211
    196212  bixDefault := -1;
    197   for i := bixRandom to nBrain - 1 do
    198     if AnsiCompareFileName(DefaultAI, Brain[i].FileName) = 0 then
     213  for i := Brains.IndexOf(BrainRandom) to Brains.Count - 1 do
     214    if AnsiCompareFileName(DefaultAI, Brains[i].FileName) = 0 then
    199215      bixDefault := i;
    200   if (bixDefault = bixRandom) and (nBrain < bixFirstAI + 2) then
     216  if (bixDefault = Brains.IndexOf(BrainRandom)) and (Brains.GetKindCount(btAI) < 2) then
    201217    bixDefault := -1;
    202   if (bixDefault < 0) and (nBrain > bixFirstAI) then
    203     bixDefault := bixFirstAI; // default AI not found, use any
     218  if (bixDefault < 0) and (Brains.GetKindCount(btAI) > 0) then
     219    begin
     220      AIBrains := TBrains.Create(False);
     221      Brains.GetByKind(btAI, AIBrains);
     222      bixDefault := Brains.IndexOf(Brains[0]);
     223      AIBrains.Free;
     224    end; // default AI not found, use any
    204225
    205226  DirectDlg.Left := (Screen.Width - DirectDlg.Width) div 2;
     
    269290    CustomizeBtn.ButtonIndex := 2;
    270291
    271   BrainPicture[0] := TBitmap.Create;
    272   BrainPicture[0].SetSize(64, 64);
    273   BitBlt(BrainPicture[0].Canvas.Handle, 0, 0, 64, 64,
     292  Brains[0].Picture := TBitmap.Create;
     293  Brains[0].Picture.SetSize(64, 64);
     294  BitBlt(Brains[0].Picture.Canvas.Handle, 0, 0, 64, 64,
    274295    GrExt[HGrSystem2].Data.Canvas.Handle, 1, 111, SRCCOPY);
    275   BrainPicture[1] := TBitmap.Create;
    276   BrainPicture[1].SetSize(64, 64);
    277   BitBlt(BrainPicture[1].Canvas.Handle, 0, 0, 64, 64,
     296  Brains[1].Picture := TBitmap.Create;
     297  Brains[1].Picture.SetSize(64, 64);
     298  BitBlt(Brains[1].Picture.Canvas.Handle, 0, 0, 64, 64,
    278299    GrExt[HGrSystem2].Data.Canvas.Handle, 66, 111, SRCCOPY);
    279   BrainPicture[2] := TBitmap.Create;
    280   BrainPicture[2].SetSize(64, 64);
    281   BitBlt(BrainPicture[2].Canvas.Handle, 0, 0, 64, 64,
     300  Brains[2].Picture := TBitmap.Create;
     301  Brains[2].Picture.SetSize(64, 64);
     302  BitBlt(Brains[2].Picture.Canvas.Handle, 0, 0, 64, 64,
    282303    GrExt[HGrSystem2].Data.Canvas.Handle, 131, 111, SRCCOPY);
    283   BrainPicture[3] := TBitmap.Create;
    284   BrainPicture[3].SetSize(64, 64);
    285   BitBlt(BrainPicture[3].Canvas.Handle, 0, 0, 64, 64,
     304  Brains[3].Picture := TBitmap.Create;
     305  Brains[3].Picture.SetSize(64, 64);
     306  BitBlt(Brains[3].Picture.Canvas.Handle, 0, 0, 64, 64,
    286307    GrExt[HGrSystem2].Data.Canvas.Handle, 131, 46, SRCCOPY);
    287   for i := bixFirstAI to nBrain - 1 do begin
    288     BrainPicture[i] := TBitmap.Create;
    289     if not LoadGraphicFile(BrainPicture[i], HomeDir + 'AI' + DirectorySeparator +
    290       Brain[i].FileName + DirectorySeparator + Brain[i].FileName + '.png',
    291       gfNoError) then begin
    292       BrainPicture[i].SetSize(64, 64);
    293       with BrainPicture[i].Canvas do begin
     308
     309  AIBrains := TBrains.Create(False);
     310  Brains.GetByKind(btAI, AIBrains);
     311  for i := 0 to AIBrains.Count - 1 do
     312  with AIBrains[I] do
     313  begin
     314    AIBrains[i].Picture := TBitmap.Create;
     315    if not LoadGraphicFile(AIBrains[i].Picture, HomeDir + 'AI' + DirectorySeparator +
     316      FileName + DirectorySeparator + FileName + '.png', gfNoError) then begin
     317      AIBrains[i].Picture.SetSize(64, 64);
     318      with AIBrains[i].Picture.Canvas do begin
    294319        Brush.Color := $904830;
    295320        FillRect(Rect(0, 0, 64, 64));
     
    297322        Font.Style := [];
    298323        Font.Color := $5FDBFF;
    299         Textout(32 - TextWidth(Brain[i].FileName) div 2,
    300           32 - TextHeight(Brain[i].FileName) div 2, Brain[i].FileName);
     324        Textout(32 - TextWidth(FileName) div 2,
     325          32 - TextHeight(FileName) div 2, FileName);
    301326      end;
    302327    end;
    303328  end;
     329  AIBrains.Free;
    304330
    305331  EmptyPicture := TBitmap.Create;
     
    318344  InitButtons;
    319345
    320   bixView[0] := bixTerm;
     346  bixView[0] := Brains.IndexOf(BrainTerm);
    321347  SlotAvailable := -1;
    322348  Tab := 2;
     
    333359
    334360procedure TStartDlg.FormDestroy(Sender: TObject);
    335 var
    336   i: integer;
    337361begin
    338362  FreeAndNil(FormerGames);
     
    341365  FreeAndNil(EmptyPicture);
    342366  FreeAndNil(LogoBuffer);
    343   for i := 0 to nBrain - 1 do
    344     FreeAndNil(BrainPicture[i]);
    345367end;
    346368
     
    624646        begin
    625647          if bixView[i] >= 0 then
    626             FrameImage(Canvas, BrainPicture[bixView[i]], xBrain[i], yBrain[i],
     648            FrameImage(Canvas, Brains[bixView[i]].Picture, xBrain[i], yBrain[i],
    627649              64, 64, 0, 0, true)
    628650          else
    629651            FrameImage(Canvas, EmptyPicture, xBrain[i], yBrain[i], 64, 64,
    630652              0, 0, true);
    631           if bixView[i] >= bixTerm then
     653          if Brains[bixView[i]].Kind in [btTerm, btRandom, btAI] then
    632654          begin
    633655            BitBlt(Canvas.Handle, xBrain[i] - 18, yBrain[i] + 19, 12, 14,
     
    658680          begin
    659681            DiffUpBtn[i].Hint := Format(Phrases.Lookup('STARTCONTROLS', 9),
    660               [Brain[bixView[i]].Name]);
     682              [Brains[bixView[i]].Name]);
    661683            DiffDownBtn[i].Hint := DiffUpBtn[i].Hint;
    662684          end
     
    681703        yMain + 164 { y0Mini-77 } , Phrases.Lookup('STARTCONTROLS', 16));
    682704      if AutoDiff = 1 then
    683         FrameImage(Canvas, BrainPicture[bixBeginner], xDefault, yDefault, 64,
     705        FrameImage(Canvas, BrainBeginner.Picture, xDefault, yDefault, 64,
    684706          64, 0, 0, false)
    685707      else
    686         FrameImage(Canvas, BrainPicture[bixDefault], xDefault, yDefault, 64, 64,
     708        FrameImage(Canvas, Brains[bixDefault].Picture, xDefault, yDefault, 64, 64,
    687709          0, 0, true);
    688710      DLine(Canvas, 56, 272, y0Mini + 61 + 19, MainTexture.clBevelLight,
     
    895917            else GameCount := 0;
    896918
    897           if (AutoDiff < 0) and (bixView[0] = bixNoTerm) then
     919          if (AutoDiff < 0) and (Brains[bixView[0]].Kind = btNoTerm) then
    898920            FileName := 'Round' + IntToStr(GetProcessID())
    899921          else begin
     
    913935                  Reg.WriteString('Control' + IntToStr(i), '')
    914936                else Reg.WriteString('Control' + IntToStr(i),
    915                   Brain[bixView[i]].FileName);
     937                  Brains[bixView[i]].FileName);
    916938                WriteInteger('Diff' + IntToStr(i), Difficulty[i]);
    917939              end;
     
    922944          if AutoDiff > 0 then
    923945          begin
    924             WriteString('DefaultAI', Brain[bixDefault].FileName);
     946            WriteString('DefaultAI', Brains[bixDefault].FileName);
    925947            SlotAvailable := 0; // bixView will be invalid hereafter
    926             bixView[0] := bixTerm;
     948            bixView[0] := Brains.IndexOf(BrainTerm);
    927949            Difficulty[0] := PlayerAutoDiff[AutoDiff];
    928950            for i := 1 to nPl - 1 do
    929951              if (Page = pgStartRandom) and (i <= AutoEnemies) or
    930952                (Page = pgStartMap) and (i < nMapStartPositions) then begin
    931                 if AutoDiff = 1 then bixView[i] := bixBeginner
     953                if AutoDiff = 1 then bixView[i] := Brains.IndexOf(BrainBeginner)
    932954                  else bixView[i] := bixDefault;
    933955                Difficulty[i] := EnemyAutoDiff[AutoDiff];
     
    935957          end else begin
    936958            for i := 6 to 8 do
    937               if (bixView[0] <> bixNoTerm) and (MultiControl and (1 shl i) <> 0)
     959              if (Brains[bixView[0]].Kind <> btNoTerm) and (MultiControl and (1 shl i) <> 0)
    938960              then begin
    939961                bixView[i + 3] := bixView[i];
     
    12181240  else
    12191241  begin
    1220     Brain[bixView[bixPopup]].Flags := Brain[bixView[bixPopup]].Flags and
     1242    Brains[bixView[bixPopup]].Flags := Brains[bixView[bixPopup]].Flags and
    12211243      not fUsed;
    12221244    bixView[bixPopup] := TMenuItem(Sender).Tag;
    1223     DiffUpBtn[bixPopup].Visible := bixView[bixPopup] >= bixTerm;
    1224     DiffDownBtn[bixPopup].Visible := bixView[bixPopup] >= bixTerm;
     1245    DiffUpBtn[bixPopup].Visible := Brains[bixView[bixPopup]].Kind in [btTerm, btRandom, btAI];
     1246    DiffDownBtn[bixPopup].Visible := Brains[bixView[bixPopup]].Kind in [btTerm, btRandom, btAI];
    12251247    if bixPopup in OfferMultiple then
    12261248    begin
    1227       MultiBtn[bixPopup].Visible := bixView[bixPopup] >= bixTerm;
     1249      MultiBtn[bixPopup].Visible := Brains[bixView[bixPopup]].Kind in [btTerm, btRandom, btAI];
    12281250      MultiBtn[bixPopup].ButtonIndex := 2 + (MultiControl shr bixPopup) and 1;
    12291251    end;
    1230     Brain[bixView[bixPopup]].Flags := Brain[bixView[bixPopup]].Flags or fUsed;
    1231     if bixView[bixPopup] < bixTerm then
     1252    Brains[bixView[bixPopup]].Flags := Brains[bixView[bixPopup]].Flags or fUsed;
     1253    if Brains[bixView[bixPopup]].Kind in [btNoTerm, btSuperVirtual] then
    12321254      Difficulty[bixPopup] := 0 { supervisor }
    12331255    else
     
    12381260    if (bixPopup = 0) and (MapFileName <> '') then
    12391261      ChangePage(Page);
    1240     if bixView[bixPopup] = bixNoTerm then
     1262    if Brains[bixView[bixPopup]].Kind = btNoTerm then
    12411263    begin // turn all local players off
    12421264      for i := 1 to nPlOffered - 1 do
    1243         if bixView[i] = bixTerm then
     1265        if Brains[bixView[i]].Kind = btTerm then
    12441266        begin
    12451267          bixView[i] := -1;
     
    12561278            DiffUpBtn[i].top + 25);
    12571279        end;
    1258       Brain[bixTerm].Flags := Brain[bixTerm].Flags and not fUsed;
     1280      BrainTerm.Flags := BrainTerm.Flags and not fUsed;
    12591281    end;
    12601282    SmartInvalidate(xBrain[bixPopup] - 31, yBrain[bixPopup] - 1,
     
    12671289  i, FixedLines: integer;
    12681290  m: TMenuItem;
     1291  AIBrains: TBrains;
    12691292
    12701293  procedure OfferBrain(Index: integer);
     
    12761299      m.Caption := Phrases.Lookup('NOMOD')
    12771300    else
    1278       m.Caption := Brain[Index].Name;
     1301      m.Caption := Brains[Index].Name;
    12791302    m.Tag := Index;
    12801303    m.OnClick := BrainClick;
     
    12971320  begin // select default AI
    12981321    FixedLines := 0;
    1299     if nBrain >= bixFirstAI + 2 then
     1322    if Brains.GetKindCount(btAI) >= 2 then
    13001323    begin
    1301       OfferBrain(bixRandom);
     1324      OfferBrain(Brains.IndexOf(BrainRandom));
    13021325      inc(FixedLines)
    13031326    end;
    1304     for i := bixFirstAI to nBrain - 1 do // offer available AIs
    1305       if Brain[i].Flags and fMultiple <> 0 then
    1306         OfferBrain(i);
     1327    AIBrains := TBrains.Create(False);
     1328    Brains.GetByKind(btAI, AIBrains);
     1329    for i := 0 to AIBrains.Count - 1 do // offer available AIs
     1330      if AIBrains[i].Flags and fMultiple <> 0 then
     1331        OfferBrain(Brains.IndexOf(AIBrains[i]));
     1332    AIBrains.Free;
    13071333  end
    13081334  else
     
    13141340      inc(FixedLines);
    13151341    end;
    1316     for i := bixTerm downto 0 do // offer game interfaces
    1317       if (bixPopup = 0) or (i = bixTerm) and (bixView[0] <> bixNoTerm) then
     1342    for i := Brains.IndexOf(BrainTerm) downto 0 do // offer game interfaces
     1343      if (bixPopup = 0) or (Brains[i].Kind = btTerm) and (Brains[bixView[0]].Kind <> btNoTerm) then
    13181344      begin
    13191345        OfferBrain(i);
     
    13261352      PopupMenu1.Items.Add(m);
    13271353      inc(FixedLines);
    1328       if nBrain >= bixFirstAI + 2 then
    1329       begin
    1330         OfferBrain(bixRandom);
     1354      if Brains.GetKindCount(btAI) >= 2 then
     1355      begin
     1356        OfferBrain(Brains.IndexOf(BrainRandom));
    13311357        inc(FixedLines);
    13321358      end;
    1333       for i := bixFirstAI to nBrain - 1 do // offer available AIs
    1334         if (Brain[i].Flags and fMultiple <> 0) or (Brain[i].Flags and fUsed = 0)
     1359      AIBrains := TBrains.Create(False);
     1360      Brains.GetByKind(btAI, AIBrains);
     1361      for i := 0 to AIBrains.Count - 1 do // offer available AIs
     1362        if (AIBrains[i].Flags and fMultiple <> 0) or (AIBrains[i].Flags and fUsed = 0)
    13351363          or (i = bixView[bixPopup]) then
    1336           OfferBrain(i);
     1364          OfferBrain(Brains.IndexOf(AIBrains[i]));
     1365      AIBrains.Free;
    13371366    end;
    13381367  end
     
    13971426          if i = 0 then
    13981427          begin
    1399             bixView[0] := bixSuper_Virtual;
     1428            bixView[0] := Brains.IndexOf(BrainSuperVirtual);
    14001429            Difficulty[0] := 0
    14011430          end;
    1402           if bixView[0] < bixTerm then
     1431          if Brains[bixView[0]].Kind in [btNoTerm, btSuperVirtual] then
    14031432            inc(i);
    14041433          if i > nPl then
     
    14211450                Difficulty[p1] := ReadInteger('Diff' + IntToStr(p1));
    14221451                if s <> '' then
    1423                   for j := 0 to nBrain - 1 do
    1424                     if AnsiCompareFileName(s, Brain[j].FileName) = 0 then
     1452                  for j := 0 to Brains.Count - 1 do
     1453                    if AnsiCompareFileName(s, Brains[j].FileName) = 0 then
    14251454                      bixView[p1] := j;
    14261455              end;
     
    14411470        SlotAvailable := InitAlive[i];
    14421471        for i := 0 to nPlOffered - 1 do
    1443           if (AutoDiff < 0) and (bixView[i] >= bixTerm) then
     1472          if (AutoDiff < 0) and (Brains[bixView[i]].Kind in [btTerm, btRandom, btAI]) then
    14441473          begin
    14451474            DiffUpBtn[i].Tag := 768;
     
    14521481          end;
    14531482        for i := 6 to 8 do
    1454           if (AutoDiff < 0) and (bixView[i] >= bixTerm) then
     1483          if (AutoDiff < 0) and (Brains[bixView[i]].Kind in [btTerm, btRandom, btAI]) then
    14551484          begin
    14561485            MultiBtn[i].Tag := 768;
     
    15931622    (x >= xDefault) and (y >= yDefault) and (x < xDefault + 64) and
    15941623    (y < yDefault + 64) then
    1595     if nBrain < bixFirstAI + 2 then
     1624    if Brains.GetKindCount(btAI) < 2 then
    15961625      SimpleMessage(Phrases.Lookup('NOALTAI'))
    15971626    else
Note: See TracChangeset for help on using the changeset viewer.