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
|
/* 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 "lastexpress/game/logic.h"
// Data
#include "lastexpress/data/animation.h"
#include "lastexpress/data/cursor.h"
#include "lastexpress/data/snd.h"
// Entities
#include "lastexpress/entities/chapters.h"
// Fight
#include "lastexpress/fight/fight.h"
// Game
#include "lastexpress/game/action.h"
#include "lastexpress/game/beetle.h"
#include "lastexpress/game/entities.h"
#include "lastexpress/game/inventory.h"
#include "lastexpress/game/object.h"
#include "lastexpress/game/savegame.h"
#include "lastexpress/game/savepoint.h"
#include "lastexpress/game/scenes.h"
#include "lastexpress/game/state.h"
#include "lastexpress/menu/menu.h"
#include "lastexpress/sound/queue.h"
#include "lastexpress/sound/sound.h"
#include "lastexpress/graphics.h"
#include "lastexpress/helpers.h"
#include "lastexpress/lastexpress.h"
#include "lastexpress/resource.h"
namespace LastExpress {
#define EVENT_TICKS_BEETWEEN_SAVEGAMES 450
#define GAME_TICKS_BEETWEEN_SAVEGAMES 2700
Logic::Logic(LastExpressEngine *engine) : _engine(engine) {
_action = new Action(engine);
_beetle = new Beetle(engine);
_entities = new Entities(engine);
_fight = new Fight(engine);
_saveload = new SaveLoad(engine);
_state = new State(engine);
// Flags
_flagActionPerformed = false;
_ignoreFrameInterval = false;
_ticksSinceLastSavegame = EVENT_TICKS_BEETWEEN_SAVEGAMES;
}
Logic::~Logic() {
SAFE_DELETE(_action);
SAFE_DELETE(_beetle);
SAFE_DELETE(_fight);
SAFE_DELETE(_entities);
SAFE_DELETE(_saveload);
SAFE_DELETE(_state);
// Zero-out passed pointers
_engine = NULL;
}
//////////////////////////////////////////////////////////////////////////
// Event Handling
//////////////////////////////////////////////////////////////////////////
#define REDRAW_CURSOR() { \
if (getInventory()->isMagnifierInUse()) \
_engine->getCursor()->setStyle(kCursorMagnifier); \
if (getInventory()->isPortraitHighlighted() \
|| getInventory()->isOpened() \
|| getInventory()->isEggHighlighted()) \
_engine->getCursor()->setStyle(kCursorNormal); \
return; \
}
void Logic::eventMouse(const Common::Event &ev) {
bool hotspotHandled = false;
// Reset mouse flags
getFlags()->mouseLeftClick = false;
getFlags()->mouseRightClick = false;
// Process event flags
if (ev.type == Common::EVENT_LBUTTONDOWN) {
if (getFlags()->frameInterval)
_ignoreFrameInterval = false;
getFlags()->frameInterval = false;
}
if (getFlags()->flag_0) {
if (ev.type == Common::EVENT_LBUTTONDOWN || ev.type == Common::EVENT_RBUTTONDOWN) {
getFlags()->flag_0 = false;
getFlags()->shouldRedraw = true;
updateCursor(true);
getFlags()->frameInterval = true;
}
return;
}
if (_ignoreFrameInterval && getScenes()->checkCurrentPosition(true) && _engine->getCursor()->getStyle() == kCursorForward) {
getFlags()->shouldRedraw = false;
getFlags()->flag_0 = true;
return;
}
// Update coordinates
getGameState()->setCoordinates(ev.mouse);
// Handle inventory
getInventory()->handleMouseEvent(ev);
// Stop processing is inside the menu
if (getMenu()->isShown())
return;
// Handle whistle case
if (getInventory()->getSelectedItem() == kItemWhistle
&& !getProgress().isEggOpen
&& !getEntities()->isPlayerPosition(kCarGreenSleeping, 59)
&& !getEntities()->isPlayerPosition(kCarGreenSleeping, 76)
&& !getInventory()->isPortraitHighlighted()
&& !getInventory()->isOpened()
&& !getInventory()->isEggHighlighted()
&& !getInventory()->isMagnifierInUse()) {
// Update cursor
_engine->getCursor()->setStyle(getInventory()->get(kItemWhistle)->cursor);
// Check if clicked
if (ev.type == Common::EVENT_LBUTTONUP && !getSoundQueue()->isBuffered("LIB045")) {
getSound()->playSoundEvent(kEntityPlayer, 45);
if (getEntities()->isPlayerPosition(kCarGreenSleeping, 26) || getEntities()->isPlayerPosition(kCarGreenSleeping, 25) || getEntities()->isPlayerPosition(kCarGreenSleeping, 23)) {
getSavePoints()->push(kEntityPlayer, kEntityMertens, kAction226078300);
} else if (getEntities()->isPlayerPosition(kCarRedSleeping, 26) || getEntities()->isPlayerPosition(kCarRedSleeping, 25) || getEntities()->isPlayerPosition(kCarRedSleeping, 23)) {
getSavePoints()->push(kEntityPlayer, kEntityCoudert, kAction226078300);
}
if (!getState()->sceneUseBackup)
getInventory()->unselectItem();
}
REDRAW_CURSOR()
}
// Handle match case
if (getInventory()->getSelectedItem() == kItemMatch
&& (getEntities()->isPlayerInCar(kCarGreenSleeping) || getEntities()->isPlayerInCar(kCarRedSleeping))
&& getProgress().jacket == kJacketGreen
&& !getInventory()->isPortraitHighlighted()
&& !getInventory()->isOpened()
&& !getInventory()->isEggHighlighted()
&& !getInventory()->isMagnifierInUse()
&& (getInventory()->get(kItem2)->location == kObjectLocationNone || getEntityData(kEntityPlayer)->car != kCarRedSleeping || getEntityData(kEntityPlayer)->entityPosition != kPosition_2300)) {
// Update cursor
_engine->getCursor()->setStyle(getInventory()->get(kItemMatch)->cursor);
if (ev.type == Common::EVENT_LBUTTONUP) {
getAction()->playAnimation(isNight() ? kEventCathSmokeNight : kEventCathSmokeDay);
if (!getState()->sceneUseBackup)
getInventory()->unselectItem();
getScenes()->processScene();
}
REDRAW_CURSOR()
}
// Handle entity item case
EntityIndex entityIndex = getEntities()->canInteractWith(ev.mouse);
if (entityIndex
&& !getInventory()->isPortraitHighlighted()
&& !getInventory()->isOpened()
&& !getInventory()->isEggHighlighted()
&& !getInventory()->isMagnifierInUse()) {
InventoryItem item = getEntityData(entityIndex)->inventoryItem;
if (getInventory()->hasItem((InventoryItem)(item & kItemToggleHigh))) {
hotspotHandled = true;
_engine->getCursor()->setStyle(getInventory()->get((InventoryItem)(item & kItemToggleHigh))->cursor);
if (ev.type == Common::EVENT_LBUTTONUP)
getSavePoints()->push(kEntityPlayer, entityIndex, kAction1, (InventoryItem)(item & kItemToggleHigh));
} else if ((InventoryItem)(item & kItemInvalid)) {
hotspotHandled = true;
_engine->getCursor()->setStyle(kCursorTalk2);
if (ev.type == Common::EVENT_LBUTTONUP)
getSavePoints()->push(kEntityPlayer, entityIndex, kAction1, kCursorNormal);
}
}
//////////////////////////////////////////////////////////////////////////
// Handle standard actions
if (getInventory()->isPortraitHighlighted() || getInventory()->isOpened() || getInventory()->isEggHighlighted())
_engine->getCursor()->setStyle(kCursorNormal);
if (hotspotHandled || getInventory()->isPortraitHighlighted() || getInventory()->isOpened() || getInventory()->isEggHighlighted())
return;
// Magnifier in use
if (getInventory()->isMagnifierInUse()) {
_engine->getCursor()->setStyle(kCursorMagnifier);
if (getInventory()->isPortraitHighlighted()
|| getInventory()->isOpened()
|| getInventory()->isEggHighlighted())
_engine->getCursor()->setStyle(kCursorNormal);
return;
}
// Check hotspots
int location = 0;
SceneHotspot *hotspot = NULL;
Scene *scene = getScenes()->get(getState()->scene);
for (Common::Array<SceneHotspot *>::iterator it = scene->getHotspots()->begin(); it != scene->getHotspots()->end(); ++it) {
if (!(*it)->isInside(ev.mouse))
continue;
if ((*it)->location < location)
continue;
if (!getAction()->getCursor(**it))
continue;
Scene *hotspotScene = getScenes()->get((*it)->scene);
if (!getEntities()->getPosition(hotspotScene->car, hotspotScene->position)
|| (*it)->cursor == kCursorTurnRight
|| (*it)->cursor == kCursorTurnLeft) {
location = (*it)->location;
hotspot = *it;
}
}
// No hotspot found: show the normal cursor
if (!hotspot) {
_engine->getCursor()->setStyle(kCursorNormal);
return;
}
// Found an hotspot: update the cursor and perform the action if the user clicked the mouse
_engine->getCursor()->setStyle(getAction()->getCursor(*hotspot));
if (ev.type != Common::EVENT_LBUTTONUP || _flagActionPerformed)
return;
_flagActionPerformed = true;
SceneIndex processedScene = getAction()->processHotspot(*hotspot);
SceneIndex testScene = (processedScene == kSceneInvalid) ? hotspot->scene : processedScene;
if (testScene) {
getFlags()->shouldRedraw = false;
getScenes()->setScene(testScene);
if (getFlags()->shouldDrawEggOrHourGlass)
getInventory()->drawEgg();
getFlags()->shouldRedraw = true;
updateCursor(true);
}
// Switch to next chapter if necessary
if (hotspot->action == SceneHotspot::kActionSwitchChapter && hotspot->param1 == getState()->progress.chapter)
switchChapter();
}
void Logic::eventTick(const Common::Event &) {
uint ticks = 1;
//////////////////////////////////////////////////////////////////////////
// Adjust ticks if an action has been performed
if (_flagActionPerformed)
ticks = 10;
_flagActionPerformed = false;
//////////////////////////////////////////////////////////////////////////
// Draw the blinking egg if needed
if (getGlobalTimer() && !getFlags()->shouldDrawEggOrHourGlass)
getInventory()->drawBlinkingEgg();
//////////////////////////////////////////////////////////////////////////
// Adjust time and save game if needed
if (getFlags()->isGameRunning) {
getState()->timeTicks += ticks;
getState()->time = (TimeValue)(getState()->time + (TimeValue)(ticks * getState()->timeDelta));
if (getState()->timeDelta) {
// Auto-save
if (!_ticksSinceLastSavegame) {
_ticksSinceLastSavegame = EVENT_TICKS_BEETWEEN_SAVEGAMES;
getSaveLoad()->saveGame(kSavegameTypeAuto, kEntityChapters, kEventNone);
}
// Save after game ticks interval
if ((getState()->timeTicks - getSaveLoad()->getLastSavegameTicks()) > GAME_TICKS_BEETWEEN_SAVEGAMES)
getSaveLoad()->saveGame(kSavegameTypeTickInterval, kEntityChapters, kEventNone);
}
}
//////////////////////////////////////////////////////////////////////////
// Load scene and process hotspot
if (getFlags()->flag_0 && !getFlags()->mouseLeftClick && !getFlags()->mouseRightClick) {
Scene *scene = getScenes()->get(getState()->scene);
if (getScenes()->checkCurrentPosition(true)
&& !getEntities()->getPosition(scene->car, scene->position)) {
// Process hotspot
SceneHotspot *hotspot = scene->getHotspot();
SceneIndex processedScene = getAction()->processHotspot(*hotspot);
SceneIndex testScene = (processedScene == kSceneInvalid) ? hotspot->scene : processedScene;
if (testScene) {
getScenes()->setScene(testScene);
} else {
getFlags()->flag_0 = false;
getFlags()->shouldRedraw = true;
updateCursor(true);
}
if (getFlags()->isGameRunning)
getSavePoints()->callAndProcess();
} else {
getFlags()->flag_0 = false;
getFlags()->shouldRedraw = true;
updateCursor(true);
}
return;
}
// Stop processing if the game is paused
if (!getFlags()->isGameRunning)
return;
//////////////////////////////////////////////////////////////////////////
// Update beetle, savepoints, entities and draw frames
if (_beetle->isLoaded())
_beetle->update();
getSavePoints()->callAndProcess();
getEntities()->updateCallbacks();
getScenes()->drawFrames(true);
//////////////////////////////////////////////////////////////////////////
// Update cursor if we can interact with an entity
EntityIndex entity = getEntities()->canInteractWith(getCoords());
if (!entity) {
if (_engine->getCursor()->getStyle() >= kCursorTalk2)
updateCursor(false);
return;
}
// Show item cursor on entity
if (getInventory()->hasItem((InventoryItem)(getEntityData(entity)->inventoryItem & kItemToggleHigh)) && (int)getEntityData(entity)->inventoryItem != (int)kCursorTalk2) {
_engine->getCursor()->setStyle(getInventory()->get((InventoryItem)(getEntityData(entity)->inventoryItem & kItemToggleHigh))->cursor);
return;
}
getLogic()->updateCursor(false);
_engine->getCursor()->setStyle(kCursorTalk2);
}
//////////////////////////////////////////////////////////////////////////
// Game over, Chapters & credits
//////////////////////////////////////////////////////////////////////////
/**
* Resets the game state.
*/
void Logic::resetState() {
getState()->scene = kSceneDefault;
warning("[Logic::resetState] Not implemented! You need to restart the engine until this is implemented.");
}
/**
* Handle game over
*
* @param type The savegame type.
* @param value The value (event, time, index, ...)
* @param sceneIndex Index of the scene to show.
* @param showScene true to show a scene, false to return to menu directly
*/
void Logic::gameOver(SavegameType type, uint32 value, SceneIndex sceneIndex, bool showScene) const {
getSoundQueue()->processEntries();
getEntities()->reset();
getFlags()->isGameRunning = false;
getSavePoints()->reset();
getFlags()->flag_entities_0 = true;
if (showScene) {
getSoundQueue()->processEntry(kSoundType11);
if (sceneIndex && !getFlags()->mouseRightClick) {
getScenes()->loadScene(sceneIndex);
while (getSoundQueue()->isBuffered(kEntityTables4)) {
if (getFlags()->mouseRightClick)
break;
getSoundQueue()->updateQueue();
}
}
}
// Show Menu
getMenu()->show(false, type, value);
}
void Logic::switchChapter() const {
getSoundQueue()->clearStatus();
switch(getState()->progress.chapter) {
default:
break;
case kChapter1:
getInventory()->addItem(kItemParchemin);
getInventory()->addItem(kItemMatchBox);
RESET_ENTITY_STATE(kEntityChapters, Chapters, setup_chapter2);
break;
case kChapter2:
getInventory()->addItem(kItemScarf);
RESET_ENTITY_STATE(kEntityChapters, Chapters, setup_chapter3);
break;
case kChapter3:
getInventory()->get(kItemFirebird)->location = kObjectLocation4;
getInventory()->get(kItemFirebird)->isPresent = false;
getInventory()->get(kItem11)->location = kObjectLocation1;
getInventory()->addItem(kItemWhistle);
getInventory()->addItem(kItemKey);
RESET_ENTITY_STATE(kEntityChapters, Chapters, setup_chapter4);
break;
case kChapter4:
RESET_ENTITY_STATE(kEntityChapters, Chapters, setup_chapter5);
break;
case kChapter5:
playFinalSequence();
break;
}
}
void Logic::playFinalSequence() const {
getSoundQueue()->processEntries();
_action->playAnimation(kEventFinalSequence);
showCredits();
getEntities()->reset();
getSavePoints()->reset();
getFlags()->flag_entities_0 = true;
getMenu()->show(false, kSavegameTypeIndex, 0);
}
void Logic::showCredits() const {
error("[Logic::showCredits] Not implemented");
}
//////////////////////////////////////////////////////////////////////////
// Misc
//////////////////////////////////////////////////////////////////////////
void Logic::updateCursor(bool) const { /* the cursor is always updated, even when we don't want to redraw it */
CursorStyle style = kCursorNormal;
bool interact = false;
if (getInventory()->getSelectedItem() != kItemWhistle
|| getProgress().isEggOpen
|| getEntities()->isPlayerPosition(kCarGreenSleeping, 59)
|| getEntities()->isPlayerPosition(kCarGreenSleeping, 76)
|| getInventory()->isPortraitHighlighted()
|| getInventory()->isOpened()
|| getInventory()->isEggHighlighted()
|| getInventory()->isMagnifierInUse()) {
if (getInventory()->getSelectedItem() != kItemMatch
|| (!getEntities()->isPlayerInCar(kCarGreenSleeping) && !getEntities()->isPlayerInCar(kCarRedSleeping))
|| getProgress().jacket != kJacketGreen
|| getInventory()->isPortraitHighlighted()
|| getInventory()->isOpened()
|| getInventory()->isEggHighlighted()
|| getInventory()->isMagnifierInUse()
|| (getInventory()->get(kItem2)->location
&& getEntityData(kEntityPlayer)->car == kCarRedSleeping
&& getEntityData(kEntityPlayer)->entityPosition == kPosition_2300)) {
EntityIndex entity = getEntities()->canInteractWith(getCoords());
if (entity
&& !getInventory()->isPortraitHighlighted()
&& !getInventory()->isOpened()
&& !getInventory()->isEggHighlighted()
&& !getInventory()->isMagnifierInUse()) {
if (getInventory()->hasItem((InventoryItem)(getEntityData(entity)->inventoryItem & kItemToggleHigh))) {
interact = true;
style = getInventory()->get((InventoryItem)(getEntityData(entity)->inventoryItem & kItemToggleHigh))->cursor;
} else if ((int)getEntityData(entity)->inventoryItem == kItemInvalid) {
interact = true;
style = kCursorTalk2;
}
}
if (!interact
&& !getInventory()->isPortraitHighlighted()
&& !getInventory()->isOpened()
&& !getInventory()->isEggHighlighted()
&& !getInventory()->isMagnifierInUse()) {
int location = 0;
SceneHotspot *hotspot = NULL;
Scene *scene = getScenes()->get(getState()->scene);
// Check all hotspots
for (Common::Array<SceneHotspot *>::iterator i = scene->getHotspots()->begin(); i != scene->getHotspots()->end(); ++i) {
if ((*i)->isInside(getCoords()) && (*i)->location >= location) {
if (getAction()->getCursor(**i)) {
Scene *hotspotScene = getScenes()->get((*i)->scene);
if (!getEntities()->getPosition(hotspotScene->car, hotspotScene->position)
|| (*i)->cursor == kCursorTurnRight
|| (*i)->cursor == kCursorTurnLeft) {
hotspot = *i;
location = (*i)->location;
}
}
}
}
style = (hotspot) ? getAction()->getCursor(*hotspot) : kCursorNormal;
}
} else {
style = getInventory()->get(kItemMatch)->cursor;
}
} else {
style = getInventory()->get(kItemWhistle)->cursor;
}
if (getInventory()->isMagnifierInUse())
style = kCursorMagnifier;
if (getInventory()->isPortraitHighlighted() || getInventory()->isOpened() || getInventory()->isEggHighlighted())
style = kCursorNormal;
_engine->getCursor()->setStyle(style);
}
} // End of namespace LastExpress
|