Skip to content

Commit

Permalink
fix #5463
Browse files Browse the repository at this point in the history
  • Loading branch information
rtri committed Feb 22, 2017
1 parent f9c2d05 commit b3f2c35
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 61 deletions.
56 changes: 27 additions & 29 deletions rts/Game/Players/Player.cpp
Expand Up @@ -62,18 +62,18 @@ void CPlayer::SetControlledTeams()
return;
}

if (!spectator) {
// my team
// my team
if (!spectator)
controlledTeams.insert(team);
}

// AI teams
const CSkirmishAIHandler::id_ai_t aiIds = skirmishAIHandler.GetAllSkirmishAIs();
for (CSkirmishAIHandler::id_ai_t::const_iterator ai = aiIds.begin(); ai != aiIds.end(); ++ai) {
const bool isHostedByUs = (ai->second.hostPlayer == playerNum);
if (isHostedByUs) {
controlledTeams.insert(ai->second.team);
}
for (const auto& p: skirmishAIHandler.GetAllSkirmishAIs()) {
const SkirmishAIData& sad = p.second;

if (sad.hostPlayer != playerNum)
continue;

controlledTeams.insert(sad.team);
}
}

Expand All @@ -82,18 +82,19 @@ void CPlayer::UpdateControlledTeams()
{
for (int p = 0; p < playerHandler->ActivePlayers(); p++) {
CPlayer* player = playerHandler->Player(p);
if (player) {
player->SetControlledTeams();
}

if (player == nullptr)
continue;

player->SetControlledTeams();
}
}


void CPlayer::StartSpectating()
{
if (spectator) {
if (spectator)
return;
}

spectator = true;

Expand All @@ -106,7 +107,8 @@ void CPlayer::StartSpectating()
//FIXME use eventHandler?
CLuaUI::UpdateTeams();
selectedUnitsHandler.ClearSelected();
if(readMap != NULL) readMap->BecomeSpectator();
if (readMap != nullptr)
readMap->BecomeSpectator();
unitTracker.Disable();
}

Expand Down Expand Up @@ -139,9 +141,8 @@ void CPlayer::JoinTeam(int newTeam)

