Freeciv-3.3
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,
41
42static struct usdata *usdata_new(void);
43static void usdata_destroy(struct usdata *data);
44
45/*************************************************************************/
48static struct usdata *usdata_new(void)
49{
51 enum unit_activity act;
52
53 struct usdata *data = fc_calloc(1, sizeof(*data));
54
55 data->utype = NULL;
56
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/*************************************************************************/
72static void usdata_destroy(struct usdata *data)
73{
74 if (data != NULL) {
76 enum unit_activity act;
77
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/*************************************************************************/
94struct usdata_hash *usdlg_data_new(const struct tile *ptile)
95{
97
98 /* Add units of the player(s). */
100 players_iterate(pplayer) {
101 unit_list_iterate(pplayer->units, punit) {
105 } else {
106 struct player *pplayer = client_player();
107 unit_list_iterate(pplayer->units, punit) {
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
121
122 if (!data) {
123 data = usdata_new();
124 data->utype = ptype;
126 }
127
129 }
131
132 return ushash;
133}
134
135/*************************************************************************/
145
146/*************************************************************************/
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
159
161
162 if (!data) {
163 data = usdata_new();
164 data->utype = ptype;
166 }
167
168 for (act = 0; act < ACTIVITY_LAST; act++) {
171
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/*************************************************************************/
186static bool usdlg_check_unit_activity(const struct unit *punit,
187 enum unit_activity act)
188{
190
191 return (punit->activity == act);
192}
193
194/*************************************************************************/
197static bool usdlg_check_unit_location(const struct unit *punit,
198 const struct tile *ptile,
200{
201 struct tile *utile;
202
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))
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:
231 && (!is_ocean(tile_terrain(utile)) || tile_city(utile) != NULL)) {
232 return TRUE;
233 }
234 break;
235 case SELLOC_SEA:
237 && (is_ocean(tile_terrain(utile)) || tile_city(utile) != NULL)) {
238 return TRUE;
239 }
240 break;
241 case SELLOC_BOTH:
243 return TRUE;
244 }
245 break;
246 case SELLOC_COUNT:
247 /* Invalid. */
249 break;
250 }
251
252 return FALSE;
253}
bool client_is_global_observer(void)
#define client_player()
char * incite_cost
Definition comments.c:76
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
struct tile * loc
Definition citydlg.c:227
#define fc_assert_ret(condition)
Definition log.h:192
#define fc_assert_ret_val(condition, val)
Definition log.h:195
#define fc_calloc(n, esz)
Definition mem.h:38
#define players_iterate_end
Definition player.h:542
#define players_iterate(_pplayer)
Definition player.h:537
struct unit_list * units
Definition player.h:282
Definition tile.h:50
struct unit_list * units
Definition tile.h:58
Definition unit.h:140
enum unit_activity activity
Definition unit.h:159
struct unit_list * units[SELLOC_COUNT][ACTIVITY_LAST]
const struct unit_type * utype
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
#define is_ocean(pterrain)
Definition terrain.h:194
struct city * tile_city(const struct tile *ptile)
Definition tile.c:83
#define tile_terrain(_tile)
Definition tile.h:111
#define tile_continent(_tile)
Definition tile.h:93
bool unit_transported(const struct unit *pcargo)
Definition unit.c:2502
#define unit_tile(_pu)
Definition unit.h:404
#define unit_list_iterate(unitlist, punit)
Definition unitlist.h:31
#define unit_list_iterate_end
Definition unitlist.h:33
struct usdata_hash * usdlg_data_new(const struct tile *ptile)
static void usdlg_data_add_unit(struct usdata_hash *ushash, const struct tile *ptile, struct unit *punit)
static void usdata_destroy(struct usdata *data)
static bool usdlg_check_unit_location(const struct unit *punit, const struct tile *ptile, enum unit_select_location_mode loc)
void usdlg_data_destroy(struct usdata_hash *ushash)
static struct usdata * usdata_new(void)
static bool usdlg_check_unit_activity(const struct unit *punit, enum unit_activity act)
#define usdata_hash_data_iterate(phash, data)
#define usdata_hash_data_iterate_end
const struct unit_type * unit_type_get(const struct unit *punit)
Definition unittype.c:123
enum unit_move_type utype_move_type(const struct unit_type *punittype)
Definition unittype.c:1557
Unit_type_id utype_index(const struct unit_type *punittype)
Definition unittype.c:91