aboutsummaryrefslogtreecommitdiff
path: root/engines/xeen/scripts.cpp
blob: 37222a3d76c51bcd6aa6bac176266c48e08e7b14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
/* 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 "xeen/scripts.h"
#include "xeen/dialogs_input.h"
#include "xeen/dialogs_whowill.h"
#include "xeen/dialogs_query.h"
#include "xeen/party.h"
#include "xeen/resources.h"
#include "xeen/xeen.h"

namespace Xeen {

MazeEvent::MazeEvent() : _direction(DIR_ALL), _line(-1), _opcode(OP_None) {
}

void MazeEvent::synchronize(Common::Serializer &s) {
	int len = 5 + _parameters.size();
	s.syncAsByte(len);

	s.syncAsByte(_position.x);
	s.syncAsByte(_position.y);
	s.syncAsByte(_direction);
	s.syncAsByte(_line);
	s.syncAsByte(_opcode);

	len -= 5;
	if (s.isLoading())
		_parameters.resize(len);
	for (int i = 0; i < len; ++i)
		s.syncAsByte(_parameters[i]);
}

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

void MazeEvents::synchronize(XeenSerializer &s) {
	MazeEvent e;

	if (s.isLoading()) {
		clear();
		while (!s.finished()) {
			e.synchronize(s);
			push_back(e);
		}
	} else {
		for (uint i = 0; i < size(); ++i)
			(*this).operator[](i).synchronize(s);
	}
}

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

bool MirrorEntry::synchronize(Common::SeekableReadStream &s) {
	if (s.pos() >= s.size())
		return false;

	char buffer[28];
	s.read(buffer, 28);
	buffer[27] = '\0';

	_name = Common::String(buffer);
	_mapId = s.readByte();
	_position.x = s.readSByte();
	_position.y = s.readSByte();
	_direction = s.readSByte();
	return true;
}

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

Scripts::Scripts(XeenEngine *vm) : _vm(vm) {
	Common::fill(&_charFX[0], &_charFX[MAX_ACTIVE_PARTY], 0);
	_whoWill = 0;
	_itemType = 0;
	_treasureItems = 0;
	_treasureGold = 0;
	_treasureGems = 0;
	_lineNum = 0;
	_charIndex = 0;
	_v2 = 0;
	_nEdamageType = 0;
	_animCounter = 0;
	_eventSkipped = false;
	_mirrorId = -1;
	_refreshIcons = false;
	_scriptResult = false;
	_scriptExecuted = false;
	_var50 = false;
}

int Scripts::checkEvents() {
	Combat &combat = *_vm->_combat;
	EventsManager &events = *_vm->_events;
	Interface &intf = *_vm->_interface;
	Map &map = *_vm->_map;
	Party &party = *_vm->_party;
	Screen &screen = *_vm->_screen;
	SoundManager &sound = *_vm->_sound;

	_refreshIcons = false;
	_itemType = 0;
	_scriptExecuted = false;
	_var50 = false;
	_whoWill = 0;
	Mode oldMode = _vm->_mode;
	Common::fill(&_charFX[0], &_charFX[MAX_ACTIVE_PARTY], 0);
	//int items = _treasureItems;
	
	if (_treasureGold & _treasureItems) {
		// TODO
	} else {
		// TODO
	}

	do {
		_lineNum = 0;
		_scriptResult = false;
		_animCounter = 0;
//		int var4E = 0;
		_currentPos = party._mazePosition;
		_charIndex = 1;
		_v2 = 1;
		_nEdamageType = 0;
//		int var40 = -1;

		while (!_vm->shouldQuit() && _lineNum >= 0) {
			// Break out of the events if there's an attacking monster
			if (combat._attackMonsters[0] != -1) {
				_eventSkipped = true;
				break;
			}

			_eventSkipped = false;
			uint eventIndex;
			for (eventIndex = 0; eventIndex < map._events.size(); ++eventIndex) {
				MazeEvent &event = map._events[eventIndex];

				if (event._position == _currentPos && party._mazeDirection != 
						(_currentPos.x | _currentPos.y) && event._line == _lineNum) {
					if (event._direction == party._mazeDirection || event._direction == DIR_ALL) {
						_vm->_mode = MODE_9;
						_scriptExecuted = true;
						doOpcode(event);
						break;
					} else {
						_var50 = true;
					}
				}
			}
			if (eventIndex == map._events.size())
				_lineNum = -1;
		}
	} while (!_vm->shouldQuit() && _lineNum != -1);

	intf._face1State = intf._face2State = 2;
	if (_refreshIcons) {
		screen.closeWindows();
		intf.drawParty(true);
	}

	// TODO

	_animCounter = 0;
	_vm->_mode = oldMode;
	screen.closeWindows();

	if (_scriptExecuted || !intf._objNumber || _var50) {
		if (_var50 && !_scriptExecuted && intf._objNumber && !map._currentIsEvent) {
			sound.playFX(21);
		}
	} else {
		Window &w = screen._windows[38];
		w.open();
		w.writeString(NOTHING_HERE);
		w.update();

		do {
			intf.draw3d(true);
			events.updateGameCounter();
			events.wait(1, true);
		} while (!events.isKeyMousePressed());
		events.clearEvents();

		w.close();
	}

	_v2 = 1;
	Common::fill(&_charFX[0], &_charFX[6], 0);

	return _scriptResult;
}

void Scripts::giveTreasure() {
	// TODO
}

void Scripts::openGrate(int v1, int v2) {
	// TODO
}

typedef void(Scripts::*ScriptMethodPtr)(Common::Array<byte> &);

/**
 * Handles executing a given script command
 */
