Freeciv-3.2
Loading...
Searching...
No Matches
citymap.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 2003 - Per I. Mathisen
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#include <stdio.h>
19#include <string.h>
20
21/* utility */
22#include "log.h"
23#include "mem.h"
24#include "support.h"
25
26/* common */
27#include "city.h"
28#include "game.h"
29#include "map.h"
30#include "unit.h"
31#include "unitlist.h"
32#include "unittype.h"
33
34#include "citymap.h"
35
36/* CITYMAP - reserve space for cities
37 *
38 * The citymap is a large int double array that corresponds to
39 * the freeciv main map. For each tile, it stores three different
40 * and exclusive values in a single int: A positive int tells you
41 * how many cities can use this tile (a crowdedness indicator).
42 * A value of zero indicates that the tile is presently unused and
43 * available. A negative value means that this tile is occupied
44 * and reserved by some city or unit: in this case the value gives
45 * the negative of the ID of the city or unit that has reserved the
46 * tile.
47 *
48 * Code that uses the citymap should modify its behaviour based on
49 * positive values encountered, and never attempt to steal a tile
50 * which has a negative value.
51 */
52
53static int *citymap = NULL;
54
55#define log_citymap log_debug
56
57/**********************************************************************/
61void citymap_turn_init(struct player *pplayer)
62{
63 const struct civ_map *nmap = &(wld.map);
64
65 /* The citymap is reinitialized at the start of ever turn. This includes
66 * a call to realloc, which only really matters if this is the first turn
67 * of the game (but it's easier than a separate function to do this). */
69 memset(citymap, 0, MAP_INDEX_SIZE * sizeof(*citymap));
70
72 city_list_iterate(pother->cities, pcity) {
73 struct tile *pcenter = city_tile(pcity);
74
75 /* Reserve at least the default (squared) city radius */
78 pcenter, ptile) {
79 struct city *pwork = tile_worked(ptile);
80
81 if (NULL != pwork) {
82 citymap[tile_index(ptile)] = -(pwork->id);
83 } else {
84 citymap[tile_index(ptile)]++;
85 }
89
90 unit_list_iterate(pplayer->units, punit) {
93
94 /* Use default (squared) city radius */
96 ptile) {
97 if (citymap[tile_index(ptile)] >= 0) {
98 citymap[tile_index(ptile)]++;
99 }
101
103 }
105}
106
107/**********************************************************************/
110void citymap_free(void)
111{
112 if (citymap != NULL) {
114 }
115}
116
117/**********************************************************************/
122void citymap_reserve_city_spot(struct tile *ptile, int id)
123{
124 const struct civ_map *nmap = &(wld.map);
125
126#ifdef FREECIV_DEBUG
127 log_citymap("id %d reserving (%d, %d), was %d",
128 id, TILE_XY(ptile), citymap[tile_index(ptile)]);
129 fc_assert_ret(0 <= citymap[tile_index(ptile)]);
130#endif /* FREECIV_DEBUG */
131
132 /* Tiles will now be "reserved" by actual workers, so free excess
133 * reservations. Also mark tiles for city overlapping, or 'crowding'.
134 * Uses the default city map size / squared city radius. */
136 if (citymap[tile_index(ptile1)] == -id) {
138 }
139 if (citymap[tile_index(ptile1)] >= 0) {
141 }
143
144 citymap[tile_index(ptile)] = -(id);
145}
146
147/**********************************************************************/
150void citymap_free_city_spot(struct tile *ptile, int id)
151{
152 const struct civ_map *nmap = &(wld.map);
153
155 if (citymap[tile_index(ptile1)] == -(id)) {
157 } else if (citymap[tile_index(ptile1)] > 0) {
159 }
161}
162
163/**********************************************************************/
167void citymap_reserve_tile(struct tile *ptile, int id)
168{
169#ifdef FREECIV_DEBUG
171#endif
172
173 citymap[tile_index(ptile)] = -id;
174}
175
176/**********************************************************************/
181int citymap_read(struct tile *ptile)
182{
183 return citymap[tile_index(ptile)];
184}
185
186/**********************************************************************/
190bool citymap_is_reserved(struct tile *ptile)
191{
192 if (NULL != tile_worked(ptile) /*|| tile_city(ptile)*/) {
193 return TRUE;
194 }
195 return (citymap[tile_index(ptile)] < 0);
196}
int city_map_radius_sq_get(const struct city *pcity)
Definition city.c:137
#define city_list_iterate(citylist, pcity)
Definition city.h:508
#define city_tile(_pcity_)
Definition city.h:564
#define city_list_iterate_end
Definition city.h:510
#define city_tile_iterate(_nmap, _radius_sq, _city_tile, _tile)
Definition city.h:230
#define CITY_MAP_DEFAULT_RADIUS_SQ
Definition city.h:82
#define city_tile_iterate_end
Definition city.h:238
void citymap_free_city_spot(struct tile *ptile, int id)
Definition citymap.c:150
void citymap_reserve_tile(struct tile *ptile, int id)
Definition citymap.c:167
void citymap_reserve_city_spot(struct tile *ptile, int id)
Definition citymap.c:122
bool citymap_is_reserved(struct tile *ptile)
Definition citymap.c:190
int citymap_read(struct tile *ptile)
Definition citymap.c:181
void citymap_free(void)
Definition citymap.c:110
#define log_citymap
Definition citymap.c:55
void citymap_turn_init(struct player *pplayer)
Definition citymap.c:61
static int * citymap
Definition citymap.c:53
char * incite_cost
Definition comments.c:75
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
int int id
Definition editgui_g.h:28
@ AUT_BUILD_CITY
Definition fc_types.h:370
struct world wld
Definition game.c:63
#define fc_assert_ret(condition)
Definition log.h:191
#define MAP_INDEX_SIZE
Definition map.h:137
#define FC_FREE(ptr)
Definition mem.h:41
#define fc_realloc(ptr, sz)
Definition mem.h:36
#define players_iterate_end
Definition player.h:537
#define players_iterate(_pplayer)
Definition player.h:532
#define MAX(x, y)
Definition shared.h:54
Definition city.h:320
struct unit_list * units
Definition player.h:280
Definition tile.h:50
enum adv_unit_task task
Definition unit.h:89
int id
Definition unit.h:145
struct unit::@81::@84 server
struct unit_adv * adv
Definition unit.h:236
struct tile * goto_tile
Definition unit.h:155
struct civ_map map
#define TRUE
Definition support.h:46
#define tile_index(_pt_)
Definition tile.h:88
#define tile_worked(_tile)
Definition tile.h:114
#define TILE_XY(ptile)
Definition tile.h:43
bool unit_is_cityfounder(const struct unit *punit)
Definition unit.c:2641
#define unit_list_iterate(unitlist, punit)
Definition unitlist.h:31
#define unit_list_iterate_end
Definition unitlist.h:33