OTECHRES.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 : OTECHRES.CPP
  21. //Description : Tech class
  22. #include <ALL.h>
  23. #include <OGAME.h>
  24. #include <OGAMESET.h>
  25. #include <OF_RESE.h>
  26. #include <OUNIT.h>
  27. #include <ORESX.h>
  28. #include <ONATION.h>
  29. #include <OTECHRES.h>
  30. //---------- #define constant ------------//
  31. #define TECH_DB "TECH"
  32. #define TECH_CLASS_DB "TECHCLAS"
  33. #define TECH_BITMAP_FILE DIR_RES"I_TECH.RES"
  34. //------- Begin of function TechRes::TechRes -----------//
  35. TechRes::TechRes()
  36. {
  37. init_flag=0;
  38. }
  39. //--------- End of function TechRes::TechRes -----------//
  40. //---------- Begin of function TechRes::init -----------//
  41. //
  42. // This function must be called after a map is generated.
  43. //
  44. void TechRes::init()
  45. {
  46. deinit();
  47. //---------- init bitmap resource ---------//
  48. res_bitmap.init(TECH_BITMAP_FILE, 1); // 1-read all into buffer
  49. //------- load database information --------//
  50. load_tech_class();
  51. load_tech_info();
  52. init_flag=1;
  53. }
  54. //---------- End of function TechRes::init -----------//
  55. //---------- Begin of function TechRes::deinit -----------//
  56. void TechRes::deinit()
  57. {
  58. if( init_flag )
  59. {
  60. mem_del(tech_class_array);
  61. mem_del(tech_info_array);
  62. res_bitmap.deinit();
  63. init_flag=0;
  64. }
  65. }
  66. //---------- End of function TechRes::deinit -----------//
  67. //------- Begin of function TechRes::load_tech_class -------//
  68. //
  69. // Read in information of TECHCLAS.DBF into memory array
  70. //
  71. void TechRes::load_tech_class()
  72. {
  73. TechClassRec *techClassRec;
  74. TechClass *techClass;
  75. Database *dbTechClass = game_set.open_db(TECH_CLASS_DB);
  76. tech_class_count = (short) dbTechClass->rec_count();
  77. tech_class_array = (TechClass*) mem_add( sizeof(TechClass)*tech_class_count );
  78. //------ read in tech information array -------//
  79. memset( tech_class_array, 0, sizeof(TechClass) * tech_class_count );
  80. for( int i=0 ; i<tech_class_count ; i++ )
  81. {
  82. techClassRec = (TechClassRec*) dbTechClass->read(i+1);
  83. techClass = tech_class_array+i;
  84. techClass->class_id = i+1;
  85. techClass->icon_index = res_bitmap.get_index( m.nullify(techClassRec->icon_name, techClassRec->ICON_NAME_LEN) );
  86. err_when( !techClass->icon_index );
  87. }
  88. }
  89. //--------- End of function TechRes::load_tech_class ---------//
  90. //------- Begin of function TechRes::load_tech_info -------//
  91. //
  92. // Read in information of TECH.DBF into memory array
  93. //
  94. void TechRes::load_tech_info()
  95. {
  96. TechRec *techRec;
  97. TechInfo *techInfo;
  98. Database *dbTech = game_set.open_db(TECH_DB);
  99. tech_count = (short) dbTech->rec_count();
  100. tech_info_array = (TechInfo*) mem_add( sizeof(TechInfo)*tech_count );
  101. //------ read in tech information array -------//
  102. memset( tech_info_array, 0, sizeof(TechInfo) * tech_count );
  103. int techClassId=0;
  104. TechClass* techClass;
  105. total_tech_level = 0;
  106. for( int i=0 ; i<tech_count ; i++ )
  107. {
  108. techRec = (TechRec*) dbTech->read(i+1);
  109. techInfo = tech_info_array+i;
  110. techInfo->tech_id = i+1;
  111. techInfo->class_id = m.atoi( techRec->class_id , techRec->ID_LEN );
  112. techInfo->max_tech_level = m.atoi( techRec->max_tech_level, techRec->MAX_TECH_LEVEL_LEN );
  113. techInfo->complex_level = m.atoi( techRec->complex_level , techRec->COMPLEX_LEVEL_LEN );
  114. techInfo->unit_id = m.atoi( techRec->unit_id , techRec->ID_LEN );
  115. techInfo->firm_id = m.atoi( techRec->firm_id , techRec->ID_LEN );
  116. techInfo->parent_unit_id = m.atoi( techRec->parent_unit_id, techRec->ID_LEN );
  117. techInfo->parent_firm_id = m.atoi( techRec->parent_firm_id, techRec->ID_LEN );
  118. techInfo->parent_level = techRec->parent_level - '0';
  119. if( techInfo->parent_unit_id || techInfo->parent_firm_id )
  120. err_when( techInfo->parent_level<1 || techInfo->parent_level>9 );
  121. techInfo->icon_index = res_bitmap.get_index( m.nullify(techRec->icon_name, techRec->ICON_NAME_LEN) );
  122. if( techClassId != techInfo->class_id )
  123. {
  124. techClass = tech_class(techInfo->class_id);
  125. techClassId = techInfo->class_id;
  126. techClass->first_tech_id = i+1;
  127. techClass->tech_count = 1;
  128. }
  129. else
  130. techClass->tech_count++;
  131. total_tech_level += techInfo->max_tech_level;
  132. }
  133. }
  134. //--------- End of function TechRes::load_tech_info ---------//
  135. //---------- Begin of function TechClass::tech_icon -----------//
  136. char* TechClass::tech_icon()
  137. {
  138. return tech_res.res_bitmap.get_data(icon_index);
  139. }
  140. //---------- End of function TechClass::tech_icon -----------//
  141. //---------- Begin of function TechInfo::tech_large_icon -----------//
  142. char* TechInfo::tech_large_icon()
  143. {
  144. if( unit_id )
  145. // ######## begin Gilbert 8/8 #########//
  146. return unit_res[unit_id]->get_large_icon_ptr(0);
  147. // ######## end Gilbert 8/8 #########//
  148. else
  149. return tech_res.res_bitmap.get_data(icon_index);
  150. }
  151. //---------- End of function TechInfo::tech_large_icon -----------//
  152. //---------- Begin of function TechInfo::tech_small_icon -----------//
  153. char* TechInfo::tech_small_icon()
  154. {
  155. if( unit_id )
  156. // ####### begin Gilbert 17/10 #########//
  157. return unit_res[unit_id]->get_small_icon_ptr(RANK_SOLDIER);
  158. // ####### end Gilbert 17/10 #########//
  159. else
  160. return tech_res.res_bitmap.get_data(icon_index);
  161. }
  162. //---------- End of function TechInfo::tech_small_icon -----------//
  163. //---------- Begin of function TechInfo::tech_des -----------//
  164. char* TechInfo::tech_des()
  165. {
  166. if( unit_id )
  167. return unit_res[unit_id]->name;
  168. else if( firm_id )
  169. return firm_res[firm_id]->name;
  170. else
  171. return "";
  172. }
  173. //---------- End of function TechInfo::tech_des -----------//
  174. #ifdef DEBUG
  175. //---------- Begin of function TechRes::operator[] -----------//
  176. TechInfo* TechRes::operator[](int techId)
  177. {
  178. err_if( techId<1 || techId>tech_count )
  179. err_now( "TechRes::operator[]" );
  180. return tech_info_array+techId-1;
  181. }
  182. //------------ End of function TechRes::operator[] -----------//
  183. //---------- Begin of function TechRes::tech_class -----------//
  184. TechClass* TechRes::tech_class(int techClassId)
  185. {
  186. err_if( techClassId<1 || techClassId>tech_count )
  187. err_now( "TechRes::tech_class" );
  188. return tech_class_array+techClassId-1;
  189. }
  190. //------------ End of function TechRes::tech_class -----------//
  191. #endif
  192. //--------- Begin of function TechRes::init_nation_tech --------//
  193. //
  194. // Close down all firms under this nation.
  195. //
  196. void TechRes::init_nation_tech(int nationRecno)
  197. {
  198. int i;
  199. TechInfo* techInfo = tech_res.tech_info_array;
  200. for( i=0 ; i<tech_res.tech_count ; i++, techInfo++ )
  201. {
  202. techInfo->set_nation_tech_level(nationRecno, 0);
  203. }
  204. }
  205. //----------- End of function TechRes::init_nation_tech ---------//
  206. //--------- Begin of function TechInfo::is_parent_tech_invented --------//
  207. //
  208. // Whether this technology can be researched or not.
  209. //
  210. int TechInfo::is_parent_tech_invented(int nationRecno)
  211. {
  212. if( parent_unit_id )
  213. {
  214. if( unit_res[parent_unit_id]->get_nation_tech_level(nationRecno) < parent_level )
  215. return 0;
  216. }
  217. if( parent_firm_id )
  218. {
  219. if( firm_res[parent_firm_id]->get_nation_tech_level(nationRecno) < parent_level )
  220. return 0;
  221. }
  222. return 1;
  223. }
  224. //----------- End of function TechInfo::is_parent_tech_invented ---------//
  225. //--------- Begin of function TechInfo::can_research --------//
  226. //
  227. // Whether this technology can be researched or not.
  228. //
  229. int TechInfo::can_research(int nationRecno)
  230. {
  231. return get_nation_tech_level(nationRecno) < max_tech_level &&
  232. is_parent_tech_invented(nationRecno);
  233. }
  234. //----------- End of function TechInfo::can_research ---------//
  235. //--------- Begin of function TechInfo::progress --------//
  236. //
  237. // Make a progress with this technology's research.
  238. //
  239. // <int> nationRecno - the nation which makes progresses on the research of this technology.
  240. // <float> progressPoint - the progress point to be added to this research
  241. //
  242. // return: <int> 1 - the research is completed
  243. // 0 - the researhc is not completed yet.
  244. //
  245. int TechInfo::progress(int nationRecno, float progressPoint)
  246. {
  247. err_when( nationRecno<1 || nationRecno>nation_array.size() );
  248. nation_research_progress_array[nationRecno-1] += progressPoint;
  249. if( nation_research_progress_array[nationRecno-1] > 100 )
  250. {
  251. set_nation_tech_level( nationRecno, nation_tech_level_array[nationRecno-1]+1 );
  252. nation_research_progress_array[nationRecno-1] = (float) 0;
  253. return 1;
  254. }
  255. return 0;
  256. }
  257. //----------- End of function TechInfo::progress ---------//
  258. //--------- Begin of function TechInfo::get_progress --------//
  259. //
  260. float TechInfo::get_progress(int nationRecno)
  261. {
  262. err_when( nationRecno<1 || nationRecno>nation_array.size() );
  263. return nation_research_progress_array[nationRecno-1];
  264. }
  265. //----------- End of function TechInfo::get_progress ---------//
  266. //------ Begin of function TechInfo::inc_nation_is_researching ------//
  267. //
  268. void TechInfo::inc_nation_is_researching(int nationRecno)
  269. {
  270. err_when( nationRecno<1 || nationRecno>nation_array.size() );
  271. nation_is_researching_array[nationRecno-1]++;
  272. }
  273. //------- End of function TechInfo::inc_nation_is_researching ------//
  274. //------ Begin of function TechInfo::dec_nation_is_researching ------//
  275. //
  276. void TechInfo::dec_nation_is_researching(int nationRecno)
  277. {
  278. err_when( nationRecno<1 || nationRecno>nation_array.size() );
  279. nation_is_researching_array[nationRecno-1]--;
  280. err_when( nation_is_researching_array[nationRecno-1] < 0 );
  281. }
  282. //------- End of function TechInfo::dec_nation_is_researching ------//
  283. //------ Begin of function TechInfo::set_nation_tech_level ------//
  284. //
  285. // Set the nation's tech level on this technology.
  286. //
  287. void TechInfo::set_nation_tech_level(int nationRecno, int techLevel)
  288. {
  289. err_when( nationRecno<1 || nationRecno>nation_array.size() );
  290. err_when( techLevel > max_tech_level );
  291. nation_tech_level_array[nationRecno-1] = techLevel;
  292. if( unit_id )
  293. unit_res[unit_id]->set_nation_tech_level( nationRecno, techLevel );
  294. else if( firm_id )
  295. firm_res[firm_id]->set_nation_tech_level( nationRecno, techLevel );
  296. //--- if the max level has been reached and there are still other firms researching this technology ---//
  297. if( techLevel == max_tech_level && is_nation_researching(nationRecno) > 0 )
  298. {
  299. //---- stop other firms researching the same tech -----//
  300. Firm* firmPtr;
  301. for( int i=firm_array.size() ; i>0 ; i-- )
  302. {
  303. if( firm_array.is_deleted(i) )
  304. continue;
  305. firmPtr = firm_array[i];
  306. if( firmPtr->firm_id == FIRM_RESEARCH &&
  307. firmPtr->nation_recno == nationRecno &&
  308. ((FirmResearch*)firmPtr)->tech_id == tech_id )
  309. {
  310. ((FirmResearch*)firmPtr)->terminate_research();
  311. }
  312. }
  313. }
  314. }
  315. //------- End of function TechInfo::set_nation_tech_level -------//
  316. //---------- Begin of function TechRes::inc_all_tech_level -----------//
  317. //
  318. // One of the cheating functions - increase the levels of all technology
  319. // by one level for the specific nation.
  320. //
  321. void TechRes::inc_all_tech_level(int nationRecno)
  322. {
  323. int curTechLevel;
  324. TechInfo* techInfo = tech_res.tech_info_array;
  325. for( int i=1 ; i<=tech_count ; i++, techInfo++ )
  326. {
  327. curTechLevel = techInfo->get_nation_tech_level(nationRecno);
  328. if( curTechLevel < techInfo->max_tech_level )
  329. techInfo->set_nation_tech_level( nationRecno, curTechLevel+1 );
  330. }
  331. }
  332. //------------ End of function TechRes::inc_all_tech_level -----------//