File: unitlist.c

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 (320 lines) | stat: -rw-r--r-- 10,627 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
/****************************************************************************
 Freeciv - Copyright (C) 2005 - The Freeciv Project
   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.
****************************************************************************/

#ifdef HAVE_CONFIG_H
#include <fc_config.h>
#endif

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

/* common */
#include "game.h"
#include "movement.h"
#include "unitlist.h"

/****************************************************************************
  Look for a unit with the given ID in the unit list.  Returns NULL if none
  is found.
****************************************************************************/
struct unit *unit_list_find(const struct unit_list *punitlist, int unit_id)
{
  unit_list_iterate(punitlist, punit) {
    if (punit->id == unit_id) {
      return punit;
    }
  } unit_list_iterate_end;

  return NULL;
}

/****************************************************************************
  Comparison function for unit_list_sort, sorting by ord_map:
  The indirection is a bit gory:
  Read from the right:
    1. cast arg "a" to "ptr to void*"   (we're sorting a list of "void*"'s)
    2. dereference to get the "void*"
    3. cast that "void*" to a "struct unit*"

  Only used in server/savegame.c.
****************************************************************************/
static int compar_unit_ord_map(const struct unit *const *ua,
                               const struct unit *const *ub)
{
  return (*ua)->server.ord_map - (*ub)->server.ord_map;
}

/****************************************************************************
  Comparison function for unit_list_sort, sorting by ord_city: see above.

  Only used in server/savegame.c.
****************************************************************************/
static int compar_unit_ord_city(const struct unit *const *ua,
                                const struct unit *const *ub)
{
  return (*ua)->server.ord_city - (*ub)->server.ord_city;
}

/****************************************************************************
  Sorts the unit list by punit->server.ord_map values.

  Only used in server/savegame.c.
****************************************************************************/
void unit_list_sort_ord_map(struct unit_list *punitlist)
{
  fc_assert_ret(is_server());
  unit_list_sort(punitlist, compar_unit_ord_map);
}

/****************************************************************************
  Sorts the unit list by punit->server.ord_city values.

  Only used in server/savegame.c.
****************************************************************************/
void unit_list_sort_ord_city(struct unit_list *punitlist)
{
  fc_assert_ret(is_server());
  unit_list_sort(punitlist, compar_unit_ord_city);
}


/****************************************************************************
  Return TRUE if the function returns true for any of the units.
****************************************************************************/
bool can_units_do(const struct unit_list *punits,
		  bool (can_fn)(const struct unit *punit))
{
  unit_list_iterate(punits, punit) {
    if (can_fn(punit)) {
      return TRUE;
    }
  } unit_list_iterate_end;

  return FALSE;
}

/****************************************************************************
  Returns TRUE if any of the units can do the activity.
****************************************************************************/
bool can_units_do_activity(const struct unit_list *punits,
			   enum unit_activity activity)
{
  /* Make sure nobody uses these old activities any more */
  fc_assert_ret_val(activity != ACTIVITY_FORTRESS
                    && activity != ACTIVITY_AIRBASE, FALSE);

  unit_list_iterate(punits, punit) {
    if (can_unit_do_activity(punit, activity)) {
      return TRUE;
    }
  } unit_list_iterate_end;

  return FALSE;
}

/****************************************************************************
  Returns TRUE if any of the units can do the targeted activity.
****************************************************************************/
bool can_units_do_activity_targeted(const struct unit_list *punits,
                                    enum unit_activity activity,
                                    struct extra_type *pextra)
{
  unit_list_iterate(punits, punit) {
    if (can_unit_do_activity_targeted(punit, activity, pextra)) {
      return TRUE;
    }
  } unit_list_iterate_end;

  return FALSE;
}

/****************************************************************************
  Returns TRUE if any of the units can build any road.
****************************************************************************/
bool can_units_do_any_road(const struct unit_list *punits)
{
  unit_list_iterate(punits, punit) {
    extra_type_by_cause_iterate(EC_ROAD, pextra) {
      struct road_type *proad = extra_road_get(pextra);

      if (can_build_road(proad, punit, unit_tile(punit))) {
        return TRUE;
      }
    } extra_type_by_cause_iterate_end;
  } unit_list_iterate_end;

  return FALSE;
}

