Remove hard limitation that AI wonder cities never build settlers
[freeciv.git] / ai / default / daidomestic.c
blob30ebaaf744214a3b214ebc797b1fd25bd88924c3
1 /***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 ***********************************************************************/
14 #ifdef HAVE_CONFIG_H
15 #include <fc_config.h>
16 #endif
18 #include <string.h>
19 #include <math.h> /* pow */
21 /* utility */
22 #include "log.h"
23 #include "mem.h"
25 /* common */
26 #include "city.h"
27 #include "game.h"
28 #include "government.h"
29 #include "map.h"
30 #include "movement.h"
31 #include "traderoutes.h"
32 #include "unit.h"
33 #include "unitlist.h"
34 #include "unittype.h"
36 /* common/aicore */
37 #include "pf_tools.h"
39 /* server */
40 #include "citytools.h"
41 #include "unittools.h"
42 #include "srv_log.h"
44 /* server/advisors */
45 #include "advbuilding.h"
46 #include "advchoice.h"
47 #include "advdata.h"
48 #include "autosettlers.h"
49 #include "infracache.h" /* adv_city */
51 /* ai */
52 #include "aitraits.h"
53 #include "handicaps.h"
55 /* ai/default */
56 #include "aicity.h"
57 #include "aidata.h"
58 #include "ailog.h"
59 #include "aiplayer.h"
60 #include "aitech.h"
61 #include "aitools.h"
62 #include "aiunit.h"
63 #include "daimilitary.h"
65 #include "daidomestic.h"
68 /***************************************************************************
69 Evaluate the need for units (like caravans) that aid wonder construction.
70 If another city is building wonder and needs help but pplayer is not
71 advanced enough to build caravans, the corresponding tech will be
72 stimulated.
73 ****************************************************************************/
74 static void dai_choose_help_wonder(struct ai_type *ait,
75 struct city *pcity,
76 struct adv_choice *choice,
77 struct adv_data *ai)
79 struct player *pplayer = city_owner(pcity);
80 Continent_id continent = tile_continent(pcity->tile);
81 struct ai_city *city_data = def_ai_city_data(pcity, ait);
82 /* Total count of caravans available or already being built
83 * on this continent */
84 int caravans = 0;
85 /* The type of the caravan */
86 struct unit_type *unit_type;
87 struct city *wonder_city = game_city_by_number(ai->wonder_city);
89 if (num_role_units(action_get_role(ACTION_HELP_WONDER)) == 0) {
90 /* No such units available in the ruleset */
91 return;
94 if (pcity == wonder_city
95 || wonder_city == NULL
96 || city_data->distance_to_wonder_city <= 0
97 || VUT_UTYPE == wonder_city->production.kind
98 || !is_wonder(wonder_city->production.value.building)) {
99 /* A distance of zero indicates we are very far away, possibly
100 * on another continent. */
101 return;
104 /* Count existing caravans */
105 unit_list_iterate(pplayer->units, punit) {
106 if (unit_can_do_action(punit, ACTION_HELP_WONDER)
107 && tile_continent(unit_tile(punit)) == continent)
108 caravans++;
109 } unit_list_iterate_end;
111 /* Count caravans being built */
112 city_list_iterate(pplayer->cities, acity) {
113 if (VUT_UTYPE == acity->production.kind
114 && utype_can_do_action(acity->production.value.utype,
115 ACTION_HELP_WONDER)
116 && tile_continent(acity->tile) == continent) {
117 caravans++;
119 } city_list_iterate_end;
121 unit_type = best_role_unit(pcity, action_get_role(ACTION_HELP_WONDER));
123 if (!unit_type) {
124 /* We cannot build such units yet
125 * but we will consider it to stimulate science */
126 unit_type = get_role_unit(action_get_role(ACTION_HELP_WONDER), 0);
129 fc_assert_msg(utype_can_do_action(unit_type, ACTION_HELP_WONDER),
130 "Non existence of wonder helper unit not caught");
132 /* Check if wonder needs a little help. */
133 if (build_points_left(wonder_city)
134 > utype_build_shield_cost(unit_type) * caravans) {
135 struct impr_type *wonder = wonder_city->production.value.building;
136 int want = wonder_city->server.adv->building_want[improvement_index(wonder)];
137 int dist = city_data->distance_to_wonder_city /
138 unit_type->move_rate;
140 fc_assert_ret(VUT_IMPROVEMENT == wonder_city->production.kind);
142 want /= MAX(dist, 1);
143 CITY_LOG(LOG_DEBUG, pcity, "want %s to help wonder in %s with %d",
144 utype_rule_name(unit_type),
145 city_name_get(wonder_city),
146 want);
147 if (want > choice->want) {
148 /* This sets our tech want in cases where we cannot actually build
149 * the unit. */
150 unit_type = dai_wants_role_unit(ait, pplayer, pcity,
151 action_get_role(ACTION_HELP_WONDER),
152 want);
153 if (unit_type != NULL) {
154 choice->want = want;
155 choice->type = CT_CIVILIAN;
156 choice->value.utype = unit_type;
157 } else {
158 CITY_LOG(LOG_DEBUG, pcity,
159 "would but could not build ACTION_HELP_WONDER unit, "
160 "bumped reqs");
166 /***************************************************************************
167 Evaluate the need for units (like caravans) that create trade routes.
168 If pplayer is not advanced enough to build caravans, the corresponding
169 tech will be stimulated.
170 ****************************************************************************/
171 static void dai_choose_trade_route(struct ai_type *ait, struct city *pcity,
172 struct adv_choice *choice,
173 struct adv_data *ai)
175 struct player *pplayer = city_owner(pcity);
176 struct unit_type *unit_type;
177 int want;
178 int income, bonus;
179 int trade_routes;
180 int caravan_units;
181 int unassigned_caravans;
182 int max_routes;
183 Continent_id continent = tile_continent(pcity->tile);
184 bool dest_city_found = FALSE;
185 bool dest_city_nat_different_cont = FALSE;
186 bool dest_city_nat_same_cont = FALSE;
187 bool dest_city_in_different_cont = FALSE;
188 bool dest_city_in_same_cont = FALSE;
189 bool prefer_different_cont;
190 int pct = 0;
191 int trader_trait;
192 bool need_boat = FALSE;
194 if (city_list_size(pplayer->cities) < 5) {
195 /* Consider trade routes only if enough destination cities.
196 * This is just a quick check. We make more detailed check below. */
197 return;
200 if (num_role_units(action_get_role(ACTION_TRADE_ROUTE)) == 0
201 && num_role_units(action_get_role(ACTION_MARKETPLACE)) == 0) {
202 /* No such units available in the ruleset */
203 return;
206 if (trade_route_type_trade_pct(TRT_NATIONAL_IC) >
207 trade_route_type_trade_pct(TRT_NATIONAL)) {
208 prefer_different_cont = TRUE;
209 } else {
210 prefer_different_cont = FALSE;
213 /* Look for proper destination city for trade. */
214 if (trade_route_type_trade_pct(TRT_NATIONAL) > 0
215 || trade_route_type_trade_pct(TRT_NATIONAL_IC) > 0) {
216 /* National traderoutes have value */
217 city_list_iterate(pplayer->cities, acity) {
218 if (can_cities_trade(pcity, acity)) {
219 dest_city_found = TRUE;
220 if (tile_continent(acity->tile) != continent) {
221 dest_city_nat_different_cont = TRUE;
222 if (prefer_different_cont) {
223 break;
225 } else {
226 dest_city_nat_same_cont = TRUE;
227 if (!prefer_different_cont) {
228 break;
232 } city_list_iterate_end;
235 /* FIXME: This check should consider more about relative
236 * income from different traderoute types. This works just
237 * with more typical ruleset setups. */
238 if (prefer_different_cont && !dest_city_nat_different_cont) {
239 if (trade_route_type_trade_pct(TRT_IN_IC) >
240 trade_route_type_trade_pct(TRT_IN)) {
241 prefer_different_cont = TRUE;
242 } else {
243 prefer_different_cont = FALSE;
246 players_iterate(aplayer) {
247 if (aplayer == pplayer || !aplayer->is_alive) {
248 continue;
250 if (pplayers_allied(pplayer, aplayer)) {
251 city_list_iterate(aplayer->cities, acity) {
252 if (can_cities_trade(pcity, acity)) {
253 dest_city_found = TRUE;
254 if (tile_continent(acity->tile) != continent) {
255 dest_city_in_different_cont = TRUE;
256 if (prefer_different_cont) {
257 break;
259 } else {
260 dest_city_in_same_cont = TRUE;
261 if (!prefer_different_cont) {
262 break;
266 } city_list_iterate_end;
268 if ((dest_city_in_different_cont && prefer_different_cont)
269 || (dest_city_in_same_cont && !prefer_different_cont)) {
270 break;
272 } players_iterate_end;
275 if (!dest_city_found) {
276 /* No proper destination city. */
277 return;
280 unit_type = best_role_unit(pcity, action_get_role(ACTION_TRADE_ROUTE));
282 if (!unit_type) {
283 /* Can't establish trade route yet. What about entering a marketplace? */
284 /* TODO: Should a future unit capable of establishing trade routes be
285 * prioritized above a present unit capable of entering a market place?
286 * In that case this should be below the check for a future unit
287 * capable of establishing a trade route. */
288 unit_type = best_role_unit(pcity, action_get_role(ACTION_MARKETPLACE));
291 if (!unit_type) {
292 /* We cannot build such units yet
293 * but we will consider it to stimulate science */
294 unit_type = get_role_unit(action_get_role(ACTION_TRADE_ROUTE), 0);
297 if (!unit_type) {
298 /* We'll never be able to establish a trade route. Consider a unit that
299 * can enter the marketplace in stead to stimulate science. */
300 unit_type = get_role_unit(action_get_role(ACTION_MARKETPLACE), 0);
303 fc_assert_msg(unit_type,
304 "Non existance of trade unit not caught");
306 trade_routes = city_num_trade_routes(pcity);
307 /* Count also caravans enroute to establish traderoutes */
308 caravan_units = 0;
309 unit_list_iterate(pcity->units_supported, punit) {
310 if (unit_can_do_action(punit, ACTION_TRADE_ROUTE)) {
311 caravan_units++;
313 } unit_list_iterate_end;
315 max_routes = max_trade_routes(pcity);
316 unassigned_caravans = caravan_units - (max_routes - trade_routes);
317 trade_routes += caravan_units;
319 /* We consider only initial benefit from establishing trade route.
320 * We may actually get only initial benefit if both cities already
321 * have four trade routes, if there already is route between them
322 * or if the Establish Trade Route action is illegal. */
324 /* The calculations of get_caravan_enter_city_trade_bonus() have to be
325 * duplicated here because the city traded with is imaginary. */
327 /* We assume that we are creating trade route to city with 75% of
328 * pcitys trade 10 squares away. */
329 income = (10 + 10) * (1.75 * pcity->surplus[O_TRADE]) / 24 * 3;
330 bonus = get_city_bonus(pcity, EFT_TRADE_REVENUE_BONUS);
331 income = (float)income * pow(2.0, (double)bonus / 1000.0);
333 if (!utype_can_do_action(unit_type, ACTION_TRADE_ROUTE)) {
334 /* Enter Marketplace has less initial income. */
335 income = (income + 2) / 3;
338 if (dest_city_nat_same_cont) {
339 pct = trade_route_type_trade_pct(TRT_NATIONAL);
341 if (dest_city_in_same_cont) {
342 int typepct = trade_route_type_trade_pct(TRT_IN);
344 pct = MAX(pct, typepct);
346 if (dest_city_nat_different_cont) {
347 int typepct = trade_route_type_trade_pct(TRT_NATIONAL_IC);
349 if (typepct > pct) {
350 pct = typepct;
351 need_boat = TRUE;
354 if (dest_city_in_different_cont) {
355 int typepct = trade_route_type_trade_pct(TRT_IN_IC);
357 if (typepct > pct) {
358 pct = typepct;
359 need_boat = TRUE;
363 income = pct * income / 100;
365 want = income * ai->gold_priority + income * ai->science_priority;
367 /* We get this income only once after all.
368 * This value is adjusted for most interesting gameplay.
369 * For optimal performance AI should build more caravans, but
370 * we want it to build less valued buildings too. */
371 trader_trait = ai_trait_get_value(TRAIT_TRADER, pplayer);
372 want /= (130 * TRAIT_DEFAULT_VALUE / trader_trait);
374 /* Increase want for trade routes if our economy is very weak.
375 * We may have enough gold, but only because we have already set
376 * tax rate to 100%. So don't check gold directly, but tax rate.
377 * This method helps us out of deadlocks of completely stalled
378 * scientific progress.
380 if (pplayer->economic.science < 50 && trade_routes < max_routes
381 && utype_can_do_action(unit_type, ACTION_TRADE_ROUTE)) {
382 want *=
383 (6 - pplayer->economic.science / 10) * (6 - pplayer->economic.science / 10);
386 if (trade_routes == 0 && max_routes > 0
387 && utype_can_do_action(unit_type, ACTION_TRADE_ROUTE)) {
388 /* If we have no trade routes at all, we are certainly creating a new one. */
389 want += trader_trait;
390 } else if (trade_routes < max_routes
391 && utype_can_do_action(unit_type, ACTION_TRADE_ROUTE)) {
392 /* Possibly creating a new trade route */
393 want += trader_trait / 4;
396 want -= utype_build_shield_cost(unit_type) * SHIELD_WEIGHTING / 150;
398 /* Don't pile too many of them */
399 if (unassigned_caravans * 10 > want && want > 0) {
400 want = 1;
401 } else {
402 want -= unassigned_caravans * 10; /* Don't pile too many of them */
405 CITY_LOG(LOG_DEBUG, pcity,
406 "want for trade route unit is %d (expected initial income %d)",
407 want,
408 income);
410 if (want > choice->want) {
411 /* This sets our tech want in cases where we cannot actually build
412 * the unit. */
413 unit_type = dai_wants_role_unit(ait, pplayer, pcity,
414 action_get_role(ACTION_TRADE_ROUTE),
415 want);
417 if (unit_type == NULL) {
418 unit_type = dai_wants_role_unit(ait, pplayer, pcity,
419 action_get_role(ACTION_MARKETPLACE),
420 want);
423 if (unit_type != NULL) {
424 choice->want = want;
425 choice->type = CT_CIVILIAN;
426 choice->value.utype = unit_type;
427 choice->need_boat = need_boat;
428 } else {
429 CITY_LOG(LOG_DEBUG, pcity,
430 "would but could not build trade route unit, bumped reqs");
435 /**************************************************************************
436 This function should fill the supplied choice structure.
438 If want is 0, this advisor doesn't want anything.
439 ***************************************************************************/
440 void domestic_advisor_choose_build(struct ai_type *ait, struct player *pplayer,
441 struct city *pcity, struct adv_choice *choice)
443 struct adv_data *adv = adv_data_get(pplayer, NULL);
444 /* Unit type with certain role */
445 struct unit_type *settler_type;
446 struct unit_type *founder_type;
447 int settler_want, founder_want;
448 struct ai_city *city_data = def_ai_city_data(pcity, ait);
450 adv_init_choice(choice);
452 /* Find out desire for workers (terrain improvers) */
453 settler_type = dai_role_utype_for_terrain_class(pcity, UTYF_SETTLERS,
454 TC_LAND);
456 /* The worker want is calculated in aicity.c called from
457 * dai_manage_cities. The expand value is the % that the AI should
458 * value expansion (basically to handicap easier difficulty levels)
459 * and is set when the difficulty level is changed (difficulty.c). */
460 settler_want = city_data->settler_want * pplayer->ai_common.expand / 100;
462 if (adv->wonder_city == pcity->id) {
463 if (!settler_type || settler_type->pop_cost > 0) {
464 settler_want /= 5;
465 } else {
466 settler_want /= 2;
470 if (settler_type
471 && pcity->surplus[O_FOOD] > utype_upkeep_cost(settler_type,
472 pplayer, O_FOOD)) {
473 if (settler_want > 0) {
474 CITY_LOG(LOG_DEBUG, pcity, "desires terrain improvers with passion %d",
475 settler_want);
476 dai_choose_role_unit(ait, pplayer, pcity, choice, CT_CIVILIAN,
477 UTYF_SETTLERS, settler_want, FALSE);
479 /* Terrain improvers don't use boats (yet) */
481 } else if (!settler_type && settler_want > 0) {
482 /* Can't build settlers. Lets stimulate science */
483 dai_wants_role_unit(ait, pplayer, pcity, UTYF_SETTLERS, settler_want);
486 /* Find out desire for city founders */
487 /* Basically, copied from above and adjusted. -- jjm */
488 if (!game.scenario.prevent_new_cities) {
489 founder_type = best_role_unit(pcity, UTYF_CITIES);
491 /* founder_want calculated in aisettlers.c */
492 founder_want = city_data->founder_want;
494 if (adv->wonder_city == pcity->id) {
495 if (founder_type->pop_cost > 0) {
496 founder_want /= 5;
497 } else {
498 founder_want /= 2;
502 if (adv->max_num_cities <= city_list_size(pplayer->cities)) {
503 founder_want /= 100;
506 /* Adjust founder want by traits */
507 founder_want *= (double)ai_trait_get_value(TRAIT_EXPANSIONIST, pplayer)
508 / TRAIT_DEFAULT_VALUE;
510 if (founder_type
511 && pcity->surplus[O_FOOD] >= utype_upkeep_cost(founder_type,
512 pplayer, O_FOOD)) {
514 if (founder_want > choice->want) {
515 CITY_LOG(LOG_DEBUG, pcity, "desires founders with passion %d",
516 founder_want);
517 dai_choose_role_unit(ait, pplayer, pcity, choice, CT_CIVILIAN,
518 UTYF_CITIES, founder_want,
519 city_data->founder_boat);
521 } else if (founder_want < -choice->want) {
522 /* We need boats to colonize! */
523 /* We might need boats even if there are boats free,
524 * if they are blockaded or in inland seas. */
525 struct ai_plr *ai = dai_plr_data_get(ait, pplayer, NULL);
527 CITY_LOG(LOG_DEBUG, pcity, "desires founders with passion %d and asks"
528 " for a new boat (%d of %d free)",
529 -founder_want, ai->stats.available_boats, ai->stats.boats);
531 /* First fill choice with founder information */
532 choice->want = 0 - founder_want;
533 choice->type = CT_CIVILIAN;
534 choice->value.utype = founder_type; /* default */
535 choice->need_boat = TRUE;
537 /* Then try to overwrite it with ferryboat information
538 * If no ferryboat is found, above founder choice stays. */
539 dai_choose_role_unit(ait, pplayer, pcity, choice, CT_CIVILIAN,
540 L_FERRYBOAT, -founder_want, TRUE);
542 } else if (!founder_type
543 && (founder_want > choice->want || founder_want < -choice->want)) {
544 /* Can't build founders. Lets stimulate science */
545 dai_wants_role_unit(ait, pplayer, pcity, UTYF_CITIES, founder_want);
550 struct adv_choice cur;
552 adv_init_choice(&cur);
553 /* Consider building caravan-type units to aid wonder construction */
554 dai_choose_help_wonder(ait, pcity, &cur, adv);
555 copy_if_better_choice(&cur, choice);
557 adv_init_choice(&cur);
558 /* Consider city improvements */
559 building_advisor_choose(pcity, &cur);
560 copy_if_better_choice(&cur, choice);
562 adv_init_choice(&cur);
563 /* Consider building caravan-type units for trade route */
564 dai_choose_trade_route(ait, pcity, &cur, adv);
565 copy_if_better_choice(&cur, choice);
568 if (choice->want >= 200) {
569 /* If we don't do following, we buy caravans in city X when we should be
570 * saving money to buy defenses for city Y. -- Syela */
571 choice->want = 199;
574 return;
577 /**************************************************************************
578 Calculate walking distances to wonder city from nearby cities.
579 **************************************************************************/
580 void dai_wonder_city_distance(struct ai_type *ait, struct player *pplayer,
581 struct adv_data *adv)
583 struct pf_map *pfm;
584 struct pf_parameter parameter;
585 struct unit_type *punittype;
586 struct unit *ghost;
587 int maxrange;
588 struct city *wonder_city = game_city_by_number(adv->wonder_city);
590 city_list_iterate(pplayer->cities, acity) {
591 /* Mark unavailable */
592 def_ai_city_data(acity, ait)->distance_to_wonder_city = 0;
593 } city_list_iterate_end;
595 if (wonder_city == NULL) {
596 return;
599 punittype = best_role_unit_for_player(pplayer,
600 action_get_role(ACTION_HELP_WONDER));
602 if (!punittype) {
603 return;
606 fc_assert_msg(utype_can_do_action(punittype, ACTION_HELP_WONDER),
607 "Non existence of wonder helper unit not caught");
609 ghost = unit_virtual_create(pplayer, wonder_city, punittype, 0);
610 maxrange = unit_move_rate(ghost) * 7;
612 pft_fill_unit_parameter(&parameter, ghost);
613 parameter.omniscience = !has_handicap(pplayer, H_MAP);
614 pfm = pf_map_new(&parameter);
616 pf_map_move_costs_iterate(pfm, ptile, move_cost, FALSE) {
617 struct city *acity = tile_city(ptile);
619 if (move_cost > maxrange) {
620 break;
622 if (!acity) {
623 continue;
625 if (city_owner(acity) == pplayer) {
626 def_ai_city_data(acity, ait)->distance_to_wonder_city = move_cost;
628 } pf_map_move_costs_iterate_end;
630 pf_map_destroy(pfm);
631 unit_virtual_destroy(ghost);