void Scripts::doOpcode(MazeEvent &event) {
	static const ScriptMethodPtr COMMAND_LIST[] = {
		nullptr, &Scripts::cmdDisplay1, &Scripts::cmdDoorTextSml,
		&Scripts::cmdDoorTextLrg, &Scripts::cmdSignText,
		&Scripts::cmdNPC, &Scripts::cmdPlayFX, &Scripts::cmdTeleport,
		&Scripts::cmdIf, &Scripts::cmdIf, &Scripts::cmdIf,
		&Scripts::cmdMoveObj, &Scripts::cmdTakeOrGive, &Scripts::cmdNoAction,
		&Scripts::cmdRemove, &Scripts::cmdSetChar, &Scripts::cmdSpawn,
		&Scripts::cmdDoTownEvent, &Scripts::cmdExit, &Scripts::cmdAlterMap,
		&Scripts::cmdGiveExtended, &Scripts::cmdConfirmWord, &Scripts::cmdDamage,
		&Scripts::cmdJumpRnd, &Scripts::cmdAlterEvent, &Scripts::cmdCallEvent,
		&Scripts::cmdReturn, &Scripts::cmdSetVar, &Scripts::cmdTakeOrGive,
		&Scripts::cmdTakeOrGive, &Scripts::cmdCutsceneEndClouds,
		&Scripts::cmdTeleport, &Scripts::cmdWhoWill,
		&Scripts::cmdRndDamage, &Scripts::cmdMoveWallObj, &Scripts::cmdAlterCellFlag,
		&Scripts::cmdAlterHed, &Scripts::cmdDisplayStat, &Scripts::cmdTakeOrGive,
		&Scripts::cmdSeatTextSml, &Scripts::cmdPlayEventVoc, &Scripts::cmdDisplayBottom,
		&Scripts::cmdIfMapFlag, &Scripts::cmdSelRndChar, &Scripts::cmdGiveEnchanted,
		&Scripts::cmdItemType, &Scripts::cmdMakeNothingHere, &Scripts::cmdCheckProtection,
		&Scripts::cmdChooseNumeric, &Scripts::cmdDisplayBottomTwoLines,
		&Scripts::cmdDisplayLarge, &Scripts::cmdExchObj, &Scripts::cmdFallToMap,
		&Scripts::cmdDisplayMain, &Scripts::cmdGoto, &Scripts::cmdConfirmWord,
		&Scripts::cmdGotoRandom, &Scripts::cmdCutsceneEndDarkside,
		&Scripts::cmdCutsceneEdWorld, &Scripts::cmdFlipWorld, &Scripts::cmdPlayCD
	};

	_event = &event;
	(this->*COMMAND_LIST[event._opcode])(event._parameters);
}

/**
 * Display a msesage on-screen
 */
void Scripts::cmdDisplay1(Common::Array<byte> &params) {
	Screen &screen = *_vm->_screen;
	Common::String paramText = _vm->_map->_events._text[_event->_parameters[0]];
	Common::String msg = Common::String::format("\r\x03""c%s", paramText.c_str());

	screen._windows[12].close();
	if (screen._windows[38]._enabled)
		screen._windows[38].open();
	screen._windows[38].writeString(msg);
	screen._windows[38].update();

	cmdNoAction(params);
}

/**
 * Displays a door text message using the small font
 */
void Scripts::cmdDoorTextSml(Common::Array<byte> &params) {
	Interface &intf = *_vm->_interface;

	Common::String paramText = _vm->_map->_events._text[_event->_parameters[0]];
	intf._screenText = Common::String::format("\x02\f""08\x03""c\t116\v025%s\x03""l\fd""\x01", 
		paramText.c_str());
	intf._upDoorText = true;
	intf.draw3d(true);

	cmdNoAction(params);
}

