59
64
memset(count, 0, sizeof(count));
61
66
impr_type_iterate(id) {
67
struct req_source source = {
69
.value = {.building = id}
62
72
ai->impr_calc[id] = AI_IMPR_ESTIMATE;
64
74
/* Find largest extension */
65
effect_type_vector_iterate(get_building_effect_types(id), ptype) {
75
effect_list_iterate(get_req_source_effects(&source), peffect) {
76
switch (peffect->type) {
69
79
case EFT_FORCE_CONTENT:
74
84
case EFT_MAKE_CONTENT_PCT:
75
85
case EFT_MAKE_HAPPY:
77
case EFT_LUXURY_BONUS:
78
case EFT_SCIENCE_BONUS:
80
87
case EFT_CAPITAL_CITY:
82
case EFT_FOOD_ADD_TILE:
83
case EFT_FOOD_INC_TILE:
84
case EFT_FOOD_PER_TILE:
85
88
case EFT_POLLU_POP_PCT:
86
89
case EFT_POLLU_PROD_PCT:
87
case EFT_PROD_ADD_TILE:
89
case EFT_PROD_INC_TILE:
90
case EFT_PROD_PER_TILE:
91
case EFT_TRADE_ADD_TILE:
92
case EFT_TRADE_INC_TILE:
93
case EFT_TRADE_PER_TILE:
90
case EFT_OUTPUT_BONUS:
91
case EFT_OUTPUT_BONUS_2:
92
case EFT_OUTPUT_WASTE_PCT:
94
93
case EFT_UPKEEP_FREE:
95
effect_list_iterate(*get_building_effects(id, *ptype), peff) {
96
ai->impr_calc[id] = AI_IMPR_CALCULATE;
97
if (peff->range > ai->impr_range[id]) {
98
ai->impr_range[id] = peff->range;
100
} effect_list_iterate_end;
94
requirement_list_iterate(peffect->reqs, preq) {
95
if (preq->source.type == REQ_BUILDING
96
&& preq->source.value.building == id) {
97
if (ai->impr_calc[id] != AI_IMPR_CALCULATE_FULL) {
98
ai->impr_calc[id] = AI_IMPR_CALCULATE;
100
if (preq->range > ai->impr_range[id]) {
101
ai->impr_range[id] = preq->range;
104
} requirement_list_iterate_end;
106
case EFT_OUTPUT_ADD_TILE:
107
case EFT_OUTPUT_PER_TILE:
108
case EFT_OUTPUT_INC_TILE:
109
requirement_list_iterate(peffect->reqs, preq) {
110
if (preq->source.type == REQ_BUILDING
111
&& preq->source.value.building == id) {
112
ai->impr_calc[id] = AI_IMPR_CALCULATE_FULL;
113
if (preq->range > ai->impr_range[id]) {
114
ai->impr_range[id] = preq->range;
117
} requirement_list_iterate_end;
106
} effect_type_vector_iterate_end;
123
} effect_list_iterate_end;
108
124
} impr_type_iterate_end;
111
127
/**************************************************************************
128
Check if the player still takes advantage of EFT_TECH_PARASITE.
129
Research is useless if there are still techs which may be given to the
131
**************************************************************************/
132
static bool player_has_really_useful_tech_parasite(struct player* pplayer)
134
int players_needed = get_player_bonus(pplayer, EFT_TECH_PARASITE);
136
if (players_needed == 0) {
140
tech_type_iterate(tech) {
143
if (get_invention(pplayer, tech) == TECH_KNOWN
144
|| !tech_is_available(pplayer, tech)) {
150
players_iterate(aplayer) {
151
if (aplayer == pplayer || !aplayer->is_alive) {
155
if (get_invention(aplayer, tech) == TECH_KNOWN
156
|| get_player_research(aplayer)->researching == tech) {
158
if (players_having >= players_needed) {
162
} players_iterate_end;
163
} tech_type_iterate_end;
167
/**************************************************************************
112
168
Analyze rulesets. Must be run after rulesets after loaded, unlike
113
169
_init, which must be run before savegames are loaded, which is usually
115
171
**************************************************************************/
116
172
void ai_data_analyze_rulesets(struct player *pplayer)
118
struct ai_data *ai = &aidata[pplayer->player_no];
174
struct ai_data *ai = &aidata[player_index(pplayer)];
120
176
ai_data_city_impr_calc(pplayer, ai);
146
if (unit_flag(punit, F_TRIREME)) {
202
if (unit_has_type_flag(punit, F_TRIREME)) {
147
203
ai->stats.units.triremes++;
149
if (unit_flag(punit, F_MISSILE)) {
205
if (unit_has_type_flag(punit, F_MISSILE)) {
150
206
ai->stats.units.missiles++;
152
if (can_upgrade_unittype(pplayer, punit->type) >= 0) {
208
if (unit_has_type_flag(punit, F_PARATROOPERS)) {
209
ai->stats.units.paratroopers++;
211
if (can_upgrade_unittype(pplayer, unit_type(punit)) >= 0) {
153
212
ai->stats.units.upgradeable++;
155
214
} unit_list_iterate_end;
166
225
defending units, and ignore enemy units that are incapable of harming
167
226
us, instead of just checking attack strength > 1.
168
227
**************************************************************************/
169
void ai_data_turn_init(struct player *pplayer)
228
void ai_data_phase_init(struct player *pplayer, bool is_new_phase)
171
struct ai_data *ai = &aidata[pplayer->player_no];
172
int i, nuke_units = num_role_units(F_NUCLEAR);
230
struct ai_data *ai = &aidata[player_index(pplayer)];
232
int nuke_units = num_role_units(F_NUCLEAR);
173
233
bool danger_of_nukes = FALSE;
174
int ally_strength = -1;
175
struct player *ally_strongest = NULL;
177
235
/*** Threats ***/
237
TIMING_LOG(AIT_AIDATA, TIMER_START);
179
239
ai->num_continents = map.num_continents;
180
240
ai->num_oceans = map.num_oceans;
181
241
ai->threats.continent = fc_calloc(ai->num_continents + 1, sizeof(bool));
195
255
* enough to warrant city walls. Concentrate instead on
196
256
* coastal fortresses and hunting down enemy transports. */
197
257
city_list_iterate(aplayer->cities, acity) {
198
Continent_id continent = map_get_continent(acity->tile);
258
Continent_id continent = tile_get_continent(acity->tile);
199
259
ai->threats.continent[continent] = TRUE;
200
260
} city_list_iterate_end;
202
262
unit_list_iterate(aplayer->units, punit) {
203
if (unit_flag(punit, F_IGWALL)) {
263
if (unit_has_type_flag(punit, F_IGWALL)) {
204
264
ai->threats.igwall = TRUE;
214
274
/* The idea is that while our enemies don't have any offensive
215
275
* seaborne units, we don't have to worry. Go on the offensive! */
216
276
if (unit_type(punit)->attack_strength > 1) {
217
if (is_ocean(map_get_terrain(punit->tile))) {
218
Continent_id continent = map_get_continent(punit->tile);
277
if (is_ocean(tile_get_terrain(punit->tile))) {
278
Continent_id continent = tile_get_continent(punit->tile);
219
279
ai->threats.ocean[-continent] = TRUE;
221
281
adjc_iterate(punit->tile, tile2) {
222
if (is_ocean(map_get_terrain(tile2))) {
223
Continent_id continent = map_get_continent(tile2);
282
if (is_ocean(tile_get_terrain(tile2))) {
283
Continent_id continent = tile_get_continent(tile2);
224
284
ai->threats.ocean[-continent] = TRUE;
226
286
} adjc_iterate_end;
239
299
/* If our enemy builds missiles, worry about missile defence. */
240
if (unit_flag(punit, F_MISSILE)
300
if (unit_has_type_flag(punit, F_MISSILE)
241
301
&& unit_type(punit)->attack_strength > 1) {
242
302
ai->threats.missile = TRUE;
245
305
/* If he builds nukes, worry a lot. */
246
if (unit_flag(punit, F_NUCLEAR)) {
306
if (unit_has_type_flag(punit, F_NUCLEAR)) {
247
307
danger_of_nukes = TRUE;
249
309
} unit_list_iterate_end;
251
311
/* Check for nuke capability */
252
312
for (i = 0; i < nuke_units; i++) {
253
Unit_Type_id nuke = get_role_unit(F_NUCLEAR, i);
313
struct unit_type *nuke = get_role_unit(F_NUCLEAR, i);
254
315
if (can_player_build_unit_direct(aplayer, nuke)) {
255
316
ai->threats.nuclear = 1;
267
328
ai->explore.continent = fc_calloc(ai->num_continents + 1, sizeof(bool));
268
329
ai->explore.ocean = fc_calloc(ai->num_oceans + 1, sizeof(bool));
269
330
whole_map_iterate(ptile) {
270
Continent_id continent = map_get_continent(ptile);
331
Continent_id continent = tile_get_continent(ptile);
272
333
if (is_ocean(ptile->terrain)) {
273
334
if (ai->explore.sea_done && ai_handicap(pplayer, H_TARGETS)
283
344
/* we don't need more explaining, we got the point */
286
if (map_has_special(ptile, S_HUT)
347
if (tile_has_special(ptile, S_HUT)
287
348
&& (!ai_handicap(pplayer, H_HUTS)
288
349
|| map_is_known(ptile, pplayer))) {
289
350
ai->explore.land_done = FALSE;
303
364
ai->stats.cities = fc_calloc(ai->num_continents + 1, sizeof(int));
304
365
ai->stats.average_production = 0;
305
366
city_list_iterate(pplayer->cities, pcity) {
306
ai->stats.cities[(int)map_get_continent(pcity->tile)]++;
307
ai->stats.average_production += pcity->shield_surplus;
367
ai->stats.cities[(int)tile_get_continent(pcity->tile)]++;
368
ai->stats.average_production += pcity->surplus[O_SHIELD];
308
369
} city_list_iterate_end;
309
ai->stats.average_production /= MAX(1, city_list_size(&pplayer->cities));
370
ai->stats.average_production /= MAX(1, city_list_size(pplayer->cities));
310
371
BV_CLR_ALL(ai->stats.diplomat_reservations);
311
372
unit_list_iterate(pplayer->units, punit) {
312
373
struct tile *ptile = punit->tile;
314
if (!is_ocean(ptile->terrain) && unit_flag(punit, F_SETTLERS)) {
315
ai->stats.workers[(int)map_get_continent(punit->tile)]++;
375
if (!is_ocean(ptile->terrain) && unit_has_type_flag(punit, F_SETTLERS)) {
376
ai->stats.workers[(int)tile_get_continent(punit->tile)]++;
317
if (unit_flag(punit, F_DIPLOMAT) && punit->ai.ai_role == AIUNIT_ATTACK) {
378
if (unit_has_type_flag(punit, F_DIPLOMAT) && punit->ai.ai_role == AIUNIT_ATTACK) {
318
379
/* Heading somewhere on a mission, reserve target. */
319
struct city *pcity = map_get_city(punit->goto_tile);
380
struct city *pcity = tile_get_city(punit->goto_tile);
322
383
BV_SET(ai->stats.diplomat_reservations, pcity->id);
328
389
/*** Diplomacy ***/
330
if (pplayer->ai.control && !is_barbarian(pplayer)) {
331
ai_diplomacy_calculate(pplayer, ai);
391
if (pplayer->ai.control && !is_barbarian(pplayer) && is_new_phase) {
392
ai_diplomacy_begin_new_phase(pplayer, ai);
334
/* Question: What can we accept as the reputation of a player before
335
* we start taking action to prevent us from being suckered?
336
* Answer: Very little. */
337
ai->diplomacy.acceptable_reputation =
338
GAME_DEFAULT_REPUTATION -
339
GAME_DEFAULT_REPUTATION / 4;
340
ai->diplomacy.acceptable_reputation_for_ceasefire =
341
GAME_DEFAULT_REPUTATION / 3;
343
395
/* Set per-player variables. We must set all players, since players
344
396
* can be created during a turn, and we don't want those to have
345
397
* invalid values. */
346
398
for (i = 0; i < MAX_NUM_PLAYERS + MAX_NUM_BARBARIANS; i++) {
347
struct player *aplayer = get_player(i);
399
struct player *aplayer = player_by_number(i);
349
401
ai->diplomacy.player_intel[i].is_allied_with_enemy = NULL;
350
402
ai->diplomacy.player_intel[i].at_war_with_ally = NULL;
351
403
ai->diplomacy.player_intel[i].is_allied_with_ally = NULL;
353
/* Determine who is the leader of our alliance. That is,
354
* whoever has the more cities. */
355
if (pplayers_allied(pplayer, aplayer)
356
&& city_list_size(&aplayer->cities) > ally_strength) {
357
ally_strength = city_list_size(&aplayer->cities);
358
ally_strongest = aplayer;
361
405
players_iterate(check_pl) {
362
406
if (check_pl == pplayer
363
407
|| check_pl == aplayer
437
476
*punit->ai.cur_pos = punit->tile;
438
477
} unit_list_iterate_end;
439
478
} players_iterate_end;
481
if (is_future_tech(get_player_research(pplayer)->researching)
482
|| player_has_really_useful_tech_parasite(pplayer)) {
483
ai->wants_no_science = TRUE;
485
ai->wants_no_science = FALSE;
489
* The idea behind this code is that novice players don't understand that
490
* expansion is critical and find it very annoying.
491
* With the following code AI players will try to be only a bit better
492
* than the best human players. This should lead to more exciting games
495
if (ai_handicap(pplayer, H_EXPANSION)) {
496
bool found_human = FALSE;
497
ai->max_num_cities = 3;
498
players_iterate(aplayer) {
499
if (aplayer == pplayer || aplayer->ai.control || !aplayer->is_alive) {
502
ai->max_num_cities = MAX(ai->max_num_cities,
503
city_list_size(aplayer->cities) + 3);
505
} players_iterate_end;
507
ai->max_num_cities = MAP_INDEX_SIZE;
510
ai->max_num_cities = MAP_INDEX_SIZE;
441
513
count_my_units(pplayer);
515
TIMING_LOG(AIT_AIDATA, TIMER_STOP);
518
TIMING_LOG(AIT_GOVERNMENT, TIMER_START);
519
ai_best_government(pplayer);
520
TIMING_LOG(AIT_GOVERNMENT, TIMER_STOP);
444
523
/**************************************************************************
445
524
Clean up our mess.
446
525
**************************************************************************/
447
void ai_data_turn_done(struct player *pplayer)
526
void ai_data_phase_done(struct player *pplayer)
449
struct ai_data *ai = &aidata[pplayer->player_no];
451
free(ai->explore.ocean); ai->explore.ocean = NULL;
452
free(ai->explore.continent); ai->explore.continent = NULL;
453
free(ai->threats.continent); ai->threats.continent = NULL;
528
struct ai_data *ai = &aidata[player_index(pplayer)];
530
free(ai->explore.ocean);
531
ai->explore.ocean = NULL;
533
free(ai->explore.continent);
534
ai->explore.continent = NULL;
536
free(ai->threats.continent);
537
ai->threats.continent = NULL;
454
539
free(ai->threats.ocean);
455
540
ai->threats.ocean = NULL;
456
free(ai->stats.workers); ai->stats.workers = NULL;
457
free(ai->stats.cities); ai->stats.cities = NULL;
542
free(ai->stats.workers);
543
ai->stats.workers = NULL;
545
free(ai->stats.cities);
546
ai->stats.cities = NULL;
548
ai->num_continents = 0;
460
552
/**************************************************************************
462
554
**************************************************************************/
463
555
struct ai_data *ai_data_get(struct player *pplayer)
465
struct ai_data *ai = &aidata[pplayer->player_no];
557
struct ai_data *ai = &aidata[player_index(pplayer)];
467
559
if (ai->num_continents != map.num_continents
468
560
|| ai->num_oceans != map.num_oceans) {
469
561
/* we discovered more continents, recalculate! */
470
ai_data_turn_done(pplayer);
471
ai_data_turn_init(pplayer);
562
ai_data_phase_done(pplayer);
563
ai_data_phase_init(pplayer, FALSE);
476
568
/**************************************************************************
569
Like ai_data_get, but no side effects.
570
**************************************************************************/
571
const struct ai_dip_intel *ai_diplomacy_get(const struct player *pplayer,
572
const struct player *aplayer)
574
const struct ai_data *ai = &aidata[player_index(pplayer)];
576
return &ai->diplomacy.player_intel[player_index(aplayer)];
579
/**************************************************************************
477
580
Initialize with sane values.
478
581
**************************************************************************/
479
582
void ai_data_init(struct player *pplayer)
481
struct ai_data *ai = &aidata[pplayer->player_no];
584
struct ai_data *ai = &aidata[player_index(pplayer)];
484
587
ai->govt_reeval = 0;
485
588
ai->government_want = fc_realloc(ai->government_want,
486
((game.government_count + 1)
589
((game.control.government_count + 1)
487
590
* sizeof(*ai->government_want)));
488
591
memset(ai->government_want, 0,
489
(game.government_count + 1) * sizeof(*ai->government_want));
592
(game.control.government_count + 1) * sizeof(*ai->government_want));
491
ai->diplomacy.target = NULL;
492
595
ai->diplomacy.strategy = WIN_OPEN;
493
596
ai->diplomacy.timer = 0;
494
ai->diplomacy.countdown = 0;
495
597
ai->diplomacy.love_coeff = 4; /* 4% */
496
ai->diplomacy.love_incr = MAX_AI_LOVE * 4 / 100;
497
ai->diplomacy.req_love_for_peace = MAX_AI_LOVE * 8 / 100;
498
ai->diplomacy.req_love_for_alliance = MAX_AI_LOVE * 16 / 100;
499
ai->diplomacy.req_love_for_ceasefire = 0;
500
ai->diplomacy.alliance_leader = pplayer;
598
ai->diplomacy.love_incr = MAX_AI_LOVE * 3 / 100;
599
ai->diplomacy.req_love_for_peace = MAX_AI_LOVE / 8;
600
ai->diplomacy.req_love_for_alliance = MAX_AI_LOVE / 4;
502
602
for (i = 0; i < MAX_NUM_PLAYERS + MAX_NUM_BARBARIANS; i++) {
503
603
ai->diplomacy.player_intel[i].spam = i % 5; /* pseudorandom */
604
ai->diplomacy.player_intel[i].countdown = -1;
605
ai->diplomacy.player_intel[i].war_reason = WAR_REASON_NONE;
504
606
ai->diplomacy.player_intel[i].distance = 1;
505
607
ai->diplomacy.player_intel[i].ally_patience = 0;
506
608
pplayer->ai.love[i] = 1;