# ==============================================================================
# GOOGLE COLAB SINGLE-CELL SCRIPT: STAGE 2 THE GRADUAL TAPER & NATURAL WALKING
# Warm-Started from Stage 1 Checkpoint + 200 Gens OpenMP + 60 FPS Export
# ==============================================================================
import os
import sys
import base64
import subprocess

print("=" * 70)
print("STAGE 2: THE GRADUAL TAPER & NATURAL BIPEDAL WALKING")
print("Curriculum Learning: Skyhook Gyro & Levitation Taper 100% -> 15%")
print("=" * 70)

# 1. Install dependencies
print("[1/6] Installing dependencies (FFmpeg, OpenMP, Pygame-CE)...")
subprocess.run(["apt-get", "update", "-qq"], stdout=subprocess.DEVNULL)
subprocess.run(["apt-get", "install", "-y", "-qq", "ffmpeg", "build-essential"], stdout=subprocess.DEVNULL)
subprocess.run([sys.executable, "-m", "pip", "install", "-q", "pygame-ce"], stdout=subprocess.DEVNULL)

# 2. Extract Pretrained Stage 1 Checkpoint
print("[2/6] Extracting Stage 1 Pretrained Checkpoint...")
os.makedirs("./training_data", exist_ok=True)
s1_bytes = base64.b64decode("jj/mvnJstj79Vz++aJ6UPiRS/DzWv2K+6nWPPoOre77P8Iu+kkZxPvpVZj3ZiJQ9KdF7u7qWCr6fbHk+zFJePXdGn77gQB2+eL4vvkSFpDveLYk9CoaxPLzPk74Fl1s+LOmQPsx7nz0si04/Jl4Uv7ror7x8d2g+rcy1vDjyCbxqpts9voPKPNHGxL6fb5u+58sEP0fAKD9+zAO+m1i6vczcyD55JjW+ziB+PuYZlT4G9Nk+B3oNvZGTUD79VbC+fCrpvvDXLTwgIWI9tSqBvup3eb7/6mS/XsqAvhmRu70mKTs9UI6jvvZ1+z0R4xE/G7GHPptqdL4Yu6U+xNcEvgA5Er4UhYW9p4KtPmZmlT1o5XK+/ti4vb7jGD8i7NG9sRd3vuanx71sfBU+xcf9PjyGHj4u85K9QaAIvuKXLD4C2h0+m7euvvD4eL7E3Dk+NbKGPCDxlj3bGfM9jk2wvlRJkj1kDiA+jPTKPphCuj7avuO8YSflPkLucDxwBxi8rOYqvR9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G+E2YPvtgYSz46Aay+eHgOPlBXGD9UxBm9INGLvMsAH78aS/A9P7cmPrNRHb4OFwA+mlDKveF8xzx6wNk+Vy9UPumT1b28YYO8EpKNvk7VKj5dRoQ+KBc2viPfkb7LETg++MUAv2rKzz6/JsA9tD1UPibdSr1dK5Q9dP8lPctALb7VpIO+sFeDPbwl9T015Ji+4wO8PmG3Zj5OeJm+E7rXPCC7JT637VS+uzPGvsBiFzsti449ia+8PeJwJj4hq3g/vpPNPVw6Fr4a4929NFxMvrWnmT7CoIi+Lj+rvHNlEj8AYD89zJgEv7iKiL4jr2s+oSdEPyJfDr0IS6g+tULAPkDaeb04lKk9zPdGPgPpjb75cxq/7uCwPiRywr7Ha7A9uolyvsBRjz0AmDc+kC0MPlRGFj5K5d+9TI10vqx0Mj6vODa+ZbGRPjxm3T56nHg9hQGUvQ+9uD6/HiS+loVGvqxwWz1aHaa90UnyvdCaqj2/M7K9sOIqvqk3170DzYS+jF8OvuCV1rodCH6+chFPPs1fRr/ky+u9Av9qPvTaVL09Xl0+YHYcPvoAGL9BcW49UsmJvpPJuj68Wjc+mHqsvnGmmL1C5xu+fCvyPQ5NYD6MctA+a6xBv3Ijtb6Q65M8BV0jPjA6xz5WRC29gEpVvqfFnD5RE82+OK/IvdSe777VSSG+WlBFPUDM87wKfLk+KISMPstDTz61ra6+bIVbPnflqb7dOns+dJfBPgNkfL65oCM+wkUpPuhMg774sHO81XMjP41M2j22t0W/x1klPmmtCr40jdE98L80viTf5zzQa0M8QNF+vV2Rzr3Tii2+EdGhvbvTIz5Z46c+pZjuPVaboL6Dx0i+y9tnP335QD5YtH89JtGOvtdM3b6KqJe+HlgwvvwZQ77sHWU9EOnLPT7SML0xZoi+DDLbvbCLBb8qFR++4PRJvoFjxj2MruW+y2Fyvg==")
with open("./training_data/stage1_balance_best.bin", "wb") as f:
    f.write(s1_bytes)
print(f"Loaded Stage 1 weights: {len(s1_bytes)} bytes")

