Freeciv-3.3
Loading...
Searching...
No Matches
overview_common.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996-2005 - Freeciv Development Team
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 <math.h> /* floor */
19
20/* utility */
21#include "log.h"
22
23/* client */
24#include "client_main.h" /* can_client_change_view() */
25#include "climap.h"
26#include "control.h"
27#include "mapview_g.h"
28#include "options.h"
29#include "zoom.h"
30
31#include "overview_common.h"
32
34#if 0
35struct overview overview = {
36 /* These are the default values. All others are zeroed automatically. */
37 .fog = TRUE,
38 .layers = {[OLAYER_BACKGROUND] = TRUE,
42};
43#endif
44
45/*
46 * Set to TRUE if the backing store is more recent than the version
47 * drawn into overview.window.
48 */
49static bool overview_dirty = FALSE;
50
51/************************************************************************/
56static void gui_to_natural_pos(const struct tileset *t,
57 double *ntl_x, double *ntl_y,
58 int gui_x, int gui_y)
59{
60 const double gui_xd = gui_x, gui_yd = gui_y;
61 const double W = tileset_tile_width(t) * map_zoom;
62 const double H = tileset_tile_height(t) * map_zoom;
63 double map_x, map_y;
64
65 /* First convert to map positions. This ignores hex conversions; we're
66 * not looking for an exact tile. */
67 if (tileset_is_isometric(t)) {
68 /* Includes hex cases. */
69 map_x = (gui_xd * H + gui_yd * W) / (W * H);
70 map_y = (gui_yd * W - gui_xd * H) / (W * H);
71 } else {
72 map_x = gui_xd / W;
73 map_y = gui_yd / H;
74 }
75
76 /* Now convert to natural positions. Note this assumes the macro form
77 * of the conversion will work with floating-point values. */
79}
80
81/************************************************************************/
84static void gui_to_overview_pos(const struct tileset *t,
85 int *ovr_x, int *ovr_y,
86 int gui_x, int gui_y)
87{
88 double ntl_x, ntl_y;
89
91
92 /* Now convert straight to overview coordinates. */
95
96 /* Now do additional adjustments. See map_to_overview_pos(). */
99 } else {
100 if (MAP_IS_ISOMETRIC) {
101 /* HACK: For iso-maps that don't wrap in the X direction we clip
102 * a half-tile off of the left and right of the overview.
103 * This means some tiles only are halfway shown. However it means we
104 * don't show any unreal tiles, which we'd otherwise be doing.
105 * The rest of the code can't handle unreal tiles in the overview. */
107 }
108 }
109
112 }
113}
114
115/************************************************************************/
118static struct color *overview_tile_color(struct tile *ptile)
119{
121 struct city *pcity = tile_city(ptile);
122
123 if (pcity) {
124 if (NULL == client.conn.playing
128 /* Includes teams. */
130 } else {
132 }
133 }
134 }
136 struct unit *punit = find_visible_unit(ptile);
137
138 if (punit) {
139 if (NULL == client.conn.playing
143 /* Includes teams. */
145 } else {
147 }
148 }
149 }
151 struct player *owner = tile_owner(ptile);
152
153 if (owner) {
156 } else if (!is_ocean_tile(ptile)) {
158 }
159 }
160 }
162 && tile_terrain(ptile) != T_UNKNOWN) {
163 return get_terrain_color(tileset, tile_terrain(ptile));
164 }
166 && tile_terrain(ptile) != T_UNKNOWN) {
169 } else {
170 if (is_ocean_tile(ptile)) {
172 } else {
174 }
175 }
176 }
177
179}
180
181/************************************************************************/
186{
187 struct canvas *dest = get_overview_window();
188
189 if (!dest) {
190 return;
191 }
193 0, 0, 0, 0,
195}
196
197/************************************************************************/
201static void redraw_overview(void)
202{
203 int i, x[4], y[4];
204
205 if (!gui_options.overview.map) {
206 return;
207 }
208
209 {
210 struct canvas *src = gui_options.overview.map;
211 struct canvas *dst = gui_options.overview.window;
216
217 canvas_copy(dst, src, 0, 0, ix, iy, x_left, y_top);
218 canvas_copy(dst, src, 0, y_top, ix, 0, x_left, iy);
219 canvas_copy(dst, src, x_left, 0, 0, iy, ix, y_top);
220 canvas_copy(dst, src, x_left, y_top, 0, 0, ix, iy);
221 }
222
223 gui_to_overview_pos(tileset, &x[0], &y[0],
225 gui_to_overview_pos(tileset, &x[1], &y[1],
227 gui_to_overview_pos(tileset, &x[2], &y[2],
230 gui_to_overview_pos(tileset, &x[3], &y[3],
232
233 for (i = 0; i < 4; i++) {
234 int src_x = x[i];
235 int src_y = y[i];
236 int dst_x = x[(i + 1) % 4];
237 int dst_y = y[(i + 1) % 4];
238
243 }
244
246
248}
249
250/************************************************************************/
253static void dirty_overview(void)
254{
256}
257
258/************************************************************************/
262{
263 /* Currently this function is called from mapview_common. However it
264 * should be made static eventually. */
265 if (overview_dirty) {
267 }
268}
269
270/************************************************************************/
273static double wrap_double(double value, double wrap)
274{
275 while (value < 0) {
276 value += wrap;
277 }
278 while (value >= wrap) {
279 value -= wrap;
280 }
281 return value;
282}
283
284/************************************************************************/
288{
289 double ntl_x, ntl_y;
290 int ox, oy;
291
295
296 /* NOTE: this embeds the map wrapping in the overview code. This is
297 * basically necessary for the overview to be efficiently
298 * updated. */
301 = wrap_double(ntl_x - (double)MAP_NATURAL_WIDTH / 2.0,
303 } else {
305 }
308 = wrap_double(ntl_y - (double)MAP_NATURAL_HEIGHT / 2.0,
310 } else {
312 }
313
315
319}
320
321/************************************************************************/
325 int map_x, int map_y)
326{
327 /* The map position may not be normal, for instance when the mapview
328 * origin is not a normal position.
329 *
330 * NOTE: this embeds the map wrapping in the overview code. */
334
337 } else {
338 if (MAP_IS_ISOMETRIC) {
339 /* HACK: For iso-maps that don't wrap in the X direction we clip
340 * a half-tile off of the left and right of the overview.
341 * This means some tiles only are halfway shown. However it means we
342 * don't show any unreal tiles, which we'd otherwise be doing.
343 * The rest of the code can't handle unreal tiles in the overview. */
344 ovr_x--;
345 }
346 }
349 }
353}
354
355/************************************************************************/
359 int overview_x, int overview_y)
360{
363
365 /* Clip half tile left and right. See comment in map_to_overview_pos(). */
366 ntl_x++;
367 }
368
370 /* All positions on the overview should be valid. */
372}
373
374/************************************************************************/
378{
379 if (!can_client_change_view()) {
380 return;
381 }
382 whole_map_iterate(&(wld.map), ptile) {
386}
387
388/************************************************************************/
395 struct tile *ptile,
396 int x, int y, int w, int h)
397{
399 overview_tile_color(ptile),
400 x, y, w, h);
404 0, 0, w, h);
405 }
406}
407
408/************************************************************************/
411void overview_update_tile(struct tile *ptile)
412{
413 int tile_x, tile_y;
414
415 /* Base overview positions are just like natural positions, but scaled to
416 * the overview tile dimensions. */
421
422 if (MAP_IS_ISOMETRIC) {
425 /* This tile is shown half on the left and half on the right
426 * side of the overview. So we have to draw it in two parts. */
431 }
432 } else {
433 /* Clip half tile left and right.
434 * See comment in map_to_overview_pos(). */
436 }
437 }
438
442
445}
446
447/************************************************************************/
451{
452 int w, h;
453 int xfact = MAP_IS_ISOMETRIC ? 2 : 1;
454
455 static int recursion = 0; /* Just to be safe. */
456
457 /* Clip half tile left and right. See comment in map_to_overview_pos(). */
459
460 if (recursion > 0 || MAP_NATIVE_WIDTH <= 0 || MAP_NATIVE_HEIGHT <= 0) {
461 return;
462 }
463 recursion++;
464
467
468 /* Set the scale of the overview map. This attempts to limit the
469 * overview to the size of the area available.
470 *
471 * It rounds up since this gives good results with the default settings.
472 * It may need tweaking if the panel resizes itself. */
473 OVERVIEW_TILE_SIZE = MIN((w - 1) / (MAP_NATIVE_WIDTH * xfact) + 1,
474 (h - 1) / MAP_NATIVE_HEIGHT + 1);
476
477 log_debug("Map size %d,%d - area size %d,%d - scale: %d", MAP_NATIVE_WIDTH,
479
483
487 }
494 0, 0,
497
498 /* Call gui specific function. */
500
503 }
504
505 recursion--;
506}
507
508/************************************************************************/
520
521/************************************************************************/
525{
526 /* This is called once for each option changed so it is slower than
527 * necessary. If this becomes a problem it could be switched to use a
528 * queue system like the mapview drawing code does. */
530}
struct canvas int int int int struct sprite *sprite canvas_put_rectangle
Definition canvas_g.h:55
struct canvas int int int int struct sprite *sprite struct canvas struct color int int int int height canvas_put_line
Definition canvas_g.h:62
canvas_put_sprite
Definition canvas_g.h:42
struct canvas * pcanvas
Definition canvas_g.h:42
@ LINE_NORMAL
Definition canvas_g.h:26
#define city_owner(_pcity_)
Definition city.h:560
struct civclient client
bool can_client_change_view(void)
enum known_type client_tile_get_known(const struct tile *ptile)
Definition climap.c:36
struct color * get_player_color(const struct tileset *t, const struct player *pplayer)
struct color * get_color(const struct tileset *t, enum color_std stdcolor)
struct color * get_terrain_color(const struct tileset *t, const struct terrain *pterrain)
char * incite_cost
Definition comments.c:76
struct unit * find_visible_unit(struct tile *ptile)
Definition control.c:822
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
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 int const struct action *paction struct unit struct city * pcity
Definition dialogs_g.h:78
struct world wld
Definition game.c:62
void canvas_free(struct canvas *store)
Definition canvas.c:44
void canvas_copy(struct canvas *dest, struct canvas *src, int src_x, int src_y, int dest_x, int dest_y, int width, int height)
Definition canvas.c:76
struct canvas * canvas_create(int width, int height)
Definition canvas.c:28
struct city * owner
Definition citydlg.c:226
void update_map_canvas_scrollbars_size(void)
Definition mapview.c:696
void get_overview_area_dimensions(int *width, int *height)
Definition mapview.c:317
void update_overview_scroll_window_pos(int x, int y)
Definition mapview.c:657
void overview_size_changed(void)
Definition mapview.c:326
struct canvas * get_overview_window(void)
Definition mapview.c:337
#define fc_assert(condition)
Definition log.h:177
#define log_debug(message,...)
Definition log.h:116
bool normalize_map_pos(const struct civ_map *nmap, int *x, int *y)
Definition map.c:1117
#define NATURAL_TO_MAP_POS(pmap_x, pmap_y, nat_x, nat_y)
Definition map.h:182
#define do_in_natural_pos(ntl_x, ntl_y, map_x, map_y)
Definition map.h:214
#define MAP_TO_NATURAL_POS(pnat_x, pnat_y, map_x, map_y)
Definition map.h:188
#define whole_map_iterate(_map, _tile)
Definition map.h:573
#define current_wrap_has_flag(flag)
Definition map.h:46
#define do_in_natural_pos_end
Definition map.h:221
#define whole_map_iterate_end
Definition map.h:582
#define index_to_map_pos(pmap_x, pmap_y, mindex)
Definition map.h:229
struct view mapview
#define H(x, y, z)
Definition md5.c:92
struct client_options gui_options
Definition options.c:71
@ OLAYER_BORDERS
Definition options.h:83
@ OLAYER_BACKGROUND
Definition options.h:81
@ OLAYER_CITIES
Definition options.h:86
@ OLAYER_BORDERS_ON_OCEAN
Definition options.h:84
@ OLAYER_UNITS
Definition options.h:85
@ OLAYER_RELIEF
Definition options.h:82
static double wrap_double(double value, double wrap)
static void gui_to_overview_pos(const struct tileset *t, int *ovr_x, int *ovr_y, int gui_x, int gui_y)
void flush_dirty_overview(void)
void overview_to_map_pos(int *map_x, int *map_y, int overview_x, int overview_y)
static void gui_to_natural_pos(const struct tileset *t, double *ntl_x, double *ntl_y, int gui_x, int gui_y)
void overview_redraw_callback(struct option *option)
void refresh_overview_canvas(void)
static bool overview_dirty
void center_tile_overviewcanvas(void)
void refresh_overview_from_canvas(void)
static void redraw_overview(void)
static struct color * overview_tile_color(struct tile *ptile)
void overview_update_tile(struct tile *ptile)
int OVERVIEW_TILE_SIZE
static void put_overview_tile_area(struct canvas *pcanvas, struct tile *ptile, int x, int y, int w, int h)
void calculate_overview_dimensions(void)
void overview_free(void)
static void dirty_overview(void)
void map_to_overview_pos(int *overview_x, int *overview_y, int map_x, int map_y)
#define OVERVIEW_TILE_WIDTH
#define OVERVIEW_TILE_HEIGHT
bool pplayers_allied(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1409
#define FC_WRAP(value, range)
Definition shared.h:65
#define MIN(x, y)
Definition shared.h:55
#define MAX(x, y)
Definition shared.h:54
struct sprite int int y
Definition sprite_g.h:31
struct sprite int x
Definition sprite_g.h:31
static int recursion[AIT_LAST]
Definition srv_log.c:45
Definition city.h:317
struct connection conn
Definition client_main.h:96
struct overview overview
Definition options.h:462
Definition colors.h:21
struct player * playing
Definition connection.h:151
double map_y0
Definition options.h:93
double map_x0
Definition options.h:93
bool fog
Definition options.h:102
bool layers[OLAYER_COUNT]
Definition options.h:103
struct canvas * map
Definition options.h:97
int width
Definition options.h:94
struct canvas * window
Definition options.h:100
int height
Definition options.h:94
Definition tile.h:50
Definition unit.h:140
int height
float gui_x0
float gui_y0
int width
struct civ_map map
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
#define T_UNKNOWN
Definition terrain.h:62
#define is_ocean_tile(ptile)
Definition terrain.h:196
#define terrain_has_flag(terr, flag)
Definition terrain.h:176
struct city * tile_city(const struct tile *ptile)
Definition tile.c:83
#define tile_index(_pt_)
Definition tile.h:89
@ TILE_KNOWN_UNSEEN
Definition tile.h:37
#define tile_terrain(_tile)
Definition tile.h:111
#define tile_owner(_tile)
Definition tile.h:97
struct sprite * get_basic_fog_sprite(const struct tileset *t)
Definition tilespec.c:7277
bool tileset_is_isometric(const struct tileset *t)
Definition tilespec.c:738
int tileset_tile_height(const struct tileset *t)
Definition tilespec.c:791
int tileset_tile_width(const struct tileset *t)
Definition tilespec.c:779
#define unit_owner(_pu)
Definition unit.h:403
#define MAP_NATIVE_WIDTH
#define MAP_IS_ISOMETRIC
#define MAP_NATURAL_HEIGHT
#define MAP_NATURAL_WIDTH
#define MAP_NATIVE_HEIGHT
float map_zoom
Definition zoom.c:25