File: road.c

package info (click to toggle)
freeciv 2.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 212,508 kB
  • sloc: ansic: 443,831; cpp: 29,541; sh: 7,982; makefile: 7,886; python: 1,933; xml: 945
file content (515 lines) | stat: -rw-r--r-- 16,487 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/****************************************************************************
 Freeciv - Copyright (C) 2004 - The Freeciv Team
   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 "extras.h"
#include "fc_types.h"
#include "game.h"
#include "map.h"
#include "movement.h"
#include "name_translation.h"
#include "unittype.h"

#include "road.h"

/**************************************************************************
  Return the road id.
**************************************************************************/
Road_type_id road_number(const struct road_type *proad)
{
  fc_assert_ret_val(NULL != proad, -1);

  return proad->id;
}

/**************************************************************************
  Return the road index.

  Currently same as road_number(), paired with road_count()
  indicates use as an array index.
**************************************************************************/
Road_type_id road_index(const struct road_type *proad)
{
  fc_assert_ret_val(NULL != proad, -1);

  /* FIXME: */
  /* return proad - roads; */
  return road_number(proad);
}

/**************************************************************************
  Return extra that road is.
**************************************************************************/
struct extra_type *road_extra_get(const struct road_type *proad)
{
  return proad->self;
}

/**************************************************************************
  Return the number of road_types.
**************************************************************************/
Road_type_id road_count(void)
{
  return game.control.num_road_types;
}

/****************************************************************************
  Return road type of given id.
****************************************************************************/
struct road_type *road_by_number(Road_type_id id)
{
  struct extra_type_list *roads;

  roads = extra_type_list_by_cause(EC_ROAD);

  if (roads == NULL || id < 0 || id >= extra_type_list_size(roads)) {
    return NULL;
  }

  return extra_road_get(extra_type_list_get(roads, id));
}

/****************************************************************************
  This function is passed to road_type_list_sort() to sort a list of roads
  in ascending move_cost (faster roads first).
****************************************************************************/
int compare_road_move_cost(const struct extra_type *const *p,
                           const struct extra_type *const *q)
{
  const struct road_type *proad = extra_road_get(*p);
  const struct road_type *qroad = extra_road_get(*q);

  if (proad->move_cost > qroad->move_cost) {
    return -1; /* q is faster */
  } else if (proad->move_cost == qroad->move_cost) {
    return 0;
  } else {
    return 1; /* p is faster */
  }
}

/****************************************************************************
  Initialize road_type structures.
****************************************************************************/
void road_type_init(struct extra_type *pextra, int idx)
{
  struct road_type *proad;

  proad = fc_malloc(sizeof(*proad));

  pextra->data.road = proad;

  requirement_vector_init(&proad->first_reqs);

  proad->id = idx;
  proad->integrators = NULL;
  proad->self = pextra;
}


/****************************************************************************
  Initialize the road integrators cache
****************************************************************************/
void road_integrators_cache_init(void)
{
  extra_type_by_cause_iterate(EC_ROAD, pextra) {
    struct road_type *proad = extra_road_get(pextra);

    proad->integrators = extra_type_list_new();
    /* Roads always integrate with themselves. */
    extra_type_list_append(proad->integrators, pextra);
    extra_type_by_cause_iterate(EC_ROAD, oextra) {
      struct road_type *oroad = extra_road_get(oextra);
      if (BV_ISSET(proad->integrates, road_index(oroad))) {
        extra_type_list_append(proad->integrators, oextra);
      }
    } extra_type_by_cause_iterate_end;
    extra_type_list_unique(proad->integrators);
    extra_type_list_sort(proad->integrators, &compare_road_move_cost);
  } extra_type_by_cause_iterate_end;
}

/****************************************************************************
  Free the memory associated with road types
****************************************************************************/
void road_types_free(void)
{
  extra_type_by_cause_iterate(EC_ROAD, pextra) {
    struct road_type *proad = extra_road_get(pextra);

    requirement_vector_free(&proad->first_reqs);

    if (proad->integrators != NULL) {
      extra_type_list_destroy(proad->integrators);
      proad->integrators = NULL;
    }
  } extra_type_by_cause_iterate_end;
}

/****************************************************************************
  Return tile special that used to represent this road type.
****************************************************************************/
enum road_compat road_compat_special(const struct road_type *proad)
{
  return proad->compat;
}

/****************************************************************************
  Return road type represented by given compatibility special, or NULL if
  special does not represent road type at all.
****************************************************************************/
struct road_type *road_by_compat_special(enum road_compat compat)
{
  if (compat == ROCO_NONE) {
    return NULL;
  }