/**
 * Displays a door text message using the large font
 */
void Scripts::cmdDoorTextLrg(Common::Array<byte> &params) {
	Interface &intf = *_vm->_interface;

	Common::String paramText = _vm->_map->_events._text[_event->_parameters[0]];
	intf._screenText = Common::String::format("\f04\x03""c\t116\v030%s\x03""l\fd",
		paramText.c_str());
	intf._upDoorText = true;
	intf.draw3d(true);

	cmdNoAction(params);
}

/**
 * Show a sign text on-screen
 */
void Scripts::cmdSignText(Common::Array<byte> &params) {
	Interface &intf = *_vm->_interface;

	Common::String paramText = _vm->_map->_events._text[_event->_parameters[0]];
	intf._screenText = Common::String::format("\f08\x03""c\t120\v088%s\x03""l\fd",
		paramText.c_str());
	intf._upDoorText = true;
	intf.draw3d(true);

	cmdNoAction(params);
}

void Scripts::cmdNPC(Common::Array<byte> &params) {
	warning("TODO: cmdNPC");
}

/**
 * Play a sound FX
 */
void Scripts::cmdPlayFX(Common::Array<byte> &params) {
	_vm->_sound->playFX(params[0]);

	cmdNoAction(params);
}

void Scripts::cmdTeleport(Common::Array<byte> &params) {
	EventsManager &events = *_vm->_events;
	Interface &intf = *_vm->_interface;
	Map &map = *_vm->_map;
	Party &party = *_vm->_party;
	Screen &screen = *_vm->_screen;
	SoundManager &sound = *_vm->_sound;

	screen.closeWindows();

	int mapId;
	Common::Point pt;

	if (params[0]) {
		mapId = params[0];
		pt = Common::Point((int8)params[1], (int8)params[2]);
	} else {
		assert(_mirrorId > 0);
		MirrorEntry &me = _mirror[_mirrorId - 1];
		mapId = me._mapId;
		pt = me._position;
		if (me._direction != -1)
			party._mazeDirection = (Direction)me._direction;

		if (pt.x == 0 && pt.y == 0)
			pt.x = 999;

		sound.playFX(51);
	}

	party._stepped = true;
	if (mapId != party._mazeId) {
		int spriteId = (intf._objNumber == 0) ? -1 :
			map._mobData._objects[intf._objNumber - 1]._spriteId;

		switch (spriteId) {
		case 47:
			sound.playFX(45);
			break;
		case 48:
			sound.playFX(44);
			break;
		default:
			break;
		}

		// Load the new map
		map.load(mapId);
	}

	if (pt.x == 999) {
		party.moveToRunLocation();
	} else {
		party._mazePosition = pt;
	}

	events.clearEvents();

	if (_event->_opcode == OP_TeleportAndContinue) {
		intf.draw3d(true);
		_lineNum = 0;
	} else {
		cmdExit(params);
	}
}

/**
 * Do a conditional check
 */
void Scripts::cmdIf(Common::Array<byte> &params) {
	Party &party = *_vm->_party;
	uint32 mask;
	int newLineNum;

	switch (params[0]) {
	case 16:
	case 34:
	case 100:
		mask = (params[4] << 24) | (params[3] << 16) | (params[2] << 8) | params[1];
		newLineNum = params[5];
		break;
	case 25:
	case 35:
	case 101:
	case 106:
		mask = (params[2] << 8) | params[1];
		newLineNum = params[3];
		break;
	default:
		mask = params[1];
		newLineNum = params[2];
		break;
	}

	bool result;
	if ((_charIndex != 0 && _charIndex != 8) || params[0] == 44) {
		result = ifProc(params[0], mask, _event->_opcode - 8, _charIndex - 1);
	} else {
		result = false;
		for (int idx = 0; idx < (int)party._activeParty.size() && !result; ++idx) {
			if (_charIndex == 0 || (_charIndex == 8 && idx != _v2)) {
				result = ifProc(params[0], mask, _event->_opcode - 8, idx);
			}
		}
	}

	if (result)
		_lineNum = newLineNum - 1;

	cmdNoAction(params);
}

/**
 * Moves the position of an object
 */
void Scripts::cmdMoveObj(Common::Array<byte> &params) { 
	MazeObject &mazeObj = _vm->_map->_mobData._objects[params[0]];

	if (mazeObj._position.x == params[1] && mazeObj._position.y == params[2]) {
		// Already in position, so simply flip it
		mazeObj._flipped = !mazeObj._flipped;
	} else {
		mazeObj._position.x = params[1];
		mazeObj._position.y = params[2];
	}
}

