section.begin
   Name = InitMapGen
   Code : struct.begin
      [*] = ;procedure CreatePlayers();
      [*] = ;begin
      [*] = ;   var i: Integer;
      [*] = ;   for i:=0 to gc_MaxPlayerCount-1 do begin
      [*] = ;      if GetPlayerHandleByIndex(i)=0 then
      [*] = ;      _player_AddGamePlayer
      [*] = ;      else begin
      [*] = ;         var hnd: Integer = _player_GetOrCreateGamePlayerByIndex(i);
      [*] = ;         ClearEnemyPlayersByHandle(hnd);  //TODO: GEC?
      [*] = ;         ClearFriendPlayersByHandle(hnd); //TODO: GEC?
      [*] = ;         ClearPlayerGameObjectsByHandle(hnd);
      [*] = ;      end;
      [*] = ;   end;
      [*] = ;   // Create env nature player, with wood, food, stone, mine objects. Player should be neutral.
      [*] = ;   _player_AddServicePlayers;
      [*] = ;end;
      [*] = ;
      [*] = ;ApplicationProcessMessages;
      [*] = ;
      [*] = ;var envplhnd: Integer = GetPlayerHandleByName(gc_racename_env);
      [*] = ;if (envplhnd<>0) then
      [*] = ;ClearPlayerGameObjectsByHandle(envplhnd);
      [*] = ;
      [*] = ;CreatePlayers;
      [*] = ;
      [*] = ;_misc_ResetGameData;
      [*] = ;
      [*] = ;var pRecordManager : Integer = ParserSelectRecordManager;
      [*] = ;var bGenRecordManager : Boolean = (ParserGetCountByHandle(pRecordManager)>0);
      [*] = ;var pGen, pMap, pPatternList : Integer;
      [*] = ;if (bGenRecordManager) then
      [*] = ;begin
      [*] = ;   gInterface.gamemode := gc_gamemode_replay;
      [*] = ;   pGen := ParserSelectByHandleByKey(pRecordManager, 'Generator');
      [*] = ;   pMap := ParserSelectByHandleByKey(pGen, 'gMap');
      [*] = ;   pPatternList := ParserSelectByHandleByKey(pGen, 'PatternList');
      [*] = ;   if (pGen=0) or (pMap=0) or (pPatternList=0) then
      [*] = ;   begin
      [*] = ;      ErrorLog('Record Manager generation data exists but corrupted. Map will be generated by default mechanism');
      [*] = ;      bGenRecordManager := False;
      [*] = ;   end;
      [*] = ;end
      [*] = ;else
      [*] = ;gInterface.gamemode := gc_gamemode_game;
      [*] = ;
      [*] = ;var pTmpRM : Integer = ParserSelectByKey('rm');
      [*] = ;if (pTmpRM=0) then
      [*] = ;pTmpRM := ParserCreate('rm')
      [*] = ;else
      [*] = ;ParserClearByHandle(pTmpRM);
      [*] = ;
      [*] = ;if (not bGenRecordManager) then
      [*] = ;begin
      [*] = ;   gRecordGeneratorVersion := gDataVersion;
      [*] = ;   gMap.name := ExtractFileName(GetCurrentMapFileName);
      [*] = ;   var pRMGenerator : Integer = _misc_SelectRecordManagerGeneratorParser(True);
      [*] = ;   if (pRMGenerator<>0) then
      [*] = ;   begin
      [*] = ;      ParserSetIntValueByKeyByHandle(pRMGenerator, 'ver', gDataVersion);
      [*] = ;      ParserSetValueByKeyByHandle(pRMGenerator, 'maskpath', '');
      [*] = ;      ParserSetValueByKeyByHandle(pRMGenerator, 'maskname', '');
      [*] = ;      StateMachineGlobalVariablesSaveToParser(pRMGenerator, 'gMap', False, True);
      [*] = ;   end;
      [*] = ;end
      [*] = ;else
      [*] = ;begin
      [*] = ;   ParserSelectByHandle(pTmpRM);
      [*] = ;   ParserCopyFromByHandle(pRecordManager);
      [*] = ;   gRecordGeneratorVersion := ParserGetIntValueByKeyByHandle(pGen, 'ver');
      [*] = ;   Log('Generate map using RecordManager parser');
      [*] = ;   StateMachineGlobalVariablesLoadFromParser(pMap, 'gMap', False, False);
      [*] = ;end;
      [*] = ;
      [*] = ;var maxCountryCount : Integer;
      [*] = ;if (gRecordGeneratorVersion<53) then
      [*] = ;maxCountryCount := gc_MaxCountryCountRelease
      [*] = ;else
      [*] = ;maxCountryCount := gc_MaxCountryCount;
      [*] = ;
      [*] = ;var dlcs : Integer;
      [*] = ;if (gRecordGeneratorVersion<63) then
      [*] = ;dlcs := 0
      [*] = ;else
      [*] = ;dlcs := gMap.dlcs;
      [*] = ;
      [*] = ;var availcountry : TIntegerList;
      [*] = ;var i : Integer;
      [*] = ;for i:=0 to maxCountryCount-1 do
      [*] = ;begin
      [*] = ;   var bVisibleInEditor, bVisibleInMultiplayer, bPlayable : Boolean;
      [*] = ;   _country_IsCountryAvailable(0, i, dlcs{or gc_lastknowndlc_dlc2}, bVisibleInEditor, bVisibleInMultiplayer, bPlayable);
      [*] = ;   if (bPlayable) then
      [*] = ;   availcountry.Add(i);
      [*] = ;end;
      [*] = ;
      [*] = ;SetRandomKey(gMap.settings.gen.randkey1);
      [*] = ;var maxpldifficulty: Integer = _misc_GetMaxPlayerDifficultyByMap();
      [*] = ;for i:=0 to gc_MaxPlayerCount-1 do begin
      [*] = ;   if (gMap.players[i].bexists) and (gMap.players[i].cid<>gc_spectator_countryid) then begin
      [*] = ;      if (gRecordGeneratorVersion<53) then
      [*] = ;      begin
      [*] = ;         if ((gMap.players[i].cid>=maxCountryCount) or (gMap.players[i].cid<0)) then
      [*] = ;         gMap.players[i].cid := floor(RandomExt*(maxCountryCount-1));
      [*] = ;      end
      [*] = ;      else
      [*] = ;      if (gRecordGeneratorVersion<63) then
      [*] = ;      begin
      [*] = ;         if ((_net_IsOnline) or ((_net_IsReplay) and (gMap.players[0].lanid>0))) then
      [*] = ;         begin
      [*] = ;            if ((gMap.players[i].cid>=maxCountryCount) or (gMap.players[i].cid<0)) then
      [*] = ;            gMap.players[i].cid := floor(RandomExt*(gc_MaxCountryCountRelease-1));
      [*] = ;         end
      [*] = ;         else
      [*] = ;         if ((gMap.players[i].cid>=maxCountryCount) or (gMap.players[i].cid<0)) then
      [*] = ;         gMap.players[i].cid := availcountry.Get(floor(RandomExt*(availcountry.GetCount-1)));
      [*] = ;      end
      [*] = ;      else
      [*] = ;      if (gRecordGeneratorVersion<68) then
      [*] = ;      begin
      [*] = ;         if ((gMap.players[i].cid>=maxCountryCount) or (gMap.players[i].cid<0)) then
      [*] = ;         gMap.players[i].cid := availcountry.Get(floor(RandomExt*(availcountry.GetCount-1)));
      [*] = ;      end
      [*] = ;      else
      [*] = ;      if ((gMap.players[i].cid>=maxCountryCount) or (gMap.players[i].cid<0)) then
      [*] = ;      gMap.players[i].cid := availcountry.Get(floor(RandomExt*(availcountry.GetCount)));
      [*] = ;
      [*] = ;      gPlayer[i].cid:=gMap.players[i].cid;
      [*] = ;      if gMap.players[i].bai then
      [*] = ;      gPlayer[i].difficulty:=gMap.players[i].aidifficulty
      [*] = ;      else gPlayer[i].difficulty:=maxpldifficulty;
      [*] = ;      gPlayer[i].bexists:=true;
      [*] = ;   end
      [*] = ;   else
      [*] = ;   if (gMap.players[i].cid<>gc_spectator_countryid) then
      [*] = ;   begin
      [*] = ;      gMap.players[i].cid:=0;
      [*] = ;      gPlayer[i].difficulty:=maxpldifficulty;
      [*] = ;   end;
      [*] = ;   _country_GetSIDByID(gMap.players[i].cid, gMap.players[i].csid);
      [*] = ;   if i<>gc_MaxPlayerCount-1 then
      [*] = ;   gPlayer[i].enemyplmask := 1 shl (gc_MaxPlayerCount-1);
      [*] = ;end;
      [*] = ;SetRandomKey(gMap.settings.gen.randkey1);
      [*] = ;
      [*] = ;//_player_ReInitAllPlayersBaseObj;
      [*] = ;ApplicationProcessMessages;
      [*] = ;_player_FixPlayersObjBaseBadIndexes;
      [*] = ;_player_ApplyMapSettings;
      [*] = ;ApplicationProcessMessages;
      [*] = ;
      [*] = ;for i:=0 to gc_MaxPlayerCount-1 do begin
      [*] = ;   var j: Integer;
      [*] = ;   var myteam: Integer = gMap.players[i].team;
      [*] = ;   for j:=0 to gc_MaxPlayerCount-1 do begin
      [*] = ;      if i<>j then begin
      [*] = ;         var histeam: Integer = gMap.players[j].team;
      [*] = ;         if(myteam<>histeam) or (histeam=0) or (myteam=0) then begin
      [*] = ;            gPlayer[i].enemyplmask:=gPlayer[i].enemyplmask or (1 shl j);
      [*] = ;            AddPlayerEnemyPlayerByHandle(GetPlayerHandleByIndex(i), GetPlayerHandleByIndex(j));
      [*] = ;         end else AddPlayerFriendPlayerByHandle(GetPlayerHandleByIndex(i), GetPlayerHandleByIndex(j));
      [*] = ;      end;
      [*] = ;   end;
      [*] = ;   for j:=0 to gc_ResCount-1 do
      [*] = ;   case gMap.settings.gen.resourcestart of
      [*] = ;      0: begin
      [*] = ;         _res_SetResToPlayerByIndex(i, j, 1000);
      [*] = ;         gPlayer[i].lanres[j] := gPlayer[i].setres[j];
      [*] = ;         gPlayer[i].res[j] := gPlayer[i].setres[j];
      [*] = ;      end;
      [*] = ;      1: begin
      [*] = ;         _res_SetResToPlayerByIndex(i, j, 4000);
      [*] = ;         gPlayer[i].lanres[j] := gPlayer[i].setres[j];
      [*] = ;         gPlayer[i].res[j] := gPlayer[i].setres[j];
      [*] = ;      end;
      [*] = ;      2: begin
      [*] = ;         _res_SetResToPlayerByIndex(i, j, 5000);
      [*] = ;         gPlayer[i].lanres[j] := gPlayer[i].setres[j];
      [*] = ;         gPlayer[i].res[j] := gPlayer[i].setres[j];
      [*] = ;      end
      [*] = ;      else
      [*] = ;      begin
      [*] = ;         _res_SetResToPlayerByIndex(i, j, 1000000);
      [*] = ;         gPlayer[i].lanres[j] := gPlayer[i].setres[j];
      [*] = ;         gPlayer[i].res[j] := gPlayer[i].setres[j];
      [*] = ;      end;
      [*] = ;   end;
      [*] = ;end;
      [*] = ;
      [*] = ;gbool_gui_bspectator := False;
      [*] = ;
      [*] = ;var bPlayerSet : Boolean;
      [*] = ;for i:=0 to gc_MaxPlayerCount-1 do
      [*] = ;if (gMap.players[i].bexists) then
      [*] = ;begin
      [*] = ;   if IsLanGame then
      [*] = ;   begin
      [*] = ;      if (gMap.players[i].lanid=LanMyInfoID) then
      [*] = ;      begin
      [*] = ;         gbool_gui_bspectator := (gMap.players[i].cid=gc_spectator_countryid);
      [*] = ;
      [*] = ;         _control_SetPlayer(i);
      [*] = ;         bPlayerSet := True;
      [*] = ;      end;
      [*] = ;   end
      [*] = ;   else
      [*] = ;   begin
      [*] = ;      if (gMap.players[i].bexists) and (gMap.players[i].bhuman) then
      [*] = ;      begin
      [*] = ;         gbool_gui_bspectator := (gMap.players[i].cid=gc_spectator_countryid);
      [*] = ;
      [*] = ;         _control_SetPlayer(i);
      [*] = ;         bPlayerSet := True;
      [*] = ;         break;
      [*] = ;      end;
      [*] = ;   end;
      [*] = ;end;
      [*] = ;
      [*] = ;gbool_editor_fogofwar := not gbool_gui_bspectator;
      [*] = ;
      [*] = ;if (not bPlayerSet) then
      [*] = ;begin
      [*] = ;   _control_SetPlayer(0);
      [*] = ;   ErrorLog('InitMapGen, _control_SetPlayer was not set, so we force set player to plind=0');
      [*] = ;end;
      [*] = ;
      [*] = ;_misc_UpdateTopologyZoneSizeToMapSize(False);
      [*] = ;
      [*] = ;ApplicationProcessMessages;
      [*] = ;ExecuteState('DoGenerate');
      [*] = ;ApplicationProcessMessages;
      [*] = ;
      [*] = ;MapWorldUpdateTerrainData(0, 0);
      [*] = ;ApplicationProcessMessages;
      [*] = ;TopologyBuild;
      [*] = ;ApplicationProcessMessages;
      [*] = ;_misc_BuildIslandsGrid;
      [*] = ;ApplicationProcessMessages;
      [*] = ;
      [*] = ;gInt_UpdateAreasCount:=0;
      [*] = ;
      [*] = ;//if IsLanGame and not IsLanReady then LanDoReady;
      [*] = ;if (not bGenRecordManager) then
      [*] = ;begin
      [*] = ;   CleanRecordManager;
      [*] = ;   pRecordManager := ParserSelectRecordManager;
      [*] = ;   if (pRecordManager<>0) then
      [*] = ;   ParserCopyFromByHandle(pTmpRM)
      [*] = ;   else
      [*] = ;   ErrorLog('InitMapGen : pRecordManager = 0');
      [*] = ;   ParserFreeByHandle(pTmpRM);
      [*] = ;end;
      [*] = ;
      [*] = ;for i:=0 to gc_MaxPlayerCount-1 do
      [*] = ;if (gMap.players[i].bexists) and (gMap.players[i].cid=gc_spectator_countryid) then
      [*] = ;begin
      [*] = ;   gMap.players[i].bexists := False;
      [*] = ;   gPlayer[i].bexists := False;
      [*] = ;
      [*] = ;   gMap.players[i].bleave := True;
      [*] = ;   gPlayer[i].victorystate := gc_player_victorystate_lose;
      [*] = ;end;
      [*] = ;
      [*] = ;if (not IsLanGame) then
      [*] = ;gMap.gamestage := gc_map_gamestage_started;
      [*] = ;
      [*] = ;Log('...InitMapGen finished...');
      [*] = ;Log('...MapNextUniqId = '+IntToStr(GetMapNextUniqId)+'...');
      [*] = ;
      [*] = ;// preserve old version compatibility
      [*] = ;if (gRecordGeneratorVersion<53) then
      [*] = ;begin
      [*] = ;   DeletePlayerByHandle(GetPlayerHandleByIndex(8));
      [*] = ;   _player_UpdateFOW(GetPlayerIndexByHandle(GetPlayerHandleInterfaceIO));
      [*] = ;end;
      [*] = ;
      [*] = ;// preserve old version compatibility
      [*] = ;if (gRecordGeneratorVersion<89) then
      [*] = ;begin
      [*] = ;   DeletePlayerByHandle(GetPlayerHandleByIndex(9));
      [*] = ;   DeletePlayerByHandle(GetPlayerHandleByIndex(10));
      [*] = ;   DeletePlayerByHandle(GetPlayerHandleByIndex(11));
      [*] = ;   _player_UpdateFOW(GetPlayerIndexByHandle(GetPlayerHandleInterfaceIO));
      [*] = ;end;
   struct.end
section.end

