<commit_before>#include <ui.h>
#include <std.h>
#include <debug.h>
#include <track_data.h>

#include <tasks/priority.h>
#include <tasks/term_server.h>
#include <tasks/name_server.h>
#include <tasks/clock_server.h>
#include <tasks/train_server.h>

#include <tasks/courier.h>
#include <tasks/path_admin.h>
#include <tasks/path_worker.h>
#include <tasks/train_console.h>
#include <tasks/train_control.h>
#include <tasks/mission_control.h>

#include <tasks/train_master.h>
#include <tasks/train_blaster.h>
#include <tasks/train_blaster_types.h>
#include <tasks/train_blaster/physics.c>

#define TEMPORARY_ACCEL_FUDGE \
    (ctxt->acceleration_time_fudge_factor - (truth.speed / 5))

static void blaster_master_reverse(blaster* const ctxt,
                                   const int time,
                                   const int tid);
static void blaster_reverse_step1(blaster* const ctxt, const int time);
static void blaster_reverse_step2(blaster* const ctxt);
static void blaster_reverse_step3(blaster* const ctxt,
                                  const int tid,
                                  const int time);
static void blaster_reverse_step4(blaster* const ctxt,
                                  const int tid,
                                  const int time);
static void blaster_set_speed(blaster* const ctxt,
                              const int speed,
                              const int time);


static inline int __attribute__ ((const))
blaster_distance_remaining(train_state* const state) {
    return state->next_distance - state->location.offset;
}

static inline int __attribute__((const, always_inline))
blaster_estimate_timeout(int time_current, int time_next) {
    if (time_next <= 0) return 0;
    return time_current + ((time_next * 5) >> 2);
}

const char* event_to_str(const train_event event) {
    switch (event) {
    case EVENT_ACCELERATION:      return "Acceleration Event";
    case EVENT_VIRTUAL:           return "Virtual Checkpoint Event";
    case EVENT_SENSOR:            return "Sensor Checkpoint Event";
    case EVENT_UNEXPECTED_SENSOR: return "Unexpected Sensor Checkpoint Event";
    }
    return "Unknown Event";
}

// choose a pseudo-magic value for speed to use in the calculation
static int __attribute__ ((unused)) blaster_estimate_speed(blaster* const ctxt) {
    return (truth.speed + truth.next_speed) >> 2;
}

#ifndef BANANANANANANANANANANANA
#define blaster_debug_state( ... )
#else
static void __attribute__ ((unused))
blaster_debug_state(blaster* const ctxt,
                    const train_state* const state) {

    const char*  event = event_to_str(state->event);
    const sensor current_loc = pos_to_sensor(state->location.sensor);
    const sensor    next_loc = pos_to_sensor(state->next_location.sensor);

    const int current_offset = state->location.offset / 1000;
    const int    next_offset = state->next_location.offset / 1000;

    char  buffer[512];
    char* ptr = log_start(buffer);
    ptr = sprintf(ptr,
                  "[%s]\n"
                  "%s @ %d ticks (travelling at %c%d.%d%s)\n"
                  "       Location: %c%d + %d mm (travelled %d mm from last)\n"
                  "  Next Location: %c%d + %d mm in %d mm @ %d ticks\n",
                  ctxt->name,
                  event,
                  state->timestamp,
                  state->direction ? '+' : '-',
                  state->speed / 10,
                  state->speed % 10,
                  state->is_accelerating ? " accelerating" : "",
                  current_loc.bank,
                  current_loc.num,
                  current_offset,
                  state->distance / 1000,
                  next_loc.bank,
                  next_loc.num,
                  next_offset,
                  state->next_distance / 1000,
                  state->next_timestamp);

    ptr = log_end(ptr);
    Puts(buffer, ptr - buffer);
}
#endif

static void create_console_reply(const blaster* const ctxt,
                                 const int s_time,
                                 int* result) {

    if (ctxt->reversing) {
        result[0] = 100;
        result[1] = truth.next_location.sensor;
        result[2] = blaster_estimate_timeout(s_time,
                                             truth.next_timestamp -
                                             truth.timestamp);
    } else {
        result[0] = truth.location.sensor;
        result[1] = truth.next_location.sensor;
        result[2] = blaster_estimate_timeout(s_time,
                                             truth.next_timestamp -
                                             truth.timestamp);
    }
}

static int blaster_create_new_delay_courier(blaster* const ctxt) {

    const int tid = Create(TIMER_COURIER_PRIORITY, time_notifier);

    assert(tid >= 0,
           "[%s] Error setting up a timer notifier (%d)",
           ctxt->name, tid);
    UNUSED(ctxt);

    return tid;
}

