Freeciv21
Develop your civilization from humble roots to a global empire
daidiplomacy.cpp File Reference
#include <cstdarg>
#include <cstring>
#include "fcintl.h"
#include "log.h"
#include "rand.h"
#include "shared.h"
#include "support.h"
#include "aisupport.h"
#include "city.h"
#include "diptreaty.h"
#include "events.h"
#include "game.h"
#include "nation.h"
#include "packets.h"
#include "player.h"
#include "research.h"
#include "spaceship.h"
#include "tech.h"
#include "unitlist.h"
#include "diplhand.h"
#include "maphand.h"
#include "notify.h"
#include "advdata.h"
#include "advtools.h"
#include "aitraits.h"
#include "handicaps.h"
#include "aidata.h"
#include "ailog.h"
#include "aiplayer.h"
#include "aitools.h"
#include "aiunit.h"
#include "daicity.h"
#include "daidiplomacy.h"
+ Include dependency graph for daidiplomacy.cpp:

Go to the source code of this file.

Macros

#define LOG_DIPL2   LOG_DEBUG
 
#define BIG_NUMBER   100000
 
#define TURNS_BEFORE_TARGET   15
 

Functions

static void dai_incident_war (struct player *violator, struct player *victim)
 War declared against a player. More...
 
static void dai_incident_simple (struct player *receiver, const struct player *violator, const struct player *victim, enum casus_belli_range scope, int how_bad)
 Called when someone caused an incident to make the receiver lose AI love. More...
 
static void dai_incident_nuclear (struct player *receiver, const struct player *violator, const struct player *victim)
 Nuclear strike against victim. More...
 
static void dai_incident_nuclear_not_target (struct player *receiver, const struct player *violator, const struct player *victim)
 Nuclear strike against someone else. More...
 
static void dai_incident_nuclear_self (struct player *receiver, const struct player *violator, const struct player *victim)
 Somebody else than victim did nuclear strike against self. More...
 
static void clear_old_treaty (struct player *pplayer, struct player *aplayer)
 Clear old clauses from the treaty between players. More...
 
static void dai_diplo_notify (struct player *pplayer, const char *text,...)
 Send a diplomatic message. More...
 
static int greed (int missing_love)
 This is your typical human reaction. More...
 
static enum diplstate_type pact_clause_to_diplstate_type (enum clause_type type)
 Convert clause into diplomatic state. More...
 
static int dai_goldequiv_tech (struct ai_type *ait, struct player *pplayer, Tech_type_id tech)
 How much is a tech worth to player measured in gold. More...
 
static bool shared_vision_is_safe (struct player *pplayer, struct player *aplayer)
 Avoid giving pplayer's vision to non-allied player through aplayer (shared vision is transitive). More...
 
static bool dai_players_can_agree_on_ceasefire (struct ai_type *ait, struct player *player1, struct player *player2)
 Checks if player1 can agree on ceasefire with player2 This function should only be used for ai players. More...
 
static int compute_tech_sell_price (struct ai_type *ait, struct player *giver, struct player *taker, int tech_id, bool *is_dangerous)
 Calculate a price of a tech. More...
 
static const struct playerget_allied_with_enemy_player (const struct player *us, const struct player *them)
 Returns an enemy player to 'us' allied with 'them' if there is one. More...
 
static int dai_goldequiv_clause (struct ai_type *ait, struct player *pplayer, struct player *aplayer, struct Clause *pclause, bool verbose, enum diplstate_type ds_after)
 Evaluate gold worth of a single clause in a treaty. More...
 
void dai_treaty_evaluate (struct ai_type *ait, struct player *pplayer, struct player *aplayer, struct Treaty *ptreaty)
 pplayer is AI player, aplayer is the other player involved, treaty is the treaty being considered. More...
 
static void dai_treaty_react (struct ai_type *ait, struct player *pplayer, struct player *aplayer, struct Clause *pclause)
 Comments to player from AI on clauses being agreed on. More...
 
void dai_treaty_accepted (struct ai_type *ait, struct player *pplayer, struct player *aplayer, struct Treaty *ptreaty)
 This function is called when a treaty has been concluded, to deal with followup issues like comments and relationship (love) changes. More...
 
static int dai_war_desire (struct ai_type *ait, struct player *pplayer, struct player *target)
 Calculate our desire to go to war against aplayer. More...
 
static void dai_diplomacy_suggest (struct player *pplayer, struct player *aplayer, enum clause_type what, bool to_pplayer, int value)
 Suggest a treaty. More...
 