void Scripts::cmdTakeOrGive(Common::Array<byte> &params) { error("TODO"); }

/**
 * Move to the next line of the script
 */
void Scripts::cmdNoAction(Common::Array<byte> &params) {
	// Move to next line
	_lineNum = _vm->_party->_partyDead ? -1 : _lineNum + 1;
}

void Scripts::cmdRemove(Common::Array<byte> &params) { 
	Interface &intf = *_vm->_interface;
	Map &map = *_vm->_map;

	if (intf._objNumber) {
		// Give the active object a completely way out of bounds position
		MazeObject &obj = map._mobData._objects[intf._objNumber - 1];
		obj._position = Common::Point(128, 128);
	}

	cmdMakeNothingHere(params);
}

/**
 * Set the currently active character for other script operations
 */
void Scripts::cmdSetChar(Common::Array<byte> &params) { 
	if (params[0] != 7) {
		_charIndex = WhoWill::show(_vm, 22, 3, false);
		if (_charIndex == 0) {
			cmdExit(params);
			return;
		}
	} else {
		_charIndex = _vm->getRandomNumber(1, _vm->_party->_activeParty.size());
	}

	_v2 = 1;
	cmdNoAction(params);
}

/**
 * Spawn a monster
 */
void Scripts::cmdSpawn(Common::Array<byte> &params) {
	Map &map = *_vm->_map;
	if (params[0] >= map._mobData._monsters.size())
		map._mobData._monsters.resize(params[0] + 1);

	MazeMonster &monster = _vm->_map->_mobData._monsters[params[0]];
	MonsterStruct &monsterData = _vm->_map->_monsterData[monster._spriteId];
	monster._position.x = params[1];
	monster._position.y = params[2];
	monster._frame = _vm->getRandomNumber(7);
	monster._field7 = 0;
	monster._isAttacking = params[1] != 0;
	monster._hp = monsterData._hp;

	cmdNoAction(params);
}

void Scripts::cmdDoTownEvent(Common::Array<byte> &params) {
	_scriptResult = _vm->_town->townAction(params[0]);
	_vm->_party->_stepped = true;
	_refreshIcons = true;

	cmdExit(params);
}

/**
 * Stop executing the script
 */
void Scripts::cmdExit(Common::Array<byte> &params) { 
	_lineNum = -1;
}

/**
 * Changes the value for the wall on a given cell
 */
void Scripts::cmdAlterMap(Common::Array<byte> &params) { 
	Map &map = *_vm->_map;

	if (params[2] == DIR_ALL) {
		for (int dir = DIR_NORTH; dir <= DIR_WEST; ++dir)
			map.setWall(Common::Point(params[0], params[1]), (Direction)dir, params[3]);
	} else {
		map.setWall(Common::Point(params[0], params[1]), (Direction)params[2], params[3]);
	}

	cmdNoAction(params);
}

void Scripts::cmdGiveExtended(Common::Array<byte> &params) { 
	Party &party = *_vm->_party;
	uint32 mask;
	int newLineNum;
	bool result;

	switch (params[0]) {
	case 16:
	case 34:
	case 100:
		mask = (params[4] << 24) | params[3] | (params[2] << 8) | (params[1] << 16);
		newLineNum = params[5];
		break;
	case 25:
	case 35:
	case 101:
	case 106:
		mask = (params[2] << 8) | params[1];
		newLineNum = params[3];
		break;
	default:
		mask = params[1];
		newLineNum = params[2];
		break;
	}

	if ((_charIndex != 0 && _charIndex != 8) || params[0] == 44) {
		result = ifProc(params[0], mask, _event->_opcode - OP_If1, _charIndex - 1);
	} else {
		result = false;
		for (int idx = 0; idx < (int)party._activeParty.size() && !result; ++idx) {
			if (_charIndex == 0 || (_charIndex == 8 && _v2 != idx)) {
				result = ifProc(params[0], mask, _event->_opcode - OP_If1, idx);
			}
		}
	}

	if (result)
		_lineNum = newLineNum - 1;

	cmdNoAction(params);
}

