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.
7
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 ***********************************************************************/
13
14 #ifdef HAVE_CONFIG_H
15 #include <fc_config.h>
16 #endif
17
18 /* utility */
19 #include "distribute.h"
20 #include "log.h"
21 #include "shared.h"
22 #include "timing.h"
23
24 /* common */
25 #include "city.h"
26 #include "game.h"
27 #include "government.h"
28 #include "map.h"
29 #include "nation.h"
30 #include "packets.h"
31 #include "player.h"
32 #include "research.h"
33 #include "unit.h"
34 #include "victory.h"
35
36 /* common/aicore */
37 #include "cm.h"
38
39 /* server */
40 #include "citytools.h"
41 #include "cityturn.h"
42 #include "plrhand.h"
43 #include "sernet.h"
44 #include "spacerace.h"
45 #include "srv_log.h"
46 #include "unithand.h"
47
48 /* server/advisors */
49 #include "advdata.h"
50 #include "advspace.h"
51 #include "advtools.h"
52
53 /* ai */
54 #include "handicaps.h"
55
56 /* ai/default */
57 #include "aicity.h"
58 #include "aidata.h"
59 #include "ailog.h"
60 #include "aiplayer.h"
61 #include "aitech.h"
62 #include "aitools.h"
63 #include "aiunit.h"
64 #include "daidiplomacy.h"
65 #include "daimilitary.h"
66
67 #include "aihand.h"
68
69
70 /****************************************************************************
71 A man builds a city
72 With banks and cathedrals
73 A man melts the sand so he can
74 See the world outside
75 A man makes a car
76 And builds a road to run them on
77 A man dreams of leaving
78 but he always stays behind
79 And these are the days when our work has come assunder
80 And these are the days when we look for something other
81 /U2 Lemon.
82 ******************************************************************************/
83
84 #define LOGLEVEL_TAX LOG_DEBUG
85
86 /* When setting rates, we accept negative balance if we have a lot of
87 * gold/bulb reserves. This is how long time gold/bulb reserves should last.
88 * For city grow due to rapture turns_for_rapture is added. */
89 #define AI_GOLD_RESERVE_MIN_TURNS 10
90 #define AI_BULBS_RESERVE_MIN_TURNS 10
91
92 /* This factor is used for the delta between the estimated and real income to
93 * get 'real' rates for the AI player. */
94 #define PCT_DELTA_TAX 50
95 #define PCT_DELTA_SCI 10
96
97 /**************************************************************************
98 handle spaceship related stuff
99 **************************************************************************/
dai_manage_spaceship(struct player * pplayer)100 static void dai_manage_spaceship(struct player *pplayer)
101 {
102 if (victory_enabled(VC_SPACERACE)) {
103 if (pplayer->spaceship.state == SSHIP_STARTED) {
104 adv_spaceship_autoplace(pplayer, &pplayer->spaceship);
105
106 /* if we have built the best possible spaceship -- AJS 19990610 */
107 if ((pplayer->spaceship.structurals == NUM_SS_STRUCTURALS)
108 && (pplayer->spaceship.components == NUM_SS_COMPONENTS)
109 && (pplayer->spaceship.modules == NUM_SS_MODULES))
110 handle_spaceship_launch(pplayer);
111 }
112 }
113 }
114
115 /***************************************************************************
116 Returns the total amount of trade generated (trade), total amount of
117 gold needed as upkeep (expenses), and total amount of gold gained (income).
118 ***************************************************************************/
dai_calc_data(const struct player * pplayer,int * trade,int * expenses,int * income)119 void dai_calc_data(const struct player *pplayer, int *trade, int *expenses,
120 int *income)
121 {
122 if (NULL != trade) {
123 *trade = 0;
124 }
125 if (NULL != expenses) {
126 *expenses = 0;
127 }
128 if (NULL != income) {
129 *income = 0;
130 }
131
132 /* Find total trade surplus, gold expenses, and gold income */
133 city_list_iterate(pplayer->cities, pcity) {
134 if (NULL != trade) {
135 *trade += pcity->surplus[O_TRADE];
136 }
137
138 if (NULL != expenses) {
139 *expenses += pcity->usage[O_GOLD];
140 }
141
142 if (NULL != income) {
143 /* Also the immediately used gold is part of income, not only surplus */
144 *income += pcity->usage[O_GOLD] + pcity->surplus[O_GOLD];
145 }
146 } city_list_iterate_end;
147
148 switch (game.info.gold_upkeep_style) {
149 case GOLD_UPKEEP_CITY:
150 break;
151 case GOLD_UPKEEP_MIXED:
152 case GOLD_UPKEEP_NATION:
153 /* Account for units with gold upkeep paid for by the nation. */
154 unit_list_iterate(pplayer->units, punit) {
155 *expenses += punit->upkeep[O_GOLD];
156 } unit_list_iterate_end;
157 break;
158 }
159 }
160
161 /*****************************************************************************
162 Additional data needed for the AI rates calculation
163 *****************************************************************************/
164 enum {
165 AI_RATE_SCI = 0,
166 AI_RATE_TAX,
167 AI_RATE_LUX,
168 AI_RATE_COUNT
169 };
170
171 enum celebration {
172 AI_CELEBRATION_UNCHECKED,
173 AI_CELEBRATION_NO,
174 AI_CELEBRATION_YES,
175 };
176
177 #define RATE_NOT_SET -1
178 #define RATE_VALID(_rate) \
179 (_rate != RATE_NOT_SET)
180 #define RATE_REMAINS(_rates) \
181 MAX(0, 100 - _rates[AI_RATE_SCI] - _rates[AI_RATE_TAX] \
182 - _rates[AI_RATE_LUX])
183
184 /*****************************************************************************
185 Set tax/science/luxury rates.
186
187 TODO: Add general support for luxuries: select the luxury rate at which
188 all cities are content and the trade output (minus what is consumed by
189 luxuries) is maximal. For this we need some more information from the
190 city management code.
191
192 TODO: Audit the use of pplayer->ai.maxbuycost in the code elsewhere,
193 then add support for it here.
194
195 This function first determins the minimum tax rate (rate_tax_min) and a tax
196 rate for a balanced treasury (rate_tax_balance). Similarily, the science
197 rates are determiend (rate_sci_min and rate_sci_balance). Considering the
198 minimum rates for tax and science the chance for celebrations is checked. If
199 celebration is possible for more than half of the cities, the needed luxury
200 rate is saved as rate_lux_min_celebrate. For celebration some reserves are
201 defined (see turns_for_rapture).
202
203 At the end the results are compared and the rates are selected as:
204
205 1. Go for celebration.
206 2. Use a balanced science and treasury.
207 3. go for a balaned treasury / science.
208 4. Try to select the best (risk of tech loss or have to sell something).
209
210 At the end the remaining is divided on science/gold/luxury depending on the
211 AI settings.
212 *****************************************************************************/
dai_manage_taxes(struct ai_type * ait,struct player * pplayer)213 static void dai_manage_taxes(struct ai_type *ait, struct player *pplayer)
214 {
215 int maxrate = (has_handicap(pplayer, H_RATES)
216 ? get_player_bonus(pplayer, EFT_MAX_RATES) : 100);
217 struct research *research = research_get(pplayer);
218 enum celebration celebrate = AI_CELEBRATION_UNCHECKED;
219 struct adv_data *ai = adv_data_get(pplayer, NULL);
220 int can_celebrate = 0, total_cities = 0;
221 int trade = 0; /* total amount of trade generated */
222 int expenses = 0; /* total amount of gold upkeep */
223 int income = 0; /* total amount of gold income */
224 int turns_for_rapture; /* additional reserve needed for rapture */
225 int rates_save[AI_RATE_COUNT], rates[AI_RATE_COUNT], result[AI_RATE_COUNT];
226 int rate_tax_min = RATE_NOT_SET;
227 int rate_tax_balance = RATE_NOT_SET;
228 int rate_sci_min = RATE_NOT_SET;
229 int rate_sci_balance = RATE_NOT_SET;
230 int rate_lux_min_celebrate = maxrate;
231 int delta_tax = 0, delta_sci = 0;
232
233 #ifdef DEBUG_TIMERS
234 struct timer *taxtimer= NULL;
235 #endif
236
237 struct cm_parameter cmp;
238
239 if (!game.info.changable_tax) {
240 return; /* This ruleset does not support changing tax rates. */
241 }
242
243 maxrate = CLIP(34, maxrate, 100);
244
245 if (government_of_player(pplayer) == game.government_during_revolution) {
246 return; /* This government does not support changing tax rates. */
247 }
248
249 #ifdef DEBUG_TIMERS
250 taxtimer = timer_new(TIMER_CPU, TIMER_DEBUG);
251 timer_start(taxtimer);
252 #endif
253
254 /* City parameters needed for celebrations. */
255 cm_init_parameter(&cmp);
256 cmp.require_happy = TRUE; /* note this one */
257 cmp.allow_disorder = FALSE;
258 cmp.allow_specialists = TRUE;
259 cmp.factor[O_FOOD] = 20;
260 cmp.minimal_surplus[O_GOLD] = -FC_INFINITY;
261
262 /* Define reserve (gold/bulbs) needed for rapture (celebration). */
263 turns_for_rapture = ai->celebrate ? 0 : (game.info.rapturedelay + 1) * 3;
264
265 log_base(LOGLEVEL_TAX, "%s [max] rate=%d", player_name(pplayer), maxrate);
266
267 /* Save AI rates. */
268 rates_save[AI_RATE_SCI] = pplayer->economic.science;
269 rates_save[AI_RATE_TAX] = pplayer->economic.tax;
270 rates_save[AI_RATE_LUX] = pplayer->economic.luxury;
271
272 log_base(LOGLEVEL_TAX, "%s [old] (Sci/Lux/Tax)=%d/%d/%d",
273 player_name(pplayer), rates_save[AI_RATE_SCI],
274 rates_save[AI_RATE_LUX], rates_save[AI_RATE_TAX]);
275
276 /* Get some data for the AI player. */
277 dai_calc_data(pplayer, &trade, &expenses, &income);
278
279 /* Get the estimates for tax with the current rates. */
280 distribute(trade, AI_RATE_COUNT, rates_save, result);
281
282 /* The delta between the estimate and the real value. */
283 delta_tax = (result[AI_RATE_TAX] - expenses) - income;
284
285 log_base(LOGLEVEL_TAX, "%s [tax] estimated=%d real=%d (delta=%d)",
286 player_name(pplayer), result[AI_RATE_TAX] - expenses, income,
287 delta_tax);
288
289 /* Find minimum tax rate which gives us a positive balance for gold and
290 * science. We assume that we want science most and luxuries least here,
291 * and reverse or modify this assumption later.
292 * Furthermore, We assume our entire civilization is one big city, and
293 * distribute total income accordingly. This is a simplification that speeds
294 * up the code significantly. */
295
296 /* === Gold === */
297
298 /* First set tax (gold) to the minimal available number */
299 rates[AI_RATE_SCI] = maxrate; /* Assume we want science here */
300 rates[AI_RATE_TAX] = MAX(0, 100 - maxrate * 2); /* If maxrate < 50% */
301 rates[AI_RATE_LUX] = (100 - rates[AI_RATE_SCI] - rates[AI_RATE_TAX]);
302
303 /* Now find the minimum tax with positive gold balance
304 * Negative balance is acceptable if we have a lot of gold. */
305
306 log_base(LOGLEVEL_TAX, "%s [tax] trade=%d gold=%d expenses=%d",
307 player_name(pplayer), trade, pplayer->economic.gold, expenses);
308
309 while(rates[AI_RATE_TAX] <= maxrate
310 && rates[AI_RATE_SCI] >= 0
311 && rates[AI_RATE_LUX] >= 0) {
312 bool refill_coffers = pplayer->economic.gold < dai_gold_reserve(pplayer);
313 int balance_tax, balance_tax_min;
314
315 distribute(trade, AI_RATE_COUNT, rates, result);
316
317 /* Consider the delta between the result and the real value from the
318 * last turn to get better estimates. */
319 balance_tax = (result[AI_RATE_TAX] - expenses)
320 - delta_tax * PCT_DELTA_TAX / 100;
321 balance_tax_min = -(pplayer->economic.gold
322 / (AI_GOLD_RESERVE_MIN_TURNS + turns_for_rapture));
323
324 log_base(LOGLEVEL_TAX, "%s [tax] Tax=%d income=%d",
325 player_name(pplayer), rates[AI_RATE_TAX], balance_tax);
326
327 if (!RATE_VALID(rate_tax_min) && balance_tax > balance_tax_min) {
328 /* Just slightly negative; we can afford that for a while */
329 rate_tax_min = rates[AI_RATE_TAX];
330 }
331
332 if (expenses == 0 || balance_tax > 0) {
333 /* Ok, got no expenses or positive balance */
334 if (refill_coffers && rates[AI_RATE_TAX] < maxrate) {
335 /* Need to refill coffers, increase tax a bit */
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 log_base(LOGLEVEL_TAX, "%s [tax] Tax=%d to refill coffers",
344 player_name(pplayer), rates[AI_RATE_TAX]);
345 }
346
347 /* This is the minimum rate for a balanced treasury (gold). */
348 rate_tax_balance = rates[AI_RATE_TAX];
349
350 /* Done! Break the while loop */
351 break;
352 }
353
354 /* Negative balance. Unacceptable - increase tax. */
355 rates[AI_RATE_TAX] += 10;
356 if (rates[AI_RATE_LUX] > 0) {
357 rates[AI_RATE_LUX] -= 10;
358 } else {
359 rates[AI_RATE_SCI] -= 10;
360 }
361 }
362
363 /* If no minimum value was found for the tax use the maximum tax rate. */
364 rate_tax_min = RATE_VALID(rate_tax_min) ? rate_tax_min : maxrate;
365
366 log_base(LOGLEVEL_TAX, "%s [tax] min=%d balanced=%d", player_name(pplayer),
367 rate_tax_min, rate_tax_balance);
368
369 /* === Science === */
370
371 if (game.info.tech_upkeep_style != TECH_UPKEEP_NONE) {
372 /* Tech upkeep activated. */
373 int tech_upkeep = player_tech_upkeep(pplayer);
374 int bulbs_researched = research->bulbs_researched;
375
376 /* The delta between the estimate and the real value. */
377 delta_sci = (result[AI_RATE_SCI] - tech_upkeep)
378 - pplayer->server.bulbs_last_turn;
379 log_base(LOGLEVEL_TAX, "%s [sci] estimated=%d real=%d (delta=%d)",
380 player_name(pplayer),
381 result[AI_RATE_SCI] - tech_upkeep,
382 pplayer->server.bulbs_last_turn, delta_sci);
383
384 log_base(LOGLEVEL_TAX, "%s [sci] trade=%d bulbs=%d upkeep=%d",
385 player_name(pplayer), trade, research->bulbs_researched,
386 tech_upkeep);
387
388 rates[AI_RATE_TAX] = maxrate; /* Assume we want gold here */
389 rates[AI_RATE_SCI] = MAX(0, 100 - maxrate * 2);
390 rates[AI_RATE_LUX] = (100 - rates[AI_RATE_SCI] - rates[AI_RATE_TAX]);
391
392 /* Now find the minimum science tax with positive bulbs balance
393 * Negative balance is acceptable if we have a lots of bulbs. */
394 while(rates[AI_RATE_SCI] <= maxrate
395 && rates[AI_RATE_TAX] >= 0
396 && rates[AI_RATE_LUX] >= 0) {
397 int balance_sci, balance_sci_min;
398
399 distribute(trade, AI_RATE_COUNT, rates, result);
400
401 /* Consider the delta between the result and the real value from the
402 * last turn. */
403 balance_sci = (result[AI_RATE_SCI] - tech_upkeep)
404 - delta_sci * PCT_DELTA_SCI / 100;
405 balance_sci_min = -(bulbs_researched
406 / (AI_BULBS_RESERVE_MIN_TURNS + turns_for_rapture));
407 log_base(LOGLEVEL_TAX, "%s [sci] Sci=%d research=%d",
408 player_name(pplayer), rates[AI_RATE_SCI], balance_sci);
409
410 if (!RATE_VALID(rate_sci_min) && balance_sci > balance_sci_min) {
411 /* Just slightly negative, if we can afford that for a while */
412 rate_sci_min = rates[AI_RATE_SCI];
413 }
414
415 if (tech_upkeep == 0 || balance_sci > 0) {
416 /* Ok, got no expenses or positive balance */
417 rate_sci_balance = rates[AI_RATE_SCI];
418
419 /* Done! Break the while loop */
420 break;
421 }
422
423 /* Negative balance. Unacceptable - increase science.*/
424 rates[AI_RATE_SCI] += 10;
425 if (rates[AI_RATE_LUX] > 0) {
426 rates[AI_RATE_LUX] -= 10;
427 } else {
428 rates[AI_RATE_TAX] -= 10;
429 }
430 }
431
432 /* If no minimum value was found for the science use the maximum science
433 * rate. */
434 rate_sci_min = RATE_VALID(rate_sci_min) ? rate_sci_min : maxrate;
435
436 log_base(LOGLEVEL_TAX, "%s [sci] min=%d balanced=%d",
437 player_name(pplayer), rate_sci_min, rate_sci_balance);
438 } else {
439 /* No tech upkeep - minimum science value is 0. */
440 rate_sci_min = 0;
441 }
442
443 /* === Luxury === */
444
445 /* Should (and can) we celebrate? */
446 /* TODO: In the future, we should check if we should
447 * celebrate for other reasons than growth. Currently
448 * this is ignored. Maybe we need ruleset AI hints. */
449 /* TODO: Allow celebrate individual cities? No modpacks use this yet. */
450 if (get_player_bonus(pplayer, EFT_RAPTURE_GROW) > 0
451 && !has_handicap(pplayer, H_AWAY)
452 && 100 > rate_tax_min + rate_sci_min) {
453 celebrate = AI_CELEBRATION_NO;
454
455 /* Set the minimum tax for a positive science and gold balance and use the
456 * remaining trade goods for luxuries (max. luxuries). */
457 rates[AI_RATE_SCI] = rate_sci_min;
458 rates[AI_RATE_TAX] = rate_tax_min;
459 rates[AI_RATE_LUX] = 0;
460
461 rates[AI_RATE_LUX] = MIN(maxrate, rates[AI_RATE_LUX]
462 + RATE_REMAINS(rates));
463 rates[AI_RATE_TAX] = MIN(maxrate, rates[AI_RATE_TAX]
464 + RATE_REMAINS(rates));
465 rates[AI_RATE_SCI] = MIN(maxrate, rates[AI_RATE_SCI]
466 + RATE_REMAINS(rates));
467
468 /* Temporary set the new rates. */
469 pplayer->economic.luxury = rates[AI_RATE_LUX];
470 pplayer->economic.tax = rates[AI_RATE_TAX];
471 pplayer->economic.science = rates[AI_RATE_SCI];
472
473 /* Check if we celebrate - the city state must be restored at the end! */
474 city_list_iterate(pplayer->cities, pcity) {
475 struct cm_result *cmr = cm_result_new(pcity);
476 struct ai_city *city_data = def_ai_city_data(pcity, ait);
477
478 cm_clear_cache(pcity);
479 cm_query_result(pcity, &cmp, cmr, FALSE); /* burn some CPU */
480
481 total_cities++;
482
483 if (cmr->found_a_valid
484 && pcity->surplus[O_FOOD] > 0
485 && city_size_get(pcity) >= game.info.celebratesize
486 && city_can_grow_to(pcity, city_size_get(pcity) + 1)) {
487 city_data->celebrate = TRUE;
488 can_celebrate++;
489 } else {
490 city_data->celebrate = FALSE;
491 }
492 cm_result_destroy(cmr);
493 } city_list_iterate_end;
494
495 /* If more than half our cities can celebrate, go for it! */
496 if (can_celebrate * 2 > total_cities) {
497 celebrate = AI_CELEBRATION_YES;
498 rate_lux_min_celebrate = pplayer->economic.luxury;
499 log_base(LOGLEVEL_TAX, "%s [lux] celebration possible (Sci/Lux/Tax)>="
500 "%d/%d/%d (%d of %d cities)",
501 player_name(pplayer), rate_sci_min, rate_lux_min_celebrate,
502 rate_tax_min, can_celebrate, total_cities);
503 } else {
504 log_base(LOGLEVEL_TAX, "%s [lux] no celebration: only %d of %d cities",
505 player_name(pplayer), can_celebrate, total_cities);
506 }
507 }
508
509 if (celebrate != AI_CELEBRATION_YES) {
510 /* TODO: Calculate a minimum luxury tax rate.
511 * Add general luxury code here. */
512 }
513
514 /* === Set the rates. === */
515
516 /* Reset rates values. */
517 rates[AI_RATE_SCI] = 0;
518 rates[AI_RATE_TAX] = 0;
519 rates[AI_RATE_LUX] = 0;
520
521 /* Now decide that to do ... */
522 if (celebrate == AI_CELEBRATION_YES
523 && rate_tax_min + rate_sci_min + rate_lux_min_celebrate <= 100) {
524 /* Celebration! */
525 rates[AI_RATE_SCI] = rate_sci_min;
526 rates[AI_RATE_TAX] = rate_tax_min;
527 rates[AI_RATE_LUX] = rate_lux_min_celebrate;
528
529 log_base(LOGLEVEL_TAX, "%s [res] celebration! (Sci/Lux/Tax)>=%d/%d/%d",
530 player_name(pplayer), rates[AI_RATE_SCI], rates[AI_RATE_LUX],
531 rates[AI_RATE_TAX]);
532 } else {
533 /* No celebration */
534 celebrate = (celebrate == AI_CELEBRATION_YES) ? AI_CELEBRATION_NO
535 : celebrate;
536
537 if (RATE_VALID(rate_tax_balance)) {
538 if (RATE_VALID(rate_sci_balance)) {
539 if (100 >= rate_tax_balance + rate_sci_balance) {
540 /* A balanced treasury and research. */
541 rates[AI_RATE_SCI] = rate_sci_balance;
542 rates[AI_RATE_TAX] = rate_tax_balance;
543
544 log_base(LOGLEVEL_TAX, "%s [res] balanced! (Sci/Lux/Tax)>=%d/%d/%d",
545 player_name(pplayer), rates[AI_RATE_SCI],
546 rates[AI_RATE_LUX], rates[AI_RATE_TAX]);
547 } else {
548 /* Try to keep all tech and get as much gold as possible. */
549 rates[AI_RATE_SCI] = rate_sci_balance;
550 rates[AI_RATE_TAX] = MIN(maxrate, RATE_REMAINS(rates));
551
552 log_base(LOGLEVEL_TAX, "%s [res] balanced sci! tax? "
553 "(Sci/Lux/Tax)>=%d/%d/%d",
554 player_name(pplayer), rates[AI_RATE_SCI],
555 rates[AI_RATE_LUX], rates[AI_RATE_TAX]);
556 }
557 } else {
558 /* A balanced tax and as much science as possible. */
559 rates[AI_RATE_TAX] = rate_tax_balance;
560 rates[AI_RATE_SCI] = MIN(maxrate, RATE_REMAINS(rates));
561
562 log_base(LOGLEVEL_TAX, "%s [res] balanced tax! sci? "
563 "(Sci/Lux/Tax)>=%d/%d/%d",
564 player_name(pplayer), rates[AI_RATE_SCI],
565 rates[AI_RATE_LUX], rates[AI_RATE_TAX]);
566 }
567 } else if (RATE_VALID(rate_sci_balance)) {
568 /* Try to keep all techs and get as much gold as possible. */
569 rates[AI_RATE_SCI] = rate_sci_balance;
570 rates[AI_RATE_TAX] = MIN(maxrate, RATE_REMAINS(rates));
571
572 log_base(LOGLEVEL_TAX, "%s [res] balanced sci! tax? "
573 "(Sci/Lux/Tax)>=%d/%d/%d",
574 player_name(pplayer), rates[AI_RATE_SCI],
575 rates[AI_RATE_LUX], rates[AI_RATE_TAX]);
576 } else {
577 /* We need more trade to get a positive gold and science balance. */
578 if (!adv_wants_science(pplayer) || dai_on_war_footing(ait, pplayer)) {
579 /* Go for gold (improvements and units) and risk the loss of a
580 * tech. */
581 rates[AI_RATE_TAX] = maxrate;
582 rates[AI_RATE_SCI] = MIN(maxrate, RATE_REMAINS(rates));
583
584 log_base(LOGLEVEL_TAX, "%s [res] risk of tech loss! (Sci/Lux/Tax)>="
585 "%d/%d/%d", player_name(pplayer),
586 rates[AI_RATE_SCI], rates[AI_RATE_LUX],
587 rates[AI_RATE_TAX]);
588 } else {
589 /* Go for science and risk the loss of improvements or units. */
590 rates[AI_RATE_SCI] = MAX(maxrate, rate_sci_min);
591 rates[AI_RATE_TAX] = MIN(maxrate, RATE_REMAINS(rates));
592
593 log_base(LOGLEVEL_TAX, "%s [res] risk of empty treasury! "
594 "(Sci/Lux/Tax)>=%d/%d/%d",
595 player_name(pplayer), rates[AI_RATE_SCI],
596 rates[AI_RATE_LUX], rates[AI_RATE_TAX]);
597 };
598 }
599 }
600
601 /* Put the remaining to tax or science. */
602 if (!adv_wants_science(pplayer)) {
603 rates[AI_RATE_TAX] = MIN(maxrate, rates[AI_RATE_TAX]
604 + RATE_REMAINS(rates));
605 rates[AI_RATE_LUX] = MIN(maxrate, rates[AI_RATE_LUX]
606 + RATE_REMAINS(rates));
607 rates[AI_RATE_SCI] = MIN(maxrate, rates[AI_RATE_SCI]
608 + RATE_REMAINS(rates));
609 } else if (dai_on_war_footing(ait, pplayer)) {
610 rates[AI_RATE_TAX] = MIN(maxrate, rates[AI_RATE_TAX]
611 + RATE_REMAINS(rates));
612 rates[AI_RATE_SCI] = MIN(maxrate, rates[AI_RATE_SCI]
613 + RATE_REMAINS(rates));
614 rates[AI_RATE_LUX] = MIN(maxrate, rates[AI_RATE_LUX]
615 + RATE_REMAINS(rates));
616 } else {
617 rates[AI_RATE_SCI] = MIN(maxrate, rates[AI_RATE_SCI]
618 + RATE_REMAINS(rates));
619 rates[AI_RATE_TAX] = MIN(maxrate, rates[AI_RATE_TAX]
620 + RATE_REMAINS(rates));
621 rates[AI_RATE_LUX] = MIN(maxrate, rates[AI_RATE_LUX]
622 + RATE_REMAINS(rates));
623 }
624
625 /* Check and set the calculated rates. */
626 fc_assert_ret(0 <= rates[AI_RATE_SCI] && rates[AI_RATE_SCI] <= maxrate);
627 fc_assert_ret(0 <= rates[AI_RATE_TAX] && rates[AI_RATE_TAX] <= maxrate);
628 fc_assert_ret(0 <= rates[AI_RATE_LUX] && rates[AI_RATE_LUX] <= maxrate);
629 fc_assert_ret(rates[AI_RATE_SCI] + rates[AI_RATE_TAX] + rates[AI_RATE_LUX]
630 == 100);
631
632 log_base(LOGLEVEL_TAX, "%s [new] (Sci/Lux/Tax)=%d/%d/%d",
633 player_name(pplayer), rates[AI_RATE_SCI], rates[AI_RATE_LUX],
634 rates[AI_RATE_TAX]);
635
636 pplayer->economic.science = rates[AI_RATE_SCI];
637 pplayer->economic.tax = rates[AI_RATE_TAX];
638 pplayer->economic.luxury = rates[AI_RATE_LUX];
639
640 /* === Cleanup === */
641
642 /* Cancel all celebrations from the last turn. */
643 ai->celebrate = FALSE;
644
645 /* Now do celebrate or reset the city states if needed. */
646 if (celebrate == AI_CELEBRATION_YES) {
647 log_base(LOGLEVEL_TAX, "*** %s CELEBRATES! ***", player_name(pplayer));
648
649 /* We do celebrate! */
650 ai->celebrate = TRUE;
651
652 city_list_iterate(pplayer->cities, pcity) {
653 struct cm_result *cmr = cm_result_new(pcity);
654
655 if (def_ai_city_data(pcity, ait)->celebrate == TRUE) {
656 log_base(LOGLEVEL_TAX, "setting %s to celebrate", city_name_get(pcity));
657 cm_query_result(pcity, &cmp, cmr, FALSE);
658 if (cmr->found_a_valid) {
659 apply_cmresult_to_city(pcity, cmr);
660 city_refresh_from_main_map(pcity, NULL);
661 if (!city_happy(pcity)) {
662 CITY_LOG(LOG_ERROR, pcity, "is NOT happy when it should be!");
663 }
664 } else {
665 CITY_LOG(LOG_ERROR, pcity, "has NO valid state!");
666 }
667 }
668 cm_result_destroy(cmr);
669 } city_list_iterate_end;
670 } else if (celebrate == AI_CELEBRATION_NO) {
671 city_list_iterate(pplayer->cities, pcity) {
672 /* KLUDGE: Must refresh to restore the original values which
673 * were clobbered in cm_query_result(), after the tax rates
674 * were changed. */
675 city_refresh_from_main_map(pcity, NULL);
676 } city_list_iterate_end;
677 }
678
679 send_player_info_c(pplayer, pplayer->connections);
680
681 #ifdef DEBUG_TIMERS
682 timer_stop(taxtimer);
683 log_base(LOGLEVEL_TAX, "Tax calculation for %s (player %d) in %.3f "
684 "seconds.", player_name(pplayer),
685 player_index(pplayer), timer_read_seconds(taxtimer));
686 timer_destroy(taxtimer);
687 #endif /* DEBUG_TIMERS */
688 }
689 #undef RATE_NOT_SET
690 #undef RATE_VALID
691 #undef RATE_REMAINS
692
693 /**************************************************************************
694 Change the government form, if it can and there is a good reason.
695 **************************************************************************/
dai_manage_government(struct ai_type * ait,struct player * pplayer)696 static void dai_manage_government(struct ai_type *ait, struct player *pplayer)
697 {
698 struct adv_data *adv = adv_data_get(pplayer, NULL);
699
700 if (!pplayer->is_alive || has_handicap(pplayer, H_AWAY)) {
701 return;
702 }
703
704 if (adv->goal.revolution != government_of_player(pplayer)) {
705 dai_government_change(pplayer, adv->goal.revolution); /* change */
706 }
707
708 /* Crank up tech want */
709 if (adv->goal.govt.req == A_UNSET
710 || research_invention_state(research_get(pplayer),
711 adv->goal.govt.req) == TECH_KNOWN) {
712 return; /* already got it! */
713 } else if (adv->goal.govt.val > 0) {
714 /* We have few cities in the beginning, compensate for this to ensure
715 * that we are sufficiently forward-looking. */
716 int want = MAX(adv->goal.govt.val, 100);
717 struct nation_type *pnation = nation_of_player(pplayer);
718 struct ai_plr *plr_data = def_ai_player_data(pplayer, ait);
719
720 if (government_of_player(pplayer) == init_government_of_nation(pnation)) {
721 /* Default government is the crappy one we start in (like Despotism).
722 * We want something better pretty soon! */
723 want += 25 * game.info.turn;
724 }
725 plr_data->tech_want[adv->goal.govt.req] += want;
726 TECH_LOG(ait, LOG_DEBUG, pplayer, advance_by_number(adv->goal.govt.req),
727 "dai_manage_government() + %d for %s",
728 want,
729 government_rule_name(adv->goal.govt.gov));
730 }
731 }
732
733 /**************************************************************************
734 Activities to be done by AI _before_ human turn. Here we just move the
735 units intelligently.
736 **************************************************************************/
dai_do_first_activities(struct ai_type * ait,struct player * pplayer)737 void dai_do_first_activities(struct ai_type *ait, struct player *pplayer)
738 {
739 TIMING_LOG(AIT_ALL, TIMER_START);
740 dai_assess_danger_player(ait, pplayer);
741 /* TODO: Make assess_danger save information on what is threatening
742 * us and make dai_manage_units and Co act upon this information, trying
743 * to eliminate the source of danger */
744
745 TIMING_LOG(AIT_UNITS, TIMER_START);
746 dai_manage_units(ait, pplayer);
747 TIMING_LOG(AIT_UNITS, TIMER_STOP);
748 /* STOP. Everything else is at end of turn. */
749
750 TIMING_LOG(AIT_ALL, TIMER_STOP);
751
752 flush_packets(); /* AIs can be such spammers... */
753 }
754
755 /**************************************************************************
756 Activities to be done by AI _after_ human turn. Here we respond to
757 dangers created by human and AI opposition by ordering defenders in
758 cities and setting taxes accordingly. We also do other duties.
759
760 We do _not_ move units here, otherwise humans complain that AI moves
761 twice.
762 **************************************************************************/
dai_do_last_activities(struct ai_type * ait,struct player * pplayer)763 void dai_do_last_activities(struct ai_type *ait, struct player *pplayer)
764 {
765 TIMING_LOG(AIT_ALL, TIMER_START);
766 dai_clear_tech_wants(ait, pplayer);
767
768 dai_manage_government(ait, pplayer);
769 dai_adjust_policies(ait, pplayer);
770 TIMING_LOG(AIT_TAXES, TIMER_START);
771 dai_manage_taxes(ait, pplayer);
772 TIMING_LOG(AIT_TAXES, TIMER_STOP);
773 TIMING_LOG(AIT_CITIES, TIMER_START);
774 dai_manage_cities(ait, pplayer);
775 TIMING_LOG(AIT_CITIES, TIMER_STOP);
776 TIMING_LOG(AIT_TECH, TIMER_START);
777 dai_manage_tech(ait, pplayer);
778 TIMING_LOG(AIT_TECH, TIMER_STOP);
779 dai_manage_spaceship(pplayer);
780
781 TIMING_LOG(AIT_ALL, TIMER_STOP);
782 }
783