Henry Triff / Mbed 2 deprecated ELEC2645_Project_el18ht

Dependencies:   mbed

Ghost/Ghost.cpp

Committer:
HenryWTriff
Date:
2020-04-29
Revision:
25:31761087a83f
Parent:
17:4c5f25d5c4d5

File content as of revision 25:31761087a83f:

#include "Ghost.h"

Ghost::Ghost()
{
}

Ghost::~Ghost()
{
}

//---------------
//  DURING GAME
//---------------

void Ghost::Record(Point_2D position, int race_time) //Save the current position of player to Ghost_Data
{
    if(race_time < 6000) { //Set Max time to be 6000 frames ≈ 2mins
        Ghost_Data[race_time] = position; //Save position to Ghost_Data
    }
}

void Ghost::Copy(int race_time, int track, int car, SDFileSystem &SD) //Copy the Ghost_Data to the SD Card
{
    int time = SD_Get_Time(track, car, SD); //Get the best time recorded for that track and car so far

    if(race_time < time) { //If the time just raced is less than the previos best
        SD_Write(track, car, race_time, SD); //Over write Ghost_Data to the SD Card
    }
}

Point_2D Ghost::Play(bool ghost_available, int race_time) //Get the position of the Ghost for a given frame (time)
{
    if(ghost_available == true) { //If in ghost mode
        if(race_time < Ghost_Held_Data_Time && race_time < 6000) { //If the time is less than the max time for a given race and less than 6000 max
            return Ghost_Held_Data[race_time]; //Return the value in the array Ghost_Held_Data for that frame
        }
    } else {
        return {1000,1000}; //Return a position that is way out out of map bounds
    }
}

//----------------------------------
//  SD INITIALISATION / FORMATTING
//----------------------------------

void Ghost::SD_init(SDFileSystem &SD, N5110 &LCD) //Sets up the SD card so that it contains the right files and writes them with data
{
    for(int i = 0; i < 6000; i++) { //Up to 6000 frames (max)
        Ghost_Data[i].x = 10000; //Set the Ghost_Data position to be way out of map bounds
        Ghost_Data[i].y = 10000;
    }
    
    //Write each different file for each combination of car and track with Ghost_Data
    LCD.clear();
    LCD.printString("FORMATTING",0,0);
    LCD.printString("|            |",0,1);
    LCD.refresh();
    SD_Write(Small, Basic, 6000, SD);
    SD_Write(Small, Offroad, 6000, SD);
    SD_Write(Small, Drifter, 6000, SD);
    LCD.clear();
    LCD.printString("FORMATTING",0,0);
    LCD.printString("|**          |",0,1);
    LCD.refresh();
    SD_Write(Small, Sportscar, 6000, SD);
    SD_Write(Small, Racecar, 6000, SD);
    SD_Write(Small, Stupid, 6000, SD);
    LCD.clear();
    LCD.printString("FORMATTING",0,0);
    LCD.printString("|****        |",0,1);
    LCD.refresh();
    SD_Write(Medium, Basic, 6000, SD);
    SD_Write(Medium, Offroad, 6000, SD);
    SD_Write(Medium, Drifter, 6000, SD);
    LCD.clear();
    LCD.printString("FORMATTING",0,0);
    LCD.printString("|******      |",0,1);
    LCD.refresh();
    SD_Write(Medium, Sportscar, 6000, SD);
    SD_Write(Medium, Racecar, 6000, SD);
    SD_Write(Medium, Stupid, 6000, SD);
    LCD.clear();
    LCD.printString("FORMATTING",0,0);
    LCD.printString("|********    |",0,1);
    LCD.refresh();
    SD_Write(Large, Basic, 6000, SD);
    SD_Write(Large, Offroad, 6000, SD);
    SD_Write(Large, Drifter, 6000, SD);
    LCD.clear();
    LCD.printString("FORMATTING",0,0);
    LCD.printString("|**********  |",0,1);
    LCD.refresh();
    SD_Write(Large, Sportscar, 6000, SD);
    SD_Write(Large, Racecar, 6000, SD);
    SD_Write(Large, Stupid, 6000, SD);
}

//----------------------------------
//  READING AND WRITING TO SD CARD
//----------------------------------

