Freeciv21
Develop your civilization from humble roots to a global empire
ailog.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 1996-2020 Freeciv21 and Freeciv contributors. This file is
3  part of Freeciv21. Freeciv21 is free software: you can redistribute it
4  and/or modify it under the terms of the GNU General Public License as
5  published by the Free Software Foundation, either version 3 of the
6  License, or (at your option) any later version. You should have received
7  a copy of the GNU General Public License along with Freeciv21. If not,
8  see https://www.gnu.org/licenses/.
9  */
10 
11 // common
12 #include "map.h"
13 #include "nation.h"
14 #include "player.h"
15 #include "research.h"
16 // server
17 #include "notify.h"
18 
19 /* ai/default */
20 #include "aidata.h"
21 #include "aiplayer.h"
22 #include "aiunit.h"
23 #include "daicity.h"
24 
25 #include "ailog.h"
26 
27 Q_LOGGING_CATEGORY(ai_category, "freeciv.ai")
28 
29 
32 void dai_city_log(struct ai_type *ait, char *buffer, int buflength,
33  const struct city *pcity)
34 {
35  struct ai_city *city_data = def_ai_city_data(pcity, ait);
36 
37  fc_snprintf(buffer, buflength, "d%d u%d g%d", city_data->danger,
38  city_data->urgency, city_data->grave_danger);
39 }
40 
44 void dai_unit_log(struct ai_type *ait, char *buffer, int buflength,
45  const struct unit *punit)
46 {
47  struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
48 
49  fc_snprintf(buffer, buflength, "%d %d", unit_data->bodyguard,
50  unit_data->ferryboat);
51 }
52 
56 QString tech_log_prefix(ai_type *ait, const player *pplayer,
57  advance *padvance)
58 {
59  // FIXME const-correctness of the arguments
60  if (!valid_advance(padvance) || advance_by_number(A_NONE) == padvance) {
61  return QStringLiteral("(invalid tech)");
62  }
63 
64  auto *plr_data = def_ai_player_data(pplayer, ait);
65  return QString::asprintf(
66  "%s::%s (want " ADV_WANT_PRINTF ", dist %d) ", player_name(pplayer),
67  advance_rule_name(padvance),
68  plr_data->tech_want[advance_index(padvance)],
70  advance_number(padvance)));
71 }
72 
78 QString diplo_log_prefix(ai_type *ait, const player *pplayer,
79  const player *aplayer)
80 {
81  // FIXME const-correctness of the arguments
82  // Don't use ai_data_get since it can have side effects.
83  auto *adip = dai_diplomacy_get(ait, pplayer, aplayer);
84 
85  return QString::asprintf(
86  "%s->%s(l%d,c%d,d%d%s): ", player_name(pplayer), player_name(aplayer),
87  pplayer->ai_common.love[player_index(aplayer)], adip->countdown,
88  adip->distance,
89  adip->is_allied_with_enemy ? "?"
90  : (adip->at_war_with_ally ? "!" : ""));
91 }
92 
98 QString bodyguard_log_prefix(ai_type *ait, const unit *punit)
99 {
100  const struct unit *pcharge;
101  const struct city *pcity;
102  int id = -1;
103  int charge_x = -1;
104  int charge_y = -1;
105  const char *type = "guard";
106  const char *s = "none";
107  struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
108 
109  pcity = game_city_by_number(unit_data->charge);
110  pcharge = game_unit_by_number(unit_data->charge);
111  if (pcharge) {
112  index_to_map_pos(&charge_x, &charge_y, tile_index(unit_tile(pcharge)));
113  id = pcharge->id;
114  type = "bodyguard";
115  s = unit_rule_name(pcharge);
116  } else if (pcity) {
117  index_to_map_pos(&charge_x, &charge_y, tile_index(city_tile(pcity)));
118  id = pcity->id;
119  type = "cityguard";
120  s = city_name_get(pcity);
121  }
122  // else perhaps the charge died
123 
124  return QString::asprintf(
125  "%s %s[%d] %s (%d,%d){%s:%d@%d,%d} ",
127  punit->id, type, TILE_XY(unit_tile(punit)), s, id, charge_x, charge_y);
128 }
struct ai_dip_intel * dai_diplomacy_get(struct ai_type *ait, const struct player *plr1, const struct player *plr2)
Returns diplomatic state type between two players.
Definition: aidata.cpp:397
QString diplo_log_prefix(ai_type *ait, const player *pplayer, const player *aplayer)
Log player messages, they will appear like this.
Definition: ailog.cpp:78
void dai_unit_log(struct ai_type *ait, char *buffer, int buflength, const struct unit *punit)
Produce logline fragment for srv_log.
Definition: ailog.cpp:44
QString bodyguard_log_prefix(ai_type *ait, const unit *punit)
Log message for bodyguards.
Definition: ailog.cpp:98
QString tech_log_prefix(ai_type *ait, const player *pplayer, advance *padvance)
Log player tech messages.
Definition: ailog.cpp:56
void dai_city_log(struct ai_type *ait, char *buffer, int buflength, const struct city *pcity)
Produce logline fragment for srv_log.
Definition: ailog.cpp: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
static struct unit_ai * def_ai_unit_data(const struct unit *punit, struct ai_type *deftype)
Definition: aiplayer.h:41
struct tile * city_tile(const struct city *pcity)
Return the tile location of the city.
Definition: city.cpp:1095
const char * city_name_get(const struct city *pcity)
Return the name of the city.
Definition: city.cpp:1077
#define ADV_WANT_PRINTF
Definition: fc_types.h:1145
struct unit * game_unit_by_number(int id)
Find unit out of all units in game: now uses fast idex method, instead of looking through all units o...
Definition: game.cpp:112
struct city * game_city_by_number(int id)
Often used function to get a city pointer from a city ID.
Definition: game.cpp:103
#define index_to_map_pos(pmap_x, pmap_y, mindex)
Definition: map.h:164
const char * nation_rule_name(const struct nation_type *pnation)
Return the (untranslated) rule name of the nation (adjective form).
Definition: nation.cpp:115
struct nation_type * nation_of_unit(const struct unit *punit)
Return the nation of the player who owns the unit.
Definition: nation.cpp:438
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
int research_goal_unknown_techs(const struct research *presearch, Tech_type_id goal)
Returns the number of technologies the player need to research to get the goal technology.
Definition: research.cpp:745
struct research * research_get(const struct player *pplayer)
Returns the research structure associated with the player.
Definition: research.cpp:110
Definition: tech.h:113
int urgency
Definition: daicity.h:46
int danger
Definition: daicity.h:44
int grave_danger
Definition: daicity.h:45
Definition: ai.h:42
Definition: city.h:291
int id
Definition: city.h:296
int love[MAX_NUM_PLAYER_SLOTS]
Definition: player.h:117
Definition: player.h:231
struct player_ai ai_common
Definition: player.h:270
Definition: aiunit.h:39
int bodyguard
Definition: aiunit.h:43
int ferryboat
Definition: aiunit.h:41
int charge
Definition: aiunit.h:44
Definition: unit.h:134
int id
Definition: unit.h:141
int fc_snprintf(char *str, size_t n, const char *format,...)
See also fc_utf8_snprintf_trunc(), fc_utf8_snprintf_rep().
Definition: support.cpp:537
struct advance * valid_advance(struct advance *padvance)
Returns pointer when the advance "exists" in this game, returns nullptr otherwise.
Definition: tech.cpp:138
const char * advance_rule_name(const struct advance *padvance)
Return the (untranslated) rule name of the advance/technology.
Definition: tech.cpp:283
struct advance * advance_by_number(const Tech_type_id atype)
Return the advance for the given advance index.
Definition: tech.cpp:94
Tech_type_id advance_index(const struct advance *padvance)
Return the advance index.
Definition: tech.cpp:76
Tech_type_id advance_number(const struct advance *padvance)
Return the advance index.
Definition: tech.cpp:85
#define A_NONE
Definition: tech.h:36
#define tile_index(_pt_)
Definition: tile.h:70
#define TILE_XY(ptile)
Definition: tile.h:36
Q_LOGGING_CATEGORY(tileset_category, "freeciv.tileset")
Functions for handling the tilespec files which describe the files and contents of tilesets.
#define unit_tile(_pu)
Definition: unit.h:371
const char * unit_rule_name(const struct unit *punit)
Return the (untranslated) rule name of the unit.
Definition: unittype.cpp:1283