| 1 | #include "playermapping.h" |
| 2 | |
| 3 | #include <base/net.h> |
| 4 | |
| 5 | #include <engine/shared/config.h> |
| 6 | |
| 7 | #include <game/server/entities/character.h> |
| 8 | #include <game/server/gamecontext.h> |
| 9 | #include <game/server/gamecontroller.h> |
| 10 | #include <game/server/player.h> |
| 11 | |
| 12 | void CPlayerMapping::Init(CGameContext *pGameServer) |
| 13 | { |
| 14 | m_pGameServer = pGameServer; |
| 15 | m_pConfig = m_pGameServer->Config(); |
| 16 | m_pServer = m_pGameServer->Server(); |
| 17 | std::fill(first: std::begin(arr&: m_aTeamSizes), last: std::end(arr&: m_aTeamSizes), value: 0); |
| 18 | m_ReserveAnyTeamSlots = true; |
| 19 | |
| 20 | for(int i = 0; i < MAX_CLIENTS; i++) |
| 21 | m_aMap[i].Init(ClientId: i, pPlayerMapping: this); |
| 22 | } |
| 23 | |
| 24 | void CPlayerMapping::Tick() |
| 25 | { |
| 26 | UpdatePlayerMap(ClientId: -1); |
| 27 | |
| 28 | // Translate StrongWeakId to clamp it to 64 players |
| 29 | bool NeedsLegacyMapping = false; |
| 30 | for(int i = 0; i < MAX_CLIENTS; i++) |
| 31 | { |
| 32 | if(GameServer()->m_apPlayers[i] && !Server()->ClientSupportsServerMaxClients(ClientId: i) && GameServer()->GetClientVersion(ClientId: i) >= VERSION_DDNET_OLD) |
| 33 | { |
| 34 | NeedsLegacyMapping = true; |
| 35 | break; |
| 36 | } |
| 37 | } |
| 38 | if(!NeedsLegacyMapping) |
| 39 | return; // or continue past this block — nothing to do on modern-only servers |
| 40 | |
| 41 | // Walk the character list ONCE per tick, not once per legacy client |
| 42 | int aCharacterIds[MAX_CLIENTS]; |
| 43 | int NumCharacters = 0; |
| 44 | CGameWorld::CEntityIterator Iterator(&GameServer()->m_World, CGameWorld::ENTTYPE_CHARACTER); |
| 45 | while(CCharacter *pChar = static_cast<CCharacter *>(Iterator.Next())) |
| 46 | { |
| 47 | aCharacterIds[NumCharacters++] = pChar->GetPlayer()->GetCid(); |
| 48 | } |
| 49 | |
| 50 | for(int i = 0; i < MAX_CLIENTS; i++) |
| 51 | { |
| 52 | CPlayer *pPlayer = GameServer()->m_apPlayers[i]; |
| 53 | if(!pPlayer || Server()->ClientSupportsServerMaxClients(ClientId: i) || GameServer()->GetClientVersion(ClientId: i) < VERSION_DDNET_OLD) |
| 54 | continue; |
| 55 | |
| 56 | int StrongWeakId = 0; |
| 57 | for(int c = 0; c < NumCharacters; c++) |
| 58 | { |
| 59 | int Id = aCharacterIds[c]; |
| 60 | if(Server()->Translate(Target&: Id, ClientId: i)) |
| 61 | pPlayer->m_aStrongWeakId[Id] = StrongWeakId++; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | void CPlayerMapping::CPlayerMap::Init(int ClientId, CPlayerMapping *pPlayerMapping) |
| 67 | { |
| 68 | m_ClientId = ClientId; |
| 69 | m_pPlayerMapping = pPlayerMapping; |
| 70 | m_pMap = m_pPlayerMapping->Server()->GetIdMap(ClientId: m_ClientId); |
| 71 | m_pReverseMap = m_pPlayerMapping->Server()->GetReverseIdMap(ClientId: m_ClientId); |
| 72 | m_ResortReserved = false; |
| 73 | std::fill(first: std::begin(arr&: m_aReserved), last: std::end(arr&: m_aReserved), value: false); |
| 74 | m_NumPages = 0; |
| 75 | m_TotalOverhang = 0; |
| 76 | m_NumReserved = 0; |
| 77 | m_LastSeeOthersVoteTick = 0; |
| 78 | m_DoSeeOthersByVote = false; |
| 79 | ResetSeeOthers(); |
| 80 | } |
| 81 | |
| 82 | CPlayer *CPlayerMapping::CPlayerMap::Player() const |
| 83 | { |
| 84 | return m_pPlayerMapping->GameServer()->m_apPlayers[m_ClientId]; |
| 85 | } |
| 86 | |
| 87 | void CPlayerMapping::CPlayerMap::InitPlayer(CSixupCfg SixupCfg) |
| 88 | { |
| 89 | std::fill(first: std::begin(arr&: m_aReserved), last: std::end(arr&: m_aReserved), value: false); |
| 90 | |
| 91 | int NextFreeId = 0; |
| 92 | const NETADDR *pOwnAddr = m_pPlayerMapping->Server()->ClientAddr(ClientId: m_ClientId); |
| 93 | for(bool Finished = false; !Finished;) |
| 94 | { |
| 95 | Finished = true; |
| 96 | for(int i = 0; i < MAX_CLIENTS; i++) |
| 97 | { |
| 98 | if(!m_pPlayerMapping->GameServer()->m_apPlayers[i]) |
| 99 | continue; |
| 100 | |
| 101 | const NETADDR *pAddr = m_pPlayerMapping->Server()->ClientAddr(ClientId: i); |
| 102 | if(net_addr_comp_noport(a: pOwnAddr, b: pAddr) == 0) |
| 103 | { |
| 104 | // For 0.7 timeout: Rejoin has to check ourselves because it's the id of the old connection that we want to skip |
| 105 | // Do not access our own reverse map on initial initialization, as it's only initialized below |
| 106 | if((i != m_ClientId || SixupCfg.m_SkipTimeoutedId) && m_pPlayerMapping->m_aMap[i].m_pReverseMap[i] == NextFreeId) |
| 107 | { |
| 108 | NextFreeId++; |
| 109 | Finished = false; |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | // make sure no outdated data is stored, so we can start and insert new values |
| 116 | // after a timeout remove all players from the previous map in correct order (important for 0.7 net msgs...) |
| 117 | if(SixupCfg.m_ClearSlots) |
| 118 | { |
| 119 | m_UpdateTeamsState = true; // to get back all teams |
| 120 | for(int i = 0; i < LEGACY_MAX_CLIENTS; i++) |
| 121 | Remove(MapId: i); |
| 122 | } |
| 123 | |
| 124 | // Clear map, for 0.7 timeouts do this after we got our id back |
| 125 | for(int i = 0; i < LEGACY_MAX_CLIENTS; i++) |
| 126 | m_pMap[i] = -1; |
| 127 | for(int i = 0; i < MAX_CLIENTS; i++) |
| 128 | m_pReverseMap[i] = -1; |
| 129 | |
| 130 | m_NumReserved = 0; |
| 131 | const bool PlayerMappingRequired = !m_pPlayerMapping->Server()->ClientSupportsServerMaxClients(ClientId: m_ClientId); |
| 132 | if(PlayerMappingRequired) |
| 133 | { |
| 134 | m_NumReserved = 2; |
| 135 | m_pMap[m_pPlayerMapping->Server()->GetMaxClients(ClientId: m_ClientId) - 1] = -1; // player with empty name to say chat msgs |
| 136 | m_pMap[m_pPlayerMapping->SeeOthersId(ClientId: m_ClientId)] = -1; // see others in spec menu |
| 137 | m_TotalOverhang = 0; |
| 138 | |
| 139 | if(m_pPlayerMapping->Server()->IsSixup(ClientId: m_ClientId)) |
| 140 | { |
| 141 | protocol7::CNetMsg_Sv_ClientInfo FakeInfo; |
| 142 | FakeInfo.m_ClientId = m_pPlayerMapping->Server()->GetMaxClients(ClientId: m_ClientId) - 1; |
| 143 | FakeInfo.m_Local = 0; |
| 144 | FakeInfo.m_Team = TEAM_BLUE; // `TEAM_BLUE` to hide from ddrace scoreboards |
| 145 | FakeInfo.m_pName = " " ; |
| 146 | FakeInfo.m_pClan = "" ; |
| 147 | FakeInfo.m_Country = -1; |
| 148 | FakeInfo.m_Silent = 1; |
| 149 | for(int p = 0; p < protocol7::NUM_SKINPARTS; p++) |
| 150 | { |
| 151 | FakeInfo.m_apSkinPartNames[p] = "standard" ; |
| 152 | FakeInfo.m_aUseCustomColors[p] = 0; |
| 153 | FakeInfo.m_aSkinPartColors[p] = 0; |
| 154 | } |
| 155 | m_pPlayerMapping->Server()->SendPackMsg(pMsg: &FakeInfo, Flags: MSGFLAG_VITAL | MSGFLAG_NORECORD | MSGFLAG_NOTRANSLATE, ClientId: m_ClientId); |
| 156 | // see others |
| 157 | UpdateSeeOthers(); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | // Breaks with more than `MapSize` tees from the same ip, but not a problem on official servers. |
| 162 | // Required for other player maps, even when this specific one doesn't need playermapping and supports max_clients |
| 163 | const bool NextIdValid = NextFreeId < LEGACY_MAX_CLIENTS; |
| 164 | if(NextFreeId < MapSize() && NextIdValid) |
| 165 | { |
| 166 | m_aReserved[m_ClientId] = true; |
| 167 | Add(MapId: NextFreeId, ClientId: m_ClientId); |
| 168 | } |
| 169 | |
| 170 | for(int i = 0; i < MAX_CLIENTS; i++) |
| 171 | { |
| 172 | if(!m_pPlayerMapping->GameServer()->m_apPlayers[i] || i == m_ClientId) |
| 173 | continue; |
| 174 | |
| 175 | const NETADDR *pAddr = m_pPlayerMapping->Server()->ClientAddr(ClientId: i); |
| 176 | if(net_addr_comp_noport(a: pOwnAddr, b: pAddr) != 0) |
| 177 | continue; |
| 178 | |
| 179 | // update us with other same ip player infos |
| 180 | if(PlayerMappingRequired && m_pPlayerMapping->m_aMap[i].m_pReverseMap[i] < MapSize()) |
| 181 | { |
| 182 | m_aReserved[i] = true; |
| 183 | Add(MapId: m_pPlayerMapping->m_aMap[i].m_pReverseMap[i], ClientId: i); |
| 184 | } |
| 185 | |
| 186 | // update other same ip players with our info |
| 187 | if(NextIdValid && NextFreeId < m_pPlayerMapping->m_aMap[i].MapSize()) |
| 188 | { |
| 189 | m_pPlayerMapping->m_aMap[i].m_aReserved[m_ClientId] = true; |
| 190 | m_pPlayerMapping->m_aMap[i].Add(MapId: NextFreeId, ClientId: m_ClientId); |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | void CPlayerMapping::CPlayerMap::Add(int MapId, int ClientId) |
| 196 | { |
| 197 | dbg_assert(Player(), "invalid player map insertion: player does not exist" ); |
| 198 | if(MapId == -1 || ClientId == -1 || m_pReverseMap[ClientId] == MapId) |
| 199 | return; |
| 200 | |
| 201 | Remove(MapId: m_pReverseMap[ClientId]); |
| 202 | |
| 203 | int OldClientId = Remove(MapId); |
| 204 | if((OldClientId == -1 && m_pPlayerMapping->GameServer()->GetDDRaceTeam(ClientId) > 0) || (OldClientId != -1 && m_pPlayerMapping->GameServer()->GetDDRaceTeam(ClientId: OldClientId) != m_pPlayerMapping->GameServer()->GetDDRaceTeam(ClientId))) |
| 205 | m_UpdateTeamsState = true; |
| 206 | |
| 207 | if(m_aReserved[ClientId]) |
| 208 | m_ResortReserved = true; |
| 209 | |
| 210 | m_pMap[MapId] = ClientId; |
| 211 | m_pReverseMap[ClientId] = MapId; |
| 212 | Player()->SendConnect(FakeId: MapId, ClientId); |
| 213 | } |
| 214 | |
| 215 | int CPlayerMapping::CPlayerMap::Remove(int MapId) |
| 216 | { |
| 217 | dbg_assert(Player(), "invalid player map removal: player does not exist" ); |
| 218 | if(MapId == -1) |
| 219 | return -1; |
| 220 | |
| 221 | int ClientId = m_pMap[MapId]; |
| 222 | if(ClientId != -1) |
| 223 | { |
| 224 | if(m_pPlayerMapping->GameServer()->GetDDRaceTeam(ClientId) > 0) |
| 225 | m_UpdateTeamsState = true; |
| 226 | |
| 227 | if(m_aReserved[ClientId]) |
| 228 | m_ResortReserved = true; |
| 229 | |
| 230 | Player()->SendDisconnect(FakeId: MapId); |
| 231 | m_pReverseMap[ClientId] = -1; |
| 232 | m_pMap[MapId] = -1; |
| 233 | } |
| 234 | return ClientId; |
| 235 | } |
| 236 | |
| 237 | void CPlayerMapping::CPlayerMap::Update() |
| 238 | { |
| 239 | if(!m_pPlayerMapping->Server()->ClientIngame(ClientId: m_ClientId) || !Player()) |
| 240 | return; |
| 241 | if(m_pPlayerMapping->Server()->ClientSupportsServerMaxClients(ClientId: m_ClientId)) |
| 242 | return; |
| 243 | |
| 244 | if(m_DoSeeOthersByVote) |
| 245 | { |
| 246 | CCharacter *pChr = m_pPlayerMapping->GameServer()->GetPlayerChar(ClientId: m_ClientId); |
| 247 | if(pChr && !pChr->IsIdle()) |
| 248 | { |
| 249 | ResetSeeOthers(); |
| 250 | m_DoSeeOthersByVote = false; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | bool ResortReserved = m_ResortReserved; |
| 255 | m_ResortReserved = false; |
| 256 | |
| 257 | for(int i = 0; i < MAX_CLIENTS; i++) |
| 258 | { |
| 259 | if(i == m_ClientId) |
| 260 | continue; |
| 261 | |
| 262 | CPlayer *pPlayer = m_pPlayerMapping->GameServer()->m_apPlayers[i]; |
| 263 | |
| 264 | if(!m_pPlayerMapping->Server()->ClientIngame(ClientId: i) || !pPlayer) |
| 265 | { |
| 266 | Remove(MapId: m_pReverseMap[i]); |
| 267 | m_aReserved[i] = false; |
| 268 | continue; |
| 269 | } |
| 270 | |
| 271 | // If a team (not 0) has more than 10 players, do not reserve their slots because it can get messy quickly if a few huge teams form. |
| 272 | // To keep teams state the same on main and dummy big teams do not get highlighted at all. |
| 273 | int DDTeam = m_pPlayerMapping->GameServer()->GetDDRaceTeam(ClientId: i); |
| 274 | bool ReserveTeamSlots = m_pPlayerMapping->ReserveTeamSlots(DDTeam, ClientId: m_ClientId); |
| 275 | |
| 276 | if(m_aReserved[i]) |
| 277 | { |
| 278 | const NETADDR *pOwnAddr = m_pPlayerMapping->Server()->ClientAddr(ClientId: m_ClientId); |
| 279 | const NETADDR *pAddr = m_pPlayerMapping->Server()->ClientAddr(ClientId: i); |
| 280 | if(net_addr_comp_noport(a: pOwnAddr, b: pAddr) != 0) |
| 281 | { |
| 282 | if(ResortReserved || !ReserveTeamSlots) // condition to unset reserved slot |
| 283 | { |
| 284 | m_aReserved[i] = false; |
| 285 | |
| 286 | // reset our team to 0 when we are in a big team for example |
| 287 | if(DDTeam != TEAM_FLOCK) |
| 288 | m_UpdateTeamsState = true; |
| 289 | } |
| 290 | } |
| 291 | continue; |
| 292 | } |
| 293 | else if(ResortReserved) |
| 294 | continue; |
| 295 | |
| 296 | int Insert = -1; |
| 297 | if(DDTeam != TEAM_FLOCK && ReserveTeamSlots) |
| 298 | { |
| 299 | for(int j = 0; j < MapSize() - m_NumSeeOthers; j++) |
| 300 | { |
| 301 | int CId = m_pMap[j]; |
| 302 | if(CId == -1 || !m_aReserved[CId]) |
| 303 | { |
| 304 | Insert = j; |
| 305 | m_aReserved[i] = true; |
| 306 | break; |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | else if(m_pReverseMap[i] != -1) |
| 311 | { |
| 312 | Insert = m_pReverseMap[i]; |
| 313 | } |
| 314 | else |
| 315 | { |
| 316 | for(int j = 0; j < MapSize() - m_NumSeeOthers; j++) |
| 317 | if(m_pMap[j] == -1) |
| 318 | { |
| 319 | Insert = j; |
| 320 | break; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | if(Insert != -1) |
| 325 | { |
| 326 | Add(MapId: Insert, ClientId: i); |
| 327 | } |
| 328 | else if(pPlayer->GetCharacter() && !pPlayer->GetCharacter()->NetworkClipped(SnappingClient: m_ClientId)) |
| 329 | { |
| 330 | InsertNextEmptyOrReplace(ClientId: i); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | if(m_UpdateTeamsState) |
| 335 | { |
| 336 | m_pPlayerMapping->GameServer()->m_pController->Teams().SendTeamsState(ClientId: m_ClientId); |
| 337 | m_UpdateTeamsState = false; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | void CPlayerMapping::CPlayerMap::InsertNextEmptyOrReplace(int ClientId) |
| 342 | { |
| 343 | if(ClientId == -1 || m_pReverseMap[ClientId] != -1) |
| 344 | return; |
| 345 | |
| 346 | // Fast path: find an empty slot or a slot occupied by a character-less player. |
| 347 | for(int i = 0; i < MapSize() - m_NumSeeOthers; i++) |
| 348 | { |
| 349 | int MappedClientId = m_pMap[i]; |
| 350 | if(MappedClientId != -1 && m_aReserved[MappedClientId]) |
| 351 | continue; |
| 352 | |
| 353 | if(MappedClientId == -1 || (!m_pPlayerMapping->GameServer()->GetPlayerChar(ClientId: MappedClientId) || m_pPlayerMapping->GameServer()->GetPlayerChar(ClientId: MappedClientId)->NetworkClipped(SnappingClient: m_ClientId))) |
| 354 | { |
| 355 | Add(MapId: i, ClientId); |
| 356 | return; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | // Overflow fallback: all visible non-reserved slots are occupied. |
| 361 | // Replace the farthest non-reserved player if the new player is closer. |
| 362 | CCharacter *pNewChar = m_pPlayerMapping->GameServer()->GetPlayerChar(ClientId); |
| 363 | if(!pNewChar || !Player()) |
| 364 | return; |
| 365 | |
| 366 | vec2 ViewPos = Player()->m_ViewPos; |
| 367 | float NewDist = distance_squared(a: ViewPos, b: pNewChar->GetPos()); |
| 368 | |
| 369 | int ReplaceIndex = -1; |
| 370 | float MaxDist = NewDist; |
| 371 | |
| 372 | for(int i = 0; i < MapSize() - m_NumSeeOthers; i++) |
| 373 | { |
| 374 | int MappedClientId = m_pMap[i]; |
| 375 | if(MappedClientId == -1 || m_aReserved[MappedClientId]) |
| 376 | continue; |
| 377 | |
| 378 | CCharacter *pMappedChar = m_pPlayerMapping->GameServer()->GetPlayerChar(ClientId: MappedClientId); |
| 379 | if(!pMappedChar) |
| 380 | continue; |
| 381 | |
| 382 | float Dist = distance_squared(a: ViewPos, b: pMappedChar->GetPos()); |
| 383 | if(Dist > MaxDist) |
| 384 | { |
| 385 | MaxDist = Dist; |
| 386 | ReplaceIndex = i; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | if(ReplaceIndex != -1) |
| 391 | { |
| 392 | Add(MapId: ReplaceIndex, ClientId); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | int CPlayerMapping::CPlayerMap::MapSize() const |
| 397 | { |
| 398 | return m_pPlayerMapping->Server()->GetMaxClients(ClientId: m_ClientId) - m_NumReserved; |
| 399 | } |
| 400 | |
| 401 | bool CPlayerMapping::ReserveTeamSlots(int DDTeam, int ClientId) const |
| 402 | { |
| 403 | const bool IsDDNet = m_pGameServer->GetClientVersion(ClientId) >= VERSION_DDNET_OLD; |
| 404 | return !g_Config.m_SvSoloServer && m_ReserveAnyTeamSlots && DDTeam != TEAM_FLOCK && m_aTeamSizes[DDTeam] <= ms_MaxTeamSizePlayerMap && IsDDNet; |
| 405 | } |
| 406 | |
| 407 | int CPlayerMapping::SeeOthersId(int ClientId) const |
| 408 | { |
| 409 | return m_pServer->GetMaxClients(ClientId) - 2; |
| 410 | } |
| 411 | |
| 412 | bool CPlayerMapping::DoSeeOthers(int ClientId, int SelectedId, bool DoByVote) |
| 413 | { |
| 414 | if(Server()->ClientSupportsServerMaxClients(ClientId)) |
| 415 | return false; |
| 416 | if(SelectedId == SeeOthersId(ClientId)) |
| 417 | { |
| 418 | if(DoByVote) |
| 419 | { |
| 420 | // Less conservative rate limit than normal 3 seconds for voting. Let's settle for 1 second for now. Comparison: +spectate has 250ms. |
| 421 | if(m_aMap[ClientId].m_LastSeeOthersVoteTick > Server()->Tick() - Server()->TickSpeed()) |
| 422 | return true; |
| 423 | m_aMap[ClientId].m_LastSeeOthersVoteTick = Server()->Tick(); |
| 424 | m_aMap[ClientId].m_DoSeeOthersByVote = true; |
| 425 | } |
| 426 | m_aMap[ClientId].DoSeeOthers(); |
| 427 | return true; |
| 428 | } |
| 429 | return false; |
| 430 | } |
| 431 | |
| 432 | void CPlayerMapping::ResetSeeOthers(int ClientId) |
| 433 | { |
| 434 | m_aMap[ClientId].ResetSeeOthers(); |
| 435 | } |
| 436 | |
| 437 | int CPlayerMapping::TotalOverhang(int ClientId) const |
| 438 | { |
| 439 | return m_aMap[ClientId].m_TotalOverhang; |
| 440 | } |
| 441 | |
| 442 | void CPlayerMapping::UpdatePlayerMap(int ClientId) |
| 443 | { |
| 444 | if(ClientId == -1) |
| 445 | { |
| 446 | bool Update = Server()->Tick() % Config()->m_SvMapUpdateRate == 0; |
| 447 | int ClientCount = Server()->ClientCount(); |
| 448 | |
| 449 | if(Update) |
| 450 | { |
| 451 | // Cache team sizes to avoid more loops |
| 452 | std::fill(first: std::begin(arr&: m_aTeamSizes), last: std::end(arr&: m_aTeamSizes), value: 0); |
| 453 | for(int i = 0; i < MAX_CLIENTS; i++) |
| 454 | { |
| 455 | CPlayer *pPlayer = GameServer()->m_apPlayers[i]; |
| 456 | if(!pPlayer) |
| 457 | continue; |
| 458 | int DDTeam = GameServer()->GetDDRaceTeam(ClientId: i); |
| 459 | m_aTeamSizes[DDTeam]++; |
| 460 | } |
| 461 | int NumReservableTeamPlayers = 0; |
| 462 | for(int Team = TEAM_FLOCK + 1; Team < NUM_DDRACE_TEAMS; Team++) |
| 463 | if(m_aTeamSizes[Team] <= ms_MaxTeamSizePlayerMap) |
| 464 | NumReservableTeamPlayers += m_aTeamSizes[Team]; |
| 465 | // Starvation is only possible when not every player fits into the legacy id map |
| 466 | m_ReserveAnyTeamSlots = ClientCount <= LEGACY_MAX_CLIENTS - 2 || NumReservableTeamPlayers <= ms_MaxTotalTeamSizePlayerMap; |
| 467 | } |
| 468 | |
| 469 | for(auto &Map : m_aMap) |
| 470 | { |
| 471 | if(!Map.Player() || Server()->ClientSupportsServerMaxClients(ClientId: Map.m_ClientId)) |
| 472 | continue; |
| 473 | |
| 474 | // Calculate overhang every tick, not only when the map updates |
| 475 | int Overhang = std::max(a: 0, b: ClientCount - Map.MapSize()); |
| 476 | if(Overhang != Map.m_TotalOverhang) |
| 477 | { |
| 478 | const int MaxNumSeeOthers = Map.MaxNumSeeOthers(); |
| 479 | Map.m_TotalOverhang = Overhang; |
| 480 | Map.m_NumPages = MaxNumSeeOthers > 0 ? std::max(a: 1, b: (Overhang + MaxNumSeeOthers - 1) / MaxNumSeeOthers) : 1; |
| 481 | if(Map.m_TotalOverhang <= 0 && Map.m_SeeOthersPage != -1) |
| 482 | Map.ResetSeeOthers(); |
| 483 | |
| 484 | Map.UpdateSeeOthers(); |
| 485 | Map.m_UpdateTeamsState = true; |
| 486 | } |
| 487 | |
| 488 | if(Update) |
| 489 | { |
| 490 | Map.Update(); |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | else |
| 495 | { |
| 496 | m_aMap[ClientId].Update(); |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | CPlayerMapping::ESeeOthersInd CPlayerMapping::SeeOthersInd(int ClientId, int MapId) const |
| 501 | { |
| 502 | if(m_aMap[ClientId].m_TotalOverhang && MapId == SeeOthersId(ClientId)) |
| 503 | return ESeeOthersInd::BUTTON; |
| 504 | if(m_aMap[ClientId].m_NumSeeOthers && MapId >= m_aMap[ClientId].MapSize() - m_aMap[ClientId].m_NumSeeOthers && MapId < m_aMap[ClientId].MapSize()) |
| 505 | return ESeeOthersInd::PLAYER; |
| 506 | return ESeeOthersInd::NONE; |
| 507 | } |
| 508 | |
| 509 | const char *CPlayerMapping::SeeOthersName(int ClientId) |
| 510 | { |
| 511 | int Page = m_aMap[ClientId].m_SeeOthersPage + 1; |
| 512 | if(m_aMap[ClientId].m_NumPages > 1 && Page == m_aMap[ClientId].m_NumPages) |
| 513 | { |
| 514 | str_format(buffer: m_aSeeOthersName, buffer_size: sizeof(m_aSeeOthersName), format: "⋅ %d/%d | Close" , Page, Page); |
| 515 | } |
| 516 | else if(m_aMap[ClientId].m_SeeOthersPage != -1) |
| 517 | { |
| 518 | if(m_aMap[ClientId].m_TotalOverhang > m_aMap[ClientId].MaxNumSeeOthers()) |
| 519 | str_format(buffer: m_aSeeOthersName, buffer_size: sizeof(m_aSeeOthersName), format: "⋅ %d/%d" , Page, m_aMap[ClientId].m_NumPages); |
| 520 | else |
| 521 | str_copy(dst&: m_aSeeOthersName, src: "⋅ Close" ); |
| 522 | } |
| 523 | else |
| 524 | { |
| 525 | str_format(buffer: m_aSeeOthersName, buffer_size: sizeof(m_aSeeOthersName), format: "⋅ %d others" , m_aMap[ClientId].m_TotalOverhang); |
| 526 | } |
| 527 | return m_aSeeOthersName; |
| 528 | } |
| 529 | |
| 530 | void CPlayerMapping::CPlayerMap::CycleSeeOthers() |
| 531 | { |
| 532 | if(m_TotalOverhang <= 0) |
| 533 | return; |
| 534 | |
| 535 | for(int i = 0; i < m_pPlayerMapping->Server()->GetMaxClients(ClientId: m_ClientId); i++) |
| 536 | if(m_pMap[i] != -1) |
| 537 | m_aWasSeeOthers[m_pMap[i]] = true; |
| 538 | |
| 539 | int Size = std::min(a: m_TotalOverhang, b: MaxNumSeeOthers()); |
| 540 | int Added = 0; |
| 541 | int MapId = MapSize() - 1; |
| 542 | for(int i = 0; i < MAX_CLIENTS; i++) |
| 543 | { |
| 544 | if(!m_pPlayerMapping->GameServer()->m_apPlayers[i] || m_aWasSeeOthers[i]) |
| 545 | continue; |
| 546 | if(Added >= Size) |
| 547 | break; |
| 548 | |
| 549 | Add(MapId, ClientId: i); |
| 550 | m_aWasSeeOthers[i] = true; |
| 551 | Added++; |
| 552 | MapId--; |
| 553 | } |
| 554 | |
| 555 | m_NumSeeOthers = Added; |
| 556 | } |
| 557 | |
| 558 | void CPlayerMapping::CPlayerMap::DoSeeOthers() |
| 559 | { |
| 560 | if(m_TotalOverhang <= 0) |
| 561 | return; |
| 562 | |
| 563 | // -1 (none) to 1 then 2, or even more if ms_MaxNumSeeOthers is lowered (currently 34 so there are only two pages at most) |
| 564 | m_SeeOthersPage++; |
| 565 | UpdateSeeOthers(); |
| 566 | |
| 567 | CycleSeeOthers(); |
| 568 | |
| 569 | // aggressively trigger reset now |
| 570 | if(m_NumSeeOthers == 0) |
| 571 | { |
| 572 | // Reset these for the next cycle so we can get the fresh page we had before |
| 573 | std::fill(first: std::begin(arr&: m_aWasSeeOthers), last: std::end(arr&: m_aWasSeeOthers), value: false); |
| 574 | CycleSeeOthers(); |
| 575 | ResetSeeOthers(); |
| 576 | } |
| 577 | |
| 578 | // instantly update so we dont have to wait for the map to be executed |
| 579 | m_UpdateTeamsState = true; |
| 580 | Update(); |
| 581 | } |
| 582 | |
| 583 | void CPlayerMapping::CPlayerMap::ResetSeeOthers() |
| 584 | { |
| 585 | m_SeeOthersPage = -1; |
| 586 | m_NumSeeOthers = 0; |
| 587 | std::fill(first: std::begin(arr&: m_aWasSeeOthers), last: std::end(arr&: m_aWasSeeOthers), value: false); |
| 588 | m_UpdateTeamsState = true; |
| 589 | UpdateSeeOthers(); |
| 590 | } |
| 591 | |
| 592 | int CPlayerMapping::CPlayerMap::MaxNumSeeOthers() |
| 593 | { |
| 594 | const int Max = m_pPlayerMapping->Server()->GetMaxClients(ClientId: m_ClientId) == VANILLA_MAX_CLIENTS ? ms_MaxNumSeeOthersVanilla : ms_MaxNumSeeOthers; |
| 595 | |
| 596 | // count non-reserved slots |
| 597 | int = 0; |
| 598 | for(int i = MapSize() - 1; i >= 0; i--) |
| 599 | { |
| 600 | int MappedClientId = m_pMap[i]; |
| 601 | if(MappedClientId != -1 && m_aReserved[MappedClientId]) |
| 602 | break; |
| 603 | NumSeeOthersSlots++; |
| 604 | } |
| 605 | |
| 606 | return std::min(l: {Max, MapSize(), NumSeeOthersSlots}); |
| 607 | } |
| 608 | |
| 609 | void CPlayerMapping::CPlayerMap::UpdateSeeOthers() const |
| 610 | { |
| 611 | if(!m_pPlayerMapping->Server()->IsSixup(ClientId: m_ClientId)) |
| 612 | return; |
| 613 | |
| 614 | int SeeOthersId = m_pPlayerMapping->SeeOthersId(ClientId: m_ClientId); |
| 615 | protocol7::CNetMsg_Sv_ClientDrop ClientDropMsg; |
| 616 | ClientDropMsg.m_ClientId = SeeOthersId; |
| 617 | ClientDropMsg.m_pReason = "" ; |
| 618 | ClientDropMsg.m_Silent = 1; |
| 619 | |
| 620 | protocol7::CNetMsg_Sv_ClientInfo NewClientInfoMsg; |
| 621 | NewClientInfoMsg.m_ClientId = SeeOthersId; |
| 622 | NewClientInfoMsg.m_Local = 0; |
| 623 | NewClientInfoMsg.m_Team = TEAM_BLUE; // `TEAM_BLUE` to hide from ddrace scoreboards |
| 624 | NewClientInfoMsg.m_pName = m_pPlayerMapping->SeeOthersName(ClientId: m_ClientId); |
| 625 | NewClientInfoMsg.m_pClan = "" ; |
| 626 | NewClientInfoMsg.m_Country = -1; |
| 627 | NewClientInfoMsg.m_Silent = 1; |
| 628 | for(int p = 0; p < protocol7::NUM_SKINPARTS; p++) |
| 629 | { |
| 630 | bool Colored = p == protocol7::SKINPART_BODY || p == protocol7::SKINPART_FEET; |
| 631 | NewClientInfoMsg.m_apSkinPartNames[p] = "standard" ; |
| 632 | NewClientInfoMsg.m_aUseCustomColors[p] = (int)Colored; |
| 633 | NewClientInfoMsg.m_aSkinPartColors[p] = Colored ? 5963600 : 0; |
| 634 | } |
| 635 | |
| 636 | m_pPlayerMapping->Server()->SendPackMsg(pMsg: &ClientDropMsg, Flags: MSGFLAG_VITAL | MSGFLAG_NORECORD | MSGFLAG_NOTRANSLATE, ClientId: m_ClientId); |
| 637 | m_pPlayerMapping->Server()->SendPackMsg(pMsg: &NewClientInfoMsg, Flags: MSGFLAG_VITAL | MSGFLAG_NORECORD | MSGFLAG_NOTRANSLATE, ClientId: m_ClientId); |
| 638 | } |
| 639 | |