Adonthell  0.4
mapsquare_walkable.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2001 Alexandre Courbot
3  Part of the Adonthell Project <http://adonthell.nongnu.org>
4 
5  Adonthell is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  Adonthell is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with Adonthell. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 
20 /**
21  * @file mapsquare_walkable.h
22  * @author Alexandre Courbot <alexandrecourbot@linuxgames.com>
23  *
24  * @brief Declares the mapsquare_walkable and mapsquare_walkable_area classes.
25  *
26  *
27  */
28 
29 
30 #ifndef MAPSQUARE_WALKABLE_H_
31 #define MAPSQUARE_WALKABLE_H_
32 
33 #include "fileops.h"
34 #include "drawable.h"
35 #include <vector>
36 
37 
38 /**
39  * Size of a mapsquare (in pixels).
40  *
41  */
42 const u_int16 MAPSQUARE_SIZE = 20;
43 
44 /**
45  * Walkable from every side.
46  *
47  */
48 #define ALL_WALKABLE 15
49 
50 /**
51  * Walkable from South.
52  *
53  */
54 #define WALKABLE_SOUTH 1
55 
56 /**
57  * Walkable from North.
58  *
59  */
60 #define WALKABLE_NORTH 2
61 
62 /**
63  * Walkable from East.
64  *
65  */
66 #define WALKABLE_EAST 4
67 
68 /**
69  * Walkable from West.
70  *
71  */
72 #define WALKABLE_WEST 8
73 
74 /**
75  * Unreachable.
76  *
77  */
78 #define NONE_WALKABLE 0
79 
80 
81 
82 /**
83  * Contains information about the walkability of a mapsquare.
84  *
85  */
87 {
88 public:
89 
90  /**
91  * Default constructor.
92  *
93  */
95 
96  /**
97  * Loads a mapsquare_walkable from an opened file.
98  *
99  * @param file the file to load from.
100  *
101  * @return 0 in case of success, error code otherwise.
102  */
103  s_int8 get (igzstream& file);
104 
105  /**
106  * Puts a mapsquare_walkable into an opened file.
107  *
108  * @param file the file where to save.
109  *
110  * @return 0 in case of success, error code otherwise.
111  */
112  s_int8 put (ogzstream& file) const;
113 
114  /**
115  * Returns whether a mapsquare is walkable from west.
116  *
117  *
118  * @return true if the mapsquare is walkable from west, false otherwise.
119  */
120  bool is_walkable_west () const
121  {
122  return walkable & WALKABLE_WEST;
123  }
124 
125  /**
126  * Returns whether a mapsquare is walkable from east.
127  *
128  *
129  * @return true if the mapsquare is walkable from east, false otherwise.
130  */
131  bool is_walkable_east () const
132  {
133  return walkable & WALKABLE_EAST;
134  }
135 
136  /**
137  * Returns whether a mapsquare is walkable from north.
138  *
139  *
140  * @return true if the mapsquare is walkable from north, false otherwise.
141  */
142  bool is_walkable_north () const
143  {
144  return walkable & WALKABLE_NORTH;
145  }
146 
147  /**
148  * Returns whether a mapsquare is walkable from south.
149  *
150  *
151  * @return true if the mapsquare is walkable from south, false otherwise.
152  */
153  bool is_walkable_south () const
154  {
155  return walkable & WALKABLE_SOUTH;
156  }
157 
158  /**
159  * Sets the reachability from west of a mapsquare.
160  *
161  * @param w true if the mapsquare should be reachable from west, false otherwise.
162  */
163  void set_walkable_west (bool w)
164  {
165  if (!w)
166  walkable &= (ALL_WALKABLE - WALKABLE_WEST);
167  else
168  walkable |= WALKABLE_WEST;
169  }
170 
171  /**
172  * Sets the reachability from east of a mapsquare.
173  *
174  * @param w true if the mapsquare should be reachable from east, false otherwise.
175  */
176  void set_walkable_east (bool w)
177  {
178  if (!w)
179  walkable &= (ALL_WALKABLE - WALKABLE_EAST);
180  else
181  walkable |= WALKABLE_EAST;
182  }
183 
184  /**
185  * Sets the reachability from north of a mapsquare.
186  *
187  * @param w true if the mapsquare should be reachable from north, false otherwise.
188  */
189  void set_walkable_north (bool w)
190  {
191  if (!w)
192  walkable &= (ALL_WALKABLE - WALKABLE_NORTH);
193  else
194  walkable |= WALKABLE_NORTH;
195  }
196 
197  /**
198  * Sets the reachability from south of a mapsquare.
199  *
200  * @param w true if the mapsquare should be reachable from south, false otherwise.
201  */
202  void set_walkable_south (bool w)
203  {
204  if (!w)
205  walkable &= (ALL_WALKABLE - WALKABLE_SOUTH);
206  else
207  walkable |= WALKABLE_SOUTH;
208  }
209 
210  /**
211  * Gets the raw walkable parameter of a mapsquare.
212  *
213  * @return walkable parameter of this mapsquare.
214  */
216  {
217  return walkable;
218  }
219 
220  /**
221  * Sets the walkable parameter of a mapsquare.
222  *
223  * @param w new walkable status.
224  */
226  {
227  walkable = w;
228  }
229 
230 private:
231  u_int8 walkable;
232 };
233 
234 
235 /**
236  * Area of mapsquare_walkables, for use with mapcharacter and mapobject classes.
237  *
238  */
240 {
241 public:
242  /**
243  * Default constructor.
244  *
245  */
247 
248  /**
249  * Destructor.
250  *
251  */
253 
254  /**
255  * Totally clears the area.
256  *
257  */
258  void clear ();
259 
260  virtual void draw (s_int16 x, s_int16 y, const drawing_area * da_opt = NULL,
261  surface * target = NULL) const = 0;
262 
263  /**
264  * @name Area settings.
265  *
266  */
267  //@{
268 
269  /**
270  * Returns the length of the area.
271  *
272  * @return length (in number of squares) of the area.
273  *
274  */
276  {
277  return area.size ();
278  }
279 
280  /**
281  * Returns the height of the area.
282  *
283  * @return height (in number of squares) of the area.
284  *
285  */
287  {
288  if (area.size ()) return area[0].size ();
289  else return 0;
290  }
291 
292  /**
293  * Returns a pointer to a desired square.
294  *
295  * @param x X position of the square to get.
296  * @param y Y position of the square to get.
297  *
298  * @return pointer to the (x,y) square.
299  */
301  {
302  return &(area[x][y]);
303  }
304 
305  /**
306  * Resize the area.
307  *
308  * @param nl new length (in number of squares) of the area.
309  * @param nh new height (in number of squares) of the area.
310  */
311  void resize_area (u_int16 nl, u_int16 nh);
312 
313  //@}
314 
315 
316  /**
317  * @name Base square settings.
318  *
319  */
320  //@{
321 
322  /**
323  * Returns the X offset of the base square of this object.
324  *
325  *
326  * @return X offset of the base square.
327  */
328  u_int16 base_x () const
329  {
330  return basex;
331  }
332 
333  /**
334  * Returns the Y offset of the base square of this object.
335  *
336  *
337  * @return Y offset of the base square.
338  */
339  u_int16 base_y () const
340  {
341  return basey;
342  }
343 
344  /**
345  * Sets the base square of this object.
346  *
347  * @param nx X offset of the new base square.
348  * @param ny Y offset of the new base square.
349  */
350  void set_base (u_int16 nx, u_int16 ny);
351 
352  //@}
353 
354  /**
355  * Loads an area from an opened file.
356  * @param file the opened file from which to load.
357  * @return 0 in case of success, error code otherwise.
358  *
359  */
360  s_int8 get (igzstream & file);
361 
362  /**
363  * Saves an area into an opened file.
364  * @param file the opened file where to write.
365  * @return 0 in case of success, error code otherwise.
366  *
367  */
368  s_int8 put (ogzstream & file) const;
369 
370 #ifndef SWIG
371  /**
372  * Area copy (similar to copy ()).
373  *
374  * @attention Not available from Python. Use copy () from Python instead.
375  * @sa copy ()
376  */
378 #endif
379 
380  /**
381  * Synonym of operator = to guarantee its access from Python.
382  *
383  * @sa operator =
384  */
385  void copy (const mapsquare_walkable_area& src)
386  {
387  *this = src;
388  }
389 
390 private:
391  /**
392  * Forbids value passing.
393  *
394  */
396 
397  mutable vector <vector<mapsquare_walkable> > area;
398 
399  u_int16 basex;
400  u_int16 basey;
401 };
402 
403 #endif
mapsquare_walkable_area::~mapsquare_walkable_area
~mapsquare_walkable_area()
Destructor.
Definition: mapsquare_walkable.cc:53
mapsquare_walkable::is_walkable_south
bool is_walkable_south() const
Returns whether a mapsquare is walkable from south.
Definition: mapsquare_walkable.h:153
mapsquare_walkable::mapsquare_walkable
mapsquare_walkable()
Default constructor.
Definition: mapsquare_walkable.cc:30
surface
Class where drawables can actually be drawn to.
Definition: surface.h:85
mapsquare_walkable_area::draw
virtual void draw(s_int16 x, s_int16 y, const drawing_area *da_opt=NULL, surface *target=NULL) const =0
Draw the object on the screen.
igzstream
Class to read data from a Gzip compressed file.
Definition: fileops.h:135
mapsquare_walkable::get_walkable
u_int8 get_walkable() const
Gets the raw walkable parameter of a mapsquare.
Definition: mapsquare_walkable.h:215
mapsquare_walkable_area::copy
void copy(const mapsquare_walkable_area &src)
Synonym of operator = to guarantee its access from Python.
Definition: mapsquare_walkable.h:385
drawable
Abstract class for drawable objects manipulation.
Definition: drawable.h:59
mapsquare_walkable::set_walkable_south
void set_walkable_south(bool w)
Sets the reachability from south of a mapsquare.
Definition: mapsquare_walkable.h:202
ogzstream
Class to write data from a Gzip compressed file.
Definition: fileops.h:227
u_int8
#define u_int8
8 bits long unsigned integer
Definition: types.h:35
s_int16
#define s_int16
16 bits long signed integer
Definition: types.h:47
fileops.h
Declares the igzstream, ogzstream and fileops classes.
mapsquare_walkable::is_walkable_north
bool is_walkable_north() const
Returns whether a mapsquare is walkable from north.
Definition: mapsquare_walkable.h:142
ALL_WALKABLE
#define ALL_WALKABLE
Walkable from every side.
Definition: mapsquare_walkable.h:48
mapsquare_walkable::is_walkable_east
bool is_walkable_east() const
Returns whether a mapsquare is walkable from east.
Definition: mapsquare_walkable.h:131
WALKABLE_SOUTH
#define WALKABLE_SOUTH
Walkable from South.
Definition: mapsquare_walkable.h:54
mapsquare_walkable_area::base_x
u_int16 base_x() const
Returns the X offset of the base square of this object.
Definition: mapsquare_walkable.h:328
s_int8
#define s_int8
8 bits long signed integer
Definition: types.h:44
mapsquare_walkable::set_walkable_east
void set_walkable_east(bool w)
Sets the reachability from east of a mapsquare.
Definition: mapsquare_walkable.h:176
mapsquare_walkable_area::get
s_int8 get(igzstream &file)
Loads an area from an opened file.
Definition: mapsquare_walkable.cc:65
mapsquare_walkable::get
s_int8 get(igzstream &file)
Loads a mapsquare_walkable from an opened file.
Definition: mapsquare_walkable.cc:35
mapsquare_walkable_area::get_square
mapsquare_walkable * get_square(u_int16 x, u_int16 y) const
Returns a pointer to a desired square.
Definition: mapsquare_walkable.h:300
WALKABLE_WEST
#define WALKABLE_WEST
Walkable from West.
Definition: mapsquare_walkable.h:72
mapsquare_walkable::is_walkable_west
bool is_walkable_west() const
Returns whether a mapsquare is walkable from west.
Definition: mapsquare_walkable.h:120
mapsquare_walkable_area::resize_area
void resize_area(u_int16 nl, u_int16 nh)
Resize the area.
Definition: mapsquare_walkable.cc:111
drawing_area
Implements "drawing zones" for drawing operations.
Definition: drawing_area.h:54
mapsquare_walkable_area::set_base
void set_base(u_int16 nx, u_int16 ny)
Sets the base square of this object.
Definition: mapsquare_walkable.cc:123
mapsquare_walkable::set_walkable
void set_walkable(u_int8 w)
Sets the walkable parameter of a mapsquare.
Definition: mapsquare_walkable.h:225
MAPSQUARE_SIZE
const u_int16 MAPSQUARE_SIZE
Size of a mapsquare (in pixels).
Definition: mapsquare_walkable.h:42
WALKABLE_NORTH
#define WALKABLE_NORTH
Walkable from North.
Definition: mapsquare_walkable.h:60
mapsquare_walkable_area::operator=
mapsquare_walkable_area & operator=(const mapsquare_walkable_area &mo)
Area copy (similar to copy ()).
Definition: mapsquare_walkable.cc:129
drawable.h
Declares the drawable class.
mapsquare_walkable::put
s_int8 put(ogzstream &file) const
Puts a mapsquare_walkable into an opened file.
Definition: mapsquare_walkable.cc:41
u_int16
#define u_int16
16 bits long unsigned integer
Definition: types.h:38
mapsquare_walkable_area::area_length
u_int16 area_length() const
Returns the length of the area.
Definition: mapsquare_walkable.h:275
mapsquare_walkable_area::base_y
u_int16 base_y() const
Returns the Y offset of the base square of this object.
Definition: mapsquare_walkable.h:339
mapsquare_walkable::set_walkable_west
void set_walkable_west(bool w)
Sets the reachability from west of a mapsquare.
Definition: mapsquare_walkable.h:163
mapsquare_walkable::set_walkable_north
void set_walkable_north(bool w)
Sets the reachability from north of a mapsquare.
Definition: mapsquare_walkable.h:189
WALKABLE_EAST
#define WALKABLE_EAST
Walkable from East.
Definition: mapsquare_walkable.h:66
mapsquare_walkable_area::area_height
u_int16 area_height() const
Returns the height of the area.
Definition: mapsquare_walkable.h:286
mapsquare_walkable_area::clear
void clear()
Totally clears the area.
Definition: mapsquare_walkable.cc:57
mapsquare_walkable_area
Area of mapsquare_walkables, for use with mapcharacter and mapobject classes.
Definition: mapsquare_walkable.h:239
mapsquare_walkable_area::mapsquare_walkable_area
mapsquare_walkable_area()
Default constructor.
Definition: mapsquare_walkable.cc:47
mapsquare_walkable_area::put
s_int8 put(ogzstream &file) const
Saves an area into an opened file.
Definition: mapsquare_walkable.cc:90
mapsquare_walkable
Contains information about the walkability of a mapsquare.
Definition: mapsquare_walkable.h:86