24 |
24 |
25 Tile *_m = NULL; ///< Tiles of the map |
25 Tile *_m = NULL; ///< Tiles of the map |
26 TileExtended *_me = NULL; ///< Extended Tiles of the map |
26 TileExtended *_me = NULL; ///< Extended Tiles of the map |
27 |
27 |
28 |
28 |
29 /** |
29 /*! |
30 * (Re)allocates a map with the given dimension |
30 * (Re)allocates a map with the given dimension |
31 * @param size_x the width of the map along the NE/SW edge |
31 * @param size_x the width of the map along the NE/SW edge |
32 * @param size_y the 'height' of the map along the SE/NW edge |
32 * @param size_y the 'height' of the map along the SE/NW edge |
33 */ |
33 */ |
34 void AllocateMap(uint size_x, uint size_y) |
34 void AllocateMap(uint size_x, uint size_y) |
95 |
95 |
96 return TileXY(x, y); |
96 return TileXY(x, y); |
97 } |
97 } |
98 #endif |
98 #endif |
99 |
99 |
100 /** |
100 /*! |
101 * Scales the given value by the map size, where the given value is |
101 * Scales the given value by the map size, where the given value is |
102 * for a 256 by 256 map |
102 * for a 256 by 256 map. |
103 * @param n the value to scale |
103 * @param n the value to scale |
104 * @return the scaled size |
104 * @return the scaled size |
105 */ |
105 */ |
106 uint ScaleByMapSize(uint n) |
106 uint ScaleByMapSize(uint n) |
107 { |
107 { |
110 * Add (1<<4)-1 to round upwards. */ |
110 * Add (1<<4)-1 to round upwards. */ |
111 return (n * (MapSize() >> 12) + (1 << 4) - 1) >> 4; |
111 return (n * (MapSize() >> 12) + (1 << 4) - 1) >> 4; |
112 } |
112 } |
113 |
113 |
114 |
114 |
115 /** |
115 /*! |
116 * Scales the given value by the maps circumference, where the given |
116 * Scales the given value by the maps circumference, where the given |
117 * value is for a 256 by 256 map |
117 * value is for a 256 by 256 map |
118 * @param n the value to scale |
118 * @param n the value to scale |
119 * @return the scaled size |
119 * @return the scaled size |
120 */ |
120 */ |
126 * (1<<9) - 1 is there to scale upwards. */ |
126 * (1<<9) - 1 is there to scale upwards. */ |
127 return (n * (MapSizeX() + MapSizeY()) + (1 << 9) - 1) >> 9; |
127 return (n * (MapSizeX() + MapSizeY()) + (1 << 9) - 1) >> 9; |
128 } |
128 } |
129 |
129 |
130 |
130 |
131 /** |
131 /*! |
132 * This function checks if we add addx/addy to tile, if we |
132 * This function checks if we add addx/addy to tile, if we |
133 * do wrap around the edges. For example, tile = (10,2) and |
133 * do wrap around the edges. For example, tile = (10,2) and |
134 * addx = +3 and addy = -4. This function will now return |
134 * addx = +3 and addy = -4. This function will now return |
135 * INVALID_TILE, because the y is wrapped. This is needed in |
135 * INVALID_TILE, because the y is wrapped. This is needed in |
136 * for example, farmland. When the tile is not wrapped, |
136 * for example, farmland. When the tile is not wrapped, |
137 * the result will be tile + TileDiffXY(addx, addy) |
137 * the result will be tile + TileDiffXY(addx, addy) |
|
138 * |
138 * @param tile the 'starting' point of the adding |
139 * @param tile the 'starting' point of the adding |
139 * @param addx the amount of tiles in the X direction to add |
140 * @param addx the amount of tiles in the X direction to add |
140 * @param addy the amount of tiles in the Y direction to add |
141 * @param addy the amount of tiles in the Y direction to add |
141 * @return translated tile, or INVALID_TILE when it would've wrapped. |
142 * @return translated tile, or INVALID_TILE when it would've wrapped. |
142 */ |
143 */ |
187 const uint dy = delta(TileY(t0), TileY(t1)); |
188 const uint dy = delta(TileY(t0), TileY(t1)); |
188 return dx + dy; |
189 return dx + dy; |
189 } |
190 } |
190 |
191 |
191 |
192 |
192 /** |
193 /*! |
193 * Gets the 'Square' distance between the two given tiles. |
194 * Gets the 'Square' distance between the two given tiles. |
194 * The 'Square' distance is the square of the shortest (straight line) |
195 * The 'Square' distance is the square of the shortest (straight line) |
195 * distance between the two tiles. |
196 * distance between the two tiles. |
196 * Also known as euclidian- or L2-Norm squared. |
197 * Also known as euclidian- or L2-Norm squared. |
197 * @param t0 the start tile |
198 * @param t0 the start tile |
204 const int dy = TileY(t0) - TileY(t1); |
205 const int dy = TileY(t0) - TileY(t1); |
205 return dx * dx + dy * dy; |
206 return dx * dx + dy * dy; |
206 } |
207 } |
207 |
208 |
208 |
209 |
209 /** |
210 /*! |
210 * Gets the biggest distance component (x or y) between the two given tiles. |
211 * Gets the biggest distance component (x or y) between the two given tiles. |
211 * Also known as L-Infinity-Norm. |
212 * Also known as L-Infinity-Norm. |
212 * @param t0 the start tile |
213 * @param t0 the start tile |
213 * @param t1 the end tile |
214 * @param t1 the end tile |
214 * @return the distance |
215 * @return the distance |
219 const uint dy = delta(TileY(t0), TileY(t1)); |
220 const uint dy = delta(TileY(t0), TileY(t1)); |
220 return dx > dy ? dx : dy; |
221 return dx > dy ? dx : dy; |
221 } |
222 } |
222 |
223 |
223 |
224 |
224 /** |
225 /*! |
225 * Gets the biggest distance component (x or y) between the two given tiles |
226 * Gets the biggest distance component (x or y) between the two given tiles |
226 * plus the Manhattan distance, i.e. two times the biggest distance component |
227 * plus the Manhattan distance, i.e. two times the biggest distance component |
227 * and once the smallest component. |
228 * and once the smallest component. |
228 * @param t0 the start tile |
229 * @param t0 the start tile |
229 * @param t1 the end tile |
230 * @param t1 the end tile |
234 const uint dx = delta(TileX(t0), TileX(t1)); |
235 const uint dx = delta(TileX(t0), TileX(t1)); |
235 const uint dy = delta(TileY(t0), TileY(t1)); |
236 const uint dy = delta(TileY(t0), TileY(t1)); |
236 return dx > dy ? 2 * dx + dy : 2 * dy + dx; |
237 return dx > dy ? 2 * dx + dy : 2 * dy + dx; |
237 } |
238 } |
238 |
239 |
239 /** |
240 /*! |
240 * Param the minimum distance to an edge |
241 * Param the minimum distance to an edge |
241 * @param tile the tile to get the distance from |
242 * @param tile the tile to get the distance from |
242 * @return the distance from the edge in tiles |
243 * @return the distance from the edge in tiles |
243 */ |
244 */ |
244 uint DistanceFromEdge(TileIndex tile) |
245 uint DistanceFromEdge(TileIndex tile) |
250 const uint minl = xl < yl ? xl : yl; |
251 const uint minl = xl < yl ? xl : yl; |
251 const uint minh = xh < yh ? xh : yh; |
252 const uint minh = xh < yh ? xh : yh; |
252 return minl < minh ? minl : minh; |
253 return minl < minh ? minl : minh; |
253 } |
254 } |
254 |
255 |
255 /** |
256 /*! |
256 * Function performing a search around a center tile and going outward, thus in circle. |
257 * Function performing a search around a center tile and going outward, thus in circle. |
257 * Although it really is a square search... |
258 * Although it really is a square search... |
258 * Every tile will be tested by means of the callback function proc, |
259 * Every tile will be tested by means of the callback function proc, |
259 * which will determine if yes or no the given tile meets criteria of search. |
260 * which will determine if yes or no the given tile meets criteria of search. |
260 * @param tile to start the search from |
261 * @param tile to start the search from |