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. 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 #ifndef FC__UNIT_H 14 #define FC__UNIT_H 15 16 #ifdef __cplusplus 17 extern "C" { 18 #endif /* __cplusplus */ 19 20 /* utility */ 21 #include "bitvector.h" 22 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" 29 30 struct road_type; 31 struct unit_move_data; /* Actually defined in "server/unittools.c". */ 32 33 /* Changing this enum will break network compatibility. */ 34 enum unit_orders { 35 ORDER_MOVE = 0, 36 ORDER_ACTIVITY = 1, 37 ORDER_FULL_MP = 2, 38 ORDER_BUILD_CITY = 3, 39 ORDER_DISBAND = 4, 40 ORDER_BUILD_WONDER = 5, 41 ORDER_TRADE_ROUTE = 6, 42 ORDER_HOMECITY = 7, 43 ORDER_ACTION_MOVE = 8, 44 /* and plenty more for later... */ 45 ORDER_LAST 46 }; 47 48 enum unit_focus_status { 49 FOCUS_AVAIL, FOCUS_WAIT, FOCUS_DONE 50 }; 51 52 enum goto_route_type { 53 ROUTE_GOTO, ROUTE_PATROL 54 }; 55 56 enum unit_add_build_city_result { 57 UAB_BUILD_OK, /* Unit OK to build city. */ 58 UAB_ADD_OK, /* Unit OK to add to city. */ 59 UAB_BAD_CITY_TERRAIN, /* Equivalent to 'CB_BAD_CITY_TERRAIN'. */ 60 UAB_BAD_UNIT_TERRAIN, /* Equivalent to 'CB_BAD_UNIT_TERRAIN'. */ 61 UAB_BAD_BORDERS, /* Equivalent to 'CB_BAD_BORDERS'. */ 62 UAB_NO_MIN_DIST, /* Equivalent to 'CB_NO_MIN_DIST'. */ 63 UAB_NOT_ADDABLE_UNIT, /* Unit is not one that can be added to cities. */ 64 UAB_NOT_BUILD_UNIT, /* Unit is not one that can build cities. */ 65 UAB_NO_MOVES_BUILD, /* Unit does not have moves left to build a city. */ 66 UAB_NO_MOVES_ADD, /* Unit does not have moves left to add to city. */ 67 UAB_NOT_OWNER, /* Owner of unit is not owner of city. */ 68 UAB_TOO_BIG, /* City is too big to be added to. */ 69 UAB_NO_SPACE /* Adding takes city past limit. */ 70 }; 71 72 enum unit_upgrade_result { 73 UU_OK, 74 UU_NO_UNITTYPE, 75 UU_NO_MONEY, 76 UU_NOT_IN_CITY, 77 UU_NOT_CITY_OWNER, 78 UU_NOT_ENOUGH_ROOM, 79 UU_NOT_TERRAIN, /* The upgraded unit could not survive. */ 80 UU_UNSUITABLE_TRANSPORT /* Can't upgrade inside current transport. */ 81 }; 82 83 enum unit_airlift_result { 84 /* Codes treated as success: */ 85 AR_OK, /* This will definitely work */ 86 AR_OK_SRC_UNKNOWN, /* Source city's airlift capability is unknown */ 87 AR_OK_DST_UNKNOWN, /* Dest city's airlift capability is unknown */ 88 /* Codes treated as failure: */ 89 AR_NO_MOVES, /* Unit has no moves left */ 90 AR_WRONG_UNITTYPE, /* Can't airlift this type of unit */ 91 AR_OCCUPIED, /* Can't airlift units with passengers */ 92 AR_NOT_IN_CITY, /* Unit not in a city */ 93 AR_BAD_SRC_CITY, /* Can't airlift from this src city */ 94 AR_BAD_DST_CITY, /* Can't airlift to this dst city */ 95 AR_SRC_NO_FLIGHTS, /* No flights available from src */ 96 AR_DST_NO_FLIGHTS /* No flights available to dst */ 97 }; 98 99 struct unit_adv { 100 enum adv_unit_task task; 101 }; 102 103 struct unit_order { 104 enum unit_orders order; 105 enum unit_activity activity; /* Only valid for ORDER_ACTIVITY. */ 106 int target; 107 /* Valid for ORDER_MOVE and ORDER_ACTION_MOVE. */ 108 enum direction8 dir; 109 }; 110 111 struct unit; 112 struct unit_list; 113 114 struct unit { 115 struct unit_type *utype; /* Cannot be NULL. */ 116 struct tile *tile; 117 int refcount; 118 enum direction8 facing; 119 struct player *owner; /* Cannot be NULL. */ 120 struct player *nationality; 121 int id; 122 int homecity; 123 124 int upkeep[O_LAST]; /* unit upkeep with regards to the homecity */ 125 126 int moves_left; 127 int hp; 128 int veteran; 129 int fuel; 130 131 struct tile *goto_tile; /* May be NULL. */ 132 133 enum unit_activity activity; 134 135 /* The amount of work that has been done on the current activity. This 136 * is counted in turns but is multiplied by ACTIVITY_FACTOR (which allows 137 * fractional values in some cases). */ 138 int activity_count; 139 140 struct extra_type *activity_target; 141 142 /* Previous activity, so it can be resumed without loss of progress 143 * if the user changes their mind during a turn. */ 144 enum unit_activity changed_from; 145 int changed_from_count; 146 struct extra_type *changed_from_target; 147 148 bool ai_controlled; /* 0: not automated; 1: automated */ 149 bool moved; 150 bool paradropped; 151 152 /* This value is set if the unit is done moving for this turn. This 153 * information is used by the client. The invariant is: 154 * - If the unit has no more moves, it's done moving. 155 * - If the unit is on a goto but is waiting, it's done moving. 156 * - Otherwise the unit is not done moving. */ 157 bool done_moving; 158 159 struct unit *transporter; /* This unit is transported by ... */ 160 struct unit_list *transporting; /* This unit transports ... */ 161 162 /* The battlegroup ID: defined by the client but stored by the server. */ 163 #define MAX_NUM_BATTLEGROUPS (4) 164 #define BATTLEGROUP_NONE (-1) 165 int battlegroup; 166 167 bool has_orders; 168 struct { 169 int length, index; 170 bool repeat; /* The path is to be repeated on completion. */ 171 bool vigilant; /* Orders should be cleared if an enemy is met. */ 172 struct unit_order *list; 173 } orders; 174 175 /* The unit may want the player to choose an action. */ 176 enum action_decision action_decision_want; 177 struct tile *action_decision_tile; 178 179 union { 180 struct { 181 /* Only used at the client (the server is omniscient; ./client/). */ 182 183 enum unit_focus_status focus_status; 184 185 int transported_by; /* Used for unit_short_info packets where we can't 186 * be sure that the information about the 187 * transporter is known. */ 188 bool occupied; /* TRUE if at least one cargo on the transporter. */ 189 190 /* Equivalent to pcity->client.color. Only for UTYF_CITIES units. */ 191 bool colored; 192 int color_index; 193 194 bool asking_city_name; 195 196 /* Used in a follow up question about a selected action. */ 197 struct act_prob *act_prob_cache; 198 } client; 199 200 struct { 201 /* Only used in the server (./ai/ and ./server/). */ 202 203 bool debug; 204 205 struct unit_adv *adv; 206 void *ais[FREECIV_AI_MOD_LAST]; 207 int birth_turn; 208 209 /* ord_map and ord_city are the order index of this unit in tile.units 210 * and city.units_supported; they are only used for save/reload */ 211 int ord_map; 212 int ord_city; 213 214 struct vision *vision; 215 time_t action_timestamp; 216 int action_turn; 217 struct unit_move_data *moving; 218 219 /* The unit is in the process of dying. */ 220 bool dying; 221 } server; 222 }; 223 }; 224 225 #ifdef FREECIV_DEBUG 226 #define CHECK_UNIT(punit) \ 227 (fc_assert(punit != NULL), \ 228 fc_assert(unit_type_get(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 /* FREECIV_DEBUG */ 234 #define CHECK_UNIT(punit) /* Do nothing */ 235 #endif /* FREECIV_DEBUG */ 236 237 void setup_real_activities_array(void); 238 239 bool is_real_activity(enum unit_activity activity); 240 241 extern Activity_type_id real_activities[ACTIVITY_LAST]; 242 243 #define activity_type_list_iterate(_act_list_, _act_) \ 244 { \ 245 int _act_i_; \ 246 for (_act_i_ = 0; _act_list_[_act_i_] != ACTIVITY_LAST; _act_i_++) { \ 247 Activity_type_id _act_ = _act_list_[_act_i_]; 248 249 #define activity_type_list_iterate_end \ 250 } \ 251 } 252 253 /* Iterates over the types of unit activity. */ 254 #define activity_type_iterate(_act_) \ 255 { \ 256 activity_type_list_iterate(real_activities, _act_) 257 258 #define activity_type_iterate_end \ 259 activity_type_list_iterate_end \ 260 } 261 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); 271 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 can_unit_continue_current_activity(struct unit *punit); 282 bool can_unit_do_activity(const struct unit *punit, 283 enum unit_activity activity); 284 bool can_unit_do_activity_targeted(const struct unit *punit, 285 enum unit_activity activity, 286 struct extra_type *target); 287 bool can_unit_do_activity_targeted_at(const struct unit *punit, 288 enum unit_activity activity, 289 struct extra_type *target, 290 const struct tile *ptile); 291 void set_unit_activity(struct unit *punit, enum unit_activity new_activity); 292 void set_unit_activity_targeted(struct unit *punit, 293 enum unit_activity new_activity, 294 struct extra_type *new_target); 295 void set_unit_activity_base(struct unit *punit, 296 Base_type_id base); 297 void set_unit_activity_road(struct unit *punit, 298 Road_type_id road); 299 int get_activity_rate(const struct unit *punit); 300 int get_activity_rate_this_turn(const struct unit *punit); 301 int get_turns_for_activity_at(const struct unit *punit, 302 enum unit_activity activity, 303 const struct tile *ptile, 304 struct extra_type *tgt); 305 bool activity_requires_target(enum unit_activity activity); 306 bool can_unit_do_autosettlers(const struct unit *punit); 307 bool is_unit_activity_on_tile(enum unit_activity activity, 308 const struct tile *ptile); 309 bv_extras get_unit_tile_pillage_set(const struct tile *ptile); 310 bool is_attack_unit(const struct unit *punit); 311 bool is_military_unit(const struct unit *punit); /* !set !dip !cara */ 312 bool unit_can_do_action(const struct unit *punit, 313 const int act_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_to_city(const struct unit *punit); 319 bool unit_can_build_city(const struct unit *punit); 320 bool unit_can_add_or_build_city(const struct unit *punit); 321 enum unit_add_build_city_result 322 unit_add_or_build_city_test(const struct unit *punit); 323 bool kills_citizen_after_attack(const struct unit *punit); 324 325 struct astring; /* Forward declaration. */ 326 void unit_activity_astr(const struct unit *punit, struct astring *astr); 327 void unit_upkeep_astr(const struct unit *punit, struct astring *astr); 328 const char *unit_activity_text(const struct unit *punit); 329 330 int get_transporter_capacity(const struct unit *punit); 331 332 #define unit_home(_pu_) (game_city_by_number((_pu_)->homecity)) 333 #define unit_owner(_pu) ((_pu)->owner) 334 #define unit_tile(_pu) ((_pu)->tile) 335 struct player *unit_nationality(const struct unit *punit); 336 void unit_tile_set(struct unit *punit, struct tile *ptile); 337 338 struct unit *is_allied_unit_tile(const struct tile *ptile, 339 const struct player *pplayer); 340 struct unit *is_enemy_unit_tile(const struct tile *ptile, 341 const struct player *pplayer); 342 struct unit *is_non_allied_unit_tile(const struct tile *ptile, 343 const struct player *pplayer); 344 struct unit *is_other_players_unit_tile(const struct tile *ptile, 345 const struct player *pplayer); 346 struct unit *is_non_attack_unit_tile(const struct tile *ptile, 347 const struct player *pplayer); 348 struct unit *unit_occupies_tile(const struct tile *ptile, 349 const struct player *pplayer); 350 351 bool is_my_zoc(const struct player *unit_owner, const struct tile *ptile); 352 bool unit_being_aggressive(const struct unit *punit); 353 bool unit_type_really_ignores_zoc(const struct unit_type *punittype); 354 355 bool is_build_activity(enum unit_activity activity, const struct tile *ptile); 356 bool is_clean_activity(enum unit_activity activity); 357 bool is_tile_activity(enum unit_activity activity); 358 359 struct unit *unit_virtual_create(struct player *pplayer, struct city *pcity, 360 struct unit_type *punittype, 361 int veteran_level); 362 void unit_virtual_destroy(struct unit *punit); 363 bool unit_is_virtual(const struct unit *punit); 364 void free_unit_orders(struct unit *punit); 365 366 int get_transporter_occupancy(const struct unit *ptrans); 367 struct unit *transporter_for_unit(const struct unit *pcargo); 368 struct unit *transporter_for_unit_at(const struct unit *pcargo, 369 const struct tile *ptile); 370 371 enum unit_upgrade_result unit_upgrade_test(const struct unit *punit, 372 bool is_free); 373 enum unit_upgrade_result unit_upgrade_info(const struct unit *punit, 374 char *buf, size_t bufsz); 375 bool unit_can_convert(const struct unit *punit); 376 377 int hp_gain_coord(struct unit *punit); 378 bool is_losing_hp(const struct unit *punit); 379 bool unit_type_is_losing_hp(const struct player *pplayer, 380 const struct unit_type *punittype); 381 382 bool unit_is_alive(int id); 383 384 void *unit_ai_data(const struct unit *punit, const struct ai_type *ai); 385 void unit_set_ai_data(struct unit *punit, const struct ai_type *ai, 386 void *data); 387 388 int unit_bribe_cost(struct unit *punit, struct player *briber); 389 390 bool unit_transport_load(struct unit *pcargo, struct unit *ptrans, 391 bool force); 392 bool unit_transport_unload(struct unit *pcargo); 393 struct unit *unit_transport_get(const struct unit *pcargo); 394 bool unit_transported(const struct unit *pcargo); 395 struct unit_list *unit_transport_cargo(const struct unit *ptrans); 396 bool unit_transport_check(const struct unit *pcargo, 397 const struct unit *ptrans); 398 bool unit_contained_in(const struct unit *pcargo, const struct unit *ptrans); 399 int unit_cargo_depth(const struct unit *pcargo); 400 int unit_transport_depth(const struct unit *ptrans); 401 402 bool unit_is_cityfounder(const struct unit *punit); 403 404 /* Iterate all transporters carrying '_pcargo', directly or indirectly. */ 405 #define unit_transports_iterate(_pcargo, _ptrans) { \ 406 struct unit *_ptrans; \ 407 for (_ptrans = unit_transport_get(_pcargo); NULL != _ptrans; \ 408 _ptrans = unit_transport_get(_ptrans)) { 409 #define unit_transports_iterate_end }} 410 411 struct cargo_iter; 412 size_t cargo_iter_sizeof(void) fc__attribute((const)); 413 414 struct iterator *cargo_iter_init(struct cargo_iter *iter, 415 const struct unit *ptrans); 416 #define unit_cargo_iterate(_ptrans, _pcargo) \ 417 generic_iterate(struct cargo_iter, struct unit *, _pcargo, \ 418 cargo_iter_sizeof, cargo_iter_init, _ptrans) 419 #define unit_cargo_iterate_end generic_iterate_end 420 421 #ifdef __cplusplus 422 } 423 #endif /* __cplusplus */ 424 425 #endif /* FC__UNIT_H */ 426