Freeciv21
Develop your civilization from humble roots to a global empire
aiunit.h
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 #pragma once
14 
15 // common
16 #include "combat.h"
17 #include "fc_types.h"
18 #include "unittype.h"
19 
20 struct pf_map;
21 class PFPath;
22 
23 struct section_file;
24 
37 };
38 
39 struct unit_ai {
40  // The following are unit ids or special indicator values (<=0)
41  int ferryboat; // the ferryboat assigned to us
42  int passenger; // the unit assigned to this ferryboat
43  int bodyguard; // the unit bodyguarding us
44  int charge; // the unit this unit is bodyguarding
45 
47  struct tile **prev_pos, **cur_pos;
48 
49  int target; // target we hunt
50  bv_player hunted; // if a player is hunting us, set by that player
51  bool done; // we are done controlling this unit this turn
52 
53  enum ai_unit_task task;
54 };
55 
56 struct unit_type_ai {
57  bool firepower1;
58  bool ferry;
61  struct unit_type_list *potential_charges;
62 };
63 
64 // Simple military macros
65 
66 /* pplayers_at_war() thinks no contacts equals war, which often is
67  * very annoying. */
68 #define WAR(plr1, plr2) (player_diplstate_get(plr1, plr2)->type == DS_WAR)
69 #define NEVER_MET(plr1, plr2) \
70  (player_diplstate_get(plr1, plr2)->type == DS_NO_CONTACT)
71 #define DEFENSE_POWER(ptype) \
72  (ptype->defense_strength * ptype->hp * ptype->firepower)
73 #define ATTACK_POWER(ptype) \
74  (ptype->attack_strength * ptype->hp * ptype->firepower)
75 #define IS_ATTACKER(ptype) \
76  (ptype->attack_strength > ptype->transport_capacity)
77 #define POTENTIALLY_HOSTILE_PLAYER(ait, pplayer, aplayer) \
78  (WAR(pplayer, aplayer) || NEVER_MET(pplayer, aplayer) \
79  || dai_diplomacy_get(ait, pplayer, aplayer)->countdown >= 0)
80 #define UNITTYPE_COSTS(ut) \
81  (ut->pop_cost * 3 + ut->happy_cost + ut->upkeep[O_SHIELD] \
82  + ut->upkeep[O_FOOD] + ut->upkeep[O_GOLD])
83 
84 // Invasion types
85 #define INVASION_OCCUPY 0
86 #define INVASION_ATTACK 1
87 
88 extern struct unit_type *simple_ai_types[U_LAST];
89 
90 #define RAMPAGE_ANYTHING 1
91 #define RAMPAGE_HUT_OR_BETTER 99998
92 #define RAMPAGE_FREE_CITY_OR_BETTER 99999
93 #define BODYGUARD_RAMPAGE_THRESHOLD (SHIELD_WEIGHTING * 4)
94 bool dai_military_rampage(struct unit *punit, int thresh_adj,
95  int thresh_move);
96 void dai_manage_units(struct ai_type *ait, struct player *pplayer);
97 void dai_manage_unit(struct ai_type *ait, struct player *pplayer,
98  struct unit *punit);
99 void dai_manage_military(struct ai_type *ait, struct player *pplayer,
100  struct unit *punit);
101 struct city *find_nearest_safe_city(struct unit *punit);
102 int look_for_charge(struct ai_type *ait, struct player *pplayer,
103  struct unit *punit, struct unit **aunit,
104  struct city **acity);
105 bool dai_can_unit_type_follow_unit_type(const struct unit_type *follower,
106  const struct unit_type *followee,
107  struct ai_type *ait);
108 
109 bool find_beachhead(const struct player *pplayer, struct pf_map *ferry_map,
110  struct tile *dest_tile,
111  const struct unit_type *cargo_type,
112  struct tile **ferry_dest, struct tile **beachhead_tile);
113 int find_something_to_kill(ai_type *ait, player *pplayer, unit *punit,
114  tile **pdest_tile, PFPath *ppath,
115  pf_map **pferrymap, unit **pferryboat,
116  const unit_type **pboattype, int *pmove_time);
117 
118 int build_cost_balanced(const struct unit_type *punittype);
119 int unittype_def_rating_squared(const struct unit_type *att_type,
120  const struct unit_type *def_type,
121  struct player *def_player,
122  struct tile *ptile, bool fortified,
123  int veteran);
124 int kill_desire(int benefit, int attack, int loss, int vuln,
125  int attack_count);
126 
127 const struct impr_type *
128 utype_needs_improvement(const struct unit_type *putype,
129  const struct city *pcity);
130 
131 void dai_consider_tile_dangerous(struct ai_type *ait, struct tile *ptile,
132  struct unit *punit,
133  enum override_bool *result);
134 
135 void dai_units_ruleset_init(struct ai_type *ait);
136 void dai_units_ruleset_close(struct ai_type *ait);
137 
138 void dai_unit_init(struct ai_type *ait, struct unit *punit);
139 void dai_unit_turn_end(struct ai_type *ait, struct unit *punit);
140 void dai_unit_close(struct ai_type *ait, struct unit *punit);
141 
142 #define simple_ai_unit_type_iterate(_ut) \
143  { \
144  struct unit_type *_ut; \
145  int _ut##_index = 0; \
146  while (nullptr != (_ut = simple_ai_types[_ut##_index++])) {
147 
148 #define simple_ai_unit_type_iterate_end \
149  } \
150  }
151 
152 void dai_unit_save(struct ai_type *ait, const char *aitstr,
153  struct section_file *file, const struct unit *punit,
154  const char *unitstr);
155 void dai_unit_load(struct ai_type *ait, const char *aitstr,
156  const struct section_file *file, struct unit *punit,
157  const char *unitstr);
158 
159 struct unit_type *dai_role_utype_for_terrain_class(struct city *pcity,
160  int role,
161  enum terrain_class tc);
162 
163 bool dai_unit_can_strike_my_unit(const struct unit *attacker,
164  const struct unit *defender);
165 
166 void dai_switch_to_explore(struct ai_type *ait, struct unit *punit,
167  struct tile *target, enum override_bool *allow);
int find_something_to_kill(ai_type *ait, player *pplayer, unit *punit, tile **pdest_tile, PFPath *ppath, pf_map **pferrymap, unit **pferryboat, const unit_type **pboattype, int *pmove_time)
int look_for_charge(struct ai_type *ait, struct player *pplayer, struct unit *punit, struct unit **aunit, struct city **acity)
See if we can find something to defend.
Definition: aiunit.cpp:707
void dai_unit_save(struct ai_type *ait, const char *aitstr, struct section_file *file, const struct unit *punit, const char *unitstr)
Save AI data of a unit.
Definition: aiunit.cpp:3283
void dai_manage_military(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Decide what to do with a military unit.
Definition: aiunit.cpp:2399
int unittype_def_rating_squared(const struct unit_type *att_type, const struct unit_type *def_type, struct player *def_player, struct tile *ptile, bool fortified, int veteran)
Defence rating of def_type unit against att_type unit, squared.
Definition: aiunit.cpp:296
void dai_units_ruleset_init(struct ai_type *ait)
Initialise the unit data from the ruleset for the AI.
Definition: aiunit.cpp:3101
struct city * find_nearest_safe_city(struct unit *punit)
Find safe city to recover in.
Definition: aiunit.cpp:1620
int kill_desire(int benefit, int attack, int loss, int vuln, int attack_count)
Compute how much we want to kill certain victim we've chosen, counted in SHIELDs.
Definition: aiunit.cpp:334
ai_unit_task
Definition: aiunit.h:25
@ AIUNIT_BUILD_CITY
Definition: aiunit.h:28
@ AIUNIT_NONE
Definition: aiunit.h:26
@ AIUNIT_ATTACK
Definition: aiunit.h:30
@ AIUNIT_EXPLORE
Definition: aiunit.h:32
@ AIUNIT_HUNTER
Definition: aiunit.h:34
@ AIUNIT_RECOVER
Definition: aiunit.h:33
@ AIUNIT_TRADE
Definition: aiunit.h:35
@ AIUNIT_DEFEND_HOME
Definition: aiunit.h:29
@ AIUNIT_ESCORT
Definition: aiunit.h:31
@ AIUNIT_AUTO_SETTLER
Definition: aiunit.h:27
@ AIUNIT_WONDER
Definition: aiunit.h:36
void dai_switch_to_explore(struct ai_type *ait, struct unit *punit, struct tile *target, enum override_bool *allow)
Switch to autoexploring.
Definition: aiunit.cpp:3390
const struct impr_type * utype_needs_improvement(const struct unit_type *putype, const struct city *pcity)
Returns an improvement that will make it possible to build units of the specified type the specified ...
Definition: aiunit.cpp:2802
struct unit_type * simple_ai_types[U_LAST]
Definition: aiunit.cpp:112
int build_cost_balanced(const struct unit_type *punittype)
In the words of Syela: "Using funky fprime variable instead of f in the denom, so that def=1 units ar...
Definition: aiunit.cpp:240
bool dai_unit_can_strike_my_unit(const struct unit *attacker, const struct unit *defender)
Returns whether 'attacker' can attack 'defender' immediately.
Definition: aiunit.cpp:3356
void dai_manage_units(struct ai_type *ait, struct player *pplayer)
Master manage unit function.
Definition: aiunit.cpp:2761
void dai_manage_unit(struct ai_type *ait, struct player *pplayer, struct unit *punit)
manage one unit Careful: punit may have been destroyed upon return from this routine!
Definition: aiunit.cpp:2551
struct unit_type * dai_role_utype_for_terrain_class(struct city *pcity, int role, enum terrain_class tc)
Get unit type player can build, suitable to role, with given move type.
Definition: aiunit.cpp:3344
void dai_unit_turn_end(struct ai_type *ait, struct unit *punit)
Free unit from use with default AI.
Definition: aiunit.cpp:3252
bool dai_can_unit_type_follow_unit_type(const struct unit_type *follower, const struct unit_type *followee, struct ai_type *ait)
See if the follower can follow the followee.
Definition: aiunit.cpp:834
void dai_unit_init(struct ai_type *ait, struct unit *punit)
Initialize unit for use with default AI.
Definition: aiunit.cpp:3229
void dai_consider_tile_dangerous(struct ai_type *ait, struct tile *ptile, struct unit *punit, enum override_bool *result)
Are there dangerous enemies at or adjacent to the tile 'ptile'?
Definition: aiunit.cpp:3017
void dai_unit_load(struct ai_type *ait, const char *aitstr, const struct section_file *file, struct unit *punit, const char *unitstr)
Load AI data of a unit.
Definition: aiunit.cpp:3302
void dai_unit_close(struct ai_type *ait, struct unit *punit)
Free unit from use with default AI.
Definition: aiunit.cpp:3264
void dai_units_ruleset_close(struct ai_type *ait)
Close AI unit type data.
Definition: aiunit.cpp:3208
bool find_beachhead(const struct player *pplayer, struct pf_map *ferry_map, struct tile *dest_tile, const struct unit_type *cargo_type, struct tile **ferry_dest, struct tile **beachhead_tile)
Returns TRUE if a beachhead as been found to reach 'dest_tile'.
Definition: aiunit.cpp:1037
bool dai_military_rampage(struct unit *punit, int thresh_adj, int thresh_move)
Find and kill anything reachable within this turn and worth more than the relevant of the given thres...
Definition: aiunit.cpp:592
static void attack(QVariant data1, QVariant data2)
Action "Attack" for choice dialog.
Definition: dialogs.cpp:2464
override_bool
Definition: fc_types.h:78
Definition: ai.h:42
Definition: city.h:291
Definition: player.h:231
Definition: tile.h:42
Definition: aiunit.h:39
struct tile ** prev_pos
Definition: aiunit.h:47
int bodyguard
Definition: aiunit.h:43
struct tile * cur_struct
Definition: aiunit.h:46
bv_player hunted
Definition: aiunit.h:50
bool done
Definition: aiunit.h:51
int ferryboat
Definition: aiunit.h:41
enum ai_unit_task task
Definition: aiunit.h:53
struct tile ** cur_pos
Definition: aiunit.h:47
int passenger
Definition: aiunit.h:42
struct tile * prev_struct
Definition: aiunit.h:46
int charge
Definition: aiunit.h:44
int target
Definition: aiunit.h:49
bool carries_occupiers
Definition: aiunit.h:60
struct unit_type_list * potential_charges
Definition: aiunit.h:61
bool ferry
Definition: aiunit.h:58
bool missile_platform
Definition: aiunit.h:59
bool firepower1
Definition: aiunit.h:57
Definition: unit.h:134
#define U_LAST
Definition: unittype.h:31