1 /* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
2 
3 #ifndef _GAME_SETUP_H
4 #define _GAME_SETUP_H
5 
6 #include <string>
7 #include <map>
8 #include <vector>
9 #include <set>
10 
11 #include "Players/PlayerBase.h"
12 #include "Sim/Misc/TeamBase.h"
13 #include "Sim/Misc/AllyTeam.h"
14 #include "ExternalAI/SkirmishAIData.h"
15 #include "System/creg/creg_cond.h"
16 
17 class TdfParser;
18 
19 class CGameSetup
20 {
21 	CR_DECLARE_STRUCT(CGameSetup)
22 
23 public:
24 	CGameSetup();
25 
26 	static void LoadSavedScript(const std::string& file, const std::string& script);
27 
28 	// these return dummy containers if the global gameSetup instance is NULL
29 	static const std::map<std::string, std::string>& GetMapOptions();
30 	static const std::map<std::string, std::string>& GetModOptions();
31 	static const std::vector<PlayerBase>& GetPlayerStartingData();
32 	static const std::vector<TeamBase>& GetTeamStartingData();
33 	static const std::vector<AllyTeam>& GetAllyStartingData();
34 
35 	void PostLoad();
36 	bool Init(const std::string& script);
37 	/**
38 	 * @brief Load startpositions from map/script
39 	 * @pre numTeams and startPosType initialized
40 	 * @post readyTeams, teamStartNum and team start positions initialized
41 	 *
42 	 * Unlike the other functions, this is not called on Init(),
43 	 * instead we wait for CPreGame to call this. The reason is that the map
44 	 * is not known before CPreGame recieves the gamedata from the server.
45 	 */
46 	void LoadStartPositions(bool withoutMap = false);
47 
GetRestrictedUnitLimit(const std::string & name,int defLimit)48 	int GetRestrictedUnitLimit(const std::string& name, int defLimit) const {
49 		const std::map<std::string, int>::const_iterator it = restrictedUnits.find(name);
50 		if (it == restrictedUnits.end())
51 			return defLimit;
52 		return (it->second);
53 	}
54 
GetMapOptionsCont()55 	const std::map<std::string, std::string>& GetMapOptionsCont() const { return mapOptions; }
GetModOptionsCont()56 	const std::map<std::string, std::string>& GetModOptionsCont() const { return modOptions; }
GetPlayerStartingDataCont()57 	const std::vector<PlayerBase>& GetPlayerStartingDataCont() const { return playerStartingData; }
GetTeamStartingDataCont()58 	const std::vector<TeamBase>& GetTeamStartingDataCont() const { return teamStartingData; }
GetAllyStartingDataCont()59 	const std::vector<AllyTeam>& GetAllyStartingDataCont() const { return allyStartingData; }
GetAIStartingDataCont()60 	const std::vector<SkirmishAIData>& GetAIStartingDataCont() const { return skirmishAIStartingData; }
GetMutatorsCont()61 	const std::vector<std::string>& GetMutatorsCont() const { return mutatorsList; }
62 
63 	const std::string MapFile() const;
64 
65 	enum StartPosType {
66 		StartPos_Fixed            = 0,
67 		StartPos_Random           = 1,
68 		StartPos_ChooseInGame     = 2,
69 		StartPos_ChooseBeforeGame = 3,
70 		StartPos_Last             = 3  // last entry in enum (for user input check)
71 	};
72 
73 	bool fixedAllies;
74 	bool useLuaGaia;
75 	bool noHelperAIs;
76 
77 	bool ghostedBuildings;
78 	bool disableMapDamage;
79 
80 	/** if true, this is a non-network game (one local client, eg. when watching a demo) */
81 	bool onlyLocal;
82 	bool hostDemo;
83 
84 	unsigned int mapHash;
85 	unsigned int modHash;
86 	unsigned int mapSeed;
87 
88 	/**
89 	 * The number of seconds till the game starts,
90 	 * counting from the moment when all players are connected and ready.
91 	 * Default: 4 (seconds)
92 	 */
93 	unsigned int gameStartDelay;
94 
95 	int numDemoPlayers;
96 	int maxUnitsPerTeam;
97 
98 	float maxSpeed;
99 	float minSpeed;
100 
101 	StartPosType startPosType;
102 
103 	std::string mapName;
104 	std::string modName;
105 	std::string gameID;
106 
107 	std::string gameSetupText;
108 	std::string demoName;
109 	std::string saveName;
110 
111 private:
112 	/**
113 	 * @brief Load startpositions from map
114 	 * @pre mapName, numTeams, teamStartNum initialized and the map loaded (LoadMap())
115 	 */
116 	void LoadStartPositionsFromMap();
117 
118 	void LoadMutators(const TdfParser& file, std::vector<std::string>& mutatorsList);
119 	/**
120 	 * @brief Load unit restrictions
121 	 * @post restrictedUnits initialized
122 	 */
123 	void LoadUnitRestrictions(const TdfParser& file);
124 	/**
125 	 * @brief Load players and remove gaps in the player numbering.
126 	 * @pre numPlayers initialized
127 	 * @post players loaded, numDemoPlayers initialized
128 	 */
129 	void LoadPlayers(const TdfParser& file, std::set<std::string>& nameList);
130 	/**
131 	 * @brief Load LUA and Skirmish AIs.
132 	 */
133 	void LoadSkirmishAIs(const TdfParser& file, std::set<std::string>& nameList);
134 	/**
135 	 * @brief Load teams and remove gaps in the team numbering.
136 	 * @pre numTeams, hostDemo initialized
137 	 * @post teams loaded
138 	 */
139 	void LoadTeams(const TdfParser& file);
140 	/**
141 	 * @brief Load allyteams and remove gaps in the allyteam numbering.
142 	 * @pre numAllyTeams initialized
143 	 * @post allyteams loaded, alliances initialised (no remapping needed here)
144 	 */
145 	void LoadAllyTeams(const TdfParser& file);
146 
147 	/** @brief Update all player indices to refer to the right player. */
148 	void RemapPlayers();
149 	/** @brief Update all team indices to refer to the right team. */
150 	void RemapTeams();
151 	/** @brief Update all allyteam indices to refer to the right allyteams. (except allies) */
152 	void RemapAllyteams();
153 
154 private:
155 	std::map<int, int> playerRemap;
156 	std::map<int, int> teamRemap;
157 	std::map<int, int> allyteamRemap;
158 
159 	std::vector<PlayerBase> playerStartingData;
160 	std::vector<TeamBase> teamStartingData;
161 	std::vector<AllyTeam> allyStartingData;
162 	std::vector<SkirmishAIData> skirmishAIStartingData;
163 	std::vector<std::string> mutatorsList;
164 
165 	std::map<int, const SkirmishAIData*> team_skirmishAI;
166 	std::map<std::string, int> restrictedUnits;
167 
168 	std::map<std::string, std::string> mapOptions;
169 	std::map<std::string, std::string> modOptions;
170 };
171 
172 extern CGameSetup* gameSetup;
173 
174 #endif // _GAME_SETUP_H
175