00001
00002
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>
00015 #include <OFACURES.H>
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
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
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128 static int loaded_random_seed;
00129
00130
00131
00132 File* GameFile::file_ptr;
00133 char GameFile::last_read_success_flag=0;
00134
00135
00142
00143 File file;
00144 String errStr;
00145
00146 power.win_opened=1;
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,
00158 §orPerCluster, &bytePerSector, &freeCluster, &totalCluster)) {
00159 DWORD freeSpace = DWORD( (double)freeCluster * sectorPerCluster * bytePerSector / 1024.0);
00160
00161 if( m.is_file_exist(file_name) ) {
00162
00163 file.file_open(file_name);
00164 freeSpace += file.file_size() / 1024;
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 );
00175
00176 if( !rc )
00177 errStr = "Error creating saved game file.";
00178 }
00179
00180 File* filePtr = &file;
00181 file_ptr = &file;
00182
00183
00184
00185 if( rc ) {
00186
00187
00188 rc = write_game_header(file_ptr);
00189
00190 if( game_file_array.demo_format )
00191 file_ptr->file_put_short( -GAME_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
00200 write_obj(player_school, 1);
00201
00202 if( !rc )
00203 errStr = "Error writing saved game data.";
00204 }
00205 }
00206
00207
00208 file.file_close();
00209
00210 power.win_opened=0;
00211
00212
00213
00214 if( !rc ) {
00215 if( !lowDiskSpaceFlag )
00216 remove( file_name );
00217
00218 #ifndef DEBUG
00219
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
00230
00231
00239
00240 File file;
00241 int rc=0;
00242 char *errMsg = NULL;
00243
00244 power.win_opened=1;
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 ) ) {
00256 rc = 0;
00257 errMsg = "Cannot open save game file";
00258 }
00259
00260
00261
00262 if( rc ) {
00263 char gameFileName[MAX_PATH+1];
00264
00265 strcpy( gameFileName, file_name );
00266
00267
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 )
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
00288 File* filePtr = &file;
00289 file_ptr = &file;
00290
00291
00292
00293
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) {
00312 case 0:
00313 case -1:
00314 box.msg( errMsg );
00315 break;
00316 }
00317
00318 last_read_success_flag = rc;
00319
00320 return rc;
00321 }
00322
00323
00324
00325
00332
00333 file_ptr = filePtr;
00334
00335
00336
00337 if( game_file_array.demo_format )
00338 filePtr->file_put_short( -GAME_VERSION );
00339 else
00340 filePtr->file_put_short( GAME_VERSION );
00341
00342
00343
00344
00345
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
00371
00372
00380
00381 file_ptr = filePtr;
00382
00383
00384
00385
00386
00387 battle.create_static_game_object();
00388
00389
00390
00391 int originalRandomSeed = m.get_random_seed();
00392
00393
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
00400
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;
00427
00428 m.set_random_seed(loaded_random_seed);
00429
00430 return 1;
00431 }
00432
00433
00434
00435
00440
00441
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);
00450
00451 write_obj(faculty_res, 5);
00452
00453 return 1;
00454 }
00455
00456
00457
00458
00465
00466
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) )
00476 return 0;
00477
00478 write_book_mark( BOOK_MARK+103 );
00479
00480 if( !info.write_file(filePtr) )
00481 return 0;
00482
00483 write_book_mark( BOOK_MARK+104 );
00484
00485 if( !finance.write_file(filePtr) )
00486 return 0;
00487
00488 write_book_mark( BOOK_MARK+105 );
00489
00490 if( !player_school.write_file(filePtr) )
00491 return 0;
00492
00493 return 1;
00494 }
00495
00496
00497
00498
00503
00504
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
00523
00524
00529
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);
00537
00538 read_obj(faculty_res, 5);
00539
00540 return 1;
00541 }
00542
00543
00544
00545
00550
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) )
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
00577
00578
00583
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
00602
00603
00605
00606 sys.yield();
00607
00608 file_ptr->file_put_short(bookMark);
00609 }
00610
00611
00612
00613
00618
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