Changeset 145 for trunk/GameServer.pas


Ignore:
Timestamp:
May 13, 2018, 3:18:55 PM (7 years ago)
Author:
chronos
Message:
  • Modified: bix variable as array of players brains indexes refactored to be direct object reference.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/GameServer.pas

    r144 r145  
    8787var
    8888  // PARAMETERS
    89   PlayersBrain: TBrains; { brain index of the players }
     89  PlayersBrain: TBrains; { brain of the players }
    9090  Difficulty: array [0 .. nPl - 1] of integer absolute Database.Difficulty;
    9191  { difficulty }
     
    146146  LastOffer: TOffer;
    147147  CCData: array [0 .. 14] of integer;
     148  bix: TBrains; { brain of the players }
    148149  DevModelTurn, { turn of last call to sResetModel }
    149   bix, { brain index of the players }
    150150  OriginalDataVersion: array [0 .. nPl - 1] of integer;
    151151  SavedTiles { , SavedResourceWeights } : array [0 .. ncmax - 1] of Cardinal;
     
    179179    HandoverStack[nHandoverStack + 1] := Command;
    180180    inc(nHandoverStack, 2);
    181     Brains[bix[p]].Client(Command, p, Data);
     181    bix[p].Client(Command, p, Data);
    182182    dec(nHandoverStack, 2);
    183183{$ELSE}
     
    193193procedure CallClient(bix, Command: integer; var Data);
    194194begin
    195   if ((Mode <> moMovie) or (bix = GameServer.bix[0])) then
     195  if ((Mode <> moMovie) or (bix = Brains.IndexOf(GameServer.bix[0]))) then
    196196  begin
    197197{$IFOPT O-}
     
    224224  for I := 0 to nPl - 1 do
    225225    PlayersBrain[I] := nil;
     226
     227  bix := TBrains.Create(False);
     228  bix.Count := nPl;
     229  for I := 0 to nPl - 1 do
     230    bix[I] := nil;
    226231
    227232  { get available brains }
     
    282287    end;
    283288  PlayersBrain.Free;
     289  bix.Free;
    284290  Brains.Free;
    285291end;
     
    313319procedure PutMessage(Level: integer; Text: string);
    314320begin
    315   Brains[bix[0]].Client(cDebugMessage, Level, pchar(Text)^);
     321  bix[0].Client(cDebugMessage, Level, pchar(Text)^);
    316322end;
    317323
     
    348354  LastClientTime := T;
    349355  PutMessage(1 shl 16 + 2, Format('CLIENT: calling %d (%s)',
    350     [CCPlayer, Brains[bix[CCPlayer]].Name]));
     356    [CCPlayer, bix[CCPlayer].Name]));
    351357  if CCCommand = cTurn then
    352358    for p := 0 to nPl - 1 do
     
    357363  CCPlayer := -1;
    358364  CallPlayer(CCCommand, p, CCData);
    359   if (Mode = moPlaying) and (Brains[bix[p]].Flags and aiThreaded = 0) and
     365  if (Mode = moPlaying) and (bix[p].Flags and aiThreaded = 0) and
    360366    (CCPlayer < 0) then
    361367  begin
     
    378384  move(GShip, RW[p].Ship, SizeOf(GShip));
    379385  for p1 := 0 to nPl - 1 do
    380     if (p1 <> p) and (bix[p1] >= 0) and (Difficulty[p1] > 0) then
     386    if (p1 <> p) and Assigned(bix[p1]) and (Difficulty[p1] > 0) then
    381387      RW[p].EnemyReport[p1].Credibility := RW[p1].Credibility;
    382388  for p1 := 0 to nPl - 1 do
     
    435441          end;
    436442      // log data changes
    437       if Brains[bix[p]].DataSize > 0 then
     443      if bix[p].DataSize > 0 then
    438444      begin
    439445        CL.PutDataChanges(sIntDataChange, p, SavedData[p], RW[p].Data,
    440           Brains[bix[p]].DataSize);
    441         move(RW[p].Data^, SavedData[p]^, Brains[bix[p]].DataSize * 4);
     446          bix[p].DataSize);
     447        move(RW[p].Data^, SavedData[p]^, bix[p].DataSize * 4);
    442448      end
    443449    end;
     
    463469        with RW[p].EnemyCity[ix] do
    464470          SavedStatus := Status;
    465       if Brains[bix[p]].DataSize > 0 then
    466         move(RW[p].Data^, SavedData[p]^, Brains[bix[p]].DataSize * 4);
     471      if bix[p].DataSize > 0 then
     472        move(RW[p].Data^, SavedData[p]^, bix[p].DataSize * 4);
    467473    end;
    468474end;
     
    493499        result := true;
    494500  if RW[p].Data <> nil then
    495     for ix := 0 to Brains[bix[p]].DataSize - 1 do
     501    for ix := 0 to bix[p].DataSize - 1 do
    496502      if PDWortList(SavedData[p])[ix] <> PDWortList(RW[p].Data)[ix] then
    497503        result := true
    498504end;
    499505
    500 procedure InitBrain(bix: integer);
     506procedure InitBrain(bix: TBrain);
    501507var
    502508  InitModuleData: TInitModuleData;
    503509begin
    504   assert(Brains[bix].Kind <> btSuperVirtual);
    505   with Brains[bix] do begin
     510  assert(bix.Kind <> btSuperVirtual);
     511  with bix do begin
    506512    if Initialized then
    507513      exit;
    508514    if Kind = btAI then
    509515    begin { get client function }
    510       Notify(ntInitModule + bix);
     516      Notify(ntInitModule + Brains.IndexOf(bix));
    511517      if Flags and fDotNet > 0 then
    512518        Client := DotNetClient
     
    517523        begin
    518524          Client := nil;
    519           Notify(ntDLLError + bix);
     525          Notify(ntDLLError + Brains.IndexOf(bix));
    520526        end
    521527        else
     
    523529          Client := GetProcAddress(hm, 'client');
    524530          if @Client = nil then
    525             Notify(ntClientError + bix);
     531            Notify(ntClientError + Brains.IndexOf(bix));
    526532        end
    527533      end
     
    534540      InitModuleData.DataSize := 0;
    535541      InitModuleData.Flags := 0;
    536       CallClient(bix, cInitModule, InitModuleData);
     542      CallClient(Brains.IndexOf(bix), cInitModule, InitModuleData);
    537543      DataVersion := InitModuleData.DataVersion;
    538544      DataSize := (InitModuleData.DataSize + 3) div 4;
     
    620626  nLocal := 0;
    621627  for i := 0 to nPl - 1 do
    622     if (bix[i] <> -1) and (Brains[bix[i]].Kind = btTerm) then
     628    if Assigned(bix[i]) and (bix[i].Kind = btTerm) then
    623629      inc(nLocal);
    624630  if Difficulty[0] = 0 then
     
    664670    LogFile.write(SaveMap, ((MapSize - 1) div 4 + 1) * 4);
    665671  for i := 0 to nPl - 1 do
    666     if bix[i] < 0 then
     672    if not Assigned(bix[i]) then
    667673      LogFile.write(zero, 4)
    668674    else
    669675    begin
    670676      if PlayersBrain[i].Kind in [btRandom, btAI] then
    671         s := Brains[bix[i]].FileName
     677        s := bix[i].FileName
    672678      else
    673679        s := PlayersBrain[i].FileName;
     
    704710  for p1 := 0 to nPl - 1 do begin
    705711    if Assigned(PlayersBrain[p1]) and (PlayersBrain[p1].Kind = btSuperVirtual) then
    706       bix[p1] := Brains.IndexOf(BrainTerm) // supervisor and local human use same module
     712      bix[p1] := BrainTerm // supervisor and local human use same module
    707713    else if Assigned(PlayersBrain[p1]) and (PlayersBrain[p1].Kind = btRandom) then
    708714      if Brains.GetKindCount(btAI) = 0 then
    709         bix[p1] := -1
     715        bix[p1] := nil
    710716      else begin
    711717        AIBrains := TBrains.Create(False);
    712718        Brains.GetByKind(btAI, AIBrains);
    713         bix[p1] := Brains.IndexOf(AIBrains[DelphiRandom(AIBrains.Count)]);
     719        bix[p1] := AIBrains[DelphiRandom(AIBrains.Count)];
    714720        AIBrains.Free;
    715721      end
    716722    else
    717       bix[p1] := Brains.IndexOf(PlayersBrain[p1]);
     723      bix[p1] := PlayersBrain[p1];
    718724    if not Assigned(PlayersBrain[p1]) then
    719725      Difficulty[p1] := -1;
    720726  end;
    721727
    722   if Brains[bix[0]].Kind <> btNoTerm then
     728  if bix[0].Kind <> btNoTerm then
    723729    Notify(ntInitLocalHuman);
    724730  BrainUsed := [];
    725731  for p := 0 to nPl - 1 do
    726     if (bix[p] >= 0) and ((Mode <> moMovie) or (p = 0)) then
     732    if Assigned(bix[p]) and ((Mode <> moMovie) or (p = 0)) then
    727733    begin { initiate selected control module }
    728       AIInfo[p] := Brains[bix[p]].Name + #0;
     734      AIInfo[p] := bix[p].Name + #0;
    729735      InitBrain(bix[p]);
    730736      if Mode = moPlaying then
    731737      begin // new game, this data version is original
    732         OriginalDataVersion[p] := Brains[bix[p]].DataVersion;
     738        OriginalDataVersion[p] := bix[p].DataVersion;
    733739        ProcessClientData[p] := true;
    734740      end
    735741      else // loading game, compare with data version read from file
    736742        ProcessClientData[p] := ProcessClientData[p] and
    737           (OriginalDataVersion[p] = Brains[bix[p]].DataVersion);
    738       if @Brains[bix[p]].Client = nil then // client function not found
    739         if Brains[bix[0]].Kind = btNoTerm then
    740           bix[p] := -1
     743          (OriginalDataVersion[p] = bix[p].DataVersion);
     744      if @bix[p].Client = nil then // client function not found
     745        if bix[0].Kind = btNoTerm then
     746          bix[p] := nil
    741747        else
    742748        begin
    743           bix[p] := Brains.IndexOf(BrainTerm);
     749          bix[p] := BrainTerm;
    744750          OriginalDataVersion[p] := -1;
    745751          ProcessClientData[p] := false;
    746752        end;
    747       if bix[p] >= 0 then
    748         include(BrainUsed, bix[p])
     753      if Assigned(bix[p]) then
     754        include(BrainUsed, Brains.IndexOf(bix[p]));
    749755    end;
    750756
     
    758764  GAI := 0;
    759765  for p1 := 0 to nPl - 1 do
    760     if bix[p1] >= 0 then
     766    if Assigned(bix[p1]) then
    761767    begin
    762768      if Mode <> moMovie then
    763769        inc(GWatching, 1 shl p1);
    764       if Brains[bix[p1]].Kind = btAI then
     770      if bix[p1].Kind = btAI then
    765771        inc(GAI, 1 shl p1);
    766772      if Difficulty[p1] > 0 then
     
    769775        inc(nAlive);
    770776      end;
    771       ServerVersion[p1] := Brains[bix[p1]].ServerVersion;
     777      ServerVersion[p1] := bix[p1].ServerVersion;
    772778    end;
    773   WinOnAlone := (Brains[bix[0]].Kind = btNoTerm) and (nAlive > 1);
     779  WinOnAlone := (bix[0].Kind = btNoTerm) and (nAlive > 1);
    774780  GWinner := 0;
    775781  GColdWarStart := -ColdWarTurns - 1;
     
    806812        OracleIncome := 0;
    807813
    808         if Brains[bix[p]].DataSize > 0 then
     814        if bix[p].DataSize > 0 then
    809815        begin
    810           GetMem(SavedData[p], Brains[bix[p]].DataSize * 4);
    811           GetMem(Data, Brains[bix[p]].DataSize * 4);
    812           FillChar(SavedData[p]^, Brains[bix[p]].DataSize * 4, 0);
    813           FillChar(Data^, Brains[bix[p]].DataSize * 4, 0);
     816          GetMem(SavedData[p], bix[p].DataSize * 4);
     817          GetMem(Data, bix[p].DataSize * 4);
     818          FillChar(SavedData[p]^, bix[p].DataSize * 4, 0);
     819          FillChar(Data^, bix[p].DataSize * 4, 0);
    814820        end
    815821        else
     
    828834        for i := 0 to nStat - 1 do
    829835          GetMem(Stat[i, p], 4 * (MaxTurn + 1));
    830         if Brains[bix[p]].Flags and fDotNet <> 0 then
     836        if bix[p].Flags and fDotNet <> 0 then
    831837        begin
    832838          GetMem(RW[p].DefaultDebugMap, MapSize * 4);
     
    854860    Human := 0;
    855861    for p1 := 0 to nPl - 1 do
    856       if Brains[bix[p1]].Kind = btTerm then
     862      if bix[p1].Kind = btTerm then
    857863        inc(Human, 1 shl p1);
    858864    InitMapGame(Human);
     
    865871
    866872  pTurn := -1;
    867   if Brains[bix[0]].Kind <> btNoTerm then
     873  if bix[0].Kind <> btNoTerm then
    868874    Notify(ntInitLocalHuman);
    869875  Game.lx := lx;
     
    885891        for p := 0 to nPl - 1 do
    886892        begin
    887           if bix[p] = i then
     893          if Brains.IndexOf(bix[p]) = i then
    888894            Game.RO[p] := @RW[p]
    889895          else
    890896            Game.RO[p] := nil;
    891           if (Kind = btTerm) and (Difficulty[0] = 0) and (bix[p] >= 0) then
     897          if (Kind = btTerm) and (Difficulty[0] = 0) and Assigned(bix[p]) then
    892898            Game.SuperVisorRO[p] := @RW[p]
    893899          else
     
    930936  AICredits := AICredits + #0;
    931937
    932   if Brains[bix[0]].Kind <> btNoTerm then
     938  if bix[0].Kind <> btNoTerm then
    933939  begin
    934940    // uni ai?
    935941    bixUni := -1;
    936942    for p1 := 0 to nPl - 1 do
    937       if (bix[p1] <> - 1) and (Brains[bix[p1]].Kind = btAI) then
     943      if Assigned(bix[p1]) and (bix[p1].Kind = btAI) then
    938944        if bixUni = -1 then
    939           bixUni := bix[p1]
    940         else if bixUni <> bix[p1] then
     945          bixUni := Brains.IndexOf(bix[p1])
     946        else if bixUni <> Brains.IndexOf(bix[p1]) then
    941947          bixUni := -2;
    942948    for p1 := 0 to nPl - 1 do
    943       if (bix[p1] <> -1) and (Brains[bix[p1]].Kind = btAI) then
     949      if Assigned(bix[p1]) and (bix[p1].Kind = btAI) then
    944950      begin
    945951        if bixUni = -2 then
    946           NotifyMessage := Brains[bix[p1]].FileName
     952          NotifyMessage := bix[p1].FileName
    947953        else
    948954          NotifyMessage := '';
     
    968974    FreeMem(MapField);
    969975  for p1 := 0 to nPl - 1 do
    970     if bix[p1] >= 0 then
     976    if Assigned(bix[p1]) then
    971977    begin
    972978      for i := 0 to nStat - 1 do
     
    11971203  if MovieMode then
    11981204  begin
    1199     Brains[bix[0]].Client(cShowGame, 0, nil^);
     1205    bix[0].Client(cShowGame, 0, nil^);
    12001206    Notify(ntBackOff);
    12011207  end
     
    12271233{$IFDEF TEXTLOG}WriteLn(TextLog, '---Turn 0 P0---'); {$ENDIF}
    12281234      for p1 := 0 to nPl - 1 do
    1229         if (bix[p1] >= 0) and ((Mode <> moMovie) or (p1 = 0)) then
     1235        if Assigned(bix[p1]) and ((Mode <> moMovie) or (p1 = 0)) then
    12301236          CallPlayer(cReplay, p1, nil^);
    12311237      BeforeTurn0;
     
    12491255{$IFDEF TEXTLOG}LoadPos0 := CL.State.LoadPos; {$ENDIF}
    12501256      if ProcessClientData[p1] then
    1251         CL.GetDataChanges(RW[p1].Data, Brains[bix[p1]].DataSize)
     1257        CL.GetDataChanges(RW[p1].Data, bix[p1].DataSize)
    12521258      else
    12531259        CL.GetDataChanges(nil, 0);
     
    12731279  begin
    12741280    Notify(ntBackOn);
    1275     Brains[bix[0]].Client(cBreakGame, -1, nil^);
     1281    bix[0].Client(cBreakGame, -1, nil^);
    12761282    EndGame;
    12771283    Notify(ntStartGo);
     
    13181324    Notify(ntLoadError);
    13191325  end;
    1320   Brains[bix[0]].Client(cShowGame, 0, nil^);
     1326  bix[0].Client(cShowGame, 0, nil^);
    13211327  Notify(ntBackOff);
    13221328  Inform(pTurn);
     
    13801386  NoLogChanges;
    13811387  for p := 0 to nPl - 1 do
    1382     if bix[p] >= 0 then
     1388    if Assigned(bix[p]) then
    13831389      CallPlayer(cGetReady, p, nil^);
    13841390  LogChanges;
     
    13891395  nLogOpened := -1;
    13901396  LastEndClientCommand := -1;
    1391   Brains[bix[0]].Client(cShowGame, 0, nil^);
     1397  bix[0].Client(cShowGame, 0, nil^);
    13921398  Notify(ntBackOff);
    13931399  Inform(pTurn);
     
    13971403procedure DirectHelp(Command: integer);
    13981404begin
    1399   InitBrain(Brains.IndexOf(BrainTerm));
     1405  InitBrain(BrainTerm);
    14001406  BrainTerm.Client(Command, -1, nil^);
    14011407  AICredits := #0;
     
    14141420  MapSize := lx * ly;
    14151421  LandMass := NewLandMass;
    1416   bix[0] := Brains.IndexOf(BrainTerm);
     1422  bix[0] := BrainTerm;
    14171423  Difficulty[0] := 0;
    1418   InitBrain(Brains.IndexOf(BrainTerm));
     1424  InitBrain(BrainTerm);
    14191425
    14201426  DelphiRandomize;
     
    14421448  DiscoverAll(0, lObserveSuper);
    14431449  Notify(ntEndInfo);
    1444   Brains[bix[0]].Client(cShowGame, 0, nil^);
     1450  bix[0].Client(cShowGame, 0, nil^);
    14451451  Notify(ntBackOff);
    14461452  ChangeClientWhenDone(cEditMap, 0, nil^, 0)
     
    18971903    then { supervisor - all tiles visible }
    18981904    begin
    1899       if (Brains[bix[pTurn]].Kind <> btNoTerm) and
     1905      if (bix[pTurn].Kind <> btNoTerm) and
    19001906        ((Difficulty[pTurn] > 0) or (Mode > moLoading_Fast)) then
    19011907        DiscoverAll(pTurn, lObserveSuper)
     
    20942100          ShowMove.Flags := ShowMove.Flags or umShipLoading;
    20952101      for p1 := 0 to nPl - 1 do
    2096         if (1 shl p1 and GWatching <> 0) and ((p1 <> p) or (Brains[bix[p1]].Kind = btTerm))
     2102        if (1 shl p1 and GWatching <> 0) and ((p1 <> p) or (bix[p1].Kind = btTerm))
    20972103        then
    20982104        begin
     
    22652271    if Mode >= moMovie then { show after-move in interface modules }
    22662272      for p1 := 0 to nPl - 1 do
    2267         if (1 shl p1 and GWatching <> 0) and ((p1 <> p) or (Brains[bix[p1]].Kind = btTerm))
     2273        if (1 shl p1 and GWatching <> 0) and ((p1 <> p) or (bix[p1].Kind = btTerm))
    22682274        then
    22692275        begin
     
    23732379    if Mode >= moMovie then { show attack in interface modules }
    23742380      for p1 := 0 to nPl - 1 do
    2375         if (1 shl p1 and GWatching <> 0) and ((p1 <> p) or (Brains[bix[p1]].Kind = btTerm))
     2381        if (1 shl p1 and GWatching <> 0) and ((p1 <> p) or (bix[p1].Kind = btTerm))
    23762382        then
    23772383        begin
     
    25352541          Lost := Destroyed[p, Owner, mix];
    25362542      for p1 := 0 to nPl - 1 do { show after-attack in interface modules }
    2537         if (1 shl p1 and GWatching <> 0) and ((p1 <> p) or (Brains[bix[p1]].Kind = btTerm))
     2543        if (1 shl p1 and GWatching <> 0) and ((p1 <> p) or (bix[p1].Kind = btTerm))
    25382544        then
    25392545        begin
     
    27992805    }
    28002806    sMessage:
    2801       Brains[bix[0]].Client(cDebugMessage, Subject, Data);
     2807      bix[0].Client(cDebugMessage, Subject, Data);
    28022808
    28032809    sSetDebugMap:
     
    28162822
    28172823    sRefreshDebugMap:
    2818       Brains[bix[0]].Client(cRefreshDebugMap, -1, Player);
     2824      bix[0].Client(cRefreshDebugMap, -1, Player);
    28192825
    28202826    sGetChart .. sGetChart + (nStat - 1) shl 4:
    2821       if (Subject >= 0) and (Subject < nPl) and (bix[Subject] >= 0) then
     2827      if (Subject >= 0) and (Subject < nPl) and Assigned(bix[Subject]) then
    28222828      begin
    28232829        StopTurn := 0;
     
    31133119        AllHumansDead := true;
    31143120        for p1 := 0 to nPl - 1 do
    3115           if (1 shl p1 and GAlive <> 0) and (Brains[bix[p1]].Kind = btTerm) then
     3121          if (1 shl p1 and GAlive <> 0) and (bix[p1].Kind = btTerm) then
    31163122            AllHumansDead := false;
    31173123        if (pDipActive >= 0) // still in negotiation mode
     
    31873193            if pTurn = 0 then
    31883194              inc(GTurn);
    3189             if (bix[pTurn] >= 0) and ((1 shl pTurn) and GAlive = 0) then
     3195            if Assigned(bix[pTurn]) and ((1 shl pTurn) and GAlive = 0) then
    31903196            begin // already made extinct -- continue statistics
    31913197              Stat[stExplore, pTurn, GTurn] := 0;
     
    32303236        if Command = sReload then
    32313237        begin
    3232           ok := (Difficulty[0] = 0) and (Brains[bix[0]].Kind <> btNoTerm) and
     3238          ok := (Difficulty[0] = 0) and (bix[0].Kind <> btNoTerm) and
    32333239            (integer(Data) >= 0) and (integer(Data) < GTurn);
    32343240          for p1 := 1 to nPl - 1 do
    3235             if Brains[bix[p1]].Kind = btTerm then
     3241            if bix[p1].Kind = btTerm then
    32363242              ok := false;
    32373243          // allow reload in AI-only games only
     
    34613467                pTarget := p1;
    34623468                Action := Command;
    3463                 Brains[bix[0]].Client(cShowNego, 1 shl 16 + 3, ShowNegoData);
     3469                bix[0].Client(cShowNego, 1 shl 16 + 3, ShowNegoData);
    34643470              end;
    34653471            pDipActive := p1;
     
    35623568                  Action := Command;
    35633569                  Offer := TOffer(Data);
    3564                   Brains[bix[0]].Client(cShowNego, 1 shl 16 + 3, ShowNegoData);
     3570                  bix[0].Client(cShowNego, 1 shl 16 + 3, ShowNegoData);
    35653571                end;
    35663572              LastOffer := TOffer(Data);
Note: See TracChangeset for help on using the changeset viewer.