unit MainForm;

{$mode objfpc}{H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ComCtrls, Spin, ExtCtrls, Grids, FileCtrl, uHCTypeDefs, uHCAnnexDefs,
  uHCAnnexHelpers, types;

type

  { TfrmMain }

  TfrmMain = class(TForm)
    btnCreateGroups: TButton;
    edGroups2Create: TEdit;
    Label1: TLabel;
    lblGroups: TLabel;
    lblGroupsBlueRed: TLabel;
    lblGroupsBlue: TLabel;
    lblGroupsRed: TLabel;
    lblGroupsGreen: TLabel;
    lbxSides: TListBox;
    MLbtnPostMessage: TButton;
    MLedtMessageToPost: TEdit;
    SGbtnRefresh: TButton;
    SGlblUnitIDName: TLabel;
    SGlblUnitInfo: TLabel;
    SGlblUnitInfo1: TLabel;
    SGpnlUnit: TPanel;
    SGlblGroups: TLabel;
    SGlbxGroups: TListBox;
    SGpnl1: TPanel;
    memDLLInit: TMemo;
    tshStartGroup: TTabSheet;
    tshDLLInit: TTabSheet;
    MLckbEnabled: TCheckBox;
    MLlblLines: TLabel;
    MLmemMsgLog: TMemo;
    PageControl1: TPageControl;
    MLedtLines: TSpinEdit;
    tshMsgLog: TTabSheet;
    procedure GTbtnResumeClick(Sender: TObject);
    procedure MLbtnPostMessageClick(Sender: TObject);
    procedure SGbtnRefreshClick(Sender: TObject);
    procedure btnCreateGroupsClick(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
    // ALE 20130812 Big bad pointers that hold most of the game data
    PStartGroup: PHCGroup;
    PHarpEventHead: PHCHarpoonEvent;
    PSonobuoyRegionHead: PHCSonobuoyRegion;
    PAllDeadUnitsPtr: PHCDeadListItem;
    PWeatherGroups: PHCGroup;
    PVictoryConditionsList: PHCVictoryConditions;
    // ALE 20130812 transient pointers
    PSelectedUnit: PHCUnit;
    // ALE 20130812 misc globals
    //blnInvestigatingStructs: Boolean;
    // ALE 20130908 pointers to functions exported by the GE
    HCQueueHarpoonEvent: THCQueueHarpoonEvent;
    NewPtr: THCNewPtr;
    DisposPtr: THCDisposPtr;
    HCdllCheckLatLonDepth: THCdllCheckLatLonDepth;
    HCannex_id_to_disk_id: THCannex_id_to_disk_id;
    HCdisk_id_to_annex_id: THCdisk_id_to_annex_id;
    HCMessageLogLine: THCMessageLogLine;
    HCassign_group_id: THCassign_group_id;
    HCCreateGroup : THCdllCreateGroup;
    procedure PopulateGroupList(const pListBox: TListBox);
    procedure CountGroups();
  end;

var
  frmMain: TfrmMain;

implementation

uses uTestGroupIdsGlobals, uHCDefines, uHCEventHelpers, uHCUtils;

{$R *.lfm}

{ TfrmMain }

procedure TfrmMain.GTbtnResumeClick(Sender: TObject);
begin
  byte(PDLLBusy^) := CFALSE; // ALE 20130816
end;

procedure TfrmMain.MLbtnPostMessageClick(Sender: TObject);
begin
  HCMessageLogLine(PAnsiChar(MLedtMessageToPost.Text));
end;

procedure TfrmMain.SGbtnRefreshClick(Sender: TObject);
begin
  CountGroups();
  // ALE 20130317 Fill up the SGlbxGroups
  PopulateGroupList(SGlbxGroups);
end;

procedure TfrmMain.btnCreateGroupsClick(Sender: TObject);
var
   lC: NativeUInt;
   lPHCGroup: PHCGroup;
begin
  for lC := 0 to StrToInt(edGroups2Create.Text) - 1 do
  begin
    lPHCGroup := HCCreateGroup(lbxSides.ItemIndex, Random(45), Random(90));
    // ALE 20220202 we've created an empty group, the below crashes the GE
    {
    if (lPHCGroup <> nil) then
    begin
      lPHCGroup^.gtype := ShortInt(UT_AIRFIELD);
      lPHCGroup^.id[2] := UT_AIRFIELD;
    end;
    }
  end;
  // ALE 20220202 crashes GE PopulateGroupList(frmMain.SGlbxGroups);
  CountGroups();
end;

procedure TfrmMain.PopulateGroupList(const pListBox: TListBox);
var
  PHCGThis: PHCGroup;
begin
  byte(PDLLBusy^) := CTRUE;
  // ALE 20130317 Fill up the SGlbxGroups
  pListBox.Items.BeginUpdate;
  pListBox.Items.Clear;
  pListBox.Sorted := True;
  PHCGThis := PStartGroup;
  while (PHCGThis <> nil) do
  begin
    pListBox.Items.Add(PHCGThis^.id);
    PHCGThis := PHCGThis^.toSouth;
  end;
  pListBox.Items.EndUpdate;
  byte(PDLLBusy^) := CFALSE;
end;

procedure TfrmMain.CountGroups();
var
  lPGroup: PHCGroup;
  lGroups, lGroupsBlueRed, lGroupsBlue, lGroupsRed, lGroupsGreen: NativeInt;
begin
  byte(PDLLBusy^) := CTRUE;
  lPGroup := PStartGroup;
  if (lPGroup <> nil) then
  begin
    lGroups := 0;
    lGroupsBlueRed := 0;
    lGroupsBlue := 0;
    lGroupsRed := 0;
    lGroupsGreen := 0;

    while (lPGroup <> nil) do
    begin
      Inc(lGroups);
      Case lPGroup^.side of
        BLUESIDE:begin Inc(lGroupsBlue);  Inc(lGroupsBlueRed); end;
        REDSIDE: begin Inc(lGroupsRed);  Inc(lGroupsBlueRed); end;
        GREENSIDE: Inc(lGroupsGreen);
      end;
      lPGroup := lPGroup^.toSouth;
    end;
  end
  else
  begin
    lGroups := -1;
    lGroupsBlueRed := -1;
    lGroupsBlue := -1;
    lGroupsRed := -1;
    lGroupsGreen := -1;
  end;
  lblGroups.Caption := 'Groups: ' + IntToStr(lGroups);
  lblGroupsBlueRed.Caption := 'Blue + Red Groups: ' + IntToStr(lGroupsBlueRed);
  lblGroupsBlue.Caption := 'Blue Groups: ' + IntToStr(lGroupsBlue);
  lblGroupsRed.Caption := 'Red Groups: ' + IntToStr(lGroupsRed);
  lblGroupsGreen.Caption := 'Green Groups: ' + IntToStr(lGroupsGreen);
  byte(PDLLBusy^) := CFALSE;
end;


end.
