RU
games/alis / src / Alis / Hd3d.php · commit 9c722cf
<?phpnamespace App\Alis;/** * The HD edition's 3D (not in the C, fn/hd3d.php): the land of Robinson's Requiem drawn a second time at twice the * resolution by the original renderer. Not reset with the VM's state (Machine::resetState()). */final class Hd3d{    /** the HD edition asks for the 3D at twice the resolution */    public static bool $on = false;    /** 1 = the game's own pass, 2 = the HD pass is running (bigger buffer, tables, projection) */    public static int $scale = 1;    /** the HD pass's buffer and tables (host blocks past the VM RAM), for the frame size they were made for */    public static int $buffer = 0;    public static int $handle = 0;    public static int $tables = 0;    public static string $size = '';    /** what the HD tables were made from (the game's altitude table, the scale, the block) */    public static string $tablesFor = '';    /** @var array<string, mixed>|null the last HD picture of the land, for the host (sys_render) */    public static ?array $land = null;    /** the HD pass is running (its sky: hd3d_sky(), the viewport's corner at 0, 0 of its buffer) */    public static bool $inPass = false;    /** the last hd3d_shown(): why the HD land is not shown (ok, none, outside, nokey, differ) and how many pixels differ */    public static string $why = '';    public static int $differ = 0;    /** the HD pass's scale (2; 1 = debugging: the pass must give back the game's own picture) */    public static int $passScale = 2;    /** debugging: check that the HD pass leaves the VM's state as it was (slow) */    public static bool $debug = false;    /** the viewport's left edge on the game's screen, during the HD pass (the sky's scroll) */    public static int $lowX = 0;    /** the VM memory where the last picture's frame starts (it must still be on the screen to be used) */    public static int $frame = 0;    /**     * The HD pass's share of the game's time (a slow computer, the browser): above it, over the last 2 seconds, that     * size of view gets the game's own land while it moves (smooth rather than slow) and the HD land once it stays     * still (Hd3d::STILL). 0 = never.     */    public static float $budget = 0.75;    /**     * @var array<int, true> the sprites (spl_object_id) the game's own land drew: the HD pass draws only those (a far     * sprite too small to show at the game's size would appear at twice it, e.g. a walker in the sky)     */    public static array $drawn = [];    public static bool $recording = false;    /** debugging (tools/run.php --hd3d-all-sprites): the HD pass draws every sprite, as before */    public static bool $allSprites = false;    /** sprites the HD pass left out (tools/run.php prints it) */    public static int $skipped = 0;    /** the time spent in HD passes since the start, and how many (tools/run.php prints them) */    public static float $passSeconds = 0.0;    public static int $passes = 0;    /** @var array<string, true> the sizes of view (game pixels, "w x h") given up */    public static array $slow = [];    /** @var list<array{0: float, 1: float}> the last passes: [when, seconds] */    private static array $spent = [];    /** a land drawn on a computer too slow for its size of view, waiting for the view to stay still (hd3d_pass()) */    public static ?array $pending = null;    public static float $pendingAt = 0.0;    /** how long the land must stay the same before a pending HD pass is made (seconds of the game's time) */    public const STILL = 0.25;    /** affiland() / landtofi(): take what the HD pass needs (it may run now, later, or not: hd3d_pass())? */    public static function wanted(int $scene): bool    {        return self::$on && self::$scale === 1;    }    public static function isSlow(int $scene): bool    {        return isset(self::$slow[self::viewSize($scene)]);    }    public static function viewSize(int $scene): string    {        return ((xread16($scene + 0x12) + 1) & 0xffff) . ' x ' . ((xread16($scene + 0x14) + 1) & 0xffff);    }    /** A pass took $seconds: too much of the real time, and that size of view is given up. */    public static function spent(int $scene, float $seconds): void    {        self::$passSeconds += $seconds;        self::$passes++;        if (self::$budget <= 0) {            return;        }        $now = SysVars::$msec / 1000;     // the game's time (a replay runs faster than real time)        if (self::$spent !== [] && $now < self::$spent[count(self::$spent) - 1][0]) {            self::$spent = [];            // a new game: its clock starts again        }        self::$spent[] = [$now, $seconds];        while (self::$spent !== [] && self::$spent[0][0] < $now - 2.0) {            array_shift(self::$spent);        }        $sum = 0.0;        foreach (self::$spent as [, $s]) {            $sum += $s;        }        if (count(self::$spent) >= 4 && $sum / 2.0 > self::$budget) {            $size = self::viewSize($scene);            self::$slow[$size] = true;            self::$spent = [];            self::$land = null;            error_log(sprintf('ALIS HD 3D: the HD land takes %d%% of the time for a %s view: made only when the view stays still', (int) round($sum * 50), $size));        }    }    /** bytes per row of the land buffer (the DOS renderer: 320) */    public static function stride(): int    {        return 0x140 * self::$scale;    }    /** rows of the row table (200 lines of the screen) */    public static function rows(): int    {        return 200 * self::$scale;    }    /** the slope table: [distance][width] for distances and widths up to 64 pixels */    public static function aliasWidth(): int    {        return 0x40 * self::$scale;    }}