Freeciv-3.1
Loading...
Searching...
No Matches
aiplayer.c
Go to the documentation of this file.
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/* common */
19#include "ai.h"
20#include "city.h"
21#include "game.h"
22#include "unit.h"
23
24/* server */
25#include "citytools.h"
26
27/* ai/default */
28#include "aidata.h"
29#include "daimilitary.h"
30
31#include "aiplayer.h"
32
33/**********************************************************************/
37void dai_player_alloc(struct ai_type *ait, struct player *pplayer)
38{
39 struct ai_plr *player_data = fc_calloc(1, sizeof(struct ai_plr));
40
41 player_set_ai_data(pplayer, ait, player_data);
42
43 dai_data_init(ait, pplayer);
44}
45
46/**********************************************************************/
49void dai_player_free(struct ai_type *ait, struct player *pplayer)
50{
51 struct ai_plr *player_data = def_ai_player_data(pplayer, ait);
52
53 dai_data_close(ait, pplayer);
54
55 if (player_data != NULL) {
56 player_set_ai_data(pplayer, ait, NULL);
57 FC_FREE(player_data);
58 }
59}
60
61/**********************************************************************/
64void dai_player_save(struct ai_type *ait, const char *aitstr,
65 struct player *pplayer, struct section_file *file,
66 int plrno)
67{
68 players_iterate(other) {
69 dai_player_save_relations(ait, aitstr, pplayer, other, file, plrno);
71}
72
73/**********************************************************************/
76void dai_player_save_relations(struct ai_type *ait, const char *aitstr,
77 struct player *pplayer, struct player *other,
78 struct section_file *file, int plrno)
79{
80 struct ai_dip_intel *adip = dai_diplomacy_get(ait, pplayer, other);
81 char buf[32];
82
83 fc_snprintf(buf, sizeof(buf), "player%d.%s%d", plrno, aitstr,
84 player_index(other));
85
86 secfile_insert_int(file, adip->spam,
87 "%s.spam", buf);
88 secfile_insert_int(file, adip->countdown,
89 "%s.countdown", buf);
91 "%s.war_reason", buf);
93 "%s.patience", buf);
95 "%s.warn_space", buf);
97 "%s.ask_peace", buf);
99 "%s.ask_alliance", buf);
101 "%s.ask_ceasefire", buf);
102}
103
104/**********************************************************************/
107void dai_player_load(struct ai_type *ait, const char *aitstr,
108 struct player *pplayer,
109 const struct section_file *file, int plrno)
110{
111 players_iterate(other) {
112 dai_player_load_relations(ait, aitstr, pplayer, other, file, plrno);
114}
115
116/**********************************************************************/
119void dai_player_load_relations(struct ai_type *ait, const char *aitstr,
120 struct player *pplayer, struct player *other,
121 const struct section_file *file, int plrno)
122{
123 struct ai_dip_intel *adip = dai_diplomacy_get(ait, pplayer, other);
124 char buf[32];
125
126 fc_snprintf(buf, sizeof(buf), "player%d.%s%d", plrno, aitstr,
127 player_index(other));
128
129 adip->spam
130 = secfile_lookup_int_default(file, 0, "%s.spam", buf);
131 adip->countdown
132 = secfile_lookup_int_default(file, -1, "%s.countdown", buf);
133 adip->war_reason
134 = secfile_lookup_int_default(file, 0, "%s.war_reason", buf);
135 adip->ally_patience
136 = secfile_lookup_int_default(file, 0, "%s.patience", buf);
138 = secfile_lookup_int_default(file, 0, "%s.warn_space", buf);
140 = secfile_lookup_int_default(file, 0, "%s.ask_peace", buf);
142 = secfile_lookup_int_default(file, 0, "%s.ask_alliance", buf);
144 = secfile_lookup_int_default(file, 0, "%s.ask_ceasefire", buf);
145}
146
147/**********************************************************************/
150void dai_player_copy(struct ai_type *ait,
151 struct player *original, struct player *created)
152{
153 bool close_original;
154 bool close_created;
155 struct ai_plr *orig_data = dai_plr_data_get(ait, original, &close_original);
156 struct ai_plr *created_data = dai_plr_data_get(ait, created, &close_created);
157
159 created_data->tech_want[i] = orig_data->tech_want[i];
161
162 if (close_original) {
163 dai_data_phase_finished(ait, original);
164 }
165 if (close_created) {
166 dai_data_phase_finished(ait, created);
167 }
168}
169
170/**********************************************************************/
173void dai_gained_control(struct ai_type *ait, struct player *pplayer)
174{
175 if (pplayer->ai_common.skill_level != AI_LEVEL_AWAY) {
176 multipliers_iterate(pmul) {
177 pplayer->multipliers[multiplier_index(pmul)].target = pmul->def;
179
180 /* Clear worker tasks, some AIs (e.g. classic) does not use those */
181 city_list_iterate(pplayer->cities, pcity) {
182 clear_worker_tasks(pcity);
184 }
185
186 dai_assess_danger_player(ait, &(wld.map), pplayer);
187}
struct ai_plr * dai_plr_data_get(struct ai_type *ait, struct player *pplayer, bool *caller_closes)
Definition aidata.c:308
struct ai_dip_intel * dai_diplomacy_get(struct ai_type *ait, const struct player *plr1, const struct player *plr2)
Definition aidata.c:402
void dai_data_init(struct ai_type *ait, struct player *pplayer)
Definition aidata.c:59
void dai_data_phase_finished(struct ai_type *ait, struct player *pplayer)
Definition aidata.c:285
void dai_data_close(struct ai_type *ait, struct player *pplayer)
Definition aidata.c:104
void dai_player_alloc(struct ai_type *ait, struct player *pplayer)
Definition aiplayer.c:37
void dai_player_save(struct ai_type *ait, const char *aitstr, struct player *pplayer, struct section_file *file, int plrno)
Definition aiplayer.c:64
void dai_player_copy(struct ai_type *ait, struct player *original, struct player *created)
Definition aiplayer.c:150
void dai_player_save_relations(struct ai_type *ait, const char *aitstr, struct player *pplayer, struct player *other, struct section_file *file, int plrno)
Definition aiplayer.c:76
void dai_player_load(struct ai_type *ait, const char *aitstr, struct player *pplayer, const struct section_file *file, int plrno)
Definition aiplayer.c:107
void dai_player_free(struct ai_type *ait, struct player *pplayer)
Definition aiplayer.c:49
void dai_gained_control(struct ai_type *ait, struct player *pplayer)
Definition aiplayer.c:173
void dai_player_load_relations(struct ai_type *ait, const char *aitstr, struct player *pplayer, struct player *other, const struct section_file *file, int plrno)
Definition aiplayer.c:119
static struct ai_plr * def_ai_player_data(const struct player *pplayer, struct ai_type *deftype)
Definition aiplayer.h:54
#define city_list_iterate(citylist, pcity)
Definition city.h:488
#define city_list_iterate_end
Definition city.h:490
void clear_worker_tasks(struct city *pcity)
Definition citytools.c:3502
void dai_assess_danger_player(struct ai_type *ait, const struct civ_map *nmap, struct player *pplayer)
struct world wld
Definition game.c:58
#define fc_calloc(n, esz)
Definition mem.h:38
#define FC_FREE(ptr)
Definition mem.h:41
Multiplier_type_id multiplier_index(const struct multiplier *pmul)
Definition multipliers.c:80
#define multipliers_iterate(_mul_)
Definition multipliers.h:61
#define multipliers_iterate_end
Definition multipliers.h:67
void player_set_ai_data(struct player *pplayer, const struct ai_type *ai, void *data)
Definition player.c:1912
int player_index(const struct player *pplayer)
Definition player.c:820
#define players_iterate_end
Definition player.h:535
#define players_iterate(_pplayer)
Definition player.h:530
int secfile_lookup_int_default(const struct section_file *secfile, int def, const char *path,...)
#define secfile_insert_int(secfile, value, path,...)
signed char asked_about_alliance
Definition aidata.h:65
enum war_reason war_reason
Definition aidata.h:62
signed char spam
Definition aidata.h:59
signed char asked_about_peace
Definition aidata.h:64
signed char warned_about_space
Definition aidata.h:67
int countdown
Definition aidata.h:61
signed char ally_patience
Definition aidata.h:63
signed char asked_about_ceasefire
Definition aidata.h:66
adv_want tech_want[A_LAST+1]
Definition aidata.h:103
Definition ai.h:50
enum ai_level skill_level
Definition player.h:122
struct city_list * cities
Definition player.h:281
struct player_ai ai_common
Definition player.h:288
struct multiplier_value multipliers[MAX_NUM_MULTIPLIERS]
Definition player.h:310
struct civ_map map
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:969
#define advance_index_iterate_end
Definition tech.h:248
#define A_NONE
Definition tech.h:43
#define advance_index_iterate(_start, _index)
Definition tech.h:244