void dai_diplomacy_first_contact (struct ai_type *ait, struct player *pplayer, struct player *aplayer)
 What to do when we first meet. More...
 
void dai_diplomacy_begin_new_phase (struct ai_type *ait, struct player *pplayer)
 Calculate our diplomatic predispositions here. More...
 
static void suggest_tech_exchange (struct ai_type *ait, struct player *player1, struct player *player2)
 Find two techs that can be exchanged and suggest that. More...
 
static void dai_share (struct ai_type *ait, struct player *pplayer, struct player *aplayer)
 Offer techs and stuff to other player and ask for techs we need. More...
 
static void dai_go_to_war (struct ai_type *ait, struct player *pplayer, struct player *target, enum war_reason reason)
 Go to war. More...
 
static void war_countdown (struct ai_type *ait, struct player *pplayer, struct player *target, int countdown, enum war_reason reason)
 Do diplomatic actions. More...
 
void dai_diplomacy_actions (struct ai_type *ait, struct player *pplayer)
 Do diplomatic actions. More...
 
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 as many units as possible. More...
 
void dai_incident (struct ai_type *ait, enum incident_type type, enum casus_belli_range scope, const struct action *paction, struct player *receiver, struct player *violator, struct player *victim)
 Handle incident caused by violator. More...
 

Variables

static bool diplomacy_verbose = true
 

Macro Definition Documentation

◆ BIG_NUMBER

#define BIG_NUMBER   100000

Definition at line 63 of file daidiplomacy.cpp.

◆ LOG_DIPL2

#define LOG_DIPL2   LOG_DEBUG

Definition at line 59 of file daidiplomacy.cpp.

◆ TURNS_BEFORE_TARGET

#define TURNS_BEFORE_TARGET   15

Definition at line 69 of file daidiplomacy.cpp.

Function Documentation

◆ clear_old_treaty()

static void clear_old_treaty ( struct player pplayer,
struct player aplayer 
)
static

Clear old clauses from the treaty between players.

Definition at line 1241 of file daidiplomacy.cpp.

Referenced by dai_diplomacy_actions(), dai_diplomacy_first_contact(), and dai_share().

◆ compute_tech_sell_price()

static int compute_tech_sell_price ( struct ai_type ait,
struct player giver,
struct player taker,
int  tech_id,
bool *  is_dangerous 
)
static

Calculate a price of a tech.

Note that both AI players always evaluate the tech worth symetrically This eases tech exchange. is_dangerous returns ig the giver is afraid of giving that tech (the taker should evaluate it normally, but giver should never give that)

Definition at line 217 of file daidiplomacy.cpp.

Referenced by dai_goldequiv_clause(), and suggest_tech_exchange().

◆ dai_diplo_notify()

static void dai_diplo_notify ( struct player pplayer,
const char *  text,
  ... 
)
static

Send a diplomatic message.

Use this instead of notify directly because we may want to highligh/present these messages differently in the future.

Definition at line 92 of file daidiplomacy.cpp.

Referenced by dai_diplomacy_actions(), dai_diplomacy_first_contact(), dai_go_to_war(), dai_goldequiv_clause(), dai_incident_war(), dai_treaty_evaluate(), dai_treaty_react(), and war_countdown().

◆ dai_diplomacy_actions()

void dai_diplomacy_actions ( struct ai_type ait,
struct player pplayer 
)

Do diplomatic actions.

Must be called only after calculate function above has been run for all AI players.

Only ever called for AI players.

Definition at line 1547 of file daidiplomacy.cpp.

Referenced by cai_diplomacy_actions(), texwai_diplomacy_actions(), and twai_diplomacy_actions().

◆ dai_diplomacy_begin_new_phase()

void dai_diplomacy_begin_new_phase ( struct ai_type ait,
struct player pplayer 
)

Calculate our diplomatic predispositions here.

Don't do anything.

Only ever called for AI players and never for barbarians.

This is called at the start of a new AI phase. It's not called when a game is loaded. So everything calculated here should be put into the savegame.

Definition at line 1010 of file daidiplomacy.cpp.

Referenced by dai_data_phase_begin().

◆ dai_diplomacy_first_contact()

void dai_diplomacy_first_contact ( struct ai_type ait,
struct player pplayer,
struct player aplayer 
)

What to do when we first meet.

pplayer is the AI player.

Definition at line 957 of file daidiplomacy.cpp.

Referenced by cai_diplomacy_first_contact(), texwai_diplomacy_first_contact(), and twai_diplomacy_first_contact().

◆ dai_diplomacy_suggest()

