Freeciv21
Develop your civilization from humble roots to a global empire
layer_goto.cpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPLv3-or-later
2 // SPDX-FileCopyrightText: Freeciv authors
3 // SPDX-FileCopyrightText: Freeciv21 authors
4 // SPDX-FileCopyrightText: Louis Moureaux <m_louis30@yahoo.com>
5 
6 #include "layer_goto.h"
7 
8 #include "goto.h"
9 #include "tilespec.h"
10 
11 namespace freeciv {
12 
13 layer_goto::layer_goto(struct tileset *ts) : freeciv::layer(ts, LAYER_GOTO)
14 {
15 }
16 
18 {
19  // Digit sprites. We have a cascade of fallbacks.
20  QStringList patterns = {QStringLiteral("path.turns_%1"),
21  QStringLiteral("city.size_%1")};
23  m_states[GTS_MP_LEFT].turns_tens.data(),
24  m_states[GTS_MP_LEFT].turns_hundreds.data(),
25  patterns);
26  patterns.prepend(QStringLiteral("path.steps_%1"));
28  m_states[GTS_TURN_STEP].turns_tens.data(),
29  m_states[GTS_TURN_STEP].turns_hundreds.data(),
30  patterns);
31  patterns.prepend(QStringLiteral("path.exhausted_mp_%1"));
33  m_states[GTS_EXHAUSTED_MP].turns_tens.data(),
34  m_states[GTS_EXHAUSTED_MP].turns_hundreds.data(),
35  patterns);
36 
37  // Turn steps
38  m_states[GTS_MP_LEFT].specific = load_sprite({"path.normal"});
39  m_states[GTS_EXHAUSTED_MP].specific = load_sprite({"path.exhausted_mp"});
40  m_states[GTS_TURN_STEP].specific = load_sprite({"path.step"});
41 
42  // Waypoint
43  m_waypoint = load_sprite({"path.waypoint"}, true);
44 }
45 
49 std::vector<drawn_sprite>
50 layer_goto::fill_sprite_array(const tile *ptile, const tile_edge *pedge,
51  const tile_corner *pcorner,
52  const unit *punit) const
53 {
54  if (!ptile || !goto_is_active()) {
55  return {};
56  }
57 
58  std::vector<drawn_sprite> sprs;
59 
60  enum goto_tile_state state;
61  int length;
62  bool waypoint;
63  if (!goto_tile_state(ptile, &state, &length, &waypoint)) {
64  return {};
65  }
66 
67  if (length >= 0) {
68  fc_assert_ret_val(state >= 0, {});
69  fc_assert_ret_val(state < m_states.size(), {});
70 
71  if (auto sprite = m_states[state].specific) {
72  sprs.emplace_back(tileset(), sprite, false, 0, 0);
73  }
74 
75  // Units
76  auto sprite = m_states[state].turns[length % NUM_TILES_DIGITS];
77  sprs.emplace_back(tileset(), sprite);
78 
79  // Tens
80  length /= NUM_TILES_DIGITS;
81  if (length > 0
82  && (sprite =
83  m_states[state].turns_tens[length % NUM_TILES_DIGITS])) {
84  sprs.emplace_back(tileset(), sprite);
85  }
86 
87  // Hundreds (optional)
88  length /= NUM_TILES_DIGITS;
89  if (length > 0
90  && (sprite =
91  m_states[state].turns_hundreds[length % NUM_TILES_DIGITS])) {
92  sprs.emplace_back(tileset(), sprite);
93  // Divide for the warning: warn for thousands if we had a hundreds
94  // sprite
95  length /= NUM_TILES_DIGITS;
96  }
97 
98  // Warn if the path is too long (only once by tileset).
99  if (length > 0 && !m_warned) {
101  tileset(), QtWarningMsg,
102  _("Tileset \"%s\" doesn't support long goto paths, such as %d. "
103  "Path not displayed as expected."),
104  tileset_name_get(tileset()), length);
105  m_warned = true;
106  }
107  }
108 
109  if (waypoint) {
110  sprs.emplace_back(tileset(), m_waypoint, false, 0, 0);
111  }
112 
113  return sprs;
114 }
115 
116 } // namespace freeciv
void load_sprites() override
Loads all sprites that do not depend on the ruleset.
Definition: layer_goto.cpp:17
bool m_warned
Did we warn the user?
Definition: layer_goto.h:30
QPixmap * m_waypoint
Definition: layer_goto.h:31
std::vector< drawn_sprite > fill_sprite_array(const tile *ptile, const tile_edge *pedge, const tile_corner *pcorner, const unit *punit) const override
Fill in the given sprite array with any needed goto sprites.
Definition: layer_goto.cpp:50
layer_goto(struct tileset *ts)
Definition: layer_goto.cpp:13
std::array< tile_state_sprites, GTS_COUNT > m_states
Definition: layer_goto.h:38
A layer when drawing the map.
Definition: layer.h:153
QPixmap * load_sprite(const QStringList &possible_names, bool required=false, bool verbose=true) const
Shortcut to load a sprite from the tileset.
Definition: layer.cpp:79
struct tileset * tileset() const
Definition: layer.h:241
#define _(String)
Definition: fcintl.h:50
bool goto_is_active()
Is goto state active?
Definition: goto.cpp:302
goto_tile_state
Definition: goto.h:21
@ GTS_EXHAUSTED_MP
Definition: goto.h:24
@ GTS_MP_LEFT
Definition: goto.h:23
@ GTS_TURN_STEP
Definition: goto.h:22
#define NUM_TILES_DIGITS
Definition: layer.h:78
#define fc_assert_ret_val(condition, val)
Definition: log.h:114
Definition: path.cpp:10
Definition: tile.h:42
Definition: unit.h:134
const char * tileset_name_get(const struct tileset *t)
Return tileset name.
Definition: tilespec.cpp:3828
void assign_digit_sprites(struct tileset *t, QPixmap *units[NUM_TILES_DIGITS], QPixmap *tens[NUM_TILES_DIGITS], QPixmap *hundreds[NUM_TILES_DIGITS], const QStringList &patterns)
Assigns the digits for city or go-to orders, for units, tens, and hundreds (i.e.
Definition: tilespec.cpp:2408
void tileset_error(struct tileset *t, QtMsgType level, const char *format,...)
Called when ever there's problem in ruleset/tileset compatibility.
Definition: tilespec.cpp:291