File: unit.h

package info (click to toggle)
freeciv 2.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 212,508 kB
  • sloc: ansic: 443,831; cpp: 29,541; sh: 7,982; makefile: 7,886; python: 1,933; xml: 945
file content (422 lines) | stat: -rw-r--r-- 16,358 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/***********************************************************************
 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
***********************************************************************/
#ifndef FC__UNIT_H
#define FC__UNIT_H

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

/* utility */
#include "bitvector.h"

/* common */
#include "base.h"
#include "fc_types.h"
#include "terrain.h"		/* enum tile_special_type */
#include "unittype.h"
#include "vision.h"

struct road_type;
struct unit_move_data; /* Actually defined in "server/unittools.c". */

/* Changing this enum will break network compatability. */
enum unit_orders {
  ORDER_MOVE = 0,
  ORDER_ACTIVITY = 1,
  ORDER_FULL_MP = 2,
  ORDER_BUILD_CITY = 3,
  ORDER_DISBAND = 4,
  ORDER_BUILD_WONDER = 5,
  ORDER_TRADE_ROUTE = 6,
  ORDER_HOMECITY = 7,
  ORDER_ACTION_MOVE = 8,
  /* and plenty more for later... */
  ORDER_LAST
};

enum unit_focus_status {
  FOCUS_AVAIL, FOCUS_WAIT, FOCUS_DONE  
};

enum goto_route_type {
  ROUTE_GOTO, ROUTE_PATROL
};

enum unit_add_build_city_result {
  UAB_BUILD_OK,         /* Unit OK to build city. */
  UAB_ADD_OK,           /* Unit OK to add to city. */
  UAB_BAD_CITY_TERRAIN, /* Equivalent to 'CB_BAD_CITY_TERRAIN'. */
  UAB_BAD_UNIT_TERRAIN, /* Equivalent to 'CB_BAD_UNIT_TERRAIN'. */
  UAB_BAD_BORDERS,      /* Equivalent to 'CB_BAD_BORDERS'. */
  UAB_NO_MIN_DIST,      /* Equivalent to 'CB_NO_MIN_DIST'. */
  UAB_NOT_ADDABLE_UNIT, /* Unit is not one that can be added to cities. */
  UAB_NOT_BUILD_UNIT,   /* Unit is not one that can build cities. */
  UAB_NO_MOVES_BUILD,   /* Unit does not have moves left to build a city. */
  UAB_NO_MOVES_ADD,     /* Unit does not have moves left to add to city. */
  UAB_NOT_OWNER,        /* Owner of unit is not owner of city. */
  UAB_TOO_BIG,          /* City is too big to be added to. */
  UAB_NO_SPACE          /* Adding takes city past limit. */
};

enum unit_upgrade_result {
  UU_OK,
  UU_NO_UNITTYPE,
  UU_NO_MONEY,
  UU_NOT_IN_CITY,
  UU_NOT_CITY_OWNER,
  UU_NOT_ENOUGH_ROOM,
  UU_NOT_TERRAIN,         /* The upgraded unit could not survive. */
  UU_UNSUITABLE_TRANSPORT /* Can't upgrade inside current transport. */
};

enum unit_airlift_result {
  /* Codes treated as success: */
  AR_OK,                /* This will definitely work */
  AR_OK_SRC_UNKNOWN,    /* Source city's airlift capability is unknown */
  AR_OK_DST_UNKNOWN,    /* Dest city's airlift capability is unknown */
  /* Codes treated as failure: */
  AR_NO_MOVES,          /* Unit has no moves left */
  AR_WRONG_UNITTYPE,    /* Can't airlift this type of unit */
  AR_OCCUPIED,          /* Can't airlift units with passengers */
  AR_NOT_IN_CITY,       /* Unit not in a city */
  AR_BAD_SRC_CITY,      /* Can't airlift from this src city */
  AR_BAD_DST_CITY,      /* Can't airlift to this dst city */
  AR_SRC_NO_FLIGHTS,    /* No flights available from src */
  AR_DST_NO_FLIGHTS     /* No flights available to dst */
};

struct unit_adv {
  enum adv_unit_task task;
};

