/ghost/game_base.cpp
http://ghostcb.googlecode.com/ · C++ · 5237 lines · 3551 code · 1091 blank · 595 comment · 1303 complexity · cf655cf2ef0b8f397572278fd51a8654 MD5 · raw file
Large files are truncated click here to view the full file
- /*
-
- Copyright [2008] [Trevor Hogan]
-
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
-
- CODE PORTED FROM THE ORIGINAL GHOST PROJECT: http://ghost.pwner.org/
-
- */
-
- // Next line enables database-stats in UI (Stats, DotA/DB)
- //#define ENABLE_UI_DB_STATS
-
- #include "ghost.h"
- #include "util.h"
- #include "config.h"
- #include "language.h"
- #include "socket.h"
- #include "ghostdb.h"
- #include "bnet.h"
- #include "map.h"
- #include "packed.h"
- #include "savegame.h"
- #include "replay.h"
- #include "gameplayer.h"
- #include "gameprotocol.h"
- #include "game_base.h"
- #include "ui/forward.h"
-
- #include <cmath>
- #include <string.h>
- #include <time.h>
-
- #include "next_combination.h"
-
- //
- // CBaseGame
- //
-
- static uint32_t nextID = 0;
-
- CBaseGame :: CBaseGame( CGHost *nGHost, CMap *nMap, CSaveGame *nSaveGame, uint16_t nHostPort, unsigned char nGameState, string nGameName, string nOwnerName, string nCreatorName, string nCreatorServer )
- {
- m_GameID = nextID++;
-
- m_GHost = nGHost;
- m_Socket = new CTCPServer( );
- m_Protocol = new CGameProtocol( m_GHost );
- m_Map = new CMap( *nMap );
- m_SaveGame = nSaveGame;
-
- if( m_GHost->m_SaveReplays && !m_SaveGame )
- m_Replay = new CReplay( );
- else
- m_Replay = NULL;
-
- m_Exiting = false;
- m_Saving = false;
- m_HostPort = nHostPort;
- m_GameState = nGameState;
- m_VirtualHostPID = 255;
- m_FakePlayerPID = 255;
-
- // wait time of 1 minute = 0 empty actions required
- // wait time of 2 minutes = 1 empty action required
- // etc...
-
- if( m_GHost->m_ReconnectWaitTime == 0 )
- m_GProxyEmptyActions = 0;
- else
- {
- m_GProxyEmptyActions = m_GHost->m_ReconnectWaitTime - 1;
-
- // clamp to 9 empty actions (10 minutes)
-
- if( m_GProxyEmptyActions > 9 )
- m_GProxyEmptyActions = 9;
- }
-
- m_GameName = nGameName;
- m_LastGameName = nGameName;
- m_VirtualHostName = m_GHost->m_VirtualHostName;
- m_OwnerName = nOwnerName;
- m_CreatorName = nCreatorName;
- m_CreatorServer = nCreatorServer;
- m_HCLCommandString = m_Map->GetMapDefaultHCL( );
- m_RandomSeed = GetTicks( );
- m_HostCounter = m_GHost->m_HostCounter++;
- m_EntryKey = rand( );
- m_Latency = m_GHost->m_Latency;
- m_SyncLimit = m_GHost->m_SyncLimit;
- m_SyncCounter = 0;
- m_GameTicks = 0;
- m_CreationTime = GetTime( );
- m_LastPingTime = GetTime( );
- m_LastRefreshTime = GetTime( );
- m_LastDownloadTicks = GetTime( );
- m_DownloadCounter = 0;
- m_LastDownloadCounterResetTicks = GetTicks( );
- m_LastAnnounceTime = 0;
- m_AnnounceInterval = 0;
- m_LastAutoStartTime = GetTime( );
- m_AutoStartPlayers = 0;
- m_LastCountDownTicks = 0;
- m_CountDownCounter = 0;
- m_StartedLoadingTicks = 0;
- m_StartPlayers = 0;
- m_LastLagScreenResetTime = 0;
- m_LastActionSentTicks = 0;
- m_LastActionLateBy = 0;
- m_StartedLaggingTime = 0;
- m_LastLagScreenTime = 0;
- m_LastReservedSeen = GetTime( );
- m_StartedKickVoteTime = 0;
- m_GameOverTime = 0;
- m_LastPlayerLeaveTicks = 0;
- m_MinimumScore = 0.0;
- m_MaximumScore = 0.0;
- m_SlotInfoChanged = false;
- m_Locked = false;
- m_RefreshMessages = m_GHost->m_RefreshMessages;
- m_RefreshError = false;
- m_RefreshRehosted = false;
- m_MuteAll = false;
- m_MuteLobby = false;
- m_CountDownStarted = false;
- m_GameLoading = false;
- m_GameLoaded = false;
- m_LoadInGame = m_Map->GetMapLoadInGame( );
- m_Lagging = false;
- m_AutoSave = m_GHost->m_AutoSave;
- m_MatchMaking = false;
- m_LocalAdminMessages = m_GHost->m_LocalAdminMessages;
- m_UsingStart = false;
- m_LastUiTime = 0;
- m_LastUiSlotsTime = 0;
-
- if( m_SaveGame )
- {
- m_EnforceSlots = m_SaveGame->GetSlots( );
- m_Slots = m_SaveGame->GetSlots( );
-
- // the savegame slots contain player entries
- // we really just want the open/closed/computer entries
- // so open all the player slots
-
- for( vector<CGameSlot> :: iterator i = m_Slots.begin( ); i != m_Slots.end( ); ++i )
- {
- if( (*i).GetSlotStatus( ) == SLOTSTATUS_OCCUPIED && (*i).GetComputer( ) == 0 )
- {
- (*i).SetPID( 0 );
- (*i).SetDownloadStatus( 255 );
- (*i).SetSlotStatus( SLOTSTATUS_OPEN );
- }
- }
- }
- else
- m_Slots = m_Map->GetSlots( );
-
- if( !m_GHost->m_IPBlackListFile.empty( ) )
- {
- ifstream in;
- in.open( m_GHost->m_IPBlackListFile.c_str( ) );
-
- if( in.fail( ) )
- CONSOLE_Print( "[GAME: " + m_GameName + "] error loading IP blacklist file [" + m_GHost->m_IPBlackListFile + "]" );
- else
- {
- CONSOLE_Print( "[GAME: " + m_GameName + "] loading IP blacklist file [" + m_GHost->m_IPBlackListFile + "]" );
- string Line;
-
- while( !in.eof( ) )
- {
- getline( in, Line );
-
- // ignore blank lines and comments
-
- if( Line.empty( ) || Line[0] == '#' )
- continue;
-
- // remove newlines and partial newlines to help fix issues with Windows formatted files on Linux systems
-
- Line.erase( remove( Line.begin( ), Line.end( ), ' ' ), Line.end( ) );
- Line.erase( remove( Line.begin( ), Line.end( ), '\r' ), Line.end( ) );
- Line.erase( remove( Line.begin( ), Line.end( ), '\n' ), Line.end( ) );
-
- // ignore lines that don't look like IP addresses
-
- if( Line.find_first_not_of( "1234567890." ) != string :: npos )
- continue;
-
- m_IPBlackList.insert( Line );
- }
-
- in.close( );
-
- CONSOLE_Print( "[GAME: " + m_GameName + "] loaded " + UTIL_ToString( m_IPBlackList.size( ) ) + " lines from IP blacklist file" );
- }
- }
-
- // start listening for connections
-
- if( !m_GHost->m_BindAddress.empty( ) )
- CONSOLE_Print( "[GAME: " + m_GameName + "] attempting to bind to address [" + m_GHost->m_BindAddress + "]" );
- else
- CONSOLE_Print( "[GAME: " + m_GameName + "] attempting to bind to all available addresses" );
-
- if( m_Socket->Listen( m_GHost->m_BindAddress, m_HostPort ) )
- CONSOLE_Print( "[GAME: " + m_GameName + "] listening on port " + UTIL_ToString( m_HostPort ) );
- else
- {
- CONSOLE_Print( "[GAME: " + m_GameName + "] error listening on port " + UTIL_ToString( m_HostPort ) );
- m_Exiting = true;
- }
-
- forward( new CFwdData( FWD_GAME_ADD, m_GameName, m_GameID ) );
-
- vector<string> MapData;
- MapData.push_back( "Local Path" ); MapData.push_back( m_Map->GetMapLocalPath( ) );
- forward( new CFwdData( FWD_GAME_MAP_INFO_ADD, MapData, m_GameID ) );
-
- MapData.clear( );
- MapData.push_back( "Players" ); MapData.push_back( UTIL_ToString( GetNumHumanPlayers( ) ) + "/" + UTIL_ToString( m_GameLoading || m_GameLoaded ? m_StartPlayers : m_Slots.size( ) ) );
- forward( new CFwdData( FWD_GAME_MAP_INFO_ADD, MapData, m_GameID ) );
-
- MapData.clear( );
- MapData.push_back( "Teams" ); MapData.push_back( UTIL_ToString( m_Map->GetMapNumTeams( ) ) );
- forward( new CFwdData( FWD_GAME_MAP_INFO_ADD, MapData, m_GameID ) );
-
- MapData.clear( );
- MapData.push_back( "Visibility" ); MapData.push_back( m_Map->GetMapVisibility( ) == 4 ? "Default" :
- m_Map->GetMapVisibility( ) == 3 ? "Always visible" :
- m_Map->GetMapVisibility( ) == 2 ? "Explored" : "Hide Terrain" );
- forward( new CFwdData( FWD_GAME_MAP_INFO_ADD, MapData, m_GameID ) );
-
- MapData.clear( );
- MapData.push_back( "Speed" ); MapData.push_back( m_Map->GetMapSpeed( ) == 3 ? "Fast" :
- m_Map->GetMapSpeed( ) == 2 ? "Normal" : "Slow" );
- forward( new CFwdData( FWD_GAME_MAP_INFO_ADD, MapData, m_GameID ) );
-
- MapData.clear( );
- MapData.push_back( "HCL" ); MapData.push_back( m_HCLCommandString );
- forward( new CFwdData( FWD_GAME_MAP_INFO_ADD, MapData, m_GameID ) );
-
- MapData.clear( );
- MapData.push_back( "Owner" ); MapData.push_back( m_OwnerName );
- forward( new CFwdData( FWD_GAME_MAP_INFO_ADD, MapData, m_GameID ) );
-
- MapData.clear( );
- MapData.push_back( "Time" );
-
- if( m_GameLoading || m_GameLoaded )
- MapData.push_back( UTIL_ToString( ( m_GameTicks / 1000 ) / 60 ) + " minutes" );
- else
- MapData.push_back( UTIL_ToString( ( GetTime( ) - m_CreationTime ) / 60 ) + " minutes" );
-
- forward( new CFwdData( FWD_GAME_MAP_INFO_ADD, MapData, m_GameID ) );
- }
-
- CBaseGame :: ~CBaseGame( )
- {
- forward( new CFwdData( FWD_GAME_REMOVE, m_GameID ) );
-
- // save replay
- // todotodo: put this in a thread
-
- if( m_Replay && ( m_GameLoading || m_GameLoaded ) )
- {
- time_t Now = time( NULL );
- char Time[17];
- memset( Time, 0, sizeof( char ) * 17 );
- strftime( Time, sizeof( char ) * 17, "%Y-%m-%d %H-%M", localtime( &Now ) );
- string MinString = UTIL_ToString( ( m_GameTicks / 1000 ) / 60 );
- string SecString = UTIL_ToString( ( m_GameTicks / 1000 ) % 60 );
-
- if( MinString.size( ) == 1 )
- MinString.insert( 0, "0" );
-
- if( SecString.size( ) == 1 )
- SecString.insert( 0, "0" );
-
- m_Replay->BuildReplay( m_GameName, m_StatString, m_GHost->m_ReplayWar3Version, m_GHost->m_ReplayBuildNumber );
- m_Replay->Save( m_GHost->m_TFT, m_GHost->m_ReplayPath + UTIL_FileSafeName( "GHost++ " + string( Time ) + " " + m_GameName + " (" + MinString + "m" + SecString + "s).w3g" ) );
- }
-
- delete m_Socket;
- delete m_Protocol;
- delete m_Map;
- delete m_Replay;
-
- for( vector<CPotentialPlayer *> :: iterator i = m_Potentials.begin( ); i != m_Potentials.end( ); ++i )
- delete *i;
-
- for( vector<CGamePlayer *> :: iterator i = m_Players.begin( ); i != m_Players.end( ); ++i )
- delete *i;
-
- for( vector<CCallableScoreCheck *> :: iterator i = m_ScoreChecks.begin( ); i != m_ScoreChecks.end( ); ++i )
- m_GHost->m_Callables.push_back( *i );
-
- while( !m_Actions.empty( ) )
- {
- delete m_Actions.front( );
- m_Actions.pop( );
- }
- }
-
- uint32_t CBaseGame :: GetNextTimedActionTicks( )
- {
- // return the number of ticks (ms) until the next "timed action", which for our purposes is the next game update
- // the main GHost++ loop will make sure the next loop update happens at or before this value
- // note: there's no reason this function couldn't take into account the game's other timers too but they're far less critical
- // warning: this function must take into account when actions are not being sent (e.g. during loading or lagging)
-
- if( !m_GameLoaded || m_Lagging )
- return 50;
-
- uint32_t TicksSinceLastUpdate = GetTicks( ) - m_LastActionSentTicks;
-
- if( TicksSinceLastUpdate > m_Latency - m_LastActionLateBy )
- return 0;
- else
- return m_Latency - m_LastActionLateBy - TicksSinceLastUpdate;
- }
-
- uint32_t CBaseGame :: GetSlotsOccupied( )
- {
- uint32_t NumSlotsOccupied = 0;
-
- for( vector<CGameSlot> :: iterator i = m_Slots.begin( ); i != m_Slots.end( ); ++i )
- {
- if( (*i).GetSlotStatus( ) == SLOTSTATUS_OCCUPIED )
- ++NumSlotsOccupied;
- }
-
- return NumSlotsOccupied;
- }
-
- uint32_t CBaseGame :: GetSlotsOpen( )
- {
- uint32_t NumSlotsOpen = 0;
-
- for( vector<CGameSlot> :: iterator i = m_Slots.begin( ); i != m_Slots.end( ); ++i )
- {
- if( (*i).GetSlotStatus( ) == SLOTSTATUS_OPEN )
- ++NumSlotsOpen;
- }
-
- return NumSlotsOpen;
- }
-
- uint32_t CBaseGame :: GetNumPlayers( )
- {
- uint32_t NumPlayers = GetNumHumanPlayers( );
-
- if( m_FakePlayerPID != 255 )
- ++NumPlayers;
-
- return NumPlayers;
- }
-
- uint32_t CBaseGame :: GetNumHumanPlayers( )
- {
- uint32_t NumHumanPlayers = 0;
-
- for( vector<CGamePlayer *> :: iterator i = m_Players.begin( ); i != m_Players.end( ); ++i )
- {
- if( !(*i)->GetLeftMessageSent( ) )
- ++NumHumanPlayers;
- }
-
- return NumHumanPlayers;
- }
-
- string CBaseGame :: GetDescription( )
- {
- string Description = m_GameName + " : " + m_OwnerName + " : " + UTIL_ToString( GetNumHumanPlayers( ) ) + "/" + UTIL_ToString( m_GameLoading || m_GameLoaded ? m_StartPlayers : m_Slots.size( ) );
-
- if( m_GameLoading || m_GameLoaded )
- Description += " : " + UTIL_ToString( ( m_GameTicks / 1000 ) / 60 ) + "m";
- else
- Description += " : " + UTIL_ToString( ( GetTime( ) - m_CreationTime ) / 60 ) + "m";
-
- return Description;
- }
-
- void CBaseGame :: SetAnnounce( uint32_t interval, string message )
- {
- m_AnnounceInterval = interval;
- m_AnnounceMessage = message;
- m_LastAnnounceTime = GetTime( );
- }
-
- unsigned int CBaseGame :: SetFD( void *fd, void *send_fd, int *nfds )
- {
- unsigned int NumFDs = 0;
-
- if( m_Socket )
- {
- m_Socket->SetFD( (fd_set *)fd, (fd_set *)send_fd, nfds );
- ++NumFDs;
- }
-
- for( vector<CPotentialPlayer *> :: iterator i = m_Potentials.begin( ); i != m_Potentials.end( ); ++i )
- {
- if( (*i)->GetSocket( ) )
- {
- (*i)->GetSocket( )->SetFD( (fd_set *)fd, (fd_set *)send_fd, nfds );
- ++NumFDs;
- }
- }
-
- for( vector<CGamePlayer *> :: iterator i = m_Players.begin( ); i != m_Players.end( ); ++i )
- {
- if( (*i)->GetSocket( ) )
- {
- (*i)->GetSocket( )->SetFD( (fd_set *)fd, (fd_set *)send_fd, nfds );
- ++NumFDs;
- }
- }
-
- return NumFDs;
- }
-
- bool CBaseGame :: Update( void *fd, void *send_fd )
- {
- // update callables
-
- for( vector<CCallableScoreCheck *> :: iterator i = m_ScoreChecks.begin( ); i != m_ScoreChecks.end( ); )
- {
- if( (*i)->GetReady( ) )
- {
- double Score = (*i)->GetResult( );
-
- for( vector<CPotentialPlayer *> :: iterator j = m_Potentials.begin( ); j != m_Potentials.end( ); ++j )
- {
- if( (*j)->GetJoinPlayer( ) && (*j)->GetJoinPlayer( )->GetName( ) == (*i)->GetName( ) )
- EventPlayerJoinedWithScore( *j, (*j)->GetJoinPlayer( ), Score );
- }
-
- m_GHost->m_DB->RecoverCallable( *i );
- delete *i;
- i = m_ScoreChecks.erase( i );
- }
- else
- ++i;
- }
-
- // update players
-
- for( vector<CGamePlayer *> :: iterator i = m_Players.begin( ); i != m_Players.end( ); )
- {
- if( (*i)->Update( fd ) )
- {
- EventPlayerDeleted( *i );
- delete *i;
- i = m_Players.erase( i );
- }
- else
- ++i;
- }
-
- for( vector<CPotentialPlayer *> :: iterator i = m_Potentials.begin( ); i != m_Potentials.end( ); )
- {
- if( (*i)->Update( fd ) )
- {
- // flush the socket (e.g. in case a rejection message is queued)
-
- if( (*i)->GetSocket( ) )
- (*i)->GetSocket( )->DoSend( (fd_set *)send_fd );
-
- delete *i;
- i = m_Potentials.erase( i );
- }
- else
- ++i;
- }
-
- // create the virtual host player
-
- if( !m_GameLoading && !m_GameLoaded && GetNumPlayers( ) < 12 )
- CreateVirtualHost( );
-
- // unlock the game
-
- if( m_Locked && !GetPlayerFromName( m_OwnerName, false ) )
- {
- SendAllChat( m_GHost->m_Language->GameUnlocked( ) );
- m_Locked = false;
- }
-
- // ping every 5 seconds
- // changed this to ping during game loading as well to hopefully fix some problems with people disconnecting during loading
- // changed this to ping during the game as well
-
- if( GetTime( ) - m_LastPingTime >= 5 )
- {
- // note: we must send pings to players who are downloading the map because Warcraft III disconnects from the lobby if it doesn't receive a ping every ~90 seconds
- // so if the player takes longer than 90 seconds to download the map they would be disconnected unless we keep sending pings
- // todotodo: ignore pings received from players who have recently finished downloading the map
-
- SendAll( m_Protocol->SEND_W3GS_PING_FROM_HOST( ) );
-
- // we also broadcast the game to the local network every 5 seconds so we hijack this timer for our nefarious purposes
- // however we only want to broadcast if the countdown hasn't started
- // see the !sendlan code later in this file for some more information about how this works
- // todotodo: should we send a game cancel message somewhere? we'll need to implement a host counter for it to work
-
- if( !m_CountDownStarted )
- {
- // construct a fixed host counter which will be used to identify players from this "realm" (i.e. LAN)
- // the fixed host counter's 4 most significant bits will contain a 4 bit ID (0-15)
- // the rest of the fixed host counter will contain the 28 least significant bits of the actual host counter
- // since we're destroying 4 bits of information here the actual host counter should not be greater than 2^28 which is a reasonable assumption
- // when a player joins a game we can obtain the ID from the received host counter
- // note: LAN broadcasts use an ID of 0, battle.net refreshes use an ID of 1-10, the rest are unused
-
- uint32_t FixedHostCounter = m_HostCounter & 0x0FFFFFFF;
-
- // we send 12 for SlotsTotal because this determines how many PID's Warcraft 3 allocates
- // we need to make sure Warcraft 3 allocates at least SlotsTotal + 1 but at most 12 PID's
- // this is because we need an extra PID for the virtual host player (but we always delete the virtual host player when the 12th person joins)
- // however, we can't send 13 for SlotsTotal because this causes Warcraft 3 to crash when sharing control of units
- // nor can we send SlotsTotal because then Warcraft 3 crashes when playing maps with less than 12 PID's (because of the virtual host player taking an extra PID)
- // we also send 12 for SlotsOpen because Warcraft 3 assumes there's always at least one player in the game (the host)
- // so if we try to send accurate numbers it'll always be off by one and results in Warcraft 3 assuming the game is full when it still needs one more player
- // the easiest solution is to simply send 12 for both so the game will always show up as (1/12) players
-
- if( m_SaveGame )
- {
- // note: the PrivateGame flag is not set when broadcasting to LAN (as you might expect)
-
- uint32_t MapGameType = MAPGAMETYPE_SAVEDGAME;
- BYTEARRAY MapWidth;
- BYTEARRAY MapHeight;
- if ( m_GHost->m_Reconnect )
- {
- MapWidth.push_back( 192 );
- MapWidth.push_back( 7 );
-
- MapHeight.push_back( 192 );
- MapHeight.push_back( 7 );
- }
- else
- {
- MapWidth.push_back( 0 );
- MapWidth.push_back( 0 );
-
- MapHeight.push_back( 0 );
- MapHeight.push_back( 0 );
- }
-
- BYTEARRAY data = m_Protocol->SEND_W3GS_GAMEINFO( m_GHost->m_TFT, m_GHost->m_LANWar3Version, UTIL_CreateByteArray( MapGameType, false ), m_Map->GetMapGameFlags( ), MapWidth, MapHeight, m_GameName, "Varlock", GetTime( ) - m_CreationTime, "Save\\Multiplayer\\" + m_SaveGame->GetFileNameNoPath( ), m_SaveGame->GetMagicNumber( ), 12, 12, m_HostPort, FixedHostCounter, m_EntryKey );
- m_GHost->m_UDPSocket->Broadcast( 6112, data );
- for(vector<CTCPSocket * >::iterator i = m_GHost->m_GameBroadcasters.begin( ); i!= m_GHost->m_GameBroadcasters.end( ); i++ )
- {
- (*i)->PutBytes( data );
- }
- }
- else
- {
- // note: the PrivateGame flag is not set when broadcasting to LAN (as you might expect)
- // note: we do not use m_Map->GetMapGameType because none of the filters are set when broadcasting to LAN (also as you might expect)
- uint32_t MapGameType = MAPGAMETYPE_UNKNOWN0;
- BYTEARRAY MapWidth;
- BYTEARRAY MapHeight;
- if ( m_GHost->m_Reconnect )
- {
- MapWidth.push_back( 192 );
- MapWidth.push_back( 7 );
- MapHeight.push_back( 192 );
- MapHeight.push_back( 7 );
- }
- else
- {
- MapWidth = m_Map->GetMapWidth( );
- MapHeight = m_Map->GetMapHeight( );
- }
- BYTEARRAY data = m_Protocol->SEND_W3GS_GAMEINFO( m_GHost->m_TFT, m_GHost->m_LANWar3Version, UTIL_CreateByteArray( MapGameType, false ), m_Map->GetMapGameFlags( ), MapWidth, MapHeight, m_GameName, "Varlock", GetTime( ) - m_CreationTime, m_Map->GetMapPath( ), m_Map->GetMapCRC( ), 12, 12, m_HostPort, FixedHostCounter, m_EntryKey );
- m_GHost->m_UDPSocket->Broadcast( 6112, data );
- for(vector<CTCPSocket * >::iterator i = m_GHost->m_GameBroadcasters.begin( ); i!= m_GHost->m_GameBroadcasters.end( ); i++ )
- {
- (*i)->PutBytes( data );
- }
- }
- }
-
- m_LastPingTime = GetTime( );
- }
-
- // auto rehost if there was a refresh error in autohosted games
-
- if( m_RefreshError && !m_CountDownStarted && m_GameState == GAME_PUBLIC && !m_GHost->m_AutoHostGameName.empty( ) && m_GHost->m_AutoHostMaximumGames != 0 && m_GHost->m_AutoHostAutoStartPlayers != 0 && m_AutoStartPlayers != 0 )
- {
- // there's a slim chance that this isn't actually an autohosted game since there is no explicit autohost flag
- // however, if autohosting is enabled and this game is public and this game is set to autostart, it's probably autohosted
- // so rehost it using the current autohost game name
-
- string GameName = m_GHost->m_AutoHostGameName + " #" + UTIL_ToString( m_GHost->m_HostCounter );
- CONSOLE_Print( "[GAME: " + m_GameName + "] automatically trying to rehost as public game [" + GameName + "] due to refresh failure" );
- m_LastGameName = m_GameName;
- m_GameName = GameName;
- m_HostCounter = m_GHost->m_HostCounter++;
- m_RefreshError = false;
-
- for( vector<CBNET *> :: iterator i = m_GHost->m_BNETs.begin( ); i != m_GHost->m_BNETs.end( ); ++i )
- {
- (*i)->QueueGameUncreate( );
- (*i)->QueueEnterChat( );
-
- // the game creation message will be sent on the next refresh
- }
-
- m_CreationTime = GetTime( );
- m_LastRefreshTime = GetTime( );
- }
-
- // refresh every 3 seconds
-
- if( !m_RefreshError && !m_CountDownStarted && m_GameState == GAME_PUBLIC && GetSlotsOpen( ) > 0 && GetTime( ) - m_LastRefreshTime >= 3 )
- {
- // send a game refresh packet to each battle.net connection
-
- bool Refreshed = false;
-
- for( vector<CBNET *> :: iterator i = m_GHost->m_BNETs.begin( ); i != m_GHost->m_BNETs.end( ); ++i )
- {
- // don't queue a game refresh message if the queue contains more than 1 packet because they're very low priority
-
- if( (*i)->GetOutPacketsQueued( ) <= 1 )
- {
- /*
- Varlock's original refresh
- (*i)->QueueGameRefresh( m_GameState, m_GameName, string( ), m_Map, m_SaveGame, GetTime( ) - m_CreationTime, m_HostCounter );
- */
-
- // Refresh code from Fire86 (http://codelain.com/forum/index.php?topic=11373.msg88767#msg88767)
- (*i)->QueueGameRefresh( m_GameState, m_GameName, string( ), m_Map, m_SaveGame, 0, m_HostCounter );
-
- Refreshed = true;
- }
- }
-
- // only print the "game refreshed" message if we actually refreshed on at least one battle.net server
-
- if( m_RefreshMessages && Refreshed )
- SendAllChat( m_GHost->m_Language->GameRefreshed( ) );
-
- m_LastRefreshTime = GetTime( );
- }
-
- // send more map data
-
- if( !m_GameLoading && !m_GameLoaded && GetTicks( ) - m_LastDownloadCounterResetTicks >= 1000 )
- {
- // hackhack: another timer hijack is in progress here
- // since the download counter is reset once per second it's a great place to update the slot info if necessary
-
- if( m_SlotInfoChanged )
- SendAllSlotInfo( );
-
- m_DownloadCounter = 0;
- m_LastDownloadCounterResetTicks = GetTicks( );
- }
-
- if( !m_GameLoading && !m_GameLoaded && GetTicks( ) - m_LastDownloadTicks >= 100 )
- {
- uint32_t Downloaders = 0;
-
- for( vector<CGamePlayer *> :: iterator i = m_Players.begin( ); i != m_Players.end( ); ++i )
- {
- if( (*i)->GetDownloadStarted( ) && !(*i)->GetDownloadFinished( ) )
- {
- ++Downloaders;
-
- if( m_GHost->m_MaxDownloaders > 0 && Downloaders > m_GHost->m_MaxDownloaders )
- break;
-
- // send up to 100 pieces of the map at once so that the download goes faster
- // if we wait for each MAPPART packet to be acknowledged by the client it'll take a long time to download
- // this is because we would have to wait the round trip time (the ping time) between sending every 1442 bytes of map data
- // doing it this way allows us to send at least 140 KB in each round trip interval which is much more reasonable
- // the theoretical throughput is [140 KB * 1000 / ping] in KB/sec so someone with 100 ping (round trip ping, not LC ping) could download at 1400 KB/sec
- // note: this creates a queue of map data which clogs up the connection when the client is on a slower connection (e.g. dialup)
- // in this case any changes to the lobby are delayed by the amount of time it takes to send the queued data (i.e. 140 KB, which could be 30 seconds or more)
- // for example, players joining and leaving, slot changes, chat messages would all appear to happen much later for the low bandwidth player
- // note: the throughput is also limited by the number of times this code is executed each second
- // e.g. if we send the maximum amount (140 KB) 10 times per second the theoretical throughput is 1400 KB/sec
- // therefore the maximum throughput is 1400 KB/sec regardless of ping and this value slowly diminishes as the player's ping increases
- // in addition to this, the throughput is limited by the configuration value bot_maxdownloadspeed
- // in summary: the actual throughput is MIN( 140 * 1000 / ping, 1400, bot_maxdownloadspeed ) in KB/sec assuming only one player is downloading the map
-
- uint32_t MapSize = UTIL_ByteArrayToUInt32( m_Map->GetMapSize( ), false );
-
- while( (*i)->GetLastMapPartSent( ) < (*i)->GetLastMapPartAcked( ) + 1442 * 100 && (*i)->GetLastMapPartSent( ) < MapSize )
- {
- if( (*i)->GetLastMapPartSent( ) == 0 )
- {
- // overwrite the "started download ticks" since this is the first time we've sent any map data to the player
- // prior to this we've only determined if the player needs to download the map but it's possible we could have delayed sending any data due to download limits
-
- (*i)->SetStartedDownloadingTicks( GetTicks( ) );
- }
-
- // limit the download speed if we're sending too much data
- // the download counter is the # of map bytes downloaded in the last second (it's reset once per second)
-
- if( m_GHost->m_MaxDownloadSpeed > 0 && m_DownloadCounter > m_GHost->m_MaxDownloadSpeed * 1024 )
- break;
-
- Send( *i, m_Protocol->SEND_W3GS_MAPPART( GetHostPID( ), (*i)->GetPID( ), (*i)->GetLastMapPartSent( ), m_Map->GetMapData( ) ) );
- (*i)->SetLastMapPartSent( (*i)->GetLastMapPartSent( ) + 1442 );
- m_DownloadCounter += 1442;
- }
- }
- }
-
- m_LastDownloadTicks = GetTicks( );
- }
-
- // announce every m_AnnounceInterval seconds
-
- if( !m_AnnounceMessage.empty( ) && !m_CountDownStarted && GetTime( ) - m_LastAnnounceTime >= m_AnnounceInterval )
- {
- SendAllChat( m_AnnounceMessage );
- m_LastAnnounceTime = GetTime( );
- }
-
- // kick players who don't spoof check within 20 seconds when spoof checks are required and the game is autohosted
-
- if( !m_CountDownStarted && m_GHost->m_RequireSpoofChecks && m_GameState == GAME_PUBLIC && !m_GHost->m_AutoHostGameName.empty( ) && m_GHost->m_AutoHostMaximumGames != 0 && m_GHost->m_AutoHostAutoStartPlayers != 0 && m_AutoStartPlayers != 0 )
- {
- for( vector<CGamePlayer *> :: iterator i = m_Players.begin( ); i != m_Players.end( ); ++i )
- {
- if( !(*i)->GetSpoofed( ) && GetTime( ) - (*i)->GetJoinTime( ) >= 20 )
- {
- (*i)->SetDeleteMe( true );
- (*i)->SetLeftReason( m_GHost->m_Language->WasKickedForNotSpoofChecking( ) );
- (*i)->SetLeftCode( PLAYERLEAVE_LOBBY );
- OpenSlot( GetSIDFromPID( (*i)->GetPID( ) ), false );
- }
- }
- }
-
- // try to auto start every 5 sec (!start) or 10 sec (!autostart)
- if( m_UsingStart == true )
- {
- if( !m_CountDownStarted && m_AutoStartPlayers != 0 && GetTime( ) - m_LastAutoStartTime >= 5 )
- {
- StartCountDownAuto( m_GHost->m_RequireSpoofChecks );
- m_LastAutoStartTime = GetTime( );
- }
- }
- else
- if( !m_CountDownStarted && m_AutoStartPlayers != 0 && GetTime( ) - m_LastAutoStartTime >= 10 )
- {
- StartCountDownAuto( m_GHost->m_RequireSpoofChecks );
- m_LastAutoStartTime = GetTime( );
- }
-
- // normal countdown if active every 1200 ms (because we need an extra sec for 0 )
-
- if ( m_GHost->m_UseNormalCountDown )
- {
- if( m_CountDownStarted && GetTicks( ) >= m_LastCountDownTicks + 1200 )
- {
- if( m_CountDownCounter > 0 )
- {
- --m_CountDownCounter;
- }
- else if( !m_GameLoading && !m_GameLoaded )
- {
- EventGameStarted( );
- }
-
- m_LastCountDownTicks = GetTicks( );
- }
- }
- else
- {
- // countdown (ghost style) every 500 ms
-
- if( m_CountDownStarted && GetTicks( ) >= m_LastCountDownTicks + 500 )
- {
- if( m_CountDownCounter > 0 )
- {
- // we use a countdown counter rather than a "finish countdown time" here because it might alternately round up or down the count
- // this sometimes resulted in a countdown of e.g. "6 5 3 2 1" during my testing which looks pretty dumb
- // doing it this way ensures it's always "5 4 3 2 1" but each interval might not be *exactly* the same length
-
- SendAllChat( UTIL_ToString( m_CountDownCounter ) + ". . ." );
- m_CountDownCounter--;
- }
- else if( !m_GameLoading && !m_GameLoaded )
- EventGameStarted( );
-
- m_LastCountDownTicks = GetTicks( );
- }
- }
-
- // check if the lobby is "abandoned" and needs to be closed since it will never start
-
- if( !m_GameLoading && !m_GameLoaded && m_AutoStartPlayers == 0 && m_GHost->m_LobbyTimeLimit > 0 )
- {
- // check if there's a player with reserved status in the game
-
- for( vector<CGamePlayer *> :: iterator i = m_Players.begin( ); i != m_Players.end( ); ++i )
- {
- if( (*i)->GetReserved( ) )
- m_LastReservedSeen = GetTime( );
- }
-
- // check if we've hit the time limit
-
- if( GetTime( ) - m_LastReservedSeen >= m_GHost->m_LobbyTimeLimit * 60 )
- {
- CONSOLE_Print( "[GAME: " + m_GameName + "] is over (lobby time limit hit)" );
- return true;
- }
- }
-
- // check if the game is loaded
-
- if( m_GameLoading )
- {
- bool FinishedLoading = true;
-
- for( vector<CGamePlayer *> :: iterator i = m_Players.begin( ); i != m_Players.end( ); ++i )
- {
- FinishedLoading = (*i)->GetFinishedLoading( );
-
- if( !FinishedLoading )
- break;
- }
-
- if( FinishedLoading )
- {
- m_LastActionSentTicks = GetTicks( );
- m_GameLoading = false;
- m_GameLoaded = true;
- EventGameLoaded( );
- }
- else
- {
- // reset the "lag" screen (the load-in-game screen) every 30 seconds
-
- if( m_LoadInGame && GetTime( ) - m_LastLagScreenResetTime >= 30 )
- {
- bool UsingGProxy = false;
-
- for( vector<CGamePlayer *> :: iterator i = m_Players.begin( ); i != m_Players.end( ); ++i )
- {
- if( (*i)->GetGProxy( ) )
- UsingGProxy = true;
- }
-
- for( vector<CGamePlayer *> :: iterator i = m_Players.begin( ); i != m_Players.end( ); ++i )
- {
- if( (*i)->GetFinishedLoading( ) )
- {
- // stop the lag screen
-
- for( vector<CGamePlayer *> :: iterator j = m_Players.begin( ); j != m_Players.end( ); ++j )
- {
- if( !(*j)->GetFinishedLoading( ) )
- Send( *i, m_Protocol->SEND_W3GS_STOP_LAG( *j, true ) );
- }
-
- // send an empty update
- // this resets the lag screen timer but creates a rather annoying problem
- // in order to prevent a desync we must make sure every player receives the exact same "desyncable game data" (updates and player leaves) in the exact same order
- // unfortunately we cannot send updates to players who are still loading the map, so we buffer the updates to those players (see the else clause a few lines down for the code)
- // in addition to this we must ensure any player leave messages are sent in the exact same position relative to these updates so those must be buffered too
-
- if( UsingGProxy && !(*i)->GetGProxy( ) )
- {
- // we must send empty actions to non-GProxy++ players
- // GProxy++ will insert these itself so we don't need to send them to GProxy++ players
- // empty actions are used to extend the time a player can use when reconnecting
-
- for( unsigned char j = 0; j < m_GProxyEmptyActions; ++j )
- Send( *i, m_Protocol->SEND_W3GS_INCOMING_ACTION( queue<CIncomingAction *>( ), 0 ) );
- }
-
- Send( *i, m_Protocol->SEND_W3GS_INCOMING_ACTION( queue<CIncomingAction *>( ), 0 ) );
-
- // start the lag screen
-
- Send( *i, m_Protocol->SEND_W3GS_START_LAG( m_Players, true ) );
- }
- else
- {
- // buffer the empty update since the player is still loading the map
-
- if( UsingGProxy && !(*i)->GetGProxy( ) )
- {
- // we must send empty actions to non-GProxy++ players
- // GProxy++ will insert these itself so we don't need to send them to GProxy++ players
- // empty actions are used to extend the time a player can use when reconnecting
-
- for( unsigned char j = 0; j < m_GProxyEmptyActions; ++j )
- (*i)->AddLoadInGameData( m_Protocol->SEND_W3GS_INCOMING_ACTION( queue<CIncomingAction *>( ), 0 ) );
- }
-
- (*i)->AddLoadInGameData( m_Protocol->SEND_W3GS_INCOMING_ACTION( queue<CIncomingAction *>( ), 0 ) );
- }
- }
-
- // add actions to replay
-
- if( m_Replay )
- {
- if( UsingGProxy )
- {
- for( unsigned char i = 0; i < m_GProxyEmptyActions; ++i )
- m_Replay->AddTimeSlot( 0, queue<CIncomingAction *>( ) );
- }
-
- m_Replay->AddTimeSlot( 0, queue<CIncomingAction *>( ) );
- }
-
- // Warcraft III doesn't seem to respond to empty actions
-
- /* if( UsingGProxy )
- m_SyncCounter += m_GProxyEmptyActions;
-
- m_SyncCounter++; */
- m_LastLagScreenResetTime = GetTime( );
- }
- }
- }
-
- // keep track of the largest sync counter (the number of keepalive packets received by each player)
- // if anyone falls behind by more than m_SyncLimit keepalives we start the lag screen
-
- if( m_GameLoaded )
- {
- // check if anyone has started lagging
- // we consider a player to have started lagging if they're more than m_SyncLimit keepalives behind
-
- if( !m_Lagging )
- {
- string LaggingString;
-
- for( vector<CGamePlayer *> :: iterator i = m_Players.begin( ); i != m_Players.end( ); ++i )
- {
- if( m_SyncCounter - (*i)->GetSyncCounter( ) > m_SyncLimit )
- {
- (*i)->SetLagging( true );
- (*i)->SetStartedLaggingTicks( GetTicks( ) );
- m_Lagging = true;
- m_StartedLaggingTime = GetTime( );
-
- if( LaggingString.empty( ) )
- LaggingString = (*i)->GetName( );
- else
- LaggingString += ", " + (*i)->GetName( );
- }
- }
-
- if( m_Lagging )
- {
- // start the lag screen
-
- CONSOLE_Print( "[GAME: " + m_GameName + "] started lagging on [" + LaggingString + "]" );
- SendAll( m_Protocol->SEND_W3GS_START_LAG( m_Players ) );
-
- // reset everyone's drop vote
-
- for( vector<CGamePlayer *> :: iterator i = m_Players.begin( ); i != m_Players.end( ); ++i )
- (*i)->SetDropVote( false );
-
- m_LastLagScreenResetTime = GetTime( );
- }
- }
-
- if( m_Lagging )
- {
- bool UsingGProxy = false;
-
- for( vector<CGamePlayer *> :: iterator i = m_Players.begin( ); i != m_Players.end( ); ++i )
- {
- if( (*i)->GetGProxy( ) )
- UsingGProxy = true;
- }
-
- uint32_t WaitTime = 60;
-
- if( UsingGProxy )
- WaitTime = ( m_GProxyEmptyActions + 1 ) * 60;
-
- if( GetTime( ) - m_StartedLaggingTime >= WaitTime )
- StopLaggers( m_GHost->m_Language->WasAutomaticallyDroppedAfterSeconds( UTIL_ToString( WaitTime ) ) );
-
- // we cannot allow the lag screen to stay up for more than ~65 seconds because Warcraft III disconnects if it doesn't receive an action packet at least this often
- // one (easy) solution is to simply drop all the laggers if they lag for more than 60 seconds
- // another solution is to reset the lag screen the same way we reset it when using load-in-game
- // this is required in order to give GProxy++ clients more time to reconnect
-
- if( GetTime( ) - m_LastLagScreenResetTime >= 60 )
- {
- for( vector<CGamePlayer *> :: iterator i = m_Players.begin( ); i != m_Players.end( ); ++i )
- {
- // stop the lag screen
-
- for( vector<CGamePlayer *> :: iterator j = m_Players.begin( ); j != m_Players.end( ); ++j )
- {
- if( (*j)->GetLagging( ) )
- Send( *i, m_Protocol->SEND_W3GS_STOP_LAG( *j ) );
- }
-
- // send an empty update
- // this resets the lag screen timer
-
- if( UsingGProxy && !(*i)->GetGProxy( ) )
- {
- // we must send additional empty actions to non-GProxy++ players
- // GProxy++ will insert these itself so we don't need to send them to GProxy++ players
- // empty actions are used to extend the time a player can use when reconnecting
-
- for( unsigned char j = 0; j < m_GProxyEmptyActions; ++j )
- Send( *i, m_Protocol->SEND_W3GS_INCOMING_ACTION( queue<CIncomingAction *>( ), 0 ) );
- }
-
- Send( *i, m_Protocol->SEND_W3GS_INCOMING_ACTION( queue<CIncomingAction *>( ), 0 ) );
-
- // start the lag screen
-
- Send( *i, m_Protocol->SEND_W3GS_START_LAG( m_Players ) );
- }
-
- // add actions to replay
-
- if( m_Replay )
- {
- if( UsingGProxy )
- {
- for( unsigned char i = 0; i < m_GProxyEmptyActions; ++i )
- m_Replay->AddTimeSlot( 0, queue<CIncomingAction *>( ) );
- }
-
- m_Replay->AddTimeSlot( 0, queue<CIncomingAction *>( ) );
- }
-
- // Warcraft III doesn't seem to respond to empty actions
-
- /* if( UsingGProxy )
- m_SyncCounter += m_GProxyEmptyActions;
-
- m_SyncCounter++; */
- m_LastLagScreenResetTime = GetTime( );
- }
-
- // check if anyone has stopped lagging normally
- // we consider a player to have stopped lagging if they're less than half m_SyncLimit keepalives behind
-
- for( vector<CGamePlayer *> :: iterator i = m_Players.begin( ); i != m_Players.end( ); ++i )
- {
- if( (*i)->GetLagging( ) && m_SyncCounter - (*i)->GetSyncCounter( ) < m_SyncLimit / 2 )
- {
- // stop the lag screen for this player
-
- CONSOLE_Print( "[GAME: " + m_GameName + "] stopped lagging on [" + (*i)->GetName( ) + "]" );
- SendAll( m_Protocol->SEND_W3GS_STOP_LAG( *i ) );
- (*i)->SetLagging( false );
- (*i)->SetStartedLaggingTicks( 0 );
- }
- }
-
- // check if everyone has stopped lagging
-
- bool Lagging = false;
-
- for( vector<CGamePlayer *> :: iterator i = m_Players.begin( ); i != m_Players.end( ); ++i )
- {
- if( (*i)->GetLagging( ) )
- Lagging = true;
- }
-
- m_Lagging = Lagging;
-
- // reset m_LastActionSentTicks because we want the game to stop running while the lag screen is up
-
- m_LastActionSentTicks = GetTicks( );
-
- // keep track of the last lag screen time so we can avoid timing out players
-
- m_LastLagScreenTime = GetTime( );
- }
- }
-
- // send actions every m_Latency milliseconds
- // actions are at the heart of every Warcraft 3 game but luckily we don't need to know their contents to relay them
- // we queue player actions in EventPlayerAction then just resend them in batches to all players here
-
- if( m_GameLoaded && !m_Lagging && GetTicks( ) - m_LastActionSentTicks >= m_Latency - m_LastActionLateBy )
- SendAllActions( );
-
- // expire the votekick
-
- if( !m_KickVotePlayer.empty( ) && GetTime( ) - m_StartedKickVoteTime >= 60 )
- {
- CONSOLE_Print( "[GAME: " + m_GameName + "] votekick against player [" + m_KickVotePlayer + "] expired" );
- SendAllChat( m_GHost->m_Language->VoteKickExpired( m_KickVotePlayer ) );
- m_KickVotePlayer.clear( );
- m_StartedKickVoteTime = 0;
- }
-
- // start the gameover timer if there's only one player left
-
- if( m_Players.size( ) == 1 && m_FakePlayerPID == 255 && m_GameOverTime == 0 && ( m_GameLoading || m_GameLoaded ) )
- {
- CONSOLE_Print( "[GAME: " + m_GameName + "] gameover timer started (one player left)" );
- m_GameOverTime = GetTime( );
- }
-
- // finish the gameover timer
-
- if( m_GameOverTime != 0 && GetTime( ) - m_GameOverTime >= 60 )
- {
- bool AlreadyStopped = true;
-
- for( vector<CGamePlayer *> :: iterator i = m_Players.begin( ); i != m_Players.end( ); ++i )
- {
- if( !(*i)->GetDeleteMe( ) )
- {
- AlreadyStopped = false;
- break;
- }
- }
-
- if( !AlreadyStopped )
- {
- CONSOLE_Print( "[GAME: " + m_GameName + "] is over (gameover timer finished)" );
- StopPlayers( "was disconnected (gameover timer finished)" );
- }
- }
-
- // end the game if there aren't any players left
-
- if( m_Players.empty( ) && ( m_GameLoading || m_GameLoaded ) )
- {
- if( !m_Saving )
- {
- CONSOLE_Print( "[GAME: " + m_GameName + "] is over (no players left)" );
- SaveGameData( );
- m_Saving = true;
- }
- else if( IsGameDataSaved( ) )
- return true;
- }
-
- // accept new connections
-
- if( m_Socket )
- {
- CTCPSocket *NewSocket = m_Socket->Accept( (fd_set *)fd );
-
- if( NewSocket )
- {
- // check the IP blacklist
-
- if( m_IPBlackList.find( NewSocket->GetIPString( ) ) == m_IPBlackList.end( ) )
- {
- if( m_GHost->m_TCPNoDelay )
- NewSocket->SetNoDelay( true );
-
- m_Potentials.push_back( new CPotentialPlayer( m_Protocol, this, NewSocket ) );
- }
- else
- {
- CONSOLE_Print( "[GAME: " + m_GameName + "] rejected connection from [" + NewSocket->GetIPString( ) + "] due to blacklist" );
- delete NewSocket;
- }
- }
-
- if( m_Socket->HasError( ) )
- return true;
- }
-
- // update time in ui
-
- if( GetTime( ) > m_LastUiTime )
- {
- vector<string> MapData;
- MapData.push_back( "Time" );
-
- if( m_GameLoading || m_GameLoaded )
- MapData.push_back( UTIL_ToString( ( m_GameTicks / 1000 ) / 60 ) + " minutes" );
- else
- MapData.push_back( UTIL_ToString( ( GetTime( ) - m_CreationTime ) / 60 ) + " minutes" );
-
- forward( new CFwdData( FWD_GAME_MAP_INFO_UPDATE, MapData, m_GameID ) );
-
- m_LastUiTime = GetTime( ) + 60;
- }
-
- // update players in ui
-
- if( GetTime( ) > m_LastUiSlotsTime )
- {
- vector<string> playerData;
- for( vector<CGamePlayer *> :: iterator i = m_Players.begin( ); i != m_Players.end( ); ++i )
- {
- playerData.clear( );
- playerData.push_back( (*i)->GetName( ) ); // name
- playerData.push_back( UTIL_ToString( GetSIDFromPID( (*i)->GetPID( ) ) + 1 ) ); // slot
- playerData.push_back( m_GHost->m_DBLocal->FromCheck( UTIL_ByteArrayToUInt32( (*i)->GetExternalIP( ), true ) ) ); // from
- playerData.push_back( UTIL_ToString( (*i)->GetPing(true) ) + " ms" ); // ping
- playerData.push_back( m_Slots[ GetSIDFromPID( (*i)->GetPID( ) ) ].GetRace( ) == 1 ? "HU" :
- m_Slots[ GetSIDFromPID( (*i)->GetPID( ) ) ].GetRace( ) == 2 ? "ORC" :
- m_Slots[ GetSIDFromPID( (*i)->GetPID( ) ) ].GetRace( ) == 4 ? "NE" :
- m_Slots[ GetSIDFromPID( (*i)->GetPID( ) ) ].GetRace( ) == 8 ? "UD" : "??" ); // race
- playerData.push_back( UTIL_ToString( m_Slots[GetSIDFromPID( (*i)->GetPID( ) )].GetTeam( ) + 1 ) ); // team
- playerData.push_back( ColourValueToString( m_Slots[GetSIDFromPID( (*i)->GetPID( ) )].GetColour( ) ) ); // color
- playerData.push_back( UTIL_ToString( m_Slots[GetSIDFromPID( (*i)->GetPID( ) )].GetHandicap( ) ) ); // handicap
- playerData.push_back( (*i)->GetGProxy( ) ? "on" : "off" ); // gproxy++
-
- forward( new CFwdData( FWD_GAME_SLOT_UPDATE, playerData, m_GameID ) );
- }
-
- m_LastUiSlotsTime = GetTime( ) + 10;
- }
-
- return m_Exiting;
- }
-
- void CBaseGame :: UpdatePost( void *send_fd )
- {
- // we need to manually call DoSend on each player now because CGamePlayer :: Update doesn't do it
- // this is in case player 2 generates a packet for player 1 during the update but it doesn't get sent because player 1 already finished updating
- // in reality since we're queueing actions it might not make a big difference but oh well
-
- for( vector<CGamePlayer *> :: iterator i = m_Players.begin( ); i != m_Players.end( ); ++i )
- {
- if( (*i)->GetSocket( ) )
- (*i)->GetSocket( )->DoSend( (fd_set *)send_fd );
- }
-
- for( vector<CPotentialPlayer *> :: iterator i = m_Potentials.begin( ); i != m_Potentials.end( ); ++i )
- {
- if( (*i)->GetSocket( ) )
- (*i)->GetSocket( )->DoSend( (fd_set *)send_fd );
- }
- }
-
- void CBaseGame :: Send( CGamePlayer *player, BYTEARRAY data )
- {
- if( player )
- player->Send( data );
- }
-
- void CBaseGame :: Send( unsigned char PID, BYTEARRAY data )
- {
- Send( GetPlayerFromPID( PID ), data );
- }
-
- void CBaseGame :: Send( BYTEARRAY PIDs, BYTEARRAY data )
- {
- for( unsigned int i = 0; i < PIDs.size( ); ++i )
- Send( PIDs[i], data );
- }
-
- void CBaseGame :: SendAll( BYTEARRAY data )
- {
- for( vector<CGamePlayer *> :: iterator i = m_Players.begin( ); i != m_Players.end( ); ++i )
- (*i)->Send( data );
- }
-
- void CBaseGame :: SendChat( unsigned char fromPID, CGamePlayer *player, string message )
- {
- // send a private message to one player - it'll be marked [Private] in Warcraft 3
-
- if( player )
- {
- if( !m_GameLoading && !m_GameLoaded )
- {
- if( message.size( ) > 254 )
- message = message.substr( 0, 254 );
-
- Send( player, m_Protocol->SEND_W3GS_CHAT_FROM_HOST( fromPID, UTIL_CreateByteArray( player->GetPID( ) ), 16, BYTEARRAY( ), message ) );
- }
- else
- {
- unsigned char ExtraFlags[] = { 3, 0, 0, 0 };
-
- // based on my limited testing it seems that the extra flags' first byte contains 3 plus the recipient's colour to denote a private message
-
- unsigned char SID = GetSIDFromPID( player->GetPID( ) );
-
- if( SID < m_Slots.size( ) )
- ExtraFlags[0] = 3 + m_Slots[SID].GetColour( );
-
- if( message.size( ) > 127 )
- message = message.substr( 0, 127 );
-
- Send( player, m_Protocol->SEND_W3GS_CHAT_FROM_HOST( fromPID, UTIL_CreateByteArray( player->GetPID( ) ), 32, UTIL_CreateByteArray( ExtraFlags, 4 ), message ) );
- }
- }
- }
-
- void CBaseGame :: SendChat( unsigned char fromPID, unsigned char toPID, string message )
- {
- SendChat( fromPID, GetPlayerFromPID( toPID ), message );
- }
-
- void CBaseGame :: SendChat( CGamePlayer *player, string message )
- {
- SendChat( GetHostPID( ), player, message );
- }
-
- void CBaseGame :: SendChat( unsigned char toPID, string message )
- {
- SendChat( GetHostPID( ), toPID, message );
- }
-
- void CBaseGame :: SendAllChat( unsigned char fromPID, string message )
- {
- // bot command
-
- if( !message.empty( ) && message[0] == m_GHost->m_CommandTrigger )
- {
- // extract the command trigger, the command, and the payload
- // e.g. "!say hello world" -> command: "say", payload: "hello world"
-
- string Command;
- string Payload;
- string :: size_type PayloadStart = message.find( " " );
-
- if( PayloadStart != string :: npos )
- {
- Command = message.substr( 1, PayloadStart - 1 );
- Payload = message.substr( PayloadStart + 1 );
- }
- else
- Command = message.substr( 1 );
-
- transform( Command.begin( ), Command.end( ), Command.begin( ), (int(*)(int))tolower );
-
- BYTEARRAY temp;
-
- CGamePlayer *Player = new CGamePlayer(0, 0, 0, fromPID, m_CreatorServer, m_CreatorName, temp, false);
- Player->SetSpoofed(true);
- Player->SetSpoofedRealm(m_CreatorServer);
-
- EventPlayerBotCommand( Player, Command, Payload );
-
- delete Player;
-
- return; // Don't show the message.
- }
-
- forward( new CFwdData( FWD_GAME_CHAT, "ADMIN:\3", 1, m_GameID ) );
- forward( new CFwdData( FWD_GAME_CHAT, message, 0, m_GameID ) );
-
- // send a public message to all players - it'll be marked [All] in Warcraft 3
-
- if( GetNumHumanPlayers( ) > 0 )
- {
- CONSOLE_Print( "[GAME: " + m_GameName + "] [Local]: " + message );
-
- if( !m_GameLoading && !m_GameLoaded )
- {
- if( message.size( ) > 254 )
- message = message.substr( 0, 254 );
-
- SendAll( m_Protocol->SEND_W3GS_CHAT_FROM_HOST( fromPID, GetPIDs( ), 16, BYTEARRAY( ), message ) );
- }
- else
- {
- if( message.size( ) > 127 )
- message = message.substr( 0, 127 );
-
- SendAll( m_Protocol->SEND_W3GS_CHAT_FROM_HOST( fromPID, GetPIDs( ), 32, UTIL_CreateByteArray( (uint32_t)0, false ), message ) );
-
- if( m_Replay )
- m_Replay->AddChatMessage( fromPID, 32, 0, message );
- }
- }
- }
-
- void CBaseGame :: SendAllChat( string message )
- {
- SendAllChat( GetHostPID( ), message );
- }
-
- void CBaseGame :: SendLocalAdminChat( string message )
- {
- if( !m_LocalAdminMessages )
- return;
-
- // send a message to LAN/local players who are admins
- // at the time of this writing it is only possible for the game owner to meet this criteria because being an admin requires spoof checking
- // this is mainly used for relaying battle.net whispers, chat messages, and emotes to these players
-
- for( vector<CGamePlayer *> :: iterator i = m_Players.begin( ); i != m_Players.end( ); ++i )
- {
- if( (*i)->GetSpoofed( ) && IsOwner( (*i)->GetName( ) ) && ( UTIL_IsLanIP( (*i)->GetExternalIP( ) ) || UTIL_IsLocalIP( (*i)->GetExternalIP( ), m_GHost->m_LocalAddresses ) ) )
- {
- if( m_VirtualHostPID != 255 )
- SendChat( m_VirtualHostPID, *i, message );
- else
- {
- // make the chat message originate from the recipient since it's not going to be logged to the replay
-
- SendChat( (*i)->GetPID( ), *i, message );
- }
- }
- }
- }
-
- void CBaseGame :: SendAllSlotInfo( )
- {
- if( !m_GameLoading && !m_GameLoaded )
- {
- SendAll( m_Protocol->SEND_W3GS_SLOTINFO( m_Slots, m_RandomSeed, m_Map->GetMapLayoutStyle( ), m_Map->GetMapNumPlayers( ) ) );
- m_SlotInfoChanged = false;
- }
- }
-
- void CBaseGame :: SendVirtualHostPlayerInfo( CGamePlayer *player )
- {
- if( m_Virt…