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
|
/* 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.
*
* Additional copyright for this file:
* Copyright (C) 1995-1997 Presto Studios, Inc.
*
* 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/memstream.h"
#include "pegasus/cursor.h"
#include "pegasus/pegasus.h"
#include "pegasus/ai/ai_area.h"
#include "pegasus/items/biochips/aichip.h"
#include "pegasus/items/biochips/biochipitem.h"
#include "pegasus/items/biochips/opticalchip.h"
#include "pegasus/items/biochips/pegasuschip.h"
#include "pegasus/items/inventory/airmask.h"
#include "pegasus/items/inventory/inventoryitem.h"
namespace Pegasus {
AIArea *g_AIArea = 0;
AIArea::AIArea(InputHandler *nextHandler) : InputHandler(nextHandler), _leftAreaMovie(kAILeftAreaID),
_middleAreaMovie(kAIMiddleAreaID), _rightAreaMovie(kAIRightAreaID), _AIMovie(kAIMovieID) {
g_AIArea = this;
_leftAreaOwner = kNoClientSignature;
_middleAreaOwner = kNoClientSignature;
_rightAreaOwner = kNoClientSignature;
_leftInventoryTime = 0xffffffff;
_middleInventoryTime = 0xffffffff;
_middleBiochipTime = 0xffffffff;
_rightBiochipTime = 0xffffffff;
_lockCount = 0;
startIdling();
}
AIArea::~AIArea() {
if (_middleAreaOwner == kBiochipSignature) {
BiochipItem *currentBiochip = ((PegasusEngine *)g_engine)->getCurrentBiochip();
if (currentBiochip && currentBiochip->isSelected())
currentBiochip->giveUpSharedArea();
} else if (_middleAreaOwner == kInventorySignature) {
InventoryItem *currentItem = ((PegasusEngine *)g_engine)->getCurrentInventoryItem();
if (currentItem && currentItem->isSelected())
currentItem->giveUpSharedArea();
}
stopIdling();
for (AIRuleList::iterator it = _AIRules.begin(); it != _AIRules.end(); it++)
delete *it;
g_AIArea = 0;
}
// Save last state of AI rules...
void AIArea::saveAIState() {
PegasusEngine *vm = (PegasusEngine *)g_engine;
delete vm->_aiSaveStream;
Common::MemoryWriteStreamDynamic out;
writeAIRules(&out);
vm->_aiSaveStream = new Common::MemoryReadStream(out.getData(), out.size(), DisposeAfterUse::YES);
}
void AIArea::restoreAIState() {
PegasusEngine *vm = (PegasusEngine *)g_engine;
if (vm->_aiSaveStream)
readAIRules(vm->_aiSaveStream);
}
void AIArea::writeAIRules(Common::WriteStream *stream) {
_AIRules.writeAIRules(stream);
}
void AIArea::readAIRules(Common::ReadStream *stream) {
_AIRules.readAIRules(stream);
}
void AIArea::initAIArea() {
allocateSurface(Common::Rect(0, 0, 384, 96));
_leftAreaMovie.shareSurface(this);
_leftAreaMovie.initFromMovieFile("Images/Items/Left Area Movie");
_leftAreaMovie.moveElementTo(kAILeftAreaLeft, kAILeftAreaTop);
_leftAreaMovie.setDisplayOrder(kAILeftAreaOrder);
_leftAreaMovie.startDisplaying();
_leftAreaMovie.setVolume(((PegasusEngine *)g_engine)->getSoundFXLevel());
_middleAreaMovie.shareSurface(this);
_middleAreaMovie.initFromMovieFile("Images/Items/Middle Area Movie");
_middleAreaMovie.moveElementTo(kAIMiddleAreaLeft, kAIMiddleAreaTop);
_middleAreaMovie.moveMovieBoxTo(kAIMiddleAreaLeft - kAILeftAreaLeft, 0);
_middleAreaMovie.setDisplayOrder(kAIMiddleAreaOrder);
_middleAreaMovie.startDisplaying();
_middleAreaMovie.setVolume(((PegasusEngine *)g_engine)->getSoundFXLevel());
_rightAreaMovie.shareSurface(this);
_rightAreaMovie.initFromMovieFile("Images/Items/Right Area Movie");
_rightAreaMovie.moveElementTo(kAIRightAreaLeft, kAIRightAreaTop);
_rightAreaMovie.moveMovieBoxTo(kAIRightAreaLeft - kAILeftAreaLeft, 0);
_rightAreaMovie.setDisplayOrder(kAIRightAreaOrder);
_rightAreaMovie.startDisplaying();
_rightAreaMovie.setVolume(((PegasusEngine *)g_engine)->getSoundFXLevel());
_AIMovie.setDisplayOrder(kAIMovieOrder);
}
void AIArea::setAIVolume(const uint16 volume) {
_leftAreaMovie.setVolume(volume);
_middleAreaMovie.setVolume(volume);
_rightAreaMovie.setVolume(volume);
}
// There are only so many legal combinations of client/area.
// Here is the list of supported pairs:
// kInventorySignature kLeftAreaSignature
// kInventorySignature kMiddleAreaSignature
// kBiochipSignature kMiddleAreaSignature
// kBiochipSignature kRightAreaSignature
// kAISignature kLeftAreaSignature
// Further, the kAISignature never sets a static frame time in the left area,
// but only plays a sequence.
// If this function is called while a sequence is playing, it will just "remember"
// the time value, so that when the sequence finishes, the new time is asserted.
void AIArea::setAIAreaToTime(const LowerClientSignature client, const LowerAreaSignature area, const TimeValue time) {
switch (area) {
case kLeftAreaSignature:
// Only support kInventorySignature client, since AI never calls SetAIAreaToTime.
_leftAreaMovie.setSegment(0, _leftAreaMovie.getDuration());
if (time == 0xffffffff) {
_leftAreaMovie.hide();
_leftAreaOwner = kNoClientSignature;
} else {
setLeftMovieTime(time);
}
break;
case kMiddleAreaSignature:
// Only support kInventorySignature and kBiochipSignature clients.
_middleAreaMovie.stop();
_middleAreaMovie.setFlags(0);
_middleAreaMovie.setSegment(0, _middleAreaMovie.getDuration());
if (time == 0xffffffff) {
if (client == kInventorySignature) {
if (_middleBiochipTime != 0xffffffff) {
setMiddleMovieTime(kBiochipSignature, _middleBiochipTime);
} else {
_middleAreaMovie.hide();
_middleAreaOwner = kNoClientSignature;
}
} else { // client == kBiochipSignature
if (_middleInventoryTime != 0xffffffff) {
setMiddleMovieTime(kInventorySignature, _middleInventoryTime);
} else {
_middleAreaMovie.hide();
_middleAreaOwner = kNoClientSignature;
}
}
} else {
setMiddleMovieTime(client, time);
}
break;
case kRightAreaSignature:
// Only support kBiochipSignature client.
_rightAreaMovie.setSegment(0, _rightAreaMovie.getDuration());
if (time == 0xffffffff) {
_rightAreaMovie.hide();
_rightAreaOwner = kNoClientSignature;
} else {
setRightMovieTime(time);
}
break;
}
}
// Plays a sequence on an area. When the sequence ends, the previous image
// is restored.
// Also, is input disabled or not?
// Easy answer: yes.
// There are only so many legal combinations of client/area.
// Here is the list of supported pairs:
// kBiochipSignature kMiddleAreaSignature
// kBiochipSignature kRightAreaSignature
// kInventorySignature kMiddleAreaSignature
void AIArea::playAIAreaSequence(const LowerClientSignature, const LowerAreaSignature area, const TimeValue start, const TimeValue stop) {
PegasusEngine *vm = (PegasusEngine *)g_engine;
lockAIOut();
switch (area) {
case kLeftAreaSignature:
break;
case kMiddleAreaSignature:
if (_middleAreaOwner == kInventorySignature)
_middleInventoryTime = _middleAreaMovie.getTime();
else if (_middleAreaOwner == kBiochipSignature)
_middleBiochipTime = _middleAreaMovie.getTime();
_middleAreaMovie.stop();
_middleAreaMovie.setFlags(0);
_middleAreaMovie.setSegment(start, stop);
_middleAreaMovie.setTime(start);
_middleAreaMovie.show();
_middleAreaMovie.start();
vm->_cursor->hide();
while (_middleAreaMovie.isRunning()) {
InputDevice.pumpEvents();
vm->checkCallBacks();
vm->refreshDisplay();
g_system->delayMillis(10);
}
_middleAreaMovie.stop();
vm->_cursor->hideUntilMoved();
if (_middleAreaOwner == kInventorySignature)
setAIAreaToTime(_middleAreaOwner, kMiddleAreaSignature, _middleInventoryTime);
else if (_middleAreaOwner == kBiochipSignature)
setAIAreaToTime(_middleAreaOwner, kMiddleAreaSignature, _middleBiochipTime);
else
setAIAreaToTime(_middleAreaOwner, kMiddleAreaSignature, 0xffffffff);
break;
case kRightAreaSignature:
_rightBiochipTime = _rightAreaMovie.getTime();
_rightAreaMovie.setSegment(start, stop);
_rightAreaMovie.setTime(start);
_rightAreaMovie.show();
_rightAreaMovie.start();
vm->_cursor->hide();
while (_rightAreaMovie.isRunning()) {
InputDevice.pumpEvents();
vm->checkCallBacks();
vm->refreshDisplay();
g_system->delayMillis(10);
}
_rightAreaMovie.stop();
vm->_cursor->hideUntilMoved();
setAIAreaToTime(_rightAreaOwner, kRightAreaSignature, _rightBiochipTime);
break;
}
unlockAI();
}
bool AIArea::playAIMovie(const LowerAreaSignature area, const Common::String &movieName, bool keepLastFrame, const InputBits interruptFilter) {
PegasusEngine *vm = (PegasusEngine *)g_engine;
lockAIOut();
InputDevice.waitInput(interruptFilter);
if (_AIMovie.isMovieValid())
_AIMovie.releaseMovie();
_AIMovie.shareSurface(this);
_AIMovie.initFromMovieFile(movieName);
if (area == kLeftAreaSignature) {
_AIMovie.moveElementTo(kAILeftAreaLeft, kAILeftAreaTop);
_leftAreaMovie.hide();
} else {
_AIMovie.moveElementTo(kAIRightAreaLeft, kAIRightAreaTop);
_AIMovie.moveMovieBoxTo(kAIRightAreaLeft - kAILeftAreaLeft, 0);
_rightAreaMovie.hide();
}
_AIMovie.setTime(0);
_AIMovie.startDisplaying();
_AIMovie.show();
_AIMovie.redrawMovieWorld();
_AIMovie.setVolume(vm->getSoundFXLevel());
_AIMovie.start();
vm->_cursor->hide();
bool result = true;
bool saveAllowed = vm->swapSaveAllowed(false);
bool openAllowed = vm->swapLoadAllowed(false);
while (_AIMovie.isRunning()) {
Input input;
InputDevice.getInput(input, interruptFilter);
if (input.anyInput() || vm->shouldQuit() || vm->saveRequested() || vm->loadRequested()) {
result = false;
break;
}
vm->checkCallBacks();
vm->refreshDisplay();
g_system->delayMillis(10);
}
_AIMovie.stop();
vm->swapSaveAllowed(saveAllowed);
vm->swapLoadAllowed(openAllowed);
// This used to keep the last frame up even if the movie was interrupted.
// However, this only occurs in the recalibration, where interruption means skip the
// whole thing, so skipping causes the AI to go away even when keepLastFrame is true.
if (!(result && keepLastFrame)) {
_AIMovie.stopDisplaying();
_AIMovie.releaseMovie();
if (area == kLeftAreaSignature) {
_leftAreaMovie.setTime(_leftInventoryTime);
_leftAreaMovie.show();
_leftAreaMovie.redrawMovieWorld();
} else {
_rightAreaMovie.setTime(_rightBiochipTime);
_rightAreaMovie.show();
_rightAreaMovie.redrawMovieWorld();
}
}
vm->_cursor->hideUntilMoved();
unlockAI();
return result;
}
// Only implemented for kMiddleAreaSignature, kInventorySignature
void AIArea::loopAIAreaSequence(const LowerClientSignature owner, const LowerAreaSignature area, const TimeValue start, const TimeValue stop) {
if (area == kMiddleAreaSignature && owner == kInventorySignature && owner == _middleAreaOwner) {
_middleAreaMovie.stop();
_middleAreaMovie.setFlags(0);
_middleAreaMovie.setSegment(start, stop);
_middleAreaMovie.setFlags(kLoopTimeBase);
_middleAreaMovie.setTime(start);
_middleAreaMovie.show();
_middleAreaMovie.start();
}
}
// Only called by kInventorySignature.
void AIArea::setLeftMovieTime(const TimeValue time) {
if (!_AIMovie.isSurfaceValid()) {
_leftAreaMovie.setTime(time);
_leftAreaMovie.show();
_leftAreaMovie.redrawMovieWorld();
}
_leftAreaOwner = kInventorySignature;
_leftInventoryTime = time;
}
void AIArea::setMiddleMovieTime(const LowerClientSignature client, const TimeValue time) {
if (client == kInventorySignature) {
_middleInventoryTime = time;
if (_middleAreaOwner == kBiochipSignature) {
BiochipItem *currentBiochip = ((PegasusEngine *)g_engine)->getCurrentBiochip();
if (currentBiochip && currentBiochip->isSelected())
currentBiochip->giveUpSharedArea();
}
} else {
_middleBiochipTime = time;
if (_middleAreaOwner == kInventorySignature) {
InventoryItem *currentItem = ((PegasusEngine *)g_engine)->getCurrentInventoryItem();
if (currentItem && currentItem->isSelected())
currentItem->giveUpSharedArea();
}
}
_middleAreaMovie.setSegment(0, _middleAreaMovie.getDuration());
_middleAreaMovie.stop();
_middleAreaMovie.setFlags(0);
_middleAreaMovie.setTime(time);
_middleAreaMovie.show();
_middleAreaMovie.redrawMovieWorld();
_middleAreaOwner = client;
}
// Only called by kBiochipSignature.
void AIArea::setRightMovieTime(const TimeValue time) {
if (!_AIMovie.isSurfaceValid()) {
// Can't do it when the AI movie is up...
_rightAreaMovie.setTime(time);
_rightAreaMovie.show();
_rightAreaMovie.redrawMovieWorld();
}
_rightAreaOwner = kBiochipSignature;
_rightBiochipTime = time;
}
void AIArea::handleInput(const Input &input, const Hotspot *cursorSpot) {
if (JMPPPInput::isToggleAIMiddleInput(input))
toggleMiddleAreaOwner();
else
InputHandler::handleInput(input, cursorSpot);
}
void AIArea::toggleMiddleAreaOwner() {
if (_middleAreaOwner == kInventorySignature) {
BiochipItem *currentBiochip = ((PegasusEngine *)g_engine)->getCurrentBiochip();
if (currentBiochip) {
setMiddleMovieTime(kBiochipSignature, currentBiochip->getSharedAreaTime());
currentBiochip->takeSharedArea();
}
} else if (_middleAreaOwner == kBiochipSignature) {
InventoryItem *currentItem = ((PegasusEngine *)g_engine)->getCurrentInventoryItem();
if (currentItem) {
setMiddleMovieTime(kInventorySignature, currentItem->getSharedAreaTime());
currentItem->takeSharedArea();
}
}
}
void AIArea::activateHotspots() {
PegasusEngine *vm = (PegasusEngine *)g_engine;
if (_middleAreaOwner == kBiochipSignature) {
BiochipItem *currentBiochip = ((PegasusEngine *)g_engine)->getCurrentBiochip();
if (currentBiochip)
switch (currentBiochip->getObjectID()) {
case kAIBiochip:
((AIChip *)currentBiochip)->activateAIHotspots();
break;
case kPegasusBiochip:
if (!vm->isDemo())
((PegasusChip *)currentBiochip)->activatePegasusHotspots();
break;
case kOpticalBiochip:
((OpticalChip *)currentBiochip)->activateOpticalHotspots();
break;
}
} else if (_middleAreaOwner == kInventorySignature) {
InventoryItem *currentItem = ((PegasusEngine *)g_engine)->getCurrentInventoryItem();
if (currentItem && currentItem->getObjectID() == kAirMask)
((AirMask *)currentItem)->activateAirMaskHotspots();
}
InputHandler::activateHotspots();
}
void AIArea::clickInHotspot(const Input &input, const Hotspot *hotspot) {
PegasusEngine *vm = (PegasusEngine *)g_engine;
bool handled = false;
if (_middleAreaOwner == kBiochipSignature) {
BiochipItem *currentBiochip = ((PegasusEngine *)g_engine)->getCurrentBiochip();
if (currentBiochip) {
switch (currentBiochip->getObjectID()) {
case kAIBiochip:
if ((hotspot->getHotspotFlags() & kAIBiochipSpotFlag) != 0) {
((AIChip *)currentBiochip)->clickInAIHotspot(hotspot->getObjectID());
handled = true;
}
break;
case kPegasusBiochip:
if (!vm->isDemo() && ((hotspot->getHotspotFlags() & kPegasusBiochipSpotFlag) != 0)) {
((PegasusChip *)currentBiochip)->clickInPegasusHotspot();
handled = true;
}
break;
case kOpticalBiochip:
if ((hotspot->getHotspotFlags() & kOpticalBiochipSpotFlag) != 0) {
((OpticalChip *)currentBiochip)->clickInOpticalHotspot(hotspot->getObjectID());
handled = true;
}
break;
}
}
} else if (_middleAreaOwner == kInventorySignature) {
InventoryItem *currentItem = ((PegasusEngine *)g_engine)->getCurrentInventoryItem();
if (currentItem) {
switch (currentItem->getObjectID()) {
case kAirMask:
if ((hotspot->getHotspotFlags() & kAirMaskSpotFlag) != 0) {
((AirMask *)currentItem)->clickInAirMaskHotspot();
handled = true;
}
break;
}
}
}
if (!handled)
InputHandler::clickInHotspot(input, hotspot);
}
void AIArea::lockAIOut() {
if (_lockCount == 0)
stopIdling();
_lockCount++;
}
void AIArea::unlockAI() {
if (_lockCount > 0) {
_lockCount--;
if (_lockCount == 0)
startIdling();
}
}
void AIArea::forceAIUnlocked() {
if (_lockCount > 0) {
_lockCount = 1;
unlockAI();
}
}
void AIArea::checkRules() {
if (_lockCount == 0 && ((PegasusEngine *)g_engine)->playerAlive())
for (AIRuleList::iterator it = _AIRules.begin(); it != _AIRules.end(); it++)
if ((*it)->fireRule())
break;
}
void AIArea::useIdleTime() {
checkRules();
}
void AIArea::addAIRule(AIRule *rule) {
_AIRules.push_back(rule);
}
void AIArea::removeAllRules() {
for (AIRuleList::iterator it = _AIRules.begin(); it != _AIRules.end(); it++)
delete *it;
_AIRules.clear();
}
void AIArea::checkMiddleArea() {
BiochipItem *currentBiochip = ((PegasusEngine *)g_engine)->getCurrentBiochip();
if (currentBiochip) {
if (_middleAreaOwner == kBiochipSignature) {
switch (currentBiochip->getObjectID()) {
case kAIBiochip:
((AIChip *)currentBiochip)->setUpAIChip();
break;
case kPegasusBiochip:
((PegasusChip *)currentBiochip)->setUpPegasusChip();
break;
}
} else {
switch (currentBiochip->getObjectID()) {
case kAIBiochip:
((AIChip *)currentBiochip)->setUpAIChipRude();
break;
case kPegasusBiochip:
((PegasusChip *)currentBiochip)->setUpPegasusChipRude();
break;
}
}
}
}
TimeValue AIArea::getBigInfoTime() {
if (_middleAreaOwner == kInventorySignature) {
InventoryItem *currentItem = ((PegasusEngine *)g_engine)->getCurrentInventoryItem();
return currentItem->getInfoLeftTime();
} else if (_middleAreaOwner == kBiochipSignature) {
BiochipItem *currentBiochip = ((PegasusEngine *)g_engine)->getCurrentBiochip();
return currentBiochip->getInfoLeftTime();
}
return 0xffffffff;
}
void AIArea::getSmallInfoSegment(TimeValue &start, TimeValue &stop) {
if (_middleAreaOwner == kInventorySignature) {
InventoryItem *currentItem = ((PegasusEngine *)g_engine)->getCurrentInventoryItem();
currentItem->getInfoRightTimes(start, stop);
} else if (_middleAreaOwner == kBiochipSignature) {
BiochipItem *currentBiochip = ((PegasusEngine *)g_engine)->getCurrentBiochip();
currentBiochip->getInfoRightTimes(start, stop);
} else {
start = 0xffffffff;
stop = 0xffffffff;
}
}
LowerClientSignature AIArea::getMiddleAreaOwner() {
return _middleAreaOwner;
}
} // End of namespace Pegasus
|