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_map_mapH 21 #define game_data_map_mapH 22 23 #include <string> 24 #include <memory> 25 #include <vector> 26 27 #include "utility/autosurface.h" 28 #include "defines.h" 29 #include "t_2.h" 30 #include "utility/position.h" 31 #include "utility/signal/signal.h" 32 #include "ui/graphical/menu/windows/windowgamesettings/gamesettings.h" 33 34 class cUnit; 35 class cVehicle; 36 class cBuilding; 37 class cPlayer; 38 class cPosition; 39 struct sUnitData; 40 41 // Resources Struktur //////////////////////////////////////////////////////// 42 struct sResources 43 { 44 public: sResourcessResources45 sResources() : value (0), typ (0) {} 46 public: 47 unsigned char value; 48 unsigned char typ; 49 }; 50 // Die Resorces-Typen: 51 const int RES_NONE = 0; 52 const int RES_METAL = 1; 53 const int RES_OIL = 2; 54 const int RES_GOLD = 3; 55 const int RES_COUNT = 4; 56 57 /** contains all information of a map field */ 58 class cMapField 59 { 60 public: 61 cMapField(); 62 63 /** returns the top vehicle on this field */ 64 cVehicle* getVehicle() const; 65 66 /** returns a Iterator for the planes on this field */ 67 cVehicle* getPlane() const; 68 69 /** returns the buildings on this field */ 70 const std::vector<cBuilding*>& getBuildings() const; 71 /** returns the vehicles on this field */ 72 const std::vector<cVehicle*>& getVehicles() const; 73 /** returns the planes on this field */ 74 const std::vector<cVehicle*>& getPlanes() const; 75 76 /** returns all units on this field */ 77 void getUnits (std::vector<cUnit*>& units) const; 78 79 /** returns a pointer for the buildings on this field */ 80 cBuilding* getBuilding() const; 81 /** returns a pointer to the top building or nullptr if the first building is a base type */ 82 cBuilding* getTopBuilding() const; 83 /** returns a pointer to the first base building or nullptr if there is no base building */ 84 cBuilding* getBaseBuilding() const; 85 /** returns a pointer to a rubble object, if there is one. */ 86 cBuilding* getRubble() const; 87 /** returns a pointer to an expl. mine, if there is one */ 88 cBuilding* getMine() const; 89 90 /** Adds the passed building before the given index to the building list of the field */ 91 void addBuilding (cBuilding& building, size_t index); 92 /** Adds the passed vehicle before the given index to the vehicle list of the field */ 93 void addVehicle (cVehicle& vehicle, size_t index); 94 /** Adds the passed plane before the given index to the plane list of the field */ 95 void addPlane (cVehicle& plane, size_t index); 96 97 /** Removes the passed building from the field's building list */ 98 void removeBuilding (const cBuilding& building); 99 /** Removes the passed vehicle from the field's vehicle list */ 100 void removeVehicle (const cVehicle& vehicle); 101 /** Removes the passed plane from the field's plane list */ 102 void removePlane (const cVehicle& plane); 103 104 /** Removed all units from the field */ 105 void removeAll(); 106 107 /** Triggered when a building has been added or removed to/from the field */ 108 mutable cSignal<void ()> buildingsChanged; 109 /** Triggered when a vehicle has been added or removed to/from the field */ 110 mutable cSignal<void ()> vehiclesChanged; 111 /** Triggered when a plane has been added or removed to/from the field */ 112 mutable cSignal<void ()> planesChanged; 113 /** Triggered when any unit (building, vehicle or plane) has been added or removed to/from the field */ 114 mutable cSignal<void ()> unitsChanged; 115 116 private: 117 cMapField (const cMapField& other) MAXR_DELETE_FUNCTION; 118 cMapField& operator= (const cMapField& other) MAXR_DELETE_FUNCTION; 119 120 /**the list with all buildings on this field 121 * the top building is always stored at first position */ 122 std::vector<cBuilding*> buildings; 123 /** the list with all planes on this field 124 * the top plane is always stored at first position */ 125 std::vector<cVehicle*> vehicles; 126 /**the list with all vehicles on this field 127 * the top vehicle is always stored at first position */ 128 std::vector<cVehicle*> planes; 129 130 }; 131 132 struct sTerrain 133 { 134 sTerrain(); 135 sTerrain(sTerrain&& other); 136 sTerrain& operator=(sTerrain&& other); 137 138 AutoSurface sf; /** the scaled surface of the terrain */ 139 AutoSurface sf_org; /** the original surface of the terrain */ 140 AutoSurface shw; /** the scaled surface of the terrain in the fog */ 141 AutoSurface shw_org; /** the original surface of the terrain in the fog */ 142 bool water; /** is this terrain water? */ 143 bool coast; /** is this terrain a coast? */ 144 bool blocked; /** is this terrain blocked? */ 145 146 private: 147 sTerrain(const sTerrain& other) MAXR_DELETE_FUNCTION; 148 sTerrain& operator=(const sTerrain& other) MAXR_DELETE_FUNCTION; 149 }; 150 151 class cStaticMap 152 { 153 public: 154 static const int tilePixelHeight = 64; 155 static const int tilePixelWidth = 64; 156 157 cStaticMap(); 158 ~cStaticMap(); 159 160 void clear(); 161 bool loadMap (const std::string& filename); 162 bool isValid() const; 163 getName()164 const std::string& getName() const { return filename; } getSize()165 cPosition getSize() const { return cPosition (size, size); } getOffset(const cPosition & pos)166 int getOffset (const cPosition& pos) const { assert (isValidPosition (pos)); return pos.y() * size + pos.x(); } 167 168 bool isValidPosition (const cPosition& position) const; 169 170 bool isBlocked (const cPosition& position) const; 171 bool isCoast (const cPosition& position) const; 172 bool isWater (const cPosition& position) const; 173 174 const sTerrain& getTerrain (const cPosition& position) const; 175 176 AutoSurface createBigSurface (int sizex, int sizey) const; 177 void generateNextAnimationFrame(); 178 void scaleSurfaces (int pixelSize); 179 static AutoSurface loadMapPreview (const std::string& mapPath, int* mapSize = nullptr); 180 private: 181 static AutoSurface loadTerrGraph (SDL_RWops* fpMapFile, int iGraphicsPos, const SDL_Color (&colors)[256], int iNum); 182 void copySrfToTerData (SDL_Surface& surface, int iNum); 183 184 std::string filename; // Name of the current map 185 int size; 186 std::vector<sTerrain> terrains; // The different terrain type. 187 std::vector<int> Kacheln; // Terrain numbers of the map fields 188 SDL_Color palette[256]; // Palette with all Colors for the terrain graphics 189 SDL_Color palette_shw[256]; 190 }; 191 192 // Die Map-Klasse //////////////////////////////////////////////////////////// 193 class cMap 194 { 195 public: 196 explicit cMap (std::shared_ptr<cStaticMap> staticMap_); 197 ~cMap(); 198 getName()199 const std::string& getName() const { return staticMap->getName(); } getSize()200 cPosition getSize() const { return staticMap->getSize(); } getOffset(const cPosition & pos)201 int getOffset (const cPosition& pos) const { return staticMap->getOffset (pos); } isValidPosition(const cPosition & pos)202 bool isValidPosition (const cPosition& pos) const { return staticMap->isValidPosition (pos); } 203 isBlocked(const cPosition & position)204 bool isBlocked (const cPosition& position) const { return staticMap->isBlocked (position); } isCoast(const cPosition & position)205 bool isCoast (const cPosition& position) const { return staticMap->isCoast (position); } isWater(const cPosition & position)206 bool isWater (const cPosition& position) const { return staticMap->isWater (position); } 207 208 bool isWaterOrCoast (const cPosition& position) const; 209 getResource(const cPosition & position)210 const sResources& getResource (const cPosition& position) const { return Resources[getOffset (position)]; } getResource(const cPosition & position)211 sResources& getResource (const cPosition& position) { return Resources[getOffset (position)]; } 212 void assignRessources (const cMap& rhs); 213 214 /** 215 * converts the resource data to a string in HEX format 216 *@author alzi alias DoctorDeath 217 */ 218 std::string resourcesToString() const; 219 /** 220 * converts from HEX-string to the resources 221 *@author alzi alias DoctorDeath 222 */ 223 void setResourcesFromString (const std::string& str); 224 225 void placeRessources (const std::vector<cPosition>& landingPositions, const cGameSettings& gameSetting); 226 227 /** 228 * Access to a map field 229 * @param the offset of the map field 230 * @return an instance of cMapField, which has several methods to access the objects on the field 231 */ 232 cMapField& operator[] (unsigned int offset) const; 233 234 cMapField& getField (const cPosition& position); 235 const cMapField& getField (const cPosition& position) const; 236 237 void addBuilding (cBuilding& building, const cPosition& position); 238 void addVehicle (cVehicle& vehicle, const cPosition& position); 239 240 /** 241 * moves a vehicle to the given position 242 * resets the vehicle to a single field, when it was centered on four fields 243 * @param height defines the flight hight, when more then one planes on a field. 0 means top/highest. 244 */ 245 void moveVehicle (cVehicle& vehicle, const cPosition& position, int height = 0); 246 247 /** 248 * places a vehicle on the 4 fields to the right and below the given position 249 */ 250 void moveVehicleBig (cVehicle& vehicle, const cPosition& position); 251 252 void deleteBuilding (const cBuilding& building); 253 void deleteVehicle (const cVehicle& vehicle); 254 void deleteUnit (const cUnit& unit); 255 256 /** 257 * checks, whether the given field is an allowed place for the vehicle 258 * if checkPlayer is passed, the function uses the players point of view, so it does not check for units that are not in sight 259 */ 260 bool possiblePlace (const cVehicle& vehicle, const cPosition& position, bool checkPlayer = false) const; 261 bool possiblePlaceVehicle (const sUnitData& vehicleData, const cPosition& position, const cPlayer* player, bool checkPlayer = false) const; 262 263 /** 264 * checks, whether the given field is an allowed place for the building 265 * if a vehicle is passed, it will be ignored in the check, so a constructing vehicle does not block its own position 266 * Note: that the function can check for map border overflows (with margin). 267 */ 268 bool possiblePlaceBuilding (const sUnitData& buildingData, const cPosition& position, const cVehicle* vehicle = nullptr) const; 269 bool possiblePlaceBuildingWithMargin (const sUnitData& buildingData, const cPosition& position, int margin, const cVehicle* vehicle = nullptr) const; 270 271 /** 272 * removes all units from the map structure 273 */ 274 void reset(); 275 276 private: 277 static int getMapLevel (const cBuilding& building); 278 static int getMapLevel (const cVehicle& vehicle); 279 static int getResourceDensityFactor (eGameSettingsResourceDensity density); 280 static int getResourceAmountFactor (eGameSettingsResourceAmount amount); 281 282 public: 283 mutable cSignal<void (const cUnit&)> addedUnit; 284 mutable cSignal<void (const cUnit&)> removedUnit; 285 mutable cSignal<void (const cVehicle&, const cPosition&)> movedVehicle; 286 287 std::shared_ptr<cStaticMap> staticMap; 288 private: 289 /** 290 * the information about the fields 291 */ 292 cMapField* fields; 293 std::vector<sResources> Resources; // field with the ressource data 294 }; 295 296 #endif // game_data_map_mapH 297