/****************************************************************************
  Returns TRUE if any of the units can build base with given gui_type.
****************************************************************************/
bool can_units_do_base_gui(const struct unit_list *punits,
                           enum base_gui_type base_gui)
{
  unit_list_iterate(punits, punit) {
    struct base_type *pbase = get_base_by_gui_type(base_gui, punit,
                                                   unit_tile(punit));

    if (pbase) {
      /* Some unit can build base of given gui_type */
      return TRUE;
    }
  } unit_list_iterate_end;

  return FALSE;
}

/****************************************************************************
  If has_flag is true, returns true iff any of the units have the flag.

  If has_flag is false, returns true iff any of the units don't have the
  flag.
****************************************************************************/
bool units_have_type_flag(const struct unit_list *punits,
                          enum unit_type_flag_id flag, bool has_flag)
{
  unit_list_iterate(punits, punit) {
    if (EQ(has_flag, unit_has_type_flag(punit, flag))) {
      return TRUE;
    }
  } unit_list_iterate_end;

  return FALSE;
}

/****************************************************************************
  Does the list contain any cityfounder units
****************************************************************************/
bool units_contain_cityfounder(const struct unit_list *punits)
{
  if (game.scenario.prevent_new_cities) {
    return FALSE;
  }

  unit_list_iterate(punits, punit) {
    if (EQ(TRUE, unit_has_type_flag(punit, UTYF_CITIES))) {
      return TRUE;
    }
  } unit_list_iterate_end;

  return FALSE;
}

/**************************************************************************
  If has_flag is true, returns true iff any of the units are able to do
  the specified action.

  If has_flag is false, returns true iff any of the units are unable do
  the specified action.
**************************************************************************/
bool units_can_do_action(const struct unit_list *punits,
                         int act_id, bool can_do)
{
  unit_list_iterate(punits, punit) {
    if (EQ(can_do, unit_can_do_action(punit, act_id))) {
      return TRUE;
    }
  } unit_list_iterate_end;

  return FALSE;
}

/****************************************************************************
  Return TRUE iff any of the units is a transporter that is occupied.
****************************************************************************/
bool units_are_occupied(const struct unit_list *punits)
{
  unit_list_iterate(punits, punit) {
    if (get_transporter_occupancy(punit) > 0) {
      return TRUE;
    }
  } unit_list_iterate_end;

  return FALSE;
}

/****************************************************************************
  Returns TRUE iff any of these units can load.
****************************************************************************/
bool units_can_load(const struct unit_list *punits)
{
  unit_list_iterate(punits, punit) {
    if (unit_can_load(punit)) {
      return TRUE;
    }
  } unit_list_iterate_end;

  return FALSE;
}

/****************************************************************************
  Return TRUE iff any of these units can unload.
****************************************************************************/
bool units_can_unload(const struct unit_list *punits)
{
  unit_list_iterate(punits, punit) {
    if (unit_transported(punit)
        && can_unit_unload(punit, unit_transport_get(punit))
        && can_unit_exist_at_tile(punit, unit_tile(punit))) {
      return TRUE;
    }
  } unit_list_iterate_end;

  return FALSE;
}

/****************************************************************************
  Return TRUE iff any of the units' tiles have the activity running
  on them.
****************************************************************************/
bool units_have_activity_on_tile(const struct unit_list *punits,
				 enum unit_activity activity)
{
  unit_list_iterate(punits, punit) {
    if (is_unit_activity_on_tile(activity, unit_tile(punit))) {
      return TRUE;
    }
  } unit_list_iterate_end;

  return FALSE;
}

/****************************************************************************
  Return TRUE iff any of the units can be upgraded to another unit type
  (for money)
****************************************************************************/
bool units_can_upgrade(const struct unit_list *punits)
{
  unit_list_iterate(punits, punit) {
    if (UU_OK == unit_upgrade_test(punit, FALSE)) {
      return TRUE;
    }
  } unit_list_iterate_end;

  return FALSE;
}

/****************************************************************************
  Return TRUE iff any of the units can convert to another unit type
****************************************************************************/
bool units_can_convert(const struct unit_list *punits)
{
  unit_list_iterate(punits, punit) {
    if (unit_can_convert(punit)) {
      return TRUE;
    }
  } unit_list_iterate_end;

  return FALSE;
}