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)
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 ***********************************************************************/
18 #endif /* __cplusplus */
21 #include "bitvector.h"
26 #include "terrain.h" /* enum tile_special_type */
31 struct unit_move_data
; /* Actually defined in "server/unittools.c". */
33 /* Changing this enum will break network compatability. */
38 ORDER_ACTION_MOVE
= 3,
39 ORDER_PERFORM_ACTION
= 4,
40 /* and plenty more for later... */
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
{
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 */
80 enum adv_unit_task task
;
84 enum unit_orders order
;
85 enum unit_activity activity
; /* Only valid for ORDER_ACTIVITY. */
87 /* Only valid for ORDER_PERFORM_ACTION */
89 /* Valid for ORDER_MOVE, ORDER_ACTION_MOVE and
90 * ORDER_PERFORM_ACTION. */
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
104 #define SPECENUM_VALUE2 USSDT_BATTLE_GROUP
105 #include "specenum_gen.h"
111 struct unit_type
*utype
; /* Cannot be NULL. */
113 enum direction8 facing
;
114 struct player
*owner
; /* Cannot be NULL. */
115 struct player
*nationality
;
119 int upkeep
[O_LAST
]; /* unit upkeep with regards to the homecity */
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). */
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 */
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. */
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)
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
;
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 bool stay
; /* Unit is prohibited from moving */
180 /* Only used at the client (the server is omniscient; ./client/). */
182 enum unit_focus_status focus_status
;
184 int transported_by
; /* Used for unit_short_info packets where we can't
185 * be sure that the information about the
186 * transporter is known. */
187 bool occupied
; /* TRUE if at least one cargo on the transporter. */
189 /* Equivalent to pcity->client.color. Only for F_CITIES units. */
193 bool asking_city_name
;
195 /* Used in a follow up question about a selected action. */
196 struct act_prob
*act_prob_cache
;
200 /* Only used in the server (./ai/ and ./server/). */
204 struct unit_adv
*adv
;
205 void *ais
[FREECIV_AI_MOD_LAST
];
208 /* ord_map and ord_city are the order index of this unit in tile.units
209 * and city.units_supported; they are only used for save/reload */
213 struct vision
*vision
;
214 time_t action_timestamp
;
216 struct unit_move_data
*moving
;
218 /* The unit is in the process of dying. */
221 /* Call back to run on unit removal. */
222 void (*removal_callback
)(struct unit
*punit
);
224 /* The upkeep that actually was payed. */
225 int upkeep_payed
[O_LAST
];
231 #define CHECK_UNIT(punit) \
232 (fc_assert(punit != NULL), \
233 fc_assert(unit_type_get(punit) != NULL), \
234 fc_assert(unit_owner(punit) != NULL), \
235 fc_assert(player_by_number(player_index(unit_owner(punit))) \
236 == unit_owner(punit)), \
237 fc_assert(game_unit_by_number(punit->id) != NULL))
238 #else /* FREECIV_DEBUG */
239 #define CHECK_UNIT(punit) /* Do nothing */
240 #endif /* FREECIV_DEBUG */
242 void setup_real_activities_array(void);
244 extern Activity_type_id real_activities
[ACTIVITY_LAST
];
246 #define activity_type_list_iterate(_act_list_, _act_) \
249 for (_act_i_ = 0; _act_list_[_act_i_] != ACTIVITY_LAST; _act_i_++) { \
250 Activity_type_id _act_ = _act_list_[_act_i_];
252 #define activity_type_list_iterate_end \
256 /* Iterates over the types of unit activity. */
257 #define activity_type_iterate(_act_) \
259 activity_type_list_iterate(real_activities, _act_)
261 #define activity_type_iterate_end \
262 activity_type_list_iterate_end \
265 bool unit_can_help_build_wonder_here(const struct unit
*punit
);
266 bool unit_can_est_trade_route_here(const struct unit
*punit
);
267 enum unit_airlift_result
268 test_unit_can_airlift_to(const struct player
*restriction
,
269 const struct unit
*punit
,
270 const struct city
*pdest_city
);
271 bool unit_can_airlift_to(const struct unit
*punit
, const struct city
*pcity
);
272 bool unit_has_orders(const struct unit
*punit
);
274 bool could_unit_load(const struct unit
*pcargo
, const struct unit
*ptrans
);
275 bool can_unit_load(const struct unit
*punit
, const struct unit
*ptrans
);
276 bool can_unit_unload(const struct unit
*punit
, const struct unit
*ptrans
);
277 bool can_unit_paradrop(const struct unit
*punit
);
278 bool can_unit_change_homecity_to(const struct unit
*punit
,
279 const struct city
*pcity
);
280 bool can_unit_change_homecity(const struct unit
*punit
);
281 const char *get_activity_text(enum unit_activity activity
);
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 extra_type
*target
);
288 bool can_unit_do_activity_targeted_at(const struct unit
*punit
,
289 enum unit_activity activity
,
290 struct extra_type
*target
,
291 const struct tile
*ptile
);
292 void set_unit_activity(struct unit
*punit
, enum unit_activity new_activity
);
293 void set_unit_activity_targeted(struct unit
*punit
,
294 enum unit_activity new_activity
,
295 struct extra_type
*new_target
);
296 void set_unit_activity_base(struct unit
*punit
,
298 void set_unit_activity_road(struct unit
*punit
,
300 int get_activity_rate(const struct unit
*punit
);
301 int get_activity_rate_this_turn(const struct unit
*punit
);
302 int get_turns_for_activity_at(const struct unit
*punit
,
303 enum unit_activity activity
,
304 const struct tile
*ptile
,
305 struct extra_type
*tgt
);
306 bool activity_requires_target(enum unit_activity activity
);
307 bool can_unit_do_autosettlers(const struct unit
*punit
);
308 bool is_unit_activity_on_tile(enum unit_activity activity
,
309 const struct tile
*ptile
);
310 bv_extras
get_unit_tile_pillage_set(const struct tile
*ptile
);
311 bool is_attack_unit(const struct unit
*punit
);
312 bool is_military_unit(const struct unit
*punit
); /* !set !dip !cara */
313 bool unit_can_do_action(const struct unit
*punit
,
314 const int action_id
);
315 bool is_square_threatened(const struct player
*pplayer
,
316 const struct tile
*ptile
, bool omniscient
);
317 bool is_field_unit(const struct unit
*punit
); /* ships+aero */
318 bool is_hiding_unit(const struct unit
*punit
);
319 bool unit_can_add_or_build_city(const struct unit
*punit
);
321 bool kills_citizen_after_attack(const struct unit
*punit
);
323 struct astring
; /* Forward declaration. */
324 void unit_activity_astr(const struct unit
*punit
, struct astring
*astr
);
325 void unit_upkeep_astr(const struct unit
*punit
, struct astring
*astr
);
326 const char *unit_activity_text(const struct unit
*punit
);
328 int get_transporter_capacity(const struct unit
*punit
);
330 #define unit_owner(_pu) ((_pu)->owner)
331 #define unit_tile(_pu) ((_pu)->tile)
332 struct player
*unit_nationality(const struct unit
*punit
);
333 void unit_tile_set(struct unit
*punit
, struct tile
*ptile
);
335 struct unit
*is_allied_unit_tile(const struct tile
*ptile
,
336 const struct player
*pplayer
);
337 struct unit
*is_enemy_unit_tile(const struct tile
*ptile
,
338 const struct player
*pplayer
);
339 struct unit
*is_non_allied_unit_tile(const struct tile
*ptile
,
340 const struct player
*pplayer
);
341 struct unit
*is_other_players_unit_tile(const struct tile
*ptile
,
342 const struct player
*pplayer
);
343 struct unit
*is_non_attack_unit_tile(const struct tile
*ptile
,
344 const struct player
*pplayer
);
345 struct unit
*unit_occupies_tile(const struct tile
*ptile
,
346 const struct player
*pplayer
);
348 bool is_my_zoc(const struct player
*unit_owner
, const struct tile
*ptile
);
349 bool unit_being_aggressive(const struct unit
*punit
);
350 bool unit_type_really_ignores_zoc(const struct unit_type
*punittype
);
352 bool is_build_activity(enum unit_activity activity
, const struct tile
*ptile
);
353 bool is_clean_activity(enum unit_activity activity
);
354 bool is_tile_activity(enum unit_activity activity
);
356 struct unit
*unit_virtual_create(struct player
*pplayer
, struct city
*pcity
,
357 struct unit_type
*punittype
,
359 void unit_virtual_destroy(struct unit
*punit
);
360 bool unit_is_virtual(const struct unit
*punit
);
361 void free_unit_orders(struct unit
*punit
);
363 int get_transporter_occupancy(const struct unit
*ptrans
);
364 struct unit
*transporter_for_unit(const struct unit
*pcargo
);
365 struct unit
*transporter_for_unit_at(const struct unit
*pcargo
,
366 const struct tile
*ptile
);
368 enum unit_upgrade_result
unit_upgrade_test(const struct unit
*punit
,
370 enum unit_upgrade_result
unit_upgrade_info(const struct unit
*punit
,
371 char *buf
, size_t bufsz
);
372 bool unit_can_convert(const struct unit
*punit
);
374 bool is_losing_hp(const struct unit
*punit
);
375 bool unit_type_is_losing_hp(const struct player
*pplayer
,
376 const struct unit_type
*punittype
);
378 bool unit_is_alive(int id
);
380 void *unit_ai_data(const struct unit
*punit
, const struct ai_type
*ai
);
381 void unit_set_ai_data(struct unit
*punit
, const struct ai_type
*ai
,
384 int unit_bribe_cost(struct unit
*punit
, struct player
*briber
);
386 bool unit_transport_load(struct unit
*pcargo
, struct unit
*ptrans
,
388 bool unit_transport_unload(struct unit
*pcargo
);
389 struct unit
*unit_transport_get(const struct unit
*pcargo
);
390 bool unit_transported(const struct unit
*pcargo
);
391 struct unit_list
*unit_transport_cargo(const struct unit
*ptrans
);
392 bool unit_transport_check(const struct unit
*pcargo
,
393 const struct unit
*ptrans
);
394 bool unit_contained_in(const struct unit
*pcargo
, const struct unit
*ptrans
);
395 int unit_cargo_depth(const struct unit
*pcargo
);
396 int unit_transport_depth(const struct unit
*ptrans
);
398 bool unit_is_cityfounder(const struct unit
*punit
);
400 /* Iterate all transporters carrying '_pcargo', directly or indirectly. */
401 #define unit_transports_iterate(_pcargo, _ptrans) { \
402 struct unit *_ptrans; \
403 for (_ptrans = unit_transport_get(_pcargo); NULL != _ptrans; \
404 _ptrans = unit_transport_get(_ptrans)) {
405 #define unit_transports_iterate_end }}
408 size_t cargo_iter_sizeof(void) fc__attribute((const));
410 struct iterator
*cargo_iter_init(struct cargo_iter
*iter
,
411 const struct unit
*ptrans
);
412 #define unit_cargo_iterate(_ptrans, _pcargo) \
413 generic_iterate(struct cargo_iter, struct unit *, _pcargo, \
414 cargo_iter_sizeof, cargo_iter_init, _ptrans)
415 #define unit_cargo_iterate_end generic_iterate_end
419 #endif /* __cplusplus */
421 #endif /* FC__UNIT_H */