File indexing completed on 2021-12-21 12:50:21

0001 /*
0002  *  ksokoban - a Sokoban game by KDE
0003  *  Copyright (C) 1998  Anders Widell  <d95-awi@nada.kth.se>
0004  *
0005  *  This program is free software; you can redistribute it and/or modify
0006  *  it under the terms of the GNU General Public License as published by
0007  *  the Free Software Foundation; either version 2 of the License, or
0008  *  (at your option) any later version.
0009  *
0010  *  This program is distributed in the hope that it will be useful,
0011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  *  GNU General Public License for more details.
0014  *
0015  *  You should have received a copy of the GNU General Public License
0016  *  along with this program; if not, write to the Free Software
0017  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
0018  */
0019 
0020 #ifndef MAP_H
0021 #define MAP_H
0022 
0023 #include <assert.h>
0024 
0025 #define MAX_X 49
0026 #define MAX_Y 49
0027 
0028 #define WALL   1
0029 #define GOAL   2
0030 #define OBJECT 4
0031 #define FLOOR  8
0032 
0033 class Map {
0034   friend class MapDelta;
0035   friend class LevelCollection;
0036 public:
0037   Map();
0038 
0039   bool completed () const { return objectsLeft_ <= 0; }
0040 
0041   bool step   (int _x, int _y);
0042   bool push   (int _x, int _y);
0043   bool unstep (int _x, int _y);
0044   bool unpush (int _x, int _y);
0045 
0046 
0047   static bool badCoords (int _x, int _y) {
0048     return _x<0 || _y<0 || _x>MAX_X || _y>MAX_Y;
0049   }
0050 
0051   static bool badDelta (int _xd, int _yd) {
0052     return (_xd!=0 && _yd!=0) || (_xd==0 && _yd==0);
0053   }
0054 
0055   int xpos () const { return xpos_; }
0056   int ypos () const { return ypos_; }
0057 
0058   bool empty  (int x, int y) {
0059     assert (x>=0 && x<=MAX_X && y>=0 && y<=MAX_Y);
0060     return (currentMap_[y+1][x+1] & (WALL|OBJECT)) == 0;
0061   }
0062   bool wall   (int x, int y) {
0063     assert (x>=0 && x<=MAX_X && y>=0 && y<=MAX_Y);
0064     return (currentMap_[y+1][x+1] & WALL) != 0;
0065   }
0066   bool goal   (int x, int y) {
0067     assert (x>=0 && x<=MAX_X && y>=0 && y<=MAX_Y);
0068     return (currentMap_[y+1][x+1] & GOAL) != 0;
0069   }
0070   bool object (int x, int y) {
0071     assert (x>=0 && x<=MAX_X && y>=0 && y<=MAX_Y);
0072     return (currentMap_[y+1][x+1] & OBJECT) != 0;
0073   }
0074   bool floor (int x, int y) {
0075     assert (x>=0 && x<=MAX_X && y>=0 && y<=MAX_Y);
0076     return (currentMap_[y+1][x+1] & FLOOR) != 0;
0077   }
0078 
0079   bool wallUp    (int x, int y) {
0080     assert (x>=0 && x<=MAX_X && y>=0 && y<=MAX_Y);
0081     return (currentMap_[y  ][x+1] & WALL) != 0;
0082   }
0083   bool wallDown  (int x, int y) {
0084     assert (x>=0 && x<=MAX_X && y>=0 && y<=MAX_Y);
0085     return (currentMap_[y+2][x+1] & WALL) != 0;
0086   }
0087   bool wallLeft  (int x, int y) {
0088     assert (x>=0 && x<=MAX_X && y>=0 && y<=MAX_Y);
0089     return (currentMap_[y+1][x  ] & WALL) != 0;
0090   }
0091   bool wallRight (int x, int y) {
0092     assert (x>=0 && x<=MAX_X && y>=0 && y<=MAX_Y);
0093     return (currentMap_[y+1][x+2] & WALL) != 0;
0094   }
0095 
0096   void   printMap ();
0097 
0098   int width() const { return width_; }
0099   int height() const { return height_; }
0100 
0101 protected:
0102   int map (int x, int y) {
0103     assert (x>=0 && x<=MAX_X && y>=0 && y<=MAX_Y);
0104     return currentMap_[y+1][x+1];
0105   }
0106   void map (int x, int y, int val);
0107 
0108   void setMap (int x, int y, int bits);
0109   void clearMap (int x, int y, int bits);
0110   void clearMap ();
0111   bool fillFloor (int x, int y);
0112   int objectsLeft () const { return objectsLeft_; }
0113 
0114   int xpos_, ypos_;
0115 
0116 private:
0117   char currentMap_[MAX_Y+3][MAX_X+3];
0118   int width_, height_;
0119   int objectsLeft_;
0120 };
0121 
0122 #endif  /* MAP_H */