  extra_type_by_cause_iterate(EC_ROAD, pextra) {
    struct road_type *proad = extra_road_get(pextra);
    if (road_compat_special(proad) == compat) {
      return proad;
    }
  } extra_type_by_cause_iterate_end;

  return NULL;
}

/****************************************************************************
  Tells if road can build to tile if all other requirements are met.
****************************************************************************/
bool road_can_be_built(const struct road_type *proad, const struct tile *ptile)
{

  if (!(road_extra_get(proad)->buildable)) {
    /* Road type not buildable. */
    return FALSE;
  }

  if (tile_has_road(ptile, proad)) {
    /* Road exist already */
    return FALSE;
  }

  if (tile_terrain(ptile)->road_time == 0) {
    return FALSE;
  }

  return TRUE;
}

/****************************************************************************
  Tells if player can build road to tile with suitable unit.
****************************************************************************/
bool can_build_road_base(const struct road_type *proad,
                         const struct player *pplayer,
                         const struct tile *ptile)
{
  if (!road_can_be_built(proad, ptile)) {
    return FALSE;
  }

  if (road_has_flag(proad, RF_REQUIRES_BRIDGE)
      && !player_knows_techs_with_flag(pplayer, TF_BRIDGE)) {
    /* TODO: Cache list of road types with RF_PREVENTS_OTHER_ROADS
     *       after ruleset loading and use that list here instead
     *       of always iterating through all road types. */
    extra_type_by_cause_iterate(EC_ROAD, poextra) {
      struct road_type *old = extra_road_get(poextra);

      if (road_has_flag(old, RF_PREVENTS_OTHER_ROADS)
          && tile_has_extra(ptile, poextra)) {
        return FALSE;
      }
    } extra_type_by_cause_iterate_end;
  }

  return TRUE;
}

/****************************************************************************
  Tells if player and optionally unit have road building requirements
  fulfilled.
****************************************************************************/
static bool are_road_reqs_fulfilled(const struct road_type *proad,
                                    const struct player *pplayer,
                                    const struct unit *punit,
                                    const struct tile *ptile)
{
  struct extra_type *pextra = road_extra_get(proad);
  const struct unit_type *utype;

  if (punit == NULL) {
    utype = NULL;
  } else {
    utype = unit_type_get(punit);
  }

  if (requirement_vector_size(&proad->first_reqs) > 0) {
    bool beginning = TRUE;

    extra_type_list_iterate(proad->integrators, iroad) {
      /* FIXME: mixing cardinal and non-cardinal roads as integrators is
       * probably not a good idea. */
      if (is_cardinal_only_road(iroad)) {
        cardinal_adjc_iterate(ptile, adjc_tile) {
          if (tile_has_extra(adjc_tile, iroad)) {
            beginning = FALSE;
            break;
          }
        } cardinal_adjc_iterate_end;
      } else {
        adjc_iterate(ptile, adjc_tile) {
          if (tile_has_extra(adjc_tile, iroad)) {
            beginning = FALSE;
            break;
          }
        } adjc_iterate_end;
      }

      if (!beginning) {
        break;
      }
    } extra_type_list_iterate_end;

    if (beginning) {
      if (!are_reqs_active(pplayer, tile_owner(ptile), NULL, NULL, ptile,
                           punit, utype, NULL, NULL,
                           &proad->first_reqs, RPT_POSSIBLE)) {
        return FALSE;
      }
    }
  }

  return are_reqs_active(pplayer, tile_owner(ptile), NULL, NULL, ptile,
                         punit, utype, NULL, NULL, &pextra->reqs,
                         RPT_POSSIBLE);
}

/****************************************************************************
  Tells if player can build road to tile with suitable unit.
****************************************************************************/
bool player_can_build_road(const struct road_type *proad,
                           const struct player *pplayer,
                           const struct tile *ptile)
{
  if (!can_build_road_base(proad, pplayer, ptile)) {
    return FALSE;
  }

  return are_road_reqs_fulfilled(proad, pplayer, NULL, ptile);
}

/****************************************************************************
  Tells if unit can build road on tile.
****************************************************************************/
bool can_build_road(struct road_type *proad,
		    const struct unit *punit,
		    const struct tile *ptile)
{
  struct player *pplayer = unit_owner(punit);

  if (!can_build_road_base(proad, pplayer, ptile)) {
    return FALSE;
  }

  return are_road_reqs_fulfilled(proad, pplayer, punit, ptile);
}

/****************************************************************************
  Count tiles with specified road near the tile. Can be called with NULL
  road.
****************************************************************************/
int count_road_near_tile(const struct tile *ptile, const struct road_type *proad)
{
  int count = 0;

  if (proad == NULL) {
    return 0;
  }

  adjc_iterate(ptile, adjc_tile) {
    if (tile_has_road(adjc_tile, proad)) {
      count++;
    }
  } adjc_iterate_end;

  return count;
}