void CPlayer::GameFrame(int frameNum)
{
if (!active || (fpsController.GetControllee() == NULL)) {
if (!active || (fpsController.GetControllee() == nullptr))
return;
}

fpsController.Update();
}
Expand All @@ -151,27 +152,25 @@ void CPlayer::GameFrame(int frameNum)
void CPlayer::StartControllingUnit()
{
CUnit* curControlleeUnit = fpsController.GetControllee();
CUnit* newControlleeUnit = NULL;
CUnit* newControlleeUnit = nullptr;

if (curControlleeUnit != NULL) {
if (curControlleeUnit != nullptr) {
// player released control
StopControllingUnit();
} else {
// player took control
const std::vector<int>& ourSelectedUnits = selectedUnitsHandler.netSelected[this->playerNum];

if (ourSelectedUnits.empty()) {
if (ourSelectedUnits.empty())
return;
}

// pick the first unit we have selected
newControlleeUnit = unitHandler->GetUnit(ourSelectedUnits[0]);

if (newControlleeUnit == NULL || newControlleeUnit->weapons.empty()) {
if (newControlleeUnit == nullptr || newControlleeUnit->weapons.empty())
return;
}

if (newControlleeUnit->fpsControlPlayer != NULL) {
if (newControlleeUnit->fpsControlPlayer != nullptr) {
if (this->playerNum == gu->myPlayerNum) {
LOG_L(L_WARNING,
"player %d (%s) is already controlling unit %d",
Expand Down Expand Up @@ -208,18 +207,17 @@ void CPlayer::StartControllingUnit()

void CPlayer::StopControllingUnit()
{
if (fpsController.GetControllee() == NULL || mouse == NULL) {
if (fpsController.GetControllee() == nullptr || mouse == nullptr)
return;
}

CPlayer* that = gu->GetMyPlayer();
CUnit* thatUnit = that->fpsController.GetControllee();
CUnit* thisUnit = this->fpsController.GetControllee();

// note: probably better to issue CMD_STOP via thisUnit->commandAI
thisUnit->AttackUnit(NULL, true, false, true);
thisUnit->fpsControlPlayer = NULL;
fpsController.SetControlleeUnit(NULL);
thisUnit->AttackUnit(nullptr, true, false, true);
thisUnit->fpsControlPlayer = nullptr;
fpsController.SetControlleeUnit(nullptr);
selectedUnitsHandler.ClearNetSelect(this->playerNum);

if (thatUnit == thisUnit) {
Expand Down
4 changes: 2 additions & 2 deletions rts/Game/Players/Player.h
Expand Up @@ -7,9 +7,9 @@
#include "PlayerStatistics.h"
#include "Game/FPSUnitController.h"
#include "System/creg/creg_cond.h"
#include "System/UnorderedSet.hpp"

#include <string>
#include <set>

class CPlayer;
class CUnit;
Expand Down Expand Up @@ -65,7 +65,7 @@ class CPlayer : public PlayerBase
FPSUnitController fpsController;

private:
std::set<int> controlledTeams;
spring::unordered_set<int> controlledTeams;
};

#endif /* PLAYER_H */
51 changes: 32 additions & 19 deletions rts/Game/PreGame.cpp
Expand Up @@ -17,24 +17,25 @@
#include "LoadScreen.h"
#include "Game/Players/Player.h"
#include "Game/Players/PlayerHandler.h"
#include "Net/GameServer.h"
#include "System/TimeProfiler.h"
#include "UI/InfoConsole.h"
#include "ExternalAI/SkirmishAIHandler.h"
#include "Map/Generation/SimpleMapGenerator.h"
#include "Menu/LuaMenuController.h"
#include "Net/GameServer.h"
#include "Net/Protocol/NetProtocol.h"

#include "aGui/Gui.h"
#include "ExternalAI/SkirmishAIHandler.h"

#include "Rendering/Fonts/glFont.h"
#include "Sim/Misc/GlobalSynced.h"
#include "Sim/Misc/GlobalConstants.h"
#include "Sim/Misc/TeamHandler.h"
#include "System/Config/ConfigHandler.h"
#include "System/Exceptions.h"
#include "Net/Protocol/NetProtocol.h"
#include "System/TimeProfiler.h"
#include "System/TdfParser.h"
#include "System/Input/KeyInput.h"
#include "System/FileSystem/ArchiveScanner.h"
//// #include "System/FileSystem/FileHandler.h"
#include "System/FileSystem/FileSystem.h"
#include "System/FileSystem/VFSHandler.h"
#include "System/LoadSave/DemoRecorder.h"
Expand Down Expand Up @@ -75,11 +76,11 @@ CPreGame::CPreGame(std::shared_ptr<ClientSetup> setup)

if (!clientSetup->isHost) {
//don't allow luasocket to connect to the host
LOG("[PreGame] connecting to: %s:%i", clientSetup->hostIP.c_str(), clientSetup->hostPort);
LOG("[%s] connecting to: %s:%i", __func__, clientSetup->hostIP.c_str(), clientSetup->hostPort);
luaSocketRestrictions->addRule(CLuaSocketRestrictions::UDP_CONNECT, clientSetup->hostIP, clientSetup->hostPort, false);
clientNet->InitClient(clientSetup->hostIP.c_str(), clientSetup->hostPort, clientSetup->myPlayerName, clientSetup->myPasswd, SpringVersion::GetFull());
} else {
LOG("[PreGame] hosting on: %s:%i", clientSetup->hostIP.c_str(), clientSetup->hostPort);
LOG("[%s] hosting on: %s:%i", __func__, clientSetup->hostIP.c_str(), clientSetup->hostPort);
clientNet->InitLocalClient();
}
}
Expand Down Expand Up @@ -126,7 +127,7 @@ int CPreGame::KeyPressed(int k, bool isRepeat)
LOG("[%s] user exited", __func__);
gu->globalQuit = true;
} else {
LOG("Use shift-esc to quit");
LOG("[%s] Use shift-esc to quit", __func__);
}
}
return 0;
Expand Down Expand Up @@ -176,7 +177,7 @@ bool CPreGame::Update()

void CPreGame::AddGameSetupArchivesToVFS(const CGameSetup* setup, bool mapOnly)
{
LOG("[%s] using map: %s", __func__, setup->mapName.c_str());
LOG("[PreGame::%s] using map: %s", __func__, setup->mapName.c_str());
// Load Map archive
vfsHandler->AddArchiveWithDeps(setup->mapName, false);

Expand All @@ -185,15 +186,15 @@ void CPreGame::AddGameSetupArchivesToVFS(const CGameSetup* setup, bool mapOnly)

// Load Mutators (if any)
for (const std::string& mut: setup->GetMutatorsCont()) {
LOG("[%s] using mutator: %s", __func__, mut.c_str());
LOG("[PreGame::%s] using mutator: %s", __func__, mut.c_str());
vfsHandler->AddArchiveWithDeps(mut, false);
}

// Load Game archive
vfsHandler->AddArchiveWithDeps(setup->modName, false);

modArchive = archiveScanner->ArchiveFromName(setup->modName);
LOG("[%s] using game: %s (archive: %s)", __func__, setup->modName.c_str(), modArchive.c_str());
LOG("[PreGame::%s] using game: %s (archive: %s)", __func__, setup->modName.c_str(), modArchive.c_str());
}

void CPreGame::StartServer(const std::string& setupscript)
Expand Down Expand Up @@ -231,7 +232,7 @@ void CPreGame::StartServer(const std::string& setupscript)
const auto mapChecksum = archiveScanner->GetArchiveCompleteChecksum(mapArchive);
startGameData->SetModChecksum(modChecksum);
startGameData->SetMapChecksum(mapChecksum);
LOG("Checksums: game=0x%X map=0x%X", modChecksum, mapChecksum);
LOG("[PreGame::%s] checksums: game=0x%X map=0x%X", __func__, modChecksum, mapChecksum);

good_fpu_control_registers("before CGameServer creation");
startGameData->SetSetupText(startGameSetup->setupText);
Expand Down Expand Up @@ -271,11 +272,20 @@ void CPreGame::UpdateClientNet()
try {
netcode::UnpackPacket pckt(packet, 3);
std::string message;

pckt >> message;
LOG("%s", message.c_str());
handleerror(nullptr, "Remote requested quit: " + message, "Quit message", MBF_OK | MBF_EXCL);

// (re)activate LuaMenu if user failed to connect
if (luaMenuController->Valid() && luaMenuController->Activate(message)) {
delete this;
return;
}

// force exit to system if no menu
LOG("[PreGame::%s] server requested quit or rejected connection (reason \"%s\")", __func__, message.c_str());
handleerror(nullptr, "server requested quit or rejected connection: " + message, "Quit message", MBF_OK | MBF_EXCL);
} catch (const netcode::UnpackPacketException& ex) {
LOG_L(L_ERROR, "Got invalid QuitMessage: %s", ex.what());
LOG_L(L_ERROR, "[PreGame::%s] invalid NETMSG_{QUIT,REJECT_CONNECT} packet (exception \"%s\")", __func__, ex.what());
}
break;
}
Expand All @@ -286,8 +296,12 @@ void CPreGame::UpdateClientNet()
// gamedata), otherwise skip to gamedata
try {
netcode::UnpackPacket pckt(packet, 3);
unsigned char spectator, team, playerNum;
std::string name;

unsigned char playerNum;
unsigned char spectator;
unsigned char team;

// since the >> operator uses dest size to extract data from
// the packet, we need to use temp variables of the same
// size of the packet, before converting to dest variable
Expand Down Expand Up @@ -341,7 +355,6 @@ void CPreGame::UpdateClientNet()

CLoadScreen::CreateInstance(gameSetup->MapFile(), modArchive, savefile);

pregame = nullptr;
delete this;
return;
}
Expand Down Expand Up @@ -493,9 +506,9 @@ void CPreGame::GameDataReceived(std::shared_ptr<const netcode::RawPacket> packet
LOG_L(L_WARNING, "Incompatible game-checksum: %s", ex.what());
}

if (clientSetup->isHost && !gameSetup->recordDemo) { //script.txt allows to disable demo file recording (host only, used for menu)
// script.txt allows to disable demo file recording (host only, used for menu)
if (clientSetup->isHost && !gameSetup->recordDemo)
wantDemo = false;
}

if (clientNet != nullptr && wantDemo) {
assert(clientNet->GetDemoRecorder() == nullptr);
Expand Down
13 changes: 10 additions & 3 deletions rts/Lua/LuaMenu.cpp
Expand Up @@ -320,24 +320,29 @@ bool CLuaMenu::LoadUnsyncedReadFunctions(lua_State* L)
/******************************************************************************/


void CLuaMenu::ActivateMenu()
void CLuaMenu::ActivateMenu(const std::string& msg)
{
LUA_CALL_IN_CHECK(L);
luaL_checkstack(L, 2, __func__);
luaL_checkstack(L, 3, __func__);

static const LuaHashString cmdStr(__func__);

if (!cmdStr.GetGlobalFunc(L))
return; // the call is not defined

// call the routine
RunCallIn(L, cmdStr, 0, 0);
lua_pushsstring(L, msg);
RunCallIn(L, cmdStr, 1, 0);
}


void CLuaMenu::ActivateGame()
{
LUA_CALL_IN_CHECK(L);
luaL_checkstack(L, 2, __func__);

static const LuaHashString cmdStr(__func__);

if (!cmdStr.GetGlobalFunc(L))
return; // the call is not defined

Expand All @@ -350,7 +355,9 @@ bool CLuaMenu::AllowDraw()
{
LUA_CALL_IN_CHECK(L);
luaL_checkstack(L, 2, __func__);

static const LuaHashString cmdStr(__func__);

if (!cmdStr.GetGlobalFunc(L))
return true; // the call is not defined, allow draw

Expand Down
2 changes: 1 addition & 1 deletion rts/Lua/LuaMenu.h
Expand Up @@ -16,7 +16,7 @@ class CLuaMenu : public CLuaHandle
static bool FreeHandler();

// callin called when LuaMenu is active with no game
void ActivateMenu();
void ActivateMenu(const std::string& msg);

// callin called when LuaMenu is active with a game
void ActivateGame();
Expand Down
5 changes: 3 additions & 2 deletions rts/Menu/LuaMenuController.cpp
Expand Up @@ -52,7 +52,7 @@ void CLuaMenuController::Reset()
infoConsole = new CInfoConsole();
}

bool CLuaMenuController::Activate()
bool CLuaMenuController::Activate(const std::string& msg)
{
LOG("[LuaMenuController::%s] luaMenu=%p", __func__, luaMenu);

Expand All @@ -62,8 +62,9 @@ bool CLuaMenuController::Activate()

assert(Valid());
activeController = luaMenuController;

mouse->ShowMouse();
luaMenu->ActivateMenu();
luaMenu->ActivateMenu(msg);
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion rts/Menu/LuaMenuController.h
Expand Up @@ -14,7 +14,7 @@ class CLuaMenuController : public CGameController

// Reloads the archives but not LuaMenu
void Reset();
bool Activate();
bool Activate(const std::string& msg);

int KeyReleased(int k) override;
int KeyPressed(int k, bool isRepeat) override;
Expand Down
5 changes: 2 additions & 3 deletions rts/Net/NetCommands.cpp
Expand Up @@ -235,10 +235,8 @@ void CGame::ClientReadNet()
// get netpacket from the queue
std::shared_ptr<const netcode::RawPacket> packet = clientNet->GetData(gs->frameNum);

if (!packet) {
// LOG_SL(LOG_SECTION_NET, L_DEBUG, "Run out of netpackets!");
if (!packet)
break;
}

lastReceivedNetPacketTime = spring_gettime();

Expand All @@ -251,6 +249,7 @@ void CGame::ClientReadNet()
try {
netcode::UnpackPacket pckt(packet, 3);
std::string message;

pckt >> message;

LOG("%s", message.c_str());
Expand Down

0 comments on commit b3f2c35

Please sign in to comment.