// src/timing/speed_controller.cpp
#include "speed_controller.h"

void SpeedController::set_ground_speed(int spd) {
    if (spd < 0) spd = 0;
    if (spd > 30) spd = 30;
    ground_speed = spd;
}

int SpeedController::frame_advance_for(int object_speed, int tick_counter) const {
    if (object_speed <= 0) {
        return 0;
    }

    if (ground_speed == 0) {
        return 1 + object_speed;
    }

    const int delta = object_speed - ground_speed;

    if (delta > 0) {
        return 1 + delta;
    }

    if (delta == 0) {
        return 1;
    }

    float ratio_f = static_cast<float>(ground_speed) / object_speed;
    int ratio = static_cast<int>(ratio_f + 0.3f);
    if (ratio < 1) ratio = 1;

    return (tick_counter % ratio == 0) ? 1 : 0;
}
