PL
games/rob-revisited / src / Revisited / Land / Solids.php · commit f9a1307
<?phpnamespace App\Revisited\Land;/** * What nothing walks through (docs/reborn.md "Collisions"): the big things of a zone as circles on the ground, and the bodies * of whoever moves (Rob, the animals, the people). The same rules for all: the cave walls / cliffs (Zone::solidAt) at four * points around the body, and the circles of the solid things (Zone::solidHit, a grid of 4 m squares built once per * Zone::$version). Movers slide along a circle (the step's direction made tangent) before turning aside. */final class Solids{    /**     * The shapes of the solid things: circles [x, z, radius] in the thing's own frame (Models: local z turned by the yaw),     * times the thing's scale. Sizes measured on the meshes at ground level (Render\Models / Props), a bit inside.     * Not solid: bushes, reeds, small stones, herbs, fires, springs, geysers, exits, ladders, bridges, signposts, bones...     */    public const SHAPES = [        'boulder' => [[0.0, 0.0, 1.75]],        'rock' => [[0.0, 0.0, 0.6]],        'basalt' => [[0.0, 0.0, 1.9]],        'tree_broad' => [[0.0, 0.0, 0.5]],        'tree_conifer' => [[0.0, 0.0, 0.38]],        'tree_palm' => [[0.0, 0.0, 0.3]],        'palm_coast' => [[0.0, 0.0, 0.3]],        'tree_dead' => [[0.0, 0.0, 0.36]],        'burnt_tree' => [[0.0, 0.0, 0.36]],        'giant_tree' => [[0.0, 0.0, 2.0]],        'mangrove' => [[0.0, 0.0, 0.9]],        'hut' => [[0.0, 0.0, 2.5]],        'tent' => [[0.0, 0.7, 1.0], [0.0, -0.7, 1.0]],        'stilt_hut' => [[0.0, 0.0, 2.1]],        'wreck_pod' => [[0.0, 0.0, 1.6]],        'wreck_colony' => [[0.0, -3.0, 2.3], [0.0, 0.0, 2.3], [0.0, 3.0, 2.3]],        'shuttle' => [[0.0, -12.0, 2.6], [0.0, -8.0, 2.9], [0.0, -4.0, 2.9], [0.0, 0.0, 2.9], [0.0, 4.0, 2.9], [0.0, 8.0, 2.7],            [0.0, 11.5, 2.0], [0.0, 13.6, 1.0]],        'boat_wreck' => [[0.0, -2.4, 1.3], [0.0, 0.0, 1.4], [0.0, 2.4, 1.2]],        'drone_wreck' => [[0.0, 0.0, 1.0]],        'crate' => [[0.0, 0.0, 0.5]],        'market_stall' => [[-0.8, 0.5, 0.8], [0.8, 0.5, 0.8]],        'totem' => [[0.0, 0.0, 0.4]],        'terminal' => [[0.0, 0.0, 0.5]],        'forge' => [[0.0, 0.0, 0.9]],        'weapon_rack' => [[0.0, 0.0, 0.5]],        'skin_rack' => [[0.0, 0.0, 0.6]],        'relay_mast' => [[0.0, 0.0, 1.4]],        // Rob's storage (Land\Chests): a chest 1.0 x 0.6 m, a basket 0.7 m round, a crate 1.2 x 0.75 m        'chest_wood' => [[-0.25, 0.0, 0.32], [0.25, 0.0, 0.32]],        'basket' => [[0.0, 0.0, 0.36]],        'chest_metal' => [[-0.3, 0.0, 0.4], [0.3, 0.0, 0.4]],    ];    /** the widest body (a glacier brute): circles are filed in every square they reach plus this */    public const MAX_BODY = 1.0;    /** the bodies' radii: Rob, a villager, the animals (walls take at most WALL of it: the cave passages stay open) */    public const ROB = 0.3;    public const PERSON = 0.35;    public const WALL = 0.4;    public const BODIES = ['grazer' => 0.6, 'stalker' => 0.5, 'crawler' => 0.35, 'burrower' => 0.45, 'glacier_brute' => 0.9,        'river_croc' => 0.8, 'climber' => 0.35, 'stalker_cub' => 0.35, 'lava_crab' => 0.35, 'fish' => 0.2, 'swarm' => 0.3, 'bird' => 0.2];    public static function body(string $species): float    {        return self::BODIES[$species] ?? 0.4;    }    /** Is this kind of thing solid? */    public static function solid(string $kind): bool    {        return isset(self::SHAPES[$kind]);    }    /** @return list<array{0: float, 1: float, 2: float}> the thing's circles on the ground (world x, z, radius), [] if not solid */    public static function circles(Thing $t): array    {        $shape = self::SHAPES[$t->kind] ?? null;        if ($shape === null || !empty($t->data['bundle'])) {     // a bundle Rob put down is a small heap, not a crate            return [];        }        $c = cos($t->yaw);        $s = sin($t->yaw);        $k = $t->scale;        $out = [];        foreach ($shape as [$lx, $lz, $r]) {            $out[] = [$t->x + ($lx * $c + $lz * $s) * $k, $t->z + ($lz * $c - $lx * $s) * $k, $r * $k];        }        return $out;    }    /**     * The distance from a point to the thing's surface (negative inside) and the nearest circle's centre.     * @return array{0: float, 1: float, 2: float}|null [distance, cx, cz], null: not solid     */    public static function surface(Thing $t, float $x, float $z): ?array    {        $best = null;        foreach (self::circles($t) as [$cx, $cz, $r]) {            $d = hypot($x - $cx, $z - $cz) - $r;            if ($best === null || $d < $best[0]) {                $best = [$d, $cx, $cz];            }        }        return $best;    }    /** Can a body of radius $r stand at ($x, $z): no wall around it, no solid thing under it? */    public static function free(Zone $zone, float $x, float $z, float $r): bool    {        $w = min($r, self::WALL);        if ($zone->solidAt($x, $z) || $zone->solidAt($x + $w, $z) || $zone->solidAt($x - $w, $z) || $zone->solidAt($x, $z + $w) || $zone->solidAt($x, $z - $w)) {            return false;        }        return $zone->solidHit($x, $z, $r) === null;    }    /**     * Moves a body standing inside a solid thing (spawned there, a thing built on it, an old save) out of it, toward the     * nearest free side; walls are never crossed. @return array{0: float, 1: float}     */    public static function pushOut(Zone $zone, float $x, float $z, float $r): array    {        for ($k = 0; $k < 6; $k++) {            $hit = $zone->solidHit($x, $z, $r);            if ($hit === null) {                return [$x, $z];            }            [$cx, $cz, $cr] = $hit;            $dx = $x - $cx;            $dz = $z - $cz;            $l = hypot($dx, $dz);            if ($l < 0.001) {                $dx = 1.0;                $dz = 0.0;                $l = 1.0;            }            $nx = $cx + $dx / $l * ($cr + $r + 0.02);            $nz = $cz + $dz / $l * ($cr + $r + 0.02);            if ($zone->solidAt($nx, $nz)) {                // the wall behind: the other way round the circle                $nx = $cx - $dx / $l * ($cr + $r + 0.02);                $nz = $cz - $dz / $l * ($cr + $r + 0.02);                if ($zone->solidAt($nx, $nz)) {                    return [$x, $z];                }            }            $x = $nx;            $z = $nz;        }        return [$x, $z];    }    /**     * A free spot near ($x, $z) for something put down (a bundle, a spawned animal): the point itself if free, else the     * nearest free one on rings around it. @return array{0: float, 1: float}     */    public static function freeSpot(Zone $zone, float $x, float $z, float $r): array    {        if (self::free($zone, $x, $z, $r)) {            return [$x, $z];        }        for ($ring = 0.5; $ring <= 6.0; $ring += 0.5) {            for ($a = 0; $a < 12; $a++) {                $px = $x + cos($a * 0.5236) * $ring;                $pz = $z + sin($a * 0.5236) * $ring;                if (self::free($zone, $px, $pz, $r)) {                    return [$px, $pz];                }            }        }        return [$x, $z];    }    /**     * One step of a body of radius $r from ($x, $z) toward ($dx, $dz) (unit) over $s metres: straight if free, else along     * the obstacle (the tangent of the circle hit), else turned aside by the angles $turns. $ok($nx, $nz) adds the mover's     * own rules (slopes, water, a cave's roof). Returns the new point and the direction taken, null: nowhere to go.     * @param list<float> $turns     * @return array{0: float, 1: float, 2: float, 3: float, 4: float}|null [x, z, mx, mz, turn]     */    public static function step(Zone $zone, float $x, float $z, float $dx, float $dz, float $s, float $r, array $turns, ?\Closure $ok = null): ?array    {        if ($zone->solidHit($x, $z, $r) !== null) {            [$x, $z] = self::pushOut($zone, $x, $z, $r);        }        foreach ($turns as $turn) {            $ca = cos($turn);            $sa = sin($turn);            $mx = $dx * $ca + $dz * $sa;            $mz = -$dx * $sa + $dz * $ca;            $nx = $x + $mx * $s;            $nz = $z + $mz * $s;            $hit = self::wallFree($zone, $nx, $nz, $r) ? $zone->solidHit($nx, $nz, $r) : false;            if ($hit === null) {                if ($ok === null || $ok($nx, $nz)) {                    return [$nx, $nz, $mx, $mz, $turn];                }                continue;            }            if ($hit === false || $turn !== 0.0) {                continue;            }            // a solid thing straight ahead: slide along it (the part of the step tangent to its circle)            [$cx, $cz] = $hit;            $ox = $x - $cx;            $oz = $z - $cz;            $ol = hypot($ox, $oz);            if ($ol < 0.001) {                continue;            }            $ox /= $ol;            $oz /= $ol;            $into = $mx * $ox + $mz * $oz;            $tx = $mx - $into * $ox;            $tz = $mz - $into * $oz;            $tl = hypot($tx, $tz);            if ($tl < 0.2) {                continue;      // head on: turning aside does better            }            $tx /= $tl;            $tz /= $tl;            $nx = $x + $tx * $s;            $nz = $z + $tz * $s;            if (self::wallFree($zone, $nx, $nz, $r) && $zone->solidHit($nx, $nz, $r) === null && ($ok === null || $ok($nx, $nz))) {                return [$nx, $nz, $tx, $tz, 0.01];            }        }        return null;    }    private static function wallFree(Zone $zone, float $x, float $z, float $r): bool    {        $w = min($r, self::WALL);        return !($zone->solidAt($x, $z) || $zone->solidAt($x + $w, $z) || $zone->solidAt($x - $w, $z) || $zone->solidAt($x, $z + $w) || $zone->solidAt($x, $z - $w));    }}