Feature: parce Contacs and laying ground for a iRacing Contact system

This commit is contained in:
Afonso Clerigo Mendes de Sousa 2025-10-17 16:24:27 +01:00
parent 1fe0452ad6
commit 2df0838be8
5 changed files with 99 additions and 29 deletions

Binary file not shown.

View File

@ -49,16 +49,18 @@ int32_t read_int32(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok
return (int32_t)ntohl(v);
}
void read_float(const u_int8_t *buf, size_t recv_len, size_t *offset, float *out, int *ok) {
float read_float(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok) {
if (!ensure(recv_len, *offset, sizeof(float))) {
*ok = 0;
return;
return 0.0f;
}
uint32_t v;
memcpy(&v, buf + *offset, sizeof(v));
*offset += sizeof(v);
v = ntohl(v);
memcpy(out, &v, sizeof(float));
u_int32_t v_int;
memcpy(&v_int, buf + *offset, sizeof(v_int));
*offset += sizeof(v_int);
v_int = ntohl(v_int);
float v_float;
memcpy(&v_float, &v_int, sizeof(v_float));
return v_float;
}
void read_bytes(const u_int8_t *buf, size_t recv_len, size_t *offset, u_int8_t *out, size_t len, int *ok) {

View File

@ -35,7 +35,7 @@ int32_t read_int32(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok
// Reads a 32-bit float from the buffer in network byte order.
// Advances the offset by 4 bytes.
void read_float(const u_int8_t *buf, size_t recv_len, size_t *offset, float *out, int *ok);
float read_float(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok);
// Reads a fixed number of bytes into the output buffer.
// The output buffer must be at least 'len' bytes long.

View File

@ -59,10 +59,18 @@ struct postion {
float z;
} __attribute__((packed));
enum flag {
NO_FLAG = 0,
YELLOW_FLAG = 1,
BLUE_FLAG = 2,
BLACK_FLAG = 3,
CHECKERED_FLAG = 4,
}
struct carAtributes {
// Related to ACSP_CAR_INFO
u_char isConnected; // 1 = connected, 0 = disconnected
u_char isLoading; // 1 = loading, 0 = not loading
u_char isLoading; // 1 = loading, 0 = not isLoading
char *car_model;
char *car_skin;
@ -80,6 +88,13 @@ struct carAtributes {
u_int32_t cuts;
u_int32_t total_cuts;
u_int32_t total_cuts_alltime;
u_int16_t total_laps_completed;
u_int16_t contacts;
u_int16_t total_contacts;
flag current_flag; // TODO: implement flag status updates
// TAG:3
float_t normalizedSplinePos;
} __attribute__((packed));

View File

@ -52,6 +52,12 @@ void init_carupdate(carAtributes *car) {
car->cuts = 0;
car->total_cuts = 0;
car->total_cuts_alltime = 0; // TODO: SQL querry to get total cuts of all time;
// TAG:1
car->total_laps_completed = 0;
car->contacts = 0;
car->total_contacts = 0; // TODO: SQL querry to get total contacts of all time;
// TAG:2
car->normalizedSplinePos = 0.0f;
}
@ -148,8 +154,9 @@ int main(void) {
str_len_8 = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
read_string((const u_int8_t *)buffer, recv_bytes, &offset, trackInfo.weather_graphics, str_len_8, &ok);
// FIX: elapsed_ms was/is incorrectly read
trackInfo.elapsed_ms = read_uint32((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
// FIX: elapsed_ms was/is incorrectly read
// TEST: Verify with actual server
trackInfo.elapsed_ms = read_uint32((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
if (!ok) {
fprintf(stderr, "[-] Error parsing NEW_SESSION packet (offset=%zu)\n", offset);
@ -384,14 +391,14 @@ int main(void) {
printf("\tServer Message: \"%s\"\n\n", message);
break;
case ACSP_LAP_COMPLETED: // TODO: Handle lap completed events
case ACSP_LAP_COMPLETED: // DONE: Handle lap completed events
printf("[+] Message Type: ACSP_LAP_COMPLETED\n");
offset = 1; // Reset offset to 1 to read after message types
update.carID = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
// TEST: Possiblity of lap_time not behing correct
// TODO: Verify with actual server
// TEST: Possiblity of lap_time not behing correct
// TODO: Verify with actual server
update.lap_time = read_uint32((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
update.cuts = read_uint32((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
@ -407,24 +414,70 @@ int main(void) {
// ============================
// EVENTS
// ============================
case ACSP_CLIENT_EVENT:
case ACSP_CLIENT_EVENT: {
printf("[+] Message Type: ACSP_CLIENT_EVENT\n");
break;
offset = 1; // Reset offset to 1 to read after message types
// ============================
// EVENT TYPES
// ============================
case ACSP_CE_COLLISION_WITH_CAR:
printf("[+] Event Type: ACSP_CE_COLLISION_WITH_CAR\n");
break;
u_int8_t event_type = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
case ACSP_CE_COLLISION_WITH_ENV:
printf("[+] Event Type: ACSP_CE_COLLISION_WITH_ENV\n");
break;
update.carID = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
u_int8_t event_car_id = 255; // If event doesnt involve another car, set to 255
// TODO: Add for ranking system
// OPTIMIZE: Make sure DB queries are optimized for speed has there can be more that 30 players querying at the same time
//
switch (event_type) {
// ============================
// EVENT TYPES
// ============================
case ACSP_CE_COLLISION_WITH_CAR: {
printf("[+] Event Type: ACSP_CE_COLLISION_WITH_CAR\n");
event_car_id = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
printf("Car ID: %d (%s) collided with Car ID: %d (%s)\n", update.carID, players[update.carID].driver_name, event_car_id, players[event_car_id].driver_name);
players[update.carID].collisions++;
players[event_car_id].collisions++;
// TODO: Update total contacts for both players in the database
// TAG:2 Update total contacts for both players
} break;
case ACSP_CE_COLLISION_WITH_ENV: {
printf("[+] Event Type: ACSP_CE_COLLISION_WITH_ENV\n");
printf("Car ID: %d (%s) collided with the environment\n", update.carID, players[update.carID].driver_name);
players[update.carID].contacts++;
// TODO: Update total contacts for the player in the database
// TAG:2 Update total contacts for the player
} break;
}
// TODO: possible iRacing style impact severity system
// With X velues and (e.g >5 m/s = 0x, >10 m/s = 2x, >15 m/s = 4x) && Blackflag limits (e.g x17 = DQ)
float impact_speed = read_float((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
postion world_pos = {0};
world_pos.x = read_float((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
world_pos.y = read_float((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
world_pos.z = read_float((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
postion rel_pos = {0};
rel_pos.x = read_float((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
rel_pos.y = read_float((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
rel_pos.z = read_float((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
printf("\tImpact Speed: %.2f m/s\n", impact_speed);
printf("\tWorld Position: X: %.2f Y: %.2f Z: %.2f\n", world_pos.x, world_pos.y, world_pos.z);
printf("\tRelative Position: X: %.2f Y: %.2f Z: %.2f\n\n", rel_pos.x, rel_pos.y, rel_pos.z);
} break;
// TODO: Add for ranking system
// OPTIMIZE: Make sure DB queries are optimized for speed has there can be more that 30 players querying at the same time
//
// ============================
// CLIENT → SERVER COMMANDS
// ============================