OAI_SPY.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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_SPY.CPP
  21. //Description: AI - Spy activities
  22. #include <ALL.h>
  23. #include <OFIRM.h>
  24. #include <OTOWN.h>
  25. #include <OUNIT.h>
  26. #include <ONATION.h>
  27. #include <OSPY.h>
  28. //--------- Begin of function Nation::think_spy --------//
  29. void Nation::think_spy()
  30. {
  31. }
  32. //---------- End of function Nation::think_spy --------//
  33. //--------- Begin of function Nation::ai_assign_spy_to_town --------//
  34. //
  35. // Think about sending spies to the specific town.
  36. //
  37. // <int> townRecno - recno of the town
  38. // [int] raceId - race id. of the spy
  39. // (default: majority_race() of the tonw)
  40. //
  41. // return: <int> 1 - a spy is assigned successfully.
  42. // 0 - failure.
  43. //
  44. int Nation::ai_assign_spy_to_town(int townRecno, int raceId)
  45. {
  46. Town* townPtr = town_array[townRecno];
  47. if( townPtr->population >= MAX_TOWN_POPULATION )
  48. return 0;
  49. if( !raceId )
  50. raceId = townPtr->majority_race();
  51. int mobileOnly = townPtr->nation_recno == nation_recno; // if assign to own towns/firms, only get mobile spies, don't get spies from existing towns/firms as that will result in a loop effect
  52. return ai_assign_spy( townPtr->loc_x1, townPtr->loc_y1, raceId, mobileOnly );
  53. }
  54. //---------- End of function Nation::ai_assign_spy_to_town --------//
  55. //--------- Begin of function Nation::ai_assign_spy_to_firm --------//
  56. //
  57. // Think about sending spies to the specific firm.
  58. //
  59. // return: <int> 1 - a spy is assigned successfully.
  60. // 0 - failure.
  61. //
  62. int Nation::ai_assign_spy_to_firm(int firmRecno)
  63. {
  64. Firm* firmPtr = firm_array[firmRecno];
  65. err_when( !firmPtr->worker_array );
  66. //---- check if the firm is full or not -----//
  67. if( firmPtr->nation_recno == nation_recno ) // if it's our own firm
  68. {
  69. if( firmPtr->is_worker_full() ) // use is_worker_full() for own firms as it take into account of units patrolling outside
  70. return 0;
  71. }
  72. else
  73. {
  74. if( firmPtr->worker_count == MAX_WORKER )
  75. return 0;
  76. }
  77. //------ look for an existing spy -------//
  78. int raceId = firmPtr->majority_race();
  79. int mobileOnly = firmPtr->nation_recno == nation_recno; // if assign to own firms/firms, only get mobile spies, don't get spies from existing firms/firms as that will result in a loop effect
  80. return ai_assign_spy( firmPtr->loc_x1, firmPtr->loc_y1, raceId, mobileOnly );
  81. }
  82. //---------- End of function Nation::ai_assign_spy_to_firm --------//
  83. //--------- Begin of function Nation::ai_assign_spy --------//
  84. //
  85. // Try to locate an existing spy for use.
  86. //
  87. // <int> targetXLoc, targetYLoc - the target location
  88. // [int] spyRaceId - if specified, only spies of this race
  89. // will be located. (default:0)
  90. // [int] mobileOnly - get mobile spies only. (default:0)
  91. //
  92. int Nation::ai_assign_spy(int targetXLoc, int targetYLoc, int spyRaceId, int mobileOnly)
  93. {
  94. int unitRecno=0;
  95. //---- try to find an existing spy ----//
  96. Spy* spyPtr = ai_find_spy( targetXLoc, targetYLoc, spyRaceId, mobileOnly );
  97. if( spyPtr )
  98. unitRecno = spyPtr->mobilize_spy();
  99. //--- if not successful, then try to hire one ---//
  100. if( !unitRecno )
  101. unitRecno = hire_unit(SKILL_SPYING, spyRaceId, targetXLoc, targetYLoc);
  102. //--- if cannot hire one, try to train one ----//
  103. int trainTownRecno=0;
  104. if( !unitRecno )
  105. unitRecno = train_unit(SKILL_SPYING, spyRaceId, targetXLoc, targetYLoc, trainTownRecno);
  106. if( !unitRecno )
  107. return 0;
  108. //------ get the spy object of the unit ------//
  109. Unit* unitPtr = unit_array[unitRecno];
  110. err_when( !unitPtr->spy_recno );
  111. spyPtr = spy_array[unitPtr->spy_recno];
  112. //------- get the nation of the assign destination -----//
  113. Location* locPtr = world.get_loc(targetXLoc, targetYLoc);
  114. int cloakedNationRecno;
  115. if( locPtr->is_firm() )
  116. {
  117. Firm* firmPtr = firm_array[locPtr->firm_recno()];
  118. err_when( firmPtr->nation_recno==0 ); // cannot assign to a monster firm
  119. cloakedNationRecno = firmPtr->nation_recno;
  120. }
  121. else if( locPtr->is_town() )
  122. {
  123. Town* townPtr = town_array[locPtr->town_recno()];
  124. cloakedNationRecno = townPtr->nation_recno;
  125. }
  126. else
  127. {
  128. return 0; // the original firm or town has been destroyed or sold
  129. }
  130. //------- Add the assign spy action --------//
  131. int actionRecno = add_action( targetXLoc, targetYLoc,
  132. -1, -1, ACTION_AI_ASSIGN_SPY, cloakedNationRecno, 1, unitRecno );
  133. if( !actionRecno )
  134. return 0;
  135. //------ if the unit is under training ------//
  136. if( trainTownRecno )
  137. town_array[trainTownRecno]->train_unit_action_id = get_action(actionRecno)->action_id;
  138. return 1;
  139. }
  140. //---------- End of function Nation::ai_assign_spy --------//
  141. //--------- Begin of function Nation::ai_find_spy --------//
  142. //
  143. // Try to locate an existing spy for use.
  144. //
  145. // <int> targetXLoc, targetYLoc - the target location
  146. // [int] spyRaceId - if specified, only spies of this race
  147. // will be located. (default:0)
  148. // [int] mobileOnly - get mobile spies only. (default:0)
  149. //
  150. Spy* Nation::ai_find_spy(int targetXLoc, int targetYLoc, int spyRaceId, int mobileOnly)
  151. {
  152. //--- first check if we have an existing spy ready for the mission ---//
  153. Spy *spyPtr, *bestSpy=NULL;
  154. int curRating, bestRating=0;
  155. int spyXLoc, spyYLoc;
  156. int targetRegionId = world.get_region_id(targetXLoc, targetYLoc);
  157. for(int i=spy_array.size(); i>0; i--)
  158. {
  159. if(spy_array.is_deleted(i))
  160. continue;
  161. spyPtr = spy_array[i];
  162. if( !spyPtr->true_nation_recno != nation_recno )
  163. continue;
  164. if( spyRaceId && spyRaceId != race_id )
  165. continue;
  166. if( spyPtr->spy_place == SPY_MOBILE )
  167. {
  168. Unit* unitPtr = unit_array[spyPtr->spy_place_para];
  169. if( !unitPtr->is_ai_all_stop() )
  170. continue;
  171. spyXLoc = unitPtr->next_x_loc();
  172. spyYLoc = unitPtr->next_y_loc();
  173. }
  174. else
  175. {
  176. if( mobileOnly )
  177. continue;
  178. if( spyPtr->spy_place == SPY_FIRM )
  179. {
  180. Firm* firmPtr = firm_array[spyPtr->spy_place_para];
  181. if( firmPtr->nation_recno != nation_recno ) // only get spies from our own firms
  182. continue;
  183. spyXLoc = firmPtr->center_x;
  184. spyYLoc = firmPtr->center_y;
  185. }
  186. else if( spyPtr->spy_place == SPY_TOWN )
  187. {
  188. Town* townPtr = town_array[spyPtr->spy_place_para];
  189. if( townPtr->nation_recno != nation_recno ) // only get spies from our own towns
  190. continue;
  191. spyXLoc = townPtr->center_x;
  192. spyYLoc = townPtr->center_y;
  193. }
  194. else
  195. continue; // in ships or undefined
  196. }
  197. //--- check if the region ids are matched ---//
  198. if( world.get_region_id(spyXLoc, spyYLoc) != targetRegionId )
  199. continue;
  200. //----------------------------------------//
  201. curRating = world.distance_rating(targetXLoc, targetYLoc, spyXLoc, spyYLoc);
  202. curRating += spyPtr->spy_skill + spyPtr->spy_loyalty/2;
  203. if( curRating > bestRating )
  204. {
  205. bestRating = curRating;
  206. bestSpy = spyPtr;
  207. }
  208. }
  209. return bestSpy;
  210. }
  211. //---------- End of function Nation::ai_find_spy --------//
  212. //----- Begin of function Nation::ai_assign_spy -----//
  213. //
  214. // action_x_loc, action_y_loc - location of the target firm or town
  215. // ref_x_loc, ref_y_loc - not used
  216. // unit_recno - unit recno of the spy
  217. // action_para - the cloak nation recno the spy should set to.
  218. //
  219. int Nation::ai_assign_spy(ActionNode* actionNode)
  220. {
  221. if(!seek_path.total_node_avail)
  222. return 0;
  223. if( unit_array.is_deleted(actionNode->unit_recno) )
  224. return -1;
  225. Unit* spyUnit = unit_array[actionNode->unit_recno];
  226. if( !spyUnit->is_visible() ) // it's still under training, not available yet
  227. return -1;
  228. if( !spyUnit->spy_recno || spyUnit->true_nation_recno() != nation_recno )
  229. return -1;
  230. //------ change the cloak of the spy ------//
  231. int newFlag;
  232. Spy* spyPtr = spy_array[spyUnit->spy_recno];
  233. if( reputation < 0 ) // if the nation's reputation is negative, use sneak mode to avoid chance of being uncovered and further damage the reputation
  234. newFlag = m.random( 2+(-(int)reputation)/5 )==0; // 2 to 22
  235. else
  236. newFlag = m.random(2)==0; // 50% chance of being 1
  237. spyPtr->notify_cloaked_nation_flag = newFlag;
  238. if( !spyUnit->can_spy_change_nation() ) // if the spy can't change nation recno now
  239. {
  240. int destXLoc = spyUnit->next_x_loc() + m.random(20) - 10;
  241. int destYLoc = spyUnit->next_y_loc() + m.random(20) - 10;
  242. destXLoc = max(0, destXLoc);
  243. destXLoc = min(MAX_WORLD_X_LOC-1, destXLoc);
  244. destYLoc = max(0, destYLoc);
  245. destYLoc = min(MAX_WORLD_Y_LOC-1, destXLoc);
  246. spyUnit->move_to( destXLoc, destYLoc );
  247. actionNode->retry_count++; // never give up
  248. return 0; // return now and try again later
  249. }
  250. spyUnit->spy_change_nation(actionNode->action_para,COMMAND_AI);
  251. //------- assign the spy to the target -------//
  252. spyUnit->assign(actionNode->action_x_loc, actionNode->action_y_loc);
  253. //----------------------------------------------------------------//
  254. // Since the spy has already changed its cloaked nation recno
  255. // we cannot set the ai_action_id of the unit as when it needs
  256. // to call action_finished() or action_failure() it will
  257. // use the cloaked nation recno, which is incorrect.
  258. // So we just return -1, noting that the action has been completed.
  259. //----------------------------------------------------------------//
  260. return -1;
  261. }
  262. //----- End of function Nation::ai_assign_spy -----//
  263. //-------- Begin of function Nation::think_assign_spy_target_camp --------//
  264. //
  265. // Think about planting spies into independent towns and enemy towns.
  266. //
  267. int Nation::think_assign_spy_target_camp(int raceId, int regionId)
  268. {
  269. Firm *firmPtr;
  270. int curRating, bestRating=0, bestFirmRecno=0;
  271. for( int firmRecno=firm_array.size() ; firmRecno>0 ; firmRecno-- )
  272. {
  273. if( firm_array.is_deleted(firmRecno) )
  274. continue;
  275. firmPtr = firm_array[firmRecno];
  276. if( firmPtr->nation_recno == nation_recno ) // don't assign to own firm
  277. continue;
  278. if( firmPtr->region_id != regionId )
  279. continue;
  280. if( firmPtr->overseer_recno == 0 ||
  281. firmPtr->worker_count == MAX_WORKER )
  282. {
  283. continue;
  284. }
  285. if( firmPtr->majority_race() != raceId )
  286. continue;
  287. //---------------------------------//
  288. Unit* overseerUnit = unit_array[firmPtr->overseer_recno];
  289. if( overseerUnit->spy_recno ) // if the overseer is already a spy
  290. continue;
  291. curRating = 100 - overseerUnit->loyalty;
  292. if( curRating > bestRating )
  293. {
  294. bestRating = curRating;
  295. bestFirmRecno = firmRecno;
  296. }
  297. }
  298. return bestFirmRecno;
  299. }
  300. //-------- End of function Nation::think_assign_spy_target_camp --------//
  301. //-------- Begin of function Nation::think_assign_spy_target_town --------//
  302. //
  303. // Think about planting spies into independent towns and enemy towns.
  304. //
  305. int Nation::think_assign_spy_target_town(int raceId, int regionId)
  306. {
  307. Town *townPtr;
  308. int townCount = town_array.size();
  309. int townRecno = m.random(townCount)+1;
  310. for( int i=town_array.size() ; i>0 ; i-- )
  311. {
  312. if( ++townRecno > townCount )
  313. townRecno = 1;
  314. if( town_array.is_deleted(townRecno) )
  315. continue;
  316. townPtr = town_array[townRecno];
  317. if( townPtr->nation_recno == nation_recno ) // don't assign to own firm
  318. continue;
  319. if( townPtr->region_id != regionId )
  320. continue;
  321. if( townPtr->population > MAX_TOWN_POPULATION-5 ) // -5 so that even if we assign too many spies to a town at the same time, there will still room for them
  322. continue;
  323. //---- for player towns, don't assign too frequently ----//
  324. if( !townPtr->ai_town )
  325. {
  326. if( m.random(3) != 0 )
  327. continue;
  328. }
  329. //----------------------------------------//
  330. if( townPtr->nation_recno )
  331. {
  332. if( townPtr->race_loyalty_array[raceId-1] < MIN_NATION_DEFEND_LOYALTY ) // no need to assign spies to these towns as they are already very low
  333. continue;
  334. }
  335. else
  336. {
  337. if( townPtr->race_resistance_array[raceId-1][nation_recno-1] < MIN_INDEPENDENT_DEFEND_LOYALTY ) // no need to assign spies to these towns as they are already very low
  338. continue;
  339. }
  340. if( townPtr->majority_race() != raceId )
  341. continue;
  342. return townRecno;
  343. }
  344. return 0;
  345. }
  346. //-------- End of function Nation::think_assign_spy_target_town --------//
  347. //-------- Begin of function Nation::think_assign_spy_own_town --------//
  348. //
  349. // Think about planting spies into independent towns and enemy towns.
  350. //
  351. int Nation::think_assign_spy_own_town(int raceId, int regionId)
  352. {
  353. Town *townPtr;
  354. int townCount = town_array.size();
  355. int townRecno = m.random(townCount)+1;
  356. int spyCount;
  357. for( int i=town_array.size() ; i>0 ; i-- )
  358. {
  359. if( ++townRecno > townCount )
  360. townRecno = 1;
  361. if( town_array.is_deleted(townRecno) )
  362. continue;
  363. townPtr = town_array[townRecno];
  364. if( townPtr->nation_recno != nation_recno ) // only assign to own firm
  365. continue;
  366. if( townPtr->region_id != regionId )
  367. continue;
  368. if( townPtr->population > MAX_TOWN_POPULATION-5 )
  369. continue;
  370. if( townPtr->majority_race() != raceId )
  371. continue;
  372. int curSpyLevel = spy_array.total_spy_skill_level( SPY_TOWN, townRecno, nation_recno, spyCount );
  373. int neededSpyLevel = townPtr->needed_anti_spy_level();
  374. if( neededSpyLevel > curSpyLevel + 30 )
  375. return townRecno;
  376. }
  377. return 0;
  378. }
  379. //-------- End of function Nation::think_assign_spy_own_town --------//