Freeciv-3.4
Loading...
Searching...
No Matches
daiactions.c
Go to the documentation of this file.
1/**********************************************************************
2 Freeciv - Copyright (C) 2019 - The Freeciv Project contributors.
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/* common */
19#include "city.h"
20#include "metaknowledge.h"
21#include "movement.h"
22#include "player.h"
23#include "research.h"
24#include "tech.h"
25#include "unit.h"
26
27/* server */
28#include "cityturn.h"
29#include "diplomats.h"
30#include "srv_log.h"
31
32/* ai/default */
33#include "daihand.h"
34
35
36#include "daiactions.h"
37
38
39#define LOG_DIPLOMAT LOG_DEBUG
40
41/**************************************************************************/
45{
46 int count = 0;
47
48 city_built_iterate(pcity, pimprove) {
49 if (pimprove->sabotage > 0) {
50 count++;
51 }
53
54 return count;
55}
56
57/**************************************************************************/
62static Tech_type_id
63choose_tech_to_steal(const struct player *actor_player,
64 const struct player *target_player)
65{
66 const struct research *actor_research = research_get(actor_player);
67 const struct research *target_research = research_get(target_player);
68
69 if (actor_research != target_research) {
70 if (can_see_techs_of_target(actor_player, target_player)) {
71 advance_iterate(padvance) {
72 Tech_type_id i = advance_number(padvance);
73
74 if (research_invention_state(target_research, i) == TECH_KNOWN
75 && research_invention_gettable(actor_research, i,
77 && (research_invention_state(actor_research, i) == TECH_UNKNOWN
78 || (research_invention_state(actor_research, i)
79 == TECH_PREREQS_KNOWN))) {
80
81 return i;
82 }
84 }
85 }
86
87 /* Unable to find a target. */
88 return A_UNSET;
89}
90
91/***********************************************************************/
97 struct unit *actor_unit,
98 struct city *target_city,
99 int sub_tgt_id,
100 int count_tech)
101{
102 /* FIXME: This is just a copy of how dai_diplomat_city() used to behave.
103 * The utility values are *not* proportional to utility values used for
104 * other tasks in the AI. They should probably be scaled to match unit
105 * the utility of building the unit that can perform the action.
106 * Feel free to adjust the utility values. Feel free to change the old
107 * rules of thumb. Feel free to make it give more weight to Casus Belli,
108 * to move cost and to other ruleset defined information. Feel free to
109 * support other actions. */
110
111 adv_want utility;
112 bool expected_kills;
113
114 struct player *actor_player = unit_owner(actor_unit);
115 struct player *target_player = city_owner(target_city);
116
117 fc_assert_ret_val(action_get_actor_kind(paction) == AAK_UNIT, 0);
118 fc_assert_ret_val(action_get_target_kind(paction) == ATK_CITY, 0);
119
120 utility = 0;
121
122 /* If tech theft is impossible when expected. */
123 expected_kills = utype_is_consumed_by_action(paction,
125
126 /* The unit was always spent */
128
129 if (action_has_result(paction, ACTRES_ESTABLISH_EMBASSY)) {
130 utility += 10000;
131 }
132
133 if (!pplayers_allied(actor_player, target_player)
134 && count_tech > 0
135 && (!expected_kills
137 == 0))
138 && action_has_result(paction, ACTRES_SPY_STEAL_TECH)) {
139 utility += 9000;
140 }
141
142 if (!pplayers_allied(actor_player, target_player)
143 && count_tech > 0
144 && (!expected_kills
146 == 0))
147 && action_has_result(paction, ACTRES_SPY_TARGETED_STEAL_TECH)) {
148 Tech_type_id tgt_tech = sub_tgt_id;
149
150 /* FIXME: Should probably just try to steal a random tech if no target
151 * is found. */
152 if (tgt_tech != A_UNSET) {
153 /* A tech target can be identified. */
154 utility += 8000;
155 }
156 }
157
158 if (!pplayers_allied(actor_player, target_player)
159 && (action_has_result(paction, ACTRES_SPY_INCITE_CITY))) {
160 int incite_cost, expenses;
161
163 dai_calc_data(actor_player, NULL, &expenses, NULL);
164
166 utility += 7000;
167 } else {
168 UNIT_LOG(LOG_DIPLOMAT, actor_unit, "%s too expensive!",
170 }
171 }
172
175 utility += 6000;
176 }
177
182
183 if (count_impr > 0) {
184 /* TODO: start caring about the sub target. */
185 utility += 5000;
186 }
187 }
188
193
194 if (count_impr > 0) {
195 /* Usually better odds than already built buildings. */
196 utility += 5010;
197 }
198 }
199
202 utility += 4000;
203 }
204
207 utility += 3000;
208 }
209
212 utility += 2500;
213 }
214
217 utility += 2000;
218 }
219
222 utility += 6500;
223 }
224
225 {
226 /* FIXME: replace the above per action hard coding of war and not allied
227 * with adjustments to the below utility changes. */
228 int i;
229
230 const enum effect_type casus_belli_eft[] = {
233 };
234
235 for (i = 0; i < ARRAY_SIZE(casus_belli_eft); i++) {
240 case CBR_NONE:
241 /* No one cares. */
242 break;
243 case CBR_VICTIM_ONLY:
244 /* The victim gets a Casus Belli against me. */
245 utility -= 50;
246 break;
248 /* Every other player gets a Casus Belli against me. */
249 utility -= 500;
250 break;
251 case CBR_LAST:
252 fc_assert_msg(FALSE, "Shouldn't happen");
253 break;
254 }
255 }
256 }
257
259 /* Choose the non consuming version if possible. */
261 } else {
262 /* Not going to spend the unit so care about move fragment cost. */
263
266
269 /* Add the cost from the move. */
272 }
273
274 /* Note: The action performer function may charge more independently. */
276 struct tile *from_tile;
277
278 /* Ugly hack to understand the OnNativeTile unit state requirements
279 * used in the Action_Success_Actor_Move_Cost effect. */
286 /* Should be accurate unless the action charges more. */
288 } else {
289 /* Don't know where the actor unit will end up. Hope that it will be
290 * in a location similar to its current location or that any
291 * Action_Success_Actor_Move_Cost don't check unit relation to its
292 * tile. */
294 }
295
296 /* Taking MAX_MOVE_FRAGS takes all the move fragments. */
298
299 /* Losing all movement is seen as losing 2 utility. */
301 }
302
303 return MAX(0, utility);
304}
305
306/***********************************************************************/
311 struct unit *actor_unit,
312 struct city *target_city)
313{
316
319
322
324
325 return tgt_tech;
326 }
327
329 /* Invalid */
330 int tgt_impr = -1;
331 int tgt_impr_vul = 0;
332
334 /* How vulnerable the target building is. */
335 int impr_vul = pimprove->sabotage;
336
339 / 100);
340
341 /* Can't be better than 100% */
342 impr_vul = MAX(impr_vul, 100);
343
344 /* Swap if better or equal probability of sabotage than
345 * production. */
346 /* TODO: More complex "better" than "listed later" and better or equal
347 * probability of success. It would probably be best to use utility
348 * like the rest of the Freeciv AI does. Building value *
349 * vulnerability + value from circumstances like an attacker that
350 * would like to get rid of a City Wall? */
351 if (impr_vul >= tgt_impr_vul) {
352 tgt_impr = improvement_number(pimprove);
354 }
356
357 if (tgt_impr > -1) {
358 return tgt_impr;
359 }
360 }
361
362 /* No sub target specified. */
363 return 0;
364}
enum action_actor_kind action_get_actor_kind(const struct action *paction)
Definition actions.c:1119
enum action_target_kind action_get_target_kind(const struct action *paction)
Definition actions.c:1129
#define action_has_result(_act_, _res_)
Definition actions.h:184
const char * city_name_get(const struct city *pcity)
Definition city.c:1137
#define city_tile(_pcity_)
Definition city.h:564
#define city_owner(_pcity_)
Definition city.h:563
#define city_built_iterate(_pcity, _p)
Definition city.h:835
#define city_built_iterate_end
Definition city.h:841
int city_incite_cost(struct player *pplayer, struct city *pcity)
Definition cityturn.c:3579
char * incite_cost
Definition comments.c:76
int dai_action_choose_sub_tgt_unit_vs_city(struct action *paction, struct unit *actor_unit, struct city *target_city)
Definition daiactions.c:310
#define LOG_DIPLOMAT
Definition daiactions.c:39
static Tech_type_id choose_tech_to_steal(const struct player *actor_player, const struct player *target_player)
Definition daiactions.c:63
adv_want dai_action_value_unit_vs_city(struct action *paction, struct unit *actor_unit, struct city *target_city, int sub_tgt_id, int count_tech)
Definition daiactions.c:96
static int count_sabotagable_improvements(struct city *pcity)
Definition daiactions.c:44
void dai_calc_data(const struct player *pplayer, int *trade, int *expenses, int *income)
Definition daihand.c:117
struct unit * actor_unit
Definition dialogs_g.h:55
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit int const struct action *paction struct unit struct city * pcity
Definition dialogs_g.h:78
struct unit struct city * target_city
Definition dialogs_g.h:56
int diplomats_unignored_tech_stealings(struct unit *pdiplomat, struct city *pcity)
Definition diplomats.c:970
int get_city_bonus(const struct city *pcity, enum effect_type effect_type)
Definition effects.c:842
float adv_want
Definition fc_types.h:1048
int Tech_type_id
Definition fc_types.h:237
struct civ_game game
Definition game.c:62
struct world wld
Definition game.c:63
Impr_type_id improvement_number(const struct impr_type *pimprove)
#define fc_assert_msg(condition, message,...)
Definition log.h:182
#define fc_assert_ret_val(condition, val)
Definition log.h:195
static int map_move_cost_unit(const struct civ_map *nmap, struct unit *punit, const struct tile *ptile)
Definition map.h:290
bool can_see_techs_of_target(const struct player *pow_player, const struct player *target_player)
#define MAX_MOVE_FRAGS
Definition movement.h:29
bool pplayers_at_war(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1396
bool pplayers_allied(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1417
enum casus_belli_range casus_belli_range_for(const struct player *offender, const struct unit_type *off_ut, const struct player *tgt_plr, const enum effect_type outcome, const struct action *paction, const struct tile *tgt_tile)
Definition player.c:1652
struct research * research_get(const struct player *pplayer)
Definition research.c:130
enum tech_state research_invention_state(const struct research *presearch, Tech_type_id tech)
Definition research.c:622
bool research_invention_gettable(const struct research *presearch, const Tech_type_id tech, bool allow_holes)
Definition research.c:696
#define ARRAY_SIZE(x)
Definition shared.h:85
#define MIN(x, y)
Definition shared.h:55
#define MAX(x, y)
Definition shared.h:54
#define UNIT_LOG(loglevel, punit, msg,...)
Definition srv_log.h:98
Definition city.h:318
struct packet_game_info info
Definition game.h:89
bool tech_steal_allow_holes
struct player_economic economic
Definition player.h:284
Definition tile.h:50
Definition unit.h:140
struct tile * tile
Definition unit.h:142
struct civ_map map
#define FALSE
Definition support.h:47
Tech_type_id advance_number(const struct advance *padvance)
Definition tech.c:98
#define advance_iterate(_p)
Definition tech.h:273
#define A_UNSET
Definition tech.h:48
#define advance_iterate_end
Definition tech.h:274
int unit_pays_mp_for_action(const struct action *paction, const struct unit *punit)
Definition unit.c:2199
#define unit_tile(_pu)
Definition unit.h:407
#define unit_owner(_pu)
Definition unit.h:406
const struct unit_type * unit_type_get(const struct unit *punit)
Definition unittype.c:126
bool utype_pays_for_regular_move_to_tgt(const struct action *paction, const struct unit_type *utype)
Definition unittype.c:1355
bool utype_is_moved_to_tgt_by_action(const struct action *paction, const struct unit_type *utype)
Definition unittype.c:1274
int unit_build_shield_cost_base(const struct unit *punit)
Definition unittype.c:1509
bool utype_is_consumed_by_action(const struct action *paction, const struct unit_type *utype)
Definition unittype.c:1244
bool utype_is_unmoved_by_action(const struct action *paction, const struct unit_type *utype)
Definition unittype.c:1313