forked from rtpHarry/Sokoban
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SokobanActiveLevel.cpp
336 lines (272 loc) · 9.22 KB
/
SokobanActiveLevel.cpp
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include "SokobanActiveLevel.h"
#include <assert.h>
//#include <vector>
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
SokobanActiveLevel::SokobanActiveLevel(void)
{
// set default values
Reset();
}
SokobanActiveLevel::~SokobanActiveLevel(void)
{
}
void SokobanActiveLevel::Reset(void)
{
m_WallData.clear();
m_ItemList.clear();
m_Player.ItemType = SYMBOL_PLAYER;
m_Player.ItemLocation.x = 0;
m_Player.ItemLocation.y = 0;
m_LevelDimensions.x = 0;
m_LevelDimensions.y = 0;
m_TargetQuota = 0;
m_TargetsTagged = 0;
m_MoveList.Reset();
}
// Replace all data with a new level and reset move list
void SokobanActiveLevel::Reset(SokobanActiveLevel NewLevel)
{
// clear current data
Reset();
// apply new
m_WallData = NewLevel.m_WallData;
m_ItemList = NewLevel.m_ItemList;
m_Player = NewLevel.m_Player;
m_LevelDimensions = NewLevel.m_LevelDimensions;
m_TargetQuota = NewLevel.m_TargetQuota;
m_TargetsTagged = NewLevel.m_TargetsTagged;
m_MoveList = NewLevel.m_MoveList;
}
void SokobanActiveLevel::_ExtractLevelItems(EncodedLevel NewLevel)
{
// reset data
Reset();
// extract item locations
///////////////////////////////
string tmpLevelDataLine;
LevelItem tmpItem;
unsigned int tmpLongestLine = 0; // longest line so far (width of level at end)
for (unsigned int outer = 0; outer < NewLevel.m_EncodedData.size(); outer++)
{
tmpLevelDataLine = NewLevel.m_EncodedData[outer];
// loop through current line
for (unsigned int inner = 0; inner < tmpLevelDataLine.length(); inner++)
{
// calculate level width
//////////////////////////////////////////////////////////////////////////
if(tmpLevelDataLine.length() > tmpLongestLine)
{
tmpLongestLine = tmpLevelDataLine.length();
}
// look for a player symbol
//////////////////////////////////////////////////
if (tmpLevelDataLine[inner] == SYMBOL_PLAYER ||
tmpLevelDataLine[inner] == SYMBOL_PLAYER_ON_TARGET)
{
m_Player.ItemLocation.x = outer;
m_Player.ItemLocation.y = inner;
}
// look for all crate symbols
//////////////////////////////////////////////////
if (tmpLevelDataLine[inner] == SYMBOL_CRATE ||
tmpLevelDataLine[inner] == SYMBOL_CRATE_ON_TARGET)
{
// add item to list
tmpItem.ItemType = SYMBOL_CRATE;
tmpItem.ItemLocation.x = outer;
tmpItem.ItemLocation.y = inner;
m_ItemList.push_back (tmpItem);
}
// look for all target symbols
//////////////////////////////////////////////////
if (tmpLevelDataLine[inner] == SYMBOL_TARGET ||
tmpLevelDataLine[inner] == SYMBOL_CRATE_ON_TARGET ||
tmpLevelDataLine[inner] == SYMBOL_PLAYER_ON_TARGET)
{
// add item to list
tmpItem.ItemType = SYMBOL_TARGET;
tmpItem.ItemLocation.x = outer;
tmpItem.ItemLocation.y = inner;
m_ItemList.push_back (tmpItem);
// increase taret quota
m_TargetQuota++;
// tagged targets
if (tmpLevelDataLine[inner] == SYMBOL_CRATE_ON_TARGET)
m_TargetsTagged++;
}
}
}
// build wall list
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
for (unsigned int outer = 0; outer < NewLevel.m_EncodedData.size(); outer++)
{
tmpLevelDataLine = NewLevel.m_EncodedData[outer];
// loop through current line
for (unsigned int inner = 0; inner < tmpLevelDataLine.length(); inner++)
{
// discard any non wall/floor/void items
//////////////////////////////////////////////////
if (tmpLevelDataLine[inner] == SYMBOL_PLAYER ||
tmpLevelDataLine[inner] == SYMBOL_PLAYER_ON_TARGET ||
tmpLevelDataLine[inner] == SYMBOL_CRATE ||
tmpLevelDataLine[inner] == SYMBOL_CRATE_ON_TARGET ||
tmpLevelDataLine[inner] == SYMBOL_TARGET)
{
// clear item
tmpLevelDataLine[inner] = SYMBOL_FLOOR;
}
}
// add clean line to wall list
m_WallData.push_back (tmpLevelDataLine);
}
// save max level width
m_LevelDimensions.x = tmpLongestLine - 1;
// save max level height
m_LevelDimensions.y = NewLevel.m_EncodedData.size() - 1;
// done
return;
}
// extracts a list of all items in the level of type ItemType
//////////////////////////////////////////////////////////////////////////
LevelItemList SokobanActiveLevel::GetMatchingItems(char ItemType)
{
LevelItemList FilteredItemList;
// loop all items in level
for (unsigned int indx = 0; indx < m_ItemList.size(); indx++)
{
// if find matching item type
if (m_ItemList[indx].ItemType == ItemType)
{
// add it to the filtered list
FilteredItemList.push_back( m_ItemList[indx] );
}
}
// return the filtered list
return FilteredItemList;
}
// extracts a list of all items located at SourceItemLocation
//////////////////////////////////////////////////////////////////////////
LevelItemList SokobanActiveLevel::GetItemsAtLocation(Vector2 SourceItemLocation)
{
LevelItemList FilteredItemList;
// loop all items in level
for (unsigned int indx = 0; indx < m_ItemList.size(); indx++)
{
// if find matching item type
if (m_ItemList[indx].ItemLocation == SourceItemLocation)
{
// add it to the filtered list
FilteredItemList.push_back( m_ItemList[indx] );
}
}
// also check architecture
LevelItem tmpArchitecture;
tmpArchitecture.ItemType = m_WallData[SourceItemLocation.x][SourceItemLocation.y];
tmpArchitecture.ItemLocation = SourceItemLocation;
FilteredItemList.push_back(tmpArchitecture);
// return the filtered list
return FilteredItemList;
}
void SokobanActiveLevel::ChangeItem(LevelItem ItemToMatch, LevelItem NewItemValues)
{
// loop all items in filtered list
for (unsigned int indx = 0; indx < m_ItemList.size(); indx++)
{
// if find matching item type
if (m_ItemList[indx] == ItemToMatch)
{
// update crate location
m_ItemList[indx] = NewItemValues;
}
}
}
// Perform the operation in MoveEvent on the gameboard in Level
void SokobanActiveLevel::ExecuteMoveEvent(const MoveEvent Event)
{
// add move to queue
m_MoveList.AddEvent(Event);
// get all locations
EventLocation Location1, Location2;
Location1 = GetOffsetLocation(Event, 1);
Location2 = GetOffsetLocation(Event, 2);
switch(Event.Type)
{
case MOVE_EVENT_NOEVENT: // empty event (for when last event popped off stack)
// EVENT_NOEVENT should not occur!
break;
case MOVE_EVENT_STEP: // normal player move
m_Player.ItemLocation = Location1;
break;
case MOVE_EVENT_BUMPCRATE: // player walks into crate that wont move
// do nothing
break;
case MOVE_EVENT_BUMPWALL: // player walks into wall
// do nothing
break;
case MOVE_EVENT_PUSHTOEMPTY: // player push crate
case MOVE_EVENT_PUSHTOWALL: // player push crate and next square is a wall (for sound of crate hitting wall)
case MOVE_EVENT_PUSHTOCRATE: // player push crate and next square is a crate (for sound of crate hitting crate)
case MOVE_EVENT_PUSHBETWEENTARGETS: // player pushed crate from one target straight onto another
ChangeItem(LevelItem(SYMBOL_CRATE, Location1), LevelItem(SYMBOL_CRATE, Location2));
m_Player.ItemLocation = Location1;
break;
case MOVE_EVENT_PUSHTOTARGET: // player push crate onto target
ChangeItem(LevelItem(SYMBOL_CRATE, Location1), LevelItem(SYMBOL_CRATE, Location2));
m_Player.ItemLocation = Location1;
m_TargetsTagged += 1;
break;
case MOVE_EVENT_PUSHFROMTARGET: // player push crate out of target
ChangeItem(LevelItem(SYMBOL_CRATE, Location1), LevelItem(SYMBOL_CRATE, Location2));
m_Player.ItemLocation = Location1;
m_TargetsTagged -= 1;
break;
}
}
// Undo the operation in MoveEvent on the gameboard in Level
void SokobanActiveLevel::ReverseMoveEvent(const MoveEvent Event)
{
// get all locations
EventLocation Location0, Location1, Location2;
Location0 = Event.Location;
Location1 = GetOffsetLocation(Event, 1);
Location2 = GetOffsetLocation(Event, 2);
// check that this IS the last move made
// works but fails on empty queue because location is 0,0
// assert(m_Player.ItemLocation == Location1);
switch(Event.Type)
{
case MOVE_EVENT_NOEVENT: // empty event (for when last event popped off stack)
// EVENT_NOEVENT should not occur!
break;
case MOVE_EVENT_STEP: // normal player move
m_Player.ItemLocation = Location0;
break;
case MOVE_EVENT_BUMPCRATE: // player walks into crate that wont move
// do nothing
break;
case MOVE_EVENT_BUMPWALL: // player walks into wall
// do nothing
break;
case MOVE_EVENT_PUSHTOEMPTY: // player push crate
case MOVE_EVENT_PUSHTOWALL: // player push crate and next square is a wall (for sound of crate hitting wall)
case MOVE_EVENT_PUSHTOCRATE: // player push crate and next square is a crate (for sound of crate hitting crate)
case MOVE_EVENT_PUSHBETWEENTARGETS: // player pushed crate from one target straight onto another
ChangeItem(LevelItem(SYMBOL_CRATE, Location2), LevelItem(SYMBOL_CRATE, Location1));
m_Player.ItemLocation = Location0;
break;
case MOVE_EVENT_PUSHTOTARGET: // player push crate onto target
ChangeItem(LevelItem(SYMBOL_CRATE, Location2), LevelItem(SYMBOL_CRATE, Location1));
m_Player.ItemLocation = Location0;
m_TargetsTagged -= 1;
break;
case MOVE_EVENT_PUSHFROMTARGET: // player push crate out of target
ChangeItem(LevelItem(SYMBOL_CRATE, Location2), LevelItem(SYMBOL_CRATE, Location1));
m_Player.ItemLocation = Location0;
m_TargetsTagged += 1;
break;
}
}