From 14038dae62757c24e98f776a0e6c4e71abb944d4 Mon Sep 17 00:00:00 2001 From: cinnaboot Date: Fri, 2 Jan 2026 13:45:17 -0500 Subject: [PATCH] Add headless mode for terminal-based simulation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add --headless/-h flag to run without graphics - Add --days N flag to specify simulation duration - Print initial state, periodic updates, and final state - Useful for testing, debugging, and headless environments 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 --- src/main.cpp | 120 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 91 insertions(+), 29 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 06d58ab..63e17fc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,16 +4,30 @@ #include "renderer.h" #include #include +#include int main(int argc, char** argv) { // Parse command line arguments const char* config_file = "configs/solar_system.txt"; - if (argc > 1) { - config_file = argv[1]; + bool headless = false; + int sim_duration_days = 365; // Default: 1 year for headless mode + + for (int i = 1; i < argc; i++) { + if (strcmp(argv[i], "--headless") == 0 || strcmp(argv[i], "-h") == 0) { + headless = true; + } else if (strcmp(argv[i], "--days") == 0 && i + 1 < argc) { + sim_duration_days = atoi(argv[++i]); + } else if (argv[i][0] != '-') { + config_file = argv[i]; + } } printf("=== Orbital Mechanics Simulation ===\n"); printf("Loading configuration: %s\n", config_file); + if (headless) { + printf("Mode: Headless (terminal output only)\n"); + printf("Duration: %d days\n", sim_duration_days); + } // Create simulation with time step of 60 seconds const int MAX_BODIES = 100; @@ -27,28 +41,75 @@ int main(int argc, char** argv) { return 1; } - // Initialize renderer - init_renderer(1280, 720, "Orbital Mechanics Simulation"); - - // Setup rendering state - RenderState render_state; - setup_camera(&render_state); + // If headless mode, run simulation without rendering + if (headless) { + const double SECONDS_PER_DAY = 86400.0; + const double total_time = sim_duration_days * SECONDS_PER_DAY; + const double output_interval = SECONDS_PER_DAY; // Print status once per day + double next_output_time = 0.0; + + printf("\n=== Initial State ===\n"); + for (int i = 0; i < sim->body_count; i++) { + CelestialBody* body = &sim->bodies[i]; + printf("%s:\n", body->name); + printf(" Position: (%.3e, %.3e, %.3e) m\n", + body->position.x, body->position.y, body->position.z); + printf(" Velocity: (%.3e, %.3e, %.3e) m/s\n", + body->velocity.x, body->velocity.y, body->velocity.z); + } - // Simulation control variables - bool paused = false; - double speed_multiplier = 1.0; - int physics_steps_per_frame = 100; // Multiple physics steps per frame for stability + printf("\n=== Running Simulation ===\n"); + while (sim->time < total_time) { + update_simulation(sim); + + // Print periodic updates + if (sim->time >= next_output_time) { + printf("Day %.1f: ", sim->time / SECONDS_PER_DAY); + for (int i = 0; i < sim->body_count && i < 3; i++) { + double dist = vec3_magnitude(sim->bodies[i].position); + printf("%s=%.3e m ", sim->bodies[i].name, dist); + } + printf("\n"); + next_output_time += output_interval; + } + } - printf("\nSimulation started!\n"); - printf("Controls:\n"); - printf(" Arrow keys: Rotate and zoom camera\n"); - printf(" Space: Pause/Resume\n"); - printf(" +/-: Speed up/slow down simulation\n"); - printf(" I: Toggle info display\n"); - printf(" ESC: Quit\n\n"); + printf("\n=== Final State (Day %.1f) ===\n", sim->time / SECONDS_PER_DAY); + for (int i = 0; i < sim->body_count; i++) { + CelestialBody* body = &sim->bodies[i]; + printf("%s:\n", body->name); + printf(" Position: (%.3e, %.3e, %.3e) m\n", + body->position.x, body->position.y, body->position.z); + printf(" Velocity: (%.3e, %.3e, %.3e) m/s\n", + body->velocity.x, body->velocity.y, body->velocity.z); + } - // Main loop - while (!WindowShouldClose()) { + destroy_simulation(sim); + return 0; + } else { + // Graphical mode + // Initialize renderer + init_renderer(1280, 720, "Orbital Mechanics Simulation"); + + // Setup rendering state + RenderState render_state; + setup_camera(&render_state); + + // Simulation control variables + bool paused = false; + double speed_multiplier = 1.0; + int physics_steps_per_frame = 100; // Multiple physics steps per frame for stability + + printf("\nSimulation started!\n"); + printf("Controls:\n"); + printf(" Arrow keys: Rotate and zoom camera\n"); + printf(" Space: Pause/Resume\n"); + printf(" +/-: Speed up/slow down simulation\n"); + printf(" I: Toggle info display\n"); + printf(" ESC: Quit\n\n"); + + // Main loop + while (!WindowShouldClose()) { // Handle input if (IsKeyPressed(KEY_SPACE)) { paused = !paused; @@ -77,14 +138,15 @@ int main(int argc, char** argv) { } } - // Render - render_simulation(sim, &render_state); - } + // Render + render_simulation(sim, &render_state); + } - // Cleanup - close_renderer(); - destroy_simulation(sim); + // Cleanup + close_renderer(); + destroy_simulation(sim); - printf("\nSimulation ended. Final time: %.2f days\n", sim->time / 86400.0); - return 0; + printf("\nSimulation ended. Final time: %.2f days\n", sim->time / 86400.0); + return 0; + } }