1 /*
2  *  This file is part of Dune Legacy.
3  *
4  *  Dune Legacy 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  *  Dune Legacy 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 Dune Legacy.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef GAMEINITINFOCLASS_H
19 #define GAMEINITINFOCLASS_H
20 
21 #include "Definitions.h"
22 #include "DataTypes.h"
23 #include <misc/InputStream.h>
24 #include <misc/OutputStream.h>
25 
26 #include <string>
27 
28 
29 class GameInitSettings
30 {
31 public:
32 
33     class PlayerInfo {
34     public:
PlayerInfo(const std::string & newPlayerName,const std::string & newPlayerClass)35         PlayerInfo(const std::string& newPlayerName, const std::string& newPlayerClass)
36          : playerName(newPlayerName), playerClass(newPlayerClass) {
37         }
38 
PlayerInfo(InputStream & stream)39         explicit PlayerInfo(InputStream& stream) {
40             playerName = stream.readString();
41             playerClass = stream.readString();
42         }
43 
save(OutputStream & stream)44         void save(OutputStream& stream) const {
45             stream.writeString(playerName);
46             stream.writeString(playerClass);
47         }
48 
49         std::string playerName;
50         std::string playerClass;
51     };
52 
53     class HouseInfo {
54     public:
HouseInfo(HOUSETYPE newHouseID,int newTeam)55         HouseInfo(HOUSETYPE newHouseID, int newTeam)
56          : houseID(newHouseID), team(newTeam) {
57         }
58 
HouseInfo(InputStream & stream)59         explicit HouseInfo(InputStream& stream) {
60             houseID = (HOUSETYPE) stream.readSint32();
61             team = stream.readSint32();
62 
63             Uint32 numPlayerInfo = stream.readUint32();
64             for(Uint32 i=0;i<numPlayerInfo;i++) {
65                 playerInfoList.push_back(PlayerInfo(stream));
66             }
67         }
68 
save(OutputStream & stream)69         void save(OutputStream& stream) const {
70             stream.writeSint32(houseID);
71             stream.writeSint32(team);
72 
73             stream.writeUint32(playerInfoList.size());
74             for(const PlayerInfo& playerInfo : playerInfoList) {
75                 playerInfo.save(stream);
76             }
77         }
78 
addPlayerInfo(const PlayerInfo & newPlayerInfo)79         inline void addPlayerInfo(const PlayerInfo& newPlayerInfo) { playerInfoList.push_back(newPlayerInfo); };
80 
81         typedef std::vector<PlayerInfo> PlayerInfoList;
82 
83         HOUSETYPE       houseID;
84         int             team;
85         PlayerInfoList  playerInfoList;
86     };
87 
88     typedef std::vector<HouseInfo> HouseInfoList;
89 
90 
91     /**
92         Default constructor.
93         The constructed GameInitSettings object is empty
94     */
95     GameInitSettings();
96 
97     /**
98         Constructor for specifying the start of a campaign
99         \param  newHouseID          the house to play the campaign with
100         \param  gameOptions         the options for this game
101     */
102     GameInitSettings(HOUSETYPE newHouseID, const SettingsClass::GameOptionsClass& gameOptions);
103 
104     /**
105         Constructor for continuing a campaign at the specified mission
106         \param  prevGameInitInfoClass       the init settings of the previous mission in the campaign
107         \param  nextMission                 the number of the mission to continue the campaign
108         \param  alreadyPlayedRegions        a bit set describing which regions were already played (used to forbid playing these again)
109         \param  alreadyShownTutorialHints   contains flags for each tutorial hint (see enum HumanPlayer::TutorialHint)
110     */
111     GameInitSettings(const GameInitSettings& prevGameInitInfoClass, int nextMission, Uint32 alreadyPlayedRegions, Uint32 alreadyShownTutorialHints);
112 
113     /**
114         Constructor for specifying the start of a skirmish mission in the campaign
115         \param  newHouseID          the house specifying from which campaign the mission is from
116         \param  newMission          the number of the mission (1 - 22)
117         \param  gameOptions         the options for this game
118     */
119     GameInitSettings(HOUSETYPE newHouseID, int newMission, const SettingsClass::GameOptionsClass& gameOptions);
120 
121     /**
122         Constructor for specifying the start of a custom map
123         \param  mapfile             the name of the map (without extension)
124         \param  filedata            the data of the map file
125         \param  multiplePlayersPerHouse     allow multiple players per house
126         \param  gameOptions         the options for this game
127     */
128     GameInitSettings(const std::string& mapfile, const std::string& filedata, bool multiplePlayersPerHouse, const SettingsClass::GameOptionsClass& gameOptions);
129 
130     /**
131         Constructor for specifying the start of a multiplayer custom map
132         \param  mapfile             the name of the map (without extension)
133         \param  filedata            the data of the map file
134         \param  serverName          the name of the game server
135         \param  multiplePlayersPerHouse     allow multiple players per house
136         \param  gameOptions         the options for this game
137     */
138     GameInitSettings(const std::string& mapfile, const std::string& filedata, const std::string& serverName, bool multiplePlayersPerHouse, const SettingsClass::GameOptionsClass& gameOptions);
139 
140     /**
141         Constructor for specifying the loading of a savegame. If the given filename contains no valid savegame
142         an exception is thrown.
143         \param  savegame    the name of the savegame
144     */
145     explicit GameInitSettings(const std::string& savegame);
146 
147     /**
148         Constructor for specifying the loading of a network savegame. If the given filename contains no valid savegame
149         an exception is thrown.
150         \param  savegame    the name of the savegame
151         \param  filedata    the data of the savegame file
152         \param  serverName  the name of the game server
153     */
154     GameInitSettings(const std::string& savegame, const std::string& filedata, const std::string& serverName);
155 
156     /**
157         Load the game init info from a stream
158         \param  stream  the stream to load from
159     */
160     explicit GameInitSettings(InputStream& stream);
161 
162     ~GameInitSettings();
163 
164     void save(OutputStream& stream) const;
165 
getGameType()166     inline GameType getGameType() const { return gameType; };
getHouseID()167     inline HOUSETYPE getHouseID() const { return houseID; };
getMission()168     inline int getMission() const { return mission; };
getAlreadyPlayedRegions()169     inline Uint32 getAlreadyPlayedRegions() const { return alreadyPlayedRegions; };
getAlreadyShownTutorialHints()170     inline Uint32 getAlreadyShownTutorialHints() const { return alreadyShownTutorialHints; };
getFilename()171     inline const std::string& getFilename() const { return filename; };
getFiledata()172     inline const std::string& getFiledata() const { return filedata; };
getServername()173     inline const std::string& getServername() const { return servername; };
getRandomSeed()174     inline Uint32 getRandomSeed() const { return randomSeed; };
175 
isMultiplePlayersPerHouse()176     inline bool isMultiplePlayersPerHouse() const { return multiplePlayersPerHouse; };
setMultiplePlayersPerHouse(bool multiplePlayersPerHouse)177     inline void setMultiplePlayersPerHouse(bool multiplePlayersPerHouse) { this->multiplePlayersPerHouse = multiplePlayersPerHouse; };
getGameOptions()178     inline const SettingsClass::GameOptionsClass& getGameOptions() const { return gameOptions; };
setGameSpeed(int gameSpeed)179     inline void setGameSpeed(int gameSpeed) { gameOptions.gameSpeed = gameSpeed; };
180 
addHouseInfo(const HouseInfo & newHouseInfo)181     inline void addHouseInfo(const HouseInfo& newHouseInfo) { houseInfoList.push_back(newHouseInfo); };
clearHouseInfo()182     inline void clearHouseInfo() { houseInfoList.clear(); };
getHouseInfoList()183     inline const HouseInfoList& getHouseInfoList() const { return houseInfoList; };
184 
setHouseID(HOUSETYPE houseID)185     inline void setHouseID(HOUSETYPE houseID) { this->houseID = houseID; };
186 
187 private:
188     static std::string getScenarioFilename(HOUSETYPE newHouse, int mission);
189 
190     /**
191         This method checks if it is possible to load a savegame and if the magic number is correct. If there is an error an exception is thrown.
192         \param savegame the name of the file to check
193     */
194     static void checkSaveGame(const std::string& savegame);
195 
196 
197     /**
198         This method checks if it is possible to load a savegame and if the magic number is correct. If there is an error an exception is thrown.
199         \param stream the strean to read the data from
200     */
201     static void checkSaveGame(InputStream& stream);
202 
203 
204     GameType        gameType;
205 
206     HOUSETYPE       houseID;
207     int             mission;
208     Uint32          alreadyPlayedRegions;
209     Uint32          alreadyShownTutorialHints;
210 
211     std::string     filename;
212     std::string     filedata;
213     std::string     servername;
214 
215     Uint32          randomSeed;
216 
217     bool            multiplePlayersPerHouse;
218 
219     SettingsClass::GameOptionsClass gameOptions;
220 
221 
222     HouseInfoList   houseInfoList;
223 };
224 
225 #endif // GAMEINITINFOCLASS_H
226