struct unit_order {
  enum unit_orders order;
  enum unit_activity activity;  /* Only valid for ORDER_ACTIVITY. */
  int target;
  /* Valid for ORDER_MOVE and ORDER_ACTION_MOVE. */
  enum direction8 dir;
};

struct unit;
struct unit_list;

struct unit {
  struct unit_type *utype; /* Cannot be NULL. */
  struct tile *tile;
  int refcount;
  enum direction8 facing;
  struct player *owner; /* Cannot be NULL. */
  struct player *nationality;
  int id;
  int homecity;

  int upkeep[O_LAST]; /* unit upkeep with regards to the homecity */

  int moves_left;
  int hp;
  int veteran;
  int fuel;

  struct tile *goto_tile; /* May be NULL. */

  enum unit_activity activity;

  /* The amount of work that has been done on the current activity.  This
   * is counted in turns but is multiplied by ACTIVITY_FACTOR (which allows
   * fractional values in some cases). */
  int activity_count;

  struct extra_type *activity_target;

  /* Previous activity, so it can be resumed without loss of progress
   * if the user changes their mind during a turn. */
  enum unit_activity changed_from;
  int changed_from_count;
  struct extra_type *changed_from_target;

  bool ai_controlled; /* 0: not automated; 1: automated */
  bool moved;
  bool paradropped;

  /* This value is set if the unit is done moving for this turn. This
   * information is used by the client.  The invariant is:
   *   - If the unit has no more moves, it's done moving.
   *   - If the unit is on a goto but is waiting, it's done moving.
   *   - Otherwise the unit is not done moving. */
  bool done_moving;

  struct unit *transporter;   /* This unit is transported by ... */
  struct unit_list *transporting; /* This unit transports ... */

  /* The battlegroup ID: defined by the client but stored by the server. */
#define MAX_NUM_BATTLEGROUPS (4)
#define BATTLEGROUP_NONE (-1)
  int battlegroup;

  bool has_orders;
  struct {
    int length, index;
    bool repeat;   /* The path is to be repeated on completion. */
    bool vigilant; /* Orders should be cleared if an enemy is met. */
    struct unit_order *list;
  } orders;

  /* The unit may want the player to choose an action. */
  enum action_decision action_decision_want;
  struct tile *action_decision_tile;

  union {
    struct {
      /* Only used at the client (the server is omniscient; ./client/). */

      enum unit_focus_status focus_status;

      int transported_by; /* Used for unit_short_info packets where we can't
                           * be sure that the information about the
                           * transporter is known. */
      bool occupied;      /* TRUE if at least one cargo on the transporter. */

      /* Equivalent to pcity->client.color. Only for UTYF_CITIES units. */
      bool colored;
      int color_index;

      bool asking_city_name;

      /* Used in a follow up question about a selected action. */
      struct act_prob *act_prob_cache;
    } client;

    struct {
      /* Only used in the server (./ai/ and ./server/). */

      bool debug;

      struct unit_adv *adv;
      void *ais[FREECIV_AI_MOD_LAST];
      int birth_turn;

      /* ord_map and ord_city are the order index of this unit in tile.units
       * and city.units_supported; they are only used for save/reload */
      int ord_map;
      int ord_city;

      struct vision *vision;
      time_t action_timestamp;
      int action_turn;
      struct unit_move_data *moving;

      /* The unit is in the process of dying. */
      bool dying;
    } server;
  };
};

#ifdef FREECIV_DEBUG
#define CHECK_UNIT(punit)                                                   \
  (fc_assert(punit != NULL),                                                \
   fc_assert(unit_type_get(punit) != NULL),                                 \
   fc_assert(unit_owner(punit) != NULL),                                    \
   fc_assert(player_by_number(player_index(unit_owner(punit)))              \
             == unit_owner(punit)),                                         \
   fc_assert(game_unit_by_number(punit->id) != NULL))
#else  /* FREECIV_DEBUG */
#define CHECK_UNIT(punit) /* Do nothing */
#endif /* FREECIV_DEBUG */

void setup_real_activities_array(void);

extern Activity_type_id real_activities[ACTIVITY_LAST];

