summaryrefslogtreecommitdiffstats
path: root/ksokoban/Move.cpp
blob: dd61df40965c92d8e7eef699f970c3b99b99f258 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
 *  ksokoban - a Sokoban game for TDE
 *  Copyright (C) 1998  Anders Widell  <[email protected]>
 *
 *  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
 */

#include <assert.h>
#include <string.h>
#include <ctype.h>

#include "Move.h"
#include "LevelMap.h"

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
}

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

void
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
Move::save (TQString &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 *
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
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
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;
}