void Ghost::SD_Write(int track, int car, int race_time, SDFileSystem &SD) //Produce the correct file name and write Ghost_Data
{
    SD_Write_Time(track, car, race_time, SD); //Write the time for the previos race

    string name_track = ""; //Declare track string
    string name_car = ""; //Declare car string

    //For each type of track, set track string to the correct name
    if(track == Small) {
        name_track = "_Sml";
    } else if(track == Medium) {
        name_track = "_Med";
    } else if(track == Large) {
        name_track = "_Lrg";
    }
    
    //For each type of car, set car string to the correct name.
    //'X' is used to ensure all strings are the same length
    if(car == Basic) {
        name_car = "_BasicXXXX";
    } else if(car == Offroad) {
        name_car = "_OffRoadXX";
    } else if(car == Drifter) {
        name_car = "_DrifterXX";
    } else if(car == Sportscar) {
        name_car = "_SportsCar";
    } else if(car == Racecar) {
        name_car = "_RaceCarXX";
    } else if(car == Stupid) {
        name_car = "_StupidXXX";
    }

    string name = "/sd/ghost" + name_track + name_car + ".txt"; //Concatenate the strings to produce the file name
    SD_Write_Data(name, race_time, SD); //Write the data
}

void Ghost::SD_Read(int track, int car, SDFileSystem &SD) //Produce the correct file name and read data
{
    SD_Read_Time(track, car, SD); //Read the time from the SD card

    string name_track; //Declare track string
    string name_car; //Declare car string
    
    //For each type of track, set track string to the correct name
    if(track == Small) {
        name_track = "_Sml";
    } else if(track == Medium) {
        name_track = "_Med";
    } else if(track == Large) {
        name_track = "_Lrg";
    }
    
    //For each type of car, set car string to the correct name.
    //'X' is used to ensure all strings are the same length
    if(car == Basic) {
        name_car = "_BasicXXXX";
    } else if(car == Offroad) {
        name_car = "_OffRoadXX";
    } else if(car == Drifter) {
        name_car = "_DrifterXX";
    } else if(car == Sportscar) {
        name_car = "_SportsCar";
    } else if(car == Racecar) {
        name_car = "_RaceCarXX";
    } else if(car == Stupid) {
        name_car = "_StupidXXX";
    }

    string name = "/sd/ghost" + name_track + name_car + ".txt"; //Concatenate the strings to produce the file name
    SD_Read_Data(name, Ghost_Held_Data_Time, SD);//Read the data
}

void Ghost::SD_Write_Data(string name, int race_time, SDFileSystem &SD) //Write Ghost data to the given file
{
    char name_char[27]; //Create the char array for the file name

    //Conver string name to char array
    for(int i = 0; i < 27; i++) {
        name_char[i] = name[i];
    }
    
    FILE *file_data; //Create file pointer
    wait(0.1);
    file_data = fopen(name_char, "w"); //Open file for writing
    if (file_data == NULL) { //If file fails to open
        while(1);
    } else { //Else
        for(int i = 0; i < race_time; i++) { //Write Ghost_Data to SD up to the race_time for the ghost_Data
            fprintf(file_data, "%f,%f\n",Ghost_Data[i].x,Ghost_Data[i].y);
            #ifdef DEBUG_GHOST_WRITE
                printf("%f,%f\n", Ghost_Data[i].x, Ghost_Data[i].y);
            #endif
        }
        for(int i = 0; i < (6000 - race_time); i++) { //For the remaining time
            float end_point = 10000; 
            fprintf(file_data, "%f,%f\n",end_point,end_point); //Set the position of the ghost to be way off map
        }
        fclose(file_data); //Close the file
    }
}

void Ghost::SD_Write_Time(int track, int car, int race_time, SDFileSystem &SD)
{
    FILE *file_time_read; //Create pointer
    wait(0.1);

    file_time_read = fopen("/sd/ghost_data.txt", "r"); //Open reading from file
    int track_time_array[3][6]; //Create an array

    if(file_time_read == NULL) { //If file failed to open
        while(1);
    } else { //Else
        fscanf(
            file_time_read,
            "%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i",
            &track_time_array[0][0],
            &track_time_array[0][1],
            &track_time_array[0][2],
            &track_time_array[0][3],
            &track_time_array[0][4],
            &track_time_array[0][5],
            &track_time_array[1][0],
            &track_time_array[1][1],
            &track_time_array[1][2],
            &track_time_array[1][3],
            &track_time_array[1][4],
            &track_time_array[1][5],
            &track_time_array[2][0],
            &track_time_array[2][1],
            &track_time_array[2][2],
            &track_time_array[2][3],
            &track_time_array[2][4],
            &track_time_array[2][5]
        ); //Read first line from file (containing all times)
    }
    fclose(file_time_read); //Close file

    track_time_array[track][car] = race_time; //Set the time for that track and car to the race_time

    FILE *file_time_write; //Create pointer
    wait(0.1);

    file_time_write = fopen("/sd/ghost_data.txt", "w"); //Open writing from file
    if(file_time_write == NULL) { //If file fails to open
        while(1);
    } else {
        fprintf(
            file_time_write,
            "%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i",
            track_time_array[0][0],
            track_time_array[0][1],
            track_time_array[0][2],
            track_time_array[0][3],
            track_time_array[0][4],
            track_time_array[0][5],
            track_time_array[1][0],
            track_time_array[1][1],
            track_time_array[1][2],
            track_time_array[1][3],
            track_time_array[1][4],
            track_time_array[1][5],
            track_time_array[2][0],
            track_time_array[2][1],
            track_time_array[2][2],
            track_time_array[2][3],
            track_time_array[2][4],
            track_time_array[2][5]
        ); //Write all timings
    }
    fclose(file_time_write); //Close file
}