#define activity_type_list_iterate(_act_list_, _act_)                        \
{                                                                            \
  int _act_i_;                                                               \
  for (_act_i_ = 0; _act_list_[_act_i_] != ACTIVITY_LAST; _act_i_++) {       \
    Activity_type_id _act_ = _act_list_[_act_i_];

#define activity_type_list_iterate_end                                       \
  }                                                                          \
}

/* Iterates over the types of unit activity. */
#define activity_type_iterate(_act_)					    \
{									    \
  activity_type_list_iterate(real_activities, _act_)

#define activity_type_iterate_end                                           \
  activity_type_list_iterate_end                                            \
}

bool unit_can_help_build_wonder_here(const struct unit *punit);
bool unit_can_est_trade_route_here(const struct unit *punit);
enum unit_airlift_result
    test_unit_can_airlift_to(const struct player *restriction,
                             const struct unit *punit,
                             const struct city *pdest_city);
bool is_successful_airlift_result(enum unit_airlift_result result);
bool unit_can_airlift_to(const struct unit *punit, const struct city *pcity);
bool unit_has_orders(const struct unit *punit);

bool could_unit_load(const struct unit *pcargo, const struct unit *ptrans);
bool can_unit_load(const struct unit *punit, const struct unit *ptrans);
bool can_unit_unload(const struct unit *punit, const struct unit *ptrans);
bool can_unit_paradrop(const struct unit *punit);
bool can_unit_bombard(const struct unit *punit);
bool can_unit_change_homecity_to(const struct unit *punit,
				 const struct city *pcity);
bool can_unit_change_homecity(const struct unit *punit);
const char *get_activity_text(enum unit_activity activity);
bool can_unit_continue_current_activity(struct unit *punit);
bool can_unit_do_activity(const struct unit *punit,
			  enum unit_activity activity);
bool can_unit_do_activity_targeted(const struct unit *punit,
				   enum unit_activity activity,
                                   struct extra_type *target);
bool can_unit_do_activity_targeted_at(const struct unit *punit,
				      enum unit_activity activity,
				      struct extra_type *target,
				      const struct tile *ptile);
void set_unit_activity(struct unit *punit, enum unit_activity new_activity);
void set_unit_activity_targeted(struct unit *punit,
				enum unit_activity new_activity,
                                struct extra_type *new_target);
void set_unit_activity_base(struct unit *punit,
                            Base_type_id base);
void set_unit_activity_road(struct unit *punit,
                            Road_type_id road);
int get_activity_rate(const struct unit *punit);
int get_activity_rate_this_turn(const struct unit *punit);
int get_turns_for_activity_at(const struct unit *punit,
			      enum unit_activity activity,
			      const struct tile *ptile,
                              struct extra_type *tgt);
bool activity_requires_target(enum unit_activity activity);
bool can_unit_do_autosettlers(const struct unit *punit); 
bool is_unit_activity_on_tile(enum unit_activity activity,
			      const struct tile *ptile);
bv_extras get_unit_tile_pillage_set(const struct tile *ptile);
bool is_attack_unit(const struct unit *punit);
bool is_military_unit(const struct unit *punit);           /* !set !dip !cara */
bool unit_can_do_action(const struct unit *punit,
                        const int act_id);
bool is_square_threatened(const struct player *pplayer,
			  const struct tile *ptile, bool omniscient);
bool is_field_unit(const struct unit *punit);              /* ships+aero */
bool is_hiding_unit(const struct unit *punit);
bool unit_can_add_to_city(const struct unit *punit);
bool unit_can_build_city(const struct unit *punit);
bool unit_can_add_or_build_city(const struct unit *punit);
enum unit_add_build_city_result
unit_add_or_build_city_test(const struct unit *punit);
bool kills_citizen_after_attack(const struct unit *punit);

struct astring; /* Forward declaration. */
void unit_activity_astr(const struct unit *punit, struct astring *astr);
void unit_upkeep_astr(const struct unit *punit, struct astring *astr);
const char *unit_activity_text(const struct unit *punit);

int get_transporter_capacity(const struct unit *punit);

