1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "sherlock/scalpel/scalpel_people.h"
24 #include "sherlock/scalpel/scalpel_map.h"
25 #include "sherlock/sherlock.h"
26 
27 namespace Sherlock {
28 
29 namespace Scalpel {
30 
31 // Walk speeds
32 #define MWALK_SPEED 2
33 #define XWALK_SPEED 4
34 #define YWALK_SPEED 1
35 
36 /*----------------------------------------------------------------*/
37 
adjustSprite()38 void ScalpelPerson::adjustSprite() {
39 	Map &map = *_vm->_map;
40 	People &people = *_vm->_people;
41 	Scene &scene = *_vm->_scene;
42 	Talk &talk = *_vm->_talk;
43 
44 	if (_type == INVALID || (_type == CHARACTER && scene._animating))
45 		return;
46 
47 	if (!talk._talkCounter && _type == CHARACTER && _walkCount) {
48 		// Handle active movement for the sprite
49 		_position += _delta;
50 		--_walkCount;
51 
52 		if (!_walkCount) {
53 			// If there any points left for the character to walk to along the
54 			// route to a destination, then move to the next point
55 			if (!people[HOLMES]._walkTo.empty()) {
56 				_walkDest = people[HOLMES]._walkTo.pop();
57 				setWalking();
58 			} else {
59 				gotoStand();
60 			}
61 		}
62 	}
63 
64 	if (_type == CHARACTER && !map._active) {
65 		if ((_position.y / FIXED_INT_MULTIPLIER) > LOWER_LIMIT) {
66 			_position.y = LOWER_LIMIT * FIXED_INT_MULTIPLIER;
67 			gotoStand();
68 		}
69 
70 		if ((_position.y / FIXED_INT_MULTIPLIER) < UPPER_LIMIT) {
71 			_position.y = UPPER_LIMIT * FIXED_INT_MULTIPLIER;
72 			gotoStand();
73 		}
74 
75 		if ((_position.x / FIXED_INT_MULTIPLIER) < LEFT_LIMIT) {
76 			_position.x = LEFT_LIMIT * FIXED_INT_MULTIPLIER;
77 			gotoStand();
78 		}
79 
80 		if ((_position.x / FIXED_INT_MULTIPLIER) > RIGHT_LIMIT) {
81 			_position.x = RIGHT_LIMIT * FIXED_INT_MULTIPLIER;
82 			gotoStand();
83 		}
84 	} else if (!map._active) {
85 		_position.y = CLIP((int)_position.y, (int)UPPER_LIMIT, (int)LOWER_LIMIT);
86 		_position.x = CLIP((int)_position.x, (int)LEFT_LIMIT, (int)RIGHT_LIMIT);
87 	}
88 
89 	if (!map._active || (map._frameChangeFlag = !map._frameChangeFlag))
90 		++_frameNumber;
91 
92 	if (_frameNumber >= (int)_walkSequences[_sequenceNumber]._sequences.size() ||
93 			_walkSequences[_sequenceNumber][_frameNumber] == 0) {
94 		switch (_sequenceNumber) {
95 		case STOP_UP:
96 		case STOP_DOWN:
97 		case STOP_LEFT:
98 		case STOP_RIGHT:
99 		case STOP_UPRIGHT:
100 		case STOP_UPLEFT:
101 		case STOP_DOWNRIGHT:
102 		case STOP_DOWNLEFT:
103 			// We're in a stop sequence, so reset back to the last frame, so
104 			// the character is shown as standing still
105 			--_frameNumber;
106 			break;
107 
108 		default:
109 			// Move 1 past the first frame - we need to compensate, since we
110 			// already passed the frame increment
111 			_frameNumber = 1;
112 			break;
113 		}
114 	}
115 
116 	// Update the _imageFrame to point to the new frame's image
117 	setImageFrame();
118 
119 	// Check to see if character has entered an exit zone
120 	if (!_walkCount && scene._walkedInScene && scene._goToScene == -1) {
121 		Common::Rect charRect(_position.x / FIXED_INT_MULTIPLIER - 5, _position.y / FIXED_INT_MULTIPLIER - 2,
122 			_position.x / FIXED_INT_MULTIPLIER + 5, _position.y / FIXED_INT_MULTIPLIER + 2);
123 		Exit *exit = scene.checkForExit(charRect);
124 
125 		if (exit) {
126 			scene._goToScene = exit->_scene;
127 
128 			if (exit->_newPosition.x != 0) {
129 				people._savedPos = exit->_newPosition;
130 
131 				if (people._savedPos._facing > 100 && people._savedPos.x < 1)
132 					people._savedPos.x = 100;
133 			}
134 		}
135 	}
136 }
137 
gotoStand()138 void ScalpelPerson::gotoStand() {
139 	ScalpelMap &map = *(ScalpelMap *)_vm->_map;
140 	People &people = *_vm->_people;
141 	_walkTo.clear();
142 	_walkCount = 0;
143 
144 	switch (_sequenceNumber) {
145 	case Scalpel::WALK_UP:
146 		_sequenceNumber = STOP_UP;
147 		break;
148 	case WALK_DOWN:
149 		_sequenceNumber = STOP_DOWN;
150 		break;
151 	case TALK_LEFT:
152 	case WALK_LEFT:
153 		_sequenceNumber = STOP_LEFT;
154 		break;
155 	case TALK_RIGHT:
156 	case WALK_RIGHT:
157 		_sequenceNumber = STOP_RIGHT;
158 		break;
159 	case WALK_UPRIGHT:
160 		_sequenceNumber = STOP_UPRIGHT;
161 		break;
162 	case WALK_UPLEFT:
163 		_sequenceNumber = STOP_UPLEFT;
164 		break;
165 	case WALK_DOWNRIGHT:
166 		_sequenceNumber = STOP_DOWNRIGHT;
167 		break;
168 	case WALK_DOWNLEFT:
169 		_sequenceNumber = STOP_DOWNLEFT;
170 		break;
171 	default:
172 		break;
173 	}
174 
175 	// Only restart frame at 0 if the sequence number has changed
176 	if (_oldWalkSequence != -1 || _sequenceNumber == Scalpel::STOP_UP)
177 		_frameNumber = 0;
178 
179 	if (map._active) {
180 		_sequenceNumber = 0;
181 		people[HOLMES]._position.x = (map[map._charPoint].x - 6) * FIXED_INT_MULTIPLIER;
182 		people[HOLMES]._position.y = (map[map._charPoint].y + 10) * FIXED_INT_MULTIPLIER;
183 	}
184 
185 	_oldWalkSequence = -1;
186 	people._allowWalkAbort = true;
187 }
188 
setWalking()189 void ScalpelPerson::setWalking() {
190 	Map &map = *_vm->_map;
191 	Scene &scene = *_vm->_scene;
192 	int oldDirection, oldFrame;
193 	Common::Point speed, delta;
194 
195 	// Flag that player has now walked in the scene
196 	scene._walkedInScene = true;
197 
198 	// Stop any previous walking, since a new dest is being set
199 	_walkCount = 0;
200 	oldDirection = _sequenceNumber;
201 	oldFrame = _frameNumber;
202 
203 	// Set speed to use horizontal and vertical movement
204 	if (map._active) {
205 		speed = Common::Point(MWALK_SPEED, MWALK_SPEED);
206 	} else {
207 		speed = Common::Point(XWALK_SPEED, YWALK_SPEED);
208 	}
209 
210 	// If the player is already close to the given destination that no
211 	// walking is needed, move to the next straight line segment in the
212 	// overall walking route, if there is one
213 	for (;;) {
214 		// Since we want the player to be centered on the destination they
215 		// clicked, but characters draw positions start at their left, move
216 		// the destination half the character width to draw him centered
217 		int temp;
218 		if (_walkDest.x >= (temp = _imageFrame->_frame.w / 2))
219 			_walkDest.x -= temp;
220 
221 		delta = Common::Point(
222 			ABS(_position.x / FIXED_INT_MULTIPLIER - _walkDest.x),
223 			ABS(_position.y / FIXED_INT_MULTIPLIER - _walkDest.y)
224 		);
225 
226 		// If we're ready to move a sufficient distance, that's it. Otherwise,
227 		// move onto the next portion of the walk path, if there is one
228 		if ((delta.x > 3 || delta.y > 0) || _walkTo.empty())
229 			break;
230 
231 		// Pop next walk segment off the walk route stack
232 		_walkDest = _walkTo.pop();
233 	}
234 
235 	// If a sufficient move is being done, then start the move
236 	if (delta.x > 3 || delta.y) {
237 		// See whether the major movement is horizontal or vertical
238 		if (delta.x >= delta.y) {
239 			// Set the initial frame sequence for the left and right, as well
240 			// as setting the delta x depending on direction
241 			if (_walkDest.x < (_position.x / FIXED_INT_MULTIPLIER)) {
242 				_sequenceNumber = (map._active ? (int)MAP_LEFT : (int)WALK_LEFT);
243 				_delta.x = speed.x * -FIXED_INT_MULTIPLIER;
244 			} else {
245 				_sequenceNumber = (map._active ? (int)MAP_RIGHT : (int)WALK_RIGHT);
246 				_delta.x = speed.x * FIXED_INT_MULTIPLIER;
247 			}
248 
249 			// See if the x delta is too small to be divided by the speed, since
250 			// this would cause a divide by zero error
251 			if (delta.x >= speed.x) {
252 				// Det the delta y
253 				_delta.y = (delta.y * FIXED_INT_MULTIPLIER) / (delta.x / speed.x);
254 				if (_walkDest.y < (_position.y / FIXED_INT_MULTIPLIER))
255 					_delta.y = -_delta.y;
256 
257 				// Set how many times we should add the delta to the player's position
258 				_walkCount = delta.x / speed.x;
259 			} else {
260 				// The delta x was less than the speed (ie. we're really close to
261 				// the destination). So set delta to 0 so the player won't move
262 				_delta = Point32(0, 0);
263 				_position = Point32(_walkDest.x * FIXED_INT_MULTIPLIER, _walkDest.y * FIXED_INT_MULTIPLIER);
264 
265 				_walkCount = 1;
266 			}
267 
268 			// See if the sequence needs to be changed for diagonal walking
269 			if (_delta.y > 150) {
270 				if (!map._active) {
271 					switch (_sequenceNumber) {
272 					case WALK_LEFT:
273 						_sequenceNumber = WALK_DOWNLEFT;
274 						break;
275 					case WALK_RIGHT:
276 						_sequenceNumber = WALK_DOWNRIGHT;
277 						break;
278 					default:
279 						break;
280 					}
281 				}
282 			} else if (_delta.y < -150) {
283 				if (!map._active) {
284 					switch (_sequenceNumber) {
285 					case WALK_LEFT:
286 						_sequenceNumber = WALK_UPLEFT;
287 						break;
288 					case WALK_RIGHT:
289 						_sequenceNumber = WALK_UPRIGHT;
290 						break;
291 					default:
292 						break;
293 					}
294 				}
295 			}
296 		} else {
297 			// Major movement is vertical, so set the sequence for up and down,
298 			// and set the delta Y depending on the direction
299 			if (_walkDest.y < (_position.y / FIXED_INT_MULTIPLIER)) {
300 				_sequenceNumber = WALK_UP;
301 				_delta.y = speed.y * -FIXED_INT_MULTIPLIER;
302 			} else {
303 				_sequenceNumber = WALK_DOWN;
304 				_delta.y = speed.y * FIXED_INT_MULTIPLIER;
305 			}
306 
307 			// If we're on the overhead map, set the sequence so we keep moving
308 			// in the same direction
309 			if (map._active)
310 				_sequenceNumber = (oldDirection == -1) ? MAP_RIGHT : oldDirection;
311 
312 			// Set the delta x
313 			_delta.x = (delta.x * FIXED_INT_MULTIPLIER) / (delta.y / speed.y);
314 			if (_walkDest.x < (_position.x / FIXED_INT_MULTIPLIER))
315 				_delta.x = -_delta.x;
316 
317 			_walkCount = delta.y / speed.y;
318 		}
319 	}
320 
321 	// See if the new walk sequence is the same as the old. If it's a new one,
322 	// we need to reset the frame number to zero so its animation starts at
323 	// its beginning. Otherwise, if it's the same sequence, we can leave it
324 	// as is, so it keeps the animation going at wherever it was up to
325 	if (_sequenceNumber != _oldWalkSequence)
326 		_frameNumber = 0;
327 	_oldWalkSequence = _sequenceNumber;
328 
329 	if (!_walkCount)
330 		gotoStand();
331 
332 	// If the sequence is the same as when we started, then Holmes was
333 	// standing still and we're trying to re-stand him, so reset Holmes'
334 	// rame to the old frame number from before it was reset to 0
335 	if (_sequenceNumber == oldDirection)
336 		_frameNumber = oldFrame;
337 }
338 
walkToCoords(const Point32 & destPos,int destDir)339 void ScalpelPerson::walkToCoords(const Point32 &destPos, int destDir) {
340 	Events &events = *_vm->_events;
341 	People &people = *_vm->_people;
342 	Scene &scene = *_vm->_scene;
343 	Talk &talk = *_vm->_talk;
344 
345 	CursorId oldCursor = events.getCursor();
346 	events.setCursor(WAIT);
347 
348 	_walkDest = Common::Point(destPos.x / FIXED_INT_MULTIPLIER + 10, destPos.y / FIXED_INT_MULTIPLIER);
349 	people._allowWalkAbort = true;
350 	goAllTheWay();
351 
352 	// Keep calling doBgAnim until the walk is done
353 	do {
354 		events.pollEventsAndWait();
355 		scene.doBgAnim();
356 	} while (!_vm->shouldQuit() && _walkCount);
357 
358 	if (!talk._talkToAbort) {
359 		// Put character exactly on destination position, and set direction
360 		_position = destPos;
361 		_sequenceNumber = destDir;
362 		gotoStand();
363 
364 		// Draw Holmes facing the new direction
365 		scene.doBgAnim();
366 
367 		if (!talk._talkToAbort)
368 			events.setCursor(oldCursor);
369 	}
370 }
371 
getSourcePoint() const372 Common::Point ScalpelPerson::getSourcePoint() const {
373 	return Common::Point(_position.x / FIXED_INT_MULTIPLIER + frameWidth() / 2,
374 		_position.y / FIXED_INT_MULTIPLIER);
375 }
376 
synchronize(Serializer & s)377 void ScalpelPerson::synchronize(Serializer &s) {
378 	if (_walkCount)
379 		gotoStand();
380 
381 	s.syncAsSint32LE(_position.x);
382 	s.syncAsSint32LE(_position.y);
383 }
384 
385 /*----------------------------------------------------------------*/
386 
ScalpelPeople(SherlockEngine * vm)387 ScalpelPeople::ScalpelPeople(SherlockEngine *vm) : People(vm) {
388 	_data.push_back(new ScalpelPerson());
389 }
390 
setTalking(int speaker)391 void ScalpelPeople::setTalking(int speaker) {
392 	Resources &res = *_vm->_res;
393 
394 	// If no speaker is specified, then we can exit immediately
395 	if (speaker == -1)
396 		return;
397 
398 	if (_portraitsOn) {
399 		delete _talkPics;
400 		Common::String filename = Common::String::format("%s.vgs", _characters[speaker]._portrait);
401 		_talkPics = new ImageFile(filename);
402 
403 		// Load portrait sequences
404 		Common::SeekableReadStream *stream = res.load("sequence.txt");
405 		stream->seek(speaker * MAX_FRAME);
406 
407 		int idx = 0;
408 		do {
409 			_portrait._sequences[idx] = stream->readByte();
410 			++idx;
411 		} while (idx < 2 || _portrait._sequences[idx - 2] || _portrait._sequences[idx - 1]);
412 
413 		delete stream;
414 
415 		_portrait._maxFrames = idx;
416 		_portrait._frameNumber = 0;
417 		_portrait._sequenceNumber = 0;
418 		_portrait._images = _talkPics;
419 		_portrait._imageFrame = &(*_talkPics)[0];
420 		_portrait._position = Common::Point(_portraitSide, 10);
421 		_portrait._delta = Common::Point(0, 0);
422 		_portrait._oldPosition = Common::Point(0, 0);
423 		_portrait._goto = Common::Point(0, 0);
424 		_portrait._flags = 5;
425 		_portrait._status = 0;
426 		_portrait._misc = 0;
427 		_portrait._allow = 0;
428 		_portrait._type = ACTIVE_BG_SHAPE;
429 		_portrait._name = " ";
430 		_portrait._description = " ";
431 		_portrait._examine = " ";
432 		_portrait._walkCount = 0;
433 
434 		if (_holmesFlip || _speakerFlip) {
435 			_portrait._flags |= 2;
436 
437 			_holmesFlip = false;
438 			_speakerFlip = false;
439 		}
440 
441 		if (_portraitSide == 20)
442 			_portraitSide = 220;
443 		else
444 			_portraitSide = 20;
445 
446 		_portraitLoaded = true;
447 	}
448 }
449 
synchronize(Serializer & s)450 void ScalpelPeople::synchronize(Serializer &s) {
451 	(*this)[HOLMES].synchronize(s);
452 	s.syncAsSint16LE(_holmesQuotient);
453 	s.syncAsByte(_holmesOn);
454 
455 	if (s.isLoading()) {
456 		_savedPos = _data[HOLMES]->_position;
457 		_savedPos._facing = _data[HOLMES]->_sequenceNumber;
458 	}
459 }
460 
setTalkSequence(int speaker,int sequenceNum)461 void ScalpelPeople::setTalkSequence(int speaker, int sequenceNum) {
462 	People &people = *_vm->_people;
463 	Scene &scene = *_vm->_scene;
464 
465 	// If no speaker is specified, then nothing needs to be done
466 	if (speaker == -1)
467 		return;
468 
469 	if (speaker) {
470 		int objNum = people.findSpeaker(speaker);
471 		if (objNum != -1) {
472 			Object &obj = scene._bgShapes[objNum];
473 
474 			if (obj._seqSize < MAX_TALK_SEQUENCES) {
475 				warning("Tried to copy too many talk frames");
476 			} else {
477 				for (int idx = 0; idx < MAX_TALK_SEQUENCES; ++idx) {
478 					obj._sequences[idx] = people._characters[speaker]._talkSequences[idx];
479 					if (idx > 0 && !obj._sequences[idx] && !obj._sequences[idx - 1])
480 						return;
481 
482 					obj._frameNumber = 0;
483 					obj._sequenceNumber = 0;
484 				}
485 			}
486 		}
487 	}
488 }
489 
loadWalk()490 bool ScalpelPeople::loadWalk() {
491 	bool result = false;
492 
493 	if (_data[HOLMES]->_walkLoaded) {
494 		return false;
495 	} else {
496 		if (!IS_3DO) {
497 			_data[HOLMES]->_images = new ImageFile("walk.vgs");
498 		} else {
499 			// Load walk.anim on 3DO, which is a cel animation file
500 			_data[HOLMES]->_images = new ImageFile3DO("walk.anim", kImageFile3DOType_CelAnimation);
501 		}
502 		_data[HOLMES]->setImageFrame();
503 		_data[HOLMES]->_walkLoaded = true;
504 
505 		result = true;
506 	}
507 
508 	_forceWalkReload = false;
509 	return result;
510 }
511 
restrictToZone(int zoneId,const Common::Point & destPos)512 const Common::Point ScalpelPeople::restrictToZone(int zoneId, const Common::Point &destPos) {
513 	Scene &scene = *_vm->_scene;
514 	Common::Point walkDest = destPos;
515 
516 	// The destination isn't in a zone
517 	if (walkDest.x >= (SHERLOCK_SCREEN_WIDTH - 1))
518 		walkDest.x = SHERLOCK_SCREEN_WIDTH - 2;
519 
520 	// Trace a line between the centroid of the found closest zone to
521 	// the destination, to find the point at which the zone will be left
522 	const Common::Rect &destRect = scene._zones[zoneId];
523 	const Common::Point destCenter((destRect.left + destRect.right) / 2,
524 		(destRect.top + destRect.bottom) / 2);
525 	const Common::Point delta = walkDest - destCenter;
526 	Point32 pt(destCenter.x * FIXED_INT_MULTIPLIER, destCenter.y * FIXED_INT_MULTIPLIER);
527 
528 	// Move along the line until the zone is left
529 	do {
530 		pt += delta;
531 	} while (destRect.contains(pt.x / FIXED_INT_MULTIPLIER, pt.y / FIXED_INT_MULTIPLIER));
532 
533 	// Set the new walk destination to the last point that was in the
534 	// zone just before it was left
535 	return Common::Point((pt.x - delta.x * 2) / FIXED_INT_MULTIPLIER,
536 		(pt.y - delta.y * 2) / FIXED_INT_MULTIPLIER);
537 }
538 
setListenSequence(int speaker,int sequenceNum)539 void ScalpelPeople::setListenSequence(int speaker, int sequenceNum) {
540 	People &people = *_vm->_people;
541 	Scene &scene = *_vm->_scene;
542 
543 	// Don't bother doing anything if no specific speaker is specified
544 	if (speaker == -1)
545 		return;
546 
547 	if (speaker) {
548 		int objNum = people.findSpeaker(speaker);
549 		if (objNum != -1) {
550 			Object &obj = scene._bgShapes[objNum];
551 
552 			if (obj._seqSize < MAX_TALK_SEQUENCES) {
553 				warning("Tried to copy too few still frames");
554 			} else {
555 				for (uint idx = 0; idx < MAX_TALK_SEQUENCES; ++idx) {
556 					obj._sequences[idx] = people._characters[speaker]._stillSequences[idx];
557 					if (idx > 0 && !people._characters[speaker]._talkSequences[idx] &&
558 						!people._characters[speaker]._talkSequences[idx - 1])
559 						break;
560 				}
561 
562 				obj._frameNumber = 0;
563 				obj._seqTo = 0;
564 			}
565 		}
566 	}
567 }
568 
569 } // End of namespace Scalpel
570 
571 } // End of namespace Sherlock
572