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 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "common/config-manager.h"
#include "audio/mixer.h"
#include "scumm/actor.h"
#include "scumm/charset.h"
#include "scumm/dialogs.h"
#include "scumm/file.h"
#include "scumm/imuse_digi/dimuse.h"
#ifdef ENABLE_HE
#include "scumm/he/intern_he.h"
#endif
#include "scumm/resource.h"
#include "scumm/scumm.h"
#include "scumm/scumm_v6.h"
#include "scumm/scumm_v8.h"
#include "scumm/verbs.h"
#include "scumm/he/sound_he.h"
namespace Scumm {
#pragma mark -
#pragma mark --- "High level" message code ---
#pragma mark -
void ScummEngine::printString(int m, const byte *msg) {
switch (m) {
case 0:
actorTalk(msg);
break;
case 1:
drawString(1, msg);
break;
case 2:
debugMessage(msg);
break;
case 3:
showMessageDialog(msg);
break;
default:
break;
}
}
#ifdef ENABLE_SCUMM_7_8
void ScummEngine_v8::printString(int m, const byte *msg) {
if (m == 4) {
const StringTab &st = _string[m];
enqueueText(msg, st.xpos, st.ypos, st.color, st.charset, st.center);
} else {
ScummEngine::printString(m, msg);
}
}
#endif
void ScummEngine::debugMessage(const byte *msg) {
byte buffer[500];
convertMessageToString(msg, buffer, sizeof(buffer));
if ((buffer[0] != 0xFF) && _debugMode) {
debug(0, "DEBUG: %s", buffer);
return;
}
if (buffer[0] == 0xFF && buffer[1] == 10) {
uint32 a, b;
int channel = 0;
a = buffer[2] | (buffer[3] << 8) | (buffer[6] << 16) | (buffer[7] << 24);
b = buffer[10] | (buffer[11] << 8) | (buffer[14] << 16) | (buffer[15] << 24);
// Sam and Max uses a caching system, printing empty messages
// and setting VAR_V6_SOUNDMODE beforehand. See patch 609791.
if (_game.id == GID_SAMNMAX)
channel = VAR(VAR_V6_SOUNDMODE);
if (channel != 2)
_sound->talkSound(a, b, 1, channel);
}
}
void ScummEngine::showMessageDialog(const byte *msg) {
// Original COMI used different code at this point.
// Seemed to use blastText for the messages
byte buf[500];
convertMessageToString(msg, buf, sizeof(buf));
if (_string[3].color == 0)
_string[3].color = 4;
InfoDialog dialog(this, (char *)buf);
VAR(VAR_KEYPRESS) = runDialog(dialog);
}
#pragma mark -
#pragma mark --- V6 blast text queue code ---
#pragma mark -
void ScummEngine_v6::enqueueText(const byte *text, int x, int y, byte color, byte charset, bool center) {
BlastText &bt = _blastTextQueue[_blastTextQueuePos++];
assert(_blastTextQueuePos <= ARRAYSIZE(_blastTextQueue));
convertMessageToString(text, bt.text, sizeof(bt.text));
bt.xpos = x;
bt.ypos = y;
bt.color = color;
bt.charset = charset;
bt.center = center;
}
void ScummEngine_v6::drawBlastTexts() {
byte *buf;
int c;
int i;
for (i = 0; i < _blastTextQueuePos; i++) {
buf = _blastTextQueue[i].text;
_charset->_top = _blastTextQueue[i].ypos + _screenTop;
_charset->_right = _screenWidth - 1;
_charset->_center = _blastTextQueue[i].center;
_charset->setColor(_blastTextQueue[i].color);
_charset->_disableOffsX = _charset->_firstChar = true;
_charset->setCurID(_blastTextQueue[i].charset);
if (_game.version >= 7 && _language == Common::HE_ISR) {
fakeBidiString(buf, false);
}
do {
_charset->_left = _blastTextQueue[i].xpos;
// Center text if necessary
if (_charset->_center) {
_charset->_left -= _charset->getStringWidth(0, buf) / 2;
if (_charset->_left < 0)
_charset->_left = 0;
}
do {
c = *buf++;
// FIXME: This is a workaround for bugs #864030 and #1399843:
// In COMI, some text contains ASCII character 11 = 0xB. It's
// not quite clear what it is good for; so for now we just ignore
// it, which seems to match the original engine (BTW, traditionally,
// this is a 'vertical tab').
if (c == 0x0B)
continue;
// Some localizations may override colors
// See credits in Chinese COMI
if (_game.id == GID_CMI && _language == Common::ZH_TWN &&
c == '^' && (buf == _blastTextQueue[i].text + 1)) {
if (*buf == 'c') {
int color = buf[3] - '0' + 10 *(buf[2] - '0');
_charset->setColor(color);
buf += 4;
c = *buf++;
}
}
if (c != 0 && c != 0xFF && c != '\n') {
if (c & 0x80 && _useCJKMode) {
if (_language == Common::JA_JPN && !checkSJISCode(c)) {
c = 0x20; //not in S-JIS
} else {
c += *buf++ * 256;
}
}
_charset->printChar(c, true);
}
} while (c && c != '\n');
_charset->_top += _charset->getFontHeight();
} while (c);
_blastTextQueue[i].rect = _charset->_str;
}
}
void ScummEngine_v6::removeBlastTexts() {
int i;
for (i = 0; i < _blastTextQueuePos; i++) {
restoreBackground(_blastTextQueue[i].rect);
}
_blastTextQueuePos = 0;
}
#pragma mark -
#pragma mark --- V7 subtitle queue code ---
#pragma mark -
#ifdef ENABLE_SCUMM_7_8
void ScummEngine_v7::processSubtitleQueue() {
for (int i = 0; i < _subtitleQueuePos; ++i) {
SubtitleText *st = &_subtitleQueue[i];
if (!st->actorSpeechMsg && (!ConfMan.getBool("subtitles") || VAR(VAR_VOICE_MODE) == 0))
// no subtitles and there's a speech variant of the message, don't display the text
continue;
enqueueText(st->text, st->xpos, st->ypos, st->color, st->charset, false);
}
}
void ScummEngine_v7::addSubtitleToQueue(const byte *text, const Common::Point &pos, byte color, byte charset) {
if (text[0] && strcmp((const char *)text, " ") != 0) {
assert(_subtitleQueuePos < ARRAYSIZE(_subtitleQueue));
SubtitleText *st = &_subtitleQueue[_subtitleQueuePos];
int i = 0;
while (1) {
st->text[i] = text[i];
if (!text[i])
break;
++i;
}
st->xpos = pos.x;
st->ypos = pos.y;
st->color = color;
st->charset = charset;
st->actorSpeechMsg = _haveActorSpeechMsg;
++_subtitleQueuePos;
}
}
void ScummEngine_v7::clearSubtitleQueue() {
memset(_subtitleQueue, 0, sizeof(_subtitleQueue));
_subtitleQueuePos = 0;
}
#endif
#pragma mark -
#pragma mark --- Core message/subtitle code ---
#pragma mark -
bool ScummEngine::handleNextCharsetCode(Actor *a, int *code) {
uint32 talk_sound_a = 0;
uint32 talk_sound_b = 0;
int color, frme, c = 0, oldy;
bool endLoop = false;
byte *buffer = _charsetBuffer + _charsetBufPos;
while (!endLoop) {
c = *buffer++;
if (!(c == 0xFF || (_game.version <= 6 && c == 0xFE))) {
break;
}
c = *buffer++;
if (_newLineCharacter != 0 && c == _newLineCharacter) {
c = 13;
break;
}
switch (c) {
case 1:
c = 13; // new line
_msgCount = _screenWidth;
endLoop = true;
break;
case 2:
_haveMsg = 0;
_keepText = true;
endLoop = true;
break;
case 3:
_haveMsg = (_game.version >= 7) ? 1 : 0xFF;
_keepText = false;
_msgCount = 0;
endLoop = true;
break;
case 8:
// Ignore this code here. Occurs e.g. in MI2 when you
// talk to the carpenter on scabb island. It works like
// code 1 (=newline) in verb texts, but is ignored in
// spoken text (i.e. here). Used for very long verb
// sentences.
break;
case 9:
frme = buffer[0] | (buffer[1] << 8);
buffer += 2;
if (a)
a->startAnimActor(frme);
break;
case 10:
// Note the similarity to the code in debugMessage()
talk_sound_a = buffer[0] | (buffer[1] << 8) | (buffer[4] << 16) | (buffer[5] << 24);
talk_sound_b = buffer[8] | (buffer[9] << 8) | (buffer[12] << 16) | (buffer[13] << 24);
buffer += 14;
if (_game.heversion >= 60) {
((SoundHE *)_sound)->startHETalkSound(talk_sound_a);
} else {
_sound->talkSound(talk_sound_a, talk_sound_b, 2);
}
_haveActorSpeechMsg = false;
break;
case 12:
color = buffer[0] | (buffer[1] << 8);
buffer += 2;
if (color == 0xFF)
_charset->setColor(_charsetColor);
else
_charset->setColor(color);
break;
case 13:
debug(0, "handleNextCharsetCode: Unknown opcode 13 %d", READ_LE_UINT16(buffer));
buffer += 2;
break;
case 14:
oldy = _charset->getFontHeight();
_charset->setCurID(*buffer++);
buffer += 2;
memcpy(_charsetColorMap, _charsetData[_charset->getCurID()], 4);
_nextTop -= _charset->getFontHeight() - oldy;
break;
default:
error("handleNextCharsetCode: invalid code %d", c);
}
}
_charsetBufPos = buffer - _charsetBuffer;
*code = c;
return (c != 2 && c != 3);
}
#ifdef ENABLE_HE
bool ScummEngine_v72he::handleNextCharsetCode(Actor *a, int *code) {
const int charsetCode = (_game.heversion >= 80) ? 127 : 64;
uint32 talk_sound_a = 0;
//uint32 talk_sound_b = 0;
int i, c = 0;
char value[32];
bool endLoop = false;
bool endText = false;
byte *buffer = _charsetBuffer + _charsetBufPos;
while (!endLoop) {
c = *buffer++;
if (c != charsetCode) {
break;
}
c = *buffer++;
switch (c) {
case 84:
i = 0;
c = *buffer++;
while (c != 44) {
value[i] = c;
c = *buffer++;
i++;
}
value[i] = 0;
talk_sound_a = atoi(value);
i = 0;
c = *buffer++;
while (c != charsetCode) {
value[i] = c;
c = *buffer++;
i++;
}
value[i] = 0;
//talk_sound_b = atoi(value);
((SoundHE *)_sound)->startHETalkSound(talk_sound_a);
break;
case 104:
_haveMsg = 0;
_keepText = true;
endLoop = endText = true;
break;
case 110:
c = 13; // new line
endLoop = true;
break;
case 116:
i = 0;
memset(value, 0, sizeof(value));
c = *buffer++;
while (c != charsetCode) {
value[i] = c;
c = *buffer++;
i++;
}
value[i] = 0;
talk_sound_a = atoi(value);
//talk_sound_b = 0;
((SoundHE *)_sound)->startHETalkSound(talk_sound_a);
break;
case 119:
_haveMsg = 0xFF;
_keepText = false;
endLoop = endText = true;
break;
default:
error("handleNextCharsetCode: invalid code %d", c);
}
}
_charsetBufPos = buffer - _charsetBuffer;
*code = c;
return (endText == 0);
}
#endif
bool ScummEngine::newLine() {
_nextLeft = _string[0].xpos;
if (_charset->_center) {
_nextLeft -= _charset->getStringWidth(0, _charsetBuffer + _charsetBufPos) / 2;
if (_nextLeft < 0)
_nextLeft = _game.version >= 6 ? _string[0].xpos : 0;
} else if (_game.version >= 4 && _game.version < 7 && _game.heversion == 0 && _language == Common::HE_ISR) {
if (_game.id == GID_MONKEY && _charset->getCurID() == 4) {
_nextLeft = _screenWidth - _charset->getStringWidth(0, _charsetBuffer + _charsetBufPos) - _nextLeft;
}
}
if (_game.version == 0) {
return false;
} else if (!(_game.platform == Common::kPlatformFMTowns) && _string[0].height) {
_nextTop += _string[0].height;
} else {
bool useCJK = _useCJKMode;
// SCUMM5 FM-Towns doesn't use the height of the ROM font here.
if (_game.platform == Common::kPlatformFMTowns && _game.version == 5)
_useCJKMode = false;
_nextTop += _charset->getFontHeight();
_useCJKMode = useCJK;
}
if (_game.version > 3) {
// FIXME: is this really needed?
_charset->_disableOffsX = true;
}
return true;
}
void ScummEngine::fakeBidiString(byte *ltext, bool ignoreVerb) {
// Provides custom made BiDi mechanism.
// Reverses texts on each line marked by control characters (considering different control characters used in verbs panel)
// While preserving original order of numbers (also negative numbers and comma separated)
int32 ll = 0;
if (_game.id == GID_INDY4 && ltext[ll] == 0x7F) {
ll++;
}
while (ltext[ll] == 0xFF) {
ll += 4;
}
int32 ipos = 0;
int32 start = 0;
byte *text = ltext + ll;
byte *current = text;
int32 bufferSize = 384;
byte * const buff = (byte *)calloc(sizeof(byte), bufferSize);
assert(buff);
byte * const stack = (byte *)calloc(sizeof(byte), bufferSize);
assert(stack);
while (1) {
if (*current == 0x0D || *current == 0 || *current == 0xFF || *current == 0xFE) {
// ignore the line break for verbs texts
if (ignoreVerb && (*(current + 1) == 8)) {
*(current + 1) = *current;
*current = 0x08;
ipos += 2;
current += 2;
continue;
}
memset(buff, 0, bufferSize);
memset(stack, 0, bufferSize);
// Reverse string on current line (between start and ipos).
int32 sthead = 0;
byte last = 0;
for (int32 j = 0; j < ipos; j++) {
byte *curr = text + start + ipos - j - 1;
// Special cases to preserve original ordering (numbers).
if (Common::isDigit(*curr) ||
(*curr == (byte)',' && j + 1 < ipos && Common::isDigit(*(curr - 1)) && Common::isDigit(last)) ||
(*curr == (byte)'-' && (j + 1 == ipos || Common::isSpace(*(curr - 1))) && Common::isDigit(last))) {
++sthead;
stack[sthead] = *curr;
} else {
while (sthead > 0) {
buff[j - sthead] = stack[sthead];
--sthead;
}
buff[j] = *curr;
}
last = *curr;
}
while (sthead > 0) {
buff[ipos - sthead] = stack[sthead];
--sthead;
}
memcpy(text + start, buff, ipos);
start += ipos + 1;
ipos = -1;
if (*current == 0xFF || *current == 0xFE) {
current++;
if (*current == 0x03 || *current == 0x02) {
break;
}
if (*current == 0x0A || *current == 0x0C) {
start += 2;
current += 2;
}
start++;
ipos++;
current++;
continue;
}
}
if (*current) {
ipos++;
current++;
continue;
}
break;
}
if (!ignoreVerb && _game.id == GID_INDY4 && ltext[0] == 0x7F) {
ltext[start + ipos + ll] = 0x80;
ltext[start + ipos + ll + 1] = 0;
}
free(buff);
free(stack);
}
void ScummEngine::CHARSET_1() {
Actor *a;
#ifdef ENABLE_SCUMM_7_8
byte subtitleBuffer[200];
byte *subtitleLine = subtitleBuffer;
Common::Point subtitlePos;
if (_game.version >= 7) {
((ScummEngine_v7 *)this)->processSubtitleQueue();
}
#endif
if (_game.heversion >= 70 && _haveMsg == 3) {
stopTalk();
return;
}
if (!_haveMsg)
return;
if (_game.version >= 4 && _game.version <= 6) {
// Do nothing while the camera is moving
if ((camera._dest.x / 8) != (camera._cur.x / 8) || camera._cur.x != camera._last.x)
return;
}
a = NULL;
if (getTalkingActor() != 0xFF)
a = derefActorSafe(getTalkingActor(), "CHARSET_1");
if (a && _string[0].overhead) {
int s;
_string[0].xpos = a->getPos().x - _virtscr[kMainVirtScreen].xstart;
_string[0].ypos = a->getPos().y - a->getElevation() - _screenTop;
if (_game.version <= 5) {
if (VAR(VAR_V5_TALK_STRING_Y) < 0) {
s = (a->_scaley * (int)VAR(VAR_V5_TALK_STRING_Y)) / 0xFF;
_string[0].ypos += (int)(((VAR(VAR_V5_TALK_STRING_Y) - s) / 2) + s);
} else {
_string[0].ypos = (int)VAR(VAR_V5_TALK_STRING_Y);
}
} else {
s = a->_scalex * a->_talkPosX / 0xFF;
_string[0].xpos += ((a->_talkPosX - s) / 2) + s;
s = a->_scaley * a->_talkPosY / 0xFF;
_string[0].ypos += ((a->_talkPosY - s) / 2) + s;
if (_string[0].ypos > _screenHeight - 40)
_string[0].ypos = _screenHeight - 40;
}
if (_string[0].ypos < 1)
_string[0].ypos = 1;
if (_string[0].xpos < 80)
_string[0].xpos = 80;
if (_string[0].xpos > _screenWidth - 80)
_string[0].xpos = _screenWidth - 80;
}
_charset->_top = _string[0].ypos + _screenTop;
_charset->_startLeft = _charset->_left = _string[0].xpos;
_charset->_right = _string[0].right;
_charset->_center = _string[0].center;
_charset->setColor(_charsetColor);
if (a && a->_charset)
_charset->setCurID(a->_charset);
else
_charset->setCurID(_string[0].charset);
if (_game.version >= 5)
memcpy(_charsetColorMap, _charsetData[_charset->getCurID()], 4);
#ifndef DISABLE_TOWNS_DUAL_LAYER_MODE
if (_keepText && _game.platform == Common::kPlatformFMTowns)
memcpy(&_charset->_str, &_curStringRect, sizeof(Common::Rect));
#endif
if (_talkDelay)
return;
if ((_game.version <= 6 && _haveMsg == 1) ||
(_game.version == 7 && _haveMsg != 1)) {
if (_game.heversion >= 60) {
if (_sound->isSoundRunning(1) == 0)
stopTalk();
} else {
if ((_sound->_sfxMode & 2) == 0)
stopTalk();
}
return;
}
if (a && !_string[0].no_talk_anim) {
a->runActorTalkScript(a->_talkStartFrame);
_useTalkAnims = true;
}
_talkDelay = (VAR_DEFAULT_TALK_DELAY != 0xFF) ? VAR(VAR_DEFAULT_TALK_DELAY) : 60;
if (!_keepText) {
if (_game.version >= 7) {
#ifdef ENABLE_SCUMM_7_8
((ScummEngine_v7 *)this)->clearSubtitleQueue();
_nextLeft = _string[0].xpos;
_nextTop = _string[0].ypos + _screenTop;
#endif
} else {
#ifndef DISABLE_TOWNS_DUAL_LAYER_MODE
if (_game.platform == Common::kPlatformFMTowns)
towns_restoreCharsetBg();
else
#endif
restoreCharsetBg();
}
_msgCount = 0;
} else if (_game.version <= 2) {
_talkDelay += _msgCount * _defaultTalkDelay;
}
if (_game.version > 3) {
int maxwidth = _charset->_right - _string[0].xpos - 1;
if (_charset->_center) {
if (maxwidth > _nextLeft)
maxwidth = _nextLeft;
maxwidth *= 2;
}
_charset->addLinebreaks(0, _charsetBuffer + _charsetBufPos, 0, maxwidth);
}
if (_charset->_center) {
_nextLeft -= _charset->getStringWidth(0, _charsetBuffer + _charsetBufPos) / 2;
if (_nextLeft < 0)
_nextLeft = _game.version >= 6 ? _string[0].xpos : 0;
} else if (_game.version >= 4 && _game.version < 7 && _game.heversion == 0 && _language == Common::HE_ISR) {
if (_game.id == GID_MONKEY && _charset->getCurID() == 4) {
_nextLeft = _screenWidth - _charset->getStringWidth(0, _charsetBuffer + _charsetBufPos) - _nextLeft;
}
}
_charset->_disableOffsX = _charset->_firstChar = !_keepText;
int c = 0;
if (_game.version >= 4 && _game.version < 7 && _game.heversion == 0 && _language == Common::HE_ISR) {
fakeBidiString(_charsetBuffer + _charsetBufPos, true);
}
while (handleNextCharsetCode(a, &c)) {
if (c == 0) {
// End of text reached, set _haveMsg accordingly
_haveMsg = (_game.version >= 7) ? 2 : 1;
_keepText = false;
_msgCount = 0;
break;
}
if (c == 13) {
#ifdef ENABLE_SCUMM_7_8
if (_game.version >= 7 && subtitleLine != subtitleBuffer) {
((ScummEngine_v7 *)this)->addSubtitleToQueue(subtitleBuffer, subtitlePos, _charsetColor, _charset->getCurID());
subtitleLine = subtitleBuffer;
}
#endif
if (!newLine())
break;
continue;
}
// Handle line overflow for V3. See also bug #1306269.
if (_game.version == 3 && _nextLeft >= _screenWidth) {
_nextLeft = _screenWidth;
}
// Handle line breaks for V1-V2
if (_game.version <= 2 && _nextLeft >= _screenWidth) {
if (!newLine())
break; // FIXME: Is this necessary? Only would be relevant for v0 games
}
_charset->_left = _nextLeft;
_charset->_top = _nextTop;
if (_game.version >= 7) {
#ifdef ENABLE_SCUMM_7_8
if (subtitleLine == subtitleBuffer) {
subtitlePos.x = _charset->_left;
// BlastText position is relative to the top of the screen, adjust y-coordinate
subtitlePos.y = _charset->_top - _screenTop;
}
*subtitleLine++ = c;
*subtitleLine = '\0';
#endif
} else {
if (c & 0x80 && _useCJKMode) {
if (checkSJISCode(c)) {
byte *buffer = _charsetBuffer + _charsetBufPos;
c += *buffer++ * 256; //LE
_charsetBufPos = buffer - _charsetBuffer;
}
}
if (_game.version <= 3) {
_charset->printChar(c, false);
_msgCount += 1;
} else {
if (_game.features & GF_16BIT_COLOR) {
// HE games which use sprites for subtitles
} else if (_game.heversion >= 60 && !ConfMan.getBool("subtitles") && _sound->isSoundRunning(1)) {
// Special case for HE games
} else if (_game.id == GID_LOOM && !ConfMan.getBool("subtitles") && (_sound->pollCD())) {
// Special case for Loom (CD), since it only uses CD audio.for sound
} else if (!ConfMan.getBool("subtitles") && (!_haveActorSpeechMsg || _mixer->isSoundHandleActive(*_sound->_talkChannelHandle))) {
// Subtitles are turned off, and there is a voice version
// of this message -> don't print it.
} else {
_charset->printChar(c, false);
}
}
_nextLeft = _charset->_left;
_nextTop = _charset->_top;
}
if (_game.version <= 2) {
_talkDelay += _defaultTalkDelay;
VAR(VAR_CHARCOUNT)++;
} else {
_talkDelay += (int)VAR(VAR_CHARINC);
}
}
#ifndef DISABLE_TOWNS_DUAL_LAYER_MODE
if (_game.platform == Common::kPlatformFMTowns && (c == 0 || c == 2 || c == 3))
memcpy(&_curStringRect, &_charset->_str, sizeof(Common::Rect));
#endif
#ifdef ENABLE_SCUMM_7_8
if (_game.version >= 7 && subtitleLine != subtitleBuffer) {
((ScummEngine_v7 *)this)->addSubtitleToQueue(subtitleBuffer, subtitlePos, _charsetColor, _charset->getCurID());
}
#endif
}
#ifdef ENABLE_SCUMM_7_8
void ScummEngine_v7::CHARSET_1() {
if (_game.id == GID_FT) {
ScummEngine::CHARSET_1();
return;
}
byte subtitleBuffer[2048];
byte *subtitleLine = subtitleBuffer;
Common::Point subtitlePos;
processSubtitleQueue();
if (!_haveMsg)
return;
Actor *a = NULL;
if (getTalkingActor() != 0xFF)
a = derefActorSafe(getTalkingActor(), "CHARSET_1");
StringTab saveStr = _string[0];
if (a && _string[0].overhead) {
int s;
_string[0].xpos = a->getPos().x - _virtscr[kMainVirtScreen].xstart;
s = a->_scalex * a->_talkPosX / 255;
_string[0].xpos += (a->_talkPosX - s) / 2 + s;
_string[0].ypos = a->getPos().y - a->getElevation() - _screenTop;
s = a->_scaley * a->_talkPosY / 255;
_string[0].ypos += (a->_talkPosY - s) / 2 + s;
}
_charset->setColor(_charsetColor);
if (a && a->_charset)
_charset->setCurID(a->_charset);
else
_charset->setCurID(_string[0].charset);
if (_talkDelay)
return;
if (VAR(VAR_HAVE_MSG)) {
if ((_sound->_sfxMode & 2) == 0) {
stopTalk();
}
return;
}
if (a && !_string[0].no_talk_anim) {
a->runActorTalkScript(a->_talkStartFrame);
}
if (!_keepText) {
clearSubtitleQueue();
_nextLeft = _string[0].xpos;
_nextTop = _string[0].ypos + _screenTop;
}
_charset->_disableOffsX = _charset->_firstChar = !_keepText;
_talkDelay = VAR(VAR_DEFAULT_TALK_DELAY);
for (int i = _charsetBufPos; _charsetBuffer[i]; ++i) {
_talkDelay += VAR(VAR_CHARINC);
}
if (_string[0].wrapping) {
_charset->addLinebreaks(0, _charsetBuffer, _charsetBufPos, _screenWidth - 20);
struct { int pos, w; } substring[10];
int count = 0;
int maxLineWidth = 0;
int lastPos = 0;
int code = 0;
while (handleNextCharsetCode(a, &code)) {
if (code == 13 || code == 0) {
*subtitleLine++ = '\0';
assert(count < 10);
substring[count].w = _charset->getStringWidth(0, subtitleBuffer + lastPos);
if (maxLineWidth < substring[count].w) {
maxLineWidth = substring[count].w;
}
substring[count].pos = lastPos;
++count;
lastPos = subtitleLine - subtitleBuffer;
} else {
*subtitleLine++ = code;
*subtitleLine = '\0';
}
if (code == 0) {
break;
}
}
int h = count * _charset->getFontHeight();
h += _charset->getFontHeight() / 2;
subtitlePos.y = _string[0].ypos;
if (subtitlePos.y + h > _screenHeight - 10) {
subtitlePos.y = _screenHeight - 10 - h;
}
if (subtitlePos.y < 10) {
subtitlePos.y = 10;
}
for (int i = 0; i < count; ++i) {
subtitlePos.x = _string[0].xpos;
if (_string[0].center) {
if (subtitlePos.x + maxLineWidth / 2 > _screenWidth - 10) {
subtitlePos.x = _screenWidth - 10 - maxLineWidth / 2;
}
if (subtitlePos.x - maxLineWidth / 2 < 10) {
subtitlePos.x = 10 + maxLineWidth / 2;
}
subtitlePos.x -= substring[i].w / 2;
} else {
if (subtitlePos.x + maxLineWidth > _screenWidth - 10) {
subtitlePos.x = _screenWidth - 10 - maxLineWidth;
}
if (subtitlePos.x - maxLineWidth < 10) {
subtitlePos.x = 10;
}
}
if (subtitlePos.y < _screenHeight - 10) {
addSubtitleToQueue(subtitleBuffer + substring[i].pos, subtitlePos, _charsetColor, _charset->getCurID());
}
subtitlePos.y += _charset->getFontHeight();
}
} else {
int code = 0;
subtitlePos.y = _string[0].ypos;
if (subtitlePos.y < 10) {
subtitlePos.y = 10;
}
while (handleNextCharsetCode(a, &code)) {
if (code == 13 || code == 0) {
subtitlePos.x = _string[0].xpos;
if (_string[0].center) {
subtitlePos.x -= _charset->getStringWidth(0, subtitleBuffer) / 2;
}
if (subtitlePos.x < 10) {
subtitlePos.x = 10;
}
if (subtitlePos.y < _screenHeight - 10) {
addSubtitleToQueue(subtitleBuffer, subtitlePos, _charsetColor, _charset->getCurID());
subtitlePos.y += _charset->getFontHeight();
}
subtitleLine = subtitleBuffer;
} else {
*subtitleLine++ = code;
}
*subtitleLine = '\0';
if (code == 0) {
break;
}
}
}
_haveMsg = (_game.version == 8) ? 2 : 1;
_keepText = false;
_string[0] = saveStr;
}
#endif
void ScummEngine::drawString(int a, const byte *msg) {
byte buf[270];
byte *space;
int i, c;
byte fontHeight = 0;
uint color;
int code = (_game.heversion >= 80) ? 127 : 64;
// drawString is not used in SCUMM v7 and v8
assert(_game.version < 7);
convertMessageToString(msg, buf, sizeof(buf));
if (_game.version >= 4 && _game.version < 7 && _game.heversion == 0 && _language == Common::HE_ISR) {
fakeBidiString(buf, false);
}
_charset->_top = _string[a].ypos + _screenTop;
_charset->_startLeft = _charset->_left = _string[a].xpos;
_charset->_right = _string[a].right;
_charset->_center = _string[a].center;
_charset->setColor(_string[a].color);
_charset->_disableOffsX = _charset->_firstChar = true;
_charset->setCurID(_string[a].charset);
// HACK: Correct positions of text in books in Indy3 Mac.
// See also patch #1851568.
if (_game.id == GID_INDY3 && _game.platform == Common::kPlatformMacintosh && a == 1) {
if (_currentRoom == 75) {
// Grail Diary Page 1 (Library)
if (_charset->_startLeft < 160)
_charset->_startLeft = _charset->_left = _string[a].xpos - 22;
else if (_charset->_startLeft < 200)
_charset->_startLeft = _charset->_left = _string[a].xpos - 10;
} else if (_currentRoom == 90) {
// Grail Diary Page 2 (Catacombs - Engravings)
if (_charset->_startLeft < 160)
_charset->_startLeft = _charset->_left = _string[a].xpos - 21;
else if (_charset->_startLeft < 200)
_charset->_startLeft = _charset->_left = _string[a].xpos - 15;
} else if (_currentRoom == 69) {
// Grail Diary Page 3 (Catacombs - Music)
if (_charset->_startLeft < 160)
_charset->_startLeft = _charset->_left = _string[a].xpos - 15;
else if (_charset->_startLeft < 200)
_charset->_startLeft = _charset->_left = _string[a].xpos - 10;
} else if (_currentRoom == 74) {
// Biplane Manual
_charset->_startLeft = _charset->_left = _string[a].xpos - 35;
}
}
if (_game.version >= 5)
memcpy(_charsetColorMap, _charsetData[_charset->getCurID()], 4);
fontHeight = _charset->getFontHeight();
if (_game.version >= 4) {
// trim from the right
byte *tmp = buf;
space = NULL;
while (*tmp) {
if (*tmp == ' ') {
if (!space)
space = tmp;
} else {
space = NULL;
}
tmp++;
}
if (space)
*space = '\0';
}
if (_charset->_center) {
_charset->_left -= _charset->getStringWidth(a, buf) / 2;
} else if (_game.version >= 4 && _game.version < 7 && _game.heversion == 0 && _game.id != GID_SAMNMAX && _language == Common::HE_ISR) {
// Ignore INDY4 verbs (but allow dialogue)
if (_game.id != GID_INDY4 || buf[0] == 127) {
int ll = 0;
if (_game.id == GID_INDY4 && buf[0] == 127) {
buf[0] = 32;
ll++;
}
// Skip control characters as they might contain '\0' which results in incorrect string width.
byte *ltext = buf;
while (ltext[ll] == 0xFF) {
ll += 4;
}
byte lenbuf[270];
memset(lenbuf, 0, sizeof(lenbuf));
int pos = ll;
while (ltext[pos]) {
if ((ltext[pos] == 0xFF || (_game.version <= 6 && ltext[pos] == 0xFE)) && ltext[pos+1] == 8) {
break;
}
pos++;
}
memcpy(lenbuf, ltext, pos);
_charset->_left = _screenWidth - _charset->_startLeft - _charset->getStringWidth(a, lenbuf);
}
}
if (!buf[0]) {
if (_game.version >= 5) {
buf[0] = ' ';
buf[1] = 0;
} else {
_charset->_str.left = _charset->_left;
_charset->_str.top = _charset->_top;
_charset->_str.right = _charset->_left;
_charset->_str.bottom = _charset->_top;
}
}
for (i = 0; (c = buf[i++]) != 0;) {
if (_game.heversion >= 72 && c == code) {
c = buf[i++];
switch (c) {
case 110:
if (_charset->_center) {
_charset->_left = _charset->_startLeft - _charset->getStringWidth(a, buf + i);
} else {
_charset->_left = _charset->_startLeft;
}
_charset->_top += fontHeight;
break;
default:
break;
}
} else if ((c == 0xFF || (_game.version <= 6 && c == 0xFE)) && (_game.heversion <= 71)) {
c = buf[i++];
switch (c) {
case 9:
case 10:
case 13:
case 14:
i += 2;
break;
case 1:
case 8:
if (_charset->_center) {
_charset->_left = _charset->_startLeft - _charset->getStringWidth(a, buf + i);
} else if (_game.version >= 4 && _game.version < 7 && _game.heversion == 0 && _language == Common::HE_ISR) {
// Skip control characters as they might contain '\0' which results in incorrect string width.
int ll = 0;
byte *ltext = buf + i;
while (ltext[ll] == 0xFF) {
ll += 4;
}
byte lenbuf[270];
memset(lenbuf, 0, sizeof(lenbuf));
memcpy(lenbuf, ltext, ll);
int u = ll;
while (ltext[u]) {
if ((ltext[u] == 0xFF || (_game.version <= 6 && ltext[u] == 0xFE)) && ltext[u + 1] == 8) {
break;
}
u++;
}
memcpy(lenbuf, ltext, u);
_charset->_left = _screenWidth - _charset->_startLeft - _charset->getStringWidth(a, lenbuf);
} else {
_charset->_left = _charset->_startLeft;
}
if (!(_game.platform == Common::kPlatformFMTowns) && _string[0].height) {
_nextTop += _string[0].height;
} else {
_charset->_top += fontHeight;
}
break;
case 12:
color = buf[i] + (buf[i + 1] << 8);
i += 2;
if (color == 0xFF)
_charset->setColor(_string[a].color);
else
_charset->setColor(color);
break;
default:
break;
}
} else {
if (a == 1 && _game.version >= 6) {
// FIXME: The following code is a bit nasty. It is used for the
// Highway surfing game in Sam&Max; there, _blitAlso is set to
// true when writing the highscore numbers. It is also in DOTT
// for parts the intro and for drawing newspaper headlines. It
// is also used for scores in bowling mini game in fbear and
// for names in load/save screen of all HE games. Maybe it is
// also being used in other places.
//
// A better name for _blitAlso might be _imprintOnBackground
if (_string[a].no_talk_anim == false) {
//debug(0, "Would have set _charset->_blitAlso = true (wanted to print '%c' = %d)", c, c);
_charset->_blitAlso = true;
}
}
if (c & 0x80 && _useCJKMode) {
if (checkSJISCode(c))
c += buf[i++] * 256;
}
_charset->printChar(c, true);
_charset->_blitAlso = false;
}
}
if (a == 0) {
_nextLeft = _charset->_left;
_nextTop = _charset->_top;
}
_string[a].xpos = _charset->_str.right;
if (_game.heversion >= 60) {
_string[a]._default.xpos = _string[a].xpos;
_string[a]._default.ypos = _string[a].ypos;
}
}
int ScummEngine::convertMessageToString(const byte *msg, byte *dst, int dstSize) {
uint num = 0;
uint32 val;
byte chr;
byte lastChr = 0;
const byte *src;
byte *end;
byte transBuf[384];
assert(dst);
end = dst + dstSize;
if (msg == NULL) {
debug(0, "Bad message in convertMessageToString, ignoring");
return 0;
}
if (_game.version >= 7) {
translateText(msg, transBuf);
src = transBuf;
} else {
src = msg;
}
num = 0;
while (1) {
chr = src[num++];
if (chr == 0)
break;
if (_game.id == GID_LOOM && _game.platform == Common::kPlatformPCEngine) {
// Code for TM character
if (chr == 0x0F && src[num] == 0x20) {
*dst++ = 0x5D;
*dst++ = 0x5E;
continue;
// Code for (C) character
} else if (chr == 0x1C && src[num] == 0x20) {
*dst++ = 0x3E;
*dst++ = 0x2A;
continue;
// Code for " character
} else if (chr == 0x19) {
*dst++ = 0x2F;
continue;
}
}
if (chr == 0xFF) {
chr = src[num++];
// WORKAROUND for bug #985948, a script bug in Indy3. Apparently,
// a german 'sz' was encoded incorrectly as 0xFF2E. We replace
// this by the correct encoding here. See also ScummEngine::resStrLen().
if (_game.id == GID_INDY3 && chr == 0x2E) {
*dst++ = 0xE1;
continue;
}
// WORKAROUND for bug #1514457: Yet another script bug in Indy3.
// Once more a german 'sz' was encoded incorrectly, but this time
// they simply encoded it as 0xFF instead of 0xE1. Happens twice
// in script 71.
if (_game.id == GID_INDY3 && chr == 0x20 && vm.slot[_currentScript].number == 71) {
num--;
*dst++ = 0xE1;
continue;
}
if (chr == 1 || chr == 2 || chr == 3 || chr == 8) {
// Simply copy these special codes
*dst++ = 0xFF;
*dst++ = chr;
} else {
val = (_game.version == 8) ? READ_LE_UINT32(src + num) : READ_LE_UINT16(src + num);
switch (chr) {
case 4:
dst += convertIntMessage(dst, end - dst, val);
break;
case 5:
dst += convertVerbMessage(dst, end - dst, val);
break;
case 6:
dst += convertNameMessage(dst, end - dst, val);
break;
case 7:
dst += convertStringMessage(dst, end - dst, val);
break;
case 9:
case 10:
case 12:
case 13:
case 14:
// Simply copy these special codes
*dst++ = 0xFF;
*dst++ = chr;
*dst++ = src[num+0];
*dst++ = src[num+1];
if (_game.version == 8) {
*dst++ = src[num+2];
*dst++ = src[num+3];
}
break;
default:
error("convertMessageToString(): string escape sequence %d unknown", chr);
}
num += (_game.version == 8) ? 4 : 2;
}
} else {
if ((chr != '@') || (_game.id == GID_CMI && _language == Common::ZH_TWN) ||
(_game.id == GID_LOOM && _game.platform == Common::kPlatformPCEngine && _language == Common::JA_JPN) ||
(_game.platform == Common::kPlatformFMTowns && _language == Common::JA_JPN && checkSJISCode(lastChr))) {
*dst++ = chr;
}
lastChr = chr;
}
// Check for a buffer overflow
if (dst >= end)
error("convertMessageToString: buffer overflow");
}
// WORKAROUND: Russian The Dig pads messages with 03. No idea why
// it does not work as is with our rendering code, thus fixing it
// with a workaround.
if (_game.id == GID_DIG) {
while (*(dst - 1) == 0x03)
dst--;
}
*dst = 0;
return dstSize - (end - dst);
}
#ifdef ENABLE_HE
int ScummEngine_v72he::convertMessageToString(const byte *msg, byte *dst, int dstSize) {
uint num = 0;
byte chr;
const byte *src;
byte *end;
assert(dst);
end = dst + dstSize;
if (msg == NULL) {
debug(0, "Bad message in convertMessageToString, ignoring");
return 0;
}
src = msg;
num = 0;
while (1) {
chr = src[num++];
if (_game.heversion >= 80 && src[num - 1] == '(' && (src[num] == 'p' || src[num] == 'P')) {
// Filter out the following prefixes in subtitles
// (pickup4)
// (PU1)
// (PU2)
while (src[num++] != ')')
;
continue;
}
if ((_game.features & GF_HE_LOCALIZED) && chr == '[') {
while (src[num++] != ']')
;
continue;
}
if (chr == 0)
break;
*dst++ = chr;
// Check for a buffer overflow
if (dst >= end)
error("convertMessageToString: buffer overflow");
}
*dst = 0;
return dstSize - (end - dst);
}
#endif
int ScummEngine::convertIntMessage(byte *dst, int dstSize, int var) {
int num;
num = readVar(var);
return snprintf((char *)dst, dstSize, "%d", num);
}
int ScummEngine::convertVerbMessage(byte *dst, int dstSize, int var) {
int num, k;
num = readVar(var);
if (num) {
for (k = 1; k < _numVerbs; k++) {
// Fix ZAK FM-TOWNS bug #1013617 by emulating exact (inconsistant?) behavior of the original code
if (num == _verbs[k].verbid && !_verbs[k].type && (!_verbs[k].saveid || (_game.version == 3 && _game.platform == Common::kPlatformFMTowns))) {
const byte *ptr = getResourceAddress(rtVerb, k);
return convertMessageToString(ptr, dst, dstSize);
}
}
}
return 0;
}
int ScummEngine::convertNameMessage(byte *dst, int dstSize, int var) {
int num;
num = readVar(var);
if (num) {
const byte *ptr = getObjOrActorName(num);
if (ptr) {
return convertMessageToString(ptr, dst, dstSize);
}
}
return 0;
}
int ScummEngine::convertStringMessage(byte *dst, int dstSize, int var) {
const byte *ptr;
if (_game.version <= 2) {
byte chr;
int i = 0;
while ((chr = (byte)_scummVars[var++])) {
if (chr != '@') {
*dst++ = chr;
i++;
}
}
return i;
}
if (_game.version == 3 || (_game.version >= 6 && _game.heversion < 72))
var = readVar(var);
if (var) {
ptr = getStringAddress(var);
if (ptr) {
return convertMessageToString(ptr, dst, dstSize);
}
}
return 0;
}
#pragma mark -
#pragma mark --- Charset initialisation ---
#pragma mark -
#ifdef ENABLE_HE
void ScummEngine_v80he::initCharset(int charsetno) {
ScummEngine::initCharset(charsetno);
VAR(VAR_CURRENT_CHARSET) = charsetno;
}
#endif
void ScummEngine::initCharset(int charsetno) {
if (_game.id == GID_FT) {
if (!_res->isResourceLoaded(rtCharset, charsetno))
loadCharset(charsetno);
} else {
if (!getResourceAddress(rtCharset, charsetno))
loadCharset(charsetno);
}
_string[0]._default.charset = charsetno;
_string[1]._default.charset = charsetno;
memcpy(_charsetColorMap, _charsetData[charsetno], sizeof(_charsetColorMap));
}
#pragma mark -
#pragma mark --- Translation/localization code ---
#pragma mark -
#ifdef ENABLE_SCUMM_7_8
static int indexCompare(const void *p1, const void *p2) {
const ScummEngine_v7::LangIndexNode *i1 = (const ScummEngine_v7::LangIndexNode *) p1;
const ScummEngine_v7::LangIndexNode *i2 = (const ScummEngine_v7::LangIndexNode *) p2;
return strcmp(i1->tag, i2->tag);
}
// Create an index of the language file.
void ScummEngine_v7::loadLanguageBundle() {
ScummFile file;
int32 size;
// if game is manually set to English, don't try to load localized text
if ((_language == Common::EN_ANY) || (_language == Common::EN_USA) || (_language == Common::EN_GRB)) {
warning("Language file is forced to be ignored");
_existLanguageFile = false;
return;
}
if (_game.id == GID_DIG) {
openFile(file, "language.bnd");
} else if (_game.id == GID_CMI) {
openFile(file, "language.tab");
} else {
return;
}
if (file.isOpen() == false) {
_existLanguageFile = false;
return;
}
_existLanguageFile = true;
size = file.size();
_languageBuffer = (char *)calloc(1, size+1);
file.read(_languageBuffer, size);
file.close();
int32 i;
char *ptr = _languageBuffer;
// Count the number of lines in the language file.
for (_languageIndexSize = 0; ; _languageIndexSize++) {
ptr = strpbrk(ptr, "\n\r");
if (ptr == NULL)
break;
while (*ptr == '\n' || *ptr == '\r')
ptr++;
}
// Fill the language file index. This is just an array of
// tags and offsets. I did consider using a balanced tree
// instead, but the extra overhead in the node structure would
// easily have doubled the memory consumption of the index.
// And anyway, using qsort + bsearch gives us the exact same
// O(log(n)) access time anyway ;-).
_languageIndex = (LangIndexNode *)calloc(_languageIndexSize, sizeof(LangIndexNode));
ptr = _languageBuffer;
if (_game.id == GID_DIG) {
int lineCount = _languageIndexSize;
const char *baseTag = "";
byte enc = 0; // Initially assume the language file is not encoded
// We'll determine the real index size as we go.
_languageIndexSize = 0;
for (i = 0; i < lineCount; i++) {
if (*ptr == '!') {
// Don't know what a line with '!' means, just ignore it
} else if (*ptr == 'h') {
// File contains Korean text (Hangul). just ignore it
} else if (*ptr == 'j') {
// File contains Japanese text. just ignore it
} else if (*ptr == 'c') {
// File contains Chinese text. just ignore it
} else if (*ptr == 'e') {
// File is encoded!
enc = 0x13;
} else if (*ptr == '@') {
// A new 'base tag'
baseTag = ptr + 1;
} else if (*ptr == '#') {
// Number of subtags following a given basetag. We don't need that
// information so we just skip it
} else if (Common::isDigit(*ptr)) {
int idx = 0;
// A number (up to three digits)...
while (Common::isDigit(*ptr)) {
idx = idx * 10 + (*ptr - '0');
ptr++;
}
// ...followed by a slash...
assert(*ptr == '/');
ptr++;
// ...and then the translated message, possibly encoded
_languageIndex[_languageIndexSize].offset = ptr - _languageBuffer;
// Decode string if necessary.
if (enc) {
while (*ptr != '\n' && *ptr != '\r')
*ptr++ ^= enc;
}
// The tag is the basetag, followed by a dot and then the index
sprintf(_languageIndex[_languageIndexSize].tag, "%s.%03d", baseTag, idx);
// That was another index entry
_languageIndexSize++;
} else {
error("Unknown language.bnd entry found: '%s'", ptr);
}
// Skip over newlines (and turn them into null bytes)
ptr = strpbrk(ptr, "\n\r");
if (ptr == NULL)
break;
while (*ptr == '\n' || *ptr == '\r')
*ptr++ = 0;
}
} else {
for (i = 0; i < _languageIndexSize; i++) {
// First 8 chars in the line give the string ID / 'tag'
int j;
for (j = 0; j < 8 && !Common::isSpace(*ptr); j++, ptr++)
_languageIndex[i].tag[j] = toupper(*ptr);
_languageIndex[i].tag[j] = 0;
// After that follows a single space which we skip
assert(Common::isSpace(*ptr));
ptr++;
// Then comes the translated string: we record an offset to that.
_languageIndex[i].offset = ptr - _languageBuffer;
// Skip over newlines (and turn them into null bytes)
ptr = strpbrk(ptr, "\n\r");
if (ptr == NULL)
break;
while (*ptr == '\n' || *ptr == '\r')
*ptr++ = 0;
// Convert '\n' code to a newline. See also bug #902415.
char *src, *dst;
src = dst = _languageBuffer + _languageIndex[i].offset;
while (*src) {
if (src[0] == '\\' && src[1] == 'n') {
*dst++ = '\n';
src += 2;
} else {
*dst++ = *src++;
}
}
*dst = 0;
}
}
// Sort the index nodes. We'll later use bsearch on it, which is just as efficient
// as using a binary tree, speed wise.
qsort(_languageIndex, _languageIndexSize, sizeof(LangIndexNode), indexCompare);
}
void ScummEngine_v7::playSpeech(const byte *ptr) {
if (_game.id == GID_DIG && (ConfMan.getBool("speech_mute") || VAR(VAR_VOICE_MODE) == 2))
return;
if ((_game.id == GID_DIG || _game.id == GID_CMI) && ptr[0]) {
Common::String pointerStr((const char *)ptr);
// Play speech
if (!(_game.features & GF_DEMO) && (_game.id == GID_CMI)) // CMI demo does not have .IMX for voice
pointerStr += ".IMX";
_sound->stopTalkSound();
_imuseDigital->stopSound(kTalkSoundID);
_imuseDigital->startVoice(kTalkSoundID, pointerStr.c_str());
_sound->talkSound(0, 0, 2);
}
}
void ScummEngine_v7::translateText(const byte *text, byte *trans_buff) {
LangIndexNode target;
LangIndexNode *found = NULL;
int i;
trans_buff[0] = 0;
_lastStringTag[0] = 0;
if (_game.version >= 7 && text[0] == '/') {
// Extract the string tag from the text: /..../
for (i = 0; (i < 12) && (text[i + 1] != '/'); i++)
_lastStringTag[i] = toupper(text[i + 1]);
_lastStringTag[i] = 0;
}
// WORKAROUND for bug #1172655.
if (_game.id == GID_DIG) {
// Based on the second release of The Dig
// Only applies to the subtitles and not speech
if (!strcmp((const char *)text, "faint light"))
text = (const byte *)"/NEW.007/faint light";
else if (!strcmp((const char *)text, "glowing crystal"))
text = (const byte *)"/NEW.008/glowing crystal";
else if (!strcmp((const char *)text, "glowing crystals"))
text = (const byte *)"/NEW.009/glowing crystals";
else if (!strcmp((const char *)text, "pit"))
text = (const byte *)"/NEW.010/pit";
else if (!strcmp((const char *)text, "You wish."))
text = (const byte *)"/NEW.011/You wish.";
else if (!strcmp((const char *)text, "In your dreams."))
text = (const byte *)"/NEW.012/In your dreams";
else if (!strcmp((const char *)text, "left"))
text = (const byte *)"/CATHPLAT.068/left";
else if (!strcmp((const char *)text, "right"))
text = (const byte *)"/CATHPLAT.070/right";
else if (!strcmp((const char *)text, "top"))
text = (const byte *)"/CATHPLAT.067/top";
else if (!strcmp((const char *)text, "exit"))
text = (const byte *)"/SKY.008/exit";
else if (!strcmp((const char *)text, "unattached lens"))
text = (const byte *)"/NEW.013/unattached lens";
else if (!strcmp((const char *)text, "lens slot"))
text = (const byte *)"/NEW.014/lens slot";
else if (!strcmp((const char *)text, "Jonathon Jackson"))
text = (const byte *)"Aram Gutowski";
else if (!strcmp((const char *)text, "Brink"))
text = (const byte *)"/CREVICE.049/Brink";
else if (!strcmp((const char *)text, "Robbins"))
text = (const byte *)"/NEST.061/Robbins";
}
if (_game.version >= 7 && text[0] == '/') {
// Extract the string tag from the text: /..../
for (i = 0; (i < 12) && (text[i + 1] != '/'); i++)
target.tag[i] = toupper(text[i + 1]);
target.tag[i] = 0;
text += i + 2;
// If a language file was loaded, try to find a translated version
// by doing a lookup on the string tag.
if (_existLanguageFile) {
// HACK: These are used for the object line in COMI when
// using one object on another. I don't know if the
// text in the language file is a placeholder or if
// we're supposed to use it, but at least in the
// English version things will work so much better if
// we can't find translations for these.
if (*text && strcmp(target.tag, "PU_M001") != 0 && strcmp(target.tag, "PU_M002") != 0)
found = (LangIndexNode *)bsearch(&target, _languageIndex, _languageIndexSize, sizeof(LangIndexNode), indexCompare);
}
}
if (found != NULL) {
strcpy((char *)trans_buff, _languageBuffer + found->offset);
if ((_game.id == GID_DIG) && !(_game.features & GF_DEMO)) {
// Replace any '%___' by the corresponding special codes in the source text
const byte *src = text;
char *dst = (char *)trans_buff;
while ((dst = strstr(dst, "%___"))) {
// Search for a special code in the message.
while (*src && *src != 0xFF) {
src++;
}
// Replace the %___ by the special code. Luckily, we can do
// that in-place.
if (*src == 0xFF) {
memcpy(dst, src, 4);
src += 4;
dst += 4;
} else
break;
}
}
} else {
// Default: just copy the string
memcpy(trans_buff, text, resStrLen(text) + 1);
}
}
#endif
void ScummEngine::translateText(const byte *text, byte *trans_buff) {
// Default: just copy the string
memcpy(trans_buff, text, resStrLen(text) + 1);
}
} // End of namespace Scumm
|