OGAMSCEN.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 : OGAMSCEN.CPP
  21. //Description : Select Game Scenario
  22. #include <OSTR.h>
  23. #include <OSYS.h>
  24. #include <ONEWS.h>
  25. #include <ODATE.h>
  26. #include <OGFILE.h>
  27. #include <OF_MONS.h>
  28. #include <OMONSRES.h>
  29. #include <OFILETXT.h>
  30. #include <ODIR.h>
  31. #include <OBOX.h>
  32. #include <OBATTLE.h>
  33. #include <OGAME.h>
  34. //--------- declare static vars ----------//
  35. static void init_scenario_var(ScenInfo* scenInfo);
  36. static int sort_scenario_func(const void *arg1, const void *arg2);
  37. //---------- Begin of function Game::select_run_scenario ----------//
  38. //
  39. // Select and play a scenario.
  40. //
  41. // return : <int> 1 - ok, a scenario is selected and run
  42. // 0 - cancel
  43. //
  44. int Game::select_run_scenario()
  45. {
  46. Directory gameDirList[MAX_SCENARIO_PATH];
  47. ScenInfo* scenInfoArray = NULL;
  48. int scenInfoSize = 0;
  49. int dirId;
  50. for( dirId = 0; dirId < MAX_SCENARIO_PATH; ++dirId )
  51. {
  52. if( DIR_SCENARIO_PATH(dirId)[0] )
  53. {
  54. Directory &gameDir = gameDirList[dirId];
  55. {
  56. String str;
  57. str = DIR_SCENARIO_PATH(dirId);
  58. str += "*.SCN";
  59. gameDir.read( str, 1 ); // Read in all file names with the "SCN" extension
  60. }
  61. if( gameDir.size() > 0)
  62. {
  63. //---- append scenario file names in this directory into an array ----//
  64. scenInfoArray = (ScenInfo *)mem_resize( scenInfoArray, sizeof(ScenInfo)*(scenInfoSize+gameDir.size()) );
  65. for( int i = 1; i <= gameDir.size(); ++i, ++scenInfoSize)
  66. {
  67. char txtFileName[20];
  68. scenInfoArray[scenInfoSize].file_name = gameDir[i]->name; // keep the pointers to the file name string
  69. scenInfoArray[scenInfoSize].dir_id = dirId;
  70. {
  71. m.change_file_ext( txtFileName, gameDir[i]->name, "SCT" );
  72. String str;
  73. str = DIR_SCENARIO_PATH(dirId);
  74. str += txtFileName;
  75. FileTxt fileTxtScen(str);
  76. //---- get the name of the scenario ----//
  77. fileTxtScen.read_line( scenInfoArray[scenInfoSize].scen_name, ScenInfo::SCEN_NAME_LEN );
  78. //---- get the difficulty level and score bonus ----//
  79. fileTxtScen.get_token(); // skip "Difficulty:"
  80. scenInfoArray[scenInfoSize].goal_difficulty = (short) fileTxtScen.get_num();
  81. fileTxtScen.get_token(); // skip "Bonus:"
  82. scenInfoArray[scenInfoSize].goal_score_bonus = (short) fileTxtScen.get_num();
  83. }
  84. }
  85. }
  86. }
  87. }
  88. if( scenInfoSize == 0 )
  89. {
  90. box.msg( "Sorry, there is no scenario in the game directory." );
  91. return 0;
  92. }
  93. //-------- sort by difficulty ---------- //
  94. qsort(scenInfoArray, scenInfoSize, sizeof(ScenInfo), sort_scenario_func);
  95. //-------- select and run a scenario --------//
  96. int rc = select_scenario( scenInfoSize, scenInfoArray );
  97. if( rc )
  98. run_scenario( scenInfoArray+rc-1 );
  99. //-------------------------------------------//
  100. mem_del(scenInfoArray);
  101. return rc;
  102. }
  103. //------------ End of function Game::select_run_scenario -----------//
  104. //---------- Begin of function Game::run_scenario ----------//
  105. //
  106. // <ScenInfo*> scenInfo - the pointer to ScenInfo of the selected scenario
  107. //
  108. int Game::run_scenario(ScenInfo* scenInfo)
  109. {
  110. String str;
  111. str = DIR_SCENARIO_PATH(scenInfo->dir_id);
  112. str += scenInfo->file_name;
  113. if( m.is_file_exist(str) )
  114. {
  115. // ###### begin Gilbert 1/11 #########//
  116. // save the name in the config
  117. char playerName[Config::PLAYER_NAME_LEN+1];
  118. strcpy(playerName, config.player_name);
  119. // ###### end Gilbert 1/11 #########//
  120. if( game_file.load_game(str) > 0 )
  121. {
  122. init_scenario_var(scenInfo);
  123. // ##### begin Gilbert 1/11 #######//
  124. // set the nation name of the player
  125. if( nation_array.player_recno )
  126. {
  127. (~nation_array)->nation_name_id = -nation_array.player_recno;
  128. nation_array.set_human_name( nation_array.player_recno, playerName);
  129. }
  130. strcpy(config.player_name, playerName);
  131. // ##### end Gilbert 1/11 #######//
  132. battle.run_loaded();
  133. }
  134. game.deinit();
  135. return 1;
  136. }
  137. return 0;
  138. }
  139. //------------ End of function Game::run_scenario -----------//
  140. //-------- Start of function init_scenario_var ----------//
  141. //
  142. static void init_scenario_var(ScenInfo* scenInfo)
  143. {
  144. //-------- init config var ----------//
  145. config.king_undie_flag = 0;
  146. config.disable_ai_flag = 0;
  147. config.fast_build = 0;
  148. config.show_ai_info = 0;
  149. config.show_all_unit_icon = 0;
  150. config.show_unit_path = 0;
  151. sys.set_speed(9, COMMAND_AUTO);
  152. //------ reset the goal deadline -------//
  153. info.goal_deadline = date.julian( date.year(info.game_date)+config.goal_year_limit,
  154. date.month(info.game_date),
  155. date.day(info.game_date) );
  156. info.goal_difficulty = scenInfo->goal_difficulty;
  157. info.goal_score_bonus = scenInfo->goal_score_bonus;
  158. info.total_play_time = 0;
  159. //------ reset vars in NationBase ---------//
  160. Nation* playerNation = ~nation_array; // we reset a whole block of vars in NationBase which are related to financial info
  161. playerNation->cheat_enabled_flag = 0;
  162. //------- reset financial vars --------//
  163. int resetSize = (char*) playerNation->relation_array
  164. - (char*)(&(playerNation->cur_year_profit));
  165. memset( &(playerNation->cur_year_profit), 0, resetSize );
  166. //------- reset statistic vars -------//
  167. resetSize = (char*)(&(playerNation->own_firm_destroyed))
  168. - (char*)(&(playerNation->enemy_soldier_killed))
  169. + sizeof(playerNation->own_firm_destroyed);
  170. memset( &(playerNation->enemy_soldier_killed), 0, resetSize );
  171. //--------- update statistic ---------//
  172. nation_array.update_statistic();
  173. //------- reset display mode --------//
  174. sys.view_mode = MODE_NORMAL;
  175. world.map_matrix->map_mode = MAP_MODE_TERRAIN;
  176. world.map_matrix->power_mode = 0;
  177. //------------ reset news ------------//
  178. news_array.reset();
  179. //------ fix firm_build_id problem -----//
  180. Firm* firmPtr;
  181. for( int i=firm_array.size() ; i>0 ; i-- )
  182. {
  183. if( firm_array.is_deleted(i) )
  184. continue;
  185. firmPtr = firm_array[i];
  186. if( firmPtr->firm_id != FIRM_MONSTER )
  187. continue;
  188. int monsterId = ((FirmMonster*)firmPtr)->monster_id;
  189. firmPtr->firm_build_id = firm_res[FIRM_MONSTER]->get_build_id( monster_res[monsterId]->firm_build_code );
  190. }
  191. }
  192. //--------- End of function init_scenario_var -----------//
  193. //-------- Start of function init_scenario_var ----------//
  194. //
  195. int sort_scenario_func(const void *arg1, const void *arg2)
  196. {
  197. return ((ScenInfo *)arg1)->goal_difficulty - ((ScenInfo *)arg2)->goal_difficulty;
  198. }
  199. //-------- End of function init_scenario_var ----------//