30 |
|
|
31 |
#include "Map/MapTerrain.h" |
#include "Map/MapTerrain.h" |
32 |
|
|
33 |
|
/** |
34 |
|
* The terrain stores a heightField which can be displayed |
35 |
|
* with lights, using normals and colors calculated at each point |
36 |
|
*/ |
37 |
|
|
38 |
@implementation MapTerrain |
@implementation MapTerrain |
39 |
|
|
40 |
/** |
/** |
41 |
* path may be a valid path to a tiff file |
* Initializes a map with a file. |
42 |
* describing a height field; |
* imageName must be a valid tiff file |
43 |
* if any problem opening the file, |
* describing a height field in gray level mode. |
44 |
* returns nil |
* If any problem opening the file, returns nil. |
45 |
*/ |
*/ |
46 |
- (id) initWithImageName: (NSString *)imageName |
- (id) initWithImageName: (NSString *)imageName |
47 |
{ |
{ |
62 |
image= [NSBitmapImageRep imageRepWithData: [file readDataToEndOfFile]]; |
image= [NSBitmapImageRep imageRepWithData: [file readDataToEndOfFile]]; |
63 |
if (!image) |
if (!image) |
64 |
return nil; |
return nil; |
65 |
|
|
66 |
|
if ( [image bitsPerSample] != sizeof (u_int8_t)) |
67 |
|
return nil; |
68 |
|
|
69 |
_heightField = [image bitmapData]; |
_heightField = [image bitmapData]; |
70 |
|
|
71 |
_xLength = [image pixelsWide]; |
_xLength = [image pixelsWide]; |
76 |
} |
} |
77 |
|
|
78 |
/** |
/** |
79 |
* creates a height field filled with 0 |
* Creates a height field filled with 0. |
80 |
* xLength and zLength must be strictly posivitve numbers |
* xLength and zLength must be strictly positive numbers. |
81 |
*/ |
*/ |
82 |
- (id) initWithXLength: (int)xLength |
- (id) initWithXLength: (int)xLength |
83 |
withZLength: (int)zLength |
withZLength: (int)zLength |
105 |
} |
} |
106 |
|
|
107 |
/** |
/** |
108 |
* frees the arrays |
* Frees the height field |
109 |
*/ |
*/ |
110 |
- (void) dealloc |
- (void) dealloc |
111 |
{ |
{ |
116 |
|
|
117 |
|
|
118 |
/** |
/** |
119 |
* xLength is the horizontal length of the map |
* Returns the x-axis length of the map. |
120 |
*/ |
*/ |
121 |
- (int) xLength |
- (int) xLength |
122 |
{ |
{ |
124 |
} |
} |
125 |
|
|
126 |
/** |
/** |
127 |
* zLength is the vertical length of the map |
* Returns is the z-axis length of the map. |
128 |
*/ |
*/ |
129 |
- (int) zLength |
- (int) zLength |
130 |
{ |
{ |
132 |
} |
} |
133 |
|
|
134 |
/** |
/** |
135 |
* returns a number n such as 0 <= n < max |
* Returns a number n such as 0 <= n < max. |
136 |
|
* Used to make a valid coordinate from any position. |
137 |
* aroundTheWorld(-1) = max - 1 |
* aroundTheWorld(-1) = max - 1 |
138 |
* aroundTheWorld(max) = 0 |
* aroundTheWorld(max) = 0 |
139 |
* aroundTheWorld(max + 1) = 1 |
* aroundTheWorld(max + 1) = 1 |
144 |
NSCParameterAssert (max > 0); |
NSCParameterAssert (max > 0); |
145 |
|
|
146 |
if (n < 0) |
if (n < 0) |
147 |
return (max + (n % max)); |
return (max + ((n + 1) % max) - 1); |
148 |
else |
else |
149 |
return (n % max); |
return (n % max); |
150 |
} |
} |
151 |
|
|
152 |
|
|
153 |
|
/** |
154 |
|
* Returns a pointer to the tab, assuming position to be a valid one |
155 |
|
*/ |
156 |
- (u_int8_t *) _tabAtPosition: (map_coord_t)position |
- (u_int8_t *) _tabAtPosition: (map_coord_t)position |
157 |
{ |
{ |
158 |
NSParameterAssert (position.x >= 0); |
NSParameterAssert (position.x >= 0); |
163 |
return &(_heightField[(int)position.x + (int)position.z * _xLength]); |
return &(_heightField[(int)position.x + (int)position.z * _xLength]); |
164 |
} |
} |
165 |
|
|
166 |
|
|
167 |
|
/** |
168 |
|
* Returns a pointer to the tab with any position. |
169 |
|
* If the coordinates go beyond the edge of the map, |
170 |
|
* they pass on other side by modulo. |
171 |
|
*/ |
172 |
- (u_int8_t *) _heightAtPosition: (map_coord_t)position |
- (u_int8_t *) _heightAtPosition: (map_coord_t)position |
173 |
{ |
{ |
174 |
return [self _tabAtPosition: |
return [self _tabAtPosition: |
177 |
} |
} |
178 |
|
|
179 |
/** |
/** |
180 |
* if the co-ordinates go beyond the edge of the map, |
* Returns the map's height. |
181 |
* they pass on other side by modulo |
* position may be any positive or negative coordinate: |
182 |
|
* it turns around the map by modulo. |
183 |
*/ |
*/ |
184 |
- (u_int8_t) heightAtPosition: (map_coord_t)position |
- (u_int8_t) heightAtPosition: (map_coord_t)position |
185 |
{ |
{ |
187 |
} |
} |
188 |
|
|
189 |
/** |
/** |
190 |
* |
* Returns the sea level |
191 |
*/ |
*/ |
192 |
- (u_int8_t) seaLevel |
- (u_int8_t) seaLevel |
193 |
{ |
{ |
196 |
|
|
197 |
|
|
198 |
/** |
/** |
199 |
* seaLevel must be a positive integer |
* Sets the value of seaLevel |
200 |
*/ |
*/ |
201 |
- (void) setSeaLevel: (u_int8_t)seaLevel |
- (void) setSeaLevel: (u_int8_t)seaLevel |
202 |
{ |
{ |
|
// NSParameterAssert (seaLevel >= 0); |
|
|
|
|
203 |
_seaLevel = seaLevel; |
_seaLevel = seaLevel; |
204 |
} |
} |
205 |
|
|
206 |
/** |
/** |
207 |
* if the co-ordinates go beyond the edge of the map, |
* Sets the map's height at position. |
208 |
* they pass on other side by modulo |
* If the coordinates go beyond the edge of the map, |
209 |
|
* they pass on other side by modulo. |
210 |
*/ |
*/ |
211 |
- (void) setHeightAtPosition: (map_coord_t)position |
- (void) setHeightAtPosition: (map_coord_t)position |
212 |
toHeight: (u_int8_t)height |
toHeight: (u_int8_t)height |
213 |
{ |
{ |
|
// NSParameterAssert (height >= 0); |
|
|
|
|
214 |
*[self _heightAtPosition: position] = height; |
*[self _heightAtPosition: position] = height; |
215 |
} |
} |
216 |
|
|