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 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "common/endian.h"
#include "common/util.h"
#include "common/textconsole.h"
#include "common/translation.h"
#include "sword1/logic.h"
#include "sword1/text.h"
#include "sword1/sound.h"
#include "sword1/eventman.h"
#include "sword1/menu.h"
#include "sword1/router.h"
#include "sword1/screen.h"
#include "sword1/mouse.h"
#include "sword1/sword1.h"
#include "sword1/music.h"
#include "sword1/swordres.h"
#include "sword1/animation.h"
#include "sword1/debug.h"
#include "gui/message.h"
namespace Sword1 {
#define MAX_STACK_SIZE 10
#define SCRIPT_VERSION 13
#define LAST_FRAME 999
uint32 Logic::_scriptVars[NUM_SCRIPT_VARS];
Logic::Logic(SwordEngine *vm, ObjectMan *pObjMan, ResMan *resMan, Screen *pScreen, Mouse *pMouse, Sound *pSound, Music *pMusic, Menu *pMenu, OSystem *system, Audio::Mixer *mixer)
: _rnd("sword1") {
_vm = vm;
_objMan = pObjMan;
_resMan = resMan;
_screen = pScreen;
_mouse = pMouse;
_music = pMusic;
_sound = pSound;
_menu = pMenu;
_textMan = NULL;
_screen->useTextManager(_textMan);
_router = new Router(_objMan, _resMan);
_eventMan = NULL;
_system = system;
_mixer = mixer;
setupMcodeTable();
}
Logic::~Logic() {
delete _textMan;
delete _router;
delete _eventMan;
}
void Logic::initialize() {
memset(_scriptVars, 0, NUM_SCRIPT_VARS * sizeof(uint32));
for (uint8 cnt = 0; cnt < NON_ZERO_SCRIPT_VARS; cnt++)
_scriptVars[_scriptVarInit[cnt][0]] = _scriptVarInit[cnt][1];
if (SwordEngine::_systemVars.isDemo)
_scriptVars[PLAYINGDEMO] = 1;
delete _eventMan;
_eventMan = new EventManager();
delete _textMan;
_textMan = new Text(_objMan, _resMan,
(SwordEngine::_systemVars.language == BS1_CZECH) ? true : false);
_screen->useTextManager(_textMan);
_textRunning = _speechRunning = false;
_speechFinished = true;
}
void Logic::newScreen(uint32 screen) {
Object *compact = (Object *)_objMan->fetchObject(PLAYER);
// work around script bug #911508
if (((screen == 25) || (_scriptVars[SCREEN] == 25)) && (_scriptVars[SAND_FLAG] == 4)) {
Object *cpt = _objMan->fetchObject(SAND_25);
Object *george = _objMan->fetchObject(PLAYER);
if (george->o_place == HOLDING_REPLICA_25) // is george holding the replica in his hands?
fnFullSetFrame(cpt, SAND_25, IMPFLRCDT, IMPFLR, 0, 0, 0, 0); // empty impression in floor
else
fnFullSetFrame(cpt, SAND_25, IMPPLSCDT, IMPPLS, 0, 0, 0, 0); // impression filled with plaster
}
// work around, at screen 69 in psx version TOP menu gets stuck at disabled, fix it at next screen (71)
if ((screen == 71) && (SwordEngine::isPsx()))
_scriptVars[TOP_MENU_DISABLED] = 0;
if (SwordEngine::_systemVars.justRestoredGame) { // if we've just restored a game - we want George to be exactly as saved
fnAddHuman(NULL, 0, 0, 0, 0, 0, 0, 0);
if (_scriptVars[GEORGE_WALKING]) { // except that if George was walking when we saveed the game
fnStandAt(compact, PLAYER, _scriptVars[CHANGE_X], _scriptVars[CHANGE_Y], _scriptVars[CHANGE_DIR], _scriptVars[CHANGE_STANCE], 0, 0);
fnIdle(compact, PLAYER, 0, 0, 0, 0, 0, 0);
_scriptVars[GEORGE_WALKING] = 0;
}
SwordEngine::_systemVars.justRestoredGame = 0;
_music->startMusic(_scriptVars[CURRENT_MUSIC], 1);
} else { // if we haven't just restored a game, set George to stand, etc
compact->o_screen = _scriptVars[NEW_SCREEN]; //move the mega/player at this point between screens
fnStandAt(compact, PLAYER, _scriptVars[CHANGE_X], _scriptVars[CHANGE_Y], _scriptVars[CHANGE_DIR], _scriptVars[CHANGE_STANCE], 0, 0);
fnChangeFloor(compact, PLAYER, _scriptVars[CHANGE_PLACE], 0, 0, 0, 0, 0);
}
}
void Logic::engine() {
debug(8, "\n\nNext logic cycle");
_eventMan->serviceGlobalEventList();
for (uint16 sectCnt = 0; sectCnt < TOTAL_SECTIONS; sectCnt++) {
if (_objMan->sectionAlive(sectCnt)) {
uint32 numCpts = _objMan->fetchNoObjects(sectCnt);
for (uint32 cptCnt = 0; cptCnt < numCpts; cptCnt++) {
uint32 currentId = sectCnt * ITM_PER_SEC + cptCnt;
Object *compact = _objMan->fetchObject(currentId);
if (compact->o_status & STAT_LOGIC) { // does the object want to be processed?
if (compact->o_status & STAT_EVENTS) {
//subscribed to the global-event-switcher? and in logic mode
switch (compact->o_logic) {
case LOGIC_pause_for_event:
case LOGIC_idle:
case LOGIC_AR_animate:
_eventMan->checkForEvent(compact);
break;
}
}
debug(7, "Logic::engine: handling compact %d (%X)", currentId, currentId);
processLogic(compact, currentId);
compact->o_sync = 0; // syncs are only available for 1 cycle.
}
if ((uint32)compact->o_screen == _scriptVars[SCREEN]) {
if (compact->o_status & STAT_FORE)
_screen->addToGraphicList(0, currentId);
if (compact->o_status & STAT_SORT)
_screen->addToGraphicList(1, currentId);
if (compact->o_status & STAT_BACK)
_screen->addToGraphicList(2, currentId);
if (compact->o_status & STAT_MOUSE)
_mouse->addToList(currentId, compact);
}
}
}
}
//_collision->checkCollisions();
}
void Logic::processLogic(Object *compact, uint32 id) {
int logicRet;
do {
switch (compact->o_logic) {
case LOGIC_idle:
logicRet = 0;
break;
case LOGIC_pause:
case LOGIC_pause_for_event:
if (compact->o_pause) {
compact->o_pause--;
logicRet = 0;
} else {
compact->o_logic = LOGIC_script;
logicRet = 1;
}
break;
case LOGIC_quit:
compact->o_logic = LOGIC_script;
logicRet = 0;
break;
case LOGIC_wait_for_sync:
if (compact->o_sync) {
logicRet = 1;
compact->o_logic = LOGIC_script;
} else
logicRet = 0;
break;
case LOGIC_choose:
_scriptVars[CUR_ID] = id;
logicRet = _menu->logicChooser(compact);
break;
case LOGIC_wait_for_talk:
logicRet = logicWaitTalk(compact);
break;
case LOGIC_start_talk:
logicRet = logicStartTalk(compact);
break;
case LOGIC_script:
_scriptVars[CUR_ID] = id;
logicRet = scriptManager(compact, id);
break;
case LOGIC_new_script:
compact->o_tree.o_script_pc[compact->o_tree.o_script_level] = _newScript;
compact->o_tree.o_script_id[compact->o_tree.o_script_level] = _newScript;
compact->o_logic = LOGIC_script;
logicRet = 1;
break;
case LOGIC_AR_animate:
logicRet = logicArAnimate(compact, id);
break;
case LOGIC_restart:
compact->o_tree.o_script_pc[compact->o_tree.o_script_level] = compact->o_tree.o_script_id[compact->o_tree.o_script_level];
compact->o_logic = LOGIC_script;
logicRet = 1;
break;
case LOGIC_bookmark:
memcpy(&(compact->o_tree.o_script_level), &(compact->o_bookmark.o_script_level), sizeof(ScriptTree));
if (id == GMASTER_79) {
// workaround for ending script.
// GMASTER_79 is not prepared for mega_interact receiving INS_quit
fnSuicide(compact, id, 0, 0, 0, 0, 0, 0);
logicRet = 0;
} else {
compact->o_logic = LOGIC_script;
logicRet = 1;
}
break;
case LOGIC_speech:
logicRet = speechDriver(compact);
break;
case LOGIC_full_anim:
logicRet = fullAnimDriver(compact);
break;
case LOGIC_anim:
logicRet = animDriver(compact);
break;
default:
error("Fatal error: compact %d's logic == %X", id, compact->o_logic);
break;
}
} while (logicRet);
}
int Logic::logicWaitTalk(Object *compact) {
Object *target = _objMan->fetchObject(compact->o_down_flag);
if (target->o_status & STAT_TALK_WAIT) {
compact->o_logic = LOGIC_script;
return 1;
} else {
return 0;
}
}
int Logic::logicStartTalk(Object *compact) {
Object *target = _objMan->fetchObject(compact->o_down_flag); //holds id of person we're waiting for
if (target->o_status & STAT_TALK_WAIT) { //response?
compact->o_logic = LOGIC_script; //back to script again
return SCRIPT_CONT;
}
if (_eventMan->eventValid(compact->o_down_flag))
return SCRIPT_STOP; //event still valid - keep waiting
//the event has gone - so back to script with error code
compact->o_down_flag = 0;
compact->o_logic = LOGIC_script;
return SCRIPT_CONT;
}
int Logic::logicArAnimate(Object *compact, uint32 id) {
WalkData *route;
int32 walkPc;
if ((_scriptVars[GEORGE_WALKING] == 0) && (id == PLAYER))
_scriptVars[GEORGE_WALKING] = 1;
compact->o_resource = compact->o_walk_resource;
compact->o_status |= STAT_SHRINK;
route = compact->o_route;
walkPc = compact->o_walk_pc;
compact->o_frame = route[walkPc].frame;
compact->o_dir = route[walkPc].dir;
compact->o_xcoord = route[walkPc].x;
compact->o_ycoord = route[walkPc].y;
compact->o_anim_x = compact->o_xcoord;
compact->o_anim_y = compact->o_ycoord;
if (((_scriptVars[GEORGE_WALKING] == 2) && (walkPc > 5) && (id == PLAYER) &&
(route[walkPc - 1].step == 5) && (route[walkPc].step == 0)) ||
((_scriptVars[GEORGE_WALKING] == 3) && (id == PLAYER))) {
compact->o_frame = 96 + compact->o_dir; //reset
if ((compact->o_dir != 2) && (compact->o_dir != 6)) { // on verticals and diagonals stand where george is
compact->o_xcoord = route[walkPc - 1].x;
compact->o_ycoord = route[walkPc - 1].y;
compact->o_anim_x = compact->o_xcoord;
compact->o_anim_y = compact->o_ycoord;
}
compact->o_logic = LOGIC_script;
compact->o_down_flag = 0; //0 means error
_scriptVars[GEORGE_WALKING] = 0;
route[compact->o_walk_pc + 1].frame = 512; //end of sequence
if (_scriptVars[MEGA_ON_GRID] == 2)
_scriptVars[MEGA_ON_GRID] = 0;
}
compact->o_walk_pc++;
if (route[compact->o_walk_pc].frame == 512) { //end of sequence
compact->o_logic = LOGIC_script;
if (((_scriptVars[GEORGE_WALKING] == 2) || (_scriptVars[GEORGE_WALKING] == 1)) &&
(id == PLAYER)) {
_scriptVars[GEORGE_WALKING] = 0;
if (_scriptVars[MEGA_ON_GRID] == 2)
_scriptVars[MEGA_ON_GRID] = 0;
}
}
return 0;
}
int Logic::speechDriver(Object *compact) {
if ((!_speechClickDelay) && (_mouse->testEvent() & BS1L_BUTTON_DOWN))
_speechFinished = true;
if (_speechClickDelay)
_speechClickDelay--;
if (_speechRunning) {
if (_sound->speechFinished())
_speechFinished = true;
} else {
if (!compact->o_speech_time)
_speechFinished = true;
else
compact->o_speech_time--;
}
if (_speechFinished) {
if (_speechRunning)
_sound->stopSpeech();
compact->o_logic = LOGIC_script;
if (_textRunning) {
_textMan->releaseText(compact->o_text_id);
_objMan->fetchObject(compact->o_text_id)->o_status = 0; // kill compact linking text sprite
}
_speechRunning = _textRunning = false;
_speechFinished = true;
}
if (compact->o_anim_resource) {
uint8 *animData = ((uint8 *)_resMan->openFetchRes(compact->o_anim_resource)) + sizeof(Header);
int32 numFrames = _resMan->readUint32(animData);
animData += 4;
compact->o_anim_pc++; // go to next frame of anim
if (_speechFinished || (compact->o_anim_pc >= numFrames) ||
(_speechRunning && (_sound->amISpeaking() == 0)))
compact->o_anim_pc = 0; //set to frame 0, closed mouth
AnimUnit *animPtr = (AnimUnit *)(animData + sizeof(AnimUnit) * compact->o_anim_pc);
if (!(compact->o_status & STAT_SHRINK)) {
compact->o_anim_x = _resMan->getUint32(animPtr->animX);
compact->o_anim_y = _resMan->getUint32(animPtr->animY);
}
compact->o_frame = _resMan->getUint32(animPtr->animFrame);
_resMan->resClose(compact->o_anim_resource);
}
return 0;
}
int Logic::fullAnimDriver(Object *compact) {
if (compact->o_sync) { // return to script immediately if we've received a sync
compact->o_logic = LOGIC_script;
return 1;
}
uint8 *data = ((uint8 *)_resMan->openFetchRes(compact->o_anim_resource)) + sizeof(Header);
uint32 numFrames = _resMan->readUint32(data);
data += 4;
AnimUnit *animPtr = (AnimUnit *)(data + compact->o_anim_pc * sizeof(AnimUnit));
compact->o_anim_x = compact->o_xcoord = _resMan->getUint32(animPtr->animX);
compact->o_anim_y = compact->o_ycoord = _resMan->getUint32(animPtr->animY);
compact->o_frame = _resMan->getUint32(animPtr->animFrame);
compact->o_anim_pc++;
if (compact->o_anim_pc == (int)numFrames)
compact->o_logic = LOGIC_script;
_resMan->resClose(compact->o_anim_resource);
return 0;
}
int Logic::animDriver(Object *compact) {
if (compact->o_sync) {
compact->o_logic = LOGIC_script;
return 1;
}
uint8 *data = ((uint8 *)_resMan->openFetchRes(compact->o_anim_resource)) + sizeof(Header);
uint32 numFrames = _resMan->readUint32(data);
AnimUnit *animPtr = (AnimUnit *)(data + 4 + compact->o_anim_pc * sizeof(AnimUnit));
if (!(compact->o_status & STAT_SHRINK)) {
compact->o_anim_x = _resMan->getUint32(animPtr->animX);
compact->o_anim_y = _resMan->getUint32(animPtr->animY);
}
compact->o_frame = _resMan->getUint32(animPtr->animFrame);
compact->o_anim_pc++;
if (compact->o_anim_pc == (int)numFrames)
compact->o_logic = LOGIC_script;
_resMan->resClose(compact->o_anim_resource);
return 0;
}
void Logic::updateScreenParams() {
Object *compact = (Object *)_objMan->fetchObject(PLAYER);
_screen->setScrolling((int16)(compact->o_xcoord - _scriptVars[FEET_X]),
(int16)(compact->o_ycoord - _scriptVars[FEET_Y]));
}
int Logic::scriptManager(Object *compact, uint32 id) {
int ret;
do {
uint32 level = compact->o_tree.o_script_level;
uint32 script = compact->o_tree.o_script_id[level];
Debug::interpretScript(id, level, script, compact->o_tree.o_script_pc[level] & ITM_ID);
ret = interpretScript(compact, id, _resMan->lockScript(script), script, compact->o_tree.o_script_pc[level] & ITM_ID);
_resMan->unlockScript(script);
if (!ret) {
if (compact->o_tree.o_script_level)
compact->o_tree.o_script_level--;
else
error("ScriptManager: basescript %d for cpt %d ended", script, id);
} else
compact->o_tree.o_script_pc[level] = ret;
} while (!ret);
return 1;
//Logic continues - but the script must have changed logic mode
//this is a radical change from S2.0 where once a script finished there
//was no more processing for that object on that cycle - the Logic_engine terminated.
//This meant that new logics that needed immediate action got a first call from the
//setup function. This was a bit tweeky. This technique ensures that the script is a
//totally seamless concept that takes up zero cycle time. The only downside is that
//an FN_quit becomes slightly more convoluted, but so what you might ask.
}
void Logic::runMouseScript(Object *cpt, int32 scriptId) {
Header *script = _resMan->lockScript(scriptId);
debug(9, "running mouse script %d", scriptId);
interpretScript(cpt, _scriptVars[SPECIAL_ITEM], script, scriptId, scriptId);
_resMan->unlockScript(scriptId);
}
int Logic::interpretScript(Object *compact, int id, Header *scriptModule, int scriptBase, int scriptNum) {
int32 *scriptCode = (int32 *)(((uint8 *)scriptModule) + sizeof(Header));
int32 stack[MAX_STACK_SIZE];
int32 stackIdx = 0;
int32 offset;
int32 pc;
if (memcmp(scriptModule->type, "Script", 6))
error("Invalid script module");
if (scriptModule->version != SCRIPT_VERSION)
error("Illegal script version");
if (scriptNum < 0)
error("negative script number");
if ((uint32)scriptNum >= scriptModule->decomp_length)
error("Script number out of bounds");
if (scriptNum < scriptCode[0])
pc = scriptCode[scriptNum + 1];
else
pc = scriptNum;
int32 startOfScript = scriptCode[(scriptBase & ITM_ID) + 1];
int32 a, b, c, d, e, f;
int mCodeReturn = 0;
int32 mCodeNumber = 0, mCodeArguments = 0;
uint32 varNum = 0;
while (1) {
assert((stackIdx >= 0) && (stackIdx <= MAX_STACK_SIZE));
switch (scriptCode[pc++]) {
case IT_MCODE:
a = b = c = d = e = f = 0;
mCodeNumber = scriptCode[pc++];
mCodeArguments = scriptCode[pc++];
switch (mCodeArguments) {
case 6: f = stack[--stackIdx]; // fall through
case 5: e = stack[--stackIdx]; // fall through
case 4: d = stack[--stackIdx]; // fall through
case 3: c = stack[--stackIdx]; // fall through
case 2: b = stack[--stackIdx]; // fall through
case 1: a = stack[--stackIdx]; // fall through
case 0:
Debug::callMCode(mCodeNumber, mCodeArguments, a, b, c, d, e, f);
mCodeReturn = (this->*_mcodeTable[mCodeNumber])(compact, id, a, b, c, d, e, f);
break;
default:
warning("mcode[%d]: too many arguments(%d)", mCodeNumber, mCodeArguments);
}
if (mCodeReturn == 0)
return pc;
break;
case IT_PUSHNUMBER:
debug(9, "IT_PUSH: %d", scriptCode[pc]);
stack[stackIdx++] = scriptCode[pc++];
break;
case IT_PUSHVARIABLE:
debug(9, "IT_PUSHVARIABLE: ScriptVar[%d] => %d", scriptCode[pc], _scriptVars[scriptCode[pc]]);
varNum = scriptCode[pc++];
if (SwordEngine::_systemVars.isDemo && SwordEngine::isWindows()) {
if (varNum >= 397) // BS1 Demo has different number of script variables
varNum++;
if (varNum >= 699)
varNum++;
}
stack[stackIdx++] = _scriptVars[varNum];
break;
case IT_NOTEQUAL:
stackIdx--;
debug(9, "IT_NOTEQUAL: RESULT = %d", stack[stackIdx - 1] != stack[stackIdx]);
stack[stackIdx - 1] = (stack[stackIdx - 1] != stack[stackIdx]);
break;
case IT_ISEQUAL:
stackIdx--;
debug(9, "IT_ISEQUAL: RESULT = %d", stack[stackIdx - 1] == stack[stackIdx]);
stack[stackIdx - 1] = (stack[stackIdx - 1] == stack[stackIdx]);
break;
case IT_PLUS:
stackIdx--;
debug(9, "IT_PLUS: RESULT = %d", stack[stackIdx - 1] + stack[stackIdx]);
stack[stackIdx - 1] = (stack[stackIdx - 1] + stack[stackIdx]);
break;
case IT_TIMES:
stackIdx--;
debug(9, "IT_TIMES: RESULT = %d", stack[stackIdx - 1] * stack[stackIdx]);
stack[stackIdx - 1] = (stack[stackIdx - 1] * stack[stackIdx]);
break;
case IT_ANDAND:
stackIdx--;
debug(9, "IT_ANDAND: RESULT = %d", stack[stackIdx - 1] && stack[stackIdx]);
stack[stackIdx - 1] = (stack[stackIdx - 1] && stack[stackIdx]);
break;
case IT_OROR: // ||
stackIdx--;
debug(9, "IT_OROR: RESULT = %d", stack[stackIdx - 1] || stack[stackIdx]);
stack[stackIdx - 1] = (stack[stackIdx - 1] || stack[stackIdx]);
break;
case IT_LESSTHAN:
stackIdx--;
debug(9, "IT_LESSTHAN: RESULT = %d", stack[stackIdx - 1] < stack[stackIdx]);
stack[stackIdx - 1] = (stack[stackIdx - 1] < stack[stackIdx]);
break;
case IT_NOT:
debug(9, "IT_NOT: RESULT = %d", stack[stackIdx - 1] ? 0 : 1);
if (stack[stackIdx - 1])
stack[stackIdx - 1] = 0;
else
stack[stackIdx - 1] = 1;
break;
case IT_MINUS:
stackIdx--;
debug(9, "IT_MINUS: RESULT = %d", stack[stackIdx - 1] - stack[stackIdx]);
stack[stackIdx - 1] = (stack[stackIdx - 1] - stack[stackIdx]);
break;
case IT_AND:
stackIdx--;
debug(9, "IT_AND: RESULT = %d", stack[stackIdx - 1] & stack[stackIdx]);
stack[stackIdx - 1] = (stack[stackIdx - 1] & stack[stackIdx]);
break;
case IT_OR:
stackIdx--;
debug(9, "IT_OR: RESULT = %d", stack[stackIdx - 1] | stack[stackIdx]);
stack[stackIdx - 1] = (stack[stackIdx - 1] | stack[stackIdx]);
break;
case IT_GTE:
stackIdx--;
debug(9, "IT_GTE: RESULT = %d", stack[stackIdx - 1] >= stack[stackIdx]);
stack[stackIdx - 1] = (stack[stackIdx - 1] >= stack[stackIdx]);
break;
case IT_LTE:
stackIdx--;
debug(9, "IT_LTE: RESULT = %d", stack[stackIdx - 1] <= stack[stackIdx]);
stack[stackIdx - 1] = (stack[stackIdx - 1] <= stack[stackIdx]);
break;
case IT_DEVIDE:
stackIdx--;
debug(9, "IT_DEVIDE: RESULT = %d", stack[stackIdx - 1] / stack[stackIdx]);
stack[stackIdx - 1] = (stack[stackIdx - 1] / stack[stackIdx]);
break;
case IT_GT:
stackIdx--;
debug(9, "IT_GT: RESULT = %d", stack[stackIdx - 1] > stack[stackIdx]);
stack[stackIdx - 1] = (stack[stackIdx - 1] > stack[stackIdx]);
break;
case IT_SCRIPTEND:
debug(9, "IT_SCRIPTEND");
return 0;
case IT_POPVAR: // pop a variable
debug(9, "IT_POPVAR: ScriptVars[%d] = %d", scriptCode[pc], stack[stackIdx - 1]);
varNum = scriptCode[pc++];
if (SwordEngine::_systemVars.isDemo && SwordEngine::isWindows()) {
if (varNum >= 397) // BS1 Demo has different number of script variables
varNum++;
if (varNum >= 699)
varNum++;
}
_scriptVars[varNum] = stack[--stackIdx];
break;
case IT_POPLONGOFFSET:
offset = scriptCode[pc++];
debug(9, "IT_POPLONGOFFSET: Cpt[%d] = %d", offset, stack[stackIdx - 1]);
*((int32 *)((uint8 *)compact + offset)) = stack[--stackIdx];
break;
case IT_PUSHLONGOFFSET:
offset = scriptCode[pc++];
debug(9, "IT_PUSHLONGOFFSET: PUSH Cpt[%d] (==%d)", offset, *((int32 *)((uint8 *)compact + offset)));
stack[stackIdx++] = *((int32 *)((uint8 *)compact + offset));
break;
case IT_SKIPONFALSE:
debug(9, "IT_SKIPONFALSE: %d (%s)", scriptCode[pc], (stack[stackIdx - 1] ? "IS TRUE (NOT SKIPPED)" : "IS FALSE (SKIPPED)"));
if (stack[--stackIdx])
pc++;
else
pc += scriptCode[pc];
break;
case IT_SKIP:
debug(9, "IT_SKIP: %d", scriptCode[pc]);
pc += scriptCode[pc];
break;
case IT_SWITCH: // The mega switch statement
debug(9, "IT_SWITCH: [SORRY, NO DEBUG INFO]");
{
int switchValue = stack[--stackIdx];
int switchCount = scriptCode[pc++];
int doneSwitch = 0;
for (int cnt = 0; (cnt < switchCount) && (doneSwitch == 0); cnt++) {
if (switchValue == scriptCode[pc]) {
pc += scriptCode[pc + 1];
doneSwitch = 1;
} else
pc += 2;
}
if (doneSwitch == 0)
pc += scriptCode[pc];
}
break;
case IT_SKIPONTRUE: // skip if expression true
debug(9, "IT_SKIPONTRUE: %d (%s)", scriptCode[pc], (stack[stackIdx - 1] ? "IS TRUE (SKIPPED)" : "IS FALSE (NOT SKIPPED)"));
stackIdx--;
if (stack[stackIdx])
pc += scriptCode[pc];
else
pc++;
break;
case IT_PRINTF:
debug(0, "IT_PRINTF(%d)", stack[stackIdx]);
break;
case IT_RESTARTSCRIPT:
debug(9, "IT_RESTARTSCRIPT");
pc = startOfScript;
break;
case IT_POPWORDOFFSET:
offset = scriptCode[pc++];
debug(9, "IT_POPWORDOFFSET: Cpt[%d] = %d", offset, stack[stackIdx - 1] & 0xFFFF);
*((int32 *)((uint8 *)compact + offset)) = stack[--stackIdx] & 0xffff;
break;
case IT_PUSHWORDOFFSET:
offset = scriptCode[pc++];
debug(9, "IT_PUSHWORDOFFSET: PUSH Cpt[%d] == %d", offset, (*((int32 *)((uint8 *)compact + offset))) & 0xffff);
stack[stackIdx++] = (*((int32 *)((uint8 *)compact + offset))) & 0xffff;
break;
default:
error("Invalid operator %d", scriptCode[pc - 1]);
return 0; // for compilers that don't support NORETURN
}
}
}
void Logic::setupMcodeTable() {
static const BSMcodeTable mcodeTable[100] = {
&Logic::fnBackground,
&Logic::fnForeground,
&Logic::fnSort,
&Logic::fnNoSprite,
&Logic::fnMegaSet,
&Logic::fnAnim,
&Logic::fnSetFrame,
&Logic::fnFullAnim,
&Logic::fnFullSetFrame,
&Logic::fnFadeDown,
&Logic::fnFadeUp,
&Logic::fnCheckFade,
&Logic::fnSetSpritePalette,
&Logic::fnSetWholePalette,
&Logic::fnSetFadeTargetPalette,
&Logic::fnSetPaletteToFade,
&Logic::fnSetPaletteToCut,
&Logic::fnPlaySequence,
&Logic::fnIdle,
&Logic::fnPause,
&Logic::fnPauseSeconds,
&Logic::fnQuit,
&Logic::fnKillId,
&Logic::fnSuicide,
&Logic::fnNewScript,
&Logic::fnSubScript,
&Logic::fnRestartScript,
&Logic::fnSetBookmark,
&Logic::fnGotoBookmark,
&Logic::fnSendSync,
&Logic::fnWaitSync,
&Logic::cfnClickInteract,
&Logic::cfnSetScript,
&Logic::cfnPresetScript,
&Logic::fnInteract,
&Logic::fnIssueEvent,
&Logic::fnCheckForEvent,
&Logic::fnWipeHands,
&Logic::fnISpeak,
&Logic::fnTheyDo,
&Logic::fnTheyDoWeWait,
&Logic::fnWeWait,
&Logic::fnChangeSpeechText,
&Logic::fnTalkError,
&Logic::fnStartTalk,
&Logic::fnCheckForTextLine,
&Logic::fnAddTalkWaitStatusBit,
&Logic::fnRemoveTalkWaitStatusBit,
&Logic::fnNoHuman,
&Logic::fnAddHuman,
&Logic::fnBlankMouse,
&Logic::fnNormalMouse,
&Logic::fnLockMouse,
&Logic::fnUnlockMouse,
&Logic::fnSetMousePointer,
&Logic::fnSetMouseLuggage,
&Logic::fnMouseOn,
&Logic::fnMouseOff,
&Logic::fnChooser,
&Logic::fnEndChooser,
&Logic::fnStartMenu,
&Logic::fnEndMenu,
&Logic::cfnReleaseMenu,
&Logic::fnAddSubject,
&Logic::fnAddObject,
&Logic::fnRemoveObject,
&Logic::fnEnterSection,
&Logic::fnLeaveSection,
&Logic::fnChangeFloor,
&Logic::fnWalk,
&Logic::fnTurn,
&Logic::fnStand,
&Logic::fnStandAt,
&Logic::fnFace,
&Logic::fnFaceXy,
&Logic::fnIsFacing,
&Logic::fnGetTo,
&Logic::fnGetToError,
&Logic::fnGetPos,
&Logic::fnGetGamepadXy,
&Logic::fnPlayFx,
&Logic::fnStopFx,
&Logic::fnPlayMusic,
&Logic::fnStopMusic,
&Logic::fnInnerSpace,
&Logic::fnRandom,
&Logic::fnSetScreen,
&Logic::fnPreload,
&Logic::fnCheckCD,
&Logic::fnRestartGame,
&Logic::fnQuitGame,
&Logic::fnDeathScreen,
&Logic::fnSetParallax,
&Logic::fnTdebug,
&Logic::fnRedFlash,
&Logic::fnBlueFlash,
&Logic::fnYellow,
&Logic::fnGreen,
&Logic::fnPurple,
&Logic::fnBlack
};
_mcodeTable = mcodeTable;
}
int Logic::fnBackground(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
cpt->o_status &= ~(STAT_FORE | STAT_SORT);
cpt->o_status |= STAT_BACK;
return SCRIPT_CONT;
}
int Logic::fnForeground(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
cpt->o_status &= ~(STAT_BACK | STAT_SORT);
cpt->o_status |= STAT_FORE;
return SCRIPT_CONT;
}
int Logic::fnSort(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
cpt->o_status &= ~(STAT_BACK | STAT_FORE);
cpt->o_status |= STAT_SORT;
return SCRIPT_CONT;
}
int Logic::fnNoSprite(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
cpt->o_status &= ~(STAT_BACK | STAT_FORE | STAT_SORT);
return SCRIPT_CONT;
}
int Logic::fnMegaSet(Object *cpt, int32 id, int32 walk_data, int32 spr, int32 e, int32 f, int32 z, int32 x) {
cpt->o_mega_resource = walk_data;
cpt->o_walk_resource = spr;
return SCRIPT_CONT;
}
int Logic::fnAnim(Object *cpt, int32 id, int32 cdt, int32 spr, int32 e, int32 f, int32 z, int32 x) {
AnimSet *animTab;
if (cdt && (!spr)) {
animTab = (AnimSet *)((uint8 *)_resMan->openFetchRes(cdt) + sizeof(Header));
animTab += cpt->o_dir;
cpt->o_anim_resource = _resMan->getUint32(animTab->cdt);
cpt->o_resource = _resMan->getUint32(animTab->spr);
_resMan->resClose(cdt);
} else {
cpt->o_anim_resource = cdt;
cpt->o_resource = spr;
}
if ((cpt->o_anim_resource == 0) || (cpt->o_resource == 0))
error("fnAnim called width (%d/%d) => (%d/%d)", cdt, spr, cpt->o_anim_resource, cpt->o_resource);
FrameHeader *frameHead = _resMan->fetchFrame(_resMan->openFetchRes(cpt->o_resource), 0);
if (frameHead->offsetX || frameHead->offsetY) { // boxed mega anim?
cpt->o_status |= STAT_SHRINK;
cpt->o_anim_x = cpt->o_xcoord; // set anim coords to 'feet' coords - only need to do this once
cpt->o_anim_y = cpt->o_ycoord;
} else {
// Anim_driver sets anim coords to cdt coords for every frame of a loose anim
cpt->o_status &= ~STAT_SHRINK;
}
_resMan->resClose(cpt->o_resource);
cpt->o_logic = LOGIC_anim;
cpt->o_anim_pc = 0;
cpt->o_sync = 0;
return SCRIPT_STOP;
}
int Logic::fnSetFrame(Object *cpt, int32 id, int32 cdt, int32 spr, int32 frameNo, int32 f, int32 z, int32 x) {
AnimUnit *animPtr;
uint8 *data = (uint8 *)_resMan->openFetchRes(cdt);
data += sizeof(Header);
if (frameNo == LAST_FRAME)
frameNo = _resMan->readUint32(data) - 1;
data += 4;
animPtr = (AnimUnit *)(data + frameNo * sizeof(AnimUnit));
cpt->o_anim_x = _resMan->getUint32(animPtr->animX);
cpt->o_anim_y = _resMan->getUint32(animPtr->animY);
cpt->o_frame = _resMan->getUint32(animPtr->animFrame);
cpt->o_resource = spr;
cpt->o_status &= ~STAT_SHRINK;
_resMan->resClose(cdt);
return SCRIPT_CONT;
}
int Logic::fnFullAnim(Object *cpt, int32 id, int32 anim, int32 graphic, int32 e, int32 f, int32 z, int32 x) {
cpt->o_logic = LOGIC_full_anim;
cpt->o_anim_pc = 0;
cpt->o_anim_resource = anim;
cpt->o_resource = graphic;
cpt->o_status &= ~STAT_SHRINK;
cpt->o_sync = 0;
return SCRIPT_STOP;
}
int Logic::fnFullSetFrame(Object *cpt, int32 id, int32 cdt, int32 spr, int32 frameNo, int32 f, int32 z, int32 x) {
uint8 *data = (uint8 *)_resMan->openFetchRes(cdt) + sizeof(Header);
if (frameNo == LAST_FRAME)
frameNo = _resMan->readUint32(data) - 1;
data += 4;
AnimUnit *animPtr = (AnimUnit *)(data + sizeof(AnimUnit) * frameNo);
cpt->o_anim_x = cpt->o_xcoord = _resMan->getUint32(animPtr->animX);
cpt->o_anim_y = cpt->o_ycoord = _resMan->getUint32(animPtr->animY);
cpt->o_frame = _resMan->getUint32(animPtr->animFrame);
cpt->o_resource = spr;
cpt->o_status &= ~STAT_SHRINK;
_resMan->resClose(cdt);
return SCRIPT_CONT;
}
int Logic::fnFadeDown(Object *cpt, int32 id, int32 speed, int32 d, int32 e, int32 f, int32 z, int32 x) {
_screen->fadeDownPalette();
return SCRIPT_CONT;
}
int Logic::fnFadeUp(Object *cpt, int32 id, int32 speed, int32 d, int32 e, int32 f, int32 z, int32 x) {
_screen->fadeUpPalette();
return SCRIPT_CONT;
}
int Logic::fnCheckFade(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
_scriptVars[RETURN_VALUE] = (uint8)_screen->stillFading();
return SCRIPT_CONT;
}
int Logic::fnSetSpritePalette(Object *cpt, int32 id, int32 spritePal, int32 d, int32 e, int32 f, int32 z, int32 x) {
_screen->fnSetPalette(184, 72, spritePal, false);
return SCRIPT_CONT;
}
int Logic::fnSetWholePalette(Object *cpt, int32 id, int32 spritePal, int32 d, int32 e, int32 f, int32 z, int32 x) {
_screen->fnSetPalette(0, 256, spritePal, false);
return SCRIPT_CONT;
}
int Logic::fnSetFadeTargetPalette(Object *cpt, int32 id, int32 spritePal, int32 d, int32 e, int32 f, int32 z, int32 x) {
_screen->fnSetPalette(0, 184, spritePal, true);
return SCRIPT_CONT;
}
int Logic::fnSetPaletteToFade(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
SwordEngine::_systemVars.wantFade = true;
return SCRIPT_CONT;
}
int Logic::fnSetPaletteToCut(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
SwordEngine::_systemVars.wantFade = false;
return SCRIPT_CONT;
}
int Logic::fnPlaySequence(Object *cpt, int32 id, int32 sequenceId, int32 d, int32 e, int32 f, int32 z, int32 x) {
// A cutscene usually (always?) means the room will change. In the
// meantime, we don't want any looping sound effects still playing.
_sound->quitScreen();
MoviePlayer *player = makeMoviePlayer(sequenceId, _vm, _textMan, _resMan, _system);
if (player) {
_screen->clearScreen();
if (player->load(sequenceId))
player->play();
delete player;
}
return SCRIPT_CONT;
}
int Logic::fnIdle(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
cpt->o_tree.o_script_level = 0; // force to level 0
cpt->o_logic = LOGIC_idle;
return SCRIPT_STOP;
}
int Logic::fnPause(Object *cpt, int32 id, int32 pause, int32 d, int32 e, int32 f, int32 z, int32 x) {
cpt->o_pause = pause;
cpt->o_logic = LOGIC_pause;
return SCRIPT_STOP;
}
int Logic::fnPauseSeconds(Object *cpt, int32 id, int32 pause, int32 d, int32 e, int32 f, int32 z, int32 x) {
cpt->o_pause = pause * FRAME_RATE;
cpt->o_logic = LOGIC_pause;
return SCRIPT_STOP;
}
int Logic::fnQuit(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
cpt->o_logic = LOGIC_quit;
return SCRIPT_STOP;
}
int Logic::fnKillId(Object *cpt, int32 id, int32 target, int32 d, int32 e, int32 f, int32 z, int32 x) {
Object *targetObj = _objMan->fetchObject(target);
targetObj->o_status = 0;
return SCRIPT_CONT;
}
int Logic::fnSuicide(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
cpt->o_status = 0;
cpt->o_logic = LOGIC_quit;
return SCRIPT_STOP;
}
int Logic::fnNewScript(Object *cpt, int32 id, int32 script, int32 d, int32 e, int32 f, int32 z, int32 x) {
cpt->o_logic = LOGIC_new_script;
_newScript = script;
return SCRIPT_STOP;
}
int Logic::fnSubScript(Object *cpt, int32 id, int32 script, int32 d, int32 e, int32 f, int32 z, int32 x) {
cpt->o_tree.o_script_level++;
if (cpt->o_tree.o_script_level == TOTAL_script_levels)
error("Compact %d: script level exceeded in fnSubScript", id);
cpt->o_tree.o_script_pc[cpt->o_tree.o_script_level] = script;
cpt->o_tree.o_script_id[cpt->o_tree.o_script_level] = script;
return SCRIPT_STOP;
}
int Logic::fnRestartScript(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
cpt->o_logic = LOGIC_restart;
return SCRIPT_STOP;
}
int Logic::fnSetBookmark(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
memcpy(&cpt->o_bookmark.o_script_level, &cpt->o_tree.o_script_level, sizeof(ScriptTree));
return SCRIPT_CONT;
}
int Logic::fnGotoBookmark(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
cpt->o_logic = LOGIC_bookmark;
return SCRIPT_STOP;
}
int Logic::fnSendSync(Object *cpt, int32 id, int32 sendId, int32 syncValue, int32 e, int32 f, int32 z, int32 x) {
Object *target = _objMan->fetchObject(sendId);
target->o_sync = syncValue;
return SCRIPT_CONT;
}
int Logic::fnWaitSync(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
cpt->o_logic = LOGIC_wait_for_sync;
return SCRIPT_STOP;
}
int Logic::cfnClickInteract(Object *cpt, int32 id, int32 target, int32 d, int32 e, int32 f, int32 z, int32 x) {
Object *tar = _objMan->fetchObject(target);
cpt = _objMan->fetchObject(PLAYER);
cpt->o_tree.o_script_level = 0;
cpt->o_tree.o_script_pc[0] = tar->o_interact;
cpt->o_tree.o_script_id[0] = tar->o_interact;
cpt->o_logic = LOGIC_script;
return SCRIPT_STOP;
}
int Logic::cfnSetScript(Object *cpt, int32 id, int32 target, int32 script, int32 e, int32 f, int32 z, int32 x) {
Object *tar = _objMan->fetchObject(target);
tar->o_tree.o_script_level = 0;
tar->o_tree.o_script_pc[0] = script;
tar->o_tree.o_script_id[0] = script;
tar->o_logic = LOGIC_script;
return SCRIPT_CONT;
}
int Logic::cfnPresetScript(Object *cpt, int32 id, int32 target, int32 script, int32 e, int32 f, int32 z, int32 x) {
Object *tar = _objMan->fetchObject(target);
tar->o_tree.o_script_level = 0;
tar->o_tree.o_script_pc[0] = script;
tar->o_tree.o_script_id[0] = script;
if (tar->o_logic == LOGIC_idle)
tar->o_logic = LOGIC_script;
return SCRIPT_CONT;
}
int Logic::fnInteract(Object *cpt, int32 id, int32 target, int32 d, int32 e, int32 f, int32 z, int32 x) {
Object *tar = _objMan->fetchObject(target);
cpt->o_place = tar->o_place;
Object *floorObject = _objMan->fetchObject(tar->o_place);
cpt->o_scale_a = floorObject->o_scale_a;
cpt->o_scale_b = floorObject->o_scale_b;
cpt->o_tree.o_script_level++;
cpt->o_tree.o_script_pc[cpt->o_tree.o_script_level] = tar->o_interact;
cpt->o_tree.o_script_id[cpt->o_tree.o_script_level] = tar->o_interact;
return SCRIPT_STOP;
}
int Logic::fnIssueEvent(Object *cpt, int32 id, int32 event, int32 delay, int32 e, int32 f, int32 z, int32 x) {
_eventMan->fnIssueEvent(cpt, id, event, delay);
return SCRIPT_CONT;
}
int Logic::fnCheckForEvent(Object *cpt, int32 id, int32 pause, int32 d, int32 e, int32 f, int32 z, int32 x) {
return _eventMan->fnCheckForEvent(cpt, id, pause);
}
int Logic::fnWipeHands(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
_scriptVars[OBJECT_HELD] = 0;
_mouse->setLuggage(0, 0);
_menu->refresh(MENU_TOP);
return SCRIPT_CONT;
}
int Logic::fnISpeak(Object *cpt, int32 id, int32 cdt, int32 textNo, int32 spr, int32 f, int32 z, int32 x) {
_speechClickDelay = 3;
if (((textNo & ~1) == 0x3f0012) && (!cdt) && (!spr)) {
cdt = GEOSTDLCDT; // workaround for missing animation when examining
spr = GEOSTDL; // the conductor on the train roof
}
cpt->o_logic = LOGIC_speech;
// first setup the talk animation
if (cdt && (!spr)) { // if 'cdt' is non-zero but 'spr' is zero - 'cdt' is an anim table tag
AnimSet *animTab = (AnimSet *)((uint8 *)_resMan->openFetchRes(cdt) + sizeof(Header));
animTab += cpt->o_dir;
cpt->o_anim_resource = _resMan->getUint32(animTab->cdt);
if (animTab->cdt)
cpt->o_resource = _resMan->getUint32(animTab->spr);
_resMan->resClose(cdt);
} else {
cpt->o_anim_resource = cdt;
if (cdt)
cpt->o_resource = spr;
}
cpt->o_anim_pc = 0; // start anim from first frame
if (cpt->o_anim_resource) {
if (!cpt->o_resource)
error("ID %d: Can't run anim with cdt=%d, spr=%d", id, cdt, spr);
FrameHeader *frameHead = _resMan->fetchFrame(_resMan->openFetchRes(cpt->o_resource), 0);
if (frameHead->offsetX && frameHead->offsetY) { // is this a boxed mega?
cpt->o_status |= STAT_SHRINK;
cpt->o_anim_x = cpt->o_xcoord;
cpt->o_anim_y = cpt->o_ycoord;
} else
cpt->o_status &= ~STAT_SHRINK;
_resMan->resClose(cpt->o_resource);
}
if (SwordEngine::_systemVars.playSpeech)
_speechRunning = _sound->startSpeech(textNo >> 16, textNo & 0xFFFF);
else
_speechRunning = false;
_speechFinished = false;
if (SwordEngine::_systemVars.showText || (!_speechRunning)) {
_textRunning = true;
char *text = _objMan->lockText(textNo);
cpt->o_speech_time = strlen(text) + 5;
uint32 textCptId = _textMan->lowTextManager((uint8 *)text, cpt->o_speech_width, (uint8)cpt->o_speech_pen);
_objMan->unlockText(textNo);
Object *textCpt = _objMan->fetchObject(textCptId);
textCpt->o_screen = cpt->o_screen;
textCpt->o_target = textCptId;
// the graphic is a property of Text, so we don't lock/unlock it.
uint16 textSpriteWidth = _resMan->getUint16(_textMan->giveSpriteData(textCpt->o_target)->width);
uint16 textSpriteHeight = _resMan->getUint16(_textMan->giveSpriteData(textCpt->o_target)->height);
cpt->o_text_id = textCptId;
// now set text coords, above the player, usually
#define TEXT_MARGIN 3 // distance kept from edges of screen
#define ABOVE_HEAD 20 // distance kept above talking sprite
uint16 textX, textY;
if (((id == GEORGE) || ((id == NICO) && (_scriptVars[SCREEN] == 10))) && (!cpt->o_anim_resource)) {
// if George is doing Voice-Over text (centered at the bottom of the screen)
textX = _scriptVars[SCROLL_OFFSET_X] + 128 + (640 / 2) - textSpriteWidth / 2;
textY = _scriptVars[SCROLL_OFFSET_Y] + 128 + 400;
} else {
if ((id == GEORGE) && (_scriptVars[SCREEN] == 79))
textX = cpt->o_mouse_x2; // move it off george's head
else
textX = (cpt->o_mouse_x1 + cpt->o_mouse_x2) / 2 - textSpriteWidth / 2;
textY = cpt->o_mouse_y1 - textSpriteHeight - ABOVE_HEAD;
}
// now ensure text is within visible screen
uint16 textLeftMargin, textRightMargin, textTopMargin, textBottomMargin;
textLeftMargin = SCREEN_LEFT_EDGE + TEXT_MARGIN + _scriptVars[SCROLL_OFFSET_X];
textRightMargin = SCREEN_RIGHT_EDGE - TEXT_MARGIN + _scriptVars[SCROLL_OFFSET_X] - textSpriteWidth;
textTopMargin = SCREEN_TOP_EDGE + TEXT_MARGIN + _scriptVars[SCROLL_OFFSET_Y];
textBottomMargin = SCREEN_BOTTOM_EDGE - TEXT_MARGIN + _scriptVars[SCROLL_OFFSET_Y] - textSpriteHeight;
textCpt->o_anim_x = textCpt->o_xcoord = CLIP<uint16>(textX, textLeftMargin, textRightMargin);
textCpt->o_anim_y = textCpt->o_ycoord = CLIP<uint16>(textY, textTopMargin, textBottomMargin);
}
return SCRIPT_STOP;
}
//send instructions to mega in conversation with player
//the instruction is interpreted by the script mega_interact
int Logic::fnTheyDo(Object *cpt, int32 id, int32 tar, int32 instruc, int32 param1, int32 param2, int32 param3, int32 x) {
Object *target;
target = _objMan->fetchObject(tar);
target->o_down_flag = instruc; // instruction for the mega
target->o_ins1 = param1;
target->o_ins2 = param2;
target->o_ins3 = param3;
return SCRIPT_CONT;
}
//send an instruction to mega we're talking to and wait
//until it has finished before returning to script
int Logic::fnTheyDoWeWait(Object *cpt, int32 id, int32 tar, int32 instruc, int32 param1, int32 param2, int32 param3, int32 x) {
// workaround for scriptbug #928791: Freeze at hospital
// in at least one game version, a script forgets to set sam_returning back to zero
if ((tar == SAM) && (instruc == INS_talk) && (param2 == 2162856))
_scriptVars[SAM_RETURNING] = 0;
Object *target = _objMan->fetchObject(tar);
target->o_down_flag = instruc; // instruction for the mega
target->o_ins1 = param1;
target->o_ins2 = param2;
target->o_ins3 = param3;
target->o_status &= ~STAT_TALK_WAIT;
cpt->o_logic = LOGIC_wait_for_talk;
cpt->o_down_flag = tar;
return SCRIPT_STOP;
}
int Logic::fnWeWait(Object *cpt, int32 id, int32 tar, int32 d, int32 e, int32 f, int32 z, int32 x) {
Object *target = _objMan->fetchObject(tar);
target->o_status &= ~STAT_TALK_WAIT;
cpt->o_logic = LOGIC_wait_for_talk;
cpt->o_down_flag = tar;
return SCRIPT_STOP;
}
int Logic::fnChangeSpeechText(Object *cpt, int32 id, int32 tar, int32 width, int32 pen, int32 f, int32 z, int32 x) {
Object *target = _objMan->fetchObject(tar);
target->o_speech_width = width;
target->o_speech_pen = pen;
return SCRIPT_STOP;
}
//mega_interact has received an instruction it does not understand -
//The game is halted for debugging. Maybe we'll remove this later.
int Logic::fnTalkError(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
error("fnTalkError for id %d, instruction %d", id, cpt->o_down_flag);
return SCRIPT_STOP; // for compilers that don't support NORETURN
}
int Logic::fnStartTalk(Object *cpt, int32 id, int32 target, int32 d, int32 e, int32 f, int32 z, int32 x) {
cpt->o_down_flag = target;
cpt->o_logic = LOGIC_start_talk;
return SCRIPT_STOP;
}
int Logic::fnCheckForTextLine(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
_scriptVars[RETURN_VALUE] = _objMan->fnCheckForTextLine(id);
return SCRIPT_CONT;
}
int Logic::fnAddTalkWaitStatusBit(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
cpt->o_status |= STAT_TALK_WAIT;
return SCRIPT_CONT;
}
int Logic::fnRemoveTalkWaitStatusBit(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
cpt->o_status &= ~STAT_TALK_WAIT;
return SCRIPT_CONT;
}
int Logic::fnNoHuman(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
_mouse->fnNoHuman();
return SCRIPT_CONT;
}
int Logic::fnAddHuman(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
_mouse->fnAddHuman();
return SCRIPT_CONT;
}
int Logic::fnBlankMouse(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
_mouse->fnBlankMouse();
return SCRIPT_CONT;
}
int Logic::fnNormalMouse(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
_mouse->fnNormalMouse();
return SCRIPT_CONT;
}
int Logic::fnLockMouse(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
_mouse->fnLockMouse();
return SCRIPT_CONT;
}
int Logic::fnUnlockMouse(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
_mouse->fnUnlockMouse();
return SCRIPT_CONT;
}
int Logic::fnSetMousePointer(Object *cpt, int32 id, int32 tag, int32 rate, int32 e, int32 f, int32 z, int32 x) {
_mouse->setPointer(tag, rate);
return SCRIPT_CONT;
}
int Logic::fnSetMouseLuggage(Object *cpt, int32 id, int32 tag, int32 rate, int32 e, int32 f, int32 z, int32 x) {
_mouse->setLuggage(tag, rate);
return SCRIPT_CONT;
}
int Logic::fnMouseOn(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
cpt->o_status |= STAT_MOUSE;
return SCRIPT_CONT;
}
int Logic::fnMouseOff(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
cpt->o_status &= ~STAT_MOUSE;
return SCRIPT_CONT;
}
int Logic::fnChooser(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
_menu->fnChooser(cpt);
return SCRIPT_STOP;
}
int Logic::fnEndChooser(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
_menu->fnEndChooser();
return SCRIPT_CONT;
}
int Logic::fnStartMenu(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
_menu->fnStartMenu();
return SCRIPT_CONT;
}
int Logic::fnEndMenu(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
_menu->fnEndMenu();
return SCRIPT_CONT;
}
int Logic::cfnReleaseMenu(Object *cpt, int32 id, int32 c, int32 d, int32 e, int32 f, int32 z, int32 x) {
_menu->cfnReleaseMenu();
return SCRIPT_STOP;
}
int Logic::fnAddSubject(Object *cpt, int32 id, int32 sub, int32 d, int32 e, int32 f, int32 z, int32 x) {
_menu->fnAddSubject(sub);
return SCRIPT_CONT;
}
int Logic::fnAddObject(Object *cpt, int32 id, int32 objectNo, int32 d, int32 e, int32 f, int32 z, int32 x) {
_scriptVars[POCKET_1 + objectNo - 1] = 1; // basically means: carrying object objectNo = true;
return SCRIPT_CONT;
}
int Logic::fnRemoveObject(Object *cpt, int32 id, int32 objectNo, int32 d, int32 e, int32 f, int32 z, int32 x) {
_scriptVars[POCKET_1 + objectNo - 1] = 0;
return SCRIPT_CONT;
}
int Logic::fnEnterSection(Object *cpt, int32 id, int32 screen, int32 d, int32 e, int32 f, int32 z, int32 x) {
if (screen >= TOTAL_SECTIONS)
error("mega %d tried entering section %d", id, screen);
/* if (cpt->o_type == TYPE_PLAYER)
^= this was the original condition from the game sourcecode.
not sure why it doesn't work*/
if (id == PLAYER)
_scriptVars[NEW_SCREEN] = screen;
else
cpt->o_screen = screen; // move the mega
_objMan->megaEntering(screen);
return SCRIPT_CONT;
}
int Logic::fnLeaveSection(Object *cpt, int32 id, int32 oldScreen, int32 d, int32 e, int32 f, int32 z, int32 x) {
if (oldScreen >= TOTAL_SECTIONS)
error("mega %d leaving section %d", id, oldScreen);
_objMan->megaLeaving(oldScreen, id);
return SCRIPT_CONT;
}
int Logic::fnChangeFloor(Object *cpt, int32 id, int32 floor, int32 d, int32 e, int32 f, int32 z, int32 x) {
cpt->o_place = floor;
Object *floorCpt = _objMan->fetchObject(floor);
cpt->o_scale_a = floorCpt->o_scale_a;
cpt->o_scale_b = floorCpt->o_scale_b;
return SCRIPT_CONT;
}
int Logic::fnWalk(Object *cpt, int32 id, int32 x, int32 y, int32 dir, int32 stance, int32 a, int32 b) {
if (stance > 0)
dir = 9;
cpt->o_walk_pc = 0;
cpt->o_route[1].frame = 512; // end of sequence
if (id == PLAYER)
_router->setPlayerTarget(x, y, dir, stance);
int32 routeRes = _router->routeFinder(id, cpt, x, y, dir);
if (id == PLAYER) {
if ((routeRes == 1) || (routeRes == 2)) {
_scriptVars[MEGA_ON_GRID] = 0;
_scriptVars[REROUTE_GEORGE] = 0;
}
}
if ((routeRes == 1) || (routeRes == 2)) {
cpt->o_down_flag = 1; // 1 means okay.
// if both mouse buttons were pressed on an exit => skip george's walk
if ((id == GEORGE) && (_mouse->testEvent() == MOUSE_BOTH_BUTTONS)) {
int32 target = _scriptVars[CLICK_ID];
// exceptions: compacts that use hand pointers but are not actually exits
if ((target != LEFT_SCROLL_POINTER) && (target != RIGHT_SCROLL_POINTER) &&
(target != FLOOR_63) && (target != ROOF_63) && (target != GUARD_ROOF_63) &&
(target != LEFT_TREE_POINTER_71) && (target != RIGHT_TREE_POINTER_71)) {
target = _objMan->fetchObject(_scriptVars[CLICK_ID])->o_mouse_on;
if ((target >= SCR_exit0) && (target <= SCR_exit9)) {
fnStandAt(cpt, id, x, y, dir, stance, 0, 0);
return SCRIPT_STOP;
}
}
}
cpt->o_logic = LOGIC_AR_animate;
return SCRIPT_STOP;
} else if (routeRes == 3)
cpt->o_down_flag = 1; // pretend it was successful
else
cpt->o_down_flag = 0; // 0 means error
return SCRIPT_CONT;
}
int Logic::fnTurn(Object *cpt, int32 id, int32 dir, int32 stance, int32 c, int32 d, int32 a, int32 b) {
if (stance > 0)
dir = 9;
int route = _router->routeFinder(id, cpt, cpt->o_xcoord, cpt->o_ycoord, dir);
if (route)
cpt->o_down_flag = 1; //1 means ok
else
cpt->o_down_flag = 0; //0 means error
cpt->o_logic = LOGIC_AR_animate;
cpt->o_walk_pc = 0; //reset
return SCRIPT_STOP;
}
int Logic::fnStand(Object *cpt, int32 id, int32 dir, int32 stance, int32 c, int32 d, int32 a, int32 b) {
if ((dir < 0) || (dir > 8)) {
warning("fnStand:: invalid direction %d", dir);
return SCRIPT_CONT;
}
if (dir == 8)
dir = cpt->o_dir;
cpt->o_resource = cpt->o_walk_resource;
cpt->o_status |= STAT_SHRINK;
cpt->o_anim_x = cpt->o_xcoord;
cpt->o_anim_y = cpt->o_ycoord;
cpt->o_frame = 96 + dir;
cpt->o_dir = dir;
return SCRIPT_STOP;
}
int Logic::fnStandAt(Object *cpt, int32 id, int32 x, int32 y, int32 dir, int32 stance, int32 a, int32 b) {
if ((dir < 0) || (dir > 8)) {
warning("fnStandAt:: invalid direction %d", dir);
return SCRIPT_CONT;
}
if (dir == 8)
dir = cpt->o_dir;
cpt->o_xcoord = x;
cpt->o_ycoord = y;
return fnStand(cpt, id, dir, stance, 0, 0, 0, 0);
}
int Logic::fnFace(Object *cpt, int32 id, int32 targetId, int32 b, int32 c, int32 d, int32 a, int32 z) {
Object *target = _objMan->fetchObject(targetId);
int32 x, y;
if ((target->o_type == TYPE_MEGA) || (target->o_type == TYPE_PLAYER)) {
x = target->o_xcoord;
y = target->o_ycoord;
} else {
x = (target->o_mouse_x1 + target->o_mouse_x2) / 2;
y = target->o_mouse_y2;
}
int32 megaTarDir = whatTarget(cpt->o_xcoord, cpt->o_ycoord, x, y);
fnTurn(cpt, id, megaTarDir, 0, 0, 0, 0, 0);
return SCRIPT_STOP;
}
int Logic::fnFaceXy(Object *cpt, int32 id, int32 x, int32 y, int32 c, int32 d, int32 a, int32 b) {
int megaTarDir = whatTarget(cpt->o_xcoord, cpt->o_ycoord, x, y);
fnTurn(cpt, id, megaTarDir, 0, 0, 0, 0, 0);
return SCRIPT_STOP;
}
int Logic::fnIsFacing(Object *cpt, int32 id, int32 targetId, int32 b, int32 c, int32 d, int32 a, int32 z) {
Object *target = _objMan->fetchObject(targetId);
int32 x, y, dir;
if ((target->o_type == TYPE_MEGA) || (target->o_type == TYPE_PLAYER)) {
x = target->o_xcoord;
y = target->o_ycoord;
dir = target->o_dir;
} else
error("fnIsFacing:: Target isn't a mega");
int32 lookDir = whatTarget(x, y, cpt->o_xcoord, cpt->o_ycoord);
lookDir -= dir;
lookDir = ABS(lookDir);
if (lookDir > 4)
lookDir = 8 - lookDir;
_scriptVars[RETURN_VALUE] = lookDir;
return SCRIPT_STOP;
}
int Logic::fnGetTo(Object *cpt, int32 id, int32 a, int32 b, int32 c, int32 d, int32 z, int32 x) {
Object *place = _objMan->fetchObject(cpt->o_place);
cpt->o_tree.o_script_level++;
cpt->o_tree.o_script_pc[cpt->o_tree.o_script_level] = place->o_get_to_script;
cpt->o_tree.o_script_id[cpt->o_tree.o_script_level] = place->o_get_to_script;
return SCRIPT_STOP;
}
int Logic::fnGetToError(Object *cpt, int32 id, int32 a, int32 b, int32 c, int32 d, int32 z, int32 x) {
debug(1, "fnGetToError: compact %d at place %d no get-to for target %d, click_id %d\n", id, cpt->o_place, cpt->o_target, _scriptVars[CLICK_ID]);
return SCRIPT_CONT;
}
int Logic::fnRandom(Object *compact, int32 id, int32 min, int32 max, int32 e, int32 f, int32 z, int32 x) {
_scriptVars[RETURN_VALUE] = _rnd.getRandomNumberRng(min, max);
return SCRIPT_CONT;
}
int Logic::fnGetPos(Object *cpt, int32 id, int32 targetId, int32 b, int32 c, int32 d, int32 z, int32 x) {
Object *target = _objMan->fetchObject(targetId);
if ((target->o_type == TYPE_MEGA) || (target->o_type == TYPE_PLAYER)) {
_scriptVars[RETURN_VALUE] = target->o_xcoord;
_scriptVars[RETURN_VALUE_2] = target->o_ycoord;
} else {
_scriptVars[RETURN_VALUE] = (target->o_mouse_x1 + target->o_mouse_x2) / 2;
_scriptVars[RETURN_VALUE_2] = target->o_mouse_y2;
}
_scriptVars[RETURN_VALUE_3] = target->o_dir;
int32 megaSeperation;
if (targetId == DUANE)
megaSeperation = 70; // George & Duane stand with feet 70 pixels apart when at full scale
else if (targetId == BENOIR)
megaSeperation = 61; // George & Benoir
else
megaSeperation = 42; // George & Nico/Goinfre stand with feet 42 pixels apart when at full scale
if (target->o_status & STAT_SHRINK) {
int32 scale = (target->o_scale_a * target->o_ycoord + target->o_scale_b) / 256;
_scriptVars[RETURN_VALUE_4] = (megaSeperation * scale) / 256;
} else
_scriptVars[RETURN_VALUE_4] = megaSeperation;
return SCRIPT_CONT;
}
int Logic::fnGetGamepadXy(Object *cpt, int32 id, int32 a, int32 b, int32 c, int32 d, int32 z, int32 x) {
// playstation only
return SCRIPT_CONT;
}
int Logic::fnPlayFx(Object *cpt, int32 id, int32 fxNo, int32 b, int32 c, int32 d, int32 z, int32 x) {
_scriptVars[RETURN_VALUE] = _sound->addToQueue(fxNo);
return SCRIPT_CONT;
}
int Logic::fnStopFx(Object *cpt, int32 id, int32 fxNo, int32 b, int32 c, int32 d, int32 z, int32 x) {
_sound->fnStopFx(fxNo);
//_sound->removeFromQueue(fxNo);
return SCRIPT_CONT;
}
int Logic::fnPlayMusic(Object *cpt, int32 id, int32 tuneId, int32 loopFlag, int32 c, int32 d, int32 z, int32 x) {
if (tuneId == 153)
return SCRIPT_CONT;
if (loopFlag == LOOPED)
_scriptVars[CURRENT_MUSIC] = tuneId; // so it gets restarted when saving & reloading
else
_scriptVars[CURRENT_MUSIC] = 0;
_music->startMusic(tuneId, loopFlag);
return SCRIPT_CONT;
}
int Logic::fnStopMusic(Object *cpt, int32 id, int32 a, int32 b, int32 c, int32 d, int32 z, int32 x) {
_scriptVars[CURRENT_MUSIC] = 0;
_music->fadeDown();
return SCRIPT_CONT;
}
int Logic::fnInnerSpace(Object *cpt, int32 id, int32 a, int32 b, int32 c, int32 d, int32 z, int32 x) {
error("fnInnerSpace() not working");
return SCRIPT_STOP; // for compilers that don't support NORETURN
}
int Logic::fnSetScreen(Object *cpt, int32 id, int32 target, int32 screen, int32 c, int32 d, int32 z, int32 x) {
_objMan->fetchObject(target)->o_screen = screen;
return SCRIPT_CONT;
}
int Logic::fnPreload(Object *cpt, int32 id, int32 resId, int32 b, int32 c, int32 d, int32 z, int32 x) {
_resMan->resOpen(resId);
_resMan->resClose(resId);
return SCRIPT_CONT;
}
int Logic::fnCheckCD(Object *cpt, int32 id, int32 screen, int32 b, int32 c, int32 d, int32 z, int32 x) {
// only a dummy, here.
// the check is done in the mainloop
return SCRIPT_CONT;
}
int Logic::fnRestartGame(Object *cpt, int32 id, int32 a, int32 b, int32 c, int32 d, int32 z, int32 x) {
SwordEngine::_systemVars.forceRestart = true;
cpt->o_logic = LOGIC_quit;
return SCRIPT_STOP;
}
int Logic::fnQuitGame(Object *cpt, int32 id, int32 a, int32 b, int32 c, int32 d, int32 z, int32 x) {
if (SwordEngine::_systemVars.isDemo) {
GUI::MessageDialog dialog(_("This is the end of the Broken Sword 1 Demo"), _("OK"), NULL);
dialog.runModal();
Engine::quitGame();
} else
error("fnQuitGame() called");
return fnQuit(cpt, id, 0, 0, 0, 0, 0, 0);
}
int Logic::fnDeathScreen(Object *cpt, int32 id, int32 a, int32 b, int32 c, int32 d, int32 z, int32 x) {
if (_scriptVars[FINALE_OPTION_FLAG] == 4) // successful end of game!
SwordEngine::_systemVars.controlPanelMode = CP_THEEND;
else
SwordEngine::_systemVars.controlPanelMode = CP_DEATHSCREEN;
cpt->o_logic = LOGIC_quit;
return SCRIPT_STOP;
}
int Logic::fnSetParallax(Object *cpt, int32 id, int32 screen, int32 resId, int32 c, int32 d, int32 z, int32 x) {
_screen->fnSetParallax(screen, resId);
return SCRIPT_CONT;
}
int Logic::fnTdebug(Object *cpt, int32 id, int32 a, int32 b, int32 c, int32 d, int32 z, int32 x) {
debug(1, "Script TDebug id %d code %d, %d", id, a, b);
return SCRIPT_CONT;
}
int Logic::fnRedFlash(Object *cpt, int32 id, int32 a, int32 b, int32 c, int32 d, int32 z, int32 x) {
_screen->fnFlash(FLASH_RED);
return SCRIPT_CONT;
}
int Logic::fnBlueFlash(Object *cpt, int32 id, int32 a, int32 b, int32 c, int32 d, int32 z, int32 x) {
_screen->fnFlash(FLASH_BLUE);
return SCRIPT_CONT;
}
int Logic::fnYellow(Object *cpt, int32 id, int32 a, int32 b, int32 c, int32 d, int32 z, int32 x) {
_screen->fnFlash(BORDER_YELLOW);
return SCRIPT_CONT;
}
int Logic::fnGreen(Object *cpt, int32 id, int32 a, int32 b, int32 c, int32 d, int32 z, int32 x) {
_screen->fnFlash(BORDER_GREEN);
return SCRIPT_CONT;
}
int Logic::fnPurple(Object *cpt, int32 id, int32 a, int32 b, int32 c, int32 d, int32 z, int32 x) {
_screen->fnFlash(BORDER_PURPLE);
return SCRIPT_CONT;
}
int Logic::fnBlack(Object *cpt, int32 id, int32 a, int32 b, int32 c, int32 d, int32 z, int32 x) {
_screen->fnFlash(BORDER_BLACK);
return SCRIPT_CONT;
}
void Logic::startPosCallFn(uint8 fnId, uint32 param1, uint32 param2, uint32 param3) {
Object *obj = NULL;
switch (fnId) {
case opcPlaySequence:
fnPlaySequence(NULL, 0, param1, 0, 0, 0, 0, 0);
break;
case opcAddObject:
fnAddObject(NULL, 0, param1, 0, 0, 0, 0, 0);
break;
case opcRemoveObject:
fnRemoveObject(NULL, 0, param1, 0, 0, 0, 0, 0);
break;
case opcMegaSet:
obj = _objMan->fetchObject(param1);
fnMegaSet(obj, param1, param2, param3, 0, 0, 0, 0);
break;
case opcNoSprite:
obj = _objMan->fetchObject(param1);
fnNoSprite(obj, param1, param2, param3, 0, 0, 0, 0);
break;
default:
error("Illegal fnCallfn argument %d", fnId);
}
}
void Logic::runStartScript(const uint8 *data) {
// Here data is a static resource defined in staticres.cpp
// It is always in little endian
uint16 varId = 0;
uint8 fnId = 0;
uint32 param1 = 0;
while (*data != opcSeqEnd) {
switch (*data++) {
case opcCallFn:
fnId = *data++;
param1 = *data++;
startPosCallFn(fnId, param1, 0, 0);
break;
case opcCallFnLong:
fnId = *data++;
startPosCallFn(fnId, READ_LE_UINT32(data), READ_LE_UINT32(data + 4), READ_LE_UINT32(data + 8));
data += 12;
break;
case opcSetVar8:
varId = READ_LE_UINT16(data);
_scriptVars[varId] = data[2];
data += 3;
break;
case opcSetVar16:
varId = READ_LE_UINT16(data);
_scriptVars[varId] = READ_LE_UINT16(data + 2);
data += 4;
break;
case opcSetVar32:
varId = READ_LE_UINT16(data);
_scriptVars[varId] = READ_LE_UINT32(data + 2);
data += 6;
break;
case opcGeorge:
_scriptVars[CHANGE_X] = READ_LE_UINT16(data + 0);
_scriptVars[CHANGE_Y] = READ_LE_UINT16(data + 2);
_scriptVars[CHANGE_DIR] = data[4];
_scriptVars[CHANGE_PLACE] = READ_LE_UINT24(data + 5);
data += 8;
break;
case opcRunStart:
data = _startData[*data];
break;
case opcRunHelper:
data = _helperData[*data];
break;
default:
error("Unexpected opcode in StartScript");
}
}
}
void Logic::startPositions(uint32 pos) {
bool spainVisit2 = false;
if ((pos >= 956) && (pos <= 962)) {
spainVisit2 = true;
pos -= 900;
}
if ((pos > 80) || (_startData[pos] == NULL))
error("Starting in Section %d is not supported", pos);
Logic::_scriptVars[CHANGE_STANCE] = STAND;
Logic::_scriptVars[GEORGE_CDT_FLAG] = GEO_TLK_TABLE;
runStartScript(_startData[pos]);
if (spainVisit2)
runStartScript(_helperData[HELP_SPAIN2]);
if (pos == 0)
pos = 1;
Object *compact = _objMan->fetchObject(PLAYER);
fnEnterSection(compact, PLAYER, pos, 0, 0, 0, 0, 0); // (automatically opens the compact resource for that section)
SwordEngine::_systemVars.controlPanelMode = CP_NORMAL;
SwordEngine::_systemVars.wantFade = true;
}
const uint32 Logic::_scriptVarInit[NON_ZERO_SCRIPT_VARS][2] = {
{ 42, 448}, { 43, 378}, { 51, 1}, { 92, 1}, { 147, 71}, { 201, 1},
{ 209, 1}, { 215, 1}, { 242, 2}, { 244, 1}, { 246, 3}, { 247, 1},
{ 253, 1}, { 297, 1}, { 398, 1}, { 508, 1}, { 605, 1}, { 606, 1},
{ 701, 1}, { 709, 1}, { 773, 1}, { 843, 1}, { 907, 1}, { 923, 1},
{ 966, 1}, { 988, 2}, {1058, 1}, {1059, 2}, {1060, 3}, {1061, 4},
{1062, 5}, {1063, 6}, {1064, 7}, {1065, 8}, {1066, 9}, {1067, 10},
{1068, 11}, {1069, 12}, {1070, 13}, {1071, 14}, {1072, 15}, {1073, 16},
{1074, 17}, {1075, 18}, {1076, 19}, {1077, 20}, {1078, 21}, {1079, 22},
{1080, 23}, {1081, 24}, {1082, 25}, {1083, 26}, {1084, 27}, {1085, 28},
{1086, 29}, {1087, 30}, {1088, 31}, {1089, 32}, {1090, 33}, {1091, 34},
{1092, 35}, {1093, 36}, {1094, 37}, {1095, 38}, {1096, 39}, {1097, 40},
{1098, 41}, {1099, 42}, {1100, 43}, {1101, 44}, {1102, 48}, {1103, 45},
{1104, 47}, {1105, 49}, {1106, 50}, {1107, 52}, {1108, 54}, {1109, 56},
{1110, 57}, {1111, 58}, {1112, 59}, {1113, 60}, {1114, 61}, {1115, 62},
{1116, 63}, {1117, 64}, {1118, 65}, {1119, 66}, {1120, 67}, {1121, 68},
{1122, 69}, {1123, 71}, {1124, 72}, {1125, 73}, {1126, 74}
};
} // End of namespace Sword1
|