Stop sharing requirement_unit_state_ereq().
[freeciv.git] / common / unit.h
blobf67024618abc96c26401077594356833151f9c0d
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.
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 #ifndef FC__UNIT_H
14 #define FC__UNIT_H
16 #ifdef __cplusplus
17 extern "C" {
18 #endif /* __cplusplus */
20 /* utility */
21 #include "bitvector.h"
23 /* common */
24 #include "base.h"
25 #include "fc_types.h"
26 #include "terrain.h" /* enum tile_special_type */
27 #include "unittype.h"
28 #include "vision.h"
30 struct road_type;
31 struct unit_move_data; /* Actually defined in "server/unittools.c". */
33 /* Changing this enum will break network compatability. */
34 enum unit_orders {
35 ORDER_MOVE = 0,
36 ORDER_ACTIVITY = 1,
37 ORDER_FULL_MP = 2,
38 ORDER_ACTION_MOVE = 3,
39 ORDER_PERFORM_ACTION = 4,
40 /* and plenty more for later... */
41 ORDER_LAST
44 enum unit_focus_status {
45 FOCUS_AVAIL, FOCUS_WAIT, FOCUS_DONE
48 enum goto_route_type {
49 ROUTE_GOTO, ROUTE_PATROL
52 enum unit_upgrade_result {
53 UU_OK,
54 UU_NO_UNITTYPE,
55 UU_NO_MONEY,
56 UU_NOT_IN_CITY,
57 UU_NOT_CITY_OWNER,
58 UU_NOT_ENOUGH_ROOM,
59 UU_NOT_TERRAIN, /* The upgraded unit could not survive. */
60 UU_UNSUITABLE_TRANSPORT /* Can't upgrade inside current transport. */
63 enum unit_airlift_result {
64 /* Codes treated as success: */
65 AR_OK, /* This will definitely work */
66 AR_OK_SRC_UNKNOWN, /* Source city's airlift capability is unknown */
67 AR_OK_DST_UNKNOWN, /* Dest city's airlift capability is unknown */
68 /* Codes treated as failure: */
69 AR_NO_MOVES, /* Unit has no moves left */
70 AR_WRONG_UNITTYPE, /* Can't airlift this type of unit */
71 AR_OCCUPIED, /* Can't airlift units with passengers */
72 AR_NOT_IN_CITY, /* Unit not in a city */
73 AR_BAD_SRC_CITY, /* Can't airlift from this src city */
74 AR_BAD_DST_CITY, /* Can't airlift to this dst city */
75 AR_SRC_NO_FLIGHTS, /* No flights available from src */
76 AR_DST_NO_FLIGHTS /* No flights available to dst */
79 struct unit_adv {
80 enum adv_unit_task task;
83 struct unit_order {
84 enum unit_orders order;
85 enum unit_activity activity; /* Only valid for ORDER_ACTIVITY. */
86 int target;
87 /* Only valid for ORDER_PERFORM_ACTION */
88 int action;
89 /* Valid for ORDER_MOVE, ORDER_ACTION_MOVE and
90 * ORDER_PERFORM_ACTION. */
91 enum direction8 dir;
94 /* Used in the network protocol */
95 #define SPECENUM_NAME unit_ss_data_type
96 /* The player wants to be reminded to ask what actions the unit can perform
97 * to a certain target tile. */
98 #define SPECENUM_VALUE0 USSDT_QUEUE
99 /* The player no longer wants the reminder to ask what actions the unit can
100 * perform to a certain target tile. */
101 #define SPECENUM_VALUE1 USSDT_UNQUEUE
102 /* The player wants to record that the unit now belongs to the specified
103 * battle group. */
104 #define SPECENUM_VALUE2 USSDT_BATTLE_GROUP
105 #include "specenum_gen.h"
107 struct unit;
108 struct unit_list;
110 struct unit {
111 struct unit_type *utype; /* Cannot be NULL. */
112 struct tile *tile;
113 enum direction8 facing;
114 struct player *owner; /* Cannot be NULL. */
115 struct player *nationality;
116 int id;
117 int homecity;
119 int upkeep[O_LAST]; /* unit upkeep with regards to the homecity */
121 int moves_left;
122 int hp;
123 int veteran;
124 int fuel;
126 struct tile *goto_tile; /* May be NULL. */
128 enum unit_activity activity;
130 /* The amount of work that has been done on the current activity. This
131 * is counted in turns but is multiplied by ACTIVITY_FACTOR (which allows
132 * fractional values in some cases). */
133 int activity_count;
135 struct extra_type *activity_target;
137 /* Previous activity, so it can be resumed without loss of progress
138 * if the user changes their mind during a turn. */
139 enum unit_activity changed_from;
140 int changed_from_count;
141 struct extra_type *changed_from_target;
143 bool ai_controlled; /* 0: not automated; 1: automated */
144 bool moved;
145 bool paradropped;
147 /* This value is set if the unit is done moving for this turn. This
148 * information is used by the client. The invariant is:
149 * - If the unit has no more moves, it's done moving.
150 * - If the unit is on a goto but is waiting, it's done moving.
151 * - Otherwise the unit is not done moving. */
152 bool done_moving;
154 struct unit *transporter; /* This unit is transported by ... */
155 struct unit_list *transporting; /* This unit transports ... */
157 struct goods_type *carrying;
159 /* The battlegroup ID: defined by the client but stored by the server. */
160 #define MAX_NUM_BATTLEGROUPS (4)
161 #define BATTLEGROUP_NONE (-1)
162 int battlegroup;
164 bool has_orders;
165 struct {
166 int length, index;
167 bool repeat; /* The path is to be repeated on completion. */
168 bool vigilant; /* Orders should be cleared if an enemy is met. */
169 struct unit_order *list;
170 } orders;
172 /* The unit may want the player to choose an action. */
173 enum action_decision action_decision_want;
174 struct tile *action_decision_tile;
176 union {
177 struct {
178 /* Only used at the client (the server is omniscient; ./client/). */
180 enum unit_focus_status focus_status;
182 int transported_by; /* Used for unit_short_info packets where we can't
183 * be sure that the information about the
184 * transporter is known. */
185 bool occupied; /* TRUE if at least one cargo on the transporter. */
187 /* Equivalent to pcity->client.color. Only for F_CITIES units. */
188 bool colored;
189 int color_index;
191 bool asking_city_name;
193 /* Used in a follow up question about a selected action. */
194 struct act_prob *act_prob_cache;
195 } client;
197 struct {
198 /* Only used in the server (./ai/ and ./server/). */
200 bool debug;
202 struct unit_adv *adv;
203 void *ais[FREECIV_AI_MOD_LAST];
204 int birth_turn;
206 /* ord_map and ord_city are the order index of this unit in tile.units
207 * and city.units_supported; they are only used for save/reload */
208 int ord_map;
209 int ord_city;
211 struct vision *vision;
212 time_t action_timestamp;
213 int action_turn;
214 struct unit_move_data *moving;
216 /* The unit is in the process of dying. */
217 bool dying;
219 /* Call back to run on unit removal. */
220 void (*removal_callback)(struct unit *punit);
222 /* The upkeep that actually was payed. */
223 int upkeep_payed[O_LAST];
224 } server;
228 #ifdef FREECIV_DEBUG
229 #define CHECK_UNIT(punit) \
230 (fc_assert(punit != NULL), \
231 fc_assert(unit_type_get(punit) != NULL), \
232 fc_assert(unit_owner(punit) != NULL), \
233 fc_assert(player_by_number(player_index(unit_owner(punit))) \
234 == unit_owner(punit)), \
235 fc_assert(game_unit_by_number(punit->id) != NULL))
236 #else /* FREECIV_DEBUG */
237 #define CHECK_UNIT(punit) /* Do nothing */
238 #endif /* FREECIV_DEBUG */
240 void setup_real_activities_array(void);
242 extern Activity_type_id real_activities[ACTIVITY_LAST];
244 #define activity_type_list_iterate(_act_list_, _act_) \
246 int _act_i_; \
247 for (_act_i_ = 0; _act_list_[_act_i_] != ACTIVITY_LAST; _act_i_++) { \
248 Activity_type_id _act_ = _act_list_[_act_i_];
250 #define activity_type_list_iterate_end \
254 /* Iterates over the types of unit activity. */
255 #define activity_type_iterate(_act_) \
257 activity_type_list_iterate(real_activities, _act_)
259 #define activity_type_iterate_end \
260 activity_type_list_iterate_end \
263 bool unit_can_help_build_wonder_here(const struct unit *punit);
264 bool unit_can_est_trade_route_here(const struct unit *punit);
265 enum unit_airlift_result
266 test_unit_can_airlift_to(const struct player *restriction,
267 const struct unit *punit,
268 const struct city *pdest_city);
269 bool unit_can_airlift_to(const struct unit *punit, const struct city *pcity);
270 bool unit_has_orders(const struct unit *punit);
272 bool could_unit_load(const struct unit *pcargo, const struct unit *ptrans);
273 bool can_unit_load(const struct unit *punit, const struct unit *ptrans);
274 bool can_unit_unload(const struct unit *punit, const struct unit *ptrans);
275 bool can_unit_paradrop(const struct unit *punit);
276 bool can_unit_change_homecity_to(const struct unit *punit,
277 const struct city *pcity);
278 bool can_unit_change_homecity(const struct unit *punit);
279 const char *get_activity_text(enum unit_activity activity);
280 bool can_unit_continue_current_activity(struct unit *punit);
281 bool can_unit_do_activity(const struct unit *punit,
282 enum unit_activity activity);
283 bool can_unit_do_activity_targeted(const struct unit *punit,
284 enum unit_activity activity,
285 struct extra_type *target);
286 bool can_unit_do_activity_targeted_at(const struct unit *punit,
287 enum unit_activity activity,
288 struct extra_type *target,
289 const struct tile *ptile);
290 void set_unit_activity(struct unit *punit, enum unit_activity new_activity);
291 void set_unit_activity_targeted(struct unit *punit,
292 enum unit_activity new_activity,
293 struct extra_type *new_target);
294 void set_unit_activity_base(struct unit *punit,
295 Base_type_id base);
296 void set_unit_activity_road(struct unit *punit,
297 Road_type_id road);
298 int get_activity_rate(const struct unit *punit);
299 int get_activity_rate_this_turn(const struct unit *punit);
300 int get_turns_for_activity_at(const struct unit *punit,
301 enum unit_activity activity,
302 const struct tile *ptile,
303 struct extra_type *tgt);
304 bool activity_requires_target(enum unit_activity activity);
305 bool can_unit_do_autosettlers(const struct unit *punit);
306 bool is_unit_activity_on_tile(enum unit_activity activity,
307 const struct tile *ptile);
308 bv_extras get_unit_tile_pillage_set(const struct tile *ptile);
309 bool is_attack_unit(const struct unit *punit);
310 bool is_military_unit(const struct unit *punit); /* !set !dip !cara */
311 bool is_diplomat_unit(const struct unit *punit);
312 bool unit_can_do_action(const struct unit *punit,
313 const int action_id);
314 bool is_square_threatened(const struct player *pplayer,
315 const struct tile *ptile, bool omniscient);
316 bool is_field_unit(const struct unit *punit); /* ships+aero */
317 bool is_hiding_unit(const struct unit *punit);
318 bool unit_can_add_or_build_city(const struct unit *punit);
320 bool kills_citizen_after_attack(const struct unit *punit);
322 struct astring; /* Forward declaration. */
323 void unit_activity_astr(const struct unit *punit, struct astring *astr);
324 void unit_upkeep_astr(const struct unit *punit, struct astring *astr);
325 const char *unit_activity_text(const struct unit *punit);
327 int get_transporter_capacity(const struct unit *punit);
329 #define unit_owner(_pu) ((_pu)->owner)
330 #define unit_tile(_pu) ((_pu)->tile)
331 struct player *unit_nationality(const struct unit *punit);
332 void unit_tile_set(struct unit *punit, struct tile *ptile);
334 struct unit *is_allied_unit_tile(const struct tile *ptile,
335 const struct player *pplayer);
336 struct unit *is_enemy_unit_tile(const struct tile *ptile,
337 const struct player *pplayer);
338 struct unit *is_non_allied_unit_tile(const struct tile *ptile,
339 const struct player *pplayer);
340 struct unit *is_other_players_unit_tile(const struct tile *ptile,
341 const struct player *pplayer);
342 struct unit *is_non_attack_unit_tile(const struct tile *ptile,
343 const struct player *pplayer);
344 struct unit *unit_occupies_tile(const struct tile *ptile,
345 const struct player *pplayer);
347 bool is_my_zoc(const struct player *unit_owner, const struct tile *ptile);
348 bool unit_being_aggressive(const struct unit *punit);
349 bool unit_type_really_ignores_zoc(const struct unit_type *punittype);
351 bool is_build_activity(enum unit_activity activity, const struct tile *ptile);
352 bool is_clean_activity(enum unit_activity activity);
353 bool is_tile_activity(enum unit_activity activity);
355 struct unit *unit_virtual_create(struct player *pplayer, struct city *pcity,
356 struct unit_type *punittype,
357 int veteran_level);
358 void unit_virtual_destroy(struct unit *punit);
359 bool unit_is_virtual(const struct unit *punit);
360 void free_unit_orders(struct unit *punit);
362 int get_transporter_occupancy(const struct unit *ptrans);
363 struct unit *transporter_for_unit(const struct unit *pcargo);
364 struct unit *transporter_for_unit_at(const struct unit *pcargo,
365 const struct tile *ptile);
367 enum unit_upgrade_result unit_upgrade_test(const struct unit *punit,
368 bool is_free);
369 enum unit_upgrade_result unit_upgrade_info(const struct unit *punit,
370 char *buf, size_t bufsz);
371 bool unit_can_convert(const struct unit *punit);
373 bool is_losing_hp(const struct unit *punit);
374 bool unit_type_is_losing_hp(const struct player *pplayer,
375 const struct unit_type *punittype);
377 bool unit_is_alive(int id);
379 void *unit_ai_data(const struct unit *punit, const struct ai_type *ai);
380 void unit_set_ai_data(struct unit *punit, const struct ai_type *ai,
381 void *data);
383 int unit_bribe_cost(struct unit *punit, struct player *briber);
385 bool unit_transport_load(struct unit *pcargo, struct unit *ptrans,
386 bool force);
387 bool unit_transport_unload(struct unit *pcargo);
388 struct unit *unit_transport_get(const struct unit *pcargo);
389 bool unit_transported(const struct unit *pcargo);
390 struct unit_list *unit_transport_cargo(const struct unit *ptrans);
391 bool unit_transport_check(const struct unit *pcargo,
392 const struct unit *ptrans);
393 bool unit_contained_in(const struct unit *pcargo, const struct unit *ptrans);
394 int unit_cargo_depth(const struct unit *pcargo);
395 int unit_transport_depth(const struct unit *ptrans);
397 bool unit_is_cityfounder(const struct unit *punit);
399 /* Iterate all transporters carrying '_pcargo', directly or indirectly. */
400 #define unit_transports_iterate(_pcargo, _ptrans) { \
401 struct unit *_ptrans; \
402 for (_ptrans = unit_transport_get(_pcargo); NULL != _ptrans; \
403 _ptrans = unit_transport_get(_ptrans)) {
404 #define unit_transports_iterate_end }}
406 struct cargo_iter;
407 size_t cargo_iter_sizeof(void) fc__attribute((const));
409 struct iterator *cargo_iter_init(struct cargo_iter *iter,
410 const struct unit *ptrans);
411 #define unit_cargo_iterate(_ptrans, _pcargo) \
412 generic_iterate(struct cargo_iter, struct unit *, _pcargo, \
413 cargo_iter_sizeof, cargo_iter_init, _ptrans)
414 #define unit_cargo_iterate_end generic_iterate_end
416 #ifdef __cplusplus
418 #endif /* __cplusplus */
420 #endif /* FC__UNIT_H */