1DOLLY 72PACKAGE airtime3REQUIRES HOST runtime@04REQUIRES HOST display@05REQUIRES HOST input@06REQUIRES HOST audio@07REQUIRES HOST touch@08REQUIRES HOST download@09REQUIRES HOST http@01011# Airtime: Foundry, its agent overlay and viewer, with their sources and the12# makefile that builds the game again (cc and raylib).13FROM /v0.1.5/demos/slopyard/Dollyfile-gamedev-sdk f90577bcd0d5c3eb3cd989913f41cdd3685f769522066bd205ee89e22a26b6d114USE /v0.1.5/demos/javascript/Dollyfile-janis 11d808dce535176b226c70d32e33b6050969f00ace9b0c22578d93a91a3744e815USE /v0.1.5/demos/sdl2/Dollyfile-sdl2 cd864c7c3220d915822ca58199fe4df7c7f2e5fb21d34c77a6aa86f77ed466dc16REQUIRES TOOL cc17REQUIRES TOOL make18REQUIRES TOOL c++19REQUIRES TOOL tar20REQUIRES TOOL rm21REQUIRES HEADER cpp22REQUIRES HEADER quickjs-runner23REQUIRES HEADER sdl224REQUIRES LIB dolly-js25REQUIRES LIB quickjs26REQUIRES LIB SDL22728SOURCE /v0.1.5/dist/static/bhop/source.tar ec72dcd9268eaad9d386b9e877b3773e419c0cc4eac66e03bc727e17036afdc4 /tmp/bhop-source.tar29SOURCE /v0.1.5/dist/static/default/stb_truetype.h ecd30b05e0dd4fea3a13c26810dd9e1992dc379049482c393d5a19e6b5090aab /tmp/bhop-stb/stb_truetype.h30SLOP tar -xf /tmp/bhop-source.tar -C /3132FILE /usr/src/dolly/bhop/bhop-movement.h33    #ifndef DOLLY_BHOP_MOVEMENT_H34    #define DOLLY_BHOP_MOVEMENT_H35    #include <math.h>36    #include <string.h>37    38    #define BH_STEP 0.01f39    #define BH_RADIUS 16.0f40    #define BH_HEIGHT 72.0f41    #define BH_GRAVITY 800.0f42    #define BH_RUN_SPEED 250.0f43    44    typedef struct { float low[3], high[3]; } bh_box;45    typedef struct {46      float position[3], velocity[3];47      int ground, jump_queue, jumps;48    } bh_player;49    typedef struct { float fraction, normal[3]; int box; } bh_trace;50    51    static float bh_speed(const bh_player *player) {52      return hypotf(player->velocity[0], player->velocity[2]);53    }54    55    // Original implementation of GoldSrc's projection-limited acceleration rule.56    // Only the air projection is capped at 30; the acceleration uses full wishspeed.57    static void bh_accelerate(bh_player *player, float yaw, float forward, float side, int air) {58      float length = hypotf(forward, side);59      if (length == 0) return;60      float direction[3] = {61        (sinf(yaw) * forward + cosf(yaw) * side) / length, 0,62        (-cosf(yaw) * forward + sinf(yaw) * side) / length63      };64      float projection = player->velocity[0] * direction[0] + player->velocity[2] * direction[2];65      float available = (air ? 30.0f : BH_RUN_SPEED) - projection;66      float acceleration = fminf(available, (air ? 10.0f : 5.0f) * BH_RUN_SPEED * BH_STEP);67      if (acceleration <= 0) return;68      player->velocity[0] += direction[0] * acceleration;69      player->velocity[2] += direction[2] * acceleration;70    }71    72    static bh_trace bh_sweep(const float position[3], const float delta[3],73                             const bh_box *boxes, int count) {74      bh_trace best = {.fraction = 1, .box = -1};75      for (int box = 0; box < count; ++box) {76        if (boxes[box].low[0] >= boxes[box].high[0] || boxes[box].low[1] >= boxes[box].high[1] ||77            boxes[box].low[2] >= boxes[box].high[2]) continue;78        float enter = -INFINITY, leave = INFINITY, normal[3] = {0};79        int intersects = 1;80        for (int axis = 0; axis < 3; ++axis) {81          float low = boxes[box].low[axis] - (axis == 1 ? BH_HEIGHT : BH_RADIUS);82          float high = boxes[box].high[axis] + (axis == 1 ? 0 : BH_RADIUS);83          if (fabsf(delta[axis]) < 1e-8f) {84            if (position[axis] <= low || position[axis] >= high) { intersects = 0; break; }85            continue;86          }87          float first = (low - position[axis]) / delta[axis];88          float last = (high - position[axis]) / delta[axis];89          float sign = -1;90          if (first > last) { float swap = first; first = last; last = swap; sign = 1; }91          if (first > enter) {92            enter = first;93            memset(normal, 0, sizeof(normal));94            normal[axis] = sign;95          }96          leave = fminf(leave, last);97          if (enter > leave) { intersects = 0; break; }98        }99        if (intersects && enter >= 0 && enter < best.fraction && leave >= 0) {100          best.fraction = enter;101          best.box = box;102          memcpy(best.normal, normal, sizeof(normal));103        }104      }105      return best;106    }107    108    static void bh_slide(bh_player *player, const bh_box *boxes, int count) {109      float remaining = BH_STEP;110      for (int bump = 0; bump < 4 && remaining > 0; ++bump) {111        float delta[3];112        for (int axis = 0; axis < 3; ++axis) delta[axis] = player->velocity[axis] * remaining;113        bh_trace hit = bh_sweep(player->position, delta, boxes, count);114        for (int axis = 0; axis < 3; ++axis)115          player->position[axis] += delta[axis] * hit.fraction + hit.normal[axis] * 0.01f;116        if (hit.box < 0) break;117        remaining *= 1 - hit.fraction;118        float inward = 0;119        for (int axis = 0; axis < 3; ++axis) inward += player->velocity[axis] * hit.normal[axis];120        if (inward < 0) for (int axis = 0; axis < 3; ++axis)121          player->velocity[axis] -= inward * hit.normal[axis];122      }123    }124    125    static void bh_ground(bh_player *player, const bh_box *boxes, int count) {126      player->ground = -1;127      if (player->velocity[1] > 0) return;128      const float down[3] = {0, -2, 0};129      bh_trace floor = bh_sweep(player->position, down, boxes, count);130      if (floor.box >= 0 && floor.normal[1] > 0.7f) {131        player->position[1] -= 2 * floor.fraction - 0.01f;132        player->velocity[1] = 0;133        player->ground = floor.box;134      }135    }136    137    static void bh_tick(bh_player *player, float yaw, float forward, float side,138                        const bh_box *boxes, int count) {139      bh_ground(player, boxes, count);140      if (player->jump_queue > 0 && player->ground >= 0) {141        player->velocity[1] = sqrtf(2 * BH_GRAVITY * 45);142        player->ground = -1;143        player->jump_queue = 0;144        ++player->jumps;145      }146      if (player->jump_queue > 0) --player->jump_queue;147      if (player->ground >= 0) {148        float speed = bh_speed(player);149        if (speed > 0) {150          float retained = fmaxf(0, speed - fmaxf(75, speed) * 4 * BH_STEP) / speed;151          player->velocity[0] *= retained;152          player->velocity[2] *= retained;153        }154      }155      bh_accelerate(player, yaw, forward, side, player->ground < 0);156      if (player->ground >= 0 && bh_speed(player) > BH_RUN_SPEED) {157        float scale = BH_RUN_SPEED / bh_speed(player);158        player->velocity[0] *= scale;159        player->velocity[2] *= scale;160      }161      player->velocity[1] -= BH_GRAVITY * BH_STEP / 2;162      bh_slide(player, boxes, count);163      player->velocity[1] -= BH_GRAVITY * BH_STEP / 2;164      bh_ground(player, boxes, count);165    }166    #endif167168FILE /usr/src/dolly/bhop/bhop-course.h169    #ifndef DOLLY_BHOP_COURSE_H170    #define DOLLY_BHOP_COURSE_H171    #include "bhop-movement.h"172    173    enum { BH_PADS = 33, BH_PAD_DELAY = 10, BH_PAD_RETURN = 200 };174    typedef struct {175      bh_box pads[BH_PADS];176      int cooldown[BH_PADS], collapses;177    } bh_course;178    179    static int bh_checkpoint(int pad) { return pad >= 0 && pad < BH_PADS && pad % 8 == 0; }180    static void bh_course_init(bh_course *course) {181      const float x[BH_PADS] = {0, 0, 48, 110, 160, 180, 140, 70, 0,182        -60, -130, -180, -160, -100, -20, 60, 120,183        180, 220, 190, 120, 40, -40, -120, -180,184        -200, -150, -80, 0, 80, 150, 190, 180};185      const float top[BH_PADS] = {0, 0, 0, 16, 16, 32, 32, 48, 48,186        32, 16, 0, -16, -16, 0, 0, 0,187        0, 16, 16, 32, 32, 48, 48, 64,188        64, 80, 80, 96, 96, 112, 112, 128};189      memset(course, 0, sizeof(*course));190      float z = 0;191      for (int i = 0; i < BH_PADS; ++i) {192        int section = i ? (i - 1) / 8 : 0;193        if (i) z -= i == 1 ? 400 : 220 + section * 10;194        float width = i == 0 ? 512 : bh_checkpoint(i) ? 224 : 88 - section * 8;195        float depth = bh_checkpoint(i) ? (i == 0 ? 512 : 256) : width;196        course->pads[i] = (bh_box){{x[i] - width / 2, top[i] - 32, z - depth / 2},197          {x[i] + width / 2, top[i], z + depth / 2}};198      }199    }200    static void bh_course_tick(bh_course *course, bh_player *player, float yaw, float forward, float side) {201      bh_box solid[BH_PADS];202      memcpy(solid, course->pads, sizeof(solid));203      for (int i = 0; i < BH_PADS; ++i) {204        if (course->cooldown[i] && --course->cooldown[i] == BH_PAD_RETURN) ++course->collapses;205        if (course->cooldown[i] > 0 && course->cooldown[i] <= BH_PAD_RETURN)206          solid[i].high[1] = solid[i].low[1];207      }208      bh_tick(player, yaw, forward, side, solid, BH_PADS);209      int pad = player->ground;210      if (pad >= 0 && !bh_checkpoint(pad) && !course->cooldown[pad])211        course->cooldown[pad] = BH_PAD_DELAY + BH_PAD_RETURN;212    }213    #endif214215FILE /usr/src/dolly/bhop/bhop-check.c216    #include "bhop-course.h"217    #include <stdio.h>218    219    #define CHECK(condition) do { if (!(condition)) { \220      fprintf(stderr, "bhop movement check failed at line %d: %s\n", __LINE__, #condition); return 1; \221    } } while (0)222    223    int main(void) {224      bh_player player = {.velocity = {0, 0, -300}};225      bh_accelerate(&player, 0, 1, 0, 1);226      CHECK(player.velocity[2] == -300);227      bh_accelerate(&player, 0, 0, 1, 1);228      CHECK(player.velocity[0] == 25 && player.velocity[2] == -300);229      bh_accelerate(&player, 0, 0, 1, 1);230      CHECK(player.velocity[0] == 30 && bh_speed(&player) > 300);231      player = (bh_player){0};232      bh_accelerate(&player, 0, 1, 1, 1);233      CHECK(fabsf(bh_speed(&player) - 25) < 0.001f);234    235      bh_player strafe = {.velocity = {0, 0, -300}}, held = strafe;236      for (int tick = 0; tick < 100; ++tick) {237        float yaw = atan2f(strafe.velocity[2], strafe.velocity[0]) + acosf(5 / bh_speed(&strafe));238        bh_accelerate(&strafe, yaw, 0, 1, 1);239        bh_accelerate(&held, 0, 0, 1, 1);240      }241      CHECK(bh_speed(&strafe) > 420 && bh_speed(&held) < 302);242      printf("bhop: synchronized strafe %.3f u/s; fixed-direction strafe %.3f u/s\n",243        bh_speed(&strafe), bh_speed(&held));244    245      const bh_box floor = {{-10000, -100, -10000}, {10000, 0, 10000}};246      player = (bh_player){.position = {0, 0.01f, 0}, .velocity = {100, 0, 0}};247      bh_tick(&player, 0, 0, 0, &floor, 1);248      CHECK(fabsf(player.velocity[0] - 96) < 0.001f && player.ground == 0);249      player = (bh_player){.position = {0, 0.01f, 0}, .velocity = {600, 0, 0}, .jump_queue = 2};250      float peak = 0;251      for (int tick = 0; tick < 65; ++tick) {252        bh_tick(&player, 0, 0, 0, &floor, 1);253        peak = fmaxf(peak, player.position[1]);254        CHECK(fabsf(player.velocity[0] - 600) < 0.001f);255      }256      CHECK(peak > 44.9f && peak < 45.1f && player.jumps == 1);257      while (player.ground < 0) bh_tick(&player, 0, 0, 0, &floor, 1);258      player.jump_queue = 2;259      bh_tick(&player, 0, 0, 0, &floor, 1);260      CHECK(player.jumps == 2 && player.ground == -1 && player.velocity[0] == 600);261      for (int tick = 0; tick < 100; ++tick) bh_tick(&player, 0, 0, 0, &floor, 1);262      CHECK(player.jumps == 2 && player.ground == 0 && bh_speed(&player) < 250);263    264      const bh_box wall = {{100, -100, -100}, {110, 200, 100}};265      player = (bh_player){.position = {60, 10, 0}, .velocity = {5000, 0, -100}};266      bh_slide(&player, &wall, 1);267      CHECK(player.position[0] < 84 && player.position[0] > 83.9f);268      CHECK(player.velocity[0] == 0 && player.velocity[2] == -100);269      CHECK(fabsf(player.position[2] + 1) < 0.001f);270      puts("bhop: projection cap, full-wish acceleration, diagonal input, friction, 45-unit jump, landing momentum and swept wall sliding passed");271    272      bh_course course;273      bh_course_init(&course);274      bh_box pad = course.pads[1];275      player = (bh_player){.position = {(pad.low[0] + pad.high[0]) / 2, pad.high[1] + 0.01f,276        (pad.low[2] + pad.high[2]) / 2}};277      bh_course_tick(&course, &player, 0, 0, 0);278      CHECK(course.cooldown[1] == BH_PAD_DELAY + BH_PAD_RETURN && player.ground == 1);279      for (int i = 1; i < BH_PAD_DELAY; ++i) {280        bh_course_tick(&course, &player, 0, 0, 0);281        CHECK(player.ground == 1);282      }283      bh_course_tick(&course, &player, 0, 0, 0);284      CHECK(player.ground == -1 && course.collapses == 1);285      for (int i = 0; i < BH_PAD_RETURN; ++i) bh_course_tick(&course, &player, 0, 0, 0);286      CHECK(course.cooldown[1] == 0 && player.position[1] < pad.low[1]);287      player = (bh_player){.position = {0, 0.01f, 0}};288      for (int i = 0; i < 250; ++i) bh_course_tick(&course, &player, 0, 0, 0);289      CHECK(player.ground == 0 && course.cooldown[0] == 0);290      bh_course_init(&course);291      player = (bh_player){.position = {0, 0.01f, -400}};292      bh_course_tick(&course, &player, 0, 0, 0);293      player.jump_queue = 2;294      for (int i = 0; i < BH_PAD_DELAY + 1; ++i) bh_course_tick(&course, &player, 0, 0, 0);295      CHECK(player.jumps == 1 && player.position[1] > 20 && course.collapses == 1);296      puts("bhop: 100ms collapse, 2s return, safe checkpoints and immediate jumping passed");297    298      float fastest = 0;299      for (int next = 1; next < BH_PADS; ++next) {300        bh_course_init(&course);301        bh_box start = course.pads[next - 1], end = course.pads[next];302        float sx = (start.low[0] + start.high[0]) / 2, sz = (start.low[2] + start.high[2]) / 2;303        float ex = (end.low[0] + end.high[0]) / 2, ez = (end.low[2] + end.high[2]) / 2;304        // Check each gap independently with real swept physics, not just the ballistic formula.305        // Checkpoints provide a run-up; ordinary pads must be crossed without stopping.306        if (bh_checkpoint(next - 1)) sz = start.low[2] + 8;307        float rise = end.high[1] - start.high[1];308        float flight = (sqrtf(2 * BH_GRAVITY * 45) + sqrtf(2 * BH_GRAVITY * (45 - rise))) / BH_GRAVITY;309        float vx = (ex - sx) / flight, vz = (ez - sz) / flight;310        float speed = hypotf(vx, vz);311        fastest = fmaxf(fastest, speed);312        CHECK(speed < 440 && (!bh_checkpoint(next - 1) || speed < BH_RUN_SPEED));313        player = (bh_player){.position = {sx, start.high[1] + 0.01f, sz},314          .velocity = {vx, 0, vz}, .jump_queue = 2};315        int landed = -1;316        for (int tick = 0; tick < 100 && landed < 0; ++tick) {317          bh_course_tick(&course, &player, 0, 0, 0);318          if (player.ground >= 0) landed = player.ground;319        }320        CHECK(landed == next);321      }322      printf("bhop: all 32 individual gaps reachable, maximum center-landing speed %.1f u/s\n", fastest);323      return 0;324    }325326FILE /usr/src/dolly/bhop/bhop.c327    #define _POSIX_C_SOURCE 200809L328    #include "bhop-course.h"329    #include "agent/input.h"330    #include <dolly/raylib.h>331    #include <raymath.h>332    #include <rlgl.h>333    #include <errno.h>334    #include <stdio.h>335    #include <stdlib.h>336    #include <time.h>337    338    enum { PLATFORM_COUNT = BH_PADS };339    static const Color SKY = {20, 28, 38, 255}, INK = {231, 235, 234, 255};340    static const Color ACCENT = {255, 185, 82, 255}, CYAN = {84, 218, 205, 255};341    static const Color MUTED = {145, 159, 172, 255};342    static const char *record_path = "/workspace/bhop-foundry-record.txt";343    344    typedef struct {345      bh_player player;346      bh_course course;347      int forward, backward, left, right, turn_left, turn_right, looking, quit;348      int checkpoint, furthest, deaths, finished, practice, ticks, jumps, air_ticks, gain_ticks;349      float yaw, pitch, elapsed, best, peak_speed, landing_speed;350    } game;351    352    static double seconds(void) {353      struct timespec now;354      return clock_gettime(CLOCK_MONOTONIC, &now) == 0355        ? now.tv_sec + now.tv_nsec / 1e9 : 0;356    }357    static Color blend(Color from, Color to, float amount) {358      amount = Clamp(amount, 0, 1);359      return (Color){from.r + (to.r - from.r) * amount, from.g + (to.g - from.g) * amount,360        from.b + (to.b - from.b) * amount, 255};361    }362    static Vector3 center(bh_box box) {363      return (Vector3){(box.low[0] + box.high[0]) / 2, (box.low[1] + box.high[1]) / 2,364        (box.low[2] + box.high[2]) / 2};365    }366    static int is_checkpoint(int index) { return bh_checkpoint(index); }367    static void clear_keys(game *state) {368      state->forward = state->backward = state->left = state->right = 0;369      state->turn_left = state->turn_right = 0;370      state->player.jump_queue = 0;371    }372    static void respawn(game *state, int restart) {373      if (restart) {374        state->checkpoint = state->furthest = state->deaths = state->finished = 0;375        state->practice = 0;376        state->ticks = state->jumps = state->air_ticks = state->gain_ticks = 0;377        state->elapsed = state->peak_speed = state->landing_speed = 0;378      }379      memset(state->course.cooldown, 0, sizeof(state->course.cooldown));380      if (restart) state->course.collapses = 0;381      bh_box pad = state->course.pads[state->checkpoint];382      state->player = (bh_player){.position = {(pad.low[0] + pad.high[0]) / 2,383        pad.high[1] + 0.01f, (pad.low[2] + pad.high[2]) / 2 + (state->checkpoint == 0 ? 115 : 40)},384        .ground = state->checkpoint};385      Vector3 here = center(pad), next = center(state->course.pads[state->checkpoint + (state->checkpoint < BH_PADS - 1)]);386      state->yaw = atan2f(next.x - here.x, here.z - next.z);387      state->pitch = -0.06f;388      clear_keys(state);389    }390    static void tick(game *state) {391      if (!state->looking || state->finished) return;392      state->yaw += (state->turn_right - state->turn_left) * BH_STEP * 1.6f;393      int grounded = state->player.ground >= 0;394      int jumps = state->player.jumps;395      float before = bh_speed(&state->player);396      bh_course_tick(&state->course, &state->player, state->yaw, state->forward - state->backward,397        state->right - state->left);398      state->jumps += state->player.jumps - jumps;399      ++state->ticks;400      float speed = bh_speed(&state->player);401      state->peak_speed = fmaxf(state->peak_speed, speed);402      if (!grounded && (state->left || state->right || state->forward || state->backward)) {403        ++state->air_ticks;404        if (speed > before + 0.001f) ++state->gain_ticks;405      }406      if (state->player.position[2] < -180 || state->elapsed > 0) state->elapsed += BH_STEP;407      int pad = state->player.ground;408      if (!grounded && pad >= 0) state->landing_speed = speed;409      if (pad > state->furthest) state->furthest = pad;410      if (pad > state->checkpoint && is_checkpoint(pad)) state->checkpoint = pad;411      if (pad == PLATFORM_COUNT - 1 && !state->finished) {412        state->finished = 1;413        if (!state->practice && (!state->best || state->elapsed < state->best)) {414          state->best = state->elapsed;415          FILE *record = fopen(record_path, "w");416          if (record) { fprintf(record, "%.3f\n", state->best); fclose(record); }417        }418      }419      if (state->player.position[1] < -200) { ++state->deaths; respawn(state, 0); }420    }421    static void input(game *state, const dolly_input_event *event) {422      if (event->type == DOLLY_INPUT_EVENT_POINTER_CAPTURE) {423        state->looking = event->action != 0;424        clear_keys(state);425      } else if (event->type == DOLLY_INPUT_EVENT_POINTER_MOTION && state->looking) {426        state->yaw += event->x * 0.0000011f;427        state->pitch = Clamp(state->pitch - event->y * 0.0000011f, -1.48f, 1.48f);428      } else if (event->type == DOLLY_INPUT_EVENT_FOCUS && !event->action) {429        state->looking = 0;430        clear_keys(state);431      } else if (event->type == DOLLY_INPUT_EVENT_DROPPED) {432        clear_keys(state);433      } else if (event->type == DOLLY_INPUT_EVENT_SCROLL && state->looking && event->y != 0) {434        state->player.jump_queue = 2;435      } else if (event->type == DOLLY_INPUT_EVENT_KEY) {436        int down = event->action != DOLLY_KEY_ACTION_RELEASE;437        if (dolly_raylib_code_is(event, "KeyW")) state->forward = down;438        if (dolly_raylib_code_is(event, "KeyS")) state->backward = down;439        if (dolly_raylib_code_is(event, "KeyA")) state->left = down;440        if (dolly_raylib_code_is(event, "KeyD")) state->right = down;441        if (dolly_raylib_code_is(event, "ArrowLeft")) state->turn_left = down;442        if (dolly_raylib_code_is(event, "ArrowRight")) state->turn_right = down;443        if (event->action == DOLLY_KEY_ACTION_PRESS) {444          if (dolly_raylib_code_is(event, "Space") && state->looking) state->player.jump_queue = 2;445          if (dolly_raylib_code_is(event, "KeyR")) respawn(state, 1);446          if (dolly_raylib_code_is(event, "KeyF")) { ++state->deaths; respawn(state, 0); }447          if (dolly_raylib_code_is(event, "KeyQ")) state->quit = 1;448          const char *sections[] = {"Digit1", "Digit2", "Digit3", "Digit4"};449          for (int i = 0; i < 4; ++i) if (dolly_raylib_code_is(event, sections[i])) {450            state->checkpoint = state->furthest = i * 8;451            state->finished = 0;452            state->practice = 1;453            state->elapsed = 0;454            respawn(state, 0);455          }456        }457      }458    }459    static void label(Font font, const char *message, float x, float y, float size, Color color) {460      DrawTextEx(font, message, (Vector2){x, y}, size, 0, color);461    }462    static void shaded_cube(Vector3 middle, Vector3 size, Color color, float fog) {463      static const int faces[6][4] = {{0, 4, 6, 2}, {1, 3, 7, 5}, {0, 1, 5, 4},464        {2, 6, 7, 3}, {0, 2, 3, 1}, {4, 5, 7, 6}};465      static const float light[] = {0.68f, 0.85f, 0.5f, 1, 0.72f, 0.9f};466      Vector3 corner[8];467      for (int i = 0; i < 8; ++i) corner[i] = (Vector3){middle.x + (i & 1 ? 0.5f : -0.5f) * size.x,468        middle.y + (i & 2 ? 0.5f : -0.5f) * size.y, middle.z + (i & 4 ? 0.5f : -0.5f) * size.z};469      for (int i = 0; i < 6; ++i) {470        Color shade = blend((Color){color.r * light[i], color.g * light[i], color.b * light[i], 255}, SKY, fog);471        DrawTriangle3D(corner[faces[i][0]], corner[faces[i][1]], corner[faces[i][2]], shade);472        DrawTriangle3D(corner[faces[i][0]], corner[faces[i][2]], corner[faces[i][3]], shade);473      }474    }475    static void platform(bh_box box, int index, int cooldown, float fade) {476      int hidden = cooldown > 0 && cooldown <= BH_PAD_RETURN;477      if (hidden) {478        int falling = BH_PAD_RETURN - cooldown;479        if (falling > 24) return;480        float drop = falling * falling * 0.65f;481        box.low[1] -= drop;482        box.high[1] -= drop;483        fade = fmaxf(fade, falling / 26.0f);484      }485      Vector3 middle = center(box);486      Color rim = blend(is_checkpoint(index) ? CYAN : cooldown ? (Color){255, 89, 58, 255} : ACCENT, SKY, fade);487      shaded_cube(middle, (Vector3){box.high[0] - box.low[0], box.high[1] - box.low[1], box.high[2] - box.low[2]},488        (Color){132, 144, 153, 255}, fade);489      DrawCube((Vector3){middle.x, box.high[1] - 3, middle.z},490        box.high[0] - box.low[0] + 1, 6, box.high[2] - box.low[2] + 1, rim);491      float y = box.high[1] + 0.07f;492      int columns = (int)ceilf((box.high[0] - box.low[0]) / 32);493      int rows = (int)ceilf((box.high[2] - box.low[2]) / 32);494      for (int row = 0; row < rows; ++row) for (int column = 0; column < columns; ++column) {495        float left = box.low[0] + column * 32, right = fminf(left + 32, box.high[0]);496        float back = box.low[2] + row * 32, front = fminf(back + 32, box.high[2]);497        Color tile = blend((row + column) & 1 ? (Color){190, 198, 201, 255} : (Color){215, 221, 220, 255}, SKY, fade);498        DrawTriangle3D((Vector3){left, y, back}, (Vector3){left, y, front}, (Vector3){right, y, front}, tile);499        DrawTriangle3D((Vector3){left, y, back}, (Vector3){right, y, front}, (Vector3){right, y, back}, tile);500      }501      if (is_checkpoint(index)) {502        DrawCube((Vector3){middle.x, box.low[1] - 116, middle.z}, 60, 232, 60,503          blend((Color){43, 59, 73, 255}, SKY, fade));504        for (int side = -1; side <= 1; side += 2) {505          float x = side < 0 ? box.low[0] - 8 : box.high[0] + 8;506          DrawCube((Vector3){x, box.high[1] + 24, middle.z + 30}, 5, 5, 130, rim);507          for (int end = -1; end <= 1; end += 2)508            DrawCube((Vector3){x, box.high[1] + 12, middle.z + 30 + end * 62}, 5, 24, 5, rim);509        }510      }511    }512    static void structure(Vector3 eye, Vector3 middle, Vector3 size, Color color) {513      float distance = Vector3Distance(eye, middle);514      if (distance < 1900) shaded_cube(middle, size, color, Clamp((distance - 550) / 1450, 0, 0.95f));515    }516    static void industrial_yard(const game *state, Vector3 eye) {517      const Color steel = {65, 82, 92, 255}, concrete = {101, 115, 120, 255};518      const Color rust = {128, 73, 46, 255}, blue = {40, 89, 117, 255};519      for (int i = 0; i < BH_PADS; i += 2) {520        Vector3 pad = center(state->course.pads[i]);521        if (fabsf(pad.z - eye.z) > 1800) continue;522        int section = i / 8;523        float floor = -250;524        structure(eye, (Vector3){0, floor, pad.z}, (Vector3){1280, 12, 480}, (Color){31, 46, 55, 255});525        for (int side = -1; side <= 1; side += 2) {526          float x = side * 520;527          structure(eye, (Vector3){x, 100, pad.z}, (Vector3){24, 700, 32}, steel);528          structure(eye, (Vector3){x - side * 12, 85, pad.z + 17}, (Vector3){5, 95, 2}, ACCENT);529          structure(eye, (Vector3){side * 625, -135, pad.z}, (Vector3){245, 210, 465}, section == 0 ? blue : concrete);530          if (section == 0) {531            // Stacked ribbed shipping containers and open sky at the loading yard.532            structure(eye, (Vector3){side * 660, 50, pad.z - 75}, (Vector3){270, 165, 300}, i % 4 ? rust : blue);533            for (int rib = 0; rib < 6; ++rib)534              structure(eye, (Vector3){side * 519, 50, pad.z - 200 + rib * 50}, (Vector3){8, 150, 7}, steel);535          } else if (section == 1) {536            // Tall windowed turbine hall, suspended ducts and overhead beams.537            structure(eye, (Vector3){side * 550, 260, pad.z}, (Vector3){32, 340, 460}, concrete);538            for (int window = 0; window < 3; ++window)539              structure(eye, (Vector3){side * 530, 285, pad.z - 150 + window * 150},540                (Vector3){3, 92, 105}, (Color){74, 136, 154, 255});541            structure(eye, (Vector3){side * 345, 330, pad.z}, (Vector3){68, 62, 475}, steel);542          } else {543            // Original reactor/silo shapes; low polygon counts suit the software renderer.544            float distance = Vector3Distance(eye, (Vector3){side * 710, 0, pad.z});545            if (distance < 1700) {546              Color tank = blend(section == 2 ? blue : concrete, SKY, Clamp((distance - 450) / 1450, 0, 0.94f));547              DrawCylinder((Vector3){side * 710, -220, pad.z}, 125, 145, section == 2 ? 590 : 790, 12, tank);548              DrawCylinder((Vector3){side * 710, section == 2 ? 220 : 400, pad.z}, 149, 149, 18, 12,549                blend(section == 2 ? CYAN : ACCENT, SKY, Clamp(distance / 2000, 0, 0.9f)));550            }551            structure(eye, (Vector3){side * 460, -170, pad.z}, (Vector3){14, 20, 470}, section == 2 ? CYAN : ACCENT);552          }553        }554        if (section == 1 || i % 4 == 0) {555          structure(eye, (Vector3){0, 425, pad.z}, (Vector3){1080, 24, 32}, steel);556          structure(eye, (Vector3){0, 409, pad.z}, (Vector3){240, 4, 18}, (Color){233, 219, 168, 255});557        }558        if (is_checkpoint(i)) {559          structure(eye, (Vector3){pad.x, pad.y + 194, pad.z - 100}, (Vector3){310, 46, 24}, blue);560          for (int side = -1; side <= 1; side += 2)561            structure(eye, (Vector3){pad.x + side * 152, pad.y + 90, pad.z - 100}, (Vector3){8, 220, 8}, CYAN);562        }563      }564    }565    static void draw(game *state, Font font, int width, int height) {566      BeginDrawing();567      ClearBackground(SKY);568      DrawRectangleGradientV(0, 0, width, height, (Color){12, 18, 27, 255}, SKY);569      Vector3 eye = {state->player.position[0], state->player.position[1] + 64, state->player.position[2]};570      Camera3D camera = {.position = eye, .target = {eye.x + sinf(state->yaw) * cosf(state->pitch),571        eye.y + sinf(state->pitch), eye.z - cosf(state->yaw) * cosf(state->pitch)},572        .up = {0, 1, 0}, .fovy = 74, .projection = CAMERA_PERSPECTIVE};573      BeginMode3D(camera);574      industrial_yard(state, eye);575      for (int i = 0; i < PLATFORM_COUNT; ++i) {576        float distance = Vector3Distance(eye, center(state->course.pads[i]));577        if (distance < 1900) platform(state->course.pads[i], i, state->course.cooldown[i],578          Clamp((distance - 450) / 1500, 0, 0.92f));579      }580      EndMode3D();581    582      DrawRectangle(0, 0, width, 62, (Color){12, 18, 25, 255});583      label(font, "A I R T I M E", 18, 10, 25, INK);584      label(font, "FOUNDRY / STRAFE TRIALS", 20, 39, 11, ACCENT);585      const char *sections[] = {"01 / LOADING YARD", "02 / TURBINE HALL", "03 / REACTOR", "04 / SILO RUN", "FINISH"};586      label(font, sections[state->checkpoint / 8], width / 2 - 95, 14, 14, CYAN);587      for (int i = 0; i < BH_PADS; ++i) {588        Color color = i <= state->furthest ? CYAN : is_checkpoint(i) ? INK : MUTED;589        DrawRectangle(width / 2 - 96 + i * 6, 43, 3, is_checkpoint(i) ? 8 : 4, color);590      }591      char text[160];592      if (state->practice) snprintf(text, sizeof(text), "%05.2f  /  PRACTICE", state->elapsed);593      else snprintf(text, sizeof(text), "%05.2f  /  BEST %s", state->elapsed, state->best > 0 ? TextFormat("%.2f", state->best) : "--");594      label(font, text, width - 230, 17, 18, INK);595      snprintf(text, sizeof(text), "PAD %02d/%02d    FALLS %d", state->furthest, PLATFORM_COUNT - 1, state->deaths);596      label(font, text, width - 230, 41, 11, MUTED);597    598      DrawRectangle(0, height - 96, width, 96, (Color){12, 18, 25, 255});599      float speed = bh_speed(&state->player);600      snprintf(text, sizeof(text), "%03.0f", speed);601      Color speed_color = speed > 300 ? CYAN : ACCENT;602      label(font, text, width / 2 - 50, height - 86, 40, speed_color);603      label(font, "UNITS / SECOND", width / 2 - 47, height - 43, 11, MUTED);604      DrawRectangle(20, height - 16, width - 40, 3, (Color){45, 58, 69, 255});605      DrawRectangle(20, height - 16, (width - 40) * fminf(speed / 800, 1), 3, speed_color);606      snprintf(text, sizeof(text), "%s  |  SYNC %.0f%%", state->player.ground < 0 ? "AIR" : "GROUND",607        state->air_ticks ? 100.0f * state->gain_ticks / state->air_ticks : 0);608      label(font, text, 20, height - 77, 13, INK);609      snprintf(text, sizeof(text), "LAND %.0f   PEAK %.0f", state->landing_speed, state->peak_speed);610      label(font, text, 20, height - 53, 11, MUTED);611      label(font, "WASD   SPACE / WHEEL JUMP", width - 222, height - 77, 11, INK);612      label(font, "R RESTART   F CHECKPOINT   Q EXIT", width - 222, height - 53, 10, MUTED);613      label(font, "1-4 PRACTICE A SECTION", width - 222, height - 33, 10, MUTED);614    615      DrawLine(width / 2 - 5, height / 2, width / 2 + 5, height / 2, INK);616      DrawLine(width / 2, height / 2 - 5, width / 2, height / 2 + 5, INK);617      int ground = state->player.ground;618      if (ground >= 0 && !is_checkpoint(ground)) {619        label(font, "JUMP!", width / 2 - 25, height / 2 + 25, 16, ACCENT);620        DrawRectangle(width / 2 - 32, height / 2 + 47,621          64 * (state->course.cooldown[ground] - BH_PAD_RETURN) / BH_PAD_DELAY, 3, ACCENT);622      }623      if (!state->looking || state->finished) {624        int panel_width = width > 540 ? 500 : width - 30, x = (width - panel_width) / 2;625        DrawRectangle(x, height / 2 - 74, panel_width, 178, (Color){12, 18, 25, 255});626        DrawRectangle(x, height / 2 - 74, panel_width, 2, ACCENT);627        label(font, state->finished ? "COURSE COMPLETE" : "CLICK TO TAKE THE MOUSE", x + 22, height / 2 - 54, 23, INK);628        label(font, "W to launch. In air, release W and strafe A / D", x + 22, height / 2 - 17, 12, MUTED);629        label(font, "while turning the mouse in the same direction.", x + 22, height / 2 + 3, 12, MUTED);630        label(font, "Yellow pads drop on touch. Cyan decks are safe.", x + 22, height / 2 + 26, 12, INK);631        label(font, state->finished ? "R to race again. Q returns to Slop." :632          "Space or either wheel direction jumps. Esc releases.", x + 22, height / 2 + 65, 12, ACCENT);633      }634    }635    static int usage(const char *name, int status) {636      fprintf(status ? stderr : stdout, "usage: %s [--frames COUNT]\n"637        "Click once for mouse look; Escape releases. WASD moves; Space and wheel up/down jump.\n"638        "Foundry: 32 jumps through the yard, turbine hall, reactor and silos.\n"639        "Yellow pads collapse 100ms after landing and return after 2s; cyan checkpoints stay solid.\n"640        "1-4 practices a section without recording a best time; R begins a full run.\n"641        "R restarts; F returns to a checkpoint; Q exits. Release W in air and match A/D with mouse turn.\n", name);642      return status;643    }644    static void agent_input(void *context, const dolly_input_event *event) { input(context, event); }645    static int agent_frame(game *state, Font font) {646      static unsigned char pixels[960 * 540 * 4];647      draw(state, font, 960, 540);648      EndDrawing();649      rlCopyFramebuffer(0, 0, 960, 540, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8, pixels);650      return bh_agent_frame(pixels);651    }652    int main(int argc, char **argv) {653      unsigned long limit = 0;654      if (argc == 2 && strcmp(argv[1], "--help") == 0) return usage(argv[0], 0);655      if (argc == 3 && strcmp(argv[1], "--frames") == 0) {656        char *end;657        errno = 0;658        limit = strtoul(argv[2], &end, 10);659        if (errno || argv[2][0] < '0' || argv[2][0] > '9' || *end || !limit) return usage(argv[0], 2);660      } else if (argc != 1) return usage(argv[0], 2);661      game state = {0};662      const int agent = bh_agent_open(agent_input, &state);663      dolly_raylib graphics = {0};664      if (agent) {665        graphics.surface.width = 960; graphics.surface.height = 540;666        // The agent's viewer shows the frames: raylib renders to memory.667        SetConfigFlags(FLAG_WINDOW_HIDDEN);668        InitWindow(960, 540, "Dolly Airtime");669        if (!IsWindowReady()) return 1;670        SetTraceLogLevel(LOG_WARNING);671      } else if (dolly_raylib_open_sized(&graphics, "Dolly Airtime", 960, 540) != 0) return 1;672      // A crosshair asks for the click that lets the page capture the pointer.673      if (!agent && (dolly_input_set_pointer_relative(graphics.input, 1) != 0 ||674                     dolly_raylib_set_cursor(&graphics, DOLLY_DISPLAY_CURSOR_CROSSHAIR) != 0)) {675        dolly_raylib_close(&graphics);676        fputs("bhop: this runtime does not support captured mouse input\n", stderr);677        return 1;678      }679      Font font = LoadFontEx("/usr/share/fonts/IosevkaTerm-SemiBold.ttf", 32, NULL, 0);680      if (!IsFontValid(font)) { if (agent) CloseWindow(); else dolly_raylib_close(&graphics); return 1; }681      SetTextureFilter(font.texture, TEXTURE_FILTER_BILINEAR);682      rlSetClipPlanes(1, 2400);683      bh_course_init(&state.course);684      respawn(&state, 1);685      FILE *record = fopen(record_path, "r");686      if (record) { if (fscanf(record, "%f", &state.best) != 1 || !isfinite(state.best) || state.best < 0) state.best = 0; fclose(record); }687      double previous = seconds(), accumulator = 0;688      unsigned long frames = 0;689      int result = 0;690      while (!state.quit && (!limit || frames < limit)) {691        if (agent) {692          const struct timespec interval = {0, 8000000}; nanosleep(&interval, NULL);693          if (bh_agent_poll()) break;694        } else if (frames && dolly_raylib_wait_frame(&graphics, 1000) < 0) { result = 1; break; }695        dolly_input_event event;696        for (int i = 0; !agent && i < 256; ++i) {697          int status = dolly_raylib_next_event(&graphics, &event, 0);698          if (status < 0) { result = 1; state.quit = 1; break; }699          if (!status) break;700          input(&state, &event);701        }702        if (state.quit) break;703        double now = seconds();704        accumulator += now >= previous ? fmin(now - previous, 0.1) : 0;705        previous = now;706        int drawn = 0;707        for (int i = 0; accumulator >= BH_STEP && i < 10; ++i, accumulator -= BH_STEP) {708          if (agent) bh_agent_tick();709          tick(&state); drawn = 0;710          if (agent && bh_agent_capture_due()) {711            if (!agent_frame(&state, font)) { result = 1; break; }712            drawn = 1;713          }714        }715        if (result) break;716        if (agent) {717          if (!drawn && !agent_frame(&state, font)) { result = 1; break; }718        } else {719          draw(&state, font, graphics.surface.width, graphics.surface.height);720          if (dolly_raylib_end_frame(&graphics) != 0) { result = 1; break; }721        }722        ++frames;723      }724      UnloadFont(font);725      if (agent) { bh_agent_close(); CloseWindow(); } else dolly_raylib_close(&graphics);726      printf("bhop: ticks=%d jumps=%d falls=%d pad=%d peak=%.3f speed=%.3f collapses=%d\n",727        state.ticks, state.jumps, state.deaths, state.furthest, state.peak_speed, bh_speed(&state.player), state.course.collapses);728      return result;729    }730731FILE /usr/src/dolly/bhop/bhop.mk732    .PHONY: all check733    all: /usr/bin/bhop734    /usr/bin/bhop: /usr/src/dolly/bhop/bhop.c /usr/src/dolly/bhop/bhop-movement.h /usr/src/dolly/bhop/bhop-course.h /usr/src/dolly/bhop/agent/input.c /usr/src/dolly/bhop/agent/input.h /usr/src/dolly/bhop/agent/timeline.h /usr/src/dolly/game-agent/control.h /usr/lib/libdolly-raylib.a /usr/lib/libraylib.a735    	cc -std=c17 -O2 $< /usr/src/dolly/bhop/agent/input.c -o $@ -ldolly-raylib -lraylib -lm736    check:737    	@output=/tmp/bhop-check.$$$$; status=0; cc -std=c17 -O2 /usr/src/dolly/bhop/bhop-check.c -o "$$output" -lm && "$$output" || status=$$?; rm -f "$$output"; exit "$$status"738739SLOP make -f /usr/src/dolly/bhop/bhop.mk all check740741SLOP c++ -O1 -std=c++11 -I/usr/include/SDL2 -I/tmp/bhop-stb /usr/src/dolly/game-agent/viewer.cpp -o /usr/bin/bhop-viewer -lSDL2 -lm742SLOP cc -std=gnu11 -I/usr/include/dolly -DEMSCRIPTEN=1 -D_GNU_SOURCE -DQUICKJS_NG_BUILD -DNDEBUG -funsigned-char /usr/src/dolly/bhop/agent/launch.c -ldolly-js -lquickjs -o /usr/bin/bhop-agent743SLOP rm -rf /tmp/bhop-source.tar /tmp/bhop-stb744745EXPORTS TOOL bhop746EXPORTS TOOL bhop-agent747EXPORTS TOOL bhop-viewer748FILE /usr/share/man/cat1/bhop.1749    bhop: Airtime: Foundry, a strafe course. A/D with the mouse builds air speed,750    Space or the wheel jumps, Escape pauses, R restarts, 1-4 practise a section,751    Q quits.752    bhop-agent    the game with the agent's overlay: Backtick switches between you753                  and the agent, Enter sends it an instruction754    bhop-viewer   the viewer the agent's overlay starts755    make -f /usr/src/dolly/bhop/bhop.mk all check756                  builds bhop again from /usr/src/dolly/bhop (cc and raylib)757EXPORTS FOLDER bhop-agent-source /usr/src/dolly/bhop/agent758EXPORTS FOLDER game-agent-source /usr/src/dolly/game-agent759EXPORTS FOLDER game-input-source /usr/src/dolly/rts760