OAI_INFO.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 : OAI_INFO.CPP
  21. //Description : AI - A.I. info structure
  22. #include <OSYS.h>
  23. #include <OTOWN.h>
  24. #include <OF_MARK.h>
  25. #include <OU_CARA.h>
  26. #include <ONATION.h>
  27. //--------- Begin of function Nation::update_ai_firm_array --------//
  28. //
  29. // <int> firmId - determine which firm array to be returned
  30. //
  31. // <int> actionType: 1 - add a record to the array
  32. // 0 - no addition or deletion, just return
  33. // -1 - del a record from the array
  34. //
  35. // <int> actionRecno : the recno to be deleted, if actionType is -1.
  36. // the recno to be added, if actionType is 1.
  37. // <int> arrayCount: for return the count of the AI info array
  38. //
  39. short* Nation::update_ai_firm_array(int firmId, int actionType, int actionRecno, int& arrayCount)
  40. {
  41. short* rc;
  42. switch(firmId)
  43. {
  44. case FIRM_BASE:
  45. rc = update_ai_array(ai_base_count, ai_base_size, &ai_base_array,
  46. AI_BASE_INC_SIZE, actionType, actionRecno);
  47. arrayCount = ai_base_count;
  48. break;
  49. case FIRM_CAMP:
  50. rc = update_ai_array(ai_camp_count, ai_camp_size, &ai_camp_array,
  51. AI_CAMP_INC_SIZE, actionType, actionRecno);
  52. arrayCount = ai_camp_count;
  53. break;
  54. case FIRM_FACTORY:
  55. rc = update_ai_array(ai_factory_count, ai_factory_size, &ai_factory_array,
  56. AI_FACTORY_INC_SIZE, actionType, actionRecno);
  57. arrayCount = ai_factory_count;
  58. break;
  59. case FIRM_MARKET:
  60. rc = update_ai_array(ai_market_count, ai_market_size, &ai_market_array,
  61. AI_MARKET_INC_SIZE, actionType, actionRecno);
  62. arrayCount = ai_market_count;
  63. break;
  64. case FIRM_INN:
  65. rc = update_ai_array(ai_inn_count, ai_inn_size, &ai_inn_array,
  66. AI_INN_INC_SIZE, actionType, actionRecno);
  67. arrayCount = ai_inn_count;
  68. break;
  69. case FIRM_MINE:
  70. rc = update_ai_array(ai_mine_count, ai_mine_size, &ai_mine_array,
  71. AI_MINE_INC_SIZE, actionType, actionRecno);
  72. arrayCount = ai_mine_count;
  73. break;
  74. case FIRM_RESEARCH:
  75. rc = update_ai_array(ai_research_count, ai_research_size, &ai_research_array,
  76. AI_RESEARCH_INC_SIZE, actionType, actionRecno);
  77. arrayCount = ai_research_count;
  78. break;
  79. case FIRM_WAR_FACTORY:
  80. rc = update_ai_array(ai_war_count, ai_war_size, &ai_war_array,
  81. AI_WAR_INC_SIZE, actionType, actionRecno);
  82. arrayCount = ai_war_count;
  83. break;
  84. case FIRM_HARBOR:
  85. rc = update_ai_array(ai_harbor_count, ai_harbor_size, &ai_harbor_array,
  86. AI_HARBOR_INC_SIZE, actionType, actionRecno);
  87. arrayCount = ai_harbor_count;
  88. break;
  89. default:
  90. err_here();
  91. return 0;
  92. }
  93. return rc;
  94. }
  95. //---------- End of function Nation::update_ai_firm_array --------//
  96. //--------- Begin of function Nation::update_ai_array --------//
  97. //
  98. // <short&> aiInfoCount - the count of the AI info array.
  99. // <short&> aiInfoSize - the size of the AI info array.
  100. // <short**> aiInfoArray - poniter to the AI info array.
  101. // <int> arrayIncSize - the increment size of the array.
  102. //
  103. // <int> actionType: 1 - add a record to the array
  104. // 0 - no addition or deletion, just return
  105. // -1 - del a record from the array
  106. //
  107. // [int] actionRecno : the recno to be deleted, if actionType is -1.
  108. // the recno to be added, if actionType is 1.
  109. //
  110. short* Nation::update_ai_array(short& aiInfoCount, short& aiInfoSize,
  111. short** aiInfoArrayPtr, int arrayIncSize, int actionType, int actionRecno)
  112. {
  113. err_when( aiInfoCount<0 );
  114. err_when( aiInfoCount > aiInfoSize );
  115. err_when( actionType<-1 || actionType>1 );
  116. short* aiInfoArray = *aiInfoArrayPtr;
  117. if( actionType == -1 )
  118. {
  119. short* aiInfoPtr = aiInfoArray;
  120. for( int i=0 ; i<aiInfoCount ; i++, aiInfoPtr++ )
  121. {
  122. if( *aiInfoPtr == actionRecno )
  123. {
  124. if( i+1==aiInfoCount ) // the record to be deleted is the last record
  125. {
  126. *aiInfoPtr = 0;
  127. }
  128. else // the record to be deleted is not the last record, somewhere in the array
  129. {
  130. //---- copy the last record to this slot which has been marked for deletion
  131. *aiInfoPtr = aiInfoArray[aiInfoCount-1];
  132. aiInfoArray[aiInfoCount-1] = 0;
  133. }
  134. aiInfoCount--;
  135. return aiInfoArray;
  136. }
  137. }
  138. err_here(); // not found, this shouldn't happen.
  139. }
  140. else if( actionType == 1 )
  141. {
  142. if( aiInfoCount == aiInfoSize )
  143. {
  144. #ifdef DEBUG
  145. short saveDate1 = aiInfoArray[0]; // for vertification of resizing that old data are kept
  146. short saveDate2 = aiInfoArray[aiInfoCount-1];
  147. #endif
  148. aiInfoSize += arrayIncSize;
  149. *aiInfoArrayPtr = (short*) mem_resize( aiInfoArray, aiInfoSize*sizeof(short) );
  150. aiInfoArray = *aiInfoArrayPtr;
  151. err_when( saveDate1 != aiInfoArray[0] ); // for vertification of resizing that old data are kept
  152. err_when( saveDate2 != aiInfoArray[aiInfoCount-1] );
  153. }
  154. aiInfoArray[aiInfoCount++] = actionRecno;
  155. }
  156. return aiInfoArray;
  157. }
  158. //---------- End of function Nation::update_ai_array --------//
  159. //--------- Begin of function Nation::add_firm_info --------//
  160. //
  161. void Nation::add_firm_info(char firmId, short firmRecno)
  162. {
  163. err_when( !firmId || !firmRecno );
  164. int aiFirmCount;
  165. update_ai_firm_array(firmId, 1, firmRecno, aiFirmCount);
  166. }
  167. //---------- End of function Nation::add_firm_info --------//
  168. //--------- Begin of function Nation::del_firm_info --------//
  169. void Nation::del_firm_info(char firmId, short firmRecno)
  170. {
  171. err_when( !firmId || !firmRecno );
  172. int aiFirmCount;
  173. update_ai_firm_array(firmId, -1, firmRecno, aiFirmCount);
  174. }
  175. //---------- End of function Nation::del_firm_info --------//
  176. //--------- Begin of function Nation::update_ai_region --------//
  177. void Nation::update_ai_region()
  178. {
  179. Town* townPtr;
  180. int regionRecno;
  181. memset( ai_region_array, 0, sizeof(ai_region_array) );
  182. ai_region_count = 0;
  183. for( int i=0 ; i<ai_town_count ; i++ )
  184. {
  185. townPtr = town_array[ ai_town_array[i] ];
  186. //---- see if this region has been included -------//
  187. regionRecno=0;
  188. for( int j=0 ; j<ai_region_count ; j++ )
  189. {
  190. if( ai_region_array[j].region_id == townPtr->region_id )
  191. {
  192. regionRecno = j+1;
  193. break;
  194. }
  195. }
  196. if( !regionRecno ) // not included yet
  197. {
  198. if( ai_region_count == MAX_AI_REGION ) // no space for adding new region
  199. continue;
  200. err_when( ai_region_count > MAX_AI_REGION );
  201. ai_region_array[ai_region_count++].region_id = townPtr->region_id;
  202. regionRecno = ai_region_count;
  203. }
  204. //--- increase the town and base_town_count of the nation ---//
  205. ai_region_array[regionRecno-1].town_count++;
  206. if( townPtr->is_base_town )
  207. ai_region_array[regionRecno-1].base_town_count++;
  208. }
  209. }
  210. //---------- End of function Nation::update_ai_region --------//
  211. //--------- Begin of function Nation::add_town_info --------//
  212. void Nation::add_town_info(short townRecno)
  213. {
  214. update_ai_array(ai_town_count, ai_town_size, &ai_town_array,
  215. AI_TOWN_INC_SIZE, 1, townRecno);
  216. update_ai_region();
  217. }
  218. //---------- End of function Nation::add_town_info --------//
  219. //--------- Begin of function Nation::del_town_info --------//
  220. void Nation::del_town_info(short townRecno)
  221. {
  222. err_when( ai_base_town_count<0 );
  223. //--- if this is a base town, decrease the base town counter ---//
  224. if( town_array[townRecno]->is_base_town )
  225. {
  226. ai_base_town_count--;
  227. err_when( ai_base_town_count<0 );
  228. }
  229. //------- delete the record from ai_town_array ------//
  230. update_ai_array(ai_town_count, ai_town_size, &ai_town_array,
  231. AI_TOWN_INC_SIZE, -1, townRecno);
  232. update_ai_region();
  233. }
  234. //---------- End of function Nation::del_town_info --------//
  235. //--------- Begin of function Nation::add_general_info --------//
  236. void Nation::add_general_info(short unitRecno)
  237. {
  238. Unit* unitPtr = unit_array[unitRecno];
  239. err_when( unitPtr->rank_id != RANK_KING && unitPtr->rank_id != RANK_GENERAL );
  240. update_ai_array(ai_general_count, ai_general_size,
  241. &ai_general_array, AI_GENERAL_INC_SIZE, 1, unitRecno);
  242. }
  243. //---------- End of function Nation::add_general_info --------//
  244. //--------- Begin of function Nation::del_general_info --------//
  245. void Nation::del_general_info(short unitRecno)
  246. {
  247. Unit* unitPtr = unit_array[unitRecno];
  248. err_when( unitPtr->rank_id != RANK_KING && unitPtr->rank_id != RANK_GENERAL );
  249. update_ai_array(ai_general_count, ai_general_size,
  250. &ai_general_array, AI_GENERAL_INC_SIZE, -1, unitRecno);
  251. }
  252. //---------- End of function Nation::del_general_info --------//
  253. //--------- Begin of function Nation::add_caravan_info --------//
  254. void Nation::add_caravan_info(short unitRecno)
  255. {
  256. update_ai_array(ai_caravan_count, ai_caravan_size, &ai_caravan_array,
  257. AI_CARAVAN_INC_SIZE, 1, unitRecno);
  258. }
  259. //---------- End of function Nation::add_caravan_info --------//
  260. //--------- Begin of function Nation::del_caravan_info --------//
  261. void Nation::del_caravan_info(short unitRecno)
  262. {
  263. update_ai_array(ai_caravan_count, ai_caravan_size, &ai_caravan_array,
  264. AI_CARAVAN_INC_SIZE, -1, unitRecno);
  265. }
  266. //---------- End of function Nation::del_caravan_info --------//
  267. //--------- Begin of function Nation::is_caravan_exist --------//
  268. //
  269. // Check whether there is an existing caravan travelling along
  270. // the specific route.
  271. //
  272. // <int> firstStop, secondStop - firm recno of the first and second stops.
  273. // [int] setStopInterval - if this is given, then only caravans
  274. // that have been set stop within the given
  275. // days will be counted as existing ones.
  276. //
  277. int Nation::is_caravan_exist(int firstStop, int secondStop, int setStopInterval)
  278. {
  279. UnitCaravan* unitCaravan;
  280. for( int i=0; i<ai_caravan_count; i++ )
  281. {
  282. unitCaravan = (UnitCaravan*) unit_array[ ai_caravan_array[i] ];
  283. if( ( unitCaravan->stop_array[0].firm_recno == firstStop &&
  284. unitCaravan->stop_array[1].firm_recno == secondStop ) ||
  285. ( unitCaravan->stop_array[1].firm_recno == firstStop &&
  286. unitCaravan->stop_array[0].firm_recno == secondStop ) )
  287. {
  288. if( setStopInterval )
  289. {
  290. if( info.game_date - unitCaravan->last_set_stop_date < setStopInterval )
  291. return unitCaravan->sprite_recno;
  292. }
  293. else
  294. return unitCaravan->sprite_recno;
  295. }
  296. }
  297. return 0;
  298. }
  299. //---------- End of function Nation::is_caravan_exist --------//
  300. //--------- Begin of function Nation::add_ship_info --------//
  301. void Nation::add_ship_info(short unitRecno)
  302. {
  303. update_ai_array(ai_ship_count, ai_ship_size, &ai_ship_array,
  304. AI_SHIP_INC_SIZE, 1, unitRecno);
  305. }
  306. //---------- End of function Nation::add_ship_info --------//
  307. //--------- Begin of function Nation::del_ship_info --------//
  308. void Nation::del_ship_info(short unitRecno)
  309. {
  310. update_ai_array(ai_ship_count, ai_ship_size, &ai_ship_array,
  311. AI_SHIP_INC_SIZE, -1, unitRecno);
  312. }
  313. //---------- End of function Nation::del_ship_info --------//
  314. //--------- Begin of function Nation::has_base_town_in_region --------//
  315. //
  316. // Return whether this nation has any base town in the given region.
  317. //
  318. int Nation::has_base_town_in_region(int regionId)
  319. {
  320. for( int i=0 ; i<ai_region_count ; i++ )
  321. {
  322. if( ai_region_array[i].region_id == regionId )
  323. return ai_region_array[i].base_town_count > 0;
  324. }
  325. return 0;
  326. }
  327. //---------- End of function Nation::has_base_town_in_region --------//
  328. //--------- Begin of function Nation::get_ai_region --------//
  329. //
  330. // Return the AIRegion of the given region id.
  331. //
  332. AIRegion* Nation::get_ai_region(int regionId)
  333. {
  334. for( int i=0 ; i<ai_region_count ; i++ )
  335. {
  336. if( ai_region_array[i].region_id == regionId )
  337. return ai_region_array+i;
  338. }
  339. return 0;
  340. }
  341. //---------- End of function Nation::get_ai_region --------//