OGAME.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 : OGAME.CPP
  21. //Description : Main Game Object
  22. #include <ALL.h>
  23. #include <COLCODE.h>
  24. #include <OSYS.h>
  25. #include <OVGA.h>
  26. #include <OFONT.h>
  27. #include <OMOUSE.h>
  28. #include <OMOUSECR.h>
  29. #include <OBUTTON.h>
  30. #include <OUNITALL.h>
  31. #include <OBULLET.h>
  32. #include <OTERRAIN.h>
  33. #include <OPLANT.h>
  34. #include <OWORLD.h>
  35. #include <OPOWER.h>
  36. #include <OSITE.h>
  37. #include <ORACERES.h>
  38. #include <OWALLRES.h>
  39. #include <OTECHRES.h>
  40. #include <OGODRES.h>
  41. #include <OMONSRES.h>
  42. #include <OTOWN.h>
  43. #include <ONATION.h>
  44. #include <OFIRM.h>
  45. #include <OIMGRES.h>
  46. #include <OINFO.h>
  47. #include <OSPRITE.h>
  48. #include <OGAMESET.h>
  49. #include <OGAME.h>
  50. #include <OREBEL.h>
  51. #include <OSPY.h>
  52. #include <OBATTLE.h>
  53. #include <ONEWS.h>
  54. #include <OWEATHER.h>
  55. #include <OHILLRES.h>
  56. #include <OTALKRES.h>
  57. #include <OSNOWRES.h>
  58. #include <OSNOWG.h>
  59. #include <OEXPMASK.h>
  60. #include <OSE.h>
  61. #include <OSERES.h>
  62. #include <OROCKRES.h>
  63. #include <OROCK.h>
  64. #include <OEFFECT.h>
  65. #include <OAUDIO.h>
  66. #include <OMUSIC.h>
  67. #include <OTORNADO.h>
  68. #include <OWARPT.h>
  69. // ##### begin Gilbert 2/10 #######//
  70. #include <OFIRMDIE.h>
  71. // ##### end Gilbert 2/10 #######//
  72. //---------------- DETECT_SPREAD ----------------//
  73. //
  74. // Spread out 2 tiles on all direction when detecting sprite.
  75. // A bigger value is required for bigger sprite.
  76. // this should be a number big enough to cover the biggest
  77. // sprite in the game.
  78. //
  79. // It would be equal to the size of the biggest
  80. // sprite in the game + 1 (+1 for the difference
  81. // between (next_x & cur_x).
  82. //
  83. //-----------------------------------------------//
  84. //-------- Begin of function Game::Game --------//
  85. //
  86. Game::Game()
  87. {
  88. init_flag = 0;
  89. game_mode = GAME_PREGAME;
  90. init_remap_table(); // initialize color remap table
  91. }
  92. //--------- End of function Game::Game ---------//
  93. //-------- Begin of function Game::init --------//
  94. //
  95. // Note: all functions called in this function cannot call
  96. // m.random(). Otherwise, it will cause random seed
  97. // sync error.
  98. //
  99. // [int] loadGameCall - weather this function is called
  100. // when a game is being loaded
  101. // (default: 0)
  102. //
  103. int Game::init(int loadGameCall)
  104. {
  105. if( init_flag )
  106. deinit();
  107. int originalRandomSeed = m.get_random_seed();
  108. music.stop();
  109. // ----- set waiting cursor -------- //
  110. int oldCursor = mouse_cursor.get_icon();
  111. mouse_cursor.set_icon(CURSOR_WAITING);
  112. //------- init game data class ---------//
  113. game_set.open_set(1); // open the default game set
  114. char tpictFile[] = DIR_RES"I_TPICT?.RES";
  115. *(strstr( tpictFile, "?")) = '0' + config.terrain_set;
  116. image_tpict.init(tpictFile,1,0); // config.terrain_set dependent, must load before town_res.init and terrain_res.init
  117. terrain_res.init();
  118. plant_res.init();
  119. tech_res.init();
  120. god_res.init();
  121. sprite_res.init(); // sprite resource object must been initialized after game_set as it relies on game_set for info.
  122. sprite_frame_res.init();
  123. unit_res.init();
  124. monster_res.init();
  125. raw_res.init();
  126. race_res.init();
  127. firm_res.init();
  128. // ##### begin Gilbert 2/10 #######//
  129. firm_die_res.init();
  130. // ##### end Gilbert 2/10 #######//
  131. town_res.init();
  132. hill_res.init();
  133. snow_res.init();
  134. rock_res.init();
  135. explored_mask.init(vga.vga_color_table);
  136. se_res.init1();
  137. se_res.init2(&se_ctrl);
  138. talk_res.init();
  139. //------- init game data class ---------//
  140. nation_array.init();
  141. firm_array.init();
  142. // ##### begin Gilbert 2/10 #######//
  143. firm_die_array.init();
  144. // ##### end Gilbert 2/10 #######//
  145. town_array.init();
  146. unit_array.init();
  147. bullet_array.init();
  148. rebel_array.init();
  149. spy_array.init();
  150. site_array.init();
  151. rock_array.init();
  152. dirt_array.init();
  153. effect_array.init();
  154. tornado_array.init();
  155. war_point_array.init();
  156. //------ init game surface class ----------//
  157. power.init();
  158. world.init();
  159. battle.init();
  160. news_array.init();
  161. if( !loadGameCall )
  162. info.init(); // it reads in the panel texture and copy it to vga_back's buffer
  163. //---------------------------------------------//
  164. int quakeFreq;
  165. if( config.random_event_frequency )
  166. {
  167. quakeFreq = 2000 - config.random_event_frequency * 400
  168. + info.random_seed%300;
  169. }
  170. else
  171. {
  172. quakeFreq = 0x0fffffff;
  173. }
  174. weather.init_date(info.game_year, info.game_month, info.game_day, config.latitude, quakeFreq);
  175. //---------------------------------------------//
  176. weather_forecast[0] = weather;
  177. weather_forecast[0].next_day();
  178. for(int foreDay=1; foreDay < MAX_WEATHER_FORECAST; ++foreDay)
  179. {
  180. weather_forecast[foreDay] = weather_forecast[foreDay-1];
  181. weather_forecast[foreDay].next_day();
  182. }
  183. snow_ground_array.init(weather.snow_scale(), info.game_year);
  184. //------------ run demo ------------//
  185. err_when( originalRandomSeed != m.get_random_seed() );
  186. // ----- restore from waiting cursor -------- //
  187. mouse_cursor.restore_icon(oldCursor);
  188. game_has_ended = 0;
  189. init_flag=1;
  190. return TRUE;
  191. }
  192. //--------- End of function Game::init ---------//
  193. //-------- Begin of function Game::deinit --------//
  194. //
  195. // [int] loadGameCall - weather this function is called
  196. // when a game is being loaded
  197. // (default: 0)
  198. //
  199. void Game::deinit(int loadGameCall)
  200. {
  201. if( !init_flag )
  202. return;
  203. power.disable(); // disable power, which handle mouse inputs
  204. music.stop();
  205. audio.stop_wav();
  206. //----- set waiting cursor -------- //
  207. int oldCursor = mouse_cursor.get_icon();
  208. mouse_cursor.set_icon(CURSOR_WAITING);
  209. //------- deinit game data class ---------//
  210. nation_array.deinit();
  211. firm_array.deinit();
  212. // ##### begin Gilbert 2/10 #######//
  213. firm_die_array.deinit();
  214. // ##### end Gilbert 2/10 #######//
  215. town_array.deinit();
  216. unit_array.deinit();
  217. bullet_array.deinit();
  218. rebel_array.deinit();
  219. spy_array.deinit();
  220. region_array.deinit();
  221. site_array.deinit();
  222. rock_array.deinit();
  223. dirt_array.deinit();
  224. effect_array.deinit();
  225. tornado_array.deinit();
  226. war_point_array.deinit();
  227. //------ deinit game surface class -------//
  228. world.deinit();
  229. battle.deinit();
  230. news_array.deinit();
  231. if( !loadGameCall )
  232. info.deinit();
  233. //------- deinit game data class ---------//
  234. image_tpict.deinit();
  235. terrain_res.deinit();
  236. plant_res.deinit();
  237. tech_res.deinit();
  238. god_res.deinit();
  239. monster_res.deinit();
  240. sprite_res.deinit();
  241. sprite_frame_res.deinit();
  242. unit_res.deinit();
  243. raw_res.deinit();
  244. race_res.deinit();
  245. firm_res.deinit();
  246. // ##### begin Gilbert 2/10 #######//
  247. firm_die_res.deinit();
  248. // ##### end Gilbert 2/10 #######//
  249. town_res.deinit();
  250. hill_res.deinit();
  251. snow_res.deinit();
  252. rock_res.deinit();
  253. explored_mask.deinit();
  254. se_res.deinit();
  255. talk_res.deinit();
  256. game_set.close_set(); // close the game set
  257. //----- restore from waiting cursor -------- //
  258. mouse_cursor.restore_icon(oldCursor);
  259. init_flag=0;
  260. }
  261. //--------- End of function Game::deinit ---------//
  262. //--------- Begin of function Game::init_remap_table --------//
  263. void Game::init_remap_table()
  264. {
  265. //------------- Define constant ------------//
  266. #define FIRST_REMAP_KEY 0xE0 // the source color code of the colors to be remapped
  267. #define REMAP_KEY_COUNT 8
  268. //-------- define color remap scheme -------//
  269. static ColorRemapMethod remap_method_array[] =
  270. {
  271. { 0xBC, 0xDC }, // the first remap table is for independent units
  272. { 0xA0, 0xC0 }, // following are eight remap table for each color code
  273. { 0xA4, 0xC4 },
  274. { 0xA8, 0xC8 },
  275. { 0xAC, 0xCC },
  276. { 0xB0, 0xD0 },
  277. { 0xB4, 0xD4 },
  278. { 0xB8, 0xD8 },
  279. { 0xBC, 0xDC },
  280. };
  281. //---- define the main color code for each color scheme ----//
  282. static int main_color_array[] =
  283. {
  284. 0xDC,
  285. 0xC0,
  286. 0xC4,
  287. 0xC8,
  288. 0xCC,
  289. 0xD0,
  290. 0xD4,
  291. 0xD8,
  292. };
  293. //-------- initialize color remap table -------//
  294. int i, j;
  295. ColorRemap *colorRemap = color_remap_array;
  296. for( i=0 ; i<MAX_COLOR_SCHEME+1 ; i++, colorRemap++ ) // MAX_COLOR_SCHEME+1, +1 for independent units
  297. {
  298. colorRemap->main_color = main_color_array[i];
  299. for( j=0 ; j<256 ; j++ )
  300. colorRemap->color_table[j] = j;
  301. for( j=0 ; j<4 ; j++ )
  302. colorRemap->color_table[FIRST_REMAP_KEY+j] = (char) (remap_method_array[i].primary_color+j);
  303. for( j=0 ; j<4 ; j++ )
  304. colorRemap->color_table[FIRST_REMAP_KEY+4+j] = (char) (remap_method_array[i].secondary_color+j);
  305. // for( j=0 ; j<4 ; j++ )
  306. // colorRemap->color_table[FIRST_REMAP_KEY+j] = (char) (remap_method_array[i].secondary_color+j);
  307. }
  308. }
  309. //---------- End of function Game::init_remap_table --------//
  310. //--------- Begin of function Game::get_color_remap_table --------//
  311. //
  312. // <int> nationRecno - 0 for independent nation
  313. // <int> selectedFlag - whether display outline around the bitmap
  314. //
  315. char* Game::get_color_remap_table(int nationRecno, int selectedFlag)
  316. {
  317. ColorRemap* colorRemap;
  318. char* colorRemapTable;
  319. // ###### begin Gilbert 1/10 ######//
  320. if( nationRecno==0 || nation_array.is_deleted(nationRecno) ) // independent units
  321. // ###### end Gilbert 1/10 ######//
  322. colorRemap = color_remap_array;
  323. else
  324. colorRemap = color_remap_array+nation_array[nationRecno]->color_scheme_id;
  325. colorRemapTable = colorRemap->color_table;
  326. //--------- set outline color ---------//
  327. #define FIRST_CYCLE_COLOR 0xEF
  328. #define CYCLE_COLOR_COUNT 1
  329. #define CYCLE_FRAME_INTERVAL 3
  330. if( selectedFlag )
  331. {
  332. int cycleId=0;
  333. /*
  334. if( CYCLE_COLOR_COUNT==1 )
  335. {
  336. cycleId = 0;
  337. }
  338. else
  339. {
  340. cycleId = sys.frame_count / CYCLE_FRAME_INTERVAL % (CYCLE_COLOR_COUNT*2-2);
  341. if( cycleId >= CYCLE_COLOR_COUNT ) // cycle in reserved order
  342. cycleId = CYCLE_COLOR_COUNT*2-2 - cycleId;
  343. }
  344. */
  345. colorRemapTable[OUTLINE_CODE] = FIRST_CYCLE_COLOR + cycleId;
  346. colorRemapTable[OUTLINE_SHADOW_CODE] = FIRST_CYCLE_COLOR + cycleId;
  347. }
  348. else
  349. {
  350. colorRemapTable[OUTLINE_CODE] = (char) TRANSPARENT_CODE;
  351. colorRemapTable[OUTLINE_SHADOW_CODE] = (char) SHADOW_CODE;
  352. }
  353. return colorRemapTable;
  354. }
  355. //--------- End of function Game::get_color_remap_table --------//