<?phpnamespace App\Alis\Compiled\Templates;use App\Alis\Alis;use App\Alis\C;use App\Alis\Compiled\Context;use App\Alis\Compiled\Templates;/** * Opcodes 0x8b-0x9f (fn/opcodes_a.php): find / list entities, sounds and music, vmov, xinv, redon... * The compiler only compiles scripts of versions below 60 (Compiler.php): the v6 branches are left out, and a * version 10 (or non-PC) game is left to the VM for the instructions whose read of the stream differs. * Not done: cnul2, capproach, cescape, cdefworld, cworld, cfindmat. */final class Ops06{ private const N = '\App\Alis\\'; private static function v10(): bool { return Alis::$platform->version <= 10; } /** crstent(): ptrent = 0, the first found entity, then cstore_continue() with the store target */ private static function rst(Context $c, array $store): string { return 'Alis::$ptrent = 0;' . "\n" . 'Alis::$varD7 = Alis::$tablent[Alis::$ptrent];' . "\n" . 'if (-1 < Alis::$tablent[Alis::$ptrent]) { Alis::$ptrent++; }' . "\n" . '[Alis::$sd7, Alis::$oldsd7] = [Alis::$oldsd7, Alis::$sd7];' . "\n" . $c->storename($store); } public static function register(): void { $n = self::N; $k = $n . 'OpcodesConst::'; $v = '$s->vram_org'; // simple ones Templates::$op['cxinv'] = static fn (array $i, Context $c): string => sprintf( $n . 'set_0x03_xinv(%s, (' . $n . 'get_0x03_xinv(%s) ^ 1) & 0xff);', $v, $v ); Templates::$op['cxinvon'] = static fn (array $i, Context $c): string => sprintf($n . 'set_0x03_xinv(%s, 1);', $v); Templates::$op['cxinvoff'] = static fn (array $i, Context $c): string => sprintf($n . 'set_0x03_xinv(%s, 0);', $v); Templates::$op['credon'] = static fn (array $i, Context $c): ?string => self::v10() ? null : sprintf($n . 'set_0x25_credon_credoff(%s, 0x0);', $v); Templates::$op['cvmov'] = static fn (array $i, Context $c): string => implode("\n", [ sprintf($n . 'xadd16(%s + \App\Alis\C::ALIS_SCR_WCX, ' . $n . 's8(' . $n . 'xread8(%s + ' . $k . 'ALIS_SCR_WCX2())));', $v, $v), sprintf($n . 'xadd16(%s + ' . $k . 'ALIS_SCR_WCY(), ' . $n . 's8(' . $n . 'xread8(%s + ' . $k . 'ALIS_SCR_WCY2())));', $v, $v), sprintf($n . 'xadd16(%s + ' . $k . 'ALIS_SCR_WCZ(), ' . $n . 's8(' . $n . 'xread8(%s + ' . $k . 'ALIS_SCR_WCZ2())));', $v, $v), ]); $wc = static fn (): string => implode("\n", [ sprintf('Alis::$wcx = ' . $n . 's16(' . $n . 's8(' . $n . 'xread8(%s + ' . $k . 'ALIS_SCR_WCX2())) + ' . $n . 'xread16(%s + \App\Alis\C::ALIS_SCR_WCX));', $v, $v), sprintf('Alis::$wcy = ' . $n . 's16(' . $n . 's8(' . $n . 'xread8(%s + ' . $k . 'ALIS_SCR_WCY2())) + ' . $n . 'xread16(%s + ' . $k . 'ALIS_SCR_WCY()));', $v, $v), sprintf('Alis::$wcz = ' . $n . 's16(' . $n . 's8(' . $n . 'xread8(%s + ' . $k . 'ALIS_SCR_WCZ2())) + ' . $n . 'xread16(%s + ' . $k . 'ALIS_SCR_WCZ()));', $v, $v), ]); Templates::$op['cvtstmov'] = static fn (array $i, Context $c): string => $wc() . "\n" . $c->opername($i['ops'][0][1]) . "\n" . 'Alis::$matmask = ' . $n . 's16(Alis::$varD7);' . "\n" . 'Alis::$wforme = ' . $n . 'get_0x1a_cforme(' . $v . ');' . "\n" . $n . 'clipform();' . "\n" . self::rst($c, $i['ops'][1][1]); Templates::$op['cvftstmov'] = static fn (array $i, Context $c): string => $wc() . "\n" . $c->opername($i['ops'][0][1]) . "\n" . 'Alis::$matmask = ' . $n . 's16(Alis::$varD7);' . "\n" . $c->opername($i['ops'][1][1]) . "\n" . 'Alis::$wforme = ' . $n . 's16(Alis::$varD7);' . "\n" . $n . 'clipform();' . "\n" . self::rst($c, $i['ops'][2][1]); // cfindtyp: PC, version 20..59: the id is the word operand (read raw: unsigned) Templates::$op['cfindtyp'] = static function (array $i, Context $c) use ($n): ?string { if (self::v10() || Alis::$platform->kind != C::EPlatformPC) { return null; } return sprintf('Alis::$varD7 = %d;', $i['ops'][0][1] & 0xffff) . "\n" . '$id = ' . $n . 's16(Alis::$varD7);' . "\n" . '$entidx = ' . $n . 's16(' . $n . 'alis_first_ent());' . "\n" . '$tabidx = 0;' . "\n" . 'do {' . "\n" . ' $tgt_vram = ' . $n . 'xread32(Alis::$atent + $entidx);' . "\n" . ' if ($id == ' . $n . 'get_0x10_script_id($tgt_vram) && $s->vram_org != $tgt_vram) {' . "\n" . ' if (' . $n . 'tabent_full($tabidx)) {' . "\n" . ' break;' . "\n" . ' }' . "\n" . ' Alis::$matent[$tabidx] = 0;' . "\n" . ' Alis::$tablent[$tabidx] = $entidx;' . "\n" . ' $tabidx++;' . "\n" . ' if (Alis::$fallent == 0) {' . "\n" . ' break;' . "\n" . ' }' . "\n" . ' }' . "\n" . ' $entidx = ' . $n . 'xread16(Alis::$atent + 4 + $entidx);' . "\n" . '} while ($entidx != 0);' . "\n" . 'Alis::$tablent[$tabidx] = ' . $n . 'ENTLIST_END();' . "\n" . 'Alis::$fallent = 0;' . "\n" . self::rst($c, $i['ops'][1][1]); }; Templates::$op['clistent'] = static function (array $i, Context $c) use ($n): ?string { if (self::v10()) { return null; } return '$entidx = ' . $n . 's16(' . $n . 'alis_first_ent());' . "\n" . '$tabidx = 0;' . "\n" . 'while (($entidx = ' . $n . 'xread16(Alis::$atent + 4 + $entidx)) != 0 && !' . $n . 'tabent_full($tabidx)) {' . "\n" . ' Alis::$matent[$tabidx] = 0;' . "\n" . ' Alis::$tablent[$tabidx] = $entidx;' . "\n" . ' $tabidx++;' . "\n" . '}' . "\n" . 'Alis::$tablent[$tabidx] = ' . $n . 'ENTLIST_END();' . "\n" . 'Alis::$fallent = 0;' . "\n" . self::rst($c, $i['ops'][0][1]); }; // sound / msound (version 20..59) $sound = static function (int $flag) use ($n): \Closure { return static function (array $i, Context $c) use ($n, $flag): ?string { if (self::v10()) { return null; } $o = $i['ops']; return 'Alis::$flagmain = ' . $flag . ';' . "\n" . $c->opername($o[0][1]) . "\n" . '$index = Alis::$varD7 & 0xff;' . "\n" . $c->opername($o[1][1]) . "\n" . '$priorson = Alis::$varD7 & 0xff;' . "\n" . $c->opername($o[2][1]) . "\n" . '$volson = Alis::$varD7 & 0xff;' . "\n" . $c->opername($o[3][1]) . "\n" . '$loopsam = Alis::$varD7 & 0xffff;' . "\n" . $c->opername($o[4][1]) . "\n" . '$speedsam = Alis::$varD7 & 0xff;' . "\n" . '$addr = ' . $n . 'adresmus($index);' . "\n" . '$type = ' . $n . 's8(' . $n . 'xread8($addr));' . "\n" . 'if ($type == 1 || $type == 2) {' . "\n" . ' if ($speedsam == 0) {' . "\n" . ' $speedsam = ' . $n . 'xread8($addr + 1);' . "\n" . ' }' . "\n" . ' $longsam = ' . $n . 's32(' . $n . 'xread32($addr + 2) - 0x10);' . "\n" . ' if (Alis::$platform->kind == \App\Alis\C::EPlatformPC && (Alis::$platform->uid == \App\Alis\C::EGameColorado || Alis::$platform->uid == \App\Alis\C::EGameWindsurfWilly || Alis::$platform->uid == \App\Alis\C::EGameMadShow || Alis::$platform->uid == \App\Alis\C::EGameLeFeticheMaya)) {' . "\n" . ' $longsam = ' . $n . 's32(' . $n . 'xread32be($addr + 2) - 0x10);' . "\n" . ' }' . "\n" . ' $startsam = $addr + 0x10;' . "\n" . ' ' . $n . 'playsample(\App\Alis\C::eChannelTypeSample, $startsam, $speedsam, $volson, $longsam, $loopsam, $priorson, ' . $n . 's16(' . $n . 'get_0x0e_script_ent($s->vram_org)));' . "\n" . '}'; }; }; Templates::$op['csound'] = $sound(0); Templates::$op['cmsound'] = $sound(1); Templates::$op['cdelmusic'] = static function (array $i, Context $c) use ($n): ?string { if (self::v10()) { return null; } return $c->opername($i['ops'][0][1]) . "\n" . 'if ((' . $n . 'Aud::$soundrout ?? null) === \'' . trim($n, '\\') . '\\\\mv1_soundrout\') {' . "\n" . ' ' . $n . 'mv1_offmusic(Alis::$varD7);' . "\n" . '} else {' . "\n" . ' ' . $n . 'mv2_offmusic(Alis::$varD7);' . "\n" . '}'; }; Templates::$op['ccadence'] = static function (array $i, Context $c) use ($n): ?string { if (self::v10()) { return null; } return $c->opername($i['ops'][0][1]) . "\n" . 'if (Alis::$varD7 == 0) {' . "\n" . ' Alis::$varD7 = 1;' . "\n" . '}' . "\n" . $n . 'Aud::$mutempo = Alis::$varD7 & 0xff;' . "\n" . $n . 'Aud::$mutemp = intdiv(' . $n . 'Aud::$mutempo * 6, 0x20) & 0xff;'; }; Templates::$op['csetvolum'] = static function (array $i, Context $c) use ($n): ?string { if (self::v10()) { return null; } $a = $n . 'Aud::'; return $c->opername($i['ops'][0][1]) . "\n" . $a . '$muvolume = Alis::$varD7 & 0xff;' . "\n" . $a . '$maxvolume = (' . $a . '$maxvolume & 0xff00) | (Alis::$varD7 & 0xff);' . "\n" . $a . '$muvol = ((' . $a . '$muvolume >> 1) + 1) & 0xff;' . "\n" . 'if (' . $a . '$muattac != 0) {' . "\n" . ' ' . $a . '$dattac = intdiv(' . $a . '$maxvolume, ' . $a . '$muattac);' . "\n" . '}' . "\n" . 'if (' . $a . '$muchute != 0) {' . "\n" . ' ' . $a . '$dchute = intdiv(' . $a . '$maxvolume, ' . $a . '$muchute);' . "\n" . '}'; }; Templates::$op['cmusic'] = static function (array $i, Context $c) use ($n): ?string { if (self::v10() || Alis::$platform->uid == C::EGameRobinsonsRequiem3DO) { return null; } $a = $n . 'Aud::'; $o = $i['ops']; return 'Alis::$flagmain = 0;' . "\n" . '$scriptId = Alis::$flagmain ? Alis::$main->data->header->id : $s->data->header->id;' . "\n" . $a . '$musicId = $scriptId & 0xffff;' . "\n" . $c->opername($o[0][1]) . "\n" . '$idx = ' . $n . 's16(Alis::$varD7);' . "\n" . '$addr = ' . $n . 'adresmus($idx);' . "\n" . '$type = ' . $n . 'xread8($addr);' . "\n" . 'if ($type == 0 || $type == 3) {' . "\n" . $a . '$muvolume = 0;' . "\n" . $a . '$mupnote = $addr + 6;' . "\n" . $c->opername($o[1][1]) . "\n" . $a . '$maxvolume = (Alis::$varD7 << 8) & 0xffff;' . "\n" . $c->opername($o[2][1]) . "\n" . $a . '$mutempo = Alis::$varD7 & 0xff;' . "\n" . $c->opername($o[3][1]) . "\n" . $a . '$muattac = (Alis::$varD7 + 1) & 0xffff;' . "\n" . $c->opername($o[4][1]) . "\n" . $a . '$muduree = ' . $n . 's16(Alis::$varD7 + 1);' . "\n" . $c->opername($o[5][1]) . "\n" . $a . '$muchute = (Alis::$varD7 + 2) & 0xffff;' . "\n" . 'if (' . $a . '$muattac != 0) {' . "\n" . ' ' . $a . '$dattac = intdiv(' . $a . '$maxvolume, ' . $a . '$muattac);' . "\n" . '}' . "\n" . 'if (' . $a . '$muchute != 0) {' . "\n" . ' ' . $a . '$dchute = intdiv(' . $a . '$maxvolume, ' . $a . '$muchute);' . "\n" . '}' . "\n" . '$type = ' . $n . 'xread8(' . $a . '$mupnote - 6);' . "\n" . 'if ($type == 0) {' . "\n" . ' ' . $n . 'mv1_gomusic();' . "\n" . '}' . "\n" . $a . '$mustate = 1;' . "\n" . '} elseif ($type == 4) {' . "\n" . $a . '$muvolume = 0;' . "\n" . $a . '$mupnote = $addr + 6;' . "\n" . $c->opername($o[1][1]) . "\n" . $a . '$maxvolume = (Alis::$varD7 << 8) & 0xffff;' . "\n" . $c->opername($o[2][1]) . "\n" . $a . '$mutempo = Alis::$varD7 & 0xff;' . "\n" . $c->opername($o[3][1]) . "\n" . $a . '$muattac = (Alis::$varD7 + 1) & 0xffff;' . "\n" . $c->opername($o[4][1]) . "\n" . $a . '$muduree = ' . $n . 's16(Alis::$varD7);' . "\n" . $c->opername($o[5][1]) . "\n" . $a . '$muchute = (Alis::$varD7 + 1) & 0xffff;' . "\n" . $a . '$dattac = 0;' . "\n" . $a . '$dchute = 0;' . "\n" . 'if (' . $a . '$muattac != 0) {' . "\n" . ' ' . $a . '$dattac = intdiv(' . $a . '$maxvolume, ' . $a . '$muattac);' . "\n" . '}' . "\n" . 'if (' . $a . '$muchute != 0) {' . "\n" . ' ' . $a . '$dchute = intdiv(' . $a . '$maxvolume, ' . $a . '$muchute);' . "\n" . '}' . "\n" . $a . '$muvolume = (' . $a . '$maxvolume >> 8) & 0xff;' . "\n" . 'if (' . $a . '$fmusic) {' . "\n" . ' ' . $n . 'mv2_gomusic();' . "\n" . '}' . "\n" . $a . '$mustate = 1;' . "\n" . '} else {' . "\n" . implode("\n", array_map(static fn (int $k): string => $c->opername($o[$k][1]), [1, 2, 3, 4, 5])) . "\n" . '}'; }; }}