Freeciv-3.3: /fast/freeciv/nightly/main/common/unitlist.c Source File

Freeciv-3.3

Loading...
Searching...
No Matches
unitlist.c
Go to the documentation of this file.
1/****************************************************************************
2 Freeciv - Copyright (C) 2005 - The Freeciv Project
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
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18/* utility */
19#include "log.h"
20
21/* common */
22#include "game.h"
23#include "movement.h"
24#include "unitlist.h"
25
26/************************************************************************/
30 struct unit *unit_list_find(const struct unit_list *punitlist, int unit_id)
31{
32 unit_list_iterate(punitlist, punit) {
33 if (punit->id == unit_id) {
34 return punit;
35 }
36 } unit_list_iterate_end;
37
38 return NULL;
39}
40
41/************************************************************************/
51 static int compar_unit_ord_map(const struct unit *const *ua,
52 const struct unit *const *ub)
53{
54 return (*ua)->server.ord_map - (*ub)->server.ord_map;
55}
56
57/************************************************************************/
62 static int compar_unit_ord_city(const struct unit *const *ua,
63 const struct unit *const *ub)
64{
65 return (*ua)->server.ord_city - (*ub)->server.ord_city;
66}
67
68/************************************************************************/
73 void unit_list_sort_ord_map(struct unit_list *punitlist)
74{
75 fc_assert_ret(is_server());
76
77 unit_list_sort(punitlist, compar_unit_ord_map);
78}
79
80/************************************************************************/
85 void unit_list_sort_ord_city(struct unit_list *punitlist)
86{
87 fc_assert_ret(is_server());
88
89 unit_list_sort(punitlist, compar_unit_ord_city);
90}
91
92/************************************************************************/
95 bool can_units_do(const struct unit_list *punits,
96 bool (can_fn)(const struct unit *punit))
97{
98 unit_list_iterate(punits, punit) {
99 if (can_fn(punit)) {
100 return TRUE;
101 }
102 } unit_list_iterate_end;
103
104 return FALSE;
105}
106
107/************************************************************************/
111 bool can_units_do_on_map(const struct civ_map *nmap,
112 const struct unit_list *punits,
113 bool (can_fn)(const struct civ_map *nmap,
114 const struct unit *punit))
115{
116 unit_list_iterate(punits, punit) {
117 if (can_fn(nmap, punit)) {
118 return TRUE;
119 }
120 } unit_list_iterate_end;
121
122 return FALSE;
123}
124
125/************************************************************************/
128 bool can_units_do_activity(const struct civ_map *nmap,
129 const struct unit_list *punits,
130 enum unit_activity activity)
131{
132 unit_list_iterate(punits, punit) {
133 if (can_unit_do_activity(nmap, punit, activity)) {
134 return TRUE;
135 }
136 } unit_list_iterate_end;
137
138 return FALSE;
139}
140
141/************************************************************************/
144 bool can_units_do_activity_targeted(const struct civ_map *nmap,
145 const struct unit_list *punits,
146 enum unit_activity activity,
147 struct extra_type *pextra)
148{
149 unit_list_iterate(punits, punit) {
150 if (can_unit_do_activity_targeted(nmap, punit, activity, pextra)) {
151 return TRUE;
152 }
153 } unit_list_iterate_end;
154
155 return FALSE;
156}
157
158/************************************************************************/
161 bool can_units_do_any_road(const struct civ_map *nmap,
162 const struct unit_list *punits)
163{
164 unit_list_iterate(punits, punit) {
165 extra_type_by_cause_iterate(EC_ROAD, pextra) {
166 struct road_type *proad = extra_road_get(pextra);
167
168 if (can_build_road(nmap, proad, punit, unit_tile(punit))) {
169 return TRUE;
170 }
171 } extra_type_by_cause_iterate_end;
172 } unit_list_iterate_end;
173
174 return FALSE;
175}
176
177/************************************************************************/
180 bool can_units_do_base_gui(const struct unit_list *punits,
181 enum base_gui_type base_gui)
182{
183 unit_list_iterate(punits, punit) {
184 struct base_type *pbase = get_base_by_gui_type(base_gui, punit,
185 unit_tile(punit));
186
187 if (pbase) {
188 /* Some unit can build base of given gui_type */
189 return TRUE;
190 }
191 } unit_list_iterate_end;
192
193 return FALSE;
194}
195
196/************************************************************************/
202 bool units_have_type_flag(const struct unit_list *punits,
203 enum unit_type_flag_id flag, bool has_flag)
204{
205 unit_list_iterate(punits, punit) {
206 if (EQ(has_flag, unit_has_type_flag(punit, flag))) {
207 return TRUE;
208 }
209 } unit_list_iterate_end;
210
211 return FALSE;
212}
213
214/************************************************************************/
217 bool units_contain_cityfounder(const struct unit_list *punits)
218{
219 if (game.scenario.prevent_new_cities) {
220 return FALSE;
221 }
222
223 unit_list_iterate(punits, punit) {
224 if (EQ(TRUE, unit_can_do_action(punit, ACTION_FOUND_CITY))) {
225 return TRUE;
226 }
227 } unit_list_iterate_end;
228
229 return FALSE;
230}
231
232/************************************************************************/
239 bool units_can_do_action(const struct unit_list *punits,
240 action_id act_id, bool can_do)
241{
242 unit_list_iterate(punits, punit) {
243 if (EQ(can_do, unit_can_do_action(punit, act_id))) {
244 return TRUE;
245 }
246 } unit_list_iterate_end;
247
248 return FALSE;
249}
250
251/************************************************************************/
258 bool units_can_do_action_with_result(const struct unit_list *punits,
259 enum action_result result, bool can_do)
260{
261 unit_list_iterate(punits, punit) {
262 if (EQ(can_do, unit_can_do_action_result(punit, result))) {
263 return TRUE;
264 }
265 } unit_list_iterate_end;
266
267 return FALSE;
268}
269
270/************************************************************************/
273 bool units_are_occupied(const struct unit_list *punits)
274{
275 unit_list_iterate(punits, punit) {
276 if (get_transporter_occupancy(punit) > 0) {
277 return TRUE;
278 }
279 } unit_list_iterate_end;
280
281 return FALSE;
282}
283
284/************************************************************************/
287 bool units_can_load(const struct unit_list *punits)
288{
289 unit_list_iterate(punits, punit) {
290 if (unit_can_load(punit)) {
291 return TRUE;
292 }
293 } unit_list_iterate_end;
294
295 return FALSE;
296}
297
298/************************************************************************/
301 bool units_can_unload(const struct civ_map *nmap,
302 const struct unit_list *punits)
303{
304 unit_list_iterate(punits, punit) {
305 if (unit_transported(punit)
306 && can_unit_unload(punit, unit_transport_get(punit))
307 && can_unit_exist_at_tile(nmap, punit, unit_tile(punit))) {
308 return TRUE;
309 }
310 } unit_list_iterate_end;
311
312 return FALSE;
313}
314
315/************************************************************************/
319 bool units_have_activity_on_tile(const struct unit_list *punits,
320 enum unit_activity activity)
321{
322 unit_list_iterate(punits, punit) {
323 if (is_unit_activity_on_tile(activity, unit_tile(punit))) {
324 return TRUE;
325 }
326 } unit_list_iterate_end;
327
328 return FALSE;
329}
330
331/************************************************************************/
335 bool units_can_upgrade(const struct civ_map *nmap,
336 const struct unit_list *punits)
337{
338 unit_list_iterate(punits, punit) {
339 if (UU_OK == unit_upgrade_test(nmap, punit, FALSE)) {
340 return TRUE;
341 }
342 } unit_list_iterate_end;
343
344 return FALSE;
345}
346
347/************************************************************************/
350 bool units_can_convert(const struct civ_map *nmap,
351 const struct unit_list *punits)
352{
353 unit_list_iterate(punits, punit) {
354 if (utype_can_do_action(unit_type_get(punit), ACTION_CONVERT)
355 && unit_can_convert(nmap, punit)) {
356 return TRUE;
357 }
358 } unit_list_iterate_end;
359
360 return FALSE;
361}
get_base_by_gui_type
struct base_type * get_base_by_gui_type(enum base_gui_type type, const struct unit *punit, const struct tile *ptile)
Definition base.c:143
incite_cost
char * incite_cost
Definition comments.c:74
punit
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit * punit
Definition dialogs_g.h:74
extra_road_get
#define extra_road_get(_e_)
Definition extras.h:191
extra_type_by_cause_iterate_end
#define extra_type_by_cause_iterate_end
Definition extras.h:339
extra_type_by_cause_iterate
#define extra_type_by_cause_iterate(_cause, _extra)
Definition extras.h:333
is_server
static bool is_server(void)
Definition fc_interface.h:69
action_id
int action_id
Definition fc_types.h:393
game
struct civ_game game
Definition game.c:61
game.h
log.h
fc_assert_ret
#define fc_assert_ret(condition)
Definition log.h:191
can_unit_exist_at_tile
bool can_unit_exist_at_tile(const struct civ_map *nmap, const struct unit *punit, const struct tile *ptile)
Definition movement.c:318
unit_can_load
bool unit_can_load(const struct unit *punit)
Definition movement.c:884
movement.h
can_build_road
bool can_build_road(const struct civ_map *nmap, struct road_type *proad, const struct unit *punit, const struct tile *ptile)
Definition road.c:301
EQ
#define EQ(p, q)
Definition shared.h:72
base_type
Definition base.h:41
civ_game::scenario
struct packet_scenario_info scenario
Definition game.h:87
civ_map
Definition map_types.h:71
extra_type
Definition extras.h:88
packet_scenario_info::prevent_new_cities
bool prevent_new_cities
Definition packets_gen.h:1597
road_type
Definition road.h:71
unit
Definition unit.h:140
unit::activity
enum unit_activity activity
Definition unit.h:159
unit::id
int id
Definition unit.h:147
unit::ord_map
int ord_map
Definition unit.h:244
unit::server
struct unit::@81::@84 server
TRUE
#define TRUE
Definition support.h:46
FALSE
#define FALSE
Definition support.h:47
get_transporter_occupancy
int get_transporter_occupancy(const struct unit *ptrans)
Definition unit.c:1821
unit_transport_get
struct unit * unit_transport_get(const struct unit *pcargo)
Definition unit.c:2499
is_unit_activity_on_tile
bool is_unit_activity_on_tile(enum unit_activity activity, const struct tile *ptile)
Definition unit.c:1152
unit_can_do_action
bool unit_can_do_action(const struct unit *punit, const action_id act_id)
Definition unit.c:386
unit_can_do_action_result
bool unit_can_do_action_result(const struct unit *punit, enum action_result result)
Definition unit.c:396
can_unit_do_activity
bool can_unit_do_activity(const struct civ_map *nmap, const struct unit *punit, enum unit_activity activity)
Definition unit.c:893
unit_upgrade_test
enum unit_upgrade_result unit_upgrade_test(const struct civ_map *nmap, const struct unit *punit, bool is_free)
Definition unit.c:2026
unit_transported
bool unit_transported(const struct unit *pcargo)
Definition unit.c:2483
can_unit_unload
bool can_unit_unload(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:776
unit_can_convert
bool unit_can_convert(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:2073
can_unit_do_activity_targeted
bool can_unit_do_activity_targeted(const struct civ_map *nmap, const struct unit *punit, enum unit_activity activity, struct extra_type *target)
Definition unit.c:932
unit_tile
#define unit_tile(_pu)
Definition unit.h:397
UU_OK
@ UU_OK
Definition unit.h:62
units_can_convert
bool units_can_convert(const struct civ_map *nmap, const struct unit_list *punits)
Definition unitlist.c:350
units_are_occupied
bool units_are_occupied(const struct unit_list *punits)
Definition unitlist.c:273
units_can_do_action_with_result
bool units_can_do_action_with_result(const struct unit_list *punits, enum action_result result, bool can_do)
Definition unitlist.c:258
units_can_unload
bool units_can_unload(const struct civ_map *nmap, const struct unit_list *punits)
Definition unitlist.c:301
compar_unit_ord_city
static int compar_unit_ord_city(const struct unit *const *ua, const struct unit *const *ub)
Definition unitlist.c:62
can_units_do_base_gui
bool can_units_do_base_gui(const struct unit_list *punits, enum base_gui_type base_gui)
Definition unitlist.c:180
units_contain_cityfounder
bool units_contain_cityfounder(const struct unit_list *punits)
Definition unitlist.c:217
unit_list_sort_ord_map
void unit_list_sort_ord_map(struct unit_list *punitlist)
Definition unitlist.c:73
can_units_do_any_road
bool can_units_do_any_road(const struct civ_map *nmap, const struct unit_list *punits)
Definition unitlist.c:161
can_units_do_activity
bool can_units_do_activity(const struct civ_map *nmap, const struct unit_list *punits, enum unit_activity activity)
Definition unitlist.c:128
unit_list_find
struct unit * unit_list_find(const struct unit_list *punitlist, int unit_id)
Definition unitlist.c:30
units_can_upgrade
bool units_can_upgrade(const struct civ_map *nmap, const struct unit_list *punits)
Definition unitlist.c:335
compar_unit_ord_map
static int compar_unit_ord_map(const struct unit *const *ua, const struct unit *const *ub)
Definition unitlist.c:51
units_have_activity_on_tile
bool units_have_activity_on_tile(const struct unit_list *punits, enum unit_activity activity)
Definition unitlist.c:319
units_have_type_flag
bool units_have_type_flag(const struct unit_list *punits, enum unit_type_flag_id flag, bool has_flag)
Definition unitlist.c:202
can_units_do
bool can_units_do(const struct unit_list *punits, bool(can_fn)(const struct unit *punit))
Definition unitlist.c:95
units_can_do_action
bool units_can_do_action(const struct unit_list *punits, action_id act_id, bool can_do)
Definition unitlist.c:239
units_can_load
bool units_can_load(const struct unit_list *punits)
Definition unitlist.c:287
unit_list_sort_ord_city
void unit_list_sort_ord_city(struct unit_list *punitlist)
Definition unitlist.c:85
can_units_do_activity_targeted
bool can_units_do_activity_targeted(const struct civ_map *nmap, const struct unit_list *punits, enum unit_activity activity, struct extra_type *pextra)
Definition unitlist.c:144
can_units_do_on_map
bool can_units_do_on_map(const struct civ_map *nmap, const struct unit_list *punits, bool(can_fn)(const struct civ_map *nmap, const struct unit *punit))
Definition unitlist.c:111
unitlist.h
unit_list_iterate
#define unit_list_iterate(unitlist, punit)
Definition unitlist.h:31
unit_list_iterate_end
#define unit_list_iterate_end
Definition unitlist.h:33
unit_type_get
const struct unit_type * unit_type_get(const struct unit *punit)
Definition unittype.c:123
unit_has_type_flag
bool unit_has_type_flag(const struct unit *punit, enum unit_type_flag_id flag)
Definition unittype.c:190
utype_can_do_action
bool utype_can_do_action(const struct unit_type *putype, const action_id act_id)
Definition unittype.c:371