Source Code (Use browser search to find items of interest.)

Class Index

ksokoban'Map (./kdegames/ksokoban/Map.H:33)

class Map {
  friend class MapDelta;
  friend class LevelCollection;
public:
  Map();

  bool completed () { return objectsLeft_ <= 0; }

  bool step   (int _x, int _y);
  bool push   (int _x, int _y);
  bool unstep (int _x, int _y);
  bool unpush (int _x, int _y);


  static bool badCoords (int _x, int _y) {
    return _x<0 || _y<0 || _x>MAX_X || _y>MAX_Y;
  }

  static bool badDelta (int _xd, int _yd) {
    return (_xd!=0 && _yd!=0) || (_xd==0 && _yd==0);
  }

  int xpos () { return xpos_; }
  int ypos () { return ypos_; }

  bool empty  (int x, int y) {
    assert (x>=0 && x<=MAX_X && y>=0 && y<=MAX_Y);
    return (currentMap_[y+1][x+1] & (WALL|OBJECT)) == 0;
  }
  bool wall   (int x, int y) {
    assert (x>=0 && x<=MAX_X && y>=0 && y<=MAX_Y);
    return (currentMap_[y+1][x+1] & WALL) != 0;
  }
  bool goal   (int x, int y) {
    assert (x>=0 && x<=MAX_X && y>=0 && y<=MAX_Y);
    return (currentMap_[y+1][x+1] & GOAL) != 0;
  }
  bool object (int x, int y) {
    assert (x>=0 && x<=MAX_X && y>=0 && y<=MAX_Y);
    return (currentMap_[y+1][x+1] & OBJECT) != 0;
  }
  bool floor (int x, int y) {
    assert (x>=0 && x<=MAX_X && y>=0 && y<=MAX_Y);
    return (currentMap_[y+1][x+1] & FLOOR) != 0;
  }

  bool wallUp    (int x, int y) {
    assert (x>=0 && x<=MAX_X && y>=0 && y<=MAX_Y);
    return (currentMap_[y  ][x+1] & WALL) != 0;
  }
  bool wallDown  (int x, int y) {
    assert (x>=0 && x<=MAX_X && y>=0 && y<=MAX_Y);
    return (currentMap_[y+2][x+1] & WALL) != 0;
  }
  bool wallLeft  (int x, int y) {
    assert (x>=0 && x<=MAX_X && y>=0 && y<=MAX_Y);
    return (currentMap_[y+1][x  ] & WALL) != 0;
  }
  bool wallRight (int x, int y) {
    assert (x>=0 && x<=MAX_X && y>=0 && y<=MAX_Y);
    return (currentMap_[y+1][x+2] & WALL) != 0;
  }

  void   printMap ();

  int width() { return width_; }
  int height() { return height_; }

protected:
  int map (int x, int y) {
    assert (x>=0 && x<=MAX_X && y>=0 && y<=MAX_Y);
    return currentMap_[y+1][x+1];
  }
  void map (int x, int y, int val);

  void setMap (int x, int y, int bits);
  void clearMap (int x, int y, int bits);
  void clearMap ();
  bool fillFloor (int x, int y);
  int objectsLeft () { return objectsLeft_; }

  int xpos_, ypos_;

private:
  char currentMap_[MAX_Y+3][MAX_X+3];
  int width_, height_;
  int objectsLeft_;
};

ksokoban'Map::Map() (./kdegames/ksokoban/Map.C:27)

Map::Map() : xpos_(-1), ypos_(-1), width_(0), height_(0), objectsLeft_(-1) {
}


void

ksokoban'Map::map() (./kdegames/ksokoban/Map.C:32)

Map::map (int x, int y, int val) {
  assert (x>=0 && x<=MAX_X && y>=0 && y<=MAX_Y);
  if ((map (x, y) & (OBJECT | GOAL)) == OBJECT) objectsLeft_--;
  if ((val & (OBJECT | GOAL)) == OBJECT) objectsLeft_++;
  currentMap_[y+1][x+1] = val;

  if (val != 0) {
    if (width_ <= x) width_ = x+1;
    if (height_ <= y) height_ = y+1;
  }
}

void

ksokoban'Map::setMap() (./kdegames/ksokoban/Map.C:45)

Map::setMap (int x, int y, int bits) {
  assert ((map (x, y) & bits) == 0);
  if (goal (x, y) && ((bits & OBJECT) == OBJECT)) objectsLeft_--;
  assert (objectsLeft_ >= 0);
  currentMap_[y+1][x+1] |= bits;

  if (bits != 0) {
    if (width_ <= x) width_ = x+1;
    if (height_ <= y) height_ = y+1;
  }
}

void

ksokoban'Map::clearMap() (./kdegames/ksokoban/Map.C:58)

