You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

119 lines
2.2 KiB

#include "cars.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct car s_cars[MAX_CARS];
#ifdef WIN32
#include <stddef.h>
#include <string.h>
#include <stdio.h>
char* strsep(char** stringp, const char* delim)
{
char* start = *stringp;
char* p;
p = (start != NULL) ? strpbrk(start, delim) : NULL;
if (p == NULL)
{
*stringp = NULL;
}
else
{
*p = '\0';
*stringp = p + 1;
}
return start;
}
#endif
void init_cars(void)
{
memset(s_cars, 0, sizeof s_cars);
FILE *f = fopen("cars.txt", "r");
if (!f) return;
struct car *car = s_cars;
while (!feof(f))
{
char buf[1024];
if (fgets(buf, sizeof buf, f) <= 0) break;
if (*buf == '#') continue;
char *bufp = buf, *p;
p = strsep(&bufp, ",");
strncpy(car->tag, p, sizeof car->tag);
p = strsep(&bufp, ",");
strncpy(car->base, p, sizeof car->base);
p = strsep(&bufp, ",");
strncpy(car->cls, p, sizeof car->cls);
p = strsep(&bufp, ",");
car->intake = strtol(p, NULL, 10);
p = strsep(&bufp, ",");
car->weight = strtol(p, NULL, 10);
p = strsep(&bufp, ",");
strncpy(car->name, p, sizeof car->name);
p = strsep(&bufp, ",");
car->maxrpm = strtof(p, NULL);
p = strsep(&bufp, ",");
car->maxfuel = strtof(p, NULL);
p = strsep(&bufp, ",");
car->maxboost = strtof(p, NULL);
p = strsep(&bufp, ",");
car->maxspeed = strtof(p, NULL);
car++;
if (car - s_cars == MAX_CARS) break;
}
fclose(f);
}
int get_car(const char tag[4], struct car *car)
{
int i;
for (i = 0; i < MAX_CARS; i++)
{
if (!strcmp(s_cars[i].tag, tag))
{
if (car != NULL) memcpy(car, &s_cars[i], sizeof *car);
return i;
}
}
return -1;
}
const char *find_car(const char tag[4], int intake, int weight)
{
const struct car *best = NULL;
int i;
for (i = 0; i < MAX_CARS; i++)
{
const struct car *car = s_cars + i;
if (!strcmp(car->base, tag))
{
if (car->intake <= intake && car->weight <= weight)
{
if (best == NULL || (car->intake >= best->intake && car->weight >= best->weight))
{
best = s_cars + i;
}
}
}
}
return best->tag;
}
void update_car(int carnum, float dist, float time, float cons)
{
s_cars[carnum].dist += dist;
s_cars[carnum].time += time;
s_cars[carnum].cons += cons;
}