summaryrefslogtreecommitdiffstats
path: root/ksokoban/Move.h
blob: 9c24a5f96c58fdb29f1d96b845d29f221f8dc8e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 *  ksokoban - a Sokoban game for TDE
 *  Copyright (C) 1998  Anders Widell  <d95-awi@nada.kth.se>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifndef MOVE_H
#define MOVE_H

#include <assert.h>
#include <tqstring.h>

#include "Map.h"
class LevelMap;

/**
 * Holds information about a move
 *
 * The move can consist of several atomic steps and pushes. An atomic
 * step/push is a step/push along a straight line. The reason why these are
 * grouped together in a Move object is that they belong to the same logical
 * move in the player's point of view. An undo/redo will undo/redo all the
 * atomic moves in one step.
 *
 * @short   Maintains game movement move
 * @author  Anders Widell <d95-awi@nada.kth.se>
 * @version 0.1
 * @see     History
 */

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 () const { return moves_[0]&0x7f; }
  int startY () const { return (moves_[0]>>8)&0x7f; }
  int finalX () const { return moves_[moveIndex_-1]&0x7f; }
  int finalY () const { return (moves_[moveIndex_-1]>>8)&0x7f; }


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

#endif  /* MOVE_H */