Split "Establish Embassy".
[freeciv.git] / server / advisors / advdata.c
blob616966d8ed348658eeee06c753000c5d895b6eb2
1 /***********************************************************************
2 Freeciv - Copyright (C) 2002 - The Freeciv Project
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 /* utility */
19 #include "log.h"
20 #include "mem.h"
22 /* common */
23 #include "actions.h"
24 #include "ai.h"
25 #include "city.h"
26 #include "effects.h"
27 #include "game.h"
28 #include "government.h"
29 #include "map.h"
30 #include "movement.h"
31 #include "research.h"
32 #include "unit.h"
33 #include "unitlist.h"
35 /* common/aicore */
36 #include "aisupport.h"
37 #include "path_finding.h"
38 #include "pf_tools.h"
40 /* server */
41 #include "cityturn.h"
42 #include "diplhand.h"
43 #include "maphand.h"
44 #include "plrhand.h"
45 #include "srv_log.h"
46 #include "unittools.h"
48 /* server/advisors */
49 #include "advbuilding.h"
50 #include "advcity.h"
51 #include "advtools.h"
52 #include "autosettlers.h"
54 /* ai */
55 #include "handicaps.h"
57 #include "advdata.h"
59 static void adv_dipl_new(const struct player *plr1,
60 const struct player *plr2);
61 static void adv_dipl_free(const struct player *plr1,
62 const struct player *plr2);
63 static struct adv_dipl *adv_dipl_get(const struct player *plr1,
64 const struct player *plr2);
66 /**************************************************************************
67 Precalculates some important data about the improvements in the game
68 that we use later in ai/aicity.c. We mark improvements as 'calculate'
69 if we want to run a full test on them, as 'estimate' if we just want
70 to do some guesses on them, or as 'unused' is they are useless to us.
71 Then we find the largest range of calculatable effects in the
72 improvement and record it for later use.
73 **************************************************************************/
74 static void adv_data_city_impr_calc(struct player *pplayer,
75 struct adv_data *adv)
77 int count[ADV_IMPR_LAST];
79 memset(count, 0, sizeof(count));
81 improvement_iterate(pimprove) {
82 struct universal source = {
83 .kind = VUT_IMPROVEMENT,
84 .value = {.building = pimprove}
87 adv->impr_calc[improvement_index(pimprove)] = ADV_IMPR_ESTIMATE;
89 /* Find largest extension */
90 effect_list_iterate(get_req_source_effects(&source), peffect) {
91 switch (peffect->type) {
92 #if 0
93 /* TODO */
94 case EFT_FORCE_CONTENT:
95 case EFT_FORCE_CONTENT_PCT:
96 case EFT_MAKE_CONTENT:
97 case EFT_MAKE_CONTENT_MIL:
98 case EFT_MAKE_CONTENT_MIL_PER:
99 case EFT_MAKE_CONTENT_PCT:
100 case EFT_MAKE_HAPPY:
101 #endif
102 case EFT_CAPITAL_CITY:
103 case EFT_POLLU_POP_PCT:
104 case EFT_POLLU_POP_PCT_2:
105 case EFT_POLLU_PROD_PCT:
106 case EFT_OUTPUT_BONUS:
107 case EFT_OUTPUT_BONUS_2:
108 case EFT_OUTPUT_WASTE_PCT:
109 case EFT_UPKEEP_FREE:
110 requirement_vector_iterate(&peffect->reqs, preq) {
111 if (VUT_IMPROVEMENT == preq->source.kind
112 && preq->source.value.building == pimprove) {
113 if (adv->impr_calc[improvement_index(pimprove)] != ADV_IMPR_CALCULATE_FULL) {
114 adv->impr_calc[improvement_index(pimprove)] = ADV_IMPR_CALCULATE;
116 if (preq->range > adv->impr_range[improvement_index(pimprove)]) {
117 adv->impr_range[improvement_index(pimprove)] = preq->range;
120 } requirement_vector_iterate_end;
121 break;
122 case EFT_OUTPUT_ADD_TILE:
123 case EFT_OUTPUT_PER_TILE:
124 case EFT_OUTPUT_INC_TILE:
125 requirement_vector_iterate(&peffect->reqs, preq) {
126 if (VUT_IMPROVEMENT == preq->source.kind
127 && preq->source.value.building == pimprove) {
128 adv->impr_calc[improvement_index(pimprove)] = ADV_IMPR_CALCULATE_FULL;
129 if (preq->range > adv->impr_range[improvement_index(pimprove)]) {
130 adv->impr_range[improvement_index(pimprove)] = preq->range;
133 } requirement_vector_iterate_end;
134 break;
135 default:
136 /* Nothing! */
137 break;
139 } effect_list_iterate_end;
140 } improvement_iterate_end;
143 /**************************************************************************
144 Check if the player still takes advantage of EFT_TECH_PARASITE.
145 Research is useless if there are still techs which may be given to the
146 player for free.
147 **************************************************************************/
148 static bool player_has_really_useful_tech_parasite(struct player* pplayer)
150 struct research *presearch, *aresearch;
151 int players_needed = get_player_bonus(pplayer, EFT_TECH_PARASITE);
153 if (players_needed == 0) {
154 return FALSE;
157 presearch = research_get(pplayer);
158 advance_index_iterate(A_FIRST, tech) {
159 int players_having;
161 if (!research_invention_gettable(presearch, tech,
162 game.info.tech_parasite_allow_holes)
163 || TECH_KNOWN == research_invention_state(presearch, tech)) {
164 continue;
167 players_having = 0;
169 players_iterate_alive(aplayer) {
170 if (aplayer == pplayer) {
171 continue;
174 aresearch = research_get(aplayer);
175 if (TECH_KNOWN == research_invention_state(aresearch, tech)
176 || aresearch->researching == tech) {
177 players_having++;
178 if (players_having >= players_needed) {
179 return TRUE;
182 } players_iterate_alive_end;
183 } advance_index_iterate_end;
184 return FALSE;
187 /**************************************************************************
188 Analyze rulesets. Must be run after rulesets are loaded, unlike
189 _init, which must be run before savegames are loaded, which is usually
190 before rulesets.
191 **************************************************************************/
192 void adv_data_analyze_rulesets(struct player *pplayer)
194 struct adv_data *adv = pplayer->server.adv;
196 fc_assert_ret(adv != NULL);
198 adv_data_city_impr_calc(pplayer, adv);
201 /**************************************************************************
202 This function is called each turn to initialize pplayer->ai.stats.units.
203 **************************************************************************/
204 static void count_my_units(struct player *pplayer)
206 struct adv_data *adv = adv_data_get(pplayer, NULL);
208 memset(&adv->stats.units, 0, sizeof(adv->stats.units));
210 unit_list_iterate(pplayer->units, punit) {
211 struct unit_class *pclass = unit_class_get(punit);
213 adv->stats.units.byclass[uclass_index(pclass)]++;
215 if (unit_has_type_flag(punit, UTYF_COAST_STRICT)) {
216 adv->stats.units.coast_strict++;
218 if (uclass_has_flag(pclass, UCF_MISSILE)) {
219 adv->stats.units.missiles++;
221 if (unit_can_do_action(punit, ACTION_PARADROP)) {
222 adv->stats.units.paratroopers++;
224 if (utype_can_do_action(punit->utype, ACTION_AIRLIFT)) {
225 adv->stats.units.airliftable++;
227 if (can_upgrade_unittype(pplayer, unit_type_get(punit)) >= 0) {
228 adv->stats.units.upgradeable++;
230 } unit_list_iterate_end;
233 /**************************************************************************
234 Return whether data phase is currently open. Data phase is open
235 between adv_data_phase_init() and adv_data_phase_done() calls.
236 **************************************************************************/
237 bool is_adv_data_phase_open(struct player *pplayer)
239 struct adv_data *adv = pplayer->server.adv;
241 return adv->phase_is_initialized;
244 /**************************************************************************
245 Make and cache lots of calculations needed for other functions.
247 Returns TRUE if new data was created, FALSE if data existed already.
249 Note: We use map.num_continents here rather than pplayer->num_continents
250 because we are omniscient and don't care about such trivialities as who
251 can see what.
253 FIXME: We should try to find the lowest common defence strength of our
254 defending units, and ignore enemy units that are incapable of harming
255 us, instead of just checking attack strength > 1.
256 **************************************************************************/
257 bool adv_data_phase_init(struct player *pplayer, bool is_new_phase)
259 struct adv_data *adv = pplayer->server.adv;
260 int i;
261 int nuke_units;
262 bool danger_of_nukes;
264 fc_assert_ret_val(adv != NULL, FALSE);
266 if (adv->phase_is_initialized) {
267 return FALSE;
269 adv->phase_is_initialized = TRUE;
271 TIMING_LOG(AIT_AIDATA, TIMER_START);
273 nuke_units = num_role_units(action_id_get_role(ACTION_NUKE));
274 danger_of_nukes = FALSE;
276 /*** Threats ***/
278 adv->num_continents = wld.map.num_continents;
279 adv->num_oceans = wld.map.num_oceans;
280 adv->threats.continent = fc_calloc(adv->num_continents + 1, sizeof(bool));
281 adv->threats.invasions = FALSE;
282 adv->threats.nuclear = 0; /* none */
283 adv->threats.ocean = fc_calloc(adv->num_oceans + 1, sizeof(bool));
284 adv->threats.igwall = FALSE;
286 players_iterate(aplayer) {
287 if (!adv_is_player_dangerous(pplayer, aplayer)) {
288 continue;
291 /* The idea is that if there aren't any hostile cities on
292 * our continent, the danger of land attacks is not big
293 * enough to warrant city walls. Concentrate instead on
294 * coastal fortresses and hunting down enemy transports. */
295 city_list_iterate(aplayer->cities, acity) {
296 Continent_id continent = tile_continent(acity->tile);
297 if (continent >= 0) {
298 adv->threats.continent[continent] = TRUE;
300 } city_list_iterate_end;
302 unit_list_iterate(aplayer->units, punit) {
303 const struct unit_class *pclass = unit_class_get(punit);
305 if (unit_type_get(punit)->adv.igwall) {
306 adv->threats.igwall = TRUE;
309 if (pclass->adv.sea_move != MOVE_NONE) {
310 /* If the enemy has not started sailing yet, or we have total
311 * control over the seas, don't worry, keep attacking. */
312 if (uclass_has_flag(pclass, UCF_CAN_OCCUPY_CITY)) {
313 /* Enemy represents a cross-continental threat! */
314 adv->threats.invasions = TRUE;
315 } else if (get_transporter_capacity(punit) > 0) {
316 unit_class_iterate(cargoclass) {
317 if (uclass_has_flag(cargoclass, UCF_CAN_OCCUPY_CITY)
318 && can_unit_type_transport(unit_type_get(punit), cargoclass)) {
319 /* Enemy can transport some threatening units! */
320 adv->threats.invasions = TRUE;
321 break;
323 } unit_class_iterate_end;
326 /* The idea is that while our enemies don't have any offensive
327 * seaborne units, we don't have to worry. Go on the offensive! */
328 if (unit_type_get(punit)->attack_strength > 1) {
329 if (is_ocean_tile(unit_tile(punit))) {
330 Continent_id continent = tile_continent(unit_tile(punit));
331 adv->threats.ocean[-continent] = TRUE;
332 } else {
333 adjc_iterate(unit_tile(punit), tile2) {
334 if (is_ocean_tile(tile2)) {
335 Continent_id continent = tile_continent(tile2);
336 adv->threats.ocean[-continent] = TRUE;
338 } adjc_iterate_end;
341 continue;
344 /* If our enemy builds missiles, worry about missile defence. */
345 if (uclass_has_flag(unit_class_get(punit), UCF_MISSILE)
346 && unit_type_get(punit)->attack_strength > 1) {
347 adv->threats.missile = TRUE;
350 /* If he builds nukes, worry a lot. */
351 if (unit_can_do_action(punit, ACTION_NUKE)) {
352 danger_of_nukes = TRUE;
354 } unit_list_iterate_end;
356 /* Check for nuke capability */
357 for (i = 0; i < nuke_units; i++) {
358 struct unit_type *nuke =
359 get_role_unit(action_id_get_role(ACTION_NUKE), i);
361 if (can_player_build_unit_direct(aplayer, nuke)) {
362 adv->threats.nuclear = 1;
365 } players_iterate_end;
367 /* Increase from fear to terror if opponent actually has nukes */
368 if (danger_of_nukes) {
369 adv->threats.nuclear++; /* sum of both fears */
372 /*** Exploration ***/
374 adv->explore.land_done = TRUE;
375 adv->explore.sea_done = TRUE;
376 adv->explore.continent = fc_calloc(adv->num_continents + 1, sizeof(bool));
377 adv->explore.ocean = fc_calloc(adv->num_oceans + 1, sizeof(bool));
379 whole_map_iterate(&(wld.map), ptile) {
380 Continent_id continent = tile_continent(ptile);
382 if (is_ocean_tile(ptile)) {
383 if (adv->explore.sea_done && has_handicap(pplayer, H_TARGETS)
384 && !map_is_known(ptile, pplayer)) {
385 /* We're not done there. */
386 adv->explore.sea_done = FALSE;
387 adv->explore.ocean[-continent] = TRUE;
389 /* skip rest, which is land only */
390 continue;
392 if (adv->explore.continent[tile_continent(ptile)]) {
393 /* we don't need more explaining, we got the point */
394 continue;
396 if (tile_has_cause_extra(ptile, EC_HUT)
397 && (!has_handicap(pplayer, H_HUTS)
398 || map_is_known(ptile, pplayer))) {
399 adv->explore.land_done = FALSE;
400 adv->explore.continent[continent] = TRUE;
401 continue;
403 if (has_handicap(pplayer, H_TARGETS) && !map_is_known(ptile, pplayer)) {
404 /* this AI must explore */
405 adv->explore.land_done = FALSE;
406 adv->explore.continent[continent] = TRUE;
408 } whole_map_iterate_end;
410 /*** Statistics ***/
412 adv->stats.cities = fc_calloc(adv->num_continents + 1, sizeof(int));
413 adv->stats.average_production = 0;
414 city_list_iterate(pplayer->cities, pcity) {
415 Continent_id continent = tile_continent(pcity->tile);
416 if (continent >= 0) {
417 adv->stats.cities[continent]++;
419 adv->stats.average_production += pcity->surplus[O_SHIELD];
420 } city_list_iterate_end;
421 adv->stats.average_production /= MAX(1, city_list_size(pplayer->cities));
423 /*** Diplomacy ***/
425 players_iterate(aplayer) {
426 struct adv_dipl *dip = adv_dipl_get(pplayer, aplayer);
428 dip->allied_with_enemy = FALSE;
429 players_iterate(check_pl) {
430 if (pplayers_allied(aplayer, check_pl)
431 && player_diplstate_get(pplayer, check_pl)->type == DS_WAR) {
432 dip->allied_with_enemy = TRUE;
434 } players_iterate_end;
435 } players_iterate_end;
437 adv->dipl.spacerace_leader = player_leading_spacerace();
439 adv->dipl.production_leader = NULL;
440 players_iterate(aplayer) {
441 if (adv->dipl.production_leader == NULL
442 || adv->dipl.production_leader->score.mfg < aplayer->score.mfg) {
443 adv->dipl.production_leader = aplayer;
445 } players_iterate_end;
447 /*** Priorities ***/
449 /* NEVER set these to zero! Weight values are usually multiplied by
450 * these values, so be careful with them. They are used in city
451 * and government calculations, and food and shields should be
452 * slightly bigger because we only look at surpluses there. They
453 * are all WAGs. */
454 adv->food_priority = FOOD_WEIGHTING;
455 adv->shield_priority = SHIELD_WEIGHTING;
456 if (adv_wants_science(pplayer)) {
457 adv->luxury_priority = 1;
458 adv->science_priority = TRADE_WEIGHTING;
459 } else {
460 adv->luxury_priority = TRADE_WEIGHTING;
461 adv->science_priority = 1;
463 adv->gold_priority = TRADE_WEIGHTING;
464 adv->happy_priority = 1;
465 adv->unhappy_priority = TRADE_WEIGHTING; /* danger */
466 adv->angry_priority = TRADE_WEIGHTING * 3; /* grave danger */
467 adv->pollution_priority = POLLUTION_WEIGHTING;
469 /* Research want */
470 if (is_future_tech(research_get(pplayer)->researching)
471 || player_has_really_useful_tech_parasite(pplayer)) {
472 adv->wants_science = FALSE;
473 } else {
474 adv->wants_science = TRUE;
477 /* max num cities
478 * The idea behind this code is that novice players don't understand that
479 * expansion is critical and find it very annoying.
480 * With the following code AI players will try to be only a bit better
481 * than the best human players. This should lead to more exciting games
482 * for the beginners.
484 if (has_handicap(pplayer, H_EXPANSION)) {
485 bool found_human = FALSE;
486 adv->max_num_cities = 3;
487 players_iterate_alive(aplayer) {
488 if (aplayer == pplayer || is_ai(aplayer)) {
489 continue;
491 adv->max_num_cities = MAX(adv->max_num_cities,
492 city_list_size(aplayer->cities) + 3);
493 found_human = TRUE;
494 } players_iterate_alive_end;
495 if (!found_human) {
496 adv->max_num_cities = MAP_INDEX_SIZE;
498 } else {
499 adv->max_num_cities = MAP_INDEX_SIZE;
502 count_my_units(pplayer);
504 TIMING_LOG(AIT_AIDATA, TIMER_STOP);
506 /* Government */
507 TIMING_LOG(AIT_GOVERNMENT, TIMER_START);
508 adv_best_government(pplayer);
509 TIMING_LOG(AIT_GOVERNMENT, TIMER_STOP);
511 return TRUE;
514 /**************************************************************************
515 Clean up our mess.
516 **************************************************************************/
517 void adv_data_phase_done(struct player *pplayer)
519 struct adv_data *adv = pplayer->server.adv;
521 fc_assert_ret(adv != NULL);
523 if (!adv->phase_is_initialized) {
524 return;
527 free(adv->explore.ocean);
528 adv->explore.ocean = NULL;
530 free(adv->explore.continent);
531 adv->explore.continent = NULL;
533 free(adv->threats.continent);
534 adv->threats.continent = NULL;
536 free(adv->threats.ocean);
537 adv->threats.ocean = NULL;
539 free(adv->stats.cities);
540 adv->stats.cities = NULL;
542 adv->num_continents = 0;
543 adv->num_oceans = 0;
545 adv->phase_is_initialized = FALSE;
548 /**************************************************************************
549 Return a pointer to our data.
550 If caller_closes is set, data phase will be opened even if it's
551 currently closed, and the boolean will be set accordingly to tell caller
552 that phase needs closing.
553 **************************************************************************/
554 struct adv_data *adv_data_get(struct player *pplayer, bool *caller_closes)
556 struct adv_data *adv = pplayer->server.adv;
558 fc_assert_ret_val(adv != NULL, NULL);
560 /* It's certainly indication of bug causing problems
561 if this adv_data_get() gets called between adv_data_phase_done() and
562 adv_data_phase_init(), since we may end up calling those
563 functions if number of known continents has changed.
565 Consider following case:
566 Correct call order would be:
567 a) adv_data_phase_init()
568 b) adv_data_get() -> adv_data_phase_done()
569 c) adv_data_get() -> adv_data_phase_init()
570 d) adv_data_phase_done()
571 e) do something
572 f) adv_data_phase_init()
574 In (e) data phase would be closed and data would be
575 correctly initialized at (f), which is probably beginning
576 next turn.
578 Buggy version where adv_data_get() (b&c) gets called after (d):
579 a) adv_data_phase_init()
580 d) adv_data_phase_done()
581 b) adv_data_get() -> adv_data_phase_done()
582 c) adv_data_get() -> adv_data_phase_init()
583 e) do something
584 f) adv_data_phase_init()
586 Now in (e) data phase would be open. When adv_data_phase_init()
587 then finally gets called and it really should recreate data
588 to match situation of new turn, it detects that data phase
589 is already initialized and does nothing.
591 So, this assertion is here for a reason!
593 Code below tries to fix the situation best it can if such a bug is
594 encountered. Since we are probably going to trust that to be enough
595 instead of making intrusive fixes for actual bug in stable branch,
596 do not assert for non-debug builds of stable versions. */
597 #if defined(FREECIV_DEBUG) || defined(IS_DEVEL_VERSION)
598 fc_assert(caller_closes != NULL || adv->phase_is_initialized);
599 #endif
601 if (caller_closes != NULL) {
602 *caller_closes = FALSE;
605 if (adv->num_continents != wld.map.num_continents
606 || adv->num_oceans != wld.map.num_oceans) {
607 /* we discovered more continents, recalculate! */
609 if (adv->phase_is_initialized) {
610 /* Only call these in this order if inside data phase.
611 This is blanket "fix" for all cases where adv_data_get() is called
612 at illegal time. This at least minimize bad effects of such calls. */
613 adv_data_phase_done(pplayer);
614 adv_data_phase_init(pplayer, FALSE);
615 } else {
616 /* Call them in "wrong" order so we return recalculated data to caller,
617 but leave data phase closed.
618 This is blanket "fix" for all cases where adv_data_get() is called
619 at illegal time. This at least minimize bad effects of such calls.
621 Arguably this is not buggy at all but works just as designed in
622 case of being called in alternate movement mode for players
623 other than currently moving one (for diplomacy between the two,
624 for example) */
625 log_debug("%s advisor data phase closed when adv_data_get() called",
626 player_name(pplayer));
627 adv_data_phase_init(pplayer, FALSE);
628 if (caller_closes != NULL) {
629 *caller_closes = TRUE;
630 } else {
631 adv_data_phase_done(pplayer);
634 } else {
635 if (!adv->phase_is_initialized && caller_closes != NULL) {
636 adv_data_phase_init(pplayer, FALSE);
637 *caller_closes = TRUE;
641 return adv;
644 /**************************************************************************
645 Allocate memory for advisor data. Save to call multiple times.
646 **************************************************************************/
647 void adv_data_init(struct player *pplayer)
649 struct adv_data *adv;
651 if (pplayer->server.adv == NULL) {
652 pplayer->server.adv = fc_calloc(1, sizeof(*pplayer->server.adv));
654 adv = pplayer->server.adv;
656 adv->government_want = NULL;
658 adv->dipl.adv_dipl_slots = fc_calloc(player_slot_count(),
659 sizeof(*adv->dipl.adv_dipl_slots));
660 player_slots_iterate(pslot) {
661 struct adv_dipl **dip_slot =
662 adv->dipl.adv_dipl_slots + player_slot_index(pslot);
663 *dip_slot = NULL;
664 } player_slots_iterate_end;
666 players_iterate(aplayer) {
667 adv_dipl_new(pplayer, aplayer);
668 if (aplayer != pplayer) {
669 adv_dipl_new(aplayer, pplayer);
671 } players_iterate_end;
673 adv_data_default(pplayer);
676 /**************************************************************************
677 Initialize with sane values.
678 **************************************************************************/
679 void adv_data_default(struct player *pplayer)
681 struct adv_data *adv = pplayer->server.adv;
683 fc_assert_ret(adv != NULL);
685 adv->govt_reeval = 0;
686 adv->government_want = fc_realloc(adv->government_want,
687 (government_count() + 1)
688 * sizeof(*adv->government_want));
689 memset(adv->government_want, 0,
690 (government_count() + 1) * sizeof(*adv->government_want));
692 adv->wonder_city = 0;
694 adv->wants_science = TRUE;
695 adv->celebrate = FALSE;
696 adv->max_num_cities = 10000;
699 /**************************************************************************
700 Free memory for advisor data.
701 **************************************************************************/
702 void adv_data_close(struct player *pplayer)
704 struct adv_data *adv = pplayer->server.adv;
706 fc_assert_ret(NULL != adv);
708 adv_data_phase_done(pplayer);
710 if (adv->government_want != NULL) {
711 free(adv->government_want);
714 if (adv->dipl.adv_dipl_slots != NULL) {
715 players_iterate(aplayer) {
716 adv_dipl_free(pplayer, aplayer);
717 if (aplayer != pplayer) {
718 adv_dipl_free(aplayer, pplayer);
720 } players_iterate_end;
721 FC_FREE(adv->dipl.adv_dipl_slots);
724 if (adv != NULL) {
725 free(adv);
728 pplayer->server.adv = NULL;
731 /****************************************************************************
732 Allocate new advisor diplomacy slot
733 ****************************************************************************/
734 static void adv_dipl_new(const struct player *plr1,
735 const struct player *plr2)
737 struct adv_dipl **dip_slot =
738 plr1->server.adv->dipl.adv_dipl_slots + player_index(plr2);
740 *dip_slot = fc_calloc(1, sizeof(struct adv_dipl));
743 /****************************************************************************
744 Free resources allocated for diplomacy information between two players.
745 ****************************************************************************/
746 static void adv_dipl_free(const struct player *plr1,
747 const struct player *plr2)
749 struct adv_dipl **dip_slot =
750 plr1->server.adv->dipl.adv_dipl_slots + player_index(plr2);
752 if (*dip_slot != NULL) {
753 FC_FREE(*dip_slot);
754 *dip_slot = NULL;
758 /**************************************************************************
759 Returns diplomatic state type between two players
760 **************************************************************************/
761 static struct adv_dipl *adv_dipl_get(const struct player *plr1,
762 const struct player *plr2)
764 struct adv_dipl **dip_slot =
765 plr1->server.adv->dipl.adv_dipl_slots + player_index(plr2);
767 return *dip_slot;
770 /**************************************************************************
771 Find best government to aim for.
772 We do it by setting our government to all possible values and calculating
773 our GDP (total ai_eval_calc_city) under this government. If the very
774 best of the governments is not available to us (it is not yet discovered),
775 we record it in the goal.gov structure with the aim of wanting the
776 necessary tech more. The best of the available governments is recorded
777 in goal.revolution. We record the want of each government, and only
778 recalculate this data every ai->govt_reeval_turns turns.
780 Note: Call this _before_ doing taxes!
781 **************************************************************************/
782 void adv_best_government(struct player *pplayer)
784 struct adv_data *adv = adv_data_get(pplayer, NULL);
785 int best_val = 0;
786 struct government *current_gov = government_of_player(pplayer);
788 adv->goal.govt.gov = current_gov;
789 adv->goal.govt.val = 0;
790 adv->goal.govt.req = A_UNSET;
791 adv->goal.revolution = current_gov;
793 if (has_handicap(pplayer, H_AWAY) || !pplayer->is_alive) {
794 return;
797 if (adv->govt_reeval == 0) {
798 const struct research *presearch = research_get(pplayer);
800 governments_iterate(gov) {
801 adv_want val = 0;
802 bool override = FALSE;
804 if (gov == game.government_during_revolution) {
805 continue; /* pointless */
807 if (gov->ai.better
808 && can_change_to_government(pplayer, gov->ai.better)) {
809 continue; /* we have better governments available */
812 CALL_PLR_AI_FUNC(gov_value, pplayer, pplayer, gov, &val, &override);
814 if (!override) {
815 int dist;
816 adv_want bonus = 0; /* in percentage */
817 int revolution_turns;
819 pplayer->government = gov;
820 /* Ideally we should change tax rates here, but since
821 * this is a rather big CPU operation, we'd rather not. */
822 check_player_max_rates(pplayer);
823 city_list_iterate(pplayer->cities, acity) {
824 auto_arrange_workers(acity);
825 } city_list_iterate_end;
826 city_list_iterate(pplayer->cities, pcity) {
827 val += adv_eval_calc_city(pcity, adv);
828 } city_list_iterate_end;
830 /* Bonuses for non-economic abilities. We increase val by
831 * a very small amount here to choose govt in cases where
832 * we have no cities yet. */
833 bonus += get_player_bonus(pplayer, EFT_VETERAN_BUILD) > 0 ? 3 : 0;
835 /* TODO: Individual and well balanced value. */
836 action_iterate(act) {
837 if (!action_immune_government(gov, act)) {
838 /* This government doesn't provide immunity againt this
839 * action. */
840 continue;
843 switch((enum gen_action)act) {
844 case ACTION_ATTACK:
845 case ACTION_SPY_INCITE_CITY:
846 case ACTION_CONQUER_CITY:
847 bonus += 4;
848 break;
849 case ACTION_SPY_BRIBE_UNIT:
850 bonus += 2;
851 break;
852 case ACTION_SPY_INVESTIGATE_CITY:
853 case ACTION_INV_CITY_SPEND:
854 case ACTION_SPY_POISON:
855 case ACTION_SPY_STEAL_GOLD:
856 case ACTION_SPY_SABOTAGE_CITY:
857 case ACTION_SPY_TARGETED_SABOTAGE_CITY:
858 case ACTION_SPY_STEAL_TECH:
859 case ACTION_SPY_TARGETED_STEAL_TECH:
860 case ACTION_SPY_SABOTAGE_UNIT:
861 case ACTION_CAPTURE_UNITS:
862 case ACTION_STEAL_MAPS:
863 case ACTION_BOMBARD:
864 case ACTION_SPY_NUKE:
865 case ACTION_NUKE:
866 case ACTION_DESTROY_CITY:
867 case ACTION_EXPEL_UNIT:
868 /* Being a target of this is usually undesireable */
869 /* TODO: Individual and well balanced values. */
870 bonus += 0.1;
871 break;
873 case ACTION_MARKETPLACE:
874 case ACTION_FOUND_CITY:
875 case ACTION_DISBAND_UNIT:
876 case ACTION_PARADROP:
877 /* Wants the ability to do this to it self. Don't want others
878 * to target it. Do nothing since action_immune_government()
879 * doesn't separate based on who the actor is. */
880 break;
882 case ACTION_ESTABLISH_EMBASSY:
883 case ACTION_ESTABLISH_EMBASSY_STAY:
884 case ACTION_TRADE_ROUTE:
885 case ACTION_JOIN_CITY:
886 case ACTION_HELP_WONDER:
887 case ACTION_RECYCLE_UNIT:
888 case ACTION_HOME_CITY:
889 case ACTION_UPGRADE_UNIT:
890 case ACTION_AIRLIFT:
891 case ACTION_HEAL_UNIT:
892 /* Could be good. An embassy gives permanent contact. A trade
893 * route gives gold per turn. Join city gives population. Help
894 * wonder gives shields. */
895 /* TODO: Individual and well balanced values. */
896 break;
898 case ACTION_COUNT:
899 /* Invalid */
900 fc_assert(act != ACTION_COUNT);
901 break;
903 } action_iterate_end;
905 bonus += get_player_bonus(pplayer, EFT_INSPIRE_PARTISANS) > 0 ? 3 : 0;
906 bonus += get_player_bonus(pplayer, EFT_RAPTURE_GROW) > 0 ? 2 : 0;
907 bonus += get_player_bonus(pplayer, EFT_FANATICS) > 0 ? 3 : 0;
908 bonus += get_player_bonus(pplayer, EFT_OUTPUT_INC_TILE) * 8;
910 revolution_turns = get_player_bonus(pplayer, EFT_REVOLUTION_UNHAPPINESS);
911 if (revolution_turns > 0) {
912 bonus -= 6 / revolution_turns;
915 val += (val * bonus) / 100;
917 /* FIXME: handle reqs other than technologies. */
918 dist = 0;
919 requirement_vector_iterate(&gov->reqs, preq) {
920 if (VUT_ADVANCE == preq->source.kind) {
921 dist += MAX(1, research_goal_unknown_techs(presearch,
922 advance_number(preq->source.value.advance)));
924 } requirement_vector_iterate_end;
925 val = amortize(val, dist);
928 adv->government_want[government_index(gov)] = val; /* Save want */
929 } governments_iterate_end;
930 /* Now reset our gov to it's real state. */
931 pplayer->government = current_gov;
932 city_list_iterate(pplayer->cities, acity) {
933 auto_arrange_workers(acity);
934 } city_list_iterate_end;
935 if (player_is_cpuhog(pplayer)) {
936 adv->govt_reeval = 1;
937 } else {
938 adv->govt_reeval = CLIP(5, city_list_size(pplayer->cities), 20);
941 adv->govt_reeval--;
943 /* Figure out which government is the best for us this turn. */
944 governments_iterate(gov) {
945 int gi = government_index(gov);
946 if (adv->government_want[gi] > best_val
947 && can_change_to_government(pplayer, gov)) {
948 best_val = adv->government_want[gi];
949 adv->goal.revolution = gov;
951 if (adv->government_want[gi] > adv->goal.govt.val) {
952 adv->goal.govt.gov = gov;
953 adv->goal.govt.val = adv->government_want[gi];
955 /* FIXME: handle reqs other than technologies. */
956 adv->goal.govt.req = A_NONE;
957 requirement_vector_iterate(&gov->reqs, preq) {
958 if (VUT_ADVANCE == preq->source.kind) {
959 adv->goal.govt.req = advance_number(preq->source.value.advance);
960 break;
962 } requirement_vector_iterate_end;
964 } governments_iterate_end;
965 /* Goodness of the ideal gov is calculated relative to the goodness of the
966 * best of the available ones. */
967 adv->goal.govt.val -= best_val;
970 /**************************************************************************
971 Return whether science would help us at all.
972 **************************************************************************/
973 bool adv_wants_science(struct player *pplayer)
975 return adv_data_get(pplayer, NULL)->wants_science;
979 /**********************************************************************
980 There are some signs that a player might be dangerous: We are at
981 war with him, he has done lots of ignoble things to us, he is an
982 ally of one of our enemies (a ticking bomb to be sure), we don't like him,
983 diplomatic state is neutral or we have cease fire.
984 ***********************************************************************/
985 bool adv_is_player_dangerous(struct player *pplayer,
986 struct player *aplayer)
988 struct adv_dipl *dip;
989 enum diplstate_type ds;
990 enum override_bool dang = NO_OVERRIDE;
992 if (is_ai(pplayer)) {
993 /* Give AI code possibility to decide itself */
994 CALL_PLR_AI_FUNC(consider_plr_dangerous, pplayer, pplayer, aplayer, &dang);
997 if (dang == OVERRIDE_FALSE) {
998 return FALSE;
1001 if (dang == OVERRIDE_TRUE) {
1002 return TRUE;
1005 if (pplayer == aplayer) {
1006 /* We always trust ourself */
1007 return FALSE;
1010 ds = player_diplstate_get(pplayer, aplayer)->type;
1012 if (ds == DS_WAR || ds == DS_CEASEFIRE) {
1013 /* It's already a war or aplayer can declare it soon */
1014 return TRUE;
1017 dip = adv_dipl_get(pplayer, aplayer);
1019 if (dip->allied_with_enemy) {
1020 /* Don't trust someone who will declare war on us soon */
1021 return TRUE;
1024 if (player_diplstate_get(pplayer, aplayer)->has_reason_to_cancel > 0) {
1025 return TRUE;
1028 if (pplayer->ai_common.love[player_index(aplayer)] < MAX_AI_LOVE / 10) {
1029 /* We don't trust players who we don't like. Note that
1030 * aplayer's units inside pplayer's borders decreases AI's love */
1031 return TRUE;
1034 return FALSE;