Game
Embed:
(wiki syntax)
Show/hide line numbers
Ghost.cpp
00001 #include "Ghost.h" 00002 00003 Ghost::Ghost() 00004 { 00005 } 00006 00007 Ghost::~Ghost() 00008 { 00009 } 00010 00011 //--------------- 00012 // DURING GAME 00013 //--------------- 00014 00015 void Ghost::Record(Point_2D position, int race_time) //Save the current position of player to Ghost_Data 00016 { 00017 if(race_time < 6000) { //Set Max time to be 6000 frames ≈ 2mins 00018 Ghost_Data[race_time] = position; //Save position to Ghost_Data 00019 } 00020 } 00021 00022 void Ghost::Copy(int race_time, int track, int car, SDFileSystem &SD) //Copy the Ghost_Data to the SD Card 00023 { 00024 int time = SD_Get_Time(track, car, SD); //Get the best time recorded for that track and car so far 00025 00026 if(race_time < time) { //If the time just raced is less than the previos best 00027 SD_Write(track, car, race_time, SD); //Over write Ghost_Data to the SD Card 00028 } 00029 } 00030 00031 Point_2D Ghost::Play(bool ghost_available, int race_time) //Get the position of the Ghost for a given frame (time) 00032 { 00033 if(ghost_available == true) { //If in ghost mode 00034 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 00035 return Ghost_Held_Data[race_time]; //Return the value in the array Ghost_Held_Data for that frame 00036 } 00037 } else { 00038 return {1000,1000}; //Return a position that is way out out of map bounds 00039 } 00040 } 00041 00042 //---------------------------------- 00043 // SD INITIALISATION / FORMATTING 00044 //---------------------------------- 00045 00046 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 00047 { 00048 for(int i = 0; i < 6000; i++) { //Up to 6000 frames (max) 00049 Ghost_Data[i].x = 10000; //Set the Ghost_Data position to be way out of map bounds 00050 Ghost_Data[i].y = 10000; 00051 } 00052 00053 //Write each different file for each combination of car and track with Ghost_Data 00054 LCD.clear(); 00055 LCD.printString("FORMATTING",0,0); 00056 LCD.printString("| |",0,1); 00057 LCD.refresh(); 00058 SD_Write(Small, Basic, 6000, SD); 00059 SD_Write(Small, Offroad, 6000, SD); 00060 SD_Write(Small, Drifter, 6000, SD); 00061 LCD.clear(); 00062 LCD.printString("FORMATTING",0,0); 00063 LCD.printString("|** |",0,1); 00064 LCD.refresh(); 00065 SD_Write(Small, Sportscar, 6000, SD); 00066 SD_Write(Small, Racecar, 6000, SD); 00067 SD_Write(Small, Stupid, 6000, SD); 00068 LCD.clear(); 00069 LCD.printString("FORMATTING",0,0); 00070 LCD.printString("|**** |",0,1); 00071 LCD.refresh(); 00072 SD_Write(Medium, Basic, 6000, SD); 00073 SD_Write(Medium, Offroad, 6000, SD); 00074 SD_Write(Medium, Drifter, 6000, SD); 00075 LCD.clear(); 00076 LCD.printString("FORMATTING",0,0); 00077 LCD.printString("|****** |",0,1); 00078 LCD.refresh(); 00079 SD_Write(Medium, Sportscar, 6000, SD); 00080 SD_Write(Medium, Racecar, 6000, SD); 00081 SD_Write(Medium, Stupid, 6000, SD); 00082 LCD.clear(); 00083 LCD.printString("FORMATTING",0,0); 00084 LCD.printString("|******** |",0,1); 00085 LCD.refresh(); 00086 SD_Write(Large, Basic, 6000, SD); 00087 SD_Write(Large, Offroad, 6000, SD); 00088 SD_Write(Large, Drifter, 6000, SD); 00089 LCD.clear(); 00090 LCD.printString("FORMATTING",0,0); 00091 LCD.printString("|********** |",0,1); 00092 LCD.refresh(); 00093 SD_Write(Large, Sportscar, 6000, SD); 00094 SD_Write(Large, Racecar, 6000, SD); 00095 SD_Write(Large, Stupid, 6000, SD); 00096 } 00097 00098 //---------------------------------- 00099 // READING AND WRITING TO SD CARD 00100 //---------------------------------- 00101 00102 void Ghost::SD_Write(int track, int car, int race_time, SDFileSystem &SD) //Produce the correct file name and write Ghost_Data 00103 { 00104 SD_Write_Time(track, car, race_time, SD); //Write the time for the previos race 00105 00106 string name_track = ""; //Declare track string 00107 string name_car = ""; //Declare car string 00108 00109 //For each type of track, set track string to the correct name 00110 if(track == Small) { 00111 name_track = "_Sml"; 00112 } else if(track == Medium) { 00113 name_track = "_Med"; 00114 } else if(track == Large) { 00115 name_track = "_Lrg"; 00116 } 00117 00118 //For each type of car, set car string to the correct name. 00119 //'X' is used to ensure all strings are the same length 00120 if(car == Basic) { 00121 name_car = "_BasicXXXX"; 00122 } else if(car == Offroad) { 00123 name_car = "_OffRoadXX"; 00124 } else if(car == Drifter) { 00125 name_car = "_DrifterXX"; 00126 } else if(car == Sportscar) { 00127 name_car = "_SportsCar"; 00128 } else if(car == Racecar) { 00129 name_car = "_RaceCarXX"; 00130 } else if(car == Stupid) { 00131 name_car = "_StupidXXX"; 00132 } 00133 00134 string name = "/sd/ghost" + name_track + name_car + ".txt"; //Concatenate the strings to produce the file name 00135 SD_Write_Data(name, race_time, SD); //Write the data 00136 } 00137 00138 void Ghost::SD_Read(int track, int car, SDFileSystem &SD) //Produce the correct file name and read data 00139 { 00140 SD_Read_Time(track, car, SD); //Read the time from the SD card 00141 00142 string name_track; //Declare track string 00143 string name_car; //Declare car string 00144 00145 //For each type of track, set track string to the correct name 00146 if(track == Small) { 00147 name_track = "_Sml"; 00148 } else if(track == Medium) { 00149 name_track = "_Med"; 00150 } else if(track == Large) { 00151 name_track = "_Lrg"; 00152 } 00153 00154 //For each type of car, set car string to the correct name. 00155 //'X' is used to ensure all strings are the same length 00156 if(car == Basic) { 00157 name_car = "_BasicXXXX"; 00158 } else if(car == Offroad) { 00159 name_car = "_OffRoadXX"; 00160 } else if(car == Drifter) { 00161 name_car = "_DrifterXX"; 00162 } else if(car == Sportscar) { 00163 name_car = "_SportsCar"; 00164 } else if(car == Racecar) { 00165 name_car = "_RaceCarXX"; 00166 } else if(car == Stupid) { 00167 name_car = "_StupidXXX"; 00168 } 00169 00170 string name = "/sd/ghost" + name_track + name_car + ".txt"; //Concatenate the strings to produce the file name 00171 SD_Read_Data(name, Ghost_Held_Data_Time, SD);//Read the data 00172 } 00173 00174 void Ghost::SD_Write_Data(string name, int race_time, SDFileSystem &SD) //Write Ghost data to the given file 00175 { 00176 char name_char[27]; //Create the char array for the file name 00177 00178 //Conver string name to char array 00179 for(int i = 0; i < 27; i++) { 00180 name_char[i] = name[i]; 00181 } 00182 00183 FILE *file_data; //Create file pointer 00184 wait(0.1); 00185 file_data = fopen(name_char, "w"); //Open file for writing 00186 if (file_data == NULL) { //If file fails to open 00187 while(1); 00188 } else { //Else 00189 for(int i = 0; i < race_time; i++) { //Write Ghost_Data to SD up to the race_time for the ghost_Data 00190 fprintf(file_data, "%f,%f\n",Ghost_Data[i].x,Ghost_Data[i].y); 00191 #ifdef DEBUG_GHOST_WRITE 00192 printf("%f,%f\n", Ghost_Data[i].x, Ghost_Data[i].y); 00193 #endif 00194 } 00195 for(int i = 0; i < (6000 - race_time); i++) { //For the remaining time 00196 float end_point = 10000; 00197 fprintf(file_data, "%f,%f\n",end_point,end_point); //Set the position of the ghost to be way off map 00198 } 00199 fclose(file_data); //Close the file 00200 } 00201 } 00202 00203 void Ghost::SD_Write_Time(int track, int car, int race_time, SDFileSystem &SD) 00204 { 00205 FILE *file_time_read; //Create pointer 00206 wait(0.1); 00207 00208 file_time_read = fopen("/sd/ghost_data.txt", "r"); //Open reading from file 00209 int track_time_array[3][6]; //Create an array 00210 00211 if(file_time_read == NULL) { //If file failed to open 00212 while(1); 00213 } else { //Else 00214 fscanf( 00215 file_time_read, 00216 "%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i", 00217 &track_time_array[0][0], 00218 &track_time_array[0][1], 00219 &track_time_array[0][2], 00220 &track_time_array[0][3], 00221 &track_time_array[0][4], 00222 &track_time_array[0][5], 00223 &track_time_array[1][0], 00224 &track_time_array[1][1], 00225 &track_time_array[1][2], 00226 &track_time_array[1][3], 00227 &track_time_array[1][4], 00228 &track_time_array[1][5], 00229 &track_time_array[2][0], 00230 &track_time_array[2][1], 00231 &track_time_array[2][2], 00232 &track_time_array[2][3], 00233 &track_time_array[2][4], 00234 &track_time_array[2][5] 00235 ); //Read first line from file (containing all times) 00236 } 00237 fclose(file_time_read); //Close file 00238 00239 track_time_array[track][car] = race_time; //Set the time for that track and car to the race_time 00240 00241 FILE *file_time_write; //Create pointer 00242 wait(0.1); 00243 00244 file_time_write = fopen("/sd/ghost_data.txt", "w"); //Open writing from file 00245 if(file_time_write == NULL) { //If file fails to open 00246 while(1); 00247 } else { 00248 fprintf( 00249 file_time_write, 00250 "%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i", 00251 track_time_array[0][0], 00252 track_time_array[0][1], 00253 track_time_array[0][2], 00254 track_time_array[0][3], 00255 track_time_array[0][4], 00256 track_time_array[0][5], 00257 track_time_array[1][0], 00258 track_time_array[1][1], 00259 track_time_array[1][2], 00260 track_time_array[1][3], 00261 track_time_array[1][4], 00262 track_time_array[1][5], 00263 track_time_array[2][0], 00264 track_time_array[2][1], 00265 track_time_array[2][2], 00266 track_time_array[2][3], 00267 track_time_array[2][4], 00268 track_time_array[2][5] 00269 ); //Write all timings 00270 } 00271 fclose(file_time_write); //Close file 00272 } 00273 00274 void Ghost::SD_Read_Data(string name, int race_time, SDFileSystem &SD) 00275 { 00276 //Create char name 00277 char name_char[27]; 00278 00279 //Convert from string to char 00280 for(int i = 0; i < name.length(); i++) { 00281 name_char[i] = name[i]; 00282 } 00283 00284 FILE *file_data; //Open pointer 00285 wait(0.1); 00286 file_data = fopen(name_char, "r"); //Open file with name 00287 float x_point = 10000; 00288 float y_point = 10000; 00289 int i = 0; 00290 if(file_data == NULL) { //If file fails to open 00291 while(1); 00292 } else { //Else 00293 for(int i = 0; i < race_time; i++) { //Up to the total race time 00294 fscanf(file_data, "%f,%f", &x_point, &y_point); //Read the point data for each frame 00295 Ghost_Held_Data[i].x = x_point; //Save each point to Ghost_Held_Data 00296 Ghost_Held_Data[i].y = y_point; 00297 #ifdef DEBUG_GHOST_READ 00298 printf("%f,%f\n", x_point, y_point); 00299 #endif 00300 } 00301 for(int i = 0; i < (6000 - race_time); i++) { //For the remaining time 00302 Ghost_Held_Data[(i + race_time)].x = 10000; //Set Ghost_Held_Data to 10000,10000 00303 Ghost_Held_Data[(i + race_time)].y = 10000; 00304 } 00305 fclose(file_data); //Close file 00306 } 00307 } 00308 00309 void Ghost::SD_Read_Time(int track, int car, SDFileSystem &SD) //Read the time for the given track and car 00310 { 00311 FILE *file_time; //Create pointer 00312 wait(0.1); 00313 00314 file_time = fopen("/sd/ghost_data.txt", "r"); //Open file 00315 int track_time_array[3][6]; //Create an array 00316 00317 if(file_time == NULL) { //If file fails to open 00318 while(1); 00319 } else { 00320 fscanf( 00321 file_time, 00322 "%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i", 00323 &track_time_array[0][0], 00324 &track_time_array[0][1], 00325 &track_time_array[0][2], 00326 &track_time_array[0][3], 00327 &track_time_array[0][4], 00328 &track_time_array[0][5], 00329 &track_time_array[1][0], 00330 &track_time_array[1][1], 00331 &track_time_array[1][2], 00332 &track_time_array[1][3], 00333 &track_time_array[1][4], 00334 &track_time_array[1][5], 00335 &track_time_array[2][0], 00336 &track_time_array[2][1], 00337 &track_time_array[2][2], 00338 &track_time_array[2][3], 00339 &track_time_array[2][4], 00340 &track_time_array[2][5] 00341 ); //Read from file 00342 } 00343 fclose(file_time); //Close file 00344 00345 Ghost_Held_Data_Time = track_time_array[track][car]; //Set Ghost_Held_Data_Time to the time for the track and car 00346 } 00347 00348 int Ghost::SD_Get_Time(int track, int car, SDFileSystem &SD) //Get the time for a given track and car 00349 { 00350 FILE *file_time; //Create pointer 00351 wait(0.1); 00352 00353 file_time = fopen("/sd/ghost_data.txt", "r"); //Open reading file 00354 int track_time_array[3][6]; //Create an array 00355 00356 if(file_time == NULL) { //If the file fails to open 00357 while(1); 00358 } else { 00359 fscanf( 00360 file_time, 00361 "%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i", 00362 &track_time_array[0][0], 00363 &track_time_array[0][1], 00364 &track_time_array[0][2], 00365 &track_time_array[0][3], 00366 &track_time_array[0][4], 00367 &track_time_array[0][5], 00368 &track_time_array[1][0], 00369 &track_time_array[1][1], 00370 &track_time_array[1][2], 00371 &track_time_array[1][3], 00372 &track_time_array[1][4], 00373 &track_time_array[1][5], 00374 &track_time_array[2][0], 00375 &track_time_array[2][1], 00376 &track_time_array[2][2], 00377 &track_time_array[2][3], 00378 &track_time_array[2][4], 00379 &track_time_array[2][5] 00380 ); //Read from the file 00381 } 00382 fclose(file_time); //Close file 00383 00384 return track_time_array[track][car]; //Return the time for a given track and car 00385 }
Generated on Wed Jul 20 2022 17:42:24 by
1.7.2