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
;
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. */
191 bool asking_city_name
;
193 /* Used in a follow up question about a selected action. */
194 struct act_prob
*act_prob_cache
;
198 /* Only used in the server (./ai/ and ./server/). */
202 struct unit_adv
*adv
;
203 void *ais
[FREECIV_AI_MOD_LAST
];
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 */
211 struct vision
*vision
;
212 time_t action_timestamp
;
214 struct unit_move_data
*moving
;
216 /* The unit is in the process of 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
];
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_) \
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
,
296 void set_unit_activity_road(struct unit
*punit
,
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 unit_can_do_action(const struct unit
*punit
,
312 const int action_id
);
313 bool is_square_threatened(const struct player
*pplayer
,
314 const struct tile
*ptile
, bool omniscient
);
315 bool is_field_unit(const struct unit
*punit
); /* ships+aero */
316 bool is_hiding_unit(const struct unit
*punit
);
317 bool unit_can_add_or_build_city(const struct unit
*punit
);
319 bool kills_citizen_after_attack(const struct unit
*punit
);
321 struct astring
; /* Forward declaration. */
322 void unit_activity_astr(const struct unit
*punit
, struct astring
*astr
);
323 void unit_upkeep_astr(const struct unit
*punit
, struct astring
*astr
);
324 const char *unit_activity_text(const struct unit
*punit
);
326 int get_transporter_capacity(const struct unit
*punit
);
328 #define unit_owner(_pu) ((_pu)->owner)
329 #define unit_tile(_pu) ((_pu)->tile)
330 struct player
*unit_nationality(const struct unit
*punit
);
331 void unit_tile_set(struct unit
*punit
, struct tile
*ptile
);
333 struct unit
*is_allied_unit_tile(const struct tile
*ptile
,
334 const struct player
*pplayer
);
335 struct unit
*is_enemy_unit_tile(const struct tile
*ptile
,
336 const struct player
*pplayer
);
337 struct unit
*is_non_allied_unit_tile(const struct tile
*ptile
,
338 const struct player
*pplayer
);
339 struct unit
*is_other_players_unit_tile(const struct tile
*ptile
,
340 const struct player
*pplayer
);
341 struct unit
*is_non_attack_unit_tile(const struct tile
*ptile
,
342 const struct player
*pplayer
);
343 struct unit
*unit_occupies_tile(const struct tile
*ptile
,
344 const struct player
*pplayer
);
346 bool is_my_zoc(const struct player
*unit_owner
, const struct tile
*ptile
);
347 bool unit_being_aggressive(const struct unit
*punit
);
348 bool unit_type_really_ignores_zoc(const struct unit_type
*punittype
);
350 bool is_build_activity(enum unit_activity activity
, const struct tile
*ptile
);
351 bool is_clean_activity(enum unit_activity activity
);
352 bool is_tile_activity(enum unit_activity activity
);
354 struct unit
*unit_virtual_create(struct player
*pplayer
, struct city
*pcity
,
355 struct unit_type
*punittype
,
357 void unit_virtual_destroy(struct unit
*punit
);
358 bool unit_is_virtual(const struct unit
*punit
);
359 void free_unit_orders(struct unit
*punit
);
361 int get_transporter_occupancy(const struct unit
*ptrans
);
362 struct unit
*transporter_for_unit(const struct unit
*pcargo
);
363 struct unit
*transporter_for_unit_at(const struct unit
*pcargo
,
364 const struct tile
*ptile
);
366 enum unit_upgrade_result
unit_upgrade_test(const struct unit
*punit
,
368 enum unit_upgrade_result
unit_upgrade_info(const struct unit
*punit
,
369 char *buf
, size_t bufsz
);
370 bool unit_can_convert(const struct unit
*punit
);
372 bool is_losing_hp(const struct unit
*punit
);
373 bool unit_type_is_losing_hp(const struct player
*pplayer
,
374 const struct unit_type
*punittype
);
376 bool unit_is_alive(int id
);
378 void *unit_ai_data(const struct unit
*punit
, const struct ai_type
*ai
);
379 void unit_set_ai_data(struct unit
*punit
, const struct ai_type
*ai
,
382 int unit_bribe_cost(struct unit
*punit
, struct player
*briber
);
384 bool unit_transport_load(struct unit
*pcargo
, struct unit
*ptrans
,
386 bool unit_transport_unload(struct unit
*pcargo
);
387 struct unit
*unit_transport_get(const struct unit
*pcargo
);
388 bool unit_transported(const struct unit
*pcargo
);
389 struct unit_list
*unit_transport_cargo(const struct unit
*ptrans
);
390 bool unit_transport_check(const struct unit
*pcargo
,
391 const struct unit
*ptrans
);
392 bool unit_contained_in(const struct unit
*pcargo
, const struct unit
*ptrans
);
393 int unit_cargo_depth(const struct unit
*pcargo
);
394 int unit_transport_depth(const struct unit
*ptrans
);
396 bool unit_is_cityfounder(const struct unit
*punit
);
398 /* Iterate all transporters carrying '_pcargo', directly or indirectly. */
399 #define unit_transports_iterate(_pcargo, _ptrans) { \
400 struct unit *_ptrans; \
401 for (_ptrans = unit_transport_get(_pcargo); NULL != _ptrans; \
402 _ptrans = unit_transport_get(_ptrans)) {
403 #define unit_transports_iterate_end }}
406 size_t cargo_iter_sizeof(void) fc__attribute((const));
408 struct iterator
*cargo_iter_init(struct cargo_iter
*iter
,
409 const struct unit
*ptrans
);
410 #define unit_cargo_iterate(_ptrans, _pcargo) \
411 generic_iterate(struct cargo_iter, struct unit *, _pcargo, \
412 cargo_iter_sizeof, cargo_iter_init, _ptrans)
413 #define unit_cargo_iterate_end generic_iterate_end
417 #endif /* __cplusplus */
419 #endif /* FC__UNIT_H */