= 0 && $index < 64) { $f = Alis::$v6fonts[$index]; $f['foasc'] = $foasc; $f['fonum'] = $fonum; $f['fomax'] = $fomax; $f['folarg'] = $folarg & 0xff; $f['fohaut'] = $fohaut & 0xff; $f['mode'] = $mode & 0xff; $f['usemain'] = Alis::$flagmain & 0xff; $f['valid'] = 1; Alis::$v6fonts[$index] = $f; $x = Alis::$v6xfonts[$index]; $x['id'] = $mode; $x['foasc'] = $foasc; $x['count'] = $fomax; $x['spacing'] = $folarg; $x['height'] = $fohaut; $x['glyphslot'] = ($fonum >= 0) ? adresdes($fonum) : 0; $x['valid'] = 1; $x['op2'] = $op2; $x['form'] = $fonum; $x['owner'] = xread16(Alis::$script->vram_org + 0x3a) & 0xffff; $x['ownername'] = mem_read(Alis::$script->vram_org + 0x3c, 8); // u8[8] as an 8-byte string Alis::$v6xfonts[$index] = $x; } } // Codesc1name no. 009 opcode 0x08 cloadtext (text_read, v6text_get, v6text_free, v6text_reload: escnames.php) // load name.to as a new text bloc function v6text_load(string $name): int { $slot = 0; while ($slot < 8 && (Alis::$v6texts[$slot]['node'] ?? 0) != 0) { $slot++; } $sz = 0; $data = $slot < 8 ? text_read($name, $sz) : 0; if ($data == 0) { return 0; } $node = node_take(0xc); if ($node == 0) { host_free($data); return 0; } node_setname($node, $name); Alis::$v6texts[$slot] = ['node' => $node & 0xffff, 'sz' => $sz, 'data' => $data]; return $node; } function cloadtext(): void { readexec_opername_swap(); // char name[24]; strncpy(name, alis.sd7, sizeof(name) - 5) $name = substr(mem_str(Alis::$sd7), 0, 24 - 5); if (Alis::$platform->version >= 60) { // a resident block of that name is not reloaded if (node_find($name) == 0) { v6text_load($name); } return; } $sz = 0; $data = text_read($name, $sz); if ($data == 0) { return; } Alis::$text_name = substr($name . '.to', 0, 15); // snprintf(text_name, 16, "%s.to", name) host_free(Alis::$text_data); Alis::$text_data = $data; Alis::$text_sz = $sz; } // v6 escape 0x09/0x0a/0x0b/0x0c function V6_VECFLAGS(int $vram): int { return $vram - 0x67; } function cafteron(): void { $vram = Alis::$script->vram_org; $data = get_0x14_script_org_offset($vram); if (xread32($data + 0x20) != 0) { xwrite8(V6_VECFLAGS($vram), xread8(V6_VECFLAGS($vram)) | 0x2); } } // Codesc1name no. 011 opcode 0x0a cafteroff function cafteroff(): void { $vram = Alis::$script->vram_org; xwrite8(V6_VECFLAGS($vram), xread8(V6_VECFLAGS($vram)) & 0xfd); } // Codesc1name no. 012 opcode 0x0b cbeforon function cbeforon(): void { $vram = Alis::$script->vram_org; $data = get_0x14_script_org_offset($vram); if (xread32($data + 0x1c) != 0) { xwrite8(V6_VECFLAGS($vram), xread8(V6_VECFLAGS($vram)) | 0x1); } } // Codesc1name no. 013 opcode 0x0c cbeforoff // Addition v40+: cbeforoff function cbeforoff(): void { $vram = Alis::$script->vram_org; xwrite8(V6_VECFLAGS($vram), xread8(V6_VECFLAGS($vram)) & 0xfe); } // Codesc1name no. 014 opcode 0x0d cscfocale function readexec_opername_v6(): void { if (Alis::$platform->version >= 60) { readexec_opername(); } else { readexec_opername_saveD7(); } } function cscfocale(): void { $scr = s32(Alis::$basemain + xread16(Alis::$script->vram_org - 0x64)); readexec_opername_v6(); xwrite16($scr + 0x48, Alis::$varD7); readexec_opername_v6(); xwrite16($scr + 0x4a, Alis::$varD7); readexec_opername_v6(); xwrite16($scr + 0x4c, Alis::$varD7); xwrite16($scr + 0x4e, 0); xwrite16($scr + 0x50, 0); xwrite16($scr + 0x52, 0); readexec_opername_v6(); xwrite32($scr + 0x142, s32(Alis::$varD7)); readexec_opername_v6(); xwrite32($scr + 0x146, s32(Alis::$varD7)); xwrite16($scr + 0x44, s16(xread32($scr + 0x146) >> 1)); xwrite16($scr + 0x42, 0); xwrite16($scr + 0x46, 0); v6x_focangle($scr); $eax = s32(shr32(xread32($scr + 0x146), 1) + 1 + 0xc8); xwrite16($scr + 0x40, s16($eax)); Alis::$v6focstep = shr32($eax, 3); v6_scdark_precalc($scr); } // Codesc1name no. 015 opcode 0x0e csccamera function csccamera(): void { if (Alis::$platform->version >= 60) { v6_skipprot(); return; } // MISSING } // resource record for a form id; $slot = its self-relative table entry function texres(int $id, int &$slot): int { $slot = adresdes(s16($id)); return s32($slot + xread32($slot)); } // a linked resource remembers the current texture id at +8 function texmark(int $res): void { if ($res) { xwrite32($res + 8, s32(Alis::$texcur | shl32(Alis::$texcur, 16))); } } // Codesc1name no. 016 opcode 0x0f ctexmap (escnames.c ctexmap, renamed: opcodes.c has one too) function esc_ctexmap(): void { if (Alis::$platform->uid != C::EGameRobinsonsRequiem3DO) { return; } Alis::$texmap = read_vaddr(); readexec_opername_v6(); Alis::$texcur = Alis::$varD7 & 0xffff; $e = s32(Alis::$texmap + s16(Alis::$varD7) * 0x14 - 0xc00); readexec_opername_v6(); xwrite8($e + 8, Alis::$varD7); readexec_opername_v6(); $res = 0; $slot = 0; $kind = s8(xread8($e + 8)); if ($kind > 0) { $res = texres(Alis::$varD7, $slot); xwrite32($e, s32($res - $e)); } elseif ($kind == 0) { xwrite32($e, 0); } readexec_opername_v6(); readexec_opername_v6(); xwrite8($e + 4, Alis::$varD7); readexec_opername_v6(); texmark($res); readexec_opername_v6(); $mode = s8(Alis::$varD7); xwrite8($e + 9, $mode); if ($mode < 0) { for ($i = 0; $i < 4; $i++) { readexec_opername_v6(); } xwrite32($e + 0xc, 0); return; } readexec_opername_v6(); if ($mode == 2) { xwrite16($e + 0x14, Alis::$varD7); for ($i = 0; $i < 3; $i++) { readexec_opername_v6(); } return; } Alis::$texcur = Alis::$varD7 & 0xffff; $res = 0; if ($mode == 0) { xwrite32($e + 0xc, 0); } else { $res = texres(Alis::$varD7, $slot); $type = xread8($res); if ($type == 6 || $type == 2) { xwrite16($e + 0xa, xread32($res + 0x14)); $sub = s16(xread32($res + 0xc)); if ($type == 6) { Alis::$texcur = (Alis::$texcur + $sub) & 0xffff; $slot = s32($slot + $sub * 4); $res = s32($slot + xread32($slot)); } else { Alis::$texcur = $sub & 0xffff; $res = texres($sub, $slot); } } else { xwrite16($e + 0xa, 0); } xwrite32($e + 0xc, s32($res - ($e + 0xc))); xwrite32($e + 0x10, xread16(Alis::$script->vram_org + 0x3a) & 0xffff); } for ($i = 0; $i < 3; $i++) { readexec_opername_v6(); } texmark($res); } // Codesc1name no. 017 opcode 0x10 cscrender function cscrender(): void { $scr = s32(Alis::$basemain + xread16(Alis::$script->vram_org - 0x64)); // MOVSX readexec_opername_v6(); xwrite16($scr + 0x13c, Alis::$varD7); readexec_opername_v6(); xwrite32($scr + 0x14a, s32(Alis::$varD7)); readexec_opername_v6(); xwrite32($scr + 0x14e, s32(Alis::$varD7)); readexec_opername_v6(); xwrite32($scr + 0x152, s32(Alis::$varD7)); readexec_opername_v6(); xwrite32($scr + 0x15a, s32(Alis::$varD7)); readexec_opername_v6(); xwrite32($scr + 0x156, s32(Alis::$varD7)); readexec_opername_v6(); xwrite32($scr + 0x15e, s32(Alis::$varD7)); readexec_opername_v6(); xwrite32($scr + 0x166, s32(Alis::$varD7)); readexec_opername_v6(); xwrite32($scr + 0x16a, s32(Alis::$varD7)); readexec_opername_v6(); xwrite32($scr + 0x162, s32(Alis::$varD7)); readexec_opername_v6(); xwrite32($scr + 0x172, s32(Alis::$varD7)); } // Codesc1name no. 018 opcode 0x11 cvideo function cvideo(): void { readexec_opername(); $mode = s16(Alis::$varD7); readexec_opername_saveD7(); $w = s16(Alis::$varD7); readexec_opername_saveD7(); $h = s16(Alis::$varD7); if (Alis::$platform->version >= 60) { if ($mode == 0 || $mode == 2) { Alis::$videomode = 0; } elseif ($mode == 1) { Alis::$videomode = 1; } resize_screen($w, $h); } } // Codesc1name no. 019 opcode 0x12 ctexture function ctexture(): void { readexec_opername_v6(); $entry = adresdes(Alis::$varD7); $desc = ($entry == 0xf) ? 0 : s32($entry + xread32($entry)); readexec_opername_v6(); $idx = s16(Alis::$varD7); if ($desc == 0 || xread8($desc) != 8 || ($idx & 0xffff) >= (xread16($desc + 0x32) & 0xffff)) { readexec_opername_v6(); readexec_opername_v6(); readexec_opername_v6(); readexec_opername_v6(); return; } $item = s32($desc + (($idx & 0xffff) << 5) + xread32($desc + 0x44)); readexec_opername_v6(); $sub = Alis::$varD7 & 0xff; switch ($sub) { case 1: readexec_opername_v6(); $e2 = adresdes(Alis::$varD7); $d2 = ($e2 == 0xf) ? 0 : s32($e2 + xread32($e2)); xwrite32($item + 0xc, s32($e2 - $item)); xwrite32($item, s32($d2 - $item)); readexec_opername_v6(); xwrite8($item + 0x10, Alis::$varD7 & 0xff); readexec_opername_v6(); xwrite8($item + 0x12, Alis::$varD7 & 0xff); xwrite8($item + 0x13, 0); xwrite8($item + 0x11, 0); break; case 5: readexec_opername_v6(); xwrite16($item + 0x14, Alis::$varD7); readexec_opername_v6(); readexec_opername_v6(); break; case 6: readexec_opername_v6(); xwrite16($item + 0x16, Alis::$varD7); readexec_opername_v6(); readexec_opername_v6(); break; case 3: readexec_opername_v6(); $p1 = s16(Alis::$varD7); readexec_opername_v6(); $p2 = s16(Alis::$varD7); readexec_opername_v6(); $p3 = s16(Alis::$varD7); xwrite8($item + 0xb, ((($p3 & 3) << 6) + (($p1 & 7) << 3) + ($p2 & 7)) & 0xff); break; case 7: readexec_opername_v6(); xwrite8($item + 0x8, xread8($item + 0x8) & 0xf7); readexec_opername_v6(); readexec_opername_v6(); break; case 8: readexec_opername_v6(); xwrite8($item + 0x8, xread8($item + 0x8) | 0x8); readexec_opername_v6(); readexec_opername_v6(); break; case 9: readexec_opername_v6(); $dl = Alis::$varD7 & 0xff; if ($dl <= 3) { $dl = 3; } xwrite8($item + 0x9, (xread8($item + 0x9) & 0xfc) | $dl); readexec_opername_v6(); readexec_opername_v6(); break; case 0xa: readexec_opername_v6(); xwrite8($item + 0xa, Alis::$varD7 & 0xff); readexec_opername_v6(); readexec_opername_v6(); break; case 0xb: readexec_opername_v6(); if (Alis::$varD7 != 0) { xwrite8($item + 0x8, xread8($item + 0x8) & 0xef); } else { xwrite8($item + 0x8, xread8($item + 0x8) | 0x10); } readexec_opername_v6(); readexec_opername_v6(); break; default: readexec_opername_v6(); readexec_opername_v6(); readexec_opername_v6(); break; } } // Codesc1name no. 020 opcode 0x13 cplaycd function cplaycd(): void { readexec_opername(); $cmd = s16(Alis::$varD7); readexec_opername(); $n = s16(Alis::$varD7); readexec_opername(); if (Alis::$platform->uid == C::EGameRobinsonsRequiem3DO) { if ($cmd == 1 && $n >= 0 && $n <= 9) { V::$trk = chr(ord('0') + $n); } elseif ($cmd == 2) { $path = Alis::$platform->path . 'track' . V::$trk; cdtrack_play($path, s8(Alis::$varD7)); } elseif ($cmd == 3) { cdtrack_stop(); } Alis::$varD7 = 0; } // TODO: CD audio playback / sound routine - Deus DOS sound (OPL2/CD) not implemented. cstore_continue(); } // Codesc1name no. 021 opcode 0x14 cnul // Codesc1name no. 022 opcode 0x15 cobjtrans // Codesc1name no. 023 opcode 0x16 conscreen function conscreen(): void { // MISSING } // Codesc1name no. 024/025 opcodes 0x17 cgetglobal / 0x18 cgetlocal: opcodes.c // Codesc1name no. 026 opcode 0x19 cmatitem function item_word(int $off): void { readexec_opername(); $desc = adresdes(s16(Alis::$varD7)); $rec = ($desc == 0xf) ? 0 : s32($desc + xread32($desc)); readexec_opername(); if ($rec == 0) { return; } $type = xread8($rec); if ($type != 3 && $type != 9 && $type != 2) { xwrite16($rec + $off, Alis::$varD7); } } function cmatitem(): void { item_word(0x8); } // Codesc1name no. 027 opcode 0x1a cmaskitem function cmaskitem(): void { item_word(0xa); } // Codesc1name no. 028 opcode 0x1b cdefitem function cdefitem(): void { if (Alis::$platform->version < 60) { readexec_opername(); readexec_opername(); readexec_opername(); readexec_opername(); readexec_opername(); return; } readexec_opername(); $form = s16(Alis::$varD7); $desc = adresdes($form); $rec = ($desc == 0xf) ? 0 : s32($desc + xread32($desc)); $type = $rec ? xread8($rec) : 0xff; if ($type == 7) { readexec_opername(); $sel = Alis::$varD7; if ($sel == 3) { readexec_opername(); if ($rec) { xwrite8($rec + 0x1b, Alis::$varD7 & 0xff); } readexec_opername(); if ($rec) { xwrite16($rec + 0x12, Alis::$varD7 & 0xffff); } readexec_opername(); if ($rec) { xwrite16($rec + 0x38, 0xff); } return; } if ($sel == 8) { readexec_opername(); if ($rec) { xwrite8($rec + 0x37, Alis::$varD7 & 0xff); } readexec_opername(); readexec_opername(); return; } readexec_opername(); readexec_opername(); readexec_opername(); readexec_opername(); return; } if ($type == 8) { readexec_opername(); $sel = Alis::$varD7; if ($sel == 7) { readexec_opername(); if ($rec) { xwrite16($rec + 0x26, Alis::$varD7 & 0xffff); } readexec_opername(); readexec_opername(); return; } if ($rec) { if ($sel == 4) { xwrite32($rec + 0x28, xread32($rec + 0x28) | 0x10); } elseif ($sel == 5) { xwrite32($rec + 0x28, xread32($rec + 0x28) | 0x20); } elseif ($sel == 6) { xwrite32($rec + 0x28, xread32($rec + 0x28) | 0x08); } elseif ($sel == 9) { xwrite32($rec + 0x28, xread32($rec + 0x28) | 0x80); } } readexec_opername(); readexec_opername(); readexec_opername(); return; } readexec_opername(); readexec_opername(); readexec_opername(); readexec_opername(); } // Codesc1name no. 029 opcode 0x1c cnul // Codesc1name no. 030 opcode 0x1d cnul // Codesc1name no. 031 opcode 0x1e cgetmap function cgetmap(): void { // MISSING } // Codesc1name no. 032 opcode 0x1f cconvmap function cconvmap(): void { // MISSING } // Codesc1name no. 033 opcode 0x20 csetfilm function csetfilm(): void { // MISSING } // Codesc1name no. 034 opcode 0x21 cscmem function cscmem(): void { // MISSING } function cmapcell(): void { if (Alis::$platform->uid == C::EGameRobinsonsRequiem3DO) { v6_mapcell(-1); } } // Codesc1name no. 035 opcode 0x22 // Codesc1name no. 036 opcode 0x23 cobjaim: opcodes.c (shares the caim tail) // Codesc1name no. 037 opcode 0x24 cloadpic function cloadpic(): void { // MISSING } // Codesc1name no. 038 opcode 0x25 cputpic function cputpic(): void { // MISSING } // --------------------------------------------------------------------------- // v6 save game, byte-exact DOS chunk format, little endian. // PORT: byte buffers are PHP strings; the static sg_hdr[16] is EscnamesV6Vars::$sg_hdr (bytes a write // does not set keep their previous value, as in the C). // --------------------------------------------------------------------------- function sg_le32s(int $v): string { return chr($v & 0xff) . chr(($v >> 8) & 0xff) . chr(($v >> 16) & 0xff) . chr(($v >> 24) & 0xff); } function sg_le16s(int $v): string { return chr($v & 0xff) . chr(($v >> 8) & 0xff); } /** C: sg_le32(p, v) on a byte buffer (string) at offset $o */ function sg_le32(string &$b, int $o, int $v): void { $b = substr_replace($b, sg_le32s($v), $o, 4); } /** C: sg_le16(p, v) on a byte buffer (string) at offset $o */ function sg_le16(string &$b, int $o, int $v): void { $b = substr_replace($b, sg_le16s($v), $o, 2); } function sg_write(string $p): void { fwrite(Alis::$fp, $p); } function sg_chunk(string $tag, int $a, int $n): void { V::$sg_hdr = substr_replace(V::$sg_hdr, substr($tag, 0, 4), 0, 4); sg_le32(V::$sg_hdr, 4, $a); sg_write(substr(V::$sg_hdr, 0, $n)); } function sg_link(int $vram, int $p): int { return $p == v6_elemhead($vram) ? $vram - 0x2c : ($p == v6_elemtail($vram) ? $vram - 0x24 : $p); } /** C: sg_ctx(s, c): returns the 0x40-byte DOS context block (c[0..0x3f], d = c + 0x40). */ function sg_ctx(ScriptLive $s): string { $v = $s->vram_org; $org = get_0x14_script_org_offset($v); $qsize = xread16($org + 0x36) & 0xffff; $stk = xread16($org + 0x34) & 0xffff; $c = str_repeat("\0", 0x40); $d = 0x40; sg_le16($c, $d - 0x40, get_0x1e_scan_clr($v)); sg_le16($c, $d - 0x3e, get_0x1c_scan_clr($v)); sg_le16($c, $d - 0x3c, get_0x0a_vacc_offset($v) + 0x40); sg_le16($c, $d - 0x3a, -(0x40 + $qsize)); sg_le16($c, $d - 0x38, -(0x40 + $qsize + $stk)); $ent = get_0x0e_script_ent($v); sg_le16($c, $d - 0x36, xread32(Alis::$atent + $ent + 8) - xread16(Alis::$atent + $ent + 6)); $mark = xread16($v - 0x7c) & 0xffff; sg_le16($c, $d - 0x32, $mark ? $mark + 0x40 : 0); $home = xread32($v - 0x80); sg_le32($c, $d - 0x30, $mark ? s32($s->pc_org + $home) : 0); sg_le32($c, $d - 0x2c, $v - 0x2c); sg_le32($c, $d - 0x28, sg_link($v, v6el_next(v6_elemhead($v)))); sg_le32($c, $d - 0x24, sg_link($v, v6el_prev(v6_elemtail($v)))); sg_le32($c, $d - 0x20, $v - 0x24); $c[$d - 0x1c] = chr(xread8($v - 0x66)); sg_le16($c, $d - 0x18, xread16($v - 0x64)); sg_le16($c, $d - 0x16, get_0x16_screen_id($v)); sg_le16($c, $d - 0x14, xread16(V6_CLINKING($v))); sg_le16($c, $d - 0x12, get_0x3a_wait_cycles($v)); sg_le32($c, $d - 0x10, get_0x3e_wait_time($v)); sg_le32($c, $d - 0x0c, $org); sg_le32($c, $d - 0x08, get_0x08_script_ret_offset($v)); $c[$d - 0x04] = chr(C::V6_SLOT_STRIDE); $c[$d - 0x03] = chr(get_0x2b_cordspr($v)); $s24 = get_0x24_scan_inter($v) & 0xff; $c[$d - 0x02] = chr((xread8($v - 0x67) & 0x0b) | ($s24 & 1 ? 0 : 4) | ($s24 & 4 ? 0 : 0x10)); $fl = xread8(V6_FARSLEEP($v)); $c[$d - 0x01] = chr((get_0x04_cstart_csleep($v) == 0 ? 1 : 0) | ($fl & (2 | AlisV6Const::V6_DORMANT)) | ($s24 & 0x80)); return $c; } // palette, 6-bit components // PORT: the port's palettes always hold packed 8-bit 0xRRGGBB entries (image.php), so the C EPal565 // branch (5/6-bit components) never applies: always the 8-bit branch. $pal = Img property name, $off = element offset. function sg_pal(string $pal, int $off): string { $out = ''; for ($i = 0; $i < 256; $i++) { $c = Img::${$pal}[$off + $i] ?? 0; $r = ($c >> 16) & 0xff; $g = ($c >> 8) & 0xff; $b = $c & 0xff; $out .= chr($r >> 2) . chr($g >> 2) . chr($b >> 2); } return $out; } function sg_save(int $flags): void { V::$sg_hdr = 'ALIS' . sg_le32s(8) . sg_le32s($flags) . sg_le32s(0x9e30); sg_write(V::$sg_hdr); if ($flags & 0x40) { sg_chunk('SYST', 0x284, 8); sg_write(sg_le32s(Alis::$basemain)); for ($i = 0; $i < 16; $i++) { $f = str_repeat("\0", 0x28); $x = Alis::$v6xfonts[$i]; if ($x['valid'] ?? 0) { sg_le32($f, 0x00, $x['id']); $f[0x04] = chr($x['op2'] & 0xff); sg_le32($f, 0x08, $x['foasc']); sg_le32($f, 0x0c, $x['form'] < 0 ? $x['form'] : $x['glyphslot']); sg_le32($f, 0x10, $x['count']); sg_le32($f, 0x14, $x['height']); sg_le32($f, 0x18, $x['spacing']); sg_le16($f, 0x1c, $x['owner']); sg_le16($f, 0x1e, $x['form']); $f = substr_replace($f, str_pad(substr($x['ownername'], 0, 8), 8, "\0"), 0x20, 8); } sg_write($f); } } if ($flags & 0x01) { sg_chunk('PROG', 0, 8); $list = []; $n = 0; for ($b = xread16(Alis::$atent + C::NODE_BLKHEAD + 0xe) & 0xffff; $b != C::NODE_BLKTAIL && $n < 256; $b = xread16(Alis::$atent + $b + 0xe) & 0xffff) { $list[$n++] = $b; } for ($i = $n - 2; $i >= 0; $i--) { $b = $list[$i]; $t = xread8(Alis::$atent + $b + 0x15); if ($t != 2 && $t != 0xc && $t != 0xd) { continue; } if (mem_read(Alis::$atent + $b + 6, 8) === EscnamesV6Const::MAIN8) { continue; } V::$sg_hdr = mem_read(Alis::$atent + $b + 6, 8) . sg_le32s(0) . sg_le32s(3 | ($t << 8)); sg_write(V::$sg_hdr); } sg_le32(V::$sg_hdr, 0, 0); sg_write(V::$sg_hdr); } if ($flags & 0x02) { sg_chunk('ENTI', 0, 8); $e = xread16(Alis::$atent + 4) & 0xffff; if (!($flags & 0x10) && $e) { $e = xread16(Alis::$atent + $e + 4) & 0xffff; } for (; $e != 0; $e = xread16(Alis::$atent + $e + 4) & 0xffff) { $s = ENTSCR($e); $v = $s->vram_org; $buf = sg_ctx($s); $vsz = ord($buf[0x40 - 0x36]) | (ord($buf[0x40 - 0x35]) << 8); $buf .= mem_read($v, $vsz); // elem links into head/tail nodes for ($g = v6el_next(v6_elemhead($v)); $g != v6_elemtail($v) && ule($v, $g) && ult($g, s32($v + $vsz)); $g = v6el_next($g)) { sg_le32($buf, 0x40 + ($g - $v), sg_link($v, v6el_prev($g))); sg_le32($buf, 0x40 + ($g - $v) + 4, sg_link($v, v6el_next($g))); } V::$sg_hdr = mem_read($v + 0x3c, 8) . sg_le32s($vsz + 0x40) . sg_le32s(0); sg_write(V::$sg_hdr); sg_write(substr($buf, 0, $vsz + 0x40)); } sg_le32(V::$sg_hdr, 0, 0); sg_write(V::$sg_hdr); } if ($flags & 0x04) { sg_chunk('PALE', 0, 8); sg_write(sg_pal('mpalet', Img::$ampalet)); sg_write(sg_pal('tpalet', Img::$atpalet)); sg_write(sg_pal('svpalet', 0)); sg_write(sg_pal('svpalet2', 0)); $dk = ''; for ($i = 0; $i < 0x300; $i++) { $dk .= sg_le16s(Img::$dkpalet[$i] ?? 0); } sg_write($dk); } $scr = s32(Alis::$basemain + (xread16(Alis::$script->vram_org - 0x64) & 0xffff)); if (($flags & 0x08) && xread32($scr + 0xb4) != 0) { $h = xread32($scr + 0xb4); if (scrnode_get($scr, 0xb4) == 0) { // fwriteg: screen +0xb4 is not a shading block, SHAD skipped } else { $data = xread32($h); $raw = s32(xread32($data - 8) - 8); $n = s32(($raw | 0xf) + 1); V::$sg_hdr = substr_replace(V::$sg_hdr, 'SHAD' . sg_le32s($n + 4) . sg_le32s($scr - Alis::$basemain), 0, 12); sg_write(substr(V::$sg_hdr, 0, 12)); sg_write(mem_read($data, $raw)); sg_write(str_repeat("\0", $n - $raw)); V::$sg_hdr = substr_replace(V::$sg_hdr, 'SHAV' . sg_le32s(0x2c) . sg_le32s($scr - Alis::$basemain), 0, 12); sg_write(substr(V::$sg_hdr, 0, 12)); sg_write(mem_read($scr + 0x94, 0x28)); } } if ($flags & 0x20) { sg_chunk('VIDE', 8, 8); sg_write(sg_le32s(Alis::$videomode) . sg_le16s(Alis::$platform->width) . sg_le16s(Alis::$platform->height)); } } // Codesc1name no. 039 opcode 0x26 cfwriteg function cfwriteg(): void { readexec_opername(); if (Alis::$platform->version >= 60 && Alis::$platform->kind != C::EPlatform3DO && Alis::$fp) { sg_save(Alis::$varD7); } } // --------------------------------------------------------------------------- // v6 load game. Reads DOS and our saves; unlike DOS it validates the chunks first, // skips entities whose program is missing and only rebases pointers that point // into the old program // PORT: the file image (C u8 *f) is a PHP string, C pointers into it are int offsets. // sSgFile is an array ['f' => string, 'len', 'syst', 'prog', 'enti', 'pale', 'shad', 'shav', 'vide']. // --------------------------------------------------------------------------- function sg_rd32(string $f, int $p): int { return s32(ord($f[$p]) | (ord($f[$p + 1]) << 8) | (ord($f[$p + 2]) << 16) | (ord($f[$p + 3]) << 24)); } function sg_rd16(string $f, int $p): int { return ord($f[$p]) | (ord($f[$p + 1]) << 8); } /** C string of at most 8 bytes at $p (char name[9] = {0}; memcpy(name, p, 8)) */ function sg_name8(string $raw): string { $nul = strpos($raw, "\0"); return $nul === false ? $raw : substr($raw, 0, $nul); } function sg_tag(array $g, int $at, string $tag): bool { return $at + 8 <= $g['len'] && substr($g['f'], $at, 4) === $tag; } // chunk walk in DOS read order; 0 if the file is broken function sg_parse(array &$g, int $flags): int { if ($g['len'] < 16 || substr($g['f'], 0, 4) !== 'ALIS') { return 0; } $at = 16; if ($flags & 0x40) { if (!sg_tag($g, $at, 'SYST') || $at + 8 + 0x284 > $g['len']) { return 0; } $g['syst'] = $at + 8; $at += 8 + 0x284; } if (sg_tag($g, $at, 'PROG')) { $g['prog'] = $at += 8; for (;; $at += 16) { if ($at + 16 > $g['len']) { return 0; } if (sg_rd32($g['f'], $at) == 0) { break; } } $at += 16; } if (sg_tag($g, $at, 'ENTI')) { $g['enti'] = $at += 8; for (;;) { if ($at + 16 > $g['len']) { return 0; } if (sg_rd32($g['f'], $at) == 0) { break; } $sz = sg_rd32($g['f'], $at + 8); // sz < 0x40 || at + 16 + sz > len (u32; at + 16 <= len here) if (ult($sz, 0x40) || ult($g['len'] - $at - 16, $sz)) { return 0; } $at += 16 + $sz; } $at += 16; } if (sg_tag($g, $at, 'PALE')) { if ($at + 8 + 0x1200 > $g['len']) { return 0; } $g['pale'] = $at + 8; $at += 8 + 0x1200; } if (($flags & 0x08) && sg_tag($g, $at, 'SHAD')) { // DOS reads it blindly (Q2) $n = sg_rd32($g['f'], $at + 4); if (ult($n, 4) || ult($g['len'] - $at - 8, $n) || !sg_tag($g, $at + 8 + $n, 'SHAV') || $at + 8 + $n + 12 + 0x28 > $g['len']) { return 0; } $g['shad'] = $at + 8; $at += 8 + $n; $g['shav'] = $at + 8; $at += 12 + 0x28; } if (($flags & 0x20) && sg_tag($g, $at, 'VIDE') && $at + 16 <= $g['len']) { $g['vide'] = $at + 8; } return 1; } function sg_reset(): void { $main = xread16(Alis::$basemain + 0x3a) & 0xffff; Alis::$ferase = 0; while (($e = xread16(Alis::$atent + $main + 4) & 0xffff) != 0) { killent($e); } for ($b = xread16(Alis::$atent + C::NODE_BLKHEAD + 0xe) & 0xffff; $b != C::NODE_BLKTAIL; ) { $next = xread16(Alis::$atent + $b + 0xe) & 0xffff; $prog = node_prog($b); if ($prog !== null && $prog !== Alis::$main->data) { $end = Alis::$finprog; for ($i = 0; $i < Alis::$nbprog; $i++) { if (ult($prog->data_org, ATPROG($i)) && ult(ATPROG($i), $end)) { $end = ATPROG($i); } } shrinkprog($prog->data_org, s32($end - $prog->data_org), $prog->header->id); } v6text_free($b); $b = $next; } for ($i = 0; $i < 16; $i++) { $sn = Alis::$scrnode[$i]; if (($sn['node'] ?? 0) && scrnode_get(s32(Alis::$basemain + ($sn['scr'] ?? 0)), $sn['field'] ?? 0)) { io_mfree($sn['handle'] ?? 0); } Alis::$scrnode[$i] = ['scr' => 0, 'field' => 0, 'node' => 0, 'handle' => 0]; } Alis::$filmnode = 0; $mprog = Alis::$main->data->node; for ($i = intdiv($main, 0x18) + 1; $i < Alis::$maxent; $i++) { if ($i * 0x18 != $mprog) { mem_set(Alis::$atent + $i * 0x18, 0, 0x18); } } xwrite16(Alis::$atent + 4, $main); xwrite16(Alis::$atent + $main + 4, 0); xwrite16(Alis::$atent + C::NODE_BLKHEAD + 0xe, $mprog); xwrite16(Alis::$atent + $mprog + 0xe, C::NODE_BLKTAIL); $first = ($main + 0x18) & 0xffff; $last = ((Alis::$maxent - 2) * 0x18) & 0xffff; // node n-1 orphaned for ($n = $first; $n < $last; $n = ($n + 0x18) & 0xffff) { xwrite16(Alis::$atent + $n + 0x10, $n + 0x18); } xwrite16(Alis::$atent + $last + 0x10, $last); Alis::$nodefree = $first; Alis::$nodetail = $last; } function sg_entity(int $X, string $f, int $r, int $fbase, int $mainold, int $dmain, int $ismain): void { $c = $r + 16; $d = $c + 0x40; $sz = s32(sg_rd32($f, $r + 8) - 0x40); $node = xread16($X + 0x3a) & 0xffff; $vsz = (xread32(Alis::$atent + $node + 8) - xread16(Alis::$atent + $node + 6)) & 0xffff; $s = ENTSCR($node); $org = get_0x14_script_org_offset($X); $oldprog = sg_rd32($f, $d - 0x0c); $dP = s32($org - $oldprog); $psz = s32(Alis::$finprog - $org); for ($i = 0; $i < Alis::$nbprog; $i++) { if (ult($org, ATPROG($i)) && ult(s32(ATPROG($i) - $org), $psz)) { $psz = s32(ATPROG($i) - $org); } } mem_write($X, substr($f, $d, ult($sz, $vsz) ? $sz : $vsz)); xwrite8($X - 0x66, ord($f[$d - 0x1c])); xwrite16($X - 0x64, sg_rd16($f, $d - 0x18)); set_0x16_screen_id($X, sg_rd16($f, $d - 0x16)); set_0x3a_wait_cycles($X, sg_rd16($f, $d - 0x12)); set_0x3e_wait_time($X, 0); set_0x2b_cordspr($X, ord($f[$d - 0x03])); $f2 = ord($f[$d - 0x02]); xwrite8($X - 0x67, ($f2 & 0x0b) | 4); $s24 = get_0x24_scan_inter($X) & 0xff & ~(0x80 | 1 | 4); set_0x24_scan_inter($X, s8($s24 | ($f2 & 4 ? 0 : 1) | ($f2 & 0x10 ? 0 : 4))); xwrite8(V6_FARSLEEP($X), 0); // -1 = 0 if (get_0x04_cstart_csleep($X) == 0) { set_0x04_cstart_csleep($X, 1); } if ($s !== null) { $s->vram_org = $X; } // the entity takes back its saved id $want = xread16($X + 0x3a) & 0xffff; if ($want != $node) { if ($want % 0x18 == 0 && $want > C::NODE_BLKTAIL && intdiv($want, 0x18) < Alis::$maxent) { node_swap($node, $want); // any node, as DOS } else { xwrite16($X + 0x3a, $node); } } $node = xread16($X + 0x3a) & 0xffff; set_0x0e_script_ent($X, $node); xwrite16(V6_CLINKING($X), $node); if ($ismain) { // reset the world render lists for ($W = v6_worldhead(); $W; ) { $T = $W + 0x58; xwrite32($W + 0x38, $T); xwrite32($W + 0x34, 0); xwrite8($W + 0x22, 0); xwrite32($T + 0x34, $W); xwrite32($T + 0x38, $T); xwrite8($T + 0x22, 0); $nx = xread32($W + 0xb4); if (!$nx) { break; } xwrite32($W + 0xb4, s32($nx - $fbase + Alis::$basemain)); // DOS keeps the old base here $W = s32($nx - $fbase + Alis::$basemain); } } // elem chain and slots: vram delta dV, program delta dP $vold = s32(sg_rd32($f, $d - 0x2c) + 0x2c); $dV = s32($X - $vold); $head = v6_elemhead($X); $tail = v6_elemtail($X); $SG_LINK = fn (int $p): int => $p == $vold - 0x2c ? $head : ($p == $vold - 0x24 ? $tail : s32($p + $dV)); v6el_set_prev($head, $head); v6el_set_next($head, $SG_LINK(sg_rd32($f, $d - 0x28))); v6el_set_prev($tail, $SG_LINK(sg_rd32($f, $d - 0x24))); v6el_set_next($tail, $tail); for ($E = v6el_next($head), $guard = 0; $E != $tail && $guard < 0x1000; $E = v6el_next($E), $guard++) { if (ult($E, $X) || ult(s32($X + $vsz), s32($E + 0x50))) { // freadg: broken elem chain v6el_set_next($head, $tail); v6el_set_prev($tail, $head); break; } v6el_set_prev($E, $SG_LINK(v6el_prev($E))); v6el_set_next($E, $SG_LINK(v6el_next($E))); $sp = $E; for ($i = xread8($E + 0x33); $i != 0; $i--, $sp += C::V6_SLOT_STRIDE) { $st = v6sp_state($sp); for ($fi = 0; $fi < 2; $fi++) { $off = $fi ? 0x50 : 0x3c; if ($fi ? ($st != 0x30 && $st != 0x3a) : $st == 0x38) { continue; } $p = xread32($sp + $off); if (ule($oldprog, $p) && ult(s32($p - $oldprog), $psz)) { xwrite32($sp + $off, s32($p + $dP)); } elseif (!$ismain && $mainold && ule($mainold, $p) && ult(s32($p - $mainold), Alis::$main->data->sz)) { xwrite32($sp + $off, s32($p + $dmain)); } } if (xread32($sp + 0x08)) { xwrite32($sp + 0x08, s32(xread32($sp + 0x08) + $dV)); } if (xread32($sp + 0x34) && xread16($sp + 0x2c)) { $W = s32(Alis::$basemain + (xread16($sp + 0x2c) & 0xffff)); $n = xread32($W + 0x38); xwrite32($W + 0x38, $sp); xwrite32($n + 0x34, $sp); xwrite32($sp + 0x34, $W); xwrite32($sp + 0x38, $n); xwrite8($sp + 0x23, xread8($sp + 0x23) | 1); } } } } // PORT: PAL_WRITE_RGB into palette $pal (Img property name) at element offset $off. function sg_pal_in(string $pal, int $off, string $f, int $in): void { for ($i = 0; $i < 256; $i++) { $r = ord($f[$in + $i * 3]) & 0x3f; $g = ord($f[$in + $i * 3 + 1]) & 0x3f; $b = ord($f[$in + $i * 3 + 2]) & 0x3f; image_pal_write($pal, $off + $i, ($r << 2 | $r >> 4) & 0xff, ($g << 2 | $g >> 4) & 0xff, ($b << 2 | $b >> 4) & 0xff); } } function sg_load(int $flags): void { fseek(Alis::$fp, 0, SEEK_END); $len = ftell(Alis::$fp); rewind(Alis::$fp); $f = $len > 0 ? fread(Alis::$fp, $len) : false; if ($f === false || strlen($f) != $len) { return; } $g = ['f' => $f, 'len' => $len, 'syst' => 0, 'prog' => 0, 'enti' => 0, 'pale' => 0, 'shad' => 0, 'shav' => 0, 'vide' => 0]; if (Alis::$script !== Alis::$main || !sg_parse($g, $flags)) { // freadg: not a valid save game return; } $scr0 = s32(Alis::$basemain + (xread16(Alis::$script->vram_org - 0x64) & 0xffff)); v6snd_stopchan(-1); sg_reset(); xwrite32($scr0 + 0xb4, 0); xwrite32($scr0 + 0xbc, 0); $fbase = Alis::$basemain; if ($g['syst']) { $fbase = sg_rd32($f, $g['syst']); for ($i = 0; $i < 16; $i++) { $r = $g['syst'] + 4 + $i * 0x28; $x = Alis::$v6xfonts[$i]; $x['id'] = sg_rd32($f, $r); $x['op2'] = ord($f[$r + 4]); $x['foasc'] = sg_rd32($f, $r + 8); $x['glyphslot'] = 0; $x['count'] = sg_rd32($f, $r + 0x10); $x['height'] = sg_rd32($f, $r + 0x14); $x['spacing'] = sg_rd32($f, $r + 0x18); $x['owner'] = sg_rd16($f, $r + 0x1c); $x['form'] = s16(sg_rd16($f, $r + 0x1e)); $x['ownername'] = substr($f, $r + 0x20, 8); $x['valid'] = (int) (ord($f[$r + 0x20]) != 0); Alis::$v6xfonts[$i] = $x; $fo = Alis::$v6fonts[$i]; $fo['foasc'] = s16($x['foasc']); $fo['fonum'] = s16($x['form']); $fo['fomax'] = s16($x['count']); $fo['folarg'] = $x['spacing'] & 0xff; $fo['fohaut'] = $x['height'] & 0xff; $fo['mode'] = $x['id'] & 0xff; $fo['usemain'] = (int) (substr($f, $r + 0x20, 8) === EscnamesV6Const::MAIN8); $fo['valid'] = $x['valid']; Alis::$v6fonts[$i] = $fo; } } for ($at = $g['prog']; $at && sg_rd32($f, $at) != 0; $at += 16) { $name = sg_name8(substr($f, $at, 8)); $t = ord($f[$at + 0xd]); if (node_find($name) != 0) { continue; } if ($t == 0xc) { v6text_load($name); } elseif ($t == 2) { $path = Alis::$platform->path . strlower($name . '.' . Alis::$platform->ext); script_load($path); } else { // freadg: block of that type not reloaded } } $prev = Alis::$basemain; $mainold = 0; $dmain = 0; for ($at = $g['enti']; $at && sg_rd32($f, $at) != 0; $at += 16 + sg_rd32($f, $at + 8)) { $r = $at; $name = sg_name8(substr($f, $r, 8)); $ismain = (int) (substr($f, $r, 8) === EscnamesV6Const::MAIN8); $X = Alis::$basemain; if ($ismain) { $mainold = sg_rd32($f, $r + 16 + 0x40 - 0x0c); $dmain = s32(Alis::$main->data->data_org - $mainold); } else { $prog = node_prog(node_find($name)); if ($prog === null) { // freadg: entity of missing program skipped continue; } $pid = xread16($prev + 0x3a) & 0xffff; $caller = Alis::$script; $d5 = s16(Alis::$varD5); ALIS_SET_SCRIPT(ENTSCR($pid)); Alis::$varD5 = $pid; $e = script_live($prog); ALIS_SET_SCRIPT($caller); Alis::$varD5 = $d5 & 0xffff; if ($e === null) { continue; } $X = $e->vram_org; } sg_entity($X, $f, $r, $fbase, $mainold, $dmain, $ismain); $prev = $X; } if ($g['pale']) { sg_pal_in('mpalet', Img::$ampalet, $f, $g['pale']); sg_pal_in('tpalet', Img::$atpalet, $f, $g['pale'] + 0x300); sg_pal_in('svpalet', 0, $f, $g['pale'] + 0x600); sg_pal_in('svpalet2', 0, $f, $g['pale'] + 0x900); for ($i = 0; $i < 0x300; $i++) { Img::$dkpalet[$i] = s16(sg_rd16($f, $g['pale'] + 0xc00 + $i * 2)); } } if ($g['shad']) { $D = s32(Alis::$basemain + sg_rd32($f, $g['shad'])); for ($o = Scr::$ptscreen & 0xffff; $o != 0; $o = xread16(Alis::$basemain + $o + 0x5c) & 0xffff) { $scr = Alis::$basemain + $o; if (xread32($scr + 0x5e)) { xwrite32($scr + 0x5e, s32(xread32($scr + 0x5e) - $fbase + Alis::$basemain)); } if (xread8($scr + 0x5a) == 6) { xwrite32($scr + 0xb4, 0); xwrite32($scr + 0xbc, 0); if (!(xread8($scr + 0x58) & 0x40)) { v6x3d_open($scr); } } } $n = s32(sg_rd32($f, $g['shad'] - 4) - 4); $node = node_take(0xb); $h = io_malloc($n); if ($h) { mem_write(xread32($h), substr($f, $g['shad'] + 4, $n)); xwrite32($D + 0xb4, $h); if ($node) { scrnode_set($D, 0xb4, $node); } } $keep = xread32($scr0 + 0xb4); mem_write($scr0 + 0x94, substr($f, $g['shav'] + 4, 0x28)); xwrite32($scr0 + 0xb4, $keep); } if ($g['vide']) { $mode = ord($f[$g['vide']]); if ($mode != Alis::$videomode) { Alis::$videomode = (int) ($mode == 1); resize_screen($mode == 1 ? 640 : 320, $mode == 1 ? 480 : 200); } } for ($i = 0; $i < 16; $i++) { if (!(Alis::$v6xfonts[$i]['valid'] ?? 0)) { continue; } $name = sg_name8(substr(Alis::$v6xfonts[$i]['ownername'], 0, 8)); $prog = node_prog(node_find($name)); $form = s16(Alis::$v6xfonts[$i]['form']); if ($prog !== null && $form >= 0 && $form < (xread16($prog->data_org + 0x3e) & 0xffff)) { Alis::$v6xfonts[$i]['glyphslot'] = s32($prog->data_org + 0x40 + $form * 4); } } Img::$ftopal = 0xff; } // Codesc1name no. 040 opcode 0x27 cfreadg function cfreadg(): void { readexec_opername(); if (Alis::$platform->version >= 60 && Alis::$platform->kind != C::EPlatform3DO && Alis::$fp) { sg_load(Alis::$varD7); } } // Codesc1name no. 041..082: not implemented in the C either (ALIS_DEBUG "MISSING" only). // Codesc1name no. 041 opcode 0x28 cobjmat function cobjmat(): void { } // Codesc1name no. 042 opcode 0x29 cobjmask function cobjmask(): void { } // Codesc1name no. 043 opcode 0x2a cscclut function cscclut(): void { } // Codesc1name no. 044 opcode 0x2b cobjclut function cobjclut(): void { } // Codesc1name no. 045 opcode 0x2c copennet function copennet(): void { } // Codesc1name no. 046 opcode 0x2d cclosenet function cclosenet(): void { } // Codesc1name no. 047 opcode 0x2e csendnet function csendnet(): void { } // Codesc1name no. 048 opcode 0x2f cmailnet function cmailnet(): void { } // Codesc1name no. 049 opcode 0x30 cplayernet function cplayernet(): void { } // Codesc1name no. 050 opcode 0x31 cgamenet function cgamenet(): void { } // Codesc1name no. 051 opcode 0x32 csyncnet function csyncnet(): void { } // Codesc1name no. 052 opcode 0x33 ctexitem function ctexitem(): void { } // Codesc1name no. 053 opcode 0x34 cgetanim function cgetanim(): void { } // Codesc1name no. 054 opcode 0x35 canimsize function canimsize(): void { } // Codesc1name no. 055 opcode 0x36 cscmath function cscmath(): void { } // Codesc1name no. 056 opcode 0x37 cintermode function cintermode(): void { } // Codesc1name no. 057 opcode 0x38 csysnet function csysnet(): void { } // Codesc1name no. 058 opcode 0x39 cobjshad function cobjshad(): void { } // Codesc1name no. 059 opcode 0x3a csphere function csphere(): void { } // Codesc1name no. 060 opcode 0x3b cscdark (escnames.c cscdark, renamed: opcodes.c has one too) function esc_cscdark(): void { } // Codesc1name no. 061 opcode 0x3c cagetlocal function cagetlocal(): void { } // Codesc1name no. 062 opcode 0x3d cfollow function cfollow(): void { } // Codesc1name no. 063 opcode 0x3e cplayfilm function cplayfilm(): void { } // Codesc1name no. 064 opcode 0x3f csoundtime function csoundtime(): void { } // Codesc1name no. 065 opcode 0x40 cinfoitem function cinfoitem(): void { } // Codesc1name no. 066 opcode 0x41 cmorph function cmorph(): void { } // Codesc1name no. 067 opcode 0x42 cscmicro function cscmicro(): void { } // Codesc1name no. 068 opcode 0x43 cptrset function cptrset(): void { } // Codesc1name no. 069 opcode 0x44 cdefproc function cdefproc(): void { } // Codesc1name no. 070 opcode 0x45 cfreadm function cfreadm(): void { } // Codesc1name no. 071 opcode 0x46 cfwritem function cfwritem(): void { } // Codesc1name no. 072 opcode 0x47 cmaskent function cmaskent(): void { } // Codesc1name no. 073 opcode 0x48 cvram function cvram(): void { } // Codesc1name no. 074 opcode 0x49 cadresnet function cadresnet(): void { } // Codesc1name no. 075 opcode 0x4a cfdir function cfdir(): void { } // Codesc1name no. 076 opcode 0x4b cnul // Codesc1name no. 077 opcode 0x4c cloadas function cloadas(): void { } // Codesc1name no. 078 opcode 0x4d cfindobjitem function cfindobjitem(): void { } // Codesc1name no. 079 opcode 0x4e cobjid function cobjid(): void { } // Codesc1name no. 080 opcode 0x4f cgetobjid function cgetobjid(): void { } // Codesc1name no. 081 opcode 0x50 cprelight function cprelight(): void { } // Codesc1name no. 082 opcode 0x51 croomdoor function croomdoor(): void { }