Freeciv-3.2: /fast/freeciv/nightly/S3_2/common/aicore/citymap.c Source File

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 inidicator). A
42 * 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
53 static int *citymap = NULL;
54
55#define log_citymap log_debug
56
57/**********************************************************************/
61 void 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). */
68 citymap = fc_realloc(citymap, MAP_INDEX_SIZE * sizeof(*citymap));
69 memset(citymap, 0, MAP_INDEX_SIZE * sizeof(*citymap));
70
71 players_iterate(pother) {
72 city_list_iterate(pother->cities, pcity) {
73 struct tile *pcenter = city_tile(pcity);
74
75 /* Reserve at least the default (squared) city radius */
76 city_tile_iterate(nmap, MAX(city_map_radius_sq_get(pcity),
77 CITY_MAP_DEFAULT_RADIUS_SQ),
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 }
86 } city_tile_iterate_end;
87 } city_list_iterate_end;
88 } players_iterate_end;
89
90 unit_list_iterate(pplayer->units, punit) {
91 if (unit_is_cityfounder(punit)
92 && punit->server.adv->task == AUT_BUILD_CITY) {
93
94 /* Use default (squared) city radius */
95 city_tile_iterate(nmap, CITY_MAP_DEFAULT_RADIUS_SQ, punit->goto_tile,
96 ptile) {
97 if (citymap[tile_index(ptile)] >= 0) {
98 citymap[tile_index(ptile)]++;
99 }
100 } city_tile_iterate_end;
101
102 citymap[tile_index(punit->goto_tile)] = -(punit->id);
103 }
104 } unit_list_iterate_end;
105}
106
107/**********************************************************************/
110 void citymap_free(void)
111{
112 if (citymap != NULL) {
113 FC_FREE(citymap);
114 }
115}
116
117/**********************************************************************/
122 void 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. */
135 city_tile_iterate(nmap, CITY_MAP_DEFAULT_RADIUS_SQ, ptile, ptile1) {
136 if (citymap[tile_index(ptile1)] == -id) {
137 citymap[tile_index(ptile1)] = 0;
138 }
139 if (citymap[tile_index(ptile1)] >= 0) {
140 citymap[tile_index(ptile1)]++;
141 }
142 } city_tile_iterate_end;
143
144 citymap[tile_index(ptile)] = -(id);
145}
146
147/**********************************************************************/
150 void citymap_free_city_spot(struct tile *ptile, int id)
151{
152 const struct civ_map *nmap = &(wld.map);
153
154 city_tile_iterate(nmap, CITY_MAP_DEFAULT_RADIUS_SQ, ptile, ptile1) {
155 if (citymap[tile_index(ptile1)] == -(id)) {
156 citymap[tile_index(ptile1)] = 0;
157 } else if (citymap[tile_index(ptile1)] > 0) {
158 citymap[tile_index(ptile1)]--;
159 }
160 } city_tile_iterate_end;
161}
162
163/**********************************************************************/
167 void citymap_reserve_tile(struct tile *ptile, int id)
168{
169#ifdef FREECIV_DEBUG
170 fc_assert_ret(!citymap_is_reserved(ptile));
171#endif
172
173 citymap[tile_index(ptile)] = -id;
174}
175
176/**********************************************************************/
181 int citymap_read(struct tile *ptile)
182{
183 return citymap[tile_index(ptile)];
184}
185
186/**********************************************************************/
190 bool 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}
city_map_radius_sq_get
int city_map_radius_sq_get(const struct city *pcity)
Definition city.c:136
city.h
city_list_iterate
#define city_list_iterate(citylist, pcity)
Definition city.h:508
city_tile
#define city_tile(_pcity_)
Definition city.h:564
city_list_iterate_end
#define city_list_iterate_end
Definition city.h:510
city_tile_iterate
#define city_tile_iterate(_nmap, _radius_sq, _city_tile, _tile)
Definition city.h:230
CITY_MAP_DEFAULT_RADIUS_SQ
#define CITY_MAP_DEFAULT_RADIUS_SQ
Definition city.h:82
city_tile_iterate_end
#define city_tile_iterate_end
Definition city.h:238
citymap_free_city_spot
void citymap_free_city_spot(struct tile *ptile, int id)
Definition citymap.c:150
citymap_reserve_tile
void citymap_reserve_tile(struct tile *ptile, int id)
Definition citymap.c:167
citymap_reserve_city_spot
void citymap_reserve_city_spot(struct tile *ptile, int id)
Definition citymap.c:122
citymap_is_reserved
bool citymap_is_reserved(struct tile *ptile)
Definition citymap.c:190
citymap_read
int citymap_read(struct tile *ptile)
Definition citymap.c:181
citymap_free
void citymap_free(void)
Definition citymap.c:110
log_citymap
#define log_citymap
Definition citymap.c:55
citymap_turn_init
void citymap_turn_init(struct player *pplayer)
Definition citymap.c:61
citymap
static int * citymap
Definition citymap.c:53
citymap.h
incite_cost
char * incite_cost
Definition comments.c:74
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:74
id
int int id
Definition editgui_g.h:28
AUT_BUILD_CITY
@ AUT_BUILD_CITY
Definition fc_types.h:373
wld
struct world wld
Definition game.c:63
game.h
log.h
fc_assert_ret
#define fc_assert_ret(condition)
Definition log.h:191
map.h
MAP_INDEX_SIZE
#define MAP_INDEX_SIZE
Definition map.h:137
mem.h
FC_FREE
#define FC_FREE(ptr)
Definition mem.h:41
fc_realloc
#define fc_realloc(ptr, sz)
Definition mem.h:36
players_iterate_end
#define players_iterate_end
Definition player.h:537
players_iterate
#define players_iterate(_pplayer)
Definition player.h:532
MAX
#define MAX(x, y)
Definition shared.h:54
city
Definition city.h:320
civ_map
Definition map_types.h:71
player
Definition player.h:247
player::units
struct unit_list * units
Definition player.h:280
tile
Definition tile.h:50
unit_adv::task
enum adv_unit_task task
Definition unit.h:89
unit::id
int id
Definition unit.h:145
unit::server
struct unit::@81::@84 server
unit::adv
struct unit_adv * adv
Definition unit.h:236
unit::goto_tile
struct tile * goto_tile
Definition unit.h:155
world::map
struct civ_map map
Definition world_object.h:38
support.h
TRUE
#define TRUE
Definition support.h:46
tile_index
#define tile_index(_pt_)
Definition tile.h:88
tile_worked
#define tile_worked(_tile)
Definition tile.h:114
TILE_XY
#define TILE_XY(ptile)
Definition tile.h:43
unit_is_cityfounder
bool unit_is_cityfounder(const struct unit *punit)
Definition unit.c:2637
unit.h
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
unittype.h