static void dai_diplomacy_suggest ( struct player pplayer,
struct player aplayer,
enum clause_type  what,
bool  to_pplayer,
int  value 
)
static

Suggest a treaty.

pplayer is the (AI) player suggesting the treaty. If to_pplayer, then aplayer is giver in the clause, else pplayer is.

Definition at line 937 of file daidiplomacy.cpp.

Referenced by dai_diplomacy_actions(), dai_diplomacy_first_contact(), dai_share(), and suggest_tech_exchange().

◆ dai_go_to_war()

static void dai_go_to_war ( struct ai_type ait,
struct player pplayer,
struct player target,
enum war_reason  reason 
)
static

Go to war.

Explain to target why we did it, and set countdown to some negative value to make us a bit stubborn to avoid immediate reversal to ceasefire.

Definition at line 1319 of file daidiplomacy.cpp.

Referenced by dai_diplomacy_actions().

◆ dai_goldequiv_clause()

static int dai_goldequiv_clause ( struct ai_type ait,
struct player pplayer,
struct player aplayer,
struct Clause pclause,
bool  verbose,
enum diplstate_type  ds_after 
)
static

Evaluate gold worth of a single clause in a treaty.

Note that it sometimes matter a great deal who is giving what to whom, and sometimes (such as with treaties) it does not matter at all. ds_after means a pact offered in the same treaty or current diplomatic state

Definition at line 291 of file daidiplomacy.cpp.

Referenced by dai_diplomacy_actions(), dai_diplomacy_first_contact(), dai_treaty_accepted(), and dai_treaty_evaluate().

◆ dai_goldequiv_tech()

static int dai_goldequiv_tech ( struct ai_type ait,
struct player pplayer,
Tech_type_id  tech 
)
static

How much is a tech worth to player measured in gold.

Definition at line 148 of file daidiplomacy.cpp.

Referenced by compute_tech_sell_price().

◆ dai_incident()

void dai_incident ( struct ai_type ait,
enum incident_type  type,
enum casus_belli_range  scope,
const struct action paction,
struct player receiver,
struct player violator,
struct player victim 
)

Handle incident caused by violator.

Definition at line 1958 of file daidiplomacy.cpp.

Referenced by cai_incident(), texwai_incident(), and twai_incident().

◆ dai_incident_nuclear()

static void dai_incident_nuclear ( struct player receiver,
const struct player violator,
const struct player victim 
)
static

Nuclear strike against victim.

Definition at line 2092 of file daidiplomacy.cpp.

Referenced by dai_incident().

◆ dai_incident_nuclear_not_target()

static void dai_incident_nuclear_not_target ( struct player receiver,
const struct player violator,
const struct player victim 
)
static

Nuclear strike against someone else.

Definition at line 2108 of file daidiplomacy.cpp.

Referenced by dai_incident().

◆ dai_incident_nuclear_self()

static void dai_incident_nuclear_self ( struct player receiver,
const struct player violator,
const struct player victim 
)
static

Somebody else than victim did nuclear strike against self.

Definition at line 2120 of file daidiplomacy.cpp.

Referenced by dai_incident().

◆ dai_incident_simple()

static void dai_incident_simple ( struct player receiver,
const struct player violator,
const struct player victim,
enum casus_belli_range  scope,
int  how_bad 
)
static

Called when someone caused an incident to make the receiver lose AI love.

Make the victim angry at the violator. Twice as angry if this was bad enough to cause International Outrage. Make the rest of the world a bit angry if the violator did this to him self. Twice as angry if the violator did it to someone else.

Parameters
receiverthe player that receives information about the incident
violatorthe player that caused the incident
victimthe victim of the incident
scopethe range of the Casus Belli that caused this
how_bada badness score for the incident

Definition at line 2142 of file daidiplomacy.cpp.

Referenced by dai_incident().

◆ dai_incident_war()

static void dai_incident_war ( struct player violator,
struct player victim 
)
static

War declared against a player.

We apply a penalty because this means he is seen as untrustworthy, especially if past relations with the victim have been cordial (betrayal).

Reasons for war and other mitigating circumstances are checked in calling code.

Definition at line 2170 of file daidiplomacy.cpp.

Referenced by dai_incident().

◆ dai_on_war_footing()

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 as many units as possible.

Definition at line 1941 of file daidiplomacy.cpp.

Referenced by dai_city_choose_build(), dai_manage_cities(), dai_manage_taxes(), and dai_spend_gold().

◆ dai_players_can_agree_on_ceasefire()

static bool dai_players_can_agree_on_ceasefire ( struct ai_type ait,
struct player player1,
struct player player2 
)
static

