OTOWNAI.cpp 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801
  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 : OTOWNAI.CPP
  21. //Description : Object Town AI
  22. #include <OWORLD.h>
  23. #include <OUNIT.h>
  24. #include <OCONFIG.h>
  25. #include <OSITE.h>
  26. #include <ONEWS.h>
  27. #include <OTECHRES.h>
  28. #include <OWALLRES.h>
  29. #include <ORACERES.h>
  30. #include <OGODRES.h>
  31. #include <OSPY.h>
  32. #include <OTOWN.h>
  33. #include <ONATION.h>
  34. #include <OINFO.h>
  35. #include <OFIRMALL.h>
  36. #include <OLOG.h>
  37. //--------- Begin of function Town::process_ai ----------//
  38. //
  39. void Town::process_ai()
  40. {
  41. Nation* ownNation = nation_array[nation_recno];
  42. #if defined(DEBUG) && defined(ENABLE_LOG)
  43. String logStr;
  44. logStr = "begin Town::process_ai, town_recno=";
  45. logStr += town_recno;
  46. logStr += " nation_recno=";
  47. logStr += nation_recno;
  48. LOG_MSG(logStr);
  49. #endif
  50. //---- think about cancelling the base town status ----//
  51. if( info.game_date%30==town_recno%30 )
  52. {
  53. LOG_MSG(" update_base_town_status");
  54. update_base_town_status();
  55. LOG_MSG(m.get_random_seed());
  56. }
  57. //------ think about granting villagers ---//
  58. if( info.game_date%30==town_recno%30 )
  59. {
  60. LOG_MSG(" think_reward");
  61. think_reward();
  62. LOG_MSG(m.get_random_seed());
  63. }
  64. //----- if this town should migrate now -----//
  65. if( should_ai_migrate() )
  66. {
  67. if( info.game_date%30==town_recno%30 )
  68. {
  69. LOG_MSG(" think_ai_migrate");
  70. think_ai_migrate();
  71. LOG_MSG(m.get_random_seed());
  72. }
  73. return; // don't do anything else if the town is about to migrate
  74. }
  75. //------ think about building camps first -------//
  76. if( info.game_date%30==town_recno%30 && !no_neighbor_space && // if there is no space in the neighbor area for building a new firm.
  77. population > 1 )
  78. {
  79. LOG_MSG(" think_build_camp");
  80. if( think_build_camp() )
  81. {
  82. LOG_MSG(m.get_random_seed());
  83. return;
  84. }
  85. LOG_MSG(m.get_random_seed());
  86. }
  87. //----- the following are base town only functions ----//
  88. if( !is_base_town )
  89. return;
  90. //------ think about collecting tax ---//
  91. if( info.game_date%30==(town_recno+15)%30 )
  92. {
  93. LOG_MSG("think_collect_tax");
  94. think_collect_tax();
  95. LOG_MSG(m.get_random_seed());
  96. }
  97. //---- think about scouting in an unexplored map ----//
  98. if( info.game_date%30==(town_recno+20)%30 )
  99. {
  100. think_scout();
  101. }
  102. //---- think about splitting the town ---//
  103. if( info.game_date%30==town_recno%30 )
  104. {
  105. LOG_MSG("think_split_town");
  106. think_split_town();
  107. LOG_MSG(m.get_random_seed() );
  108. LOG_MSG("think_move_between_town");
  109. think_move_between_town();
  110. LOG_MSG(m.get_random_seed() );
  111. }
  112. //---- think about attacking firms/units nearby ---//
  113. if( info.game_date%30==town_recno%30 )
  114. {
  115. LOG_MSG(" think_attack_nearby_enemy");
  116. if( think_attack_nearby_enemy() )
  117. {
  118. LOG_MSG(m.get_random_seed());
  119. return;
  120. }
  121. LOG_MSG(m.get_random_seed());
  122. LOG_MSG(" think_attack_linked_enemy");
  123. if( think_attack_linked_enemy() )
  124. {
  125. LOG_MSG(m.get_random_seed());
  126. return;
  127. }
  128. LOG_MSG(m.get_random_seed());
  129. }
  130. //---- think about capturing linked enemy firms ----//
  131. if( info.game_date%60==town_recno%60 )
  132. {
  133. LOG_MSG(" think_capture_linked_firm");
  134. think_capture_linked_firm();
  135. LOG_MSG(m.get_random_seed());
  136. }
  137. //---- think about capturing enemy towns ----//
  138. if( info.game_date%120==town_recno%120 )
  139. {
  140. LOG_MSG(" think_capture_enemy_town");
  141. think_capture_enemy_town();
  142. LOG_MSG(m.get_random_seed());
  143. }
  144. //---- think about using spies on enemies ----//
  145. if( info.game_date%60==(town_recno+10)%60 )
  146. {
  147. LOG_MSG(" think_spying_town");
  148. if( think_spying_town() )
  149. {
  150. LOG_MSG(m.get_random_seed());
  151. return;
  152. }
  153. }
  154. //---- think about anti-spies activities ----//
  155. if( info.game_date%60==(town_recno+20)%60 )
  156. {
  157. LOG_MSG(" think_counter_spy");
  158. if( think_counter_spy() )
  159. {
  160. LOG_MSG(m.get_random_seed());
  161. return;
  162. }
  163. }
  164. //--- think about setting up firms next to this town ---//
  165. if( info.game_date%30==town_recno%30 && !no_neighbor_space && // if there is no space in the neighbor area for building a new firm.
  166. population >= 5 )
  167. {
  168. LOG_MSG(" think_build_market");
  169. if( think_build_market() )
  170. {
  171. LOG_MSG(m.get_random_seed());
  172. return;
  173. }
  174. LOG_MSG(m.get_random_seed());
  175. //--- the following functions will only be called when the nation has at least a mine ---//
  176. if( site_array.untapped_raw_count > 0 &&
  177. ownNation->ai_mine_count==0 && // don't build other structures if there are untapped raw sites and our nation still doesn't have any
  178. ownNation->true_profit_365days() < 0 )
  179. {
  180. return;
  181. }
  182. //---- only build the following if we have enough food ----//
  183. if( ownNation->ai_has_enough_food() )
  184. {
  185. LOG_MSG(" think_build_research");
  186. if( think_build_research() )
  187. {
  188. LOG_MSG(m.get_random_seed());
  189. return;
  190. }
  191. LOG_MSG(m.get_random_seed());
  192. LOG_MSG(" think_build_war_factory");
  193. if( think_build_war_factory() )
  194. {
  195. LOG_MSG(m.get_random_seed());
  196. return;
  197. }
  198. LOG_MSG(m.get_random_seed());
  199. LOG_MSG(" think_build_base");
  200. if( think_build_base() )
  201. {
  202. LOG_MSG(m.get_random_seed());
  203. return;
  204. }
  205. LOG_MSG(m.get_random_seed());
  206. }
  207. //-------- think build inn ---------//
  208. LOG_MSG(" think_build_inn");
  209. think_build_inn();
  210. LOG_MSG(m.get_random_seed());
  211. }
  212. }
  213. //--------- End of function Town::process_ai ----------//
  214. //--------- Begin of function Town::think_defense ----------//
  215. //
  216. void Town::think_defense()
  217. {
  218. int enemyUnitRecno = detect_enemy(3); // only when 3 units are detected, we consider them as enemy
  219. if( !enemyUnitRecno )
  220. return;
  221. Unit* enemyUnit = unit_array[enemyUnitRecno];
  222. int enemyXLoc = enemyUnit->cur_x_loc();
  223. int enemyYLoc = enemyUnit->cur_y_loc();
  224. //----- get our unit that is closet to it to attack it -------//
  225. int i, closestUnitRecno=0, curDis, minDis=0x7FFF;
  226. Unit* unitPtr;
  227. for( i=unit_array.size() ; i>0 ; i-- )
  228. {
  229. if( unit_array.is_deleted(i) )
  230. continue;
  231. unitPtr = unit_array[i];
  232. if( unitPtr->nation_recno == nation_recno )
  233. {
  234. curDis = max( abs(unitPtr->cur_x_loc()-enemyXLoc), abs(unitPtr->cur_y_loc()-enemyYLoc) );
  235. if( curDis < minDis )
  236. {
  237. minDis = curDis;
  238. closestUnitRecno = i;
  239. }
  240. }
  241. }
  242. //-------- attack the enemy now ----------//
  243. if( closestUnitRecno )
  244. unit_array[closestUnitRecno]->attack_unit(enemyUnit->sprite_recno);
  245. }
  246. //--------- End of function Town::think_defense ----------//
  247. //--------- Begin of function Town::detect_enemy ----------//
  248. //
  249. // Detect if there is any enemy in the town. (within the city
  250. // wall area.)
  251. //
  252. // <int> alertNum - only alert when this number of units are detected.
  253. //
  254. int Town::detect_enemy(int alertNum)
  255. {
  256. //------ check if any enemies have entered in the city -----//
  257. int xLoc1 = max(0, loc_x1-WALL_SPACE_LOC);
  258. int yLoc1 = max(0, loc_y1-WALL_SPACE_LOC);
  259. int xLoc2 = min(MAX_WORLD_X_LOC-1, loc_x2+WALL_SPACE_LOC);
  260. int yLoc2 = min(MAX_WORLD_Y_LOC-1, loc_y2+WALL_SPACE_LOC);
  261. int xLoc, yLoc, unitRecno;
  262. int enemyCount=0;
  263. Location* locPtr;
  264. Unit* unitPtr;
  265. for( yLoc=yLoc1 ; yLoc<=yLoc2 ; yLoc++ )
  266. {
  267. locPtr = world.get_loc(xLoc1, yLoc);
  268. for( xLoc=xLoc1 ; xLoc<=xLoc2 ; xLoc++, locPtr++ )
  269. {
  270. if( locPtr->has_unit(UNIT_LAND) )
  271. {
  272. unitRecno = locPtr->unit_recno(UNIT_LAND);
  273. //------- if any enemy detected -------//
  274. unitPtr = unit_array[unitRecno];
  275. if( unitPtr->nation_recno != nation_recno && unitPtr->nation_recno > 0 )
  276. {
  277. if( ++enemyCount >= alertNum )
  278. return unitRecno;
  279. }
  280. }
  281. }
  282. }
  283. return 0;
  284. }
  285. //--------- End of function Town::detect_enemy ----------//
  286. //------- Begin of function Town::think_build_firm --------//
  287. //
  288. // Think about building a specific type of firm next to this town.
  289. //
  290. // <int> firmId - id. of the firm to be built.
  291. // <int> maxFirm - max. no. of firm of this type to be built next to this town.
  292. //
  293. int Town::think_build_firm(int firmId, int maxFirm)
  294. {
  295. Nation* nationPtr = nation_array[nation_recno];
  296. //--- check whether the AI can build a new firm next this firm ---//
  297. if( !nationPtr->can_ai_build(firmId) )
  298. return 0;
  299. //-- only build one market place next to this town, check if there is any existing one --//
  300. Firm* firmPtr;
  301. int firmCount=0;
  302. for(int i=0; i<linked_firm_count; i++)
  303. {
  304. err_when(!linked_firm_array[i] || firm_array.is_deleted(linked_firm_array[i]));
  305. firmPtr = firm_array[linked_firm_array[i]];
  306. //---- if there is one firm of this type near the town already ----//
  307. if( firmPtr->firm_id == firmId &&
  308. firmPtr->nation_recno == nation_recno )
  309. {
  310. if( ++firmCount >= maxFirm )
  311. return 0;
  312. }
  313. }
  314. //------ queue building a new firm -------//
  315. return ai_build_neighbor_firm(firmId);
  316. }
  317. //-------- End of function Town::think_build_firm ---------//
  318. //------- Begin of function Town::think_collect_tax --------//
  319. //
  320. // Think about collecting tax.
  321. //
  322. void Town::think_collect_tax()
  323. {
  324. if( !has_linked_own_camp )
  325. return;
  326. if( should_ai_migrate() ) // if the town should migrate, do collect tax, otherwise the loyalty will be too low for mobilizing the peasants.
  327. return;
  328. if( accumulated_collect_tax_penalty > 0 )
  329. return;
  330. //--- collect tax if the loyalty of all the races >= minLoyalty (55-85) ---//
  331. int yearProfit = (int) nation_array[nation_recno]->profit_365days();
  332. int minLoyalty = 55 + 30 * nation_array[nation_recno]->pref_loyalty_concern / 100;
  333. if( yearProfit < 0 ) // we are losing money now
  334. minLoyalty -= (-yearProfit) / 100; // more aggressive in collecting tax if we are losing a lot of money
  335. minLoyalty = max( 55, minLoyalty );
  336. //---------------------------------------------//
  337. int achievableLoyalty = average_target_loyalty()-10; // -10 because it's an average, -10 will be safer
  338. if( achievableLoyalty > minLoyalty ) // if the achievable loyalty is higher, then use it
  339. minLoyalty = achievableLoyalty;
  340. if( average_loyalty() < minLoyalty )
  341. return;
  342. //---------- collect tax now ----------//
  343. collect_tax( COMMAND_AI );
  344. }
  345. //-------- End of function Town::think_collect_tax ---------//
  346. //------- Begin of function Town::think_reward --------//
  347. //
  348. // Think about granting the villagers.
  349. //
  350. void Town::think_reward()
  351. {
  352. if( !has_linked_own_camp )
  353. return;
  354. if( accumulated_reward_penalty > 0 )
  355. return;
  356. //---- if accumulated_reward_penalty>0, don't grant unless the villagers are near the rebel level ----//
  357. Nation* ownNation = nation_array[nation_recno];
  358. int averageLoyalty = average_loyalty();
  359. if( averageLoyalty < REBEL_LOYALTY + 5 + ownNation->pref_loyalty_concern/10 ) // 35 to 45
  360. {
  361. int importanceRating;
  362. if( averageLoyalty < REBEL_LOYALTY+5 )
  363. importanceRating = 40+population;
  364. else
  365. importanceRating = population;
  366. if( ownNation->ai_should_spend(importanceRating) )
  367. reward( COMMAND_AI );
  368. }
  369. }
  370. //-------- End of function Town::think_reward ---------//
  371. //------- Begin of function Town::think_ai_migrate --------//
  372. //
  373. // Think about migrating to another town.
  374. //
  375. int Town::think_ai_migrate()
  376. {
  377. if( info.game_date < setup_date+90 ) // don't move if this town has just been set up for less than 90 days. It may be a town set up by think_split_town()
  378. return 0;
  379. Nation* nationPtr = nation_array[nation_recno];
  380. //-- the higher the loyalty, the higher the chance all the unit can be migrated --//
  381. int averageLoyalty = average_loyalty();
  382. int minMigrateLoyalty = 35 + nationPtr->pref_loyalty_concern/10; // 35 to 45
  383. if( averageLoyalty < minMigrateLoyalty )
  384. {
  385. //-- if the total population is low (we need people) and the cash is high (we have money), then grant to increase loyalty for migration --//
  386. if( accumulated_reward_penalty==0 &&
  387. average_target_loyalty() < minMigrateLoyalty+5 ) // if the average target loyalty is also lower than
  388. {
  389. if( nationPtr->ai_should_spend( 20+nationPtr->pref_territorial_cohesiveness/2 ) ) // 20 to 70
  390. reward( COMMAND_AI );
  391. }
  392. if( average_loyalty() < minMigrateLoyalty )
  393. return 0;
  394. }
  395. if( !should_ai_migrate() )
  396. return 0;
  397. //------ think about which town to migrate to ------//
  398. int bestTownRecno = think_ai_migrate_to_town();
  399. if( !bestTownRecno )
  400. return 0;
  401. //--- check if there are already units currently migrating to the destination town ---//
  402. Town* destTownPtr = town_array[bestTownRecno];
  403. if( nationPtr->is_action_exist( destTownPtr->loc_x1, destTownPtr->loc_y1,
  404. loc_x1, loc_y1, ACTION_AI_SETTLE_TO_OTHER_TOWN, 0, 0, 1 ) ) // last 1-check duplication on the destination town only
  405. {
  406. return 0;
  407. }
  408. //--------- queue for migration now ---------//
  409. int migrateCount = (average_loyalty() - MIN_RECRUIT_LOYALTY) / 5;
  410. migrateCount = min( migrateCount, jobless_population );
  411. if( migrateCount <= 0 )
  412. return 0;
  413. nationPtr->add_action( destTownPtr->loc_x1, destTownPtr->loc_y1,
  414. loc_x1, loc_y1, ACTION_AI_SETTLE_TO_OTHER_TOWN, 0, migrateCount);
  415. return 1;
  416. }
  417. //-------- End of function Town::think_ai_migrate ---------//
  418. //------- Begin of function Town::think_ai_migrate_to_town --------//
  419. //
  420. // Think about the town to migrate to.
  421. //
  422. int Town::think_ai_migrate_to_town()
  423. {
  424. //------ think about which town to migrate to ------//
  425. Nation* nationPtr = nation_array[nation_recno];
  426. int curRating, bestRating=0, bestTownRecno=0;
  427. short *aiTownPtr = nationPtr->ai_town_array;
  428. int majorityRace = majority_race();
  429. Town *townPtr;
  430. for(int i=0; i<nationPtr->ai_town_count; i++, aiTownPtr++)
  431. {
  432. if( town_recno == *aiTownPtr )
  433. continue;
  434. townPtr = town_array[*aiTownPtr];
  435. err_when( townPtr->nation_recno != nation_recno );
  436. if( !townPtr->is_base_town ) // only migrate to base towns
  437. continue;
  438. if( townPtr->region_id != region_id )
  439. continue;
  440. if( population > MAX_TOWN_POPULATION-townPtr->population ) // if the town does not have enough space for the migration
  441. continue;
  442. //--------- compare the ratings ---------//
  443. curRating = 1000 * townPtr->race_pop_array[majorityRace-1] / townPtr->population; // *1000 so that this will have a much bigger weight than the distance rating
  444. curRating += world.distance_rating( center_x, center_y, townPtr->center_x, townPtr->center_y );
  445. if( curRating > bestRating )
  446. {
  447. //--- if there is a considerable population of this race, then must migrate to a town with the same race ---//
  448. if( race_pop_array[majorityRace-1] >= 6 )
  449. {
  450. if( townPtr->majority_race() != majorityRace ) // must be commented out otherwise low population town will never be optimized
  451. continue;
  452. }
  453. bestRating = curRating;
  454. bestTownRecno = townPtr->town_recno;
  455. }
  456. }
  457. return bestTownRecno;
  458. }
  459. //-------- End of function Town::think_ai_migrate_to_town ---------//
  460. //------- Begin of function Town::should_ai_migrate --------//
  461. //
  462. // Whether this town should think about migration or not.
  463. //
  464. int Town::should_ai_migrate()
  465. {
  466. //--- if this town is the base town of the nation's territory, don't migrate ---//
  467. Nation* nationPtr = nation_array[nation_recno];
  468. if( is_base_town || nationPtr->ai_base_town_count==0 ) // don't migrate if this is a base town or there is no base town in this nation
  469. return 0;
  470. if( population-jobless_population>0 ) // if there are workers in this town, don't migrate the town people
  471. return 0;
  472. return 1;
  473. }
  474. //-------- End of function Town::should_ai_migrate ---------//
  475. //-------- Begin of function Town::protection_needed ------//
  476. //
  477. // Return an index from 0 to 100 indicating the military
  478. // protection needed for this town.
  479. //
  480. int Town::protection_needed()
  481. {
  482. int protectionNeeded = population * 10;
  483. Firm* firmPtr;
  484. for( int i=linked_firm_count-1 ; i>=0 ; i-- )
  485. {
  486. firmPtr = firm_array[ linked_firm_array[i] ];
  487. if( firmPtr->nation_recno != nation_recno )
  488. continue;
  489. //----- if this is a camp, add combat level points -----//
  490. if( firmPtr->firm_id == FIRM_MARKET )
  491. {
  492. protectionNeeded += ((FirmMarket*)firmPtr)->stock_value_index()*2;
  493. }
  494. else
  495. {
  496. protectionNeeded += (int) firmPtr->productivity*2;
  497. if( firmPtr->firm_id == FIRM_MINE ) // more protection for mines
  498. protectionNeeded += 200;
  499. }
  500. }
  501. return protectionNeeded;
  502. }
  503. //---------- End of function Town::protection_needed ------//
  504. //-------- Begin of function Town::protection_available ------//
  505. //
  506. // Return an index from 0 to 100 indicating the military
  507. // protection currently available for this town.
  508. //
  509. int Town::protection_available()
  510. {
  511. int protectionLevel=0;
  512. Firm* firmPtr;
  513. for( int i=linked_firm_count-1 ; i>=0 ; i-- )
  514. {
  515. firmPtr = firm_array[ linked_firm_array[i] ];
  516. if( firmPtr->nation_recno != nation_recno )
  517. continue;
  518. //----- if this is a camp, add combat level points -----//
  519. if( firmPtr->firm_id == FIRM_CAMP )
  520. protectionLevel += 10 + ((FirmCamp*)firmPtr)->total_combat_level(); // +10 for the existence of the camp structure
  521. }
  522. return protectionLevel;
  523. }
  524. //---------- End of function Town::protection_available ------//
  525. //-------- Begin of function Town::think_build_market ------//
  526. //
  527. int Town::think_build_market()
  528. {
  529. if( info.game_date < setup_date + 90 ) // don't build the market too soon, as it may need to migrate to other town
  530. return 0;
  531. Nation* ownNation = nation_array[nation_recno];
  532. if( population < 10 + (100-ownNation->pref_trading_tendency)/20 )
  533. return 0;
  534. if( no_neighbor_space ) // if there is no space in the neighbor area for building a new firm.
  535. return 0;
  536. //--- check whether the AI can build a new firm next this firm ---//
  537. if( !ownNation->can_ai_build(FIRM_MARKET) )
  538. return 0;
  539. //----------------------------------------------------//
  540. // If there is already a firm queued for building with
  541. // a building location that is within the effective range
  542. // of the this firm.
  543. //----------------------------------------------------//
  544. if( ownNation->is_build_action_exist(FIRM_MARKET, center_x, center_y) )
  545. return 0;
  546. //-- only build one market place next to this mine, check if there is any existing one --//
  547. FirmMarket* firmPtr;
  548. for(int i=0; i<linked_firm_count; i++)
  549. {
  550. firmPtr = (FirmMarket*) firm_array[linked_firm_array[i]];
  551. if(firmPtr->firm_id!=FIRM_MARKET)
  552. continue;
  553. //------ if this market is our own one ------//
  554. if( firmPtr->nation_recno == nation_recno &&
  555. ((FirmMarket*)firmPtr)->is_retail_market )
  556. {
  557. return 0;
  558. }
  559. }
  560. //------ queue building a new market -------//
  561. short buildXLoc, buildYLoc;
  562. if( !ownNation->find_best_firm_loc(FIRM_MARKET, loc_x1, loc_y1, buildXLoc, buildYLoc) )
  563. {
  564. no_neighbor_space = 1;
  565. return 0;
  566. }
  567. ownNation->add_action(buildXLoc, buildYLoc, loc_x1, loc_y1, ACTION_AI_BUILD_FIRM, FIRM_MARKET);
  568. return 1;
  569. }
  570. //-------- End of function Town::think_build_market ------//
  571. //-------- Begin of function Town::think_build_camp ------//
  572. //
  573. // Think about building military camps for protecting this town.
  574. //
  575. int Town::think_build_camp()
  576. {
  577. //----- check if any of the other camps protecting this town is still recruiting soldiers, if so, wait until their recruitment is finished. So we can measure the protection available accurately.
  578. Nation* ownNation = nation_array[nation_recno];
  579. FirmCamp* firmCamp;
  580. Firm* firmPtr;
  581. int campCount=0;
  582. for(int i=linked_firm_count-1; i>=0; --i)
  583. {
  584. err_when(firm_array.is_deleted(linked_firm_array[i]));
  585. firmPtr = firm_array[linked_firm_array[i]];
  586. if(firmPtr->firm_id!=FIRM_CAMP)
  587. continue;
  588. firmCamp = (FirmCamp*) firmPtr;
  589. if( firmCamp->nation_recno != nation_recno )
  590. continue;
  591. if( firmCamp->under_construction || firmCamp->ai_recruiting_soldier ) // if this camp is still trying to recruit soldiers
  592. return 0;
  593. campCount++;
  594. }
  595. //--- this is one of the few base towns the nation has, then a camp must be built ---//
  596. if( campCount==0 && is_base_town && ownNation->ai_base_town_count<=2 )
  597. {
  598. return ai_build_neighbor_firm(FIRM_CAMP);
  599. }
  600. //---- only build camp if we have enough cash and profit ----//
  601. if( !ownNation->ai_should_spend(70+ownNation->pref_military_development/4) ) // 70 to 95
  602. return 0;
  603. //---- only build camp if we need more protection than it is currently available ----//
  604. int protectionNeeded = protection_needed();
  605. int protectionAvailable = protection_available();
  606. if( protectionAvailable >= protectionNeeded )
  607. return 0;
  608. Nation* nationPtr = nation_array[nation_recno];
  609. if( !(protectionNeeded>0 && protectionAvailable==0) ) // if protection needed > 0, and protection available is 0, we must build a camp now
  610. {
  611. int needUrgency = 100 * (protectionNeeded-protectionAvailable) / protectionNeeded;
  612. if( nationPtr->total_jobless_population-MAX_WORKER <
  613. (100-needUrgency) * (200 - nationPtr->pref_military_development) / 200 )
  614. {
  615. return 0;
  616. }
  617. }
  618. //--- check if we have enough people to recruit ---//
  619. int buildFlag = 0;
  620. if( nationPtr->total_jobless_population >= 16 )
  621. {
  622. buildFlag = 1;
  623. }
  624. if( nationPtr->total_jobless_population >= 8 && is_base_town )
  625. {
  626. buildFlag = 1;
  627. }
  628. else if( nationPtr->ai_has_should_close_camp(region_id) ) // there is camp that can be closed
  629. {
  630. buildFlag = 1;
  631. }
  632. if( !buildFlag )
  633. return 0;
  634. return ai_build_neighbor_firm(FIRM_CAMP);
  635. }
  636. //---------- End of function Town::think_build_camp ------//
  637. //-------- Begin of function Town::update_product_supply ------//
  638. //
  639. void Town::update_product_supply()
  640. {
  641. FirmMarket* firmPtr;
  642. int productId;
  643. memset( has_product_supply, 0, sizeof(has_product_supply) );
  644. //----- scan for linked market place -----//
  645. for( int i=linked_firm_count-1 ; i>=0 ; i-- )
  646. {
  647. firmPtr = (FirmMarket*) firm_array[ linked_firm_array[i] ];
  648. if( firmPtr->nation_recno != nation_recno ||
  649. firmPtr->firm_id != FIRM_MARKET )
  650. {
  651. continue;
  652. }
  653. //---- check what type of products they are selling ----//
  654. for( int j=0 ; j<MAX_MARKET_GOODS ; j++ )
  655. {
  656. productId = firmPtr->market_goods_array[j].product_raw_id;
  657. has_product_supply[productId-1]++;
  658. }
  659. }
  660. }
  661. //---------- End of function Town::update_product_supply ------//
  662. //------- Begin of function Town::think_build_research -------//
  663. int Town::think_build_research()
  664. {
  665. Nation* nationPtr = nation_array[nation_recno];
  666. if( !is_base_town )
  667. return 0;
  668. if( jobless_population < MAX_WORKER ||
  669. nationPtr->total_jobless_population < MAX_WORKER*2 )
  670. {
  671. return 0;
  672. }
  673. if( nationPtr->true_profit_365days() < 0 )
  674. return 0;
  675. if( !nationPtr->ai_should_spend( 25 + nationPtr->pref_use_weapon/2 - nationPtr->ai_research_count*10 ) )
  676. return 0;
  677. int totalTechLevel = nationPtr->total_tech_level();
  678. if( totalTechLevel == tech_res.total_tech_level ) // all technology have been researched
  679. return 0;
  680. //--------------------------------------------//
  681. int maxResearch = 2 * (50+nationPtr->pref_use_weapon) / 50;
  682. maxResearch = min(nationPtr->ai_town_count, maxResearch);
  683. if( nationPtr->ai_research_count >= maxResearch )
  684. return 0;
  685. //---- if any of the existing ones are not full employed ----//
  686. FirmResearch* firmResearch;
  687. for( int i=0 ; i<nationPtr->ai_research_count ; i++ )
  688. {
  689. firmResearch = (FirmResearch*) firm_array[ nationPtr->ai_research_array[i] ];
  690. if( firmResearch->region_id != region_id )
  691. continue;
  692. if( firmResearch->worker_count < MAX_WORKER )
  693. return 0;
  694. }
  695. //------- queue building a war factory -------//
  696. return ai_build_neighbor_firm(FIRM_RESEARCH);
  697. }
  698. //-------- End of function Town::think_build_research -------//
  699. //------- Begin of function Town::think_build_war_factory -------//
  700. int Town::think_build_war_factory()
  701. {
  702. Nation* nationPtr = nation_array[nation_recno];
  703. if( !is_base_town )
  704. return 0;
  705. if( jobless_population < MAX_WORKER ||
  706. nationPtr->total_jobless_population < MAX_WORKER*2 )
  707. {
  708. return 0;
  709. }
  710. int totalWeaponTechLevel = nationPtr->total_tech_level(UNIT_CLASS_WEAPON);
  711. if( totalWeaponTechLevel==0 )
  712. return 0;
  713. //----- see if we have enough money to build & support the weapon ----//
  714. if( nationPtr->true_profit_365days() < 0 && nationPtr->ai_war_count > 0 ) // if we don't have any war factory, we may want to build one despite that we are losing money
  715. return 0;
  716. if( !nationPtr->ai_should_spend( nationPtr->pref_use_weapon/2 ) )
  717. return 0;
  718. //--------------------------------------------//
  719. int maxWarFactory = (1+totalWeaponTechLevel) * nationPtr->pref_use_weapon / 100;
  720. maxWarFactory = min(nationPtr->ai_town_count, maxWarFactory);
  721. if( nationPtr->ai_war_count >= maxWarFactory )
  722. return 0;
  723. //---- if any of the existing ones are not full employed or under capacity ----//
  724. FirmWar* firmWar;
  725. for( int i=0 ; i<nationPtr->ai_war_count ; i++ )
  726. {
  727. firmWar = (FirmWar*) firm_array[ nationPtr->ai_war_array[i] ];
  728. if( firmWar->region_id != region_id )
  729. continue;
  730. if( firmWar->worker_count < MAX_WORKER || !firmWar->build_unit_id )
  731. return 0;
  732. }
  733. //------- queue building a war factory -------//
  734. return ai_build_neighbor_firm(FIRM_WAR_FACTORY);
  735. }
  736. //-------- End of function Town::think_build_war_factory -------//
  737. //------- Begin of function Town::think_build_base -------//
  738. //
  739. // Think about building seats of powre.
  740. //
  741. int Town::think_build_base()
  742. {
  743. Nation* nationPtr = nation_array[nation_recno];
  744. if( !is_base_town )
  745. return 0;
  746. //----- see if we have enough money to build & support the weapon ----//
  747. if( !nationPtr->ai_should_spend(50) )
  748. return 0;
  749. if( jobless_population < MAX_BASE_PRAYER/2 ||
  750. nationPtr->total_jobless_population < MAX_BASE_PRAYER )
  751. {
  752. return 0;
  753. }
  754. //------ do a scan on the existing bases first ------//
  755. static short buildRatingArray[MAX_RACE];
  756. memset( buildRatingArray, 0, sizeof(buildRatingArray) );
  757. //--- increase build rating for the seats that this nation knows how to build ---//
  758. GodInfo* godInfo;
  759. int i;
  760. for( i=1 ; i<=god_res.god_count ; i++ )
  761. {
  762. godInfo = god_res[i];
  763. if( godInfo->is_nation_know(nation_recno) )
  764. buildRatingArray[godInfo->race_id-1] += 100;
  765. }
  766. //--- decrease build rating for the seats that the nation currently has ---//
  767. FirmBase* firmBase;
  768. for( i=0 ; i<nationPtr->ai_base_count ; i++ )
  769. {
  770. firmBase = (FirmBase*) firm_array[ nationPtr->ai_base_array[i] ];
  771. buildRatingArray[ god_res[firmBase->god_id]->race_id-1 ] = 0; // only build one
  772. /*
  773. if( firmBase->prayer_count < MAX_BASE_PRAYER )
  774. buildRatingArray[ god_res[firmBase->god_id]->race_id-1 ] = 0;
  775. else
  776. buildRatingArray[ god_res[firmBase->god_id]->race_id-1 ] -= 10; // -10 for one existing instance
  777. */
  778. }
  779. //------ decide which is the best to build -------//
  780. int bestRating=0, bestRaceId=0;
  781. for( i=0 ; i<MAX_RACE ; i++ )
  782. {
  783. if( buildRatingArray[i] > bestRating )
  784. {
  785. bestRating = buildRatingArray[i];
  786. bestRaceId = i+1;
  787. }
  788. }
  789. //------- queue building a seat of power ------//
  790. if( bestRaceId )
  791. return ai_build_neighbor_firm(FIRM_BASE, bestRaceId);
  792. return 0;
  793. }
  794. //-------- End of function Town::think_build_base -------//
  795. //------- Begin of function Town::think_build_inn -------//
  796. //
  797. int Town::think_build_inn()
  798. {
  799. Nation* ownNation = nation_array[nation_recno];
  800. if( ownNation->ai_inn_count < ownNation->ai_supported_inn_count() )
  801. {
  802. return think_build_firm( FIRM_INN, 1 );
  803. }
  804. return 0;
  805. }
  806. //-------- End of function Town::think_build_inn -------//
  807. //------- Begin of function Town::ai_build_neighbor_firm -------//
  808. //
  809. // Build a specific firm next to this town.
  810. //
  811. int Town::ai_build_neighbor_firm(int firmId, int firmRaceId)
  812. {
  813. short buildXLoc, buildYLoc;
  814. Nation* nationPtr = nation_array[nation_recno];
  815. if( !nationPtr->find_best_firm_loc(firmId, loc_x1, loc_y1, buildXLoc, buildYLoc) )
  816. {
  817. no_neighbor_space = 1;
  818. return 0;
  819. }
  820. nationPtr->add_action(buildXLoc, buildYLoc, loc_x1, loc_y1, ACTION_AI_BUILD_FIRM, firmId, 1, 0, firmRaceId);
  821. return 1;
  822. }
  823. //-------- End of function Town::ai_build_neighbor_firm -------//
  824. //------- Begin of function Town::think_split_town -------//
  825. //
  826. // Split the town into two, migrating some population to the new
  827. // town.
  828. //
  829. int Town::think_split_town()
  830. {
  831. if( jobless_population==0 ) // cannot move if we don't have any mobilizable unit
  832. return 0;
  833. //--- split when the population is close to its limit ----//
  834. Nation* nationPtr = nation_array[nation_recno];
  835. if( population < 45 + nationPtr->pref_territorial_cohesiveness / 10 )
  836. return 0;
  837. //-------- think about which race to move --------//
  838. int mostRaceId1, mostRaceId2, raceId=0;
  839. get_most_populated_race(mostRaceId1, mostRaceId2);
  840. if( mostRaceId2 && jobless_race_pop_array[mostRaceId2-1] > 0 && can_recruit(mostRaceId2) )
  841. raceId = mostRaceId2;
  842. else if( mostRaceId1 && jobless_race_pop_array[mostRaceId1-1] > 0 && can_recruit(mostRaceId1) )
  843. raceId = mostRaceId1;
  844. else
  845. raceId = pick_random_race(0, 1); // 0-recruitable only, 1-also pick spy.
  846. if( !raceId )
  847. {
  848. //---- if the racial mix favors a split of town -----//
  849. //
  850. // This is when there are two major races, both of significant
  851. // population in the town. This is a good time for us to move
  852. // the second major race to a new town.
  853. //
  854. //---------------------------------------------------//
  855. if( mostRaceId2 && jobless_race_pop_array[mostRaceId2] > 0 &&
  856. race_pop_array[mostRaceId2] >= population/3 && // the race's has at least 1/3 of the town's total population
  857. can_recruit(mostRaceId2) )
  858. {
  859. raceId = mostRaceId2;
  860. }
  861. }
  862. if( !raceId )
  863. return 0;
  864. //---- check if there is already a town of this race with low population linked to this town, then don't split a new one ---//
  865. Town* townPtr;
  866. for( int i=0 ; i<linked_town_count ; i++ )
  867. {
  868. townPtr = town_array[ linked_town_array[i] ];
  869. if( townPtr->nation_recno == nation_recno &&
  870. townPtr->population < 20 &&
  871. townPtr->majority_race() == raceId )
  872. {
  873. return 0;
  874. }
  875. }
  876. //-------- settle to a new town ---------//
  877. return ai_settle_new(raceId);
  878. }
  879. //-------- End of function Town::think_split_town -------//
  880. //------- Begin of function Town::think_move_between_town -------//
  881. //
  882. void Town::think_move_between_town()
  883. {
  884. //------ move people between linked towns ------//
  885. int ourMajorityRace = majority_race();
  886. int raceId, rc, loopCount;
  887. Town* townPtr;
  888. for( int i=0 ; i<linked_town_count ; i++ )
  889. {
  890. townPtr = town_array[ linked_town_array[i] ];
  891. if( townPtr->nation_recno != nation_recno )
  892. continue;
  893. loopCount=0;
  894. //--- migrate people to the new town ---//
  895. while(1)
  896. {
  897. err_when( ++loopCount > 100 );
  898. rc = 0;
  899. raceId = townPtr->majority_race(); // get the linked town's major race
  900. if( ourMajorityRace != raceId ) // if our major race is not this race, then move the person to the target town
  901. {
  902. rc = 1;
  903. }
  904. else //-- if this town's major race is the same as the target town --//
  905. {
  906. if( population - townPtr->population > 10 ) // if this town's population is larger than the target town by 10 people, then move
  907. rc = 1;
  908. }
  909. if( rc )
  910. {
  911. if( !migrate_to(townPtr->town_recno, COMMAND_AI, raceId) )
  912. break;
  913. }
  914. else
  915. break;
  916. }
  917. }
  918. }
  919. //-------- End of function Town::think_move_between_town -------//
  920. //------- Begin of function Town::ai_settle_new -------//
  921. //
  922. int Town::ai_settle_new(int raceId)
  923. {
  924. //-------- locate space for the new town --------//
  925. int xLoc=loc_x1, yLoc=loc_y1; // xLoc & yLoc are used for returning results
  926. if( !world.locate_space( xLoc, yLoc, loc_x2, loc_y2, STD_TOWN_LOC_WIDTH+2, // STD_TOWN_LOC_WIDTH+2 for space around the town
  927. STD_TOWN_LOC_HEIGHT+2, UNIT_LAND, region_id, 1 ) )
  928. {
  929. return 0;
  930. }
  931. //------- it must be within the effective town-to-town distance ---//
  932. if( m.points_distance( center_x, center_y, xLoc+(STD_TOWN_LOC_WIDTH-1)/2,
  933. yLoc+(STD_TOWN_LOC_HEIGHT-1)/2 ) > EFFECTIVE_TOWN_TOWN_DISTANCE )
  934. {
  935. return 0;
  936. }
  937. //--- recruit a unit from the town and order it to settle a new town ---//
  938. int unitRecno = recruit(-1, raceId, COMMAND_AI);
  939. if( !unitRecno || !unit_array[unitRecno]->is_visible() )
  940. return 0;
  941. unit_array[unitRecno]->settle( xLoc+1, yLoc+1 );
  942. return 1;
  943. }
  944. //-------- End of function Town::ai_settle_new -------//
  945. //------- Begin of function Town::think_attack_nearby_enemy -------//
  946. //
  947. // Think about attacking enemies near the town.
  948. //
  949. int Town::think_attack_nearby_enemy()
  950. {
  951. enum { SCAN_X_RANGE = 6,
  952. SCAN_Y_RANGE = 6 };
  953. int xLoc1 = loc_x1 - SCAN_X_RANGE;
  954. int yLoc1 = loc_y1 - SCAN_Y_RANGE;
  955. int xLoc2 = loc_x2 + SCAN_X_RANGE;
  956. int yLoc2 = loc_y2 + SCAN_Y_RANGE;
  957. xLoc1 = max( xLoc1, 0 );
  958. yLoc1 = max( yLoc1, 0 );
  959. xLoc2 = min( xLoc2, MAX_WORLD_X_LOC-1 );
  960. yLoc2 = min( yLoc2, MAX_WORLD_Y_LOC-1 );
  961. //------------------------------------------//
  962. int enemyCombatLevel=0; // the higher the rating, the easier we can attack the target town.
  963. int xLoc, yLoc;
  964. int enemyXLoc= -1, enemyYLoc;
  965. Unit* unitPtr;
  966. Nation* nationPtr = nation_array[nation_recno];
  967. Firm* firmPtr;
  968. Location* locPtr;
  969. for( yLoc=yLoc1 ; yLoc<=yLoc2 ; yLoc++ )
  970. {
  971. locPtr = world.get_loc(xLoc1, yLoc);
  972. for( xLoc=xLoc1 ; xLoc<=xLoc2 ; xLoc++, locPtr++ )
  973. {
  974. //--- if there is an enemy unit here ---//
  975. if( locPtr->has_unit(UNIT_LAND) )
  976. {
  977. unitPtr = unit_array[ locPtr->unit_recno(UNIT_LAND) ];
  978. if( !unitPtr->nation_recno )
  979. continue;
  980. //--- if the unit is idle and he is our enemy ---//
  981. if( unitPtr->cur_action == SPRITE_IDLE &&
  982. nationPtr->get_relation_status(unitPtr->nation_recno) == NATION_HOSTILE )
  983. {
  984. err_when( unitPtr->nation_recno == nation_recno );
  985. enemyCombatLevel += (int) unitPtr->hit_points;
  986. if( enemyXLoc == -1 || m.random(5)==0 )
  987. {
  988. enemyXLoc = xLoc;
  989. enemyYLoc = yLoc;
  990. }
  991. }
  992. }
  993. //--- if there is an enemy firm here ---//
  994. else if( locPtr->is_firm() )
  995. {
  996. firmPtr = firm_array[locPtr->firm_recno()];
  997. //------- if this is a monster firm ------//
  998. if( firmPtr->firm_id == FIRM_MONSTER ) // don't attack monster here, OAI_MONS.CPP will handle that
  999. continue;
  1000. //------- if this is a firm of our enemy -------//
  1001. if( nationPtr->get_relation_status(firmPtr->nation_recno) == NATION_HOSTILE )
  1002. {
  1003. err_when( firmPtr->nation_recno == nation_recno );
  1004. if( firmPtr->worker_count == 0 )
  1005. enemyCombatLevel += 50; // empty firm
  1006. else
  1007. {
  1008. Worker* workerPtr = firmPtr->worker_array;
  1009. for( int i=firmPtr->worker_count ; i>0 ; i--, workerPtr++ )
  1010. enemyCombatLevel += workerPtr->hit_points;
  1011. }
  1012. if( enemyXLoc == -1 || m.random(5)==0 )
  1013. {
  1014. enemyXLoc = firmPtr->loc_x1;
  1015. enemyYLoc = firmPtr->loc_y1;
  1016. }
  1017. }
  1018. }
  1019. }
  1020. }
  1021. //--------- attack the target now -----------//
  1022. if( enemyCombatLevel > 0 )
  1023. {
  1024. err_when( enemyXLoc < 0 );
  1025. nationPtr->ai_attack_target( enemyXLoc, enemyYLoc, enemyCombatLevel );
  1026. return 1;
  1027. }
  1028. return 0;
  1029. }
  1030. //-------- End of function Town::think_attack_nearby_enemy -------//
  1031. //------- Begin of function Town::think_attack_linked_enemy -------//
  1032. //
  1033. // Think about attacking enemy camps that are linked to this town.
  1034. //
  1035. int Town::think_attack_linked_enemy()
  1036. {
  1037. Firm* firmPtr;
  1038. Nation* ownNation = nation_array[nation_recno];
  1039. int targetCombatLevel;
  1040. for(int i=0; i<linked_firm_count; i++)
  1041. {
  1042. if( linked_firm_enable_array[i] != LINK_EE ) // don't attack if the link is not enabled
  1043. continue;
  1044. firmPtr = firm_array[ linked_firm_array[i] ];
  1045. if( firmPtr->nation_recno == nation_recno )
  1046. continue;
  1047. if( firmPtr->should_close_flag ) // if this camp is about to close
  1048. continue;
  1049. //--- only attack AI firms when they belong to a hostile nation ---//
  1050. if( firmPtr->firm_ai &&
  1051. ownNation->get_relation_status(firmPtr->nation_recno) != NATION_HOSTILE )
  1052. {
  1053. continue;
  1054. }
  1055. //---- if this is a camp -----//
  1056. if( firmPtr->firm_id == FIRM_CAMP )
  1057. {
  1058. //----- if we are friendly with the target nation ------//
  1059. int nationStatus = ownNation->get_relation_status(firmPtr->nation_recno);
  1060. if( nationStatus >= NATION_FRIENDLY )
  1061. {
  1062. if( !ownNation->ai_should_attack_friendly(firmPtr->nation_recno, 100) )
  1063. continue;
  1064. ownNation->ai_end_treaty(firmPtr->nation_recno);
  1065. }
  1066. else if( nationStatus == NATION_NEUTRAL )
  1067. {
  1068. //-- if the link is off and the nation's miliary strength is bigger than us, don't attack --//
  1069. if( nation_array[firmPtr->nation_recno]->military_rank_rating() >
  1070. ownNation->military_rank_rating() )
  1071. {
  1072. continue;
  1073. }
  1074. }
  1075. //--- don't attack when the trade rating is high ----//
  1076. int tradeRating = ownNation->trade_rating(firmPtr->nation_recno);
  1077. if( tradeRating > 50 ||
  1078. tradeRating + ownNation->ai_trade_with_rating(firmPtr->nation_recno) > 100 )
  1079. {
  1080. continue;
  1081. }
  1082. targetCombatLevel = ((FirmCamp*)firmPtr)->total_combat_level();
  1083. }
  1084. else //--- if this is another type of firm ----//
  1085. {
  1086. //--- only attack other types of firm when the status is hostile ---//
  1087. if( ownNation->get_relation_status(firmPtr->nation_recno) != NATION_HOSTILE )
  1088. continue;
  1089. targetCombatLevel = 50;
  1090. }
  1091. //--------- attack now ----------//
  1092. ownNation->ai_attack_target( firmPtr->loc_x1, firmPtr->loc_y1, targetCombatLevel );
  1093. return 1;
  1094. }
  1095. return 0;
  1096. }
  1097. //-------- End of function Town::think_attack_linked_enemy -------//
  1098. //-------- Begin of function Town::think_capture_linked_firm --------//
  1099. //
  1100. // Try to capture firms linked to this town if all the workers in firms
  1101. // resident in this town.
  1102. //
  1103. void Town::think_capture_linked_firm()
  1104. {
  1105. Firm* firmPtr;
  1106. //----- scan for linked firms -----//
  1107. for( int i=linked_firm_count-1 ; i>=0 ; i-- )
  1108. {
  1109. firmPtr = firm_array[ linked_firm_array[i] ];
  1110. if( firmPtr->nation_recno == nation_recno ) // this is our own firm
  1111. continue;
  1112. if( firmPtr->can_worker_capture(nation_recno) ) // if we can capture this firm, capture it now
  1113. {
  1114. firmPtr->capture_firm(nation_recno);
  1115. }
  1116. }
  1117. }
  1118. //-------- End of function Town::think_capture_linked_firm --------//
  1119. //-------- Begin of function Town::think_capture_enemy_town --------//
  1120. //
  1121. // Think about sending troops from this town to capture enemy towns.
  1122. //
  1123. int Town::think_capture_enemy_town()
  1124. {
  1125. if( !is_base_town )
  1126. return 0;
  1127. Nation* nationPtr = nation_array[nation_recno];
  1128. if( nationPtr->ai_capture_enemy_town_recno ) // a capturing action is already in process
  1129. return 0;
  1130. int surplusProtection = protection_available() - protection_needed();
  1131. //-- only attack enemy town if we have enough military protection surplus ---//
  1132. if( surplusProtection < 300 - nationPtr->pref_military_courage*2 )
  1133. return 0;
  1134. return nationPtr->think_capture_new_enemy_town(this);
  1135. }
  1136. //-------- End of function Town::think_capture_enemy_town --------//
  1137. //-------- Begin of function Town::think_counter_spy --------//
  1138. //
  1139. // Think about planting anti-spy units in this town.
  1140. //
  1141. int Town::think_counter_spy()
  1142. {
  1143. if( population >= MAX_TOWN_POPULATION )
  1144. return 0;
  1145. Nation* ownNation = nation_array[nation_recno];
  1146. if( ownNation->total_spy_count > ownNation->total_population * (10+ownNation->pref_spy/5) / 100 ) // 10% to 30%
  1147. return 0;
  1148. if( !ownNation->ai_should_spend(ownNation->pref_counter_spy/2) ) // 0 to 50
  1149. return 0;
  1150. //--- the expense of spies should not be too large ---//
  1151. if( ownNation->expense_365days(EXPENSE_SPY) >
  1152. ownNation->expense_365days() * (50+ownNation->pref_counter_spy) / 400 )
  1153. {
  1154. return 0;
  1155. }
  1156. //------- check if we need additional spies ------//
  1157. int spyCount;
  1158. int curSpyLevel = spy_array.total_spy_skill_level( SPY_TOWN, town_recno, nation_recno, spyCount );
  1159. if( spyCount >= 1+ownNation->pref_counter_spy/40 ) // 1 to 3 spies at max in each town
  1160. return 0;
  1161. //---- if the spy skill needed is more than the current skill level ----//
  1162. int neededSpyLevel = needed_anti_spy_level();
  1163. if( neededSpyLevel > curSpyLevel+30 )
  1164. {
  1165. return ownNation->ai_assign_spy_to_town(town_recno);
  1166. }
  1167. else
  1168. return 0;
  1169. }
  1170. //-------- End of function Town::think_counter_spy --------//
  1171. //-------- Begin of function Town::needed_anti_spy_level --------//
  1172. //
  1173. // Return the total needed spy level for anti-spying in this town.
  1174. //
  1175. int Town::needed_anti_spy_level()
  1176. {
  1177. return ( linked_firm_count*10 +
  1178. 100 * population / MAX_TOWN_POPULATION )
  1179. * nation_array[nation_recno]->pref_counter_spy / 100;
  1180. }
  1181. //-------- End of function Town::needed_anti_spy_level --------//
  1182. //-------- Begin of function Town::think_spying_town --------//
  1183. //
  1184. // Think about planting spies into independent towns and enemy towns.
  1185. //
  1186. int Town::think_spying_town()
  1187. {
  1188. int majorityRace = majority_race();
  1189. //---- don't recruit spy if we are low in cash or losing money ----//
  1190. Nation* ownNation = nation_array[nation_recno];
  1191. if( ownNation->total_population < 30-ownNation->pref_spy/10 ) // don't use spies if the population is too low, we need to use have people to grow population
  1192. return 0;
  1193. if( ownNation->total_spy_count > ownNation->total_population * (10+ownNation->pref_spy/5) / 100 ) // 10% to 30%
  1194. return 0;
  1195. if( !ownNation->ai_should_spend(ownNation->pref_spy/2) ) // 0 to 50
  1196. return 0;
  1197. //--- pick minority units first (also for increasing town harmony) ---//
  1198. for( int raceId=1 ; raceId<=MAX_RACE ; raceId++ )
  1199. {
  1200. if( race_pop_array[raceId-1]==0 || raceId==majorityRace )
  1201. continue;
  1202. if( !can_recruit(raceId) )
  1203. continue;
  1204. if( think_spying_town_assign_to(raceId) )
  1205. return 1;
  1206. }
  1207. //---- then think about assign spies of majority race ----//
  1208. if( ownNation->pref_spy > 50 ) // only if pref_spy is > 50, it will consider using spies of majority race
  1209. {
  1210. if( can_recruit(majorityRace) )
  1211. {
  1212. if( think_spying_town_assign_to(majorityRace) )
  1213. return 1;
  1214. }
  1215. }
  1216. return 0;
  1217. }
  1218. //-------- End of function Town::think_spying_town --------//
  1219. //-------- Begin of function Town::think_spying_town_assign_to --------//
  1220. //
  1221. // Think about planting spies into independent towns and enemy towns.
  1222. //
  1223. int Town::think_spying_town_assign_to(int raceId)
  1224. {
  1225. Nation* ownNation = nation_array[nation_recno];
  1226. int targetTownRecno = ownNation->think_assign_spy_target_town(race_id, region_id);
  1227. if( !targetTownRecno )
  1228. return 0;
  1229. //------- assign the spy now -------//
  1230. int unitRecno = recruit(SKILL_SPYING, raceId, COMMAND_AI);
  1231. if( !unitRecno )
  1232. return 0;
  1233. Town* targetTown = town_array[targetTownRecno];
  1234. int actionRecno = ownNation->add_action( targetTown->loc_x1, targetTown->loc_y1,
  1235. -1, -1, ACTION_AI_ASSIGN_SPY, targetTown->nation_recno, 1, unitRecno );
  1236. if( !actionRecno )
  1237. return 0;
  1238. train_unit_action_id = ownNation->get_action(actionRecno)->action_id;
  1239. return 1;
  1240. }
  1241. //-------- End of function Town::think_spying_town_assign_to --------//
  1242. //-------- Begin of function Town::update_base_town_status --------//
  1243. //
  1244. void Town::update_base_town_status()
  1245. {
  1246. int newBaseTownStatus = new_base_town_status();
  1247. if( newBaseTownStatus == is_base_town )
  1248. return;
  1249. Nation* ownNation = nation_array[nation_recno];
  1250. if( is_base_town )
  1251. ownNation->ai_base_town_count--;
  1252. if( newBaseTownStatus )
  1253. ownNation->ai_base_town_count++;
  1254. is_base_town = newBaseTownStatus;
  1255. }
  1256. //-------- End of function Town::update_base_town_status --------//
  1257. //-------- Begin of function Town::new_base_town_status --------//
  1258. //
  1259. int Town::new_base_town_status()
  1260. {
  1261. Nation* ownNation = nation_array[nation_recno];
  1262. if( population > 20 + ownNation->pref_territorial_cohesiveness/10 )
  1263. return 1;
  1264. //---- if there is only 1 town, then it must be the base town ---//
  1265. if( ownNation->ai_town_count==1 )
  1266. return 1;
  1267. //---- if there is only 1 town in this region, then it must be the base town ---//
  1268. AIRegion* aiRegion = ownNation->get_ai_region(region_id);
  1269. if( aiRegion && aiRegion->town_count==1 )
  1270. return 1;
  1271. //--- don't drop if there are employed villagers ---//
  1272. if( jobless_population != population )
  1273. return 1;
  1274. //---- if this town is linked to a base town, also don't drop it ----//
  1275. Town* townPtr;
  1276. for( int i=linked_town_count-1 ; i>=0 ; i-- )
  1277. {
  1278. townPtr = town_array[ linked_town_array[i] ];
  1279. if( townPtr->nation_recno == nation_recno &&
  1280. townPtr->is_base_town )
  1281. {
  1282. return 1;
  1283. }
  1284. }
  1285. /*
  1286. //---- if there is not a single town meeting the above criteria, then set the town with the largest population to be the base town. ----//
  1287. if( ownNation->ai_base_town_count <= 1 )
  1288. {
  1289. for( i=ownNation->ai_town_count-1 ; i>=0 ; i-- )
  1290. {
  1291. townPtr = town_array[ ownNation->ai_town_array[i] ];
  1292. if( townPtr->population > population ) // if this town is not the largest town return 0.
  1293. return 0;
  1294. }
  1295. return 1; // return 1 if this town is the largest town.
  1296. }
  1297. */
  1298. return 0;
  1299. }
  1300. //-------- End of function Town::new_base_town_status --------//
  1301. //-------- Begin of function Town::think_scout --------//
  1302. //
  1303. int Town::think_scout()
  1304. {
  1305. Nation* ownNation = nation_array[nation_recno];
  1306. if( config.explore_whole_map )
  1307. return 0;
  1308. if( info.game_date > info.game_start_date + 50 + ownNation->pref_scout ) // only in the first half year of the game
  1309. return 0;
  1310. if( ownNation->ai_town_count > 1 ) // only when there is only one town
  1311. return 0;
  1312. //-------------------------------------------//
  1313. int destX, destY;
  1314. if( m.random(2)==0 )
  1315. destX = center_x + 50 + m.random(50);
  1316. else
  1317. destX = center_x - 50 - m.random(50);
  1318. if( m.random(2)==0 )
  1319. destY = center_y + 50 + m.random(50);
  1320. else
  1321. destY = center_y - 50 - m.random(50);
  1322. destX = max(0, destX);
  1323. destX = min(MAX_WORLD_X_LOC-1, destX);
  1324. destY = max(0, destY);
  1325. destY = min(MAX_WORLD_Y_LOC-1, destY);
  1326. ownNation->add_action( destX, destY, loc_x1, loc_y1, ACTION_AI_SCOUT, 0);
  1327. return 1;
  1328. }
  1329. //-------- End of function Town::think_scout --------//