Freeciv21
Develop your civilization from humble roots to a global empire
aihand.cpp
Go to the documentation of this file.
1 /*__ ___ ***************************************
2 / \ / \ Copyright (c) 1996-2020 Freeciv21 and Freeciv
3 \_ \ / __/ contributors. This file is part of Freeciv21.
4  _\ \ / /__ Freeciv21 is free software: you can redistribute it
5  \___ \____/ __/ and/or modify it under the terms of the GNU General
6  \_ _/ Public License as published by the Free Software
7  | @ @ \_ Foundation, either version 3 of the License,
8  | or (at your option) any later version.
9  _/ /\ You should have received a copy of the GNU
10  /o) (o/\ \_ General Public License along with Freeciv21.
11  \_____/ / If not, see https://www.gnu.org/licenses/.
12  \____/ ********************************************************/
13 
14 // utility
15 #include "distribute.h"
16 #include "log.h"
17 #include "shared.h"
18 #include "timing.h"
19 
20 // common
21 #include "city.h"
22 #include "game.h"
23 #include "government.h"
24 #include "nation.h"
25 #include "packets.h"
26 #include "player.h"
27 #include "research.h"
28 #include "unit.h"
29 #include "victory.h"
30 
31 /* common/aicore */
32 #include "cm.h"
33 
34 // server
35 #include "cityturn.h"
36 #include "plrhand.h"
37 #include "sernet.h"
38 #include "spacerace.h"
39 #include "srv_log.h"
40 
41 /* server/advisors */
42 #include "advdata.h"
43 #include "advspace.h"
44 
45 // ai
46 #include "handicaps.h"
47 
48 /* ai/default */
49 #include "aidata.h"
50 #include "ailog.h"
51 #include "aiplayer.h"
52 #include "aitech.h"
53 #include "aitools.h"
54 #include "aiunit.h"
55 #include "daicity.h"
56 #include "daidiplomacy.h"
57 #include "daimilitary.h"
58 
59 #include "aihand.h"
60 
75 #define LOGLEVEL_TAX LOG_DEBUG
76 
77 /* When setting rates, we accept negative balance if we have a lot of
78  * gold/bulb reserves. This is how long time gold/bulb reserves should last.
79  * For city grow due to rapture turns_for_rapture is added. */
80 #define AI_GOLD_RESERVE_MIN_TURNS 10
81 #define AI_BULBS_RESERVE_MIN_TURNS 10
82 
83 /* This factor is used for the delta between the estimated and real income to
84  * get 'real' rates for the AI player. */
85 #define PCT_DELTA_TAX 50
86 #define PCT_DELTA_SCI 10
87 
91 static void dai_manage_spaceship(struct player *pplayer)
92 {
94  if (pplayer->spaceship.state == SSHIP_STARTED) {
95  adv_spaceship_autoplace(pplayer, &pplayer->spaceship);
96 
97  // if we have built the best possible spaceship -- AJS 19990610
98  if ((pplayer->spaceship.structurals == NUM_SS_STRUCTURALS)
99  && (pplayer->spaceship.components == NUM_SS_COMPONENTS)
100  && (pplayer->spaceship.modules == NUM_SS_MODULES)) {
101  handle_spaceship_launch(pplayer);
102  }
103  }
104  }
105 }
106 
111 void dai_calc_data(const struct player *pplayer, int *trade, int *expenses,
112  int *income)
113 {
114  int d0;
115  if (!income) { // assign dummy instead null
116  income = &d0;
117  };
118  if (!trade) {
119  trade = &d0;
120  };
121  if (!expenses) {
122  expenses = &d0;
123  }
124  *trade = 0;
125  *expenses = 0;
126  *income = 0;
127 
128  // Find total trade surplus and gold expenses
129  city_list_iterate(pplayer->cities, pcity)
130  {
131  *trade += pcity->surplus[O_TRADE];
132  *expenses += pcity->usage[O_GOLD];
133  *income += pcity->surplus[O_GOLD];
134  }
136 
137  switch (game.info.gold_upkeep_style) {
138  case GOLD_UPKEEP_CITY:
139  break;
140  case GOLD_UPKEEP_MIXED:
141  case GOLD_UPKEEP_NATION:
142  // Account for units with gold upkeep paid for by the nation.
143  unit_list_iterate(pplayer->units, punit)
144  {
145  *expenses += punit->upkeep[O_GOLD];
146  }
148  break;
149  }
150 }
151 
156 
161 };
162 
163 #define RATE_NOT_SET -1
164 #define RATE_VALID(_rate) (_rate != RATE_NOT_SET)
165 #define RATE_REMAINS(_rates) \
166  MAX(0, 100 - _rates[AI_RATE_SCI] - _rates[AI_RATE_TAX] \
167  - _rates[AI_RATE_LUX])
168 
198 static void dai_manage_taxes(struct ai_type *ait, struct player *pplayer)
199 {
200  int maxrate = (has_handicap(pplayer, H_RATES)
201  ? get_player_bonus(pplayer, EFT_MAX_RATES)
202  : 100);
203  struct research *research = research_get(pplayer);
204  enum celebration celebrate = AI_CELEBRATION_UNCHECKED;
205  struct adv_data *ai = adv_data_get(pplayer, nullptr);
206  int can_celebrate = 0, total_cities = 0;
207  int trade = 0; // total amount of trade generated
208  int expenses = 0; // total amount of gold upkeep
209  int income = 0; // total amount of gold income
210  int turns_for_rapture; // additional reserve needed for rapture
211  int rates_save[AI_RATE_COUNT], rates[AI_RATE_COUNT], result[AI_RATE_COUNT];
212  int rate_tax_min = RATE_NOT_SET;
213  int rate_tax_balance = RATE_NOT_SET;
214  int rate_sci_min = RATE_NOT_SET;
215  int rate_sci_balance = RATE_NOT_SET;
216  int rate_lux_min_celebrate = maxrate;
217  int delta_tax = 0, delta_sci = 0;
218 
219  civtimer *taxtimer = nullptr;
220 
221  struct cm_parameter cmp;
222 
223  if (!game.info.changeable_budget) {
224  return; // This ruleset does not support changing national budget.
225  }
226 
227  maxrate = CLIP(34, maxrate, 100);
228 
230  return; // This government does not support changing national budget.
231  }
232 
233  taxtimer = timer_new(TIMER_CPU, TIMER_DEBUG);
234  timer_start(taxtimer);
235 
236  // City parameters needed for celebrations.
238  cmp.require_happy = true; // note this one
239  cmp.allow_disorder = false;
240  cmp.allow_specialists = true;
241  cmp.factor[O_FOOD] = 20;
242  cmp.minimal_surplus[O_GOLD] = -FC_INFINITY;
243 
244  /* Define reserve (gold/bulbs) needed for rapture (celebration). */
245  turns_for_rapture = ai->celebrate ? 0 : (game.info.rapturedelay + 1) * 3;
246 
247  log_base(LOGLEVEL_TAX, "%s [max] rate=%d", player_name(pplayer), maxrate);
248 
249  // Save AI rates.
250  rates_save[AI_RATE_SCI] = pplayer->economic.science;
251  rates_save[AI_RATE_TAX] = pplayer->economic.tax;
252  rates_save[AI_RATE_LUX] = pplayer->economic.luxury;
253 
254  log_base(LOGLEVEL_TAX, "%s [old] (Sci/Lux/Tax)=%d/%d/%d",
255  player_name(pplayer), rates_save[AI_RATE_SCI],
256  rates_save[AI_RATE_LUX], rates_save[AI_RATE_TAX]);
257 
258  // Get some data for the AI player.
259  dai_calc_data(pplayer, &trade, &expenses, &income);
260 
261  // Get the estimates for tax with the current rates.
262  distribute(trade, AI_RATE_COUNT, rates_save, result);
263 
264  // The delta between the estimate and the real value.
265  delta_tax = (result[AI_RATE_TAX] - expenses) - income;
266 
267  log_base(LOGLEVEL_TAX, "%s [tax] estimated=%d real=%d (delta=%d)",
268  player_name(pplayer), result[AI_RATE_TAX] - expenses, income,
269  delta_tax);
270 
271  /* Find minimum tax rate which gives us a positive balance for gold and
272  * science. We assume that we want science most and luxuries least here,
273  * and reverse or modify this assumption later.
274  * Furthermore, We assume our entire civilization is one big city, and
275  * distribute total income accordingly. This is a simplification that
276  * speeds up the code significantly. */
277 
278  // === Gold ===
279 
280  // First set tax (gold) to the minimal available number
281  rates[AI_RATE_SCI] = maxrate; // Assume we want science here
282  rates[AI_RATE_TAX] = MAX(0, 100 - maxrate * 2); // If maxrate < 50%
283  rates[AI_RATE_LUX] = (100 - rates[AI_RATE_SCI] - rates[AI_RATE_TAX]);
284 
285  /* Now find the minimum tax with positive gold balance
286  * Negative balance is acceptable if we have a lot of gold. */
287 
288  log_base(LOGLEVEL_TAX, "%s [tax] trade=%d gold=%d expenses=%d",
289  player_name(pplayer), trade, pplayer->economic.gold, expenses);
290 
291  while (rates[AI_RATE_TAX] <= maxrate && rates[AI_RATE_SCI] >= 0
292  && rates[AI_RATE_LUX] >= 0) {
293  bool refill_coffers = pplayer->economic.gold < dai_gold_reserve(pplayer);
294  int balance_tax, balance_tax_min;
295 
296  distribute(trade, AI_RATE_COUNT, rates, result);
297 
298  /* Consider the delta between the result and the real value from the
299  * last turn to get better estimates. */
300  balance_tax =
301  (result[AI_RATE_TAX] - expenses) - delta_tax * PCT_DELTA_TAX / 100;
302  balance_tax_min = -(pplayer->economic.gold
303  / (AI_GOLD_RESERVE_MIN_TURNS + turns_for_rapture));
304 
305  log_base(LOGLEVEL_TAX, "%s [tax] Tax=%d income=%d", player_name(pplayer),
306  rates[AI_RATE_TAX], balance_tax);
307 
308  if (!RATE_VALID(rate_tax_min) && balance_tax > balance_tax_min) {
309  // Just slightly negative; we can afford that for a while
310  rate_tax_min = rates[AI_RATE_TAX];
311  }
312 
313  if (expenses == 0 || balance_tax > 0) {
314  // Ok, got no expenses or positive balance
315  if (refill_coffers && rates[AI_RATE_TAX] < maxrate) {
316  // Need to refill coffers, increase tax a bit
317  rates[AI_RATE_TAX] += 10;
318  if (rates[AI_RATE_LUX] > 0) {
319  rates[AI_RATE_LUX] -= 10;
320  } else {
321  rates[AI_RATE_SCI] -= 10;
322  }
323 
324  log_base(LOGLEVEL_TAX, "%s [tax] Tax=%d to refill coffers",
325  player_name(pplayer), rates[AI_RATE_TAX]);
326  }
327 
328  // This is the minimum rate for a balanced treasury (gold).
329  rate_tax_balance = rates[AI_RATE_TAX];
330 
331  // Done! Break the while loop
332  break;
333  }
334 
335  // Negative balance. Unacceptable - increase tax.
336  rates[AI_RATE_TAX] += 10;
337  if (rates[AI_RATE_LUX] > 0) {
338  rates[AI_RATE_LUX] -= 10;
339  } else {
340  rates[AI_RATE_SCI] -= 10;
341  }
342  }
343 
344  // If no minimum value was found for the tax use the maximum tax rate.
345  rate_tax_min = RATE_VALID(rate_tax_min) ? rate_tax_min : maxrate;
346 
347  log_base(LOGLEVEL_TAX, "%s [tax] min=%d balanced=%d", player_name(pplayer),
348  rate_tax_min, rate_tax_balance);
349 
350  // === Science ===
351 
352  if (game.info.tech_upkeep_style != TECH_UPKEEP_NONE) {
353  // Tech upkeep activated.
354  int tech_upkeep = player_tech_upkeep(pplayer);
355  int bulbs_researched = research->bulbs_researched;
356 
357  // The delta between the estimate and the real value.
358  delta_sci = (result[AI_RATE_SCI] - tech_upkeep)
359  - pplayer->server.bulbs_last_turn;
360  log_base(LOGLEVEL_TAX, "%s [sci] estimated=%d real=%d (delta=%d)",
361  player_name(pplayer), result[AI_RATE_SCI] - tech_upkeep,
362  pplayer->server.bulbs_last_turn, delta_sci);
363 
364  log_base(LOGLEVEL_TAX, "%s [sci] trade=%d bulbs=%d upkeep=%d",
365  player_name(pplayer), trade, research->bulbs_researched,
366  tech_upkeep);
367 
368  rates[AI_RATE_TAX] = maxrate; // Assume we want gold here
369  rates[AI_RATE_SCI] = MAX(0, 100 - maxrate * 2);
370  rates[AI_RATE_LUX] = (100 - rates[AI_RATE_SCI] - rates[AI_RATE_TAX]);
371 
372  /* Now find the minimum science tax with positive bulbs balance
373  * Negative balance is acceptable if we have a lots of bulbs. */
374  while (rates[AI_RATE_SCI] <= maxrate && rates[AI_RATE_TAX] >= 0
375  && rates[AI_RATE_LUX] >= 0) {
376  int balance_sci, balance_sci_min;
377 
378  distribute(trade, AI_RATE_COUNT, rates, result);
379 
380  /* Consider the delta between the result and the real value from the
381  * last turn. */
382  balance_sci = (result[AI_RATE_SCI] - tech_upkeep)
383  - delta_sci * PCT_DELTA_SCI / 100;
384  balance_sci_min =
385  -(bulbs_researched
386  / (AI_BULBS_RESERVE_MIN_TURNS + turns_for_rapture));
387  log_base(LOGLEVEL_TAX, "%s [sci] Sci=%d research=%d",
388  player_name(pplayer), rates[AI_RATE_SCI], balance_sci);
389 
390  if (!RATE_VALID(rate_sci_min) && balance_sci > balance_sci_min) {
391  // Just slightly negative, if we can afford that for a while
392  rate_sci_min = rates[AI_RATE_SCI];
393  }
394 
395  if (tech_upkeep == 0 || balance_sci > 0) {
396  // Ok, got no expenses or positive balance
397  rate_sci_balance = rates[AI_RATE_SCI];
398 
399  // Done! Break the while loop
400  break;
401  }
402 
403  /* Negative balance. Unacceptable - increase science.*/
404  rates[AI_RATE_SCI] += 10;
405  if (rates[AI_RATE_LUX] > 0) {
406  rates[AI_RATE_LUX] -= 10;
407  } else {
408  rates[AI_RATE_TAX] -= 10;
409  }
410  }
411 
412  /* If no minimum value was found for the science use the maximum science
413  * rate. */
414  rate_sci_min = RATE_VALID(rate_sci_min) ? rate_sci_min : maxrate;
415 
416  log_base(LOGLEVEL_TAX, "%s [sci] min=%d balanced=%d",
417  player_name(pplayer), rate_sci_min, rate_sci_balance);
418  } else {
419  // No tech upkeep - minimum science value is 0.
420  rate_sci_min = 0;
421  }
422 
423  // === Luxury ===
424 
425  // Should (and can) we celebrate?
426  /* TODO: In the future, we should check if we should
427  * celebrate for other reasons than growth. Currently
428  * this is ignored. Maybe we need ruleset AI hints. */
429  // TODO: Allow celebrate individual cities? No modpacks use this yet.
430  if (get_player_bonus(pplayer, EFT_RAPTURE_GROW) > 0
431  && !has_handicap(pplayer, H_AWAY)
432  && 100 > rate_tax_min + rate_sci_min) {
433  celebrate = AI_CELEBRATION_NO;
434 
435  /* Set the minimum tax for a positive science and gold balance and use
436  * the remaining trade goods for luxuries (max. luxuries). */
437  rates[AI_RATE_SCI] = rate_sci_min;
438  rates[AI_RATE_TAX] = rate_tax_min;
439  rates[AI_RATE_LUX] = 0;
440 
441  rates[AI_RATE_LUX] =
442  MIN(maxrate, rates[AI_RATE_LUX] + RATE_REMAINS(rates));
443  rates[AI_RATE_TAX] =
444  MIN(maxrate, rates[AI_RATE_TAX] + RATE_REMAINS(rates));
445  rates[AI_RATE_SCI] =
446  MIN(maxrate, rates[AI_RATE_SCI] + RATE_REMAINS(rates));
447 
448  // Temporary set the new rates.
449  pplayer->economic.luxury = rates[AI_RATE_LUX];
450  pplayer->economic.tax = rates[AI_RATE_TAX];
451  pplayer->economic.science = rates[AI_RATE_SCI];
452 
453  // Check if we celebrate - the city state must be restored at the end!
454  city_list_iterate(pplayer->cities, pcity)
455  {
456  auto cmr = cm_result_new(pcity);
457  struct ai_city *city_data = def_ai_city_data(pcity, ait);
458 
459  cm_query_result(pcity, &cmp, cmr, false); // burn some CPU
460 
461  total_cities++;
462 
463  if (cmr->found_a_valid && pcity->surplus[O_FOOD] > 0
464  && city_size_get(pcity) >= game.info.celebratesize
465  && city_can_grow_to(pcity, city_size_get(pcity) + 1)) {
466  city_data->celebrate = true;
467  can_celebrate++;
468  } else {
469  city_data->celebrate = false;
470  }
471  }
473 
474  // If more than half our cities can celebrate, go for it!
475  if (can_celebrate * 2 > total_cities) {
477  rate_lux_min_celebrate = pplayer->economic.luxury;
479  "%s [lux] celebration possible (Sci/Lux/Tax)>="
480  "%d/%d/%d (%d of %d cities)",
481  player_name(pplayer), rate_sci_min, rate_lux_min_celebrate,
482  rate_tax_min, can_celebrate, total_cities);
483  } else {
484  log_base(LOGLEVEL_TAX, "%s [lux] no celebration: only %d of %d cities",
485  player_name(pplayer), can_celebrate, total_cities);
486  }
487  }
488 
489  if (celebrate != AI_CELEBRATION_YES) {
490  /* TODO: Calculate a minimum luxury tax rate.
491  * Add general luxury code here. */
492  }
493 
494  // === Set the rates. ===
495 
496  // Reset rates values.
497  rates[AI_RATE_SCI] = 0;
498  rates[AI_RATE_TAX] = 0;
499  rates[AI_RATE_LUX] = 0;
500 
501  // Now decide that to do ...
503  && rate_tax_min + rate_sci_min + rate_lux_min_celebrate <= 100) {
504  // Celebration!
505  rates[AI_RATE_SCI] = rate_sci_min;
506  rates[AI_RATE_TAX] = rate_tax_min;
507  rates[AI_RATE_LUX] = rate_lux_min_celebrate;
508 
509  log_base(LOGLEVEL_TAX, "%s [res] celebration! (Sci/Lux/Tax)>=%d/%d/%d",
510  player_name(pplayer), rates[AI_RATE_SCI], rates[AI_RATE_LUX],
511  rates[AI_RATE_TAX]);
512  } else {
513  // No celebration
514  celebrate =
516 
517  if (RATE_VALID(rate_tax_balance)) {
518  if (RATE_VALID(rate_sci_balance)) {
519  if (100 >= rate_tax_balance + rate_sci_balance) {
520  // A balanced treasury and research.
521  rates[AI_RATE_SCI] = rate_sci_balance;
522  rates[AI_RATE_TAX] = rate_tax_balance;
523 
525  "%s [res] balanced! (Sci/Lux/Tax)>=%d/%d/%d",
526  player_name(pplayer), rates[AI_RATE_SCI],
527  rates[AI_RATE_LUX], rates[AI_RATE_TAX]);
528  } else {
529  // Try to keep all tech and get as much gold as possible.
530  rates[AI_RATE_SCI] = rate_sci_balance;
531  rates[AI_RATE_TAX] = MIN(maxrate, RATE_REMAINS(rates));
532 
534  "%s [res] balanced sci! tax? "
535  "(Sci/Lux/Tax)>=%d/%d/%d",
536  player_name(pplayer), rates[AI_RATE_SCI],
537  rates[AI_RATE_LUX], rates[AI_RATE_TAX]);
538  }
539  } else {
540  // A balanced tax and as much science as possible.
541  rates[AI_RATE_TAX] = rate_tax_balance;
542  rates[AI_RATE_SCI] = MIN(maxrate, RATE_REMAINS(rates));
543 
545  "%s [res] balanced tax! sci? "
546  "(Sci/Lux/Tax)>=%d/%d/%d",
547  player_name(pplayer), rates[AI_RATE_SCI],
548  rates[AI_RATE_LUX], rates[AI_RATE_TAX]);
549  }
550  } else if (RATE_VALID(rate_sci_balance)) {
551  // Try to keep all techs and get as much gold as possible.
552  rates[AI_RATE_SCI] = rate_sci_balance;
553  rates[AI_RATE_TAX] = MIN(maxrate, RATE_REMAINS(rates));
554 
556  "%s [res] balanced sci! tax? "
557  "(Sci/Lux/Tax)>=%d/%d/%d",
558  player_name(pplayer), rates[AI_RATE_SCI], rates[AI_RATE_LUX],
559  rates[AI_RATE_TAX]);
560  } else {
561  // We need more trade to get a positive gold and science balance.
562  if (!adv_wants_science(pplayer) || dai_on_war_footing(ait, pplayer)) {
563  /* Go for gold (improvements and units) and risk the loss of a
564  * tech. */
565  rates[AI_RATE_TAX] = maxrate;
566  rates[AI_RATE_SCI] = MIN(maxrate, RATE_REMAINS(rates));
567 
569  "%s [res] risk of tech loss! (Sci/Lux/Tax)>="
570  "%d/%d/%d",
571  player_name(pplayer), rates[AI_RATE_SCI],
572  rates[AI_RATE_LUX], rates[AI_RATE_TAX]);
573  } else {
574  // Go for science and risk the loss of improvements or units.
575  rates[AI_RATE_SCI] = MAX(maxrate, rate_sci_min);
576  rates[AI_RATE_TAX] = MIN(maxrate, RATE_REMAINS(rates));
577 
579  "%s [res] risk of empty treasury! "
580  "(Sci/Lux/Tax)>=%d/%d/%d",
581  player_name(pplayer), rates[AI_RATE_SCI],
582  rates[AI_RATE_LUX], rates[AI_RATE_TAX]);
583  };
584  }
585  }
586 
587  // Put the remaining to tax or science.
588  if (!adv_wants_science(pplayer)) {
589  rates[AI_RATE_TAX] =
590  MIN(maxrate, rates[AI_RATE_TAX] + RATE_REMAINS(rates));
591  rates[AI_RATE_LUX] =
592  MIN(maxrate, rates[AI_RATE_LUX] + RATE_REMAINS(rates));
593  rates[AI_RATE_SCI] =
594  MIN(maxrate, rates[AI_RATE_SCI] + RATE_REMAINS(rates));
595  } else if (dai_on_war_footing(ait, pplayer)) {
596  rates[AI_RATE_TAX] =
597  MIN(maxrate, rates[AI_RATE_TAX] + RATE_REMAINS(rates));
598  rates[AI_RATE_SCI] =
599  MIN(maxrate, rates[AI_RATE_SCI] + RATE_REMAINS(rates));
600  rates[AI_RATE_LUX] =
601  MIN(maxrate, rates[AI_RATE_LUX] + RATE_REMAINS(rates));
602  } else {
603  rates[AI_RATE_SCI] =
604  MIN(maxrate, rates[AI_RATE_SCI] + RATE_REMAINS(rates));
605  rates[AI_RATE_TAX] =
606  MIN(maxrate, rates[AI_RATE_TAX] + RATE_REMAINS(rates));
607  rates[AI_RATE_LUX] =
608  MIN(maxrate, rates[AI_RATE_LUX] + RATE_REMAINS(rates));
609  }
610 
611  // Check and set the calculated rates.
612  fc_assert_ret(0 <= rates[AI_RATE_SCI] && rates[AI_RATE_SCI] <= maxrate);
613  fc_assert_ret(0 <= rates[AI_RATE_TAX] && rates[AI_RATE_TAX] <= maxrate);
614  fc_assert_ret(0 <= rates[AI_RATE_LUX] && rates[AI_RATE_LUX] <= maxrate);
615  fc_assert_ret(rates[AI_RATE_SCI] + rates[AI_RATE_TAX] + rates[AI_RATE_LUX]
616  == 100);
617 
618  log_base(LOGLEVEL_TAX, "%s [new] (Sci/Lux/Tax)=%d/%d/%d",
619  player_name(pplayer), rates[AI_RATE_SCI], rates[AI_RATE_LUX],
620  rates[AI_RATE_TAX]);
621 
622  pplayer->economic.science = rates[AI_RATE_SCI];
623  pplayer->economic.tax = rates[AI_RATE_TAX];
624  pplayer->economic.luxury = rates[AI_RATE_LUX];
625 
626  // === Cleanup ===
627 
628  // Cancel all celebrations from the last turn.
629  ai->celebrate = false;
630 
631  // Now do celebrate or reset the city states if needed.
632  if (celebrate == AI_CELEBRATION_YES) {
633  log_base(LOGLEVEL_TAX, "*** %s CELEBRATES! ***", player_name(pplayer));
634 
635  // We do celebrate!
636  ai->celebrate = true;
637 
638  auto gov_centers = player_gov_centers(pplayer);
639 
640  city_list_iterate(pplayer->cities, pcity)
641  {
642  auto cmr = cm_result_new(pcity);
643 
644  if (def_ai_city_data(pcity, ait)->celebrate) {
645  log_base(LOGLEVEL_TAX, "setting %s to celebrate",
646  city_name_get(pcity));
647  cm_query_result(pcity, &cmp, cmr, false);
648  if (cmr->found_a_valid) {
649  apply_cmresult_to_city(pcity, cmr);
650  city_refresh_from_main_map(pcity, nullptr, gov_centers);
651  if (!city_happy(pcity)) {
652  CITY_LOG(LOG_ERROR, pcity, "is NOT happy when it should be!");
653  }
654  } else {
655  CITY_LOG(LOG_ERROR, pcity, "has NO valid state!");
656  }
657  }
658  }
660  } else if (celebrate == AI_CELEBRATION_NO) {
661  auto gov_centers = player_gov_centers(pplayer);
662  city_list_iterate(pplayer->cities, pcity)
663  {
664  /* KLUDGE: Must refresh to restore the original values which
665  * were clobbered in cm_query_result(), after the national budget
666  * were changed. */
667  city_refresh_from_main_map(pcity, nullptr, gov_centers);
668  }
670  }
671 
672  send_player_info_c(pplayer, pplayer->connections);
673 
674  timer_stop(taxtimer);
675  qCDebug(timers_category,
676  "Tax calculation for %s (player %d) in %.3f "
677  "seconds.",
678  player_name(pplayer), player_index(pplayer),
679  timer_read_seconds(taxtimer));
680  timer_destroy(taxtimer);
681 }
682 #undef RATE_NOT_SET
683 #undef RATE_VALID
684 #undef RATE_REMAINS
685 
689 static void dai_manage_government(struct ai_type *ait,
690  struct player *pplayer)
691 {
692  struct adv_data *adv = adv_data_get(pplayer, nullptr);
693 
694  if (!pplayer->is_alive || has_handicap(pplayer, H_AWAY)) {
695  return;
696  }
697 
698  if (adv->goal.revolution != government_of_player(pplayer)) {
699  dai_government_change(pplayer, adv->goal.revolution); // change
700  }
701 
702  // Crank up tech want
703  if (adv->goal.govt.req == A_UNSET
704  || research_invention_state(research_get(pplayer), adv->goal.govt.req)
705  == TECH_KNOWN) {
706  return; // already got it!
707  } else if (adv->goal.govt.val > 0) {
708  /* We have few cities in the beginning, compensate for this to ensure
709  * that we are sufficiently forward-looking. */
710  int want = MAX(adv->goal.govt.val, 100);
711  struct nation_type *pnation = nation_of_player(pplayer);
712  struct ai_plr *plr_data = def_ai_player_data(pplayer, ait);
713 
714  if (government_of_player(pplayer)
715  == init_government_of_nation(pnation)) {
716  /* Default government is the crappy one we start in (like Despotism).
717  * We want something better pretty soon! */
718  want += 25 * game.info.turn;
719  }
720  plr_data->tech_want[adv->goal.govt.req] += want;
721  TECH_LOG(ait, LOG_DEBUG, pplayer, advance_by_number(adv->goal.govt.req),
722  "dai_manage_government() + %d for %s", want,
723  government_rule_name(adv->goal.govt.gov));
724  }
725 }
726 
731 void dai_do_first_activities(struct ai_type *ait, struct player *pplayer)
732 {
734  dai_assess_danger_player(ait, pplayer, &(wld.map));
735  /* TODO: Make assess_danger save information on what is threatening
736  * us and make dai_manage_units and Co act upon this information, trying
737  * to eliminate the source of danger */
738 
740  dai_manage_units(ait, pplayer);
742  // STOP. Everything else is at end of turn.
743 
745 
746  flush_packets(); // AIs can be such spammers...
747 }
748 
757 void dai_do_last_activities(struct ai_type *ait, struct player *pplayer)
758 {
760  dai_clear_tech_wants(ait, pplayer);
761 
762  dai_manage_government(ait, pplayer);
763  dai_adjust_policies(ait, pplayer);
765  dai_manage_taxes(ait, pplayer);
768  dai_manage_cities(ait, pplayer);
771  dai_manage_tech(ait, pplayer);
773  dai_manage_spaceship(pplayer);
774 
776 }
struct adv_data * adv_data_get(struct player *pplayer, bool *caller_closes)
Return a pointer to our data.
Definition: advdata.cpp:591
bool adv_wants_science(struct player *pplayer)
Return whether science would help us at all.
Definition: advdata.cpp:1051
bool adv_spaceship_autoplace(struct player *pplayer, struct player_spaceship *ship)
Place all available spaceship components.
Definition: advspace.cpp:29
void dai_adjust_policies(struct ai_type *ait, struct player *pplayer)
Adjust multiplier values.
Definition: aidata.cpp:437
celebration
Definition: aihand.cpp:157
@ AI_CELEBRATION_NO
Definition: aihand.cpp:159
@ AI_CELEBRATION_UNCHECKED
Definition: aihand.cpp:158
@ AI_CELEBRATION_YES
Definition: aihand.cpp:160
void dai_calc_data(const struct player *pplayer, int *trade, int *expenses, int *income)
Returns the total amount of trade generated (trade) and total amount of gold needed as upkeep (expens...
Definition: aihand.cpp:111
static void dai_manage_government(struct ai_type *ait, struct player *pplayer)
Change the government form, if it can and there is a good reason.
Definition: aihand.cpp:689
#define RATE_VALID(_rate)
Definition: aihand.cpp:164
#define PCT_DELTA_TAX
Definition: aihand.cpp:85
#define RATE_NOT_SET
Definition: aihand.cpp:163
#define LOGLEVEL_TAX
A man builds a city With banks and cathedrals A man melts the sand so he can See the world outside A ...
Definition: aihand.cpp:75
void dai_do_last_activities(struct ai_type *ait, struct player *pplayer)
Activities to be done by AI after human turn.
Definition: aihand.cpp:757
static void dai_manage_taxes(struct ai_type *ait, struct player *pplayer)
Set tax/science/luxury rates.
Definition: aihand.cpp:198
#define RATE_REMAINS(_rates)
Definition: aihand.cpp:165
static void dai_manage_spaceship(struct player *pplayer)
Handle spaceship related stuff.
Definition: aihand.cpp:91
#define AI_GOLD_RESERVE_MIN_TURNS
Definition: aihand.cpp:80
@ AI_RATE_COUNT
Definition: aihand.cpp:155
@ AI_RATE_TAX
Definition: aihand.cpp:155
@ AI_RATE_SCI
Definition: aihand.cpp:155
@ AI_RATE_LUX
Definition: aihand.cpp:155
void dai_do_first_activities(struct ai_type *ait, struct player *pplayer)
Activities to be done by AI before human turn.
Definition: aihand.cpp:731
#define AI_BULBS_RESERVE_MIN_TURNS
Definition: aihand.cpp:81
#define PCT_DELTA_SCI
Definition: aihand.cpp:86
#define TECH_LOG(ait, _, pplayer, padvance, msg,...)
Definition: ailog.h:32
static struct ai_plr * def_ai_player_data(const struct player *pplayer, struct ai_type *deftype)
Definition: aiplayer.h:47
static struct ai_city * def_ai_city_data(const struct city *pcity, struct ai_type *deftype)
Definition: aiplayer.h:35
void dai_clear_tech_wants(struct ai_type *ait, struct player *pplayer)
Zero player tech wants.
Definition: aitech.cpp:604
void dai_manage_tech(struct ai_type *ait, struct player *pplayer)
Key AI research function.
Definition: aitech.cpp:335
void dai_government_change(struct player *pplayer, struct government *gov)
Change government, pretty fast...
Definition: aitools.cpp:1099
int dai_gold_reserve(struct player *pplayer)
Credits the AI wants to have in reserves.
Definition: aitools.cpp:1120
void dai_manage_units(struct ai_type *ait, struct player *pplayer)
Master manage unit function.
Definition: aiunit.cpp:2761
const char * city_name_get(const struct city *pcity)
Return the name of the city.
Definition: city.cpp:1077
bool city_can_grow_to(const struct city *pcity, int pop_size)
Return TRUE iff the city can grow to the given size.
Definition: city.cpp:1914
bool city_happy(const struct city *pcity)
Return TRUE iff the city is happy.
Definition: city.cpp:1531
static int cmp(int v1, int v2)
Compare two integer values, as required by qsort.
Definition: city.cpp:317
void city_refresh_from_main_map(struct city *pcity, bool *workers_map, const std::vector< city * > &gov_centers, const std::array< cached_waste, O_LAST > *pcwaste, const std::vector< std::array< int, O_LAST >> *pcsoutputs)
Refreshes the internal cached data in the city structure.
Definition: city.cpp:3052
citizens city_size_get(const struct city *pcity)
Get the city size.
Definition: city.cpp:1101
#define city_list_iterate(citylist, pcity)
Definition: city.h:482
#define city_list_iterate_end
Definition: city.h:484
void apply_cmresult_to_city(struct city *pcity, const std::unique_ptr< cm_result > &cmr)
Rearrange workers according to a cm_result struct.
Definition: cityturn.cpp:276
void cm_init_parameter(struct cm_parameter *dest)
Initialize the parameter to sane default values.
Definition: cm.cpp:2172
void cm_query_result(struct city *pcity, const struct cm_parameter *param, std::unique_ptr< cm_result > &result, bool negative_ok)
Wrapper that actually runs the branch & bound, and returns the best solution.
Definition: cm.cpp:2115
std::unique_ptr< cm_result > cm_result_new(struct city *pcity)
Create a new cm_result.
Definition: cm.cpp:330
void dai_manage_cities(struct ai_type *ait, struct player *pplayer)
One of the top level AI functions.
Definition: daicity.cpp:861
bool dai_on_war_footing(struct ai_type *ait, struct player *pplayer)
Are we going to be declaring war in a few turns time? If so, go on a war footing, and try to buy out ...
void dai_assess_danger_player(struct ai_type *ait, struct player *pplayer, const struct civ_map *dmap)
Call assess_danger() for all cities owned by pplayer.
void distribute(int number, int groups, int *ratios, int *result)
Distribute "number" elements into "groups" groups with ratios given by the elements in "ratios".
Definition: distribute.cpp:29
int get_player_bonus(const struct player *pplayer, enum effect_type effect_type)
Returns the effect bonus for a player.
Definition: effects.cpp:673
@ VC_SPACERACE
Definition: fc_types.h:1083
@ O_FOOD
Definition: fc_types.h:85
@ O_TRADE
Definition: fc_types.h:87
@ O_GOLD
Definition: fc_types.h:88
struct civ_game game
Definition: game.cpp:47
struct world wld
Definition: game.cpp:48
struct government * government_of_player(const struct player *pplayer)
Return the government of a player.
Definition: government.cpp:107
const char * government_rule_name(const struct government *pgovern)
Return the (untranslated) rule name of the government.
Definition: government.cpp:126
bool has_handicap(const struct player *pplayer, enum handicap_type htype)
AI players may have handicaps - allowing them to cheat or preventing them from using certain algorith...
Definition: handicaps.cpp:62
@ H_RATES
Definition: handicaps.h:22
@ H_AWAY
Definition: handicaps.h:18
constexpr auto LOG_DEBUG
Definition: log.h:27
#define fc_assert_ret(condition)
Definition: log.h:112
constexpr auto LOG_ERROR
Definition: log.h:23
#define log_base(level, message,...)
Definition: log.h:41
struct government * init_government_of_nation(const struct nation_type *pnation)
Returns initial government type for this nation.
Definition: nation.cpp:545
struct nation_type * nation_of_player(const struct player *pplayer)
Return the nation of a player.
Definition: nation.cpp:419
std::vector< city * > player_gov_centers(const struct player *pplayer)
Locate the player's government centers.
Definition: player.cpp:1264
int player_index(const struct player *pplayer)
Return the player index.
Definition: player.cpp:748
const char * player_name(const struct player *pplayer)
Return the leader name of the player.
Definition: player.cpp:816
void send_player_info_c(struct player *src, struct conn_list *dest)
Send information about player slot 'src', or all valid (i.e.
Definition: plrhand.cpp:1048
int player_tech_upkeep(const struct player *pplayer)
Calculate the bulb upkeep needed for all techs of a player.
Definition: research.cpp:1037
struct research * research_get(const struct player *pplayer)
Returns the research structure associated with the player.
Definition: research.cpp:110
enum tech_state research_invention_state(const struct research *presearch, Tech_type_id tech)
Returns state of the tech for current research.
Definition: research.cpp:609
void flush_packets()
Attempt to flush all information in the send buffers for upto 'netwait' seconds.
Definition: sernet.cpp:187
#define CLIP(lower, current, upper)
Definition: shared.h:51
#define MIN(x, y)
Definition: shared.h:49
#define FC_INFINITY
Definition: shared.h:32
#define MAX(x, y)
Definition: shared.h:48
void handle_spaceship_launch(struct player *pplayer)
Handle spaceship launch request.
Definition: spacerace.cpp:160
#define NUM_SS_MODULES
Definition: spaceship.h:86
#define NUM_SS_COMPONENTS
Definition: spaceship.h:85
#define NUM_SS_STRUCTURALS
Definition: spaceship.h:84
@ SSHIP_STARTED
Definition: spaceship.h:79
@ AIT_TAXES
Definition: srv_log.h:50
@ AIT_UNITS
Definition: srv_log.h:45
@ AIT_CITIES
Definition: srv_log.h:51
@ AIT_TECH
Definition: srv_log.h:55
@ AIT_ALL
Definition: srv_log.h:43
@ TIMER_STOP
Definition: srv_log.h:77
@ TIMER_START
Definition: srv_log.h:77
#define CITY_LOG(_, pcity, msg,...)
Definition: srv_log.h:80
#define TIMING_LOG(timer, activity)
Definition: srv_log.h:121
struct government * revolution
Definition: advdata.h:124
bool celebrate
Definition: advdata.h:131
struct adv_data::@89 goal
struct adv_data::@89::@91 govt
bool celebrate
Definition: daicity.h:54
Definition: aidata.h:63
adv_want tech_want[A_LAST+1]
Definition: aidata.h:95
Definition: ai.h:42
struct packet_game_info info
Definition: game.h:80
struct government * government_during_revolution
Definition: game.h:85
enum spaceship_state state
Definition: spaceship.h:105
Definition: player.h:231
struct city_list * cities
Definition: player.h:263
struct player::@65::@67 server
struct unit_list * units
Definition: player.h:264
struct conn_list * connections
Definition: player.h:280
bool is_alive
Definition: player.h:250
struct player_economic economic
Definition: player.h:266
struct player_spaceship spaceship
Definition: player.h:268
int bulbs_researched
Definition: research.h:46
struct civ_map map
Definition: world_object.h:21
struct advance * advance_by_number(const Tech_type_id atype)
Return the advance for the given advance index.
Definition: tech.cpp:94
#define A_UNSET
Definition: tech.h:41
void timer_destroy(civtimer *t)
Deletes timer.
Definition: timing.cpp:66
double timer_read_seconds(civtimer *t)
Read value from timer.
Definition: timing.cpp:137
civtimer * timer_new(enum timer_timetype type, enum timer_use use)
Allocate a new timer with specified "type" and "use".
Definition: timing.cpp:43
void timer_start(civtimer *t)
Start timing, adding to previous accumulated time if timer has not been cleared.
Definition: timing.cpp:95
void timer_stop(civtimer *t)
Stop timing, and accumulate time so far.
Definition: timing.cpp:116
#define TIMER_DEBUG
Definition: timing.h:39
@ TIMER_CPU
Definition: timing.h:20
#define unit_list_iterate(unitlist, punit)
Definition: unitlist.h:25
#define unit_list_iterate_end
Definition: unitlist.h:27
bool victory_enabled(enum victory_condition_type victory)
Whether victory condition is enabled.
Definition: victory.cpp:20