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 #ifndef game_data_player_playerH
21 #define game_data_player_playerH
22 
23 #include <SDL.h>
24 #include <cassert>
25 #include <string>
26 #include <vector>
27 
28 #include "defines.h"
29 #include "game/data/base/base.h"
30 #include "main.h" // for sID
31 #include "game/data/units/unit.h" // sUnitLess
32 #include "game/logic/upgradecalculator.h"
33 #include "utility/position.h"
34 #include "utility/signal/signal.h"
35 #include "utility/flatset.h"
36 #include "game/data/player/playerbasicdata.h"
37 
38 class cBuilding;
39 class cHud;
40 class cMapField;
41 class cUnit;
42 class cVehicle;
43 class cPosition;
44 
45 struct sTurnstartReport;
46 struct sUnitData;
47 
48 class cSavedReport;
49 
50 typedef std::vector<int> PointsHistory;
51 
52 // the Player class //////////////////////////////
53 class cPlayer
54 {
55 	cPlayer (const cPlayer&) MAXR_DELETE_FUNCTION;
56 public:
57 	explicit cPlayer (const cPlayerBasicData& splayer);
58 	~cPlayer();
59 
getName()60 	const std::string& getName() const { return splayer.getName(); }
setName(const std::string & name)61 	void setName (const std::string& name) { splayer.setName (name); }
62 
getColor()63 	const cPlayerColor& getColor() const { return splayer.getColor(); }
setColor(cPlayerColor color)64 	void setColor (cPlayerColor color) { return splayer.setColor (std::move (color)); }
65 
getNr()66 	int getNr() const { return splayer.getNr(); }
67 
getSocketNum()68 	int getSocketNum() const { return splayer.getSocketIndex(); }
setSocketIndex(int index)69 	void setSocketIndex (int index) { splayer.setSocketIndex (index); }
70 
onSocketIndexDisconnected(unsigned int socketIndex)71 	void onSocketIndexDisconnected (unsigned int socketIndex) { splayer.onSocketIndexDisconnected (socketIndex); }
72 
setLocal()73 	void setLocal() { splayer.setLocal(); }
isLocal()74 	bool isLocal() const { return splayer.isLocal(); }
75 
76 	int getCredits() const;
77 	void setCredits (int credits);
78 
79 	/** Get the most modern version of a unit (including all his upgrades). */
80 	sUnitData* getUnitDataCurrentVersion (const sID& id);
81 	const sUnitData* getUnitDataCurrentVersion (const sID& id) const;
82 
setLandingPos(int x,int y)83 	void setLandingPos (int x, int y) { landingPosX = x; landingPosY = y; }
getLandingPosX()84 	int getLandingPosX() const { return landingPosX; }
getLandingPosY()85 	int getLandingPosY() const { return landingPosY; }
86 
87 	void initMaps (cMap& map);
88 	const cPosition& getMapSize() const;
89 	void doScan();
90 	void revealMap();
91 	void revealPosition (const cPosition& position);
92 	void revealResource();
getOffset(const cPosition & pos)93 	unsigned int getOffset (const cPosition& pos) const { return pos.x() + pos.y() * mapSize.x(); }
94 	bool canSeeAnyAreaUnder (const cUnit& unit) const;
95 	bool canSeeAt (const cPosition& position) const;
96 
97 	cVehicle& addNewVehicle (const cPosition& position, const sID& id, unsigned int uid);
98 	cBuilding& addNewBuilding (const cPosition& position, const sID& id, unsigned int uid);
99 
100 	void addUnit (std::shared_ptr<cVehicle> vehicle);
101 	void addUnit (std::shared_ptr<cBuilding> building);
102 
103 	std::shared_ptr<cBuilding> removeUnit (const cBuilding& building);
104 	std::shared_ptr<cVehicle> removeUnit (const cVehicle& vehicle);
105 
106 	void removeAllUnits();
107 
108 	cVehicle* getVehicleFromId (unsigned int id) const;
109 	cBuilding* getBuildingFromId (unsigned int id) const;
110 
111 	const cFlatSet<std::shared_ptr<cVehicle>, sUnitLess<cVehicle>>& getVehicles() const;
112 	const cFlatSet<std::shared_ptr<cBuilding>, sUnitLess<cBuilding>>& getBuildings() const;
113 
114 	cUnit* getNextUnit (cUnit* start) const;
115 	cUnit* getPrevUnit (cUnit* start) const;
116 
117 	bool hasUnits() const;
118 
119 	void addSentry (cUnit& u);
120 	void deleteSentry (cUnit& u);
121 	void upgradeUnitTypes (const std::vector<int>& areasReachingNextLevel, std::vector<sUnitData*>& resultUpgradedUnitDatas);
122 	void countEcoSpheres();
123 	int getScore (int turn) const;
124 	int getScore() const;
125 	void setScore (int score, int turn);
126 	void clearDone();
127 
128 	void addSavedReport (std::unique_ptr<cSavedReport> savedReport);
129 	const std::vector<std::unique_ptr<cSavedReport>>& getSavedReports() const;
130 
131 	void setClan (int newClan);
getClan()132 	int getClan() const { return clan; }
133 
134 	bool getHasFinishedTurn() const;
135 	void setHasFinishedTurn (bool value);
136 
137 	bool getIsRemovedFromGame() const;
138 	void setIsRemovedFromGame (bool value);
139 
exploreResource(const cPosition & pos)140 	void exploreResource (const cPosition& pos) { ResourceMap[getOffset (pos)] = 1; }
hasResourceExplored(const cPosition & pos)141 	bool hasResourceExplored (const cPosition& pos) const { return ResourceMap[getOffset (pos)] != 0; }
hasSentriesAir(const cPosition & pos)142 	bool hasSentriesAir (const cPosition& pos) const { return SentriesMapAir[getOffset (pos)] != 0; }
hasSentriesGround(const cPosition & pos)143 	bool hasSentriesGround (const cPosition& pos) const { return SentriesMapGround[getOffset (pos)] != 0; }
hasLandDetection(const cPosition & pos)144 	bool hasLandDetection (const cPosition& pos) const { return DetectLandMap[getOffset (pos)] != 0; }
hasMineDetection(const cPosition & pos)145 	bool hasMineDetection (const cPosition& pos) const { return DetectMinesMap[getOffset (pos)] != 0; }
hasSeaDetection(const cPosition & pos)146 	bool hasSeaDetection (const cPosition& pos) const { return DetectSeaMap[getOffset (pos)] != 0; }
147 
148 	void doResearch (cServer& server);  ///< proceed with the research at turn end
149 	void accumulateScore (cServer& server); // at turn end
150 
151 	void refreshSentryAir();
152 	void refreshSentryGround();
153 
154 	bool mayHaveOffensiveUnit() const;
155 
156 	void addTurnReportUnit (const sID& unitId);
157 	void resetTurnReportData();
158 	const std::vector<sTurnstartReport>& getCurrentTurnUnitReports() const;
159 
160 	const std::vector<int>& getCurrentTurnResearchAreasFinished() const;
161 	void setCurrentTurnResearchAreasFinished (std::vector<int> areas);
162 
163 	bool isCurrentTurnResearchAreaFinished (cResearch::ResearchArea area) const;
164 
165 	const cResearch& getResearchState() const;
166 	cResearch& getResearchState();
167 
168 	int getResearchCentersWorkingTotal() const;
169 	int getResearchCentersWorkingOnArea (cResearch::ResearchArea area) const;
170 
171 	void startAResearch (cResearch::ResearchArea researchArea);
172 	void stopAResearch (cResearch::ResearchArea researchArea);
173 
174 	void refreshResearchCentersWorkingOnArea();
175 
176 	mutable cSignal<void ()> nameChanged;
177 	mutable cSignal<void ()> colorChanged;
178 	mutable cSignal<void ()> creditsChanged;
179 	mutable cSignal<void (const cSavedReport&)> reportAdded;
180 	mutable cSignal<void ()> hasFinishedTurnChanged;
181 	mutable cSignal<void ()> isRemovedFromGameChanged;
182 	mutable cSignal<void (cResearch::ResearchArea)> researchCentersWorkingOnAreaChanged;
183 	mutable cSignal<void ()> researchCentersWorkingTotalChanged;
184 private:
185 	/**
186 	* draws a circle on the map for the fog
187 	* @author alzi alias DoctorDeath
188 	* @param iX X coordinate to the center of the circle
189 	* @param iY Y coordinate to the center of the circle
190 	* @param iRadius radius of the circle
191 	* @param map map were to store the data of the circle
192 	*/
193 	void drawSpecialCircle (const cPosition& position, int iRadius, std::vector<char>& map, const cPosition& mapsize);
194 	/**
195 	* draws a big circle on the map for the fog
196 	* @author alzi alias DoctorDeath
197 	* @param iX X coordinate to the center of the circle
198 	* @param iY Y coordinate to the center of the circle
199 	* @param iRadius radius of the circle
200 	* @param map map were to store the data of the circle
201 	*/
202 	void drawSpecialCircleBig (const cPosition& position, int iRadius, std::vector<char>& map, const cPosition& mapsize);
203 
204 	cBuilding* getNextBuilding (cBuilding* start) const;
205 	cBuilding* getNextMiningStation (cBuilding* start) const;
206 	cVehicle* getNextVehicle (cVehicle* start) const;
207 
208 	cBuilding* getPrevBuilding (cBuilding* start) const;
209 	cBuilding* getPrevMiningStation (cBuilding* start) const;
210 	cVehicle* getPrevVehicle (cVehicle* start) const;
211 
212 private:
213 	cPlayerBasicData splayer;
214 public:
215 	std::vector<sUnitData> VehicleData; // Current version of vehicles.
216 	std::vector<sUnitData> BuildingData; // Current version of buildings.
217 	cBase base;               // Die Basis dieses Spielers.
218 private:
219 	cFlatSet<std::shared_ptr<cVehicle>, sUnitLess<cVehicle>> vehicles;
220 	cFlatSet<std::shared_ptr<cBuilding>, sUnitLess<cBuilding>> buildings;
221 
222 	int landingPosX;
223 	int landingPosY;
224 	cPosition mapSize; // Width and Height of the map.
225 
226 	std::vector<char> ScanMap;            // seen Map tile.
227 	std::vector<char> ResourceMap;        // Map with explored resources.
228 	std::vector<char> SentriesMapAir;     /**< the covered air area */
229 	std::vector<char> SentriesMapGround;  /**< the covered ground area */
230 	std::vector<char> DetectLandMap;      // Map mit den Gebieten, die an Land gesehen werden kˆnnen.
231 	std::vector<char> DetectSeaMap;       // Map mit den Gebieten, die im Wasser gesehen werden kˆnnen.
232 	std::vector<char> DetectMinesMap;     /** the area where the player can detect mines */
233 public:
234 	mutable PointsHistory pointsHistory; // history of player's total score (from eco-spheres) for graph
235 	std::vector<std::unique_ptr<cSavedReport>> savedReportsList;
236 	bool isDefeated;        // true if the player has been defeated
237 	int numEcos;            // number of ecospheres. call countEcoSpheres on server to update.
238 	unsigned int lastDeletedUnit;  /*!< used for detecting ownerchanges of a unit, e.g. a unit is readded with different player*/
239 private:
240 	int clan;
241 
242 	int credits;
243 
244 	std::vector<sTurnstartReport> currentTurnUnitReports;
245 	std::vector<int> currentTurnResearchAreasFinished;
246 
247 	bool hasFinishedTurn;
248 	bool isRemovedFromGame;
249 
250 	cResearch researchState;   ///< stores the current research level of the player
251 	int researchCentersWorkingOnArea[cResearch::kNrResearchAreas]; ///< counts the number of research centers that are currently working on each area
252 	int researchCentersWorkingTotal;  ///< number of working research centers
253 };
254 
255 #endif // game_data_player_playerH
256