1 // ==============================================================
2 //	This file is part of Glest (www.glest.org)
3 //
4 //	Copyright (C) 2001-2008 Martiño Figueroa
5 //
6 //	You can redistribute this code and/or modify it under
7 //	the terms of the GNU General Public License as published
8 //	by the Free Software Foundation; either version 2 of the
9 //	License, or (at your option) any later version
10 // ==============================================================
11 
12 #ifndef _MAPPREVIEW_MAP_H_
13 #define _MAPPREVIEW_MAP_H_
14 
15 #include "util.h"
16 #include "data_types.h"
17 #include "randomgen.h"
18 #include "vec.h"
19 #include <vector>
20 #include <string>
21 
22 using Shared::Platform::int8;
23 using Shared::Platform::int32;
24 using Shared::Platform::float32;
25 using Shared::Util::RandomGen;
26 using Shared::Graphics::Vec2i;
27 
28 namespace Shared { namespace Map {
29 
30 enum MapSurfaceType {
31 	st_Grass = 1,
32 	st_Secondary_Grass,
33 	st_Road,
34 	st_Stone,
35 	st_Ground
36 
37 };
38 
39 static const int MAX_TITLE_LENGTH 		= 128;
40 static const int MAX_AUTHOR_LENGTH 		= 128;
41 static const int MAX_DESCRIPTION_LENGTH = 256;
42 static const int MAX_DESCRIPTION_LENGTH_VERSION2 = 128;
43 
44 static const int MIN_MAP_CELL_DIMENSION		= 16;
45 static const int MAX_MAP_CELL_DIMENSION		= 1024;
46 
47 static const int MIN_MAP_CELL_HEIGHT 		= 0;
48 static const int MAX_MAP_CELL_HEIGHT 		= 20;
49 static const int DEFAULT_MAP_CELL_HEIGHT 	= 10;
50 
51 static const int MIN_MAP_FACTIONCOUNT 		= 1;
52 static const int MAX_MAP_FACTIONCOUNT 		= 8;
53 static const int DEFAULT_MAP_FACTIONCOUNT 	= 8;
54 
55 static const int DEFAULT_MAP_CELL_WIDTH 		= 128;
56 static const int DEFAULT_MAP_CELL_LENGTH		= 128;
57 
58 static const MapSurfaceType DEFAULT_MAP_CELL_SURFACE_TYPE 	= st_Grass;
59 
60 static const int DEFAULT_MAP_CELL_HEIGHT_FACTOR		= 3;
61 static const int DEFAULT_MAP_WATER_DEPTH			= 4;
62 static const int DEFAULT_CLIFF_HEIGHT				= 0;
63 
64 enum MapVersionType {
65 	mapver_1 = 1,
66 	mapver_2,
67 
68 	mapver_MAX
69 };
70 
71 static const int MAP_FORMAT_VERSION = mapver_MAX - 1;
72 
73 struct MapFileHeader {
74 	int32 version;
75 	int32 maxFactions;
76 	int32 width;
77 	int32 height;
78 	int32 heightFactor;
79 	int32 waterLevel;
80 	int8 title[MAX_TITLE_LENGTH];
81 	int8 author[MAX_AUTHOR_LENGTH];
82 	union {
83 		int8 description[MAX_DESCRIPTION_LENGTH];
84 		struct {
85 			int8 short_desc[MAX_DESCRIPTION_LENGTH_VERSION2];
86 			int32 magic; // 0x01020304 for meta
87 			int32 cliffLevel;
88 			int32 cameraHeight;
89 			int8 meta[116];
90 		} version2;
91 	};
92 };
93 
94 void toEndianMapFileHeader(MapFileHeader &header);
95 void fromEndianMapFileHeader(MapFileHeader &header);
96 
97 class MapInfo {
98 public:
99 
100 	Vec2i size;
101 	int players;
102 	string desc;
103 
MapInfo()104 	MapInfo() {
105 		size 	= Vec2i(0,0);
106 		players = 0;
107 		desc 	= "";
108 	}
109 };
110 
111 // ===============================================
112 //	class Map
113 // ===============================================
114 
115 class MapPreview {
116 public:
117 	static const int maxHeight = 20;
118 	static const int minHeight = 0;
119 
120 private:
121 	struct Cell {
122 		int surface;
123 		int object;
124 		int resource;
125 		float height;
126 	};
127 
128 	struct StartLocation {
129 		int x;
130 		int y;
131 	};
132 
133 	RandomGen random;
134 	string title;
135 	string author;
136 	string desc;
137 	string recScn;
138 	int type;
139 	int h;
140 	int w;
141 	int heightFactor;
142 	int waterLevel;
143 	int cliffLevel;
144 	int cameraHeight;
145 	//Cell **cells;
146 	std::vector<std::vector<Cell> > cells;
147 
148 	int maxFactions;
149 	//StartLocation *startLocations;
150 	std::vector<StartLocation> startLocations;
151 	int refAlt;
152 
153 	bool fileLoaded;
154 	string mapFileLoaded;
155 	bool hasChanged;
156 
157 public:
158 	MapPreview();
159 	~MapPreview();
160 
getHasChanged()161 	bool getHasChanged() const { return hasChanged; }
setHasChanged(bool value)162 	void setHasChanged(bool value) { hasChanged = value; }
163 
164 	float getHeight(int x, int y) const;
165 	bool isCliff(int x,int y);
166 	MapSurfaceType getSurface(int x, int y) const;
167 	int getObject(int x, int y) const;
168 	int getResource(int x, int y) const;
169 	int getStartLocationX(int index) const;
170 	int getStartLocationY(int index) const;
getHeightFactor()171 	int getHeightFactor() const{return heightFactor;}
getWaterLevel()172 	int getWaterLevel() const{return waterLevel;}
getCliffLevel()173 	int getCliffLevel() const{return cliffLevel;}
getCameraHeight()174 	int getCameraHeight() const{return cameraHeight;}
175 
176 	bool inside(int x, int y);
177 
178 	void setRefAlt(int x, int y);
179 	void setAdvanced(int heightFactor, int waterLevel, int cliffLevel, int cameraHeight);
180 	void setTitle(const string &title);
181 	void setDesc(const string &desc);
182 	void setAuthor(const string &author);
183 
getH()184 	int getH() const			{return h;}
getW()185 	int getW() const			{return w;}
getMaxFactions()186 	int getMaxFactions() const	{return maxFactions;}
getTitle()187 	string getTitle() const		{return title;}
getDesc()188 	string getDesc() const		{return desc;}
getAuthor()189 	string getAuthor() const	{return author;}
190 
191 	void glestChangeHeight(int x, int y, int height, int radius);
192 	void pirateChangeHeight(int x, int y, int height, int radius);
193 	void changeSurface(int x, int y, MapSurfaceType surface, int radius);
194 	void changeObject(int x, int y, int object, int radius);
195 	void changeResource(int x, int y, int resource, int radius);
196 	void changeStartLocation(int x, int y, int player);
197 
198 	void setHeight(int x, int y, float height);
199 	void setSurface(int x, int y, MapSurfaceType surface);
200 	void setObject(int x, int y, int object);
201 	void setResource(int x, int y, int resource);
202 
203 	void flipX();
204 	void flipY();
205 	void copyXY(int x, int y, int sx, int sy);  // destination x,y = source sx,sy
206 	void swapXY(int x, int y, int sx, int sy);
207 	void reset(int w, int h, float alt, MapSurfaceType surf);
208 	void resize(int w, int h, float alt, MapSurfaceType surf);
209 	void resetFactions(int maxFactions);
210 	void randomizeHeights(bool withReset,int minimumHeight, int maximumHeight, int chanceDevider, int smoothRecursions);
211 	void randomizeFactions();
212 	void smoothSurface(bool limitHeights);
213 	void switchSurfaces(MapSurfaceType surf1, MapSurfaceType surf2);
214 
215 	void loadFromFile(const string &path);
216 	void saveToFile(const string &path);
217 
218 	void resetHeights(int height);
219 	void realRandomize(int minimumHeight, int maximumHeight, int chanceDivider, int smoothRecursions);
220 	void applyNewHeight(float newHeight, int x, int y, int strenght);
221 
hasFileLoaded()222 	bool hasFileLoaded() const {return fileLoaded;}
getMapFileLoaded()223 	string getMapFileLoaded() const { return mapFileLoaded; }
224 
225 	static bool loadMapInfo(string file, MapInfo *mapInfo, string i18nMaxMapPlayersTitle,string i18nMapSizeTitle,bool errorOnInvalidMap=true);
226 	static string getMapPath(const vector<string> &pathList, const string &mapName, string scenarioDir="", bool errorOnNotFound=true);
227 	static vector<string> findAllValidMaps(const vector<string> &pathList,
228 			string scenarioDir, bool getUserDataOnly=false, bool cutExtension=true,
229 			vector<string> *invalidMapList=NULL);
230 };
231 
232 }}// end namespace
233 
234 #endif
235