Remove SIGTYPE_LAST_NOPBS
[openttd/fttd.git] / src / currency.cpp
blob52060e21c853937af8864d86ec6708a22224035a
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /** @file currency.cpp Support for different currencies. */
12 #include "stdafx.h"
13 #include "core/bitmath_func.hpp"
15 #include "currency.h"
16 #include "news_func.h"
17 #include "settings_type.h"
18 #include "date_func.h"
19 #include "string_type.h"
21 #include "table/strings.h"
23 /* exchange rate prefix symbol_pos
24 * | separator | postfix |
25 * | | Euro year | | | name
26 * | | | | | | | */
27 /** The original currency specifications. */
28 static const CurrencySpec origin_currency_specs[CURRENCY_END] = {
29 { 1, "", CF_NOEURO, "\xC2\xA3", "", 0, STR_GAME_OPTIONS_CURRENCY_GBP }, ///< british pound
30 { 2, "", CF_NOEURO, "$", "", 0, STR_GAME_OPTIONS_CURRENCY_USD }, ///< american dollar
31 { 2, "", CF_ISEURO, "\xE2\x82\xAC", "", 0, STR_GAME_OPTIONS_CURRENCY_EUR }, ///< euro
32 { 220, "", CF_NOEURO, "\xC2\xA5", "", 0, STR_GAME_OPTIONS_CURRENCY_JPY }, ///< japanese yen
33 { 27, "", 2002, "", NBSP "S.", 1, STR_GAME_OPTIONS_CURRENCY_ATS }, ///< austrian schilling
34 { 81, "", 2002, "BEF" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_BEF }, ///< belgian franc
35 { 2, "", CF_NOEURO, "CHF" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_CHF }, ///< swiss franc
36 { 41, "", CF_NOEURO, "", NBSP "K\xC4\x8D", 1, STR_GAME_OPTIONS_CURRENCY_CZK }, ///< czech koruna
37 { 4, "", 2002, "DM" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_DEM }, ///< deutsche mark
38 { 11, "", CF_NOEURO, "", NBSP "kr", 1, STR_GAME_OPTIONS_CURRENCY_DKK }, ///< danish krone
39 { 333, "", 2002, "Pts" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_ESP }, ///< spanish peseta
40 { 12, "", 2002, "", NBSP "mk", 1, STR_GAME_OPTIONS_CURRENCY_FIM }, ///< finnish markka
41 { 13, "", 2002, "FF" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_FRF }, ///< french franc
42 { 681, "", 2002, "", "Dr.", 1, STR_GAME_OPTIONS_CURRENCY_GRD }, ///< greek drachma
43 { 378, "", CF_NOEURO, "", NBSP "Ft", 1, STR_GAME_OPTIONS_CURRENCY_HUF }, ///< hungarian forint
44 { 130, "", CF_NOEURO, "", NBSP "Kr", 1, STR_GAME_OPTIONS_CURRENCY_ISK }, ///< icelandic krona
45 { 3873, "", 2002, "", NBSP "L.", 1, STR_GAME_OPTIONS_CURRENCY_ITL }, ///< italian lira
46 { 4, "", 2002, "NLG" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_NLG }, ///< dutch gulden
47 { 12, "", CF_NOEURO, "", NBSP "Kr", 1, STR_GAME_OPTIONS_CURRENCY_NOK }, ///< norwegian krone
48 { 6, "", CF_NOEURO, "", NBSP "z\xC5\x82", 1, STR_GAME_OPTIONS_CURRENCY_PLN }, ///< polish zloty
49 { 5, "", CF_NOEURO, "", NBSP "Lei", 1, STR_GAME_OPTIONS_CURRENCY_RON }, ///< romanian leu
50 { 50, "", CF_NOEURO, "", NBSP "p", 1, STR_GAME_OPTIONS_CURRENCY_RUR }, ///< russian rouble
51 { 479, "", 2007, "", NBSP "SIT", 1, STR_GAME_OPTIONS_CURRENCY_SIT }, ///< slovenian tolar
52 { 13, "", CF_NOEURO, "", NBSP "Kr", 1, STR_GAME_OPTIONS_CURRENCY_SEK }, ///< swedish krona
53 { 3, "", CF_NOEURO, "", NBSP "TL", 1, STR_GAME_OPTIONS_CURRENCY_TRY }, ///< turkish lira
54 { 60, "", 2009, "", NBSP "Sk", 1, STR_GAME_OPTIONS_CURRENCY_SKK }, ///< slovak koruna
55 { 4, "", CF_NOEURO, "R$" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_BRL }, ///< brazil real
56 { 31, "", 2011, "", NBSP "EEK", 1, STR_GAME_OPTIONS_CURRENCY_EEK }, ///< estonian krooni
57 { 4, "", 2014, "", NBSP "Lt", 1, STR_GAME_OPTIONS_CURRENCY_LTL }, ///< lithuanian litas
58 { 1850, "", CF_NOEURO, "\xE2\x82\xA9", "", 0, STR_GAME_OPTIONS_CURRENCY_KRW }, ///< south korean won
59 { 13, "", CF_NOEURO, "R" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_ZAR }, ///< south african rand
60 { 1, "", CF_NOEURO, "", "", 2, STR_GAME_OPTIONS_CURRENCY_CUSTOM }, ///< custom currency (add further languages below)
61 { 3, "", CF_NOEURO, "", NBSP "GEL", 1, STR_GAME_OPTIONS_CURRENCY_GEL }, ///< Georgian Lari
62 { 4901, "", CF_NOEURO, "", NBSP "Rls", 1, STR_GAME_OPTIONS_CURRENCY_IRR }, ///< Iranian Rial
65 /** Array of currencies used by the system */
66 CurrencySpec _currency_specs[CURRENCY_END];
68 /**
69 * This array represent the position of OpenTTD's currencies,
70 * compared to TTDPatch's ones.
71 * When a grf sends currencies, they are based on the order defined by TTDPatch.
72 * So, we must reindex them to our own order.
74 const byte TTDPatch_To_OTTDIndex[] =
76 CURRENCY_GBP,
77 CURRENCY_USD,
78 CURRENCY_FRF,
79 CURRENCY_DEM,
80 CURRENCY_JPY,
81 CURRENCY_ESP,
82 CURRENCY_HUF,
83 CURRENCY_PLN,
84 CURRENCY_ATS,
85 CURRENCY_BEF,
86 CURRENCY_DKK,
87 CURRENCY_FIM,
88 CURRENCY_GRD,
89 CURRENCY_CHF,
90 CURRENCY_NLG,
91 CURRENCY_ITL,
92 CURRENCY_SEK,
93 CURRENCY_RUR,
94 CURRENCY_EUR,
97 /**
98 * Will return the ottd's index correspondence to
99 * the ttdpatch's id. If the id is bigger than the array,
100 * it is a grf written for ottd, thus returning the same id.
101 * Only called from newgrf.cpp
102 * @param grfcurr_id currency id coming from newgrf
103 * @return the corrected index
105 byte GetNewgrfCurrencyIdConverted(byte grfcurr_id)
107 return (grfcurr_id >= lengthof(TTDPatch_To_OTTDIndex)) ? grfcurr_id : TTDPatch_To_OTTDIndex[grfcurr_id];
111 * get a mask of the allowed currencies depending on the year
112 * @return mask of currencies
114 uint64 GetMaskOfAllowedCurrencies()
116 uint64 mask = 0LL;
117 uint i;
119 for (i = 0; i < CURRENCY_END; i++) {
120 Year to_euro = _currency_specs[i].to_euro;
122 if (to_euro != CF_NOEURO && to_euro != CF_ISEURO && _cur_year >= to_euro) continue;
123 if (to_euro == CF_ISEURO && _cur_year < 2000) continue;
124 SetBit(mask, i);
126 SetBit(mask, CURRENCY_CUSTOM); // always allow custom currency
127 return mask;
131 * Verify if the currency chosen by the user is about to be converted to Euro
133 void CheckSwitchToEuro()
135 if (_currency_specs[_settings_game.locale.currency].to_euro != CF_NOEURO &&
136 _currency_specs[_settings_game.locale.currency].to_euro != CF_ISEURO &&
137 _cur_year >= _currency_specs[_settings_game.locale.currency].to_euro) {
138 _settings_game.locale.currency = 2; // this is the index of euro above.
139 AddNewsItem(STR_NEWS_EURO_INTRODUCTION, NT_ECONOMY, NF_NORMAL);
144 * Will fill _currency_specs array with
145 * default values from origin_currency_specs
146 * Called only from newgrf.cpp and settings.cpp.
147 * @param preserve_custom will not reset custom currency
149 void ResetCurrencies(bool preserve_custom)
151 for (uint i = 0; i < CURRENCY_END; i++) {
152 if (preserve_custom && i == CURRENCY_CUSTOM) continue;
153 _currency_specs[i] = origin_currency_specs[i];
158 * Build a list of currency names StringIDs to use in a dropdown list
159 * @return Pointer to a (static) array of StringIDs
161 StringID *BuildCurrencyDropdown()
163 /* Allow room for all currencies, plus a terminator entry */
164 static StringID names[CURRENCY_END + 1];
165 uint i;
167 /* Add each name */
168 for (i = 0; i < CURRENCY_END; i++) {
169 names[i] = _currency_specs[i].name;
171 /* Terminate the list */
172 names[i] = INVALID_STRING_ID;
174 return names;