# 3. Write C++ simulation source
print("[3/6] Writing stage2_walking.cpp...")
cpp_source = r"""#include <iostream>
#include <vector>
#include <cmath>
#include <random>
#include <algorithm>
#include <fstream>
#include <chrono>
#include <cstring>
#include <filesystem>
#include <omp.h>

namespace fs = std::filesystem;

constexpr float PI = 3.14159265358979323846f;
constexpr float DT = 0.01666667f; // 60 Hz physics
constexpr float FLOOR_Y = 560.0f;
constexpr float WALL_LEFT = 100.0f;
constexpr float WALL_RIGHT = 1180.0f;

constexpr int NUM_JOINTS = 19;
constexpr float HEAD_RADIUS = 14.0f;
constexpr float STRIDE_FREQ = 1.30f;

struct Vec2 {
    float x = 0.0f;
    float y = 0.0f;

    Vec2() = default;
    Vec2(float x_, float y_) : x(x_), y(y_) {}

    Vec2 operator+(const Vec2& o) const { return {x + o.x, y + o.y}; }
    Vec2 operator-(const Vec2& o) const { return {x - o.x, y - o.y}; }
    Vec2 operator*(float s) const { return {x * s, y * s}; }
    Vec2& operator+=(const Vec2& o) { x += o.x; y += o.y; return *this; }
    Vec2& operator-=(const Vec2& o) { x -= o.x; y -= o.y; return *this; }

    float lengthSq() const { return x * x + y * y; }
    float length() const { return std::sqrt(lengthSq()); }

    Vec2 normalized() const {
        float l = length();
        return (l > 1e-5f) ? Vec2(x / l, y / l) : Vec2(0, 0);
    }
};

float cross(const Vec2& a, const Vec2& b) { return a.x * b.y - a.y * b.x; }

inline float wrapAngle(float a) {
    while (a > PI) a -= 2.0f * PI;
    while (a < -PI) a += 2.0f * PI;
    return a;
}

struct Stick {
    int p1;
    int p2;
    float restLen;
};

// -----------------------------------------------------------------------------
// STAGE 2 NEURAL NETWORK: 23 Inputs -> 32 Hidden -> 32 Hidden -> 6 Outputs
// -----------------------------------------------------------------------------
constexpr int NN_IN = 23;
constexpr int NN_H1 = 32;
constexpr int NN_H2 = 32;
constexpr int NN_OUT = 6;

struct Stage1Brain {
    float w1[19][32] = {0};
    float b1[32] = {0};
    float w2[32][32] = {0};
    float b2[32] = {0};
    float w3[32][6] = {0};
    float b3[6] = {0};
};

struct Brain {
    float w1[NN_IN][NN_H1] = {0};
    float b1[NN_H1] = {0};
    float w2[NN_H1][NN_H2] = {0};
    float b2[NN_H2] = {0};
    float w3[NN_H2][NN_OUT] = {0};
    float b3[NN_OUT] = {0};

    void initRandom(std::mt19937& rng) {
        std::normal_distribution<float> dist(0.0f, 0.25f);
        for (int i = 0; i < NN_IN; ++i)
            for (int j = 0; j < NN_H1; ++j) w1[i][j] = dist(rng);
        for (int j = 0; j < NN_H1; ++j) b1[j] = dist(rng);

        for (int i = 0; i < NN_H1; ++i)
            for (int j = 0; j < NN_H2; ++j) w2[i][j] = dist(rng);
        for (int j = 0; j < NN_H2; ++j) b2[j] = dist(rng);

        for (int i = 0; i < NN_H2; ++i)
            for (int j = 0; j < NN_OUT; ++j) w3[i][j] = dist(rng);
        for (int j = 0; j < NN_OUT; ++j) b3[j] = dist(rng);
    }

    bool warmStartFromStage1(const std::string& path, std::mt19937& rng) {
        std::ifstream in(path, std::ios::binary);
        if (!in) {
            std::cout << "[WARN] Could not find " << path << ", initializing random." << std::endl;
            initRandom(rng);
            return false;
        }
        Stage1Brain s1;
        in.read(reinterpret_cast<char*>(&s1), sizeof(s1));
        in.close();

        // Copy Stage 1 postural weights (inputs 0..18)
        for (int i = 0; i < 19; ++i) {
            for (int j = 0; j < NN_H1; ++j) {
                w1[i][j] = s1.w1[i][j];
            }
        }
        // Small initial weights for new inputs 19..22 (target_vx, sin, cos, taper)
        std::normal_distribution<float> smallDist(0.0f, 0.04f);
        for (int i = 19; i < NN_IN; ++i) {
            for (int j = 0; j < NN_H1; ++j) {
                w1[i][j] = smallDist(rng);
            }
        }
        for (int j = 0; j < NN_H1; ++j) b1[j] = s1.b1[j];

        // Deep layers are preserved exactly
        for (int i = 0; i < NN_H1; ++i)
            for (int j = 0; j < NN_H2; ++j) w2[i][j] = s1.w2[i][j];
        for (int j = 0; j < NN_H2; ++j) b2[j] = s1.b2[j];

        for (int i = 0; i < NN_H2; ++i)
            for (int j = 0; j < NN_OUT; ++j) w3[i][j] = s1.w3[i][j];
        for (int j = 0; j < NN_OUT; ++j) b3[j] = s1.b3[j];

        std::cout << "[WARM-START] Loaded Stage 1 weights successfully from " << path << "!" << std::endl;
        return true;
    }

    void mutate(std::mt19937& rng, float rate = 0.05f, float magnitude = 0.06f) {
        std::uniform_real_distribution<float> rDist(0.0f, 1.0f);
        std::normal_distribution<float> mDist(0.0f, magnitude);

        for (int i = 0; i < NN_IN; ++i)
            for (int j = 0; j < NN_H1; ++j)
                if (rDist(rng) < rate) w1[i][j] += mDist(rng);
        for (int j = 0; j < NN_H1; ++j)
            if (rDist(rng) < rate) b1[j] += mDist(rng);

        for (int i = 0; i < NN_H1; ++i)
            for (int j = 0; j < NN_H2; ++j)
                if (rDist(rng) < rate) w2[i][j] += mDist(rng);
        for (int j = 0; j < NN_H2; ++j)
            if (rDist(rng) < rate) b2[j] += mDist(rng);

        for (int i = 0; i < NN_H2; ++i)
            for (int j = 0; j < NN_OUT; ++j)
                if (rDist(rng) < rate) w3[i][j] += mDist(rng);
        for (int j = 0; j < NN_OUT; ++j)
            if (rDist(rng) < rate) b3[j] += mDist(rng);
    }

    void forward(const float in[NN_IN], float out[NN_OUT]) const {
        float h1[NN_H1];
        for (int j = 0; j < NN_H1; ++j) {
            float sum = b1[j];
            for (int i = 0; i < NN_IN; ++i) sum += in[i] * w1[i][j];
            h1[j] = std::tanh(sum);
        }

        float h2[NN_H2];
        for (int j = 0; j < NN_H2; ++j) {
            float sum = b2[j];
            for (int i = 0; i < NN_H1; ++i) sum += h1[i] * w2[i][j];
            h2[j] = std::tanh(sum);
        }

        for (int j = 0; j < NN_OUT; ++j) {
            float sum = b3[j];
            for (int i = 0; i < NN_H2; ++i) sum += h2[i] * w3[i][j];
            out[j] = std::tanh(sum);
        }
    }
};

// -----------------------------------------------------------------------------
// STAGE 2 RAGDOLL PHYSICS & DYNAMIC ASSIST TAPER
// -----------------------------------------------------------------------------
struct Ragdoll {
    Vec2 pos[NUM_JOINTS];
    Vec2 prevPos[NUM_JOINTS];
    Vec2 acc[NUM_JOINTS];
    float invMass[NUM_JOINTS];
    std::vector<Stick> sticks;

    float nominalHeight = 224.0f;
    float startX = 640.0f;

    int headGroundStrikes = 0;
    bool prevHeadGrounded = false;
    float slouchTimer = 0.0f;
    bool disqualified = false;

    bool leftGrounded = false;
    bool rightGrounded = false;
    float spineAngle = 0.0f;
    float spineAngVel = 0.0f;
    Vec2 comVel = {0, 0};
    Vec2 comPos = {0, 0};

    float pushRecoveryTimer = 0.0f;
    bool pushActive = false;
    float lastPushForce = 0.0f;
    int pushesRecovered = 0;
    float currentNnOffsets[NN_OUT] = {0};

    // Stage 2 Navigation & Walking State
    float walkPhase = 0.0f;
    float targetVx = 100.0f;
    int stepsSinceTurn = 0;
    int directionChanges = 0;
    float currentGaitDir = 1.0f;

    void reset(float startPosX = 640.0f) {
        startX = startPosX;
        headGroundStrikes = 0;
        prevHeadGrounded = false;
        slouchTimer = 0.0f;
        disqualified = false;
        pushRecoveryTimer = 0.0f;
        pushActive = false;
        lastPushForce = 0.0f;
        pushesRecovered = 0;
        for (int i = 0; i < NN_OUT; ++i) currentNnOffsets[i] = 0.0f;

        walkPhase = 0.0f;
        targetVx = 100.0f;
        stepsSinceTurn = 0;
        directionChanges = 0;
        currentGaitDir = 1.0f;

        float groundY = FLOOR_Y;

        // Feet flat on ground (widened 32px base)
        pos[6]  = {startX - 10.0f - 20.0f, groundY};         // L_Heel
        pos[7]  = {startX - 10.0f + 12.0f, groundY};         // L_Toe
        pos[5]  = {startX - 10.0f,         groundY - 10.0f}; // L_Ankle

        pos[11] = {startX + 10.0f - 20.0f, groundY};         // R_Heel
        pos[12] = {startX + 10.0f + 12.0f, groundY};         // R_Toe
        pos[10] = {startX + 10.0f,         groundY - 10.0f}; // R_Ankle

        // Knees (standing posture)
        pos[4]  = {startX - 10.0f, groundY - 60.0f}; // L_Knee
        pos[9]  = {startX + 10.0f, groundY - 60.0f}; // R_Knee

        // Hips & Pelvis
        pos[3]  = {startX - 8.0f,  groundY - 112.0f}; // L_Hip
        pos[8]  = {startX + 8.0f,  groundY - 112.0f}; // R_Hip
        pos[2]  = {startX,         groundY - 112.0f}; // Pelvis

        // Torso / Neck / Head
        pos[1]  = {startX,         groundY - 180.0f}; // Neck/Chest
        pos[0]  = {startX,         groundY - 210.0f}; // Head Center

        // Arms (Natural downward resting pose with slight bend)
        pos[13] = {startX - 14.0f, groundY - 176.0f}; // L_Shoulder
        pos[14] = {startX - 12.0f, groundY - 146.0f}; // L_Elbow
        pos[15] = {startX - 6.0f,  groundY - 118.0f}; // L_Hand

        pos[16] = {startX + 14.0f, groundY - 176.0f}; // R_Shoulder
        pos[17] = {startX + 16.0f, groundY - 146.0f}; // R_Elbow
        pos[18] = {startX + 22.0f,  groundY - 118.0f}; // R_Hand

        nominalHeight = groundY - (pos[0].y - HEAD_RADIUS); // 224px

        for (int i = 0; i < NUM_JOINTS; ++i) {
            prevPos[i] = pos[i];
            acc[i] = {0, 0};
            invMass[i] = 1.0f;
        }

        invMass[0]  = 0.50f; // Head
        invMass[1]  = 0.40f; // Chest/Torso
        invMass[2]  = 0.40f; // Pelvis
        invMass[3]  = 0.70f; // L_Hip
        invMass[8]  = 0.70f; // R_Hip
        invMass[4]  = 0.75f; // L_Knee
        invMass[9]  = 0.75f; // R_Knee
        invMass[5]  = 0.90f; // L_Ankle
        invMass[10] = 0.90f; // R_Ankle
        invMass[6]  = 1.00f; // L_Heel
        invMass[7]  = 1.00f; // L_Toe
        invMass[11] = 1.00f; // R_Heel
        invMass[12] = 1.00f; // R_Toe
        invMass[13] = 0.80f; invMass[16] = 0.80f;
        invMass[14] = 0.90f; invMass[15] = 0.90f;
        invMass[17] = 0.90f; invMass[18] = 0.90f;

        sticks.clear();
        auto addStick = [&](int a, int b) {
            sticks.push_back({a, b, (pos[a] - pos[b]).length()});
        };

        // Torso & Pelvis Box
        addStick(0, 1);   // Head - Neck
        addStick(1, 2);   // Spine
        addStick(2, 3); addStick(2, 8); addStick(3, 8);
        addStick(1, 3); addStick(1, 8); // Cross braces

        // Left Leg
        addStick(3, 4); addStick(4, 5);
        addStick(5, 6); addStick(5, 7); addStick(6, 7);

        // Right Leg
        addStick(8, 9); addStick(9, 10);
        addStick(10, 11); addStick(10, 12); addStick(11, 12);

        // Arms
        addStick(1, 13); addStick(13, 14); addStick(14, 15);
        addStick(1, 16); addStick(16, 17); addStick(17, 18);
    }

    float getCurrentHeight() const {
        return FLOOR_Y - (pos[0].y - HEAD_RADIUS);
    }

    void computeCOM(Vec2& com, Vec2& vCom) const {
        com = {0, 0}; vCom = {0, 0};
        float totalMass = 0.0f;
        for (int i = 0; i < NUM_JOINTS; ++i) {
            float m = 1.0f / invMass[i];
            com += pos[i] * m;
            Vec2 v = (pos[i] - prevPos[i]) * (1.0f / DT);
            vCom += v * m;
            totalMass += m;
        }
        com = com * (1.0f / totalMass);
        vCom = vCom * (1.0f / totalMass);
    }

    void enforceArmLimits(int jShoulder, int jElbow, int jHand, float shoulderTarget, float elbowTarget, float stiffnessShoulder = 0.4f, float stiffnessElbow = 0.5f) {
        Vec2 spine = pos[1] - pos[2];
        float torsoAngle = std::atan2(spine.x, -spine.y);

        Vec2 rUpper = pos[jElbow] - pos[jShoulder];
        float lenUpper = rUpper.length();
        if (lenUpper < 1e-4f) lenUpper = 30.0f;

        float armAngle = std::atan2(rUpper.x, rUpper.y);
        float relShoulder = wrapAngle(armAngle - torsoAngle);

        constexpr float minShoulder = -45.0f * (PI / 180.0f);
        constexpr float maxShoulder =  110.0f * (PI / 180.0f);

        float targetS = std::clamp(shoulderTarget, minShoulder, maxShoulder);
        float diffS = wrapAngle(relShoulder - targetS);
        float newRelS = std::clamp(relShoulder - diffS * stiffnessShoulder, minShoulder, maxShoulder);

        float newWorldArmAngle = torsoAngle + newRelS;
        pos[jElbow] = pos[jShoulder] + Vec2(std::sin(newWorldArmAngle), std::cos(newWorldArmAngle)) * lenUpper;

        Vec2 rForearm = pos[jHand] - pos[jElbow];
        float lenForearm = rForearm.length();
        if (lenForearm < 1e-4f) lenForearm = 28.0f;

        float forearmAngle = std::atan2(rForearm.x, rForearm.y);
        float relElbow = wrapAngle(forearmAngle - newWorldArmAngle);

        constexpr float minElbow = 0.0f;
        constexpr float maxElbow = 135.0f * (PI / 180.0f);

        float targetE = std::clamp(elbowTarget, minElbow, maxElbow);
        float diffE = wrapAngle(relElbow - targetE);
        float newRelE = std::clamp(relElbow - diffE * stiffnessElbow, minElbow, maxElbow);

        float newWorldForearmAngle = newWorldArmAngle + newRelE;
        pos[jHand] = pos[jElbow] + Vec2(std::sin(newWorldForearmAngle), std::cos(newWorldForearmAngle)) * lenForearm;
    }

    void enforceJointAngle(int jParent, int jPivot, int jChild, float targetAngle, float stiffness = 0.4f) {
        Vec2 rA = pos[jParent] - pos[jPivot];
        Vec2 rB = pos[jChild] - pos[jPivot];
        float lenB = rB.length();
        if (lenB < 1e-4f) return;

        float curAngle = wrapAngle(std::atan2(rB.y, rB.x) - std::atan2(rA.y, rA.x));
        float diff = wrapAngle(curAngle - targetAngle);

        float correction = -diff * stiffness;
        float cosC = std::cos(correction);
        float sinC = std::sin(correction);

        Vec2 newRB = {
            rB.x * cosC - rB.y * sinC,
            rB.x * sinC + rB.y * cosC
        };
        pos[jChild] = pos[jPivot] + newRB;
    }

    void applyForces(const float nnOffsets[NN_OUT], float taper, float externalPushX = 0.0f) {
        for (int i = 0; i < NN_OUT; ++i) currentNnOffsets[i] = nnOffsets[i];

        // 1. Advance Central Pattern Generator (1.30 Hz rhythm clock)
        walkPhase += (2.0f * PI * STRIDE_FREQ) * DT;
        if (walkPhase > 2.0f * PI) walkPhase -= 2.0f * PI;

        // 2. Navigation Target (Patrol Route: switch at x=950 / x=250 or 8.0s = 480 steps)
        stepsSinceTurn++;
        if (targetVx > 0.0f) {
            if (pos[2].x >= 950.0f || stepsSinceTurn >= 480) {
                targetVx = -100.0f;
                stepsSinceTurn = 0;
                directionChanges++;
            }
        } else {
            if (pos[2].x <= 250.0f || stepsSinceTurn >= 480) {
                targetVx = 100.0f;
                stepsSinceTurn = 0;
                directionChanges++;
            }
        }

        // Smooth direction slew (prevents joint-snap launches during turnarounds)
        float desiredDir = (targetVx > 0.0f) ? 1.0f : -1.0f;
        currentGaitDir += std::clamp(desiredDir - currentGaitDir, -0.05f, 0.05f);

        // 3. Gravity
        for (int i = 0; i < NUM_JOINTS; ++i) {
            acc[i] = {0, 980.0f};
        }

        // 4. Horizontal Push Impulse on Torso & Pelvis
        if (std::abs(externalPushX) > 1e-3f) {
            acc[1].x += externalPushX * 2.2f * invMass[1];
            acc[2].x += externalPushX * 1.8f * invMass[2];
            lastPushForce = externalPushX;
            pushActive = true;
            pushRecoveryTimer = 1.5f;
        }

        // 5. SKYHOOK GYRO STABILIZER SCALED BY CURRICULUM TAPER
        // spineTorque = (-4500.0f * curSpineAng - 380.0f * spineAngVel) * taper;
        Vec2 spine = pos[1] - pos[2];
        float curSpineAng = std::atan2(spine.x, -spine.y);
        float spineTorque = (-4500.0f * curSpineAng - 380.0f * spineAngVel) * taper;
        spineTorque += nnOffsets[0] * 1200.0f; // AI active spine posture torque

        float spineLen = spine.length();
        if (spineLen > 1e-3f) {
            Vec2 gyroF = Vec2(spineTorque / spineLen, 0.0f);
            acc[1] += gyroF * invMass[1];
            acc[2] -= gyroF * invMass[2];
        }

        // 6. PELVIS SUSPENSION ASSIST SCALED BY CURRICULUM TAPER
        // supportForce = clamp(1200.0f * yDiff - 150.0f * vyPelvis, 0.0f, 1500.0f) * taper;
        if (leftGrounded || rightGrounded) {
            float targetPelvisY = FLOOR_Y - 112.0f;
            float yDiff = pos[2].y - targetPelvisY;
            if (yDiff > 0.0f) {
                float vyPelvis = (pos[2].y - prevPos[2].y) / DT;
                float supportForce = std::clamp(1200.0f * yDiff - 150.0f * vyPelvis, 0.0f, 1500.0f) * taper;
                acc[2].y -= supportForce * invMass[2];
            }
        }
    }

    void verletIntegrate(float taper) {
        for (int i = 0; i < NUM_JOINTS; ++i) {
            Vec2 v = (pos[i] - prevPos[i]) * 0.990f;
            v.x = std::clamp(v.x, -25.0f, 25.0f);
            v.y = std::clamp(v.y, -25.0f, 25.0f);

            Vec2 next = pos[i] + v + acc[i] * (DT * DT);
            prevPos[i] = pos[i];
            pos[i] = next;
        }

        float dir = currentGaitDir;
        float sPhi = std::sin(walkPhase);

        // PBD Infinite Rigidity Taper: Soften position constraint stiffness from 0.60 down to 0.35
        float legStiffness = 0.35f + 0.25f * taper;
        float kneeStiffness = std::min(0.70f, legStiffness + 0.10f);

        // Constraint relaxation loop
        for (int iter = 0; iter < 10; ++iter) {
            for (const auto& s : sticks) {
                Vec2 delta = pos[s.p2] - pos[s.p1];
                float d = delta.length();
                if (d < 1e-6f) continue;
                float diff = (d - s.restLen) / d;
                float w1 = invMass[s.p1];
                float w2 = invMass[s.p2];
                float totalW = w1 + w2;
                if (totalW < 1e-6f) continue;

                Vec2 offset = delta * (diff * (1.0f / totalW));
                pos[s.p1] += offset * w1;
                pos[s.p2] -= offset * w2;
            }

            // -----------------------------------------------------------------
            // BIPEDAL CPG WALKING ENGINE & ANGLE CONSTRAINTS
            // -----------------------------------------------------------------
            // Hip stride: alternating forward (PI - stride) / backward (PI + stride)
            float stride = 0.18f * sPhi;
            float lHipTarget = PI - (dir * stride) + (currentNnOffsets[1] * 0.25f);
            float rHipTarget = PI + (dir * stride) + (currentNnOffsets[3] * 0.25f);

            // Knee swing flexion: natural backward bend > PI (lifts foot >= 12px)
            float lKneeBend = (sPhi > 0.10f) ? (0.30f * sPhi) : 0.0f;
            float rKneeBend = (sPhi < -0.10f) ? (-0.30f * sPhi) : 0.0f;
            float lKneeTarget = PI + lKneeBend + (currentNnOffsets[2] * 0.25f);
            float rKneeTarget = PI + rKneeBend + (currentNnOffsets[4] * 0.25f);

            enforceJointAngle(1, 3, 4, lHipTarget, legStiffness);   // Left Hip
            enforceJointAngle(3, 4, 5, lKneeTarget, kneeStiffness); // Left Knee
            enforceJointAngle(1, 8, 9, rHipTarget, legStiffness);   // Right Hip
            enforceJointAngle(8, 9, 10, rKneeTarget, kneeStiffness); // Right Knee

            // Ankle drive & late stance toe-off
            float toeOff = currentNnOffsets[5] * 0.20f;
            float lAnkleTarget = PI * 0.5f + ((sPhi < -0.15f) ? toeOff : 0.0f);
            float rAnkleTarget = PI * 0.5f + ((sPhi >  0.15f) ? toeOff : 0.0f);
            enforceJointAngle(4, 5, 7, lAnkleTarget, 0.7f); // Left Ankle
            enforceJointAngle(9, 10, 12, rAnkleTarget, 0.7f); // Right Ankle

            // Arms natural counter-swing
            float lArmS = -dir * 0.20f * sPhi + (currentNnOffsets[5] * 0.10f);
            float rArmS =  dir * 0.20f * sPhi - (currentNnOffsets[5] * 0.10f);
            constexpr float elbT = 20.0f * (PI / 180.0f);
            enforceArmLimits(13, 14, 15, lArmS, elbT, 0.4f, 0.5f);
            enforceArmLimits(16, 17, 18, rArmS, elbT, 0.4f, 0.5f);

            // Knee angle constraints: prevent bending forward
            enforceKneeLimit(3, 4, 5);
            enforceKneeLimit(8, 9, 10);

            // Keep head upright on neck
            if (pos[0].y > pos[1].y - 20.0f) pos[0].y = pos[1].y - 28.0f;

            // Ground Contact & Coulomb Friction
            leftGrounded = false;
            rightGrounded = false;

            for (int i = 0; i < NUM_JOINTS; ++i) {
                float radius = (i == 0) ? HEAD_RADIUS : 0.0f;
                float contactY = FLOOR_Y - radius;

                if (pos[i].y >= contactY) {
                    pos[i].y = contactY;

                    if (i == 6 || i == 7 || i == 5) leftGrounded = true;
                    if (i == 11 || i == 12 || i == 10) rightGrounded = true;

                    if (prevPos[i].y > pos[i].y) prevPos[i].y = pos[i].y;

                    // Coulomb Friction: Static latch vs kinetic slip
                    float vx = pos[i].x - prevPos[i].x;
                    if (std::abs(vx) < 4.0f * DT) {
                        prevPos[i].x = pos[i].x;
                    } else {
                        prevPos[i].x = pos[i].x - vx * 0.40f;
                    }
                }

                if (pos[i].x < WALL_LEFT) { pos[i].x = WALL_LEFT; prevPos[i].x = pos[i].x; }
                if (pos[i].x > WALL_RIGHT) { pos[i].x = WALL_RIGHT; prevPos[i].x = pos[i].x; }
            }
        }

        // Spine Angle Update
        Vec2 spine = pos[1] - pos[2];
        float prevSpineAngle = spineAngle;
        spineAngle = std::atan2(spine.x, -spine.y);
        spineAngVel = (spineAngle - prevSpineAngle) / DT;

        computeCOM(comPos, comVel);

        // ---------------------------------------------------------------------
        // STRICT DISQUALIFICATION WATCHDOG
        // ---------------------------------------------------------------------
        bool currentHeadGrounded = (pos[0].y + HEAD_RADIUS >= FLOOR_Y - 0.5f);
        if (currentHeadGrounded && !prevHeadGrounded) {
            headGroundStrikes++;
            if (headGroundStrikes >= 2) {
                disqualified = true;
            }
        }
        prevHeadGrounded = currentHeadGrounded;

        // Instant disqualification if upper body or head contacts floor
        if (pos[0].y + HEAD_RADIUS >= FLOOR_Y - 0.5f || pos[1].y >= FLOOR_Y - 5.0f) {
            disqualified = true;
        }

        float curH = getCurrentHeight();
        // Ceiling limiter (prevents catapult launches)
        if (curH > 1.35f * nominalHeight || pos[0].y < 80.0f) {
            disqualified = true;
        }

        // Strict 75% slouch watchdog (2.0s grace period)
        if (curH < 0.75f * nominalHeight) {
            slouchTimer += DT;
            if (slouchTimer >= 2.0f) {
                disqualified = true;
            }
        } else {
            slouchTimer = std::max(0.0f, slouchTimer - DT * 3.0f);
        }

        if (pushActive) {
            pushRecoveryTimer -= DT;
            if (pushRecoveryTimer <= 0.0f) {
                if (!disqualified && curH >= 0.75f * nominalHeight && std::abs(spineAngle) < 0.45f) {
                    pushesRecovered++;
                }
                pushActive = false;
            }
        }
    }

    void enforceKneeLimit(int hip, int knee, int ankle) {
        Vec2 thigh = pos[knee] - pos[hip];
        Vec2 shin = pos[ankle] - pos[knee];
        float crossProd = cross(thigh, shin);
        if (crossProd < -1e-3f) {
            Vec2 perp = Vec2(-thigh.y, thigh.x).normalized();
            pos[ankle] += perp * (-crossProd * 0.15f);
        }
    }

    void buildSensors(float in[NN_IN], float taper) const {
        // EXACT STAGE 1 POSTURAL SENSORS (0..18)
        in[0] = std::clamp(spineAngle / (PI * 0.5f), -2.0f, 2.0f);
        in[1] = std::clamp(spineAngVel / 8.0f, -2.0f, 2.0f);
        in[2] = std::clamp((FLOOR_Y - pos[2].y) / nominalHeight, 0.0f, 1.5f);
        in[3] = leftGrounded ? 1.0f : 0.0f;
        in[4] = rightGrounded ? 1.0f : 0.0f;

        Vec2 lThigh = pos[4] - pos[3];
        in[5] = std::clamp(std::atan2(lThigh.x, lThigh.y) / PI, -1.0f, 1.0f);
        in[6] = std::clamp((lThigh.x - (prevPos[4].x - prevPos[3].x)) / (DT * 100.0f), -2.0f, 2.0f);

        Vec2 lShin = pos[5] - pos[4];
        in[7] = std::clamp(std::atan2(lShin.x, lShin.y) / PI, -1.0f, 1.0f);
        in[8] = std::clamp((lShin.x - (prevPos[5].x - prevPos[4].x)) / (DT * 100.0f), -2.0f, 2.0f);

        Vec2 lFoot = pos[7] - pos[6];
        in[9] = std::clamp(std::atan2(lFoot.y, lFoot.x) / PI, -1.0f, 1.0f);
        in[10] = std::clamp((pos[5].x - prevPos[5].x) / (DT * 100.0f), -2.0f, 2.0f);

        Vec2 rThigh = pos[9] - pos[8];
        in[11] = std::clamp(std::atan2(rThigh.x, rThigh.y) / PI, -1.0f, 1.0f);
        in[12] = std::clamp((rThigh.x - (prevPos[9].x - prevPos[8].x)) / (DT * 100.0f), -2.0f, 2.0f);

        Vec2 rShin = pos[10] - pos[9];
        in[13] = std::clamp(std::atan2(rShin.x, rShin.y) / PI, -1.0f, 1.0f);
        in[14] = std::clamp((rShin.x - (prevPos[10].x - prevPos[9].x)) / (DT * 100.0f), -2.0f, 2.0f);

        Vec2 rFoot = pos[12] - pos[11];
        in[15] = std::clamp(std::atan2(rFoot.y, rFoot.x) / PI, -1.0f, 1.0f);
        in[16] = std::clamp((pos[10].x - prevPos[10].x) / (DT * 100.0f), -2.0f, 2.0f);

        in[17] = std::clamp(comVel.x / 150.0f, -2.0f, 2.0f);
        in[18] = std::clamp(comVel.y / 150.0f, -2.0f, 2.0f);

        // STAGE 2 NAVIGATION & TAPER SENSORS (19..22)
        in[19] = std::clamp(targetVx / 120.0f, -1.0f, 1.0f);
        in[20] = std::sin(walkPhase);
        in[21] = std::cos(walkPhase);
        in[22] = std::clamp(taper, 0.0f, 1.0f);
    }
};

// -----------------------------------------------------------------------------
// STAGE 2 FITNESS EVALUATION WITH GRADUAL TAPER
// -----------------------------------------------------------------------------
struct EvalResult {
    float fitness;
    int steps;
    int pushesRecovered;
    bool survived;
    float avgSpeed;
};

constexpr int EPISODE_STEPS = 1200; // 20.0s per episode

EvalResult evaluateAgent(const Brain& brain, float taper, std::mt19937& rng) {
    Ragdoll ragdoll;
    ragdoll.reset(640.0f);

    std::uniform_int_distribution<int> pushIntervalDist(240, 360);
    std::uniform_real_distribution<float> pushMagnitudeDist(240.0f, 360.0f);
    std::uniform_int_distribution<int> pushDirDist(0, 1);

    int nextPushStep = pushIntervalDist(rng);

    float totalReward = 0.0f;
    int steps = 0;
    float totalProgress = 0.0f;

    for (int step = 0; step < EPISODE_STEPS; ++step) {
        steps++;

        float pushForce = 0.0f;
        if (step == nextPushStep) {
            float mag = pushMagnitudeDist(rng);
            pushForce = (pushDirDist(rng) == 1) ? mag : -mag;
            nextPushStep = step + pushIntervalDist(rng);
        }

        float sensors[NN_IN];
        ragdoll.buildSensors(sensors, taper);

        float nnOffsets[NN_OUT];
        brain.forward(sensors, nnOffsets);

        ragdoll.applyForces(nnOffsets, taper, pushForce);
        ragdoll.verletIntegrate(taper);

        if (ragdoll.disqualified) {
            totalReward -= 200.0f;
            break;
        }

        float curH = ragdoll.getCurrentHeight();
        float heightRatio = std::clamp(curH / ragdoll.nominalHeight, 0.0f, 1.2f);
        float cosSpine = std::cos(ragdoll.spineAngle);

        float targetDir = (ragdoll.targetVx > 0.0f) ? 1.0f : -1.0f;
        float progress = ragdoll.comVel.x * targetDir;
        totalProgress += progress;

        // 1. VELOCITY TRACKING REWARD
        // + 3.5 * (comVel.x * sign(target_vx))
        float frameReward = 3.5f * (progress / 100.0f);

        // 2. BIOMECHANICAL QUALITY & FOOT CLEARANCE
        // + 2.0 * step_clearance_bonus
        float lClear = FLOOR_Y - ragdoll.pos[7].y;
        float rClear = FLOOR_Y - ragdoll.pos[12].y;
        bool lSwing = (std::sin(ragdoll.walkPhase) > 0.12f);
        bool rSwing = (std::sin(ragdoll.walkPhase) < -0.12f);

        if (lSwing && lClear >= 12.0f && ragdoll.rightGrounded) frameReward += 2.0f;
        if (rSwing && rClear >= 12.0f && ragdoll.leftGrounded)  frameReward += 2.0f;

        // - 2.0 * double_foot_drag_penalty (strictly penalize shuffling/skating)
        if (ragdoll.leftGrounded && ragdoll.rightGrounded && std::abs(ragdoll.comVel.x) > 25.0f) {
            frameReward -= 2.0f * (std::abs(ragdoll.comVel.x) / 100.0f);
        }

        // - 1.0 * abs(comVel.y) (penalize excessive vertical bouncing/pogo hopping)
        frameReward -= 1.0f * std::min(std::abs(ragdoll.comVel.y) / 50.0f, 2.0f);

        // 3. POSTURE RETENTION (Must maintain tall posture above 205 px)
        // + 2.5 * (curH / nominalHeight)
        frameReward += 2.5f * heightRatio;
        frameReward += 1.5f * cosSpine;

        if (heightRatio >= 0.915f) frameReward += 2.5f; // >= 205 px bonus
        else if (heightRatio < 0.80f) frameReward -= 2.0f;

        // 4. NATURAL ARM MOVEMENT & HAND HEIGHT
        if (ragdoll.pos[15].y > ragdoll.pos[1].y && ragdoll.pos[18].y > ragdoll.pos[1].y) {
            frameReward += 1.0f;
        }
        if (ragdoll.pos[15].y < ragdoll.pos[0].y) frameReward -= 3.0f;
        if (ragdoll.pos[18].y < ragdoll.pos[0].y) frameReward -= 3.0f;

        totalReward += frameReward;
    }

    totalReward += (ragdoll.pushesRecovered * 40.0f);
    totalReward += (ragdoll.directionChanges * 35.0f);

    bool survived = (!ragdoll.disqualified && steps == EPISODE_STEPS);
    float avgSpeed = (steps > 0) ? (totalProgress / steps) : 0.0f;

    return {totalReward, steps, ragdoll.pushesRecovered, survived, avgSpeed};
}

// -----------------------------------------------------------------------------
// SHOWCASE REPLAY RECORDER WITH LIVE TAPER GAUGE
// -----------------------------------------------------------------------------
void exportShowcaseReplay(const Brain& champion, const std::string& filename) {
    std::cout << "Recording 60-Second Stage 2 Locomotion Showcase to " << filename << " (3,600 frames)..." << std::endl;
    Ragdoll ragdoll;
    ragdoll.reset(640.0f);

    std::ofstream out(filename, std::ios::binary);
    if (!out) return;

    int totalFrames = 3600;
    int numJoints = NUM_JOINTS;
    out.write(reinterpret_cast<const char*>(&totalFrames), sizeof(totalFrames));
    out.write(reinterpret_cast<const char*>(&numJoints), sizeof(numJoints));
    out.write(reinterpret_cast<const char*>(&ragdoll.nominalHeight), sizeof(float));

    std::mt19937 rng(42);
    std::uniform_int_distribution<int> pushIntervalDist(240, 360);
    std::uniform_real_distribution<float> pushMagnitudeDist(280.0f, 380.0f);
    std::uniform_int_distribution<int> pushDirDist(0, 1);

    int nextPushStep = 240;
    float activePushDisplay = 0.0f;
    int pushDisplayFrames = 0;

    for (int frame = 0; frame < totalFrames; ++frame) {
        // Showcase live taper: dials down from 100% to 15% across frames, demonstrating cheat removal
        float liveTaper = std::max(0.15f, 1.0f - ((float)frame / 2400.0f));

        float pushForce = 0.0f;
        if (frame == nextPushStep) {
            float mag = pushMagnitudeDist(rng);
            pushForce = (pushDirDist(rng) == 1) ? mag : -mag;
            nextPushStep = frame + pushIntervalDist(rng);
            activePushDisplay = pushForce;
            pushDisplayFrames = 35;
        }

        if (pushDisplayFrames > 0) pushDisplayFrames--;
        else activePushDisplay = 0.0f;

        float sensors[NN_IN];
        ragdoll.buildSensors(sensors, liveTaper);
        float nnOffsets[NN_OUT];
        champion.forward(sensors, nnOffsets);

        ragdoll.applyForces(nnOffsets, liveTaper, pushForce);
        ragdoll.verletIntegrate(liveTaper);

        if (ragdoll.disqualified) {
            ragdoll.reset(640.0f);
        }

        // Frame header: 14 values ("ifffiifififfff")
        out.write(reinterpret_cast<const char*>(&frame), sizeof(frame));
        float curH = ragdoll.getCurrentHeight();
        out.write(reinterpret_cast<const char*>(&curH), sizeof(float));
        out.write(reinterpret_cast<const char*>(&ragdoll.spineAngle), sizeof(float));
        out.write(reinterpret_cast<const char*>(&ragdoll.spineAngVel), sizeof(float));
        int lG = ragdoll.leftGrounded ? 1 : 0;
        int rG = ragdoll.rightGrounded ? 1 : 0;
        out.write(reinterpret_cast<const char*>(&lG), sizeof(int));
        out.write(reinterpret_cast<const char*>(&rG), sizeof(int));
        out.write(reinterpret_cast<const char*>(&activePushDisplay), sizeof(float));
        out.write(reinterpret_cast<const char*>(&ragdoll.headGroundStrikes), sizeof(int));
        out.write(reinterpret_cast<const char*>(&ragdoll.slouchTimer), sizeof(float));
        out.write(reinterpret_cast<const char*>(&ragdoll.pushesRecovered), sizeof(int));
        out.write(reinterpret_cast<const char*>(&ragdoll.targetVx), sizeof(float));
        out.write(reinterpret_cast<const char*>(&ragdoll.walkPhase), sizeof(float));
        out.write(reinterpret_cast<const char*>(&ragdoll.comVel.x), sizeof(float));
        out.write(reinterpret_cast<const char*>(&liveTaper), sizeof(float)); // 14th field: taper

        for (int j = 0; j < NUM_JOINTS; ++j) {
            float x = ragdoll.pos[j].x, y = ragdoll.pos[j].y;
            out.write(reinterpret_cast<const char*>(&x), sizeof(float));
            out.write(reinterpret_cast<const char*>(&y), sizeof(float));
        }
    }

    out.close();
    std::cout << "Showcase replay successfully saved!" << std::endl;
}

// -----------------------------------------------------------------------------
// MAIN TRAINING LOOP (200 GENERATIONS WITH GRADUAL ASSIST TAPER)
// -----------------------------------------------------------------------------
constexpr int POP_SIZE = 128;
constexpr int NUM_GENS = 200;

struct Agent {
    Brain brain;
    float fitness = 0.0f;
    int stepsAlive = 0;
    int pushesRecovered = 0;
    bool survived = false;
    float avgSpeed = 0.0f;
};

int main(int argc, char* argv[]) {
    int maxGenerations = NUM_GENS;
    if (argc > 1) maxGenerations = std::atoi(argv[1]);

    std::cout << "=================================================================" << std::endl;
    std::cout << "CURRICULUM STAGE 2: THE GRADUAL TAPER & NATURAL BIPEDAL WALKING" << std::endl;
    std::cout << POP_SIZE << " Agents x " << maxGenerations << " Generations | OpenMP Parallel Headless" << std::endl;
    std::cout << "Curriculum: Skyhook Gyro & Levitation Taper 100% -> 15% (Self-Support)" << std::endl;
    std::cout << "=================================================================" << std::endl;

    std::mt19937 masterRng(1337);
    Brain baseBrain;
    baseBrain.warmStartFromStage1("./training_data/stage1_balance_best.bin", masterRng);

    std::vector<Agent> pop(POP_SIZE);
    pop[0].brain = baseBrain;

    for (int i = 1; i < POP_SIZE; ++i) {
        pop[i].brain = baseBrain;
        pop[i].brain.mutate(masterRng, 0.07f, 0.08f);
    }

    fs::create_directories("./training_data");
    std::ofstream logFile("./training_data/stage2_walking_log.csv");
    logFile << "generation,taper,best_fitness,avg_fitness,alive_time,survival_rate,pushes_recovered,avg_speed\n";

    auto startClock = std::chrono::high_resolution_clock::now();

    for (int gen = 1; gen <= maxGenerations; ++gen) {
        // Dynamic Curriculum Taper Schedule: 1.0 down to 0.15 by gen 140
        float taper = std::max(0.15f, 1.0f - ((float)gen / 140.0f));

        #pragma omp parallel for schedule(dynamic)
        for (int i = 0; i < POP_SIZE; ++i) {
            std::mt19937 threadRng(gen * 1000 + i);
            EvalResult res = evaluateAgent(pop[i].brain, taper, threadRng);
            pop[i].fitness = res.fitness;
            pop[i].stepsAlive = res.steps;
            pop[i].pushesRecovered = res.pushesRecovered;
            pop[i].survived = res.survived;
            pop[i].avgSpeed = res.avgSpeed;
        }

        std::sort(pop.begin(), pop.end(), [](const Agent& a, const Agent& b) {
            return a.fitness > b.fitness;
        });

        float sumFit = 0.0f;
        int survivedCount = 0;
        float sumSpeed = 0.0f;
        for (const auto& a : pop) {
            sumFit += a.fitness;
            if (a.survived) survivedCount++;
            sumSpeed += a.avgSpeed;
        }
        float avgFit = sumFit / POP_SIZE;
        float survivalPct = (float)survivedCount / POP_SIZE * 100.0f;
        float avgSpeed = sumSpeed / POP_SIZE;

        logFile << gen << "," << taper << "," << pop[0].fitness << "," << avgFit << ","
                << (pop[0].stepsAlive * DT) << "," << survivalPct << ","
                << pop[0].pushesRecovered << "," << avgSpeed << "\n";
        logFile.flush();

        if (gen == 1 || gen % 20 == 0 || gen == maxGenerations) {
            std::cout << "[Gen " << gen << "/" << maxGenerations << "] "
                      << "Taper: " << (int)(taper * 100.0f) << "% ("
                      << (int)((1.0f - taper) * 100.0f) << "% Self-Support)"
                      << " | Best Fit: " << pop[0].fitness
                      << " | Avg Fit: " << avgFit
                      << " | Alive: " << (pop[0].stepsAlive * DT) << "s"
                      << " | Survived: " << (int)survivalPct << "%"
                      << " | Speed: " << pop[0].avgSpeed << " px/s" << std::endl;
        }

        if (gen == maxGenerations) break;

        // Elitism & Genetic Recombination
        int eliteCount = 16;
        std::vector<Agent> nextPop;
        nextPop.reserve(POP_SIZE);

        for (int i = 0; i < eliteCount; ++i) nextPop.push_back(pop[i]);

        int parentPool = 28;
        for (int i = eliteCount; i < POP_SIZE - 8; ++i) {
            Agent child = pop[i % parentPool];
            child.brain.mutate(masterRng, 0.05f, 0.06f);
            nextPop.push_back(child);
        }

        for (int i = 0; i < 8; ++i) {
            Agent fresh = pop[0];
            fresh.brain.mutate(masterRng, 0.12f, 0.12f);
            nextPop.push_back(fresh);
        }

        pop = std::move(nextPop);
    }

    logFile.close();

    auto endClock = std::chrono::high_resolution_clock::now();
    double totalTime = std::chrono::duration<double>(endClock - startClock).count();
    std::cout << "Training finished in " << totalTime << "s (" << (totalTime / 60.0) << " mins)!" << std::endl;

    std::ofstream cp("./training_data/stage2_walking_best.bin", std::ios::binary);
    cp.write(reinterpret_cast<const char*>(&pop[0].brain), sizeof(Brain));
    cp.close();

    exportShowcaseReplay(pop[0].brain, "./training_data/stage2_walking_showcase.dat");
    return 0;
}
"""
with open("stage2_walking.cpp", "w") as f:
    f.write(cpp_source)

