Support changing players' science box in the editor.
[freeciv.git] / common / unit.h
blob095db91e81f84df697c4a49e5d812b942e0f3fd3
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 "ai.h" /* FC_AI_LAST */
25 #include "base.h"
26 #include "fc_types.h"
27 #include "terrain.h" /* enum tile_special_type */
28 #include "unittype.h"
29 #include "vision.h"
31 /* Changing this enum will break network compatability. */
32 enum unit_orders {
33 ORDER_MOVE = 0,
34 ORDER_ACTIVITY = 1,
35 ORDER_FULL_MP = 2,
36 ORDER_BUILD_CITY = 3,
37 ORDER_DISBAND = 4,
38 ORDER_BUILD_WONDER = 5,
39 ORDER_TRADE_ROUTE = 6,
40 ORDER_HOMECITY = 7,
41 /* and plenty more for later... */
42 ORDER_LAST
45 enum unit_focus_status {
46 FOCUS_AVAIL, FOCUS_WAIT, FOCUS_DONE
49 /* Changing this enum will break network compatability. */
50 enum diplomat_actions {
51 DIPLOMAT_MOVE = 0, /* move onto city square - only for allied cities */
52 DIPLOMAT_EMBASSY = 1,
53 DIPLOMAT_BRIBE = 2,
54 DIPLOMAT_INCITE = 3,
55 DIPLOMAT_INVESTIGATE = 4,
56 DIPLOMAT_SABOTAGE = 5,
57 DIPLOMAT_STEAL = 6,
58 SPY_POISON = 7,
59 SPY_SABOTAGE_UNIT = 8,
60 DIPLOMAT_ANY_ACTION /* leave this one last */
63 enum goto_route_type {
64 ROUTE_GOTO, ROUTE_PATROL
67 enum unit_add_build_city_result {
68 UAB_BUILD_OK, /* Unit OK to build city. */
69 UAB_ADD_OK, /* Unit OK to add to city. */
70 UAB_BAD_CITY_TERRAIN, /* Equivalent to 'CB_BAD_CITY_TERRAIN'. */
71 UAB_BAD_UNIT_TERRAIN, /* Equivalent to 'CB_BAD_UNIT_TERRAIN'. */
72 UAB_BAD_BORDERS, /* Equivalent to 'CB_BAD_BORDERS'. */
73 UAB_NO_MIN_DIST, /* Equivalent to 'CB_NO_MIN_DIST'. */
74 UAB_NOT_ADDABLE_UNIT, /* Unit is not one that can be added to cities. */
75 UAB_NOT_BUILD_UNIT, /* Unit is not one that can build cities. */
76 UAB_NO_MOVES_BUILD, /* Unit does not have moves left to build a city. */
77 UAB_NO_MOVES_ADD, /* Unit does not have moves left to add to city. */
78 UAB_NOT_OWNER, /* Owner of unit is not owner of city. */
79 UAB_TOO_BIG, /* City is too big to be added to. */
80 UAB_NO_SPACE /* Adding takes city past limit. */
83 enum unit_upgrade_result {
84 UU_OK,
85 UU_NO_UNITTYPE,
86 UU_NO_MONEY,
87 UU_NOT_IN_CITY,
88 UU_NOT_CITY_OWNER,
89 UU_NOT_ENOUGH_ROOM,
90 UU_NOT_TERRAIN /* The upgraded unit could not survive. */
93 enum unit_airlift_result {
94 /* Codes treated as success: */
95 AR_OK, /* This will definitely work */
96 AR_OK_SRC_UNKNOWN, /* Source city's airlift capability is unknown */
97 AR_OK_DST_UNKNOWN, /* Dest city's airlift capability is unknown */
98 /* Codes treated as failure: */
99 AR_NO_MOVES, /* Unit has no moves left */
100 AR_WRONG_UNITTYPE, /* Can't airlift this type of unit */
101 AR_OCCUPIED, /* Can't airlift units with passengers */
102 AR_NOT_IN_CITY, /* Unit not in a city */
103 AR_BAD_SRC_CITY, /* Can't airlift from this src city */
104 AR_BAD_DST_CITY, /* Can't airlift to this dst city */
105 AR_SRC_NO_FLIGHTS, /* No flights available from src */
106 AR_DST_NO_FLIGHTS /* No flights available to dst */
109 struct unit_adv {
110 enum adv_unit_task task;
113 struct unit_order {
114 enum unit_orders order;
115 enum unit_activity activity; /* Only valid for ORDER_ACTIVITY. */
116 Base_type_id base; /* Only valid for activity ACTIVITY_BASE */
117 Road_type_id road; /* Only valid for activity ACTIVITY_GEN_ROAD */
118 enum direction8 dir; /* Only valid for ORDER_MOVE. */
121 struct unit;
122 struct unit_list;
124 struct unit {
125 struct unit_type *utype; /* Cannot be NULL. */
126 struct tile *tile;
127 enum direction8 facing;
128 struct player *owner; /* Cannot be NULL. */
129 struct player *nationality;
130 int id;
131 int homecity;
133 int upkeep[O_LAST]; /* unit upkeep with regards to the homecity */
135 int moves_left;
136 int hp;
137 int veteran;
138 int fuel;
140 struct tile *goto_tile; /* May be NULL. */
142 enum unit_activity activity;
144 /* The amount of work that has been done on the current activity. This
145 * is counted in turns but is multiplied by ACTIVITY_COUNT (which allows
146 * fractional values in some cases). */
147 int activity_count;
149 struct act_tgt activity_target;
151 /* Previous activity, so it can be resumed without loss of progress
152 * if the user changes their mind during a turn. */
153 enum unit_activity changed_from;
154 int changed_from_count;
155 struct act_tgt changed_from_target;
157 bool ai_controlled; /* 0: not automated; 1: automated */
158 bool moved;
159 bool paradropped;
161 /* This value is set if the unit is done moving for this turn. This
162 * information is used by the client. The invariant is:
163 * - If the unit has no more moves, it's done moving.
164 * - If the unit is on a goto but is waiting, it's done moving.
165 * - Otherwise the unit is not done moving. */
166 bool done_moving;
168 struct unit *transporter; /* This unit is transported by ... */
169 struct unit_list *transporting; /* This unit transports ... */
171 /* The battlegroup ID: defined by the client but stored by the server. */
172 #define MAX_NUM_BATTLEGROUPS (4)
173 #define BATTLEGROUP_NONE (-1)
174 int battlegroup;
176 bool has_orders;
177 struct {
178 int length, index;
179 bool repeat; /* The path is to be repeated on completion. */
180 bool vigilant; /* Orders should be cleared if an enemy is met. */
181 struct unit_order *list;
182 } orders;
184 union {
185 struct {
186 /* Only used at the client (the server is omniscient; ./client/). */
188 enum unit_focus_status focus_status;
190 int transported_by; /* Used for unit_short_info packets where we can't
191 * be sure that the information about the
192 * transporter is known. */
193 bool occupied; /* TRUE if at least one cargo on the transporter. */
195 /* Equivalent to pcity->client.color. Only for F_CITIES units. */
196 bool colored;
197 int color_index;
199 bool asking_city_name;
200 } client;
202 struct {
203 /* Only used in the server (./ai/ and ./server/). */
205 bool debug;
207 struct unit_adv *adv;
208 void *ais[FC_AI_LAST];
209 int birth_turn;
211 /* ord_map and ord_city are the order index of this unit in tile.units
212 * and city.units_supported; they are only used for save/reload */
213 int ord_map;
214 int ord_city;
216 struct vision *vision;
217 time_t action_timestamp;
218 int action_turn;
220 bool last_order_move_is_safe;
221 } server;
225 #ifdef DEBUG
226 #define CHECK_UNIT(punit) \
227 (fc_assert(punit != NULL), \
228 fc_assert(unit_type(punit) != NULL), \
229 fc_assert(unit_owner(punit) != NULL), \
230 fc_assert(player_by_number(player_index(unit_owner(punit))) \
231 == unit_owner(punit)), \
232 fc_assert(game_unit_by_number(punit->id) != NULL))
233 #else
234 #define CHECK_UNIT(punit) /* Do nothing */
235 #endif
237 void setup_real_activities_array(void);
239 extern Activity_type_id real_activities[ACTIVITY_LAST];
241 /* Iterates over the types of unit activity. */
242 #define activity_type_iterate(act) \
244 int _act_i_; \
246 for (_act_i_ = 0; real_activities[_act_i_] != ACTIVITY_LAST; _act_i_++) { \
247 Activity_type_id act = real_activities[_act_i_];
249 #define activity_type_iterate_end \
253 bool diplomat_can_do_action(const struct unit *pdiplomat,
254 enum diplomat_actions action,
255 const struct tile *ptile);
256 bool is_diplomat_action_available(const struct unit *pdiplomat,
257 enum diplomat_actions action,
258 const struct tile *ptile);
260 bool unit_can_help_build_wonder(const struct unit *punit,
261 const struct city *pcity);
262 bool unit_can_help_build_wonder_here(const struct unit *punit);
263 bool unit_can_est_trade_route_here(const struct unit *punit);
264 enum unit_airlift_result
265 test_unit_can_airlift_to(const struct player *restriction,
266 const struct unit *punit,
267 const struct city *pdest_city);
268 bool is_successful_airlift_result(enum unit_airlift_result result);
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_bombard(const struct unit *punit);
277 bool can_unit_change_homecity_to(const struct unit *punit,
278 const struct city *pcity);
279 bool can_unit_change_homecity(const struct unit *punit);
280 const char *get_activity_text(enum unit_activity activity);
281 bool cmp_act_tgt(struct act_tgt *act1, struct act_tgt *act2);
282 bool can_unit_continue_current_activity(struct unit *punit);
283 bool can_unit_do_activity(const struct unit *punit,
284 enum unit_activity activity);
285 bool can_unit_do_activity_targeted(const struct unit *punit,
286 enum unit_activity activity,
287 struct act_tgt *target);
288 bool can_unit_do_activity_targeted_at(const struct unit *punit,
289 enum unit_activity activity,
290 struct act_tgt *target,
291 const struct tile *ptile);
292 bool can_unit_do_activity_base(const struct unit *punit,
293 Base_type_id base);
294 bool can_unit_do_activity_road(const struct unit *punit,
295 Road_type_id road);
296 void set_unit_activity(struct unit *punit, enum unit_activity new_activity);
297 void set_unit_activity_targeted(struct unit *punit,
298 enum unit_activity new_activity,
299 struct act_tgt *new_target);
300 void set_unit_activity_base(struct unit *punit,
301 Base_type_id base);
302 void set_unit_activity_road(struct unit *punit,
303 Road_type_id road);
304 int get_activity_rate(const struct unit *punit);
305 int get_activity_rate_this_turn(const struct unit *punit);
306 int get_turns_for_activity_at(const struct unit *punit,
307 enum unit_activity activity,
308 const struct tile *ptile);
309 int get_turns_for_road_at(const struct unit *punit,
310 const struct road_type *proad,
311 const struct tile *ptile);
312 int get_turns_for_base_at(const struct unit *punit,
313 const struct base_type *pbase,
314 const struct tile *ptile);
315 bool activity_requires_target(enum unit_activity activity);
316 bool can_unit_do_autosettlers(const struct unit *punit);
317 bool is_unit_activity_on_tile(enum unit_activity activity,
318 const struct tile *ptile);
319 bv_special get_unit_tile_pillage_set(const struct tile *ptile);
320 bv_bases get_unit_tile_pillage_base_set(const struct tile *ptile);
321 bv_roads get_unit_tile_pillage_road_set(const struct tile *ptile);
322 bool is_attack_unit(const struct unit *punit);
323 bool is_military_unit(const struct unit *punit); /* !set !dip !cara */
324 bool is_diplomat_unit(const struct unit *punit);
325 bool is_square_threatened(const struct player *pplayer,
326 const struct tile *ptile);
327 bool is_field_unit(const struct unit *punit); /* ships+aero */
328 bool is_hiding_unit(const struct unit *punit);
329 bool unit_can_add_to_city(const struct unit *punit);
330 bool unit_can_build_city(const struct unit *punit);
331 bool unit_can_add_or_build_city(const struct unit *punit);
332 enum unit_add_build_city_result
333 unit_add_or_build_city_test(const struct unit *punit);
334 bool kills_citizen_after_attack(const struct unit *punit);
336 struct astring; /* Forward declaration. */
337 void unit_activity_astr(const struct unit *punit, struct astring *astr);
338 void unit_upkeep_astr(const struct unit *punit, struct astring *astr);
339 const char *unit_activity_text(const struct unit *punit);
341 int get_transporter_capacity(const struct unit *punit);
343 struct player *unit_owner(const struct unit *punit);
344 struct player *unit_nationality(const struct unit *punit);
345 struct tile *unit_tile(const struct unit *punit);
346 void unit_tile_set(struct unit *punit, struct tile *ptile);
348 struct unit *is_allied_unit_tile(const struct tile *ptile,
349 const struct player *pplayer);
350 struct unit *is_enemy_unit_tile(const struct tile *ptile,
351 const struct player *pplayer);
352 struct unit *is_non_allied_unit_tile(const struct tile *ptile,
353 const struct player *pplayer);
354 struct unit *is_non_attack_unit_tile(const struct tile *ptile,
355 const struct player *pplayer);
356 struct unit *unit_occupies_tile(const struct tile *ptile,
357 const struct player *pplayer);
359 bool is_my_zoc(const struct player *unit_owner, const struct tile *ptile);
360 bool unit_being_aggressive(const struct unit *punit);
361 bool unit_type_really_ignores_zoc(const struct unit_type *punittype);
363 bool is_build_or_clean_activity(enum unit_activity activity);
365 struct unit *unit_virtual_create(struct player *pplayer, struct city *pcity,
366 struct unit_type *punittype,
367 int veteran_level);
368 void unit_virtual_destroy(struct unit *punit);
369 bool unit_is_virtual(const struct unit *punit);
370 void free_unit_orders(struct unit *punit);
372 int get_transporter_occupancy(const struct unit *ptrans);
373 struct unit *transporter_for_unit(const struct unit *pcargo);
375 enum unit_upgrade_result unit_upgrade_test(const struct unit *punit,
376 bool is_free);
377 enum unit_upgrade_result unit_upgrade_info(const struct unit *punit,
378 char *buf, size_t bufsz);
379 bool unit_can_convert(const struct unit *punit);
381 bool is_losing_hp(const struct unit *punit);
382 bool unit_type_is_losing_hp(const struct player *pplayer,
383 const struct unit_type *punittype);
385 bool unit_alive(int id);
387 void *unit_ai_data(const struct unit *punit, const struct ai_type *ai);
388 void unit_set_ai_data(struct unit *punit, const struct ai_type *ai,
389 void *data);
391 int unit_bribe_cost(struct unit *punit);
393 bool unit_transport_load(struct unit *pcargo, struct unit *ptrans,
394 bool force);
395 bool unit_transport_unload(struct unit *pcargo);
396 struct unit *unit_transport_get(const struct unit *pcargo);
397 bool unit_transported(const struct unit *pcargo);
398 struct unit_list *unit_transport_cargo(const struct unit *ptrans);
399 bool unit_transport_check(const struct unit *pcargo,
400 const struct unit *ptrans);
401 bool unit_contained_in(const struct unit *pcargo, const struct unit *ptrans);
402 int unit_cargo_depth(const struct unit *pcargo);
403 int unit_transport_depth(const struct unit *ptrans);
405 /* Iterate all transporters carrying '_pcargo', directly or indirectly. */
406 #define unit_transports_iterate(_pcargo, _ptrans) { \
407 struct unit *_ptrans; \
408 for (_ptrans = unit_transport_get(_pcargo); NULL != _ptrans; \
409 _ptrans = unit_transport_get(_ptrans)) {
410 #define unit_transports_iterate_end }}
412 struct cargo_iter;
413 size_t cargo_iter_sizeof(void) fc__attribute((const));
415 struct iterator *cargo_iter_init(struct cargo_iter *iter,
416 const struct unit *ptrans);
417 #define unit_cargo_iterate(_ptrans, _pcargo) \
418 generic_iterate(struct cargo_iter, struct unit *, _pcargo, \
419 cargo_iter_sizeof, cargo_iter_init, _ptrans)
420 #define unit_cargo_iterate_end generic_iterate_end
422 #ifdef __cplusplus
424 #endif /* __cplusplus */
426 #endif /* FC__UNIT_H */