-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.cpp
1258 lines (1016 loc) · 27.8 KB
/
database.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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common/debug.h"
#include "common/file.h"
#include "common/list.h"
#include "common/util.h"
#include "common/endian.h"
#include "common/textconsole.h"
#include "kom/kom.h"
#include "kom/actor.h"
#include "kom/database.h"
#include "kom/character.h"
#include "kom/game.h"
using Common::File;
namespace Kom {
const int Database::_locRoutesSize = 111;
Database::Database(KomEngine *vm)
: _vm(vm) {
_locations = 0;
_characters = 0;
_objects = 0;
_routes = 0;
_map = 0;
_locRoutes = 0;
_convData = 0;
}
Database::~Database() {
delete[] _locations;
delete[] _characters;
delete[] _objects;
free(_variables);
delete[] _processes;
delete[] _convIndex;
delete[] _narrIndex;
_narrData.close();
_convData->close();
delete _convData;
delete[] _routes;
delete[] _map;
delete[] _locRoutes;
}
void Database::init(Common::String databasePrefix) {
_databasePrefix = databasePrefix;
if (_databasePrefix == "thid")
_pathPrefix = "install/db0/";
else
_pathPrefix = "install/db1/";
_convData = new File();
_convData->open(_pathPrefix + "conv.bin");
loadConvIndex();
loadNarratorIndex();
initLocations();
initCharacters();
initObjects();
initEvents();
initObjectLocs();
initCharacterLocs();
initProcs();
initRoutes();
for (int i = 0; i < _procsNum; ++i) {
for (Common::List<Command>::iterator j = _processes[i].commands.begin();
j != _processes[i].commands.end(); ++j) {
if (j->cmd == 312) { // Init
debug(1, "Processing init in %s", _processes[i].name);
_vm->game()->doStat(&(*j));
}
}
}
}
void Database::loadConvIndex() {
File f;
f.open(_pathPrefix + "conv.idx");
_convIndexLen = f.readUint32LE();
_convIndex = new byte[_convIndexLen * 24];
f.read(_convIndex, _convIndexLen * 24);
f.close();
}
void Database::loadNarratorIndex() {
File f;
f.open("kom/conv/narr.idx");
_narrIndexSize = f.size();
_narrIndex = new byte[_narrIndexSize];
f.read(_narrIndex, _narrIndexSize);
f.close();
_narrData.open("kom/conv/narr.bin");
}
byte *Database::getConvIndex(const char *entry) {
int entrySize = strlen(entry);
for (uint i = 0; i < _convIndexLen; i++)
if (strncmp(entry, (const char *)_convIndex + i * 24, entrySize) == 0)
return _convIndex + i * 24;
return 0;
}
/**
* Allocates a new string to use.
* Caller should delete it
*/
char *Database::getNarratorText(const char *entry) {
int entrySize = strlen(entry);
char *result;
for (int i = 0; i < _narrIndexSize; i+=16) {
if (strncmp(entry, (const char *)_narrIndex + i, entrySize) == 0) {
uint32 offset = READ_LE_UINT32(_narrIndex + i + 8);
uint32 size = READ_LE_UINT32(_narrIndex + i + 12);
result = new char[size + 1];
_narrData.seek(offset);
_narrData.read(result, size);
result[size] = '\0';
return result;
}
}
return 0;
}
void Database::initLocations() {
File f;
Common::String line;
f.open(_pathPrefix + _databasePrefix + ".loc");
// Get number of entries in file
line = f.readLine();
sscanf(line.c_str(), "%d", &_locationsNum);
_locations = new Location[_locationsNum];
for (int i = 0; i < _locationsNum; ++i) {
int index;
// Skip empty line
f.readLine();
line = f.readLine();
sscanf(line.c_str(), "%d", &index);
sscanf(line.c_str(), "%*d %s %d %d",
_locations[index].name,
&(_locations[index].xtend),
&(_locations[index].allowedTime));
line = f.readLine();
Common::strlcpy(_locations[index].desc, line.c_str(), sizeof(_locations[index].desc));
stripUndies(_locations[index].desc);
// Skip line containing "-1"
f.readLine();
}
f.close();
/* for (int i = 0; i < _locationsNum; ++i) {
debug("%d %s %d %d %s\n",
i,
_locations[i].name,
_locations[i].xtend,
_locations[i].allowedTime,
_locations[i].desc);
}*/
}
void Database::initCharacters() {
File f;
Common::String line;
f.open(_pathPrefix + _databasePrefix + ".chr");
// Get number of entries in file
line = f.readLine();
sscanf(line.c_str(), "%d", &_charactersNum);
_characters = new Character[_charactersNum];
Character::_vm = _vm;
for (int i = 0; i < _charactersNum; ++i) {
int index;
do {
line = f.readLine();
} while (line.empty());
sscanf(line.c_str(), "%d", &index);
_characters[index]._id = index;
sscanf(line.c_str(), "%*d %s %d %d",
_characters[index]._name,
&(_characters[index]._xtend),
&(_characters[index]._type));
line = f.readLine();
Common::strlcpy(_characters[index]._desc, line.c_str(), sizeof(_characters[index]._desc));
stripUndies(_characters[index]._desc);
line = f.readLine();
sscanf(line.c_str(), "%d",
&(_characters[index]._proc));
line = f.readLine();
sscanf(line.c_str(), "%d %d %d %d %d",
&(_characters[index]._locationId),
&(_characters[index]._box),
&(_characters[index]._data5),
&(_characters[index]._data6),
&(_characters[index]._data7));
line = f.readLine();
sscanf(line.c_str(), "%d %d %d",
&(_characters[index]._data8),
&(_characters[index]._isMortal),
&(_characters[index]._hitPoints));
line = f.readLine();
sscanf(line.c_str(), "%d %d %d %d",
&(_characters[index]._strength),
&(_characters[index]._defense),
&(_characters[index]._minDamage),
&(_characters[index]._maxDamage));
line = f.readLine();
sscanf(line.c_str(), "%d %d %d %d",
&(_characters[index]._data14),
&(_characters[index]._data15),
&(_characters[index]._data16),
&(_characters[index]._spellPoints));
_characters[index]._destLoc = _characters[index]._locationId;
_characters[index]._destBox = _characters[index]._box;
_characters[index]._hitPointsMax = _characters[index]._hitPoints;
_characters[index]._spellPointsMax = _characters[index]._spellPoints;
}
f.close();
/* for (int i = 0; i < _charactersNum; ++i) {
debug("%d %s %d %d %s %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n",
i,
_characters[i].name,
_characters[i].xtend,
_characters[i].data2,
_characters[i].desc,
_characters[i].proc,
_characters[i].locationId,
_characters[i].box,
_characters[i].data5,
_characters[i].data6,
_characters[i].data7,
_characters[i].data8,
_characters[i].data9,
_characters[i].hitPoints,
_characters[i].strength,
_characters[i].defense,
_characters[i].damageMin,
_characters[i].damageMax,
_characters[i].data14,
_characters[i].data15,
_characters[i].data16,
_characters[i].spellPoints);
}*/
initScopes();
}
void Database::initObjects() {
File f;
Common::String line;
f.open(_pathPrefix + _databasePrefix + ".obs");
// Get number of entries in file
line = f.readLine();
sscanf(line.c_str(), "%d", &_objectsNum);
_objects = new Object[_objectsNum];
// There are less objects than the maximum object id
for (int i = 0; i < 327; ++i) {
int index;
// Skip empty line
f.readLine();
line = f.readLine();
sscanf(line.c_str(), "%d", &index);
// Object indices in .pro are smaller than in .obs - adjusting
index--;
sscanf(line.c_str(), "%*d %s %d",
_objects[index].name,
&(_objects[index].data1));
line = f.readLine();
Common::strlcpy(_objects[index].desc, line.c_str(), sizeof(_objects[index].desc));
stripUndies(_objects[index].desc);
line = f.readLine();
sscanf(line.c_str(), "%d %d %d",
&(_objects[index].type),
&(_objects[index].spellType),
&(_objects[index].proc));
line = f.readLine();
sscanf(line.c_str(), "%d %d %d %d %d %d %d %d",
&(_objects[index].data4),
&(_objects[index].isCarryable),
&(_objects[index].isContainer),
&(_objects[index].isVisible),
&(_objects[index].isSprite),
&(_objects[index].isUseImmediate),
&(_objects[index].isPickable),
&(_objects[index].isUsable));
line = f.readLine();
sscanf(line.c_str(), "%d %d %d %d %d",
&(_objects[index].price),
&(_objects[index].data11),
&(_objects[index].spellCost),
&(_objects[index].minDamage),
&(_objects[index].maxDamage));
line = f.readLine();
sscanf(line.c_str(), "%d %d",
&(_objects[index].ownerType),
&(_objects[index].ownerId));
if (_objects[index].ownerType == 1) {
sscanf(line.c_str(), "%*d %*d %d %d %d %d",
&(_objects[index].box),
&(_objects[index].data16),
&(_objects[index].data17),
&(_objects[index].data18));
}
}
f.close();
/* for (int i = 0; i < _objectsNum; ++i) {
debug("%d %s %d %s %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n",
i,
_objects[i].name,
_objects[i].data1,
_objects[i].desc,
_objects[i].type,
_objects[i].spellType,
_objects[i].proc,
_objects[i].data4,
_objects[i].isCarryable,
_objects[i].isContainer,
_objects[i].isVisible,
_objects[i].isSprite,
_objects[i].isUseImmediate,
_objects[i].isPickable,
_objects[i].isUsable,
_objects[i].price,
_objects[i].data11,
_objects[i].spellCost,
_objects[i].minDamage,
_objects[i].maxDamage,
_objects[i].ownerType,
_objects[i].ownerId,
_objects[i].box,
_objects[i].data16,
_objects[i].data17,
_objects[i].data18);
}*/
}
void Database::initEvents() {
int entries;
File f;
Common::String line;
f.open(_pathPrefix + _databasePrefix + ".box");
// Get number of entries in file
line = f.readLine();
sscanf(line.c_str(), "%d", &entries);
for (int i = 0; i < entries; ++i) {
int loc;
EventLink ev;
line = f.readLine();
sscanf(line.c_str(), "%d %d %d", &loc, &(ev.exitBox), &(ev.proc));
// Skip line
f.readLine();
_locations[loc].events.push_back(ev);
}
f.close();
/* for (int i = 0; i < _locationsNum; ++i) {
debug("location %d", i);
for (Common::List<EventLink>::iterator j = _locations[i].events.begin(); j != _locations[i].events.end(); ++j) {
debug("%d %d",
j->exitBox, j->proc);
}
}*/
}
void Database::initObjectLocs() {
for (int i = 0; i < _objectsNum; ++i) {
if (_objects[i].type == 0)
continue;
if (_objects[i].ownerType == 1)
_locations[_objects[i].ownerId].objects.push_front(i);
else switch (_objects[i].type) {
case 1:
case 5:
_characters[_objects[i].ownerId]._inventory.push_front(i);
break;
case 2:
_characters[_objects[i].ownerId]._weapons.push_front(i);
break;
case 3:
_characters[_objects[i].ownerId]._spells.push_front(i);
break;
}
}
}
void Database::initCharacterLocs() {
for (int i = 0; i < _charactersNum; ++i)
_locations[_characters[i]._locationId].characters.push_back(i);
_vm->game()->settings()->currLocation = _characters[0]._locationId;
}
void Database::initProcs() {
File f;
Common::String line;
f.open(_pathPrefix + _databasePrefix + ".pro");
line = f.readLine();
sscanf(line.c_str(), "%d", &_varSize);
_variables = (int16 *)calloc(_varSize, sizeof(_variables[0]));
// Get number of entries in file
line = f.readLine();
sscanf(line.c_str(), "%d", &_procsNum);
_processes = new Process[_procsNum];
for (int i = 0; i < _procsNum; ++i) {
int index;
int cmd, opcode;
do {
line = f.readLine();
} while (line.empty());
sscanf(line.c_str(), "%d", &index);
line = f.readLine();
Common::strlcpy(_processes[index].name, line.c_str(), sizeof(_processes[index].name));
do {
line = f.readLine();
} while (line.empty());
sscanf(line.c_str(), "%d", &cmd);
// Read commands
while (cmd != -1) {
Command cmdObject;
cmdObject.cmd = cmd;
// Read special cmd with value
if (cmd == 319 || cmd == 320 || cmd == 321) {
sscanf(line.c_str(), "%*d %hu", &(cmdObject.value));
}
// Read opcodes
do {
line = f.readLine();
} while (line.empty());
sscanf(line.c_str(), "%d", &opcode);
while (opcode != -1) {
OpCode opObject;
opObject.opcode = opcode;
switch (opcode) {
// string + int/short
case 474:
line = f.readLine();
Common::strlcpy(opObject.arg1, line.c_str(), sizeof(opObject.arg1));
line = f.readLine();
sscanf(line.c_str(), "%d", &(opObject.arg2));
break;
// string
case 467:
case 469:
line = f.readLine();
Common::strlcpy(opObject.arg1, line.c_str(), sizeof(opObject.arg1));
// Skip empty line
f.readLine();
break;
// string + int + int + int
case 468:
line = f.readLine();
Common::strlcpy(opObject.arg1, line.c_str(), sizeof(opObject.arg1));
line = f.readLine();
sscanf(line.c_str(), "%d %d %d", &(opObject.arg2),
&(opObject.arg3), &(opObject.arg4));
break;
// short + string - never reached
case 99999:
break;
// int
case 331:
case 337:
case 338:
case 373:
case 374:
case 381:
case 382:
case 383:
case 384:
case 387:
case 405:
case 406:
case 407:
case 408:
case 409:
case 410:
case 411:
case 412:
case 413:
case 414:
case 419:
case 433:
case 432:
case 444:
case 445:
case 446:
case 448:
case 491:
sscanf(line.c_str(), "%*d %d", &(opObject.arg2));
break;
// int + int
case 327:
case 328:
case 334:
case 340:
case 345:
case 346:
case 350:
case 353:
case 356:
case 359:
case 376:
case 377:
case 379:
case 380:
case 393:
case 398:
case 399:
case 404:
case 416:
case 417:
case 418:
case 420:
case 422:
case 423:
case 424:
case 425:
case 426:
case 427:
case 428:
case 430:
case 431:
case 434:
case 436:
case 437:
case 439:
case 458:
case 459:
case 462:
case 465:
case 466:
case 476:
case 477:
case 481:
case 482:
case 483:
case 484:
case 487:
case 489:
case 490:
case 492:
sscanf(line.c_str(), "%*d %d %d", &(opObject.arg2), &(opObject.arg3));
break;
// int + int + int
case 394:
case 395:
case 402:
case 403:
case 441:
case 478:
case 479:
case 488:
sscanf(line.c_str(), "%*d %d %d %d", &(opObject.arg2), &(opObject.arg3), &(opObject.arg4));
break;
// int + int + int + int + int
case 438:
sscanf(line.c_str(), "%*d %d %d %d %d %d", &(opObject.arg2), &(opObject.arg3), &(opObject.arg4),
&(opObject.arg5), &(opObject.arg6));
break;
// No arguments
case 391:
case 392:
case 449:
case 450:
case 451:
case 452:
case 453:
case 454:
case 473:
case 475:
case 485:
case 486:
case 494:
break;
default:
error("Unknown opcode %d in command %d in index %d", opcode, cmd, index);
break;
}
cmdObject.opcodes.push_back(opObject);
do {
line = f.readLine();
} while (line.empty());
sscanf(line.c_str(), "%d", &opcode);
}
_processes[index].commands.push_back(cmdObject);
do {
line = f.readLine();
} while (line.empty());
sscanf(line.c_str(), "%d", &cmd);
}
}
f.close();
}
void Database::initRoutes() {
File f;
Common::String line;
char keyword[30];
int16 locIndex = 0;
int16 boxIndex = 0;
int32 parmIndex, parmData;
f.open(_pathPrefix + "test0r.rou");
_routesSize = f.size();
_routes = new byte[_routesSize];
f.read(_routes, _routesSize);
f.close();
f.open(_pathPrefix + "test0r.map");
_mapSize = f.size();
_map = new byte[_mapSize];
f.read(_map, _mapSize);
f.close();
_locRoutes = new LocRoute[_locRoutesSize];
f.open(_pathPrefix + "test0r.ked");
do {
line = f.readLine();
} while (line.empty());
while (!f.eos()) {
sscanf(line.c_str(), "%s", keyword);
if (strcmp(keyword, "LOC") == 0) {
sscanf(line.c_str(), "%*s %hd", &locIndex);
} else if (strcmp(keyword, "exits") == 0) {
sscanf(line.c_str(), "%*s %d %d", &parmIndex, &parmData);
_locRoutes[locIndex].exits[parmIndex].exit = parmData;
} else if (strcmp(keyword, "exit_locs") == 0) {
sscanf(line.c_str(), "%*s %d %d", &parmIndex, &parmData);
_locRoutes[locIndex].exits[parmIndex].exitLoc = parmData;
} else if (strcmp(keyword, "exit_boxs") == 0) {
sscanf(line.c_str(), "%*s %d %d", &parmIndex, &parmData);
_locRoutes[locIndex].exits[parmIndex].exitBox = parmData;
} else if (strcmp(keyword, "BOX") == 0) {
sscanf(line.c_str(), "%*s %hd", &boxIndex);
_locRoutes[locIndex].boxes[boxIndex].enabled = true;
} else if (strcmp(keyword, "x1") == 0) {
sscanf(line.c_str(), "%*s %d", &parmData);
_locRoutes[locIndex].boxes[boxIndex].x1 = parmData;
} else if (strcmp(keyword, "y1") == 0) {
sscanf(line.c_str(), "%*s %d", &parmData);
_locRoutes[locIndex].boxes[boxIndex].y1 = parmData;
} else if (strcmp(keyword, "x2") == 0) {
sscanf(line.c_str(), "%*s %d", &parmData);
_locRoutes[locIndex].boxes[boxIndex].x2 = parmData;
} else if (strcmp(keyword, "y2") == 0) {
sscanf(line.c_str(), "%*s %d", &parmData);
_locRoutes[locIndex].boxes[boxIndex].y2 = parmData;
} else if (strcmp(keyword, "priority") == 0) {
sscanf(line.c_str(), "%*s %d", &parmData);
_locRoutes[locIndex].boxes[boxIndex].priority = parmData;
} else if (strcmp(keyword, "z1") == 0) {
sscanf(line.c_str(), "%*s %d", &parmData);
_locRoutes[locIndex].boxes[boxIndex].z1 = parmData;
} else if (strcmp(keyword, "z2") == 0) {
sscanf(line.c_str(), "%*s %d", &parmData);
_locRoutes[locIndex].boxes[boxIndex].z2 = parmData;
} else if (strcmp(keyword, "attrib") == 0) {
sscanf(line.c_str(), "%*s %d", &parmData);
_locRoutes[locIndex].boxes[boxIndex].attrib = parmData;
} else if (strcmp(keyword, "joins") == 0) {
sscanf(line.c_str(), "%*s %d %d", &parmIndex, &parmData);
_locRoutes[locIndex].boxes[boxIndex].joins[parmIndex] = parmData;
}
do {
line = f.readLine();
} while (line.empty() && !f.eos());
}
f.close();
}
void Database::initScopes() {
File f;
Common::String line;
char keyword[30];
uint8 actorIndex = 0;
uint8 scopeIndex = 0;
Character *charScope = NULL;
// Temporary variables to bypass scanf's %hhu - not supported by ISO C++
uint16 tmp1, tmp2, tmp3;
f.open(_pathPrefix + _databasePrefix + ".scp");
do {
line = f.readLine();
} while (line.empty());
while (!f.eos()) {
sscanf(line.c_str(), "%s", keyword);
if (strcmp(keyword, "ACTOR") == 0) {
sscanf(line.c_str(), "%*s %hu", &tmp1);
actorIndex = tmp1;
charScope = getChar(actorIndex);
} else if (strcmp(keyword, "SCOPE") == 0) {
sscanf(line.c_str(), "%*s %hu", &tmp1);
scopeIndex = tmp1;
sscanf(line.c_str(), "%*s %*u %hu %hu %hu",
&(tmp1), &(tmp2), &(tmp3));
charScope->_scopes[scopeIndex].minFrame = tmp1;
charScope->_scopes[scopeIndex].maxFrame = tmp2;
charScope->_scopes[scopeIndex].startFrame = tmp3;
} else if (strcmp(keyword, "SPEED") == 0) {
sscanf(line.c_str(), "%*s %hu %hu",
&(charScope->_walkSpeed),
&(charScope->_animSpeed));
if (charScope->_walkSpeed == 0) {
charScope->_stopped = true;
charScope->_timeout = 50;
charScope->_offset78 = 0x80000;
} else {
charScope->_stopped = false;
charScope->_timeout = 0;
charScope->_offset78 = 0xc0000;
}
} else if (strcmp(keyword, "TIMEOUT") == 0) {
sscanf(line.c_str(), "%*s %hu",
&(charScope->_timeout));
charScope->_timeout *= 24;
} else if (strcmp(keyword, "START") == 0) {
sscanf(line.c_str(), "%*s %d %d %d %d %d",
&(charScope->_lastLocation),
&(charScope->_lastBox),
&(charScope->_start3),
&(charScope->_start4),
&(charScope->_start5));
charScope->_gotoLoc = charScope->_lastLocation; // FIXME: Not in original. just in case
charScope->_gotoBox = charScope->_lastBox;
charScope->_screenX = charScope->_gotoX = charScope->_start3 / 256;
charScope->_screenY = charScope->_gotoY = charScope->_start4 / 256;
charScope->_start3Prev = charScope->_start3PrevPrev = charScope->_start3;
charScope->_start4Prev = charScope->_start4PrevPrev = charScope->_start4;
charScope->_start5Prev = charScope->_start5PrevPrev = charScope->_start5;
}
do {
line = f.readLine();
} while (line.empty() && !f.eos());
}
f.close();
}
int8 Database::box2box(int loc, int fromBox, int toBox) {
uint32 locOffset;
if ((loc | fromBox | toBox) > 127)
return -1;
locOffset = READ_LE_UINT32(_map + loc * 4);
return (int8)(_map[locOffset + *(int8*)(_map + locOffset) * toBox + fromBox + 1]);
}
int8 Database::whatBox(int locId, int x, int y) {
Box *boxes = _locRoutes[locId].boxes;
for (int i = 0; i < 32; ++i)
if (boxes[i].enabled &&
!(boxes[i].attrib & 6) &&
x >= boxes[i].x1 &&
x <= boxes[i].x2 &&
y >= boxes[i].y1 &&
y <= boxes[i].y2)
return i;
return -1;
}
int8 Database::whatBoxLinked(int locId, int8 boxId, int x, int y) {
Box *box = &(_locRoutes[locId].boxes[boxId]);
if ((box->attrib & 14) == 0 &&
x >= box->x1 && x <= box->x2 &&
y >= box->y1 && y <= box->y2) {
return boxId;
}
// search joined boxes
for (int i = 0; i < 6; ++i) {
if (box->joins[i] >= 0) {
Box *link = &(_locRoutes[locId].boxes[box->joins[i]]);
if ((link->attrib & 14) == 0 &&
x >= link->x1 && x <= link->x2 &&
y >= link->y1 && y <= link->y2) {
return box->joins[i];
}
}
}
return -1;
}
int16 Database::getMidOverlapX(int loc, int box1, int box2) {
int16 x1Max, x2Min;
Box *b1 = &(_locRoutes[loc].boxes[box1]);
Box *b2 = &(_locRoutes[loc].boxes[box2]);
x1Max = MAX(b1->x1, b2->x1);
x2Min = MIN(b1->x2, b2->x2);
if (x2Min < x1Max) {
x2Min ^= x1Max;
x1Max ^= x2Min;
x2Min ^= x1Max;
}
if (x1Max + x2Min <= 1)
return x1Max;
else
return x1Max + 1;
}
int16 Database::getMidOverlapY(int loc, int box1, int box2) {
int16 y1Max, y2Min;
Box *b1 = &(_locRoutes[loc].boxes[box1]);
Box *b2 = &(_locRoutes[loc].boxes[box2]);
y1Max = MAX(b1->y1, b2->y1);
y2Min = MIN(b1->y2, b2->y2);
if (y2Min < y1Max) {
y2Min ^= y1Max;
y1Max ^= y2Min;
y2Min ^= y1Max;
}
if (y1Max + y2Min <= 1)
return y1Max;
else
return y1Max + 1;
}
int16 Database::getZValue(int loc, int box, int32 y) {
Box *b;
if (loc < 0 || box < 0) return 1;
b = getBox(loc, box);
if (b->z2 == 0) return 1;
return b->z1 - ((y - b->y1 * 256) / (b->y2 - b->y1) * (b->z1 - b->z2)) / 256;
}