1 /***************************************************************************
2  *      Mechanized Assault and Exploration Reloaded Projectfile            *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
18  ***************************************************************************/
19 
20 #include "game/startup/network/client/networkclientgamereconnection.h"
21 #include "ui/graphical/menu/windows/windowgamesettings/gamesettings.h"
22 #include "ui/graphical/application.h"
23 #include "game/logic/client.h"
24 #include "game/logic/server.h"
25 #include "game/data/player/player.h"
26 #include "game/logic/clientevents.h"
27 
28 //------------------------------------------------------------------------------
cNetworkClientGameReconnection()29 cNetworkClientGameReconnection::cNetworkClientGameReconnection()
30 {}
31 
32 //------------------------------------------------------------------------------
start(cApplication & application)33 void cNetworkClientGameReconnection::start (cApplication& application)
34 {
35 	localClient = std::make_unique<cClient> (nullptr, network);
36 
37 	localClient->setPlayers (players, localPlayerIndex);
38 	localClient->setMap (staticMap);
39 
40 	sendReconnectionSuccess (*localClient);
41 
42 	gameGuiController = std::make_unique<cGameGuiController> (application, staticMap);
43 
44 	gameGuiController->setSingleClient (localClient);
45 
46 	gameGuiController->start();
47 
48 	using namespace std::placeholders;
49 	signalConnectionManager.connect (gameGuiController->triggeredSave, std::bind (&cNetworkClientGameReconnection::save, this, _1, _2));
50 
51 	terminate = false;
52 
53 	application.addRunnable (shared_from_this());
54 
55 	signalConnectionManager.connect (gameGuiController->terminated, [&]() { terminate = true; });
56 }
57 
58 //------------------------------------------------------------------------------
setPlayers(std::vector<cPlayerBasicData> players_,const cPlayerBasicData & localPlayer)59 void cNetworkClientGameReconnection::setPlayers (std::vector<cPlayerBasicData> players_, const cPlayerBasicData& localPlayer)
60 {
61 	players = players_;
62 	auto localPlayerIter = std::find_if (players.begin(), players.end(), [&] (const cPlayerBasicData & player) { return player.getNr() == localPlayer.getNr(); });
63 	assert (localPlayerIter != players.end());
64 	localPlayerIndex = localPlayerIter - players.begin();
65 }
66 
67 //------------------------------------------------------------------------------
setStaticMap(std::shared_ptr<cStaticMap> staticMap_)68 void cNetworkClientGameReconnection::setStaticMap (std::shared_ptr<cStaticMap> staticMap_)
69 {
70 	staticMap = staticMap_;
71 }
72 
73 //------------------------------------------------------------------------------
getStaticMap()74 const std::shared_ptr<cStaticMap>& cNetworkClientGameReconnection::getStaticMap()
75 {
76 	return staticMap;
77 }
78 
79 //------------------------------------------------------------------------------
getPlayers()80 const std::vector<cPlayerBasicData>& cNetworkClientGameReconnection::getPlayers()
81 {
82 	return players;
83 }
84 
85 //------------------------------------------------------------------------------
getLocalPlayer()86 const cPlayerBasicData& cNetworkClientGameReconnection::getLocalPlayer()
87 {
88 	return players[localPlayerIndex];
89 }
90