void Ghost::SD_Read_Data(string name, int race_time, SDFileSystem &SD)
{
    //Create char name
    char name_char[27];
    
    //Convert from string to char
    for(int i = 0; i < name.length(); i++) {
        name_char[i] = name[i];
    }
    
    FILE *file_data; //Open pointer
    wait(0.1);
    file_data = fopen(name_char, "r"); //Open file with name
    float x_point = 10000;
    float y_point = 10000;
    int i = 0;
    if(file_data == NULL) { //If file fails to open
        while(1);
    } else { //Else 
        for(int i = 0; i < race_time; i++) { //Up to the total race time
            fscanf(file_data, "%f,%f", &x_point, &y_point); //Read the point data for each frame
            Ghost_Held_Data[i].x = x_point; //Save each point to Ghost_Held_Data
            Ghost_Held_Data[i].y = y_point; 
            #ifdef DEBUG_GHOST_READ
                printf("%f,%f\n", x_point, y_point);
            #endif
        }
        for(int i = 0; i < (6000 - race_time); i++) { //For the remaining time
            Ghost_Held_Data[(i + race_time)].x = 10000; //Set Ghost_Held_Data to 10000,10000
            Ghost_Held_Data[(i + race_time)].y = 10000;
        }
        fclose(file_data); //Close file
    }
}

void Ghost::SD_Read_Time(int track, int car, SDFileSystem &SD) //Read the time for the given track and car
{
    FILE *file_time; //Create pointer
    wait(0.1);

    file_time = fopen("/sd/ghost_data.txt", "r"); //Open file 
    int track_time_array[3][6]; //Create an array

    if(file_time == NULL) { //If file fails to open
        while(1);
    } else {
        fscanf(
            file_time,
            "%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i",
            &track_time_array[0][0],
            &track_time_array[0][1],
            &track_time_array[0][2],
            &track_time_array[0][3],
            &track_time_array[0][4],
            &track_time_array[0][5],
            &track_time_array[1][0],
            &track_time_array[1][1],
            &track_time_array[1][2],
            &track_time_array[1][3],
            &track_time_array[1][4],
            &track_time_array[1][5],
            &track_time_array[2][0],
            &track_time_array[2][1],
            &track_time_array[2][2],
            &track_time_array[2][3],
            &track_time_array[2][4],
            &track_time_array[2][5]
        ); //Read from file
    }
    fclose(file_time); //Close file

    Ghost_Held_Data_Time = track_time_array[track][car]; //Set Ghost_Held_Data_Time to the time for the track and car
}

int Ghost::SD_Get_Time(int track, int car, SDFileSystem &SD) //Get the time for a given track and car
{
    FILE *file_time; //Create pointer
    wait(0.1);

    file_time = fopen("/sd/ghost_data.txt", "r"); //Open reading file
    int track_time_array[3][6]; //Create an array

    if(file_time == NULL) { //If the file fails to open
        while(1);
    } else {
        fscanf(
            file_time,
            "%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i",
            &track_time_array[0][0],
            &track_time_array[0][1],
            &track_time_array[0][2],
            &track_time_array[0][3],
            &track_time_array[0][4],
            &track_time_array[0][5],
            &track_time_array[1][0],
            &track_time_array[1][1],
            &track_time_array[1][2],
            &track_time_array[1][3],
            &track_time_array[1][4],
            &track_time_array[1][5],
            &track_time_array[2][0],
            &track_time_array[2][1],
            &track_time_array[2][2],
            &track_time_array[2][3],
            &track_time_array[2][4],
            &track_time_array[2][5]
        ); //Read from the file
    }
    fclose(file_time); //Close file

    return track_time_array[track][car]; //Return the time for a given track and car
}