forked from scummvm/scummvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.cpp
2489 lines (1911 loc) · 61.5 KB
/
function.cpp
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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* 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) 1994-1998 Revolution Software Ltd.
*
* 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/system.h"
#include "common/file.h"
#include "common/textconsole.h"
#include "sword2/sword2.h"
#include "sword2/defs.h"
#include "sword2/header.h"
#include "sword2/screen.h"
#include "sword2/console.h"
#include "sword2/interpreter.h"
#include "sword2/logic.h"
#include "sword2/maketext.h"
#include "sword2/mouse.h"
#include "sword2/resman.h"
#include "sword2/router.h"
#include "sword2/sound.h"
#include "sword2/animation.h"
namespace Sword2 {
int32 Logic::fnTestFunction(int32 *params) {
// params: 0 address of a flag
return IR_CONT;
}
int32 Logic::fnTestFlags(int32 *params) {
// params: 0 value of flag
return IR_CONT;
}
int32 Logic::fnRegisterStartPoint(int32 *params) {
// params: 0 id of startup script to call - key
// 1 pointer to ascii message
int32 key = params[0];
char *name = (char *)decodePtr(params[1]);
_vm->registerStartPoint(key, name);
return IR_CONT;
}
int32 Logic::fnInitBackground(int32 *params) {
// this screen defines the size of the back buffer
// params: 0 res id of normal background layer - cannot be 0
// 1 1 yes 0 no for a new palette
if (Sword2Engine::isPsx())
_vm->_screen->initPsxBackground(params[0], params[1]);
else
_vm->_screen->initBackground(params[0], params[1]);
return IR_CONT;
}
/**
* This function is used by start scripts.
*/
int32 Logic::fnSetSession(int32 *params) {
// params: 0 id of new run list
expressChangeSession(params[0]);
return IR_CONT;
}
int32 Logic::fnBackSprite(int32 *params) {
// params: 0 pointer to object's graphic structure
_router->setSpriteStatus(decodePtr(params[0]), BACK_SPRITE);
return IR_CONT;
}
int32 Logic::fnSortSprite(int32 *params) {
// params: 0 pointer to object's graphic structure
_router->setSpriteStatus(decodePtr(params[0]), SORT_SPRITE);
return IR_CONT;
}
int32 Logic::fnForeSprite(int32 *params) {
// params: 0 pointer to object's graphic structure
_router->setSpriteStatus(decodePtr(params[0]), FORE_SPRITE);
return IR_CONT;
}
int32 Logic::fnRegisterMouse(int32 *params) {
// this call would be made from an objects service script 0
// the object would be one with no graphic but with a mouse - i.e. a
// floor or one whose mouse area is manually defined rather than
// intended to fit sprite shape
// params: 0 pointer to ObjectMouse or 0 for no write to mouse
// list
_vm->_mouse->registerMouse(decodePtr(params[0]), NULL);
return IR_CONT;
}
int32 Logic::fnAnim(int32 *params) {
// params: 0 pointer to object's logic structure
// 1 pointer to object's graphic structure
// 2 resource id of animation file
// Normal forward animation
return _router->doAnimate(
decodePtr(params[0]),
decodePtr(params[1]),
params[2], false);
}
int32 Logic::fnRandom(int32 *params) {
// params: 0 min
// 1 max
writeVar(RESULT, _vm->_rnd.getRandomNumberRng(params[0], params[1]));
return IR_CONT;
}
int32 Logic::fnPreLoad(int32 *params) {
// Forces a resource into memory before it's "officially" opened for
// use. eg. if an anim needs to run on smoothly from another,
// "preloading" gets it into memory in advance to avoid the cacheing
// delay that normally occurs before the first frame.
// params: 0 resource to preload
_vm->_resman->openResource(params[0]);
_vm->_resman->closeResource(params[0]);
return IR_CONT;
}
int32 Logic::fnAddSubject(int32 *params) {
// params: 0 id
// 1 daves reference number
_vm->_mouse->addSubject(params[0], params[1]);
return IR_CONT;
}
int32 Logic::fnInteract(int32 *params) {
// Run targets action on a subroutine. Called by player on his base
// level 0 idle, for example.
// params: 0 id of target from which we derive action script
// reference
writeVar(PLAYER_ACTION, 0); // must clear this
logicUp((params[0] << 16) | 2); // 3rd script of clicked on id
// Out, up and around again - pc is saved for current level to be
// returned to.
return IR_GOSUB;
}
int32 Logic::fnChoose(int32 *params) {
// params: none
// This opcode is used to open the conversation menu. The human is
// switched off so there will be no normal mouse engine.
// The player's choice is piggy-backed on the standard opcode return
// values, to be used with the CP_JUMP_ON_RETURNED opcode. As far as I
// can tell, this is the only function that uses that feature.
uint32 response = _vm->_mouse->chooseMouse();
if (response == (uint32)-1)
return IR_REPEAT;
return IR_CONT | (response << 3);
}
/**
* Walk mega to (x,y,dir). Set RESULT to 0 if it succeeded. Otherwise, set
* RESULT to 1.
*/
int32 Logic::fnWalk(int32 *params) {
// params: 0 pointer to object's logic structure
// 1 pointer to object's graphic structure
// 2 pointer to object's mega structure
// 3 pointer to object's walkdata structure
// 4 target x-coord
// 5 target y-coord
// 6 target direction (8 means end walk on ANY direction)
return _router->doWalk(
decodePtr(params[0]),
decodePtr(params[1]),
decodePtr(params[2]),
decodePtr(params[3]),
params[4], params[5], params[6]);
}
/**
* Walk mega to start position of anim
*/
int32 Logic::fnWalkToAnim(int32 *params) {
// params: 0 pointer to object's logic structure
// 1 pointer to object's graphic structure
// 2 pointer to object's mega structure
// 3 pointer to object's walkdata structure
// 4 anim resource id
return _router->walkToAnim(
decodePtr(params[0]),
decodePtr(params[1]),
decodePtr(params[2]),
decodePtr(params[3]),
params[4]);
}
/**
* Turn mega to the specified direction.
*/
int32 Logic::fnTurn(int32 *params) {
// params: 0 pointer to object's logic structure
// 1 pointer to object's graphic structure
// 2 pointer to object's mega structure
// 3 pointer to object's walkdata structure
// 4 target direction
return _router->doFace(
decodePtr(params[0]),
decodePtr(params[1]),
decodePtr(params[2]),
decodePtr(params[3]),
params[4]);
}
/**
* Stand mega at (x,y,dir)
* Sets up the graphic object, but also needs to set the new 'current_dir' in
* the mega object, so the router knows in future
*/
int32 Logic::fnStandAt(int32 *params) {
// params: 0 pointer to object's graphic structure
// 1 pointer to object's mega structure
// 2 target x-coord
// 3 target y-coord
// 4 target direction
_router->standAt(
decodePtr(params[0]),
decodePtr(params[1]),
params[2], params[3], params[4]);
return IR_CONT;
}
/**
* Stand mega into the specified direction at current feet coords.
* Just needs to call standAt() with current feet coords.
*/
int32 Logic::fnStand(int32 *params) {
// params: 0 pointer to object's graphic structure
// 1 pointer to object's mega structure
// 2 target direction
byte *ob_mega = decodePtr(params[1]);
ObjectMega obMega(ob_mega);
_router->standAt(
decodePtr(params[0]),
ob_mega, obMega.getFeetX(), obMega.getFeetY(), params[2]);
return IR_CONT;
}
/**
* stand mega at end position of anim
*/
int32 Logic::fnStandAfterAnim(int32 *params) {
// params: 0 pointer to object's graphic structure
// 1 pointer to object's mega structure
// 2 anim resource id
_router->standAfterAnim(
decodePtr(params[0]),
decodePtr(params[1]),
params[2]);
return IR_CONT;
}
int32 Logic::fnPause(int32 *params) {
// params: 0 pointer to object's logic structure
// 1 number of game-cycles to pause
// NB. Pause-value of 0 causes script to continue, 1 causes a 1-cycle
// quit, 2 gives 2 cycles, etc.
ObjectLogic obLogic(decodePtr(params[0]));
if (obLogic.getLooping() == 0) {
obLogic.setLooping(1);
obLogic.setPause(params[1]);
}
if (obLogic.getPause()) {
obLogic.setPause(obLogic.getPause() - 1);
return IR_REPEAT;
}
obLogic.setLooping(0);
return IR_CONT;
}
int32 Logic::fnMegaTableAnim(int32 *params) {
// params: 0 pointer to object's logic structure
// 1 pointer to object's graphic structure
// 2 pointer to object's mega structure
// 3 pointer to animation table
// Normal forward anim
return _router->megaTableAnimate(
decodePtr(params[0]),
decodePtr(params[1]),
decodePtr(params[2]),
decodePtr(params[3]),
false);
}
int32 Logic::fnAddMenuObject(int32 *params) {
// params: 0 pointer to a MenuObject structure to copy down
_vm->_mouse->addMenuObject(decodePtr(params[0]));
return IR_CONT;
}
/**
* Start a conversation.
*
* Note that fnStartConversation() might accidentally be called every time the
* script loops back for another chooser, but we only want to reset the chooser
* count flag the first time this function is called, i.e. when the talk flag
* is zero.
*/
int32 Logic::fnStartConversation(int32 *params) {
// params: none
_vm->_mouse->startConversation();
return IR_CONT;
}
/**
* End a conversation.
*/
int32 Logic::fnEndConversation(int32 *params) {
// params: none
_vm->_mouse->endConversation();
return IR_CONT;
}
int32 Logic::fnSetFrame(int32 *params) {
// params: 0 pointer to object's graphic structure
// 1 resource id of animation file
// 2 frame flag (0=first 1=last)
int32 res = params[1];
assert(res);
// open the resource (& check it's valid)
byte *anim_file = _vm->_resman->openResource(res);
assert(_vm->_resman->fetchType(res) == ANIMATION_FILE);
// set up pointer to the animation header
AnimHeader anim_head;
anim_head.read(_vm->fetchAnimHeader(anim_file));
// set up anim resource in graphic object
ObjectGraphic obGraph(decodePtr(params[0]));
obGraph.setAnimResource(res);
obGraph.setAnimPc(params[2] ? anim_head.noAnimFrames - 1 : 0);
// Close the anim file and drop out of script
_vm->_resman->closeResource(obGraph.getAnimResource());
return IR_CONT;
}
int32 Logic::fnRandomPause(int32 *params) {
// params: 0 pointer to object's logic structure
// 1 minimum number of game-cycles to pause
// 2 maximum number of game-cycles to pause
ObjectLogic obLogic(decodePtr(params[0]));
int32 pars[2];
if (obLogic.getLooping() == 0) {
pars[0] = params[1];
pars[1] = params[2];
fnRandom(pars);
pars[1] = readVar(RESULT);
}
pars[0] = params[0];
return fnPause(pars);
}
int32 Logic::fnRegisterFrame(int32 *params) {
// this call would be made from an objects service script 0
// params: 0 pointer to mouse structure or NULL for no write to
// mouse list (non-zero means write sprite-shape to
// mouse list)
// 1 pointer to graphic structure
// 2 pointer to mega structure or NULL if not a mega
_vm->_screen->registerFrame(
decodePtr(params[0]),
decodePtr(params[1]),
decodePtr(params[2]));
return IR_CONT;
}
int32 Logic::fnNoSprite(int32 *params) {
// params: 0 pointer to object's graphic structure
_router->setSpriteStatus(decodePtr(params[0]), NO_SPRITE);
return IR_CONT;
}
int32 Logic::fnSendSync(int32 *params) {
// params: 0 sync's recipient
// 1 sync value
sendSync(params[0], params[1]);
return IR_CONT;
}
int32 Logic::fnUpdatePlayerStats(int32 *params) {
// engine needs to know certain info about the player
// params: 0 pointer to mega structure
ObjectMega obMega(decodePtr(params[0]));
ScreenInfo *screenInfo = _vm->_screen->getScreenInfo();
screenInfo->player_feet_x = obMega.getFeetX();
screenInfo->player_feet_y = obMega.getFeetY();
// for the script
writeVar(PLAYER_FEET_X, obMega.getFeetX());
writeVar(PLAYER_FEET_Y, obMega.getFeetY());
writeVar(PLAYER_CUR_DIR, obMega.getCurDir());
writeVar(SCROLL_OFFSET_X, screenInfo->scroll_offset_x);
debug(5, "fnUpdatePlayerStats: %d %d",
obMega.getFeetX(), obMega.getFeetY());
return IR_CONT;
}
int32 Logic::fnPassGraph(int32 *params) {
// makes an engine local copy of passed ObjectGraphic - run script 4
// of an object to request this used by fnTurnTo(id) etc
//
// remember, we cannot simply read a compact any longer but instead
// must request it from the object itself
// params: 0 pointer to an ObjectGraphic structure
warning("fnPassGraph() is a no-op now");
return IR_CONT;
}
int32 Logic::fnInitFloorMouse(int32 *params) {
// params: 0 pointer to object's mouse structure
byte *ob_mouse = decodePtr(params[0]);
ScreenInfo *screenInfo = _vm->_screen->getScreenInfo();
// floor is always lowest priority
ObjectMouse mouse;
mouse.x1 = 0;
mouse.y1 = 0;
mouse.x2 = screenInfo->screen_wide - 1;
mouse.y2 = screenInfo->screen_deep - 1;
mouse.priority = 9;
mouse.pointer = NORMAL_MOUSE_ID;
mouse.write(ob_mouse);
return IR_CONT;
}
int32 Logic::fnPassMega(int32 *params) {
// makes an engine local copy of passed mega_structure - run script 4
// of an object to request this used by fnTurnTo(id) etc
//
// remember, we cannot simply read a compact any longer but instead
// must request it from the object itself
// params: 0 pointer to a mega structure
memcpy(_engineMega, decodePtr(params[0]), ObjectMega::size());
return IR_CONT;
}
/**
* Turn mega to face point (x,y) on the floor
*/
int32 Logic::fnFaceXY(int32 *params) {
// params: 0 pointer to object's logic structure
// 1 pointer to object's graphic structure
// 2 pointer to object's mega structure
// 3 pointer to object's walkdata structure
// 4 target x-coord
// 5 target y-coord
return _router->faceXY(
decodePtr(params[0]),
decodePtr(params[1]),
decodePtr(params[2]),
decodePtr(params[3]),
params[4], params[5]);
}
/**
* Causes no more objects in this logic loop to be processed. The logic engine
* will restart at the beginning of the new list. The current screen will not
* be drawn!
*/
int32 Logic::fnEndSession(int32 *params) {
// params: 0 id of new run-list
// terminate current and change to next run-list
expressChangeSession(params[0]);
// stop the script - logic engine will now go around and the new
// screen will begin
return IR_STOP;
}
int32 Logic::fnNoHuman(int32 *params) {
// params: none
_vm->_mouse->noHuman();
return IR_CONT;
}
int32 Logic::fnAddHuman(int32 *params) {
// params: none
_vm->_mouse->addHuman();
return IR_CONT;
}
/**
* Wait for a target to become waiting, i.e. not busy.
*/
int32 Logic::fnWeWait(int32 *params) {
// params: 0 target
assert(_vm->_resman->fetchType(params[0]) == GAME_OBJECT);
// Run the target's get-speech-state script
runResScript(params[0], 5);
if (readVar(RESULT) == 0) {
// The target is busy. Try again.
_vm->_debugger->_speechScriptWaiting = params[0];
return IR_REPEAT;
}
// The target is waiting, i.e. not busy.
_vm->_debugger->_speechScriptWaiting = 0;
return IR_CONT;
}
/**
* Wait for a target to become waiting, i.e. not busy, send a command to it,
* then wait for it to finish.
*/
int32 Logic::fnTheyDoWeWait(int32 *params) {
// params: 0 pointer to ob_logic
// 1 target
// 2 command
// 3 ins1
// 4 ins2
// 5 ins3
// 6 ins4
// 7 ins5
assert(_vm->_resman->fetchType(params[1]) == GAME_OBJECT);
// Run the target's get-speech-state script
runResScript(params[1], 5);
ObjectLogic obLogic(decodePtr(params[0]));
if (readVar(RESULT) == 1 && readVar(INS_COMMAND) == 0 && obLogic.getLooping() == 0) {
// The target is waiting, i.e. not busy, and there is no other
// command queued. We haven't sent the command yet, so do it.
debug(5, "fnTheyDoWeWait: sending command to %d", params[1]);
_vm->_debugger->_speechScriptWaiting = params[1];
obLogic.setLooping(1);
writeVar(SPEECH_ID, params[1]);
writeVar(INS_COMMAND, params[2]);
writeVar(INS1, params[3]);
writeVar(INS2, params[4]);
writeVar(INS3, params[5]);
writeVar(INS4, params[6]);
writeVar(INS5, params[7]);
return IR_REPEAT;
}
if (obLogic.getLooping() == 0) {
// The command has not been sent yet. Keep waiting.
_vm->_debugger->_speechScriptWaiting = params[1];
return IR_REPEAT;
}
if (readVar(RESULT) == 0) {
// The command has been sent, and the target is busy doing it.
// Wait for it to finish.
debug(5, "fnTheyDoWeWait: Waiting for %d to finish", params[1]);
_vm->_debugger->_speechScriptWaiting = params[1];
return IR_REPEAT;
}
debug(5, "fnTheyDoWeWait: %d finished", params[1]);
obLogic.setLooping(0);
_vm->_debugger->_speechScriptWaiting = 0;
return IR_CONT;
}
/**
* Wait for a target to become waiting, i.e. not busy, then send a command to
* it.
*/
int32 Logic::fnTheyDo(int32 *params) {
// params: 0 target
// 1 command
// 2 ins1
// 3 ins2
// 4 ins3
// 5 ins4
// 6 ins5
assert(_vm->_resman->fetchType(params[0]) == GAME_OBJECT);
// Run the target's get-speech-state script
runResScript(params[0], 5);
if (readVar(RESULT) == 1 && !readVar(INS_COMMAND)) {
// The target is waiting, i.e. not busy, and there is no other
// command queued. Send the command.
debug(5, "fnTheyDo: sending command to %d", params[0]);
_vm->_debugger->_speechScriptWaiting = 0;
writeVar(SPEECH_ID, params[0]);
writeVar(INS_COMMAND, params[1]);
writeVar(INS1, params[2]);
writeVar(INS2, params[3]);
writeVar(INS3, params[4]);
writeVar(INS4, params[5]);
writeVar(INS5, params[6]);
return IR_CONT;
}
// The target is busy. Come back again next cycle.
_vm->_debugger->_speechScriptWaiting = params[0];
return IR_REPEAT;
}
/**
* Route to the left or right hand side of target id, if possible.
*/
int32 Logic::fnWalkToTalkToMega(int32 *params) {
// params: 0 pointer to object's logic structure
// 1 pointer to object's graphic structure
// 2 pointer to object's mega structure
// 3 pointer to object's walkdata structure
// 4 id of target mega to face
// 5 separation
return _router->walkToTalkToMega(
decodePtr(params[0]),
decodePtr(params[1]),
decodePtr(params[2]),
decodePtr(params[3]),
params[4], params[5]);
}
int32 Logic::fnFadeDown(int32 *params) {
// NONE means up! can only be called when screen is fully faded up -
// multiple calls wont have strange effects
// params: none
if (_vm->_screen->getFadeStatus() == RDFADE_NONE)
_vm->_screen->fadeDown();
return IR_CONT;
}
enum {
S_OB_GRAPHIC = 0,
S_OB_SPEECH = 1,
S_OB_LOGIC = 2,
S_OB_MEGA = 3,
S_TEXT = 4,
S_WAV = 5,
S_ANIM = 6,
S_DIR_TABLE = 7,
S_ANIM_MODE = 8
};
/**
* It's the super versatile fnSpeak. Text and wavs can be selected in any
* combination.
*
* @note We can assume no human - there should be no human, at least!
*/
int32 Logic::fnISpeak(int32 *params) {
// params: 0 pointer to ob_graphic
// 1 pointer to ob_speech
// 2 pointer to ob_logic
// 3 pointer to ob_mega
// 4 encoded text number
// 5 wav res id
// 6 anim res id
// 7 anim table res id
// 8 animation mode 0 lip synced,
// 1 just straight animation
// Set up the pointers which we know we'll always need
ObjectLogic obLogic(decodePtr(params[S_OB_LOGIC]));
ObjectGraphic obGraph(decodePtr(params[S_OB_GRAPHIC]));
// FIRST TIME ONLY: create the text, load the wav, set up the anim,
// etc.
if (obLogic.getLooping() == 0) {
// New fudge to wait for smacker samples to finish
// since they can over-run into the game
if (_vm->_sound->getSpeechStatus() != RDSE_SAMPLEFINISHED)
return IR_REPEAT;
// New fudge for 'fx' subtitles: If subtitles switched off, and
// we don't want to use a wav for this line either, then just
// quit back to script right now!
if (!_vm->getSubtitles() && !wantSpeechForLine(params[S_WAV]))
return IR_CONT;
// Drop out for 1st cycle to allow walks/anims to end and
// display last frame before system locks while speech loaded
if (!_cycleSkip) {
_cycleSkip = true;
return IR_REPEAT;
}
_cycleSkip = false;
_vm->_debugger->_textNumber = params[S_TEXT];
// Pull out the text line to get the official text number
// (for wav id). Once the wav id's go into all script text
// commands, we'll only need this for debugging.
uint32 text_res = params[S_TEXT] / SIZE;
uint32 local_text = params[S_TEXT] & 0xffff;
// For testing all text & speech!
//
// A script loop can send any text number to fnISpeak and it
// will only run the valid ones or return with 'result' equal
// to '1' or '2' to mean 'invalid text resource' and 'text
// number out of range' respectively
//
// See 'testing_routines' object in George's Player Character
// section of linc
if (readVar(SYSTEM_TESTING_TEXT)) {
if (!_vm->_resman->checkValid(text_res)) {
// Not a valid resource number - invalid (null
// resource)
writeVar(RESULT, 1);
return IR_CONT;
}
if (_vm->_resman->fetchType(text_res) != TEXT_FILE) {
// Invalid - not a text resource
_vm->_resman->closeResource(text_res);
writeVar(RESULT, 1);
return IR_CONT;
}
if (!_vm->checkTextLine(_vm->_resman->openResource(text_res), local_text)) {
// Line number out of range
_vm->_resman->closeResource(text_res);
writeVar(RESULT, 2);
return IR_CONT;
}
_vm->_resman->closeResource(text_res);
writeVar(RESULT, 0);
}
byte *text = _vm->fetchTextLine(_vm->_resman->openResource(text_res), local_text);
_officialTextNumber = READ_LE_UINT16(text);
_vm->_resman->closeResource(text_res);
// Prevent dud lines from appearing while testing text & speech
// since these will not occur in the game anyway
if (readVar(SYSTEM_TESTING_TEXT)) {
// If actor number is 0 and text line is just a 'dash'
// character
if (_officialTextNumber == 0 && text[2] == '-' && text[3] == 0) {
writeVar(RESULT, 3);
return IR_CONT;
}
}
// Set the 'looping_flag' and the text-click-delays. We can
// left-click past the text after half a second, and
// right-click past it after a quarter of a second.
obLogic.setLooping(1);
_leftClickDelay = 6;
_rightClickDelay = 3;
if (readVar(PLAYER_ID) != CUR_PLAYER_ID)
debug(5, "(%d) Nico: %s", _officialTextNumber, text + 2);
else {
debug(5, "(%d) %s: %s", _officialTextNumber, _vm->_resman->fetchName(readVar(ID)), text + 2);
}
// Set up the speech animation
if (params[S_ANIM]) {
// Just a straight anim.
_animId = params[6];
} else if (params[S_DIR_TABLE]) {
// Use this direction table to derive the anim
// NB. ASSUMES WE HAVE A MEGA OBJECT!!
ObjectMega obMega(decodePtr(params[S_OB_MEGA]));
byte *anim_table = decodePtr(params[S_DIR_TABLE]);
_animId = READ_LE_UINT32(anim_table + 4 * obMega.getCurDir());
} else {
// No animation choosen
_animId = 0;
}
if (_animId) {
// Set the talker's graphic to the first frame of this
// speech anim for now.
_speechAnimType = readVar(SPEECHANIMFLAG);
obGraph.setAnimResource(_animId);
obGraph.setAnimPc(0);
}
// Default back to looped lip synced anims.
writeVar(SPEECHANIMFLAG, 0);
// Set up _textX and _textY for speech panning and/or text
// sprite position.
locateTalker(params);
// If the speech is associated with a specific animation, and
// not just a voice-over, set the focus area to the calculated
// position.
if (_animId) {
_vm->_system->setFocusRectangle(Common::Rect::center(_textX, _textY, 192, 128));
}
// Is it to be speech or subtitles or both?
// Assume not running until know otherwise
_speechRunning = false;
// New fudge for 'fx' subtitles: If speech is selected, and
// this line is allowed speech (not if it's an fx subtitle!)
if (!_vm->_sound->isSpeechMute() && wantSpeechForLine(_officialTextNumber)) {
// If the wavId parameter is zero because not yet
// compiled into speech command, we can still get it
// from the 1st 2 chars of the text line.
if (!params[S_WAV])
params[S_WAV] = (int32)_officialTextNumber;
// Panning goes from -16 (left) to 16 (right)
int8 speech_pan = ((_textX - 320) * 16) / 320;
if (speech_pan < -16)
speech_pan = -16;
else if (speech_pan > 16)
speech_pan = 16;
uint32 rv = _vm->_sound->playCompSpeech(params[S_WAV], 16, speech_pan);
if (rv == RD_OK) {
// Ok, we've got something to play. Set it
// playing now. (We might want to do this the
// next cycle, don't know yet.)
_speechRunning = true;
_vm->_sound->unpauseSpeech();
} else {
debug(5, "ERROR: PlayCompSpeech(wav=%d (res=%d pos=%d)) returned %.8x", params[S_WAV], text_res, local_text, rv);
}
}
if (_vm->getSubtitles() || !_speechRunning) {
// We want subtitles, or the speech failed to load.
// Either way, we're going to show the text so create
// the text sprite.
formText(params);
}
}
// EVERY TIME: run a cycle of animation, if there is one
if (_animId) {
// There is an animation - Increment the anim frame number.
obGraph.setAnimPc(obGraph.getAnimPc() + 1);
byte *anim_file = _vm->_resman->openResource(obGraph.getAnimResource());
AnimHeader anim_head;
anim_head.read(_vm->fetchAnimHeader(anim_file));
if (!_speechAnimType) {
// ANIM IS TO BE LIP-SYNC'ED & REPEATING
if (obGraph.getAnimPc() == (int32)anim_head.noAnimFrames) {
// End of animation - restart from frame 0
obGraph.setAnimPc(0);
} else if (_speechRunning && _vm->_sound->amISpeaking() == RDSE_QUIET) {
// The speech is running, but we're at a quiet
// bit. Restart from frame 0 (closed mouth).
obGraph.setAnimPc(0);
}
} else {
// ANIM IS TO PLAY ONCE ONLY
if (obGraph.getAnimPc() == (int32)anim_head.noAnimFrames - 1) {