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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* 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 "sherlock/map.h"
#include "sherlock/sherlock.h"
namespace Sherlock {
/**
* Load the data for the paths between locations on the map
*/
void MapPaths::load(int numLocations, Common::SeekableReadStream &s) {
_numLocations = numLocations;
_paths.resize(_numLocations * _numLocations);
for (int idx = 0; idx < (numLocations * numLocations); ++idx) {
Common::Array<byte> &path = _paths[idx];
int v;
do {
v = s.readByte();
path.push_back(v);
} while (v && v < 254);
}
}
/**
* Get the path between two locations on the map
*/
const byte *MapPaths::getPath(int srcLocation, int destLocation) {
return &_paths[srcLocation * _numLocations + destLocation][0];
}
/*----------------------------------------------------------------*/
Map::Map(SherlockEngine *vm): _vm(vm), _topLine(SHERLOCK_SCREEN_WIDTH, 12) {
_active = false;
_mapCursors = nullptr;
_shapes = nullptr;
_iconShapes = nullptr;
_point = 0;
_placesShown = false;
_cursorIndex = -1;
_drawMap = false;
_overPos = Common::Point(13000, 12600);
for (int idx = 0; idx < MAX_HOLMES_SEQUENCE; ++idx)
Common::fill(&_sequences[idx][0], &_sequences[idx][MAX_FRAME], 0);
loadData();
}
/**
* Loads the list of points for locations on the map for each scene
*/
void Map::loadPoints(int count, const int *xList, const int *yList, const int *transList) {
for (int idx = 0; idx < count; ++idx, ++xList, ++yList, ++transList) {
_points.push_back(MapEntry(*xList, *yList, *transList));
}
}
/**
* Load the sequence data for player icon animations
*/
void Map::loadSequences(int count, const byte *seq) {
for (int idx = 0; idx < count; ++idx, seq += MAX_FRAME)
Common::copy(seq, seq + MAX_FRAME, &_sequences[idx][0]);
}
/**
* Load data needed for the map
*/
void Map::loadData() {
// Load the list of location names
Common::SeekableReadStream *txtStream = _vm->_res->load("chess.txt");
char c;
while (txtStream->pos() < txtStream->size()) {
Common::String line;
while ((c = txtStream->readByte()) != '\0')
line += c;
_locationNames.push_back(line);
}
delete txtStream;
// Load the path data
Common::SeekableReadStream *pathStream = _vm->_res->load("chess.pth");
// Get routes between different locations on the map
_paths.load(31, *pathStream);
// Load in the co-ordinates that the paths refer to
_pathPoints.resize(208);
for (uint idx = 0; idx < _pathPoints.size(); ++idx) {
_pathPoints[idx].x = pathStream->readSint16LE();
_pathPoints[idx].y = pathStream->readSint16LE();
}
delete pathStream;
}
/**
* Show the map
*/
int Map::show() {
Events &events = *_vm->_events;
People &people = *_vm->_people;
Scene &scene = *_vm->_scene;
Screen &screen = *_vm->_screen;
Common::Point lDrawn(-1, -1);
bool changed = false, exitFlag = false;
_active = true;
// Set font and custom cursor for the map
int oldFont = screen.fontNumber();
screen.setFont(0);
// Load the entire map
ImageFile bigMap("bigmap.vgs");
screen.setPalette(bigMap._palette);
// Load need sprites
setupSprites();
screen._backBuffer1.blitFrom(bigMap[0], Common::Point(-_bigPos.x, -_bigPos.y));
screen._backBuffer1.blitFrom(bigMap[1], Common::Point(-_bigPos.x, SHERLOCK_SCREEN_HEIGHT - _bigPos.y));
screen._backBuffer1.blitFrom(bigMap[2], Common::Point(SHERLOCK_SCREEN_WIDTH - _bigPos.x, -_bigPos.y));
screen._backBuffer1.blitFrom(bigMap[3], Common::Point(SHERLOCK_SCREEN_WIDTH - _bigPos.x, SHERLOCK_SCREEN_HEIGHT - _bigPos.y));
_drawMap = true;
_point = -1;
people[AL]._position = _lDrawnPos = _overPos;
// Show place icons
showPlaces();
saveTopLine();
_placesShown = true;
// Keep looping until either a location is picked, or the game is ended
while (!_vm->shouldQuit() && !exitFlag) {
events.pollEventsAndWait();
events.setButtonState();
// Keyboard handling
if (events.kbHit()) {
Common::KeyState keyState = events.getKey();
if (keyState.keycode == Common::KEYCODE_RETURN || keyState.keycode == Common::KEYCODE_SPACE) {
// Both space and enter simulate a mouse release
events._pressed = false;
events._released = true;
events._oldButtons = 0;
}
}
// Ignore scrolling attempts until the screen is drawn
if (!_drawMap) {
Common::Point pt = events.mousePos();
// Check for vertical map scrolling
if ((pt.y > (SHERLOCK_SCREEN_HEIGHT - 10) && _bigPos.y < 200) || (pt.y < 10 && _bigPos.y > 0)) {
if (pt.y > (SHERLOCK_SCREEN_HEIGHT - 10))
_bigPos.y += 10;
else
_bigPos.y -= 10;
changed = true;
}
// Check for horizontal map scrolling
if ((pt.x > (SHERLOCK_SCREEN_WIDTH - 10) && _bigPos.x < 315) || (pt.x < 10 && _bigPos.x > 0)) {
if (pt.x > (SHERLOCK_SCREEN_WIDTH - 10))
_bigPos.x += 15;
else
_bigPos.x -= 15;
changed = true;
}
}
if (changed) {
// Map has scrolled, so redraw new map view
changed = false;
screen._backBuffer1.blitFrom(bigMap[0], Common::Point(-_bigPos.x, -_bigPos.y));
screen._backBuffer1.blitFrom(bigMap[1], Common::Point(-_bigPos.x, SHERLOCK_SCREEN_HEIGHT - _bigPos.y));
screen._backBuffer1.blitFrom(bigMap[2], Common::Point(SHERLOCK_SCREEN_WIDTH - _bigPos.x, -_bigPos.y));
screen._backBuffer1.blitFrom(bigMap[3], Common::Point(SHERLOCK_SCREEN_WIDTH - _bigPos.x, SHERLOCK_SCREEN_HEIGHT - _bigPos.y));
showPlaces();
_placesShown = false;
saveTopLine();
_savedPos.x = -1;
updateMap(true);
} else if (!_drawMap) {
if (!_placesShown) {
showPlaces();
_placesShown = true;
}
if (_cursorIndex == 0) {
Common::Point pt = events.mousePos();
highlightIcon(Common::Point(pt.x - 4 + _bigPos.x, pt.y + _bigPos.y));
}
updateMap(false);
}
if ((events._released || events._rightReleased) && _point != -1) {
if (people[AL]._walkCount == 0) {
people._walkDest = _points[_point] + Common::Point(4, 9);
scene._charPoint = _point;
// Start walking to selected location
walkTheStreets();
// Show wait cursor
_cursorIndex = 1;
events.setCursor((*_mapCursors)[_cursorIndex]);
}
}
// Check if a scene has beeen selected and we've finished "moving" to it
if (people[AL]._walkCount == 0) {
if (scene._charPoint >= 1 && scene._charPoint < (int)_points.size())
exitFlag = true;
}
if (_drawMap) {
_drawMap = false;
if (screen._fadeStyle)
screen.randomTransition();
else
screen.slamArea(0, 0, SHERLOCK_SCREEN_WIDTH, SHERLOCK_SCREEN_HEIGHT);
}
// Wait for a frame
events.wait(1);
}
freeSprites();
_overPos = people[AL]._position;
// Reset font and cursor
screen.setFont(oldFont);
events.setCursor(ARROW);
_active = false;
return scene._charPoint;
}
/**
* Load and initialize all the sprites that are needed for the map display
*/
void Map::setupSprites() {
Events &events = *_vm->_events;
People &people = *_vm->_people;
Scene &scene = *_vm->_scene;
typedef byte Sequences[16][MAX_FRAME];
_savedPos.x = -1;
_mapCursors = new ImageFile("omouse.vgs");
_cursorIndex = 0;
events.setCursor((*_mapCursors)[_cursorIndex]);
_shapes = new ImageFile("mapicon.vgs");
_iconShapes = new ImageFile("overicon.vgs");
Person &p = people[AL];
p._description = " ";
p._type = CHARACTER;
p._position = Common::Point(12400, 5000);
p._sequenceNumber = 0;
p._sequences = &_sequences;
p._images = _shapes;
p._imageFrame = nullptr;
p._frameNumber = 0;
p._delta = Common::Point(0, 0);
p._oldSize = Common::Point(0, 0);
p._oldSize = Common::Point(0, 0);
p._misc = 0;
p._walkCount = 0;
p._allow = 0;
p._noShapeSize = Common::Point(0, 0);
p._goto = Common::Point(28000, 15000);
p._status = 0;
p.setImageFrame();
scene._bgShapes.clear();
}
/**
* Free the sprites and data used by the map
*/
void Map::freeSprites() {
delete _mapCursors;
delete _shapes;
delete _iconShapes;
}
/**
* Draws an icon for every place that's currently known
*/
void Map::showPlaces() {
Screen &screen = *_vm->_screen;
for (uint idx = 0; idx < _points.size(); ++idx) {
const MapEntry &pt = _points[idx];
if (pt.x != 0 && pt.y != 0) {
if (pt.x >= _bigPos.x && (pt.x - _bigPos.x) < SHERLOCK_SCREEN_WIDTH
&& pt.y >= _bigPos.y && (pt.y - _bigPos.y) < SHERLOCK_SCREEN_HEIGHT) {
if (_vm->readFlags(idx)) {
screen._backBuffer1.transBlitFrom((*_iconShapes)[pt._translate],
Common::Point(pt.x - _bigPos.x - 6, pt.y - _bigPos.y - 12));
}
}
}
}
}
/**
* Makes a copy of the top rows of the screen that are used to display location names
*/
void Map::saveTopLine() {
_topLine.blitFrom(_vm->_screen->_backBuffer1, Common::Point(0, 0), Common::Rect(0, 0, SHERLOCK_SCREEN_WIDTH, 12));
}
/**
* Erases anything shown in the top line by restoring the previously saved original map background
*/
void Map::eraseTopLine() {
Screen &screen = *_vm->_screen;
screen.blitFrom(_topLine, Common::Point(0, 0));
}
/**
* Prints the name of the specified icon
*/
void Map::showPlaceName(int idx, bool highlighted) {
People &people = *_vm->_people;
Screen &screen = *_vm->_screen;
Common::String name = _locationNames[idx];
int width = screen.stringWidth(name);
if (!_cursorIndex) {
saveIcon(people[AL]._imageFrame, _lDrawnPos);
bool flipped = people[AL]._sequenceNumber == MAP_DOWNLEFT || people[AL]._sequenceNumber == MAP_LEFT
|| people[AL]._sequenceNumber == MAP_UPLEFT;
screen._backBuffer1.transBlitFrom(people[AL]._imageFrame->_frame, _lDrawnPos, flipped);
}
if (highlighted) {
int xp = (SHERLOCK_SCREEN_WIDTH - screen.stringWidth(name)) / 2;
screen.gPrint(Common::Point(xp + 2, 2), 0, name.c_str());
screen.gPrint(Common::Point(xp + 1, 1), 0, name.c_str());
screen.gPrint(Common::Point(xp, 0), 12, name.c_str());
screen.slamArea(xp, 0, width + 2, 15);
}
}
/**
* Update all on-screen sprites to account for any scrolling of the map
*/
void Map::updateMap(bool flushScreen) {
Events &events = *_vm->_events;
People &people = *_vm->_people;
Screen &screen = *_vm->_screen;
Common::Point osPos = _savedPos;
Common::Point osSize = _savedSize;
Common::Point hPos;
if (_cursorIndex >= 1) {
if (++_cursorIndex > (1 + 8))
_cursorIndex = 1;
events.setCursor((*_mapCursors)[(_cursorIndex + 1) / 2]);
}
if (!_drawMap && !flushScreen)
restoreIcon();
else
_savedPos.x = -1;
people[AL].adjustSprite();
_lDrawnPos.x = hPos.x = people[AL]._position.x / 100 - _bigPos.x;
_lDrawnPos.y = hPos.y = people[AL]._position.y / 100 - people[AL].frameHeight() - _bigPos.y;
// Draw the person icon
saveIcon(people[AL]._imageFrame, hPos);
if (people[AL]._sequenceNumber == MAP_DOWNLEFT || people[AL]._sequenceNumber == MAP_LEFT
|| people[AL]._sequenceNumber == MAP_UPLEFT)
screen._backBuffer1.transBlitFrom(people[AL]._imageFrame->_frame, hPos, true);
else
screen._backBuffer1.transBlitFrom(people[AL]._imageFrame->_frame, hPos, false);
if (flushScreen) {
screen.slamArea(0, 0, SHERLOCK_SCREEN_WIDTH, SHERLOCK_SCREEN_HEIGHT);
} else if (!_drawMap) {
if (hPos.x > 0 && hPos.y >= 0 && hPos.x < SHERLOCK_SCREEN_WIDTH && hPos.y < SHERLOCK_SCREEN_HEIGHT)
screen.flushImage(people[AL]._imageFrame, Common::Point(people[AL]._position.x / 100 - _bigPos.x,
people[AL]._position.y / 100 - people[AL].frameHeight() - _bigPos.y),
&people[AL]._oldPosition.x, &people[AL]._oldPosition.y, &people[AL]._oldSize.x, &people[AL]._oldSize.y);
if (osPos.x != -1)
screen.slamArea(osPos.x, osPos.y, osSize.x, osSize.y);
}
}
/**
* Handle moving icon for player from their previous location on the map to a destination location
*/
void Map::walkTheStreets() {
People &people = *_vm->_people;
Scene &scene = *_vm->_scene;
bool reversePath = false;
Common::Array<Common::Point> tempPath;
// Get indexes into the path lists for the start and destination scenes
int start = _points[scene._oldCharPoint]._translate;
int dest = _points[scene._charPoint]._translate;
// Get pointer to start of path
const byte *path = _paths.getPath(start, dest);
// Add in destination position
people._walkTo.clear();
people._walkTo.push(people._walkDest);
// Check for any intermediate points between the two locations
if (path[0] || scene._charPoint > 50 || scene._oldCharPoint > 50) {
people[AL]._sequenceNumber = -1;
if (scene._charPoint == 51 || scene._oldCharPoint == 51) {
people.setWalking();
} else {
// Check for moving the path backwards or forwards
if (path[0] == 255) {
reversePath = true;
SWAP(start, dest);
path = _paths.getPath(start, dest);
}
do {
int idx = *path++;
tempPath.push_back(_pathPoints[idx - 1] + Common::Point(4, 4));
} while (*path != 254);
// Load up the path to use
people._walkTo.clear();
if (!reversePath) {
for (int idx = 0; idx < (int)tempPath.size(); ++idx)
people._walkTo.push(tempPath[idx]);
people._walkDest = tempPath.front();
} else {
for (int idx = 0; idx < ((int)tempPath.size() - 1); ++idx)
people._walkTo.push(tempPath[idx]);
people._walkDest = tempPath[tempPath.size() - 1];
}
people._walkDest.x += 12;
people._walkDest.y += 6;
people.setWalking();
}
} else {
people[AL]._walkCount = 0;
}
}
/**
* Save the area under the player's icon
*/
void Map::saveIcon(ImageFrame *src, const Common::Point &pt) {
Screen &screen = *_vm->_screen;
Common::Point size(src->_width, src->_height);
Common::Point pos = pt;
if (pos.x < 0) {
size.x += pos.x;
pos.x = 0;
}
if (pos.y < 0) {
size.y += pos.y;
pos.y = 0;
}
if ((pos.x + size.x) > SHERLOCK_SCREEN_WIDTH)
size.x -= (pos.x + size.x) - SHERLOCK_SCREEN_WIDTH;
if ((pos.y + size.y) > SHERLOCK_SCREEN_HEIGHT)
size.y -= (pos.y + size.y) - SHERLOCK_SCREEN_HEIGHT;
if (size.x < 1 || size.y < 1 || pos.x >= SHERLOCK_SCREEN_WIDTH || pos.y >= SHERLOCK_SCREEN_HEIGHT || _drawMap) {
// Flag as the area not needing to be saved
_savedPos.x = -1;
return;
}
_iconSave.create(size.x, size.y);
_iconSave.blitFrom(screen._backBuffer1, Common::Point(0, 0),
Common::Rect(pos.x, pos.y, pos.x + size.x, pos.y + size.y));
}
/**
* Restore the area under the player's icon
*/
void Map::restoreIcon() {
Screen &screen = *_vm->_screen;
if (_savedPos.x >= 0 && _savedPos.y >= 0 && _savedPos.x <= SHERLOCK_SCREEN_WIDTH
&& _savedPos.y < SHERLOCK_SCREEN_HEIGHT)
screen._backBuffer1.blitFrom(_iconSave, _savedPos);
}
/**
* Handles highlighting map icons, showing their names
*/
void Map::highlightIcon(const Common::Point &pt) {
int oldPoint = _point;
// Iterate through the icon list
bool done = false;
for (int idx = 0; idx < (int)_points.size(); ++idx) {
const MapEntry &entry = _points[idx];
// Check whether the mouse is over a given icon
if (entry.x != 0 && entry.y != 0) {
if (Common::Rect(entry.x - 8, entry.y - 8, entry.x + 9, entry.y + 9).contains(pt)) {
done = true;
if (_point != idx && _vm->readFlags(idx)) {
// Changed to a new valid (visible) location
eraseTopLine();
showPlaceName(idx, true);
_point = idx;
}
}
}
}
if (!done) {
// No icon was highlighted
if (_point != -1) {
// No longer highlighting previously highlighted icon, so erase it
showPlaceName(_point, false);
eraseTopLine();
}
_point = -1;
} else if (oldPoint != -1 && oldPoint != _point) {
showPlaceName(oldPoint, false);
eraseTopLine();
}
}
} // End of namespace Sherlock
|