void Scripts::cmdConfirmWord(Common::Array<byte> &params) { 
	Map &map = *_vm->_map;
	Common::String msg1 = params[2] ? map._events._text[params[2]] :
		_vm->_interface->_interfaceText;
	Common::String msg2;

	if (_event->_opcode == OP_ConfirmWord_2) {
		msg2 = map._events._text[params[3]];
	} else if (params[3]) {
		msg2 = "";
	} else {
		msg2 = WHATS_THE_PASSWORD;
	}

	int result = StringInput::show(_vm, params[0], msg1, msg2,_event->_opcode);
	if (result) {
		if (result == 33 && _vm->_files->_isDarkCc) {
			doEndGame2();
		} else if (result == 34 && _vm->_files->_isDarkCc) {
			doWorldEnd();
		} else if (result == 35 && _vm->_files->_isDarkCc &&
				_vm->getGameID() == GType_WorldOfXeen) {
			doEndGame();
		} else if (result == 40 && !_vm->_files->_isDarkCc) {
			doEndGame();
		} else if (result == 60 && !_vm->_files->_isDarkCc) {
			doEndGame2();
		}
		else if (result == 61 && !_vm->_files->_isDarkCc) {
			doWorldEnd();
		} else {		
			if (result == 59 && !_vm->_files->_isDarkCc) {
				for (int idx = 0; idx < TOTAL_ITEMS; ++idx) {
					XeenItem &item = _vm->_treasure._weapons[idx];
					if (!item._id) {
						item._id = 34;
						item._material = 0;
						item._bonusFlags = 0;
						_vm->_treasure._hasItems = true;
						
						cmdExit(params);
						return;
					}
				}
			}

			_lineNum = result == -1 ? params[3] : params[1];
			
			return;
		}
	}

	cmdNoAction(params);
}

void Scripts::cmdDamage(Common::Array<byte> &params) { error("TODO"); }

/**
 * Jump if a random number matches a given value
 */
void Scripts::cmdJumpRnd(Common::Array<byte> &params) {
	int v = _vm->getRandomNumber(1, params[0]);
	if (v == params[1])
		_lineNum = params[2] - 1;

	cmdNoAction(params);
}

/**
 * Alter an existing event
 */
void Scripts::cmdAlterEvent(Common::Array<byte> &params) {
	Map &map = *_vm->_map;
	Party &party = *_vm->_party;

	for (uint idx = 0; idx < map._events.size(); ++idx) {
		MazeEvent &evt = map._events[idx];
		if (evt._position == party._mazePosition &&
				(evt._direction == DIR_ALL || evt._direction == party._mazeDirection) &&
				evt._line == params[0]) {
			evt._opcode = (Opcode)params[1];
		}
	}

	cmdNoAction(params);
}

/**
 * Stores the current location and line for later resuming, and set up to execute
 * a script at a given location
 */
void Scripts::cmdCallEvent(Common::Array<byte> &params) { 
	_stack.push(StackEntry(_currentPos, _lineNum));
	_currentPos = Common::Point(params[0], params[1]);
	_lineNum = params[2] - 1;

	cmdNoAction(params);
}

/**
 * Return from executing a script to the script location that previously 
 * called the script
 */
void Scripts::cmdReturn(Common::Array<byte> &params) {
	StackEntry &se = _stack.top();
	_currentPos = se;
	_lineNum = se.line;

	cmdNoAction(params);
}

void Scripts::cmdSetVar(Common::Array<byte> &params) { 
	Party &party = *_vm->_party;
	uint val;
	_refreshIcons = true;

	switch (params[0]) {
	case 25:
	case 35:
	case 101:
	case 106:
		val = (params[2] << 8) | params[1];
		break;
	case 16:
	case 34:
	case 100:
		val = (params[4] << 24) | (params[3] << 16) | (params[2] << 8) | params[3];
		break;
	default:
		val = params[1];
		break;
	}

	if (_charIndex != 0 && _charIndex != 8) {
		party._activeParty[_charIndex - 1].setValue(params[0], val);
	} else {
		// Set value for entire party
		for (int idx = 0; idx < (int)party._activeParty.size(); ++idx) {
			if (_charIndex == 0 || (_charIndex == 8 && _v2 != idx)) {
				party._activeParty[idx].setValue(params[0], val);
			}
		}
	}

	cmdNoAction(params);
}

void Scripts::cmdCutsceneEndClouds(Common::Array<byte> &params) { error("TODO"); }

void Scripts::cmdWhoWill(Common::Array<byte> &params) { 
	_charIndex = WhoWill::show(_vm, params[0], params[1], true);

	if (_charIndex == 0)
		cmdExit(params);
	else
		cmdNoAction(params);
}

void Scripts::cmdRndDamage(Common::Array<byte> &params) { error("TODO"); }
void Scripts::cmdMoveWallObj(Common::Array<byte> &params) { error("TODO"); }
void Scripts::cmdAlterCellFlag(Common::Array<byte> &params) { error("TODO"); }
void Scripts::cmdAlterHed(Common::Array<byte> &params) { error("TODO"); }
void Scripts::cmdDisplayStat(Common::Array<byte> &params) { error("TODO"); }
void Scripts::cmdSeatTextSml(Common::Array<byte> &params) { error("TODO"); }
void Scripts::cmdPlayEventVoc(Common::Array<byte> &params) { error("TODO"); }
void Scripts::cmdDisplayBottom(Common::Array<byte> &params) { error("TODO"); }
void Scripts::cmdIfMapFlag(Common::Array<byte> &params) { error("TODO"); }
void Scripts::cmdSelRndChar(Common::Array<byte> &params) { error("TODO"); }
void Scripts::cmdGiveEnchanted(Common::Array<byte> &params) { error("TODO"); }
void Scripts::cmdItemType(Common::Array<byte> &params) { error("TODO"); }

