File: unitselect_common.c

package info (click to toggle)
freeciv 2.6.2-1~bpo10+1
  • links: PTS, VCS
  • area: main
  • in suites: buster-backports
  • size: 212,500 kB
  • sloc: ansic: 443,831; cpp: 29,541; sh: 7,982; makefile: 7,886; python: 1,933; xml: 945
file content (253 lines) | stat: -rw-r--r-- 8,016 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
/*****************************************************************************
 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
   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

/* common */
#include "fc_types.h"
#include "game.h"
#include "player.h"
#include "unit.h"
#include "unitlist.h"
#include "unittype.h"

/* client */
#include "client_main.h"
#include "control.h"

#include "unitselect_common.h"

static void usdlg_data_add_unit(struct usdata_hash *ushash,
                                const struct tile *ptile,
                                struct unit *punit);

static bool usdlg_check_unit_activity(const struct unit *punit,
                                      enum unit_activity act);
static bool usdlg_check_unit_location(const struct unit *punit,
                                      const struct tile *ptile,
                                      enum unit_select_location_mode loc);

static struct usdata *usdata_new(void);
static void usdata_destroy(struct usdata *data);

/*****************************************************************************
  Create a unit selection data struct.
*****************************************************************************/
static struct usdata *usdata_new(void)
{
  enum unit_select_location_mode loc;
  enum unit_activity act;

  struct usdata *data = fc_calloc(1, sizeof(*data));

  data->utype = NULL;

  for (loc = unit_select_location_mode_begin();
       loc != unit_select_location_mode_end();
       loc = unit_select_location_mode_next(loc)) {
    for (act = 0; act < ACTIVITY_LAST; act++) {
      /* Create a unit list for all activities - even invalid once. */
      data->units[loc][act] = unit_list_new();
    }
  }

  return data;
}

/*****************************************************************************
  Destroy a unit selection struct.
*****************************************************************************/
static void usdata_destroy(struct usdata *data)
{
  if (data != NULL) {
    enum unit_select_location_mode loc;
    enum unit_activity act;

    for (loc = unit_select_location_mode_begin();
         loc != unit_select_location_mode_end();
         loc = unit_select_location_mode_next(loc)) {
      for (act = 0; act < ACTIVITY_LAST; act++) {
        if (data->units[loc][act] != NULL) {
          unit_list_destroy(data->units[loc][act]);
        }
      }
    }
    free(data);
  }
}

/*****************************************************************************
  Create a unit selection data set.
*****************************************************************************/
struct usdata_hash *usdlg_data_new(const struct tile *ptile)
{
  struct usdata_hash *ushash = usdata_hash_new();

  /* Add units of the player(s). */
  if (client_is_global_observer()) {
    players_iterate(pplayer) {
      unit_list_iterate(pplayer->units, punit) {
        usdlg_data_add_unit(ushash, ptile, punit);
      } unit_list_iterate_end;
    } players_iterate_end;
  } else {
    struct player *pplayer = client_player();
    unit_list_iterate(pplayer->units, punit) {
      usdlg_data_add_unit(ushash, ptile, punit);
    } unit_list_iterate_end;
  }

  /* Add units on the tile (SELLOC_UNITS). */
  unit_list_iterate(ptile->units, punit) {
    /* Save all top level transporters on the tile (SELLOC_UNITS) as 'idle'
     * units (ACTIVITY_IDLE). */
    if (!unit_transported(punit)) {
      struct usdata *data;
      struct unit_type *ptype = unit_type_get(punit);

      usdata_hash_lookup(ushash, utype_index(ptype), &data);

      if (!data) {
        data = usdata_new();
        data->utype = ptype;
        usdata_hash_insert(ushash, utype_index(ptype), data);
      }

      unit_list_append(data->units[SELLOC_UNITS][ACTIVITY_IDLE], punit);
    }
  } unit_list_iterate_end;

  return ushash;
}

/*****************************************************************************
  Destroy a unit selection data set.
*****************************************************************************/
void usdlg_data_destroy(struct usdata_hash *ushash)
{
  usdata_hash_data_iterate(ushash, data) {
    usdata_destroy(data);
  } usdata_hash_data_iterate_end;
  usdata_hash_destroy(ushash);
}

/*****************************************************************************
  Add a unit into the unit selection data hash.
*****************************************************************************/
static void usdlg_data_add_unit(struct usdata_hash *ushash,
                                const struct tile *ptile,
                                struct unit *punit)
{
  struct usdata *data;
  enum unit_activity act;
  struct unit_type *ptype = unit_type_get(punit);

  fc_assert_ret(ushash);
  fc_assert_ret(punit);

  usdata_hash_lookup(ushash, utype_index(ptype), &data);

  if (!data) {
    data = usdata_new();
    data->utype = ptype;
    usdata_hash_insert(ushash, utype_index(ptype), data);
  }

  for (act = 0; act < ACTIVITY_LAST; act++) {
    if (usdlg_check_unit_activity(punit, act)) {
      enum unit_select_location_mode loc;

      for (loc = unit_select_location_mode_begin();
           loc != unit_select_location_mode_end();
           loc = unit_select_location_mode_next(loc)) {
        if (usdlg_check_unit_location(punit, ptile, loc)) {
          unit_list_append(data->units[loc][act], punit);
        }
      }
    }
  }
}

/*****************************************************************************
  Returns TRUE if the unit activity is equal to 'act'.
*****************************************************************************/
static bool usdlg_check_unit_activity(const struct unit *punit,
                                      enum unit_activity act)
{
  fc_assert_ret_val(punit != NULL, FALSE);

  return (punit->activity == act);
}

/*****************************************************************************
  Returns TRUE if the unit locations corresponds to 'loc'.
*****************************************************************************/
static bool usdlg_check_unit_location(const struct unit *punit,
                                      const struct tile *ptile,
                                      enum unit_select_location_mode loc)
{
  struct tile *utile;

  fc_assert_ret_val(punit != NULL, FALSE);

  utile = unit_tile(punit);

  fc_assert_ret_val(utile != NULL, FALSE);

  switch (loc) {
  case SELLOC_UNITS:
    /* Special case - never TRUE. See usdlg_data_new(). */
    return FALSE;
    break;
  case SELLOC_TILE:
    if (utile == ptile) {
      return TRUE;
    }
    break;
  case SELLOC_CONT:
    if (!is_ocean(tile_terrain(utile))
        && utype_move_type(unit_type_get(punit)) == UMT_LAND
        && tile_continent(utile) == tile_continent(ptile)) {
      return TRUE;
    }
    break;
  case SELLOC_WORLD:
    return TRUE;
    break;
  case SELLOC_LAND:
    if (utype_move_type(unit_type_get(punit)) == UMT_LAND
        && (!is_ocean(tile_terrain(utile)) || tile_city(utile) != NULL)) {
      return TRUE;
    }
    break;
  case SELLOC_SEA:
    if (utype_move_type(unit_type_get(punit)) == UMT_SEA
        && (is_ocean(tile_terrain(utile)) || tile_city(utile) != NULL)) {
      return TRUE;
    }
    break;
  case SELLOC_BOTH:
    if (utype_move_type(unit_type_get(punit)) == UMT_BOTH) {
      return TRUE;
    }
    break;
  case SELLOC_COUNT:
    /* Invalid. */
    fc_assert_ret_val(loc != SELLOC_COUNT, FALSE);
    break;
  }

  return FALSE;
}