OGFILE2.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  1. /*
  2. * Seven Kingdoms: Ancient Adversaries
  3. *
  4. * Copyright 1997,1998 Enlight Software Ltd.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. //Filename : OGFILE2.CPP
  21. //Description : Object Game file, save game and restore game, part 2
  22. #include <OUNITRES.h>
  23. #include <OFIRMRES.h>
  24. #include <OUNIT.h>
  25. #include <OBULLET.h>
  26. #include <OSITE.h>
  27. #include <OTOWN.h>
  28. #include <ONATION.h>
  29. #include <OFIRM.h>
  30. #include <OTECHRES.h>
  31. #include <ORACERES.h>
  32. #include <OTOWNRES.h>
  33. #include <ORAWRES.h>
  34. #include <OGODRES.h>
  35. #include <OTALKRES.h>
  36. #include <OTORNADO.h>
  37. #include <OMONSRES.h>
  38. #include <OREBEL.h>
  39. #include <OSPY.h>
  40. #include <OSNOWG.h>
  41. #include <OSYS.h>
  42. #include <OREGION.h>
  43. #include <ONEWS.h>
  44. #include <OTUTOR.h>
  45. #include <OINFO.h>
  46. #include <OWEATHER.h>
  47. #include <OGAME.h>
  48. #include <OPOWER.h>
  49. #include <OWORLD.h>
  50. #include <OGFILE.h>
  51. #include <OROCK.h>
  52. // ##### begin Gilbert 2/10 #######//
  53. #include <OFIRMDIE.h>
  54. // ##### end Gilbert 2/10 #######//
  55. //-------- Define constant ----------//
  56. #define BOOK_MARK 0x1000 // book mark for validing saving data
  57. //----------- Game file format --------------//
  58. //
  59. // Data of the following objects will be saved :
  60. //
  61. // RaceRes race_res;
  62. // UnitRes unit_res;
  63. // FirmRes firm_res;
  64. // TownRes town_res;
  65. // TechRes tech_res;
  66. // TalkRes talk_res;
  67. // RawRes raw_res;
  68. // GodRes god_res;
  69. // MonsterRes monster_res;
  70. //
  71. // UnitArray unit_array(100); // with derived classes
  72. // BulletArray bullet_array(100);
  73. // SiteArray site_array;
  74. // TownArray town_array;
  75. // NationArray nation_array;
  76. // FirmArray firm_array; // with derived classes
  77. // TornadoArray tornado_array(10);
  78. // RebelArray rebel_array;
  79. // SpyArray spy_array;
  80. // SnowGroundArray snow_ground_array;
  81. // RegionArray region_array;
  82. // NewsArray news_array;
  83. //
  84. // Game game
  85. // Config config
  86. // Sys sys
  87. // Info info
  88. // Power power
  89. // Weather weather, weather_forecast[MAX_WEATHER_FORECAST];
  90. // MagicWeather magic_weather
  91. // News news
  92. // World world
  93. // Tutor tutor
  94. //### begin alex 23/9 ###//
  95. // SeekPath seek_path
  96. //#### end alex 23/9 ####//
  97. //
  98. //-------------------------------------------//
  99. //--------- Define static vars ---------//
  100. static int loaded_random_seed;
  101. //-------- Define static class member vars -------//
  102. File* GameFile::file_ptr;
  103. char GameFile::last_read_success_flag=0;
  104. //-------- Begin of function GameFile::write_file -------//
  105. //
  106. // Save a game to file
  107. //
  108. // Return : 1 - file written successfully
  109. // 0 - not successful
  110. //
  111. int GameFile::write_file(File* filePtr)
  112. {
  113. file_ptr = filePtr; // for write_book_mark()
  114. //----- check valid version first ------//
  115. if( game_file_array.demo_format )
  116. filePtr->file_put_short( -GAME_VERSION ); // negative no. means shareware version
  117. else
  118. filePtr->file_put_short( GAME_VERSION );
  119. //------------------------------------------------//
  120. //
  121. // The order of writing data is different between
  122. // the shareware and registered version.
  123. //
  124. //------------------------------------------------//
  125. if( game_file_array.demo_format )
  126. {
  127. if( !write_file_1(filePtr) )
  128. return 0;
  129. if( !write_file_2(filePtr) )
  130. return 0;
  131. }
  132. else
  133. {
  134. if( !write_file_2(filePtr) )
  135. return 0;
  136. if( !write_file_1(filePtr) )
  137. return 0;
  138. }
  139. if( !write_file_3(filePtr) )
  140. return 0;
  141. return 1;
  142. }
  143. //---------- End of function GameFile::write_file -------//
  144. //-------- Begin of function GameFile::read_file -------//
  145. //
  146. // Restore a game from file
  147. //
  148. // Return : 1 - file read successfully
  149. // 0 - not successful
  150. // -1 - incorrect game data version error
  151. //
  152. int GameFile::read_file(File* filePtr)
  153. {
  154. file_ptr = filePtr; // for read_book_mark()
  155. //----- check version no. first ------//
  156. int originalRandomSeed = m.get_random_seed();
  157. //### begin alex 5/3 ###//
  158. #ifdef AMPLUS
  159. // game_file_array.load_file_game_version = filePtr->file_get_short();
  160. game_file_array.load_file_game_version = filePtr->file_get_short();
  161. // compare if same demo format or not
  162. if( game_file_array.demo_format && game_file_array.load_file_game_version > 0
  163. || !game_file_array.demo_format && game_file_array.load_file_game_version < 0)
  164. return -1;
  165. // take the absolute value of game version
  166. game_file_array.load_file_game_version = abs(game_file_array.load_file_game_version);
  167. if(game_file_array.load_file_game_version > GAME_VERSION)
  168. return -1; // the executing program can't handle saved game in future version
  169. // game_file_array.same_version = (game_file_array.load_file_game_version/100==
  170. // (game_file_array.demo_format ? -(GAME_VERSION/100) : GAME_VERSION/100));
  171. game_file_array.same_version = ( game_file_array.load_file_game_version/100==GAME_VERSION/100 );
  172. #else
  173. // ###### patch begin Gilbert 20/1 ########//
  174. // compare major version
  175. //if( filePtr->file_get_short() != (game_file_array.demo_format ? -GAME_VERSION : GAME_VERSION) )
  176. if( filePtr->file_get_short()/100 != (game_file_array.demo_format ? -(GAME_VERSION/100) : GAME_VERSION/100) )
  177. return -1;
  178. // ###### patch end Gilbert 20/1 ########//
  179. #endif
  180. //#### end alex 5/3 ####//
  181. //------------------------------------------------//
  182. //
  183. // The order of writing data is different between
  184. // the shareware and registered version.
  185. //
  186. //------------------------------------------------//
  187. if( game_file_array.demo_format )
  188. {
  189. if( !read_file_1(filePtr) )
  190. return 0;
  191. if( !read_file_2(filePtr) )
  192. return 0;
  193. }
  194. else
  195. {
  196. if( !read_file_2(filePtr) )
  197. return 0;
  198. if( !read_file_1(filePtr) )
  199. return 0;
  200. }
  201. if( !read_file_3(filePtr) )
  202. return 0;
  203. //-------------------------------------//
  204. err_when( originalRandomSeed != m.get_random_seed() );
  205. m.set_random_seed(loaded_random_seed);
  206. return 1;
  207. }
  208. //---------- End of function GameFile::read_file -------//
  209. //-------- Begin of function GameFile::write_file_1 -------//
  210. //
  211. // Return : 1 - file written successfully
  212. // 0 - not successful
  213. //
  214. int GameFile::write_file_1(File* filePtr)
  215. {
  216. write_book_mark( BOOK_MARK+1 );
  217. if( !race_res.write_file(filePtr) )
  218. return 0;
  219. write_book_mark( BOOK_MARK+2 );
  220. if( !unit_res.write_file(filePtr) )
  221. return 0;
  222. write_book_mark( BOOK_MARK+3 );
  223. if( !firm_res.write_file(filePtr) )
  224. return 0;
  225. write_book_mark( BOOK_MARK+4 );
  226. if( !town_res.write_file(filePtr) )
  227. return 0;
  228. write_book_mark( BOOK_MARK+5 );
  229. if( !tech_res.write_file(filePtr) )
  230. return 0;
  231. write_book_mark( BOOK_MARK+6 );
  232. if( !talk_res.write_file(filePtr) )
  233. return 0;
  234. write_book_mark( BOOK_MARK+7 );
  235. if( !raw_res.write_file(filePtr) )
  236. return 0;
  237. write_book_mark( BOOK_MARK+8 );
  238. if( !god_res.write_file(filePtr) )
  239. return 0;
  240. write_book_mark( BOOK_MARK+9 );
  241. if( !monster_res.write_file(filePtr) )
  242. return 0;
  243. return 1;
  244. }
  245. //---------- End of function GameFile::write_file_1 -------//
  246. //-------- Begin of function GameFile::write_file_2 -------//
  247. //
  248. // Save a game to file
  249. //
  250. // Return : 1 - file written successfully
  251. // 0 - not successful
  252. //
  253. int GameFile::write_file_2(File* filePtr)
  254. {
  255. write_book_mark( BOOK_MARK+101 );
  256. if( !game.write_file(filePtr) )
  257. return 0;
  258. write_book_mark( BOOK_MARK+102 );
  259. if( !config.write_file(filePtr) )
  260. return 0;
  261. write_book_mark( BOOK_MARK+103 );
  262. if( !sys.write_file(filePtr) )
  263. return 0;
  264. write_book_mark( BOOK_MARK+104 );
  265. if( !info.write_file(filePtr) )
  266. return 0;
  267. write_book_mark( BOOK_MARK+105 );
  268. if( !power.write_file(filePtr) )
  269. return 0;
  270. write_book_mark( BOOK_MARK+106 );
  271. if( !weather.write_file(filePtr) )
  272. return 0;
  273. write_book_mark( BOOK_MARK+107 );
  274. if( !magic_weather.write_file(filePtr) )
  275. return 0;
  276. write_book_mark( BOOK_MARK+108 );
  277. if( !news_array.write_file(filePtr) )
  278. return 0;
  279. write_book_mark( BOOK_MARK+109 );
  280. if( !world.write_file(filePtr) )
  281. return 0;
  282. write_book_mark( BOOK_MARK+110 );
  283. if( !tutor.write_file(filePtr) )
  284. return 0;
  285. //### begin alex 23/9 ###//
  286. write_book_mark( BOOK_MARK+111 );
  287. if( !seek_path.write_file(filePtr) )
  288. return 0;
  289. //#### end alex 23/9 ####//
  290. return 1;
  291. }
  292. //---------- End of function GameFile::write_file_2 -------//
  293. //-------- Begin of function GameFile::write_file_3 -------//
  294. //
  295. // Return : 1 - file written successfully
  296. // 0 - not successful
  297. //
  298. int GameFile::write_file_3(File* filePtr)
  299. {
  300. write_book_mark( BOOK_MARK+201 );
  301. if( !unit_array.write_file(filePtr) )
  302. return 0;
  303. write_book_mark( BOOK_MARK+202 );
  304. if( !bullet_array.write_file(filePtr) )
  305. return 0;
  306. write_book_mark( BOOK_MARK+203 );
  307. if( !site_array.write_file(filePtr) )
  308. return 0;
  309. write_book_mark( BOOK_MARK+204 );
  310. if( !town_array.write_file(filePtr) ) // job will affect firm, group, item
  311. return 0;
  312. write_book_mark( BOOK_MARK+205 );
  313. if( !nation_array.write_file(filePtr) )
  314. return 0;
  315. write_book_mark( BOOK_MARK+206 );
  316. if( !firm_array.write_file(filePtr) )
  317. return 0;
  318. write_book_mark( BOOK_MARK+207 );
  319. if( !tornado_array.write_file(filePtr) )
  320. return 0;
  321. write_book_mark( BOOK_MARK+208 );
  322. if( !rebel_array.write_file(filePtr) )
  323. return 0;
  324. write_book_mark( BOOK_MARK+209 );
  325. if( !spy_array.write_file(filePtr) )
  326. return 0;
  327. write_book_mark( BOOK_MARK+210 );
  328. if( !snow_ground_array.write_file(filePtr) )
  329. return 0;
  330. write_book_mark( BOOK_MARK+211 );
  331. if( !region_array.write_file(filePtr) )
  332. return 0;
  333. write_book_mark( BOOK_MARK+212 );
  334. if( !news_array.write_file(filePtr) )
  335. return 0;
  336. write_book_mark( BOOK_MARK+213 );
  337. if( !rock_array.write_file(filePtr) )
  338. return 0;
  339. write_book_mark( BOOK_MARK+214 );
  340. if( !dirt_array.write_file(filePtr) )
  341. return 0;
  342. // ##### begin Gilbert 2/10 ######//
  343. write_book_mark( BOOK_MARK+215 );
  344. if( !firm_die_array.write_file(filePtr) )
  345. return 0;
  346. // ##### end Gilbert 2/10 ######//
  347. return 1;
  348. }
  349. //---------- End of function GameFile::write_file_3 -------//
  350. //-------- Begin of function GameFile::read_file_1 -------//
  351. //
  352. // Return : 1 - file read successfully
  353. // 0 - not successful
  354. //
  355. int GameFile::read_file_1(File* filePtr)
  356. {
  357. if( !read_book_mark( BOOK_MARK+1 ) )
  358. return 0;
  359. if( !race_res.read_file(filePtr) )
  360. return 0;
  361. if( !read_book_mark( BOOK_MARK+2 ) )
  362. return 0;
  363. if( !unit_res.read_file(filePtr) )
  364. return 0;
  365. if( !read_book_mark( BOOK_MARK+3 ) )
  366. return 0;
  367. if( !firm_res.read_file(filePtr) )
  368. return 0;
  369. if( !read_book_mark( BOOK_MARK+4 ) )
  370. return 0;
  371. if( !town_res.read_file(filePtr) )
  372. return 0;
  373. if( !read_book_mark( BOOK_MARK+5 ) )
  374. return 0;
  375. if( !tech_res.read_file(filePtr) )
  376. return 0;
  377. if( !read_book_mark( BOOK_MARK+6 ) )
  378. return 0;
  379. if( !talk_res.read_file(filePtr) )
  380. return 0;
  381. if( !read_book_mark( BOOK_MARK+7 ) )
  382. return 0;
  383. if( !raw_res.read_file(filePtr) )
  384. return 0;
  385. if( !read_book_mark( BOOK_MARK+8 ) )
  386. return 0;
  387. if( !god_res.read_file(filePtr) )
  388. return 0;
  389. if( !read_book_mark( BOOK_MARK+9 ) )
  390. return 0;
  391. if( !monster_res.read_file(filePtr) )
  392. return 0;
  393. return 1;
  394. }
  395. //---------- End of function GameFile::read_file_1 -------//
  396. //-------- Begin of function GameFile::read_file_2 -------//
  397. //
  398. // Return : 1 - file read successfully
  399. // 0 - not successful
  400. //
  401. int GameFile::read_file_2(File* filePtr)
  402. {
  403. if( !read_book_mark( BOOK_MARK+101 ) )
  404. return 0;
  405. if( !game.read_file(filePtr) )
  406. return 0;
  407. if( !read_book_mark( BOOK_MARK+102 ) )
  408. return 0;
  409. if( !config.read_file(filePtr, 1) ) // 1-keep system settings
  410. return 0;
  411. if( !read_book_mark( BOOK_MARK+103 ) )
  412. return 0;
  413. if( !sys.read_file(filePtr) )
  414. return 0;
  415. if( !read_book_mark( BOOK_MARK+104 ) )
  416. return 0;
  417. if( !info.read_file(filePtr) )
  418. return 0;
  419. if( !read_book_mark( BOOK_MARK+105 ) )
  420. return 0;
  421. if( !power.read_file(filePtr) )
  422. return 0;
  423. if( !read_book_mark( BOOK_MARK+106 ) )
  424. return 0;
  425. if( !weather.read_file(filePtr) )
  426. return 0;
  427. weather_forecast[0] = weather;
  428. weather_forecast[0].next_day();
  429. for(int foreDay=1; foreDay < MAX_WEATHER_FORECAST; ++foreDay)
  430. {
  431. weather_forecast[foreDay] = weather_forecast[foreDay-1];
  432. weather_forecast[foreDay].next_day();
  433. }
  434. if( !read_book_mark( BOOK_MARK+107 ) )
  435. return 0;
  436. if( !magic_weather.read_file(filePtr) )
  437. return 0;
  438. sprite_res.update_speed();
  439. if( !read_book_mark( BOOK_MARK+108 ) )
  440. return 0;
  441. if( !news_array.read_file(filePtr) )
  442. return 0;
  443. if( !read_book_mark( BOOK_MARK+109 ) )
  444. return 0;
  445. if( !world.read_file(filePtr) )
  446. return 0;
  447. if( !read_book_mark( BOOK_MARK+110 ) )
  448. return 0;
  449. if( !tutor.read_file(filePtr) )
  450. return 0;
  451. //### begin alex 23/9 ###//
  452. if( !read_book_mark( BOOK_MARK+111 ) )
  453. return 0;
  454. if( !seek_path.read_file(filePtr) )
  455. return 0;
  456. //#### end alex 23/9 ####//
  457. return 1;
  458. }
  459. //---------- End of function GameFile::read_file_2 -------//
  460. //-------- Begin of function GameFile::read_file_3 -------//
  461. //
  462. // Return : 1 - file read successfully
  463. // 0 - not successful
  464. //
  465. int GameFile::read_file_3(File* filePtr)
  466. {
  467. if( !read_book_mark( BOOK_MARK+201 ) )
  468. return 0;
  469. if( !unit_array.read_file(filePtr) )
  470. return 0;
  471. if( !read_book_mark( BOOK_MARK+202 ) )
  472. return 0;
  473. if( !bullet_array.read_file(filePtr) )
  474. return 0;
  475. if( !read_book_mark( BOOK_MARK+203 ) )
  476. return 0;
  477. if( !site_array.read_file(filePtr) )
  478. return 0;
  479. if( !read_book_mark( BOOK_MARK+204 ) )
  480. return 0;
  481. if( !town_array.read_file(filePtr) ) // job will affect firm, group, item
  482. return 0;
  483. if( !read_book_mark( BOOK_MARK+205 ) )
  484. return 0;
  485. if( !nation_array.read_file(filePtr) )
  486. return 0;
  487. if( !read_book_mark( BOOK_MARK+206 ) )
  488. return 0;
  489. if( !firm_array.read_file(filePtr) )
  490. return 0;
  491. if( !read_book_mark( BOOK_MARK+207 ) )
  492. return 0;
  493. if( !tornado_array.read_file(filePtr) )
  494. return 0;
  495. if( !read_book_mark( BOOK_MARK+208 ) )
  496. return 0;
  497. if( !rebel_array.read_file(filePtr) )
  498. return 0;
  499. if( !read_book_mark( BOOK_MARK+209 ) )
  500. return 0;
  501. if( !spy_array.read_file(filePtr) )
  502. return 0;
  503. if( !read_book_mark( BOOK_MARK+210 ) )
  504. return 0;
  505. if( !snow_ground_array.read_file(filePtr) )
  506. return 0;
  507. if( !read_book_mark( BOOK_MARK+211 ) )
  508. return 0;
  509. if( !region_array.read_file(filePtr) )
  510. return 0;
  511. if( !read_book_mark( BOOK_MARK+212 ) )
  512. return 0;
  513. if( !news_array.read_file(filePtr) )
  514. return 0;
  515. if( !read_book_mark( BOOK_MARK+213 ) )
  516. return 0;
  517. if( !rock_array.read_file(filePtr) )
  518. return 0;
  519. if( !read_book_mark( BOOK_MARK+214 ) )
  520. return 0;
  521. if( !dirt_array.read_file(filePtr) )
  522. return 0;
  523. // ##### begin Gilbert 2/10 ######//
  524. if( !read_book_mark( BOOK_MARK+215 ) )
  525. if( !firm_die_array.read_file(filePtr) )
  526. return 0;
  527. // ##### end Gilbert 2/10 ######//
  528. return 1;
  529. }
  530. //---------- End of function GameFile::read_file_3 -------//
  531. //-------- Begin of function GameFile::write_book_mark -------//
  532. //
  533. void GameFile::write_book_mark(short bookMark)
  534. {
  535. sys.yield();
  536. file_ptr->file_put_short(bookMark);
  537. }
  538. //---------- End of function GameFile::write_book_mark -------//
  539. //-------- Begin of function GameFile::read_book_mark -------//
  540. //
  541. // Return : 1 - the book mark is matched
  542. // 0 - the book mark is not matched
  543. //
  544. int GameFile::read_book_mark(short bookMark)
  545. {
  546. sys.yield();
  547. return file_ptr->file_get_short() == bookMark;
  548. }
  549. //---------- End of function GameFile::read_book_mark -------//
  550. //***//
  551. //-------- Start of function RaceRes::write_file -------------//
  552. //
  553. int RaceRes::write_file(File* filePtr)
  554. {
  555. //------- write RaceInfo -------//
  556. RaceInfo* raceInfo = race_info_array;
  557. for( int i=1 ; i<=race_res.race_count ; i++, raceInfo++ )
  558. {
  559. filePtr->file_put_short( raceInfo->town_name_used_count );
  560. }
  561. return filePtr->file_write( name_used_array, sizeof(name_used_array[0]) * name_count );
  562. }
  563. //--------- End of function RaceRes::write_file ---------------//
  564. //-------- Start of function RaceRes::read_file -------------//
  565. //
  566. int RaceRes::read_file(File* filePtr)
  567. {
  568. //------- read RaceInfo -------//
  569. RaceInfo* raceInfo = race_info_array;
  570. for( int i=1 ; i<=race_res.race_count ; i++, raceInfo++ )
  571. {
  572. #ifdef AMPLUS
  573. raceInfo->town_name_used_count = (!game_file_array.same_version && i>VERSION_1_MAX_RACE) ?
  574. 0 : filePtr->file_get_short();
  575. #else
  576. raceInfo->town_name_used_count = filePtr->file_get_short();
  577. #endif
  578. }
  579. #ifdef AMPLUS
  580. if(!game_file_array.same_version)
  581. {
  582. memset(name_used_array, 0, sizeof(name_used_array[0]) * name_count);
  583. return filePtr->file_read( name_used_array, sizeof(name_used_array[0]) * VERSION_1_RACERES_NAME_COUNT );
  584. }
  585. else
  586. return filePtr->file_read( name_used_array, sizeof(name_used_array[0]) * name_count );
  587. #else
  588. return filePtr->file_read( name_used_array, sizeof(name_used_array[0]) * name_count );
  589. #endif
  590. }
  591. //--------- End of function RaceRes::read_file ---------------//
  592. //***//
  593. //-------- Start of function UnitRes::write_file -------------//
  594. //
  595. int UnitRes::write_file(File* filePtr)
  596. {
  597. filePtr->file_put_short(mobile_monster_count);
  598. UnitInfo* unitInfo = unit_info_array;
  599. for( int i=1 ; i<=unit_res.unit_info_count ; i++, unitInfo++ )
  600. {
  601. if( !filePtr->file_write( unitInfo->nation_tech_level_array, sizeof(unitInfo->nation_tech_level_array) ) )
  602. return 0;
  603. if( !filePtr->file_write( unitInfo->nation_unit_count_array, sizeof(unitInfo->nation_unit_count_array) ) )
  604. return 0;
  605. if( !filePtr->file_write( unitInfo->nation_general_count_array, sizeof(unitInfo->nation_general_count_array) ) )
  606. return 0;
  607. }
  608. return 1;
  609. }
  610. //--------- End of function UnitRes::write_file ---------------//
  611. //-------- Start of function UnitRes::read_file -------------//
  612. //
  613. int UnitRes::read_file(File* filePtr)
  614. {
  615. mobile_monster_count = filePtr->file_get_short();
  616. UnitInfo* unitInfo = unit_info_array;
  617. for( int i=1 ; i<=unit_res.unit_info_count ; i++, unitInfo++ )
  618. {
  619. #ifdef AMPLUS
  620. if(!game_file_array.same_version && i > VERSION_1_UNITRES_UNIT_INFO_COUNT)
  621. {
  622. memset(unitInfo->nation_tech_level_array, 0, sizeof(unitInfo->nation_tech_level_array));
  623. memset(unitInfo->nation_unit_count_array, 0, sizeof(unitInfo->nation_unit_count_array));
  624. memset(unitInfo->nation_general_count_array, 0, sizeof(unitInfo->nation_general_count_array));
  625. continue;
  626. }
  627. #endif
  628. if( !filePtr->file_read( unitInfo->nation_tech_level_array, sizeof(unitInfo->nation_tech_level_array) ) )
  629. return 0;
  630. if( !filePtr->file_read( unitInfo->nation_unit_count_array, sizeof(unitInfo->nation_unit_count_array) ) )
  631. return 0;
  632. if( !filePtr->file_read( unitInfo->nation_general_count_array, sizeof(unitInfo->nation_general_count_array) ) )
  633. return 0;
  634. }
  635. return 1;
  636. }
  637. //--------- End of function UnitRes::read_file ---------------//
  638. //***//
  639. //-------- Start of function FirmRes::write_file -------------//
  640. //
  641. int FirmRes::write_file(File* filePtr)
  642. {
  643. return filePtr->file_write( firm_info_array, firm_count * sizeof(FirmInfo) );
  644. }
  645. //--------- End of function FirmRes::write_file ---------------//
  646. //-------- Start of function FirmRes::read_file -------------//
  647. //
  648. int FirmRes::read_file(File* filePtr)
  649. {
  650. int arraySize = firm_count * sizeof(FirmInfo);
  651. //----- save the firm names, so that it won't be overwritten by the saved game file ----//
  652. FirmInfo* oldFirmInfoArray = (FirmInfo*) mem_add(arraySize);
  653. memcpy( oldFirmInfoArray, firm_info_array, arraySize );
  654. int rc = filePtr->file_read( firm_info_array, arraySize );
  655. for( int i=0 ; i<firm_count ; i++ )
  656. {
  657. memcpy( firm_info_array[i].name , oldFirmInfoArray[i].name , FirmInfo::NAME_LEN+1 );
  658. memcpy( firm_info_array[i].short_name , oldFirmInfoArray[i].short_name , FirmInfo::SHORT_NAME_LEN+1 );
  659. memcpy( firm_info_array[i].overseer_title, oldFirmInfoArray[i].overseer_title, FirmInfo::TITLE_LEN+1 );
  660. memcpy( firm_info_array[i].worker_title , oldFirmInfoArray[i].worker_title , FirmInfo::TITLE_LEN+1 );
  661. // ###### patch begin Gilbert 11/3 ########//
  662. firm_info_array[i].first_build_id = oldFirmInfoArray[i].first_build_id;
  663. firm_info_array[i].build_count = oldFirmInfoArray[i].build_count;
  664. // ###### patch end Gilbert 11/3 ########//
  665. }
  666. mem_del( oldFirmInfoArray );
  667. return rc;
  668. }
  669. //--------- End of function FirmRes::read_file ---------------//
  670. //***//
  671. //-------- Start of function TownRes::write_file -------------//
  672. //
  673. int TownRes::write_file(File* filePtr)
  674. {
  675. return filePtr->file_write( town_name_used_array, sizeof(town_name_used_array[0]) * town_name_count );
  676. }
  677. //--------- End of function TownRes::write_file ---------------//
  678. //-------- Start of function TownRes::read_file -------------//
  679. //
  680. int TownRes::read_file(File* filePtr)
  681. {
  682. #ifdef AMPLUS
  683. if(!game_file_array.same_version)
  684. {
  685. memset(town_name_used_array, 0, sizeof(town_name_used_array));
  686. return filePtr->file_read( town_name_used_array, sizeof(town_name_used_array[0]) * VERSION_1_TOWNRES_TOWN_NAME_COUNT );
  687. }
  688. else
  689. return filePtr->file_read( town_name_used_array, sizeof(town_name_used_array[0]) * town_name_count );
  690. #else
  691. return filePtr->file_read( town_name_used_array, sizeof(town_name_used_array[0]) * town_name_count );
  692. #endif
  693. }
  694. //--------- End of function TownRes::read_file ---------------//
  695. //***//
  696. //-------- Start of function TechRes::write_file -------------//
  697. //
  698. int TechRes::write_file(File* filePtr)
  699. {
  700. if( !filePtr->file_write( tech_class_array, tech_class_count * sizeof(TechClass) ) )
  701. return 0;
  702. if( !filePtr->file_write( tech_info_array, tech_count * sizeof(TechInfo) ) )
  703. return 0;
  704. return 1;
  705. }
  706. //--------- End of function TechRes::write_file ---------------//
  707. //-------- Start of function TechRes::read_file -------------//
  708. //
  709. int TechRes::read_file(File* filePtr)
  710. {
  711. if( !filePtr->file_read( tech_class_array, tech_class_count * sizeof(TechClass) ) )
  712. return 0;
  713. #ifdef AMPLUS
  714. if(!game_file_array.same_version)
  715. {
  716. if(!filePtr->file_read( tech_info_array, VERSION_1_TECH_COUNT * sizeof(TechInfo) ) )
  717. return 0;
  718. TechInfo *techInfoPtr = tech_info_array + VERSION_1_TECH_COUNT;
  719. for(int i=VERSION_1_TECH_COUNT; i<tech_count; ++i, techInfoPtr++)
  720. {
  721. memset(techInfoPtr->nation_tech_level_array, 0, sizeof(techInfoPtr->nation_tech_level_array));
  722. memset(techInfoPtr->nation_is_researching_array, 0, sizeof(techInfoPtr->nation_is_researching_array));
  723. memset(techInfoPtr->nation_research_progress_array, 0, sizeof(techInfoPtr->nation_research_progress_array));
  724. }
  725. }
  726. else
  727. {
  728. if( !filePtr->file_read( tech_info_array, tech_count * sizeof(TechInfo) ) )
  729. return 0;
  730. }
  731. #else
  732. if( !filePtr->file_read( tech_info_array, tech_count * sizeof(TechInfo) ) )
  733. return 0;
  734. #endif
  735. return 1;
  736. }
  737. //--------- End of function TechRes::read_file ---------------//
  738. //***//
  739. //-------- Start of function TalkRes::write_file -------------//
  740. //
  741. int TalkRes::write_file(File* filePtr)
  742. {
  743. if( !filePtr->file_write( this, sizeof(TalkRes) ) )
  744. return 0;
  745. if( !talk_msg_array.write_file(filePtr) )
  746. return 0;
  747. return 1;
  748. }
  749. //--------- End of function TalkRes::write_file ---------------//
  750. //-------- Start of function TalkRes::read_file -------------//
  751. //
  752. int TalkRes::read_file(File* filePtr)
  753. {
  754. //------ save talk_msg_array --------//
  755. char* tempArray = mem_add(sizeof(DynArrayB));
  756. memcpy( tempArray, &talk_msg_array, sizeof(DynArrayB) );
  757. //------ read in TalkRes --------//
  758. if( !filePtr->file_read( this, sizeof(TalkRes) ) )
  759. return 0;
  760. //------ restore talk_msg_array --------//
  761. memcpy( &talk_msg_array, tempArray, sizeof(DynArrayB) );
  762. mem_del(tempArray);
  763. //---- read in talk_msg_array ----//
  764. if( !talk_msg_array.read_file(filePtr) )
  765. return 0;
  766. return 1;
  767. }
  768. //--------- End of function TalkRes::read_file ---------------//
  769. //***//
  770. //-------- Start of function RawRes::write_file -------------//
  771. //
  772. int RawRes::write_file(File* filePtr)
  773. {
  774. for( int i=0 ; i<MAX_RAW ; i++ )
  775. {
  776. if( !raw_info_array[i].raw_supply_firm_array.write_file(filePtr) )
  777. return 0;
  778. if( !raw_info_array[i].product_supply_firm_array.write_file(filePtr) )
  779. return 0;
  780. }
  781. return 1;
  782. }
  783. //--------- End of function RawRes::write_file ---------------//
  784. //-------- Start of function RawRes::read_file -------------//
  785. //
  786. int RawRes::read_file(File* filePtr)
  787. {
  788. for( int i=0 ; i<MAX_RAW ; i++ )
  789. {
  790. if( !raw_info_array[i].raw_supply_firm_array.read_file(filePtr) )
  791. return 0;
  792. if( !raw_info_array[i].product_supply_firm_array.read_file(filePtr) )
  793. return 0;
  794. }
  795. return 1;
  796. }
  797. //--------- End of function RawRes::read_file ---------------//
  798. //***//
  799. //-------- Start of function GodRes::write_file -------------//
  800. //
  801. int GodRes::write_file(File* filePtr)
  802. {
  803. return filePtr->file_write( god_info_array, sizeof(GodInfo) * god_count );
  804. }
  805. //--------- End of function GodRes::write_file ---------------//
  806. //-------- Start of function GodRes::read_file -------------//
  807. //
  808. int GodRes::read_file(File* filePtr)
  809. {
  810. #ifdef AMPLUS
  811. if(!game_file_array.same_version)
  812. {
  813. memset(god_info_array, 0, sizeof(god_info_array));
  814. return filePtr->file_read( god_info_array, sizeof(GodInfo) * VERSION_1_GODRES_GOD_COUNT );
  815. }
  816. else
  817. return filePtr->file_read( god_info_array, sizeof(GodInfo) * god_count );
  818. #else
  819. return filePtr->file_read( god_info_array, sizeof(GodInfo) * god_count );
  820. #endif
  821. }
  822. //--------- End of function GodRes::read_file ---------------//
  823. //***//
  824. //-------- Start of function MonsterRes::write_file -------------//
  825. //
  826. int MonsterRes::write_file(File* filePtr)
  827. {
  828. return filePtr->file_write( active_monster_array, sizeof(active_monster_array) );
  829. }
  830. //--------- End of function MonsterRes::write_file ---------------//
  831. //-------- Start of function MonsterRes::read_file -------------//
  832. //
  833. int MonsterRes::read_file(File* filePtr)
  834. {
  835. return filePtr->file_read( active_monster_array, sizeof(active_monster_array) );
  836. }
  837. //--------- End of function MonsterRes::read_file ---------------//
  838. //***//
  839. //-------- Start of function Game::write_file -------------//
  840. //
  841. int Game::write_file(File* filePtr)
  842. {
  843. return filePtr->file_write( this, sizeof(Game) );
  844. }
  845. //--------- End of function Game::write_file ---------------//
  846. //-------- Start of function Game::read_file -------------//
  847. //
  848. int Game::read_file(File* filePtr)
  849. {
  850. return filePtr->file_read( this, sizeof(Game) );
  851. }
  852. //--------- End of function Game::read_file ---------------//
  853. //***//
  854. //-------- Start of function Config::write_file -------------//
  855. //
  856. int Config::write_file(File* filePtr)
  857. {
  858. return filePtr->file_write( this, sizeof(Config) );
  859. }
  860. //--------- End of function Config::write_file ---------------//
  861. //-------- Start of function Config::read_file -------------//
  862. //
  863. int Config::read_file(File* filePtr, int keepSysSettings)
  864. {
  865. //--- these settings are not game dependent -----//
  866. char musicFlag = music_flag;
  867. short cdMusicVol = cd_music_volume;
  868. short wavMusicVol = wav_music_volume;
  869. char soundEffectFlag = sound_effect_flag;
  870. short soundEffectVol = sound_effect_volume;
  871. char helpMode = help_mode;
  872. int rc = filePtr->file_read( this, sizeof(Config) );
  873. if( keepSysSettings )
  874. {
  875. music_flag = musicFlag;
  876. cd_music_volume = cdMusicVol;
  877. wav_music_volume = wavMusicVol;
  878. sound_effect_flag = soundEffectFlag;
  879. sound_effect_volume = soundEffectVol;
  880. help_mode = helpMode;
  881. }
  882. return rc;
  883. }
  884. //--------- End of function Config::read_file ---------------//
  885. //***//
  886. //-------- Start of function Info::write_file -------------//
  887. //
  888. int Info::write_file(File* filePtr)
  889. {
  890. int writeSize = (char*)(&last_write_offset) - (char*)(this);
  891. //---------- write the info data -----------//
  892. return filePtr->file_write( this, writeSize );
  893. }
  894. //--------- End of function Info::write_file ---------------//
  895. //-------- Start of function Info::read_file -------------//
  896. //
  897. int Info::read_file(File* filePtr)
  898. {
  899. int readSize = (char*)(&last_write_offset) - (char*)(this);
  900. //------- read the info data ----------//
  901. return filePtr->file_read( this, readSize );
  902. }
  903. //--------- End of function Info::read_file ---------------//
  904. //***//
  905. //-------- Start of function Power::write_file -------------//
  906. //
  907. int Power::write_file(File* filePtr)
  908. {
  909. return filePtr->file_write( this, sizeof(Power) );
  910. }
  911. //--------- End of function Power::write_file ---------------//
  912. //-------- Start of function Power::read_file -------------//
  913. //
  914. int Power::read_file(File* filePtr)
  915. {
  916. return filePtr->file_read( this, sizeof(Power) );
  917. }
  918. //--------- End of function Power::read_file ---------------//
  919. //***//
  920. //-------- Start of function Sys::write_file -------------//
  921. //
  922. int Sys::write_file(File* filePtr)
  923. {
  924. //---- write the current random seed first ----//
  925. if( !filePtr->file_put_long(m.get_random_seed()) )
  926. return 0;
  927. //---------- write some Sys data -----------//
  928. filePtr->file_put_long(day_frame_count);
  929. filePtr->file_put_long(frame_count);
  930. filePtr->file_put_short(view_mode);
  931. return 1;
  932. }
  933. //--------- End of function Sys::write_file ---------------//
  934. //-------- Start of function Sys::read_file -------------//
  935. //
  936. int Sys::read_file(File* filePtr)
  937. {
  938. //------- read the random seed --------//
  939. loaded_random_seed = filePtr->file_get_long();
  940. //--------- read some Sys data -----------//
  941. day_frame_count = filePtr->file_get_long();
  942. frame_count = filePtr->file_get_long();
  943. view_mode = (char) filePtr->file_get_short();
  944. return 1;
  945. }
  946. //--------- End of function Sys::read_file ---------------//
  947. //***//
  948. //-------- Start of function Weather::write_file -------------//
  949. //
  950. int Weather::write_file(File* filePtr)
  951. {
  952. return filePtr->file_write( this, sizeof(Weather) );
  953. }
  954. //--------- End of function Weather::write_file ---------------//
  955. //-------- Start of function Weather::read_file -------------//
  956. //
  957. int Weather::read_file(File* filePtr)
  958. {
  959. return filePtr->file_read( this, sizeof(Weather) );
  960. }
  961. //--------- End of function Weather::read_file ---------------//
  962. //***//
  963. //-------- Start of function MagicWeather::write_file -------------//
  964. //
  965. int MagicWeather::write_file(File* filePtr)
  966. {
  967. return filePtr->file_write( this, sizeof(MagicWeather) );
  968. }
  969. //--------- End of function MagicWeahter::write_file ---------------//
  970. //-------- Start of function MagicWeahter::read_file -------------//
  971. //
  972. int MagicWeather::read_file(File* filePtr)
  973. {
  974. return filePtr->file_read( this, sizeof(MagicWeather) );
  975. }
  976. //--------- End of function MagicWeahter::read_file ---------------//
  977. //***//
  978. //-------- Start of function World::write_file -------------//
  979. //
  980. int World::write_file(File* filePtr)
  981. {
  982. //--------- save map -------------//
  983. if( !filePtr->file_write(loc_matrix, max_x_loc*max_y_loc*sizeof(Location) ) )
  984. return 0;
  985. //--------- save vars -----------//
  986. filePtr->file_put_short(scan_fire_x);
  987. filePtr->file_put_short(scan_fire_y);
  988. filePtr->file_put_short(lightning_signal);
  989. // ######## begin Gilbert 18/7 #########//
  990. filePtr->file_put_long(plant_count);
  991. filePtr->file_put_long(plant_limit);
  992. // ######## end Gilbert 18/7 #########//
  993. map_matrix->last_map_mode = -1;
  994. filePtr->file_put_short(map_matrix->map_mode);
  995. filePtr->file_put_short(map_matrix->power_mode);
  996. filePtr->file_put_long(map_matrix->cur_x_loc);
  997. filePtr->file_put_long(map_matrix->cur_y_loc);
  998. filePtr->file_put_long(zoom_matrix->init_lightning);
  999. filePtr->file_put_long(zoom_matrix->vibration);
  1000. filePtr->file_put_short(zoom_matrix->lightning_x1);
  1001. filePtr->file_put_short(zoom_matrix->lightning_y1);
  1002. filePtr->file_put_short(zoom_matrix->lightning_x2);
  1003. filePtr->file_put_short(zoom_matrix->lightning_y2);
  1004. return 1;
  1005. }
  1006. //--------- End of function World::write_file ---------------//
  1007. //-------- Start of function World::read_file -------------//
  1008. //
  1009. int World::read_file(File* filePtr)
  1010. {
  1011. //-------- read in the map --------//
  1012. loc_matrix = (Location*) mem_resize( loc_matrix, max_x_loc * max_y_loc
  1013. * sizeof(Location) );
  1014. if( !filePtr->file_read(loc_matrix, max_x_loc*max_y_loc*sizeof(Location) ) )
  1015. return 0;
  1016. assign_map();
  1017. //--------- read in vars ----------//
  1018. scan_fire_x = (char) filePtr->file_get_short();
  1019. scan_fire_y = (char) filePtr->file_get_short();
  1020. lightning_signal = (char) filePtr->file_get_short();
  1021. // ######## begin Gilbert 18/7 #########//
  1022. plant_count = filePtr->file_get_long();
  1023. plant_limit = filePtr->file_get_long();
  1024. // ######## end Gilbert 18/7 #########//
  1025. map_matrix->last_map_mode = -1;
  1026. map_matrix->map_mode = (char) filePtr->file_get_short();
  1027. map_matrix->power_mode = (char) filePtr->file_get_short();
  1028. map_matrix->cur_x_loc = filePtr->file_get_long();
  1029. map_matrix->cur_y_loc = filePtr->file_get_long();
  1030. zoom_matrix->top_x_loc = map_matrix->cur_x_loc;
  1031. zoom_matrix->top_y_loc = map_matrix->cur_y_loc;
  1032. sys.zoom_need_redraw = 1;
  1033. zoom_matrix->init_lightning = filePtr->file_get_long();
  1034. zoom_matrix->vibration = filePtr->file_get_long();
  1035. zoom_matrix->lightning_x1 = filePtr->file_get_short();
  1036. zoom_matrix->lightning_y1 = filePtr->file_get_short();
  1037. zoom_matrix->lightning_x2 = filePtr->file_get_short();
  1038. zoom_matrix->lightning_y2 = filePtr->file_get_short();
  1039. return 1;
  1040. }
  1041. //--------- End of function World::read_file ---------------//
  1042. //***//
  1043. //-------- Start of function Tutor::write_file -------------//
  1044. //
  1045. int Tutor::write_file(File* filePtr)
  1046. {
  1047. filePtr->file_put_short(cur_tutor_id);
  1048. filePtr->file_put_short(cur_text_block_id);
  1049. return 1;
  1050. }
  1051. //--------- End of function Tutor::write_file ---------------//
  1052. //-------- Start of function Tutor::read_file -------------//
  1053. //
  1054. int Tutor::read_file(File* filePtr)
  1055. {
  1056. int curTutorId = filePtr->file_get_short();
  1057. if( curTutorId > 0 )
  1058. tutor.load(curTutorId); // load() will reset cur_text_block_id
  1059. cur_text_block_id = filePtr->file_get_short();
  1060. last_text_block_id = 0;
  1061. return 1;
  1062. }
  1063. //--------- End of function Tutor::read_file ---------------//
  1064. //### begin alex 23/9 ###//
  1065. //-------- Start of function SeekPath::write_file -------------//
  1066. //
  1067. int SeekPath::write_file(File* filePtr)
  1068. {
  1069. filePtr->file_put_short(total_node_avail);
  1070. return 1;
  1071. }
  1072. //--------- End of function SeekPath::write_file ---------------//
  1073. //-------- Start of function SeekPath::read_file -------------//
  1074. //
  1075. int SeekPath::read_file(File* filePtr)
  1076. {
  1077. total_node_avail = filePtr->file_get_short();
  1078. return 1;
  1079. }
  1080. //--------- End of function SeekPath::read_file ---------------//
  1081. //#### end alex 23/9 ####//