Freeciv21
Develop your civilization from humble roots to a global empire
climap.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 1996-2023 Freeciv21 and Freeciv contributors. This file is
3  part of Freeciv21. Freeciv21 is free software: you can redistribute it
4  and/or modify it under the terms of the GNU General Public License as
5  published by the Free Software Foundation, either version 3 of the
6  License, or (at your option) any later version. You should have received
7  a copy of the GNU General Public License along with Freeciv21. If not,
8  see https://www.gnu.org/licenses/.
9  */
10 
11 // common
12 #include "map.h"
13 #include "shared.h"
14 
15 // client
16 #include "client_main.h"
17 #include "climap.h"
18 #include "tileset/tilespec.h" // tileset_is_isometric(tileset)
19 
29 enum known_type client_tile_get_known(const struct tile *ptile)
30 {
31  if (nullptr == client.conn.playing) {
32  if (client_is_observer()) {
33  return TILE_KNOWN_SEEN;
34  } else {
35  return TILE_UNKNOWN;
36  }
37  }
38  return tile_get_known(ptile, client.conn.playing);
39 }
40 
50 enum direction8 gui_to_map_dir(enum direction8 gui_dir)
51 {
53  return dir_ccw(gui_dir);
54  } else {
55  return gui_dir;
56  }
57 }
58 
64 struct tile *client_city_tile(const struct city *pcity)
65 {
66  int dx, dy;
67  double x = 0, y = 0;
68  size_t num = 0;
69 
70  if (nullptr == pcity) {
71  return nullptr;
72  }
73 
74  if (nullptr != city_tile(pcity)) {
75  // Normal city case.
76  return city_tile(pcity);
77  }
78 
79  whole_map_iterate(&(wld.map), ptile)
80  {
81  int tile_x, tile_y;
82 
83  index_to_map_pos(&tile_x, &tile_y, tile_index(ptile));
84  if (pcity == tile_worked(ptile)) {
85  if (0 == num) {
86  x = tile_x;
87  y = tile_y;
88  num = 1;
89  } else {
90  num++;
91  base_map_distance_vector(&dx, &dy, static_cast<int>(x),
92  static_cast<int>(y), tile_x, tile_y);
93  x += static_cast<double>(dx) / num;
94  y += static_cast<double>(dy) / num;
95  }
96  }
97  }
99 
100  if (0 < num) {
101  return map_pos_to_tile(&(wld.map), static_cast<int>(x),
102  static_cast<int>(y));
103  } else {
104  return nullptr;
105  }
106 }
107 
114 bool client_city_can_work_tile(const struct city *pcity,
115  const struct tile *ptile)
116 {
117  return base_city_can_work_tile(client_player(), pcity, ptile);
118 }
bool base_city_can_work_tile(const struct player *restriction, const struct city *pcity, const struct tile *ptile)
Returns TRUE when a tile is available to be worked, or the city itself is currently working the tile ...
Definition: city.cpp:1339
struct tile * city_tile(const struct city *pcity)
Return the tile location of the city.
Definition: city.cpp:1095
struct player * client_player()
Either controlling or observing.
struct civclient client
bool client_is_observer()
Returns whether client is observer.
enum known_type client_tile_get_known(const struct tile *ptile)
A tile's "known" field is used by the server to store whether each player knows the tile.
Definition: climap.cpp:29
enum direction8 gui_to_map_dir(enum direction8 gui_dir)
Convert the given GUI direction into a map direction.
Definition: climap.cpp:50
bool client_city_can_work_tile(const struct city *pcity, const struct tile *ptile)
Returns TRUE when a tile is available to be worked, or the city itself is currently working the tile ...
Definition: climap.cpp:114
struct tile * client_city_tile(const struct city *pcity)
Client variant of city_tile().
Definition: climap.cpp:64
struct world wld
Definition: game.cpp:48
struct tile * map_pos_to_tile(const struct civ_map *nmap, int map_x, int map_y)
Return the tile for the given cartesian (map) position.
Definition: map.cpp:391
enum direction8 dir_ccw(enum direction8 dir)
Returns the next direction counter-clock-wise.
Definition: map.cpp:1141
void base_map_distance_vector(int *dx, int *dy, int x0dv, int y0dv, int x1dv, int y1dv)
Finds the difference between the two (unnormalized) positions, in cartesian (map) coordinates.
Definition: map.cpp:961
#define whole_map_iterate(_map, _tile)
Definition: map.h:473
#define whole_map_iterate_end
Definition: map.h:480
#define index_to_map_pos(pmap_x, pmap_y, mindex)
Definition: map.h:164
Definition: city.h:291
struct connection conn
Definition: client_main.h:89
struct player * playing
Definition: connection.h:142
Definition: tile.h:42
struct civ_map map
Definition: world_object.h:21
enum known_type tile_get_known(const struct tile *ptile, const struct player *pplayer)
Return a known_type enumeration value for the tile.
Definition: tile.cpp:398
#define tile_index(_pt_)
Definition: tile.h:70
#define tile_worked(_tile)
Definition: tile.h:97
known_type
Definition: tile.h:28
@ TILE_UNKNOWN
Definition: tile.h:29
@ TILE_KNOWN_SEEN
Definition: tile.h:31
bool tileset_is_isometric(const struct tileset *t)
Return whether the current tileset is isometric.
Definition: tilespec.cpp:336