# 4. Write Python 60 FPS Renderer
print("[4/6] Writing render_walking.py...")
py_renderer_source = r"""import os
import sys
import struct
import math
import random
import subprocess
import pygame

def render_showcase(dat_file="./training_data/stage2_walking_showcase.dat", output_mp4="./training_data/stage2_walking_showcase.mp4"):
    if not os.path.exists(dat_file):
        print(f"Error: {dat_file} not found!")
        return False

    os.environ["SDL_VIDEODRIVER"] = "dummy"
    pygame.init()
    pygame.font.init()

    WIDTH, HEIGHT = 1280, 720
    FPS = 60
    surface = pygame.Surface((WIDTH, HEIGHT))

    font_huge = pygame.font.SysFont("DejaVu Sans,Arial", 22, bold=True)
    font_large = pygame.font.SysFont("DejaVu Sans,Arial", 18, bold=True)
    font_med = pygame.font.SysFont("DejaVu Sans,Arial", 14, bold=True)
    font_small = pygame.font.SysFont("DejaVu Sans,Arial", 11)
    font_mono = pygame.font.SysFont("monospace,Courier New", 13, bold=True)
    font_gauge = pygame.font.SysFont("monospace,Courier New", 15, bold=True)

    with open(dat_file, "rb") as f:
        total_frames, num_joints, nominal_h = struct.unpack("iif", f.read(12))
        print(f"Loading {total_frames} frames from {dat_file} (nominal_h = {nominal_h:.1f}px)...")

        FLOOR_Y = 560
        WALL_LEFT = 100
        WALL_RIGHT = 1180
        PATROL_LEFT = 250
        PATROL_RIGHT = 950
        TARGET_H_Y = int(FLOOR_Y - nominal_h)
        DISQUALIFY_H_Y = int(FLOOR_Y - 0.75 * nominal_h)

        BONES = [
            (0, 1), (1, 2),
            (2, 3), (3, 4), (4, 5), (5, 6), (5, 7), (6, 7),
            (2, 8), (8, 9), (9, 10), (10, 11), (10, 12), (11, 12),
            (1, 13), (13, 14), (14, 15),
            (1, 16), (16, 17), (17, 18)
        ]

        ffmpeg_cmd = [
            "ffmpeg", "-y",
            "-f", "rawvideo",
            "-vcodec", "rawvideo",
            "-s", f"{WIDTH}x{HEIGHT}",
            "-pix_fmt", "rgb24",
            "-r", str(FPS),
            "-i", "-",
            "-c:v", "libx264",
            "-preset", "fast",
            "-crf", "18",
            "-pix_fmt", "yuv420p",
            output_mp4
        ]

        proc = subprocess.Popen(ffmpeg_cmd, stdin=subprocess.PIPE, stderr=subprocess.DEVNULL)

        header_fmt = "ifffiifififfff"
        header_size = struct.calcsize(header_fmt)
        joints_size = num_joints * 2 * 4

        particles = []
        screen_shake = 0
        push_text_timer = 0
        current_push_val = 0.0

        for frame_idx in range(total_frames):
            h_data = f.read(header_size)
            if len(h_data) < header_size:
                break
            (frame, curH, spine_ang, spine_ang_vel,
             lG, rG, active_push, strikes, slouch_t, rec_pushes,
             target_vx, walk_phase, com_vx, taper) = struct.unpack(header_fmt, h_data)

            j_data = f.read(joints_size)
            coords = struct.unpack(f"{num_joints * 2}f", j_data)
            pts = [(coords[i*2], coords[i*2+1]) for i in range(num_joints)]

            if abs(active_push) > 1e-3:
                push_text_timer = 35
                current_push_val = active_push
                screen_shake = 6
                for _ in range(25):
                    ang = random.uniform(-math.pi, math.pi)
                    spd = random.uniform(3.0, 9.0)
                    particles.append([pts[1][0], pts[1][1], spd * math.cos(ang), spd * math.sin(ang), 20, 20, (255, 180, 40)])

            surface.fill((10, 12, 18))
            ox = random.randint(-screen_shake, screen_shake) if screen_shake > 0 else 0
            oy = random.randint(-screen_shake, screen_shake) if screen_shake > 0 else 0
            if screen_shake > 0: screen_shake -= 1

            # Grid background
            for gx in range(0, WIDTH, 80):
                pygame.draw.line(surface, (18, 22, 32), (gx + ox, 60), (gx + ox, FLOOR_Y + oy), 1)
            for gy in range(80, FLOOR_Y, 60):
                pygame.draw.line(surface, (16, 20, 28), (40, gy + oy), (WIDTH - 40, gy + oy), 1)

            # Arena Boundary Walls
            pygame.draw.line(surface, (0, 150, 220), (WALL_LEFT + ox, 60), (WALL_LEFT + ox, FLOOR_Y + oy), 2)
            pygame.draw.line(surface, (0, 150, 220), (WALL_RIGHT + ox, 60), (WALL_RIGHT + ox, FLOOR_Y + oy), 2)

            # Patrol Turnaround Markers (x=250 and x=950)
            for dot_y in range(120, FLOOR_Y, 15):
                pygame.draw.line(surface, (255, 180, 40), (PATROL_LEFT + ox, dot_y + oy), (PATROL_LEFT + ox, dot_y + 8 + oy), 1)
                pygame.draw.line(surface, (255, 180, 40), (PATROL_RIGHT + ox, dot_y + oy), (PATROL_RIGHT + ox, dot_y + 8 + oy), 1)
            lbl_pl = font_small.render("PATROL TURN (X=250)", True, (255, 180, 40))
            surface.blit(lbl_pl, (PATROL_LEFT - lbl_pl.get_width() // 2 + ox, FLOOR_Y - 24 + oy))
            lbl_pr = font_small.render("PATROL TURN (X=950)", True, (255, 180, 40))
            surface.blit(lbl_pr, (PATROL_RIGHT - lbl_pr.get_width() // 2 + ox, FLOOR_Y - 24 + oy))

            # Target nominal height and Disqualification line
            for dot_x in range(WALL_LEFT, WALL_RIGHT, 20):
                pygame.draw.line(surface, (0, 200, 255), (dot_x + ox, TARGET_H_Y + oy), (dot_x + 10 + ox, TARGET_H_Y + oy), 2)
            lbl_tgt = font_small.render("H_NOMINAL (224 PX STANDING)", True, (0, 200, 255))
            surface.blit(lbl_tgt, (WALL_RIGHT - lbl_tgt.get_width(), TARGET_H_Y - 14 + oy))

            for dot_x in range(WALL_LEFT, WALL_RIGHT, 20):
                pygame.draw.line(surface, (255, 60, 60), (dot_x + ox, DISQUALIFY_H_Y + oy), (dot_x + 10 + ox, DISQUALIFY_H_Y + oy), 1)
            lbl_disq = font_small.render("DISQUALIFICATION LIMIT (< 0.75 H_NOMINAL FOR 2.0s)", True, (255, 80, 80))
            surface.blit(lbl_disq, (WALL_RIGHT - lbl_disq.get_width(), DISQUALIFY_H_Y + 4 + oy))

            # Floor with Coulomb static/kinetic sheen
            pygame.draw.line(surface, (60, 85, 120), (40, FLOOR_Y + oy), (WIDTH - 40, FLOOR_Y + oy), 4)
            pygame.draw.line(surface, (0, 220, 255), (40, FLOOR_Y + 3 + oy), (WIDTH - 40, FLOOR_Y + 3 + oy), 1)

            # Push Impact Particles
            for p in particles:
                p[0] += p[2]; p[1] += p[3]; p[4] -= 1
                rad = max(1, int(3 * (p[4] / p[5])))
                pygame.draw.circle(surface, p[6], (int(p[0]) + ox, int(p[1]) + oy), rad)
            particles = [p for p in particles if p[4] > 0]

            # Impulse visual
            if push_text_timer > 0:
                push_text_timer -= 1
                p_dir = ">>>" if current_push_val > 0 else "<<<"
                txt_p = font_large.render(f"{p_dir} IMPULSE: {int(abs(current_push_val))} N {p_dir}", True, (255, 200, 40))
                surface.blit(txt_p, (WIDTH // 2 - txt_p.get_width() // 2, 115))
                arrow_start = (int(pts[1][0] - (160 if current_push_val > 0 else -160)), int(pts[1][1]))
                arrow_end = (int(pts[1][0]), int(pts[1][1]))
                pygame.draw.line(surface, (255, 200, 40), arrow_start, arrow_end, 4)
                pygame.draw.circle(surface, (255, 240, 80), arrow_end, 7)

            # Draw Stickman Bones
            tilt_deg = abs(spine_ang * (180.0 / math.pi))
            spine_color = (0, 240, 255) if tilt_deg < 15.0 else ((255, 180, 40) if tilt_deg < 35.0 else (255, 60, 60))

            for b1, b2 in BONES:
                pA = (int(pts[b1][0]) + ox, int(pts[b1][1]) + oy)
                pB = (int(pts[b2][0]) + ox, int(pts[b2][1]) + oy)

                is_spine = (b1 == 1 and b2 == 2)
                is_lleg = b1 in [2, 3, 4, 5, 6] and b2 in [3, 4, 5, 6, 7]
                is_rleg = b1 in [2, 8, 9, 10, 11] and b2 in [8, 9, 10, 11, 12]
                is_lfoot = (b1 == 6 and b2 == 7)
                is_rfoot = (b1 == 11 and b2 == 12)

                if is_spine:
                    col = spine_color
                    thick = 6
                elif is_lfoot:
                    col = (0, 255, 120) if lG else (255, 220, 40)
                    thick = 5
                elif is_rfoot:
                    col = (0, 255, 120) if rG else (255, 220, 40)
                    thick = 5
                elif is_lleg:
                    col = (0, 220, 255) # Cyan Left Leg
                    thick = 4
                elif is_rleg:
                    col = (130, 170, 220) # Slate Blue Right Leg
                    thick = 4
                else:
                    col = (200, 220, 240) # Arms & braces
                    thick = 3

                pygame.draw.line(surface, (0, 0, 0), pA, pB, thick + 2)
                pygame.draw.line(surface, col, pA, pB, thick)

            # Draw Joints
            for j in range(num_joints):
                if j == 0: continue
                jp = (int(pts[j][0]) + ox, int(pts[j][1]) + oy)
                jcol = (0, 255, 200) if j in [5, 6, 7, 10, 11, 12] else (0, 180, 255)
                pygame.draw.circle(surface, (0, 0, 0), jp, 4)
                pygame.draw.circle(surface, jcol, jp, 3)

            # Alternating Foot Contact Markers (Green = Planted Drive, Yellow = Airborne Step)
            l_foot_pt = (int(pts[7][0]) + ox, int(pts[7][1]) + oy)
            r_foot_pt = (int(pts[12][0]) + ox, int(pts[12][1]) + oy)

            l_col = (0, 255, 120) if lG else (255, 220, 40)
            r_col = (0, 255, 120) if rG else (255, 220, 40)

            # Left Foot Marker
            pygame.draw.circle(surface, l_col, l_foot_pt, 7, 2)
            pygame.draw.circle(surface, l_col, l_foot_pt, 4)
            lbl_l_state = "PLANTED" if lG else "AIRBORNE"
            lbl_l_foot = font_small.render(f"L: {lbl_l_state}", True, l_col)
            surface.blit(lbl_l_foot, (l_foot_pt[0] - lbl_l_foot.get_width() // 2, l_foot_pt[1] + 8))

            # Right Foot Marker
            pygame.draw.circle(surface, r_col, r_foot_pt, 7, 2)
            pygame.draw.circle(surface, r_col, r_foot_pt, 4)
            lbl_r_state = "PLANTED" if rG else "AIRBORNE"
            lbl_r_foot = font_small.render(f"R: {lbl_r_state}", True, r_col)
            surface.blit(lbl_r_foot, (r_foot_pt[0] - lbl_r_foot.get_width() // 2, r_foot_pt[1] + 8))

            # Head with Glowing Visor
            head_p = (int(pts[0][0]) + ox, int(pts[0][1]) + oy)
            pygame.draw.circle(surface, (0, 0, 0), head_p, 16)
            pygame.draw.circle(surface, (0, 220, 255), head_p, 14, 2)
            pygame.draw.circle(surface, (230, 245, 255), head_p, 12)
            v_dir = 1 if target_vx > 0 else -1
            visor_center = (head_p[0] + v_dir * 5, head_p[1] - 2)
            pygame.draw.circle(surface, (0, 180, 255), visor_center, 4)

            # Target Velocity Arrow Above Agent
            tgt_arrow_y = head_p[1] - 40
            tgt_dir_sign = 1 if target_vx > 0 else -1
            arr_start_x = head_p[0] - tgt_dir_sign * 35
            arr_end_x = head_p[0] + tgt_dir_sign * 35

            pygame.draw.line(surface, (0, 240, 255), (arr_start_x, tgt_arrow_y), (arr_end_x, tgt_arrow_y), 3)
            # Arrow head
            head_tip = (arr_end_x, tgt_arrow_y)
            head_top = (arr_end_x - tgt_dir_sign * 10, tgt_arrow_y - 6)
            head_bot = (arr_end_x - tgt_dir_sign * 10, tgt_arrow_y + 6)
            pygame.draw.polygon(surface, (0, 240, 255), [head_tip, head_top, head_bot])

            txt_tgt_vel = font_small.render(f"TARGET: {target_vx:+4.0f} px/s", True, (0, 240, 255))
            surface.blit(txt_tgt_vel, (head_p[0] - txt_tgt_vel.get_width() // 2, tgt_arrow_y - 16))

            # -----------------------------------------------------------------
            # HUD TELEMETRY OVERLAYS
            # -----------------------------------------------------------------
            # 1. Top Center: Assist Taper Live Gauge
            taper_pct = taper * 100.0
            self_support_pct = (1.0 - taper) * 100.0

            gauge_box = pygame.Rect(WIDTH // 2 - 280, 14, 560, 48)
            pygame.draw.rect(surface, (14, 18, 26), gauge_box, border_radius=12)
            gauge_border_col = (0, 255, 180) if self_support_pct > 70.0 else (0, 200, 255)
            pygame.draw.rect(surface, gauge_border_col, gauge_box, width=2, border_radius=12)

            txt_taper_title = font_gauge.render(f"ASSIST TAPER: {taper_pct:4.1f}% | SELF-SUPPORT: {self_support_pct:4.1f}%", True, (255, 255, 255))
            surface.blit(txt_taper_title, (WIDTH // 2 - txt_taper_title.get_width() // 2, 20))

            # Bar inside gauge
            bar_w = 460
            bar_h = 8
            bx = WIDTH // 2 - bar_w // 2
            by = 46
            pygame.draw.rect(surface, (30, 36, 48), (bx, by, bar_w, bar_h), border_radius=4)
            fill_w = int(bar_w * (self_support_pct / 100.0))
            if fill_w > 0:
                pygame.draw.rect(surface, (0, 255, 160), (bx, by, fill_w, bar_h), border_radius=4)

            # 2. Left HUD: Gait & Footwork Telemetry
            box_gait = pygame.Rect(20, 20, 310, 180)
            pygame.draw.rect(surface, (12, 16, 24), box_gait, border_radius=14)
            pygame.draw.rect(surface, (0, 200, 255, 60), box_gait, width=1, border_radius=14)

            surface.blit(font_large.render("GAIT & FOOTWORK TELEMETRY", True, (0, 220, 255)), (34, 28))

            speed_col = (0, 255, 180) if abs(com_vx) > 70.0 else (255, 200, 60)
            surface.blit(font_mono.render(f"COM Velocity  : {com_vx:+6.1f} px/s", True, speed_col), (34, 54))
            surface.blit(font_mono.render(f"Target Speed  : {target_vx:+6.1f} px/s", True, (200, 220, 240)), (34, 74))

            gait_state = "LEFT SWING" if math.sin(walk_phase) > 0.0 else "RIGHT SWING"
            surface.blit(font_mono.render(f"CPG Phase     : {walk_phase:.2f} rad ({gait_state})", True, (0, 220, 255)), (34, 94))

            l_clr = max(0.0, FLOOR_Y - pts[7][1])
            r_clr = max(0.0, FLOOR_Y - pts[12][1])
            l_str = "(PLANTED)" if lG else "(SWING)"
            r_str = "(PLANTED)" if rG else "(SWING)"
            surface.blit(font_mono.render(f"L Foot Clear  : {l_clr:4.1f} px {l_str}", True, (0, 240, 180) if l_clr >= 12.0 else (180, 200, 220)), (34, 114))
            surface.blit(font_mono.render(f"R Foot Clear  : {r_clr:4.1f} px {r_str}", True, (0, 240, 180) if r_clr >= 12.0 else (180, 200, 220)), (34, 134))
            surface.blit(font_mono.render("Stride Rhythm : 1.30 Hz Alternating", True, (150, 180, 210)), (34, 154))
            stiff_val = 0.35 + 0.25 * taper
            surface.blit(font_mono.render(f"Leg Stiffness : {stiff_val:.2f} PBD Softened", True, (255, 200, 80)), (34, 172))

            # 3. Right HUD: Posture Watchdog & Cheat Removal Telemetry
            box_wd = pygame.Rect(WIDTH - 330, 20, 310, 180)
            pygame.draw.rect(surface, (12, 16, 24), box_wd, border_radius=14)
            pygame.draw.rect(surface, (255, 80, 80, 60), box_wd, width=1, border_radius=14)

            surface.blit(font_large.render("POSTURE WATCHDOG (TAPER)", True, (255, 100, 100)), (WIDTH - 316, 28))

            h_pct = (curH / nominal_h) * 100.0
            h_col = (0, 255, 180) if h_pct >= 91.5 else ((255, 200, 40) if h_pct >= 75.0 else (255, 60, 60))
            surface.blit(font_mono.render(f"Standing Height: {curH:5.1f} px ({h_pct:4.1f}%)", True, h_col), (WIDTH - 316, 54))
            surface.blit(font_mono.render(f"Spine Angle    : {math.degrees(spine_ang):+5.1f}° (UPRIGHT)", True, spine_color), (WIDTH - 316, 74))

            slouch_col = (255, 60, 60) if slouch_t > 1.0 else (200, 220, 240)
            surface.blit(font_mono.render(f"Slouch Watchdog: {slouch_t:4.2f}s / 2.00s", True, slouch_col), (WIDTH - 316, 94))
            surface.blit(font_mono.render(f"Ground Strikes : {strikes} / 2 (LIMIT)", True, (255, 80, 80) if strikes > 0 else (0, 220, 255)), (WIDTH - 316, 114))
            surface.blit(font_mono.render(f"Pushes Absorbed: {rec_pushes} Recovered", True, (0, 255, 180)), (WIDTH - 316, 134))
            surface.blit(font_mono.render(f"Skyhook Gyro   : {taper * 100.0:4.1f}% Active", True, (255, 160, 60)), (WIDTH - 316, 154))
            surface.blit(font_mono.render(f"Levitation Spr : {taper * 100.0:4.1f}% Active", True, (255, 160, 60)), (WIDTH - 316, 172))

            # Bottom progress bar
            prog_w = int((frame_idx / total_frames) * (WIDTH - 80))
            pygame.draw.line(surface, (30, 40, 55), (40, HEIGHT - 16), (WIDTH - 40, HEIGHT - 16), 2)
            pygame.draw.line(surface, (0, 220, 255), (40, HEIGHT - 16), (40 + prog_w, HEIGHT - 16), 2)

            raw_rgb = pygame.image.tobytes(surface, "RGB")
            proc.stdin.write(raw_rgb)

        proc.stdin.close()
        proc.wait()
        print(f"Successfully generated {output_mp4} ({os.path.getsize(output_mp4) / 1024 / 1024:.2f} MB)")
        return True

if __name__ == "__main__":
    render_showcase()
"""
with open("render_walking.py", "w") as f:
    f.write(py_renderer_source)