Checks if player1 can agree on ceasefire with player2 This function should only be used for ai players.

Definition at line 201 of file daidiplomacy.cpp.

Referenced by dai_goldequiv_clause().

◆ dai_share()

static void dai_share ( struct ai_type ait,
struct player pplayer,
struct player aplayer 
)
static

Offer techs and stuff to other player and ask for techs we need.

Definition at line 1263 of file daidiplomacy.cpp.

Referenced by dai_diplomacy_actions().

◆ dai_treaty_accepted()

void dai_treaty_accepted ( struct ai_type ait,
struct player pplayer,
struct player aplayer,
struct Treaty ptreaty 
)

This function is called when a treaty has been concluded, to deal with followup issues like comments and relationship (love) changes.

pplayer is AI player, aplayer is the other player involved, ptreaty is the treaty accepted.

Definition at line 720 of file daidiplomacy.cpp.

Referenced by cai_treaty_accepted(), texwai_treaty_accepted(), and twai_treaty_accepted().

◆ dai_treaty_evaluate()

void dai_treaty_evaluate ( struct ai_type ait,
struct player pplayer,
struct player aplayer,
struct Treaty ptreaty 
)

pplayer is AI player, aplayer is the other player involved, treaty is the treaty being considered.

It is all a question about money :-)

Definition at line 598 of file daidiplomacy.cpp.

Referenced by cai_treaty_evaluate(), texwai_treaty_evaluate(), and twai_treaty_evaluate().

◆ dai_treaty_react()

static void dai_treaty_react ( struct ai_type ait,
struct player pplayer,
struct player aplayer,
struct Clause pclause 
)
static

Comments to player from AI on clauses being agreed on.

Does not alter any state.

Definition at line 680 of file daidiplomacy.cpp.

Referenced by dai_treaty_accepted().

◆ dai_war_desire()

static int dai_war_desire ( struct ai_type ait,
struct player pplayer,
struct player target 
)
static

Calculate our desire to go to war against aplayer.

We want to attack a player that is easy to beat and will yield a nice profit.

This function is full of hardcoded constants by necessity. They are not #defines since they are not used anywhere else.

Definition at line 784 of file daidiplomacy.cpp.

Referenced by dai_diplomacy_begin_new_phase().

◆ get_allied_with_enemy_player()

static const struct player* get_allied_with_enemy_player ( const struct player us,
const struct player them 
)
static

Returns an enemy player to 'us' allied with 'them' if there is one.

Definition at line 270 of file daidiplomacy.cpp.

Referenced by dai_goldequiv_clause().

◆ greed()

static int greed ( int  missing_love)
static

This is your typical human reaction.

Convert lack of love into lust for gold.

Definition at line 114 of file daidiplomacy.cpp.

Referenced by dai_goldequiv_clause().

◆ pact_clause_to_diplstate_type()

static enum diplstate_type pact_clause_to_diplstate_type ( enum clause_type  type)
static

Convert clause into diplomatic state.

Definition at line 114 of file daidiplomacy.cpp.

Referenced by dai_treaty_accepted(), and dai_treaty_evaluate().

◆ shared_vision_is_safe()

static bool shared_vision_is_safe ( struct player pplayer,
struct player aplayer 
)
static

Avoid giving pplayer's vision to non-allied player through aplayer (shared vision is transitive).

Definition at line 174 of file daidiplomacy.cpp.

Referenced by dai_diplomacy_actions(), dai_goldequiv_clause(), and dai_share().

◆ suggest_tech_exchange()

static void suggest_tech_exchange ( struct ai_type ait,
struct player player1,
struct player player2 
)
static

Find two techs that can be exchanged and suggest that.

Definition at line 1163 of file daidiplomacy.cpp.

Referenced by dai_share().

◆ war_countdown()

static void war_countdown ( struct ai_type ait,
struct player pplayer,
struct player target,
int  countdown,
enum war_reason  reason 
)
static

Do diplomatic actions.

Must be called only after calculate function above has been run for all AI players.

Only ever called for AI players and never for barbarians.

When the adip->coundown variable is set to a positive value, it counts down to a declaration of war. When it is set to a value smaller than -1, it is a countup towards a "neutral" stance of -1, in which time the AI will refuse to make treaties. This is to make the AI more stubborn.

Definition at line 1426 of file daidiplomacy.cpp.

Referenced by dai_diplomacy_actions().

Variable Documentation

◆ diplomacy_verbose

bool diplomacy_verbose = true
static

Definition at line 66 of file daidiplomacy.cpp.

Referenced by dai_diplo_notify(), and dai_goldequiv_clause().