/****************************************************************************
  Count tiles with any river near the tile.
****************************************************************************/
int count_river_near_tile(const struct tile *ptile,
                          const struct extra_type *priver)
{
  int count = 0;

  cardinal_adjc_iterate(ptile, adjc_tile) {
    if (priver == NULL && tile_has_river(adjc_tile)) {
      /* Some river */
      count++;
    } else if (priver != NULL && tile_has_extra(adjc_tile, priver)) {
      /* Specific river */
      count++;
    }
  } cardinal_adjc_iterate_end;

  return count;
}

/****************************************************************************
  Count tiles with river of specific type cardinally adjacent to the tile.
****************************************************************************/
int count_river_type_tile_card(const struct tile *ptile,
                               const struct extra_type *priver,
                               bool percentage)
{
  int count = 0;
  int total = 0;

  fc_assert(priver != NULL);

  cardinal_adjc_iterate(ptile, adjc_tile) {
    if (tile_has_extra(adjc_tile, priver)) {
      count++;
    }
    total++;
  } cardinal_adjc_iterate_end;

  if (percentage) {
    count = count * 100 / total;
  }
  return count;
}

/****************************************************************************
  Count tiles with river of specific type near the tile.
****************************************************************************/
int count_river_type_near_tile(const struct tile *ptile,
                               const struct extra_type *priver,
                               bool percentage)
{
  int count = 0;
  int total = 0;

  fc_assert(priver != NULL);

  adjc_iterate(ptile, adjc_tile) {
    if (tile_has_extra(adjc_tile, priver)) {
      count++;
    }
    total++;
  } adjc_iterate_end;

  if (percentage) {
    count = count * 100 / total;
  }
  return count;
}

/****************************************************************************
  Check if road provides effect
****************************************************************************/
bool road_has_flag(const struct road_type *proad, enum road_flag_id flag)
{
  return BV_ISSET(proad->flags, flag);
}

/****************************************************************************
  Returns TRUE iff any cardinally adjacent tile contains a road with
  the given flag (does not check ptile itself).
****************************************************************************/
bool is_road_flag_card_near(const struct tile *ptile, enum road_flag_id flag)
{
  extra_type_by_cause_iterate(EC_ROAD, pextra) {
    if (road_has_flag(extra_road_get(pextra), flag)) {
      cardinal_adjc_iterate(ptile, adjc_tile) {
        if (tile_has_extra(adjc_tile, pextra)) {
          return TRUE;
        }
      } cardinal_adjc_iterate_end;
    }
  } extra_type_by_cause_iterate_end;

  return FALSE;
}

/****************************************************************************
  Returns TRUE iff any adjacent tile contains a road with the given flag
  (does not check ptile itself).
****************************************************************************/
bool is_road_flag_near_tile(const struct tile *ptile, enum road_flag_id flag)
{
  extra_type_by_cause_iterate(EC_ROAD, pextra) {
    if (road_has_flag(extra_road_get(pextra), flag)) {
      adjc_iterate(ptile, adjc_tile) {
        if (tile_has_extra(adjc_tile, pextra)) {
          return TRUE;
        }
      } adjc_iterate_end;
    }
  } extra_type_by_cause_iterate_end;

  return FALSE;
}

/****************************************************************************
  Is tile native to road?
****************************************************************************/
bool is_native_tile_to_road(const struct road_type *proad,
                            const struct tile *ptile)
{
  struct extra_type *pextra;

  if (road_has_flag(proad, RF_RIVER)) {
    if (!terrain_has_flag(tile_terrain(ptile), TER_CAN_HAVE_RIVER)) {
      return FALSE;
    }
  } else if (tile_terrain(ptile)->road_time == 0) {
    return FALSE;
  }

  pextra = road_extra_get(proad);

  return are_reqs_active(NULL, NULL, NULL, NULL, ptile,
                         NULL, NULL, NULL, NULL,
                         &pextra->reqs, RPT_POSSIBLE);
}

/****************************************************************************
  Is extra cardinal only road.
****************************************************************************/
bool is_cardinal_only_road(const struct extra_type *pextra)
{
  const struct road_type *proad;

  if (!is_extra_caused_by(pextra, EC_ROAD)) {
    return FALSE;
  }

  proad = extra_road_get(pextra);

  return proad->move_mode == RMM_CARDINAL || proad->move_mode == RMM_RELAXED;
}

/****************************************************************************
  Does road type provide move bonus
****************************************************************************/
bool road_provides_move_bonus(const struct road_type *proad)
{
  return proad->move_cost >= 0;
}