static inline void blaster_master_where_am_i(blaster* const ctxt,
                                             const int time) {

    physics_update_velocity_ui(ctxt);
    physics_update_prediction_ui(ctxt, time);
    blaster_debug_state(ctxt, &truth);<commit_msg>Avoid potential issues with sensor attribution while reversing<commit_after>#include <ui.h>
#include <std.h>
#include <debug.h>
#include <track_data.h>

#include <tasks/priority.h>
#include <tasks/term_server.h>
#include <tasks/name_server.h>
#include <tasks/clock_server.h>
#include <tasks/train_server.h>

#include <tasks/courier.h>
#include <tasks/path_admin.h>
#include <tasks/path_worker.h>
#include <tasks/train_console.h>
#include <tasks/train_control.h>
#include <tasks/mission_control.h>

#include <tasks/train_master.h>
#include <tasks/train_blaster.h>
#include <tasks/train_blaster_types.h>
#include <tasks/train_blaster/physics.c>

#define TEMPORARY_ACCEL_FUDGE \
    (ctxt->acceleration_time_fudge_factor - (truth.speed / 5))

static void blaster_master_reverse(blaster* const ctxt,
                                   const int time,
                                   const int tid);
static void blaster_reverse_step1(blaster* const ctxt, const int time);
static void blaster_reverse_step2(blaster* const ctxt);
static void blaster_reverse_step3(blaster* const ctxt,
                                  const int tid,
                                  const int time);
static void blaster_reverse_step4(blaster* const ctxt,
                                  const int tid,
                                  const int time);
static void blaster_set_speed(blaster* const ctxt,
                              const int speed,
                              const int time);


static inline int __attribute__ ((const))
blaster_distance_remaining(train_state* const state) {
    return state->next_distance - state->location.offset;
}

static inline int __attribute__((const, always_inline))
blaster_estimate_timeout(int time_current, int time_next) {
    if (time_next <= 0) return 0;
    return time_current + ((time_next * 5) >> 2);
}

const char* event_to_str(const train_event event) {
    switch (event) {
    case EVENT_ACCELERATION:      return "Acceleration Event";
    case EVENT_VIRTUAL:           return "Virtual Checkpoint Event";
    case EVENT_SENSOR:            return "Sensor Checkpoint Event";
    case EVENT_UNEXPECTED_SENSOR: return "Unexpected Sensor Checkpoint Event";
    }
    return "Unknown Event";
}

// choose a pseudo-magic value for speed to use in the calculation
static int __attribute__ ((unused)) blaster_estimate_speed(blaster* const ctxt) {
    return (truth.speed + truth.next_speed) >> 2;
}

#ifndef BANANANANANANANANANANANA
#define blaster_debug_state( ... )
#else
static void __attribute__ ((unused))
blaster_debug_state(blaster* const ctxt,
                    const train_state* const state) {

    const char*  event = event_to_str(state->event);
    const sensor current_loc = pos_to_sensor(state->location.sensor);
    const sensor    next_loc = pos_to_sensor(state->next_location.sensor);

    const int current_offset = state->location.offset / 1000;
    const int    next_offset = state->next_location.offset / 1000;

    char  buffer[512];
    char* ptr = log_start(buffer);
    ptr = sprintf(ptr,
                  "[%s]\n"
                  "%s @ %d ticks (travelling at %c%d.%d%s)\n"
                  "       Location: %c%d + %d mm (travelled %d mm from last)\n"
                  "  Next Location: %c%d + %d mm in %d mm @ %d ticks\n",
                  ctxt->name,
                  event,
                  state->timestamp,
                  state->direction ? '+' : '-',
                  state->speed / 10,
                  state->speed % 10,
                  state->is_accelerating ? " accelerating" : "",
                  current_loc.bank,
                  current_loc.num,
                  current_offset,
                  state->distance / 1000,
                  next_loc.bank,
                  next_loc.num,
                  next_offset,
                  state->next_distance / 1000,
                  state->next_timestamp);

    ptr = log_end(ptr);
    Puts(buffer, ptr - buffer);
}
#endif

static void create_console_reply(const blaster* const ctxt,
                                 const int s_time,
                                 int* result) {

    result[0] = truth.location.sensor;
    result[1] = truth.next_location.sensor;
    result[2] = blaster_estimate_timeout(s_time,
                                         truth.next_timestamp -
                                         truth.timestamp);
}

static int blaster_create_new_delay_courier(blaster* const ctxt) {

    const int tid = Create(TIMER_COURIER_PRIORITY, time_notifier);

    assert(tid >= 0,
           "[%s] Error setting up a timer notifier (%d)",
           ctxt->name, tid);
    UNUSED(ctxt);

    return tid;
}

static inline void blaster_master_where_am_i(blaster* const ctxt,
                                             const int time) {

    physics_update_velocity_ui(ctxt);
    physics_update_prediction_ui(ctxt, time);
    blaster_debug_state(ctxt, &truth);
{'fraction_non_alphanumeric': 0.09723320158102766, 'fraction_numerical': 0.007905138339920948, 'mean_word_length': 2.080974124809741, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 88, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '631994', 'n_tokens_mistral': 3068, 'n_tokens_neox': 2922, 'n_words': 701}