1 /***************************************************************************
2 						map.h  -  description
3 							-------------------
4 	begin                : january 24th, 2004
5 	copyright            : (C) 2004-2010 Duong Khang NGUYEN
6 	email                : neoneurone @ gmail com
7 
8 	$Id: map.h 450 2010-11-21 19:11:43Z neoneurone $
9  ***************************************************************************/
10 
11 /***************************************************************************
12  *                                                                         *
13  *   This program is free software; you can redistribute it and/or modify  *
14  *   it under the terms of the GNU General Public License as published by  *
15  *   the Free Software Foundation; either version 2 of the License, or     *
16  *   any later version.                                                    *
17  *                                                                         *
18  ***************************************************************************/
19 
20 #ifndef _OPENCITY_MAP_H_
21 #define _OPENCITY_MAP_H_ 1
22 
23 #include "main.h"
24 #include "persistence.h"
25 
26 enum OPENCITY_DIRECTION;
27 class Layer;
28 
29 
30 //========================================================================
31 /** A "Map" object contains an array of square's OY height. Each square
32 	has 4 values of heights. They match the map square's corners.
33 */
34 class Map: public Persistence {
35 public:
36 	Map(
37 		const uint width,
38 		const uint height );
39 
40 	~Map();
41 
42 
43 //========================================================================
44 /** Save the data to the specified fstream
45 	\param rfs A reference to a file stream which is ready for writing
46 	\see Persistence
47 */
48 	void
49 	SaveTo( std::fstream& rfs );
50 
51 
52 //========================================================================
53 /** Load the data from the specified stream
54 	\param rfs A reference to a file stream which is ready for reading
55 	\see Persistence
56 */
57 	void
58 	LoadFrom( std::fstream& rfs );
59 
60 
61 //========================================================================
62 /** Change the square's OY heights. The square is designated
63 	by its W,H coordinates. ( OX, OZ OpenGL coordinates )
64 */
65 	OPENCITY_ERR_CODE
66 	ChangeHeight(
67 		const uint& rcuiW,
68 		const uint& rcuiH,
69 		const OPENCITY_MAP_VARIATION& enumVar );
70 
71 
72 	void
73 	SetLayer(
74 		const Layer*  layer );
75 
76 
77 	const char
78 	GetLinearHeight(
79 		const uint& rcuiIndex ) const;
80 
81 
82 	const uint
83 	GetMaxLinear() const;
84 
85 
86 //========================================================================
87 /** Get all the 4 heights of a square designated by its W,H. The corner's
88 height is returned in the following order:
89 left-up, left-down, right-down, right-up
90 	\param rcuiW,rcuiH The W, L (x, y) map coordinates
91 	\param tabH The height table
92 */
93 	void
94 	GetSquareHeight(
95 		const uint& rcuiW,
96 		const uint& rcuiH,
97 		signed char tabH[] ) const;
98 
99 
100 	const signed char
101 	GetSquareMinHeight(
102 		const uint& rcuiW,
103 		const uint& rcuiL ) const;
104 
105 
106 	const signed char
107 	GetSquareMaxHeight(
108 		const uint& rcuiW,
109 		const uint& rcuiH ) const;
110 
111 
112 //========================================================================
113 /** Check if the specified square is plane
114 	\param w,l The WL coordinates of the square to check
115 	\return True if it's plane, false otherwise
116 */
117 	const bool
118 	IsSquarePlane(
119 		const uint w,
120 		const uint l ) const;
121 
122 
123 	const bool
124 	IsSurfacePlane(
125 		const uint w1, const uint l1,
126 		const uint w2, const uint l2 ) const;
127 
128 
129 	const bool
130 	IsSquareLinearPlane(
131 		const uint& rcuiLinearIndex );
132 
133 
134 //========================================================================
135 /** Calculate the WH coordinates of the neighbour square given the direction
136 	\return true if the WH of the neighbour were found false otherwise
137 	(the current square is located at the edge of the map)
138 */
139 	const bool
140 	GetNeighbourWH(
141 		const uint& rcuiMapW,
142 		const uint& rcuiMapH,
143 		uint& ruiNbrW,
144 		uint& ruiNbrH,
145 		const OPENCITY_DIRECTION& enumDir ) const;
146 
147 
148 //========================================================================
149 /** Calculate the new possible WH given the changes. If the delta values
150 are too big to be incorporated in the new WH, the new WH values are
151 cliped to the map edges
152 */
153 	void
154 	GetPossibleWH(
155 		uint& rW,
156 		uint& rH,
157 		const int& deltaW,
158 		const int& deltaH ) const;
159 
160 
161 private:
162 	uint _uiMapWidth;
163 	uint _uiMapHeight;
164 
165 	signed char* _btabSquareHeight;
166 
167 /// we need this to know if a square can be safely raised up or lowered down
168 	const Layer* _pclayer;
169 
170 
171 
172    /*=====================================================================*/
173    /*                        PRIVATE     METHODS                          */
174    /*=====================================================================*/
175 
176 //========================================================================
177 /** Convert the linear index to the square's W,H coordinates
178 */
179 	void
180 	_Linear2WH(
181 		const uint& linear,
182 		uint& w,
183 		uint& h ) const;
184 
185 
186 //========================================================================
187 /** Convert the W,H coordinates to the linear index according to the
188 datas stocked inside the class
189 	\note Must be dishtinguished from structure linear index
190 */
191 	inline uint
192 	_WH2Linear(
193 		const uint& w,
194 		const uint& h,
195 		uint& linear ) const;
196 };
197 
198 #endif
199 
200 
201 
202 
203 
204 
205 
206 
207 
208 
209 
210 
211 
212 
213 
214 
215 
216 
217 
218 
219 
220 
221 
222 
223 
224 
225 
226