Freeciv-3.3
Loading...
Searching...
No Matches
aiguard.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 2004 - The Freeciv Project
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/* utility */
19#include "log.h"
20
21/* common */
22#include "game.h"
23#include "unit.h"
24
25/* server */
26#include "srv_log.h"
27
28/* ai/default */
29#include "dailog.h"
30#include "daiplayer.h"
31#include "daiunit.h"
32
33#include "aiguard.h"
34
39
40/**********************************************************************/
48void aiguard_check_guard(struct ai_type *ait, const struct unit *guard)
49{
51 const struct unit *charge_unit = game_unit_by_number(guard_data->charge);
52 const struct city *charge_city = game_city_by_number(guard_data->charge);
53 const struct player *guard_owner = unit_owner(guard);
54 const struct player *charge_owner = NULL;
55 struct unit_ai *charge_data = NULL;
56
58 /* IDs always distinct */
60
61 if (charge_unit) {
64 } else if (charge_city) {
66 }
67
68 if (charge_unit && charge_data->bodyguard != guard->id) {
69 BODYGUARD_LOG(ait, LOG_DEBUG, guard, "inconsistent guard references");
70 } else if (!charge_unit && !charge_city && 0 < guard_data->charge) {
71 BODYGUARD_LOG(ait, LOG_DEBUG, guard, "dangling guard reference");
72 }
74 /* Probably due to civil war */
75 BODYGUARD_LOG(ait, LOG_DEBUG, guard, "enemy charge");
76 } else if (charge_owner && charge_owner != guard_owner) {
77 /* Probably sold a city with its supported units. */
78 BODYGUARD_LOG(ait, LOG_DEBUG, guard, "foreign charge");
79 }
80}
81
82/**********************************************************************/
90void aiguard_check_charge_unit(struct ai_type *ait, const struct unit *charge)
91{
93 const struct player *charge_owner = unit_owner(charge);
94 const struct unit *guard = game_unit_by_number(charge_data->bodyguard);
95 struct unit_ai *guard_data = NULL;
96
97 if (guard) {
99 }
100
103
104 if (guard && guard_data->charge != charge->id) {
106 "inconsistent guard references");
107 } else if (guard && unit_owner(guard) != charge_owner) {
108 UNIT_LOG(LOG_DEBUG, charge, "foreign guard");
109 }
110}
111
112/**********************************************************************/
117void aiguard_clear_charge(struct ai_type *ait, struct unit *guard)
118{
122
123 /* IDs always distinct */
125
126 if (charge_unit) {
127 BODYGUARD_LOG(ait, LOGLEVEL_BODYGUARD, guard, "unassigned (unit)");
129 } else if (charge_city) {
130 BODYGUARD_LOG(ait, LOGLEVEL_BODYGUARD, guard, "unassigned (city)");
131 }
132 /* else not assigned or charge was destroyed */
133 guard_data->charge = BODYGUARD_NONE;
134
135 CHECK_GUARD(ait, guard);
136}
137
138/**********************************************************************/
146void aiguard_clear_guard(struct ai_type *ait, struct unit *charge)
147{
149
150 if (0 < charge_data->bodyguard) {
151 struct unit *guard = game_unit_by_number(charge_data->bodyguard);
152
153 if (guard) {
155
156 if (guard_data->charge == charge->id) {
157 /* charge doesn't want us anymore */
158 guard_data->charge = BODYGUARD_NONE;
159 }
160 }
161 }
162
163 charge_data->bodyguard = BODYGUARD_NONE;
164
166}
167
168/**********************************************************************/
174 struct unit *guard)
175{
180
181 /* Remove previous assignment: */
184
185 def_ai_unit_data(guard, ait)->charge = charge->id;
187
188 BODYGUARD_LOG(ait, LOGLEVEL_BODYGUARD, guard, "assigned charge");
189 CHECK_GUARD(ait, guard);
191}
192
193/**********************************************************************/
197 struct unit *guard)
198{
200
203 /*
204 * Usually, but not always, city_owner(charge) == unit_owner(guard).
205 */
206
207 if (0 < guard_data->charge
208 && guard_data->charge != charge->id) {
209 /* Remove previous assignment: */
211 }
212
213 guard_data->charge = charge->id;
215 /* Peculiar, but not always an error */
216 BODYGUARD_LOG(ait, LOGLEVEL_BODYGUARD, guard, "assigned foreign charge");
217 } else {
218 BODYGUARD_LOG(ait, LOGLEVEL_BODYGUARD, guard, "assigned charge");
219 }
220
221 CHECK_GUARD(ait, guard);
222}
223
224/**********************************************************************/
227void aiguard_request_guard(struct ai_type *ait, struct unit *punit)
228{
229 /* Remove previous assignment */
231
232 UNIT_LOG(LOGLEVEL_BODYGUARD, punit, "requests a guard");
234
236}
237
238/**********************************************************************/
241bool aiguard_wanted(struct ai_type *ait, struct unit *charge)
242{
245}
246
247/**********************************************************************/
250bool aiguard_has_charge(struct ai_type *ait, struct unit *guard)
251{
252 CHECK_GUARD(ait, guard);
253 return (def_ai_unit_data(guard, ait)->charge != BODYGUARD_NONE);
254}
255
256/**********************************************************************/
259bool aiguard_has_guard(struct ai_type *ait, struct unit *charge)
260{
262 return (0 < def_ai_unit_data(charge, ait)->bodyguard);
263}
264
265/**********************************************************************/
269struct unit *aiguard_guard_of(struct ai_type *ait, struct unit *charge)
270{
271 CHECK_CHARGE_UNIT(ait, charge);
272 return game_unit_by_number(def_ai_unit_data(charge, ait)->bodyguard);
273}
274
275/**********************************************************************/
279struct unit *aiguard_charge_unit(struct ai_type *ait, struct unit *guard)
280{
281 CHECK_GUARD(ait, guard);
282 return game_unit_by_number(def_ai_unit_data(guard, ait)->charge);
283}
284
285/**********************************************************************/
289struct city *aiguard_charge_city(struct ai_type *ait, struct unit *guard)
290{
291 CHECK_GUARD(ait, guard);
292 return game_city_by_number(def_ai_unit_data(guard, ait)->charge);
293}
294
295/**********************************************************************/
299void aiguard_update_charge(struct ai_type *ait, struct unit *guard)
300{
302 const struct unit *charge_unit = game_unit_by_number(guard_data->charge);
303 const struct city *charge_city = game_city_by_number(guard_data->charge);
304 const struct player *guard_owner = unit_owner(guard);
305 const struct player *charge_owner = NULL;
306
308 /* IDs always distinct */
310
311 if (charge_unit) {
313 } else if (charge_city) {
315 }
316
317 if (!charge_unit && !charge_city && 0 < guard_data->charge) {
318 guard_data->charge = BODYGUARD_NONE;
319 BODYGUARD_LOG(ait, LOGLEVEL_BODYGUARD, guard, "charge was destroyed");
320 }
322 BODYGUARD_LOG(ait, LOGLEVEL_BODYGUARD, guard, "charge transferred, dismiss");
324 }
325
326 CHECK_GUARD(ait, guard);
327}
struct city * aiguard_charge_city(struct ai_type *ait, struct unit *guard)
Definition aiguard.c:289
void aiguard_clear_charge(struct ai_type *ait, struct unit *guard)
Definition aiguard.c:117
void aiguard_request_guard(struct ai_type *ait, struct unit *punit)
Definition aiguard.c:227
bool aiguard_has_guard(struct ai_type *ait, struct unit *charge)
Definition aiguard.c:259
void aiguard_assign_guard_unit(struct ai_type *ait, struct unit *charge, struct unit *guard)
Definition aiguard.c:173
bool aiguard_wanted(struct ai_type *ait, struct unit *charge)
Definition aiguard.c:241
void aiguard_assign_guard_city(struct ai_type *ait, struct city *charge, struct unit *guard)
Definition aiguard.c:196
struct unit * aiguard_charge_unit(struct ai_type *ait, struct unit *guard)
Definition aiguard.c:279
bodyguard_enum
Definition aiguard.c:35
@ BODYGUARD_NONE
Definition aiguard.c:37
@ BODYGUARD_WANTED
Definition aiguard.c:36
void aiguard_clear_guard(struct ai_type *ait, struct unit *charge)
Definition aiguard.c:146
struct unit * aiguard_guard_of(struct ai_type *ait, struct unit *charge)
Definition aiguard.c:269
bool aiguard_has_charge(struct ai_type *ait, struct unit *guard)
Definition aiguard.c:250
void aiguard_update_charge(struct ai_type *ait, struct unit *guard)
Definition aiguard.c:299
void aiguard_check_charge_unit(struct ai_type *ait, const struct unit *charge)
Definition aiguard.c:90
void aiguard_check_guard(struct ai_type *ait, const struct unit *guard)
Definition aiguard.c:48
#define CHECK_CHARGE_UNIT(ait, charge)
Definition aiguard.h:22
#define CHECK_GUARD(ait, guard)
Definition aiguard.h:21
#define city_owner(_pcity_)
Definition city.h:560
char * incite_cost
Definition comments.c:76
#define BODYGUARD_LOG(ait, loglevel, punit, msg,...)
Definition dailog.h:69
static struct unit_ai * def_ai_unit_data(const struct unit *punit, struct ai_type *deftype)
Definition daiplayer.h:48
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 * punit
Definition dialogs_g.h:74
struct unit * game_unit_by_number(int id)
Definition game.c:115
struct city * game_city_by_number(int id)
Definition game.c:106
#define fc_assert_ret(condition)
Definition log.h:192
@ LOG_DEBUG
Definition log.h:35
bool pplayers_at_war(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1388
#define UNIT_LOG(loglevel, punit, msg,...)
Definition srv_log.h:98
#define LOGLEVEL_BODYGUARD
Definition srv_log.h:30
Definition ai.h:50
Definition city.h:317
int bodyguard
Definition daiunit.h:36
int charge
Definition daiunit.h:37
Definition unit.h:140
#define unit_owner(_pu)
Definition unit.h:403