/
logic.cpp
1812 lines (1596 loc) · 59.4 KB
/
logic.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.
*
* 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/endian.h"
#include "common/util.h"
#include "common/textconsole.h"
#include "common/translation.h"
#include "sword1/logic.h"
#include "sword1/text.h"
#include "sword1/sound.h"
#include "sword1/eventman.h"
#include "sword1/menu.h"
#include "sword1/router.h"
#include "sword1/screen.h"
#include "sword1/mouse.h"
#include "sword1/sword1.h"
#include "sword1/music.h"
#include "sword1/swordres.h"
#include "sword1/animation.h"
#include "sword1/debug.h"
#include "gui/message.h"
namespace Sword1 {
#define MAX_STACK_SIZE 10
#define SCRIPT_VERSION 13
#define LAST_FRAME 999
uint32 Logic::_scriptVars[NUM_SCRIPT_VARS];
Logic::Logic(SwordEngine *vm, ObjectMan *pObjMan, ResMan *resMan, Screen *pScreen, Mouse *pMouse, Sound *pSound, Music *pMusic, Menu *pMenu, OSystem *system, Audio::Mixer *mixer)
: _rnd("sword1") {
_vm = vm;
_objMan = pObjMan;
_resMan = resMan;
_screen = pScreen;
_mouse = pMouse;
_music = pMusic;
_sound = pSound;
_menu = pMenu;
_textMan = NULL;
_screen->useTextManager(_textMan);
_router = new Router(_objMan, _resMan);
_eventMan = NULL;
_system = system;
_mixer = mixer;
setupMcodeTable();
}
Logic::~Logic() {
delete _textMan;
delete _router;
delete _eventMan;
}
void Logic::initialize() {
memset(_scriptVars, 0, NUM_SCRIPT_VARS * sizeof(uint32));
for (uint8 cnt = 0; cnt < NON_ZERO_SCRIPT_VARS; cnt++)
_scriptVars[_scriptVarInit[cnt][0]] = _scriptVarInit[cnt][1];
if (SwordEngine::_systemVars.isDemo)
_scriptVars[PLAYINGDEMO] = 1;
delete _eventMan;
_eventMan = new EventManager();
delete _textMan;
_textMan = new Text(_objMan, _resMan,
(SwordEngine::_systemVars.language == BS1_CZECH) ? true : false);
_screen->useTextManager(_textMan);
_textRunning = _speechRunning = false;
_speechFinished = true;
}
void Logic::newScreen(uint32 screen) {
Object *compact = (Object *)_objMan->fetchObject(PLAYER);
// work around script bug #911508
if (((screen == 25) || (_scriptVars[SCREEN] == 25)) && (_scriptVars[SAND_FLAG] == 4)) {
Object *cpt = _objMan->fetchObject(SAND_25);
Object *george = _objMan->fetchObject(PLAYER);
if (george->o_place == HOLDING_REPLICA_25) // is george holding the replica in his hands?
fnFullSetFrame(cpt, SAND_25, IMPFLRCDT, IMPFLR, 0, 0, 0, 0); // empty impression in floor
else
fnFullSetFrame(cpt, SAND_25, IMPPLSCDT, IMPPLS, 0, 0, 0, 0); // impression filled with plaster
}
// work around, at screen 69 in psx version TOP menu gets stuck at disabled, fix it at next screen (71)
if ((screen == 71) && (SwordEngine::isPsx()))
_scriptVars[TOP_MENU_DISABLED] = 0;
if (SwordEngine::_systemVars.justRestoredGame) { // if we've just restored a game - we want George to be exactly as saved
fnAddHuman(NULL, 0, 0, 0, 0, 0, 0, 0);
if (_scriptVars[GEORGE_WALKING]) { // except that if George was walking when we saveed the game
fnStandAt(compact, PLAYER, _scriptVars[CHANGE_X], _scriptVars[CHANGE_Y], _scriptVars[CHANGE_DIR], _scriptVars[CHANGE_STANCE], 0, 0);
fnIdle(compact, PLAYER, 0, 0, 0, 0, 0, 0);
_scriptVars[GEORGE_WALKING] = 0;
}
SwordEngine::_systemVars.justRestoredGame = 0;
_music->startMusic(_scriptVars[CURRENT_MUSIC], 1);
} else { // if we haven't just restored a game, set George to stand, etc
compact->o_screen = _scriptVars[NEW_SCREEN]; //move the mega/player at this point between screens
fnStandAt(compact, PLAYER, _scriptVars[CHANGE_X], _scriptVars[CHANGE_Y], _scriptVars[CHANGE_DIR], _scriptVars[CHANGE_STANCE], 0, 0);
fnChangeFloor(compact, PLAYER, _scriptVars[CHANGE_PLACE], 0, 0, 0, 0, 0);
}
}
void Logic::engine() {
debug(8, "\n\nNext logic cycle");
_eventMan->serviceGlobalEventList();
for (uint16 sectCnt = 0; sectCnt < TOTAL_SECTIONS; sectCnt++) {
if (_objMan->sectionAlive(sectCnt)) {
uint32 numCpts = _objMan->fetchNoObjects(sectCnt);
for (uint32 cptCnt = 0; cptCnt < numCpts; cptCnt++) {
uint32 currentId = sectCnt * ITM_PER_SEC + cptCnt;
Object *compact = _objMan->fetchObject(currentId);
if (compact->o_status & STAT_LOGIC) { // does the object want to be processed?
if (compact->o_status & STAT_EVENTS) {
//subscribed to the global-event-switcher? and in logic mode
switch (compact->o_logic) {
case LOGIC_pause_for_event:
case LOGIC_idle:
case LOGIC_AR_animate:
_eventMan->checkForEvent(compact);
break;
}
}
debug(7, "Logic::engine: handling compact %d (%X)", currentId, currentId);
processLogic(compact, currentId);
compact->o_sync = 0; // syncs are only available for 1 cycle.
}
if ((uint32)compact->o_screen == _scriptVars[SCREEN]) {
if (compact->o_status & STAT_FORE)
_screen->addToGraphicList(0, currentId);
if (compact->o_status & STAT_SORT)
_screen->addToGraphicList(1, currentId);
if (compact->o_status & STAT_BACK)
_screen->addToGraphicList(2, currentId);
if (compact->o_status & STAT_MOUSE)
_mouse->addToList(currentId, compact);
}
}
}
}
//_collision->checkCollisions();
}
void Logic::processLogic(Object *compact, uint32 id) {
int logicRet;
do {
switch (compact->o_logic) {
case LOGIC_idle:
logicRet = 0;
break;
case LOGIC_pause:
case LOGIC_pause_for_event:
if (compact->o_pause) {
compact->o_pause--;
logicRet = 0;
} else {
compact->o_logic = LOGIC_script;
logicRet = 1;
}
break;
case LOGIC_quit:
compact->o_logic = LOGIC_script;
logicRet = 0;
break;
case LOGIC_wait_for_sync:
if (compact->o_sync) {
logicRet = 1;
compact->o_logic = LOGIC_script;
} else
logicRet = 0;
break;
case LOGIC_choose:
_scriptVars[CUR_ID] = id;
logicRet = _menu->logicChooser(compact);
break;
case LOGIC_wait_for_talk:
logicRet = logicWaitTalk(compact);
break;
case LOGIC_start_talk:
logicRet = logicStartTalk(compact);
break;
case LOGIC_script:
_scriptVars[CUR_ID] = id;
logicRet = scriptManager(compact, id);
break;
case LOGIC_new_script:
compact->o_tree.o_script_pc[compact->o_tree.o_script_level] = _newScript;
compact->o_tree.o_script_id[compact->o_tree.o_script_level] = _newScript;
compact->o_logic = LOGIC_script;
logicRet = 1;
break;
case LOGIC_AR_animate:
logicRet = logicArAnimate(compact, id);
break;
case LOGIC_restart:
compact->o_tree.o_script_pc[compact->o_tree.o_script_level] = compact->o_tree.o_script_id[compact->o_tree.o_script_level];
compact->o_logic = LOGIC_script;
logicRet = 1;
break;
case LOGIC_bookmark:
memcpy(&(compact->o_tree.o_script_level), &(compact->o_bookmark.o_script_level), sizeof(ScriptTree));
if (id == GMASTER_79) {
// workaround for ending script.
// GMASTER_79 is not prepared for mega_interact receiving INS_quit
fnSuicide(compact, id, 0, 0, 0, 0, 0, 0);
logicRet = 0;
} else {
compact->o_logic = LOGIC_script;
logicRet = 1;
}
break;
case LOGIC_speech:
logicRet = speechDriver(compact);
break;
case LOGIC_full_anim:
logicRet = fullAnimDriver(compact);
break;
case LOGIC_anim:
logicRet = animDriver(compact);
break;
default:
error("Fatal error: compact %d's logic == %X", id, compact->o_logic);
break;
}
} while (logicRet);
}
int Logic::logicWaitTalk(Object *compact) {
Object *target = _objMan->fetchObject(compact->o_down_flag);
if (target->o_status & STAT_TALK_WAIT) {
compact->o_logic = LOGIC_script;
return 1;
} else {
return 0;
}
}
int Logic::logicStartTalk(Object *compact) {
Object *target = _objMan->fetchObject(compact->o_down_flag); //holds id of person we're waiting for
if (target->o_status & STAT_TALK_WAIT) { //response?
compact->o_logic = LOGIC_script; //back to script again
return SCRIPT_CONT;
}
if (_eventMan->eventValid(compact->o_down_flag))
return SCRIPT_STOP; //event still valid - keep waiting
//the event has gone - so back to script with error code
compact->o_down_flag = 0;
compact->o_logic = LOGIC_script;
return SCRIPT_CONT;
}
int Logic::logicArAnimate(Object *compact, uint32 id) {
WalkData *route;
int32 walkPc;
if ((_scriptVars[GEORGE_WALKING] == 0) && (id == PLAYER))
_scriptVars[GEORGE_WALKING] = 1;
compact->o_resource = compact->o_walk_resource;
compact->o_status |= STAT_SHRINK;
route = compact->o_route;
walkPc = compact->o_walk_pc;
compact->o_frame = route[walkPc].frame;
compact->o_dir = route[walkPc].dir;
compact->o_xcoord = route[walkPc].x;
compact->o_ycoord = route[walkPc].y;
compact->o_anim_x = compact->o_xcoord;
compact->o_anim_y = compact->o_ycoord;
if (((_scriptVars[GEORGE_WALKING] == 2) && (walkPc > 5) && (id == PLAYER) &&
(route[walkPc - 1].step == 5) && (route[walkPc].step == 0)) ||
((_scriptVars[GEORGE_WALKING] == 3) && (id == PLAYER))) {
compact->o_frame = 96 + compact->o_dir; //reset
if ((compact->o_dir != 2) && (compact->o_dir != 6)) { // on verticals and diagonals stand where george is
compact->o_xcoord = route[walkPc - 1].x;
compact->o_ycoord = route[walkPc - 1].y;
compact->o_anim_x = compact->o_xcoord;
compact->o_anim_y = compact->o_ycoord;
}
compact->o_logic = LOGIC_script;
compact->o_down_flag = 0; //0 means error
_scriptVars[GEORGE_WALKING] = 0;
route[compact->o_walk_pc + 1].frame = 512; //end of sequence
if (_scriptVars[MEGA_ON_GRID] == 2)
_scriptVars[MEGA_ON_GRID] = 0;
}
compact->o_walk_pc++;
if (route[compact->o_walk_pc].frame == 512) { //end of sequence
compact->o_logic = LOGIC_script;
if (((_scriptVars[GEORGE_WALKING] == 2) || (_scriptVars[GEORGE_WALKING] == 1)) &&
(id == PLAYER)) {
_scriptVars[GEORGE_WALKING] = 0;
if (_scriptVars[MEGA_ON_GRID] == 2)
_scriptVars[MEGA_ON_GRID] = 0;
}
}
return 0;
}
int Logic::speechDriver(Object *compact) {
if ((!_speechClickDelay) && (_mouse->testEvent() & BS1L_BUTTON_DOWN))
_speechFinished = true;
if (_speechClickDelay)
_speechClickDelay--;
if (_speechRunning) {
if (_sound->speechFinished())
_speechFinished = true;
} else {
if (!compact->o_speech_time)
_speechFinished = true;
else
compact->o_speech_time--;
}
if (_speechFinished) {
if (_speechRunning)
_sound->stopSpeech();
compact->o_logic = LOGIC_script;
if (_textRunning) {
_textMan->releaseText(compact->o_text_id);
_objMan->fetchObject(compact->o_text_id)->o_status = 0; // kill compact linking text sprite
}
_speechRunning = _textRunning = false;
_speechFinished = true;
}
if (compact->o_anim_resource) {
uint8 *animData = ((uint8 *)_resMan->openFetchRes(compact->o_anim_resource)) + sizeof(Header);
int32 numFrames = _resMan->readUint32(animData);
animData += 4;
compact->o_anim_pc++; // go to next frame of anim
if (_speechFinished || (compact->o_anim_pc >= numFrames) ||
(_speechRunning && (_sound->amISpeaking() == 0)))
compact->o_anim_pc = 0; //set to frame 0, closed mouth
AnimUnit *animPtr = (AnimUnit *)(animData + sizeof(AnimUnit) * compact->o_anim_pc);
if (!(compact->o_status & STAT_SHRINK)) {
compact->o_anim_x = _resMan->getUint32(animPtr->animX);
compact->o_anim_y = _resMan->getUint32(animPtr->animY);
}
compact->o_frame = _resMan->getUint32(animPtr->animFrame);
_resMan->resClose(compact->o_anim_resource);
}
return 0;
}
int Logic::fullAnimDriver(Object *compact) {
if (compact->o_sync) { // return to script immediately if we've received a sync
compact->o_logic = LOGIC_script;
return 1;
}
uint8 *data = ((uint8 *)_resMan->openFetchRes(compact->o_anim_resource)) + sizeof(Header);
uint32 numFrames = _resMan->readUint32(data);
data += 4;
AnimUnit *animPtr = (AnimUnit *)(data + compact->o_anim_pc * sizeof(AnimUnit));
compact->o_anim_x = compact->o_xcoord = _resMan->getUint32(animPtr->animX);
compact->o_anim_y = compact->o_ycoord = _resMan->getUint32(animPtr->animY);
compact->o_frame = _resMan->getUint32(animPtr->animFrame);
compact->o_anim_pc++;
if (compact->o_anim_pc == (int)numFrames)
compact->o_logic = LOGIC_script;
_resMan->resClose(compact->o_anim_resource);
return 0;
}
int Logic::animDriver(Object *compact) {
if (compact->o_sync) {
compact->o_logic = LOGIC_script;
return 1;
}
uint8 *data = ((uint8 *)_resMan->openFetchRes(compact->o_anim_resource)) + sizeof(Header);
uint32 numFrames = _resMan->readUint32(data);
AnimUnit *animPtr = (AnimUnit *)(data + 4 + compact->o_anim_pc * sizeof(AnimUnit));
if (!(compact->o_status & STAT_SHRINK)) {
compact->o_anim_x = _resMan->getUint32(animPtr->animX);
compact->o_anim_y = _resMan->getUint32(animPtr->animY);
}
compact->o_frame = _resMan->getUint32(animPtr->animFrame);
compact->o_anim_pc++;
if (compact->o_anim_pc == (int)numFrames)
compact->o_logic = LOGIC_script;
_resMan->resClose(compact->o_anim_resource);
return 0;
}
void Logic::updateScreenParams() {
Object *compact = (Object *)_objMan->fetchObject(PLAYER);
_screen->setScrolling((int16)(compact->o_xcoord - _scriptVars[FEET_X]),
(int16)(compact->o_ycoord - _scriptVars[FEET_Y]));
}
int Logic::scriptManager(Object *compact, uint32 id) {
int ret;
do {
uint32 level = compact->o_tree.o_script_level;
uint32 script = compact->o_tree.o_script_id[level];
Debug::interpretScript(id, level, script, compact->o_tree.o_script_pc[level] & ITM_ID);
ret = interpretScript(compact, id, _resMan->lockScript(script), script, compact->o_tree.o_script_pc[level] & ITM_ID);
_resMan->unlockScript(script);
if (!ret) {
if (compact->o_tree.o_script_level)
compact->o_tree.o_script_level--;
else
error("ScriptManager: basescript %d for cpt %d ended", script, id);
} else
compact->o_tree.o_script_pc[level] = ret;
} while (!ret);
return 1;
//Logic continues - but the script must have changed logic mode
//this is a radical change from S2.0 where once a script finished there
//was no more processing for that object on that cycle - the Logic_engine terminated.
//This meant that new logics that needed immediate action got a first call from the
//setup function. This was a bit tweeky. This technique ensures that the script is a
//totally seamless concept that takes up zero cycle time. The only downside is that
//an FN_quit becomes slightly more convoluted, but so what you might ask.
}
void Logic::runMouseScript(Object *cpt, int32 scriptId) {
Header *script = _resMan->lockScript(scriptId);
debug(9, "running mouse script %d", scriptId);
interpretScript(cpt, _scriptVars[SPECIAL_ITEM], script, scriptId, scriptId);
_resMan->unlockScript(scriptId);
}
int Logic::interpretScript(Object *compact, int id, Header *scriptModule, int scriptBase, int scriptNum) {
int32 *scriptCode = (int32 *)(((uint8 *)scriptModule) + sizeof(Header));
int32 stack[MAX_STACK_SIZE];
int32 stackIdx = 0;
int32 offset;
int32 pc;
if (memcmp(scriptModule->type, "Script", 6))
error("Invalid script module");
if (scriptModule->version != SCRIPT_VERSION)
error("Illegal script version");
if (scriptNum < 0)
error("negative script number");
if ((uint32)scriptNum >= scriptModule->decomp_length)
error("Script number out of bounds");
if (scriptNum < scriptCode[0])
pc = scriptCode[scriptNum + 1];
else
pc = scriptNum;
int32 startOfScript = scriptCode[(scriptBase & ITM_ID) + 1];
int32 a, b, c, d, e, f;
int mCodeReturn = 0;
int32 mCodeNumber = 0, mCodeArguments = 0;
uint32 varNum = 0;
while (1) {
assert((stackIdx >= 0) && (stackIdx <= MAX_STACK_SIZE));
switch (scriptCode[pc++]) {
case IT_MCODE:
a = b = c = d = e = f = 0;
mCodeNumber = scriptCode[pc++];
mCodeArguments = scriptCode[pc++];
switch (mCodeArguments) {
case 6: f = stack[--stackIdx];
case 5: e = stack[--stackIdx];
case 4: d = stack[--stackIdx];
case 3: c = stack[--stackIdx];
case 2: b = stack[--stackIdx];
case 1: a = stack[--stackIdx];
case 0:
Debug::callMCode(mCodeNumber, mCodeArguments, a, b, c, d, e, f);
mCodeReturn = (this->*_mcodeTable[mCodeNumber])(compact, id, a, b, c, d, e, f);
break;
default:
warning("mcode[%d]: too many arguments(%d)", mCodeNumber, mCodeArguments);
}
if (mCodeReturn == 0)
return pc;
break;
case IT_PUSHNUMBER:
debug(9, "IT_PUSH: %d", scriptCode[pc]);
stack[stackIdx++] = scriptCode[pc++];
break;
case IT_PUSHVARIABLE:
debug(9, "IT_PUSHVARIABLE: ScriptVar[%d] => %d", scriptCode[pc], _scriptVars[scriptCode[pc]]);
varNum = scriptCode[pc++];
if (SwordEngine::_systemVars.isDemo && SwordEngine::isWindows()) {
if (varNum >= 397) // BS1 Demo has different number of script variables
varNum++;
if (varNum >= 699)
varNum++;
}
stack[stackIdx++] = _scriptVars[varNum];
break;
case IT_NOTEQUAL:
stackIdx--;
debug(9, "IT_NOTEQUAL: RESULT = %d", stack[stackIdx - 1] != stack[stackIdx]);
stack[stackIdx - 1] = (stack[stackIdx - 1] != stack[stackIdx]);
break;
case IT_ISEQUAL:
stackIdx--;
debug(9, "IT_ISEQUAL: RESULT = %d", stack[stackIdx - 1] == stack[stackIdx]);
stack[stackIdx - 1] = (stack[stackIdx - 1] == stack[stackIdx]);
break;
case IT_PLUS:
stackIdx--;
debug(9, "IT_PLUS: RESULT = %d", stack[stackIdx - 1] + stack[stackIdx]);
stack[stackIdx - 1] = (stack[stackIdx - 1] + stack[stackIdx]);
break;
case IT_TIMES:
stackIdx--;
debug(9, "IT_TIMES: RESULT = %d", stack[stackIdx - 1] * stack[stackIdx]);
stack[stackIdx - 1] = (stack[stackIdx - 1] * stack[stackIdx]);
break;
case IT_ANDAND:
stackIdx--;
debug(9, "IT_ANDAND: RESULT = %d", stack[stackIdx - 1] && stack[stackIdx]);
stack[stackIdx - 1] = (stack[stackIdx - 1] && stack[stackIdx]);
break;
case IT_OROR: // ||
stackIdx--;
debug(9, "IT_OROR: RESULT = %d", stack[stackIdx - 1] || stack[stackIdx]);
stack[stackIdx - 1] = (stack[stackIdx - 1] || stack[stackIdx]);
break;
case IT_LESSTHAN:
stackIdx--;
debug(9, "IT_LESSTHAN: RESULT = %d", stack[stackIdx - 1] < stack[stackIdx]);
stack[stackIdx - 1] = (stack[stackIdx - 1] < stack[stackIdx]);
break;
case IT_NOT:
debug(9, "IT_NOT: RESULT = %d", stack[stackIdx - 1] ? 0 : 1);
if (stack[stackIdx - 1])
stack[stackIdx - 1] = 0;
else
stack[stackIdx - 1] = 1;
break;
case IT_MINUS:
stackIdx--;
debug(9, "IT_MINUS: RESULT = %d", stack[stackIdx - 1] - stack[stackIdx]);
stack[stackIdx - 1] = (stack[stackIdx - 1] - stack[stackIdx]);
break;
case IT_AND:
stackIdx--;
debug(9, "IT_AND: RESULT = %d", stack[stackIdx - 1] & stack[stackIdx]);
stack[stackIdx - 1] = (stack[stackIdx - 1] & stack[stackIdx]);
break;
case IT_OR:
stackIdx--;
debug(9, "IT_OR: RESULT = %d", stack[stackIdx - 1] | stack[stackIdx]);
stack[stackIdx - 1] = (stack[stackIdx - 1] | stack[stackIdx]);
break;
case IT_GTE:
stackIdx--;
debug(9, "IT_GTE: RESULT = %d", stack[stackIdx - 1] >= stack[stackIdx]);
stack[stackIdx - 1] = (stack[stackIdx - 1] >= stack[stackIdx]);
break;
case IT_LTE:
stackIdx--;
debug(9, "IT_LTE: RESULT = %d", stack[stackIdx - 1] <= stack[stackIdx]);
stack[stackIdx - 1] = (stack[stackIdx - 1] <= stack[stackIdx]);
break;
case IT_DEVIDE:
stackIdx--;
debug(9, "IT_DEVIDE: RESULT = %d", stack[stackIdx - 1] / stack[stackIdx]);
stack[stackIdx - 1] = (stack[stackIdx - 1] / stack[stackIdx]);
break;
case IT_GT:
stackIdx--;
debug(9, "IT_GT: RESULT = %d", stack[stackIdx - 1] > stack[stackIdx]);
stack[stackIdx - 1] = (stack[stackIdx - 1] > stack[stackIdx]);
break;
case IT_SCRIPTEND:
debug(9, "IT_SCRIPTEND");
return 0;
case IT_POPVAR: // pop a variable
debug(9, "IT_POPVAR: ScriptVars[%d] = %d", scriptCode[pc], stack[stackIdx - 1]);
varNum = scriptCode[pc++];
if (SwordEngine::_systemVars.isDemo && SwordEngine::isWindows()) {
if (varNum >= 397) // BS1 Demo has different number of script variables
varNum++;
if (varNum >= 699)
varNum++;
}
_scriptVars[varNum] = stack[--stackIdx];
break;
case IT_POPLONGOFFSET:
offset = scriptCode[pc++];
debug(9, "IT_POPLONGOFFSET: Cpt[%d] = %d", offset, stack[stackIdx - 1]);
*((int32 *)((uint8 *)compact + offset)) = stack[--stackIdx];
break;
case IT_PUSHLONGOFFSET:
offset = scriptCode[pc++];
debug(9, "IT_PUSHLONGOFFSET: PUSH Cpt[%d] (==%d)", offset, *((int32 *)((uint8 *)compact + offset)));
stack[stackIdx++] = *((int32 *)((uint8 *)compact + offset));
break;
case IT_SKIPONFALSE:
debug(9, "IT_SKIPONFALSE: %d (%s)", scriptCode[pc], (stack[stackIdx - 1] ? "IS TRUE (NOT SKIPPED)" : "IS FALSE (SKIPPED)"));
if (stack[--stackIdx])
pc++;
else
pc += scriptCode[pc];
break;
case IT_SKIP:
debug(9, "IT_SKIP: %d", scriptCode[pc]);
pc += scriptCode[pc];
break;
case IT_SWITCH: // The mega switch statement
debug(9, "IT_SWITCH: [SORRY, NO DEBUG INFO]");
{
int switchValue = stack[--stackIdx];
int switchCount = scriptCode[pc++];
int doneSwitch = 0;
for (int cnt = 0; (cnt < switchCount) && (doneSwitch == 0); cnt++) {
if (switchValue == scriptCode[pc]) {
pc += scriptCode[pc + 1];
doneSwitch = 1;
} else
pc += 2;
}
if (doneSwitch == 0)
pc += scriptCode[pc];
}
break;
case IT_SKIPONTRUE: // skip if expression true
debug(9, "IT_SKIPONTRUE: %d (%s)", scriptCode[pc], (stack[stackIdx - 1] ? "IS TRUE (SKIPPED)" : "IS FALSE (NOT SKIPPED)"));
stackIdx--;
if (stack[stackIdx])
pc += scriptCode[pc];
else
pc++;
break;
case IT_PRINTF:
debug(0, "IT_PRINTF(%d)", stack[stackIdx]);
break;
case IT_RESTARTSCRIPT:
debug(9, "IT_RESTARTSCRIPT");
pc = startOfScript;
break;
case IT_POPWORDOFFSET:
offset = scriptCode[pc++];
debug(9, "IT_POPWORDOFFSET: Cpt[%d] = %d", offset, stack[stackIdx - 1] & 0xFFFF);
*((int32 *)((uint8 *)compact + offset)) = stack[--stackIdx] & 0xffff;
break;
case IT_PUSHWORDOFFSET:
offset = scriptCode[pc++];
debug(9, "IT_PUSHWORDOFFSET: PUSH Cpt[%d] == %d", offset, (*((int32 *)((uint8 *)compact + offset))) & 0xffff);
stack[stackIdx++] = (*((int32 *)((uint8 *)compact + offset))) & 0xffff;
break;
default:
error("Invalid operator %d", scriptCode[pc - 1]);
return 0; // for compilers that don't support NORETURN
}
}
}
void Logic::setupMcodeTable() {
static const BSMcodeTable mcodeTable[100] = {
&Logic::fnBackground,
&Logic::fnForeground,
&Logic::fnSort,
&Logic::fnNoSprite,
&Logic::fnMegaSet,
&Logic::fnAnim,
&Logic::fnSetFrame,
&Logic::fnFullAnim,
&Logic::fnFullSetFrame,
&Logic::fnFadeDown,
&Logic::fnFadeUp,
&Logic::fnCheckFade,
&Logic::fnSetSpritePalette,
&Logic::fnSetWholePalette,
&Logic::fnSetFadeTargetPalette,
&Logic::fnSetPaletteToFade,
&Logic::fnSetPaletteToCut,
&Logic::fnPlaySequence,
&Logic::fnIdle,
&Logic::fnPause,
&Logic::fnPauseSeconds,
&Logic::fnQuit,
&Logic::fnKillId,
&Logic::fnSuicide,
&Logic::fnNewScript,
&Logic::fnSubScript,
&Logic::fnRestartScript,
&Logic::fnSetBookmark,
&Logic::fnGotoBookmark,
&Logic::fnSendSync,
&Logic::fnWaitSync,
&Logic::cfnClickInteract,
&Logic::cfnSetScript,
&Logic::cfnPresetScript,
&Logic::fnInteract,
&Logic::fnIssueEvent,
&Logic::fnCheckForEvent,
&Logic::fnWipeHands,
&Logic::fnISpeak,
&Logic::fnTheyDo,
&Logic::fnTheyDoWeWait,
&Logic::fnWeWait,
&Logic::fnChangeSpeechText,
&Logic::fnTalkError,
&Logic::fnStartTalk,
&Logic::fnCheckForTextLine,
&Logic::fnAddTalkWaitStatusBit,
&Logic::fnRemoveTalkWaitStatusBit,
&Logic::fnNoHuman,
&Logic::fnAddHuman,
&Logic::fnBlankMouse,
&Logic::fnNormalMouse,
&Logic::fnLockMouse,
&Logic::fnUnlockMouse,
&Logic::fnSetMousePointer,
&Logic::fnSetMouseLuggage,
&Logic::fnMouseOn,
&Logic::fnMouseOff,
&Logic::fnChooser,
&Logic::fnEndChooser,
&Logic::fnStartMenu,
&Logic::fnEndMenu,
&Logic::cfnReleaseMenu,
&Logic::fnAddSubject,
&Logic::fnAddObject,
&Logic::fnRemoveObject,
&Logic::fnEnterSection,
&Logic::fnLeaveSection,
&Logic::fnChangeFloor,
&Logic::fnWalk,
&Logic::fnTurn,
&Logic::fnStand,
&Logic::fnStandAt,
&Logic::fnFace,
&Logic::fnFaceXy,
&Logic::fnIsFacing,
&Logic::fnGetTo,
&Logic::fnGetToError,
&Logic::fnGetPos,
&Logic::fnGetGamepadXy,
&Logic::fnPlayFx,
&Logic::fnStopFx,
&Logic::fnPlayMusic,
&Logic::fnStopMusic,
&Logic::fnInnerSpace,
&Logic::fnRandom,
&Logic::fnSetScreen,
&Logic::fnPreload,
&Logic::fnCheckCD,
&Logic::fnRestartGame,
&Logic::fnQuitGame,
&Logic::fnDeathScreen,
&Logic::fnSetParallax,
&Logic::fnTdebug,
&Logic::fnRedFlash,
&Logic::fnBlueFlash,
&Logic::fnYellow,
&Logic::fnGreen,
&Logic::fnPurple,
&Logic::fnBlack
};
_mcodeTable = mcodeTable;
}
int Logic::fnBackground(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
cpt->o_status &= ~(STAT_FORE | STAT_SORT);
cpt->o_status |= STAT_BACK;
return SCRIPT_CONT;
}
int Logic::fnForeground(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
cpt->o_status &= ~(STAT_BACK | STAT_SORT);
cpt->o_status |= STAT_FORE;
return SCRIPT_CONT;
}
int Logic::fnSort(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
cpt->o_status &= ~(STAT_BACK | STAT_FORE);
cpt->o_status |= STAT_SORT;
return SCRIPT_CONT;
}
int Logic::fnNoSprite(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
cpt->o_status &= ~(STAT_BACK | STAT_FORE | STAT_SORT);
return SCRIPT_CONT;
}
int Logic::fnMegaSet(Object *cpt, int32 id, int32 walk_data, int32 spr, int32 e, int32 f, int32 z, int32 x) {
cpt->o_mega_resource = walk_data;
cpt->o_walk_resource = spr;
return SCRIPT_CONT;
}
int Logic::fnAnim(Object *cpt, int32 id, int32 cdt, int32 spr, int32 e, int32 f, int32 z, int32 x) {
AnimSet *animTab;
if (cdt && (!spr)) {
animTab = (AnimSet *)((uint8 *)_resMan->openFetchRes(cdt) + sizeof(Header));
animTab += cpt->o_dir;
cpt->o_anim_resource = _resMan->getUint32(animTab->cdt);
cpt->o_resource = _resMan->getUint32(animTab->spr);
_resMan->resClose(cdt);
} else {
cpt->o_anim_resource = cdt;
cpt->o_resource = spr;
}
if ((cpt->o_anim_resource == 0) || (cpt->o_resource == 0))
error("fnAnim called width (%d/%d) => (%d/%d)", cdt, spr, cpt->o_anim_resource, cpt->o_resource);
FrameHeader *frameHead = _resMan->fetchFrame(_resMan->openFetchRes(cpt->o_resource), 0);
if (frameHead->offsetX || frameHead->offsetY) { // boxed mega anim?
cpt->o_status |= STAT_SHRINK;
cpt->o_anim_x = cpt->o_xcoord; // set anim coords to 'feet' coords - only need to do this once
cpt->o_anim_y = cpt->o_ycoord;
} else {
// Anim_driver sets anim coords to cdt coords for every frame of a loose anim
cpt->o_status &= ~STAT_SHRINK;
}
_resMan->resClose(cpt->o_resource);
cpt->o_logic = LOGIC_anim;
cpt->o_anim_pc = 0;
cpt->o_sync = 0;
return SCRIPT_STOP;
}
int Logic::fnSetFrame(Object *cpt, int32 id, int32 cdt, int32 spr, int32 frameNo, int32 f, int32 z, int32 x) {
AnimUnit *animPtr;
uint8 *data = (uint8 *)_resMan->openFetchRes(cdt);
data += sizeof(Header);
if (frameNo == LAST_FRAME)
frameNo = _resMan->readUint32(data) - 1;
data += 4;
animPtr = (AnimUnit *)(data + frameNo * sizeof(AnimUnit));
cpt->o_anim_x = _resMan->getUint32(animPtr->animX);
cpt->o_anim_y = _resMan->getUint32(animPtr->animY);
cpt->o_frame = _resMan->getUint32(animPtr->animFrame);
cpt->o_resource = spr;
cpt->o_status &= ~STAT_SHRINK;
_resMan->resClose(cdt);
return SCRIPT_CONT;
}
int Logic::fnFullAnim(Object *cpt, int32 id, int32 anim, int32 graphic, int32 e, int32 f, int32 z, int32 x) {
cpt->o_logic = LOGIC_full_anim;
cpt->o_anim_pc = 0;
cpt->o_anim_resource = anim;
cpt->o_resource = graphic;
cpt->o_status &= ~STAT_SHRINK;
cpt->o_sync = 0;
return SCRIPT_STOP;
}
int Logic::fnFullSetFrame(Object *cpt, int32 id, int32 cdt, int32 spr, int32 frameNo, int32 f, int32 z, int32 x) {
uint8 *data = (uint8 *)_resMan->openFetchRes(cdt) + sizeof(Header);
if (frameNo == LAST_FRAME)
frameNo = _resMan->readUint32(data) - 1;
data += 4;
AnimUnit *animPtr = (AnimUnit *)(data + sizeof(AnimUnit) * frameNo);
cpt->o_anim_x = cpt->o_xcoord = _resMan->getUint32(animPtr->animX);
cpt->o_anim_y = cpt->o_ycoord = _resMan->getUint32(animPtr->animY);
cpt->o_frame = _resMan->getUint32(animPtr->animFrame);
cpt->o_resource = spr;
cpt->o_status &= ~STAT_SHRINK;
_resMan->resClose(cdt);
return SCRIPT_CONT;
}
int Logic::fnFadeDown(Object *cpt, int32 id, int32 speed, int32 d, int32 e, int32 f, int32 z, int32 x) {
_screen->fadeDownPalette();
return SCRIPT_CONT;
}
int Logic::fnFadeUp(Object *cpt, int32 id, int32 speed, int32 d, int32 e, int32 f, int32 z, int32 x) {
_screen->fadeUpPalette();
return SCRIPT_CONT;
}
int Logic::fnCheckFade(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
_scriptVars[RETURN_VALUE] = (uint8)_screen->stillFading();
return SCRIPT_CONT;
}
int Logic::fnSetSpritePalette(Object *cpt, int32 id, int32 spritePal, int32 d, int32 e, int32 f, int32 z, int32 x) {
_screen->fnSetPalette(184, 72, spritePal, false);
return SCRIPT_CONT;
}
int Logic::fnSetWholePalette(Object *cpt, int32 id, int32 spritePal, int32 d, int32 e, int32 f, int32 z, int32 x) {
_screen->fnSetPalette(0, 256, spritePal, false);
return SCRIPT_CONT;
}
int Logic::fnSetFadeTargetPalette(Object *cpt, int32 id, int32 spritePal, int32 d, int32 e, int32 f, int32 z, int32 x) {
_screen->fnSetPalette(0, 184, spritePal, true);
return SCRIPT_CONT;
}
int Logic::fnSetPaletteToFade(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
SwordEngine::_systemVars.wantFade = true;
return SCRIPT_CONT;
}
int Logic::fnSetPaletteToCut(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
SwordEngine::_systemVars.wantFade = false;
return SCRIPT_CONT;
}
int Logic::fnPlaySequence(Object *cpt, int32 id, int32 sequenceId, int32 d, int32 e, int32 f, int32 z, int32 x) {
// A cutscene usually (always?) means the room will change. In the
// meantime, we don't want any looping sound effects still playing.
_sound->quitScreen();
MoviePlayer *player = makeMoviePlayer(sequenceId, _vm, _textMan, _resMan, _system);
if (player) {
_screen->clearScreen();
if (player->load(sequenceId))
player->play();
delete player;
}
return SCRIPT_CONT;
}
int Logic::fnIdle(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
cpt->o_tree.o_script_level = 0; // force to level 0
cpt->o_logic = LOGIC_idle;
return SCRIPT_STOP;
}
int Logic::fnPause(Object *cpt, int32 id, int32 pause, int32 d, int32 e, int32 f, int32 z, int32 x) {
cpt->o_pause = pause;
cpt->o_logic = LOGIC_pause;
return SCRIPT_STOP;
}
int Logic::fnPauseSeconds(Object *cpt, int32 id, int32 pause, int32 d, int32 e, int32 f, int32 z, int32 x) {
cpt->o_pause = pause * FRAME_RATE;
cpt->o_logic = LOGIC_pause;
return SCRIPT_STOP;
}
int Logic::fnQuit(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
cpt->o_logic = LOGIC_quit;
return SCRIPT_STOP;
}
int Logic::fnKillId(Object *cpt, int32 id, int32 target, int32 d, int32 e, int32 f, int32 z, int32 x) {
Object *targetObj = _objMan->fetchObject(target);
targetObj->o_status = 0;
return SCRIPT_CONT;
}