#define unit_home(_pu_) (game_city_by_number((_pu_)->homecity))
#define unit_owner(_pu) ((_pu)->owner)
#define unit_tile(_pu) ((_pu)->tile)
struct player *unit_nationality(const struct unit *punit);
void unit_tile_set(struct unit *punit, struct tile *ptile);

struct unit *is_allied_unit_tile(const struct tile *ptile,
				 const struct player *pplayer);
struct unit *is_enemy_unit_tile(const struct tile *ptile,
				const struct player *pplayer);
struct unit *is_non_allied_unit_tile(const struct tile *ptile,
				     const struct player *pplayer);
struct unit *is_other_players_unit_tile(const struct tile *ptile,
					const struct player *pplayer);
struct unit *is_non_attack_unit_tile(const struct tile *ptile,
				     const struct player *pplayer);
struct unit *unit_occupies_tile(const struct tile *ptile,
				const struct player *pplayer);

bool is_my_zoc(const struct player *unit_owner, const struct tile *ptile);
bool unit_being_aggressive(const struct unit *punit);
bool unit_type_really_ignores_zoc(const struct unit_type *punittype);

bool is_build_activity(enum unit_activity activity, const struct tile *ptile);
bool is_clean_activity(enum unit_activity activity);
bool is_tile_activity(enum unit_activity activity);

struct unit *unit_virtual_create(struct player *pplayer, struct city *pcity,
                                 struct unit_type *punittype,
				 int veteran_level);
void unit_virtual_destroy(struct unit *punit);
bool unit_is_virtual(const struct unit *punit);
void free_unit_orders(struct unit *punit);

int get_transporter_occupancy(const struct unit *ptrans);
struct unit *transporter_for_unit(const struct unit *pcargo);
struct unit *transporter_for_unit_at(const struct unit *pcargo,
                                     const struct tile *ptile);

enum unit_upgrade_result unit_upgrade_test(const struct unit *punit,
                                           bool is_free);
enum unit_upgrade_result unit_upgrade_info(const struct unit *punit,
                                           char *buf, size_t bufsz);
bool unit_can_convert(const struct unit *punit);

bool is_losing_hp(const struct unit *punit);
bool unit_type_is_losing_hp(const struct player *pplayer,
                            const struct unit_type *punittype);

bool unit_is_alive(int id);

void *unit_ai_data(const struct unit *punit, const struct ai_type *ai);
void unit_set_ai_data(struct unit *punit, const struct ai_type *ai,
                      void *data);

int unit_bribe_cost(struct unit *punit, struct player *briber);

bool unit_transport_load(struct unit *pcargo, struct unit *ptrans,
                         bool force);
bool unit_transport_unload(struct unit *pcargo);
struct unit *unit_transport_get(const struct unit *pcargo);
bool unit_transported(const struct unit *pcargo);
struct unit_list *unit_transport_cargo(const struct unit *ptrans);
bool unit_transport_check(const struct unit *pcargo,
                          const struct unit *ptrans);
bool unit_contained_in(const struct unit *pcargo, const struct unit *ptrans);
int unit_cargo_depth(const struct unit *pcargo);
int unit_transport_depth(const struct unit *ptrans);

bool unit_is_cityfounder(const struct unit *punit);

/* Iterate all transporters carrying '_pcargo', directly or indirectly. */
#define unit_transports_iterate(_pcargo, _ptrans) {                         \
  struct unit *_ptrans;                                                     \
  for (_ptrans = unit_transport_get(_pcargo); NULL != _ptrans;              \
       _ptrans = unit_transport_get(_ptrans)) {
#define unit_transports_iterate_end }}

struct cargo_iter;
size_t cargo_iter_sizeof(void) fc__attribute((const));

struct iterator *cargo_iter_init(struct cargo_iter *iter,
                                 const struct unit *ptrans);
#define unit_cargo_iterate(_ptrans, _pcargo)                                \
  generic_iterate(struct cargo_iter, struct unit *, _pcargo,                \
                  cargo_iter_sizeof, cargo_iter_init, _ptrans)
#define unit_cargo_iterate_end generic_iterate_end

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif  /* FC__UNIT_H */