Freeciv-3.1: /fast/freeciv/nightly/S3_1/common/unitlist.c Source File

Freeciv-3.1

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 unit_list_sort(punitlist, compar_unit_ord_city);
89}
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 /* Make sure nobody uses these old activities any more */
133 fc_assert_ret_val(activity != ACTIVITY_FORTRESS
134 && activity != ACTIVITY_AIRBASE, FALSE);
135
136 unit_list_iterate(punits, punit) {
137 if (can_unit_do_activity(nmap, punit, activity)) {
138 return TRUE;
139 }
140 } unit_list_iterate_end;
141
142 return FALSE;
143}
144
145/************************************************************************/
148 bool can_units_do_activity_targeted(const struct civ_map *nmap,
149 const struct unit_list *punits,
150 enum unit_activity activity,
151 struct extra_type *pextra)
152{
153 unit_list_iterate(punits, punit) {
154 if (can_unit_do_activity_targeted(nmap, punit, activity, pextra)) {
155 return TRUE;
156 }
157 } unit_list_iterate_end;
158
159 return FALSE;
160}
161
162/************************************************************************/
165 bool can_units_do_any_road(const struct civ_map *nmap,
166 const struct unit_list *punits)
167{
168 unit_list_iterate(punits, punit) {
169 extra_type_by_cause_iterate(EC_ROAD, pextra) {
170 struct road_type *proad = extra_road_get(pextra);
171
172 if (can_build_road(nmap, proad, punit, unit_tile(punit))) {
173 return TRUE;
174 }
175 } extra_type_by_cause_iterate_end;
176 } unit_list_iterate_end;
177
178 return FALSE;
179}
180
181/************************************************************************/
184 bool can_units_do_base_gui(const struct unit_list *punits,
185 enum base_gui_type base_gui)
186{
187 unit_list_iterate(punits, punit) {
188 struct base_type *pbase = get_base_by_gui_type(base_gui, punit,
189 unit_tile(punit));
190
191 if (pbase) {
192 /* Some unit can build base of given gui_type */
193 return TRUE;
194 }
195 } unit_list_iterate_end;
196
197 return FALSE;
198}
199
200/************************************************************************/
206 bool units_have_type_flag(const struct unit_list *punits,
207 enum unit_type_flag_id flag, bool has_flag)
208{
209 unit_list_iterate(punits, punit) {
210 if (EQ(has_flag, unit_has_type_flag(punit, flag))) {
211 return TRUE;
212 }
213 } unit_list_iterate_end;
214
215 return FALSE;
216}
217
218/************************************************************************/
221 bool units_contain_cityfounder(const struct unit_list *punits)
222{
223 if (game.scenario.prevent_new_cities) {
224 return FALSE;
225 }
226
227 unit_list_iterate(punits, punit) {
228 if (EQ(TRUE, unit_can_do_action(punit, ACTION_FOUND_CITY))) {
229 return TRUE;
230 }
231 } unit_list_iterate_end;
232
233 return FALSE;
234}
235
236/************************************************************************/
243 bool units_can_do_action(const struct unit_list *punits,
244 action_id act_id, bool can_do)
245{
246 unit_list_iterate(punits, punit) {
247 if (EQ(can_do, unit_can_do_action(punit, act_id))) {
248 return TRUE;
249 }
250 } unit_list_iterate_end;
251
252 return FALSE;
253}
254
255/************************************************************************/
262 bool units_can_do_action_with_result(const struct unit_list *punits,
263 enum action_result result, bool can_do)
264{
265 unit_list_iterate(punits, punit) {
266 if (EQ(can_do, unit_can_do_action_result(punit, result))) {
267 return TRUE;
268 }
269 } unit_list_iterate_end;
270
271 return FALSE;
272}
273
274/************************************************************************/
277 bool units_are_occupied(const struct unit_list *punits)
278{
279 unit_list_iterate(punits, punit) {
280 if (get_transporter_occupancy(punit) > 0) {
281 return TRUE;
282 }
283 } unit_list_iterate_end;
284
285 return FALSE;
286}
287
288/************************************************************************/
291 bool units_can_load(const struct unit_list *punits)
292{
293 unit_list_iterate(punits, punit) {
294 if (unit_can_load(punit)) {
295 return TRUE;
296 }
297 } unit_list_iterate_end;
298
299 return FALSE;
300}
301
302/************************************************************************/
305 bool units_can_unload(const struct unit_list *punits)
306{
307 unit_list_iterate(punits, punit) {
308 if (unit_transported(punit)
309 && can_unit_unload(punit, unit_transport_get(punit))
310 && can_unit_exist_at_tile(&(wld.map), punit, unit_tile(punit))) {
311 return TRUE;
312 }
313 } unit_list_iterate_end;
314
315 return FALSE;
316}
317
318/************************************************************************/
322 bool units_have_activity_on_tile(const struct unit_list *punits,
323 enum unit_activity activity)
324{
325 unit_list_iterate(punits, punit) {
326 if (is_unit_activity_on_tile(activity, unit_tile(punit))) {
327 return TRUE;
328 }
329 } unit_list_iterate_end;
330
331 return FALSE;
332}
333
334/************************************************************************/
338 bool units_can_upgrade(const struct civ_map *nmap,
339 const struct unit_list *punits)
340{
341 unit_list_iterate(punits, punit) {
342 if (UU_OK == unit_upgrade_test(nmap, punit, FALSE)) {
343 return TRUE;
344 }
345 } unit_list_iterate_end;
346
347 return FALSE;
348}
349
350/************************************************************************/
353 bool units_can_convert(const struct civ_map *nmap,
354 const struct unit_list *punits)
355{
356 unit_list_iterate(punits, punit) {
357 if (utype_can_do_action(unit_type_get(punit), ACTION_CONVERT)
358 && unit_can_convert(nmap, punit)) {
359 return TRUE;
360 }
361 } unit_list_iterate_end;
362
363 return FALSE;
364}
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:139
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:73
extra_road_get
#define extra_road_get(_e_)
Definition extras.h:185
extra_type_by_cause_iterate_end
#define extra_type_by_cause_iterate_end
Definition extras.h:315
extra_type_by_cause_iterate
#define extra_type_by_cause_iterate(_cause, _extra)
Definition extras.h:309
is_server
static bool is_server(void)
Definition fc_interface.h:69
action_id
int action_id
Definition fc_types.h:359
game
struct civ_game game
Definition game.c:57
wld
struct world wld
Definition game.c:58
game.h
log.h
fc_assert_ret
#define fc_assert_ret(condition)
Definition log.h:191
fc_assert_ret_val
#define fc_assert_ret_val(condition, val)
Definition log.h:194
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:304
unit_can_load
bool unit_can_load(const struct unit *punit)
Definition movement.c:841
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:295
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:85
packet_scenario_info::prevent_new_cities
bool prevent_new_cities
Definition packets_gen.h:1611
road_type
Definition road.h:75
unit
Definition unit.h:138
unit::activity
enum unit_activity activity
Definition unit.h:157
unit::id
int id
Definition unit.h:145
unit::ord_map
int ord_map
Definition unit.h:239
unit::server
struct unit::@80::@83 server
world::map
struct civ_map map
Definition world_object.h:38
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:1763
unit_transport_get
struct unit * unit_transport_get(const struct unit *pcargo)
Definition unit.c:2421
is_unit_activity_on_tile
bool is_unit_activity_on_tile(enum unit_activity activity, const struct tile *ptile)
Definition unit.c:1139
unit_can_do_action
bool unit_can_do_action(const struct unit *punit, const action_id act_id)
Definition unit.c:330
unit_can_do_action_result
bool unit_can_do_action_result(const struct unit *punit, enum action_result result)
Definition unit.c:340
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:869
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:1968
unit_transported
bool unit_transported(const struct unit *pcargo)
Definition unit.c:2405
can_unit_unload
bool can_unit_unload(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:767
unit_can_convert
bool unit_can_convert(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:2015
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:908
unit_tile
#define unit_tile(_pu)
Definition unit.h:388
UU_OK
@ UU_OK
Definition unit.h:61
units_can_convert
bool units_can_convert(const struct civ_map *nmap, const struct unit_list *punits)
Definition unitlist.c:353
units_are_occupied
bool units_are_occupied(const struct unit_list *punits)
Definition unitlist.c:277
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:262
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:184
units_contain_cityfounder
bool units_contain_cityfounder(const struct unit_list *punits)
Definition unitlist.c:221
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:165
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:338
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_can_unload
bool units_can_unload(const struct unit_list *punits)
Definition unitlist.c:305
units_have_activity_on_tile
bool units_have_activity_on_tile(const struct unit_list *punits, enum unit_activity activity)
Definition unitlist.c:322
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:206
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:243
units_can_load
bool units_can_load(const struct unit_list *punits)
Definition unitlist.c:291
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:148
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:184
utype_can_do_action
bool utype_can_do_action(const struct unit_type *putype, const action_id act_id)
Definition unittype.c:443