JA
games/alis / src / Alis / fn / platform.php · commit 9c722cf
<?phpnamespace App\Alis;// Port of alis/src/platform.c (MIT, Olivier Huguenot, Vadim Kindl).// TODO: Manhattan Dealers (Operation: Cleanstreets in the US), the first game on the ALIS engine, is different.// Atari ST: it uses 'OO' file extension and the main script is in 'man.sng' (not 'OO'!),// Amiga:    it uses 'OO' file extension and the main script is in 'prog.oo',// PC:       it uses 'IO' file extension and the main script is in 'prog.io'./** C: static sPlatform platforms[] — built from PlatformVars::platform_rows on first use. */function pl_platforms(): array{    if (!PlatformVars::$platforms) {        foreach (PlatformVars::platform_rows as $r) {            $pl = new Platform();            [$pl->kind, $pl->version, $pl->desc, $pl->ext, $pl->ram_sz, $pl->video_ram_sz,                $pl->width, $pl->height, $pl->bpp, $pl->dbl_buf, $pl->is_little_endian] = $r;            $pl->uid = C::EGameUnknown;            $pl->px_format = C::EPxFormatChunky;            $pl->path = '';            PlatformVars::$platforms[] = $pl;        }    }    return PlatformVars::$platforms;}function pl_supported(Platform $platform): int{    return (int)($platform->kind != C::EPlatformUnknown);}// alis.mem heap size. 1 MB floor (io_model's minimum Atari value 1010 = 1 MB); 2 MB for 8bpp,// ALIS_CONV_INPLACE builds and non-textured RRQ; 4 MB for textured RRQ (DOS, Falcon CD).function pl_compute_ram_size(Platform $pl): int{    if ($pl->uid == C::EGameRobinsonsRequiem0 || $pl->uid == C::EGameRobinsonsRequiem1 || $pl->uid == C::EGameRobinsonsRequiem3DO) {        if ($pl->kind == C::EPlatformPC || $pl->kind == C::EPlatformFalcon || $pl->kind == C::EPlatform3DO) {            return 4 * 1024 * 1024;        }        return 2 * 1024 * 1024;    }    if ($pl->bpp >= 8) {        return 2 * 1024 * 1024;    }    return 2 * 1024 * 1024;}// RAM of the machine the original ST/Amiga release targeted (0 = not listed).function pl_original_ram(Platform $pl): int{    if ($pl->kind != C::EPlatformAtari && $pl->kind != C::EPlatformAmiga) {        return 0;    }    switch ($pl->uid) {        case C::EGameManhattanDealers0: case C::EGameManhattanDealers1: case C::EGameMadShow:        case C::EGameTarghan0: case C::EGameTarghan1: case C::EGameWindsurfWilly: case C::EGameLeFeticheMaya:        case C::EGameColorado: case C::EGameStarblade: case C::EGameCrystalsOfArborea0:        case C::EGameCrystalsOfArborea1: case PlatformConst::EGameMetalMutant: case C::EGameBostonBombClub:        case C::EGameStormMaster: case C::EGameBunnyBricks: case C::EGameIshar_1:            return 512 * 1024;        case C::EGameIshar_2: case C::EGameTransarctica: case C::EGameIshar_3:        case C::EGameRobinsonsRequiem0: case C::EGameRobinsonsRequiem1:            return 1024 * 1024;        default:            return 0;    }}// Arena bounds: *pref = the tier above; *floor = 2x the original machine's RAM (8-bit sprites),// or *pref when the original isn't listed.function pl_arena_range(Platform $pl, int &$floor, int &$pref): void{    $orig = pl_original_ram($pl);    $pref = pl_compute_ram_size($pl);    $floor = ($orig && 2 * $orig < $pref) ? 2 * $orig : $pref;}// omodel memory class (ST data) the original would report for an arena of `ram_sz`:// 1000 = 512K, 1010 = 1MB, 1020 = more. Assumes 8-bit sprites need twice the original RAM.function pl_model_memclass(int $ram_sz): int{    $orig = intdiv($ram_sz, 2);    return $orig < 1024 * 1024 ? 1000 : ($orig < 2 * 1024 * 1024 ? 1010 : 1020);}function pl_get(string $main_script): Platform{    $platforms = pl_platforms();    $pl = $platforms[C::EPlatformUnknown];    // Manhattan Dealers on Atari and Amiga use files with the same 'OO' extension,    // but the main script is in the 'main.sng' and 'main.oo' files respectively.    // We identify them by the main script name, not by used extension    if (!strncasecmp($main_script, PlatformConst::kManAtariScriptName, strlen(PlatformConst::kManAtariScriptName))) {        return clone $platforms[C::EPlatformOldAtari];    }    if (!strncasecmp($main_script, PlatformConst::kManAmigaScriptName, strlen(PlatformConst::kManAmigaScriptName))) {        return clone $platforms[C::EPlatformOldAmiga];    }    // get file extension    $dot = strrpos($main_script, '.');    $ext = $dot === false ? '' : substr($main_script, $dot + 1);    // check platform by script file extension    for ($i = 0; $i <= C::EPlatformUnknown; $i++) {        $pl = $platforms[$i];        if (!strncasecmp($ext, $pl->ext, strlen($ext))) {            break;        }    }    return clone $pl; // C returns the sPlatform by value: never hand out the table entry}// mode = 0 path can only be a folder, mode = 1 path can also be a file// PORT: the folder is listed with PHP's scandir() (sorted names; C readdir() order is unspecified).function pl_guess(?string $path, int $mode): Platform{    $platforms = pl_platforms();    $platform = clone $platforms[C::EPlatformUnknown];    if ($path === null) {        return $platform;    }    $pathlen = strlen($path);    if ($pathlen <= 0) {        return $platform;    }    $data_path = $path;    if ($path[$pathlen - 1] == '/' || $path[$pathlen - 1] == '\\') {        $data_path = substr($data_path, 0, $pathlen - 1);        $pathlen -= 1;    }    $dir = is_dir($data_path) ? scandir($data_path) : false;    if ($dir === false) {        if (is_file($data_path) && is_readable($data_path)) { // Maybe it is a file?            if ($mode) {                $platform = pl_get($data_path);                if ($platform->kind != C::EPlatformUnknown) {                    $platform->path = $data_path;                }            }        }        return $platform;    }    $data_path .= AlisConst::kPathSeparator;    // open path ok, loop thru files until we find the main script    // that matches one of the declared platforms    foreach ($dir as $d_name) {        if ($d_name === '.' || $d_name === '..') {            continue;        }        $main_path = $data_path . $d_name;        if (!file_exists($main_path)) {            continue;                            // an error        }        if (is_file($main_path)) {            // look for main script            if (!strncasecmp($d_name, PlatformConst::kMainScriptName, strlen(PlatformConst::kMainScriptName)) ||                !strncasecmp($d_name, PlatformConst::kManAtariScriptName, strlen(PlatformConst::kManAtariScriptName)) ||                !strncasecmp($d_name, PlatformConst::kManAmigaScriptName, strlen(PlatformConst::kManAmigaScriptName)) ||                !strncasecmp($d_name, PlatformConst::kManMSDOSScriptName, strlen(PlatformConst::kManMSDOSScriptName))) {                $main_path = $data_path . $d_name;                if (!is_readable($main_path)) {                    return $platform;                }                $platform = pl_get($d_name);                if ($platform->kind != C::EPlatformUnknown) {                    $platform->main = $main_path;                    $platform->path = $data_path;                    break;                }            }        }    }    // PORT: source edition (SysVars::$sourceDir): the main script is src/main.als, compiled into main.io    if ($platform->kind == C::EPlatformUnknown && SysVars::$sourceDir !== '' && sys_find_file(SysVars::$sourceDir . '/main.als') !== null) {        $platform = pl_get('main.io');        if ($platform->kind != C::EPlatformUnknown) {            $platform->main = $data_path . 'main.io';            $platform->path = $data_path;        }    }    return $platform;}