Example #1
0
int AgiEngine::testIfCode(int lognum) {
    int ec = true;
    int retval = true;
    uint8 op = 0;
    uint8 notTest = false;
    uint8 orTest = false;
    uint16 lastIp = ip;
    uint8 p[16] = { 0 };
    bool end_test = false;

    while (retval && !(shouldQuit() || _restartGame) && !end_test) {
        if (_debug.enabled && (_debug.logic0 || lognum))
            debugConsole(lognum, lTEST_MODE, NULL);

        lastIp = ip;
        op = *(code + ip++);
        memmove(p, (code + ip), 16);

        switch (op) {
        case 0xFF:	// END IF, TEST true
            end_test = true;
            break;
        case 0xFD:
            notTest = !notTest;
            continue;
        case 0xFC:	// OR
            // if or_test is ON and we hit 0xFC, end of OR, then
            // or is STILL false so break.
            if (orTest) {
                ec = false;
                retval = false;
                end_test = true;
            }

            orTest = true;
            continue;

        case 0x00:
            // return true?
            end_test = true;
            break;
        case 0x01:
            ec = testEqual(p[0], p[1]);
            if (p[0] == 11)
                _timerHack++;
            break;
        case 0x02:
            ec = testEqual(p[0], getvar(p[1]));
            if (p[0] == 11 || p[1] == 11)
                _timerHack++;
            break;
        case 0x03:
            ec = testLess(p[0], p[1]);
            if (p[0] == 11)
                _timerHack++;
            break;
        case 0x04:
            ec = testLess(p[0], getvar(p[1]));
            if (p[0] == 11 || p[1] == 11)
                _timerHack++;
            break;
        case 0x05:
            ec = testGreater(p[0], p[1]);
            if (p[0] == 11)
                _timerHack++;
            break;
        case 0x06:
            ec = testGreater(p[0], getvar(p[1]));
            if (p[0] == 11 || p[1] == 11)
                _timerHack++;
            break;
        case 0x07:
            ec = testIsSet(p[0]);
            break;
        case 0x08:
            ec = testIsSet(getvar(p[0]));
            break;
        case 0x09:
            ec = testHas(p[0]);
            break;
        case 0x0A:
            ec = testObjInRoom(p[0], p[1]);
            break;
        case 0x0B:
            ec = testPosn(p[0], p[1], p[2], p[3], p[4]);
            break;
        case 0x0C:
            ec = testController(p[0]);
            break;
        case 0x0D:
            ec = testKeypressed();
            break;
        case 0x0E:
            ec = testSaid(p[0], (uint8 *) code + (ip + 1));
            ip = lastIp;
            ip++;	// skip opcode
            ip += p[0] * 2;	// skip num_words * 2
            ip++;	// skip num_words opcode
            break;
        case 0x0F:
            debugC(7, kDebugLevelScripts, "comparing [%s], [%s]", _game.strings[p[0]], _game.strings[p[1]]);
            ec = testCompareStrings(p[0], p[1]);
            break;
        case 0x10:
            ec = testObjInBox(p[0], p[1], p[2], p[3], p[4]);
            break;
        case 0x11:
            ec = testObjCenter(p[0], p[1], p[2], p[3], p[4]);
            break;
        case 0x12:
            ec = testObjRight(p[0], p[1], p[2], p[3], p[4]);
            break;
        case 0x13: // Unknown test command 19
            // My current theory is that this command checks whether the ego is currently moving
            // and that that movement has been caused using the mouse and not using the keyboard.
            // I base this theory on the game's behavior on an Amiga emulator, not on disassembly.
            // This command is used at least in the Amiga version of Gold Rush! v2.05 1989-03-09
            // (AGI 2.316) in logics 1, 3, 5, 6, 137 and 192 (Logic.192 revealed this command's nature).
            // TODO: Check this command's implementation using disassembly just to be sure.
            ec = _game.viewTable[0].flags & ADJ_EGO_XY;
            debugC(7, kDebugLevelScripts, "op_test: in.motion.using.mouse = %s (Amiga-specific testcase 19)", ec ? "true" : "false");
            break;
        default:
            ec = false;
            end_test = true;
        }

        if (!end_test) {
            if (op <= 0x12)
                ip += logicNamesTest[op].numArgs;

            // exchange ec value
            if (notTest)
                ec = !ec;

            // not is only enabled for 1 test command
            notTest = false;

            if (orTest && ec) {
                // a true inside an OR statement passes
                // ENTIRE statement scan for end of OR

                // CM: test for opcode < 0xfc changed from 'op' to
                //     '*(code+ip)', to avoid problem with the 0xfd (NOT)
                //     opcode byte. Changed a bad ip += ... ip++ construct.
                //     This should fix the crash with Larry's logic.0 code:
                //
                //     if ((isset(4) ||
                //          !isset(2) ||
                //          v30 == 2 ||
                //          v30 == 1)) {
                //       goto Label1;
                //     }
                //
                //     The bytecode is:
                //     ff fc 07 04 fd 07 02 01 1e 02 01 1e 01 fc ff

                // find end of OR
                while (*(code + ip) != 0xFC) {
                    if (*(code + ip) == 0x0E) {	// said
                        ip++;

                        // cover count + ^words
                        ip += 1 + ((*(code + ip)) * 2);
                        continue;
                    }

                    if (*(code + ip) < 0xFC)
                        ip += logicNamesTest[*(code + ip)].numArgs;
                    ip++;
                }
                ip++;

                orTest = false;
                retval = true;
            } else {
                retval = orTest ? retval || ec : retval && ec;
            }
        }
    }

    // if false, scan for end of IP?
    if (retval)
        ip += 2;
    else {
        ip = lastIp;
        while (*(code + ip) != 0xff) {
            if (*(code + ip) == 0x0e) {
                ip++;
                ip += (*(code + ip)) * 2 + 1;
            } else if (*(code + ip) < 0xfc) {
                ip += logicNamesTest[*(code + ip)].numArgs;
                ip++;
            } else {
                ip++;
            }
        }
        ip++;		// skip over 0xFF
        ip += READ_LE_UINT16(code + ip) + 2;
    }

    if (_debug.enabled && (_debug.logic0 || lognum))
        debugConsole(lognum, 0xFF, retval ? "=true" : "=false");

    return retval;
}
Example #2
0
void condIsSetV(AgiGame *state, uint8 *p) {
	state->testResult = testIsSet(getvar(p[0]));
}