# 5. Compile C++ simulation
print("[5/6] Compiling stage2_walking.cpp with g++ -O3 -fopenmp -std=c++17...")
compile_cmd = ["g++", "-O3", "-fopenmp", "-std=c++17", "stage2_walking.cpp", "-o", "stage2_walking"]
res = subprocess.run(compile_cmd)
if res.returncode != 0:
    print("Compilation failed!")
    sys.exit(1)
print("Compilation successful!")

# 6. Execute evolutionary training
print("[6/6] Launching parallel evolutionary training (200 generations)...")
subprocess.run(["./stage2_walking", "200"])

# 7. Render showcase video
print("Exporting 60 FPS Locomotion Showcase video with Live Assist Taper Gauge...")
subprocess.run([sys.executable, "render_walking.py"])

print("\n" + "=" * 70)
print("STAGE 2 TRAINING & SHOWCASE VIDEO COMPLETE!")
print("Saved files:")
print(" - Champion Weights : ./training_data/stage2_walking_best.bin")
print(" - Training Logs    : ./training_data/stage2_walking_log.csv")
print(" - Showcase Video   : ./training_data/stage2_walking_showcase.mp4")
print("=" * 70)

# Auto-shutdown runtime to preserve Colab compute units
try:
    from google.colab import runtime
    print("Auto-releasing Colab runtime to preserve compute units...")
    runtime.unassign()
except Exception:
    pass
