Freeciv-3.1: /fast/freeciv/nightly/S3_1/client/unitselect_common.c Source File

Freeciv-3.1

Loading...
Searching...
No Matches
unitselect_common.c
Go to the documentation of this file.
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
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18/* common */
19#include "fc_types.h"
20#include "game.h"
21#include "player.h"
22#include "unit.h"
23#include "unitlist.h"
24#include "unittype.h"
25
26/* client */
27#include "client_main.h"
28#include "control.h"
29
30#include "unitselect_common.h"
31
32static void usdlg_data_add_unit(struct usdata_hash *ushash,
33 const struct tile *ptile,
34 struct unit *punit);
35
36static bool usdlg_check_unit_activity(const struct unit *punit,
37 enum unit_activity act);
38static bool usdlg_check_unit_location(const struct unit *punit,
39 const struct tile *ptile,
40 enum unit_select_location_mode loc);
41
42static struct usdata *usdata_new(void);
43static void usdata_destroy(struct usdata *data);
44
45/*************************************************************************/
48 static struct usdata *usdata_new(void)
49{
50 enum unit_select_location_mode loc;
51 enum unit_activity act;
52
53 struct usdata *data = fc_calloc(1, sizeof(*data));
54
55 data->utype = NULL;
56
57 for (loc = unit_select_location_mode_begin();
58 loc != unit_select_location_mode_end();
59 loc = unit_select_location_mode_next(loc)) {
60 for (act = 0; act < ACTIVITY_LAST; act++) {
61 /* Create a unit list for all activities - even invalid once. */
62 data->units[loc][act] = unit_list_new();
63 }
64 }
65
66 return data;
67}
68
69/*************************************************************************/
72 static void usdata_destroy(struct usdata *data)
73{
74 if (data != NULL) {
75 enum unit_select_location_mode loc;
76 enum unit_activity act;
77
78 for (loc = unit_select_location_mode_begin();
79 loc != unit_select_location_mode_end();
80 loc = unit_select_location_mode_next(loc)) {
81 for (act = 0; act < ACTIVITY_LAST; act++) {
82 if (data->units[loc][act] != NULL) {
83 unit_list_destroy(data->units[loc][act]);
84 }
85 }
86 }
87 free(data);
88 }
89}
90
91/*************************************************************************/
94 struct usdata_hash *usdlg_data_new(const struct tile *ptile)
95{
96 struct usdata_hash *ushash = usdata_hash_new();
97
98 /* Add units of the player(s). */
99 if (client_is_global_observer()) {
100 players_iterate(pplayer) {
101 unit_list_iterate(pplayer->units, punit) {
102 usdlg_data_add_unit(ushash, ptile, punit);
103 } unit_list_iterate_end;
104 } players_iterate_end;
105 } else {
106 struct player *pplayer = client_player();
107 unit_list_iterate(pplayer->units, punit) {
108 usdlg_data_add_unit(ushash, ptile, punit);
109 } unit_list_iterate_end;
110 }
111
112 /* Add units on the tile (SELLOC_UNITS). */
113 unit_list_iterate(ptile->units, punit) {
114 /* Save all top level transporters on the tile (SELLOC_UNITS) as 'idle'
115 * units (ACTIVITY_IDLE). */
116 if (!unit_transported(punit)) {
117 struct usdata *data;
118 const struct unit_type *ptype = unit_type_get(punit);
119
120 usdata_hash_lookup(ushash, utype_index(ptype), &data);
121
122 if (!data) {
123 data = usdata_new();
124 data->utype = ptype;
125 usdata_hash_insert(ushash, utype_index(ptype), data);
126 }
127
128 unit_list_append(data->units[SELLOC_UNITS][ACTIVITY_IDLE], punit);
129 }
130 } unit_list_iterate_end;
131
132 return ushash;
133}
134
135/*************************************************************************/
138 void usdlg_data_destroy(struct usdata_hash *ushash)
139{
140 usdata_hash_data_iterate(ushash, data) {
141 usdata_destroy(data);
142 } usdata_hash_data_iterate_end;
143 usdata_hash_destroy(ushash);
144}
145
146/*************************************************************************/
149 static void usdlg_data_add_unit(struct usdata_hash *ushash,
150 const struct tile *ptile,
151 struct unit *punit)
152{
153 struct usdata *data;
154 enum unit_activity act;
155 const struct unit_type *ptype = unit_type_get(punit);
156
157 fc_assert_ret(ushash);
158 fc_assert_ret(punit);
159
160 usdata_hash_lookup(ushash, utype_index(ptype), &data);
161
162 if (!data) {
163 data = usdata_new();
164 data->utype = ptype;
165 usdata_hash_insert(ushash, utype_index(ptype), data);
166 }
167
168 for (act = 0; act < ACTIVITY_LAST; act++) {
169 if (usdlg_check_unit_activity(punit, act)) {
170 enum unit_select_location_mode loc;
171
172 for (loc = unit_select_location_mode_begin();
173 loc != unit_select_location_mode_end();
174 loc = unit_select_location_mode_next(loc)) {
175 if (usdlg_check_unit_location(punit, ptile, loc)) {
176 unit_list_append(data->units[loc][act], punit);
177 }
178 }
179 }
180 }
181}
182
183/*************************************************************************/
186 static bool usdlg_check_unit_activity(const struct unit *punit,
187 enum unit_activity act)
188{
189 fc_assert_ret_val(punit != NULL, FALSE);
190
191 return (punit->activity == act);
192}
193
194/*************************************************************************/
197 static bool usdlg_check_unit_location(const struct unit *punit,
198 const struct tile *ptile,
199 enum unit_select_location_mode loc)
200{
201 struct tile *utile;
202
203 fc_assert_ret_val(punit != NULL, FALSE);
204
205 utile = unit_tile(punit);
206
207 fc_assert_ret_val(utile != NULL, FALSE);
208
209 switch (loc) {
210 case SELLOC_UNITS:
211 /* Special case - never TRUE. See usdlg_data_new(). */
212 return FALSE;
213 break;
214 case SELLOC_TILE:
215 if (utile == ptile) {
216 return TRUE;
217 }
218 break;
219 case SELLOC_CONT:
220 if (!is_ocean(tile_terrain(utile))
221 && utype_move_type(unit_type_get(punit)) == UMT_LAND
222 && tile_continent(utile) == tile_continent(ptile)) {
223 return TRUE;
224 }
225 break;
226 case SELLOC_WORLD:
227 return TRUE;
228 break;
229 case SELLOC_LAND:
230 if (utype_move_type(unit_type_get(punit)) == UMT_LAND
231 && (!is_ocean(tile_terrain(utile)) || tile_city(utile) != NULL)) {
232 return TRUE;
233 }
234 break;
235 case SELLOC_SEA:
236 if (utype_move_type(unit_type_get(punit)) == UMT_SEA
237 && (is_ocean(tile_terrain(utile)) || tile_city(utile) != NULL)) {
238 return TRUE;
239 }
240 break;
241 case SELLOC_BOTH:
242 if (utype_move_type(unit_type_get(punit)) == UMT_BOTH) {
243 return TRUE;
244 }
245 break;
246 case SELLOC_COUNT:
247 /* Invalid. */
248 fc_assert_ret_val(loc != SELLOC_COUNT, FALSE);
249 break;
250 }
251
252 return FALSE;
253}
client_is_global_observer
bool client_is_global_observer(void)
Definition client_main.c:1346
client_main.h
client_player
#define client_player()
Definition client_main.h:106
control.h
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
fc_types.h
game.h
loc
struct tile * loc
Definition citydlg.c:220
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
fc_calloc
#define fc_calloc(n, esz)
Definition mem.h:38
player.h
players_iterate_end
#define players_iterate_end
Definition player.h:535
players_iterate
#define players_iterate(_pplayer)
Definition player.h:530
player
Definition player.h:249
player::units
struct unit_list * units
Definition player.h:282
tile
Definition tile.h:49
tile::units
struct unit_list * units
Definition tile.h:57
unit_type
Definition unittype.h:481
unit
Definition unit.h:138
unit::activity
enum unit_activity activity
Definition unit.h:157
usdata
Definition unitselect_common.h:24
usdata::units
struct unit_list * units[SELLOC_COUNT][ACTIVITY_LAST]
Definition unitselect_common.h:26
usdata::utype
const struct unit_type * utype
Definition unitselect_common.h:25
TRUE
#define TRUE
Definition support.h:46
FALSE
#define FALSE
Definition support.h:47
is_ocean
#define is_ocean(pterrain)
Definition terrain.h:287
tile_city
struct city * tile_city(const struct tile *ptile)
Definition tile.c:83
tile_terrain
#define tile_terrain(_tile)
Definition tile.h:109
tile_continent
#define tile_continent(_tile)
Definition tile.h:91
unit_transported
bool unit_transported(const struct unit *pcargo)
Definition unit.c:2411
unit.h
unit_tile
#define unit_tile(_pu)
Definition unit.h:388
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
usdlg_data_new
struct usdata_hash * usdlg_data_new(const struct tile *ptile)
Definition unitselect_common.c:94
usdlg_data_add_unit
static void usdlg_data_add_unit(struct usdata_hash *ushash, const struct tile *ptile, struct unit *punit)
Definition unitselect_common.c:149
usdata_destroy
static void usdata_destroy(struct usdata *data)
Definition unitselect_common.c:72
usdlg_check_unit_location
static bool usdlg_check_unit_location(const struct unit *punit, const struct tile *ptile, enum unit_select_location_mode loc)
Definition unitselect_common.c:197
usdlg_data_destroy
void usdlg_data_destroy(struct usdata_hash *ushash)
Definition unitselect_common.c:138
usdata_new
static struct usdata * usdata_new(void)
Definition unitselect_common.c:48
usdlg_check_unit_activity
static bool usdlg_check_unit_activity(const struct unit *punit, enum unit_activity act)
Definition unitselect_common.c:186
unitselect_common.h
usdata_hash_data_iterate
#define usdata_hash_data_iterate(phash, data)
Definition unitselect_common.h:34
usdata_hash_data_iterate_end
#define usdata_hash_data_iterate_end
Definition unitselect_common.h:36
unit_type_get
const struct unit_type * unit_type_get(const struct unit *punit)
Definition unittype.c:123
utype_move_type
enum unit_move_type utype_move_type(const struct unit_type *punittype)
Definition unittype.c:1603
utype_index
Unit_type_id utype_index(const struct unit_type *punittype)
Definition unittype.c:91
unittype.h