KO
games/alis / src / Alis / Compiled / Templates / Ops02.php · commit 9c722cf
<?phpnamespace App\Alis\Compiled\Templates;use App\Alis\Compiled\Context;use App\Alis\Compiled\Templates;/** * Instructions 0x37-0x4b (canim .. cerasen) for the games below version 60 (the compiler leaves v6 to the VM). * Those whose handler reads the stream in a way the decoder does not give (cwlive, clive, cload, cdefsc, cputnat, * cunload) stay with the VM. */final class Ops02{    public static function register(): void    {        // no operand: the VM's own function, no stream read        foreach (['canim', 'cclipping', 'cerase', 'cstopret', 'cexit', 'csleep'] as $n) {            Templates::$op[$n] = static fn (array $i, Context $c): string => "\\App\\Alis\\$n();";        }        Templates::$op['cswitching'] = static fn (array $i, Context $c): string => 'Alis::$fswitch = 1;';        Templates::$op['cstop'] = static fn (array $i, Context $c): string => 'Alis::$script->running = 0;';        // fn/opcodes_a.php cscreen() (version < 60)        Templates::$op['cscreen'] = static fn (array $i, Context $c): string => sprintf(            '$t = %d;' . "\n" . 'if ($t != \App\Alis\get_0x16_screen_id(Alis::$script->vram_org)) { \App\Alis\set_0x16_screen_id(Alis::$script->vram_org, $t); }',            $i['ops'][0][1] & 0xffff        );        // ckill: D7 saved, then killent        Templates::$op['ckill'] = static fn (array $i, Context $c): string => $c->opernameSaveD7($i['ops'][0][1]) . "\n"            . 'if (Alis::$platform->version >= 30 ? \App\Alis\s16(Alis::$varD7) > 0 : Alis::$varD7 >= 0) { \App\Alis\killent(Alis::$varD7 & 0xffff); }';        // cwakeup        Templates::$op['cwakeup'] = static fn (array $i, Context $c): string => $c->opername($i['ops'][0][1]) . "\n"            . 'if (-1 < Alis::$varD7) { $t = \App\Alis\xread32(Alis::$atent + Alis::$varD7); if ($t) { \App\Alis\set_0x04_cstart_csleep($t, 1); } }';        // cerasen        Templates::$op['cerasen'] = static fn (array $i, Context $c): string => $c->opernameSaveD7($i['ops'][0][1]) . "\n"            . '\App\Alis\Img::$numelem = \App\Alis\s8(Alis::$varD7);' . "\n"            . '$t1 = 0; $t2 = 0;' . "\n"            . 'while (\App\Alis\searchelem($t1, $t2)) { \App\Alis\killelem($t1, $t2); }' . "\n"            . 'Alis::$ferase = 0;';        // cput (version < 60)        Templates::$op['cput'] = static fn (array $i, Context $c): string => 'Alis::$flagmain = 0;' . "\n"            . '\App\Alis\Img::$invert_x = \App\Alis\get_0x03_xinv(Alis::$script->vram_org);' . "\n"            . $c->opername($i['ops'][0][1]) . "\n"            . '\App\Alis\Img::$depx = 0; \App\Alis\Img::$depy = 0; \App\Alis\Img::$depz = 0; \App\Alis\Img::$numelem = 0;' . "\n"            . '\App\Alis\put(Alis::$varD7 & 0xffff);';        // cscmov / cscset (version >= 30 branch and the older one, as the VM)        Templates::$op['cscmov'] = static fn (array $i, Context $c): string => self::three($i, $c, true);        Templates::$op['cscset'] = static fn (array $i, Context $c): string => self::three($i, $c, false);    }    private static function three(array $i, Context $c, bool $mov): string    {        $save = $mov ? 'opernameSaveD7' : 'opername';        $php = '';        foreach (['x', 'y', 'z'] as $k => $v) {            $php .= $c->$save($i['ops'][$k][1]) . "\n" . "\$$v = \\App\\Alis\\s16(Alis::\$varD7);\n";        }        $php .= '$t = \App\Alis\get_0x16_screen_id(Alis::$script->vram_org);' . "\n";        $A = '\App\Alis\\';        $old = ['z' => '2e', 'y' => '2c', 'x' => '2a'];        $dep = ['z' => 'depz', 'y' => 'depy', 'x' => 'depx'];        $php .= 'if (Alis::$platform->version < 30) {' . "\n";        foreach ($old as $v => $h) {            $val = $mov ? "\$$v" : "\$$v - {$A}get_scr_{$dep[$v]}(\$t)";            $php .= "{$A}set_scr_unknown0x$h(\$t, $val);\n";        }        $php .= "{$A}set_scr_state(\$t, {$A}get_scr_state(\$t) | 0x80);\n} else {\n";        foreach ($dep as $v => $d) {            $cond = $mov ? "\$$v != 0" : "\$$v != {$A}get_scr_$d(\$t)";            $val = $mov ? "{$A}get_scr_$d(\$t) + \$$v" : "\$$v";            $php .= "if ($cond) { {$A}set_scr_$d(\$t, $val); {$A}set_scr_state(\$t, {$A}get_scr_state(\$t) | 0x80); }\n";        }        return $php . '}';    }}