aboutsummaryrefslogtreecommitdiff
path: root/engines/mads/action.cpp
blob: 03c0c1ed8a61b4b9572ab935a75d34b75a5fe170 (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
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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/* 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 "common/scummsys.h"
#include "mads/mads.h"
#include "mads/action.h"
#include "mads/inventory.h"
#include "mads/scene.h"
#include "mads/staticres.h"

namespace MADS {

void ActionDetails::synchronize(Common::Serializer &s) {
	s.syncAsUint16LE(_verbId);
	s.syncAsUint16LE(_objectNameId);
	s.syncAsUint16LE(_indirectObjectId);
}

/*------------------------------------------------------------------------*/

MADSAction::MADSAction(MADSEngine *vm) : _vm(vm) {
	clear();
	_statusTextIndex = -1;
	_selectedAction = 0;
	_inProgress = false;

	_savedFields._commandSource = 0;
	_savedFields._mainObjectSource = 0;
	_savedFields._command = -1;
	_savedFields._mainObject = 0;
	_savedFields._secondObject = 0;
	_savedFields._secondObjectSource = 0;
	_savedFields._articleNumber = 0;
	_savedFields._lookFlag = false;
}

void MADSAction::clear() {
	_interAwaiting = AWAITING_COMMAND;
	_commandSource = CAT_NONE;
	_mainObjectSource = CAT_NONE;
	_secondObjectSource = CAT_NONE;
	_recentCommandSource = CAT_NONE;
	_articleNumber = 0;
	_lookFlag = false;
	_pointEstablished = 0;
	_selectedRow = -1;
	_hotspotId = -1;
	_secondObject = -1;
	_recentCommand = -1;
	_action._verbId = VERB_NONE;
	_action._objectNameId = -1;
	_action._indirectObjectId = -1;
	_textChanged = true;
	_pickedWord = 0;
}

void MADSAction::appendVocab(int vocabId, bool capitalise) {
	Common::String vocabStr = _vm->_game->_scene.getVocab(vocabId);
	if (capitalise)
		vocabStr.setChar(toupper(vocabStr[0]), 0);

	_statusText += vocabStr;
	_statusText += " ";
}

void MADSAction::startWalkingDirectly(int walkType) {
	Scene &scene = _vm->_game->_scene;
	Player &player = _vm->_game->_player;

	if (_pointEstablished && (walkType == -3 || _savedFields._command < 0)) {
		player._needToWalk = true;
		player._prepareWalkPos = scene._customDest;
	}
}

void MADSAction::set() {
	Scene &scene = _vm->_game->_scene;
	UserInterface &userInterface = scene._userInterface;
	bool flag = false;
	_statusText = "";

	_action._verbId = VERB_NONE;
	_action._objectNameId = -1;
	_action._indirectObjectId = -1;

	if (_commandSource == CAT_TALK_ENTRY) {
		// Handle showing the conversation selection. Rex at least doesn't actually seem to use this
		if (_selectedRow >= 0) {
			_action._verbId = userInterface._talkIds[_selectedRow];
			Common::String desc = userInterface._talkStrings[_selectedRow];
			if (!desc.empty())
				_statusText = desc;
		}
	} else if (_lookFlag && (_selectedRow == 0)) {
		// Two 'look' actions in succession, so the action becomes 'Look around'
		_statusText = kLookAroundStr;
	} else {
		if ((_commandSource == CAT_INV_VOCAB) && (_selectedRow >= 0) 
				&& (_verbType == VERB_THAT) && (_prepType == PREP_NONE)) {
			// Use/to action
			int invIndex = userInterface._selectedInvIndex;
			InventoryObject &objEntry = _vm->_game->_objects.getItem(invIndex);

			_action._objectNameId = objEntry._descId;
			_action._verbId = objEntry._vocabList[_selectedRow]._vocabId;

			// Set up the status text stirng
			_statusText = kUseStr;
			appendVocab(_action._objectNameId);
			_statusText += kToStr;
			appendVocab(_action._verbId);
		} else {
			// Handling for if an action has been selected
			if (_selectedRow >= 0) {
				if (_commandSource == CAT_COMMAND) {
					// Standard verb action
					_action._verbId = scene._verbList[_selectedRow]._id;
				} else {
					// Selected action on an inventory object
					int invIndex = userInterface._selectedInvIndex;
					InventoryObject &objEntry = _vm->_game->_objects.getItem(invIndex);

					_action._verbId = objEntry._vocabList[_selectedRow]._vocabId;
				}

				appendVocab(_action._verbId, true);

				if (_action._verbId == VERB_LOOK) {
					// Add in the word 'add'
					_statusText += kArticleList[PREP_AT];
					_statusText += " ";
				}
			}

			// Handling for if a hotspot has been selected/highlighted
			if ((_hotspotId >= 0) && (_selectedRow >= 0) && (_articleNumber > 0) && (_verbType == VERB_THAT)) {
				flag = true;

				_statusText += kArticleList[_articleNumber];
				_statusText += " ";
			}

			if (_hotspotId >= 0) {
				if (_selectedRow < 0) {
					int verbId;

					if (_hotspotId < (int)scene._hotspots.size()) {
						// Get the verb Id from the hotspot
						verbId = scene._hotspots[_hotspotId]._verbId;
					} else {
						// Get the verb Id from the scene object
						verbId = scene._dynamicHotspots[_hotspotId - scene._hotspots.size()]._vocabId;
					}

					if (verbId > 0) {
						// Set the specified action
						_action._verbId = verbId;
						appendVocab(_action._verbId, true);
					} else {
						// Default to a standard 'walk to'
						_action._verbId = VERB_WALKTO;
						_statusText += kWalkToStr;
					}
				}

				if ((_mainObjectSource == CAT_INV_LIST) || (_mainObjectSource == CAT_INV_ANIM)) {
					// Get name from given inventory object
					InventoryObject &invObject = _vm->_game->_objects.getItem(_hotspotId);
					_action._objectNameId = invObject._descId;
				} else if (_hotspotId < (int)scene._hotspots.size()) {
					// Get name from scene hotspot
					_action._objectNameId = scene._hotspots[_hotspotId]._vocabId;
				} else {
					// Get name from temporary scene hotspot
					_action._objectNameId = scene._dynamicHotspots[_hotspotId - scene._hotspots.size()]._vocabId;
				}
				appendVocab(_action._objectNameId);
			}
		}

		if ((_hotspotId >= 0) && (_articleNumber > 0) && !flag) {
			if (_articleNumber == -1) {
				if (_secondObject >= 0) {
					int articleNum = 0;

					if ((_secondObjectSource == 2) || (_secondObjectSource == 5)) {
						InventoryObject &invObject = _vm->_game->_objects.getItem(_hotspotId);
						articleNum = invObject._article;
					} else if (_secondObject < (int)scene._hotspots.size()) {
						articleNum = scene._hotspots[_hotspotId]._articleNumber;
					} else {
						articleNum = scene._hotspots[_hotspotId - scene._hotspots.size()]._articleNumber;
					}

					_statusText += kArticleList[articleNum];
					_statusText += " ";
				}
			} else if ((_articleNumber == VERB_LOOK) || (_vm->getGameID() != GType_RexNebular) ||
				(scene._vocabStrings[_action._indirectObjectId] != kFenceStr)) {
				// Write out the article
				_statusText += kArticleList[_articleNumber];
			} else {
				// Special case for a 'fence' entry in Rex Nebular
				_statusText += kOverStr;
			}

			_statusText += " ";
		}

		// Append object description if necessary
		if (_secondObject >= 0)
			appendVocab(_action._indirectObjectId);

		// Remove any trailing space character
		if (_statusText.hasSuffix(" "))
			_statusText.deleteLastChar();
	}

	_textChanged = true;
}

void MADSAction::refresh() {
	Scene &scene = _vm->_game->_scene;

	// Exit immediately if nothing has changed
	if (!_textChanged)
		return;

	// Remove any old copy of the status text
	if (_statusTextIndex >= 0) {
		scene._textDisplay.expire(_statusTextIndex);
		_statusTextIndex = -1;
	}

	if (!_statusText.empty()) {
		if ((_vm->_game->_screenObjects._inputMode == kInputBuildingSentences) || 
				(_vm->_game->_screenObjects._inputMode == kInputLimitedSentences)) {
			Font *font = _vm->_font->getFont(FONT_MAIN);
			int textSpacing = -1;

			int strWidth = font->getWidth(_statusText);
			if (strWidth > MADS_SCREEN_WIDTH) {
				// Too large to fit, so fall back on interface font
				font = _vm->_font->getFont(FONT_INTERFACE);
				strWidth = font->getWidth(_statusText, 0);
				textSpacing = 0;
			}

			// Add a new text display entry to display the status text at the bottom of the screen area
			_statusTextIndex = scene._textDisplay.add(160 - (strWidth / 2),
				MADS_SCENE_HEIGHT + scene._posAdjust.y - 13, 3, textSpacing, _statusText, font);
		}
	}

	_textChanged = false;
}

void MADSAction::startAction() {
	Game &game = *_vm->_game;
	Player &player = game._player;
	Scene &scene = _vm->_game->_scene;
	DynamicHotspots &dynHotspots = scene._dynamicHotspots;
	Hotspots &hotspots = scene._hotspots;

	player.cancelCommand();

	_inProgress = true;
	_savedFields._commandError = false;
	_savedFields._command = _selectedRow;
	_savedFields._mainObject = _hotspotId;
	_savedFields._secondObject = _secondObject;
	_savedFields._articleNumber = _articleNumber;
	_savedFields._commandSource = _commandSource;
	_savedFields._mainObjectSource = _mainObjectSource;
	_savedFields._secondObjectSource = _secondObjectSource;
	_savedFields._lookFlag = _lookFlag;
	_activeAction = _action;

	// Copy the action to be active
	_activeAction = _action;
	_sentence = _statusText;

	if ((_mainObjectSource == CAT_HOTSPOT) && (_secondObjectSource == 4))
		_savedFields._commandError = true;

	player._needToWalk = false;
	int hotspotId = -1;

	if (!_savedFields._lookFlag && (_vm->_game->_screenObjects._inputMode != kInputConversation)) {
		if (_savedFields._mainObjectSource == CAT_HOTSPOT)
			hotspotId = _savedFields._mainObject;
		else if (_secondObjectSource == 4)
			hotspotId = _savedFields._secondObject;

		if (hotspotId >= (int)hotspots.size()) {
			DynamicHotspot &hs = dynHotspots[hotspotId - hotspots.size()];
			if ((hs._feetPos.x == -1) || (hs._feetPos.x == -3)) {
				startWalkingDirectly(hs._feetPos.x);
			} else if (hs._feetPos.x == 0) {
				player._prepareWalkFacing = hs._facing;
			} else if (_savedFields._commandSource == CAT_NONE || hs._cursor >= CURSOR_WAIT) {
				player._needToWalk = true;
				player._prepareWalkPos = hs._feetPos;
			}

			player._prepareWalkFacing = hs._facing;
			hotspotId = -1;
		}
	}

	if (hotspotId >= 0 && hotspotId < (int)hotspots.size()) {
		Hotspot &hs = hotspots[hotspotId];

		if (hs._feetPos.x == -1 || hs._feetPos.x == -3) {
			startWalkingDirectly(hs._feetPos.x);
		} else if (hs._feetPos.x >= 0) {
			if (_savedFields._commandSource == CAT_NONE || hs._cursor < CURSOR_WAIT) {
				player._needToWalk = true;
				player._prepareWalkPos = hs._feetPos;
			}
		}

		player._prepareWalkFacing = hs._facing;
	}

	player._readyToWalk = player._needToWalk;
}

void MADSAction::checkAction() {
	if (isAction(VERB_LOOK) || isAction(VERB_THROW))
		_vm->_game->_player._needToWalk = false;
}

bool MADSAction::isAction(int verbId, int objectNameId, int indirectObjectId) {
	if (_activeAction._verbId != verbId)
		return false;
	if ((objectNameId != 0) && (_activeAction._objectNameId != objectNameId))
		return false;
	if ((indirectObjectId != 0) && (_activeAction._indirectObjectId != indirectObjectId))
		return false;

	return true;
}

void MADSAction::checkActionAtMousePos() {
	Scene &scene = _vm->_game->_scene;
	UserInterface &userInterface = scene._userInterface;

	if ((userInterface._category == CAT_COMMAND || userInterface._category == CAT_INV_VOCAB) &&
			_interAwaiting != AWAITING_COMMAND && _pickedWord >= 0) {
		if (_recentCommandSource == userInterface._category || _recentCommand != _pickedWord ||
			(_interAwaiting != AWAITING_THIS && _interAwaiting != 3))
			clear();
		else if (_selectedRow != 0 || userInterface._category != CAT_COMMAND)
			scene._lookFlag = false;
		else
			scene._lookFlag = true;
	}

	if (_vm->_events->_rightMousePressed && _vm->_events->_mouseButtons) {
		switch (userInterface._category) {
		case CAT_COMMAND:
		case CAT_INV_VOCAB:
			return;

		case CAT_INV_LIST:
		case CAT_HOTSPOT:
		case CAT_INV_ANIM:
			if (_interAwaiting != AWAITING_THAT) {
				if (userInterface._selectedActionIndex >= 0) {
					_commandSource = CAT_COMMAND;
					_selectedRow = userInterface._selectedActionIndex;
					_verbType = scene._verbList[_selectedRow]._verbType;
					_prepType = scene._verbList[_selectedRow]._prepType;
					_interAwaiting = AWAITING_THIS;
				} else if (userInterface._selectedItemVocabIdx >= 0) {
					_commandSource = CAT_INV_VOCAB;
					_selectedRow = userInterface._selectedItemVocabIdx;
					int objectId = _vm->_game->_objects._inventoryList[_selectedRow];
					InventoryObject &invObject = _vm->_game->_objects[objectId];

					_verbType = invObject._vocabList[_selectedRow - 1]._verbType;
					_prepType = invObject._vocabList[_selectedRow - 1]._prepType;
					_mainObjectSource = CAT_INV_LIST;
					_hotspotId = userInterface._selectedInvIndex;
					_articleNumber = _prepType;

					if ((_verbType == VERB_THIS && _prepType == PREP_NONE) || 
							(_verbType == VERB_THAT && _prepType != PREP_NONE))
						_interAwaiting = AWAITING_RIGHT_MOUSE;
					else
						_interAwaiting = AWAITING_THAT;
				}
			}
			break;

		default:
			break;
		}
	}

	switch (_interAwaiting) {
	case AWAITING_COMMAND:
		_articleNumber = 0;
		switch (userInterface._category) {
		case CAT_COMMAND:
			_commandSource = CAT_COMMAND;
			_selectedRow = _pickedWord;
			if (_selectedRow >= 0) {
				_verbType = scene._verbList[_selectedRow]._verbType;
				_prepType = scene._verbList[_selectedRow]._prepType;
			}
			break;

		case CAT_INV_VOCAB:
			_commandSource = CAT_INV_VOCAB;
			_selectedRow = _pickedWord;
			if (_selectedRow < 0) {
				_hotspotId = -1;
				_mainObjectSource = CAT_NONE;
			} else {
				InventoryObject &invObject = _vm->_game->_objects.getItem(userInterface._selectedInvIndex);
				_verbType = invObject._vocabList[_selectedRow + 1]._verbType;
				_prepType = invObject._vocabList[_selectedRow + 1]._prepType;
				_hotspotId = userInterface._selectedInvIndex;
				_mainObjectSource = CAT_INV_LIST;

				if (_verbType == VERB_THAT)
					_articleNumber = _prepType;
			}
			break;

		case CAT_HOTSPOT:
			_selectedRow = -1;
			_commandSource = CAT_NONE;
			_mainObjectSource = CAT_HOTSPOT;
			_hotspotId = _pickedWord;
			break;

		case CAT_TALK_ENTRY:
			_commandSource = CAT_TALK_ENTRY;
			_selectedRow = _pickedWord;
			break;

		default:
			break;
		}
		break;

	case AWAITING_THIS:
		_articleNumber = 0;
		switch (userInterface._category) {
		case CAT_INV_LIST:
		case CAT_HOTSPOT:
		case CAT_INV_ANIM:
			// TODO: We may not need a separate ActionMode2 enum
			_mainObjectSource = userInterface._category;
			_hotspotId = _pickedWord;
			break;
		default:
			break;
		}
		break;

	case AWAITING_THAT:
		switch (userInterface._category) {
		case CAT_INV_LIST:
		case CAT_HOTSPOT:
		case CAT_INV_ANIM:
			_secondObjectSource = userInterface._category;
			_secondObject = _pickedWord;
			break;
		default:
			break;
		}
		break;

	default:
		break;
	}
}

void MADSAction::leftClick() {
	Scene &scene = _vm->_game->_scene;
	UserInterface &userInterface = scene._userInterface;
	bool abortFlag = false;

	if ((userInterface._category == CAT_COMMAND || userInterface._category == CAT_INV_VOCAB) &&
		_interAwaiting != 1 && _pickedWord >= 0 &&
			_recentCommandSource == userInterface._category && _recentCommand == _pickedWord &&
			(_interAwaiting == 2 || userInterface._category == CAT_INV_VOCAB)) {
		abortFlag = true;
		if (_selectedRow == 0 && userInterface._category == CAT_COMMAND) {
			_selectedAction = CAT_COMMAND;
			scene._lookFlag = true;
		} else {
			_selectedAction = CAT_NONE;
			scene._lookFlag = false;
			clear();
		}
	}

	if (abortFlag || (_vm->_events->_rightMousePressed && (userInterface._category == CAT_COMMAND ||
			userInterface._category == CAT_INV_VOCAB)))
		return;

	switch (_interAwaiting) {
	case AWAITING_COMMAND:
		switch (userInterface._category) {
		case CAT_COMMAND:
			if (_selectedRow >= 0) {
				if (_verbType == VERB_ONLY) {
					_selectedAction = -1;
				}
				else {
					_recentCommand = _selectedRow;
					_recentCommandSource = _commandSource;
					_interAwaiting = AWAITING_THIS;
				}
			}
			break;

		case CAT_INV_LIST:
			if (_pickedWord >= 0) {
				userInterface.selectObject(_pickedWord);
			}
			break;

		case CAT_INV_VOCAB:
			if (_selectedRow >= 0) {
				if (_verbType != VERB_THIS || _prepType != PREP_NONE) {
					if (_verbType != VERB_THAT || _prepType == PREP_NONE) {
						_interAwaiting = AWAITING_THAT;
						_articleNumber = _prepType;
					} else {
						_articleNumber = _prepType;
						_selectedAction = -1;
					}
				} else {
					_selectedAction = -1;
				}

				_recentCommand = _selectedRow;
				_recentCommandSource = _commandSource;
			}
			break;

		case CAT_HOTSPOT:
			_recentCommand = -1;
			_recentCommandSource = CAT_NONE;

			if (_vm->_events->currentPos().y < MADS_SCENE_HEIGHT) {
				scene._customDest = _vm->_events->currentPos() + scene._posAdjust;
				_selectedAction = -1;
				_pointEstablished = true;
			}
			break;

		case CAT_TALK_ENTRY:
			if (_selectedRow >= 0)
				_selectedAction = -1;
			break;

		default:
			break;
		}
		break;

	case AWAITING_THIS:
		switch (userInterface._category) {
		case CAT_INV_LIST:
		case CAT_HOTSPOT:
		case CAT_INV_ANIM:
			if (_hotspotId >= 0) {
				if (_prepType) {
					_articleNumber = _prepType;
					_interAwaiting = AWAITING_THAT;
				} else {
					_selectedAction = -1;
				}

				if (userInterface._category == CAT_HOTSPOT) {
					scene._customDest = _vm->_events->mousePos() + scene._posAdjust;
					_pointEstablished = true;
				}
			}
			break;
		default:
			break;
		}
		break;

	case AWAITING_THAT:
		switch (userInterface._category) {
		case CAT_INV_LIST:
		case CAT_HOTSPOT:
		case CAT_INV_ANIM:
			if (_secondObject >= 0) {
				_selectedAction = -1;

				if (userInterface._category == CAT_HOTSPOT) {
					if (!_pointEstablished) {
						scene._customDest = _vm->_events->mousePos() + scene._posAdjust;
						_pointEstablished = true;
					}
				}
			}
			break;
		default:
			break;
		}
		break;

	default:
		break;
	}
}

} // End of namespace MADS