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

Class Index

ksokoban'Move (./kdegames/ksokoban/Move.H:44)

class Move {
  friend class MoveSequence;
private:
  unsigned short *moves_;
  int moveIndex_;
  bool finished_;

#ifndef NDEBUG
  int lastX_, lastY_;
#endif


public:
  Move (int _startX, int _startY);
  ~Move ();

  /**
   * Add an atomic move.
   * NOTE: either (x != (previous x)) or (y != (previous y))
   * must be true (but not both).
   *
   * @see LevelMap#move
   *
   * @param x  x position of destination
   * @param y  y position of destination
   */
  void step (int _x, int _y) {
#ifndef NDEBUG
    assert (!finished_);
    assert (_x>=0 && _x<=MAX_X && _y>=0 && _y<=MAX_Y);
    assert (moveIndex_ < 400);
    assert ((_x!=lastX_ && _y==lastY_) || (_x==lastX_ && _y!=lastY_));
    lastX_ = _x;
    lastY_ = _y;
#endif

    moves_[moveIndex_++] = _x | (_y<<8);
  }

  /**
   * Same as move above, but used when an object is pushed.
   *
   * @see LevelMap#push
   */
  void push (int _x, int _y) {
#ifndef NDEBUG
    assert (!finished_);
    assert (_x>=0 && _x<=MAX_X && _y>=0 && _y<=MAX_Y);
    assert (moveIndex_ < 400);
    assert ((_x!=lastX_ && _y==lastY_) || (_x==lastX_ && _y!=lastY_));
    lastX_ = _x;
    lastY_ = _y;
#endif

    moves_[moveIndex_++] = _x | (_y<<8) | 0x80;
  }

  void finish ();

  int startX () { return moves_[0]&0x7f; }
  int startY () { return (moves_[0]>>8)&0x7f; }
  int finalX () { return moves_[moveIndex_-1]&0x7f; }
  int finalY () { return (moves_[moveIndex_-1]>>8)&0x7f; }


  void save (QString &_str);
  const char *load (const char *_str);
  bool redo (LevelMap *map);
  bool undo (LevelMap *map);
};

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

Move::Move (int _startX, int _startY) {
  assert (_startX>=0 && _startX<=MAX_X && _startY>=0 && _startY<=MAX_Y);

  moves_ = new unsigned short[400];
  moves_[0] = _startX | (_startY<<8);
  moveIndex_ = 1;
  finished_ = false;

#ifndef NDEBUG
  lastX_ = _startX;
  lastY_ = _startY;
#endif
}


ksokoban'Move::~Move() (./kdegames/ksokoban/Move.C:41)

Move::~Move () {
  delete [] moves_;
}

void

ksokoban'Move::finish() (./kdegames/ksokoban/Move.C:46)

Move::finish () {
  assert (!finished_);
  assert (moveIndex_ > 1);

  unsigned short *newMoves = new unsigned short[moveIndex_];
  memcpy (newMoves, moves_, moveIndex_*sizeof (unsigned short));
  delete [] moves_;
  moves_ = newMoves;

  finished_ = true;
}

void

ksokoban'Move::save() (./kdegames/ksokoban/Move.C:59)

Move::save (QString &s) {
  static const char move1[] = "lrud";
  static const char push1[] = "LRUD";
  static const char move2[] = "wens";
  static const char push2[] = "WENS";

  assert (finished_);
  int x=startX ();
  int y=startY ();
  int pos=1;

  int x2, y2, dist=0;
  int dir=-1;
  bool push=false;
  while (pos<moveIndex_) {
    if (dir >= 0) s += push ? push1[dir] : move1[dir];

    x2 = moves_[pos]&0x7f;
    y2 = (moves_[pos]>>8)&0x7f;
    push = (moves_[pos++]&0x80)==0x80;

    if (x2<x) {
      dir = 0;
      dist = x-x2;
    } else if (x2>x) {
      dir = 1;
      dist = x2-x;
    } else if (y2<y) {
      dir = 2;
      dist = y-y2;
    } else if (y2>y) {
      dir = 3;
      dist = y2-y;
    } else {
      assert (0);
    }
    assert (dist > 0);

    if (dist > 1) {
      if (dist>=10) {
	s += '0' + (dist/10);
	dist %= 10;
      }
      s += '0' + dist;
    }

    x = x2;
    y = y2;
  }

  if (dir >= 0) s += push ? push2[dir] : move2[dir];
}

const char *

ksokoban'Move::load() (./kdegames/ksokoban/Move.C:113)

Move::load (const char *s) {
  assert (!finished_);
  int x=finalX ();
  int y=finalY ();

  int dist;
  bool last=false;
  char c;
  while ((c = *s++) != '\0') {
    dist = 1;
    if (c >= '0' && c <= '9') {
      dist = c - '0';
      c = *s++;
      if (c >= '0' && c <= '9') {
	dist = 10*dist + c - '0';
	c = *s++;
      }
    }

    switch (tolower (c)) {
    case 'w':
      last = true;
    case 'l':
      x -= dist;
      break;
    case 'e':
      last = true;
    case 'r':
      x += dist;
      break;
    case 'n':
      last = true;
    case 'u':
      y -= dist;
      break;
    case 's':
      last = true;
    case 'd':
      y += dist;
      break;

    default:
      //printf ("2><>%s\n", s);
      //abort ();
      return 0;
    }

    if (x<=0 || x>=MAX_X || y<=0 || y>=MAX_Y) {
      //printf ("x: %d, y:%d ><>%s\n", x, y, s);
      //abort ();

      return 0;
    }

    if (isupper (c)) push (x, y);
    else step (x, y);

    if (last) break;
  }
  finish ();

  return s;
}

bool

ksokoban'Move::redo() (./kdegames/ksokoban/Move.C:178)

Move::redo (LevelMap *map) {
  assert (finished_);

  for (int pos=1; pos<moveIndex_; pos++) {
    int x = moves_[pos]&0x7f;
    int y = (moves_[pos]>>8)&0x7f;
    bool push = (moves_[pos]&0x80)==0x80;
    bool ret;

    if (push) ret = map->push (x, y);
    else ret = map->step (x, y);

    if (!ret) return false;
  }

  return true;
}

bool

ksokoban'Move::undo() (./kdegames/ksokoban/Move.C:197)

Move::undo (LevelMap *map) {
  assert (finished_);

  for (int pos=moveIndex_-2; pos>=0; --pos) {
    int x = moves_[pos]&0x7f;
    int y = (moves_[pos]>>8)&0x7f;
    bool push = (moves_[pos+1]&0x80)==0x80;
    bool ret;

    if (push) ret = map->unpush (x, y);
    else ret = map->unstep (x, y);

    if (!ret) return false;
  }

  return true;
}