PL
games/alis / src / Alis / Compiled / Templates / Ops14.php · commit 9c722cf
<?phpnamespace App\Alis\Compiled\Templates;use App\Alis\Alis;use App\Alis\C;use App\Alis\Compiled\Context;use App\Alis\Compiled\Templates;/** * The screen, map, palette, near / form tests and sound instructions that were left to the VM: topalet, scpos, scsize, * scmap, defsc, defmouse, matent, asleepfar, tstform, nearent, ding, defmap, setmap, putmap. Their handlers are split * in the VM into a reader part and a core function (ctopalet_core(), cdefsc_core(), sviewent_list()...) when the core * does not read the stream; the map instructions (Robinson's Requiem) are written out here in the handler's order. */final class Ops14{    private const N = '\App\Alis\\';    /** is this game one of the two PC Robinson's Requiem editions (the maps' branch of the VM)? */    private static function rr(bool $with3do = false): bool    {        $u = Alis::$platform->uid;        return $u == C::EGameRobinsonsRequiem0 || $u == C::EGameRobinsonsRequiem1 || ($with3do && $u == C::EGameRobinsonsRequiem3DO);    }    /** the map's base address (the script's variables, or the main script's when the offset was written long) */    private static function base(array $v, string $t): string    {        return sprintf('%s = %s + %d;', $t, $v[2] ? 'Alis::$basemain' : '$s->vram_org', $v[1]);    }    /** crstent() + its store target: ptrent = 0, then csuccent() */    private static function rst(array $store, Context $c): 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;        // ctopalet / cscpos / cscsize: two expressions (D7, then D6 with D7 kept), then the core        foreach (['topalet' => false, 'scpos' => true, 'scsize' => true] as $name => $v6) {            Templates::$op['c' . $name] = static function (array $i, Context $c) use ($name, $v6, $n): ?string {                if ($v6 && Alis::$platform->version >= 60) {                    return null;                }                return $c->opername($i['ops'][0][1]) . "\n" . $c->opernameSaveD6($i['ops'][1][1]) . "\n"                    . sprintf('%sc%s_core();', $n, $name);            };        }        // cscmap: the expression (D7, unused), the map offset (0 = the entity is 0 and a second offset follows)        Templates::$op['cscmap'] = static function (array $i, Context $c) use ($n): string {            $v = $i['ops'][1];            return implode("\n", [                $c->opername($i['ops'][0][1]),                '$ent = ' . ($v[2] ? '0' : $n . 'get_0x0e_script_ent($s->vram_org)') . ';',                sprintf('if (%sget_0x16_screen_id($s->vram_org) != 0) {', $n),                sprintf('    $a = Alis::$basemain + %sget_0x16_screen_id($s->vram_org);', $n),                sprintf('    %sxwrite16($a + 0x42, %d);', $n, $v[1]),                sprintf('    %sxwrite16($a + 0x40, $ent);', $n),                '}',            ]);        };        // cdefsc (versions 30 .. 59): the screen, the element, the 32 bytes of its header        Templates::$op['cdefsc'] = static function (array $i, Context $c) use ($n): ?string {            if (Alis::$platform->version == 10 || Alis::$platform->version >= 60) {                return null;            }            return sprintf('%scdefsc_core(%d, %d, %s);', $n, $i['ops'][0][1], $i['ops'][1][1], Context::str($i['ops'][2][1]));        };        // cdefmouse: flagmain = 0, the expression, then the core        Templates::$op['cdefmouse'] = static fn (array $i, Context $c): string => 'Alis::$flagmain = 0;' . "\n"            . $c->opername($i['ops'][0][1]) . "\n" . $n . 'cdefmouse_core();';        // cmatent (versions < 60): the match of the entity before ptrent        Templates::$op['cmatent'] = static function (array $i, Context $c): ?string {            if (Alis::$platform->version >= 60) {                return null;            }            return 'Alis::$varD7 = Alis::$matent[Alis::$ptrent - 1];' . "\n"                . '[Alis::$sd7, Alis::$oldsd7] = [Alis::$oldsd7, Alis::$sd7];' . "\n" . $c->storename($i['ops'][0][1]);        };        // casleepfar (versions < 60)        Templates::$op['casleepfar'] = static fn (array $i, Context $c): ?string => Alis::$platform->version >= 60 ? null            : $c->opername($i['ops'][0][1]) . "\n" . $n . 'casleepfar_core();';        // ctstform: the position of the script, the mask and the form, clipform(), then crstent()        Templates::$op['ctstform'] = static function (array $i, Context $c) use ($n): string {            $k = $n . 'Opcodes2Const::';            return implode("\n", [                sprintf('Alis::$wcx = %sxread16($s->vram_org + %sC::ALIS_SCR_WCX);', $n, $n),                sprintf('Alis::$wcy = %sxread16($s->vram_org + %sALIS_SCR_WCY());', $n, $k),                sprintf('Alis::$wcz = %sxread16($s->vram_org + %sALIS_SCR_WCZ());', $n, $k),                $c->opername($i['ops'][0][1]),                sprintf('Alis::$matmask = %ss16(Alis::$varD7);', $n),                $c->opername($i['ops'][1][1]),                sprintf('Alis::$wforme = %ss16(Alis::$varD7);', $n),                $n . 'clipform();',                self::rst($i['ops'][2][1], $c),            ]);        };        // cnearent (versions 30 .. 59): the distance, the entity, the list, then crstent()        Templates::$op['cnearent'] = static function (array $i, Context $c) use ($n): ?string {            if (Alis::$platform->version < 30 || Alis::$platform->version >= 60) {                return null;            }            return implode("\n", [                'Alis::$fview = 0;',                $c->opername($i['ops'][0][1]),                sprintf('Alis::$valnorme = %smul32(Alis::$varD7, Alis::$varD7);', $n),                $c->opername($i['ops'][1][1]),                $n . 'sviewent_list();',                self::rst($i['ops'][2][1], $c),            ]);        };        // cding (versions < 60, not 10): the four expressions (nothing is played on DOS v3), then the sound        Templates::$op['cding'] = static function (array $i, Context $c) use ($n): ?string {            if (Alis::$platform->version >= 60 || Alis::$platform->version == 10) {                return null;            }            $e = array_map(static fn (array $o): array => $o[1], $i['ops']);            if (\App\Alis\is_rrq_dos_v3()) {                return implode("\n", array_map(static fn (array $x): string => $c->opername($x), $e));            }            return implode("\n", [                sprintf('$pereson = %sget_0x0e_script_ent($s->vram_org) & 0xff;', $n),                $c->opername($e[0]), '$priorson = Alis::$varD7 & 0xff;',                $c->opername($e[1]), sprintf('$volson = %ss16(Alis::$varD7 << 8);', $n),                $c->opername($e[2]), '$freqson = (Alis::$varD7 >> 1) & 0xffff;',                $c->opername($e[3]), '$longson = Alis::$varD7 & 0xffff;',                'if ($longson != 0) {',                sprintf('    $dvolson = %sudiv32($volson, $longson);', $n),                '    if ($dvolson == 0) {',                '        $dvolson = 1;',                '    }',                sprintf('    %srunson(%sC::eChannelTypeDingZap, $pereson, $priorson, $volson, $freqson, $longson, %ss16(-%ss16($dvolson)), 0);', $n, $n, $n, $n),                '}',            ]);        };        // cdefmap (Robinson's Requiem PC): the map's header, one expression after the other        Templates::$op['cdefmap'] = static function (array $i, Context $c) use ($n): ?string {            if (!self::rr()) {                return null;            }            $m = '$t' . (++$c->tmp);            $e = array_map(static fn (array $o): array => $o[1], array_slice($i['ops'], 1));            $out = [self::base($i['ops'][0], $m)];            $w16 = static fn (string $off, string $val): string => sprintf('%sxwrite16(%s - %s, %s);', $n, $m, $off, $val);            $out[] = $c->opername($e[0]);            $out[] = sprintf('%sxwrite8(%s - 0x400, %ss8(Alis::$varD7));', $n, $m, $n);            $out[] = $c->opername($e[1]);            $out[] = $w16('0x3fe', 'Alis::$varD7');            $out[] = $w16('0x3fc', $n . 'get_0x0e_script_ent($s->vram_org)');            $out[] = $c->opername($e[2]);            $out[] = $w16('0x3fa', 'Alis::$varD7');            $out[] = $w16('0x3f8', 'Alis::$varD7');            $out[] = $c->opername($e[3]);            $out[] = $w16('0x3f8', sprintf('Alis::$varD7 - 1 + %sxread16(%s - 0x3f8)', $n, $m));            foreach ([4 => '0x3f4', 5 => '0x3f2', 6 => '0x3f0'] as $k => $off) {                $out[] = $c->opername($e[$k]);                $out[] = 'if (Alis::$varD7 == 0) {' . "\n" . '    Alis::$varD7 = 1;' . "\n" . '}';                $out[] = $w16($off, 'Alis::$varD7');            }            $out[] = sprintf('%sxwrite8(%s - 0x3ff, 0);', $n, $m);            $out[] = sprintf('%sxwrite32(%s - 0x3dc, 4);', $n, $m);            $out[] = sprintf('%sxwrite8(%s - 0x3d8, 0x7f);', $n, $m);            $out[] = sprintf('%scdefmap_rr_tail(%s);', $n, $m);            return implode("\n", $out);        };        // csetmap (Robinson's Requiem, PC or 3DO)        Templates::$op['csetmap'] = static function (array $i, Context $c) use ($n): ?string {            if (!self::rr(true)) {                return null;            }            $m = '$t' . (++$c->tmp);            $e = array_map(static fn (array $o): array => $o[1], array_slice($i['ops'], 1));            $w16 = static fn (string $off, string $val): string => sprintf('%sxwrite16(%s - %s, %s);', $n, $m, $off, $val);            $out = [self::base($i['ops'][0], $m)];            $out[] = $c->opername($e[0]);            $out[] = $w16('0x3ee', 'Alis::$varD7');            $out[] = $c->opername($e[1]);            $out[] = $w16('0x3ec', 'Alis::$varD7');            $out[] = $c->opername($e[2]);            $out[] = $w16('0x3ea', 'Alis::$varD7');            $out[] = $c->opername($e[3]);            $out[] = $w16('0x3e8', 'Alis::$varD7');            $out[] = $w16('0x3d6', '(Alis::$varD7 & 0xffff) - 1');            $out[] = $w16('0x3ee', sprintf('(((Alis::$varD7 - 1) & 0xffff) >> 1) + %sxread16(%s - 0x3ee)', $n, $m));            $out[] = $c->opername($e[4]);            $out[] = $w16('0x3e6', 'Alis::$varD7');            $out[] = $c->opername($e[5]);            $out[] = $w16('0x3e4', 'Alis::$varD7');            $out[] = $w16('0x3d4', '(Alis::$varD7 & 0xffff) - 1');            $out[] = $w16('0x3ea', sprintf('(((Alis::$varD7 - 1) & 0xffff) >> 1) + %sxread16(%s - 0x3ea)', $n, $m));            return implode("\n", $out);        };        // cputmap (Robinson's Requiem PC): the position and the element, then the map's sprite        Templates::$op['cputmap'] = static function (array $i, Context $c) use ($n): ?string {            if (!self::rr()) {                return null;            }            $m = '$t' . (++$c->tmp);            $e = array_map(static fn (array $o): array => $o[1], array_slice($i['ops'], 1));            $w16 = static fn (string $off, string $val): string => sprintf('%sxwrite16(%s - %s, %s);', $n, $m, $off, $val);            $new = '$t' . (++$c->tmp);            $old = '$t' . (++$c->tmp);            $spr = '$t' . (++$c->tmp);            return implode("\n", [                self::base($i['ops'][0], $m),                $c->opername($e[0]), $w16('0x3e2', 'Alis::$varD7'),                $c->opername($e[1]), $w16('0x3e0', 'Alis::$varD7'),                $c->opername($e[2]), $w16('0x3de', 'Alis::$varD7'),                $c->opername($e[3]),                sprintf('%sImg::$numelem = %ss8(Alis::$varD7);', $n, $n),                sprintf('$a = %sxread16(%s - 0x3de);', $n, $m),                sprintf('$b = %sxread16(%s - 0x3f0) & 0xffff;', $n, $m),                sprintf('$a = %sumod32($a, $b) & 0xffff;', $n),                $w16('0x3d2', $n . 's16($a)'),                'Alis::$fmuldes = 0;',                "$new = 0;",                "$old = 0;",                sprintf('if (!%ssearchelem(%s, %s)) {', $n, $new, $old),                sprintf('    %screatelem(%s, %s);', $n, $new, $old),                '}',                sprintf('%s = %ssprite_var(%s);', $spr, $n, $new),                sprintf('if (-1 < %s->state) {', $spr),                sprintf('    %s->state = 2;', $spr),                '}',                sprintf('%sputmapin(%s, %s - 0x3dc);', $n, $new, $m),                sprintf('if (%ssearchelem(%s, %s)) {', $n, $new, $old),                sprintf('    %s = %ssprite_var(%s);', $spr, $n, $new),                sprintf('    %s->credon_off = -1;', $spr),                sprintf('    %s->flaginvx = 0;', $spr),                sprintf('    %s->chsprite = 0;', $spr),                sprintf('    %s->depx = %sxread16(%s - 0x3ee);', $spr, $n, $m),                sprintf('    %s->depy = %sxread16(%s - 0x3ec);', $spr, $n, $m),                sprintf('    %s->depz = %sxread16(%s - 0x3ea);', $spr, $n, $m),                '}',            ]);        };    }}