Virtual U.org
Get Personal Training on VU Today
    
Top shadow
 
 register/help
User Name:

Password:

OGFILE2.CPP Source File
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

OGFILE2.CPP

Go to the documentation of this file.
00001 //Filename    : OGFILE2.CPP
00002 //Description : Object Game file, save game and restore game, part 2
00003 
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 
00007 #include <OSYS.H>
00008 #include <OBATTLE.H>
00009 
00010 //---------------------------//
00011 #include <ODEPTRES.H>
00012 #include <OSCHLRES.H>
00013 #include <OENROLL.H>
00014 #include <OLETTER.H>                              //##chea 990519
00015 #include <OFACURES.H>                             //## chea 090699
00016 
00017 //---------------------------//
00018 #include <OCONFIG.H>
00019 #include <OGAME.H>
00020 #include <OINFO.H>
00021 #include <OFINANCE.H>
00022 #include <OPSCHOOL.H>
00023 #include <OPOWER.H>
00024 
00025 #include <OBOX.H>
00026 #include <OSTR.H>
00027 #include <ODATE.H>
00028 #include <OMOUSECR.H>
00029 #include <OWORLD.H>
00030 #include <OGAME.H>
00031 #include <OINFO.H>
00032 #include <OAUDIO.H>
00033 #include <OMUSIC.H>
00034 
00035 //---------------------------//
00036 #include <ODEPT.H>
00037 #include <ONEWS.H>
00038 #include <OTASK.H>
00039 #include <OCHANCE.H>
00040 
00041 #include <OATHLETI.H>
00042 #include <ODEVELOP.H>
00043 #include <OFACILIT.H>
00044 #include <OLIBTECH.H>
00045 #include <OINVEST.H>
00046 #include <OSTUOFF.H>
00047 
00048 //---------------------------//
00049 #include <OGFILE.H>
00050 
00051 //-------- Define constant ----------//
00052 
00053 #define BOOK_MARK 0x1000                          // book mark for validing saving data
00054 
00055 #define read_obj(obj, mark)\
00056 { \
00057       if( !read_book_mark( BOOK_MARK+(mark) ) )\
00058                                                    return 0;\
00059                                                                 if( !((obj).read_file(filePtr)) )\
00060                                                                                                      return 0;\
00061                                                                                                                   }
00062 
00063 #define write_obj(obj, mark)\
00064 { \
00065       write_book_mark( BOOK_MARK+(mark) );\
00066                                               if( (!(obj).write_file(filePtr)) )\
00067                                                                                     return 0;\
00068                                                                                                  }
00069 
00070 //----------- Game file format --------------//
00071 //
00072 // Data of the following objects will be saved: (yes/no)
00073 //
00074 // DepartmentRes                department_res;
00075 //
00076 //      n       ImageRes          image_sys, image_interface, image_mascot, image_pict;
00077 //      y       SchoolRes                       school_res;
00078 //      n       CourseRes                       course_res;
00079 //      n       FieldRes                                field_res;
00080 //      y       DepartmentRes           department_res;
00081 //      n       FirmRes                         firm_res;
00082 //      n       TerrainRes                      terrain_res;
00083 //      n       PlantRes                                plant_res;
00084 //      n       RoadRes                         road_res;
00085 //      n       SpriteRes                       sprite_res;
00086 //      y       FacultyRes                      faculty_res;
00087 //      n       FacultyPhotoRes faculty_photo_res;
00088 //      n       GameStrRes                      game_str_res;
00089 //      n       NameRes                         name_res;
00090 //      y       EnrollRes                       enroll_res;
00091 //      y       Letter                          letter;
00092 //      n       BlackBoard                      blackboard;
00093 //
00094 // y PlayerSchool               player_school;
00095 // n Battle            battle;
00096 // y Config            config;
00097 // y Game              game;
00098 // n GameSet           game_set;
00099 // y Finance                            finance;
00100 // y Info              info;
00101 // n Power             power;
00102 //   World             world;
00103 //
00104 //              FirmArray         firm_array;                   // call battle.generate_static_objects() instead
00105 //      n       SpriteArray                     sprite_array;
00106 //      y       DepartmentArray department_array;
00107 //      y       NewsArray                       news_array;
00108 //      y       TaskArray                       task_array;
00109 //      y       ChanceEvent                     chance_event;
00110 //
00111 //      n       UserInterface           user_interface;
00112 //      n       AdmOffice                       adm_office;
00113 //      y       Development                     development_office;
00114 //      n       Optimization            optimization;
00115 //      n       OptStage3         opt_stage3;
00116 //      y       Athletics                       athletics_office;
00117 //      y       LibTech                         library_tech_office;
00118 //      y       Facility                                facility_office;
00119 //      y       Investment                      investment_office;
00120 //      y       StudentOffice           student_office;         // fred 980918
00121 //      n       Help                                    help;
00122 //      n       Test                                    test;
00123 //      n       GameSetting                     game_setting;
00124 //-------------------------------------------//
00125 
00126 //--------- Define static vars ---------//
00127 
00128 static int loaded_random_seed;
00129 
00130 //-------- Define static class member vars -------//
00131 
00132 File* GameFile::file_ptr;
00133 char  GameFile::last_read_success_flag=0;
00134 
00135 //-------- Begin of function GameFile::write_express_file -------//
00142 int GameFile::save_express_game(char* fileName) {
00143     File   file;
00144     String errStr;
00145 
00146     power.win_opened=1;                             // to disable power.mouse_handler()
00147 
00148     if( fileName )
00149         strcpy( file_name, fileName );
00150 
00151     int rc = 1;
00152     char lowDiskSpaceFlag = 0;
00153     DWORD sectorPerCluster = 0;
00154     DWORD bytePerSector = 0;
00155     DWORD freeCluster = 0;
00156     DWORD totalCluster = 0;
00157     if( GetDiskFreeSpace( NULL,                     // address of root path, NULL means the current root directory
00158                           &sectorPerCluster, &bytePerSector, &freeCluster, &totalCluster)) {
00159         DWORD freeSpace = DWORD( (double)freeCluster * sectorPerCluster * bytePerSector / 1024.0);
00160 
00161         if( m.is_file_exist(file_name) ) {
00162             // if overwritting existing file, count the file size of the existing file
00163             file.file_open(file_name);
00164             freeSpace += file.file_size() / 1024;       // count the existing space
00165             file.file_close();
00166         }
00167         if( !(rc = freeSpace >= MIN_FREE_SPACE) ) {
00168             errStr = "Insufficient disk space ! The game is not saved.";
00169             lowDiskSpaceFlag = 1;
00170         }
00171     }
00172 
00173     if( rc ) {
00174         rc = file.file_create( file_name, 0, 1 );     // 0=tell File don't handle error itself
00175         // 1=allow the writing size and the read size to be different
00176         if( !rc )
00177             errStr = "Error creating saved game file.";
00178     }
00179 
00180     File* filePtr = &file;
00181     file_ptr = &file;                               // for write_book_mark()
00182 
00183     //----- check valid version first ------//
00184 
00185     if( rc ) {
00186         //              save_process();      // process game data before saving the game
00187 
00188         rc = write_game_header(file_ptr);             // write saved game header information
00189 
00190         if( game_file_array.demo_format )
00191             file_ptr->file_put_short( -GAME_VERSION );  // negative no. means shareware version
00192         else
00193             file_ptr->file_put_short( GAME_VERSION );
00194 
00195         if( !rc )
00196             errStr = "Error creating saved game header.";
00197 
00198         if( rc ) {
00199             // put data to file here
00200             write_obj(player_school, 1);
00201 
00202             if( !rc )
00203                 errStr = "Error writing saved game data.";
00204         }
00205     }
00206 
00207     //------------Close file----------------------------//
00208     file.file_close();
00209 
00210     power.win_opened=0;
00211 
00212     //------- when saving error ---------//
00213 
00214     if( !rc ) {
00215         if( !lowDiskSpaceFlag )
00216             remove( file_name );                        // delete the file as it is not complete
00217 
00218 #ifndef DEBUG
00219         // use this message for all types of error message in the release version
00220         errStr = "Insufficient disk space ! The game is not saved.";
00221 #endif
00222 
00223         box.msg( errStr );
00224     }
00225 
00226     return rc;
00227 }
00228 
00229 //---------- End of function GameFile::save_express_game -------//
00230 
00231 //-------- Begin of function GameFile::load_express_game -------//
00239 int GameFile::load_express_game(char* fileName) {
00240     File file;
00241     int  rc=0;
00242     char *errMsg = NULL;
00243 
00244     power.win_opened=1;                             // to disable power.mouse_handler()
00245 
00246     int oldCursor = mouse_cursor.get_icon();
00247     mouse_cursor.set_icon( CURSOR_WAITING );
00248 
00249     int powerEnableFlag = power.enable_flag;
00250 
00251     if( fileName )
00252         strcpy( file_name, fileName );
00253 
00254     rc = 1;
00255     if( !file.file_open( file_name, 0, 1 ) ) {      // 0=tell File don't handle error itself
00256         rc = 0;
00257         errMsg = "Cannot open save game file";
00258     }
00259 
00260     //-------- read in the GameFile class --------//
00261 
00262     if( rc ) {
00263         char gameFileName[MAX_PATH+1];
00264 
00265         strcpy( gameFileName, file_name );            // save the file name actually read, in case that the file names are different
00266 
00267         // read_file_header: read the whole object from the saved game file
00268         if( !file.file_read(this, sizeof(GameFile)) ) {
00269             rc = 0;
00270             errMsg = "Cannot read file header";
00271         }
00272 
00273         if( rc ) {
00274             err_if ( file.file_get_short() != 0x99 )    // the end of header
00275                 err_here();
00276 
00277             if( !validate_header() ) {
00278                 rc = 0;
00279                 errMsg = "Save game incompatible";
00280             }
00281             else
00282                 strcpy( file_name, gameFileName );
00283         }
00284     }
00285 
00286     //--------------------------------------------//
00287     //----- check version no. first ------//
00288     File* filePtr = &file;
00289     file_ptr = &file;                               // for read_book_mark()
00290 
00291     //-------------------------------------//
00292 
00293     // compare major version
00294     if( file_ptr->file_get_short()/100 != (game_file_array.demo_format ? -(GAME_VERSION/100) : GAME_VERSION/100) )
00295         return -1;
00296 
00297     read_obj(player_school, 1);
00298 
00299     //-------------------------------------//
00300 
00301     file.file_close();
00302 
00303     power.enable_flag = powerEnableFlag;
00304 
00305     mouse_cursor.restore_icon( oldCursor );
00306 
00307     power.win_opened=0;
00308 
00309     //---------------------------------------//
00310 
00311     switch(rc) {                                    // don't display msg if loaded successfully (rc==1)
00312     case 0:
00313     case -1:
00314         box.msg( errMsg );
00315         break;
00316     }
00317 
00318     last_read_success_flag = rc;                    // for external functions to read.
00319 
00320     return rc;
00321 }
00322 
00323 //---------- End of function GameFile::load_express_file -------//
00324 
00325 //-------- Begin of function GameFile::write_file -------//
00332 int GameFile::write_file(File* filePtr) {
00333     file_ptr = filePtr;                             // for write_book_mark()
00334 
00335     //----- check valid version first ------//
00336 
00337     if( game_file_array.demo_format )
00338         filePtr->file_put_short( -GAME_VERSION );     // negative no. means shareware version
00339     else
00340         filePtr->file_put_short( GAME_VERSION );
00341 
00342     //------------------------------------------------//
00343     //
00344     // The order of writing data is different between
00345     // the shareware and registered version.
00346     //
00347     //------------------------------------------------//
00348 
00349     if( game_file_array.demo_format ) {
00350         if( !write_file_1(filePtr) )
00351             return 0;
00352 
00353         if( !write_file_2(filePtr) )
00354             return 0;
00355     }
00356     else {
00357         if( !write_file_2(filePtr) )
00358             return 0;
00359 
00360         if( !write_file_1(filePtr) )
00361             return 0;
00362     }
00363 
00364     if( !write_file_3(filePtr) )
00365         return 0;
00366 
00367     return 1;
00368 }
00369 
00370 //---------- End of function GameFile::write_file -------//
00371 
00372 //-------- Begin of function GameFile::read_file -------//
00380 int GameFile::read_file(File* filePtr) {
00381     file_ptr = filePtr;                             // for read_book_mark()
00382 
00383     //----- check version no. first ------//
00384 
00385     //-------------------------------------//
00386 
00387     battle.create_static_game_object();             // in order to call deptPtr->create_department_firm() later; since we don't save firm_array
00388 
00389     //-------------------------------------//
00390 
00391     int originalRandomSeed = m.get_random_seed();
00392 
00393     // compare major version
00394     if( filePtr->file_get_short()/100 != (game_file_array.demo_format ? -(GAME_VERSION/100) : GAME_VERSION/100) )
00395         return -1;
00396 
00397     //------------------------------------------------//
00398     //
00399     // The order of writing data is different between
00400     // the shareware and registered version.
00401     //
00402     //------------------------------------------------//
00403 
00404     if( game_file_array.demo_format ) {
00405         if( !read_file_1(filePtr) )
00406             return 0;
00407 
00408         if( !read_file_2(filePtr) )
00409             return 0;
00410     }
00411     else {
00412         if( !read_file_2(filePtr) )
00413             return 0;
00414 
00415         if( !read_file_1(filePtr) )
00416             return 0;
00417     }
00418 
00419     if( !read_file_3(filePtr) )
00420         return 0;
00421 
00422     //-------------------------------------//
00423 
00424     err_when( originalRandomSeed != m.get_random_seed() );
00425 
00426     loaded_random_seed = info.random_seed_4_save;   //## chea 210899 to load the random seed
00427 
00428     m.set_random_seed(loaded_random_seed);
00429 
00430     return 1;
00431 }
00432 
00433 //---------- End of function GameFile::read_file -------//
00434 
00435 //-------- Begin of function GameFile::write_file_1 -------//
00440 int GameFile::write_file_1(File* filePtr) {
00441     // write *_res objects
00442 
00443     write_obj(department_res, 1);
00444 
00445     write_obj(school_res, 2);
00446 
00447     write_obj(enroll_res, 3);
00448 
00449     write_obj(letter, 4);                           //## chea 990519
00450 
00451     write_obj(faculty_res, 5);                      //## chea 090699
00452 
00453     return 1;
00454 }
00455 
00456 //---------- End of function GameFile::write_file_1 -------//
00457 
00458 //-------- Begin of function GameFile::write_file_2 -------//
00465 int GameFile::write_file_2(File* filePtr) {
00466     // write game surface objects
00467 
00468     write_book_mark( BOOK_MARK+101 );
00469 
00470     if( !game.write_file(filePtr) )
00471         return 0;
00472 
00473     write_book_mark( BOOK_MARK+102 );
00474 
00475     if( !config.write_file(filePtr) )               // 1110
00476         return 0;
00477 
00478     write_book_mark( BOOK_MARK+103 );
00479 
00480     if( !info.write_file(filePtr) )                 // 1110
00481         return 0;
00482 
00483     write_book_mark( BOOK_MARK+104 );
00484 
00485     if( !finance.write_file(filePtr) )              // 1110
00486         return 0;
00487 
00488     write_book_mark( BOOK_MARK+105 );
00489 
00490     if( !player_school.write_file(filePtr) )        // 1110
00491         return 0;
00492 
00493     return 1;
00494 }
00495 
00496 //---------- End of function GameFile::write_file_2 -------//
00497 
00498 //-------- Begin of function GameFile::write_file_3 -------//
00503 int GameFile::write_file_3(File* filePtr) {
00504     // write game data objects
00505 
00506     write_obj(department_array, 201);
00507 
00508     write_obj(news_array, 202);
00509     write_obj(task_array, 203);
00510     write_obj(chance_event, 204);
00511 
00512     write_obj(development_office, 205);
00513     write_obj(athletics_office, 206);
00514     write_obj(library_tech_office, 207);
00515     write_obj(facility_office, 208);
00516     write_obj(investment_office, 209);
00517     write_obj(student_office, 210);
00518 
00519     return 1;
00520 }
00521 
00522 //---------- End of function GameFile::write_file_3 -------//
00523 
00524 //-------- Begin of function GameFile::read_file_1 -------//
00529 int GameFile::read_file_1(File* filePtr) {
00530     read_obj(department_res, 1);
00531 
00532     read_obj(school_res, 2);
00533 
00534     read_obj(enroll_res, 3);
00535 
00536     read_obj(letter, 4);                            //## chea 990519
00537 
00538     read_obj(faculty_res, 5);                       //## chea 090699
00539 
00540     return 1;
00541 }
00542 
00543 //---------- End of function GameFile::read_file_1 -------//
00544 
00545 //-------- Begin of function GameFile::read_file_2 -------//
00550 int GameFile::read_file_2(File* filePtr) {
00551     if( !read_book_mark( BOOK_MARK+101 ) )
00552         return 0;
00553 
00554     if( !game.read_file(filePtr) )
00555         return 0;
00556 
00557     if( !read_book_mark( BOOK_MARK+102 ) )
00558         return 0;
00559 
00560     if( !config.read_file(filePtr, 1) )             // 1-keep system settings
00561         return 0;
00562 
00563     if( !read_book_mark( BOOK_MARK+103 ) )
00564         return 0;
00565 
00566     if( !info.read_file(filePtr) )
00567         return 0;
00568 
00569     read_obj(finance, 104);
00570 
00571     read_obj(player_school, 105)
00572 
00573         return 1;
00574 }
00575 
00576 //---------- End of function GameFile::read_file_2 -------//
00577 
00578 //-------- Begin of function GameFile::read_file_3 -------//
00583 int GameFile::read_file_3(File* filePtr) {
00584 
00585     read_obj(department_array, 201);
00586 
00587     read_obj(news_array, 202);
00588     read_obj(task_array, 203);
00589     read_obj(chance_event, 204);
00590 
00591     read_obj(development_office, 205);
00592     read_obj(athletics_office, 206);
00593     read_obj(library_tech_office, 207);
00594     read_obj(facility_office, 208);
00595     read_obj(investment_office, 209);
00596     read_obj(student_office, 210);
00597 
00598     return 1;
00599 }
00600 
00601 //---------- End of function GameFile::read_file_3 -------//
00602 
00603 //-------- Begin of function GameFile::write_book_mark -------//
00605 void GameFile::write_book_mark(short bookMark) {
00606     sys.yield();
00607 
00608     file_ptr->file_put_short(bookMark);
00609 }
00610 
00611 //---------- End of function GameFile::write_book_mark -------//
00612 
00613 //-------- Begin of function GameFile::read_book_mark -------//
00618 int GameFile::read_book_mark(short bookMark) {
00619     sys.yield();
00620 
00621     short chk = file_ptr->file_get_short();
00622 
00623 #ifdef DEBUG
00624     err_when( chk != bookMark);
00625 #endif
00626 
00627     return chk == bookMark;
00628 }
00629 
00630 //---------- End of function GameFile::read_book_mark -------//

Generated on Fri Aug 23 01:37:46 2002 for VirtualU by doxygen1.2.17