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