Freeciv-3.2: /fast/freeciv/nightly/S3_2/server/generator/temperature_map.c Source File

Freeciv-3.2

Loading...
Searching...
No Matches
temperature_map.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 2004 - Marcelo J. Burda
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/* utilities */
19#include "log.h"
20
21/* common */
22#include "map.h"
23
24/* generator */
25#include "height_map.h"
26#include "mapgen_topology.h"
27#include "mapgen_utils.h"
28
29#include "temperature_map.h"
30
31 static int *temperature_map;
32
33#define tmap(_tile) (temperature_map[tile_index(_tile)])
34
35/**********************************************************************/
38#ifdef FREECIV_DEBUG
39static char *tmap_y2str(int ycoor)
40{
41 static char buf[MAP_MAX_LINEAR_SIZE + 1];
42 char *p = buf;
43 int i, idx;
44
45 for (i = 0; i < wld.map.xsize; i++) {
46 idx = ycoor * wld.map.xsize + i;
47
48 if (idx > wld.map.xsize * wld.map.ysize) {
49 break;
50 }
51
52 switch (temperature_map[idx]) {
53 case TT_TROPICAL:
54 *p++ = 't'; /* tropical */
55 break;
56 case TT_TEMPERATE:
57 *p++ = 'm'; /* medium */
58 break;
59 case TT_COLD:
60 *p++ = 'c'; /* cold */
61 break;
62 case TT_FROZEN:
63 *p++ = 'f'; /* frozen */
64 break;
65 }
66 }
67
68 *p = '\0';
69
70 return buf;
71}
72#endif /* FREECIV_DEBUG */
73
74/**********************************************************************/
77 bool temperature_is_initialized(void)
78{
79 return temperature_map != NULL;
80}
81
82/**********************************************************************/
85 bool tmap_is(const struct tile *ptile, temperature_type tt)
86{
87 return BOOL_VAL(tmap(ptile) & (tt));
88}
89
90/**********************************************************************/
93 bool is_temperature_type_near(const struct tile *ptile, temperature_type tt)
94{
95 adjc_iterate(&(wld.map), ptile, tile1) {
96 if (BOOL_VAL(tmap(tile1) & (tt))) {
97 return TRUE;
98 };
99 } adjc_iterate_end;
100
101 return FALSE;
102}
103
104/**********************************************************************/
107 void destroy_tmap(void)
108{
109 fc_assert_ret(NULL != temperature_map);
110 free(temperature_map);
111 temperature_map = NULL;
112}
113
114/**********************************************************************/
119 void create_tmap(bool real)
120{
121 int i;
122
123 /* if map is defined this is not changed */
124 /* TODO: load if from scenario game with tmap */
125 /* to debug, never load at this time */
126 fc_assert_ret(NULL == temperature_map);
127
128 temperature_map = fc_malloc(sizeof(*temperature_map) * MAP_INDEX_SIZE);
129 whole_map_iterate(&(wld.map), ptile) {
130 /* the base temperature is equal to base map_colatitude */
131 int t = map_colatitude(ptile);
132
133 if (!real) {
134 tmap(ptile) = t;
135 } else {
136 /* high land can be 30% cooler */
137 float height = - 0.3 * MAX(0, hmap(ptile) - hmap_shore_level)
138 / (hmap_max_level - hmap_shore_level);
139 int tcn = count_terrain_class_near_tile(&(wld.map), ptile, FALSE, TRUE, TC_OCEAN);
140 /* near ocean temperature can be 15% more "temperate" */
141 float temperate = (0.15 * (wld.map.server.temperature / 100 - t
142 / MAX_COLATITUDE)
143 * 2 * MIN(50, tcn)
144 / 100);
145
146 tmap(ptile) = t * (1.0 + temperate) * (1.0 + height);
147 }
148 } whole_map_iterate_end;
149
150 /* adjust to get evenly distributed frequencies
151 * Only call adjust when the colatitude range is large enough for this to
152 * make sense - if most variation comes from height and coast, don't try
153 * to squish that back into its original narrow range */
154 if (REAL_COLATITUDE_RANGE(wld.map) >= MAX_COLATITUDE * 2 / 5) {
155 adjust_int_map(temperature_map, MIN_REAL_COLATITUDE(wld.map),
156 MAX_REAL_COLATITUDE(wld.map));
157 }
158
159 /* now simplify to 4 base values */
160 for (i = 0; i < MAP_INDEX_SIZE; i++) {
161 int t = temperature_map[i];
162
163 if (t >= TROPICAL_LEVEL) {
164 temperature_map[i] = TT_TROPICAL;
165 } else if (t >= COLD_LEVEL) {
166 temperature_map[i] = TT_TEMPERATE;
167 } else if (t >= 2 * ICE_BASE_LEVEL) {
168 temperature_map[i] = TT_COLD;
169 } else {
170 temperature_map[i] = TT_FROZEN;
171 }
172 }
173
174 log_debug("%stemperature map ({f}rozen, {c}old, {m}edium, {t}ropical):",
175 real ? "real " : "");
176 for (i = 0; i < wld.map.ysize; i++) {
177 log_debug("%5d: %s", i, tmap_y2str(i));
178 }
179}
height
struct canvas int int struct sprite int int int int height
Definition canvas_g.h:44
incite_cost
char * incite_cost
Definition comments.c:74
hmap_shore_level
int hmap_shore_level
Definition height_map.c:30
wld
struct world wld
Definition game.c:63
height_map.h
hmap_max_level
#define hmap_max_level
Definition height_map.h:33
hmap
#define hmap(_tile)
Definition height_map.h:17
log.h
fc_assert_ret
#define fc_assert_ret(condition)
Definition log.h:191
log_debug
#define log_debug(message,...)
Definition log.h:115
map.h
MAP_MAX_LINEAR_SIZE
#define MAP_MAX_LINEAR_SIZE
Definition map.h:640
adjc_iterate_end
#define adjc_iterate_end
Definition map.h:433
MAP_INDEX_SIZE
#define MAP_INDEX_SIZE
Definition map.h:137
adjc_iterate
#define adjc_iterate(nmap, center_tile, itr_tile)
Definition map.h:428
whole_map_iterate
#define whole_map_iterate(_map, _tile)
Definition map.h:545
whole_map_iterate_end
#define whole_map_iterate_end
Definition map.h:554
map_colatitude
int map_colatitude(const struct tile *ptile)
Definition mapgen_topology.c:38
mapgen_topology.h
REAL_COLATITUDE_RANGE
#define REAL_COLATITUDE_RANGE(_nmap)
Definition mapgen_topology.h:35
MIN_REAL_COLATITUDE
#define MIN_REAL_COLATITUDE(_nmap)
Definition mapgen_topology.h:33
COLD_LEVEL
#define COLD_LEVEL
Definition mapgen_topology.h:50
MAX_REAL_COLATITUDE
#define MAX_REAL_COLATITUDE(_nmap)
Definition mapgen_topology.h:31
ICE_BASE_LEVEL
#define ICE_BASE_LEVEL
Definition mapgen_topology.h:63
TROPICAL_LEVEL
#define TROPICAL_LEVEL
Definition mapgen_topology.h:52
MAX_COLATITUDE
#define MAX_COLATITUDE
Definition mapgen_topology.h:21
mapgen_utils.h
adjust_int_map
#define adjust_int_map(int_map, int_map_min, int_map_max)
Definition mapgen_utils.h:105
fc_malloc
#define fc_malloc(sz)
Definition mem.h:34
MIN
#define MIN(x, y)
Definition shared.h:55
BOOL_VAL
#define BOOL_VAL(x)
Definition shared.h:70
MAX
#define MAX(x, y)
Definition shared.h:54
civ_map::xsize
int xsize
Definition map_types.h:78
civ_map::ysize
int ysize
Definition map_types.h:78
civ_map::server
struct civ_map::@42::@44 server
civ_map::temperature
int temperature
Definition map_types.h:103
tile
Definition tile.h:50
world::map
struct civ_map map
Definition world_object.h:38
TRUE
#define TRUE
Definition support.h:46
FALSE
#define FALSE
Definition support.h:47
temperature_is_initialized
bool temperature_is_initialized(void)
Definition temperature_map.c:77
destroy_tmap
void destroy_tmap(void)
Definition temperature_map.c:107
tmap
#define tmap(_tile)
Definition temperature_map.c:33
tmap_is
bool tmap_is(const struct tile *ptile, temperature_type tt)
Definition temperature_map.c:85
temperature_map
static int * temperature_map
Definition temperature_map.c:31
is_temperature_type_near
bool is_temperature_type_near(const struct tile *ptile, temperature_type tt)
Definition temperature_map.c:93
create_tmap
void create_tmap(bool real)
Definition temperature_map.c:119
temperature_map.h
temperature_type
int temperature_type
Definition temperature_map.h:24
TT_COLD
#define TT_COLD
Definition temperature_map.h:27
TT_FROZEN
#define TT_FROZEN
Definition temperature_map.h:26
TT_TROPICAL
#define TT_TROPICAL
Definition temperature_map.h:29
TT_TEMPERATE
#define TT_TEMPERATE
Definition temperature_map.h:28
count_terrain_class_near_tile
int count_terrain_class_near_tile(const struct civ_map *nmap, const struct tile *ptile, bool cardinal_only, bool percentage, enum terrain_class tclass)
Definition terrain.c:633