unit UFormNew;

{$mode delphi}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ComCtrls, Spin, ExtCtrls, ActnList, ExtDlgs, Menus, UGame, UGeometry, UPlayer,
  UGameServer, UServerList, UMap, UGameSystem, UFormList;

type

  { TFormNew }

  TFormNew = class(TForm)
    ActionList1: TActionList;
    AServerAdd: TAction;
    AServerModify: TAction;
    AServerRemove: TAction;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    ButtonGameSystems: TButton;
    ButtonRandomize: TButton;
    ButtonCancel: TButton;
    ButtonImageBrowse: TButton;
    ButtonOk: TButton;
    CheckBoxBridges: TCheckBox;
    CheckBoxCity: TCheckBox;
    CheckBoxCyclicMap: TCheckBox;
    CheckBoxFogOfWar: TCheckBox;
    CheckBoxSymetricMap: TCheckBox;
    CheckBoxVoid: TCheckBox;
    ComboBoxGameSystem: TComboBox;
    ComboBoxGridType: TComboBox;
    ComboBoxMapShape: TComboBox;
    ComboBoxWinObjective: TComboBox;
    EditImageFile: TEdit;
    EditServerAddress: TEdit;
    Image1: TImage;
    Label1: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label14: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    ListViewServers: TListView;
    MenuItem4: TMenuItem;
    MenuItem5: TMenuItem;
    MenuItem6: TMenuItem;
    OpenPictureDialog1: TOpenPictureDialog;
    PageControl1: TPageControl;
    PageControlWinObjective: TPageControl;
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    Panel4: TPanel;
    Panel5: TPanel;
    PanelButtons: TPanel;
    PanelChat: TPanel;
    PopupMenuServers: TPopupMenu;
    RadioButtonModeLocal: TRadioButton;
    RadioButtonModeNetworkClient: TRadioButton;
    RadioButtonModeNetworkServer: TRadioButton;
    RadioGroupGrowAmount: TRadioGroup;
    RadioGroupGrowCells: TRadioGroup;
    SpinEditCityPercent: TSpinEdit;
    SpinEditMapSizeX: TSpinEdit;
    SpinEditMapSizeY: TSpinEdit;
    SpinEditMaxPower: TSpinEdit;
    SpinEditNeutralUnits: TSpinEdit;
    SpinEditServerPort: TSpinEdit;
    SpinEditSpecialCells: TSpinEdit;
    SpinEditTurns: TSpinEdit;
    SpinEditVoidPercent: TSpinEdit;
    Splitter1: TSplitter;
    TabSheetUnits: TTabSheet;
    TabSheetCaptureEntireMap: TTabSheet;
    TabSheetNone: TTabSheet;
    TabSheetCaptureCells: TTabSheet;
    TabSheetCaptureCities: TTabSheet;
    TabSheetDefeatEnemies: TTabSheet;
    TabSheetPlayers: TTabSheet;
    TabSheetMap: TTabSheet;
    TabSheetRules: TTabSheet;
    TabSheetMode: TTabSheet;
    TabSheetStayAliveTurns: TTabSheet;
    TimerMapPreview: TTimer;
    TrackBarSizeX: TTrackBar;
    TrackBarSizeY: TTrackBar;
    procedure AServerAddExecute(Sender: TObject);
    procedure AServerModifyExecute(Sender: TObject);
    procedure AServerRemoveExecute(Sender: TObject);
    procedure ButtonGameSystemsClick(Sender: TObject);
    procedure ButtonImageBrowseClick(Sender: TObject);
    procedure ButtonOkClick(Sender: TObject);
    procedure ButtonRandomizeClick(Sender: TObject);
    procedure CheckBoxBridgesChange(Sender: TObject);
    procedure CheckBoxCityChange(Sender: TObject);
    procedure CheckBoxCyclicMapChange(Sender: TObject);
    procedure CheckBoxSymetricMapChange(Sender: TObject);
    procedure CheckBoxVoidChange(Sender: TObject);
    procedure ComboBoxGameSystemChange(Sender: TObject);
    procedure ComboBoxGridTypeChange(Sender: TObject);
    procedure ComboBoxMapShapeChange(Sender: TObject);
    procedure ComboBoxWinObjectiveChange(Sender: TObject);
    procedure EditImageFileChange(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Image1Resize(Sender: TObject);
    procedure ListViewServersData(Sender: TObject; Item: TListItem);
    procedure ListViewServersKeyPress(Sender: TObject; var Key: char);
    procedure ListViewServersSelectItem(Sender: TObject; Item: TListItem;
      Selected: Boolean);
    procedure PageControl1Change(Sender: TObject);
    procedure RadioButtonModeLocalChange(Sender: TObject);
    procedure SpinEditCityPercentChange(Sender: TObject);
    procedure SpinEditMapSizeXChange(Sender: TObject);
    procedure SpinEditMapSizeYChange(Sender: TObject);
    procedure SpinEditMaxPowerChange(Sender: TObject);
    procedure SpinEditNeutralUnitsChange(Sender: TObject);
    procedure SpinEditVoidPercentChange(Sender: TObject);
    procedure TabSheetMapShow(Sender: TObject);
    procedure TimerMapPreviewTimer(Sender: TObject);
    procedure TrackBarSizeYChange(Sender: TObject);
    procedure TrackBarSizeXChange(Sender: TObject);
  private
    FServer: TServer;
    ServerList: TServerList;
    GamePreview: TGame;
    MapPreviewRedrawPending: Boolean;
    NewRandSeed: Cardinal;
    FormPlayers: TFormList;
    procedure LoadGame(Game: TGame);
    procedure SaveGame(Game: TGame);
    procedure Load(Server: TServer);
    procedure Save(Server: TServer);
    procedure MapPreviewPaint;
    procedure MapPreviewRedraw;
    procedure ReloadGameSystemsMenu;
    procedure SetServer(AValue: TServer);
  public
    procedure Translate;
    procedure ReloadView;
    procedure UpdateInterface;
    property Server: TServer read FServer write SetServer;
  end;

var
  FormNew: TFormNew;


implementation

{$R *.lfm}

uses
  UFormChat, UCore, UFormServer, UClientGUI, UFormClient,
  UFormGameSystems, UMapType;

resourcestring
  SWinObjectiveNone = 'None';
  SWinObjectiveDefeatAllOponents = 'Defeat all oponents';
  SWinObjectiveDefeatAllCities = 'Defeat all oponents cities';
  SWinObjectiveCapturePosition = 'Capture all special cells';
  SWinObjectiveStayAliveForTurns = 'Stay alive for number of turns';
  SWinObjectiveCaptureEntireMap = 'Capture entire map';
  SGrowNone = 'None';
  SGrowPlayerCities = 'Player cities';
  SGrowPlayerAllCells = 'Player all cells';
  SGrowAmountByOne = 'By one';
  SGrowAmountBySquareRoot = 'By square root';
  SRectangular = 'Rectangular';
  SRounded = 'Rounded';
  SFromImageFile = 'From image file';
  SRemoveServer = 'Remove server';
  SRemoveServerQuery = 'Do you want to remove server?';

{ TFormNew }

procedure TFormNew.ListViewServersData(Sender: TObject; Item: TListItem);
begin
  if Item.Index < ServerList.Items.Count then
  with TServerInfo(ServerList.Items[Item.Index]) do begin
    Item.Caption := Name;
    Item.Data := ServerList.Items[Item.Index];
    Item.SubItems.Add(Address + ':' + IntToStr(Port));
  end;
end;

procedure TFormNew.ListViewServersKeyPress(Sender: TObject; var Key: char);
begin
  if Key = #13 then AServerModify.Execute;
end;

procedure TFormNew.ListViewServersSelectItem(Sender: TObject; Item: TListItem;
  Selected: Boolean);
begin
  UpdateInterface;
end;

procedure TFormNew.PageControl1Change(Sender: TObject);
begin
  MapPreviewRedraw;
end;

procedure TFormNew.RadioButtonModeLocalChange(Sender: TObject);
begin
  UpdateInterface;
end;

procedure TFormNew.SpinEditCityPercentChange(Sender: TObject);
begin
  MapPreviewRedraw;
end;

procedure TFormNew.SpinEditMapSizeXChange(Sender: TObject);
begin
  TrackBarSizeX.Position := SpinEditMapSizeX.Value;
  MapPreviewRedraw;
end;

procedure TFormNew.SpinEditMapSizeYChange(Sender: TObject);
begin
  TrackBarSizeY.Position := SpinEditMapSizeY.Value;
  MapPreviewRedraw;
end;

procedure TFormNew.SpinEditMaxPowerChange(Sender: TObject);
begin
  UpdateInterface;
end;

procedure TFormNew.SpinEditNeutralUnitsChange(Sender: TObject);
begin
  MapPreviewRedraw;
end;

procedure TFormNew.SpinEditVoidPercentChange(Sender: TObject);
begin
  MapPreviewRedraw;
end;

procedure TFormNew.TabSheetMapShow(Sender: TObject);
begin
  MapPreviewRedraw;
end;

procedure TFormNew.TimerMapPreviewTimer(Sender: TObject);
begin
  if MapPreviewRedrawPending then begin
    MapPreviewPaint;
    MapPreviewRedrawPending := False;
  end;
end;

procedure TFormNew.TrackBarSizeYChange(Sender: TObject);
begin
  SpinEditMapSizeY.Value := TrackBarSizeY.Position;
end;

procedure TFormNew.TrackBarSizeXChange(Sender: TObject);
begin
  SpinEditMapSizeX.Value := TrackBarSizeX.Position;
end;

procedure TFormNew.LoadGame(Game: TGame);
begin
  with Game do begin
    CheckBoxSymetricMap.Checked := SymetricMap;
    CheckBoxCyclicMap.Checked := CyclicMap;
    CheckBoxVoid.Checked := VoidEnabled;
    SpinEditVoidPercent.Value := VoidPercentage;
    SpinEditVoidPercent.Enabled := VoidEnabled;
    CheckBoxCity.Checked := CityEnabled;
    SpinEditCityPercent.Value := CityPercentage;
    SpinEditMapSizeX.Value := Map.Size.X;
    SpinEditMapSizeY.Value := Map.Size.Y;
    TrackBarSizeX.Position := Map.Size.X;
    TrackBarSizeY.Position := Map.Size.Y;
    RadioGroupGrowAmount.ItemIndex := Integer(GrowAmount);
    RadioGroupGrowCells.ItemIndex := Integer(GrowCells);
    ComboBoxGridType.ItemIndex := ComboBoxGridType.Items.IndexOfObject(TObject(MapType));
    ComboBoxWinObjective.ItemIndex := Integer(WinObjective);
    SpinEditNeutralUnits.Value := MaxNeutralUnits;
    ComboBoxMapShape.ItemIndex := Integer(Map.Shape);
    EditImageFile.Text := MapImageFileName;
    CheckBoxFogOfWar.Checked := FogOfWar;
    CheckBoxBridges.Checked := BridgeEnabled;
    SpinEditMaxPower.Value := MaxPower;
    SpinEditTurns.Value := StayAliveForDefinedTurns;
    SpinEditSpecialCells.Value := SpecialCaptureCellCount;
    ComboBoxGameSystem.ItemIndex := ComboBoxGameSystem.Items.IndexOf(GameSystem.GetName);
    if (ComboBoxGameSystem.ItemIndex = -1) and (ComboBoxGameSystem.Items.Count > 0) then
      ComboBoxGameSystem.ItemIndex := 0;
  end;
end;

procedure TFormNew.SaveGame(Game: TGame);
begin
  with Game do begin
    StoredRandSeed := NewRandSeed;
    SymetricMap := CheckBoxSymetricMap.Checked;
    CyclicMap := CheckBoxCyclicMap.Checked;
    VoidEnabled := CheckBoxVoid.Checked;
    VoidPercentage := SpinEditVoidPercent.Value;
    CityEnabled := CheckBoxCity.Checked;
    CityPercentage := SpinEditCityPercent.Value;
    Map.Cyclic := CyclicMap;
    Map.Size := TPoint.Create(SpinEditMapSizeX.Value, SpinEditMapSizeY.Value);
    GrowAmount := TGrowAmount(RadioGroupGrowAmount.ItemIndex);
    GrowCells := TGrowCells(RadioGroupGrowCells.ItemIndex);
    MapType := TMapType(ComboBoxGridType.Items.Objects[ComboBoxGridType.ItemIndex]);
    WinObjective := TWinObjective(ComboBoxWinObjective.ItemIndex);
    MaxNeutralUnits := SpinEditNeutralUnits.Value;
    Map.Shape := TMapShape(ComboBoxMapShape.ItemIndex);
    MapImageFileName := EditImageFile.Text;
    FogOfWar := CheckBoxFogOfWar.Checked;
    BridgeEnabled := CheckBoxBridges.Checked;
    MaxPower := SpinEditMaxPower.Value;
    StayAliveForDefinedTurns := SpinEditTurns.Value;
    SpecialCaptureCellCount := SpinEditSpecialCells.Value;
  end;
end;

procedure TFormNew.MapPreviewRedraw;
begin
  MapPreviewRedrawPending := True;
end;

procedure TFormNew.ReloadGameSystemsMenu;
var
  I: Integer;
  LastSystem: Pointer;
begin
  if ComboBoxGameSystem.ItemIndex <> -1 then
    LastSystem := ComboBoxGameSystem.Items.Objects[ComboBoxGameSystem.ItemIndex]
    else LastSystem := nil;
  ComboBoxGameSystem.Items.BeginUpdate;
  try
    while ComboBoxGameSystem.Items.Count > Core.GameSystems.Count do
      ComboBoxGameSystem.Items.Delete(ComboBoxGameSystem.Items.Count - 1);
    while ComboBoxGameSystem.Items.Count < Core.GameSystems.Count do
      ComboBoxGameSystem.Items.Add('');
    for I := 0 to Core.GameSystems.Count - 1 do begin
      ComboBoxGameSystem.Items[I] := Core.GameSystems[I].GetName;
      ComboBoxGameSystem.Items.Objects[I] := Core.GameSystems[I];
    end;
    if Assigned(LastSystem) then
      ComboBoxGameSystem.ItemIndex := ComboBoxGameSystem.Items.IndexOfObject(LastSystem);
    if (ComboBoxGameSystem.ItemIndex = -1) and (ComboBoxGameSystem.Items.Count > 0) then
      ComboBoxGameSystem.ItemIndex := 0;
  finally
    ComboBoxGameSystem.Items.EndUpdate;
  end;
  if (ComboBoxGameSystem.ItemIndex = -1) and (ComboBoxGameSystem.Items.Count > 0) then
    ComboBoxGameSystem.ItemIndex := 0;
end;

procedure TFormNew.SetServer(AValue: TServer);
begin
  if FServer = AValue then Exit;
  FServer := AValue;
  if Assigned(FServer) then begin
    Load(FServer);
    FormPlayers.List := FServer.Game.Players;
  end else begin
    FormPlayers.List := nil;
  end;
end;

procedure TFormNew.MapPreviewPaint;
var
  Client: TClientGUI;
  Bitmap: TBitmap;
begin
  Image1.Height := Round(Image1.Width * SpinEditMapSizeY.Value / SpinEditMapSizeX.Value);
  GamePreview.GameSystem.Assign(FServer.Game.GameSystem);
  SaveGame(GamePreview);
  GamePreview.PostConfig;
  GamePreview.GeneratePlayers := False;
  GamePreview.New;
  GamePreview.CurrentPlayer := TPlayer(GamePreview.Players.First);
  Bitmap := Image1.Picture.Bitmap;
  Bitmap.SetSize(Image1.Width, Image1.Height);
  Client := TClientGUI.Create;
  Client.Game := GamePreview;
  Client.View.Game := GamePreview;
  Client.View.DestRect := TRect.Create(TPoint.Create(0, 0), TPoint.Create(Image1.Width, Image1.Height));
  Client.View.ZoomAll;
  //Client.ControlPlayer := GamePreview.Players.First;
  Client.CellGridVisible := True;
  Bitmap.Canvas.Brush.Color := MapBackgroundColor; //clBackground; //PaintBox1.GetColorResolvingParent;
  Bitmap.Canvas.FillRect(0, 0, Bitmap.Width, Bitmap.Height);
  Client.Paint(Bitmap.Canvas, Client.View);
  Client.Free;
end;

procedure TFormNew.Translate;
var
  LastIndex: Integer;
begin
  with ComboBoxGridType do begin
    LastIndex := ItemIndex;
    Clear;
    Items.AddObject(SGridTypeHexagonVertical, TObject(mtHexagonVertical));
    Items.AddObject(SGridTypeHexagonHorizontal, TObject(mtHexagonHorizontal));
    Items.AddObject(SGridTypeSquare, TObject(mtSquare));
    Items.AddObject(SGridTypeTriangle, TObject(mtTriangle));
    Items.AddObject(SGridTypeRandom, TObject(mtRandom));
    Items.AddObject(SGridTypeIsometric, TObject(mtIsometric));
    ItemIndex := LastIndex;
  end;
  with ComboBoxWinObjective do begin
    LastIndex := ItemIndex;
    Clear;
    Items.Add(SWinObjectiveNone);
    Items.Add(SWinObjectiveDefeatAllOponents);
    Items.Add(SWinObjectiveDefeatAllCities);
    Items.Add(SWinObjectiveCapturePosition);
    Items.Add(SWinObjectiveStayAliveForTurns);
    Items.Add(SWinObjectiveCaptureEntireMap);
    ItemIndex := LastIndex;
  end;
  with ComboBoxMapShape do begin
    LastIndex := ItemIndex;
    Clear;
    Items.Add(SRectangular);
    Items.Add(SFromImageFile);
    Items.Add(SRounded);
    ItemIndex := LastIndex;
  end;
  with RadioGroupGrowCells do begin
    LastIndex := ItemIndex;
    Items.Clear;
    Items.Add(SGrowNone);
    Items.Add(SGrowPlayerCities);
    Items.Add(SGrowPlayerAllCells);
    ItemIndex := LastIndex;
  end;
  with RadioGroupGrowAmount do begin
    LastIndex := ItemIndex;
    Items.Clear;
    Items.Add(SGrowAmountByOne);
    Items.Add(SGrowAmountBySquareRoot);
    ItemIndex := LastIndex;
  end;
  Core.CoolTranslator1.TranslateComponentRecursive(Self);
end;

procedure TFormNew.ReloadView;
begin
  ListViewServers.Items.Count := ServerList.Items.Count;
  ListViewServers.Refresh;
  UpdateInterface;
end;

procedure TFormNew.UpdateInterface;
var
  WinObjective: TWinObjective;
begin

  ComboBoxGridType.Enabled := FServer.Game.GameSystem.PreferedMapType = mtNone;
  if FServer.Game.GameSystem.PreferedMapType <> mtNone then
    ComboBoxGridType.ItemIndex := ComboBoxGridType.Items.IndexOfObject(TObject(FServer.Game.GameSystem.PreferedMapType));
  EditImageFile.Enabled := ComboBoxMapShape.ItemIndex = Integer(msImage);
  ButtonImageBrowse.Enabled := ComboBoxMapShape.ItemIndex = Integer(msImage);
  EditServerAddress.Enabled := RadioButtonModeNetworkServer.Checked;
  SpinEditServerPort.Enabled := RadioButtonModeNetworkServer.Checked;
  ListViewServers.Enabled := RadioButtonModeNetworkClient.Checked;
  PanelChat.Visible := RadioButtonModeNetworkClient.Checked or RadioButtonModeNetworkServer.Checked;
  SpinEditNeutralUnits.MaxValue := SpinEditMaxPower.Value;
  AServerRemove.Enabled := RadioButtonModeNetworkClient.Checked and Assigned(ListViewServers.Selected);
  AServerAdd.Enabled := RadioButtonModeNetworkClient.Checked;
  AServerModify.Enabled := RadioButtonModeNetworkClient.Checked;
  WinObjective := TWinObjective(ComboBoxWinObjective.ItemIndex);
  case WinObjective of
    woNone: PageControlWinObjective.TabIndex := PageControlWinObjective.IndexOf(TabSheetNone);
    woDefeatAllOponents: PageControlWinObjective.TabIndex := PageControlWinObjective.IndexOf(TabSheetDefeatEnemies);
    woDefeatAllOponentsCities: PageControlWinObjective.TabIndex := PageControlWinObjective.IndexOf(TabSheetCaptureCities);
    woSpecialCaptureCell: PageControlWinObjective.TabIndex := PageControlWinObjective.IndexOf(TabSheetCaptureCells);
    woStayAliveForDefinedTurns: PageControlWinObjective.TabIndex := PageControlWinObjective.IndexOf(TabSheetStayAliveTurns);
    woCaptureEntireMap: PageControlWinObjective.TabIndex := PageControlWinObjective.IndexOf(TabSheetCaptureEntireMap);
  end;
end;

procedure TFormNew.FormShow(Sender: TObject);
begin
  Randomize;
  NewRandSeed := RandSeed;
  Core.PersistentForm.Load(Self);
  Core.ThemeManager1.UseTheme(Self);
  ReloadView;
  //Height := Trunc(1.5 * Height);
  //PageControl1.TabIndex := 0;
  {$IFNDEF DEBUG}
  TabSheetMode.TabVisible := False;
  TabSheetMode.Enabled := False;
  {$ENDIF}
end;

procedure TFormNew.Image1Resize(Sender: TObject);
begin
  MapPreviewRedraw;
end;

procedure TFormNew.FormCreate(Sender: TObject);
begin
  GamePreview := TGame.Create;
  ServerList := TServerList.Create;
  FormChat := TFormChat.Create(nil);
  FormChat.ManualDock(PanelChat, nil, alClient);
  FormChat.Align := alClient;
  FormChat.Show;
  FormPlayers := TFormList.Create(nil);
  FormPlayers.MinItemCount := MinPlayerCount;
  FormPlayers.MaxItemCount := MaxPlayerCount;
  FormPlayers.ManualDock(Panel2, nil, alClient);
  FormPlayers.Align := alClient;
  FormPlayers.Visible := True;
  Translate;
end;

procedure TFormNew.CheckBoxCityChange(Sender: TObject);
begin
  SpinEditCityPercent.Enabled := CheckBoxCity.Checked;
  MapPreviewRedraw;
end;

procedure TFormNew.CheckBoxCyclicMapChange(Sender: TObject);
begin
  MapPreviewRedraw;
end;

procedure TFormNew.CheckBoxSymetricMapChange(Sender: TObject);
begin
  MapPreviewRedraw;
end;

procedure TFormNew.CheckBoxVoidChange(Sender: TObject);
begin
  SpinEditVoidPercent.Enabled := CheckBoxVoid.Checked;
  MapPreviewRedraw;
end;

procedure TFormNew.ComboBoxGameSystemChange(Sender: TObject);
begin
  FServer.Game.GameSystem.Assign(TGameSystem(ComboBoxGameSystem.Items.Objects[ComboBoxGameSystem.ItemIndex]));
  UpdateInterface;
  MapPreviewRedraw;
end;

procedure TFormNew.ComboBoxGridTypeChange(Sender: TObject);
begin
  MapPreviewRedraw;
end;

procedure TFormNew.ComboBoxMapShapeChange(Sender: TObject);
begin
  ReloadView;
  MapPreviewRedraw;
end;

procedure TFormNew.ComboBoxWinObjectiveChange(Sender: TObject);
begin
  UpdateInterface;
end;

procedure TFormNew.EditImageFileChange(Sender: TObject);
begin
  MapPreviewRedraw;
end;

procedure TFormNew.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  Core.PersistentForm.Save(Self);
end;

procedure TFormNew.AServerAddExecute(Sender: TObject);
var
  NewServerInfo: TServerInfo;
begin
  FormServer := TFormServer.Create(nil);
  NewServerInfo := TServerInfo.Create;
  FormServer.ServerInfo := NewServerInfo;
  if FormServer.ShowModal = mrOK then begin
    ServerList.Items.Add(NewServerInfo);
    ReloadView;
  end else NewServerInfo.Free;
  FormServer.Free;
end;

procedure TFormNew.AServerModifyExecute(Sender: TObject);
var
  ServerInfo: TServerInfo;
begin
  FormServer := TFormServer.Create(nil);
  ServerInfo := TServerInfo(ListViewServers.Selected.Data);
  FormServer.ServerInfo := ServerInfo;
  if FormServer.ShowModal = mrOK then begin
    ReloadView;
  end;
  FormServer.Free;
end;

procedure TFormNew.AServerRemoveExecute(Sender: TObject);
var
  I: Integer;
begin
  if MessageDlg(SRemoveServer, SRemoveServerQuery,
  TMsgDlgType.mtConfirmation, [mbCancel, mbOk], 0) = mrOk then begin
    for I := ListViewServers.Items.Count - 1 downto 0 do
    if ListViewServers.Items[I].Selected then
      ServerList.Items.Delete(I);
    ReloadView;
  end;
end;

procedure TFormNew.ButtonGameSystemsClick(Sender: TObject);
begin
  FormGameSystems := TFormGameSystems.Create(nil);
  try
    FormGameSystems.GameSystems := Core.GameSystems;
    FormGameSystems.ShowModal;
    ReloadGameSystemsMenu;
  finally
    FreeAndNil(FormGameSystems);
  end;
end;

procedure TFormNew.ButtonImageBrowseClick(Sender: TObject);
begin
  OpenPictureDialog1.FileName := EditImageFile.Text;
  if OpenPictureDialog1.Execute then begin
    EditImageFile.Text := OpenPictureDialog1.FileName;
  end;
end;

procedure TFormNew.ButtonOkClick(Sender: TObject);
begin
  Save(FServer);
end;

procedure TFormNew.ButtonRandomizeClick(Sender: TObject);
begin
  Randomize;
  NewRandSeed := RandSeed;
  MapPreviewRedraw;
end;

procedure TFormNew.CheckBoxBridgesChange(Sender: TObject);
begin
  MapPreviewRedraw;
end;

procedure TFormNew.FormDestroy(Sender: TObject);
begin
  FreeAndNil(ServerList);
  FreeAndNil(FormChat);
  FreeAndNil(FormPlayers);
  FreeAndNil(GamePreview);
end;

procedure TFormNew.Load(Server: TServer);
begin
  ReloadGameSystemsMenu;
  with Server do begin
    RadioButtonModeLocal.Checked := Mode = smLocal;
    RadioButtonModeNetworkClient.Checked := Mode = smNetworkClient;
    RadioButtonModeNetworkServer.Checked := Mode = smNetworkServer;
    EditServerAddress.Text := LocalNetworkAddress;
    SpinEditServerPort.Value := LocalNetworkPort;
  end;
  ServerList.Assign(Core.ServerList);
  LoadGame(Server.Game);
end;

procedure TFormNew.Save(Server: TServer);
begin
  with Server do begin
    if RadioButtonModeLocal.Checked then Mode := smLocal
    else if RadioButtonModeNetworkClient.Checked then Mode := smNetworkClient
    else if RadioButtonModeNetworkServer.Checked then Mode := smNetworkServer;
    LocalNetworkAddress := EditServerAddress.Text;
    LocalNetworkPort := SpinEditServerPort.Value;
    if Assigned(ListViewServers.Selected) then begin
      RemoteNetworkAddress := TServerInfo(ListViewServers.Selected.Data).Address;
      RemoteNetworkPort := TServerInfo(ListViewServers.Selected.Data).Port;
    end else begin
      RemoteNetworkAddress := '';
      RemoteNetworkPort := 0;
    end;
  end;
  Core.ServerList.Assign(ServerList);
  SaveGame(Server.Game);
end;

end.

