OF_MARK2.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  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 : OF_MARK2.CPP
  21. //Description : Firm Market Place - AI functions
  22. #include <OINFO.h>
  23. #include <ORAWRES.h>
  24. #include <ORACERES.h>
  25. #include <OTOWN.h>
  26. #include <OGAME.h>
  27. #include <ONATION.h>
  28. #include <OU_CARA.h>
  29. #include <OWORLD.h>
  30. #include <OSYS.h>
  31. #include <OF_FACT.h>
  32. #include <OF_MINE.h>
  33. #include <OF_MARK.h>
  34. //------- Begin of function FirmMarket::process_ai -----------//
  35. //
  36. void FirmMarket::process_ai()
  37. {
  38. //---- think about deleting this firm ----//
  39. if( info.game_date%30==firm_recno%30 )
  40. {
  41. if( think_del() )
  42. return;
  43. }
  44. //----- think about demand trade treaty -----//
  45. if( info.game_date%30==firm_recno%30 )
  46. think_demand_trade_treaty();
  47. //----- think about building a factory next to this market ----//
  48. if( info.game_date%60==firm_recno%60 )
  49. think_market_build_factory();
  50. //-------- think about new trading routes --------//
  51. if( info.game_date < last_import_new_goods_date+60 ) // don't new imports until it's 60 days after the last one was imported
  52. return;
  53. if( can_hire_caravan() )
  54. {
  55. Nation* ownNation = nation_array[nation_recno];
  56. int thinkInterval = 10 + (100-ownNation->pref_trading_tendency)/5; // think once every 10 to 30 days
  57. if( is_market_linked_to_town() )
  58. {
  59. if( info.game_date%thinkInterval==firm_recno%thinkInterval )
  60. think_import_new_product();
  61. if( info.game_date%60 == (firm_recno+20)%60 )
  62. {
  63. //------------------------------------------------------//
  64. // Don't think about increaseing existing product supply
  65. // if we have just import a new goods, it takes time
  66. // to transport and pile up goods.
  67. //------------------------------------------------------//
  68. if( !last_import_new_goods_date ||
  69. info.game_date > last_import_new_goods_date+180 ) // only think increase existing supply 180 days after importing a new one
  70. {
  71. think_increase_existing_product_supply();
  72. }
  73. }
  74. }
  75. if( info.game_date%thinkInterval == firm_recno%thinkInterval )
  76. think_export_product();
  77. }
  78. }
  79. //--------- End of function FirmMarket::process_ai -----------//
  80. //------- Begin of function FirmMarket::think_market_build_factory ------//
  81. //
  82. void FirmMarket::think_market_build_factory()
  83. {
  84. if( no_neighbor_space ) // if there is no space in the neighbor area for building a new firm.
  85. return;
  86. //---- think about building factories to manufacture goods using raw materials in the market place ---//
  87. MarketGoods* marketGoods = market_goods_array;
  88. Firm* firmPtr;
  89. ai_should_build_factory_count = 2; // always set it to 2, so think_build_factory() will start to build a market as soon as there is a need
  90. for( int i=0 ; i<MAX_MARKET_GOODS ; i++, marketGoods++ )
  91. {
  92. if( !marketGoods->raw_id )
  93. continue;
  94. if( marketGoods->stock_qty < 250 ) // only when the stock is >= 250
  95. continue;
  96. //----- check if the raw materials are from a local mine, if so don't build a factory, we only build a factory to manufacture goods using raw materials from a remote town.
  97. int j;
  98. for( j=0 ; j<linked_firm_count ; j++ )
  99. {
  100. firmPtr = firm_array[ linked_firm_array[j] ];
  101. if( firmPtr->firm_id == FIRM_MINE &&
  102. firmPtr->nation_recno == nation_recno &&
  103. ((FirmMine*)firmPtr)->raw_id == marketGoods->raw_id )
  104. {
  105. break;
  106. }
  107. }
  108. if( j<linked_firm_count ) // if this raw material is from a local mine
  109. continue;
  110. //-------------------------------------------//
  111. if( think_build_factory(marketGoods->raw_id) )
  112. return;
  113. }
  114. }
  115. //-------- End of function FirmMarket::think_market_build_factory ------//
  116. //------- Begin of function FirmMarket::is_market_linked_to_town --------//
  117. //
  118. // Return whether this market is linked (enabled link) to any town.
  119. //
  120. // [int] ownBaseTownOnly - whether only count own base town or not.
  121. // (default:0)
  122. //
  123. int FirmMarket::is_market_linked_to_town(int ownBaseTownOnly)
  124. {
  125. Town* townPtr;
  126. for(int i=0 ; i<linked_town_count ; i++ )
  127. {
  128. if( linked_town_enable_array[i] != LINK_EE )
  129. continue;
  130. if( ownBaseTownOnly )
  131. {
  132. townPtr = town_array[ linked_town_array[i] ];
  133. if( townPtr->nation_recno == nation_recno &&
  134. townPtr->is_base_town )
  135. {
  136. return 1;
  137. }
  138. }
  139. else
  140. {
  141. return 1;
  142. }
  143. }
  144. return 0;
  145. }
  146. //--------- End of function FirmMarket::is_market_linked_to_town --------//
  147. //------ Begin of function FirmMarket::ai_update_link_status ------//
  148. //
  149. void FirmMarket::ai_update_link_status()
  150. {
  151. //---- consider enabling/disabling links to firms ----//
  152. Nation* nationPtr = nation_array[nation_recno];
  153. Firm* firmPtr;
  154. int rc;
  155. int i;
  156. for(i=0; i<linked_firm_count; i++)
  157. {
  158. err_when(!linked_firm_array[i] || firm_array.is_deleted(linked_firm_array[i]));
  159. firmPtr = firm_array[linked_firm_array[i]];
  160. //-------- check product type ----------//
  161. if( is_retail_market )
  162. rc = firmPtr->firm_id == FIRM_FACTORY;
  163. else
  164. rc = firmPtr->firm_id == FIRM_MINE || firmPtr->firm_id == FIRM_FACTORY; // for output raw materials to the factory to manufacture
  165. toggle_firm_link( i+1, rc, COMMAND_AI ); // enable the link
  166. }
  167. //----- always enable links to towns as there is no downsides for selling goods to the villagers ----//
  168. for(i=0; i<linked_town_count; i++)
  169. {
  170. err_when(!linked_town_array[i] || town_array.is_deleted(linked_town_array[i]));
  171. toggle_town_link( i+1, 1, COMMAND_AI ); // enable the link
  172. }
  173. }
  174. //------- End of function FirmMarket::ai_update_link_status --------//
  175. //------- Begin of function FirmMarket::think_del -----------//
  176. //
  177. // Think about deleting this firm.
  178. //
  179. int FirmMarket::think_del()
  180. {
  181. if( linked_town_count > 0 )
  182. {
  183. no_linked_town_since_date = 0; // reset it
  184. return 0;
  185. }
  186. else
  187. {
  188. no_linked_town_since_date = info.game_date;
  189. }
  190. //---- don't delete it if there are still signiciant stockhere ---//
  191. if( stock_value_index() >= 10 )
  192. {
  193. Nation* ownNation = nation_array[nation_recno];
  194. //--- if the market has been sitting idle for too long, delete it ---//
  195. if( info.game_date < no_linked_town_since_date
  196. + 180 + 180 * ownNation->pref_trading_tendency / 100 )
  197. {
  198. return 0;
  199. }
  200. }
  201. //------------------------------------------------//
  202. ai_del_firm();
  203. return 1;
  204. }
  205. //--------- End of function FirmMarket::think_del -----------//
  206. //------- Begin of function FirmMarket::think_import_new_product ------//
  207. //
  208. // Think about importing goods to sell in this market place.
  209. //
  210. int FirmMarket::think_import_new_product()
  211. {
  212. //---- check if the market place has free space for new supply ----//
  213. int i, j, emptySlot=0;
  214. MarketGoods *marketGoods = market_goods_array;
  215. for( i=0 ; i<MAX_MARKET_GOODS ; i++, marketGoods++ )
  216. {
  217. if( !marketGoods->product_raw_id && !marketGoods->raw_id )
  218. emptySlot++;
  219. }
  220. if( emptySlot==0 )
  221. return 0;
  222. //--- update what products are needed for this market place ---//
  223. Town* townPtr;
  224. short needProductSupplyPop[MAX_PRODUCT]; // the total population in the towns linked to the market that needs the supply of the product
  225. Nation* nationPtr = nation_array[nation_recno];
  226. memset( needProductSupplyPop, 0, sizeof(needProductSupplyPop) );
  227. for( i=0; i<linked_town_count; i++ )
  228. {
  229. err_when(!linked_town_array[i] || town_array.is_deleted(linked_town_array[i]));
  230. if( linked_town_enable_array[i] != LINK_EE )
  231. continue;
  232. townPtr = town_array[linked_town_array[i]];
  233. if( townPtr->region_id != region_id )
  234. continue;
  235. if( !townPtr->is_base_town ) // don't import if it isn't a base town
  236. continue;
  237. //------------------------------------------------//
  238. //
  239. // Only if the population of the town is equal or
  240. // larger than minTradePop, the AI will try to do trade.
  241. // The minTradePop is between 10 to 20 depending on the
  242. // pref_trading_tendency.
  243. //
  244. //------------------------------------------------//
  245. townPtr->update_product_supply();
  246. for( j=0 ; j<MAX_PRODUCT ; j++ )
  247. {
  248. if( !townPtr->has_product_supply[j] )
  249. needProductSupplyPop[j] += townPtr->population;
  250. }
  251. }
  252. //---- think about importing the products that need supply ----//
  253. int minTradePop = 10;
  254. if( is_retail_market )
  255. {
  256. for( int productId=1 ; productId<=MAX_PRODUCT ; productId++ )
  257. {
  258. if( needProductSupplyPop[productId-1] >= minTradePop || emptySlot==MAX_MARKET_GOODS ) // if market is empty, try to import some goods
  259. {
  260. if( think_import_specific_product(productId) )
  261. {
  262. last_import_new_goods_date = info.game_date;
  263. return 1;
  264. }
  265. }
  266. }
  267. }
  268. //----------------------------------------------------------//
  269. // Think about importing the raw materials of the needed
  270. // products and build factories to manufacture them ourselves
  271. //----------------------------------------------------------//
  272. //--- first check if we can build a new factory to manufacture the products ---//
  273. if( !is_retail_market && is_market_linked_to_town(1) ) // 1-only count towns that are our own and are base towns
  274. {
  275. if( !no_neighbor_space &&
  276. nationPtr->total_jobless_population >= MAX_WORKER*2 &&
  277. can_hire_caravan() >= 2 ) // if there is a shortage of caravan supplies, use it for transporting finished products instead of raw materials
  278. {
  279. if( nationPtr->can_ai_build(FIRM_FACTORY) )
  280. {
  281. for( int productId=1 ; productId<=MAX_PRODUCT ; productId++ )
  282. {
  283. if( needProductSupplyPop[productId-1] >= minTradePop )
  284. {
  285. if( think_mft_specific_product(productId) )
  286. {
  287. last_import_new_goods_date = info.game_date;
  288. return 1;
  289. }
  290. }
  291. }
  292. }
  293. }
  294. }
  295. return 0;
  296. }
  297. //--------- End of function FirmMarket::think_import_new_product -------//
  298. //--- Begin of function FirmMarket::think_increase_existing_product_supply ---//
  299. //
  300. // Think about increasing the supply of existing products.
  301. //
  302. int FirmMarket::think_increase_existing_product_supply()
  303. {
  304. MarketGoods *marketGoods = market_goods_array;
  305. for( int i=0 ; i<MAX_MARKET_GOODS ; i++, marketGoods++ )
  306. {
  307. if( marketGoods->product_raw_id )
  308. {
  309. if( marketGoods->stock_qty < MAX_MARKET_STOCK/10 &&
  310. marketGoods->month_demand * 0.8 > marketGoods->supply_30days() ) // the supply falls behind the demand by at least 20%
  311. {
  312. if( think_import_specific_product(marketGoods->product_raw_id) )
  313. return 1;
  314. }
  315. }
  316. }
  317. return 0;
  318. }
  319. //---- End of function FirmMarket::think_increase_existing_product_supply ---//
  320. //--- Begin of function FirmMarket::think_import_specific_product ---//
  321. //
  322. // Think about importing a specific product.
  323. //
  324. int FirmMarket::think_import_specific_product(int productId)
  325. {
  326. int i, firmRecno;
  327. Firm* firmPtr, *bestFirmPtr=NULL;
  328. Nation* nationPtr = nation_array[nation_recno];
  329. int stockLevel, curRating, bestRating=0;
  330. int canHireCaravan = can_hire_caravan();
  331. RawInfo* rawInfo = raw_res[productId];
  332. for( i=rawInfo->product_supply_firm_array.size() ; i>0 ; i-- )
  333. {
  334. firmRecno = rawInfo->get_product_supply_firm(i);
  335. if( firm_array.is_deleted(firmRecno) || firmRecno==firm_recno )
  336. continue;
  337. //-- if there is already a caravan travelling between two points --//
  338. firmPtr = firm_array[firmRecno];
  339. if( firmPtr->region_id != region_id )
  340. continue;
  341. //-----------------------------------------//
  342. // The rating of a supply is determined by:
  343. // - distance
  344. // - supply
  345. // - nation relationship
  346. //-----------------------------------------//
  347. //------ determine the stock level of this supply ------//
  348. stockLevel = 0;
  349. //---- think about inputing goods from this factory ----//
  350. if( firmPtr->firm_id == FIRM_FACTORY )
  351. {
  352. if( firmPtr->nation_recno != nation_recno ) // can import goods from own factories only
  353. continue;
  354. FirmFactory* firmFactory = (FirmFactory*) firmPtr;
  355. if( firmFactory->product_raw_id == productId )
  356. stockLevel = 100 * (int) firmFactory->stock_qty / (int) firmFactory->max_stock_qty;
  357. }
  358. //---- think about inputing goods from this market ----//
  359. else if( firmPtr->firm_id == FIRM_MARKET )
  360. {
  361. //--- if this is a foreign sale market, don't import from it (e.g. Nation A's market built near Nation B's village -----//
  362. if( firmPtr->nation_recno != nation_recno ) // if this is not our market
  363. {
  364. int j;
  365. for( j=firmPtr->linked_town_count-1 ; j>=0 ; j-- )
  366. {
  367. Town* townPtr = town_array[ firmPtr->linked_town_array[j] ];
  368. if( townPtr->nation_recno == firmPtr->nation_recno )
  369. break;
  370. }
  371. if( j<0 ) // if this market is not linked to its own town (then it must be a foreign market)
  372. continue;
  373. }
  374. //-- only either from own market place or from nations that trade with you --//
  375. if( nation_array[firmPtr->nation_recno]->get_relation(nation_recno)->trade_treaty == 0 )
  376. continue;
  377. MarketGoods* marketGoods = ((FirmMarket*)firmPtr)->market_product_array[productId-1];
  378. //--- if this market has the supply of this goods ----//
  379. if( marketGoods && marketGoods->supply_30days() > 0 )
  380. {
  381. stockLevel = 100 * (int) marketGoods->stock_qty / MAX_MARKET_STOCK;
  382. }
  383. }
  384. //----------------------------------------------//
  385. if( firmPtr->nation_recno == nation_recno )
  386. {
  387. if( stockLevel < 10 ) // for our own market, the stock level requirement is lower
  388. continue;
  389. curRating = 50;
  390. }
  391. else
  392. {
  393. if( stockLevel < 20 ) // for other player's market, only import when the stock level is high enough
  394. continue;
  395. curRating = nationPtr->get_relation_status(firmPtr->nation_recno) * 5;
  396. }
  397. //---- calculate the current overall rating ----//
  398. curRating += stockLevel/2 + world.distance_rating(center_x, center_y, firmPtr->center_x, firmPtr->center_y);
  399. //----------- compare ratings -------------//
  400. if( curRating > bestRating )
  401. {
  402. bestRating = curRating;
  403. bestFirmPtr = firmPtr;
  404. }
  405. }
  406. //---- if a suitable supplier is found -----//
  407. if( bestFirmPtr )
  408. return ai_create_new_trade(bestFirmPtr, 0, PICK_UP_PRODUCT_FIRST+productId-1);
  409. return 0;
  410. }
  411. //---- End of function FirmMarket::think_import_specific_product ----//
  412. //------- Begin of function FirmMarket::think_export_product -----------//
  413. //
  414. // Think about exporting products from this market to another market.
  415. //
  416. int FirmMarket::think_export_product()
  417. {
  418. //--- first check if there is any excessive supply for export ---//
  419. int exportProductId = 0;
  420. MarketGoods *marketGoods = market_goods_array;
  421. for( int i=0 ; i<MAX_MARKET_GOODS ; i++, marketGoods++ )
  422. {
  423. if( marketGoods->product_raw_id )
  424. {
  425. if( marketGoods->stock_qty > MAX_MARKET_STOCK * 3 / 4 &&
  426. marketGoods->month_demand < marketGoods->supply_30days() / 2 ) // the supply is at least double of the demand
  427. {
  428. exportProductId = marketGoods->product_raw_id;
  429. break;
  430. }
  431. }
  432. }
  433. if( !exportProductId )
  434. return 0;
  435. //----- locate for towns that do not have the supply of the product ----//
  436. Town* townPtr;
  437. Nation* nationPtr = nation_array[nation_recno];
  438. for( int townRecno=town_array.size() ; townRecno>0 ; townRecno-- )
  439. {
  440. if( town_array.is_deleted(townRecno) )
  441. continue;
  442. townPtr = town_array[townRecno];
  443. if( townPtr->population < 20 - (10*nationPtr->pref_trading_tendency/100) ) // 10 to 20 as the minimum population for considering trade
  444. continue;
  445. if( townPtr->has_product_supply[exportProductId-1] ) // if the town already has the supply of product, return now
  446. continue;
  447. if( townPtr->region_id != region_id )
  448. continue;
  449. if( townPtr->no_neighbor_space ) // if there is no space in the neighbor area for building a new firm.
  450. continue;
  451. if( m.points_distance( center_x, center_y, townPtr->center_x, center_y ) > MAX_WORLD_X_LOC/4 ) // don't consider if it is too far away
  452. continue;
  453. if( townPtr->town_recno &&
  454. nationPtr->get_relation_status(townPtr->town_recno) < NATION_FRIENDLY ) // only build markets to friendly nation's town
  455. {
  456. continue;
  457. }
  458. //-----------------------------------------//
  459. if( townPtr->nation_recno )
  460. {
  461. //--- if it's a nation town, only export if we have trade treaty with it ---//
  462. if( !nationPtr->get_relation(townPtr->nation_recno)->trade_treaty )
  463. continue;
  464. }
  465. else
  466. {
  467. //--- if it's an independent town, only export if the resistance is low ---//
  468. if( townPtr->average_resistance(nation_recno) > INDEPENDENT_LINK_RESISTANCE )
  469. continue;
  470. }
  471. //----- think about building a new market to the town for exporting our goods -----//
  472. if( think_build_export_market(townRecno) )
  473. return 1;
  474. }
  475. return 0;
  476. }
  477. //--------- End of function FirmMarket::think_export_product -----------//
  478. //------- Begin of function FirmMarket::think_build_export_market -----------//
  479. //
  480. // Think about export goods of this market to other markets.
  481. //
  482. int FirmMarket::think_build_export_market(int townRecno)
  483. {
  484. Town* townPtr = town_array[townRecno];
  485. Firm* firmPtr;
  486. Nation* nationPtr = nation_array[nation_recno];
  487. //---- see if we already have a market linked to this town ----//
  488. for( int i=0 ; i<townPtr->linked_firm_count ; i++ )
  489. {
  490. firmPtr = firm_array[ townPtr->linked_firm_array[i] ];
  491. if( firmPtr->firm_id != FIRM_MARKET || firmPtr->firm_recno==firm_recno )
  492. continue;
  493. //--- if we already have a market there, no need to build a new market ----//
  494. if( firmPtr->nation_recno == nation_recno )
  495. return 0;
  496. }
  497. //--- if there is no market place linked to this town, we can set up one ourselves ---//
  498. short buildXLoc, buildYLoc;
  499. if( !nationPtr->find_best_firm_loc(FIRM_MARKET, townPtr->loc_x1, townPtr->loc_y1, buildXLoc, buildYLoc) )
  500. {
  501. townPtr->no_neighbor_space = 1;
  502. return 0;
  503. }
  504. nationPtr->add_action(buildXLoc, buildYLoc, townPtr->loc_x1, townPtr->loc_y1, ACTION_AI_BUILD_FIRM, FIRM_MARKET);
  505. return 1;
  506. }
  507. //--------- End of function FirmMarket::think_build_export_market -----------//
  508. //--- Begin of function FirmMarket::think_mft_specific_product ---//
  509. //
  510. // Think about importing a specific type of raw material and build a
  511. // factory to manufacture ourselves.
  512. //
  513. // <int> rawId - id. of the raw material.
  514. //
  515. int FirmMarket::think_mft_specific_product(int rawId)
  516. {
  517. int i, j, firmRecno;
  518. Firm* firmPtr, *bestFirmPtr=NULL;
  519. Nation* nationPtr = nation_array[nation_recno];
  520. int stockLevel, curRating, bestRating=0;
  521. RawInfo* rawInfo = raw_res[rawId];
  522. for( i=rawInfo->raw_supply_firm_array.size() ; i>0 ; i-- )
  523. {
  524. firmRecno = rawInfo->get_raw_supply_firm(i);
  525. if( firm_array.is_deleted(firmRecno) || firmRecno == firm_recno )
  526. continue;
  527. //-- if there is already a caravan travelling between two points --//
  528. firmPtr = firm_array[firmRecno];
  529. if( firmPtr->region_id != region_id )
  530. continue;
  531. //-- if this is our own supply, don't import the raw material, but import the finished goods instead. --//
  532. if( firmPtr->nation_recno == nation_recno )
  533. continue;
  534. //-----------------------------------------//
  535. // The rating of a supply is determined by:
  536. // - distance
  537. // - supply
  538. // - nation relationship
  539. //-----------------------------------------//
  540. //------ determine the stock level of this supply ------//
  541. stockLevel = 0;
  542. if( firmPtr->firm_id == FIRM_MARKET )
  543. {
  544. //-- only either from own market place or from nations that trade with you --//
  545. if( nation_array[firmPtr->nation_recno]->get_relation(nation_recno)->trade_treaty == 0 )
  546. continue;
  547. //----- check if this market is linked to any mines directly ----//
  548. for( j=firmPtr->linked_firm_count-1 ; j>=0 ; j-- )
  549. {
  550. Firm* linkedFirm = firm_array[ firmPtr->linked_firm_array[j] ];
  551. if( linkedFirm->firm_id == FIRM_MINE && linkedFirm->nation_recno == firmPtr->nation_recno )
  552. {
  553. if( ((FirmMine*)linkedFirm)->raw_id == rawId )
  554. break;
  555. }
  556. }
  557. if( j<0 ) // this market does not have any direct supplies, so don't pick up goods from it
  558. continue;
  559. //---------------------------------------------------------------//
  560. MarketGoods* marketGoods = ((FirmMarket*)firmPtr)->market_goods_array;
  561. for( j=0 ; j<MAX_MARKET_GOODS ; j++, marketGoods++ )
  562. {
  563. if( marketGoods->stock_qty > MAX_MARKET_STOCK / 5 )
  564. {
  565. if( marketGoods->raw_id == rawId )
  566. stockLevel = 100 * (int) marketGoods->stock_qty / MAX_MARKET_STOCK;
  567. }
  568. }
  569. }
  570. if( stockLevel < 50 ) // if the stock is too low, don't consider it
  571. continue;
  572. //---- calculate the current overall rating ----//
  573. NationRelation* nationRelation = nationPtr->get_relation(firmPtr->nation_recno);
  574. curRating = stockLevel
  575. - 100 * m.points_distance( center_x, center_y,
  576. firmPtr->center_x, firmPtr->center_y ) / MAX_WORLD_X_LOC;
  577. if( firmPtr->nation_recno == nation_recno )
  578. curRating += 100;
  579. else
  580. curRating += nationRelation->status * 20;
  581. //----------- compare ratings -------------//
  582. if( curRating > bestRating )
  583. {
  584. bestRating = curRating;
  585. bestFirmPtr = firmPtr;
  586. }
  587. }
  588. if( !bestFirmPtr )
  589. return 0;
  590. if( !ai_create_new_trade(bestFirmPtr, 0, PICK_UP_RAW_FIRST+rawId-1) )
  591. return 0;
  592. return 1;
  593. }
  594. //---- End of function FirmMarket::think_mft_specific_product ----//
  595. //------- Begin of function FirmMarket::think_demand_trade_treaty ------//
  596. //
  597. void FirmMarket::think_demand_trade_treaty()
  598. {
  599. Nation* nationPtr = nation_array[nation_recno];
  600. int nationRecno;
  601. //----- demand towns to open up market ----//
  602. for( int i=0 ; i<linked_town_count ; i++ )
  603. {
  604. //----- if the link is not enabled -----//
  605. if( linked_town_enable_array[i] != LINK_EE )
  606. {
  607. nationRecno = town_array[ linked_town_array[i] ]->nation_recno;
  608. if( nationRecno )
  609. nationPtr->get_relation(nationRecno)->ai_demand_trade_treaty++;
  610. }
  611. }
  612. }
  613. //-------- End of function FirmMarket::think_demand_trade_treaty -------//
  614. //------- Begin of function FirmMarket::ai_create_new_trade ------//
  615. //
  616. int FirmMarket::ai_create_new_trade(Firm* firmPtr, int stop1PickUpType, int stop2PickUpType)
  617. {
  618. //---- see if there is already a caravan moving along the route -----//
  619. Nation* ownNation = nation_array[nation_recno];
  620. UnitCaravan* unitCaravan;
  621. int rc, stop1Id, stop2Id;
  622. int caravanInRouteCount=0;
  623. for( int i=ownNation->ai_caravan_count-1 ; i>=0 ; i-- )
  624. {
  625. unitCaravan = (UnitCaravan*) unit_array[ ownNation->ai_caravan_array[i] ];
  626. err_when( unitCaravan->nation_recno != nation_recno );
  627. err_when( unitCaravan->unit_id != UNIT_CARAVAN );
  628. if( unitCaravan->stop_defined_num < 2 )
  629. continue;
  630. if( unitCaravan->stop_array[0].firm_recno == firm_recno &&
  631. unitCaravan->stop_array[1].firm_recno == firmPtr->firm_recno )
  632. {
  633. stop1Id = 1;
  634. stop2Id = 2;
  635. }
  636. else if( unitCaravan->stop_array[1].firm_recno == firm_recno &&
  637. unitCaravan->stop_array[0].firm_recno == firmPtr->firm_recno )
  638. {
  639. stop1Id = 2;
  640. stop2Id = 1;
  641. }
  642. else
  643. {
  644. continue;
  645. }
  646. //------- add the goods to the pick up list ----//
  647. rc = 0;
  648. if( stop1PickUpType && !unitCaravan->has_pick_up_type(stop1Id, stop1PickUpType) )
  649. {
  650. if( unitCaravan->is_visible() ) // can't set stop when the caravan is in a firm
  651. unitCaravan->set_stop_pick_up(stop1Id, stop1PickUpType, COMMAND_AI);
  652. rc = 1;
  653. }
  654. if( stop2PickUpType && !unitCaravan->has_pick_up_type(stop2Id, stop2PickUpType) )
  655. {
  656. if( unitCaravan->is_visible() ) // can't set stop when the caravan is in a firm
  657. unitCaravan->set_stop_pick_up(stop2Id, stop2PickUpType, COMMAND_AI);
  658. rc = 1;
  659. }
  660. if( rc ) // don't add one if we can utilize an existing one.
  661. return 1;
  662. caravanInRouteCount++;
  663. }
  664. if( caravanInRouteCount >= 2 ) // don't have more than 2 caravans on a single route
  665. return 0;
  666. //----------- hire a new caravan -----------//
  667. int unitRecno = hire_caravan(COMMAND_AI);
  668. if( !unitRecno )
  669. return 0;
  670. //----------- set up the trade route ----------//
  671. unitCaravan = (UnitCaravan*) unit_array[unitRecno];
  672. unitCaravan->set_stop(2, firmPtr->loc_x1, firmPtr->loc_y1, COMMAND_AI);
  673. err_when( unitCaravan->stop_array[0].firm_recno == firmPtr->firm_recno ); // cannot set both stops to the same firm
  674. unitCaravan->set_stop_pick_up(1, NO_PICK_UP, COMMAND_AI);
  675. unitCaravan->set_stop_pick_up(2, NO_PICK_UP, COMMAND_AI);
  676. if( stop1PickUpType )
  677. unitCaravan->set_stop_pick_up(1, stop1PickUpType, COMMAND_AI);
  678. if( stop2PickUpType )
  679. unitCaravan->set_stop_pick_up(2, stop2PickUpType, COMMAND_AI);
  680. return 1;
  681. }
  682. //-------- End of function FirmMarket::ai_create_new_trade -------//