/**
 * Disable all the scripts at the party's current position
 */
void Scripts::cmdMakeNothingHere(Common::Array<byte> &params) {
	Map &map = *_vm->_map;
	Party &party = *_vm->_party;

	// Scan through the event list and mark the opcodes for all the lines of any scripts
	// on the party's current cell as having no operation, effectively disabling them
	for (uint idx = 0; idx < map._events.size(); ++idx) {
		MazeEvent &evt = map._events[idx];
		if (evt._position == party._mazePosition)
			evt._opcode = OP_None;
	}

	cmdExit(params);
}

void Scripts::cmdCheckProtection(Common::Array<byte> &params) {
	if (copyProtectionCheck())
		cmdNoAction(params);
	else
		cmdExit(params);
}

void Scripts::cmdChooseNumeric(Common::Array<byte> &params) { error("TODO"); }
void Scripts::cmdDisplayBottomTwoLines(Common::Array<byte> &params) { error("TODO"); }
void Scripts::cmdDisplayLarge(Common::Array<byte> &params) { error("TODO"); }

/**
 * Exchange the positions of two objects in the maze
 */
void Scripts::cmdExchObj(Common::Array<byte> &params) {
	MazeObject &obj1 = _vm->_map->_mobData._objects[params[0]];
	MazeObject &obj2 = _vm->_map->_mobData._objects[params[1]];

	Common::Point pt = obj1._position;
	obj1._position = obj2._position;
	obj2._position = pt;

	cmdNoAction(params);
}

void Scripts::cmdFallToMap(Common::Array<byte> &params) {
	Interface &intf = *_vm->_interface;
	Party &party = *_vm->_party;
	party._fallMaze = params[0];
	party._fallPosition = Common::Point(params[1], params[2]);
	party._fallDamage = params[3];
	intf.startFalling(true);

	_lineNum = -1;
}

void Scripts::cmdDisplayMain(Common::Array<byte> &params) { 
	error("TODO"); 
}

/**
 * Jumps to a given line number if the surface at relative cell position 1 matches
 * a specified surface.
 * @remarks		This opcode is apparently never actually used
 */
void Scripts::cmdGoto(Common::Array<byte> &params) { 
	Map &map = *_vm->_map;
	map.getCell(1);
	if (params[0] == map._currentSurfaceId)
		_lineNum = params[1] - 1;

	cmdNoAction(params);
}

/**
 * Pick a random value from the parameter list and jump to that line number
 */
void Scripts::cmdGotoRandom(Common::Array<byte> &params) { 
	_lineNum = params[_vm->getRandomNumber(1, params[0])] - 1;
	cmdNoAction(params);
}

void Scripts::cmdCutsceneEndDarkside(Common::Array<byte> &params) { 
	Party &party = *_vm->_party;
	_vm->_saves->_wonDarkSide = true;
	party._questItems[53] = 1;
	party._darkSideEnd = true;
	party._mazeId = 29;
	party._mazeDirection = DIR_NORTH;
	party._mazePosition = Common::Point(25, 21);

	doEndGame2();
}

void Scripts::cmdCutsceneEdWorld(Common::Array<byte> &params) { 
	_vm->_saves->_wonWorld = true;
	_vm->_party->_worldEnd = true;
	doWorldEnd();
}

void Scripts::cmdFlipWorld(Common::Array<byte> &params) { 
	_vm->_map->_loadDarkSide = params[0] != 0;
}

void Scripts::cmdPlayCD(Common::Array<byte> &params) { error("TODO"); }

void Scripts::doEndGame() {
	doEnding("ENDGAME", 0);
}

void Scripts::doEndGame2() {
	Party &party = *_vm->_party;
	int v2 = 0;

	for (uint idx = 0; idx < party._activeParty.size(); ++idx) {
		Character &player = party._activeParty[idx];
		if (player.hasAward(77)) {
			v2 = 2;
			break;
		}
		else if (player.hasAward(76)) {
			v2 = 1;
			break;
		}
	}

	doEnding("ENDGAME2", v2);
}

void Scripts::doWorldEnd() {

}

void Scripts::doEnding(const Common::String &endStr, int v2) {
	_vm->_saves->saveChars();
	
	warning("TODO: doEnding");
}

