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
|
/* 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.
*
* $URL$
* $Id$
*
*/
#include "common/array.h"
#include "common/hashmap.h"
#include "m4/converse.h"
#include "m4/resource.h"
#include "m4/globals.h"
#include "m4/m4_views.h"
#include "m4/compression.h"
namespace M4 {
#define CONV_ENTRIES_X_OFFSET 20
#define CONV_ENTRIES_Y_OFFSET 4
#define CONV_ENTRIES_HEIGHT 20
#define CONV_MAX_SHOWN_ENTRIES 5
#define CONVERSATION_ENTRY_HIGHLIGHTED 22
#define CONVERSATION_ENTRY_NORMAL 3
// Conversation chunks
// Header
#define HEAD_CONV MKID_BE('CONV') // conversation
#define CHUNK_DECL MKID_BE('DECL') // declaration
#define CHUNK_NODE MKID_BE('NODE') // node
#define CHUNK_LNOD MKID_BE('LNOD') // linear node
#define CHUNK_ETRY MKID_BE('ETRY') // entry
#define CHUNK_TEXT MKID_BE('TEXT') // text
#define CHUNK_MESG MKID_BE('MESG') // message
// Conversation chunks - entry related (unconditional)
#define CHUNK_RPLY MKID_BE('RPLY') // reply
#define CHUNK_HIDE MKID_BE('HIDE') // hide entry
#define CHUNK_UHID MKID_BE('UHID') // unhide entry
#define CHUNK_DSTR MKID_BE('DSTR') // destroy entry
// Conversation chunks - entry related (conditional)
#define CHUNK_CRPL MKID_BE('CRPL') // reply
#define CHUNK_CHDE MKID_BE('CHDE') // hide entry
#define CHUNK_CUHD MKID_BE('CUHD') // unhide entry
#define CHUNK_CDST MKID_BE('DDTS') // destroy entry
// Conversation chunks - branching and logic (unconditional)
#define CHUNK_ASGN MKID_BE('ASGN') // assign
#define CHUNK_GOTO MKID_BE('GOTO') // goto chunk
#define CHUNK_EXIT MKID_BE('EXIT') // exit/return from goto
// Conversation chunks - branching and logic (conditional)
#define CHUNK_CASN MKID_BE('CASN') // assign
#define CHUNK_CCGO MKID_BE('CCGO') // goto chunk
#define CHUNK_CEGO MKID_BE('CEGO') // exit/return from goto
// Others
#define CHUNK_FALL MKID_BE('FALL') // fallthrough
#define CHUNK_WRPL MKID_BE('WRPL') // weighted reply chunk
#define CHUNK_WPRL MKID_BE('WPRL') // weighted preply chunk
ConversationView::ConversationView(MadsM4Engine *vm): View(vm, Common::Rect(0,
vm->_screen->height() - INTERFACE_HEIGHT, vm->_screen->width(), vm->_screen->height())) {
_screenType = VIEWID_CONVERSATION;
_screenFlags.layer = LAYER_INTERFACE;
_screenFlags.visible = false;
_screenFlags.get = SCREVENT_MOUSE;
_conversationState = kNoConversation;
_currentHandle = NULL;
}
ConversationView::~ConversationView() {
_activeItems.clear();
}
void ConversationView::setNode(int32 nodeIndex) {
_highlightedIndex = -1;
_xEnd = CONV_ENTRIES_X_OFFSET;
_vm->_font->setFont(FONT_CONVERSATION);
// TODO: Conversation styles and colors
_vm->_font->current()->setColours(2, 1, 3);
_currentNodeIndex = nodeIndex;
_activeItems.clear();
if (nodeIndex != -1) {
ConvEntry *node = _m4Vm->_converse->getNode(nodeIndex);
for (uint i = 0; i < node->entries.size(); ++i) {
if (!node->entries[i]->visible)
continue;
if ((int)_activeItems.size() > CONV_MAX_SHOWN_ENTRIES) {
warning("TODO: scrolling. Max shown entries are %i, skipping entry %i",
CONV_MAX_SHOWN_ENTRIES, i);
}
// Add node to active items list
_activeItems.push_back(node->entries[i]);
if (node->entries[i]->autoSelect || strlen(node->entries[i]->text) == 0) {
//printf("Auto selecting entry %i of node %i\n", i, nodeIndex);
selectEntry(i);
return;
}
// Figure out the longest string to determine where option highlighting ends
int tempX = _vm->_font->current()->getWidth(node->entries[i]->text, 0) +
CONV_ENTRIES_X_OFFSET + 10;
_xEnd = MAX(_xEnd, tempX);
}
// Make sure that there aren't too many entries
//assert((int)_activeItems.size() < (height() - CONV_ENTRIES_Y_OFFSET) / CONV_ENTRIES_HEIGHT);
// Fallthrough
if (node->fallthroughMinEntries >= 0 && node->fallthroughOffset >= 0) {
//printf("Current node falls through node at offset %i when entries are less or equal than %i\n",
// node->fallthroughOffset, node->fallthroughMinEntries);
if (_activeItems.size() <= (uint32)node->fallthroughMinEntries) {
const EntryInfo *entryInfo = _m4Vm->_converse->getEntryInfo(node->fallthroughOffset);
//printf("Entries are less than or equal to %i, falling through to node at offset %i, index %i\n",
// node->fallthroughMinEntries, node->fallthroughOffset, entryInfo->nodeIndex);
setNode(entryInfo->nodeIndex);
return;
}
}
_entriesShown = true;
_conversationState = kConversationOptionsShown;
}
}
void ConversationView::onRefresh(RectList *rects, M4Surface *destSurface) {
//if (!this->isVisible())
// return;
clear();
if (_entriesShown) {
// Write out the conversation options
_vm->_font->setFont(FONT_CONVERSATION);
for (int i = 0; i < (int)_activeItems.size(); ++i) {
// TODO: scrolling
if (i > CONV_MAX_SHOWN_ENTRIES - 1)
break;
_vm->_font->current()->setColour((_highlightedIndex == i) ? CONVERSATION_ENTRY_HIGHLIGHTED :
CONVERSATION_ENTRY_NORMAL);
_vm->_font->current()->writeString(this, _activeItems[i]->text, CONV_ENTRIES_X_OFFSET,
CONV_ENTRIES_Y_OFFSET + CONV_ENTRIES_HEIGHT * i, 0, 0);
}
}
View::onRefresh(rects, destSurface);
}
bool ConversationView::onEvent(M4EventType eventType, int32 param, int x, int y, bool &captureEvents) {
//if (!this->isVisible())
// return false;
if (!_entriesShown)
return false;
if (eventType == KEVENT_KEY)
return false;
int localY = y - _coords.top;
int selectedIndex = _highlightedIndex;
switch (eventType) {
case MEVENT_MOVE:
if ((x < CONV_ENTRIES_X_OFFSET) || (x >= _xEnd) || (localY < CONV_ENTRIES_Y_OFFSET))
_highlightedIndex = -1;
else {
int index = (localY - CONV_ENTRIES_Y_OFFSET) / CONV_ENTRIES_HEIGHT;
_highlightedIndex = (index >= (int)_activeItems.size()) ? -1 : index;
}
break;
case MEVENT_LEFT_RELEASE:
if (_highlightedIndex != -1) {
selectEntry(selectedIndex);
}
break;
default:
break;
}
return true;
}
void ConversationView::selectEntry(int entryIndex) {
char buffer[20];
sprintf(buffer, "%s.raw", _activeItems[entryIndex]->voiceFile);
_entriesShown = false;
_conversationState = kEntryIsActive;
_vm->_player->setCommandsAllowed(false);
// Necessary, as entries can be selected programmatically
_highlightedIndex = entryIndex;
// Play the selected entry's voice
if (strlen(_activeItems[entryIndex]->voiceFile) > 0) {
_currentHandle = _vm->_sound->getHandle();
_vm->_sound->playVoice(buffer, 255);
} else {
_currentHandle = NULL;
}
// Hide selected entry, unless it has a persistent flag set
if (!(_activeItems[entryIndex]->flags & kEntryPersists)) {
//printf("Hiding selected entry\n");
_m4Vm->_converse->getNode(_currentNodeIndex)->entries[entryIndex]->visible = false;
} else {
//printf("Selected entry is persistent, not hiding it\n");
}
}
void ConversationView::updateState() {
switch (_conversationState) {
case kConversationOptionsShown:
return;
case kEntryIsActive:
case kReplyIsActive:
// FIXME: for now, we determine whether a conversation entry is
// finished by waiting for its associated speech file to finish playing
if (_currentHandle != NULL && _vm->_sound->isHandleActive(_currentHandle)) {
return;
} else {
playNextReply();
} // end else
break;
case kNoConversation:
return;
default:
error("Unknown converstation state");
break;
}
}
void ConversationView::playNextReply() {
char buffer[20];
assert(_highlightedIndex >= 0);
// Start playing the first reply
for (uint32 i = 0; i < _activeItems[_highlightedIndex]->entries.size(); i++) {
ConvEntry *currentEntry = _activeItems[_highlightedIndex]->entries[i];
if (currentEntry->isConditional) {
if (!_m4Vm->_converse->evaluateCondition(
_m4Vm->_converse->getValue(currentEntry->condition.offset),
currentEntry->condition.op, currentEntry->condition.val))
continue; // don't play this reply
}
if (currentEntry->entryType != kWeightedReply) {
sprintf(buffer, "%s.raw", currentEntry->voiceFile);
if (strlen(currentEntry->voiceFile) > 0) {
_currentHandle = _vm->_sound->getHandle();
_vm->_sound->playVoice(buffer, 255);
// Remove reply from the list of replies
_activeItems[_highlightedIndex]->entries.remove_at(i);
_conversationState = kReplyIsActive;
return;
} else {
_currentHandle = NULL;
}
} else {
int selectedWeight = _vm->_random->getRandomNumber(currentEntry->totalWeight - 1) + 1;
//printf("Selected weight: %i\n", selectedWeight);
int previousWeight = 1;
int currentWeight = 0;
for (uint32 j = 0; j < currentEntry->entries.size(); j++) {
currentWeight += currentEntry->entries[j]->weight;
if (selectedWeight >= previousWeight && selectedWeight <= currentWeight) {
sprintf(buffer, "%s.raw", currentEntry->entries[j]->voiceFile);
if (strlen(currentEntry->entries[j]->voiceFile) > 0) {
_currentHandle = _vm->_sound->getHandle();
_vm->_sound->playVoice(buffer, 255);
// Remove reply from the list of replies
_activeItems[_highlightedIndex]->entries.remove_at(i);
_conversationState = kReplyIsActive;
return;
} else {
_currentHandle = NULL;
}
break;
}
previousWeight += currentWeight;
} // end for j
} // end if
} // end for i
// If we reached here, there are no more replies, so perform the active entry's actions
//printf("Current selection does %i actions\n", _activeItems[entryIndex]->actions.size());
for (uint32 i = 0; i < _activeItems[_highlightedIndex]->actions.size(); i++) {
if (!_m4Vm->_converse->performAction(_activeItems[_highlightedIndex]->actions[i]))
break;
} // end for
// Refresh the conversation node, in case it hasn't changed
setNode(_currentNodeIndex);
_entriesShown = true;
_vm->_player->setCommandsAllowed(true);
// Check if the conversation has been ended
if (_currentNodeIndex == -1) {
_conversationState = kNoConversation;
}
}
//--------------------------------------------------------------------------
void Converse::startConversation(const char *convName, bool showConverseBox, ConverseStyle style) {
if (_vm->isM4())
loadConversation(convName);
else
loadConversationMads(convName);
if (!_vm->isM4()) showConverseBox = false; // TODO: remove
_playerCommandsAllowed = _vm->_player->commandsAllowed;
if (_vm->isM4()) // TODO: remove (interface not implemented yet in MADS games)
_interfaceWasVisible = _m4Vm->scene()->getInterface()->isVisible();
_vm->_player->setCommandsAllowed(false);
_style = style;
if (showConverseBox) {
_vm->_conversationView->show();
_vm->_mouse->lockCursor(CURSOR_ARROW);
if (_interfaceWasVisible)
_m4Vm->scene()->getInterface()->hide();
_vm->_conversationView->setNode(0);
_vm->_conversationView->show();
}
}
void Converse::endConversation() {
_vm->_conversationView->setNode(-1);
_vm->_conversationView->hide();
// TODO: do a more proper cleanup here
_convNodes.clear();
_variables.clear();
_offsetMap.clear();
_vm->_player->setCommandsAllowed(_playerCommandsAllowed);
if (_interfaceWasVisible)
_m4Vm->scene()->getInterface()->show();
}
void Converse::loadConversation(const char *convName) {
char name[40];
char buffer[256];
sprintf(name, "%s.chk", convName);
Common::SeekableReadStream *convS = _vm->res()->get(name);
uint32 header = convS->readUint32LE();
uint32 size;
uint32 chunk;
uint32 data = 0;
uint32 i = 0;
ConvEntry* curEntry = NULL;
ConvEntry* replyEntry = NULL;
int32 currentWeightedEntry = -1;
EntryAction* curAction = NULL;
uint32 curNode = 0;
uint32 chunkPos = 0;
uint32 val;
int32 autoSelectIndex = -1;
int returnAddress = -1;
bool debugFlag = false; // set to true for debug messages
// Conversation *.chk files contain a 'CONV' header in LE format
if (header != HEAD_CONV) {
warning("Unexpected conversation file external header");
return;
}
size = convS->readUint32LE(); // is this used at all?
if (debugFlag) printf("Conv chunk size (external header): %i\n", size);
// Conversation name, which is the conversation file's name
// without the extension
convS->read(buffer, 8);
if (debugFlag) printf("Conversation name: %s\n", buffer);
while (true) {
chunkPos = convS->pos();
chunk = convS->readUint32LE(); // read chunk
if (convS->eos()) break;
if (debugFlag) printf("***** Pos: %i -> ", chunkPos);
switch (chunk) {
case CHUNK_DECL: // Declare
if (debugFlag) printf("DECL chunk\n");
data = convS->readUint32LE();
if (debugFlag) printf("Tag: %i\n", data);
if (data > 0)
warning("Tag > 0 in DECL chunk, value is: %i", data); // TODO
val = convS->readUint32LE();
if (debugFlag) printf("Value: %i\n", val);
data = convS->readUint32LE();
if (debugFlag) printf("Flags: %i\n", data);
if (data > 0)
warning("Flags != 0 in DECL chunk, value is %i", data); // TODO
setValue(chunkPos, val);
break;
// ----------------------------------------------------------------------------
case CHUNK_NODE: // Node
case CHUNK_LNOD: // Linear node
// Create new node
curEntry = new ConvEntry();
curEntry->offset = chunkPos;
curEntry->entryType = (chunk == CHUNK_NODE) ? kNode : kLinearNode;
curEntry->fallthroughMinEntries = -1;
curEntry->fallthroughOffset = -1;
if (chunk == CHUNK_NODE) {
if (debugFlag) printf("NODE chunk\n");
} else {
if (debugFlag) printf("LNOD chunk\n");
}
curNode = convS->readUint32LE();
if (debugFlag) printf("Node number: %i\n", curNode);
data = convS->readUint32LE();
if (debugFlag) printf("Tag: %i\n", data);
if (chunk == CHUNK_LNOD) {
autoSelectIndex = convS->readUint32LE();
if (debugFlag) printf("Autoselect entry number: %i\n", autoSelectIndex);
}
size = convS->readUint32LE();
if (debugFlag) printf("Number of entries: %i\n", size);
for (i = 0; i < size; i++) {
data = convS->readUint32LE();
if (debugFlag) printf("Entry %i: %i\n", i + 1, data);
}
_convNodes.push_back(curEntry);
setEntryInfo(curEntry->offset, curEntry->entryType, curNode, -1);
break;
case CHUNK_ETRY: // Entry
// Create new entry
curEntry = new ConvEntry();
curEntry->offset = chunkPos;
curEntry->entryType = kEntry;
strcpy(curEntry->voiceFile, "");
strcpy(curEntry->text, "");
if (debugFlag) printf("ETRY chunk\n");
data = convS->readUint32LE();
if (debugFlag) printf("Unknown (attributes perhaps?): %i\n", data);
data = convS->readUint32LE();
if (debugFlag) printf("Entry flags: ");
if (debugFlag) if (data & kEntryInitial) printf ("Initial ");
if (debugFlag) if (data & kEntryPersists) printf ("Persists ");
if (debugFlag) printf("\n");
curEntry->flags = data;
curEntry->visible = (curEntry->flags & kEntryInitial) ? true : false;
if (autoSelectIndex >= 0) {
if (_convNodes[curNode]->entries.size() == (uint32)autoSelectIndex) {
curEntry->autoSelect = true;
autoSelectIndex = -1;
} else {
curEntry->autoSelect = false;
}
} else {
curEntry->autoSelect = false;
}
_convNodes[curNode]->entries.push_back(curEntry);
setEntryInfo(curEntry->offset, curEntry->entryType,
curNode, _convNodes[curNode]->entries.size() - 1);
replyEntry = NULL;
break;
case CHUNK_WPRL: // Weighted preply
// WPRL chunks are random entries that the character would say, not an NPC
// They don't seem to be used in Orion Burger
warning("WPRL chunk - treating as WRPL chunk");
case CHUNK_WRPL: // Weighted reply
case CHUNK_CRPL: // Conditional reply
case CHUNK_RPLY: // Reply
{
ConvEntry* weightedEntry = NULL;
// Create new reply
replyEntry = new ConvEntry();
replyEntry->offset = chunkPos;
strcpy(replyEntry->voiceFile, "");
// Conditional part
if (chunk == CHUNK_CRPL) {
replyEntry->isConditional = true;
replyEntry->condition.offset = convS->readUint32LE();
replyEntry->condition.op = convS->readUint32LE();
replyEntry->condition.val = convS->readUint32LE();
} else {
replyEntry->isConditional = false;
}
if (chunk == CHUNK_WPRL || chunk == CHUNK_WRPL) {
replyEntry->entryType = kWeightedReply;
replyEntry->totalWeight = 0;
if (debugFlag) printf("WRPL chunk\n");
size = convS->readUint32LE();
if (debugFlag) printf("Weighted reply %i - %i entries:\n", i, size);
for (i = 0; i < size; i++) {
weightedEntry = new ConvEntry();
weightedEntry->offset = chunkPos;
strcpy(weightedEntry->voiceFile, "");
weightedEntry->entryType = kWeightedReply;
data = convS->readUint32LE();
if (debugFlag) printf("- Weight: %i ", data);
weightedEntry->weight = data;
replyEntry->totalWeight += weightedEntry->weight;
data = convS->readUint32LE();
if (debugFlag) printf("offset: %i\n", data);
replyEntry->entries.push_back(weightedEntry);
}
currentWeightedEntry = 0;
} else {
replyEntry->entryType = kReply;
if (debugFlag) printf("RPLY chunk\n");
data = convS->readUint32LE();
if (debugFlag) printf("Reply data offset: %i\n", data);
}
if (!curEntry)
error("Converse::loadConversation(): curEntry is NULL while adding a reply");
curEntry->entries.push_back(replyEntry);
setEntryInfo(replyEntry->offset, replyEntry->entryType,
curNode, _convNodes[curNode]->entries.size() - 1);
// Seek to chunk data (i.e. TEXT/MESG tag, which is usually right
// after this chunk but it can be further on in conditional reply chunks
assert((int)data >= convS->pos());
// If the entry's data is not right after the entry, remember the position
// to return to after the data is read
if (chunk == CHUNK_CRPL && (int)data != convS->pos())
returnAddress = convS->pos();
convS->seek(data, SEEK_SET);
}
break;
// ----------------------------------------------------------------------------
case CHUNK_TEXT: // Text (contains text and voice)
case CHUNK_MESG: // Message (contains voice only)
{
ConvEntry* parentEntry = NULL;
if (chunk == CHUNK_TEXT) {
if (debugFlag) printf("TEXT chunk\n");
} else {
if (debugFlag) printf("MESG chunk\n");
}
if (replyEntry == NULL) {
parentEntry = curEntry;
} else if (replyEntry != NULL && replyEntry->entryType != kWeightedReply) {
parentEntry = replyEntry;
} else if (replyEntry != NULL && replyEntry->entryType == kWeightedReply) {
parentEntry = replyEntry->entries[currentWeightedEntry];
currentWeightedEntry++;
} else {
error("Converse::loadConversation(): Unexpected reply entry while processing TEXT/MESG chunk");
}
size = convS->readUint32LE();
if (debugFlag) printf("Entry data size: %i\n", size);
convS->read(buffer, 8);
size -= 8; // subtract maximum length of voice file name
// If the file name is 8 characters, it will not be 0-terminated, so use strncpy
strncpy(parentEntry->voiceFile, buffer, 8);
parentEntry->voiceFile[8] = '\0';
if (debugFlag) printf("Voice file: %s\n", parentEntry->voiceFile);
if (chunk == CHUNK_TEXT) {
convS->read(buffer, size);
if (debugFlag) printf("Text: %s\n", buffer);
sprintf(parentEntry->text, "%s", buffer);
} else {
while (size > 0) {
data = convS->readUint32LE();
if (debugFlag) printf("Unknown: %i\n", data); // TODO
size -= 4;
}
}
// Now that the data chunk has been read, if we skipped a reply node,
// jump back to it
if (returnAddress != -1) {
convS->seek(returnAddress, SEEK_SET);
returnAddress = -1;
}
}
break;
// ----------------------------------------------------------------------------
// Entry action chunks
case CHUNK_CASN: // Conditional assign
case CHUNK_ASGN: // Assign
curAction = new EntryAction();
if (debugFlag) printf("ASGN chunk\n");
curAction->actionType = kAssignValue;
// Conditional part
if (chunk == CHUNK_CASN) {
curAction->isConditional = true;
curAction->condition.offset = convS->readUint32LE();
curAction->condition.op = convS->readUint32LE();
curAction->condition.val = convS->readUint32LE();
} else {
curAction->isConditional = false;
}
curAction->targetOffset = convS->readUint32LE();
assert(convS->readUint32LE() == kOpAssign);
curAction->value = convS->readUint32LE();
break;
case CHUNK_CCGO: // Conditional go to entry
case CHUNK_CHDE: // Conditional hide entry
case CHUNK_CUHD: // Conditional unhide entry
case CHUNK_CDST: // Conditional destroy entry
case CHUNK_CEGO: // Conditional exit conversation / go to
case CHUNK_GOTO: // Go to entry
case CHUNK_HIDE: // Hide entry
case CHUNK_UHID: // Unhide entry
case CHUNK_DSTR: // Destroy entry
case CHUNK_EXIT: // Exit conversation
curAction = new EntryAction();
// Conditional part
if (chunk == CHUNK_CCGO || chunk == CHUNK_CHDE || chunk == CHUNK_CUHD ||
chunk == CHUNK_CDST || chunk == CHUNK_CEGO) {
curAction->isConditional = true;
curAction->condition.offset = convS->readUint32LE();
curAction->condition.op = convS->readUint32LE();
curAction->condition.val = convS->readUint32LE();
} else {
curAction->isConditional = false;
}
if (chunk == CHUNK_GOTO || chunk == CHUNK_CCGO) {
curAction->actionType = kGotoEntry;
if (debugFlag) printf("GOTO chunk\n");
} else if (chunk == CHUNK_HIDE || chunk == CHUNK_CHDE) {
curAction->actionType = kHideEntry;
if (debugFlag) printf("HIDE chunk\n");
} else if (chunk == CHUNK_UHID || chunk == CHUNK_CUHD) {
curAction->actionType = kUnhideEntry;
if (debugFlag) printf("UHID chunk\n");
} else if (chunk == CHUNK_DSTR || chunk == CHUNK_CDST) {
curAction->actionType = kDestroyEntry;
if (debugFlag) printf("DSTR chunk\n");
} else if (chunk == CHUNK_EXIT || chunk == CHUNK_CEGO) {
curAction->actionType = kExitConv;
if (debugFlag) printf("EXIT chunk\n");
}
data = convS->readUint32LE();
if (debugFlag) printf("Offset: %i\n", data);
curAction->targetOffset = data;
curEntry->actions.push_back(curAction);
break;
case CHUNK_FALL: // Fallthrough
if (debugFlag) printf("FALL chunk\n");
size = convS->readUint32LE();
if (debugFlag) printf("Minimum nodes: %i\n", size);
_convNodes[curNode]->fallthroughMinEntries = size;
data = convS->readUint32LE();
if (debugFlag) printf("Offset: %i\n", data);
_convNodes[curNode]->fallthroughOffset = data;
break;
// ----------------------------------------------------------------------------
default:
// Should never happen
error("Unknown conversation chunk");
break;
}
}
_vm->res()->toss(name);
}
void Converse::loadConversationMads(const char *convName) {
char name[40];
char buffer[256];
char *buf;
Common::SeekableReadStream *convS;
int curPos = 0;
int unk = 0;
uint32 stringIndex = 0;
uint32 stringCount = 0;
int flags = 0;
int count = 0;
uint32 i, j;
ConvEntry* curEntry = NULL;
MessageEntry *curMessage;
Common::Array<char *> messageList;
// TODO
// CND file
sprintf(name, "%s.cnd", convName);
MadsPack convDataD(name, _vm);
// ------------------------------------------------------------
// Chunk 0
convS = convDataD.getItemStream(0);
printf("Chunk 0\n");
printf("Conv stream size: %i\n", convS->size());
while (!convS->eos()) { // FIXME (eos changed)
printf("%i ", convS->readByte());
}
printf("\n");
// ------------------------------------------------------------
// Chunk 1
convS = convDataD.getItemStream(1);
printf("Chunk 1\n");
printf("Conv stream size: %i\n", convS->size());
while (!convS->eos()) { // FIXME (eos changed)
printf("%i ", convS->readByte());
}
printf("\n");
// ------------------------------------------------------------
// Chunk 2
convS = convDataD.getItemStream(2);
printf("Chunk 2\n");
printf("Conv stream size: %i\n", convS->size());
while (!convS->eos()) { // FIXME (eos changed)
printf("%i ", convS->readByte());
}
printf("\n");
// ------------------------------------------------------------
// CNV file
sprintf(name, "%s.cnv", convName);
MadsPack convData(name, _vm);
// *.cnv files have 7 chunks
// Here is the chunk output of conv001.cnv (from the dump_file command)
/*
Dumping conv001.cnv, size: 3431
Dumping compressed chunk 1 of 7, size is 150
Dumping compressed chunk 2 of 7, size is 130
Dumping compressed chunk 3 of 7, size is 224
Dumping compressed chunk 4 of 7, size is 92
Dumping compressed chunk 5 of 7, size is 168
Dumping compressed chunk 6 of 7, size is 4064
Dumping compressed chunk 7 of 7, size is 2334
*/
// ------------------------------------------------------------
// TODO: finish this
// Chunk 0
convS = convData.getItemStream(0);
printf("Chunk 0\n");
printf("Conv stream size: %i\n", convS->size());
printf("%i ", convS->readUint16LE());
printf("%i ", convS->readUint16LE());
printf("%i ", convS->readUint16LE());
printf("%i ", convS->readUint16LE());
printf("%i ", convS->readUint16LE());
printf("%i ", convS->readUint16LE());
printf("\n");
count = convS->readUint16LE(); // conversation face count (usually 2)
printf("Conversation faces: %i\n", count);
for (i = 0; i < 5; i++) {
convS->read(buffer, 16);
printf("Face %i: %s ", i + 1, buffer);
}
printf("\n");
// 5 face slots
// 1 = face slot has a face (with the filename specified above)
// 0 = face slot doesn't contain face data
for (i = 0; i < 5; i++) {
printf("%i ", convS->readUint16LE());
}
printf("\n");
convS->read(buffer, 14); // speech file
printf("Speech file: %s\n", buffer);
while (!convS->eos()) { // FIXME: eos changed
printf("%i ", convS->readByte());
}
printf("\n");
delete convS;
// ------------------------------------------------------------
// Chunk 1: Conversation nodes
convS = convData.getItemStream(1);
printf("Chunk 1: conversation nodes\n");
printf("Conv stream size: %i\n", convS->size());
while (true) {
uint16 id = convS->readUint16LE();
if (convS->eos()) break;
curEntry = new ConvEntry();
curEntry->id = id;
curEntry->entryCount = convS->readUint16LE();
curEntry->flags = convS->readUint16LE();
if (curEntry->entryCount == 1 && curEntry->flags != 65535) {
warning("Entry count is 1 and flags is not 65535 (it's %i)", flags);
} else if (curEntry->entryCount != 1 && flags != 0) {
warning("Entry count > 1 and flags is not 0 (it's %i)", flags);
}
unk = convS->readUint16LE();
assert (unk == 65535);
unk = convS->readUint16LE();
assert (unk == 65535);
_convNodes.push_back(curEntry);
printf("Node %i, ID %i, entries %i\n", _convNodes.size(), curEntry->id, curEntry->entryCount);
// flags = 0: node has more than 1 entry
// flags = 65535: node has 1 entry
}
printf("Conversation has %i nodes\n", _convNodes.size());
printf("\n");
delete convS;
// ------------------------------------------------------------
// Chunk 4 contains the conversation string offsets of chunk 5
// (unused, as it's unneeded - we find the offsets ourselves)
// ------------------------------------------------------------
// Chunk 5 contains the conversation strings
convS = convData.getItemStream(5);
//printf("Chunk 5: conversation strings\n");
//printf("Conv stream size: %i\n", convS->size());
*buffer = 0;
while (true) {
//if (curPos == 0)
// printf("%i: Offset %i: ", _convStrings.size(), convS->pos());
uint8 b = convS->readByte();
if (convS->eos()) break;
buffer[curPos++] = b;
if (buffer[curPos - 1] == '~') { // filter out special characters
curPos--;
continue;
}
if (buffer[curPos - 1] == '\0') {
// end of string
//printf("%s\n", buffer);
buf = new char[strlen(buffer) + 1];
strcpy(buf, buffer);
_convStrings.push_back(buf);
curPos = 0;
*buffer = 0;
}
}
delete convS;
// ------------------------------------------------------------
// Chunk 2: entry data
convS = convData.getItemStream(2);
//printf("Chunk 2 - entry data\n");
//printf("Conv stream size: %i\n", convS->size());
for (i = 0; i < _convNodes.size(); i++) {
for (j = 0; j < _convNodes[i]->entryCount; j++) {
curEntry = new ConvEntry();
stringIndex = convS->readUint16LE();
if (stringIndex != 65535)
sprintf(curEntry->text, "%s", _convStrings[stringIndex]);
else
*curEntry->text = 0;
curEntry->id = convS->readUint16LE();
curEntry->offset = convS->readUint16LE();
curEntry->size = convS->readUint16LE();
_convNodes[i]->entries.push_back(curEntry);
//printf("Node %i, entry %i, id %i, offset %i, size %i, text: %s\n",
// i, j, curEntry->id, curEntry->offset, curEntry->size, curEntry->text);
}
}
delete convS;
// ------------------------------------------------------------
// Chunk 3: message (MESG) chunks, created from the strings of chunk 5
convS = convData.getItemStream(3);
//printf("Chunk 3 - MESG chunk data\n");
//printf("Conv stream size: %i\n", convS->size());
while (true) {
uint16 index = convS->readUint16LE();
if (convS->eos()) break;
curMessage = new MessageEntry();
stringIndex = index;
stringCount = convS->readUint16LE();
*buffer = 0;
//printf("Message: %i\n", _madsMessageList.size());
for (i = stringIndex; i < stringIndex + stringCount; i++) {
//printf("%i: %s\n", i, _convStrings[i]);
curMessage->messageStrings.push_back(_convStrings[i]);
}
_madsMessageList.push_back(curMessage);
//printf("----------\n");
}
//printf("\n");
delete convS;
// ------------------------------------------------------------
// TODO: finish this
// Chunk 6: conversation script
convS = convData.getItemStream(6);
printf("Chunk 6\n");
printf("Conv stream size: %i\n", convS->size());
/*while (!convS->eos()) { // FIXME (eos changed)
printf("%i ", convS->readByte());
printf("%i ", convS->readByte());
printf("%i ", convS->readByte());
printf("%i ", convS->readByte());
printf("%i ", convS->readByte());
printf("%i ", convS->readByte());
printf("%i ", convS->readByte());
printf("%i ", convS->readByte());
printf("%i ", convS->readByte());
printf("%i ", convS->readByte());
printf("\n");
}
return;*/
for (i = 0; i < _convNodes.size(); i++) {
for (j = 0; j < _convNodes[i]->entryCount; j++) {
printf("*** Node %i entry %i data size %i\n", i, j, _convNodes[i]->entries[j]->size);
printf("Entry ID %i, text %s\n", _convNodes[i]->entries[j]->id, _convNodes[i]->entries[j]->text);
Common::SubReadStream *entryStream = new Common::SubReadStream(convS, _convNodes[i]->entries[j]->size);
readConvEntryActions(entryStream, _convNodes[i]->entries[j]);
delete entryStream;
printf("--------------------\n");
}
}
delete convS;
}
void Converse::readConvEntryActions(Common::SubReadStream *convS, ConvEntry *curEntry) {
uint8 chunk;
uint8 type; // 255: normal, 11: conditional
uint8 hasText1, hasText2;
int target;
int count = 0;
int var, val;
int messageIndex = 0;
int unk = 0;
while (true) {
chunk = convS->readByte();
if (convS->eos()) break;
type = convS->readByte();
switch (chunk) {
case 1:
printf("TODO: chunk type %i\n", chunk);
break;
case 2:
printf("HIDE\n");
convS->readByte();
count = convS->readByte();
printf("%i entries: ", count);
for (int i = 0; i < count; i++)
printf("%i %d", i, convS->readUint16LE());
printf("\n");
break;
case 3:
printf("UNHIDE\n");
convS->readByte();
count = convS->readByte();
printf("%i entries: ", count);
for (int i = 0; i < count; i++)
printf("%i %d", i, convS->readUint16LE());
printf("\n");
break;
case 4: // MESSAGE
printf("MESSAGE\n");
if (type == 255) {
//printf("unconditional\n");
} else if (type == 11) {
//printf("conditional\n");
} else {
printf("unknown type: %i\n", type);
}
// Conditional part
if (type == 11) {
unk = convS->readUint16LE(); // 1
if (unk != 1)
printf("Message: unk != 1 (it's %i)\n", unk);
var = convS->readUint16LE();
val = convS->readUint16LE();
printf("Var %i == %i\n", var, val);
}
unk = convS->readUint16LE(); // 256
if (unk != 256)
printf("Message: unk != 256 (it's %i)\n", unk);
// it seems that the first text entry is set when the message
// chunk is supposed to be shown unconditionally, whereas the second text
// entry is set when the message is supposed to be shown conditionally
hasText1 = convS->readByte();
hasText2 = convS->readByte();
if (hasText1 == 1) {
messageIndex = convS->readUint16LE();
printf("Message 1 index: %i, text:\n", messageIndex);
for (uint32 i = 0; i < _madsMessageList[messageIndex]->messageStrings.size(); i++) {
printf("%s\n", _madsMessageList[messageIndex]->messageStrings[i]);
}
}
if (hasText2 == 1) {
messageIndex = convS->readUint16LE();
if (hasText1 == 0) {
printf("Message 2 index: %i, text:\n", messageIndex);
for (uint32 i = 0; i < _madsMessageList[messageIndex]->messageStrings.size(); i++) {
printf("%s\n", _madsMessageList[messageIndex]->messageStrings[i]);
}
}
}
break;
case 5: // AUTO
printf("AUTO\n");
for (int k = 0; k < 4; k++)
convS->readByte();
messageIndex = convS->readUint16LE();
printf("Message index: %i, text:\n", messageIndex);
for (uint32 i = 0; i < _madsMessageList[messageIndex]->messageStrings.size(); i++) {
printf("%s\n", _madsMessageList[messageIndex]->messageStrings[i]);
}
convS->readUint16LE();
break;
case 6:
printf("TODO: chunk type %i\n", chunk);
break;
case 7: // GOTO
unk = convS->readUint32LE(); // 0
if (unk != 0 && unk != 1)
printf("Goto: unk != 0 or 1 (it's %i)\n", unk);
target = convS->readUint16LE();
convS->readUint16LE(); // 255
if (unk != 0)
printf("Goto: unk != 0 (it's %i)\n", unk);
if (target != 65535)
printf("GOTO node %i\n", target);
else
printf("GOTO exit\n");
break;
case 8:
printf("TODO: chunk type %i\n", chunk);
break;
case 9: // ASSIGN
//printf("ASSIGN\n");
unk = convS->readUint32LE(); // 0
if (unk != 0)
printf("Assign: unk != 0 (it's %i)\n", unk);
val = convS->readUint16LE();
var = convS->readUint16LE();
//printf("Var %i = %i\n", var, val);
break;
default:
printf ("Unknown chunk type! (%i)\n", chunk);
break;
}
}
printf("\n");
}
void Converse::setEntryInfo(int32 offset, EntryType type, int32 nodeIndex, int32 entryIndex) {
char hashOffset[10];
sprintf(hashOffset, "%i", offset);
EntryInfo info;
info.targetType = type;
info.nodeIndex = nodeIndex;
info.entryIndex = entryIndex;
_offsetMap[hashOffset] = info;
//printf("Set entry info: offset %i, type %i, node %i, entry %i\n", offset, type, nodeIndex, entryIndex);
}
const EntryInfo* Converse::getEntryInfo(int32 offset) {
char hashOffset[10];
sprintf(hashOffset, "%i", offset);
OffsetHashMap::const_iterator entry = _offsetMap.find(hashOffset);
if (entry != _offsetMap.end())
return &(entry->_value);
else
error("Undeclared entry offset: %i", offset);
}
void Converse::setValue(int32 offset, int32 value) {
char hashOffset[10];
sprintf(hashOffset, "%i", offset);
_variables[hashOffset] = value;
}
int32 Converse::getValue(int32 offset) {
char hashOffset[10];
sprintf(hashOffset, "%i", offset);
ConvVarHashMap::const_iterator entry = _variables.find(hashOffset);
if (entry != _variables.end())
return entry->_value;
else
error("Undeclared variable offset: %i", offset);
}
bool Converse::evaluateCondition(int32 leftVal, int32 op, int32 rightVal) {
switch (op) {
case kOpPercent:
return (leftVal % rightVal == 0);
case kOpGreaterOrEqual:
return leftVal >= rightVal;
case kOpLessOrEqual:
return leftVal <= rightVal;
case kOpGreaterThan:
return leftVal > rightVal;
case kOpLessThan:
return leftVal < rightVal;
case kOpNotEqual:
case kOpCondNotEqual:
return leftVal != rightVal;
case kOpAssign:
return leftVal == rightVal;
case kOpAnd:
return leftVal && rightVal;
case kOpOr:
return leftVal || rightVal;
default:
error("Unknown conditional operator: %i", op);
}
}
bool Converse::performAction(EntryAction *action) {
if (action->isConditional) {
if (!evaluateCondition(getValue(action->condition.offset),
action->condition.op, action->condition.val))
return true; // don't perform this action
}
if (action->actionType == kAssignValue) {
//printf("Assigning variable at offset %i to value %i\n",
// action->targetOffset, action->value);
setValue(action->targetOffset, action->value);
return true; // nothing else to do in an assignment action
}
const EntryInfo *entryInfo = getEntryInfo(action->targetOffset);
ConvEntry *targetEntry;
if (entryInfo->nodeIndex >= 0 && entryInfo->entryIndex >= 0)
targetEntry = getNode(entryInfo->nodeIndex)->entries[entryInfo->entryIndex];
else if (entryInfo->nodeIndex >= 0)
targetEntry = getNode(entryInfo->nodeIndex);
else
error("Target node id is negative");
switch (action->actionType) {
case kGotoEntry:
//printf("Goto entry at offset %i. Associated node is %i, entry %i\n",
// action->targetOffset, entryInfo->nodeIndex, entryInfo->entryIndex);
_vm->_conversationView->setNode(entryInfo->nodeIndex);
if (entryInfo->entryIndex >= 0)
_vm->_conversationView->selectEntry(entryInfo->entryIndex);
return false;
case kHideEntry:
//printf("Hide entry at offset %i. Associated node is %i, entry %i\n",
// targetEntry->offset, entryInfo->nodeIndex, entryInfo->entryIndex);
targetEntry->visible = false;
return true;
case kUnhideEntry:
//printf("Show entry at offset %i. Associated node is %i, entry %i\n",
// targetEntry->offset, entryInfo->nodeIndex, entryInfo->entryIndex);
targetEntry->visible = true;
return true;
case kDestroyEntry:
//printf("Destroy entry at offset %i. Associated node is %i, entry %i\n",
// targetEntry->offset, entryInfo->nodeIndex, entryInfo->entryIndex);
if (entryInfo->entryIndex >= 0)
getNode(entryInfo->nodeIndex)->entries.remove_at(entryInfo->entryIndex);
else
warning("Target entry is a node, not destroying it");
targetEntry->visible = true;
return true;
case kExitConv:
//printf("Exit conversation\n");
endConversation();
return false;
default:
warning("Unknown entry action");
return false;
} // end switch
}
/*--------------------------------------------------------------------------*/
MadsConversation::MadsConversation() {
for (int i = 0; i < MADS_TALK_SIZE; ++i) {
_talkList[i].desc = NULL;
_talkList[i].id = 0;
}
}
} // End of namespace M4
|