Map::clearMap (int x, int y, int bits) {
  assert ((map (x, y) & bits) == bits);
  if (goal (x, y) && ((bits & OBJECT) == OBJECT)) objectsLeft_++;
  currentMap_[y+1][x+1] &= ~bits;
}

void

ksokoban'Map::clearMap() (./kdegames/ksokoban/Map.C:65)

Map::clearMap () {
  memset (currentMap_, 0, (MAX_Y+3)*(MAX_X+3)*sizeof (char));
  objectsLeft_ = 0;
  width_ = height_ = 0;
}

bool

ksokoban'Map::fillFloor() (./kdegames/ksokoban/Map.C:72)

Map::fillFloor (int x, int y) {
  if (badCoords (x, y)) return false;
  if ((currentMap_[y+1][x+1] & (WALL|FLOOR)) != 0) return true;

  currentMap_[y+1][x+1] |= FLOOR;
  bool a = fillFloor (x, y-1);
  bool b = fillFloor (x, y+1);
  bool c = fillFloor (x-1, y);
  bool d = fillFloor (x+1, y);

  return a && b && c && d;
}

bool

ksokoban'Map::step() (./kdegames/ksokoban/Map.C:86)

Map::step (int _x, int _y) {
  assert (!badCoords (xpos_, ypos_));
  assert (empty (xpos_, ypos_));

  int xd=0, yd=0;
  if (_x < xpos_) xd = -1;
  if (_x > xpos_) xd =  1;
  if (_y < ypos_) yd = -1;
  if (_y > ypos_) yd =  1;
  if (badDelta (xd, yd) || badCoords (_x, _y)) return false;

  int x=xpos_, y=ypos_;
  do {
    x += xd;
    y += yd;
    if (!empty (x, y)) return false;
  } while (!(x==_x && y==_y));

  xpos_ = _x;
  ypos_ = _y;

  return true;
}

bool

ksokoban'Map::push() (./kdegames/ksokoban/Map.C:111)

Map::push (int _x, int _y) {
  assert (!badCoords (xpos_, ypos_));
  assert (empty (xpos_, ypos_));

  int xd=0, yd=0;
  if (_x < xpos_) xd = -1;
  if (_x > xpos_) xd =  1;
  if (_y < ypos_) yd = -1;
  if (_y > ypos_) yd =  1;
  if (badDelta (xd, yd) || badCoords (_x+xd, _y+yd)) return false;

  int x=xpos_+xd, y=ypos_+yd;
  if (!object (x, y)) return false;
  if (!empty (_x+xd, _y+yd)) return false;

  while (!(x==_x && y==_y)) {
    x += xd;
    y += yd;
    if (!empty (x, y)) return false;
  }

  clearMap (xpos_+xd, ypos_+yd, OBJECT);
  setMap (_x+xd, _y+yd, OBJECT);

  xpos_ = _x;
  ypos_ = _y;

  return true;
}

bool

ksokoban'Map::unstep() (./kdegames/ksokoban/Map.C:142)

Map::unstep (int _x, int _y) {
  return Map::step (_x, _y);
}

bool

ksokoban'Map::unpush() (./kdegames/ksokoban/Map.C:147)

Map::unpush (int _x, int _y) {
  assert (!badCoords (xpos_, ypos_));
  assert (empty (xpos_, ypos_));

  int xd=0, yd=0;
  if (_x < xpos_) xd = -1;
  if (_x > xpos_) xd =  1;
  if (_y < ypos_) yd = -1;
  if (_y > ypos_) yd =  1;
  if (badDelta (xd, yd) || badCoords (_x+xd, _y+yd)) return false;

  int x=xpos_, y=ypos_;
  if (!object (x-xd, y-yd)) return false;

  do {
    x += xd;
    y += yd;
    if (!empty (x, y)) return false;
  } while (!(x==_x && y==_y));

  clearMap (xpos_-xd, ypos_-yd, OBJECT);
  setMap (_x-xd, _y-yd, OBJECT);

  xpos_ = _x;
  ypos_ = _y;

  return true;
}

void

ksokoban'Map::printMap() (./kdegames/ksokoban/Map.C:177)

Map::printMap(void) {
  for (int y=0; y<height_; y++) {
    for (int x=0; x<width_; x++) {
      switch (map (x, y) & ~FLOOR) {
      case WALL:
	printf("#");
	break;
      case GOAL:
	printf("%c", x==xpos_ && y==ypos_ ? '+' : '.');
	break;
      case OBJECT:
	printf("$");
	break;
      case OBJECT|GOAL:
	printf("*");
	break;
      case 0:
	printf("%c", x==xpos_ && y==ypos_ ? '@' : ' ');
	break;
      default:
	printf("<%X>", map(x,y)&FLOOR);
	break;
      }
    }
    printf ("\n");
  }
}