Change: Let AI developers edit non-editable AI/Game Script Parameters (#8895)
[openttd-github.git] / src / strings_func.h
blobbdb4fc6cd6d73dd985833e417d9dac3039319625
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
8 /** @file strings_func.h Functions related to OTTD's strings. */
10 #ifndef STRINGS_FUNC_H
11 #define STRINGS_FUNC_H
13 #include "strings_type.h"
14 #include "string_type.h"
15 #include "gfx_type.h"
16 #include "core/bitmath_func.hpp"
18 /**
19 * Extract the StringTab from a StringID.
20 * @param str String identifier
21 * @return StringTab from \a str
23 static inline StringTab GetStringTab(StringID str)
25 StringTab result = (StringTab)(str >> TAB_SIZE_BITS);
26 if (result >= TEXT_TAB_NEWGRF_START) return TEXT_TAB_NEWGRF_START;
27 if (result >= TEXT_TAB_GAMESCRIPT_START) return TEXT_TAB_GAMESCRIPT_START;
28 return result;
31 /**
32 * Extract the StringIndex from a StringID.
33 * @param str String identifier
34 * @return StringIndex from \a str
36 static inline uint GetStringIndex(StringID str)
38 return str - (GetStringTab(str) << TAB_SIZE_BITS);
41 /**
42 * Create a StringID
43 * @param tab StringTab
44 * @param index StringIndex
45 * @return StringID composed from \a tab and \a index
47 static inline StringID MakeStringID(StringTab tab, uint index)
49 if (tab == TEXT_TAB_NEWGRF_START) {
50 assert(index < TAB_SIZE_NEWGRF);
51 } else if (tab == TEXT_TAB_GAMESCRIPT_START) {
52 assert(index < TAB_SIZE_GAMESCRIPT);
53 } else {
54 assert(tab < TEXT_TAB_END);
55 assert(index < TAB_SIZE);
57 return (tab << TAB_SIZE_BITS) + index;
60 class StringParameters {
61 StringParameters *parent; ///< If not nullptr, this instance references data from this parent instance.
62 uint64 *data; ///< Array with the actual data.
63 WChar *type; ///< Array with type information about the data. Can be nullptr when no type information is needed. See #StringControlCode.
65 public:
66 uint offset; ///< Current offset in the data/type arrays.
67 uint num_param; ///< Length of the data array.
69 /** Create a new StringParameters instance. */
70 StringParameters(uint64 *data, uint num_param, WChar *type) :
71 parent(nullptr),
72 data(data),
73 type(type),
74 offset(0),
75 num_param(num_param)
76 { }
78 /** Create a new StringParameters instance. */
79 template <size_t Tnum_param>
80 StringParameters(int64 (&data)[Tnum_param]) :
81 parent(nullptr),
82 data((uint64 *)data),
83 type(nullptr),
84 offset(0),
85 num_param(Tnum_param)
87 static_assert(sizeof(data[0]) == sizeof(uint64));
90 /**
91 * Create a new StringParameters instance that can reference part of the data of
92 * the given partent instance.
94 StringParameters(StringParameters &parent, uint size) :
95 parent(&parent),
96 data(parent.data + parent.offset),
97 offset(0),
98 num_param(size)
100 assert(size <= parent.GetDataLeft());
101 if (parent.type == nullptr) {
102 this->type = nullptr;
103 } else {
104 this->type = parent.type + parent.offset;
108 ~StringParameters()
110 if (this->parent != nullptr) {
111 this->parent->offset += this->num_param;
115 void ClearTypeInformation();
117 int64 GetInt64(WChar type = 0);
119 /** Read an int32 from the argument array. @see GetInt64. */
120 int32 GetInt32(WChar type = 0)
122 return (int32)this->GetInt64(type);
125 /** Get a pointer to the current element in the data array. */
126 uint64 *GetDataPointer() const
128 return &this->data[this->offset];
131 /** Return the amount of elements which can still be read. */
132 uint GetDataLeft() const
134 return this->num_param - this->offset;
137 /** Get a pointer to a specific element in the data array. */
138 uint64 *GetPointerToOffset(uint offset) const
140 assert(offset < this->num_param);
141 return &this->data[offset];
144 /** Does this instance store information about the type of the parameters. */
145 bool HasTypeInformation() const
147 return this->type != nullptr;
150 /** Get the type of a specific element. */
151 WChar GetTypeAtOffset(uint offset) const
153 assert(offset < this->num_param);
154 assert(this->HasTypeInformation());
155 return this->type[offset];
158 void SetParam(uint n, uint64 v)
160 assert(n < this->num_param);
161 this->data[n] = v;
164 uint64 GetParam(uint n) const
166 assert(n < this->num_param);
167 return this->data[n];
170 extern StringParameters _global_string_params;
172 char *GetString(char *buffr, StringID string, const char *last);
173 std::string GetString(StringID string);
174 char *GetStringWithArgs(char *buffr, StringID string, StringParameters *args, const char *last, uint case_index = 0, bool game_script = false);
175 const char *GetStringPtr(StringID string);
177 uint ConvertKmhishSpeedToDisplaySpeed(uint speed);
178 uint ConvertDisplaySpeedToKmhishSpeed(uint speed);
181 * Set a string parameter \a v at index \a n in a given array \a s.
182 * @param s Array of string parameters.
183 * @param n Index of the string parameter.
184 * @param v Value of the string parameter.
186 static inline void SetDParamX(uint64 *s, uint n, uint64 v)
188 s[n] = v;
192 * Set a string parameter \a v at index \a n in the global string parameter array.
193 * @param n Index of the string parameter.
194 * @param v Value of the string parameter.
196 static inline void SetDParam(uint n, uint64 v)
198 _global_string_params.SetParam(n, v);
201 void SetDParamMaxValue(uint n, uint64 max_value, uint min_count = 0, FontSize size = FS_NORMAL);
202 void SetDParamMaxDigits(uint n, uint count, FontSize size = FS_NORMAL);
204 void SetDParamStr(uint n, const char *str);
205 void SetDParamStr(uint n, const std::string &str);
206 void SetDParamStr(uint n, std::string &&str) = delete; // block passing temporaries to SetDParamStr
208 void CopyInDParam(int offs, const uint64 *src, int num);
209 void CopyOutDParam(uint64 *dst, int offs, int num);
210 void CopyOutDParam(uint64 *dst, const char **strings, StringID string, int num);
213 * Get the current string parameter at index \a n from parameter array \a s.
214 * @param s Array of string parameters.
215 * @param n Index of the string parameter.
216 * @return Value of the requested string parameter.
218 static inline uint64 GetDParamX(const uint64 *s, uint n)
220 return s[n];
224 * Get the current string parameter at index \a n from the global string parameter array.
225 * @param n Index of the string parameter.
226 * @return Value of the requested string parameter.
228 static inline uint64 GetDParam(uint n)
230 return _global_string_params.GetParam(n);
233 extern TextDirection _current_text_dir; ///< Text direction of the currently selected language
235 void InitializeLanguagePacks();
236 const char *GetCurrentLanguageIsoCode();
238 bool StringIDSorter(const StringID &a, const StringID &b);
241 * A searcher for missing glyphs.
243 class MissingGlyphSearcher {
244 public:
245 /** Make sure everything gets destructed right. */
246 virtual ~MissingGlyphSearcher() {}
249 * Get the next string to search through.
250 * @return The next string or nullptr if there is none.
252 virtual const char *NextString() = 0;
255 * Get the default (font) size of the string.
256 * @return The font size.
258 virtual FontSize DefaultSize() = 0;
261 * Reset the search, i.e. begin from the beginning again.
263 virtual void Reset() = 0;
266 * Whether to search for a monospace font or not.
267 * @return True if searching for monospace.
269 virtual bool Monospace() = 0;
272 * Set the right font names.
273 * @param settings The settings to modify.
274 * @param font_name The new font name.
275 * @param os_data Opaque pointer to OS-specific data.
277 virtual void SetFontNames(struct FontCacheSettings *settings, const char *font_name, const void *os_data = nullptr) = 0;
279 bool FindMissingGlyphs();
282 void CheckForMissingGlyphs(bool base_font = true, MissingGlyphSearcher *search = nullptr);
284 #endif /* STRINGS_FUNC_H */