Changeset 202 for trunk/UCore.pas


Ignore:
Timestamp:
May 17, 2018, 5:41:47 PM (6 years ago)
Author:
chronos
Message:
  • Modified: AI player related code moved to UClientAI unit. It is now extension of TClient class to simulate regular human client.
  • Modified: More work on client-server architecture.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UCore.pas

    r199 r202  
    99  UGame, UApplicationInfo, UPersistentForm, UScaleDPI, UCoolTranslator,
    1010  URegistry, ULastOpenedList, Registry, Menus, UGeometry, UFormClient,
    11   UGameServer, UGameClient, fgl, UServerList;
     11  UGameServer, UGameClient, fgl, UServerList, UGameProtocol;
    1212
    1313type
     
    6565    GameLoaded: Boolean;
    6666    procedure LoadRecentExecute(Sender: TObject);
    67     procedure ProcessComputerTurns;
    6867    procedure StartNewGame;
    6968    procedure DoPlayerChange(Sender: TObject);
     
    9190    ReopenLastFile: Boolean;
    9291    FormClients: TFPGObjectList<TFormClient>;
    93     Clients: TClients;
     92    LocalClients: TClients;
    9493    ServerList: TServerList;
    9594    procedure Spectate(Player: TPlayer);
     
    109108
    110109uses
    111   UFormMain, UFormNew, UFormSettings, UFormAbout,
     110  UFormMain, UFormNew, UFormSettings, UFormAbout, UClientAI,
    112111  UFormHelp, UFormCharts, UFormUnitMoves, UFormPlayersStats;
    113112
     
    240239begin
    241240  FirstHuman := Game.Players.GetFirstHuman;
    242   if Assigned(FirstHuman) then FormClient.Client := Clients.SearchPlayer(FirstHuman)
    243     else FormClient.Client := TClient(Clients.First);
     241  if Assigned(FirstHuman) then FormClient.Client := LocalClients.SearchPlayer(FirstHuman)
     242    else FormClient.Client := TClient(LocalClients.First);
    244243end;
    245244
     
    275274end;
    276275
    277 procedure TCore.ProcessComputerTurns;
    278 begin
    279   while Game.Running and (Game.CurrentPlayer.Mode <> pmHuman) do begin
    280     if Game.CurrentPlayer.Mode = pmComputer then begin
    281       Game.CurrentPlayer.Computer.Process;
    282       RedrawClients;
    283       Delay(Trunc((100 - AnimationSpeed) / 100 * 2000));
    284     end;
    285     Game.NextTurn;
    286     RedrawClients;
    287     Application.ProcessMessages;
    288     Sleep(1);
    289   end;
    290 end;
    291 
    292276procedure TCore.AGameEndTurnExecute(Sender: TObject);
    293277begin
    294   Game.NextTurn;
     278  Game.NextPlayer;
    295279  RedrawClients;
    296   ProcessComputerTurns;
    297280  UpdateActions;
    298281end;
     
    399382begin
    400383  Server := TServer.Create;
    401   Clients := TClients.Create;
     384  LocalClients := TClients.Create;
    402385  GameSettings := TGame.Create;
    403386  Game := TGame.Create;
     
    407390  Game.OnPlayerChange := DoPlayerChange;
    408391  Server.Game := Game;
    409   Clients.Game := Game;
     392  LocalClients.Game := Game;
    410393  StoredDimension := TControlDimension.Create;
    411394  XMLConfig1.Filename := GetAppConfigDir(False) + 'Config.xml';
     
    424407  FreeAndNil(StoredDimension);
    425408  FreeAndNil(Server);
    426   FreeAndNil(Clients);
     409  FreeAndNil(LocalClients);
    427410  FreeAndNil(Game);
    428411  FreeAndNil(GameSettings);
     
    448431  Game.LoadFromFile(FileName);
    449432  Server.InitClients;
    450 
    451   // Create local clients for human players
     433  Server.Active := True;
     434
     435  // Create local LocalClients for human players
    452436  for Player in Game.Players do
    453437  with Player do
    454438  if Mode = pmHuman then begin
    455     NewClient := Clients.New(Name);
     439    NewClient := LocalClients.New(Name);
    456440    NewClient.ControlPlayer := Player;
    457441    NewClient.View.Clear;
    458442    NewClient.View.Zoom := 1;
     443    NewClient.LocalServer := Server;
     444    NewClient.ConnectType := ctLocal;
     445    NewClient.Active := True;
    459446    if Assigned(NewClient.ControlPlayer.StartCell) then
    460447      NewClient.View.CenterPlayerCity(NewClient.ControlPlayer)
    461448      else NewClient.View.CenterMap;
     449  end else
     450  if Mode = pmComputer then begin
     451    NewClient := TComputer.Create;
     452    NewClient.Game := Game;
     453    NewClient.Name := Name;
     454    LocalClients.Add(NewClient);
     455    NewClient.ControlPlayer := Player;
     456    NewClient.LocalServer := Server;
     457    NewClient.ConnectType := ctLocal;
     458    NewClient.Active := True;
    462459  end;
    463460
     
    493490begin
    494491  Form := TFormClient.Create(nil);
    495   Form.Client := Clients.New(SSpectator);
     492  Form.Client := LocalClients.New(SSpectator);
    496493  //Form.Client.Form := Form;
    497494  //Form.Client.ControlPlayer := Player;
     
    505502  NewClient: TClient;
    506503  Player: TPlayer;
     504  ServerClient: TServerClient;
    507505begin
    508506  Game.New;
    509507  Server.InitClients;
    510 
    511   // Create local clients for human players
     508  Server.Active := True;
     509
     510  // Create local LocalClients for human players
    512511  for Player in Game.Players do
    513512  with Player do
    514513  if Mode = pmHuman then begin
    515     NewClient := Clients.New(Name);
     514    NewClient := LocalClients.New(Name);
    516515    NewClient.ControlPlayer := Player;
    517516    NewClient.View.Clear;
    518517    NewClient.View.Zoom := 1;
     518    NewClient.LocalServer := Server;
     519    NewClient.ConnectType := ctLocal;
     520    NewClient.Active := True;
    519521    if Assigned(NewClient.ControlPlayer.StartCell) then
    520522      NewClient.View.CenterPlayerCity(NewClient.ControlPlayer)
    521523      else NewClient.View.CenterMap;
     524  end else
     525  if Mode = pmComputer then begin
     526    NewClient := TComputer.Create;
     527    NewClient.Game := Game;
     528    NewClient.Name := Name;
     529    LocalClients.Add(NewClient);
     530    NewClient.ControlPlayer := Player;
     531    NewClient.LocalServer := Server;
     532    NewClient.ConnectType := ctLocal;
     533    NewClient.Active := True;
    522534  end;
    523535
     
    528540  RedrawClients;
    529541  UpdateOtherForms;
    530   ProcessComputerTurns;
    531542  UpdateActions;
     543  ServerClient := Server.Clients.SearchByPlayer(Game.CurrentPlayer);
     544  if Assigned(ServerClient) then ServerClient.TurnStart
     545    else raise Exception.Create('Server client for current player not found.');
    532546end;
    533547
     
    535549var
    536550  PlayerClient: TClient;
     551  ServerClient: TServerClient;
    537552begin
    538553  if Assigned(Game.CurrentPlayer) then begin
    539     PlayerClient := Clients.SearchPlayer(Game.CurrentPlayer);
     554    PlayerClient := LocalClients.SearchPlayer(Game.CurrentPlayer);
    540555    if Assigned(PlayerClient) then FormClient.Client := PlayerClient;
     556
     557    ServerClient := Server.Clients.SearchByPlayer(Game.CurrentPlayer);
     558    if Assigned(ServerClient) then ServerClient.TurnStart;
    541559  end;
    542560  UpdateOtherForms;
Note: See TracChangeset for help on using the changeset viewer.