/**
 * This monstrosity handles doing the various types of If checks on various data
 */
bool Scripts::ifProc(int action, uint32 mask, int mode, int charIndex) {
	Party &party = *_vm->_party;
	Character &ps = party._activeParty[charIndex];
	uint v = 0;

	switch (action) {
	case 3:
		// Player sex
		v = (uint)ps._sex;
		break;
	case 4:
		// Player race
		v = (uint)ps._race;
		break;
	case 5:
		// Player class
		v = (uint)ps._class;
		break;
	case 8:
		// Current health points
		v = (uint)ps._currentHp;
		break;
	case 9:
		// Current spell points
		v = (uint)ps._currentSp;
		break;
	case 10:
		// Get armor class
		v = (uint)ps.getArmorClass(false);
		break;
	case 11:
		// Level bonus (extra beyond base)
		v = ps._level._temporary;
		break;
	case 12:
		// Current age, including unnatural aging
		v = ps.getAge(false);
		break;
	case 13:
		assert(mask < 18);
		if (ps._skills[mask])
			v = mask;
		break;
	case 15:
		// Award
		assert(mask < 128);
		if (ps.hasAward(mask))
			v = mask;
		break;
	case 16:
		// Experience
		v = ps._experience;
		break;
	case 17:
		// Party poison resistence
		v = party._poisonResistence;
		break;
	case 18:
		// Condition
		assert(mask < 16);
		if (!ps._conditions[mask] && !(mask & 0x10))
			v = mask;
		break;
	case 19: {
		// Can player cast a given spell

		// Get the type of character
		int category;
		switch (ps._class) {
		case CLASS_KNIGHT:
		case CLASS_ARCHER:
			category = 0;
			break;
		case CLASS_PALADIN:
		case CLASS_CLERIC:
			category = 1;
			break;
		case CLASS_BARBARIAN:
		case CLASS_DRUID:
			category = 2;
			break;
		default:
			category = 0;
			break;
		}

		// Check if the character class can cast the particular spell
		for (int idx = 0; idx < 39; ++idx) {
			if (SPELLS_ALLOWED[mode][idx] == mask) {
				// Can cast it. Check if the player has it in their spellbook
				if (ps._spells[idx])
					v = mask;
				break;
			}
		}
		break;
	}
	case 20:
		if (_vm->_files->_isDarkCc)
			mask += 0x100;
		assert(mask < 0x200);
		v = party._gameFlags[mask] ? mask : 0xffffffff;
		break;
	case 21:
		// Scans inventories for given item number
		v = 0xFFFFFFFF;
		if (mask < 82) {
			for (int idx = 0; idx < 9; ++idx) {
				if (mask == 35) {
					if (ps._weapons[idx]._id == mask) {
						v = mask;
						break;
					}
				} else if (mask < 49) {
					if (ps._armor[idx]._id == (mask - 35)) {
						v = mask;
						break;
					}
				} else if (mask < 60) {
					if (ps._accessories[idx]._id == (mask - 49)) {
						v = mask;
						break;
					}
				} else {
					if (ps._misc[idx]._id == (mask - 60)) {
						v = mask;
						break;
					}
				}
			}
		} else {
			int baseFlag = 8 * (6 + mask);
			for (int idx = 0; idx < 8; ++idx) {
				if (party._gameFlags[baseFlag + idx]) {
					v = mask;
					break;
				}
			}
		}
		break;
	case 25:
		// Returns number of minutes elapsed in the day (0-1440)
		v = party._minutes;
		break;
	case 34:
		// Current party gold
		v = party._gold;
		break;
	case 35:
		// Current party gems
		v = party._gems;
		break;
	case 37:
		// Might bonus (extra beond base)
		v = ps._might._temporary;
		break;
	case 38:
		// Intellect bonus (extra beyond base)
		v = ps._intellect._temporary;
		break;
	case 39:
		// Personality bonus (extra beyond base)
		v = ps._personality._temporary;
		break;
	case 40:
		// Endurance bonus (extra beyond base)
		v = ps._endurance._temporary;
		break;
	case 41:
		// Speed bonus (extra beyond base)
		v = ps._speed._temporary;
		break;
	case 42:
		// Accuracy bonus (extra beyond base)
		v = ps._accuracy._temporary;
		break;
	case 43:
		// Luck bonus (extra beyond base)
		v = ps._luck._temporary;
		break;
	case 44:
		v = YesNo::show(_vm, mask);
		v = (!v && !mask) ? 2 : mask;
		break;
	case 45:
		// Might base (before bonus)
		v = ps._might._permanent;
		break;
	case 46:
		// Intellect base (before bonus)
		v = ps._intellect._permanent;
		break;
	case 47:
		// Personality base (before bonus)
		v = ps._personality._permanent;
		break;
	case 48:
		// Endurance base (before bonus)
		v = ps._endurance._permanent;
		break;
	case 49:
		// Speed base (before bonus)
		v = ps._speed._permanent;
		break;
	case 50:
		// Accuracy base (before bonus)
		v = ps._accuracy._permanent;
		break;
	case 51:
		// Luck base (before bonus)
		v = ps._luck._permanent;
		break;
	case 52:
		// Fire resistence (before bonus)
		v = ps._fireResistence._permanent;
		break;
	case 53:
		// Elecricity resistence (before bonus)
		v = ps._electricityResistence._permanent;
		break;
	case 54:
		// Cold resistence (before bonus)
		v = ps._coldResistence._permanent;
		break;
	case 55:
		// Poison resistence (before bonus)
		v = ps._poisonResistence._permanent;
		break;
	case 56:
		// Energy reistence (before bonus)
		v = ps._energyResistence._permanent;
		break;
	case 57:
		// Energy resistence (before bonus)
		v = ps._magicResistence._permanent;
		break;
	case 58:
		// Fire resistence (extra beyond base)
		v = ps._fireResistence._temporary;
		break;
	case 59:
		// Electricity resistence (extra beyond base)
		v = ps._electricityResistence._temporary;
		break;
	case 60:
		// Cold resistence (extra beyond base)
		v = ps._coldResistence._temporary;
		break;
	case 61:
		// Poison resistence (extra beyod base)
		v = ps._poisonResistence._temporary;
		break;
	case 62:
		// Energy resistence (extra beyond base)
		v = ps._energyResistence._temporary;
		break;
	case 63:
		// Magic resistence (extra beyond base)
		v = ps._magicResistence._temporary;
		break;
	case 64:
		// Level (before bonus)
		v = ps._level._permanent;
		break;
	case 65:
		// Total party food
		v = party._food;
		break;
	case 69:
		// Test for Levitate being active
		v = party._levitateActive ? 1 : 0;
		break;
	case 70:
		// Amount of light
		v = party._lightCount;
		break;
	case 71:
		// Party magical fire resistence
		v = party._fireResistence;
		break;
	case 72:
		// Party magical electricity resistence
		v = party._electricityResistence;
		break;
	case 73:
		// Party magical cold resistence
		v = party._coldResistence;
		break;
	case 76:
		// Day of the year (100 per year)
		v = party._day;
		break;
	case 77:
		// Armor class (extra beyond base)
		v = ps._ACTemp;
		break;
	case 78:
		// Test whether current Hp is equal to or exceeds the max HP
		v = ps._currentHp >= ps.getMaxHP() ? 1 : 0;
		break;
	case 79:
		// Test for Wizard Eye being active
		v = party._wizardEyeActive ? 1 : 0;
		break;
	case 81:
		// Test whether current Sp is equal to or exceeds the max SP
		v = ps._currentSp >= ps.getMaxSP() ? 1 : 0;
		break;
	case 84:
		// Current facing direction
		v = (uint)party._mazeDirection;
		break;
	case 85:
		// Current game year since start
		v = party._year;
		break;
	case 86:
	case 87:
	case 88:
	case 89:
	case 90:
	case 91:
	case 92:
		// Get a player stat
		v = ps.getStat((Attribute)(action - 86), 0);
		break;
	case 93:
		// Current day of the week (10 days per week)
		v = party._day / 10;
		break;
	case 94:
		// Test whether Walk on Water is currently active
		v = party._walkOnWaterActive ? 1 : 0;
		break;
	case 99:
		// Party skills check
		v = party.checkSkill((Skill)mask) ? mask : 0xffffffff;
		break;
	case 102:
		// Thievery skill
		v = ps.getThievery();
		break;
	case 103:
		// Get value of world flag
		v = party._worldFlags[mask] ? mask : 0xffffffff;
		break;
	case 104:
		// Get value of quest flag
		v = party._quests[mask + (_vm->_files->_isDarkCc ? 30 : 0)] ?
			mask : 0xffffffff;
		break;
	case 105:
		// Test number of Megacredits in party. Only used by King's Engineer in Castle Burlock
		v = party._questItems[26];
		break;
	case 107:
		// Get value of character flag
		error("Unused");
		break;
	default:
		break;
	}

	switch (mode) {
	case 0:
		return mask >= v;
	case 1:
		return mask == v;
	case 2:
		return mask <= v;
	default:
		return false;
	}
}

bool Scripts::copyProtectionCheck() {
	// Currentl not implemented
	return true;
}

} // End of namespace Xeen