version >= 60) { cwmov_v6branch(); return; } $x = 0; $y = 0; $z = 0; get_vector($x, $y, $z); xadd16(Alis::$script->vram_org + C::ALIS_SCR_WCX, $x); xadd16(Alis::$script->vram_org + Opcodes2Const::ALIS_SCR_WCY(), $y); xadd16(Alis::$script->vram_org + Opcodes2Const::ALIS_SCR_WCZ(), $z); } // Codopname no. 164 opcode 0xa3 cwtstmov function cwtstmov(): void { // MISSING in the C engine } // Codopname no. 165 opcode 0xa4 cwftstmov function cwftstmov(): void { // MISSING in the C engine } // Codopname no. 166 opcode 0xa5 ctstform function ctstform(): void { Alis::$wcx = xread16(Alis::$script->vram_org + C::ALIS_SCR_WCX); Alis::$wcy = xread16(Alis::$script->vram_org + Opcodes2Const::ALIS_SCR_WCY()); Alis::$wcz = xread16(Alis::$script->vram_org + Opcodes2Const::ALIS_SCR_WCZ()); readexec_opername(); Alis::$matmask = s16(Alis::$varD7); readexec_opername(); Alis::$wforme = s16(Alis::$varD7); clipform(); crstent(); } // Codopname no. 167 opcode 0xa6 cxput function cxput(): void { Alis::$flagmain = 0; Img::$invert_x = (get_0x03_xinv(Alis::$script->vram_org) ^ 1) & 0xff; readexec_opername(); Img::$depx = 0; Img::$depy = 0; Img::$depz = 0; Img::$numelem = 0; $idx = Alis::$varD7 & 0xffff; put($idx); } // Codopname no. 168 opcode 0xa7 cxputat function cxputat(): void { Alis::$flagmain = 0; Img::$invert_x = (get_0x03_xinv(Alis::$script->vram_org) ^ 1) & 0xff; readexec_opername_saveD7(); Img::$depx = s16(Alis::$varD7); readexec_opername_saveD7(); Img::$depy = s16(Alis::$varD7); readexec_opername_saveD7(); Img::$depz = s16(Alis::$varD7); readexec_opername_saveD7(); readexec_opername_saveD6(); $idx = Alis::$varD7 & 0xffff; Img::$numelem = s8(Alis::$varD6); put($idx); } // Codopname no. 169 opcode 0xa8 cmput function cmput(): void { Alis::$flagmain = 1; Img::$invert_x = get_0x03_xinv(Alis::$script->vram_org); readexec_opername(); $idx = Alis::$varD7 & 0xffff; Img::$depx = 0; Img::$depy = 0; Img::$depz = 0; Img::$numelem = 0; put($idx); } // Codopname no. 170 opcode 0xa9 cmputat function cmputat(): void { Alis::$flagmain = 1; Img::$invert_x = get_0x03_xinv(Alis::$script->vram_org); readexec_opername_saveD7(); Img::$depx = s16(Alis::$varD7); readexec_opername_saveD7(); Img::$depy = s16(Alis::$varD7); readexec_opername_saveD7(); Img::$depz = s16(Alis::$varD7); readexec_opername_saveD7(); readexec_opername_saveD6(); Img::$numelem = s8(Alis::$varD6); put(Alis::$varD7 & 0xffff); } // Codopname no. 171 opcode 0xaa cmxput function cmxput(): void { Alis::$flagmain = 1; Img::$invert_x = (get_0x03_xinv(Alis::$script->vram_org) ^ 1) & 0xff; readexec_opername(); Img::$depx = 0; Img::$depy = 0; Img::$depz = 0; Img::$numelem = 0; put(Alis::$varD7 & 0xffff); } // Codopname no. 172 opcode 0xab cmxputat function cmxputat(): void { Alis::$flagmain = 1; Img::$invert_x = (get_0x03_xinv(Alis::$script->vram_org) ^ 1) & 0xff; readexec_opername_saveD7(); Img::$depx = s16(Alis::$varD7); readexec_opername_saveD7(); Img::$depy = s16(Alis::$varD7); readexec_opername_saveD7(); Img::$depz = s16(Alis::$varD7); readexec_opername_saveD7(); readexec_opername_saveD6(); Img::$numelem = s8(Alis::$varD6); put(Alis::$varD7 & 0xffff); } // Codopname no. 173 opcode 0xac cmmusic function cmmusic(): void { if (Alis::$platform->uid == C::EGameRobinsonsRequiem3DO) { for ($i = 0; $i < 6; $i++) { readexec_opername(); } return; } Alis::$flagmain = 1; music(); } // Codopname no. 174 opcode 0xad cmforme function cmforme(): void { readexec_opername(); set_0x1a_cforme(Alis::$script->vram_org, Alis::$varD7); } // Codopname no. 175 opcode 0xae csettime function csettime(): void { readexec_opername(); $h = Alis::$varD7 & 0xffff; readexec_opername(); $m = Alis::$varD7 & 0xffff; readexec_opername(); $s = Alis::$varD7 & 0xffff; sys_set_time($h, $m, $s); } // Codopname no. 176 opcode 0xaf cgettime function cgettime(): void { // (sic) the C shifts left then masks: only the last value is non zero $t = sys_get_time(); Alis::$varD7 = ($t << 16) & 0xff; cstore_continue(); Alis::$varD7 = ($t << 8) & 0xff; cstore_continue(); Alis::$varD7 = ($t << 0) & 0xff; cstore_continue(); } // PORT: the C busy-waits on io_getkey() (key press, then release). Non-blocking here: the keys are // polled with io_inkey(); a key is taken when it is released (like io_getkey). While Enter has not // been typed, vm_yield_retry() restarts the calling opcode (cvinput) at the next frame. Its only // side effect before getval() is `alis.charmode = 0`, which is idempotent. The typed length and the // pending key are kept in Opcodes2Vars::$input_len / $input_key (characters are already in memory at // alis.sd7 and already echoed). function getval(): void { $ptr = Alis::$sd7 + Opcodes2Vars::$input_len; while (Alis::$state) { $c = input_nextkey(); if ($c < 0) { Opcodes2Vars::$input_len = $ptr - Alis::$sd7; vm_yield_retry(); } if ($c == 0x0d) { // '\r' break; } if ($c == 0x08) { // '\b' if ($ptr != Alis::$sd7) { $ptr--; Alis::$mem[$ptr] = "\0"; put_char(0x08); put_char(0x20); put_char(0x08); } } elseif (($c == 0x2d) || ((-1 < s8($c - 0x30)) && ($c < 0x3a))) { Alis::$mem[$ptr++] = chr($c); put_char(s8($c)); } } Opcodes2Vars::$input_len = 0; Opcodes2Vars::$input_key = 0; Alis::$mem[$ptr] = "\0"; oval(); } /** * PORT: non-blocking io_getkey() for getval()/getstring(): returns the key once it has been pressed * then released, or -1 when nothing is available yet (the caller yields). */ function input_nextkey(): int { if (Opcodes2Vars::$input_key != 0) { if (io_inkey() != 0) { return -1; // still held } $c = Opcodes2Vars::$input_key; Opcodes2Vars::$input_key = 0; return $c; } $c = io_inkey() & 0xff; if ($c == 0) { return -1; } Opcodes2Vars::$input_key = $c; return -1; // wait for the release } // Codopname no. 177 opcode 0xb0 cvinput function cvinput(): void { Alis::$charmode = 0; getval(); cstore_continue(); } // PORT: non-blocking, see getval(). function getstring(): void { $ptr = Alis::$sd7 + Opcodes2Vars::$input_len; while (Alis::$state) { $c = input_nextkey(); if ($c < 0) { Opcodes2Vars::$input_len = $ptr - Alis::$sd7; vm_yield_retry(); } if ($c == 0x0d) { // '\r' break; } if ($c == 0x08) { // '\b' if ($ptr != Alis::$sd7) { $ptr--; Alis::$mem[$ptr] = "\0"; put_char(0x08); put_char(0x20); put_char(0x08); } } else { Alis::$mem[$ptr++] = chr($c); put_char(s8($c)); } } Opcodes2Vars::$input_len = 0; Opcodes2Vars::$input_key = 0; Alis::$mem[$ptr] = "\0"; } // Codopname no. 178 opcode 0xb1 csinput function csinput(): void { Alis::$charmode = 0; getstring(); cstore_continue(); } function sendmess(int $vram, int $idx): void { if ((get_0x24_scan_inter($vram) & 1) == 0) { $val = s16(get_0x1c_scan_clr($vram) + 2); if (-0x3f < $val) { $val = s16($val - xread16(get_0x14_script_org_offset($vram) + 0x16)); } if ($val == get_0x1e_scan_clr($vram)) { set_0x1c_scan_clr($vram, $val); } else { xwrite16($vram + get_0x1c_scan_clr($vram), $idx); set_0x1c_scan_clr($vram, $val); set_0x24_scan_inter($vram, get_0x24_scan_inter($vram) | 0x80); } } } // Codopname no. 179 opcode 0xb2 casleepfar function casleepfar(): void { if (Alis::$platform->version >= 60) { casleepfar_v6(); return; } readexec_opername(); casleepfar_core(); } /** casleepfar() (versions < 60) once its operand is in D7 */ function casleepfar_core(): void { Alis::$valnorme = mul32(Alis::$varD7 & 0xffff, Alis::$varD7 & 0xffff); $ent = s16(alis_first_ent()); do { $vram = xread32(Alis::$atent + $ent); if ($vram != 0 && (get_0x24_scan_inter($vram) & 4) == 0) { $xdif = xread16($vram) - xread16(Alis::$script->vram_org); $ydif = xread16($vram + 0x8) - xread16(Alis::$script->vram_org + 0x8); $zdif = xread16($vram + 0x10) - xread16(Alis::$script->vram_org + 0x10); if (ult(s32(mul32($zdif, $zdif) + mul32($ydif, $ydif) + mul32($xdif, $xdif)), Alis::$valnorme)) { if (get_0x04_cstart_csleep($vram) == 0) { set_0x04_cstart_csleep($vram, 1); sendmess($vram, 0x8001); sendmess($vram, get_0x0e_script_ent(Alis::$script->vram_org)); } } else { if (get_0x04_cstart_csleep($vram) != 0) { set_0x04_cstart_csleep($vram, 0); sendmess($vram, 0x8000); sendmess($vram, get_0x0e_script_ent(Alis::$script->vram_org)); } } } } while (($ent = xread16(Alis::$atent + 4 + $ent))); } // Codopname no. 180 opcode 0xb3 casleepon function casleepon(): void { set_0x24_scan_inter(Alis::$script->vram_org, get_0x24_scan_inter(Alis::$script->vram_org) & 0xfb); } // Codopname no. 181 opcode 0xb4 casleepoff function casleepoff(): void { set_0x24_scan_inter(Alis::$script->vram_org, get_0x24_scan_inter(Alis::$script->vram_org) | 4); } // Codopname no. 182 opcode 0xb5 crunfilm function crunfilm(): void { readexec_opername(); film_check_boot(); if (Alis::$platform->uid == C::EGameRobinsonsRequiem3DO) { film3do_run(Alis::$varD7); Alis::$varD7 = 0; cstore_continue(); Alis::$varD7 = 0; cstore_continue(); return; } // PORT: runfilm() yields between the film frames and this opcode restarts: batchframes is set once. if (!film_run_resuming()) { FilmVars::$bfilm->batchframes = s16(Alis::$varD7); } runfilm(); Alis::$varD7 = FilmVars::$bfilm->frame; cstore_continue(); Alis::$varD7 = FilmVars::$bfilm->result; cstore_continue(); } function printd0(int $d0w): void { $ptr = Alis::$sd7; valtostr(Alis::$sd7, $d0w); while (Alis::$mem[$ptr] !== "\0") { put_char(s8(ord(Alis::$mem[$ptr++]))); } } function putval(int $d7w): void { printd0($d7w); } // Codopname no. 183 opcode 0xb6 cvpicprint function cvpicprint(): void { Alis::$charmode = 1; readexec_opername_saveD7(); putval(s16(Alis::$varD7)); } // Codopname no. 184 opcode 0xb7 cspicprint function cspicprint(): void { Alis::$charmode = 1; readexec_opername_swap(); put_string(); } // Codopname no. 185 opcode 0xb8 cvputprint function cvputprint(): void { if (Alis::$platform->version >= 60) { Alis::$charmode = 2; readexec_opername(); v6x_textput((string) s32(Alis::$varD7)); return; } Alis::$charmode = 2; readexec_opername_saveD7(); putval(s16(Alis::$varD7)); } // Codopname no. 186 opcode 0xb9 csputprint function csputprint(): void { if (Alis::$platform->version >= 60) { Alis::$charmode = 2; readexec_opername_swap(); v6x_textput(mem_str(Alis::$sd7)); return; } Alis::$charmode = 2; readexec_opername_swap(); put_string(); } // Codopname no. 187 opcode 0xba cfont function cfont(): void { readexec_opername(); if (sys_is_dos_v3()) { cfont_dos_v3(); return; } Alis::$foasc = Alis::$varD7 & 0xffff; readexec_opername(); Alis::$fonum = Alis::$varD7 & 0xffff; readexec_opername(); Alis::$folarg = Alis::$varD7 & 0xff; readexec_opername(); Alis::$fohaut = Alis::$varD7 & 0xff; readexec_opername(); Alis::$fomax = Alis::$varD7 & 0xffff; } /** * v3 DOS font (0x3c64), first operand already in D7: `font -16,colour,_,_,_` only sets the text colour and keeps * the font; any other negative first operand means no font (foasc 0xffff, fonum 0) but still sets the advance, * the line height and the count; otherwise the five fields, fonum signed (put_char tests it with js). */ function cfont_dos_v3(): void { $a = s16(Alis::$varD7); if ($a == -16) { readexec_opername(); Alis::$focolor = Alis::$varD7 & 0xff; readexec_opername(); readexec_opername(); readexec_opername(); return; } if ($a < 0) { Alis::$foasc = 0xffff; Alis::$fonum = 0; readexec_opername(); } else { Alis::$foasc = $a & 0xffff; readexec_opername(); Alis::$fonum = s16(Alis::$varD7); } readexec_opername(); Alis::$folarg = Alis::$varD7 & 0xff; readexec_opername(); Alis::$fohaut = Alis::$varD7 & 0xff; readexec_opername(); Alis::$fomax = Alis::$varD7 & 0xffff; } // Codopname no. 188 opcode 0xbb cpaper function cpaper(): void { // STUBBED in the C engine readexec_opername(); } // Codopname no. 189 opcode 0xbc ctoblack // fade-out to black function ctoblack(): void { readexec_opername_saveD6(); $duration = s16(Alis::$varD6); if (dos_pal_active()) { dos_ctoblack($duration); } else { toblackpal($duration); } } function subcol(int $change, int $component): int { $result = s16(($component & 0xf) - ($change & 0xf)); if ($result < 0) { return 0; } if ($result < 8) { return $result; } return 7; } function addcol(int $change, int $component): int { $result = s16(($component & 0xf) + ($change & 0xf)); if ($result < 0) { return 0; } if ($result < 8) { return $result; } return 7; } // Codopname no. 190 opcode 0xbd cmovcolor function cmovcolor(): void { readexec_opername(); readexec_opername_saveD6(); $index = s16(Alis::$varD7); $change = s16(Alis::$varD6); if (Alis::$platform->bpp == 4) { // palette entries are packed 0xRRGGBB (PAL_READ_RGB / PAL_WRITE_RGB) $v = Img::$mpalet[$index]; $r = ($v >> 16) & 0xff; $g = ($v >> 8) & 0xff; $b = $v & 0xff; if ($change < 0) { $change = s16(-$change); $r = (subcol($change, $r >> 5) << 5) & 0xff; $g = (subcol($change >> 4, $g >> 5) << 5) & 0xff; $b = (subcol($change >> 8, $b >> 5) << 5) & 0xff; } else { $r = (addcol($change, $r >> 5) << 5) & 0xff; $g = (addcol($change >> 4, $g >> 5) << 5) & 0xff; $b = (addcol($change >> 8, $b >> 5) << 5) & 0xff; } Img::$mpalet[$index] = ($r << 16) | ($g << 8) | $b; Img::$ftopal = 0xff; Img::$thepalet = 0; Img::$defpalet = 0; set_update_cursor(); } } // Codopname no. 191 opcode 0xbe ctopalet // fade-in to palette function ctopalet(): void { readexec_opername(); readexec_opername_saveD6(); ctopalet_core(); } /** ctopalet() once its two operands are in D7 / D6 (the compiled scripts call it) */ function ctopalet_core(): void { Alis::$flagmain = 0; $palidx = s16(Alis::$varD7); // negative = restore saved palette (original: tst.w / bmi) $duration = Alis::$varD6 & 0xffff; if (dos_pal_active()) { $addr = $palidx < 0 ? 0 : adresdes($palidx); // PORT: C passes NULL when palidx < 0: 0 here. dos_ctopalet($palidx, s16($duration), $palidx < 0 ? 0 : $addr + xread32($addr)); } elseif ($palidx < 0) { restorepal($palidx, $duration); } else { $addr = adresdes($palidx); $paldata = $addr + xread32($addr); topalette($paldata, $duration); } } // Codopname no. 192 opcode 0xbf cnumput function cnumput(): void { if (Alis::$platform->version >= 60) { cnumput_v6branch(); return; } readexec_opername(); Img::$depx = s16(Alis::$varD7); readexec_opername(); Img::$depy = s16(Alis::$varD7); readexec_opername(); Img::$depz = s16(Alis::$varD7); readexec_opername(); Img::$numelem = s8(Alis::$varD7); } // Codopname no. 193 opcode 0xc0 cscheart function cscheart(): void { readexec_opername(); readexec_opername_saveD6(); if (Alis::$platform->version >= 60) { $scr = s16(xread16(Alis::$script->vram_org - 0x64)); if ($scr != 0) { $s = s32(Alis::$basemain + ($scr & 0xffff)); xwrite16($s + 0x62, Alis::$varD7); xwrite16($s + 0x64, Alis::$varD6); xwrite8($s + 0x58, xread8($s + 0x58) | 0x80); } return; } $screen_id = s16(get_0x16_screen_id(Alis::$script->vram_org)); if ($screen_id != 0) { set_scr_unknown0x0a($screen_id, Alis::$varD7); set_scr_unknown0x0c($screen_id, Alis::$varD6); set_scr_state($screen_id, get_scr_state($screen_id) | 0x80); } } // Codopname no. 194 opcode 0xc1 cscpos function cscpos(): void { readexec_opername(); readexec_opername_saveD6(); cscpos_core(); } /** cscpos() once its two operands are in D7 / D6 */ function cscpos_core(): void { if (Alis::$platform->version >= 60) { $scr = s16(xread16(Alis::$script->vram_org - 0x64)); if ($scr != 0) { $s = s32(Alis::$basemain + ($scr & 0xffff)); $x = s16(Alis::$varD7); $y = s16(Alis::$varD6); xwrite16($s + 0x66, $x); xwrite16($s + 0x68, $y); xwrite16($s + 0x6a, $x + s16(xread16($s + 0x6e)) - 1); xwrite16($s + 0x6c, $y + s16(xread16($s + 0x70)) - 1); v6_screfresh($s); xwrite8($s + 0x58, xread8($s + 0x58) | 0x80); } return; } $screen_id = s16(get_0x16_screen_id(Alis::$script->vram_org)); if ($screen_id != 0) { if (Alis::$platform->kind == C::EPlatformMac) { $t7 = s16(Alis::$varD7); $t6 = s16(Alis::$varD6); mac_update_pos($t7, $t6); Alis::$varD7 = $t7; Alis::$varD6 = $t6; } set_scr_newx($screen_id, Alis::$varD7); set_scr_newy($screen_id, Alis::$varD6); set_scr_state($screen_id, get_scr_state($screen_id) | 0x80); } } // Codopname no. 195 opcode 0xc2 cscsize function cscsize(): void { readexec_opername(); readexec_opername_saveD6(); cscsize_core(); } /** cscsize() once its two operands are in D7 / D6 */ function cscsize_core(): void { if (Alis::$platform->version >= 60) { $scr = s16(xread16(Alis::$script->vram_org - 0x64)); if ($scr != 0) { $s = s32(Alis::$basemain + ($scr & 0xffff)); $w = s16(Alis::$varD7); $h = s16(Alis::$varD6); xwrite16($s + 0x6e, $w); xwrite16($s + 0x70, $h); xwrite16($s + 0x6a, $w + s16(xread16($s + 0x66)) - 1); xwrite16($s + 0x6c, $h + s16(xread16($s + 0x68)) - 1); v6_screfresh($s); xwrite8($s + 0x58, xread8($s + 0x58) | 0x80); } return; } $screen_id = s16(get_0x16_screen_id(Alis::$script->vram_org)); if ($screen_id != 0) { if (Alis::$platform->version < 30) { if (Alis::$platform->kind == C::EPlatformMac) { $t7 = s16(Alis::$varD7 - 1); $t6 = s16(Alis::$varD6 - 1); mac_update_pos($t7, $t6); Alis::$varD7 = $t7 + 1; Alis::$varD6 = $t6 + 1; } set_scr_width($screen_id, Alis::$varD7); set_scr_height($screen_id, Alis::$varD6); } else { set_scr_width($screen_id, Alis::$varD7 - 1); set_scr_height($screen_id, Alis::$varD6 - 1); scdosprite($screen_id); } set_scr_state($screen_id, get_scr_state($screen_id) | 0x80); } } // Codopname no. 196 opcode 0xc3 cschoriz function cschoriz(): void { $screen_id = s16(get_0x16_screen_id(Alis::$script->vram_org)); if ($screen_id != 0) { readexec_opername(); set_scr_unknown0x20($screen_id, Alis::$varD7 & 0xff); readexec_opername(); set_scr_unknown0x21($screen_id, Alis::$varD7 & 0xff); readexec_opername(); set_scr_unknown0x22($screen_id, Alis::$varD7 & 0xff); set_scr_state($screen_id, get_scr_state($screen_id) | 0x80); } } // Codopname no. 197 opcode 0xc4 cscvertic function cscvertic(): void { if (Alis::$platform->uid == C::EGameRobinsonsRequiem3DO) { readexec_opername(); return; } $screen_id = s16(get_0x16_screen_id(Alis::$script->vram_org)); if ($screen_id != 0) { readexec_opername(); set_scr_unknown0x23($screen_id, Alis::$varD7 & 0xff); readexec_opername(); set_scr_unknown0x24($screen_id, Alis::$varD7 & 0xff); readexec_opername(); set_scr_unknown0x25($screen_id, Alis::$varD7 & 0xff); set_scr_state($screen_id, get_scr_state($screen_id) | 0x80); } } // Codopname no. 198 opcode 0xc5 cscreduce function cscreduce(): void { if (Alis::$platform->uid == C::EGameRobinsonsRequiem3DO) { // (C: warns once "cscreduce 3DO: entity wake/sleep by distance not ported") readexec_opername(); readexec_opername(); return; } readexec_opername(); readexec_opername_saveD6(); $screen_id = get_0x16_screen_id(Alis::$script->vram_org); if ($screen_id != 0) { set_scr_creducing($screen_id, Alis::$varD7); readexec_opername(); set_scr_redmax($screen_id, (Alis::$varD6 - 1) & 0xff); set_scr_redshift($screen_id, Alis::$varD7 & 0xff); set_scr_state($screen_id, get_scr_state($screen_id) | 0x80); } } // Codopname no. 199 opcode 0xc6 cscscale function cscscale(): void { if (Alis::$platform->uid == C::EGameRobinsonsRequiem3DO) { readexec_opername(); $id = xread16(Alis::$script->vram_org - 0x64) & 0xffff; if ($id == 0) { return; } $scr = s32(Alis::$basemain + $id); for ($i = 0; $i < 3; $i++) { readexec_opername(); xwrite16($scr + 0x1e + $i * 2, s16(Alis::$varD7)); } xwrite8($scr + 0x58, xread8($scr + 0x58) | 0x80); return; } readexec_opername(); $scale = s8(Alis::$varD7); $screen_id = get_0x16_screen_id(Alis::$script->vram_org); if ($screen_id != 0) { set_scr_credon_off($screen_id, $scale); set_scr_state($screen_id, get_scr_state($screen_id) | 0x80); } } // Codopname no. 200 opcode 0xc7 creducing function creducing(): void { readexec_opername(); set_0x26_creducing(Alis::$script->vram_org, Alis::$varD7); readexec_opername(); set_0x27_creducing(Alis::$script->vram_org, Alis::$varD7); } // Codopname no. 201 opcode 0xc8 cscmap function cscmap(): void { readexec_opername(); $ent = get_0x0e_script_ent(Alis::$script->vram_org); $val = s16(script_read16()); if ($val == 0) { $ent = 0; $val = s16(script_read16()); } if (get_0x16_screen_id(Alis::$script->vram_org) != 0) { $tgtaddr = Alis::$basemain + get_0x16_screen_id(Alis::$script->vram_org); xwrite16($tgtaddr + 0x42, $val); xwrite16($tgtaddr + 0x40, $ent); } } // Codopname no. 203 opcode 0xca cfindcla function cfindcla(): void { readexec_opername(); $tabidx = 0; $entidx = s16(alis_first_ent()); if (Alis::$platform->version >= 60) { do { $tgt_vram = xread32(Alis::$atent + $entidx); if ($tgt_vram != 0 && s16(Alis::$varD7) == xread16($tgt_vram + 0x46)) { if (tabent_full($tabidx)) { break; } Alis::$matent[$tabidx] = 0; Alis::$tablent[$tabidx] = $entidx; $tabidx++; if (Alis::$fallent == 0) { break; } } $entidx = xread16(Alis::$atent + $entidx + 4); } while ($entidx != 0); Alis::$tablent[$tabidx] = ENTLIST_END(); Alis::$fallent = 0; crstent(); return; } $veradd = Alis::$platform->version >= 30 ? 0x4c : 0xc; do { $tgt_vram = xread32(Alis::$atent + $entidx); if (s8(Alis::$varD7) == s8(xread8($tgt_vram + $veradd)) && Alis::$script->vram_org != $tgt_vram) { if (tabent_full($tabidx)) { break; } Alis::$matent[$tabidx] = 0; Alis::$tablent[$tabidx] = $entidx; $tabidx++; if (Alis::$fallent == 0) { break; } } $entidx = xread16(Alis::$atent + $entidx + 4); } while ($entidx != 0); Alis::$tablent[$tabidx] = ENTLIST_END(); Alis::$fallent = 0; crstent(); } // Codopname no. 204 opcode 0xcb cnearcla function cnearcla(): void { if (Alis::$platform->version >= 60) { near_v6(2); return; } } // Codopname no. 205 opcode 0xcc cviewcla function cviewcla(): void { if (Alis::$platform->version >= 60) { near_v6(2); return; } } // Codopname no. 206 opcode 0xcd cinstru function cinstru(): void { Alis::$flagmain = 0; readexec_opername(); $tabidx = s16(Alis::$varD7); readexec_opername(); $scridx = s16(Alis::$varD7); readexec_opername(); $instidx = s16(Alis::$varD7); $type = 0; if ($scridx < 0) { $instidx = 0; $addr = 0; } else { $addr = adresmus($scridx); $type = xread8($addr); if ($type != 1 && $type != 2 && $type != 5 && $type != 6) { return; } $addr += 0x10; } Aud::$tabinst[$tabidx]->address = $addr; Aud::$tabinst[$tabidx]->unknown = $instidx; } // Codopname no. 207 opcode 0xce cminstru function cminstru(): void { Alis::$flagmain = 1; readexec_opername(); $tabidx = s16(Alis::$varD7); readexec_opername(); $scridx = s16(Alis::$varD7); readexec_opername(); $instidx = s16(Alis::$varD7); if ($scridx < 0) { $instidx = 0; $addr = 0; } else { $addr = adresmus($scridx); $type = xread8($addr); if ($type != 1 && $type != 2 && $type != 5 && $type != 6) { return; } $addr += 0x10; } Aud::$tabinst[$tabidx]->address = $addr; Aud::$tabinst[$tabidx]->unknown = $instidx; } // Codopname no. 208 opcode 0xcf cordspr function cordspr(): void { readexec_opername(); set_0x2b_cordspr(Alis::$script->vram_org, Alis::$varD7 & 0xff); } // Codopname no. 209 opcode 0xd0 calign function calign(): void { readexec_opername(); $v = Alis::$varD7 & 0xff; if ($v & 0x10) { $bit = 4; } elseif ($v & 0x8) { $bit = 3; } elseif ($v & 0x4) { $bit = 2; } elseif ($v & 0x2) { $bit = 1; } else { $bit = 0; } set_0x2c_calign(Alis::$script->vram_org, $bit); readexec_opername(); set_0x2d_calign(Alis::$script->vram_org, Alis::$varD7); } function rescmode(int $at, int $value): void { sprite_var($at)->numelem = s8($value); } // Codopname no. 210 opcode 0xd1 cbackstar function cbackstar(): void { // STUBBED in the C engine readexec_opername(); Opcodes2Vars::$starbuff[0] = s16(Alis::$varD7); readexec_opername(); Opcodes2Vars::$starbuff[1] = s16(Alis::$varD7); readexec_opername(); Opcodes2Vars::$starbuff[2] = s16(Alis::$varD7); readexec_opername(); Opcodes2Vars::$starbuff[3] = s16(Alis::$varD7); readexec_opername(); Opcodes2Vars::$starbuff[4] = s16(Alis::$varD7); readexec_opername(); Opcodes2Vars::$starbuff[5] = s16(Alis::$varD7); Opcodes2Vars::$starbuff[6] = 0; switch (Alis::$platform->uid) { case C::EGameColorado: if (-1 < s16(get_0x16_screen_id(Alis::$script->vram_org))) { $value = xread8(Alis::$basemain + 1); $value &= 0xf7; $value &= 0xef; if (Alis::$varD7 != 0) { $value |= 0x18; } xwrite8(Alis::$basemain + 1, $value); } break; case C::EGameManhattanDealers0: case C::EGameManhattanDealers1: case C::EGameMadShow: case C::EGameWindsurfWilly: case C::EGameLeFeticheMaya: // NOP break; case C::EGameStarblade: // TODO (C): starblade implementation differs break; case C::EGameStormMaster: case C::EGameTarghan0: case C::EGameTarghan1: case C::EGameCrystalsOfArborea0: case C::EGameCrystalsOfArborea1: case C::EGameCrystalsOfArborea2: case Opcodes2Const::EGameMetalMutant: case C::EGameTransarctica: case C::EGameBostonBombClub: case C::EGameBunnyBricks: case C::EGameIshar_1: case C::EGameIshar_2: case C::EGameIshar_3: case C::EGameRobinsonsRequiem0: case C::EGameRobinsonsRequiem1: if (-1 < s16(get_0x16_screen_id(Alis::$script->vram_org))) { $value = xread8(Alis::$basemain + 1); $value &= 0xf7; $value &= 0xef; if (Alis::$varD7 != 0) { $value |= 0x18; $at = xread16(Alis::$basemain + get_0x16_screen_id(Alis::$script->vram_org) + 2); rescmode($at, $value); } xwrite8(Alis::$basemain + get_0x16_screen_id(Alis::$script->vram_org) + 1, $value); } break; default: break; } } // Codopname no. 211 opcode 0xd2 cstarring function cstarring(): void { // STUBBED in the C engine readexec_opername(); Opcodes2Vars::$starbuff[0] = s16(Alis::$varD7); readexec_opername(); Opcodes2Vars::$starbuff[1] = s16(Alis::$varD7); readexec_opername(); Opcodes2Vars::$starbuff[2] = s16(Alis::$varD7); readexec_opername(); // not used in colorado if (Alis::$platform->uid != C::EGameColorado) { Opcodes2Vars::$starbuff[3] = s16(Alis::$varD7); readexec_opername(); Opcodes2Vars::$starbuff[4] = s16(Alis::$varD7); readexec_opername(); Opcodes2Vars::$starbuff[5] = s16(Alis::$varD7); readexec_opername(); Opcodes2Vars::$starbuff[6] = s16(Alis::$varD7); readexec_opername(); Opcodes2Vars::$starbuff[7] = s16(Alis::$varD7); } } // Codopname no. 212 opcode 0xd3 cengine // [N/I]: Robinson's Requiem, Ishar 3, Storm Master (IBM PC) function cengine(): void { if (Alis::$platform->uid == C::EGameRobinsonsRequiem3DO) { return; } if (Alis::$platform->kind == C::EPlatformPC) { return; } readexec_opername(); readexec_opername(); readexec_opername(); readexec_opername(); } // Codopname no. 214 opcode 0xd5 cquality function cquality(): void { readexec_opername(); Alis::$vquality = Alis::$varD7 & 0xffff; } // Codopname no. 215 opcode 0xd6 chsprite function chsprite(): void { // STUBBED in the C engine: the itmov locals it computes are never used readexec_opername(); set_0x2f_chsprite(Alis::$script->vram_org, s8(Alis::$varD7)); if (s8(Alis::$varD7) == 9) { readexec_opername(); readexec_opername(); readexec_opername(); } else { readexec_opername(); readexec_opername(); readexec_opername(); if (get_0x2f_chsprite(Alis::$script->vram_org) != 0) { return; } } } // Codopname no. 216 opcode 0xd7 cselpalet function cselpalet(): void { readexec_opername(); if (dos_pal_active()) { dos_cselpalet(Alis::$varD7); return; } Alis::$varD7 &= 0x3; // 4 palettes: 0...3 Img::$thepalet = Alis::$varD7; Img::$defpalet = 1; // the original only records the bank; palette writes select it } // Codopname no. 217 opcode 0xd8 clinepalet function clinepalet(): void { readexec_opername(); readexec_opername_saveD6(); if (Alis::$platform->bpp < 8 && !dos_pal_active() && !(Alis::$platform->uid == Opcodes2Const::EGameMetalMutant && Alis::$platform->kind == C::EPlatformPC)) { setlinepalet(); } } // Codopname no. 218 opcode 0xd9 cautomode function cautomode(): void { if (Alis::$platform->uid == C::EGameRobinsonsRequiem3DO) { readexec_opername(); return; } // MISSING in the C engine } // Codopname no. 219 opcode 0xda cautofile function cautofile(): void { readexec_opername_swap(); if (Alis::$platform->version >= 30) { // autoname[0..11] = sd7[0..11]; autoname[12] = 0 (autoname is a PHP string) $s = mem_read(Alis::$sd7, 0xc); $z = strpos($s, "\0"); Alis::$autoname = $z === false ? $s : substr($s, 0, $z); } } // Codopname no. 220 opcode 0xdb ccancel function ccancel(): void { // MISSING in the C engine } // Codopname no. 221 opcode 0xdc ccancall function ccancall(): void { Alis::$ferase = 1; $tmpidx = 0; $curidx = get_0x18_unknown(Alis::$script->vram_org); while ($curidx) { killelem($curidx, $tmpidx); } Alis::$ferase = 0; } // Codopname no. 222 opcode 0xdd ccancen function ccancen(): void { Alis::$ferase = 1; cerasen(); } // Codopname no. 223 opcode 0xde cblast function cblast(): void { if (Alis::$platform->version >= 60) { readexec_opername_saveD7(); Alis::$ferase = 0; if (Alis::$varD7 > 0) { killent(Alis::$varD7 & 0xffff); } return; } Alis::$ferase = 1; ckill(); } function iniback(int $addr): void { // Background cache: full-screen shadow laid out like image.logic, so behind-sprites use the // blitters' absolute addressing and backsprite is a plain rect copy. (The original packs a tight // rect buffer, only consistent when backy1==0.) Never cleared, as in the original. if (Img::$bgcache_alloc) { host_free(Img::$bgcache_alloc); } // PORT: host_free() is a no-op, so free + malloc of the same size would grow Alis::$mem by // image_buf_cap at each iniback(): the block this function allocated last (in this boot of the // VM: Alis::$platform is a new object at each alis_init()) is reused instead when its size still // fits and no other buffer took over bgcache_alloc (the C clears the new block below anyway). static $owned = 0, $ownedCap = 0, $ownedVm = null; if ($ownedVm !== Alis::$platform || $ownedCap < ImageVars::$image_buf_cap || (Img::$bgcache_alloc && Img::$bgcache_alloc !== $owned)) { $owned = host_alloc(ImageVars::$image_buf_cap); $ownedCap = ImageVars::$image_buf_cap; $ownedVm = Alis::$platform; } Img::$bgcache_alloc = $owned; mem_set(Img::$bgcache_alloc, 0, Img::$buffer_alloc_size); $surface_h = Host::$pixelbuf->surface_h; Img::$backmap = Img::$bgcache_alloc + $surface_h * Alis::$platform->width; Img::$backlarg = s16(Alis::$platform->width); Img::$backdes = 0; Img::$backaddr = 0; } // Codopname no. 224 opcode 0xdf cscback function cscback(): void { $oldsback = s8(Img::$sback); readexec_opername(); Img::$sback = Alis::$varD7 & 0xff; readexec_opername(); Img::$backprof = s16(Alis::$varD7); readexec_opername(); Img::$backx1 = s16(Alis::$varD7); readexec_opername(); Img::$backy1 = s16(Alis::$varD7); readexec_opername(); Img::$backx2 = s16(Alis::$varD7); readexec_opername(); Img::$backy2 = s16(Alis::$varD7); if (Alis::$platform->version >= 30) { $tgtaddr = Alis::$basemain + get_0x16_screen_id(Alis::$script->vram_org); // (C: s8 oldsback compared with the u8 image.sback) if ($oldsback != Img::$sback) { if (Img::$sback == 0) { if (Img::$bgcache_alloc) { host_free(Img::$bgcache_alloc); Img::$bgcache_alloc = 0; Img::$backmap = 0; } Img::$cback = 0; Img::$pback = 0; xwrite8($tgtaddr + 1, xread8($tgtaddr + 1) & 0xfb); for ($elemidx = xread16($tgtaddr + 2); $elemidx != 0; $elemidx = s16(sprite_var($elemidx)->link)) { $sprite = sprite_var($elemidx); if (Img::$backsprite == $sprite->link) { $sprite->link = sprite_var(Img::$backsprite)->link; return; } } } else { iniback($tgtaddr); if (Img::$sback != 0) { $backsprite = sprite_var(Img::$backsprite); $backsprite->screen_id = s16(get_0x16_screen_id(Alis::$script->vram_org)); $backsprite->newx = Img::$backx1; $backsprite->newy = Img::$backy1; $backsprite->newd = Img::$backprof; $backsprite->cordspr = 0x80; $backsprite->numelem = -0x80; // (s8)0x80 $backsprite->clinking = -1; // (s16)0xffff $backsprite->chsprite = 0xff; $backsprite->state = 0; $backsprite->newad = Img::$backdes & 0xffffff; // fenetre31 requires newf >= 0, which cscback never sets: force it drawable. $backsprite->newf = 0; $elemidx = xread16($tgtaddr + 0x02); // asm: bset.b #2,(0x1,A2) — byte RMW of numelem; a 16-bit one clobbers its other flags. xwrite8($tgtaddr + 0x01, xread8($tgtaddr + 0x01) | 4); Img::$wback = 0; rangesprite(Img::$backsprite & 0xffff, 0xffff, $elemidx & 0xffff); $elemidx = s16(sprite_var($elemidx)->link); do { if ($elemidx == Img::$backsprite) { return; } $sprite = sprite_var($elemidx); $sprite->state = 2; $elemidx = s16($sprite->link); } while ($elemidx != 0); } } } } } // Codopname no. 225 opcode 0xe0 cscrolpage function cscrolpage(): void { readexec_opername(); Img::$spag = s8(Alis::$varD7); readexec_opername(); Img::$pagdx = s16(Alis::$varD7); readexec_opername(); Img::$pagdy = s16(Alis::$varD7); readexec_opername(); Img::$pagcount = s16(Alis::$varD7); if (Img::$spag != 0) { $screen = Alis::$basemain + get_0x16_screen_id(Alis::$script->vram_org); hwrite8($screen, hread8($screen) | 0x20); } } // Codopname no. 226 opcode 0xe1 cmatent function cmatent(): void { if (Alis::$platform->version >= 60) { Alis::$varD7 = (OpcodesV6Vars::$v6mat_n > 0) ? s16(OpcodesV6Vars::$v6mat_id[OpcodesV6Vars::$v6mat_n - 1] & 0xffff) : 0; cstore_continue(); return; } $index = Alis::$ptrent - 1; // (alis.ptrent - alis.tablent) - 1: ptrent is an index into tablent Alis::$varD7 = Alis::$matent[$index]; cstore_continue(); } // Codopname no. 227 opcode 0xe2 cshrink function cshrink(): void { // TODO (C): this is I3 implementation I2 and older differs verify if its ok!!! // delete bitmap and shift following data readexec_opername(); readexec_opername_saveD6(); cshrink_core(); } /** cshrink after its two operands were evaluated (the compiled scripts call it: Compiled/Templates/Ops13.php). */ function cshrink_core(): void { Alis::$flagmain = 0; $addr = adresdes(Alis::$varD7); $addr = s32($addr + xread32($addr)); $data = $addr; $d = xread8($data); if ($d == 0 || $d == 2) { $width = s16((read16($data + 2) + 1) >> 1); } elseif ($d == 0x1a || $d == 0x18) { $width = s16(read16($data + 2) + 1); } elseif ($d == 0x16 || $d == 0x14 || $d == 0x1e || $d == 0x1c) { $width = s16(read16($data + 2) + 1); } elseif ($d == 0x12 || $d == 0x10) { $width = s16((read16($data + 2) + 1) >> 1); } else { return; } $height = s16(read16($data + 4)); if ($height != 0) { $height = s16($height + 1); $bits = s32($width * $height); $offset = get_0x14_script_org_offset(Alis::$script->vram_org); $l = xread32($offset + 0xe); $e = xread16($offset + $l + 4); $a = s32(xread32($offset + $l) + $l); for ($i = 0; $i < $e; $i++, $a += 4) { $t = xread32($offset + $a); $addr2 = s32($a + $offset + $t); if (ult($addr, $addr2)) { $t = s32($t - $bits); xwrite32($offset + $a, $t); } } $org = get_0x14_script_org_offset(Alis::$script->vram_org); xwrite32($org + $l + 0x6, s32(xread32($org + $l + 0x6) - $bits)); xwrite32($org + $l + 0xc, s32(xread32($org + $l + 0xc) - $bits)); xwrite16($addr + 2, 0xf); xwrite16($addr + 4, 0); shrinkprog($addr + 6, $bits, 0); Alis::$script->data->sz = s32(Alis::$script->data->sz - $bits); } } // Codopname no. 228 opcode 0xe3 cdefmap // Ishar 3 Korean (IBM PC): cdefmap => map_cnul function cdefmap(): void { if (Alis::$platform->uid == C::EGameRobinsonsRequiem3DO) { cdefmap_3do(); return; } if (Alis::$platform->uid == C::EGameTransarctica) { $mapram = Alis::$script->vram_org; $offset = s16(script_read16()); if ($offset == 0) { $offset = s16(script_read16()); $mapram = Alis::$basemain; } $mapram += $offset; readexec_opername(); xwrite8($mapram - 0x50, Alis::$varD7); readexec_opername(); xwrite16($mapram - 0x4e, Alis::$varD7); xwrite16($mapram - 0x4c, get_0x0e_script_ent(Alis::$script->vram_org)); readexec_opername(); xwrite16($mapram - 0x4a, Alis::$varD7); xwrite16($mapram - 0x48, Alis::$varD7); readexec_opername(); xwrite16($mapram - 0x48, Alis::$varD7 - 1 + xread16($mapram - 0x48)); readexec_opername(); xwrite16($mapram - 0x44, Alis::$varD7); readexec_opername(); xwrite16($mapram - 0x42, Alis::$varD7); readexec_opername(); xwrite16($mapram - 0x40, Alis::$varD7); xwrite8($mapram - 0x4f, 0); xwrite32($mapram - 0x2c, 4); xwrite8($mapram - 0x28, 0x7f); } elseif (Alis::$platform->uid == C::EGameRobinsonsRequiem0 || Alis::$platform->uid == C::EGameRobinsonsRequiem1) { $mapram = Alis::$script->vram_org; $offset = s16(script_read16()); if ($offset == 0) { $offset = s16(script_read16()); $mapram = Alis::$basemain; } $mapram += $offset; readexec_opername(); xwrite8($mapram - 0x400, s8(Alis::$varD7)); readexec_opername(); xwrite16($mapram - 0x3fe, Alis::$varD7); xwrite16($mapram - 0x3fc, get_0x0e_script_ent(Alis::$script->vram_org)); readexec_opername(); xwrite16($mapram - 0x3fa, Alis::$varD7); xwrite16($mapram - 0x3f8, Alis::$varD7); readexec_opername(); xwrite16($mapram - 0x3f8, Alis::$varD7 - 1 + xread16($mapram - 0x3f8)); readexec_opername(); if (Alis::$varD7 == 0) { Alis::$varD7 = 1; } xwrite16($mapram - 0x3f4, Alis::$varD7); readexec_opername(); if (Alis::$varD7 == 0) { Alis::$varD7 = 1; } xwrite16($mapram - 0x3f2, Alis::$varD7); readexec_opername(); if (Alis::$varD7 == 0) { Alis::$varD7 = 1; } xwrite16($mapram - 0x3f0, Alis::$varD7); xwrite8($mapram - 0x3ff, 0); xwrite32($mapram - 0x3dc, 4); xwrite8($mapram - 0x3d8, 0x7f); cdefmap_rr_tail($mapram); } else { // MISSING in the C engine } } /** cdefmap() of Robinson's Requiem (PC): what follows the last operand (the compiled scripts call it) */ function cdefmap_rr_tail(int $mapram): void { { if (s8(xread8($mapram - 1)) < 0) { $valb = xread32($mapram - 6); $vala = xread32($mapram - 10); } else { $valb = xread16($mapram - 4) & 0xffff; $vala = xread16($mapram - 6) & 0xffff; } $ratio = udiv32($vala, $valb & 0xffff) & 0xffff; xwrite16($mapram - 0x3c6, $ratio); xwrite16($mapram - 0x3cc, $ratio); xwrite16($mapram - 0x3c4, $valb & 0xffff); xwrite16($mapram - 0x3ca, ($valb & 0xffff) >> 1); xwrite16($mapram - 0x3b6, 0); for ($i = 0; $i < 3; $i++) { $result = -1; $count = xread16($mapram - (0x3f4 - $i * 2)) & 0xffff; do { $result++; $count >>= 1; } while ($count != 0); xwrite16($mapram - (0x3c0 - $i * 2), $result); } if (xread8($mapram - 0x3d8) == 0x0a) { // '\n' $scrid = get_0x16_screen_id(Alis::$script->vram_org); if ($scrid != 0) { xwrite16(Alis::$basemain + $scrid + 0x40, get_0x0e_script_ent(Alis::$script->vram_org)); xwrite16(Alis::$basemain + $scrid + 0x42, $mapram - Alis::$script->vram_org); } } } } // Codopname no. 229 opcode 0xe4 csetmap // Ishar 3 Korean (IBM PC): csetmap => map_cnul function csetmap(): void { $mapram = Alis::$script->vram_org; $offset = s16(script_read16()); if ($offset == 0) { $offset = s16(script_read16()); $mapram = Alis::$basemain; } $mapram += $offset; if (Alis::$platform->uid == C::EGameTransarctica) { readexec_opername(); xwrite16($mapram - 0x3e, Alis::$varD7); readexec_opername(); xwrite16($mapram - 0x3c, Alis::$varD7); readexec_opername(); xwrite16($mapram - 0x3a, Alis::$varD7); readexec_opername(); xwrite16($mapram - 0x38, Alis::$varD7); xwrite16($mapram - 0x26, (Alis::$varD7 & 0xffff) - 1); xwrite16($mapram - 0x3e, (((Alis::$varD7 - 1) & 0xffff) >> 1) + xread16($mapram - 0x3e)); readexec_opername(); xwrite16($mapram - 0x36, Alis::$varD7); readexec_opername(); xwrite16($mapram - 0x34, Alis::$varD7); xwrite16($mapram - 0x24, (Alis::$varD7 & 0xffff) - 1); xwrite16($mapram - 0x3a, (((Alis::$varD7 - 1) & 0xffff) >> 1) + xread16($mapram - 0x3a)); } elseif (Alis::$platform->uid == C::EGameRobinsonsRequiem0 || Alis::$platform->uid == C::EGameRobinsonsRequiem1 || Alis::$platform->uid == C::EGameRobinsonsRequiem3DO) { readexec_opername(); xwrite16($mapram - 0x3ee, Alis::$varD7); readexec_opername(); xwrite16($mapram - 0x3ec, Alis::$varD7); readexec_opername(); xwrite16($mapram - 0x3ea, Alis::$varD7); readexec_opername(); xwrite16($mapram - 0x3e8, Alis::$varD7); xwrite16($mapram - 0x3d6, (Alis::$varD7 & 0xffff) - 1); xwrite16($mapram - 0x3ee, (((Alis::$varD7 - 1) & 0xffff) >> 1) + xread16($mapram - 0x3ee)); readexec_opername(); xwrite16($mapram - 0x3e6, Alis::$varD7); readexec_opername(); xwrite16($mapram - 0x3e4, Alis::$varD7); xwrite16($mapram - 0x3d4, (Alis::$varD7 & 0xffff) - 1); xwrite16($mapram - 0x3ea, (((Alis::$varD7 - 1) & 0xffff) >> 1) + xread16($mapram - 0x3ea)); } } function putmap(int $spridx, int $bitmap): void { $vram = Alis::$script->vram_org; $sprite = sprite_var($spridx); $sprite->data = $bitmap & 0xffffff; $sprite->flaginvx = Img::$invert_x & 0xff; $sprite->clinking = s16(get_0x2a_clinking($vram)); $sprite->script_ent = ((get_0x2c_calign($vram) << 8) | get_0x2d_calign($vram)) & 0xffff; $sprite->cordspr = get_0x2b_cordspr($vram); $sprite->chsprite = get_0x2f_chsprite($vram); $sprite->credon_off = s8(get_0x25_credon_credoff($vram)); // TODO (C): credon_off >= 0 path (creducing from 0x26/0x27). $sprite->chsprite = get_0x2f_chsprite($vram); if (s8(get_0x25_credon_credoff($vram)) == -0x80) { $sprite->newzoomx = get_0x36_unknown($vram); $sprite->newzoomy = get_0x38_unknown($vram); $sprite->creducing = 0; } else { $sprite->creducing = get_0x27_creducing($vram); $sprite->credon_off = s8(get_0x26_creducing($vram)); } $sprite->depx = s16(Img::$oldcx + Img::$depx); $sprite->depy = s16(Img::$oldcy + Img::$depy); $sprite->depz = s16(Img::$oldcz + Img::$depz); if (Alis::$fmuldes == 0) { $newidx = 0; $oldidx = 0; if (!searchelem($newidx, $oldidx)) { return; } do { $cursprite = sprite_var($newidx); while ($cursprite->state == 0) { killelem($newidx, $oldidx); if ($newidx == 0) { Alis::$fadddes = 0; return; } if (!testnum($newidx)) { Alis::$fadddes = 0; return; } } } while (nextnum($newidx, $oldidx)); } Alis::$fadddes = 0; } // Codopname no. 230 opcode 0xe5 cputmap // Ishar 3 Korean (IBM PC): cputmap => map_cnul function cputmap(): void { // Resolve map base address $map_base = Alis::$script->vram_org; $offset = s16(script_read16()); if ($offset == 0) { $offset = s16(script_read16()); $map_base = Alis::$basemain; } $map_base += $offset; if (Alis::$platform->uid == C::EGameRobinsonsRequiem3DO) { readexec_opername(); xwrite16($map_base - 0x3e2, Alis::$varD7); readexec_opername(); xwrite16($map_base - 0x3e0, Alis::$varD7); readexec_opername(); xwrite16($map_base - 0x3de, Alis::$varD7); readexec_opername(); $pos = xread16($map_base - 0x3de) & 0xffff; $w = xread16($map_base - 0x3f0) & 0xffff; xwrite16($map_base - 0x3d2, $w ? $pos % $w : $pos); return; } if (Alis::$platform->uid == C::EGameTransarctica) { // Read map parameters readexec_opername(); xwrite16($map_base - 0x32, Alis::$varD7); readexec_opername(); xwrite16($map_base - 0x30, Alis::$varD7); readexec_opername(); xwrite16($map_base - 0x2e, Alis::$varD7); readexec_opername(); Img::$numelem = s8(Alis::$varD7); // Compute wrapped column index (C: u32 from the sign-extended s16) $map_pos = xread16($map_base - 0x2e); $map_width = xread16($map_base - 0x40) & 0xffff; $col_idx = umod32($map_pos, $map_width) & 0xffff; xwrite16($map_base - 0x22, s16($col_idx)); Alis::$fmuldes = 0; // Find or create sprite element $newidx = 0; $oldidx = 0; if (!searchelem($newidx, $oldidx)) { createlem($newidx, $oldidx); } $sprite = sprite_var($newidx); if (-1 < $sprite->state) { $sprite->state = 2; } putmap($newidx, $map_base - 0x2c); // Set up sprite display properties if (searchelem($newidx, $oldidx)) { $sprite = sprite_var($newidx); $sprite->credon_off = -1; $sprite->flaginvx = 0; $sprite->chsprite = 0; $sprite->depx = xread16($map_base - 0x3e); $sprite->depy = xread16($map_base - 0x3c); $sprite->depz = xread16($map_base - 0x3a); } } elseif (Alis::$platform->uid == C::EGameRobinsonsRequiem0 || Alis::$platform->uid == C::EGameRobinsonsRequiem1) { // Read map parameters readexec_opername(); xwrite16($map_base - 0x3e2, Alis::$varD7); readexec_opername(); xwrite16($map_base - 0x3e0, Alis::$varD7); readexec_opername(); xwrite16($map_base - 0x3de, Alis::$varD7); readexec_opername(); Img::$numelem = s8(Alis::$varD7); // Compute wrapped column index (C: u32 from the sign-extended s16) $map_pos = xread16($map_base - 0x3de); $map_width = xread16($map_base - 0x3f0) & 0xffff; $col_idx = umod32($map_pos, $map_width) & 0xffff; xwrite16($map_base - 0x3d2, s16($col_idx)); Alis::$fmuldes = 0; // Find or create sprite element $newidx = 0; $oldidx = 0; if (!searchelem($newidx, $oldidx)) { createlem($newidx, $oldidx); } $sprite = sprite_var($newidx); if (-1 < $sprite->state) { $sprite->state = 2; } putmapin($newidx, $map_base - 0x3dc); // Set up sprite display properties if (searchelem($newidx, $oldidx)) { $sprite = sprite_var($newidx); $sprite->credon_off = -1; $sprite->flaginvx = 0; $sprite->chsprite = 0; $sprite->depx = xread16($map_base - 0x3ee); $sprite->depy = xread16($map_base - 0x3ec); $sprite->depz = xread16($map_base - 0x3ea); } } } // Codopname no. 231 opcode 0xe6 csavepal function csavepal(): void { readexec_opername(); if (dos_pal_active()) { dos_csavepal(Alis::$varD7); } else { savepal(Alis::$varD7); } } // Codopname no. 232 opcode 0xe7 csczoom function csczoom(): void { if (Alis::$platform->uid == C::EGameRobinsonsRequiem3DO) { if (xread16(Alis::$script->vram_org - 0x64) != 0) { for ($i = 0; $i < 5; $i++) { readexec_opername(); } } return; } // MISSING in the C engine } // Codopname no. 233 opcode 0xe8 ctexmap // Ishar 3 Korean (IBM PC): ctexmap => map_cnul function ctexmap(): void { Alis::$flagmain = 0; // Resolve scene base address $scene_addr = Alis::$script->vram_org; $offset = s16(script_read16()); if ($offset == 0) { $offset = s16(script_read16()); $scene_addr = Alis::$basemain; } $scene_addr += $offset; // Compute texture entry address from slot index readexec_opername_saveD6(); $entry = s32($scene_addr + (Alis::$varD6 * 0x20) - 0xc00); // (s16)0xf400 // Read exists flag (param 1) - stored at entry[8] on DOS, entry[1] on ST readexec_opername(); if (Alis::$platform->is_little_endian) { xwrite8($entry + 8, Alis::$varD7); } else { xwrite8($entry + 1, Alis::$varD7); } // Resolve texture resource address if exists flag > 0 readexec_opername(); $exists_flag = Alis::$platform->is_little_endian ? s8(xread8($entry + 8)) : s8(xread8($entry + 1)); if ($exists_flag > 0) { xwrite32($entry + 4, adresdes(Alis::$varD7)); $res_addr = xread32($entry + 4); if ($res_addr != 0) { $bitmap_addr = s32($res_addr + xread32($res_addr)); $fmt = xread8($bitmap_addr); // 0x14/0x16 and 0x1C/0x1E carry an 8-byte header; 0x00/0x02 carry 6. // The v3 DOS VM (0x4cb8: lea esi,[ebx+8]) always starts the scan at +8, whatever the format. $dos_v3 = sys_is_dos_v3(); $pixel_offset = ($dos_v3 || $fmt == 0x1C || $fmt == 0x1E || $fmt == 0x14 || $fmt == 0x16) ? 8 : 6; // Compute yshift = bit count of x_mask (matches DOS: INC CL; SHR AX,1; JNZ) $x_mask = xread16($bitmap_addr + 2) & 0xffff; $yshift = 0; $tmp = $x_mask; do { $yshift++; $tmp >>= 1; } while ($tmp); xwrite8($entry, $yshift); // Initialize yorigin to 0 xwrite16($entry + 2, 0); // Scan texture rows for first fully opaque row (no zero pixels). // Stores yorigin at entry[2] to skip transparent top rows. $y_mask = xread16($bitmap_addr + 4); $pixel_ptr = $bitmap_addr + $pixel_offset; $rows_rem = $y_mask; $found_opaque = 0; while ($rows_rem >= 0) { $has_zero = 0; if ($dos_v3) { // v3 DOS (0x4cbb: mov cx,[ebx+2] ... dec cl; jns): an 8-bit counter per row, so a low byte // of x_mask >= 0x80 checks a single byte per row; the pointer walks on contiguously. $cl = $x_mask & 0xff; do { if (xread8($pixel_ptr) == 0) { $has_zero = 1; } $pixel_ptr++; $cl = ($cl - 1) & 0xff; } while ($cl < 0x80); } else { $cols = s16($x_mask); while ($cols >= 0) { if (xread8($pixel_ptr) == 0) { $has_zero = 1; } $pixel_ptr++; $cols--; } } if (!$has_zero) { $found_opaque = 1; break; } $rows_rem--; } if ($found_opaque) { $yorigin = s16($y_mask - $rows_rem); xwrite16($entry + 2, $yorigin); if ($yorigin != 0) { // Adjust bitmap y_mask to snap to power-of-2 within opaque region $available = ($y_mask - $yorigin) & 0xffff; // v3 DOS: the executable's own table at 0x1620d (0 first, 3 at index 6, ends at 0x3fff); // past its end, the last entry + yorigin is written. $pow2m1 = $dos_v3 ? Opcodes2Vars::TEXMAP_SNAP_DOS_V3 : Opcodes2Vars::POW2M1; $snapped = false; for ($i = 0; $i < 15; $i++) { if ($pow2m1[$i] == $available) { $snapped = true; break; // exact match, keep original y_mask } if ($pow2m1[$i] > $available) { if ($i > 0) { xwrite16($bitmap_addr + 4, $pow2m1[$i - 1] + $yorigin); } $snapped = true; break; } } if ($dos_v3 && !$snapped) { xwrite16($bitmap_addr + 4, $pow2m1[14] + $yorigin); } } } } } elseif (Alis::$platform->is_little_endian && $exists_flag == 0) { // DOS: explicitly clear texture fields for no-texture types xwrite32($entry + 4, 0); xwrite8($entry, 0); xwrite16($entry + 2, 0); } // Read texture parameters readexec_opername(); xwrite16($entry + 10, Alis::$varD7); readexec_opername(); if (Alis::$platform->is_little_endian) { // DOS: param 4 stores only at entry[9] xwrite8($entry + 9, Alis::$varD7); } else { // ST/Amiga: param 4 stores at entry[8] and entry[9]=param+1 xwrite8($entry + 8, Alis::$varD7); xwrite8($entry + 9, s8(Alis::$varD7) + 1); } readexec_opername(); xwrite16($entry + 0xe, Alis::$varD7); // Read mapping type and dispatch readexec_opername(); $type = s8(Alis::$varD7); xwrite8($entry + 0x14, $type); if ($type == 0 && sys_is_dos_v3()) { // v3 DOS (0x4d47): type 0 shares the sprite path, with a null sprite address readexec_opername(); xwrite32($entry + 0x10, 0); xwrite16($entry + 0x1c, xread16(Alis::$script->vram_org - 0xe)); readexec_opername(); xwrite16($entry + 0x16, Alis::$varD7); readexec_opername(); xwrite8($entry + 0x15, Alis::$varD7); readexec_opername(); xwrite16($entry + 0x1a, Alis::$varD7); } elseif ($type == 0) { // Type 0: no mapping, skip 4 operands readexec_opername(); readexec_opername(); readexec_opername(); readexec_opername(); } elseif ($type < 0) { // Negative type: grid-based terrain texture mapping readexec_opername(); xwrite16($entry + 0x10, Alis::$varD7); readexec_opername(); xwrite16($entry + 0x12, Alis::$varD7); readexec_opername(); xwrite16($entry + 0x16, Alis::$varD7); readexec_opername(); xwrite16($entry + 0x1a, Alis::$varD7); $tex_offset = calctop($scene_addr, xread16($entry + 0x10), xread16($entry + 0x12)); xwrite32($entry + 0x10, $tex_offset); if ($tex_offset < 0 && !sys_is_dos_v3()) { // The v3 DOS VM (0x4dc6) keeps the negative type whatever the offset xwrite8($entry + 0x14, 0); } } elseif ($type == 2) { // Type 2: simple texture with scale readexec_opername(); xwrite16($entry + 0x16, Alis::$varD7); readexec_opername(); readexec_opername(); readexec_opername(); xwrite16($entry + 0x1a, Alis::$varD7); } else { // Type 1+: sprite-based texture mapping readexec_opername(); xwrite32($entry + 0x10, adresdes(Alis::$varD7)); xwrite16($entry + 0x1c, xread16(Alis::$script->vram_org - 0xe)); readexec_opername(); xwrite16($entry + 0x16, Alis::$varD7); readexec_opername(); xwrite8($entry + 0x15, Alis::$varD7); readexec_opername(); xwrite16($entry + 0x1a, Alis::$varD7); } if (Alis::$platform->bpp == 8) { // Histogram analysis for 8-bit textures (types 0x1c/0x1e) // Computes the two most frequent pixel colors for terrain dithering (entry[8], entry[9]) if (!Alis::$platform->is_little_endian && s8(xread8($entry + 8)) <= 0 && s8(xread8($entry + 1)) > 0) { $res_addr = xread32($entry + 4); $bitmap_addr = s32($res_addr + xread32($res_addr)); $bmp_type = xread8($bitmap_addr); if ($bmp_type == 0x1c || $bmp_type == 0x1e) { $histogram = array_fill(0, 256, 0); $width = s32(xread16($bitmap_addr + 2) + 1); $height = s32(xread16($bitmap_addr + 4) + 1); $total_pixels = mul32($width, $height); // Count pixel occurrences $pixel_addr = $bitmap_addr + 8; for ($i = 0; ult($i, $total_pixels); $i++) { $color = xread8($pixel_addr + $i); $histogram[$color] = ($histogram[$color] + 1) & 0xffff; } // Pass 1: find most frequent non-zero color $max_count = 0; $winner1 = 0; for ($i = 1; $i < 256; $i++) { if ($histogram[$i] >= $max_count) { $max_count = $histogram[$i]; $winner1 = $i; } } xwrite8($entry + 8, $winner1); // Pass 2: find second most frequent, excluding first winner $max_count = 0; $winner2 = 0; for ($i = 1; $i < 256; $i++) { if ($histogram[$i] >= $max_count && $i != $winner1) { $max_count = $histogram[$i]; $winner2 = $i; } } xwrite8($entry + 9, $winner2); } } } } function tabcell_flag(int $cell): int { return xread8($cell - (Alis::$platform->version >= 60 ? 2 : 1)); } // Codopname no. 234 opcode 0xe9 calloctab function calloctab(): void { $cell = read_vaddr(); if (s8(tabcell_flag($cell)) >= 0 || xread32($cell) != 0) { return; } if (Alis::$platform->version >= 60) { calloctab_v6($cell); return; } $size = xread32($cell - 6 - (xread8($cell - 1) & 0xf) * 4); xwrite32($cell, io_malloc($size)); } // Codopname no. 235 opcode 0xea cfreetab function cfreetab(): void { if (Alis::$platform->version < 30) { // MISSING in the C engine return; } $cell = read_vaddr(); $h = xread32($cell); if (s8(tabcell_flag($cell)) >= 0 || $h == 0) { return; } xwrite32($cell, 0); if (Alis::$platform->version < 60) { io_mfree($h); return; } for ($i = 0; $i < 0x10; $i++) { if (xread32(Alis::$tabptr + $i * 8) == $h) { io_mfree(Alis::$tabptr + $i * 8); return; } } } // Codopname no. 238 opcode 0xed cscsun function cscsun(): void { if (Alis::$platform->version < 60) { // MISSING in the C engine return; } $p = ['elem' => 0, 'limit' => 0, 'aux' => 0]; getprot_x($p); $v = [0, 0, 0]; for ($i = 0; $i < 3; $i++) { readexec_opername(); $v[$i] = Alis::$varD7 & 0xff; } if ($p['elem'] <= 0) { return; } $slot = s32($p['elem'] + ($p['aux'] == -1 ? 0 : $p['aux'])); $n = $p['aux'] == -1 ? xread8($p['elem'] + 0x33) : 1; $is3do = Alis::$platform->uid == C::EGameRobinsonsRequiem3DO; for (; $n; $n--, $slot = s32($slot + C::V6_SLOT_STRIDE)) { if ($is3do) { xwrite8($slot + 0x30, $v[0]); xwrite8($slot + 0x2e, $v[1]); xwrite8($slot + 0x2f, $v[2]); continue; } xwrite8($slot + 0x2e, $v[0]); xwrite8($slot + 0x30, $v[1]); xwrite8($slot + 0x2f, $v[2]); if ($p['aux'] == -1) { xwrite8($slot + 0x23, xread8($slot + 0x23) | 1); } } } // Codopname no. 239 opcode 0xee cdarkpal function cdarkpal(): void { readexec_opername(); $darkpar = s16(Alis::$varD7); readexec_opername(); $darkpav = s16(Alis::$varD7); readexec_opername(); $darkpab = s16(Alis::$varD7); readexec_opername(); $darkpac = s16(Alis::$varD7); readexec_opername(); $darkpan = s16(Alis::$varD7); if (Alis::$platform->uid == C::EGameDeus) { $first = $darkpac & 0xffff; $last = $darkpan & 0xffff; if ($last > 0xff) { $last = 0xff; } if ($last - $first + 1 <= 0) { return; } for ($i = $first; $i <= $last; $i++) { Img::$dkpalet[$i * 3 + 0] = $darkpar; Img::$dkpalet[$i * 3 + 1] = $darkpav; Img::$dkpalet[$i * 3 + 2] = $darkpab; } Img::$dkpalet[0] = Img::$dkpalet[1] = Img::$dkpalet[2] = 0; Img::$fdarkpal = 0; for ($i = 3; $i < 3 + 0x2fd; $i++) { if (Img::$dkpalet[$i] != 0x100) { Img::$fdarkpal = 1; break; } } return; } $val0 = 0x3c; $val1 = 0x3f; $val2 = 0x40; if (Alis::$platform->bpp == 8) { $val0 = 0x100; $val1 = 0xff; $val2 = 0x100; } if ($darkpac < $val0) { $length = s16($darkpac + $darkpan); if ($val1 < $length) { $length = $val2; } for ($i = $darkpac; $i < $length; $i++) { Img::$dkpalet[$i * 3 + 0] = $darkpar; Img::$dkpalet[$i * 3 + 1] = $darkpav; Img::$dkpalet[$i * 3 + 2] = $darkpab; } Img::$dkpalet[0] = 0; Img::$dkpalet[1] = 0; Img::$dkpalet[2] = 0; // The 8-bit DOS VM scans all 0x2fd words (the 256 colours), not only the first 60. $scanEnd = Alis::$platform->bpp == 8 ? 0x300 : 0xb6; for ($i = 3; $i < $scanEnd; $i++) { if (Img::$dkpalet[$i] != 0x100) { Img::$fdarkpal = 1; return; } } Img::$fdarkpal = 0; } } // Codopname no. 240 opcode 0xef cscdark function cscdark(): void { Alis::$flagmain = 0; $screen = Alis::$basemain + get_0x16_screen_id(Alis::$script->vram_org); // Read darkness config value readexec_opername(); xwrite16($screen + 0x98, Alis::$varD7); // Resolve darkness palette resource address readexec_opername(); $dark_addr = adresdes(Alis::$varD7); xwrite32($screen + 0x9a, s32(xread32($dark_addr) + $dark_addr)); // Read darkness level readexec_opername(); xwrite16($screen + 0x9e, Alis::$varD7); // Mark screen as having darkness enabled xwrite8($screen, xread8($screen) | 0x80); // Compute darkness table height from resource header $dark_height = s16(xread16(xread32($screen + 0x9a) + 4) + 1); xwrite16($screen + 0xa0, $dark_height); if (xread16($screen + 0x9e) < 0) { // Negative dark level: default to half table height xwrite16($screen + 0x9e, $dark_height >> 1); } // Set global darkness base and pointer Alis::$basedark = xread16($screen + 0x9e) & 0xffff; Alis::$ptrdark = s32(xread32($screen + 0x9a) + (Alis::$platform->bpp == 4 ? 6 : 8)); } // Codopname no. 241 opcode 0xf0 caset function caset(): void { if (Alis::$platform->version < 30) { // MISSING in the C engine return; } $vram = Alis::$script->vram_org; readexec_opername(); xwrite16($vram + 0x18, Alis::$varD7); readexec_opername(); xwrite16($vram + 0x20, Alis::$varD7); readexec_opername(); xwrite16($vram + 0x28, Alis::$varD7); } // Codopname no. 242 opcode 0xf1 camov function camov(): void { if (Alis::$platform->version < 30) { // MISSING in the C engine return; } $vram = Alis::$script->vram_org; readexec_opername(); xadd16($vram + 0x18, Alis::$varD7); readexec_opername(); xadd16($vram + 0x20, Alis::$varD7); readexec_opername(); xadd16($vram + 0x28, Alis::$varD7); } // Codopname no. 243 opcode 0xf2 cscaset function cscaset(): void { // MISSING in the C engine } // Codopname no. 244 opcode 0xf3 cscamov function cscamov(): void { // MISSING in the C engine } // Codopname no. 245 opcode 0xf4 cscfollow function cscfollow(): void { $screen = Alis::$basemain + get_0x16_screen_id(Alis::$script->vram_org); readexec_opername(); xwrite16($screen + 0x60, Alis::$varD7); readexec_opername(); xwrite8($screen + 0x84, Alis::$varD7); readexec_opername(); xwrite16($screen + 0x86, Alis::$varD7); readexec_opername(); xwrite16($screen + 0x88, Alis::$varD7); readexec_opername(); xwrite16($screen + 0x8a, Alis::$varD7); readexec_opername(); xwrite16($screen + 0x8c, Alis::$varD7); readexec_opername(); xwrite16($screen + 0x8e, Alis::$varD7); readexec_opername(); xwrite16($screen + 0x90, Alis::$varD7); if (Alis::$platform->version >= 30 && Alis::$platform->is_little_endian) { // v3 DOS (0x43eb, bts [ebx],7): a new follow forces a full redraw of the screen, like cscview xwrite8($screen, xread8($screen) | 0x80); } } // Codopname no. 246 opcode 0xf5 cscview function cscview(): void { $screen = Alis::$basemain + get_0x16_screen_id(Alis::$script->vram_org); readexec_opername(); xwrite16($screen + 0x64, Alis::$varD7); readexec_opername(); xwrite16($screen + 0x66, Alis::$varD7); readexec_opername(); xwrite16($screen + 0x68, Alis::$varD7); readexec_opername(); xwrite16($screen + 0x6a, Alis::$varD7); readexec_opername(); xwrite16($screen + 0x6c, Alis::$varD7); readexec_opername(); xwrite16($screen + 0x6e, Alis::$varD7); xwrite8($screen, xread8($screen) | 0x80); } // Codopname no. 247 opcode 0xf6 cfilm function cfilm(): void { film_check_boot(); fli_stream_close(); // stale streaming film: don't leak the file handle film_reset(); Alis::$filmnode = 0; readexec_opername(); FilmVars::$bfilm->id = s16(Alis::$varD7); readexec_opername(); $addr = s32(adresdes(Alis::$varD7) + 0x20 + 4); // PORT: the film resource is copied out of the VM memory (film_use_mem) $off = film_use_mem($addr); FilmVars::$bfilm->addr1 = $off; FilmVars::$bfilm->addr2 = $off; readexec_opername(); FilmVars::$bfilm->waitclock = s16(Alis::$varD7); // DOS v3: the film window is the current script's screen record (basemain + screen id, 0x8e3e) FilmVars::$bfilm->basemain = Alis::$basemain + (Alis::$platform->version < 60 ? s16(get_0x16_screen_id(Alis::$script->vram_org)) : 0); inifilm(); Alis::$varD7 = FilmVars::$bfilm->frames; cstore_continue(); } function headland(int $d4w): void { $vram = Alis::$script->vram_org; $newax = s16($d4w - xread16($vram + Opcodes2Const::ALIS_SCR_WCAX())); if ($newax != 0) { $newax >>= 2; if ($newax < 0) { if ($newax < -0xf) { $newax = -0xf; } } else { if ($newax == 0) { $newax = 1; } if (0xf < $newax) { $newax = 0xf; } } $newax = s16($newax + xread16($vram + Opcodes2Const::ALIS_SCR_WCAX())); if ($newax < 0x4c) { if ($newax < -0x4b) { $newax = -0x4b; } } else { $newax = 0x4b; } xwrite16($vram + Opcodes2Const::ALIS_SCR_WCAX(), $newax); } } /** C: tabsin[i] (tabsin = tables + 720, opcodes.c first half). */ function op2_tabsin(int $i): int { return OpcodesVars::tables[OpcodesVars::tabsin + $i]; } /** C: tabcos[i] (tabcos = tables + 810). */ function op2_tabcos(int $i): int { return OpcodesVars::tables[OpcodesVars::tabcos + $i]; } function walkland(int $scene_addr): int { $vram = Alis::$script->vram_org; // Set material collision mask from walktete sign Alis::$matmask = 0; if (Opcodes2Vars::$walktete < 0) { Alis::$matmask = Opcodes2Vars::$walktete; } // Read azimuth trig values and walk offset from VRAM $sin_az = op2_tabsin(xread16($vram + Opcodes2Const::ALIS_SCR_WCAZ())); $cos_az = op2_tabcos(xread16($vram + Opcodes2Const::ALIS_SCR_WCAZ())); $offset_x = s8(xread8($vram + Opcodes2Const::ALIS_SCR_WCX2())); $offset_y = s8(xread8($vram + Opcodes2Const::ALIS_SCR_WCY2())); // DOS v3 (original executable, walk routine 0xd127): the look-ahead probes use the rotated step vector and // always run; the dim-based vector is only built for head-bob (0xd3f1) $dos3 = Alis::$platform->version >= 30 && Alis::$platform->is_little_endian; $stepx = mul32($cos_az * $offset_x - $sin_az * $offset_y, 0x80); $stepy = mul32($cos_az * $offset_y + $sin_az * $offset_x, 0x80); // Compute forward walk direction vectors if ($dos3) { if (0 < Opcodes2Vars::$walktete) { Opcodes2Vars::$walkdx = mul32(mul32(-xread16($scene_addr - 0x3f4), $sin_az), 0x80); Opcodes2Vars::$walkdy = mul32(mul32(xread16($scene_addr - 0x3f4), $cos_az), 0x80); } } elseif (Opcodes2Vars::$walktete != 0) { Opcodes2Vars::$walkdx = mul32(mul32(-xread16($scene_addr - 0x3f4), $sin_az), 0x80); Opcodes2Vars::$walkdy = mul32(mul32(xread16($scene_addr - 0x3f4), $cos_az), 0x80); } $walkdx = Opcodes2Vars::$walkdx; $walkdy = Opcodes2Vars::$walkdy; // the probe vectors: the step on DOS v3 (0x1aad0 / 0x1aad4 written at 0xd1ac / 0xd1e8) $probedx = $dos3 ? $stepx : $walkdx; $probedy = $dos3 ? $stepy : $walkdy; // Compute new world position by rotating walk offset by azimuth $world_x = xread32_wc($vram, C::ALIS_SCR_WCX); $world_y = xread32_wc($vram, Opcodes2Const::ALIS_SCR_WCY()); Opcodes2Vars::$walkcx = s32($world_x + $stepx); Opcodes2Vars::$walkcy = s32($world_y + $stepy); Opcodes2Vars::$walkocz = xread16($vram + Opcodes2Const::ALIS_SCR_WCZ()); $walkocz = Opcodes2Vars::$walkocz; // Query terrain altitude at new position $probe_z = 0; $probe_cell = 0; landalti($scene_addr, $vram, s16(Opcodes2Vars::$walkcx >> 0x10), s16(Opcodes2Vars::$walkcy >> 0x10), Opcodes2Vars::$walkcz, $probe_cell); $delta_z = s16(Opcodes2Vars::$walkcz - $walkocz); // Multi-point collision probes around walk path (DOS v3: always, 0xd215-0xd3cb) if ($dos3 || Alis::$matmask != 0) { landalti($scene_addr, $vram, s16(shr32(s32($world_x + $probedy + $probedx), 0x10)), s16(shr32(s32($world_y + $probedx + $probedy), 0x10)), $probe_z, $probe_cell); if (0xfe < $probe_z) { $delta_z = s16($probe_z - $walkocz); Opcodes2Vars::$walkcz = $probe_z; } landalti($scene_addr, $vram, s16(shr32(s32($world_x + ($probedx - $probedy)), 0x10)), s16(shr32(s32($world_y + ($probedy - $probedx)), 0x10)), $probe_z, $probe_cell); if (0xfe < $probe_z) { $delta_z = s16($probe_z - $walkocz); Opcodes2Vars::$walkcz = $probe_z; } landalti($scene_addr, $vram, s16(shr32(s32($world_x + $probedx * 2), 0x10)), s16(shr32(s32($world_y + $probedy * 2), 0x10)), $probe_z, $probe_cell); if (0xfe < $probe_z) { $delta_z = s16($probe_z - $walkocz); Opcodes2Vars::$walkcz = $probe_z; } landalti($scene_addr, $vram, s16(shr32(s32($world_x + $probedy + $probedx * 2), 0x10)), s16(shr32(s32($world_y + $probedx + $probedy * 2), 0x10)), $probe_z, $probe_cell); if (0xfe < $probe_z) { $delta_z = s16($probe_z - $walkocz); Opcodes2Vars::$walkcz = $probe_z; } landalti($scene_addr, $vram, s16(shr32(s32($world_x + ($probedx * 2 - $probedy)), 0x10)), s16(shr32(s32($world_y + ($probedy * 2 - $probedx)), 0x10)), $probe_z, $probe_cell); if (0xfe < $probe_z) { $delta_z = s16($probe_z - $walkocz); Opcodes2Vars::$walkcz = $probe_z; } } // Look-ahead slope estimation for head bobbing if (0 < Opcodes2Vars::$walktete) { $abs_delta = $delta_z; if ($delta_z < 0) { $abs_delta = s16(-$delta_z); } if ($abs_delta <= Opcodes2Vars::$walkhaut) { // Sample altitude at 1x, 2x, 4x walk steps with decreasing weights $sample_z = 0; landalti($scene_addr, $vram, s16(shr32(s32($world_x + $walkdx), 0x10)), s16(shr32(s32($world_y + $walkdy), 0x10)), $sample_z, $probe_cell); $weighted_1x = $sample_z = s16(($sample_z - $walkocz) * 4); landalti($scene_addr, $vram, s16(shr32(s32($world_x + $walkdx * 2), 0x10)), s16(shr32(s32($world_y + $walkdy * 2), 0x10)), $sample_z, $probe_cell); $sample_z = s16(($sample_z - $walkocz) * 2); // (sic) overwritten by the next landalti, except on DOS v3 $weighted_2x = $sample_z; landalti($scene_addr, $vram, s16(shr32(s32($world_x + $walkdx * 4), 0x10)), s16(shr32(s32($world_y + $walkdy * 4), 0x10)), $sample_z, $probe_cell); if ($dos3) { // 0xd455-0xd53e: sum = 4*(s1-oz) + 2*(s2-oz) + (s4-oz); |sum| times the signed high byte of // walktete (the >0x3c0 clamp only zeroes SI, unused); dead zone = the low byte of walktete $abs_slope = (($sample_z - $walkocz) + $weighted_1x + $weighted_2x) & 0xffff; if ($abs_slope & 0x8000) { $abs_slope = (-$abs_slope) & 0xffff; } $wt = s8((Opcodes2Vars::$walktete >> 8) & 0xff); $sample_z = s16(cdiv((s16($abs_slope) * $wt) >> 2, 0xc)); $dead_zone = Opcodes2Vars::$walktete & 0xff; } else { $slope_sum = (($sample_z - $walkocz) + $weighted_1x + $sample_z) & 0xffff; $abs_slope = $slope_sum; if (s16($slope_sum) < 0) { $abs_slope = (-$slope_sum) & 0xffff; } // Clamp extreme slopes to zero if (0x3c0 < $abs_slope) { $slope_sum = 0; } // Scale slope by walktete factor if (Alis::$platform->is_little_endian) { // DOS: uses high byte of walktete (typically 0 for values < 256, disabling head-bob) $wt = s16((Opcodes2Vars::$walktete & 0xffff) >> 8); $sample_z = s16(cdiv((s16($slope_sum) * $wt) >> 2, 0xc)); } else { $sample_z = s16(cdiv((s16($slope_sum) * Opcodes2Vars::$walktete) >> 2, 0xc)); } $dead_zone = Opcodes2Vars::$walkmap3; } $abs_head = $sample_z; if ($sample_z < 0) { $abs_head = s16(-$sample_z); } // Apply dead zone threshold if (($abs_head & 0xff) < $dead_zone) { $sample_z = 0; } headland($sample_z); } } // Final altitude check: reject move if height change exceeds walkhaut $abs_delta_z = (Opcodes2Vars::$walkcz - $walkocz) & 0xffff; if ($abs_delta_z & 0x8000) { // (s32)((u32)abs_delta_z << 0x10) < 0 $abs_delta_z = (-$abs_delta_z) & 0xffff; } if (Opcodes2Vars::$walkhaut < s16($abs_delta_z)) { // Move rejected: keep old XY, update Z at current position (DOS v3: writes nothing, 0xd55c jg 0xd581) if (!$dos3) { landalti($scene_addr, $vram, xread16($vram + C::ALIS_SCR_WCX), xread16($vram + Opcodes2Const::ALIS_SCR_WCY()), $probe_z, $probe_cell); xwrite16($vram + Opcodes2Const::ALIS_SCR_WCZ(), $probe_z); } } else { // Move accepted: update full world position xwrite32_wc($vram, C::ALIS_SCR_WCX, Opcodes2Vars::$walkcx); xwrite32_wc($vram, Opcodes2Const::ALIS_SCR_WCY(), Opcodes2Vars::$walkcy); xwrite16($vram + Opcodes2Const::ALIS_SCR_WCZ(), Opcodes2Vars::$walkcz); } return s16(Opcodes2Vars::$walkcz - $walkocz); } // Codopname no. 248 opcode 0xf7 cwalkmap // Ishar 3 Korean (IBM PC): cwalkmap => map_cnul function cwalkmap(): void { $offset = s16(script_read16()); $vram = Alis::$script->vram_org; if ($offset == 0) { $offset = s16(script_read16()); $vram = Alis::$basemain; } readexec_opername(); Opcodes2Vars::$walkhaut = s16(Alis::$varD7); readexec_opername(); Opcodes2Vars::$walktete = s16(Alis::$varD7); readexec_opername_saveD6(); Opcodes2Vars::$walkdim = Alis::$varD6 & 0xffff; Alis::$varD7 = walkland($vram + $offset); cstore_continue(); } function polarmov(int $wcx2, int $wcy2, int $wcz2, int $wcax, int $wcaz): sVector { $sinax = op2_tabsin($wcax); $sinaz = op2_tabsin($wcaz); $cosax = op2_tabcos($wcax); $cosaz = op2_tabcos($wcaz); $result = new sVector(); if (Alis::$platform->is_little_endian) { // DOS: only wcy2 terms get *0x80 $result->x = s32($cosaz * $wcx2 - mul32(s16(($sinaz * $cosax) >> 9) * $wcy2, 0x80)); $result->y = s32(mul32(s16(($cosaz * $cosax) >> 9) * $wcy2, 0x80) + $sinaz * $wcx2); } else { // m68k: everything * 0x80 $result->x = mul32($cosaz * $wcx2 - s16(($sinaz * $cosax) >> 9) * $wcy2, 0x80); $result->y = mul32(s16(($cosaz * $cosax) >> 9) * $wcy2 + $sinaz * $wcx2, 0x80); } $result->z = mul32($wcy2 * $sinax + $wcz2 * $cosax, 0x80); return $result; } function landalti(int $addr, int $vram, int $world_x, int $world_y, int &$out_alt, int &$out_cell): void { // Convert world coordinates to grid coordinates $grid_x = ($world_x >> ((xread16($addr - 0x3c0) & 0xffff) & 0x3f)) & 0xffff; $grid_y = ($world_y >> ((xread16($addr - 0x3c0) & 0xffff) & 0x3f)) & 0xffff; // Bounds check against grid dimensions if (($grid_y <= (xread16($addr - 0x292) & 0xffff)) && ($grid_x <= (xread16($addr - 0x294) & 0xffff))) { // Read terrain cell data $cell_addr = s32(s16($grid_y) + s16($grid_y) + xread32(Img::$atlland + s16($grid_x * 4))); $out_cell = xread16($cell_addr); $out_alt = $out_cell & 0xff; // Compute terrain type offset (6-bit type from cell, scaled) $terrain_offset = s16((($out_cell & 0x3f00) >> 3) - 0xc00); $terrain_flag = s8(xread8($addr + 0x14 + $terrain_offset)); if ($terrain_flag != 0) { if ($terrain_flag < 0) { // Overhang/bridge: follow redirect and test altitude $cell_addr = s32($cell_addr + xread32($addr + 0x10 + $terrain_offset)); $alt_diff = s16((xread16($cell_addr) & 0xff) - $out_alt); $terrain_offset = s16(xread16($addr + 0x16 + $terrain_offset) - $alt_diff); if (($alt_diff <= $terrain_offset) || (s16((xread16($cell_addr) & 0xff) - $terrain_offset) <= xread16($vram + Opcodes2Const::ALIS_SCR_WCZ()))) { $out_cell = xread16($cell_addr); $out_alt = $out_cell & 0xff; } } elseif ((Alis::$matmask != 0) && (xread16($addr + 0x1a + $terrain_offset) != 0)) { // Material modifier if (s8(xread8($addr + 0x14 + $terrain_offset)) == 1) { if (xread32($addr + 0x10 + $terrain_offset) != 0) { if (Alis::$platform->version >= 30 && Alis::$platform->is_little_endian) { // DOS v3 (0xd67d `add cx,0xff`): adds 0xff to the altitude, the cell blocks $out_alt = s16($out_alt + 0xff); } // other versions, C: `out_alt += 0xff;` adds to the pointer, not the value: no effect } } else { $out_alt = s16($out_alt + xread16($addr + 0x16 + $terrain_offset)); } } } } } // Codopname no. 249 opcode 0xf8 catstmap // Ishar 3 Korean (IBM PC): catstmap => map_cnul function catstmap(): void { if (Alis::$platform->uid == C::EGameRobinsonsRequiem3DO) { v6_mapcell(0); return; } $offset = s16(script_read16()); $addr = Alis::$script->vram_org; if ($offset == 0) { $offset = s16(script_read16()); $addr = Alis::$basemain; } $addr += $offset; readexec_opername(); $wcx2 = s16(Alis::$varD7); readexec_opername(); $wcy2 = s16(Alis::$varD7); readexec_opername_saveD6(); $wcz2 = s16(Alis::$varD6); $vram = Alis::$script->vram_org; $wcax = xread16($vram + Opcodes2Const::ALIS_SCR_WCAX()); $wcaz = xread16($vram + Opcodes2Const::ALIS_SCR_WCAZ()); $vec = polarmov($wcx2, $wcy2, $wcz2, $wcax, $wcaz); Alis::$matmask = 0; $d2w = 0; $t7 = s16(Alis::$varD7); landalti($addr, $vram, s16(shr32(s32(xread32_wc($vram, C::ALIS_SCR_WCX) + $vec->x), 0x10)), s16(shr32(s32(xread32_wc($vram, Opcodes2Const::ALIS_SCR_WCY()) + $vec->y), 0x10)), $d2w, $t7); Alis::$varD7 = $t7; cstore_continue(); } // Codopname no. 250 opcode 0xf9 cavtstmov function cavtstmov(): void { $vram = Alis::$script->vram_org; $wcx2 = s8(xread8($vram + Opcodes2Const::ALIS_SCR_WCX2())); $wcy2 = s8(xread8($vram + Opcodes2Const::ALIS_SCR_WCY2())); $wcz2 = s8(xread8($vram + Opcodes2Const::ALIS_SCR_WCZ2())); if (Alis::$platform->version >= 31) { $wcax = xread16($vram + Opcodes2Const::ALIS_SCR_WCAX()); $wcaz = xread16($vram + Opcodes2Const::ALIS_SCR_WCAZ()); $vec = polarmov($wcx2, $wcy2, $wcz2, $wcax, $wcaz); Alis::$wcx = s16(s32(xread32_wc($vram, C::ALIS_SCR_WCX) + $vec->x) >> 0x10); Alis::$wcy = s16(s32(xread32_wc($vram, Opcodes2Const::ALIS_SCR_WCY()) + $vec->y) >> 0x10); Alis::$wcz = s16(s32(xread32_wc($vram, Opcodes2Const::ALIS_SCR_WCZ()) + $vec->z) >> 0x10); } else { Alis::$wcx = s16(xread32_wc($vram, C::ALIS_SCR_WCX) + $wcx2); Alis::$wcy = s16(xread32_wc($vram, Opcodes2Const::ALIS_SCR_WCY()) + $wcy2); Alis::$wcz = s16(xread32_wc($vram, Opcodes2Const::ALIS_SCR_WCZ()) + $wcz2); } readexec_opername(); Alis::$matmask = s16(Alis::$varD7); Alis::$wforme = get_0x1a_cforme($vram); clipform(); crstent(); } // Codopname no. 251 opcode 0xfa cavmov function cavmov(): void { if (Alis::$platform->version >= 60) { $v = Alis::$script->vram_org; v6_polar($v, s16(xread16($v + 0x26)), s16(xread16($v + 0x28)), s16(xread16($v + 0x2a))); xwrite8($v + 0x45, 1); return; } if (Alis::$platform->version >= 31) { $vram = Alis::$script->vram_org; $wcx2 = s8(xread8($vram + Opcodes2Const::ALIS_SCR_WCX2())); $wcy2 = s8(xread8($vram + Opcodes2Const::ALIS_SCR_WCY2())); $wcz2 = s8(xread8($vram + Opcodes2Const::ALIS_SCR_WCZ2())); $wcax = xread16($vram + Opcodes2Const::ALIS_SCR_WCAX()); $wcaz = xread16($vram + Opcodes2Const::ALIS_SCR_WCAZ()); $vec = polarmov($wcx2, $wcy2, $wcz2, $wcax, $wcaz); if (Alis::$platform->is_little_endian) { // DOS: add only integer part (high 16 bits of polarmov result) xadd16($vram + C::ALIS_SCR_WCX, s16($vec->x >> 16)); xadd16($vram + Opcodes2Const::ALIS_SCR_WCY(), s16($vec->y >> 16)); xadd16($vram + Opcodes2Const::ALIS_SCR_WCZ(), s16($vec->z >> 16)); } else { // m68k: full 32-bit add to position xadd32($vram + C::ALIS_SCR_WCX, $vec->x); xadd32($vram + Opcodes2Const::ALIS_SCR_WCY(), $vec->y); xadd32($vram + Opcodes2Const::ALIS_SCR_WCZ(), $vec->z); } } } function getangle(int &$atx, int &$aty): void { $magnitude = $aty; if ($atx == 0) { // X component is zero: angle is 0° or 180° (0xb4) $angle = 0; if ($aty == 0) { $magnitude = 0; } elseif ($aty < 0) { $angle = 0xb4; } } else { $abs_x = $atx; if ($atx < 0) { $abs_x = s16(-$atx); } if ($aty != 0) { $abs_y = $aty; if ($aty < 0) { $abs_y = s16(-$aty); } if ($abs_y < $abs_x) { // |x| > |y|: compute atan(|x|/|y|), angle near 90° (0x5a) $angle = 0x5a; $abs_x = s16(cdiv($abs_x << 5, $abs_y)); // reused as atan ratio if (($abs_x & 0xffff) >= count(OpcodesVars::tabatan) || ($angle = OpcodesVars::tabatan[$abs_x]) != 0) { if ($aty < 0) { $angle = 0xb4 - $angle; } if (-1 < $atx) { $angle = s16(-$angle); } // Recover magnitude via sin $magnitude = s16(cdiv($atx * 0x200, op2_tabsin($angle)) & 0xffff); } else { // Degenerate atan: treat as X ≈ 0 $angle = 0; if ($aty == 0) { $magnitude = 0; } elseif ($aty < 0) { $angle = 0xb4; } } } else { // |y| >= |x|: compute atan(|y|/|x|), angle near 0° $angle = 0x5a; $abs_x = s16(cdiv($abs_y << 5, $abs_x)); // reused as atan ratio if (($abs_x & 0xffff) >= count(OpcodesVars::tabatan) || ($angle = OpcodesVars::tabatan[$abs_x]) != 0) { if ($aty < 0) { $angle = s16(-$angle); } $angle = s16($angle - 0x5a); if ($atx < 0) { $angle = s16(-$angle); } // Recover magnitude via cos $magnitude = s16(cdiv($aty * 0x200, op2_tabcos($angle)) & 0xffff); } else { // Degenerate atan: treat as Y ≈ 0 $magnitude = $atx; $angle = $atx < 0 ? 0x5a : -0x5a; // (s16)0xffa6 } } } else { // Y component is zero: angle is ±90° (0x5a / 0xffa6) $magnitude = $atx; $angle = $atx < 0 ? 0x5a : -0x5a; } } if ($magnitude < 0) { $magnitude = s16(-$magnitude); } $atx = $angle; $aty = $magnitude; } function polarang(int &$cx, int &$cy, int &$cz, int &$cur_x, int &$cur_y, int &$cur_z): void { // Compute deltas from current position to target $cx = s16($cx - $cur_x); $cy = s16($cy - $cur_y); $cz = s16($cz - $cur_z); // Horizontal angle: getangle returns azimuth in *cx, horizontal distance in *cy getangle($cx, $cy); // Vertical angle: if there is a height difference, compute elevation $elevation = 0; if ($cz != 0) { $elevation = s16(-$cz); getangle($elevation, $cy); } // Output: *cx = azimuth, *cy = elevation, *cz = distance $cz = $cy; $cy = $elevation; } // Codopname no. 252 opcode 0xfb caim function caim(): void { $vram = Alis::$script->vram_org; // Read target world position readexec_opername(); $cx = s16(Alis::$varD7); readexec_opername(); $cy = s16(Alis::$varD7); readexec_opername(); $cz = s16(Alis::$varD7); // Read current world position $cur_x = xread16($vram + C::ALIS_SCR_WCX); $cur_y = xread16($vram + Opcodes2Const::ALIS_SCR_WCY()); $cur_z = xread16($vram + Opcodes2Const::ALIS_SCR_WCZ()); // Compute polar angle from current position to target // After this: cx = target azimuth, cz = elevation (0 = same plane) polarang($cx, $cy, $cz, $cur_x, $cur_y, $cur_z); // If target is at same altitude, keep current azimuth if ($cz == 0) { $cx = xread16($vram + Opcodes2Const::ALIS_SCR_WCAZ()); } // Read max turn rate per step (degrees) readexec_opername(); $max_turn = Alis::$varD7 & 0xffff; // Clamp angular change to max_turn (0xb3 = 179°, 0x168 = 360°) if (0xb3 >= $max_turn) { $cy = xread16($vram + Opcodes2Const::ALIS_SCR_WCAZ()); $angle_diff = ($cx - $cy) & 0xffff; if (s16($angle_diff) < 0) { if (s16($angle_diff) < -0xb3) { // Wrapped past -180°, adjust by +360° $angle_diff = ($angle_diff + 0x168) & 0xffff; if (s16($angle_diff) < 0) { if (s16($angle_diff) <= s16(-$max_turn)) { $angle_diff = (-$max_turn) & 0xffff; } $cx = s16($cy + $angle_diff); } if (s16($max_turn) <= s16($angle_diff)) { $angle_diff = $max_turn; } $cx = s16($cy + $angle_diff); } else { // Negative turn within half-circle if (s16($angle_diff) <= s16(-$max_turn)) { $angle_diff = (-$max_turn) & 0xffff; } $cx = s16($cy + $angle_diff); } } else { if (0xb3 < s16($angle_diff)) { // Wrapped past +180°, adjust by -360° $angle_diff = ($angle_diff - 0x168) & 0xffff; if (s16($angle_diff) < 0) { if (s16($angle_diff) <= s16(-$max_turn)) { $angle_diff = (-$max_turn) & 0xffff; } $cx = s16($cy + $angle_diff); } } else { // Positive turn within half-circle if (s16($max_turn) <= s16($angle_diff)) { $angle_diff = $max_turn; } $cx = s16($cy + $angle_diff); } } } // Normalize azimuth to [-359°, +359°] range if ($cx < 0x168) { if ($cx < -0x167) { $cx = s16($cx + 0x168); } } else { $cx = s16($cx - 0x168); } // Store results: current azimuth, new azimuth, elevation Alis::$varD7 = $cy; cstore_continue(); Alis::$varD7 = $cx; cstore_continue(); Alis::$varD7 = $cz; cstore_continue(); } // Codopname no. 253 opcode 0xfc cpointpix function cpointpix(): void { readexec_opername(); readexec_opername_saveD6(); pointpix(Alis::$varD7, Alis::$varD6); Alis::$varD7 = Img::$cxtstpix; cstore_continue(); Alis::$varD7 = Img::$cytstpix; cstore_continue(); Alis::$varD7 = Img::$cztstpix; cstore_continue(); Alis::$varD7 = Img::$dtstpix; cstore_continue(); Alis::$varD7 = Img::$ntstpix; cstore_continue(); Alis::$varD7 = Img::$etstpix; cstore_continue(); } // Codopname no. 254 opcode 0xfd cchartmap function cchartmap(): void { $vram = Alis::$script->vram_org; $offset = s16(script_read16()); if ($offset == 0) { $vram = Alis::$basemain; $offset = s16(script_read16()); } $addr = $vram + $offset; readexec_opername(); $value = s16(Alis::$varD7); if ($value == 0 || $value == 1) { cchartmap_fill($addr, $value == 0 ? 0 : -1); $vram = Alis::$script->vram_org; $offset = s16(script_read16()); if ($offset == 0) { $offset = s16(script_read16()); $vram = Alis::$basemain; } xwrite32($addr - 0x3ba, $vram + $offset); return; } readexec_opername(); cchartmap_sub($addr, $value); } /** * cchartmap sub-commands 2..: D7 = the operand just evaluated (the compiled scripts call it, * Compiled/Templates/Ops13.php). */ function cchartmap_sub(int $addr, int $value): void { { { // Sub-command 2/3: set chart type if ($value == 2 || $value == 3) { xwrite16($addr - 0x3b6, Alis::$varD7); return; } // Sub-command 4: set chart color base if ($value == 4) { xwrite16($addr - (Alis::$platform->is_little_endian ? 0x3ae : 0x3b4), Alis::$varD7); return; } if ($value != 5) { // Sub-command >= 6: clear rectangular area in chart bitmap $radius = s16(s16(Alis::$varD7) - 1); if ($radius < 0) { $radius = 0; } // Compute clipped tile bounds from extent $entity_data = xread32($addr - 0x3ba); // Centre on the current script's own integer position (0x4f31 / 0x4f8f: es:ebp+0 / +8), // not on the globals of the last tstmov, which may be stale or offset $pos_x = s16(xread16(Alis::$script->vram_org + C::ALIS_SCR_WCX)); $pos_y = s16(xread16(Alis::$script->vram_org + Opcodes2Const::ALIS_SCR_WCY())); $cell_w = xread16($entity_data - 0x3f4); $cell_h = xread16($entity_data - 0x3f2); $max_cols = xread16($entity_data - 0x3e8); $max_rows = xread16($entity_data - 0x3e6); $div_result = cdiv(s16($pos_x - $radius), $cell_w); $min_col = s16($div_result); if ($div_result < 0) { $min_col = 0; } if ($max_cols <= $min_col) { $min_col = $max_cols; } $div_result = cdiv(s16($pos_x + $radius), $cell_w); $max_col = s16($div_result); if ($div_result < 0) { $max_col = 0; } if ($max_cols <= $max_col) { $max_col = $max_cols; } $div_result = cdiv(s16($pos_y - $radius), $cell_h); $min_row = s16($div_result); if ($div_result < 0) { $min_row = 0; } if ($max_rows <= $min_row) { $min_row = $max_rows; } $div_result = cdiv(s16($pos_y + $radius), $cell_h); $max_row = s16($div_result); if ($div_result < 0) { $max_row = 0; } if ($max_rows <= $max_row) { $max_row = $max_rows; } // Shift tile bounds to bitmap coordinates $shift_x = ((xread16($addr - 0x3c0) - 4) - xread16($entity_data - 0x3c0)) & 0xffff; $min_x = ($min_col >> ($shift_x & 0x3f)) & 0xffff; $max_x = ($max_col >> ($shift_x & 0x3f)) & 0xffff; $shift_y = (xread16($addr - 0x3be) - xread16($entity_data - 0x3be)) & 0xffff; $start_row = s16($min_row >> ($shift_y & 0x3f)); $row_count = s16(($max_row >> ($shift_y & 0x3f)) - $start_row); // Compute bitmasks for left and right edges of the rectangle $left_mask = (~(0xffff >> (($min_x - ($min_x & 0xfff0)) & 0x3f))) & 0xffff; $aligned_max = $max_x | 0xf; $right_mask = (~(-1 << ((-($max_x - $aligned_max)) & 0x3f))) & 0xffff; $word_span = (($aligned_max - ($min_x & 0xfff0)) & 0xffff) >> 4; $mid_words = s16($word_span - 1); if ($mid_words < 0) { $left_mask = $right_mask | $left_mask; $right_mask = 0; } if ($right_mask != 0) { $mid_words = s16($word_span - 2); } // Resolve data pointer and compute starting position $data_base = $addr; if (s8(xread8($addr - 1)) < 0) { $data_base = xread32(xread32($addr)); } $stride = xread16($addr - 0x3c4); $row_ptr = s32($data_base + $start_row * 2 + ($min_x >> 4) * ($stride & 0xffff)); // Clear rectangle row by row using bitmasks do { xwrite16($row_ptr, $left_mask & xread16($row_ptr)); $word_ptr = $row_ptr + $stride; $mid_count = $mid_words; if (-1 < $mid_words) { do { xwrite16($word_ptr, 0); $word_ptr += $stride; $mid_count--; } while ($mid_count != -1); } if ($right_mask != 0) { xwrite16($word_ptr, $right_mask & xread16($word_ptr)); } $row_ptr += 2; $row_count = s16($row_count - 1); } while ($row_count != -1); return; } // Sub-command 5: set chart color count xwrite16($addr - (Alis::$platform->is_little_endian ? 0x3b0 : 0x3b2), Alis::$varD7); return; } } } /** cchartmap sub-commands 0 and 1: fill the whole chart bitmap with $fill_value (0 or -1). */ function cchartmap_fill(int $addr, int $fill_value): void { // Fill entire chart bitmap with fill_value (0 or -1), then link entity // 0x5146: low 16 bits of the product, then a logical shift (shr ax,1) $loops = ((xread16($addr - 0x3c6) * xread16($addr - 0x3c4)) & 0xffff) >> 1; $data_ptr = $addr; if (s8(xread8($data_ptr - 1)) < 0) { $data_ptr = xread32($data_ptr); $data_ptr = xread32($data_ptr); } for ($i = 0; $i < $loops; $i++, $data_ptr += 2) { xwrite16($data_ptr, $fill_value); } } // Codopname no. 255 opcode 0xfe cscsky function cscsky(): void { Alis::$flagmain = 0; $mem = Alis::$basemain + get_0x16_screen_id(Alis::$script->vram_org); readexec_opername(); xwrite16($mem + 0xa2, Alis::$varD7); readexec_opername(); xwrite32($mem + 0xa4, adresdes(Alis::$varD7)); readexec_opername(); xwrite16($mem + 0xa8, Alis::$varD7); $sprite = xread32($mem + 0xa4); $spdata = s32(xread32($sprite) + $sprite); if (xread8($spdata) == 3) { $sprite = $sprite + xread16($spdata + 2) * 4; $spdata = s32(xread32($sprite) + $sprite); } // C: (((((h6c * 4) >> 3) * 0x168) / (w + 1)) + 0x80U & 0xff00) * (w + 1) / 0x168, the tail in unsigned int $w1 = xread16($spdata + 2) + 1; $q = cdiv(((xread16($mem + 0x6c) * 4) >> 3) * 0x168, $w1); $u = ($q + 0x80) & 0xff00; xwrite16($mem + 0xa8, udiv32(mul32($u, $w1), 0x168)); xwrite8($mem, xread8($mem) | 0x80); } // Codopname no. 256 opcode 0xff czoom function czoom(): void { readexec_opername(); xwrite16(Alis::$script->vram_org - 0x36, Alis::$varD7); readexec_opername(); xwrite16(Alis::$script->vram_org - 0x38, Alis::$varD7); } // Codopname no. 055 opcode 0x36 cclock function cclock(): void { readexec_opername(); set_0x3a_wait_cycles(Alis::$script->vram_org, Alis::$varD7); set_0x3e_wait_time(Alis::$script->vram_org, s32(Alis::$varD7 + Alis::$timeclock)); } // ============================================================================ // Flow control - Subroutines // ============================================================================ // Codopname no. 018 opcode 0x11 cret function cret(): void { if (Alis::$fseq == 0 && Alis::$saversp <= Alis::$script->vacc_off) { cstop(); } else { Alis::$script->pc = s32(Alis::$script->pc_org + xpeek32()); if (Alis::$script->vacc_off == get_0x0c_vacc_offset(Alis::$script->vram_org)) { set_0x0c_vacc_offset(Alis::$script->vram_org, 0); } // (C: debug message when vacc_off >= -0x34: "VACC out of bounds") Alis::$script->vacc_off = s16(Alis::$script->vacc_off + 4); } } function cjsr(int $offset): void { // save return **OFFSET**, not ADDRESS xpush32(s32(Alis::$script->pc - Alis::$script->pc_org)); Alis::$script->pc += $offset; } // Codopname no. 006 opcode 0x05 cjsr8 function cjsr8(): void { // read byte, extend sign $offset = s8(script_read8()); cjsr($offset); } // Codopname no. 007 opcode 0x06 cjsr16 function cjsr16(): void { $offset = s16(script_read16()); cjsr($offset); } // Codopname no. 008 opcode 0x07 cjsr24 function cjsr24(): void { $offset = read_off24(); cjsr($offset); } // ============================================================================ // Flow control - Jump // ============================================================================ // Codopname no. 009 opcode 0x08 cjmp8 function cjmp8(): void { $offset = s8(script_read8()); Alis::$script->pc += $offset; } // Codopname no. 010 opcode 0x09 cjmp16 function cjmp16(): void { $offset = s16(script_read16()); Alis::$script->pc += $offset; } // Codopname no. 011 opcode 0x0a cjmp24 function cjmp24(): void { $offset = read_off24(); Alis::$script->pc += $offset; } // ============================================================================ // Flow control - Branch if zero // ============================================================================ // value tested by cbz/cbnz function bztest(): int { return Alis::$platform->uid == C::EGameRobinsonsRequiem3DO ? s32(Alis::$varD7) : s16(Alis::$varD7); } // Codopname no. 019 opcode 0x12 cbz8 function cbz8(): void { $d7 = bztest(); $offset = $d7 ? 1 : s8(script_read8()); Alis::$script->pc += $offset; } // Codopname no. 020 opcode 0x13 cbz16 function cbz16(): void { $d7 = bztest(); $offset = $d7 ? 2 : s16(script_read16()); Alis::$script->pc += $offset; } // Codopname no. 021 opcode 0x14 cbz24 function cbz24(): void { $d7 = bztest(); $offset = $d7 ? 3 : read_off24(); Alis::$script->pc += $offset; } // ============================================================================ // Flow control - Branch if non-zero // ============================================================================ // Codopname no. 022 opcode 0x15 cbnz8 function cbnz8(): void { $d7 = bztest(); $offset = $d7 == 0 ? 1 : s8(script_read8()); Alis::$script->pc += $offset; } // Codopname no. 023 opcode 0x16 cbnz16 function cbnz16(): void { $d7 = bztest(); $offset = $d7 == 0 ? 2 : s16(script_read16()); Alis::$script->pc += $offset; } // Codopname no. 024 opcode 0x17 cbnz24 function cbnz24(): void { $d7 = bztest(); $offset = $d7 == 0 ? 3 : read_off24(); Alis::$script->pc += $offset; } // ============================================================================ // Flow control - Branch if equal // ============================================================================ function beqtest(): int { return (Alis::$platform->uid == C::EGameRobinsonsRequiem3DO) ? (int) (s32(Alis::$varD7) == s32(Alis::$varD6)) : (int) (s16(Alis::$varD7) == s16(Alis::$varD6)); } function cbcond(int $jump, int $bits): void { if ($bits == 8) { $offset = s8(script_read8()); } elseif ($bits == 16) { $offset = s16(script_read16()); } else { $offset = read_off24(); } if ($jump) { Alis::$script->pc += $offset; } } // Codopname no. 025 opcode 0x18 cbeq8 function cbeq8(): void { cbcond(beqtest(), 8); } // Codopname no. 026 opcode 0x19 cbeq16 function cbeq16(): void { cbcond(beqtest(), 16); } // Codopname no. 027 opcode 0x1a cbeq24 function cbeq24(): void { cbcond(beqtest(), 24); } // Codopname no. 028 opcode 0x1b cbne8 function cbne8(): void { cbcond((int) !beqtest(), 8); } // Codopname no. 029 opcode 0x1c cbne16 function cbne16(): void { cbcond((int) !beqtest(), 16); } // Codopname no. 030 opcode 0x1d cbne24 function cbne24(): void { cbcond((int) !beqtest(), 24); } // ============================================================================ // Flow control - Start // ============================================================================ function cstart(int $offset): void { if (Alis::$platform->version >= 60) { cstart_v6branch($offset); return; } $vram = Alis::$script->vram_org; if (Alis::$fseq == 0) { if (get_0x0c_vacc_offset($vram) == 0) { $vacc_offset = s16(get_0x0a_vacc_offset($vram) - 4); set_0x0c_vacc_offset($vram, $vacc_offset); set_0x0a_vacc_offset($vram, $vacc_offset); xwrite32($vram + $vacc_offset, s32(get_0x08_script_ret_offset($vram) - Alis::$script->pc_org)); set_0x08_script_ret_offset($vram, s32($offset + Alis::$script->pc)); } else { set_0x0a_vacc_offset($vram, get_0x0c_vacc_offset($vram)); set_0x08_script_ret_offset($vram, s32($offset + Alis::$script->pc)); } if (Alis::$platform->version != 10) { set_0x04_cstart_csleep($vram, 1); set_0x01_wait_count($vram, 1); } if (Alis::$platform->version >= 30) { set_0x3e_wait_time($vram, 0); } } else { if (get_0x0c_vacc_offset($vram) == 0) { xpush32(s32(Alis::$script->pc - Alis::$script->pc_org)); set_0x0c_vacc_offset($vram, Alis::$script->vacc_off); } else { Alis::$script->vacc_off = get_0x0c_vacc_offset($vram); } Alis::$script->pc += $offset; } } // Codopname no. 049 opcode 0x30 cstart8 function cstart8(): void { // read byte, extend sign to word, then to long cstart(s8(script_read8())); } // Codopname no. 050 opcode 0x31 cstart16 function cstart16(): void { cstart(s16(script_read16())); } // Codopname no. 051 opcode 0x32 cstart24 function cstart24(): void { cstart(read_off24()); } // ============================================================================ // Additions // ============================================================================ function killent(int $killent): void { $killent &= 0xffff; $vram = xread32(Alis::$atent + $killent); if ($vram == 0) { if (Alis::$platform->version < 60) { Alis::$ferase = 0; } return; } // make sure music playback is stopped (a dormant v6 entity has no program) $script = ENTSCR($killent); if ($script->data && $script->data->header->id == Aud::$musicId) { sys_sleep_until_music_stops(); } if (Alis::$platform->version >= 60) { killallelem_v6($vram); } else { Alis::$script = ENTSCR($killent); cerasall(); Alis::$script = ENTSCR(Alis::$varD5); } $shrinkby = xread32($vram - 0x14); $contextsize = get_context_size(); if (Alis::$platform->version >= 60) { $datasize = xread16(Alis::$atent + $killent + 6) & 0xffff; $target = s32($vram - $datasize); $shrinkby = xread32(Alis::$atent + $killent + 8); } else { $datasize = s32(xread16($shrinkby + 0x16) + xread16($shrinkby + 0x12) + $contextsize); $target = s32($vram - $datasize); $shrinkby = s32(xread16($shrinkby + 0x14) + $datasize); } $source = s32($target + $shrinkby); // rebase every v6 slot/elem pointer that lives above the freed block before copy if (Alis::$platform->version >= 60) { v6_relocate_vram($target, $shrinkby); } // copy work mem to freed space (C: u16 copy loop `do {...} while (source < finent)`, unsigned compare) $n = ult($source, Alis::$finent) ? ((Alis::$finent - $source + 1) >> 1) * 2 : 2; mem_move($target, $source, $n); Alis::$finent = s32(Alis::$finent - $shrinkby); $curent = 0; $prevent = 0; while (true) { $nextent = xread16(Alis::$atent + 4 + $prevent) & 0xffff; if ($killent == $nextent) { $curent = $prevent; } if ($nextent == 0) { break; } $prevent = $nextent; if ($vram <= xread32(Alis::$atent + $nextent)) { $curscript = ENTSCR($nextent); $curscript->vram_org -= $shrinkby; xwrite32(Alis::$atent + $nextent, s32(xread32(Alis::$atent + $nextent) - $shrinkby)); } } xwrite16(Alis::$atent + $curent + 4, xread16(Alis::$atent + 4 + $killent)); xwrite32(Alis::$atent + $killent, 0); if (Alis::$platform->version >= 60) { node_free($killent); Alis::$live_scripts[intdiv($killent, ENT_STRIDE())] = null; } else { xwrite16(Alis::$atent + $killent + 4, Alis::$dernent); Alis::$dernent = s16($killent); } Alis::$nbent = s16(Alis::$nbent - 1); // if we deleted currently running script, set previous script and restart loop if (Alis::$varD5 == $killent) { Alis::$varD5 = $curent; Alis::$restart_loop = 1; cstop(); } } function shrinkprog(int $start, int $length, int $id): void { $id &= 0xffff; $oldfin = Alis::$finprog; if (Alis::$platform->version >= 60) { v6_relocate_prog(s32($start + $length), $oldfin, -$length); } // Shift [start+length, finprog) down by `length` to close the gap. if (Alis::$finprog - $length > $start) { mem_move($start, $start + $length, Alis::$finprog - $length - $start); } Alis::$finprog = s32(Alis::$finprog - $length); // Address-keyed caches and the cursor pointer must follow the move. if (Alis::$desmouse) { $lo = $start; $hi = $lo + $length; if (Alis::$desmouse >= $hi) { Alis::$desmouse -= $length; } elseif (Alis::$desmouse >= $lo) { Alis::$desmouse = 0; } } audio_relocate($start, $start + $length, -$length); if ($id != 0) { $idx = -1; for ($i = 0; $i < Alis::$nbprog; $i++) { $scr = Alis::$loaded_scripts[$i]; if (Alis::$platform->version >= 60 ? $scr->data_org == $start : $scr->header->id == $id) { $idx = $i; } if ($idx >= 0) { Alis::$loaded_scripts[$i] = $i < Alis::$maxprog ? Alis::$loaded_scripts[$i + 1] : null; } } $found = 0; for ($i = 0; $i < Alis::$nbprog; $i++) { $location = xread32(Alis::$atprog + $i * 4); // ATPROG(i) if ($start == $location) { $found = 1; } if ($found) { xwrite32(Alis::$atprog + $i * 4, $i < Alis::$maxprog ? xread32(Alis::$atprog + ($i + 1) * 4) : 0); } } Alis::$dernprog = s32(Alis::$dernprog - 4); Alis::$nbprog = (Alis::$nbprog - 1) & 0xffff; } for ($i = 0; $i < Alis::$nbprog; $i++) { $script = Alis::$loaded_scripts[$i]; if ($start <= $script->data_org) { $script->data_org -= $length; xwrite32(Alis::$atprog + $i * 4, s32(xread32(Alis::$atprog + $i * 4) - $length)); } } if ($id != 0) { $contextsize = get_context_size(); $entidx = 0; $prevent = 0; while (($entidx = xread16(Alis::$atent + 4 + $entidx))) { $script = ENTSCR($entidx); if (Alis::$platform->version >= 60) { if (!(xread8(V6_FARSLEEP($script->vram_org)) & AlisV6Const::V6_DORMANT) && $start <= get_0x14_script_org_offset($script->vram_org)) { $script->pc -= $length; $script->pc_org -= $length; set_0x14_script_org_offset($script->vram_org, s32(get_0x14_script_org_offset($script->vram_org) - $length)); set_0x08_script_ret_offset($script->vram_org, s32(get_0x08_script_ret_offset($script->vram_org) - $length)); } } elseif ($id == get_0x10_script_id($script->vram_org)) { killent($entidx); $entidx = $prevent; //alis.varD5; } elseif ($start <= get_0x14_script_org_offset($script->vram_org)) { $script->pc -= $length; $script->pc_org -= $length; set_0x14_script_org_offset($script->vram_org, s32(get_0x14_script_org_offset($script->vram_org) - $length)); set_0x08_script_ret_offset($script->vram_org, s32(get_0x08_script_ret_offset($script->vram_org) - $length)); $org_offset = get_0x14_script_org_offset($script->vram_org); $script->vacc_off = s16(-$contextsize - xread16($org_offset + 0x16)); } $prevent = $entidx; } } // move addresses to sprite data to match actual new locations (pre-v6 screens) for ($scidx = Alis::$platform->version >= 60 ? 0 : s16(Scr::$ptscreen); $scidx != 0; $scidx = s16(get_scr_to_next($scidx))) { $spridx = get_scr_screen_id($scidx); if ($spridx != 0) { while (($spridx = sprite_var($spridx)->link) != 0) { $sprite = sprite_var($spridx); // pending removal or pointing into the freed script: drop instead of shifting into its neighbour if ($sprite->state == 1 || ($sprite->newad >= $start && $sprite->newad < $start + $length)) { $sprite->newad = 0; } elseif ($start < $sprite->newad) { $sprite->newad = ($sprite->newad - $length) & 0xffffff; } if ($sprite->data >= $start && $sprite->data < $start + $length) { $sprite->data = 0; } elseif ($start < $sprite->data) { $sprite->data = ($sprite->data - $length) & 0xffffff; } } } } } // Mirror of shrinkprog (id==0 path): open `length` bytes at `start` by shifting [start, finprog) up. // Returns 0 if it would overflow VM RAM. function growprog(int $start, int $length): int { if (ult(Alis::$finmem, s32(Alis::$finprog + $length))) { return 0; } // memmove handles the overlap (dst > src). mem_move($start + $length, $start, Alis::$finprog - $start); Alis::$finprog = s32(Alis::$finprog + $length); audio_relocate($start, $start, $length); for ($i = 0; $i < Alis::$nbprog; $i++) { if ($start <= s32(Alis::$loaded_scripts[$i]->data_org)) { Alis::$loaded_scripts[$i]->data_org += $length; xwrite32(Alis::$atprog + $i * 4, s32(xread32(Alis::$atprog + $i * 4) + $length)); } } for ($scidx = Alis::$platform->version >= 60 ? 0 : s16(Scr::$ptscreen); $scidx != 0; $scidx = s16(get_scr_to_next($scidx))) { $spridx = get_scr_screen_id($scidx); if ($spridx != 0) { while (($spridx = sprite_var($spridx)->link) != 0) { $sprite = sprite_var($spridx); if ($start < $sprite->newad) { $sprite->newad = ($sprite->newad + $length) & 0xffffff; } if ($start < $sprite->data) { $sprite->data = ($sprite->data + $length) & 0xffffff; } } } } return 1; } // v6 entities and programs share the arena, as the DOS pool function v6_enttop(): int { return s32(((Alis::$debent + Alis::$specs->max_allocatable_vram) | 0xf) + 1); } // ---------------------------------------------------------------------------- // Load-time, IN PLACE per-format converters to 0x14 (transparent) / 0x16 (opaque), named after // destofen's draw_*() handlers. Each writes pixels at +8 (8-bit chunky) and returns the bytes added // (0 = skip); the caller applies the table fixups. // ---------------------------------------------------------------------------- function conv_planar_allowed_for(?string $name): int { // v6 composes chunky, see v6_present if (Alis::$platform->version >= 60) { return 0; } if (!Alis::$platform->is_little_endian) { return 1; // BE data: 3D resources carry their own formats, convert all } if ($name === null) { return 0; } // Compare the basename without extension, case-insensitively ("MAIN.IO", "objet.io", ...). $dot = strpos($name, '.'); $base = strtolower($dot === false ? $name : substr($name, 0, $dot)); foreach (Opcodes2Vars::K_2D_ONLY_SCRIPTS as $b) { if ($base === $b) { return 1; } } return 0; } // Callers are all inside ALIS_PLANAR_CONV blocks (not in this build). function conv_planar_enabled(): int { return Opcodes2Vars::$s_conv_planar_ok; } // Planar iff byte 6 holds the plane count stamped by conv_finish (never in this chunky build). function sprite_is_planar(int $bitmap): int { return 0; } // Conversion scratch buffer. PORT: the decoded pixels are a local PHP string in each converter; // this only keeps the C bookkeeping (size of the largest scratch request) and never fails. function conv_tmp(int $n): int { if ($n > Opcodes2Vars::$s_conv_tmp_sz) { Opcodes2Vars::$s_conv_tmp_sz = $n; } return 1; } // Shared tail. `tmp` holds the decoded 8-bit indices (w*h); writes header + pixels to `dst`. // do_grow: open room in place with growprog (dst==src); else space is pre-allocated and the header // is relocated. Returns bytes added. function conv_finish(int $dst, int $src, int $src_off, int $old_pix_bytes, int $w, int $h, string $tmp, int $transp, int $do_grow): int { $npix = s32($w * $h); $out_bytes = $npix; // 8-bit chunky $grow = (8 + $out_bytes) - ($src_off + $old_pix_bytes); if ($do_grow) { if ($grow > 0 && !growprog($src + $src_off, $grow)) { return 0; } } elseif ($dst != $src) { mem_move($dst, $src, $src_off); // relocate header bytes } mem_write($dst + 8, $tmp); Alis::$mem[$dst + 6] = "\0"; Alis::$mem[$dst + 7] = "\0"; Alis::$mem[$dst] = $transp ? "\x14" : "\x16"; return $grow; } // 0x00/0x02 — ST 4-bit chunky (pixels at +6, no palette offset). function conv_st_4bit(int $src, int $dst, int $do_grow): int { $w = xread16($src + 2) + 1; $h = xread16($src + 4) + 1; $npix = s32($w * $h); if ($npix <= 0) { return 0; } $transp = (int) (Alis::$mem[$src] === "\0"); if (!conv_tmp($npix)) { return 0; } $s = substr(Alis::$mem, $src + 6, ($npix + 1) >> 1); $tmp = ''; for ($p = 0; $p < $npix; $p++) { $byte = ord($s[$p >> 1]); $nib = ($p & 1) ? ($byte & 0x0f) : ($byte >> 4); $tmp .= chr(($transp && $nib == 0) ? 0 : $nib); } return conv_finish($dst, $src, 6, $npix >> 1, $w, $h, $tmp, $transp, $do_grow); } // 0x10/0x12 — 4-bit chunky with palette-bank offset (pixels at +8, paloff at +6). function conv_4to8bit(int $src, int $dst, int $do_grow): int { $w = xread16($src + 2) + 1; $h = xread16($src + 4) + 1; $npix = s32($w * $h); if ($npix <= 0) { return 0; } $transp = (int) (Alis::$mem[$src] === "\x10"); $paloff = ord(Alis::$mem[$src + 6]); if (!conv_tmp($npix)) { return 0; } $s = substr(Alis::$mem, $src + 8, ($npix + 1) >> 1); $tmp = ''; for ($p = 0; $p < $npix; $p++) { $byte = ord($s[$p >> 1]); $nib = ($p & 1) ? ($byte & 0x0f) : ($byte >> 4); $tmp .= chr(($transp && $nib == 0) ? 0 : (($paloff + $nib) & 0xff)); } return conv_finish($dst, $src, 8, $npix >> 1, $w, $h, $tmp, $transp, $do_grow); } // 0x10/0x12 on Amiga — 5-bit planar (pixels at +6, 5 separate bitplanes, linear bitstream). function conv_ami_5bit(int $src, int $dst, int $do_grow): int { $w = xread16($src + 2) + 1; $h = xread16($src + 4) + 1; $npix = s32($w * $h); if ($npix <= 0) { return 0; } $transp = (int) (Alis::$mem[$src] === "\x10"); $planesize = $npix >> 3; if (!conv_tmp($npix)) { return 0; } $s = $src + 6; $tmp = ''; for ($p = 0; $p < $npix; $p++) { $byte = $p >> 3; $bit = 7 - ($p & 7); $idx = 0; for ($pl = 0; $pl < 5; $pl++) { $idx |= ((ord(Alis::$mem[$s + $byte + $pl * $planesize]) >> $bit) & 1) << $pl; } $tmp .= chr(($transp && $idx == 0) ? 0 : $idx); } return conv_finish($dst, $src, 6, 5 * $planesize, $w, $h, $tmp, $transp, $do_grow); } // Dispatch + convert one sprite from `src` to `dst` (matches PASS-1/2 platform gating exactly). // Returns bytes added, or 0 if this sprite is not converted on this platform. function convert_one(int $src, int $dst, int $do_grow): int { $fmt = ord(Alis::$mem[$src]); if ($fmt == 0x00) { if (Alis::$platform->kind != C::EPlatformMac && !PL_CGA()) { return conv_st_4bit($src, $dst, $do_grow); } } elseif ($fmt == 0x02) { if (Alis::$platform->kind != C::EPlatformMac && !(PL_CGA() && Alis::$platform->uid == C::EGameMadShow)) { return conv_st_4bit($src, $dst, $do_grow); } } elseif ($fmt == 0x10 || $fmt == 0x12) { return (Alis::$platform->px_format == C::EPxFormatAmPlanar) ? conv_ami_5bit($src, $dst, $do_grow) : conv_4to8bit($src, $dst, $do_grow); } return 0; } // Measure-only twin of convert_one: returns the grow (bytes added) and old on-disk total for the // sprite at `addr`, WITHOUT converting, so the compactor can lay out final positions up front. function sprite_measure(int $addr, int &$p_old_total): int { $fmt = ord(Alis::$mem[$addr]); $converts = 0; if ($fmt == 0x00) { $converts = (int) (Alis::$platform->kind != C::EPlatformMac && !PL_CGA()); $src_off = 6; } elseif ($fmt == 0x02) { $converts = (int) (Alis::$platform->kind != C::EPlatformMac && !(PL_CGA() && Alis::$platform->uid == C::EGameMadShow)); $src_off = 6; } elseif ($fmt == 0x10 || $fmt == 0x12) { $converts = 1; $src_off = (Alis::$platform->px_format == C::EPxFormatAmPlanar) ? 6 : 8; } elseif ($fmt == 0x14 || $fmt == 0x16) { $src_off = 8; } else { return -1; // not a convertible format (grow>=0 means "converts") } if (!$converts) { return -1; } $w = xread16($addr + 2) + 1; $h = xread16($addr + 4) + 1; $npix = s32($w * $h); if ($npix <= 0) { return -1; } if ($fmt == 0x10 || $fmt == 0x12) { $old_pix = (Alis::$platform->px_format == C::EPxFormatAmPlanar) ? 5 * ($npix >> 3) : ($npix >> 1); } elseif ($fmt == 0x14 || $fmt == 0x16) { $old_pix = $npix; } else { $old_pix = $npix >> 1; // 0x00 / 0x02 } $out_bytes = $npix; $p_old_total = $src_off + $old_pix; return (8 + $out_bytes) - ($src_off + $old_pix); } // One converting sprite, captured up front (addresses are original, pre-move): ['addr', 'grow', 'old_total']. function conv_items(int $n): int { if ($n > Opcodes2Vars::$s_conv_items_cap) { Opcodes2Vars::$s_conv_items_cap = $n; } Opcodes2Vars::$s_conv_items = []; return 1; } function conv_item_cmp(array $a, array $b): int { $x = $a['addr']; $y = $b['addr']; return ($x > $y) - ($x < $y); } // ---- The original per-sprite path: each conversion calls growprog, which shifts everything above // it (O(sprites x size) total bytes moved). Used by convert_sprites_inplace. ---- function convert_incremental(int $org, int $l, int $e, int $tab): int { $total = 0; for ($i = 0; $i < $e; $i++) { $a = $tab + $i * 4; $t = xread32($org + $a); $addr = s32($org + $a + $t); // re-read after grows $grow = convert_one($addr, $addr, 1 /*do_grow*/); if ($grow <= 0) { continue; } for ($j = 0; $j < $e; $j++) { // every sprite above this one shifted up by `grow` $aj = $tab + $j * 4; $tj = xread32($org + $aj); if ($addr < s32($org + $aj + $tj)) { xwrite32($org + $aj, s32($tj + $grow)); } } xwrite32($org + $l + 0x6, s32(xread32($org + $l + 0x6) + $grow)); xwrite32($org + $l + 0xc, s32(xread32($org + $l + 0xc) + $grow)); $total += $grow; } return $total; } // ---- Single-pass compaction (ALIS_CONV_COMPACT, unused by convert_sprites_inplace in the C either): // measure every sprite's grow up front, lay out final positions, then relocate each region exactly // once (top-down) while converting each sprite straight from its old slot into its final slot. ---- function convert_compact(int $org, int $l, int $e, int $tab): int { if (!conv_items($e > 0 ? $e : 1)) { return convert_incremental($org, $l, $e, $tab); // OOM → safe fallback } // PASS 2a: capture converting sprites + their grows (original, pre-move addresses). $it = []; $m = 0; $total = 0; for ($i = 0; $i < $e; $i++) { $addr = s32($org + $tab + $i * 4 + xread32($org + $tab + $i * 4)); $old_total = 0; $grow = sprite_measure($addr, $old_total); // grow >= 0 means the sprite converts; grow == 0 (e.g. 8-bit chunky -> 8-plane) must too. if ($grow >= 0) { $it[$m] = ['addr' => $addr, 'grow' => $grow, 'old_total' => $old_total]; $m++; $total += $grow; } } if ($m == 0) { return 0; } usort($it, conv_item_cmp(...)); // ascending by address // Offset table: entry j gains the sum of grows of all converting sprites BELOW its target. for ($j = 0; $j < $e; $j++) { $aj = $tab + $j * 4; $tj = xread32($org + $aj); $addrj = s32($org + $aj + $tj); $delta = 0; for ($k = 0; $k < $m && $it[$k]['addr'] < $addrj; $k++) { $delta += $it[$k]['grow']; } if ($delta) { xwrite32($org + $aj, s32($tj + $delta)); } } xwrite32($org + $l + 0x6, s32(xread32($org + $l + 0x6) + $total)); xwrite32($org + $l + 0xc, s32(xread32($org + $l + 0xc) + $total)); // Reserve the total space at the top (a single finprog bump suffices during load). $old_finprog = Alis::$finprog; if (ult(Alis::$finmem, s32($old_finprog + $total))) { return 0; } Alis::$finprog = s32($old_finprog + $total); // Relocate + convert, top-down. `shift` = bytes the region just above sprite k moves up. $read_top = $old_finprog; $shift = $total; for ($k = $m - 1; $k >= 0; $k--) { $ak = $it[$k]['addr']; $reg_lo = $ak + $it[$k]['old_total']; // first byte above sprite k's old data $reg_len = $read_top - $reg_lo; if ($reg_len > 0 && $shift > 0) { mem_move($reg_lo + $shift, $reg_lo, $reg_len); } $Gk = $shift - $it[$k]['grow']; // sprite k's new slot = ak + Gk convert_one($ak, $ak + $Gk, 0 /*space pre-allocated*/); $read_top = $ak; $shift = $Gk; } Opcodes2Vars::$s_conv_items = $it; return $total; } // Load-time, IN PLACE: convert every sprite to 0x14/0x16 (8-bit chunky) via per-format converters // dispatched like destofen. Applies cshrink's fixups with the sign flipped. Returns total bytes added // (caller bumps script->sz). function convert_sprites_inplace(int $org, ?string $name): int { // Decide ONCE per script, before pass 1. Opcodes2Vars::$s_conv_planar_ok = conv_planar_allowed_for($name); $l = xread32($org + 0xe); $e = xread16($org + $l + 4); $tab = s32(xread32($org + $l) + $l); // PASS 2: convert every sprite. $total = convert_incremental($org, $l, $e, $tab); return $total; } // ============================================================================ // Opcode / Codop (Code-op) routines pointer table (256 values) // ============================================================================ /** C: opcodes[] — the 256 opcode handlers in C order (also fills Tables::$names['opcodes']). */ function table_opcodes(): array { Tables::$names['opcodes'] = [ 'cnul', 'cesc1', 'cesc2', 'cesc3', 'cbreakpt', 'cjsr8', 'cjsr16', 'cjsr24', 'cjmp8', 'cjmp16', 'cjmp24', 'cjsrabs', 'cjmpabs', 'cjsrind16', 'cjsrind24', 'cjmpind16', 'cjmpind24', 'cret', 'cbz8', 'cbz16', 'cbz24', 'cbnz8', 'cbnz16', 'cbnz24', 'cbeq8', 'cbeq16', 'cbeq24', 'cbne8', 'cbne16', 'cbne24', 'cstore', 'ceval', 'cadd', 'csub', 'cmul', 'cdiv', 'cvprint', 'csprinti', 'csprinta', 'clocate', 'ctab', 'cdim', 'crandom', 'cloop8', 'cloop16', 'cloop24', 'cswitch1', 'cswitch2', 'cstart8', 'cstart16', 'cstart24', 'cleave', 'cprotect', 'casleep', 'cclock', 'canim', 'cscmov', 'cscset', 'cclipping', 'cswitching', 'cwlive', 'cunload', 'cwakeup', 'csleep', 'clive', 'ckill', 'cstop', 'cstopret', 'cexit', 'cload', 'cdefsc', 'cscreen', 'cput', 'cputnat', 'cerase', 'cerasen', 'cset', 'cmov', 'copensc', 'cclosesc', 'cerasall', 'cforme', 'cdelforme', 'ctstmov', 'ctstset', 'cftstmov', 'cftstset', 'csuccent', 'cpredent', 'cnearent', 'cneartyp', 'cnearmat', 'cviewent', 'cviewtyp', 'cviewmat', 'corient', 'crstent', 'csend', 'cscanon', 'cscanoff', 'cinteron', 'cinteroff', 'cscanclr', 'callentity', 'cpalette', 'cdefcolor', 'ctiming', 'czap', 'cexplode', 'cding', 'cnoise', 'cinitab', 'cfopen', 'cfclose', 'cfcreat', 'cfdel', 'cfreadv', 'cfwritev', 'cfwritei', 'cfreadb', 'cfwriteb', 'cplot', 'cdraw', 'cbox', 'cboxf', 'cink', 'cpset', 'cpmove', 'cpmode', 'cpicture', 'cxyscroll', 'clinking', 'cmouson', 'cmousoff', 'cmouse', 'cdefmouse', 'csetmouse', 'cdefvect', 'csetvect', 'cnul', 'capproach', 'cescape', 'cvtstmov', 'cvftstmov', 'cvmov', 'cdefworld', 'cworld', 'cfindmat', 'cfindtyp', 'cmusic', 'cdelmusic', 'ccadence', 'csetvolum', 'cxinv', 'cxinvon', 'cxinvoff', 'clistent', 'csound', 'cmsound', 'credon', 'credoff', 'cdelsound', 'cwmov', 'cwtstmov', 'cwftstmov', 'ctstform', 'cxput', 'cxputat', 'cmput', 'cmputat', 'cmxput', 'cmxputat', 'cmmusic', 'cmforme', 'csettime', 'cgettime', 'cvinput', 'csinput', 'casleepfar', 'casleepon', 'casleepoff', 'crunfilm', 'cvpicprint', 'cspicprint', 'cvputprint', 'csputprint', 'cfont', 'cpaper', 'ctoblack', 'cmovcolor', 'ctopalet', 'cnumput', 'cscheart', 'cscpos', 'cscsize', 'cschoriz', 'cscvertic', 'cscreduce', 'cscscale', 'creducing', 'cscmap', 'cscdump', 'cfindcla', 'cnearcla', 'cviewcla', 'cinstru', 'cminstru', 'cordspr', 'calign', 'cbackstar', 'cstarring', 'cengine', 'cautobase', 'cquality', 'chsprite', 'cselpalet', 'clinepalet', 'cautomode', 'cautofile', 'ccancel', 'ccancall', 'ccancen', 'cblast', 'cscback', 'cscrolpage', 'cmatent', 'cshrink', 'cdefmap', 'csetmap', 'cputmap', 'csavepal', 'csczoom', 'ctexmap', 'calloctab', 'cfreetab', 'cscantab', 'cneartab', 'cscsun', 'cdarkpal', 'cscdark', 'caset', 'camov', 'cscaset', 'cscamov', 'cscfollow', 'cscview', 'cfilm', 'cwalkmap', 'catstmap', 'cavtstmov', 'cavmov', 'caim', 'cpointpix', 'cchartmap', 'cscsky', 'czoom', ]; return [ cnul(...), // 0x00 cesc1(...), // 0x01 cesc2(...), // 0x02 cesc3(...), // 0x03 cbreakpt(...), // 0x04 cjsr8(...), // 0x05 cjsr16(...), // 0x06 cjsr24(...), // 0x07 cjmp8(...), // 0x08 cjmp16(...), // 0x09 cjmp24(...), // 0x0a cjsrabs(...), // 0x0b cjmpabs(...), // 0x0c cjsrind16(...), // 0x0d cjsrind24(...), // 0x0e cjmpind16(...), // 0x0f cjmpind24(...), // 0x10 cret(...), // 0x11 cbz8(...), // 0x12 cbz16(...), // 0x13 cbz24(...), // 0x14 cbnz8(...), // 0x15 cbnz16(...), // 0x16 cbnz24(...), // 0x17 cbeq8(...), // 0x18 cbeq16(...), // 0x19 cbeq24(...), // 0x1a cbne8(...), // 0x1b cbne16(...), // 0x1c cbne24(...), // 0x1d cstore(...), // 0x1e ceval(...), // 0x1f cadd(...), // 0x20 csub(...), // 0x21 cmul(...), // 0x22 op_cdiv(...), // 0x23 cdiv (op_cdiv: name clash with the runtime helper) cvprint(...), // 0x24 csprinti(...), // 0x25 csprinta(...), // 0x26 clocate(...), // 0x27 ctab(...), // 0x28 cdim(...), // 0x29 crandom(...), // 0x2a cloop8(...), // 0x2b cloop16(...), // 0x2c cloop24(...), // 0x2d cswitch1(...), // 0x2e cswitch2(...), // 0x2f cstart8(...), // 0x30 cstart16(...), // 0x31 cstart24(...), // 0x32 cleave(...), // 0x33 cprotect(...), // 0x34 casleep(...), // 0x35 cclock(...), // 0x36 canim(...), // 0x37 cscmov(...), // 0x38 cscset(...), // 0x39 cclipping(...), // 0x3a cswitching(...), // 0x3b cwlive(...), // 0x3c cunload(...), // 0x3d cwakeup(...), // 0x3e csleep(...), // 0x3f clive(...), // 0x40 ckill(...), // 0x41 cstop(...), // 0x42 cstopret(...), // 0x43 cexit(...), // 0x44 cload(...), // 0x45 cdefsc(...), // 0x46 cscreen(...), // 0x47 cput(...), // 0x48 cputnat(...), // 0x49 cerase(...), // 0x4a cerasen(...), // 0x4b cset(...), // 0x4c cmov(...), // 0x4d copensc(...), // 0x4e cclosesc(...), // 0x4f cerasall(...), // 0x50 cforme(...), // 0x51 cdelforme(...), // 0x52 ctstmov(...), // 0x53 ctstset(...), // 0x54 cftstmov(...), // 0x55 cftstset(...), // 0x56 csuccent(...), // 0x57 cpredent(...), // 0x58 cnearent(...), // 0x59 cneartyp(...), // 0x5a cnearmat(...), // 0x5b cviewent(...), // 0x5c cviewtyp(...), // 0x5d cviewmat(...), // 0x5e corient(...), // 0x5f crstent(...), // 0x60 csend(...), // 0x61 cscanon(...), // 0x62 cscanoff(...), // 0x63 cinteron(...), // 0x64 cinteroff(...), // 0x65 cscanclr(...), // 0x66 callentity(...), // 0x67 cpalette(...), // 0x68 cdefcolor(...), // 0x69 ctiming(...), // 0x6a czap(...), // 0x6b cexplode(...), // 0x6c cding(...), // 0x6d cnoise(...), // 0x6e cinitab(...), // 0x6f cfopen(...), // 0x70 cfclose(...), // 0x71 cfcreat(...), // 0x72 cfdel(...), // 0x73 cfreadv(...), // 0x74 cfwritev(...), // 0x75 cfwritei(...), // 0x76 cfreadb(...), // 0x77 cfwriteb(...), // 0x78 cplot(...), // 0x79 cdraw(...), // 0x7a cbox(...), // 0x7b cboxf(...), // 0x7c cink(...), // 0x7d cpset(...), // 0x7e cpmove(...), // 0x7f cpmode(...), // 0x80 cpicture(...), // 0x81 cxyscroll(...), // 0x82 clinking(...), // 0x83 cmouson(...), // 0x84 cmousoff(...), // 0x85 cmouse(...), // 0x86 cdefmouse(...), // 0x87 csetmouse(...), // 0x88 cdefvect(...), // 0x89 csetvect(...), // 0x8a cnul(...), // 0x8b capproach(...), // 0x8c cescape(...), // 0x8d cvtstmov(...), // 0x8e cvftstmov(...), // 0x8f cvmov(...), // 0x90 cdefworld(...), // 0x91 cworld(...), // 0x92 cfindmat(...), // 0x93 cfindtyp(...), // 0x94 cmusic(...), // 0x95 cdelmusic(...), // 0x96 ccadence(...), // 0x97 csetvolum(...), // 0x98 cxinv(...), // 0x99 cxinvon(...), // 0x9a cxinvoff(...), // 0x9b clistent(...), // 0x9c csound(...), // 0x9d cmsound(...), // 0x9e credon(...), // 0x9f credoff(...), // 0xa0 cdelsound(...), // 0xa1 cwmov(...), // 0xa2 cwtstmov(...), // 0xa3 cwftstmov(...), // 0xa4 ctstform(...), // 0xa5 cxput(...), // 0xa6 cxputat(...), // 0xa7 cmput(...), // 0xa8 cmputat(...), // 0xa9 cmxput(...), // 0xaa cmxputat(...), // 0xab cmmusic(...), // 0xac cmforme(...), // 0xad csettime(...), // 0xae cgettime(...), // 0xaf cvinput(...), // 0xb0 csinput(...), // 0xb1 casleepfar(...), // 0xb2 casleepon(...), // 0xb3 casleepoff(...), // 0xb4 crunfilm(...), // 0xb5 cvpicprint(...), // 0xb6 cspicprint(...), // 0xb7 cvputprint(...), // 0xb8 csputprint(...), // 0xb9 cfont(...), // 0xba cpaper(...), // 0xbb ctoblack(...), // 0xbc cmovcolor(...), // 0xbd ctopalet(...), // 0xbe cnumput(...), // 0xbf cscheart(...), // 0xc0 cscpos(...), // 0xc1 cscsize(...), // 0xc2 cschoriz(...), // 0xc3 cscvertic(...), // 0xc4 cscreduce(...), // 0xc5 cscscale(...), // 0xc6 creducing(...), // 0xc7 cscmap(...), // 0xc8 cscdump(...), // 0xc9 cfindcla(...), // 0xca cnearcla(...), // 0xcb cviewcla(...), // 0xcc cinstru(...), // 0xcd cminstru(...), // 0xce cordspr(...), // 0xcf calign(...), // 0xd0 cbackstar(...), // 0xd1 cstarring(...), // 0xd2 cengine(...), // 0xd3 cautobase(...), // 0xd4 cquality(...), // 0xd5 chsprite(...), // 0xd6 cselpalet(...), // 0xd7 clinepalet(...), // 0xd8 cautomode(...), // 0xd9 cautofile(...), // 0xda ccancel(...), // 0xdb ccancall(...), // 0xdc ccancen(...), // 0xdd cblast(...), // 0xde cscback(...), // 0xdf cscrolpage(...), // 0xe0 cmatent(...), // 0xe1 cshrink(...), // 0xe2 cdefmap(...), // 0xe3 csetmap(...), // 0xe4 cputmap(...), // 0xe5 csavepal(...), // 0xe6 csczoom(...), // 0xe7 ctexmap(...), // 0xe8 calloctab(...), // 0xe9 cfreetab(...), // 0xea cscantab(...), // 0xeb cneartab(...), // 0xec cscsun(...), // 0xed cdarkpal(...), // 0xee cscdark(...), // 0xef caset(...), // 0xf0 camov(...), // 0xf1 cscaset(...), // 0xf2 cscamov(...), // 0xf3 cscfollow(...), // 0xf4 cscview(...), // 0xf5 cfilm(...), // 0xf6 cwalkmap(...), // 0xf7 catstmap(...), // 0xf8 cavtstmov(...), // 0xf9 cavmov(...), // 0xfa caim(...), // 0xfb cpointpix(...), // 0xfc cchartmap(...), // 0xfd cscsky(...), // 0xfe czoom(...), // 0xff ]; } /** * C: alis_tab_opcodes() — the live opcode table: the base table for version < 60, else a copy of it with the * v6 overrides (table_opcodes_v6_ov(), opcodes_v6b.php) applied. PORT: the C static cache is also keyed by * the table kind (OpcodesV6bVars::$live_v6), as one PHP process may run several games. */ function alis_tab_opcodes(): array { $v6 = Alis::$platform->version >= 60; if (Opcodes2Vars::$live === null || OpcodesV6bVars::$live_v6 !== $v6) { OpcodesV6bVars::$live_v6 = $v6; if (!$v6) { Opcodes2Vars::$live = table_opcodes(); } else { $live = table_opcodes(); foreach (table_opcodes_v6_ov() as $code => $entry) { $live[$code & 0xff] = $entry[0]; Tables::$names['opcodes'][$code & 0xff] = $entry[1]; } Opcodes2Vars::$live = $live; } } return Opcodes2Vars::$live; }